@0perator/operator 0.1.153 → 0.1.154

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -4,6 +4,18 @@ Operator is AXE's terminal coding agent (npm `@0perator/operator`), running on A
4
4
  Newest first. Every release bumps `src/cli.tsx` VERSION + `package.json`, adds an entry here, and ships to
5
5
  npm + both git remotes (memjar/kode, axetechnologies/kode). Test counts are the full suite at that release.
6
6
 
7
+ ## 0.1.154 — 2026-09-29 — engine: per-session persistent shell (sandboxed-exec Phase 1)
8
+ - The `bash`/`shell_exec` persistent shell is now **per session**, not one process-wide singleton. Before, the
9
+ foreground and every background sub-agent shared ONE bash: a sub-agent's `cd`/`export` corrupted the
10
+ foreground's cwd/env (and vice versa — proven live on JL1), and every command head-of-line-blocked every other
11
+ session on the one FIFO. Now the `Executor` carries a stable per-session `shellKey` (the session's `sessionId`,
12
+ else a unique `exec-N` fallback so two keyless lanes never collide) through tool execution via an
13
+ `AsyncLocalStorage`; the registry keys `ShellSession` by it, and a sub-agent disposes its shell on completion.
14
+ Default (`KODE_SANDBOX=off`) behavior is **byte-identical** — this is the correctness fix + prerequisite for
15
+ the sandboxed-exec tiers, no sandbox change yet. Design + JL1 validation:
16
+ `_ai-docs/architecture/sandboxed-code-execution.md`; decision: `_ai-docs/decisions/2026-09-29-per-session-shell.md`.
17
+ Suite 699 green (+5). Also: releases now publish to **npmjs** (private) instead of pkg.axe.onl (`publishConfig`).
18
+
7
19
  ## 0.1.153 — 2026-09-29 — speed: KV prefix-cache reuse (`cache_prompt: true`) — fixes slow compaction
8
20
  - Every Aurora request now sends `cache_prompt: true`. Without it the single-slot llama.cpp server RE-PREFILLED
9
21
  the whole growing prefix (system + tools + history) each round, and re-prefilled the full evicted span on every
@@ -324,6 +324,12 @@ export class BackgroundAgents {
324
324
  this.onDone(info, `[${id} error: ${e instanceof Error ? e.message : String(e)}]`);
325
325
  }
326
326
  finally {
327
+ // Kill this sub-agent's own persistent shell so its bash doesn't linger for the whole parent
328
+ // session (each session now gets its OWN shell; without this they'd accumulate). Best-effort.
329
+ try {
330
+ session.disposeShell();
331
+ }
332
+ catch { /* shell may never have started */ }
327
333
  // This agent set itself idle/stopped above, freeing a slot — promote the next queued agent.
328
334
  this.pump();
329
335
  }
package/dist/session.js CHANGED
@@ -14,7 +14,7 @@ import { AuroraRouter } from "./router/router.js";
14
14
  import { operatorAgentPersona } from "./config/operator-harness.js";
15
15
  import { modelRegistry, findModel, selectModel, endpointBase, DEFAULT_MODEL_ID } from "./router/models.js";
16
16
  import { Executor } from "./tools/executor.js";
17
- import { toSchemas, registry } from "./tools/registry.js";
17
+ import { toSchemas, registry, disposeShellSessionKey } from "./tools/registry.js";
18
18
  import { MemoryStore } from "./memory/index.js";
19
19
  import { McpHub, withMcp } from "./mcp/index.js";
20
20
  import { withPermissions, PermissionPolicy } from "./permissions/index.js";
