Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.
Go agentagenticaichatbotdsh-pluginembeddingsevaluationgenerative-aigolangknowledge-basellmmulti-tenantollamaopenaiquestion-answeringragrerankingsemantic-searchvector-searchwiki
feat(sandbox): 按分配记录定位共享沙箱(门控仍关闭) (#4509)
nullptr-errorcc29cc8d774d
diff --git a/internal/agent/prompts.go b/internal/agent/prompts.goindex 3f08b7164..e04d0a243 100644--- a/internal/agent/prompts.go+++ b/internal/agent/prompts.go@@ -391,6 +391,20 @@ func formatToolGuidanceForMode(names []string, skillInstallMode bool, layout sanb.WriteString(" on every call unless work_dir names a subdirectory. " +"Files persist on the user's machine.\n")}+ } else if layout.IsSharedRemote() {+ root := sandbox.PromptSafePath(layout.Root)+ input := sandbox.PromptSafePath(layout.InputDir)+ output := sandbox.PromptSafePath(layout.OutputDir)+ if root != "" && input != "" && output != "" {+ b.WriteString("Session workspace: " + root + ". Preserve uploaded originals in " + input + ". ")+ b.WriteString(output + " is the only directory collected for download, " ++ "so it takes finished deliverables only; " ++ "keep drafts and intermediate files in another directory under " ++ root + ". Commands start from " + root + " unless work_dir names a subdirectory. " ++ "Your other conversations share this machine under /workspace/sessions; " ++ "do not read, modify or delete them. Installed packages are shared with those conversations.\n")+ b.WriteString(sandboxArtifactReferenceGuidance())+ }} else {b.WriteString("Session workspace: /workspace. Preserve uploaded originals in /workspace/input. ")b.WriteString(skills.ArtifactOutputDir())diff --git a/internal/agent/prompts_shell_test.go b/internal/agent/prompts_shell_test.goindex 0144fd602..a649ba57a 100644--- a/internal/agent/prompts_shell_test.go+++ b/internal/agent/prompts_shell_test.go@@ -78,3 +78,14 @@ func TestArtifactGuidanceUsesConfiguredOutputDirectory(t *testing.T) {require.Contains(t, guidance, "/workspace/deliverables is the only directory collected for download")require.NotContains(t, guidance, "/workspace/output is the only directory collected")}++func TestSharedLayoutPromptNamesTheSessionDirectory(t *testing.T) {+ l, err := sandbox.UserWorkspaceLayout("0f8fad5b-d9cb-469f-a165-70867728950e")+ require.NoError(t, err)+ text := formatToolGuidanceForMode([]string{"shell_exec"}, false, l)+ require.Contains(t, text, "Session workspace: "+l.Root)+ require.Contains(t, text, l.InputDir)+ require.Contains(t, text, l.OutputDir)+ require.Contains(t, text, "share this machine")+ require.NotContains(t, text, "Session workspace: /workspace.")+}diff --git a/internal/agent/skills/shell_environment.go b/internal/agent/skills/shell_environment.goindex 896443762..b906617c1 100644--- a/internal/agent/skills/shell_environment.go+++ b/internal/agent/skills/shell_environment.go@@ -34,11 +34,16 @@ func (m *Manager) PrepareShellEnvironment(ctx context.Context, sessionID, skillNapplySkillNodePath(runtimeEnv, dir)runtimeEnv[skillDirEnvVar] = diroutputDir, inputDir := ArtifactOutputDir(), sandbox.SessionInputRoot- if root, ok := m.hostWorkspaceRoot(ctx, sessionID); ok {- // Host has no collected output tree or attachment directory yet: point- // deliverables at the workspace and leave the input variable unset- // rather than naming a /workspace path that does not exist.- outputDir, inputDir = root, ""+ if layout, ok := m.sessionLayout(ctx, sessionID); ok {+ switch {+ case layout.IsHost() && layout.HasRoot():+ // Host has no collected output tree or attachment directory yet:+ // point deliverables at the workspace and leave the input+ // variable unset rather than naming a /workspace path.+ outputDir, inputDir = layout.Root, ""+ case layout.IsSharedRemote():+ outputDir, inputDir = layout.OutputDir, layout.InputDir+ }}runtimeEnv[artifactOutputEnvVar] = outputDirruntimeEnv[artifactHistoryEnvVar] = outputDir@@ -53,14 +58,14 @@ func (m *Manager) PrepareShellEnvironment(ctx context.Context, sessionID, skillNreturn wrapped, runtimeEnv, nil}-func (m *Manager) hostWorkspaceRoot(ctx context.Context, sessionID string) (string, bool) {+func (m *Manager) sessionLayout(ctx context.Context, sessionID string) (sandbox.WorkspaceLayout, bool) {provider, ok := m.sandboxMgr.(sandbox.SessionWorkspaceLayoutProvider)if !ok || provider == nil {- return "", false+ return sandbox.WorkspaceLayout{}, false}layout, err := provider.SessionWorkspaceLayout(ctx, sessionID)- if err != nil || !layout.IsHost() || !layout.HasRoot() {- return "", false+ if err != nil {+ return sandbox.WorkspaceLayout{}, false}- return layout.Root, true+ return layout.Normalized(), true}diff --git a/internal/agent/skills/shell_environment_test.go b/internal/agent/skills/shell_environment_test.goindex 4b4bcfd29..6c8a0fa18 100644--- a/internal/agent/skills/shell_environment_test.go+++ b/internal/agent/skills/shell_environment_test.go@@ -64,3 +64,37 @@ func TestPrepareShellEnvironmentOnHostUsesLocalSkillsAndWorkspace(t *testing.T)_, hasInput := env["WEKNORA_SESSION_INPUT_DIR"]require.False(t, hasInput)}++// layoutEnvManager is a sandbox that only advertises a workspace layout.+// The embedded Manager stays nil: PrepareShellEnvironment asks for the+// layout and does not execute a command.+type layoutEnvManager struct {+ sandbox.Manager+ layout sandbox.WorkspaceLayout+}++func (m layoutEnvManager) SessionWorkspaceLayout(context.Context, string) (sandbox.WorkspaceLayout, error) {+ return m.layout, nil+}++func newShellEnvManager(t *testing.T, layout sandbox.WorkspaceLayout) *Manager {+ t.Helper()+ m := NewManager(&ManagerConfig{Enabled: true, AllowedSkills: []string{"pdf"}}, layoutEnvManager{layout: layout})+ m.WithTenantSource(NewTenantSkillSource([]*types.TenantSkillEntity{+ {Name: "pdf", Status: types.SkillStatusReady, Enabled: true},+ }, nil))+ require.NoError(t, m.Initialize(context.Background()))+ return m+}++func TestPrepareShellEnvironmentUsesSharedSessionPaths(t *testing.T) {+ l, err := sandbox.UserWorkspaceLayout("0f8fad5b-d9cb-469f-a165-70867728950e")+ require.NoError(t, err)+ m := newShellEnvManager(t, l)+ _, env, err := m.PrepareShellEnvironment(+ context.Background(), "0f8fad5b-d9cb-469f-a165-70867728950e", "pdf", "true", nil)+ require.NoError(t, err)+ require.Equal(t, l.OutputDir, env[artifactOutputEnvVar])+ require.Equal(t, l.OutputDir, env[artifactHistoryEnvVar])+ require.Equal(t, l.InputDir, env[sessionInputEnvVar])+}diff --git a/internal/agent/skills/shell_staging.go b/internal/agent/skills/shell_staging.goindex 554c8f1ba..e0f7bab8b 100644--- a/internal/agent/skills/shell_staging.go+++ b/internal/agent/skills/shell_staging.go@@ -97,7 +97,11 @@ func (m *Manager) stageShellSkill(ctx context.Context, sessionID, name string) (if len(resources) == 0 {return "", fmt.Errorf("skill %q has no resources to stage", name)}- dir := path.Join(sandbox.SessionWorkspaceRoot, ".skills", name, fmt.Sprintf("%x", digest.Sum(nil)[:12]))+ root := sandbox.SessionWorkspaceRoot+ if layout, ok := m.sessionLayout(ctx, sessionID); ok && layout.IsSharedRemote() {+ root = layout.Root+ }+ dir := path.Join(root, ".skills", name, fmt.Sprintf("%x", digest.Sum(nil)[:12]))payload := make([]sandbox.SessionWorkspaceFile, 0, len(resources))for _, file := range resources {payload = append(payload, sandbox.SessionWorkspaceFile{diff --git a/internal/agent/tools/shell_exec.go b/internal/agent/tools/shell_exec.goindex 7e29a9b37..3f81b4d60 100644--- a/internal/agent/tools/shell_exec.go+++ b/internal/agent/tools/shell_exec.go@@ -161,9 +161,57 @@ func shellExecDescription(l sandbox.WorkspaceLayout) string {root := layoutRootOrGeneric(l)return fmt.Sprintf(hostShellExecDescription, root, root, root)}+ if l.IsSharedRemote() {+ return sharedShellExecDescription(l)+ }return rewriteRemoteWorkspaceCopy(legacyShellExecDescription, l)}+// sharedShellExecDescription is the shell_exec copy for a machine several+// sessions share. The session directory is private; /tmp and the skills+// image are not, and the machine outlives the session.+func sharedShellExecDescription(l sandbox.WorkspaceLayout) string {+ root := layoutRootOrGeneric(l)+ input := sandbox.PromptSafePath(l.InputDir)+ if input == "" {+ input = "the attachment directory"+ }+ output := sandbox.PromptSafePath(l.OutputDir)+ if output == "" {+ output = "the output directory"+ }+ return fmt.Sprintf(sharedShellExecDescriptionTemplate, root, root, input, output, root)+}++const sharedShellExecDescriptionTemplate = `Execute a command in this session's directory on a shared machine as root.+This machine is shared with the owner's other sessions; this session's files are only under %s.+- CWD defaults to %s on every call; cd does not persist.+ work_dir must be that directory, /tmp, or the skills root (` + sandbox.SkillsImageRoot + `); other paths are rejected.+- Use ls/find to discover files, grep/awk to search, and cat/head/tail/sed to inspect text.+ Read known paths directly; no mandatory discovery call.+- Use write_sandbox_file for scripts or large text; edit_sandbox_file for precise changes.+ Commands are limited to 8192 bytes. Execution is synchronous (no nohup or trailing &).+- skill_name selects a listed skill for this call. Installed skills use their Python virtualenv and Node modules;+ host resources are staged automatically and use the system runtime until a local .venv is created.+ Scoped credentials apply to both. Example: skill_name="pdf", command="python3 report.py".+ Run bundled scripts via "$WEKNORA_SKILL_DIR/scripts/...". Omit skill_name for system commands.+- %s contains user attachments: preserve originals. %s is the only directory collected+ for download, so it takes finished deliverables only; keep scratch and intermediate files elsewhere under %s.+ apt-get is available when the sandbox network policy allows it; permanent dependencies belong in the skill installer.+- Install extras with skill_name set and the default work_dir:+ ` + "`" + skillPythonPackageInstallCommand + "`" + ` (no pip needed), or+ ` + "`" + skillNodePackageInstallCommand + "`" + `.+ If .venv is absent, create it with python3 -m venv --without-pip "${WEKNORA_SKILL_DIR:?}/.venv".+ Without uv, run the venv Python with -m ensurepip --upgrade before -m pip install.+ Deleting this session removes its directory, not the machine.+- Non-zero exit_code is a command result: inspect stderr before deciding whether a corrected call is useful.+ Transport failures/timeouts are tool failures. Changing tools does not change permissions;+ do not repeat a denied operation through another tool.+- stdout/stderr have independent byte limits, preserving head and tail when truncated.+ Full output is not automatically saved; redirect verbose commands to a workspace log when it must be retained.+ Binary bytes are suppressed.+- Use  with exact links from Output files, never stdout or temporary paths.`+const hostShellExecDescription = "Execute a command in %s. The process is OS-sandboxed on this machine.\n" +"- CWD defaults to %s on every call; cd does not persist. " +"work_dir must be that folder or a subdirectory.\n" +@@ -809,9 +857,10 @@ func (t *ShellExecTool) effectiveDefaultWorkDir() string {//// A remote sandbox is a disposable container the session owns outright, so any// directory in it stays allowed; clamping those to /workspace would break-// skills that legitimately build in /tmp or /opt. A host workspace is the-// user's own disk, so there work_dir is clamped to the layout's writable-// roots. Install mode keeps the skill-image roots and ignores session layouts.+// skills that legitimately build in /tmp or /opt. A shared machine clamps+// work_dir to this session's roots. A host workspace is the user's own disk,+// so there work_dir is clamped to the layout's writable roots. Install mode+// keeps the skill-image roots and ignores session layouts.func (t *ShellExecTool) workDirRootsFor(layout sandbox.WorkspaceLayout) []string {if t.isInstallMode() {if len(t.workDirRoots) == 0 {@@ -819,6 +868,10 @@ func (t *ShellExecTool) workDirRootsFor(layout sandbox.WorkspaceLayout) []string}return t.workDirRoots}+ if layout.IsSharedRemote() {+ // Several sessions share this machine; work_dir stays in this one.+ return layout.WorkDirRoots+ }if !layout.IsHost() {return []string{"/"}}diff --git a/internal/agent/tools/workspace_layout.go b/internal/agent/tools/workspace_layout.goindex 1f6f6cbad..fb0e4f4fa 100644--- a/internal/agent/tools/workspace_layout.go+++ b/internal/agent/tools/workspace_layout.go@@ -151,14 +151,14 @@ func inspectableRootIn(l sandbox.WorkspaceLayout, clean string) (string, bool) {return "", false}-// inspectRoot reports the metadata root for a read/list. Remote sandboxes-// label misses as "/" and still inspect (tmp, installed skills). Host-// layouts refuse anything outside ReadRoots.+// inspectRoot reports the metadata root for a read/list. A session-owned+// remote sandbox labels misses as "/" and still inspects (tmp, installed+// skills). Host layouts and shared remote machines allow ReadRoots only.func inspectRoot(l sandbox.WorkspaceLayout, clean string) (string, bool) {if root, ok := inspectableRootIn(l, clean); ok {return root, true}- if l.IsHost() {+ if l.IsHost() || l.IsSharedRemote() {return "", false}return "/", true@@ -267,13 +267,23 @@ func layoutDefaultListDir(l sandbox.WorkspaceLayout) string {// rewriteRemoteWorkspaceCopy replaces the remote /workspace constants in// tool copy with the session layout's model-safe wording. Longer paths// are substituted first so /workspace/input is not split into Hint+"/input".+// Placeholders keep a session directory that still starts with /workspace+// from being rewritten again when the root itself is replaced.func rewriteRemoteWorkspaceCopy(text string, l sandbox.WorkspaceLayout) string {hint := layoutHintOrRemote(l)input := modelSafeLayoutPath(l.InputDir, hint)output := modelSafeLayoutPath(l.OutputDir, hint)- text = strings.ReplaceAll(text, sandbox.SessionInputRoot, input)- text = strings.ReplaceAll(text, sandbox.SessionOutputRoot, output)- return strings.ReplaceAll(text, sandbox.SessionWorkspaceRoot, hint)+ const (+ inputSlot = "\x00weknora-input\x00"+ outputSlot = "\x00weknora-output\x00"+ rootSlot = "\x00weknora-root\x00"+ )+ text = strings.ReplaceAll(text, sandbox.SessionInputRoot, inputSlot)+ text = strings.ReplaceAll(text, sandbox.SessionOutputRoot, outputSlot)+ text = strings.ReplaceAll(text, sandbox.SessionWorkspaceRoot, rootSlot)+ text = strings.ReplaceAll(text, inputSlot, input)+ text = strings.ReplaceAll(text, outputSlot, output)+ return strings.ReplaceAll(text, rootSlot, hint)}func schemaForLayout(schema json.RawMessage, l sandbox.WorkspaceLayout) json.RawMessage {diff --git a/internal/agent/tools/workspace_layout_test.go b/internal/agent/tools/workspace_layout_test.goindex 2528164fa..31074f464 100644--- a/internal/agent/tools/workspace_layout_test.go+++ b/internal/agent/tools/workspace_layout_test.go@@ -558,3 +558,46 @@ func TestListSandboxFilesScopeErrorNamesResolvedDefaultDir(t *testing.T) {require.Contains(t, result.Error, "/Users/dev/My Project")require.NotContains(t, result.Error, `path ""`)}++func TestSharedLayoutClampsInspectAndWorkDir(t *testing.T) {+ l, err := sandbox.UserWorkspaceLayout("0f8fad5b-d9cb-469f-a165-70867728950e")+ require.NoError(t, err)+ _, ok := inspectRoot(l, "/workspace/sessions/7c9e6679-7425-40de-944b-e07fc1f90ae7/a.md")+ require.False(t, ok, "a shared machine must not read a sibling session through the tools")+ root, ok := inspectRoot(l, sandbox.SkillsImageRoot+"/pdf/SKILL.md")+ require.True(t, ok)+ require.Equal(t, sandbox.SkillsImageRoot, root)++ tool := &ShellExecTool{}+ require.Equal(t, l.WorkDirRoots, tool.workDirRootsFor(l))+ require.Equal(t, []string{"/"}, tool.workDirRootsFor(sandbox.RemoteWorkspaceLayout()))+}++func TestSharedLayoutToolCopyKeepsSessionPathsIntact(t *testing.T) {+ l, err := sandbox.UserWorkspaceLayout("0f8fad5b-d9cb-469f-a165-70867728950e")+ require.NoError(t, err)+ text := shellExecDescription(l)+ require.Contains(t, text, l.InputDir)+ require.Contains(t, text, l.OutputDir)+ require.Contains(t, text, l.Root)+ require.NotContains(t, text, l.Root+"/sessions/",+ "substituting /workspace must not rewrite a path that already names the session")+}++func TestSharedLayoutShellDescriptionStatesTheWorkDirRestriction(t *testing.T) {+ l, err := sandbox.UserWorkspaceLayout("0f8fad5b-d9cb-469f-a165-70867728950e")+ require.NoError(t, err)+ text := shellExecDescription(l)+ require.Contains(t, text, "shared with the owner's other sessions")+ require.Contains(t, text, "this session's files are only under "+l.Root)+ require.Contains(t, text, "work_dir must be that directory, /tmp, or the skills root ("+sandbox.SkillsImageRoot+")")+ require.Contains(t, text, "other paths are rejected")+ require.Contains(t, text, "Deleting this session removes its directory, not the machine")+ require.Contains(t, text, "skill_name")+ require.Contains(t, text, l.InputDir)+ require.Contains(t, text, l.OutputDir)+ require.Contains(t, text, "no nohup")+ require.NotContains(t, text, "belongs to this session alone")+ require.NotContains(t, text, "any directory")+ require.Contains(t, shellExecDescription(sandbox.RemoteWorkspaceLayout()), "belongs to this session alone")+}diff --git a/internal/application/service/pinned_session_sandbox.go b/internal/application/service/pinned_session_sandbox.goindex eb3a807f7..ae4f585e4 100644--- a/internal/application/service/pinned_session_sandbox.go+++ b/internal/application/service/pinned_session_sandbox.go@@ -193,6 +193,19 @@ func (a *PinnedSessionSandbox) DeleteForkSnapshot(return errors.New("sandbox: fork snapshot delete is not supported")}+// SessionWorkspaceLayout forwards the pinned manager's layout. manager does+// not return the lookup error (it degrades to nil), so a missing pin keeps+// the /workspace contract instead of failing the checkpoint closed.+func (a *PinnedSessionSandbox) SessionWorkspaceLayout(+ ctx context.Context, sessionID string,+) (sandbox.WorkspaceLayout, error) {+ mgr := a.manager(ctx, sessionID)+ if provider, ok := mgr.(sandbox.SessionWorkspaceLayoutProvider); ok && provider != nil {+ return provider.SessionWorkspaceLayout(ctx, sessionID)+ }+ return sandbox.RemoteWorkspaceLayout(), nil+}+// VersionsWorkspace reports whether this session's backend keeps a git history// of the sandbox workspace. Host sessions return false: the workspace is a// real directory, often shared across sessions, and must not be auto-committed@@ -219,6 +232,8 @@ var _ SessionRewindSandboxPort = (*PinnedSessionSandbox)(nil)var _ WorkspaceVersioning = (*PinnedSessionSandbox)(nil)+var _ sandbox.SessionWorkspaceLayoutProvider = (*PinnedSessionSandbox)(nil)+var _ SessionForkSandboxPort = (*sandbox.SessionBoundManager)(nil)var _ SessionRewindSandboxPort = (*sandbox.SessionBoundManager)(nil)diff --git a/internal/application/service/sandboxalloc/service.go b/internal/application/service/sandboxalloc/service.goindex 8966b984b..8e32999cc 100644--- a/internal/application/service/sandboxalloc/service.go+++ b/internal/application/service/sandboxalloc/service.go@@ -10,11 +10,13 @@ import ("strings""time"+ apperrors "github.com/Tencent/WeKnora/internal/errors""github.com/Tencent/WeKnora/internal/logger""github.com/Tencent/WeKnora/internal/sandbox""github.com/Tencent/WeKnora/internal/sandbox/allocation""github.com/Tencent/WeKnora/internal/types""github.com/Tencent/WeKnora/internal/types/interfaces"+ "gorm.io/gorm")// ConfigReader loads a sandbox config by its owning workspace.@@ -290,6 +292,79 @@ func (s *Service) Release(ctx context.Context, sessionID string) (interfaces.Retreturn out[0], nil}+// SessionAllocation implements sandbox.AllocationDirectory.+func (s *Service) SessionAllocation(ctx context.Context, sessionID string) (allocation.Assignment, bool, error) {+ return s.Lookup(ctx, sessionID)+}++// VerifySessionAllocation implements sandbox.AllocationDirectory (C4, C15,+// C17). The row must still be live and name the expected address. For a+// shared machine, the digest recomputed from the session's CURRENT owner must+// be the stored one — the digest binds the owner, so this is stricter than+// comparing a snapshot — and the session's current auth source must still+// qualify. A retired session's row is released (or a tombstone), so the+// release check covers retirement.+func (s *Service) VerifySessionAllocation(+ ctx context.Context, sessionTenantID uint64, sessionID string, expected allocation.Assignment,+) error {+ row, err := s.repo.GetAssignment(ctx, sessionID)+ if err != nil {+ return fmt.Errorf("sandboxalloc: read assignment %s: %w", sessionID, err)+ }+ if row == nil || row.ReleasedAt != nil {+ return sandbox.ErrSandboxSessionDeleted+ }+ if row.ConfigTenantID != expected.ConfigTenantID ||+ sandbox.NormalizeConfigID(row.ConfigID) != sandbox.NormalizeConfigID(expected.ConfigID) ||+ row.MachineDigest != s.digestOf(expected) {+ return fmt.Errorf("%w: session %s was re-addressed", sandbox.ErrUserAllocationRejected, sessionID)+ }+ sess, err := s.sessions.GetByID(ctx, sessionTenantID, sessionID)+ if errors.Is(err, gorm.ErrRecordNotFound) || errors.Is(err, apperrors.ErrSessionNotFound) ||+ (err == nil && sess == nil) {+ return sandbox.ErrSandboxSessionDeleted+ }+ if err != nil {+ return fmt.Errorf("sandboxalloc: read session %s: %w", sessionID, err)+ }+ if row.MachineDigest == "" {+ return nil+ }+ current := allocation.UserMachine{+ OwnerTenantID: sess.TenantID, OwnerID: sess.UserID,+ ConfigTenantID: row.ConfigTenantID, ConfigID: row.ConfigID,+ }+ if s.Digest(current) != row.MachineDigest {+ return fmt.Errorf("%w: session %s no longer belongs to the machine's owner",+ sandbox.ErrUserAllocationRejected, sessionID)+ }+ owner := ownerFromSession(sess)+ if !allocation.EligibleForUser(owner.Kind, owner.Source) {+ return sandbox.ErrUserAllocationRejected+ }+ return nil+}++// UserMachinesByConfig implements sandbox.AllocationDirectory.+func (s *Service) UserMachinesByConfig(+ ctx context.Context, configTenantID uint64, configID string,+) ([]allocation.UserMachine, error) {+ rows, err := s.repo.ListUserMachinesByConfig(ctx, configTenantID, configID)+ if err != nil {+ return nil, err+ }+ out := make([]allocation.UserMachine, 0, len(rows))+ for _, r := range rows {+ out = append(out, allocation.UserMachine{+ OwnerTenantID: r.OwnerTenantID, OwnerID: r.OwnerID,+ ConfigTenantID: r.ConfigTenantID, ConfigID: r.ConfigID,+ })+ }+ return out, nil+}++var _ sandbox.AllocationDirectory = (*Service)(nil)+// ForkAssignment is the child's assignment: same mode, machine and config,// fresh session id. The caller persists it with the forked session.func (s *Service) ForkAssignment(source allocation.Assignment, targetSessionID string) allocation.Assignment {diff --git a/internal/application/service/sandboxalloc/service_test.go b/internal/application/service/sandboxalloc/service_test.goindex 3d4e0de49..3a07fd74f 100644--- a/internal/application/service/sandboxalloc/service_test.go+++ b/internal/application/service/sandboxalloc/service_test.go@@ -299,3 +299,37 @@ func TestReleaseWithoutASessionIDIsAnError(t *testing.T) {_, err := e.svc.Release(context.Background(), " ")require.Error(t, err)}++func (e *testEnv) updateSession(t *testing.T, id string, cols map[string]any) {+ t.Helper()+ require.NoError(t, e.db.Session(&gorm.Session{SkipHooks: true}).+ Model(&types.Session{}).Where("id = ?", id).Updates(cols).Error)+}++var _ sandbox.AllocationDirectory = (*Service)(nil)++func TestVerifySessionAllocationChecksTheCurrentOwnerAndRetirement(t *testing.T) {+ enableUserMode(t)+ id := uuid.NewString()+ e := newEnv(t, fakeConfigs{"cfg-a": "user"}, webSession(id))+ a := e.place(t, id, "cfg-a")+ ctx := context.Background()+ require.NoError(t, e.svc.VerifySessionAllocation(ctx, 7, id, a))++ stale := a+ stale.ConfigID = "cfg-b"+ require.ErrorIs(t, e.svc.VerifySessionAllocation(ctx, 7, id, stale), sandbox.ErrUserAllocationRejected,+ "a cached address that no longer matches the row is refused")++ e.updateSession(t, id, map[string]any{"user_id": "u-2"})+ require.ErrorIs(t, e.svc.VerifySessionAllocation(ctx, 7, id, a), sandbox.ErrUserAllocationRejected,+ "the session changed hands; the old owner's machine is off limits (C15)")+ e.updateSession(t, id, map[string]any{"user_id": "u-1", "owner_auth_source": "api_key"})+ require.ErrorIs(t, e.svc.VerifySessionAllocation(ctx, 7, id, a), sandbox.ErrUserAllocationRejected)+ e.updateSession(t, id, map[string]any{"owner_auth_source": "web"})+ require.NoError(t, e.svc.VerifySessionAllocation(ctx, 7, id, a))++ _, err := e.svc.Retire(ctx, []string{id})+ require.NoError(t, err)+ require.ErrorIs(t, e.svc.VerifySessionAllocation(ctx, 7, id, a), sandbox.ErrSandboxSessionDeleted)+}diff --git a/internal/application/service/session.go b/internal/application/service/session.goindex 2ccc0ff07..759d2a448 100644--- a/internal/application/service/session.go+++ b/internal/application/service/session.go@@ -1064,50 +1064,55 @@ func (s *sessionService) GenerateTitleAsync(// cannot silently proceed while this store is unreachable either.func (s *sessionService) holdSandboxTurn(ctx context.Context, sessionID, configID string,-) (func(), error) {+) (context.Context, func(), error) {noop := func() {}if strings.TrimSpace(sessionID) == "" {- return noop, nil+ return ctx, noop, nil}releaseGate, err := s.busyGate.HoldSend(sessionID)if err != nil {- return noop, err+ return ctx, noop, err}- begin := func(mgr sandbox.Manager) (sandbox.SessionTurnHolder, error) {+ begin := func(mgr sandbox.Manager) (context.Context, func(), error) {if mgr == nil {- return nil, nil+ return ctx, nil, nil}holder, ok := mgr.(sandbox.SessionTurnHolder)if !ok {- return nil, nil+ return ctx, nil, nil}- if err := holder.BeginSessionTurn(ctx, sessionID); err != nil {+ turnCtx, end, err := holder.BeginSessionTurn(ctx, sessionID)+ if err != nil {logger.Warnf(ctx, "[sandbox] begin turn for session %s failed: %v", sessionID, err)if stderrors.Is(err, sandbox.ErrSessionRewindLocked) {- return nil, err+ return ctx, nil, err}- return nil, nil+ return ctx, nil, nil+ }+ if turnCtx == nil {+ turnCtx = ctx+ }+ if end == nil {+ end = noop}- return holder, nil+ return turnCtx, end, nil}- holder, err := begin(s.sandboxMgr)+ turnCtx, end, err := begin(s.sandboxMgr)if err != nil {releaseGate()- return noop, err+ return ctx, noop, err}- if holder != nil {- return func() {- if err := holder.EndSessionTurn(ctx, sessionID); err != nil {- logger.Warnf(ctx, "[sandbox] end turn for session %s failed: %v", sessionID, err)- }+ if end != nil {+ return turnCtx, func() {+ end()releaseGate()}, nil}tenantID, _ := types.TenantIDFromContext(ctx)if s.sandboxResolver == nil || tenantID == 0 {- return releaseGate, nil+ return ctx, releaseGate, nil}mgr, _, err := resolveSandboxForExecution(ctx, s.sandboxResolver, s.sandboxMgr, s.sandboxPinner,@@ -1117,20 +1122,18 @@ func (s *sessionService) holdSandboxTurn(if err != nil {logger.Warnf(ctx, "[sandbox] resolve config %s to begin turn of session %s failed: %v",configID, sessionID, err)- return releaseGate, nil+ return ctx, releaseGate, nil}- holder, err = begin(mgr)+ turnCtx, end, err = begin(mgr)if err != nil {releaseGate()- return noop, err+ return ctx, noop, err}- if holder == nil {- return releaseGate, nil+ if end == nil {+ return ctx, releaseGate, nil}- return func() {- if err := holder.EndSessionTurn(ctx, sessionID); err != nil {- logger.Warnf(ctx, "[sandbox] end turn for session %s failed: %v", sessionID, err)- }+ return turnCtx, func() {+ end()releaseGate()}, nil}@@ -1172,6 +1175,6 @@ func (s *sessionService) RejectSendIfRewinding(ctx context.Context, sessionID st// lease before persisting messages, matching rewind's busy check.func (s *sessionService) HoldSandboxTurn(ctx context.Context, sessionID, configID string,-) (func(), error) {+) (context.Context, func(), error) {return s.holdSandboxTurn(ctx, sessionID, configID)}diff --git a/internal/application/service/session_agent_qa.go b/internal/application/service/session_agent_qa.goindex 92448798d..2e61f8795 100644--- a/internal/application/service/session_agent_qa.go+++ b/internal/application/service/session_agent_qa.go@@ -237,11 +237,12 @@ func (s *sessionService) AgentQA(// where the new image is picked up. HTTP send already holds the lease it// took before persisting the turn, so only direct callers take one here.if !req.TurnLeaseHeld {- releaseTurn, err := s.holdSandboxTurn(ctx, sessionID, agentConfig.SandboxConfigID)+ turnCtx, releaseTurn, err := s.holdSandboxTurn(ctx, sessionID, agentConfig.SandboxConfigID)if err != nil {return err}defer releaseTurn()+ ctx = turnCtx}// Reconcile all durable session attachments into the session's remotediff --git a/internal/application/service/session_knowledge_qa.go b/internal/application/service/session_knowledge_qa.goindex d2cc7a4c1..1dacea1f7 100644--- a/internal/application/service/session_knowledge_qa.go+++ b/internal/application/service/session_knowledge_qa.go@@ -46,11 +46,12 @@ func (s *sessionService) KnowledgeQA(if req.CustomAgent != nil {configID = req.CustomAgent.Config.SandboxConfigID}- releaseTurn, holdErr := s.holdSandboxTurn(ctx, req.Session.ID, configID)+ turnCtx, releaseTurn, holdErr := s.holdSandboxTurn(ctx, req.Session.ID, configID)if holdErr != nil {return holdErr}defer releaseTurn()+ ctx = turnCtx}// Span the request setup (KB / model resolution, search target building,diff --git a/internal/application/service/session_rewind_test.go b/internal/application/service/session_rewind_test.goindex 1bf8f3583..0c9608818 100644--- a/internal/application/service/session_rewind_test.go+++ b/internal/application/service/session_rewind_test.go@@ -762,7 +762,7 @@ func TestRewindBlocksLocalSendWithoutSandbox(t *testing.T) {svc.busyGate = gatesessionSvc := &sessionService{busyGate: gate}- release, err := sessionSvc.holdSandboxTurn(context.Background(), "src", "")+ _, release, err := sessionSvc.holdSandboxTurn(context.Background(), "src", "")require.NoError(t, err)got, err := svc.Rewind(context.Background(), 1, "u1", "src", "u-2")diff --git a/internal/application/service/session_sandbox_turn_test.go b/internal/application/service/session_sandbox_turn_test.goindex c8aca3116..0751c7994 100644--- a/internal/application/service/session_sandbox_turn_test.go+++ b/internal/application/service/session_sandbox_turn_test.go@@ -12,7 +12,7 @@ func TestHoldSandboxTurnOpensAndClosesTheLease(t *testing.T) {holder := &turnLeaseManager{}svc := &sessionService{sandboxMgr: holder}- release, err := svc.holdSandboxTurn(context.Background(), "session-a", "")+ _, release, err := svc.holdSandboxTurn(context.Background(), "session-a", "")require.NoError(t, err)require.Equal(t, 1, holder.begins)require.Zero(t, holder.ends)@@ -27,7 +27,7 @@ func TestHoldSandboxTurnDegradesWhenBeginFails(t *testing.T) {holder := &turnLeaseManager{beginErr: context.Canceled}svc := &sessionService{sandboxMgr: holder}- release, err := svc.holdSandboxTurn(context.Background(), "session-a", "")+ _, release, err := svc.holdSandboxTurn(context.Background(), "session-a", "")require.NoError(t, err)require.Equal(t, 1, holder.begins)release()@@ -38,7 +38,7 @@ func TestHoldSandboxTurnFailsWhenRewindLocked(t *testing.T) {holder := &turnLeaseManager{beginErr: sandbox.ErrSessionRewindLocked}svc := &sessionService{sandboxMgr: holder}- release, err := svc.holdSandboxTurn(context.Background(), "session-a", "")+ _, release, err := svc.holdSandboxTurn(context.Background(), "session-a", "")require.ErrorIs(t, err, sandbox.ErrSessionRewindLocked)release()require.Zero(t, holder.ends)@@ -48,7 +48,7 @@ func TestHoldSandboxTurnUsesBusyGateWithoutSandboxManager(t *testing.T) {gate := NewSessionBusyGate()svc := &sessionService{busyGate: gate}- release, err := svc.holdSandboxTurn(context.Background(), "session-a", "")+ _, release, err := svc.holdSandboxTurn(context.Background(), "session-a", "")require.NoError(t, err)_, err = gate.TryLockRewind("session-a")require.ErrorIs(t, err, ErrRewindSourceBusy)@@ -87,14 +87,12 @@ type turnLeaseManager struct {endErr error}-func (m *turnLeaseManager) BeginSessionTurn(context.Context, string) error {+func (m *turnLeaseManager) BeginSessionTurn(ctx context.Context, _ string) (context.Context, func(), error) {m.begins++- return m.beginErr-}--func (m *turnLeaseManager) EndSessionTurn(context.Context, string) error {- m.ends++- return m.endErr+ if m.beginErr != nil {+ return nil, nil, m.beginErr+ }+ return ctx, func() { m.ends++ }, nil}type rewindLockManager struct {diff --git a/internal/application/service/workspace_checkpointer.go b/internal/application/service/workspace_checkpointer.goindex a589f4cbb..6f0db9e48 100644--- a/internal/application/service/workspace_checkpointer.go+++ b/internal/application/service/workspace_checkpointer.go@@ -76,13 +76,19 @@ func NewWorkspaceCheckpointer(runner SandboxShellRunner) *WorkspaceCheckpointer// message with no checkpoint and making it unusable as a fork point. With// it, every turn has exactly one checkpoint and fork boundaries are always// resolvable.-func checkpointScript(workspace, gitDir, messageID string) string {+func checkpointScriptFor(l sandbox.WorkspaceLayout, messageID string) string {return fmt.Sprintf(`set -e%s%sgit_ws add -Agit_ws commit -q --allow-empty -m 'turn:%s'-git_ws rev-parse HEAD`, gitWorkspacePreamble(workspace, gitDir), gitWorkspaceEnsureRepo(), messageID)+git_ws rev-parse HEAD`, gitWorkspacePreambleFor(l), gitWorkspaceEnsureRepo(), messageID)+}++func checkpointScript(workspace, gitDir, messageID string) string {+ return checkpointScriptFor(sandbox.WorkspaceLayout{+ Origin: sandbox.WorkspaceOriginRemote, Root: workspace, GitDir: gitDir,+ }, messageID)}// Checkpoint commits /workspace and returns the resulting checkpoint, or nil@@ -106,19 +112,23 @@ func (c *WorkspaceCheckpointer) Checkpoint(return nil}+ layout, err := workspaceLayoutFor(ctx, c.runner, sessionID)+ if err != nil {+ logger.Warnf(ctx, "[WorkspaceCheckpointer] layout unavailable session=%s: %v", sessionID, err)+ return nil+ }+ command := checkpointScriptFor(layout, messageID)var result *sandbox.ExecuteResult- var err error- command := checkpointScript(sandbox.SessionWorkspaceRoot, sandbox.SessionGitDir, messageID)if runner, ok := c.runner.(sandbox.SessionInstallShellExecutor); ok {// Git operates on the existing workspace. Do not prepare artifact/input// directories or let a late checkpoint provision a replacement sandbox.result, err = runner.ExecShellCommandWithOptions(ctx, sessionID, command, sandbox.ShellExecOptions{- WorkDir: sandbox.SessionWorkspaceRoot, Timeout: workspaceCheckpointTimeout,+ WorkDir: layout.Root, Timeout: workspaceCheckpointTimeout,SkipWorkspacePrep: true, ExpectedSandboxID: sandboxID,})} else {result, err = c.runner.ExecShellCommand(ctx, sessionID, command,- sandbox.SessionWorkspaceRoot, workspaceCheckpointTimeout, nil)+ layout.Root, workspaceCheckpointTimeout, nil)}if err != nil {logger.Warnf(ctx, "[WorkspaceCheckpointer] exec failed session=%s message=%s: %v",diff --git a/internal/application/service/workspace_git_layout_test.go b/internal/application/service/workspace_git_layout_test.goindex e0a6a54e2..891170786 100644--- a/internal/application/service/workspace_git_layout_test.go+++ b/internal/application/service/workspace_git_layout_test.go@@ -7,6 +7,7 @@ import ("path/filepath""strings""testing"+ "time""github.com/Tencent/WeKnora/internal/sandbox""github.com/stretchr/testify/require"@@ -319,3 +320,64 @@ func TestAdoptionIsOnlyAttemptedWhileTheNewGitDirIsMissing(t *testing.T) {require.DirExists(t, filepath.Join(workspace, ".git"))require.Equal(t, agentHEAD, strings.TrimSpace(runGitAt(t, env, workspace, "rev-parse", "HEAD")))}++// layoutRunner records the checkpoint command. It does not embed+// hostShellRunner: that fake returns VersionsWorkspace=false and would skip+// the checkpoint before the layout is consulted.+type layoutRunner struct {+ layout sandbox.WorkspaceLayout+ cmds []string+ dirs []string+ exitCode int+}++func (r *layoutRunner) SessionWorkspaceLayout(context.Context, string) (sandbox.WorkspaceLayout, error) {+ return r.layout, nil+}++func (r *layoutRunner) ExecShellCommand(+ _ context.Context, _ string, command, workDir string, _ time.Duration, _ map[string]string,+) (*sandbox.ExecuteResult, error) {+ r.cmds = append(r.cmds, command)+ r.dirs = append(r.dirs, workDir)+ return &sandbox.ExecuteResult{ExitCode: r.exitCode, Stdout: strings.Repeat("a", 40) + "\n"}, nil+}++func (r *layoutRunner) VersionsWorkspace(context.Context, string) bool { return true }++func TestCheckpointUsesTheSessionLayout(t *testing.T) {+ l, err := sandbox.UserWorkspaceLayout("0f8fad5b-d9cb-469f-a165-70867728950e")+ require.NoError(t, err)+ runner := &layoutRunner{layout: l}+ cp := NewWorkspaceCheckpointer(runner).Checkpoint(+ context.Background(), l.Root[len("/workspace/sessions/"):], "sb-1", "m-1")+ require.NotNil(t, cp)+ require.Contains(t, runner.cmds[0], "WORK_TREE='"+l.Root+"'")+ require.Contains(t, runner.cmds[0], "GIT_DIR='"+l.GitDir+"'")+ require.NotContains(t, runner.cmds[0], "legacy_git_dir", "a shared machine never adopts a work-tree .git")+ require.Equal(t, l.Root, runner.dirs[0])+}++func TestCheckpointScriptForRemoteLayoutIsUnchanged(t *testing.T) {+ require.Equal(t,+ checkpointScript(sandbox.SessionWorkspaceRoot, sandbox.SessionGitDir, "m-1"),+ checkpointScriptFor(sandbox.RemoteWorkspaceLayout(), "m-1"))+}++func TestResetScriptChecksTheCommitFirst(t *testing.T) {+ script, err := workspaceResetScriptFor(sandbox.RemoteWorkspaceLayout(), strings.Repeat("b", 40))+ require.NoError(t, err)+ require.Contains(t, script, `cat-file -e "`+strings.Repeat("b", 40)+`^{commit}" 2>/dev/null || exit 3`)+ require.Less(t, strings.Index(script, "cat-file -e"), strings.Index(script, "reset --hard"))+}++func TestResetMapsMissingCommitToErrWorkspaceCommitMissing(t *testing.T) {+ l, err := sandbox.UserWorkspaceLayout("0f8fad5b-d9cb-469f-a165-70867728950e")+ require.NoError(t, err)+ runner := &layoutRunner{layout: l, exitCode: 3}+ err = resetWorkspaceToCommit(context.Background(), runner, "s", strings.Repeat("c", 40), "sb")+ require.ErrorIs(t, err, errWorkspaceCommitMissing)+ require.Contains(t, runner.cmds[0], "GIT_DIR='"+l.GitDir+"'")+ require.Contains(t, runner.cmds[0], "WORK_TREE='"+l.Root+"'")+ require.Equal(t, l.Root, runner.dirs[0])+}diff --git a/internal/application/service/workspace_reset.go b/internal/application/service/workspace_reset.goindex ca98e02af..5ec3a314c 100644--- a/internal/application/service/workspace_reset.go+++ b/internal/application/service/workspace_reset.go@@ -21,6 +21,13 @@ import (// so this is more generous than the per-turn checkpoint.const workspaceResetTimeout = 90 * time.Second+// errWorkspaceCommitMissing: the requested checkpoint is not in this+// session's repository (it was rebuilt, deleted, or belongs elsewhere).+// Detected before anything is modified.+var errWorkspaceCommitMissing = errors.New("workspace reset: checkpoint commit is not in the session repository")++const exitCommitMissing = 3+// workspaceResetScript renders the rollback of workspace to sha.//// reset --hard restores tracked files (including input/ and output/). Other@@ -37,6 +44,18 @@ func workspaceResetScript(workspace, gitDir, sha string) (string, error) {gitWorkspacePruneAndClean(), nil}+// workspaceResetScriptFor rolls l back to sha. The commit is checked first so+// a missing checkpoint fails before any file changes.+func workspaceResetScriptFor(l sandbox.WorkspaceLayout, sha string) (string, error) {+ if !gitSHAPattern.MatchString(sha) {+ return "", fmt.Errorf("workspace reset: invalid commit sha %q", truncateForLog(sha))+ }+ return "set -e\n" + gitWorkspacePreambleFor(l) ++ fmt.Sprintf("git_ws cat-file -e \"%s^{commit}\" 2>/dev/null || exit %d\n", sha, exitCommitMissing) ++ "git_ws reset --hard " + sha + "\n" ++ gitWorkspacePruneAndClean(), nil+}+// workspaceEmptyResetScript is the start-over path: kept history has no SHA,// so we commit an empty tree and run the same reset+prune+clean as a SHA rewind.func workspaceEmptyResetScript(workspace, gitDir string) string {@@ -46,6 +65,13 @@ git_ws reset --hard "$empty"` + gitWorkspacePruneAndClean()}+func workspaceEmptyResetScriptFor(l sandbox.WorkspaceLayout) string {+ return "set -e\n" + gitWorkspacePreambleFor(l) + gitWorkspaceEnsureRepo() + `+empty=$(git_ws commit-tree "$(git_ws mktree </dev/null)" -m 'rewind: empty')+git_ws reset --hard "$empty"+` + gitWorkspacePruneAndClean()+}+// gitWorkspaceEnsureRepo is the checkpointer's first-use init. Empty rewind// reuses it so a sandbox that never checkpointed can still reset to empty.func gitWorkspaceEnsureRepo() string {@@ -76,13 +102,45 @@ git_ws clean -fdx// cannot drift onto different git layouts. GIT_DIR is outside /workspace;// --work-tree still edits the session files.func gitWorkspacePreamble(workspace, gitDir string) string {+ return gitWorkspaceVars(workspace, gitDir) + gitWorkspaceAdoptLegacyRepo()+}++func gitWorkspaceVars(workspace, gitDir string) string {return fmt.Sprintf(`WORK_TREE=%[1]sGIT_DIR=%[2]sgit_ws() {git -c safe.directory='*' --git-dir="$GIT_DIR" --work-tree="$WORK_TREE" "$@"}mkdir -p "$WORK_TREE"-`, shellSingleQuote(workspace), shellSingleQuote(gitDir)) + gitWorkspaceAdoptLegacyRepo()+`, shellSingleQuote(workspace), shellSingleQuote(gitDir))+}++// gitWorkspacePreambleFor is gitWorkspacePreamble for a layout. Only a+// per-session machine adopts a legacy $WORK_TREE/.git; on a shared machine+// that directory is an agent's own repository and must be left alone.+func gitWorkspacePreambleFor(l sandbox.WorkspaceLayout) string {+ if l.IsSharedRemote() {+ return gitWorkspaceVars(l.Root, l.GitDir)+ }+ return gitWorkspacePreamble(l.Root, l.GitDir)+}++// workspaceLayoutFor asks the runner for the session's layout; runners that+// do not advertise one keep the /workspace contract.+func workspaceLayoutFor(ctx context.Context, runner any, sessionID string) (sandbox.WorkspaceLayout, error) {+ provider, ok := runner.(sandbox.SessionWorkspaceLayoutProvider)+ if !ok || provider == nil {+ return sandbox.RemoteWorkspaceLayout(), nil+ }+ l, err := provider.SessionWorkspaceLayout(ctx, sessionID)+ if err != nil {+ return sandbox.WorkspaceLayout{}, err+ }+ l = l.Normalized()+ if !l.HasRoot() || l.GitDir == "" {+ return sandbox.WorkspaceLayout{}, errors.New("workspace reset: session layout has no versioned workspace")+ }+ return l, nil}// gitWorkspaceAdoptLegacyRepo migrates a sandbox that still keeps its@@ -120,21 +178,21 @@ func gitResetFailure(sha, stderr string) error {}func execWorkspaceReset(- ctx context.Context, runner SandboxShellRunner, sessionID, script, expectedSandboxID string,+ ctx context.Context, runner SandboxShellRunner, sessionID, script, expectedSandboxID, workDir string,) (*sandbox.ExecuteResult, error) {if runner == nil {return nil, errors.New("workspace reset: no shell runner wired")}if withOpts, ok := runner.(sandbox.SessionInstallShellExecutor); ok {return withOpts.ExecShellCommandWithOptions(ctx, sessionID, script, sandbox.ShellExecOptions{- WorkDir: sandbox.SessionWorkspaceRoot,+ WorkDir: workDir,Timeout: workspaceResetTimeout,SkipWorkspacePrep: true,ExpectedSandboxID: expectedSandboxID,})}return runner.ExecShellCommand(- ctx, sessionID, script, sandbox.SessionWorkspaceRoot, workspaceResetTimeout, nil,+ ctx, sessionID, script, workDir, workspaceResetTimeout, nil,)}@@ -149,14 +207,21 @@ func resetWorkspaceToCommit(ctx context.Context, runner SandboxShellRunner, sessionID, sha, expectedSandboxID string,) error {sha = strings.TrimSpace(sha)- script, err := workspaceResetScript(sandbox.SessionWorkspaceRoot, sandbox.SessionGitDir, sha)+ layout, err := workspaceLayoutFor(ctx, runner, sessionID)+ if err != nil {+ return err+ }+ script, err := workspaceResetScriptFor(layout, sha)if err != nil {return err}- result, err := execWorkspaceReset(ctx, runner, sessionID, script, expectedSandboxID)+ result, err := execWorkspaceReset(ctx, runner, sessionID, script, expectedSandboxID, layout.Root)if err != nil {return fmt.Errorf("workspace reset: git reset exec: %w", err)}+ if result != nil && result.ExitCode == exitCommitMissing {+ return errWorkspaceCommitMissing+ }if result == nil || result.ExitCode != 0 {stderr := ""if result != nil {@@ -170,8 +235,12 @@ func resetWorkspaceToCommit(func resetWorkspaceToEmpty(ctx context.Context, runner SandboxShellRunner, sessionID, expectedSandboxID string,) error {- script := workspaceEmptyResetScript(sandbox.SessionWorkspaceRoot, sandbox.SessionGitDir)- result, err := execWorkspaceReset(ctx, runner, sessionID, script, expectedSandboxID)+ layout, err := workspaceLayoutFor(ctx, runner, sessionID)+ if err != nil {+ return err+ }+ script := workspaceEmptyResetScriptFor(layout)+ result, err := execWorkspaceReset(ctx, runner, sessionID, script, expectedSandboxID, layout.Root)if err != nil {return fmt.Errorf("workspace reset: empty workspace exec: %w", err)}diff --git a/internal/container/container.go b/internal/container/container.goindex 8121e8d9b..fbb4c5fb4 100644--- a/internal/container/container.go+++ b/internal/container/container.go@@ -235,6 +235,16 @@ func BuildContainer(container *dig.Container) *dig.Container {return b}))must(container.Provide(newTenantSandboxResolver))+ // The resolver admits user-mode sessions, so the allocation service has to+ // exist before the first Invoke that builds a TenantSandboxResolver+ // (the chat pipeline plugins construct SessionService, which does).+ must(container.Provide(func(+ repo interfaces.SandboxAllocationRepository,+ configs repository.TenantSandboxConfigRepository,+ sessions interfaces.SessionRepository,+ ) *sandboxalloc.Service {+ return sandboxalloc.New(repo, configs, sessions, sandboxRedisNamespace())+ }))// Business service layerlogger.Debugf(ctx, "[Container] Registering business services...")@@ -548,13 +558,8 @@ func BuildContainer(container *dig.Container) *dig.Container {// it), but Invoke constructs the whole chain. SessionService needs// *chatpipeline.EventManager, which only exists after the pipeline// block above — starting the reaper any earlier panics.- must(container.Provide(func(- repo interfaces.SandboxAllocationRepository,- configs repository.TenantSandboxConfigRepository,- sessions interfaces.SessionRepository,- ) *sandboxalloc.Service {- return sandboxalloc.New(repo, configs, sessions, sandboxRedisNamespace())- }))+ // The allocation service itself is provided before the resolver: the+ // pipeline Invokes above construct SessionService, which needs it.must(container.Invoke(func(p *service.SessionSandboxPinner, skills *service.TenantSkillService, a *sandboxalloc.Service,) {diff --git a/internal/container/sandbox.go b/internal/container/sandbox.goindex 7d4043e7a..25fd72803 100644--- a/internal/container/sandbox.go+++ b/internal/container/sandbox.go@@ -11,9 +11,11 @@ import ("github.com/redis/go-redis/v9""gorm.io/gorm"+ "github.com/Tencent/WeKnora/internal/application/service/sandboxalloc"apperrors "github.com/Tencent/WeKnora/internal/errors""github.com/Tencent/WeKnora/internal/logger""github.com/Tencent/WeKnora/internal/sandbox"+ "github.com/Tencent/WeKnora/internal/sandbox/admission""github.com/Tencent/WeKnora/internal/sandbox/allocation""github.com/Tencent/WeKnora/internal/types""github.com/Tencent/WeKnora/internal/types/interfaces"@@ -38,10 +40,12 @@ func sandboxRedisNamespace() string {return namespace}-// userModeRuntimeReady is false until the session manager places sessions by-// their assignment (plan T10). Until then a user-mode assignment would be-// recorded for life while the session still runs on a machine of its own in-// /workspace, so the gate stays closed whatever the env says.+// userModeRuntimeReady stays false until a deleted user-mode session is+// retired, fork and rewind take the shared machine's exclusive window, and+// the desktop slot is per machine (plan T12–T18). T10 can already place and+// run those sessions, but opening the gate before the rest leaves a shared+// machine that is never released, forked, or exclusively rewound. The gate+// also needs Redis and the env opt-in; without those it stays closed.const userModeRuntimeReady = falsefunc selectSessionBindingStore(@@ -144,6 +148,7 @@ func newTenantSandboxResolver(redisClient *redis.Client,sessionRepo interfaces.SessionRepository,bootstrapper sandbox.SessionBootstrapper,+ alloc *sandboxalloc.Service,) sandbox.TenantSandboxResolver {ctx := context.Background()@@ -162,6 +167,9 @@ func newTenantSandboxResolver(Store: store,Checker: sessionExistenceCheckerFor(sessionRepo),Bootstrapper: bootstrapper,+ Allocations: alloc,+ Admission: newAdmissionRegistry(redisClient),+ Namespace: sandboxRedisNamespace(),SharedTransport: sandbox.NewGuardedTransport(),})if err != nil {@@ -173,3 +181,12 @@ func newTenantSandboxResolver(logger.Infof(ctx, "Tenant sandbox resolver configured: binding=%s", storeKind)return resolver}++// newAdmissionRegistry shares Redis with the binding store; the key prefix+// matches the user binding keys so both live under one hash tag.+func newAdmissionRegistry(redisClient *redis.Client) admission.Registry {+ if redisClient == nil {+ return admission.NewMemory()+ }+ return admission.NewRedis(redisClient, "weknora:sandbox:user:")+}diff --git a/internal/handler/session/qa.go b/internal/handler/session/qa.goindex 32724c5fe..fe1c8a371 100644--- a/internal/handler/session/qa.go+++ b/internal/handler/session/qa.go@@ -22,6 +22,7 @@ import ("github.com/Tencent/WeKnora/internal/logger""github.com/Tencent/WeKnora/internal/models/api""github.com/Tencent/WeKnora/internal/sandbox"+ "github.com/Tencent/WeKnora/internal/sandbox/admission""github.com/Tencent/WeKnora/internal/storageurl""github.com/Tencent/WeKnora/internal/stream""github.com/Tencent/WeKnora/internal/tracing/langfuse"@@ -784,7 +785,21 @@ func (h *Handler) setupSSEStream(reqCtx *qaRequestContext, generateTitle bool, m}eventBus := event.NewEventBus()- asyncCtx, cancel := context.WithCancel(logger.CloneContext(baseCtx))+ asyncCtx, cancelCause := context.WithCancelCause(logger.CloneContext(baseCtx))+ // Detach HTTP cancellation, but keep the turn's lease-loss signal: once+ // admission is lost another session may rebuild the shared machine.+ propagateLeaseLoss := func() {+ if cause := context.Cause(baseCtx); stderrors.Is(cause, admission.ErrLeaseLost) {+ cancelCause(cause)+ }+ }+ stopLeaseWatch := context.AfterFunc(baseCtx, propagateLeaseLoss)+ // An already-lost lease must cancel QA before any async work starts.+ propagateLeaseLoss()+ cancel := func() {+ stopLeaseWatch()+ cancelCause(context.Canceled)+ }streamCtx := &sseStreamContext{eventBus: eventBus,@@ -1215,23 +1230,23 @@ func (h *Handler) rejectIfSessionRewinding(ctx context.Context, sessionID stringreturn errors.NewInternalServerError(err.Error())}-func (h *Handler) holdQATurn(ctx context.Context, reqCtx *qaRequestContext) (func(), bool) {+func (h *Handler) holdQATurn(ctx context.Context, reqCtx *qaRequestContext) (context.Context, func(), bool) {noop := func() {}if reqCtx == nil {- return noop, true+ return ctx, noop, true}type sessionTurnHolder interface {- HoldSandboxTurn(context.Context, string, string) (func(), error)+ HoldSandboxTurn(context.Context, string, string) (context.Context, func(), error)}holder, ok := h.sessionService.(sessionTurnHolder)if !ok {- return noop, true+ return ctx, noop, true}configID := ""if reqCtx.customAgent != nil {configID = reqCtx.customAgent.Config.SandboxConfigID}- release, err := holder.HoldSandboxTurn(ctx, reqCtx.sessionID, configID)+ turnCtx, release, err := holder.HoldSandboxTurn(ctx, reqCtx.sessionID, configID)if err == nil {// The QA service reads this off the built request and skips its own// hold, so the send path opens and closes the lease exactly once.@@ -1247,13 +1262,16 @@ func (h *Handler) holdQATurn(ctx context.Context, reqCtx *qaRequestContext) (fun} else {logger.ErrorWithFields(ctx, err, map[string]interface{}{"session_id": reqCtx.sessionID})}- return noop, false+ return ctx, noop, false+ }+ if turnCtx == nil {+ turnCtx = ctx}if release == nil {release = noop}var once sync.Once- return func() { once.Do(release) }, true+ return turnCtx, func() { once.Do(release) }, true}// executeQA is the unified execution flow for both KnowledgeQA and AgentQA modes.@@ -1295,10 +1313,13 @@ func (h *Handler) executeQA(reqCtx *qaRequestContext, mode qaMode, generateTitleh.supersedeSuspendedRuns(ctx, sessionID)}- releaseQATurn, held := h.holdQATurn(ctx, reqCtx)+ ctx, releaseQATurn, held := h.holdQATurn(ctx, reqCtx)if !held {return}+ // setupSSEStream clones reqCtx.ctx. The turn's admission members have to+ // be on that context or a detached tool call waits on its own hold.+ reqCtx.ctx = ctx// Agent mode: emit agent query event before message creationif mode == qaModeAgent {diff --git a/internal/handler/session/qa_turn_lease_test.go b/internal/handler/session/qa_turn_lease_test.gonew file mode 100644index 000000000..7eb01c7ec--- /dev/null+++ b/internal/handler/session/qa_turn_lease_test.go@@ -0,0 +1,83 @@+package session++import (+ "context"+ "testing"+ "time"++ "github.com/Tencent/WeKnora/internal/logger"+ "github.com/Tencent/WeKnora/internal/sandbox/admission"+ "github.com/Tencent/WeKnora/internal/stream"+ "github.com/Tencent/WeKnora/internal/types"+ "github.com/stretchr/testify/require"+)++func setupLeaseTestStream(ctx context.Context, t *testing.T, mode qaMode) *sseStreamContext {+ t.Helper()+ h := &Handler{streamManager: stream.NewMemoryStreamManager()}+ req := &qaRequestContext{+ ctx: ctx,+ skipSSE: true,+ sessionID: "session",+ session: &types.Session{ID: "session", TenantID: 42},+ assistantMessage: &types.Message{ID: "assistant", SessionID: "session"},+ }+ if mode == qaModeAgent {+ req.customAgent = &types.CustomAgent{ID: "agent"}+ }+ got := h.setupSSEStream(req, false, mode)+ t.Cleanup(got.cancel)+ return got+}++func TestSetupSSEStreamCancelsOnTurnLeaseLoss(t *testing.T) {+ for name, mode := range map[string]qaMode{"normal": qaModeNormal, "agent": qaModeAgent} {+ for _, lostBeforeSetup := range []bool{false, true} {+ t.Run(name+map[bool]string{false: "/during-run", true: "/before-setup"}[lostBeforeSetup],+ func(t *testing.T) {+ requestCtx, disconnect := context.WithCancel(t.Context())+ defer disconnect()+ // The handler detaches from HTTP before acquiring the turn lease.+ baseCtx := logger.CloneContext(requestCtx)+ reg := admission.NewMemory()+ scope := admission.MachineScope("machine")+ member := admission.SessionMember("session", "turn", "token")+ lease, err := reg.Hold(baseCtx, scope, member, admission.DefaultHoldTTL)+ require.NoError(t, err)+ defer lease.Release()+ turnCtx, endTurn := admission.Bind(baseCtx, lease)+ defer endTurn()+ turnCtx = context.WithValue(turnCtx, types.SandboxAdmissionExemptContextKey, []string{member})+ if lostBeforeSetup {+ reg.ExpireMember(scope, member)+ require.Eventually(t, func() bool { return turnCtx.Err() != nil },+ time.Second, time.Millisecond)+ }++ got := setupLeaseTestStream(turnCtx, t, mode)+ require.Equal(t, []string{member}, got.asyncCtx.Value(types.SandboxAdmissionExemptContextKey))+ if lostBeforeSetup {+ require.ErrorIs(t, context.Cause(got.asyncCtx), admission.ErrLeaseLost)+ } else {+ disconnect()+ require.NoError(t, got.asyncCtx.Err(), "HTTP disconnect must not stop the detached turn")+ reg.ExpireMember(scope, member)+ }+ require.Eventually(t, func() bool { return got.asyncCtx.Err() != nil },+ time.Second, time.Millisecond,+ "the actual QA context must stop when the turn loses its admission")+ require.ErrorIs(t, context.Cause(got.asyncCtx), admission.ErrLeaseLost)+ })+ }+ }+}++func TestSetupSSEStreamKeepsOrdinaryCancellationDetached(t *testing.T) {+ parent, cancel := context.WithCancel(t.Context())+ cancel()+ got := setupLeaseTestStream(parent, t, qaModeNormal)+ require.Never(t, func() bool { return got.asyncCtx.Err() != nil }, 30*time.Millisecond, time.Millisecond,+ "ordinary request cancellation is not a lost sandbox lease")+ got.cancel()+ require.ErrorIs(t, got.asyncCtx.Err(), context.Canceled, "explicit stop must still cancel QA")+}diff --git a/internal/sandbox/admission/memory.go b/internal/sandbox/admission/memory.goindex 24107a2c7..16e780dfd 100644--- a/internal/sandbox/admission/memory.go+++ b/internal/sandbox/admission/memory.go@@ -81,6 +81,22 @@ func (r *MemoryRegistry) Close(_ context.Context, scope Scope, _ time.Duration)return b, nil}+// ExpireScope marks every member of scope lost. Tests use it when the+// member token is chosen inside the call under test.+func (r *MemoryRegistry) ExpireScope(scope Scope) {+ r.mu.Lock()+ set := r.members[scope]+ delete(r.members, scope)+ lost := make([]*lease, 0, len(set))+ for _, l := range set {+ lost = append(lost, l)+ }+ r.mu.Unlock()+ for _, l := range lost {+ l.markLost()+ }+}+// ExpireMember simulates a lease that expired server-side.func (r *MemoryRegistry) ExpireMember(scope Scope, member string) {r.mu.Lock()diff --git a/internal/sandbox/capabilities.go b/internal/sandbox/capabilities.goindex 75ba0ed1a..88c5ffe6f 100644--- a/internal/sandbox/capabilities.go+++ b/internal/sandbox/capabilities.go@@ -14,11 +14,25 @@ package sandboximport ("context"+ "errors""time""github.com/gorilla/websocket")+var (+ // ErrSharedSandbox is returned when the session lives on its owner's shared+ // machine, so destroying its sandbox would destroy every sibling session's+ // workspace. Delete the session workspace instead.+ ErrSharedSandbox = errors.New("sandbox: session shares its owner's machine")+ // ErrSessionWorkspaceBusy is returned when the session's workspace is held+ // exclusively, or has operations that an exclusive request must not cut off.+ ErrSessionWorkspaceBusy = errors.New("sandbox: session workspace is busy")+ // ErrUserAllocationRejected is returned when a stored user-mode assignment's+ // owner identity is not, or is no longer, eligible to share a machine.+ ErrUserAllocationRejected = errors.New("sandbox: user-mode allocation rejected for this owner")+)+// SessionShellExecutor executes ad-hoc shell commands inside a session-// scoped remote sandbox. SessionBoundManager surfaces it while Cube, E2B,// or Docker is active.@@ -196,11 +210,11 @@ type SessionDesktopProvider interface {SessionDesktopManager() SessionDesktopManager}-// SessionTurnHolder marks the start and end of one chat turn on a session's-// sandbox. While the turn is open, a stale image mark waits: the first-// resolve of the turn may rebuild, later resolves of the same turn keep the-// sandbox so /workspace scratch and in-flight execs survive an admin install.+// SessionTurnHolder marks one chat turn on a session's sandbox.type SessionTurnHolder interface {- BeginSessionTurn(ctx context.Context, sessionID string) error- EndSessionTurn(ctx context.Context, sessionID string) error+ // BeginSessionTurn opens one turn hold. Run the turn with the returned+ // context: on a shared machine it carries this hold's admission member,+ // so the turn's own tool calls do not wait for it, and it ends if the+ // registration is lost. end closes exactly this hold; call it once.+ BeginSessionTurn(ctx context.Context, sessionID string) (turnCtx context.Context, end func(), err error)}diff --git a/internal/sandbox/config_sandboxes.go b/internal/sandbox/config_sandboxes.goindex 0f57a77ae..ca03a54b5 100644--- a/internal/sandbox/config_sandboxes.go+++ b/internal/sandbox/config_sandboxes.go@@ -45,6 +45,12 @@ func MetadataSessionIDKey() string {return remoteMetadataSessionID}+// MetadataAllocationUser marks a shared user machine in provider metadata.+const MetadataAllocationUser = "user"++// MetadataAllocationKey returns the provider metadata key for the allocation mode.+func MetadataAllocationKey() string { return remoteMetadataAllocation }+// configSandboxFilter narrows a listing to one workspace's one config.func configSandboxFilter(tenantID uint64, configID string) RemoteListFilter {return RemoteListFilter{diff --git a/internal/sandbox/remote_fake_test.go b/internal/sandbox/remote_fake_test.goindex de7be862a..f0991ddbe 100644--- a/internal/sandbox/remote_fake_test.go+++ b/internal/sandbox/remote_fake_test.go@@ -4,6 +4,7 @@ import ("context""fmt""sync"+ "sync/atomic""testing""time"@@ -102,6 +103,14 @@ type fakeRemoteClient struct {// snapshots maps snapshotID -> source sandboxID.snapshots map[string]stringsnapshotSeq int++ // ptyClosed is set when a fake PTY opened on this client is closed.+ ptyClosed atomic.Bool++ // openTerminalHook, when set, runs instead of opening immediately. It+ // sees the context the manager passed, so a test can block the dial+ // until that context is cancelled.+ openTerminalHook func(context.Context) error}func newFakeRemoteClient(provider RemoteProvider) *fakeRemoteClient {@@ -455,6 +464,39 @@ func (c *fakeRemoteClient) counts() (creates, connects, gets, lists, deletes intreturn c.createCount, len(c.connectIDs), len(c.getIDs), c.listCount, len(c.deleteIDs)}+func (c *fakeRemoteClient) OpenTerminal(+ ctx context.Context, _ RemoteSandboxHandle, _ RemoteTerminalOptions,+) (RemoteTerminalSession, error) {+ if c.openTerminalHook != nil {+ if err := c.openTerminalHook(ctx); err != nil {+ return nil, err+ }+ }+ return &fakeRemoteTerminal{client: c}, nil+}++func (c *fakeRemoteClient) terminalClosed() bool { return c.ptyClosed.Load() }++// fakeRemoteTerminal is the PTY the shared fake opens. Close records that+// the stream was torn down, which is how a lost admission lease is observed.+type fakeRemoteTerminal struct {+ client *fakeRemoteClient+}++func (s *fakeRemoteTerminal) Output() <-chan RemoteTerminalEvent { return nil }+func (s *fakeRemoteTerminal) PID() uint32 { return 1 }+func (s *fakeRemoteTerminal) Write(context.Context, []byte) error { return nil }+func (s *fakeRemoteTerminal) Resize(context.Context, uint32, uint32) error {+ return nil+}++func (s *fakeRemoteTerminal) Close() error {+ if s.client != nil {+ s.client.ptyClosed.Store(true)+ }+ return nil+}+func (c *fakeRemoteClient) hasSandbox(id string) bool {c.mu.Lock()defer c.mu.Unlock()diff --git a/internal/sandbox/remote_sandbox.go b/internal/sandbox/remote_sandbox.goindex e5257b369..fdb032a30 100644--- a/internal/sandbox/remote_sandbox.go+++ b/internal/sandbox/remote_sandbox.go@@ -94,13 +94,19 @@ func (s *RemoteSandbox) Execute(ctx context.Context, cfg *ExecuteConfig) (*Execureturn s.ExecuteOnHandle(execCtx, handle, cfg)}-// ExecuteOnHandle runs cfg's script against an existing handle without-// allocating or deleting the underlying sandbox. It is the persistent path-// SessionBoundManager drives after resolving the session's handle.+// ExecuteOnHandle runs cfg's script on an existing handle; uploads land in+// /workspace and on-disk scripts run from there.func (s *RemoteSandbox) ExecuteOnHandle(- ctx context.Context,- handle RemoteSandboxHandle,- cfg *ExecuteConfig,+ ctx context.Context, handle RemoteSandboxHandle, cfg *ExecuteConfig,+) (*ExecuteResult, error) {+ return s.ExecuteOnHandleIn(ctx, handle, cfg, SessionWorkspaceRoot, remoteScriptDir)+}++// ExecuteOnHandleIn is ExecuteOnHandle with explicit directories. A shared+// machine passes the session root and a script directory inside it, so two+// sessions uploading run.py never overwrite each other.+func (s *RemoteSandbox) ExecuteOnHandleIn(+ ctx context.Context, handle RemoteSandboxHandle, cfg *ExecuteConfig, workDir, scriptDir string,) (*ExecuteResult, error) {if s == nil || s.client == nil {return nil, ErrSandboxDisabled@@ -127,7 +133,7 @@ func (s *RemoteSandbox) ExecuteOnHandle(Args: append(baseArgs, cfg.Args...),Stdin: cfg.Stdin,Env: cfg.Env,- WorkDir: SessionWorkspaceRoot,+ WorkDir: workDir,User: DefaultSandboxExecUser,Timeout: effectiveTimeout(cfg, 0),}@@ -144,7 +150,12 @@ func (s *RemoteSandbox) ExecuteOnHandle(if scriptName == "" || scriptName == "." || scriptName == "/" {return nil, ErrInvalidScript}- remoteScript := path.Join(remoteScriptDir, scriptName)+ remoteScript := path.Join(scriptDir, scriptName)+ if scriptDir != remoteScriptDir {+ if err := ignoreExistingDir(s.client.MakeDir(ctx, handle, scriptDir)); err != nil {+ return nil, fmt.Errorf("remote sandbox: create script dir %s: %w", scriptDir, err)+ }+ }if err := s.client.WriteFile(ctx, handle, remoteScript, content); err != nil {return nil, fmt.Errorf("remote sandbox: upload script %s: %w", remoteScript, err)@@ -156,7 +167,7 @@ func (s *RemoteSandbox) ExecuteOnHandle(Args: append([]string{remoteScript}, cfg.Args...),Stdin: cfg.Stdin,Env: cfg.Env,- WorkDir: remoteScriptDir,+ WorkDir: scriptDir,User: DefaultSandboxExecUser,Timeout: timeout,}diff --git a/internal/sandbox/session_binding.go b/internal/sandbox/session_binding.goindex 5661a15ce..cb4cddf21 100644--- a/internal/sandbox/session_binding.go+++ b/internal/sandbox/session_binding.go@@ -8,19 +8,69 @@ import ("sync""time""unicode"++ "github.com/Tencent/WeKnora/internal/sandbox/allocation"+ "github.com/Tencent/WeKnora/internal/sandbox/workspace")-// SessionSandboxBindingVersion is the current persisted binding schema.+// SessionSandboxBindingVersion is the schema of a per-session machine binding.const SessionSandboxBindingVersion = 1-// SessionSandboxKey identifies one tenant-scoped persistent sandbox.+// UserSandboxBindingVersion is the schema of a shared user machine binding.+const UserSandboxBindingVersion = 2++// SessionSandboxKey addresses persistent sandbox state.+//+// With User zero it is today's per-session key. With User set and SessionID+// empty it addresses a shared user machine (binding, lifecycle lock); with+// both set it addresses one session's leases on that machine (turn lease,+// rewind lock).type SessionSandboxKey struct {TenantID uint64SessionID string+ User allocation.UserMachine+}++// IsUserScoped reports a key on a shared user machine.+func (k SessionSandboxKey) IsUserScoped() bool { return k.User.OwnerID != "" }++// IsUserMachine reports the machine-level key of a shared user machine.+func (k SessionSandboxKey) IsUserMachine() bool { return k.IsUserScoped() && k.SessionID == "" }++// Machine is the key the binding and the lifecycle lock live under.+func (k SessionSandboxKey) Machine() SessionSandboxKey {+ if k.IsUserScoped() {+ return SessionSandboxKey{TenantID: k.TenantID, User: k.User}+ }+ return k}-// Validate rejects keys that cannot identify a tenant session.+// String identifies the key in logs without the owner id.+func (k SessionSandboxKey) String() string {+ if k.IsUserScoped() {+ if k.SessionID == "" {+ return fmt.Sprintf("user-machine(tenant=%d,config=%d/%s)",+ k.TenantID, k.User.ConfigTenantID, k.User.ConfigID)+ }+ return fmt.Sprintf("user-session(tenant=%d,session=%s)", k.TenantID, k.SessionID)+ }+ return fmt.Sprintf("session(tenant=%d,session=%s)", k.TenantID, k.SessionID)+}++// Validate rejects keys that cannot address sandbox state.func (k SessionSandboxKey) Validate() error {+ if k.IsUserScoped() {+ if err := k.User.Validate(); err != nil {+ return fmt.Errorf("sandbox binding: %w", err)+ }+ if k.TenantID != k.User.OwnerTenantID {+ return errors.New("sandbox binding tenant must be the user machine's owner tenant")+ }+ if k.SessionID != "" && !workspace.IsCanonicalUUID(k.SessionID) {+ return errors.New("sandbox binding session on a user machine must be a canonical UUID")+ }+ return nil+ }if k.TenantID == 0 || strings.TrimSpace(k.SessionID) == "" {return errors.New("sandbox binding requires tenant and session")}@@ -74,26 +124,42 @@ type SessionSandboxBinding struct {// Empty for Docker (no such credential) and for bindings written before// this field existed; both are valid.TrafficAccessToken string `json:"traffic_access_token,omitempty"`++ // OwnerID and ConfigTenantID complete a user machine's identity+ // (version 2). Empty on per-session bindings.+ OwnerID string `json:"owner_id,omitempty"`+ ConfigTenantID uint64 `json:"config_tenant_id,omitempty"`}-// Validate checks a binding against the current schema and authoritative key.+// Validate checks a binding against its schema and authoritative key.func (b SessionSandboxBinding) Validate(key SessionSandboxKey) error {if err := key.Validate(); err != nil {return err}- if b.Version != SessionSandboxBindingVersion {- return fmt.Errorf(- "sandbox binding version must be %d, got %d",- SessionSandboxBindingVersion,- b.Version,- )+ if key.IsUserScoped() {+ if !key.IsUserMachine() {+ return errors.New("sandbox binding must be addressed by its user machine key")+ }+ if b.Version != UserSandboxBindingVersion {+ return fmt.Errorf("user sandbox binding version must be %d, got %d", UserSandboxBindingVersion, b.Version)+ }+ if b.TenantID != key.TenantID || b.SessionID != "" ||+ b.OwnerID != key.User.OwnerID ||+ b.ConfigTenantID != key.User.ConfigTenantID ||+ b.ConfigID != key.User.ConfigID {+ return errors.New("sandbox binding identity does not match its key")+ }+ } else {+ if b.Version != SessionSandboxBindingVersion {+ return fmt.Errorf("sandbox binding version must be %d, got %d", SessionSandboxBindingVersion, b.Version)+ }+ if b.TenantID != key.TenantID || b.SessionID != key.SessionID {+ return errors.New("sandbox binding identity does not match its key")+ }}if !isRemoteProvider(b.Provider) {return fmt.Errorf("unsupported sandbox binding provider %q", b.Provider)}- if b.TenantID != key.TenantID || b.SessionID != key.SessionID {- return errors.New("sandbox binding identity does not match its key")- }if strings.TrimSpace(b.SandboxID) == "" {return errors.New("sandbox binding requires sandbox ID")}@@ -209,7 +275,15 @@ func invalidateBindingsByConfig(if err != nil {return 0, err}+ return markBindingKeysStale(ctx, store, keys, configID)+}+// markBindingKeysStale marks every listed binding of configID stale, one key+// at a time under that key's lifecycle lock. Shared with the user machines+// the manager enumerates from the allocation registry.+func markBindingKeysStale(+ ctx context.Context, store tenantBindingScanner, keys []SessionSandboxKey, configID string,+) (int, error) {wanted := NormalizeConfigID(configID)marked := 0var failures []error@@ -253,7 +327,7 @@ func invalidateBindingsByConfig(acquireTimer.Stop()cancel()if err != nil {- failures = append(failures, fmt.Errorf("mark session %s stale: %w", key.SessionID, err))+ failures = append(failures, fmt.Errorf("mark %s stale: %w", key, err))}}return marked, errors.Join(failures...)@@ -321,11 +395,14 @@ func (s *MemorySessionSandboxBindingStore) Get(s.mu.Lock()defer s.mu.Unlock()- binding, ok := s.bindings[key]+ binding, ok := s.bindings[key.Machine()]if !ok {return nil, nil}result := binding+ if err := result.Validate(key); err != nil {+ return nil, fmt.Errorf("validate sandbox binding: %w", err)+ }return &result, nil}@@ -344,10 +421,11 @@ func (s *MemorySessionSandboxBindingStore) Create(s.mu.Lock()defer s.mu.Unlock()- if _, exists := s.bindings[key]; exists {+ machine := key.Machine()+ if _, exists := s.bindings[machine]; exists {return false, nil}- s.bindings[key] = binding+ s.bindings[machine] = bindingreturn true, nil}@@ -367,11 +445,12 @@ func (s *MemorySessionSandboxBindingStore) DeleteIfMatch(s.mu.Lock()defer s.mu.Unlock()- binding, exists := s.bindings[key]+ machine := key.Machine()+ binding, exists := s.bindings[machine]if !exists || binding.Provider != provider || binding.SandboxID != sandboxID {return false, nil}- delete(s.bindings, key)+ delete(s.bindings, machine)return true, nil}@@ -395,7 +474,8 @@ func (s *MemorySessionSandboxBindingStore) ReplaceTrafficTokenIfMatch(s.mu.Lock()defer s.mu.Unlock()- binding, exists := s.bindings[key]+ machine := key.Machine()+ binding, exists := s.bindings[machine]if !exists ||binding.Provider != expected.Provider ||binding.SandboxID != expected.SandboxID {@@ -405,7 +485,7 @@ func (s *MemorySessionSandboxBindingStore) ReplaceTrafficTokenIfMatch(return false, nil}binding.TrafficAccessToken = token- s.bindings[key] = binding+ s.bindings[machine] = bindingreturn true, nil}@@ -429,7 +509,7 @@ func (s *MemorySessionSandboxBindingStore) listTenantBindingKeys(defer s.mu.Unlock()var keys []SessionSandboxKeyfor key := range s.bindings {- if key.TenantID == tenantID {+ if key.TenantID == tenantID && !key.IsUserScoped() {keys = append(keys, key)}}@@ -447,7 +527,8 @@ func (s *MemorySessionSandboxBindingStore) markBindingStale(}s.mu.Lock()defer s.mu.Unlock()- binding, exists := s.bindings[key]+ machine := key.Machine()+ binding, exists := s.bindings[machine]if !exists ||binding.Provider != expected.Provider ||binding.SandboxID != expected.SandboxID {@@ -455,11 +536,11 @@ func (s *MemorySessionSandboxBindingStore) markBindingStale(}marked := staleAtbinding.StaleAt = &marked- s.bindings[key] = binding+ s.bindings[machine] = bindingreturn true, nil}-// WithLifecycleLock runs fn while holding the process-local lock for key.+// WithLifecycleLock runs fn while holding the process-local lock for key.Machine().func (s *MemorySessionSandboxBindingStore) WithLifecycleLock(ctx context.Context,key SessionSandboxKey,@@ -471,12 +552,13 @@ func (s *MemorySessionSandboxBindingStore) WithLifecycleLock(if fn == nil {return errors.New("sandbox lifecycle lock callback is required")}+ machine := key.Machine()s.mu.Lock()- lock := s.locks[key]+ lock := s.locks[machine]if lock == nil {lock = &memoryLifecycleLock{semaphore: make(chan struct{}, 1)}- s.locks[key] = lock+ s.locks[machine] = lock}lock.users++s.mu.Unlock()@@ -484,13 +566,13 @@ func (s *MemorySessionSandboxBindingStore) WithLifecycleLock(select {case lock.semaphore <- struct{}{}:case <-ctx.Done():- s.dropMemoryLifecycleLockUser(key, lock)+ s.dropMemoryLifecycleLockUser(machine, lock)return ctx.Err()}defer func() {<-lock.semaphore- s.dropMemoryLifecycleLockUser(key, lock)+ s.dropMemoryLifecycleLockUser(machine, lock)}()if err := ctx.Err(); err != nil {return errdiff --git a/internal/sandbox/session_binding_redis.go b/internal/sandbox/session_binding_redis.goindex 8b99e6893..26e66d209 100644--- a/internal/sandbox/session_binding_redis.go+++ b/internal/sandbox/session_binding_redis.go@@ -460,11 +460,17 @@ func (s *RedisSessionSandboxBindingStore) ConsumeTurnRebuild(}func (s *RedisSessionSandboxBindingStore) turnKey(key SessionSandboxKey) string {- return "weknora:sandbox:session:{" + s.hashTag(key) + "}:turn"+ if key.IsUserScoped() {+ return s.keyBase(key) + ":session:" + key.SessionID + ":turn"+ }+ return s.keyBase(key) + ":turn"}func (s *RedisSessionSandboxBindingStore) rewindKey(key SessionSandboxKey) string {- return "weknora:sandbox:session:{" + s.hashTag(key) + "}:rewind"+ if key.IsUserScoped() {+ return s.keyBase(key) + ":session:" + key.SessionID + ":rewind"+ }+ return s.keyBase(key) + ":rewind"}// TryLockRewind takes a distributed exclusive rewind lock for key.@@ -596,14 +602,23 @@ func (s *RedisSessionSandboxBindingStore) renewRewindLock(}}+// keyBase is the hash-tagged prefix of every key of one machine. User keys+// carry the allocation digest, which already includes the namespace.+func (s *RedisSessionSandboxBindingStore) keyBase(key SessionSandboxKey) string {+ if key.IsUserScoped() {+ return "weknora:sandbox:user:{" + key.User.Digest(s.namespace) + "}"+ }+ return "weknora:sandbox:session:{" + s.hashTag(key) + "}"+}+func (s *RedisSessionSandboxBindingStore) bindingKey(key SessionSandboxKey) string {- return "weknora:sandbox:session:{" + s.hashTag(key) + "}:binding"+ return s.keyBase(key.Machine()) + ":binding"}func (s *RedisSessionSandboxBindingStore) lockKey(key SessionSandboxKey) string {// Keep the historical suffix used by the saved multi-node Cube// implementation so rolling upgrades serialize on the same lock.- return "weknora:sandbox:session:{" + s.hashTag(key) + "}:create-lock"+ return s.keyBase(key.Machine()) + ":create-lock"}func (s *RedisSessionSandboxBindingStore) hashTag(key SessionSandboxKey) string {diff --git a/internal/sandbox/session_binding_redis_test.go b/internal/sandbox/session_binding_redis_test.goindex 5b968bbe1..c4c8ff2bd 100644--- a/internal/sandbox/session_binding_redis_test.go+++ b/internal/sandbox/session_binding_redis_test.go@@ -301,3 +301,23 @@ func TestRedisLifecycleLockAllowsDifferentKeys(t *testing.T) {close(releaseFirst)require.NoError(t, <-firstDone)}++func TestRedisUserMachineKeysShareOneHashTag(t *testing.T) {+ store, _, mini := newRedisBindingTestStore(t)+ ctx := context.Background()+ key := userMachineKey()+ created, err := store.Create(ctx, key, validUserBinding("sb-1"))+ require.NoError(t, err)+ require.True(t, created)++ digest := testUserMachine().Digest(store.namespace)+ require.True(t, mini.Exists("weknora:sandbox:user:{"+digest+"}:binding"))++ scoped := SessionSandboxKey{TenantID: 42, SessionID: userKeySessionID, User: testUserMachine()}+ require.NoError(t, store.BeginTurn(ctx, scoped))+ require.True(t, mini.Exists("weknora:sandbox:user:{"+digest+"}:session:"+userKeySessionID+":turn"))++ got, err := store.Get(ctx, key)+ require.NoError(t, err)+ require.Equal(t, "sb-1", got.SandboxID)+}diff --git a/internal/sandbox/session_binding_test.go b/internal/sandbox/session_binding_test.goindex d88b7c0e6..89c88d8cf 100644--- a/internal/sandbox/session_binding_test.go+++ b/internal/sandbox/session_binding_test.go@@ -10,6 +10,8 @@ import ("time""github.com/stretchr/testify/require"++ "github.com/Tencent/WeKnora/internal/sandbox/allocation")func validSessionSandboxBinding(key SessionSandboxKey, sandboxID string) SessionSandboxBinding {@@ -530,3 +532,124 @@ func TestMemoryLifecycleLockHonorsContextAndCallbackError(t *testing.T) {})require.ErrorIs(t, err, want)}++const userKeySessionID = "0f8fad5b-d9cb-469f-a165-70867728950e"++func testUserMachine() allocation.UserMachine {+ return allocation.UserMachine{OwnerTenantID: 42, OwnerID: "u-1", ConfigTenantID: 42, ConfigID: "cfg-1"}+}++func userMachineKey() SessionSandboxKey {+ return SessionSandboxKey{TenantID: 42, User: testUserMachine()}+}++func validUserBinding(sandboxID string) SessionSandboxBinding {+ m := testUserMachine()+ return SessionSandboxBinding{+ Version: UserSandboxBindingVersion, Provider: SandboxTypeCube, TenantID: 42,+ SandboxID: sandboxID, TemplateID: "tpl", CreatedAt: time.Now().UTC(),+ ConfigID: m.ConfigID, OwnerID: m.OwnerID, ConfigTenantID: m.ConfigTenantID,+ }+}++func TestSessionSandboxKeyShapes(t *testing.T) {+ machine := userMachineKey()+ require.NoError(t, machine.Validate())+ require.True(t, machine.IsUserScoped())+ require.True(t, machine.IsUserMachine())++ scoped := SessionSandboxKey{TenantID: 42, SessionID: userKeySessionID, User: testUserMachine()}+ require.NoError(t, scoped.Validate())+ require.False(t, scoped.IsUserMachine())+ require.Equal(t, machine, scoped.Machine())++ legacy := SessionSandboxKey{TenantID: 42, SessionID: "session-a"}+ require.Equal(t, legacy, legacy.Machine())+ require.False(t, legacy.IsUserScoped())++ require.Error(t, SessionSandboxKey{TenantID: 43, User: testUserMachine()}.Validate(),+ "the key tenant must be the machine's owner tenant")+ require.Error(t, SessionSandboxKey{TenantID: 42, SessionID: "not-a-uuid", User: testUserMachine()}.Validate())+}++func TestUserBindingValidation(t *testing.T) {+ key := userMachineKey()+ require.NoError(t, validUserBinding("sb-1").Validate(key))++ wrongVersion := validUserBinding("sb-1")+ wrongVersion.Version = SessionSandboxBindingVersion+ require.Error(t, wrongVersion.Validate(key))++ wrongOwner := validUserBinding("sb-1")+ wrongOwner.OwnerID = "u-2"+ require.Error(t, wrongOwner.Validate(key))++ withSession := validUserBinding("sb-1")+ withSession.SessionID = userKeySessionID+ require.Error(t, withSession.Validate(key))++ scoped := SessionSandboxKey{TenantID: 42, SessionID: userKeySessionID, User: testUserMachine()}+ require.Error(t, validUserBinding("sb-1").Validate(scoped), "bindings are addressed by the machine key only")++ // A legacy binding still validates exactly as before.+ legacyKey := SessionSandboxKey{TenantID: 42, SessionID: "session-a"}+ require.NoError(t, validSessionSandboxBinding(legacyKey, "sb-2").Validate(legacyKey))+}++func TestMemoryStoreHoldsUserMachineBindings(t *testing.T) {+ ctx := context.Background()+ store := NewMemorySessionSandboxBindingStore()+ key := userMachineKey()+ created, err := store.Create(ctx, key, validUserBinding("sb-1"))+ require.NoError(t, err)+ require.True(t, created)+ got, err := store.Get(ctx, key)+ require.NoError(t, err)+ require.Equal(t, "sb-1", got.SandboxID)++ keys, err := store.listTenantBindingKeys(ctx, 42)+ require.NoError(t, err)+ require.Empty(t, keys, "the session-binding scan never returns user machines")+}++func TestMemoryLifecycleLockSerializesUserMachineAndItsSession(t *testing.T) {+ store := NewMemorySessionSandboxBindingStore()+ machine := userMachineKey()+ scoped := SessionSandboxKey{TenantID: 42, SessionID: userKeySessionID, User: testUserMachine()}+ var active atomic.Int32+ var overlapped atomic.Bool+ start := make(chan struct{})+ done := make(chan error, 2)++ for _, key := range []SessionSandboxKey{machine, scoped} {+ go func(key SessionSandboxKey) {+ <-start+ done <- store.WithLifecycleLock(context.Background(), key, func(context.Context) error {+ if active.Add(1) != 1 {+ overlapped.Store(true)+ }+ time.Sleep(20 * time.Millisecond)+ active.Add(-1)+ return nil+ })+ }(key)+ }+ close(start)++ require.NoError(t, <-done)+ require.NoError(t, <-done)+ require.False(t, overlapped.Load())+}++func TestMemoryGetRejectsSessionOnUserMachineBinding(t *testing.T) {+ ctx := context.Background()+ store := NewMemorySessionSandboxBindingStore()+ created, err := store.Create(ctx, userMachineKey(), validUserBinding("sb-1"))+ require.NoError(t, err)+ require.True(t, created)++ scoped := SessionSandboxKey{TenantID: 42, SessionID: userKeySessionID, User: testUserMachine()}+ got, err := store.Get(ctx, scoped)+ require.Nil(t, got)+ require.ErrorContains(t, err, "validate sandbox binding: sandbox binding must be addressed by its user machine key")+}diff --git a/internal/sandbox/session_lifecycle.go b/internal/sandbox/session_lifecycle.goindex 3b11c915a..020a5cd5c 100644--- a/internal/sandbox/session_lifecycle.go+++ b/internal/sandbox/session_lifecycle.go@@ -10,6 +10,8 @@ import ("strings""time"+ "github.com/Tencent/WeKnora/internal/sandbox/admission"+ "github.com/Tencent/WeKnora/internal/sandbox/allocation""github.com/Tencent/WeKnora/internal/types")@@ -23,6 +25,14 @@ const (// Two configs in one workspace may share a provider account, so cleanup// must filter by config as well as tenant/session ownership.remoteMetadataConfigID = "weknora_sandbox_config_id"++ // User machines are identified by their allocation digest, never by a+ // session. weknora_tenant_id on a user machine is the CONFIG owner, so+ // the config inventory and the credential-change sweep include every+ // borrower's machine; the owner tenant is recorded separately.+ remoteMetadataOwnerTenantID = "weknora_owner_tenant_id"+ remoteMetadataMachineDigest = "weknora_machine_digest"+ remoteMetadataAllocation = "weknora_allocation")// ErrSandboxSessionDeleted reports that the owning WeKnora session no longer@@ -48,6 +58,21 @@ type remoteSessionLifecycle struct {// bootstrapper customises the first create of individual sessions// (session fork). Optional; nil keeps the default behaviour.bootstrapper SessionBootstrapper++ // namespace and admission are set only when the deployment can host+ // shared user machines; user keys are refused without them.+ namespace string+ admission admission.Registry+}++type lifecycleOption func(*remoteSessionLifecycle)++// withUserMachines enables shared user machines on this lifecycle.+func withUserMachines(namespace string, reg admission.Registry) lifecycleOption {+ return func(l *remoteSessionLifecycle) {+ l.namespace = namespace+ l.admission = reg+ }}func newRemoteSessionLifecycle(@@ -58,6 +83,7 @@ func newRemoteSessionLifecycle(cleanupTimeout time.Duration,sandboxConfigID string,bootstrapper SessionBootstrapper,+ opts ...lifecycleOption,) (*remoteSessionLifecycle, error) {if client == nil {return nil, errors.New("remote sandbox client is required")@@ -85,7 +111,7 @@ func newRemoteSessionLifecycle(}createRequest.Metadata = cloneMetadata(createRequest.Metadata)createRequest.EnvVars = cloneMetadata(createRequest.EnvVars)- return &remoteSessionLifecycle{+ l := &remoteSessionLifecycle{client: client,bindings: bindings,sessionChecker: sessionChecker,@@ -94,7 +120,15 @@ func newRemoteSessionLifecycle(sandboxConfigID: sandboxConfigID,now: time.Now,bootstrapper: bootstrapper,- }, nil+ }+ for _, opt := range opts {+ opt(l)+ }+ return l, nil+}++func (l *remoteSessionLifecycle) digest(key SessionSandboxKey) string {+ return key.User.Digest(l.namespace)}// Resolve returns the current provider's sandbox for key, creating or@@ -106,9 +140,12 @@ func (l *remoteSessionLifecycle) Resolve(if err := key.Validate(); err != nil {return nil, err}+ if key.IsUserScoped() && l.admission == nil {+ return nil, errors.New("resolve remote sandbox: user machines require an admission registry")+ }var handle RemoteSandboxHandle- err := l.bindings.WithLifecycleLock(ctx, key, func(lockCtx context.Context) error {+ err := l.bindings.WithLifecycleLock(ctx, key.Machine(), func(lockCtx context.Context) error {resolved, err := l.resolveLocked(lockCtx, key)if err != nil {return err@@ -151,29 +188,32 @@ func (l *remoteSessionLifecycle) resolveLocked(ctx context.Context,key SessionSandboxKey,) (RemoteSandboxHandle, error) {- binding, err := l.readBinding(ctx, key)+ machine := key.Machine()+ binding, err := l.readBinding(ctx, machine)if err != nil {return nil, err}- exists, err := l.sessionChecker.SessionExists(ctx, key)- if err != nil {- return nil, fmt.Errorf("check owning session: %w", err)- }- if !exists {- if binding != nil {- err = l.destroyBindingLocked(ctx, key, *binding)+ // A per-session machine dies with its session. A shared machine never+ // does: the manager refuses a deleted session before calling here, and+ // the machine is reclaimed only by explicit release or the empty-machine+ // GC. Running this check on a user key would let one stale request+ // destroy every session's workspace.+ if !key.IsUserScoped() {+ exists, err := l.sessionChecker.SessionExists(ctx, key)+ if err != nil {+ return nil, fmt.Errorf("check owning session: %w", err)+ }+ if !exists {+ if binding != nil {+ err = l.destroyBindingLocked(ctx, key, *binding)+ }+ return nil, errors.Join(ErrSandboxSessionDeleted, err)}- return nil, errors.Join(ErrSandboxSessionDeleted, err)}if binding != nil && binding.Provider != l.client.Provider() {- deleted, err := l.bindings.DeleteIfMatch(- ctx,- key,- binding.Provider,- binding.SandboxID,- )+ deleted, err := l.bindings.DeleteIfMatch(ctx, machine, binding.Provider, binding.SandboxID)if err != nil {return nil, fmt.Errorf("delete mismatched provider binding: %w", err)}@@ -193,30 +233,28 @@ func (l *remoteSessionLifecycle) resolveLocked(// Destroying before rebuilding is not optional: the recovery pass below// adopts any live sandbox carrying this session's metadata, so a surviving// one would simply be picked up again.- if binding != nil && binding.StaleAt != nil && l.shouldRebuildStaleBinding(ctx, key) {- if err := l.destroyBindingLocked(ctx, key, *binding); err != nil {- return nil, fmt.Errorf("release stale sandbox binding: %w", err)+ if binding != nil && binding.StaleAt != nil {+ rebuilt, err := l.rebuildStale(ctx, key, *binding)+ if err != nil {+ return nil, err+ }+ if rebuilt {+ binding = nil}- binding = nil}// First resolve of a turn spends rebuildOnce even when nothing was stale,// so a later install in the same turn cannot tear the sandbox down.l.consumeTurnRebuild(ctx, key)if binding != nil {- handle, replace, err := l.connectBinding(ctx, key, *binding)+ handle, replace, err := l.connectBinding(ctx, machine, *binding)if err != nil {return nil, err}if !replace {return handle, nil}- deleted, err := l.bindings.DeleteIfMatch(- ctx,- key,- binding.Provider,- binding.SandboxID,- )+ deleted, err := l.bindings.DeleteIfMatch(ctx, machine, binding.Provider, binding.SandboxID)if err != nil {return nil, fmt.Errorf("delete terminal sandbox binding: %w", err)}@@ -225,14 +263,82 @@ func (l *remoteSessionLifecycle) resolveLocked(}}- recovered, ok, err := l.recoverOwnedSandbox(ctx, key)+ recovered, ok, err := l.recoverOwnedSandbox(ctx, machine)if err != nil {return nil, err}if ok {return recovered, nil}- return l.createAndBind(ctx, key)+ return l.createAndBind(ctx, machine)+}++// rebuildStale replaces a sandbox booted from a superseded image. A+// per-session machine rebuilds at its turn boundary. A shared machine also+// needs every operation except this request's own to be gone, and holds the+// machine barrier while it destroys so none can start in between; a closer+// already at work wins. Losing the barrier stops before any further step.+func (l *remoteSessionLifecycle) rebuildStale(+ ctx context.Context, key SessionSandboxKey, binding SessionSandboxBinding,+) (bool, error) {+ if !l.shouldRebuildStaleBinding(ctx, key) {+ return false, nil+ }+ if !key.IsUserScoped() {+ if err := l.destroyBindingLocked(ctx, key, binding); err != nil {+ return false, fmt.Errorf("release stale sandbox binding: %w", err)+ }+ return true, nil+ }+ scope := admission.MachineScope(l.digest(key))+ barrier, err := l.admission.Close(ctx, scope, redisLifecycleLockLease)+ if err != nil {+ if !errors.Is(err, admission.ErrClosed) {+ log.Printf("[sandbox] close %s for rebuild failed (%v); keeping the current sandbox", key, err)+ }+ return false, nil+ }+ defer barrier.Release()+ // C3: a lost barrier also cancels the provider call in flight.+ guarded, stop := admission.Bind(ctx, barrier)+ defer stop()+ others, err := l.admission.ActiveExcept(guarded, scope, admissionExemptFrom(ctx)...)+ if err != nil || others > 0 {+ return false, nil+ }+ if err := barrier.Check(); err != nil {+ return false, err+ }+ if err := l.destroyBindingLocked(guarded, key.Machine(), binding); err != nil {+ if cause := barrier.Check(); cause != nil {+ return false, cause+ }+ return false, fmt.Errorf("release stale sandbox binding: %w", err)+ }+ if err := barrier.Check(); err != nil {+ // The old machine is gone but admission may already be open again:+ // do not create a replacement under a lost barrier.+ return false, err+ }+ return true, nil+}++// withAdmissionExempt records the admission members that belong to the+// current request, so a rebuild it triggers does not wait for itself. The key+// is a types.ContextKey that survives logger.CloneContext: the async QA run+// (internal/handler/session/qa.go setupSSEStream) is built through it, and a+// dropped exemption would make every turn block its own rebuild forever.+func withAdmissionExempt(ctx context.Context, members ...string) context.Context {+ prev := admissionExemptFrom(ctx)+ all := make([]string, 0, len(prev)+len(members))+ all = append(all, prev...)+ all = append(all, members...)+ return context.WithValue(ctx, types.SandboxAdmissionExemptContextKey, all)+}++func admissionExemptFrom(ctx context.Context) []string {+ members, _ := ctx.Value(types.SandboxAdmissionExemptContextKey).([]string)+ return members}func (l *remoteSessionLifecycle) connectBinding(@@ -277,8 +383,8 @@ func persistInboundToken(}if _, err := store.ReplaceTrafficTokenIfMatch(ctx, key, binding, token); err != nil {log.Printf(- "[sandbox] persist inbound token of session %s failed: %v",- key.SessionID, err,+ "[sandbox] persist inbound token of %s failed: %v",+ key, err,)}}@@ -371,10 +477,10 @@ func (l *remoteSessionLifecycle) recoverOwnedSandbox(// Connect above has already resumed this sandbox and refreshed its// TTL, so leaving it would strand a billable sandbox per restart.log.Printf(- "[sandbox] deleting remote sandbox %s of session %s: "++ "[sandbox] deleting remote sandbox %s of %s: "+"adopted by metadata without the inbound traffic token "+"this config's network policy requires",- summary.ID, key.SessionID,+ summary.ID, key,)if err := l.cleanupSandboxID(ctx, summary.ID); err != nil {return nil, false, fmt.Errorf(@@ -439,9 +545,16 @@ func (l *remoteSessionLifecycle) createAndBind(}}request.EnvVars = cloneMetadata(l.createRequest.EnvVars)+ userMachine := key.IsUserMachine()+ if userMachine {+ // Session paths are per call on a shared machine; a container-level+ // value would point every session at one tree.+ delete(request.EnvVars, skillOutputEnvVar)+ delete(request.EnvVars, sessionInputEnvVar)+ }usedOverride := false- if l.bootstrapper != nil {+ if l.bootstrapper != nil && !userMachine {override, err := l.bootstrapper.TemplateOverride(ctx, key)if err != nil {return nil, fmt.Errorf("resolve session template override: %w", err)@@ -477,15 +590,17 @@ func (l *remoteSessionLifecycle) createAndBind()}- exists, checkErr := l.sessionChecker.SessionExists(ctx, key)- if checkErr != nil {- return nil, errors.Join(- fmt.Errorf("recheck owning session: %w", checkErr),- l.cleanupCreated(ctx, handle),- )- }- if !exists {- return nil, errors.Join(ErrSandboxSessionDeleted, l.cleanupCreated(ctx, handle))+ if !userMachine {+ exists, checkErr := l.sessionChecker.SessionExists(ctx, key)+ if checkErr != nil {+ return nil, errors.Join(+ fmt.Errorf("recheck owning session: %w", checkErr),+ l.cleanupCreated(ctx, handle),+ )+ }+ if !exists {+ return nil, errors.Join(ErrSandboxSessionDeleted, l.cleanupCreated(ctx, handle))+ }}binding := l.newBinding(@@ -503,8 +618,10 @@ func (l *remoteSessionLifecycle) createAndBind()}if created {- if err := l.runBootstrap(ctx, key, handle); err != nil {- return nil, err+ if !userMachine {+ if err := l.runBootstrap(ctx, key, handle); err != nil {+ return nil, err+ }}return handle, nil}@@ -591,10 +708,7 @@ func (l *remoteSessionLifecycle) connectKnownWinner(l.client.Provider(),)}- handle, replace, err := l.connectBinding(ctx, SessionSandboxKey{- TenantID: winner.TenantID,- SessionID: winner.SessionID,- }, winner)+ handle, replace, err := l.connectBinding(ctx, bindingConnectKey(winner), winner)if err != nil {return nil, err}@@ -747,7 +861,7 @@ func (l *remoteSessionLifecycle) newBinding(if createdAt.IsZero() {createdAt = l.now().UTC()}- return SessionSandboxBinding{+ b := SessionSandboxBinding{Version: SessionSandboxBindingVersion,Provider: l.client.Provider(),TenantID: key.TenantID,@@ -758,9 +872,28 @@ func (l *remoteSessionLifecycle) newBinding(ConfigID: l.sandboxConfigID,TrafficAccessToken: trafficAccessToken,}+ if key.IsUserMachine() {+ b.Version = UserSandboxBindingVersion+ b.SessionID = ""+ b.OwnerID = key.User.OwnerID+ b.ConfigTenantID = key.User.ConfigTenantID+ b.ConfigID = key.User.ConfigID+ }+ return b}func (l *remoteSessionLifecycle) metadata(key SessionSandboxKey) map[string]string {+ if key.IsUserMachine() {+ return map[string]string{+ remoteMetadataTenantID: strconv.FormatUint(key.User.ConfigTenantID, 10),+ remoteMetadataOwnerTenantID: strconv.FormatUint(key.User.OwnerTenantID, 10),+ remoteMetadataMachineDigest: l.digest(key),+ remoteMetadataAllocation: MetadataAllocationUser,+ remoteMetadataBindingVersion: strconv.Itoa(UserSandboxBindingVersion),+ remoteMetadataProvider: string(l.client.Provider()),+ remoteMetadataConfigID: l.sandboxConfigID,+ }+ }return map[string]string{remoteMetadataTenantID: strconv.FormatUint(key.TenantID, 10),remoteMetadataSessionID: key.SessionID,@@ -779,39 +912,79 @@ func (l *remoteSessionLifecycle) shouldRebuildStaleBinding(key SessionSandboxKey,) bool {leaser, ok := l.bindings.(sessionTurnLeaseStore)- if !ok {- return true+ active, rebuildOnce := false, false+ if ok && key.SessionID != "" {+ var err error+ active, rebuildOnce, err = leaser.TurnState(ctx, key)+ if err != nil {+ log.Printf("[sandbox] turn lease of %s unavailable (%v); keeping the current sandbox", key, err)+ return false+ }}- active, rebuildOnce, err := leaser.TurnState(ctx, key)- if err != nil {- log.Printf(- "[sandbox] turn lease of session %s unavailable (%v); keeping the current sandbox",- key.SessionID, err,- )+ if active && !rebuildOnce {return false}- if !active {+ if !key.IsUserScoped() {return true}- return rebuildOnce+ others, err := l.admission.ActiveExcept(ctx, admission.MachineScope(l.digest(key)), admissionExemptFrom(ctx)...)+ if err != nil {+ log.Printf("[sandbox] admission of %s unavailable (%v); keeping the current sandbox", key, err)+ return false+ }+ return others == 0}func (l *remoteSessionLifecycle) consumeTurnRebuild(ctx context.Context,key SessionSandboxKey,) {+ if key.SessionID == "" {+ return+ }leaser, ok := l.bindings.(sessionTurnLeaseStore)if !ok {return}if err := leaser.ConsumeTurnRebuild(ctx, key); err != nil {log.Printf(- "[sandbox] consume turn rebuild of session %s failed: %v",- key.SessionID, err,+ "[sandbox] consume turn rebuild of %s failed: %v",+ key, err,)}}+// stateFilter is the provider listing filter that finds key's machine+// without Connect (lookup-only state peeks).+func (l *remoteSessionLifecycle) stateFilter(key SessionSandboxKey) map[string]string {+ if key.IsUserScoped() {+ return map[string]string{remoteMetadataMachineDigest: l.digest(key.Machine())}+ }+ return map[string]string{+ remoteMetadataTenantID: strconv.FormatUint(key.TenantID, 10),+ remoteMetadataSessionID: key.SessionID,+ }+}++// bindingConnectKey is the address connectBinding must use for a winner.+// Per-session bindings stay {tenant, session}. A user-machine binding stores+// an empty session id, so the key is rebuilt from the binding's machine+// identity: connectKnownWinner takes the binding, not the key.+func bindingConnectKey(winner SessionSandboxBinding) SessionSandboxKey {+ if winner.Version != UserSandboxBindingVersion {+ return SessionSandboxKey{TenantID: winner.TenantID, SessionID: winner.SessionID}+ }+ return SessionSandboxKey{+ TenantID: winner.TenantID,+ User: allocation.UserMachine{+ OwnerTenantID: winner.TenantID,+ OwnerID: winner.OwnerID,+ ConfigTenantID: winner.ConfigTenantID,+ ConfigID: winner.ConfigID,+ },+ }+}+func metadataMatches(candidate, required map[string]string) bool {for key, value := range required {if candidate[key] != value {diff --git a/internal/sandbox/session_lifecycle_test.go b/internal/sandbox/session_lifecycle_test.goindex ba0c37472..521bff6f9 100644--- a/internal/sandbox/session_lifecycle_test.go+++ b/internal/sandbox/session_lifecycle_test.go@@ -7,6 +7,8 @@ import ("testing""time"+ "github.com/Tencent/WeKnora/internal/sandbox/admission"+ "github.com/Tencent/WeKnora/internal/sandbox/allocation""github.com/Tencent/WeKnora/internal/types""github.com/stretchr/testify/require")@@ -1356,3 +1358,161 @@ func newTestLifecycleWithConfigID(t *testing.T, configID string) *remoteSessionLrequire.NoError(t, err)return lifecycle}++const (+ userSessionA = "0f8fad5b-d9cb-469f-a165-70867728950e"+ userSessionB = "7c9e6679-7425-40de-944b-e07fc1f90ae7"+)++type userLifecycleFixture struct {+ lifecycle *remoteSessionLifecycle+ bindings *MemorySessionSandboxBindingStore+ client *fakeRemoteClient+ checker *fakeSessionExistenceChecker+ admission *admission.MemoryRegistry+ machine allocation.UserMachine+}++func newUserLifecycleFixture(t *testing.T) *userLifecycleFixture {+ t.Helper()+ client := newFakeRemoteClient(SandboxTypeCube)+ bindings := NewMemorySessionSandboxBindingStore()+ checker := &fakeSessionExistenceChecker{exists: true}+ reg := admission.NewMemory()+ lifecycle, err := newRemoteSessionLifecycle(+ client, bindings, checker,+ RemoteCreateRequest{TemplateID: "template-a", EnvVars: map[string]string{+ skillOutputEnvVar: SessionOutputRoot, sessionInputEnvVar: SessionInputRoot, "KEEP": "1",+ }},+ time.Minute, "cfg-1", nil,+ withUserMachines("weknora", reg),+ )+ require.NoError(t, err)+ return &userLifecycleFixture{+ lifecycle: lifecycle, bindings: bindings, client: client, checker: checker, admission: reg,+ machine: allocation.UserMachine{OwnerTenantID: 42, OwnerID: "u-1", ConfigTenantID: 42, ConfigID: "cfg-1"},+ }+}++func (f *userLifecycleFixture) sessionKey(sessionID string) SessionSandboxKey {+ return SessionSandboxKey{TenantID: 42, SessionID: sessionID, User: f.machine}+}++func TestUserMachineIsSharedBySessions(t *testing.T) {+ f := newUserLifecycleFixture(t)+ ctx := context.Background()+ a, err := f.lifecycle.Resolve(ctx, f.sessionKey(userSessionA))+ require.NoError(t, err)+ b, err := f.lifecycle.Resolve(ctx, f.sessionKey(userSessionB))+ require.NoError(t, err)+ require.Equal(t, a.ID(), b.ID())+ creates, _, _, _, _ := f.client.counts()+ require.Equal(t, 1, creates)++ req := f.client.lastCreateRequest+ require.Equal(t, "user", req.Metadata[remoteMetadataAllocation])+ require.Equal(t, f.machine.Digest("weknora"), req.Metadata[remoteMetadataMachineDigest])+ require.Equal(t, "42", req.Metadata[remoteMetadataTenantID])+ require.NotContains(t, req.Metadata, remoteMetadataSessionID)+ require.NotContains(t, req.EnvVars, skillOutputEnvVar, "session paths are per call on a shared machine")+ require.NotContains(t, req.EnvVars, sessionInputEnvVar)+ require.Equal(t, "1", req.EnvVars["KEEP"])+}++func TestUserMachineSurvivesAMissingSession(t *testing.T) {+ f := newUserLifecycleFixture(t)+ ctx := context.Background()+ first, err := f.lifecycle.Resolve(ctx, f.sessionKey(userSessionA))+ require.NoError(t, err)++ f.checker.exists = false // every session row is gone+ again, err := f.lifecycle.Resolve(ctx, f.sessionKey(userSessionB))+ require.NoError(t, err, "the lifecycle never consults one session to destroy a shared machine")+ require.Equal(t, first.ID(), again.ID())+ _, _, _, _, deletes := f.client.counts()+ require.Zero(t, deletes)+}++func TestUserMachineRecoversFromLostBindingByDigest(t *testing.T) {+ f := newUserLifecycleFixture(t)+ ctx := context.Background()+ first, err := f.lifecycle.Resolve(ctx, f.sessionKey(userSessionA))+ require.NoError(t, err)+ machineKey := f.sessionKey(userSessionA).Machine()+ deleted, err := f.bindings.DeleteIfMatch(ctx, machineKey, SandboxTypeCube, first.ID())+ require.NoError(t, err)+ require.True(t, deleted)++ again, err := f.lifecycle.Resolve(ctx, f.sessionKey(userSessionB))+ require.NoError(t, err)+ require.Equal(t, first.ID(), again.ID(), "metadata recovery must adopt the same machine, not create a second")+}++func TestStaleUserMachineWaitsForOtherSessions(t *testing.T) {+ f := newUserLifecycleFixture(t)+ ctx := context.Background()+ first, err := f.lifecycle.Resolve(ctx, f.sessionKey(userSessionA))+ require.NoError(t, err)+ // InvalidateByConfig's scan does not see user keys; mark the machine the+ // way the manager does (T10).+ n, err := markBindingKeysStale(ctx, f.bindings, []SessionSandboxKey{f.sessionKey(userSessionA).Machine()}, "cfg-1")+ require.NoError(t, err)+ require.Equal(t, 1, n)++ machine := admission.MachineScope(f.machine.Digest("weknora"))+ self := admission.SessionMember(userSessionA, "shell", "self")+ selfLease, err := f.admission.Hold(ctx, machine, self, time.Minute)+ require.NoError(t, err)+ defer selfLease.Release()+ ownTurn := admission.SessionMember(userSessionA, "turn", "own")+ ownTurnLease, err := f.admission.Hold(ctx, machine, ownTurn, time.Minute)+ require.NoError(t, err)+ defer ownTurnLease.Release()+ ctxA := withAdmissionExempt(ctx, self, ownTurn)++ other, err := f.admission.Hold(ctx, machine, admission.SessionMember(userSessionB, "shell", "t"), time.Minute)+ require.NoError(t, err)+ kept, err := f.lifecycle.Resolve(ctxA, f.sessionKey(userSessionA))+ require.NoError(t, err)+ require.Equal(t, first.ID(), kept.ID(), "B is running a command; A's turn must not rebuild under it")+ other.Release()++ ownPTY, err := f.admission.Hold(ctx, machine, admission.SessionMember(userSessionA, "pty", "p"), time.Minute)+ require.NoError(t, err)+ kept, err = f.lifecycle.Resolve(ctxA, f.sessionKey(userSessionA))+ require.NoError(t, err)+ require.Equal(t, first.ID(), kept.ID(), "A's own terminal is not this request; it blocks the rebuild too")+ ownPTY.Release()++ rebuilt, err := f.lifecycle.Resolve(ctxA, f.sessionKey(userSessionA))+ require.NoError(t, err)+ require.NotEqual(t, first.ID(), rebuilt.ID())+ creates, _, _, _, _ := f.client.counts()+ require.Equal(t, 2, creates)+}++func TestStaleRebuildStopsWhenTheBarrierIsLost(t *testing.T) {+ f := newUserLifecycleFixture(t)+ ctx := context.Background()+ first, err := f.lifecycle.Resolve(ctx, f.sessionKey(userSessionA))+ require.NoError(t, err)+ _, err = markBindingKeysStale(ctx, f.bindings, []SessionSandboxKey{f.sessionKey(userSessionA).Machine()}, "cfg-1")+ require.NoError(t, err)++ f.client.deleteHook = func(context.Context, string) error {+ f.admission.LoseBarrier(admission.MachineScope(f.machine.Digest("weknora")))+ return nil+ }+ _, err = f.lifecycle.Resolve(ctx, f.sessionKey(userSessionA))+ require.ErrorIs(t, err, admission.ErrLeaseLost, "after losing the barrier no further destructive step runs")+ _ = first+}++func TestUserKeyWithoutAdmissionIsRefused(t *testing.T) {+ fx := newLifecycleFixture(t) // per-session fixture, no withUserMachines+ _, err := fx.lifecycle.Resolve(context.Background(), SessionSandboxKey{+ TenantID: 42, SessionID: userSessionA,+ User: allocation.UserMachine{OwnerTenantID: 42, OwnerID: "u", ConfigTenantID: 42, ConfigID: "cfg-1"},+ })+ require.ErrorContains(t, err, "admission")+}diff --git a/internal/sandbox/session_manager.go b/internal/sandbox/session_manager.goindex 3a02d7a97..f11488b53 100644--- a/internal/sandbox/session_manager.go+++ b/internal/sandbox/session_manager.go@@ -29,11 +29,14 @@ import ("log""os""path"- "strconv""strings""sync""time"+ "github.com/Tencent/WeKnora/internal/common/redislock"+ "github.com/Tencent/WeKnora/internal/logger"+ "github.com/Tencent/WeKnora/internal/sandbox/admission"+ "github.com/Tencent/WeKnora/internal/sandbox/allocation""github.com/Tencent/WeKnora/internal/sandbox/workspace""github.com/Tencent/WeKnora/internal/types")@@ -100,11 +103,43 @@ type SessionBoundManager struct {// activeType is the effective sandbox type callers observe.activeType SandboxType+ configID string+ allocations AllocationDirectory+ admission admission.Registry+ namespace string++ // addresses caches user-mode assignments for this manager's lifetime+ // (one request). Only the address is trusted from it; validity is read+ // fresh by admitUser.+ addresses sync.Map++ // admissionWait bounds waiting for a closed scope (rebuild, exclusive).+ admissionWait time.Duration+// mu guards Cleanup's idempotency flag.mu sync.RWMutexclosed bool}+// AllocationDirectory resolves a session's recorded placement. Implemented by+// internal/application/service/sandboxalloc; nil keeps every session on the+// historical per-session path.+type AllocationDirectory interface {+ // SessionAllocation is the session's address. The manager caches it for+ // one request; it is never trusted for validity.+ SessionAllocation(ctx context.Context, sessionID string) (allocation.Assignment, bool, error)+ // VerifySessionAllocation reads the database on every call and returns+ // nil only if expected is still the session's live assignment and the+ // session exists, is not retired, and its current owner is expected's+ // owner and still eligible to share. It returns ErrSandboxSessionDeleted+ // or ErrUserAllocationRejected for those cases, other errors for+ // failures to verify.+ VerifySessionAllocation(+ ctx context.Context, sessionTenantID uint64, sessionID string, expected allocation.Assignment,+ ) error+ UserMachinesByConfig(ctx context.Context, configTenantID uint64, configID string) ([]allocation.UserMachine, error)+}+// SessionBoundManagerConfig bundles the wired dependencies. Test helpers and// the production container use it so callers only have to name the moving// parts they actually override.@@ -127,6 +162,12 @@ type SessionBoundManagerConfig struct {// Bootstrapper customises the first sandbox create of individual sessions// (session fork). Optional: nil is the ordinary path.Bootstrapper SessionBootstrapper++ // Allocations, Admission and Namespace enable shared user machines. All+ // three are optional; a user-mode assignment is refused without them.+ Allocations AllocationDirectory+ Admission admission.Registry+ Namespace string}// NewSessionBoundManager wires the manager with an explicit RemoteSandboxClient@@ -196,6 +237,10 @@ func NewSessionBoundManager(deps SessionBoundManagerConfig) (*SessionBoundManagebootstrapper = withClient.WithClient(client)}+ var lifecycleOpts []lifecycleOption+ if deps.Admission != nil {+ lifecycleOpts = append(lifecycleOpts, withUserMachines(deps.Namespace, deps.Admission))+ }lifecycle, err := newRemoteSessionLifecycle(client,deps.Store,@@ -204,20 +249,26 @@ func NewSessionBoundManager(deps SessionBoundManagerConfig) (*SessionBoundManagesessionLifecycleCleanupTimeout,deps.ConfigID,bootstrapper,+ lifecycleOpts...,)if err != nil {return nil, fmt.Errorf("session bound manager: %w", err)}m := &SessionBoundManager{- config: cfg,- validator: NewScriptValidator(),- client: client,- bindings: deps.Store,- checker: deps.Checker,- lifecycle: lifecycle,- ephemeral: NewRemoteSandbox(client, createRequest),- activeType: provider,+ config: cfg,+ validator: NewScriptValidator(),+ client: client,+ bindings: deps.Store,+ checker: deps.Checker,+ lifecycle: lifecycle,+ ephemeral: NewRemoteSandbox(client, createRequest),+ activeType: provider,+ configID: deps.ConfigID,+ allocations: deps.Allocations,+ admission: deps.Admission,+ namespace: deps.Namespace,+ admissionWait: 30 * time.Second,}// Per-tenant managers are rebuilt on every request, so probing here would@@ -304,15 +355,32 @@ func (m *SessionBoundManager) Execute(ctx context.Context, cfg *ExecuteConfig) (if strings.TrimSpace(cfg.SessionID) == "" {return m.ephemeral.Execute(ctx, cfg)}-- handle, err := m.resolveSession(ctx, cfg.SessionID)+ sc, err := m.scope(ctx, cfg.SessionID)if err != nil {return nil, err}- if err := m.ensureSessionWorkspaceDirs(ctx, handle, executionOutputDir(cfg)); err != nil {+ ctx, release, err := m.admitUser(ctx, sc, "exec")+ if err != nil {return nil, err}- return m.ephemeral.ExecuteOnHandle(ctx, handle, cfg)+ defer release()+ handle, err := m.resolveScope(ctx, sc)+ if err != nil {+ return nil, err+ }+ outputDir := executionOutputDir(cfg)+ if sc.user {+ outputDir = sc.layout.OutputDir+ }+ if err := m.prepareSessionDirs(ctx, handle, DefaultSandboxExecUser, sc.layout.InputDir, outputDir); err != nil {+ return nil, err+ }+ if !sc.user {+ return m.ephemeral.ExecuteOnHandle(ctx, handle, cfg)+ }+ userCfg := *cfg+ userCfg.Env = withSessionEnv(cfg.Env, sc.layout)+ return m.ephemeral.ExecuteOnHandleIn(ctx, handle, &userCfg, sc.layout.Root, userScriptDir(sc.layout))}// ensureSessionWorkspaceDirs prepares the shared execution layout as the same@@ -411,11 +479,14 @@ func (m *SessionBoundManager) DestroySession(ctx context.Context, sessionID striif m.remoteDisabled() {return nil}- key, err := m.sessionKey(ctx, sessionID)+ sc, err := m.scope(ctx, sessionID)if err != nil {return err}- return m.lifecycle.Destroy(ctx, key)+ if sc.user {+ return ErrSharedSandbox+ }+ return m.lifecycle.Destroy(ctx, sc.key)}// InvalidateConfigSandboxes marks every session sandbox this config owns stale,@@ -432,7 +503,32 @@ func (m *SessionBoundManager) InvalidateConfigSandboxes(if err := m.requireRemoteBackend(); err != nil {return 0, err}- return m.bindings.InvalidateByConfig(ctx, tenantID, configID)+ marked, err := m.bindings.InvalidateByConfig(ctx, tenantID, configID)+ if m.allocations == nil {+ return marked, err+ }+ // tenantID is the config's owner, so this covers every borrower's machine.+ machines, listErr := m.allocations.UserMachinesByConfig(ctx, tenantID, NormalizeConfigID(configID))+ if listErr != nil {+ return marked, errors.Join(err, fmt.Errorf("list user machines: %w", listErr))+ }+ n, markErr := m.InvalidateConfigSandboxesForMachines(ctx, configID, machines)+ return marked + n, errors.Join(err, markErr)+}++// InvalidateConfigSandboxesForMachines marks the listed user machines stale.+func (m *SessionBoundManager) InvalidateConfigSandboxesForMachines(+ ctx context.Context, configID string, machines []allocation.UserMachine,+) (int, error) {+ scanner, ok := m.bindings.(tenantBindingScanner)+ if !ok {+ return 0, nil+ }+ keys := make([]SessionSandboxKey, 0, len(machines))+ for _, machine := range machines {+ keys = append(keys, m.UserMachineKey(machine))+ }+ return markBindingKeysStale(ctx, scanner, keys, configID)}// CreateSnapshot forwards provider snapshot creation for the live sandbox bound@@ -448,7 +544,16 @@ func (m *SessionBoundManager) CreateSnapshot(if !ok || !m.client.Capabilities().SupportsSnapshots {return RemoteSnapshotRef{}, errors.New("sandbox: remote provider does not support snapshots")}- handle, err := m.resolveSession(ctx, sessionID)+ sc, err := m.scope(ctx, sessionID)+ if err != nil {+ return RemoteSnapshotRef{}, err+ }+ ctx, release, err := m.admitUser(ctx, sc, "exec")+ if err != nil {+ return RemoteSnapshotRef{}, err+ }+ defer release()+ handle, err := m.resolveScope(ctx, sc)if err != nil {return RemoteSnapshotRef{}, err}@@ -499,6 +604,21 @@ func (m *SessionBoundManager) EnsureSessionDir(ctx context.Context, sessionID, dif strings.TrimSpace(dir) == "" {return nil}+ if m.remoteDisabled() || strings.TrimSpace(sessionID) == "" {+ return nil+ }+ sc, err := m.scope(ctx, sessionID)+ if err != nil {+ return err+ }+ if !sc.allowsPath(dir) {+ return fmt.Errorf("sandbox: path %q is outside the session workspace %s", dir, sc.layout.Root)+ }+ ctx, release, err := m.admitUser(ctx, sc, "file")+ if err != nil {+ return err+ }+ defer release()handle, ok, err := m.lookupSessionHandle(ctx, sessionID)if err != nil || !ok {return err@@ -522,11 +642,23 @@ func (m *SessionBoundManager) WriteSessionInputFile(if strings.TrimSpace(sessionID) == "" {return errors.New("sandbox: session ID required for input staging")}+ sc, err := m.scope(ctx, sessionID)+ if err != nil {+ return err+ }clean, err := cleanSessionInputPath(filePath)+ if sc.user {+ clean, err = workspace.CleanInputPathIn(sc.layout, filePath)+ }if err != nil {return err}- handle, err := m.resolveSession(ctx, sessionID)+ ctx, release, err := m.admitUser(ctx, sc, "file")+ if err != nil {+ return err+ }+ defer release()+ handle, err := m.resolveScope(ctx, sc)if err != nil {return err}@@ -566,6 +698,10 @@ func (m *SessionBoundManager) WriteSessionWorkspaceFiles(if len(files) == 0 {return nil}+ sc, err := m.scope(ctx, sessionID)+ if err != nil {+ return err+ }type item struct {path stringcontent []byte@@ -573,18 +709,34 @@ func (m *SessionBoundManager) WriteSessionWorkspaceFiles(items := make([]item, 0, len(files))parents := make(map[string]struct{}, len(files))for _, file := range files {+ // Session mode keeps the historical cleaner, which is pinned to+ // /workspace and ignores a host WEKNORA_SKILL_OUTPUT_DIR overlay.clean, err := cleanSessionWorkspaceWritePath(file.Path)+ if sc.user {+ clean, err = workspace.CleanWriteFileIn(sc.layout, file.Path)+ }if err != nil {return err}items = append(items, item{path: clean, content: file.Content})parents[path.Dir(clean)] = struct{}{}}- handle, err := m.resolveSession(ctx, sessionID)+ ctx, release, err := m.admitUser(ctx, sc, "file")+ if err != nil {+ return err+ }+ defer release()+ handle, err := m.resolveScope(ctx, sc)if err != nil {return err}- if err := m.ensureSessionWorkspaceDirs(ctx, handle, SessionOutputRoot); err != nil {+ if sc.user {+ if err := m.prepareSessionDirs(+ ctx, handle, DefaultSandboxExecUser, sc.layout.InputDir, sc.layout.OutputDir,+ ); err != nil {+ return err+ }+ } else if err := m.ensureSessionWorkspaceDirs(ctx, handle, SessionOutputRoot); err != nil {return err}for parent := range parents {@@ -611,10 +763,28 @@ func (m *SessionBoundManager) RemoveSessionInputPath(if err := m.requireRemoteBackend(); err != nil {return err}+ if strings.TrimSpace(sessionID) == "" {+ if _, err := cleanSessionInputPath(targetPath); err != nil {+ return err+ }+ return nil+ }+ sc, err := m.scope(ctx, sessionID)+ if err != nil {+ return err+ }clean, err := cleanSessionInputPath(targetPath)+ if sc.user {+ clean, err = workspace.CleanInputPathIn(sc.layout, targetPath)+ }if err != nil {return err}+ ctx, release, err := m.admitUser(ctx, sc, "file")+ if err != nil {+ return err+ }+ defer release()handle, ok, err := m.lookupSessionHandle(ctx, sessionID)if err != nil || !ok {return err@@ -634,6 +804,21 @@ func (m *SessionBoundManager) ListSessionFiles(if strings.TrimSpace(dir) == "" {return nil, errors.New("sandbox: dir required for ListSessionFiles")}+ if m.remoteDisabled() || strings.TrimSpace(sessionID) == "" {+ return nil, nil+ }+ sc, err := m.scope(ctx, sessionID)+ if err != nil {+ return nil, err+ }+ if !sc.allowsPath(dir) {+ return nil, fmt.Errorf("sandbox: path %q is outside the session workspace %s", dir, sc.layout.Root)+ }+ ctx, release, err := m.admitUser(ctx, sc, "file")+ if err != nil {+ return nil, err+ }+ defer release()handle, ok, err := m.lookupSessionHandle(ctx, sessionID)if err != nil || !ok {return nil, err@@ -651,6 +836,21 @@ func (m *SessionBoundManager) StatSessionFile(if strings.TrimSpace(filePath) == "" {return nil, errors.New("sandbox: path required for StatSessionFile")}+ if m.remoteDisabled() || strings.TrimSpace(sessionID) == "" {+ return nil, fmt.Errorf("sandbox: no live sandbox for session %s", sessionID)+ }+ sc, err := m.scope(ctx, sessionID)+ if err != nil {+ return nil, err+ }+ if !sc.allowsPath(filePath) {+ return nil, fmt.Errorf("sandbox: path %q is outside the session workspace %s", filePath, sc.layout.Root)+ }+ ctx, release, err := m.admitUser(ctx, sc, "file")+ if err != nil {+ return nil, err+ }+ defer release()handle, ok, err := m.lookupSessionHandle(ctx, sessionID)if err != nil {return nil, err@@ -669,6 +869,21 @@ func (m *SessionBoundManager) ReadSessionFile(if strings.TrimSpace(filePath) == "" {return nil, errors.New("sandbox: path required for ReadSessionFile")}+ if m.remoteDisabled() || strings.TrimSpace(sessionID) == "" {+ return nil, fmt.Errorf("sandbox: no live sandbox for session %s", sessionID)+ }+ sc, err := m.scope(ctx, sessionID)+ if err != nil {+ return nil, err+ }+ if !sc.allowsPath(filePath) {+ return nil, fmt.Errorf("sandbox: path %q is outside the session workspace %s", filePath, sc.layout.Root)+ }+ ctx, release, err := m.admitUser(ctx, sc, "file")+ if err != nil {+ return nil, err+ }+ defer release()handle, ok, err := m.lookupSessionHandle(ctx, sessionID)if err != nil {return nil, err@@ -692,12 +907,21 @@ func (m *SessionBoundManager) WriteSessionFile(if strings.TrimSpace(sessionID) == "" {return errors.New("sandbox: session ID required for file staging")}+ sc, err := m.scope(ctx, sessionID)+ if err != nil {+ return err+ }clean := path.Clean(strings.TrimSpace(filePath))if clean != SkillsImageRoot && !strings.HasPrefix(clean, SkillsImageRoot+"/") {return fmt.Errorf("sandbox: install file path %q is outside %s", filePath, SkillsImageRoot)}+ ctx, release, err := m.admitUser(ctx, sc, "file")+ if err != nil {+ return err+ }+ defer release()ctx = withMaintenanceFilesystem(ctx)- handle, err := m.resolveSession(ctx, sessionID)+ handle, err := m.resolveScope(ctx, sc)if err != nil {return err}@@ -784,11 +1008,7 @@ type ShellOutputSnapshot struct {func (m *SessionBoundManager) ExecShellCommandWithOutputSnapshot(ctx context.Context, sessionID, command string, opts ShellExecOptions, outputDir string,) (*ExecuteResult, *ShellOutputSnapshot, error) {- clean, ok := ValidatedSessionOutputDir(outputDir)- if !ok {- return nil, nil, errors.New("sandbox: invalid output directory")- }- return m.execShellCommandWithOutputSnapshot(ctx, sessionID, command, opts, clean)+ return m.execShellCommandWithOutputSnapshot(ctx, sessionID, command, opts, outputDir)}func (m *SessionBoundManager) execShellCommandWithOutputSnapshot(@@ -814,30 +1034,55 @@ func (m *SessionBoundManager) execShellCommandWithOutputSnapshot(timeout = DefaultTimeout}+ sc, err := m.scope(ctx, sessionID)+ if err != nil {+ return nil, nil, err+ }+ if outputDir != "" {+ clean, ok := ValidatedOutputDirIn(sc.layout, outputDir)+ if !sc.user {+ clean, ok = ValidatedSessionOutputDir(outputDir)+ }+ if !ok {+ return nil, nil, errors.New("sandbox: invalid output directory")+ }+ outputDir = clean+ }workDir := strings.TrimSpace(opts.WorkDir)if workDir == "" {- workDir = SessionWorkspaceRoot+ workDir = sc.layout.Root+ }+ workDir, err = workspace.CleanWorkDirIn(sc.layout, workDir, opts.AllowSkillsRoot)+ if !sc.user {+ fallback := SessionWorkspaceRoot+ if strings.TrimSpace(opts.WorkDir) != "" {+ fallback = strings.TrimSpace(opts.WorkDir)+ }+ workDir, err = cleanSessionWorkDir(fallback, opts.AllowSkillsRoot)+ }+ if err != nil {+ return nil, nil, err}- workDir, err := cleanSessionWorkDir(workDir, opts.AllowSkillsRoot)+ ctx, release, err := m.admitUser(ctx, sc, "shell")if err != nil {return nil, nil, err}+ defer release()+var handle RemoteSandboxHandleif opts.ExpectedSandboxID != "" {- key, keyErr := m.sessionKey(ctx, sessionID)- if keyErr != nil {- return nil, nil, keyErr- }- exists, checkErr := m.checker.SessionExists(ctx, key)- if checkErr != nil {- return nil, nil, checkErr- }- if !exists {- return nil, nil, ErrSandboxSessionDeleted+ if !sc.user {+ exists, checkErr := m.checker.SessionExists(ctx, sc.sessionKeyOnly())+ if checkErr != nil {+ return nil, nil, checkErr+ }+ if !exists {+ return nil, nil, ErrSandboxSessionDeleted+ }}var found bool// Maintenance must recheck the binding even inside a file-operation scope.- handle, found, err = m.lookupSessionHandleForKey(ctx, key, opts.ExpectedSandboxID)+ handle, found, err = m.lookupSessionHandleForKey(ctx, sc.machine(), opts.ExpectedSandboxID)if err != nil {return nil, nil, err}@@ -845,7 +1090,7 @@ func (m *SessionBoundManager) execShellCommandWithOutputSnapshot(return nil, nil, errors.New("sandbox: bound sandbox changed before maintenance command")}} else {- handle, err = m.resolveSession(ctx, sessionID)+ handle, err = m.resolveScope(ctx, sc)if err != nil {return nil, nil, err}@@ -867,21 +1112,29 @@ func (m *SessionBoundManager) execShellCommandWithOutputSnapshot(if err := m.prepareSessionDirs(ctx, handle, user, workDir); err != nil {return nil, nil, err}- } else {+ } else if sc.user {if err := m.prepareSessionDirs(- ctx, handle, user, SessionInputRoot, SessionOutputRoot, workDir,+ ctx, handle, user, sc.layout.InputDir, sc.layout.OutputDir, workDir,); err != nil {return nil, nil, err}+ } else if err := m.prepareSessionDirs(+ ctx, handle, user, SessionInputRoot, SessionOutputRoot, workDir,+ ); err != nil {+ return nil, nil, err}}+ env := opts.Env+ if sc.user {+ env = withSessionEnv(opts.Env, sc.layout)+ }start := time.Now()execResult, execErr := m.client.Exec(ctx, handle, RemoteExecRequest{Command: command,OnOutput: commandOutputCallback(ctx, opts.OnOutput),Shell: true,- Env: opts.Env,+ Env: env,WorkDir: workDir,User: user,Timeout: timeout,@@ -914,12 +1167,20 @@ func (m *SessionBoundManager) SessionInstallShellExecutor() SessionInstallShellEreturn m}-// SessionWorkspaceLayout reports the /workspace contract every remote-// session shares. sessionID is ignored: remote layouts are not per-session.-// A validated WEKNORA_SKILL_OUTPUT_DIR overlays OutputDir and the matching-// ReadRoots entry; RemoteWorkspaceLayout itself stays the constant baseline.-func (m *SessionBoundManager) SessionWorkspaceLayout(context.Context, string) (WorkspaceLayout, error) {- return withValidatedSkillOutputDir(RemoteWorkspaceLayout()), nil+// SessionWorkspaceLayout reports where sessionID's files live. A session-owned+// machine keeps the /workspace contract (a validated WEKNORA_SKILL_OUTPUT_DIR+// overlays OutputDir). A shared machine reports that session's directory.+// Lookups do not admit and do not require a tenant: tool descriptions call+// this before any provider operation.+func (m *SessionBoundManager) SessionWorkspaceLayout(ctx context.Context, sessionID string) (WorkspaceLayout, error) {+ a, ok, err := m.addressFor(ctx, strings.TrimSpace(sessionID))+ if err != nil {+ return WorkspaceLayout{}, err+ }+ if !ok || !a.IsUser() {+ return withValidatedSkillOutputDir(RemoteWorkspaceLayout()), nil+ }+ return a.Layout()}func withValidatedSkillOutputDir(layout WorkspaceLayout) WorkspaceLayout {@@ -991,19 +1252,103 @@ func (m *SessionBoundManager) OpenSessionTerminal(if !ok {return nil, ErrTerminalUnsupported}- if !opts.AllowResume {- if err := m.RequireRunningSessionSandbox(ctx, sessionID); err != nil {- return nil, err- }+ sc, err := m.scope(ctx, sessionID)+ if err != nil {+ return nil, err}- handle, found, err := m.lookupSessionHandle(ctx, sessionID)+ if sc.user {+ if strings.TrimSpace(opts.Cwd) == "" {+ opts.Cwd = sc.layout.Root+ }+ opts.Envs = withSessionEnv(opts.Envs, sc.layout)+ // C10: PIDs on a shared machine belong to every session of the+ // owner; a client-supplied PID could attach another session's shell.+ opts.AttachPID = 0+ }+ // The PTY outlives this call: admit it on a context detached from the+ // HTTP request; heldTerminalSession releases it on Close.+ opCtx, release, err := m.admitUser(context.WithoutCancel(ctx), sc, "pty")if err != nil {return nil, err}- if !found {- return nil, ErrNoLiveSessionSandbox+ // The dial and the stream both stop if the lease is lost. callCtx lives+ // as long as the terminal: cancelling it when this function returns+ // would close a PTY that has only just opened.+ callCtx, cancelCall := context.WithCancelCause(ctx)+ stopCall := context.AfterFunc(opCtx, func() { cancelCall(context.Cause(opCtx)) })+ abandon := func(err error) (RemoteTerminalSession, error) {+ stopCall()+ cancelCall(err)+ release()+ return nil, err+ }+ if !opts.AllowResume {+ if err := m.RequireRunningSessionSandbox(callCtx, sessionID); err != nil {+ return abandon(err)+ }+ }+ handle, found, err := m.lookupSessionHandle(callCtx, sessionID)+ if err != nil || !found {+ if err != nil {+ return abandon(err)+ }+ return abandon(ErrNoLiveSessionSandbox)}- return terminal.OpenTerminal(ctx, handle, opts)+ session, err := terminal.OpenTerminal(callCtx, handle, opts)+ if err != nil {+ return abandon(err)+ }+ if !sc.user {+ stopCall()+ release()+ return session, nil+ }+ held := &heldTerminalSession{+ RemoteTerminalSession: session,+ release: release,+ cancelStream: cancelCall,+ stopLeaseWatch: stopCall,+ }+ if exempt := admissionExemptFrom(opCtx); len(exempt) > 0 {+ held.member = exempt[len(exempt)-1]+ }+ // C3: once the lease is lost another replica may rebuild or reclaim the+ // machine, so the PTY must not keep writing to it.+ held.stopWatch = context.AfterFunc(opCtx, func() { _ = held.Close() })+ return held, nil+}++// heldTerminalSession keeps the shared machine's admission for as long as+// the PTY is attached, so a rebuild or reclaim waits for the user to close+// the terminal.+type heldTerminalSession struct {+ RemoteTerminalSession+ once sync.Once+ closeErr error+ release func()+ cancelStream func(error)+ stopLeaseWatch func() bool+ stopWatch func() bool+ member string // the PTY's admission member; tests use it to inject loss+}++// Close closes the PTY and releases its admission. It runs at most once,+// whether the user closed the terminal or the lease was lost.+func (s *heldTerminalSession) Close() error {+ s.once.Do(func() {+ if s.stopWatch != nil {+ s.stopWatch()+ }+ if s.stopLeaseWatch != nil {+ s.stopLeaseWatch()+ }+ s.closeErr = s.RemoteTerminalSession.Close()+ if s.cancelStream != nil {+ s.cancelStream(context.Canceled)+ }+ s.release()+ })+ return s.closeErr}var _ SessionTerminalProvider = (*SessionBoundManager)(nil)@@ -1119,39 +1464,46 @@ func (m *SessionBoundManager) Cleanup(_ context.Context) error {// --- internal helpers ---------------------------------------------------------// BeginSessionTurn opens the chat-turn lease for sessionID. The first-// resolve after this may rebuild a stale image; later resolves of the same-// turn keep the sandbox.-func (m *SessionBoundManager) BeginSessionTurn(ctx context.Context, sessionID string) error {+// BeginSessionTurn opens one chat-turn hold for sessionID. The first resolve+// after this may rebuild a stale image; later resolves of the same turn keep+// the sandbox. end closes exactly this hold.+func (m *SessionBoundManager) BeginSessionTurn(ctx context.Context, sessionID string) (context.Context, func(), error) {+ noop := func() {}if m == nil {- return nil+ return ctx, noop, nil}leaser, ok := m.bindings.(sessionTurnLeaseStore)if !ok {- return nil+ return ctx, noop, nil}- key, err := m.sessionKey(ctx, sessionID)+ sc, err := m.scope(ctx, sessionID)if err != nil {- return err+ return nil, nil, err}- return leaser.BeginTurn(ctx, key)-}--// EndSessionTurn closes the chat-turn lease. It ignores request cancellation-// so a disconnected client still releases the lease.-func (m *SessionBoundManager) EndSessionTurn(ctx context.Context, sessionID string) error {- if m == nil {- return nil+ if err := leaser.BeginTurn(ctx, sc.key); err != nil {+ return nil, nil, err}- leaser, ok := m.bindings.(sessionTurnLeaseStore)- if !ok {- return nil+ endLease := func() {+ if err := leaser.EndTurn(context.WithoutCancel(ctx), sc.key); err != nil {+ logger.Warnf(ctx, "[sandbox] end turn for session %s failed: %v", sessionID, err)+ }}- key, err := m.sessionKey(ctx, sessionID)+ if !sc.user {+ var once sync.Once+ return ctx, func() { once.Do(endLease) }, nil+ }+ turnCtx, release, err := m.admitUser(ctx, sc, "turn")if err != nil {- return err+ endLease()+ return nil, nil, err}- return leaser.EndTurn(context.WithoutCancel(ctx), key)+ var once sync.Once+ return turnCtx, func() {+ once.Do(func() {+ release()+ endLease()+ })+ }, nil}// HasActiveTurn reports whether an agent turn currently holds the session's@@ -1165,11 +1517,11 @@ func (m *SessionBoundManager) HasActiveTurn(ctx context.Context, sessionID strinif !ok {return false, nil}- key, err := m.sessionKey(ctx, sessionID)+ sc, err := m.scope(ctx, sessionID)if err != nil {return false, err}- active, _, err := leaser.TurnState(ctx, key)+ active, _, err := leaser.TurnState(ctx, sc.key)return active, err}@@ -1187,11 +1539,11 @@ func (m *SessionBoundManager) TryLockRewind(ctx context.Context, sessionID strinif !ok {return noop, nil}- key, err := m.sessionKey(ctx, sessionID)+ sc, err := m.scope(ctx, sessionID)if err != nil {return nil, err}- return locker.TryLockRewind(ctx, key)+ return locker.TryLockRewind(ctx, sc.key)}// HasRewindLock reports whether rewind currently holds sessionID.@@ -1205,11 +1557,11 @@ func (m *SessionBoundManager) HasRewindLock(ctx context.Context, sessionID strinif !ok {return false, nil}- key, err := m.sessionKey(ctx, sessionID)+ sc, err := m.scope(ctx, sessionID)if err != nil {return false, err}- return reader.HasRewindLock(ctx, key)+ return reader.HasRewindLock(ctx, sc.key)}// CreateForkSnapshot snapshots the session's already-bound sandbox. It never@@ -1225,6 +1577,13 @@ func (m *SessionBoundManager) CreateForkSnapshot(if !ok || !m.client.Capabilities().SupportsSnapshots {return "", errors.New("sandbox: remote provider does not support snapshots")}+ sc, err := m.scope(ctx, sessionID)+ if err != nil {+ return "", err+ }+ if sc.user {+ return "", errors.New("sandbox: shared machines are forked by copying the session workspace")+ }sandboxID, bound := m.BoundSandboxID(ctx, sessionID)if !bound || sandboxID == "" {return "", errors.New("sandbox: session has no bound sandbox")@@ -1267,11 +1626,11 @@ func (m *SessionBoundManager) resolveSession(ctx context.Context,sessionID string,) (RemoteSandboxHandle, error) {- key, err := m.sessionKey(ctx, sessionID)+ sc, err := m.scope(ctx, sessionID)if err != nil {return nil, err}- return m.lifecycle.Resolve(ctx, key)+ return m.resolveScope(ctx, sc)}// peekBoundSandboxState reads provider listing for the bound sandbox without@@ -1286,10 +1645,11 @@ func (m *SessionBoundManager) peekBoundSandboxState(if m.remoteDisabled() || strings.TrimSpace(sessionID) == "" {return "", false, nil}- key, err := m.sessionKey(ctx, sessionID)+ sc, err := m.scope(ctx, sessionID)if err != nil {return "", false, err}+ key := sc.machine()binding, err := m.bindings.Get(ctx, key)if err != nil {return "", false, fmt.Errorf("sandbox: read session binding: %w", err)@@ -1298,10 +1658,7 @@ func (m *SessionBoundManager) peekBoundSandboxState(return "", false, nil}summaries, err := m.client.List(ctx, RemoteListFilter{- Metadata: map[string]string{- remoteMetadataTenantID: strconv.FormatUint(key.TenantID, 10),- remoteMetadataSessionID: key.SessionID,- },+ Metadata: m.lifecycle.stateFilter(sc.key),States: []RemoteSandboxState{RemoteStateRunning,RemoteStatePaused,@@ -1331,11 +1688,11 @@ func (m *SessionBoundManager) BoundSandboxID(if m.remoteDisabled() || strings.TrimSpace(sessionID) == "" {return "", false}- key, err := m.sessionKey(ctx, sessionID)+ sc, err := m.scope(ctx, sessionID)if err != nil {return "", false}- binding, err := m.bindings.Get(ctx, key)+ binding, err := m.bindings.Get(ctx, sc.machine())if err != nil {return "", false}@@ -1359,10 +1716,11 @@ func (m *SessionBoundManager) lookupSessionHandle(if m.remoteDisabled() || strings.TrimSpace(sessionID) == "" {return nil, false, nil}- key, err := m.sessionKey(ctx, sessionID)+ sc, err := m.scope(ctx, sessionID)if err != nil {return nil, false, err}+ key := sc.machine()// Serialise scope lookups so even concurrent reads connect only once.// The scope is owned by one operation, not by this long-lived manager.if scope, ok := ctx.Value(sessionFileOperationKey{}).(*sessionFileOperation); ok {@@ -1456,6 +1814,339 @@ func (m *SessionBoundManager) walkSessionFiles(return files, nil}+// sessionScope is where one session's state lives.+type sessionScope struct {+ key SessionSandboxKey+ layout WorkspaceLayout+ assignment allocation.Assignment+ user bool+}++func (s sessionScope) machine() SessionSandboxKey { return s.key.Machine() }++func (s sessionScope) sessionKeyOnly() SessionSandboxKey {+ return SessionSandboxKey{TenantID: s.key.TenantID, SessionID: s.key.SessionID}+}++func (s sessionScope) digest(namespace string) string { return s.key.User.Digest(namespace) }++// addressFor returns the session's recorded placement. User-mode rows are+// cached as addresses; session-mode rows and misses are not, because a turn+// may record the placement after this manager was built.+func (m *SessionBoundManager) addressFor(ctx context.Context, sessionID string) (allocation.Assignment, bool, error) {+ if m == nil || m.allocations == nil || strings.TrimSpace(sessionID) == "" {+ return allocation.Assignment{}, false, nil+ }+ if cached, ok := m.addresses.Load(sessionID); ok {+ return cached.(allocation.Assignment), true, nil+ }+ a, ok, err := m.allocations.SessionAllocation(ctx, sessionID)+ if err != nil {+ return allocation.Assignment{}, false, fmt.Errorf("sandbox: read session allocation: %w", err)+ }+ if ok && a.IsUser() {+ m.addresses.Store(sessionID, a)+ }+ return a, ok, nil+}++// scope resolves where sessionID's state lives. Every exported per-session+// method goes through here.+func (m *SessionBoundManager) scope(ctx context.Context, sessionID string) (sessionScope, error) {+ key, err := m.sessionKey(ctx, sessionID)+ if err != nil {+ return sessionScope{}, err+ }+ a, ok, err := m.addressFor(ctx, key.SessionID)+ if err != nil {+ return sessionScope{}, err+ }+ if !ok || !a.IsUser() {+ return sessionScope{key: key, layout: withValidatedSkillOutputDir(RemoteWorkspaceLayout()), assignment: a}, nil+ }+ // Eligibility is not in the assignment (C17); admitUser verifies it+ // from the session row before any provider call.+ if a.Machine.OwnerTenantID != key.TenantID {+ return sessionScope{}, fmt.Errorf("sandbox: session %s belongs to tenant %d, not %d",+ key.SessionID, a.Machine.OwnerTenantID, key.TenantID)+ }+ if NormalizeConfigID(a.ConfigID) != NormalizeConfigID(m.configID) {+ return sessionScope{}, fmt.Errorf("sandbox: session %s is assigned to config %s, not %s",+ key.SessionID, a.ConfigID, m.configID)+ }+ if m.admission == nil {+ return sessionScope{}, errors.New("sandbox: a user-mode session requires the admission registry")+ }+ layout, err := a.Layout()+ if err != nil {+ return sessionScope{}, err+ }+ machine, _ := a.UserMachine()+ return sessionScope{+ key: SessionSandboxKey{TenantID: key.TenantID, SessionID: key.SessionID, User: machine},+ layout: layout,+ assignment: a,+ user: true,+ }, nil+}++type exclusiveSessionKey struct{}++// withExclusiveSession marks ctx as the holder of scope's exclusive window,+// so the holder's own operations skip the session lease it closed.+func withExclusiveSession(ctx context.Context, scope admission.Scope) context.Context {+ return context.WithValue(ctx, exclusiveSessionKey{}, scope)+}++func holdsExclusive(ctx context.Context, scope admission.Scope) bool {+ held, ok := ctx.Value(exclusiveSessionKey{}).(admission.Scope)+ return ok && held == scope+}++// holdWithWait registers member on scope, waiting out a closed scope for at+// most admissionWait.+func (m *SessionBoundManager) holdWithWait(+ ctx context.Context, scope admission.Scope, member string,+) (admission.Lease, error) {+ deadline := time.Now().Add(m.admissionWait)+ for {+ lease, err := m.admission.Hold(ctx, scope, member, admission.DefaultHoldTTL)+ if err == nil {+ return lease, nil+ }+ if !errors.Is(err, admission.ErrClosed) || time.Now().After(deadline) {+ return nil, fmt.Errorf("sandbox: shared machine is busy: %w", err)+ }+ select {+ case <-ctx.Done():+ return nil, ctx.Err()+ case <-time.After(250 * time.Millisecond):+ }+ }+}++// verifyLive re-reads, from the database, everything that makes the cached+// address usable now (C4, C15): the same live assignment, and a session that+// exists, is not retired, and whose current owner is the assignment's owner.+func (m *SessionBoundManager) verifyLive(ctx context.Context, sc sessionScope) error {+ if err := m.allocations.VerifySessionAllocation(ctx, sc.key.TenantID, sc.key.SessionID, sc.assignment); err != nil {+ if errors.Is(err, ErrSandboxSessionDeleted) || errors.Is(err, ErrUserAllocationRejected) {+ return err+ }+ return fmt.Errorf("sandbox: verify session allocation: %w", err)+ }+ return nil+}++// admitUser registers one operation of a user-mode session (C4): machine+// lease, then session lease, then a fresh validity check. The returned+// context is cancelled when either lease is lost (C3); pass it to every+// provider call of the operation. It adds this operation's member to the+// exemptions already on ctx (a turn's own members), never a member guessed+// from the session id (C6). Session-mode scopes pass through untouched.+func (m *SessionBoundManager) admitUser(+ ctx context.Context, sc sessionScope, kind string,+) (context.Context, func(), error) {+ if !sc.user {+ return ctx, func() {}, nil+ }+ token, err := redislock.NewToken()+ if err != nil {+ return nil, nil, err+ }+ member := admission.SessionMember(sc.key.SessionID, kind, token)+ digest := sc.digest(m.namespace)+ machineLease, err := m.holdWithWait(ctx, admission.MachineScope(digest), member)+ if err != nil {+ return nil, nil, err+ }+ leases := []admission.Lease{machineLease}+ sessionLeaseScope := admission.SessionScope(digest, sc.key.SessionID)+ if !holdsExclusive(ctx, sessionLeaseScope) {+ sessionLease, err := m.holdWithWait(ctx, sessionLeaseScope, member)+ if err != nil {+ machineLease.Release()+ if errors.Is(err, admission.ErrClosed) {+ return nil, nil, fmt.Errorf("%w: %v", ErrSessionWorkspaceBusy, err)+ }+ return nil, nil, err+ }+ leases = append(leases, sessionLease)+ }+ releaseLeases := func() {+ for i := len(leases) - 1; i >= 0; i-- {+ leases[i].Release()+ }+ }+ if err := m.verifyLive(ctx, sc); err != nil {+ releaseLeases()+ return nil, nil, err+ }+ signals := make([]admission.Signal, 0, len(leases))+ for _, l := range leases {+ signals = append(signals, l)+ }+ opCtx, stop := admission.Bind(ctx, signals...)+ // Exempt this operation; a turn's own members arrive through ctx.+ opCtx = withAdmissionExempt(opCtx, member)+ var once sync.Once+ return opCtx, func() {+ once.Do(func() {+ stop()+ releaseLeases()+ })+ }, nil+}++// acquireExclusive closes sessionID's session scope and waits (bounded) for+// its registered operations to leave. The returned context lets this+// caller's own operations run inside the window.+func (m *SessionBoundManager) acquireExclusive(+ ctx context.Context, digest, sessionID string, wait time.Duration,+) (context.Context, admission.Barrier, error) {+ scope := admission.SessionScope(digest, sessionID)+ barrier, err := m.admission.Close(ctx, scope, admission.DefaultHoldTTL)+ if errors.Is(err, admission.ErrClosed) {+ return nil, nil, ErrSessionWorkspaceBusy+ }+ if err != nil {+ return nil, nil, err+ }+ deadline := time.Now().Add(wait)+ for {+ n, err := m.admission.ActiveExcept(ctx, scope)+ if err != nil {+ barrier.Release()+ return nil, nil, err+ }+ if n == 0 {+ break+ }+ if time.Now().After(deadline) {+ barrier.Release()+ return nil, nil, ErrSessionWorkspaceBusy+ }+ select {+ case <-ctx.Done():+ barrier.Release()+ return nil, nil, ctx.Err()+ case <-time.After(100 * time.Millisecond):+ }+ }+ return withExclusiveSession(ctx, scope), barrier, nil+}++// AcquireSessionExclusive is acquireExclusive for one user-mode session.+// Use the returned context for the exclusive work; it is cancelled with+// admission.ErrLeaseLost if the window is lost. Call the function to end the+// window. Session-mode sessions get ctx back unchanged.+func (m *SessionBoundManager) AcquireSessionExclusive(+ ctx context.Context, sessionID string,+) (context.Context, func(), error) {+ return m.acquireSessionExclusive(ctx, sessionID, m.admissionWait)+}++func (m *SessionBoundManager) acquireSessionExclusive(+ ctx context.Context, sessionID string, wait time.Duration,+) (context.Context, func(), error) {+ sc, err := m.scope(ctx, sessionID)+ if err != nil {+ return nil, nil, err+ }+ if !sc.user {+ return ctx, func() {}, nil+ }+ // C4: the exclusive window is itself a machine member. Rebuild and reclaim+ // only count the machine scope, so a session barrier alone does not stop+ // them deleting the machine under a rewind or a directory delete.+ token, err := redislock.NewToken()+ if err != nil {+ return nil, nil, err+ }+ digest := sc.digest(m.namespace)+ lease, err := m.holdWithWait(ctx, admission.MachineScope(digest),+ admission.SessionMember(sc.key.SessionID, "exclusive", token))+ if err != nil {+ return nil, nil, err+ }+ exCtx, barrier, err := m.acquireExclusive(ctx, digest, sc.key.SessionID, wait)+ if err != nil {+ lease.Release()+ return nil, nil, err+ }+ // The machine lease stays out of the exemption list: this request's own+ // later exec must not rebuild the machine out from under the window.+ bound, stop := admission.Bind(exCtx, lease, barrier)+ var once sync.Once+ return bound, func() {+ once.Do(func() {+ stop()+ lease.Release()+ barrier.Release()+ })+ }, nil+}++// HoldSessionOperation registers a long-lived operation (desktop relay) of+// sessionID; ctx is cancelled if the lease is lost. A no-op for per-session+// machines.+func (m *SessionBoundManager) HoldSessionOperation(+ ctx context.Context, sessionID, kind string,+) (context.Context, func(), error) {+ sc, err := m.scope(ctx, sessionID)+ if err != nil {+ return nil, nil, err+ }+ return m.admitUser(ctx, sc, kind)+}++// resolveScope resolves (or lazily creates) the scope's machine. Callers+// admit user-mode scopes first; a deleted user-mode session never reaches+// the lifecycle, which never destroys a shared machine for one session.+func (m *SessionBoundManager) resolveScope(ctx context.Context, sc sessionScope) (RemoteSandboxHandle, error) {+ return m.lifecycle.Resolve(ctx, sc.key)+}++// Namespace is the deployment namespace user machine digests use.+func (m *SessionBoundManager) Namespace() string { return m.namespace }++// UserMachineKey is the machine-level key of a shared user machine.+func (m *SessionBoundManager) UserMachineKey(machine allocation.UserMachine) SessionSandboxKey {+ return SessionSandboxKey{TenantID: machine.OwnerTenantID, User: machine}+}++// withSessionEnv overlays the session's input/output paths. On a shared+// machine they are reserved: no config or caller value may redirect them.+func withSessionEnv(env map[string]string, layout WorkspaceLayout) map[string]string {+ out := make(map[string]string, len(env)+2)+ for k, v := range env {+ out[k] = v+ }+ out[skillOutputEnvVar] = layout.OutputDir+ out[sessionInputEnvVar] = layout.InputDir+ return out+}++// allowsPath reports whether a shared-machine file access stays in the+// session's scope (its read roots, /tmp, the skills image).+func (s sessionScope) allowsPath(p string) bool {+ if !s.user {+ return true+ }+ clean := path.Clean(strings.TrimSpace(p))+ for _, root := range append(append([]string{}, s.layout.ReadRoots...), s.layout.WorkDirRoots...) {+ if workspace.IsUnder(clean, root) {+ return true+ }+ }+ return false+}++// userScriptDir is where an uploaded skill script lands on a shared machine.+func userScriptDir(layout WorkspaceLayout) string {+ return path.Join(layout.Root, ".weknora", "scripts")+}+// sessionKey resolves the tenant-scoped binding key. Tenant ID comes from the// request context; empty tenant is treated as a caller error to keep session// bindings globally addressable in Redis.diff --git a/internal/sandbox/session_manager_test.go b/internal/sandbox/session_manager_test.goindex b5cb72b92..fda70b7a1 100644--- a/internal/sandbox/session_manager_test.go+++ b/internal/sandbox/session_manager_test.go@@ -3,12 +3,20 @@ package sandboximport ("context""errors"+ "os"+ "path/filepath""strings"+ "sync""testing""time"+ "github.com/alicebob/miniredis/v2"+ "github.com/redis/go-redis/v9""github.com/stretchr/testify/require"+ "github.com/Tencent/WeKnora/internal/logger"+ "github.com/Tencent/WeKnora/internal/sandbox/admission"+ "github.com/Tencent/WeKnora/internal/sandbox/allocation""github.com/Tencent/WeKnora/internal/types")@@ -364,9 +372,10 @@ func TestSessionBoundManagerEndSessionTurnIgnoresCancel(t *testing.T) {require.NoError(t, err)cancelled, cancel := context.WithCancel(ctx)- require.NoError(t, mgr.BeginSessionTurn(cancelled, "sess-1"))+ _, end, err := mgr.BeginSessionTurn(cancelled, "sess-1")+ require.NoError(t, err)cancel()- require.NoError(t, mgr.EndSessionTurn(cancelled, "sess-1"))+ end()active, _, err := store.TurnState(ctx, SessionSandboxKey{TenantID: 10000, SessionID: "sess-1"})require.NoError(t, err)@@ -633,7 +642,9 @@ func TestHasActiveTurnReportsLeaseState(t *testing.T) {require.NoError(t, err)require.False(t, busy)- require.NoError(t, mgr.BeginSessionTurn(ctx, "sess-1"))+ _, endTurn, err := mgr.BeginSessionTurn(ctx, "sess-1")+ require.NoError(t, err)+ defer endTurn()busy, err = mgr.HasActiveTurn(ctx, "sess-1")require.NoError(t, err)require.True(t, busy)@@ -679,9 +690,12 @@ func TestBeginSessionTurnFailsWhenRewindLocked(t *testing.T) {held, err := mgr.HasRewindLock(ctx, "sess-1")require.NoError(t, err)require.True(t, held)- require.ErrorIs(t, mgr.BeginSessionTurn(ctx, "sess-1"), ErrSessionRewindLocked)+ _, _, err = mgr.BeginSessionTurn(ctx, "sess-1")+ require.ErrorIs(t, err, ErrSessionRewindLocked)unlock()- require.NoError(t, mgr.BeginSessionTurn(ctx, "sess-1"))+ _, endTurn, err := mgr.BeginSessionTurn(ctx, "sess-1")+ require.NoError(t, err)+ endTurn()}func TestTryLockRewindFailsWhenTurnActive(t *testing.T) {@@ -698,10 +712,11 @@ func TestTryLockRewindFailsWhenTurnActive(t *testing.T) {})require.NoError(t, err)- require.NoError(t, mgr.BeginSessionTurn(ctx, "sess-1"))+ _, endTurn, err := mgr.BeginSessionTurn(ctx, "sess-1")+ require.NoError(t, err)_, err = mgr.TryLockRewind(ctx, "sess-1")require.ErrorIs(t, err, ErrSessionTurnActive)- require.NoError(t, mgr.EndSessionTurn(ctx, "sess-1"))+ endTurn()unlock, err := mgr.TryLockRewind(ctx, "sess-1")require.NoError(t, err)unlock()@@ -958,3 +973,441 @@ func TestNewSessionBoundManagerBindsBootstrapperToClient(t *testing.T) {type leaseFreeBindingStore struct {SessionSandboxBindingStore}++type fakeAllocations struct {+ mu sync.Mutex+ byID map[string]allocation.Assignment+ ownerOverride map[string]string // session id -> the session's current owner+ ineligible map[string]bool // session id -> its current auth source no longer qualifies+ machines []allocation.UserMachine+}++func (f *fakeAllocations) SessionAllocation(_ context.Context, id string) (allocation.Assignment, bool, error) {+ f.mu.Lock()+ defer f.mu.Unlock()+ a, ok := f.byID[id]+ return a, ok, nil+}++// VerifySessionAllocation mirrors the sandboxalloc check: same assignment,+// still live. Tests change the owner with setOwner to exercise C15.+func (f *fakeAllocations) VerifySessionAllocation(+ _ context.Context, _ uint64, id string, expected allocation.Assignment,+) error {+ f.mu.Lock()+ defer f.mu.Unlock()+ a, ok := f.byID[id]+ switch {+ case !ok || a.Released():+ return ErrSandboxSessionDeleted+ case f.ownerOverride[id] != "" && f.ownerOverride[id] != expected.Machine.OwnerID:+ return ErrUserAllocationRejected+ case a.Machine != expected.Machine || a.ConfigTenantID != expected.ConfigTenantID ||+ a.ConfigID != expected.ConfigID:+ return ErrUserAllocationRejected+ case a.IsUser() && f.ineligible[id]:+ return ErrUserAllocationRejected+ }+ return nil+}++func (f *fakeAllocations) setIneligible(id string) {+ f.mu.Lock()+ defer f.mu.Unlock()+ if f.ineligible == nil {+ f.ineligible = map[string]bool{}+ }+ f.ineligible[id] = true+}++func (f *fakeAllocations) setOwner(id, owner string) {+ f.mu.Lock()+ defer f.mu.Unlock()+ if f.ownerOverride == nil {+ f.ownerOverride = map[string]string{}+ }+ f.ownerOverride[id] = owner+}++func (f *fakeAllocations) UserMachinesByConfig(context.Context, uint64, string) ([]allocation.UserMachine, error) {+ return f.machines, nil+}++func (f *fakeAllocations) release(id string) {+ f.mu.Lock()+ defer f.mu.Unlock()+ a := f.byID[id]+ now := time.Now()+ a.ReleasedAt = &now+ f.byID[id] = a+}++func userAssignmentFor(sessionID string) allocation.Assignment {+ return allocation.Assignment{+ SessionID: sessionID, ConfigTenantID: 42, ConfigID: "cfg-1",+ Machine: allocation.UserMachine{OwnerTenantID: 42, OwnerID: "u-1", ConfigTenantID: 42, ConfigID: "cfg-1"},+ }+}++type userHarness struct {+ mgr *SessionBoundManager+ client *fakeRemoteClient+ allocs *fakeAllocations+ admission *admission.MemoryRegistry+}++func newUserManagerHarness(t *testing.T, assignments ...allocation.Assignment) *userHarness {+ t.Helper()+ client := newFakeRemoteClient(SandboxTypeCube)+ // The shared fake does not advertise PTYs; session-mode tests rely on that.+ // User-mode terminal coverage needs the flag without changing those tests.+ client.capabilities.SupportsTerminals = true+ return newUserManagerHarnessWith(t, client, NewMemorySessionSandboxBindingStore(),+ admission.NewMemory(), assignments...)+}++func newUserManagerHarnessWith(+ t *testing.T, client *fakeRemoteClient, store SessionSandboxBindingStore, reg admission.Registry,+ assignments ...allocation.Assignment,+) *userHarness {+ t.Helper()+ cfg := DefaultConfig()+ cfg.CubeTemplate = "tpl-test"+ allocs := &fakeAllocations{byID: map[string]allocation.Assignment{}}+ for _, a := range assignments {+ allocs.byID[a.SessionID] = a+ }+ mgr, err := NewSessionBoundManager(SessionBoundManagerConfig{+ Config: cfg, Client: client, Store: store,+ Checker: &fakeSessionExistenceChecker{exists: true}, SkipHealthProbe: true,+ ConfigID: "cfg-1", Allocations: allocs, Admission: reg, Namespace: "weknora",+ })+ require.NoError(t, err)+ mgr.admissionWait = 200 * time.Millisecond+ mem, _ := reg.(*admission.MemoryRegistry)+ return &userHarness{mgr: mgr, client: client, allocs: allocs, admission: mem}+}++func ownerCtx() context.Context {+ return types.WithSandboxTenantID(context.Background(), 42)+}++func machineDigest(t *testing.T, sessionID string) string {+ t.Helper()+ m, ok := userAssignmentFor(sessionID).UserMachine()+ require.True(t, ok)+ return m.Digest("weknora")+}++func TestUserSessionsShareOneMachineWithSeparateDirectories(t *testing.T) {+ h := newUserManagerHarness(t, userAssignmentFor(userSessionA), userAssignmentFor(userSessionB))+ ctx := ownerCtx()++ _, err := h.mgr.ExecShellCommand(ctx, userSessionA, "true", "", time.Second, nil)+ require.NoError(t, err)+ reqA := lastExecRequest(t, h.client)+ _, err = h.mgr.ExecShellCommand(ctx, userSessionB, "true", "", time.Second, map[string]string{+ "WEKNORA_SKILL_OUTPUT_DIR": "/workspace/output", // a config value must not win+ })+ require.NoError(t, err)+ reqB := lastExecRequest(t, h.client)++ creates, _, _, _, _ := h.client.counts()+ require.Equal(t, 1, creates)+ require.Equal(t, "/workspace/sessions/"+userSessionA, reqA.WorkDir)+ require.Equal(t, "/workspace/sessions/"+userSessionB, reqB.WorkDir)+ require.Equal(t, "/workspace/sessions/"+userSessionB+"/output", reqB.Env["WEKNORA_SKILL_OUTPUT_DIR"])+ require.Equal(t, "/workspace/sessions/"+userSessionB+"/input", reqB.Env["WEKNORA_SESSION_INPUT_DIR"])++ idA, ok := h.mgr.BoundSandboxID(ctx, userSessionA)+ require.True(t, ok)+ idB, _ := h.mgr.BoundSandboxID(ctx, userSessionB)+ require.Equal(t, idA, idB)++ layout, err := h.mgr.SessionWorkspaceLayout(context.Background(), userSessionA)+ require.NoError(t, err, "layout lookups run without a tenant (tool Description)")+ require.True(t, layout.IsSharedRemote())++ n, err := h.admission.ActiveExcept(ctx, admission.MachineScope(machineDigest(t, userSessionA)))+ require.NoError(t, err)+ require.Zero(t, n, "every operation released its leases")+}++func TestUserSessionCannotLeaveItsWorkspace(t *testing.T) {+ h := newUserManagerHarness(t, userAssignmentFor(userSessionA))+ ctx := ownerCtx()+ _, err := h.mgr.ExecShellCommand(ctx, userSessionA, "true", "/workspace", time.Second, nil)+ require.ErrorContains(t, err, "outside allowed roots")+ err = h.mgr.WriteSessionWorkspaceFile(ctx, userSessionA, "/workspace/sessions/"+userSessionB+"/x", []byte("x"))+ require.ErrorContains(t, err, "outside the session workspace")+ err = h.mgr.WriteSessionInputFile(ctx, userSessionA, "/workspace/input/a.pdf", []byte("x"))+ require.Error(t, err)+ _, err = h.mgr.ReadSessionFile(ctx, userSessionA, "/workspace/sessions/"+userSessionB+"/x")+ require.ErrorContains(t, err, "outside the session workspace")+}++func TestValidityIsReadFreshNotCached(t *testing.T) {+ h := newUserManagerHarness(t, userAssignmentFor(userSessionA))+ ctx := ownerCtx()+ _, err := h.mgr.ExecShellCommand(ctx, userSessionA, "true", "", time.Second, nil)+ require.NoError(t, err)++ h.allocs.release(userSessionA) // deleted by another replica after this manager cached the address+ _, err = h.mgr.ExecShellCommand(ctx, userSessionA, "true", "", time.Second, nil)+ require.ErrorIs(t, err, ErrSandboxSessionDeleted)+ _, err = h.mgr.ListSessionFiles(ctx, userSessionA, "/workspace/sessions/"+userSessionA+"/output")+ require.ErrorIs(t, err, ErrSandboxSessionDeleted, "lookup-only reads are admitted the same way")+}++func TestUserSessionDestroyIsRefused(t *testing.T) {+ h := newUserManagerHarness(t, userAssignmentFor(userSessionA))+ ctx := ownerCtx()+ _, err := h.mgr.ExecShellCommand(ctx, userSessionA, "true", "", time.Second, nil)+ require.NoError(t, err)+ require.ErrorIs(t, h.mgr.DestroySession(ctx, userSessionA), ErrSharedSandbox)+ _, _, _, _, deletes := h.client.counts()+ require.Zero(t, deletes)+}++func TestIneligibleStoredUserAssignmentIsRejected(t *testing.T) {+ h := newUserManagerHarness(t, userAssignmentFor(userSessionA))+ // The session's current auth source no longer qualifies (C15, C17).+ h.allocs.setIneligible(userSessionA)+ _, err := h.mgr.ExecShellCommand(ownerCtx(), userSessionA, "true", "", time.Second, nil)+ require.ErrorIs(t, err, ErrUserAllocationRejected)+}++func TestSessionModeAssignmentKeepsHistoricalPath(t *testing.T) {+ a := userAssignmentFor(userSessionA)+ a.Machine = allocation.UserMachine{} // no machine: a machine of its own+ h := newUserManagerHarness(t, a)+ ctx := ownerCtx()+ _, err := h.mgr.ExecShellCommand(ctx, userSessionA, "true", "", time.Second, nil)+ require.NoError(t, err)+ req := lastExecRequest(t, h.client)+ require.Equal(t, SessionWorkspaceRoot, req.WorkDir)+ require.Equal(t, userSessionA, h.client.lastCreateRequest.Metadata[remoteMetadataSessionID])+}++func TestUserTurnHoldsAreIndependentAndTravelInTheirContext(t *testing.T) {+ h := newUserManagerHarness(t, userAssignmentFor(userSessionA))+ ctx := ownerCtx()+ scope := admission.MachineScope(machineDigest(t, userSessionA))++ outerCtx, endOuter, err := h.mgr.BeginSessionTurn(ctx, userSessionA)+ require.NoError(t, err)+ innerCtx, endInner, err := h.mgr.BeginSessionTurn(outerCtx, userSessionA) // nested send hold+ require.NoError(t, err)+ n, _ := h.admission.ActiveExcept(ctx, scope)+ require.Equal(t, 2, n, "each hold registers its own member")++ n, _ = h.admission.ActiveExcept(ctx, scope, admissionExemptFrom(innerCtx)...)+ require.Zero(t, n, "a tool call of this turn exempts exactly the turn's own holds")+ n, _ = h.admission.ActiveExcept(ctx, scope, admissionExemptFrom(ctx)...)+ require.Equal(t, 2, n, "a request outside the turn exempts nothing")++ endOuter()+ n, _ = h.admission.ActiveExcept(ctx, scope)+ require.Equal(t, 1, n, "ending one hold never removes another")+ endInner()+ endInner() // idempotent+ n, _ = h.admission.ActiveExcept(ctx, scope)+ require.Zero(t, n)+}++func TestUserTurnExemptionSurvivesCloneContext(t *testing.T) {+ h := newUserManagerHarness(t, userAssignmentFor(userSessionA))+ turnCtx, end, err := h.mgr.BeginSessionTurn(ownerCtx(), userSessionA)+ require.NoError(t, err)+ defer end()+ detached := logger.CloneContext(turnCtx) // how setupSSEStream builds the async QA context+ require.Equal(t, admissionExemptFrom(turnCtx), admissionExemptFrom(detached))+}++func TestUserTurnContextEndsWhenItsLeaseIsLost(t *testing.T) {+ h := newUserManagerHarness(t, userAssignmentFor(userSessionA))+ ctx := ownerCtx()+ turnCtx, end, err := h.mgr.BeginSessionTurn(ctx, userSessionA)+ require.NoError(t, err)+ defer end()+ scope := admission.MachineScope(machineDigest(t, userSessionA))+ h.admission.ExpireMember(scope, admissionExemptFrom(turnCtx)[0])+ select {+ case <-turnCtx.Done():+ require.ErrorIs(t, context.Cause(turnCtx), admission.ErrLeaseLost)+ case <-time.After(time.Second):+ t.Fatal("the turn kept running after its registration was lost")+ }+}++func TestVerifyRejectsASessionWhoseOwnerChanged(t *testing.T) {+ h := newUserManagerHarness(t, userAssignmentFor(userSessionA))+ ctx := ownerCtx()+ _, err := h.mgr.ExecShellCommand(ctx, userSessionA, "true", "", time.Second, nil)+ require.NoError(t, err)+ h.allocs.setOwner(userSessionA, "u-2")+ _, err = h.mgr.ExecShellCommand(ctx, userSessionA, "true", "", time.Second, nil)+ require.ErrorIs(t, err, ErrUserAllocationRejected, "the assignment snapshot is not enough (C15)")+}++func TestUserTerminalDialStopsWhenTheLeaseIsLost(t *testing.T) {+ h := newUserManagerHarness(t, userAssignmentFor(userSessionA))+ ctx := ownerCtx()+ _, err := h.mgr.ExecShellCommand(ctx, userSessionA, "true", "", time.Second, nil)+ require.NoError(t, err)+ started := make(chan struct{})+ h.client.openTerminalHook = func(dialCtx context.Context) error {+ close(started)+ <-dialCtx.Done()+ return context.Cause(dialCtx)+ }+ errCh := make(chan error, 1)+ go func() {+ _, openErr := h.mgr.OpenSessionTerminal(ctx, userSessionA, RemoteTerminalOptions{})+ errCh <- openErr+ }()+ select {+ case <-started:+ case <-time.After(2 * time.Second):+ t.Fatal("dial did not start")+ }+ h.admission.ExpireScope(admission.MachineScope(machineDigest(t, userSessionA)))+ select {+ case err := <-errCh:+ require.ErrorIs(t, err, admission.ErrLeaseLost)+ case <-time.After(2 * time.Second):+ t.Fatal("dial kept running after its lease was lost")+ }+}++func TestTerminalIsClosedWhenItsLeaseIsLost(t *testing.T) {+ h := newUserManagerHarness(t, userAssignmentFor(userSessionA))+ ctx := ownerCtx()+ _, err := h.mgr.ExecShellCommand(ctx, userSessionA, "true", "", time.Second, nil)+ require.NoError(t, err)+ term, err := h.mgr.OpenSessionTerminal(ctx, userSessionA, RemoteTerminalOptions{})+ require.NoError(t, err)+ defer func() { _ = term.Close() }()+ scope := admission.MachineScope(machineDigest(t, userSessionA))+ member := term.(*heldTerminalSession).member+ h.admission.ExpireMember(scope, member)+ require.Eventually(t, func() bool { return h.client.terminalClosed() }, time.Second, 10*time.Millisecond,+ "a PTY must not keep running on a machine another replica may now rebuild")+}++func TestLostLeaseCancelsTheOperation(t *testing.T) {+ h := newUserManagerHarness(t, userAssignmentFor(userSessionA))+ ctx := ownerCtx()+ opCtx, release, err := h.mgr.HoldSessionOperation(ctx, userSessionA, "desktop")+ require.NoError(t, err)+ defer release()+ members := admissionExemptFrom(opCtx)+ require.NotEmpty(t, members)+ h.admission.ExpireMember(admission.MachineScope(machineDigest(t, userSessionA)), members[0])+ select {+ case <-opCtx.Done():+ require.ErrorIs(t, context.Cause(opCtx), admission.ErrLeaseLost)+ case <-time.After(time.Second):+ t.Fatal("an operation whose lease is lost must be cancelled")+ }+}++func TestSessionExclusiveWaitsAndOwnerKeepsWorking(t *testing.T) {+ h := newUserManagerHarness(t, userAssignmentFor(userSessionA))+ ctx := ownerCtx()+ _, release, err := h.mgr.HoldSessionOperation(ctx, userSessionA, "pty")+ require.NoError(t, err)++ _, _, err = h.mgr.AcquireSessionExclusive(ctx, userSessionA)+ require.ErrorIs(t, err, ErrSessionWorkspaceBusy, "an attached terminal blocks exclusive work")+ release()++ exCtx, unlock, err := h.mgr.AcquireSessionExclusive(ctx, userSessionA)+ require.NoError(t, err)+ defer unlock()+ _, err = h.mgr.ExecShellCommand(exCtx, userSessionA, "true", "", time.Second, nil)+ require.NoError(t, err, "the exclusive owner's own commands do not lock themselves out")+ _, err = h.mgr.ExecShellCommand(ctx, userSessionA, "true", "", time.Second, nil)+ require.ErrorContains(t, err, "busy", "everyone else waits for the exclusive window")+}++func TestExclusiveWindowIsAMachineMemberAndBlocksRebuild(t *testing.T) {+ h := newUserManagerHarness(t, userAssignmentFor(userSessionA), userAssignmentFor(userSessionB))+ ctx := ownerCtx()+ _, err := h.mgr.ExecShellCommand(ctx, userSessionA, "true", "", time.Second, nil)+ require.NoError(t, err)+ first, _ := h.mgr.BoundSandboxID(ctx, userSessionA)+ _, _, _, _, deletesBefore := h.client.counts()++ _, unlock, err := h.mgr.AcquireSessionExclusive(ctx, userSessionA)+ require.NoError(t, err)++ scope := admission.MachineScope(machineDigest(t, userSessionA))+ n, err := h.admission.ActiveExcept(ctx, scope)+ require.NoError(t, err)+ require.Equal(t, 1, n, "rewind and directory delete must be visible to machine rebuild and reclaim")++ m, _ := userAssignmentFor(userSessionA).UserMachine()+ _, err = h.mgr.InvalidateConfigSandboxesForMachines(ctx, "cfg-1", []allocation.UserMachine{m})+ require.NoError(t, err)+ _, err = h.mgr.ExecShellCommand(ctx, userSessionB, "true", "", time.Second, nil)+ require.NoError(t, err)+ kept, _ := h.mgr.BoundSandboxID(ctx, userSessionB)+ require.Equal(t, first, kept, "a stale rebuild must not delete the machine during the exclusive window")+ _, _, _, _, deletesAfter := h.client.counts()+ require.Equal(t, deletesBefore, deletesAfter)++ unlock()+ n, err = h.admission.ActiveExcept(ctx, scope)+ require.NoError(t, err)+ require.Zero(t, n, "ending the window releases the machine member")+}++func TestUploadedScriptsStayInTheSessionDirectory(t *testing.T) {+ h := newUserManagerHarness(t, userAssignmentFor(userSessionA))+ script := filepath.Join(t.TempDir(), "run.py")+ require.NoError(t, os.WriteFile(script, []byte("print(1)"), 0o600))+ _, err := h.mgr.Execute(ownerCtx(), &ExecuteConfig{SessionID: userSessionA, Script: script, SkipValidation: true})+ require.NoError(t, err)+ h.client.mu.Lock()+ defer h.client.mu.Unlock()+ require.NotEmpty(t, h.client.writeFiles)+ require.Equal(t, "/workspace/sessions/"+userSessionA+"/.weknora/scripts/run.py",+ h.client.writeFiles[len(h.client.writeFiles)-1].path)+}++func TestMultiReplicaAdmissionBlocksRebuildAcrossManagers(t *testing.T) {+ mini := miniredis.RunT(t)+ client := newFakeRemoteClient(SandboxTypeCube) // one provider shared by both replicas+ replica := func() *userHarness {+ rc := redis.NewClient(&redis.Options{Addr: mini.Addr()})+ t.Cleanup(func() { _ = rc.Close() })+ store, err := NewRedisSessionSandboxBindingStore(rc, "weknora")+ require.NoError(t, err)+ return newUserManagerHarnessWith(t, client, store, admission.NewRedis(rc, "weknora:sandbox:user:"),+ userAssignmentFor(userSessionA), userAssignmentFor(userSessionB))+ }+ r1, r2 := replica(), replica()+ ctx := ownerCtx()++ _, err := r1.mgr.ExecShellCommand(ctx, userSessionA, "true", "", time.Second, nil)+ require.NoError(t, err)+ first, _ := r1.mgr.BoundSandboxID(ctx, userSessionA)++ _, release, err := r2.mgr.HoldSessionOperation(ctx, userSessionB, "desktop")+ require.NoError(t, err)+ m, _ := userAssignmentFor(userSessionA).UserMachine()+ _, err = r1.mgr.InvalidateConfigSandboxesForMachines(ctx, "cfg-1", []allocation.UserMachine{m})+ require.NoError(t, err)++ _, err = r1.mgr.ExecShellCommand(ctx, userSessionA, "true", "", time.Second, nil)+ require.NoError(t, err)+ kept, _ := r1.mgr.BoundSandboxID(ctx, userSessionA)+ require.Equal(t, first, kept, "a desktop on another replica blocks the rebuild")++ release()+ _, err = r1.mgr.ExecShellCommand(ctx, userSessionA, "true", "", time.Second, nil)+ require.NoError(t, err)+ rebuilt, _ := r1.mgr.BoundSandboxID(ctx, userSessionA)+ require.NotEqual(t, first, rebuilt)+}diff --git a/internal/sandbox/tenant_resolver.go b/internal/sandbox/tenant_resolver.goindex b0171765d..21b6a1948 100644--- a/internal/sandbox/tenant_resolver.go+++ b/internal/sandbox/tenant_resolver.go@@ -31,6 +31,7 @@ import ("strings""time"+ "github.com/Tencent/WeKnora/internal/sandbox/admission""github.com/Tencent/WeKnora/internal/types")@@ -88,6 +89,12 @@ type TenantSandboxResolverDeps struct {// (session fork). Optional: nil is the ordinary path.Bootstrapper SessionBootstrapper+ // Allocations, Admission and Namespace are passed to every session-bound+ // manager; see SessionBoundManagerConfig.+ Allocations AllocationDirectory+ Admission admission.Registry+ Namespace string+// SharedTransport is reused by every tenant's HTTP client. Optional; a// guarded transport is installed when nil.SharedTransport *http.Transport@@ -194,6 +201,9 @@ func (r *tenantSandboxResolver) Resolve(SkipHealthProbe: true,ConfigID: configID,Bootstrapper: r.deps.Bootstrapper,+ Allocations: r.deps.Allocations,+ Admission: r.deps.Admission,+ Namespace: r.deps.Namespace,})default:return NewDisabledManager(), nildiff --git a/internal/types/const.go b/internal/types/const.goindex 751f9d17a..9fb12af83 100644--- a/internal/types/const.go+++ b/internal/types/const.go@@ -58,6 +58,9 @@ const (// distinct key from SessionTenantIDContextKey, which also re-scopes// session/message repository lookups. See SandboxTenantIDFromContext.SandboxTenantIDContextKey ContextKey = "SandboxTenantID"+ // SandboxAdmissionExemptContextKey carries the shared-machine admission+ // members of the current request and its chat turn ([]string).+ SandboxAdmissionExemptContextKey ContextKey = "sandbox_admission_exempt"// EmbedQueryContextKey marks an embedding call as a search query; see// WithEmbedQuery.EmbedQueryContextKey ContextKey = "EmbedQuery"diff --git a/internal/types/context_clone.go b/internal/types/context_clone.goindex 77a64225f..38acd213b 100644--- a/internal/types/context_clone.go+++ b/internal/types/context_clone.go@@ -60,6 +60,10 @@ var contextCloneAcrossDetach = map[ContextKey]bool{SessionIDContextKey: true,SessionTenantIDContextKey: true,SandboxTenantIDContextKey: true,+ // The request's own admission members. Not a grant: it only exempts+ // members this turn holds itself, by unique token, from a rebuild check.+ // Dropping it would make detached QA work wait on its own turn.+ SandboxAdmissionExemptContextKey: true,// Embed callers: the anonymous visitor id isolates embed OAuth, so losing// it would merge visitors together.