Files
runny 9058149576
Build & Publish (dsh-academy) / End-to-end inside dsh (latest, advisory) (push) Skipped
CLI / test (macos-latest / go 1.26) (push) Canceled after 0s
CLI / test (ubuntu-latest / go 1.26) (push) Canceled after 0s
CLI / test (windows-latest / go 1.26) (push) Canceled after 0s
Build and Push Docker Image / build-ui (push) Canceled after 0s
Build and Push Docker Image / build-docreader (push) Canceled after 0s
Build and Push Docker Image / build-sandbox (push) Canceled after 0s
Build and Push Docker Image / build-app (amd64, linux/amd64, ubuntu-latest) (push) Canceled after 0s
Build and Push Docker Image / build-app (arm64, linux/arm64, ubuntu-24.04-arm) (push) Canceled after 0s
Build and Push Docker Image / merge (push) Canceled after 0s
Frontend / Test, type-check, and build (push) Canceled after 0s
Frontend / Docker multi-stage image (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.10) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.11) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.12) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.13) (push) Canceled after 0s
Build & Publish (mcp-server) / Build sdist + wheel (push) Canceled after 0s
Build & Publish (mcp-server) / Publish to PyPI (push) Canceled after 0s
Anydoc / anydoc parser engine (push) Canceled after 0s
App / Format, vet, test, and build (push) Canceled after 0s
DocReader / Python tests and Go client tests (push) Canceled after 0s
Build & Publish (dsh-academy) / Typecheck, test, pack (push) Canceled after 0s
Build & Publish (dsh-academy) / End-to-end inside dsh (pinned) (push) Canceled after 0s
Build & Publish (dsh-academy) / Publish to npm (push) Canceled after 0s
Go Lint Cache / warm lint cache (.) (push) Canceled after 0s
Go Lint Cache / warm lint cache (cli) (push) Canceled after 0s
Go Lint Cache / warm lint cache (client) (push) Canceled after 0s
Helm / chart (push) Canceled after 0s
refactor: 项目更名 WeKnora → Academy
- 全量替换代码/配置/文档中的项目名(保留上游 GitHub 链接、weixin 文档域名、Docker Hub 镜像名)
- 重命名 32 个含项目名的文件/目录;Go module 路径改为 academy(含 client/cli 子模块)
- docreader proto 描述符采用等长替换,保持序列化字节布局有效
- Dockerfile.app 构建网络修复: browserskill 离线产物通道、PyPI/apt 国内镜像
- 运行验证: go build ✓ docker compose up ✓ 健康检查 ✓
2026-11-13 20:52:51 +08:00

143 lines
4.4 KiB
Go

package allocation
import (
"errors"
"strings"
"unicode"
"academy/internal/sandbox/workspace"
)
// OwnerKind classifies who sessions.user_id names.
type OwnerKind string
// Kinds derived from owner_auth_source and the shape of user_id.
const (
KindUnknown OwnerKind = ""
KindWebUser OwnerKind = "web_user"
KindAPIExternalUser OwnerKind = "api_external_user"
KindAPITenantKey OwnerKind = "api_tenant_key"
KindAPIPlatformKey OwnerKind = "api_platform_key"
KindMCPEndpoint OwnerKind = "mcp_endpoint"
KindEmbed OwnerKind = "embed"
KindIM OwnerKind = "im"
)
// AuthSource is how the server authenticated the owner when the session was
// created or its owner was first bound. It is recorded server-side only.
type AuthSource string
// Sources stamped by the server. SourceUnknown is an empty or legacy row.
const (
SourceUnknown AuthSource = "unknown"
SourceWeb AuthSource = "web"
SourceAPIDirectHeader AuthSource = "api_direct_header"
SourceAPISignedToken AuthSource = "api_signed_token"
SourceAPITenantKey AuthSource = "api_tenant_key"
SourceAPIPlatformKey AuthSource = "api_platform_key"
SourceMCPEndpoint AuthSource = "mcp_endpoint"
SourceEmbed AuthSource = "embed"
SourceIM AuthSource = "im"
SourceInternal AuthSource = "internal"
)
// MaxOwnerIDLength matches sessions.user_id.
const MaxOwnerIDLength = 512
// Owner is the persisted owner of a session as the allocator sees it.
type Owner struct {
TenantID uint64
ID string
Kind OwnerKind
Source AuthSource
}
// ValidateOwnerID rejects ids that cannot be part of a machine identity.
func ValidateOwnerID(id string) error {
if strings.TrimSpace(id) == "" {
return errors.New("allocation: owner id is empty")
}
if len(id) > MaxOwnerIDLength {
return errors.New("allocation: owner id is too long")
}
if strings.ContainsAny(id, "{}") {
return errors.New("allocation: owner id must not contain braces")
}
for _, r := range id {
if unicode.IsControl(r) {
return errors.New("allocation: owner id must not contain control characters")
}
}
return nil
}
// DecideInput is everything Decide needs; callers load it, Decide is pure.
type DecideInput struct {
ConfigMode Mode
Purpose Purpose
Owner Owner
SessionID string
UserModeAvailable bool
}
// Decision is the mode a session gets and why it differs from the config.
type Decision struct {
Mode Mode
Reason FallbackReason
}
// Decide applies the user-mode admission whitelist. Only a server-verified web
// account or API external user may share a machine; every other owner keeps a
// machine per session. Checks run in a fixed order so the recorded reason is
// deterministic: maintenance, deployment gate, invalid owner, shared entry or
// channel, unverified identity, session id shape.
func Decide(in DecideInput) Decision {
session := func(reason FallbackReason) Decision { return Decision{Mode: ModeSession, Reason: reason} }
if in.Purpose == PurposeMaintenance {
return session(ReasonMaintenanceDedicated)
}
if in.ConfigMode != ModeUser {
return session(ReasonNone)
}
if !in.UserModeAvailable {
return session(ReasonUserModeUnavailable)
}
if strings.TrimSpace(in.Owner.ID) == "" {
return session(ReasonOwnerIdentityUnverified)
}
if ValidateOwnerID(in.Owner.ID) != nil {
return session(ReasonOwnerIdentityInvalid)
}
if EligibleForUser(in.Owner.Kind, in.Owner.Source) {
if !workspace.IsCanonicalUUID(in.SessionID) {
return session(ReasonSessionIDNotUUID)
}
return Decision{Mode: ModeUser}
}
switch in.Owner.Kind {
case KindAPITenantKey, KindAPIPlatformKey:
return session(ReasonSharedAPIKey)
case KindMCPEndpoint:
return session(ReasonSharedMCPEndpoint)
case KindEmbed, KindIM:
return session(ReasonChannelIdentity)
default:
return session(ReasonOwnerIdentityUnverified)
}
}
// EligibleForUser is the whitelist itself: a server-verified web account, or
// an API external user whose subject came from the direct header or a signed
// token. Decide applies it at first use; the manager re-applies it to stored
// assignments so a row whose identity is no longer acceptable stops running.
func EligibleForUser(kind OwnerKind, source AuthSource) bool {
switch kind {
case KindWebUser:
return source == SourceWeb
case KindAPIExternalUser:
return source == SourceAPIDirectHeader || source == SourceAPISignedToken
default:
return false
}
}