@@ -190,6 +190,7 @@ export class Session {
190
190
  moaConfig;
191
191
  modelId = DEFAULT_MODEL_ID; // active /model selection
192
192
  executor;
193
+ shellKey; // this session's persistent-shell key (see disposeShell)
193
194
  localSchemas;
194
195
  hub;
195
196
  tools;
@@ -237,7 +238,8 @@ export class Session {
237
238
  if (cfg.askUser)
238
239
  reg.set("ask_user", makeAskTool(cfg.askUser));
239
240
  }
240
- this.executor = new Executor(reg, cfg.cwd);
241
+ this.executor = new Executor(reg, cfg.cwd, cfg.sessionId);
242
+ this.shellKey = this.executor.shellKey; // for disposeShell() on teardown
241
243
  this.localSchemas = toSchemas(reg);
242
244
  this.inner = cfg.router ?? new AuroraRouter();
243
245
  this.router = engineRouter(this.inner);
@@ -548,9 +550,17 @@ export class Session {
548
550
  mcpServers() {
549
551
  return this.hub?.serversWithTools() ?? [];
550
552
  }
551
- /** Tear down session resources (kills any spawned stdio MCP servers). Call on shell exit. */
553
+ /** Tear down session resources (kills any spawned stdio MCP servers + this session's persistent
554
+ * shell). Call on shell exit. */
552
555
  shutdown() {
553
556
  this.hub?.shutdown();
557
+ this.disposeShell();
558
+ }
559
+ /** Kill just this session's persistent shell — call when a background sub-agent finishes so its
560
+ * bash doesn't linger for the whole parent session. Safe to call more than once; a no-op if the
561
+ * shell was never started. Does NOT touch the shared MCP hub (a sub-agent may share it). */
562
+ disposeShell() {
563
+ disposeShellSessionKey(this.shellKey);
554
564
  }
555
565
  /** Store a durable memory note (for /remember and end-of-session capture). Best-effort. */
556
566
  remember(text, tags) {
@@ -10,6 +10,7 @@
10
10
  // doesn't bleed across turns)
11
11
  import { isAbsolute, join } from "node:path";
12
12
  import { registry } from "./registry.js";
13
+ import { shellKeyStore } from "./shell-context.js";
13
14
  // Re-exported for back-compat (existing importers reach it via executor.js) — moved to its own
14
15
  // file since it's a pure helper with no node deps; see result-cap.ts for why that separation matters.
15
16
  import { capToolResult } from "./result-cap.js";
@@ -38,14 +39,25 @@ export function rootArgs(name, args, root) {
38
39
  const LOOP_GUARD_THRESHOLD = 5; // must match agent.ts LOOP_GUARD_LIMIT so this secondary guard never fires first
39
40
  import { MUTATING_TOOLS } from "./mutating.js"; // single source of truth (see tools/mutating.ts) — a workspace edit ⇒ prior identical-call counts are stale
40
41
  const LOOP_GUARD_EXEMPT = new Set(["agent_status", "bash_output"]); // poll/status tools — repeated calls are legit, never guarded
42
+ // Unique fallback id per Executor instance, so two sessions with no sessionId and no root (the
43
+ // foreground and a read-only sub-agent lane) still get DISTINCT shell keys. One Executor lives for
44
+ // its session's whole lifetime (the Session is constructed once and reused across turns), so this id
45
+ // is stable across a session's turns — the persistent shell survives, keyed per session, not shared.
46
+ let _execSeq = 0;
41
47
  export class Executor {
42
48
  reg;
43
49
  root;
50
+ /** Stable per-session key for this Executor's persistent shell (see shell-context.ts). A caller-
51
+ * supplied id (the Session's sessionId) when present, else a unique per-instance fallback. */
52
+ shellKey;
44
53
  /** @param root optional working directory to root every tool call under (a background agent's isolated
45
- * worktree) — so its file/shell mutations never touch the shared process cwd. Undefined = shared cwd. */
46
- constructor(reg = registry, root) {
54
+ * worktree) — so its file/shell mutations never touch the shared process cwd. Undefined = shared cwd.
55
+ * @param shellKey stable session id used to key this session's persistent shell; falls back to a
56
+ * unique per-instance id so distinct sessions never share (and corrupt) one shell. */
57
+ constructor(reg = registry, root, shellKey) {
47
58
  this.reg = reg;
48
59
  this.root = root;
60
+ this.shellKey = shellKey || `exec-${++_execSeq}`;
49
61
  }
50
62
  seen = new Map();
51
63
  resetTurn() {
@@ -65,7 +77,9 @@ export class Executor {
65
77
  if (!tool)
66
78
  return `error: unknown tool "${name}"`;
67
79
  try {
68
- const out = capToolResult(await tool.run(args));
80
+ // Run the tool inside THIS session's shell-key context so a persistent-shell tool (bash) picks
81
+ // this session's own ShellSession, not a process-wide singleton shared with other sub-agents.
82
+ const out = capToolResult(await shellKeyStore.run(this.shellKey, () => tool.run(args)));
69
83
  // A file edit changed the workspace, so prior identical-call counts are stale — re-running the same
70
84
  // test/build after an edit is verify-after-fix iteration, not a loop. Clear so repeats start fresh.
71
85
  if (MUTATING_TOOLS.has(name))
@@ -15,26 +15,44 @@ import { ensureSearxng, noteSearxngUsed } from "./searxng.js";
15
15
  function str(v, fallback = "") {
16
16
  return v === undefined || v === null ? fallback : String(v);
17
17
  }
18
- // One persistent shell per Operator process (frontier-grade: cwd/env/venv persist across calls,
19
- // like a real terminal — so the model sources a venv or `cd`s ONCE, not on every call). Lazily
20
- // created; survives for the process lifetime.
21
- let _shellSession = null;
18
+ // One persistent shell PER SESSION (frontier-grade: cwd/env/venv persist across calls, like a real
19
+ // terminal — so the model sources a venv or `cd`s ONCE, not on every call). Keyed by the session's
20
+ // shell key (Executor.shellKey, carried through shellKeyStore) so the foreground and each background
21
+ // sub-agent get their OWN bash — previously a single process-wide shell was shared by all, and a
22
+ // sub-agent's `cd`/`export` corrupted the foreground's cwd/env (and every command head-of-line-blocked
23
+ // every other session on one FIFO). Lazily created per key; each survives until its session disposes.
24
+ const _shells = new Map();
22
25
  async function getShellSession() {
23
- if (!_shellSession) {
24
- // Variable specifier (nodeImport), not a string literal: keeps shell-session.ts — which loads
25
- // node:child_process — OUT of the platform:neutral (mobile/JSCore) bundle's static graph. The
26
- // `as typeof import(...)` is a TYPE-ONLY annotation (erased at build), so it restores the real
27
- // types without reintroducing a static specifier esbuild would follow.
26
+ // Variable specifier (nodeImport), not a string literal: keeps these node-only modules
27
+ // (shell-context → node:async_hooks, shell-session → node:child_process) OUT of the
28
+ // platform:neutral (mobile/JSCore) bundle's static graph. The `as typeof import(...)` is a
29
+ // TYPE-ONLY annotation (erased at build), so it restores the real types without reintroducing a
30
+ // static specifier esbuild would follow.
31
+ const { shellKeyStore, DEFAULT_SHELL_KEY } = (await nodeImport("./shell-context.js"));
32
+ const key = shellKeyStore.getStore() ?? DEFAULT_SHELL_KEY;
33
+ let sess = _shells.get(key);
34
+ if (!sess) {
28
35
  const { ShellSession } = (await nodeImport("./shell-session.js"));
29
- _shellSession = new ShellSession();
36
+ sess = new ShellSession();
37
+ _shells.set(key, sess);
30
38
  }
31
- return _shellSession;
39
+ return sess;
32
40
  }
33
- /** Kill the persistent shell (and its process group) on process shutdown so it never orphans.
34
- * Wired into the CLI's exit/SIGTERM handlers — no-op if a shell was never started. */
41
+ /** Kill EVERY persistent shell (and their process groups) on process shutdown so none orphan.
42
+ * Wired into the CLI's exit/SIGTERM handlers — no-op if no shell was ever started. */
35
43
  export function disposeShellSession() {
36
- _shellSession?.dispose();
37
- _shellSession = null;
44
+ for (const sess of _shells.values())
45
+ sess.dispose();
46
+ _shells.clear();
47
+ }
48
+ /** Kill one session's persistent shell by its key (Session.disposeShell → here on sub-agent
49
+ * completion) so a long foreground session spawning many sub-agents doesn't leak idle bash processes. */
50
+ export function disposeShellSessionKey(key) {
51
+ const sess = _shells.get(key);
52
+ if (sess) {
53
+ sess.dispose();
54
+ _shells.delete(key);
55
+ }
38
56
  }
39
57
  async function shellExec(args) {
40
58
  const command = str(args.command);
@@ -0,0 +1,17 @@
1
+ // node-only: carries the active session's shell key through async tool execution, so the
2
+ // persistent-shell tool (bash/shell_exec → getShellSession) resolves a PER-SESSION shell instead of
3
+ // one process-wide singleton. Before this, the foreground and every background sub-agent shared ONE
4
+ // bash process: a sub-agent's `cd`/`export` corrupted the foreground's cwd/env (and vice versa), and
5
+ // one slow command head-of-line-blocked all of them on the single FIFO. AsyncLocalStorage isolates
6
+ // the key per Executor.execute() call, so concurrent executions never read each other's key.
7
+ //
8
+ // Kept in its own module (not registry.ts, which is ALSO bundled platform:neutral for tool SCHEMAS)
9
+ // so registry.ts can pull it via the lazy nodeImport() path and keep node:async_hooks out of the
10
+ // neutral (mobile/JSCore) graph; the node-only executor.ts imports it statically.
11
+ import { AsyncLocalStorage } from "node:async_hooks";
12
+ /** Fallback key when a shell tool runs outside any Executor context (should not happen in normal
13
+ * flow — getShellSession is only reached via shellExec — but a stable default keeps it correct). */
14
+ export const DEFAULT_SHELL_KEY = "__main__";
15
+ /** The current session's shell key. Set by Executor.execute() around each tool.run; read by
16
+ * getShellSession() to pick this session's own ShellSession from the per-key map. */
17
+ export const shellKeyStore = new AsyncLocalStorage();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@0perator/operator",
3
- "version": "0.1.153",
3
+ "version": "0.1.154",
4
4
  "description": "Operator (KODE) — TypeScript + Ink terminal coding agent. Full-frame render, single-engine core shared across CLI/web/mobile; runs against a local Aurora model.",
5
5
  "type": "module",
6
6
  "bin": {