@0perator/operator 0.1.313 → 0.1.321

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.
Files changed (71) hide show
  1. package/dist/autonomy/agent-cli.js +6 -2
  2. package/dist/autonomy/cron.js +1 -1
  3. package/dist/autonomy/delegate.js +29 -4
  4. package/dist/autonomy/index.js +22 -0
  5. package/dist/autonomy/profile.js +1 -1
  6. package/dist/browser/cdp.js +1 -1
  7. package/dist/cli.js +119 -43
  8. package/dist/config/persona.js +29 -12
  9. package/dist/config/settings.js +12 -7
  10. package/dist/engine/agent.js +4 -2
  11. package/dist/engine/aurora.js +8 -3
  12. package/dist/engine/batch-scheduler.js +3 -3
  13. package/dist/engine/cli.js +1171 -0
  14. package/dist/engine/compaction.js +1 -1
  15. package/dist/engine/loop-guard.js +1 -1
  16. package/dist/engine/router-adapter.js +1 -0
  17. package/dist/idle/activity.js +20 -0
  18. package/dist/idle/index.js +5 -0
  19. package/dist/idle/monitor.js +71 -0
  20. package/dist/idle/policy.js +66 -0
  21. package/dist/idle/proc.js +22 -0
  22. package/dist/idle/reaper.js +69 -0
  23. package/dist/idle/session-record.js +59 -0
  24. package/dist/index.js +1 -1
  25. package/dist/mcp/config.js +72 -14
  26. package/dist/mcp/index.js +9 -2
  27. package/dist/memory/index.js +1 -1
  28. package/dist/permissions/index.js +22 -7
  29. package/dist/permissions/tier.js +1 -1
  30. package/dist/remote/host.js +20 -2
  31. package/dist/remote/safe-commands.js +38 -0
  32. package/dist/router/models.js.bak +78 -0
  33. package/dist/router/models.js.bak-1791259923 +78 -0
  34. package/dist/router/models.js.bak-pre-gateway +78 -0
  35. package/dist/router/models.js.bak-pre-patch2 +78 -0
  36. package/dist/router/models.js.bak2 +78 -0
  37. package/dist/services/model-autodetect.js +240 -0
  38. package/dist/services/preflight.js +54 -2
  39. package/dist/services/store-sync.js +111 -0
  40. package/dist/session-teardown.js +10 -1
  41. package/dist/session.js +22 -0
  42. package/dist/skills/catalogue.js +1 -1
  43. package/dist/tools/chauffeur-native.js +155 -0
  44. package/dist/tools/chauffeur.js +14 -3
  45. package/dist/tools/download.js +229 -0
  46. package/dist/tools/executor.js +1 -1
  47. package/dist/tools/monitoring-tools.js +312 -0
  48. package/dist/tools/native-redirect.js +140 -0
  49. package/dist/tools/paths.js +154 -0
  50. package/dist/tools/power-tools.js +627 -0
  51. package/dist/tools/registry.js +309 -18
  52. package/dist/tools/registry.js.bak-1791259948 +1914 -0
  53. package/dist/tools/registry.js.bak-pre-plugins +1914 -0
  54. package/dist/tools/shell-jobs.js +5 -2
  55. package/dist/tools/shell-session.js +4 -2
  56. package/dist/tools/tool-output-spill.js +1 -1
  57. package/dist/ui/BusTray.js +70 -0
  58. package/dist/ui/Composer.js +5 -2
  59. package/dist/ui/QuestionPrompt.js +1 -1
  60. package/dist/ui/ToolStep.js +16 -1
  61. package/dist/ui/liveTools.js +1 -1
  62. package/dist/ui/resize.js +160 -0
  63. package/dist/util/mentions.js +1 -1
  64. package/dist/vault/store.js +1 -1
  65. package/mcp/README.md +88 -0
  66. package/mcp/ocr-macos.swift +45 -0
  67. package/mcp/powertools-media.mjs +464 -0
  68. package/mcp/powertools-toolbox.mjs +1114 -0
  69. package/mcp/powertools.md +210 -0
  70. package/mcp/powertools.mjs +861 -0
  71. package/package.json +2 -1
@@ -0,0 +1,140 @@
1
+ // Pre-exec redirect for "reinvent a native tool" shell commands. A model whose image/video/vision/voice
2
+ // tool hit a transient worker outage used to read "work around it" as a mandate to BUILD the stack: it
3
+ // pip-installed comfy-cli, cloned ComfyUI and downloaded diffusion weights onto the serving box (live aurora
4
+ // session, 10-09) — instead of retrying the native tool it already had. This is the last line of defence at
5
+ // the shell: an install/download verb COMBINED WITH a media-stack token is answered with a TOOL RESULT (never
6
+ // a second system message — llama.cpp's chat template 400s on that, see engine/unrestricted.ts) that points
7
+ // at the tool the model already has. Pure and unit-tested (test/native-redirect.test.mts); shellExec and
8
+ // shellBackground in registry.ts hook it at the top, modeled on subAgentRedirect.
9
+ //
10
+ // A weight download (*.safetensors/.gguf/.ckpt, `hf download`) is HARD-BLOCKED every time: it pins GBs on the
11
+ // box that serves the model and collapses decode to ~1 tok/s. A generic install that merely mentions a media
12
+ // token is redirected ONCE — the identical command re-issued goes through, so a legitimate non-media install
13
+ // is never permanently blocked. KODE_ALLOW_TOOLING_INSTALL=1 (set by the OWNER in the engine's environment;
14
+ // a `export` inside the model's shell cannot reach it) lifts the guard entirely.
15
+ //
16
+ // Heuristic, not a security boundary: it catches the honest mistake (the plain command), not a model that
17
+ // hides the install in a script it writes and then runs. The levers that matter are the tool failure
18
+ // semantics and the capability map (registry.ts); this is the backstop.
19
+ const STACKS = [
20
+ {
21
+ name: "image",
22
+ tools: "`image_generate` (FLUX fast default; `quality: \"photoreal\"` or a face `reference` → RealVisXL via the identity broker) and `image_analyze`",
23
+ re: /\bcomfy(?:ui|[-_]cli)?\b|\bdiffusers\b|\bdiffusionkit\b|\bmflux\b|\bsdxl\b|\bstable[-_ .]?diffusion\w*|\brealvis\w*|\bjuggernaut\w*|\bflux[.\-_]?1\b|\bflux[-_](?:dev|schnell|pro)\b|\bblack-forest-labs\b|\binference\.sh\b|\binfsh\b|\binvokeai\b|\bfooocus\b|\bautomatic1111\b|\bsd-webui\b|\bip[-_]adapter\w*|\binsightface\b|\.safetensors\b|\.ckpt\b/i,
24
+ },
25
+ {
26
+ name: "video",
27
+ tools: "`video_generate` (LTX) and `video_analyze`",
28
+ re: /\bltx[-_]?video\b|\banimatediff\b|\bcogvideo\w*|\bstable[-_]video\b|\bwan2\S*|\bhunyuan[-_]?video\b|\bmochi\b/i,
29
+ },
30
+ {
31
+ name: "vision",
32
+ tools: "`image_analyze` / `video_analyze` (Qwen2-VL)",
33
+ re: /\bllava\b|\bqwen[\d.]*[-_]?vl\b|\bmlx[-_]vlm\b|\bmoondream\b|\bpaligemma\b|\bminicpm[-_]v\b/i,
34
+ },
35
+ {
36
+ name: "voice",
37
+ tools: "`voice_speak` / `voice_transcribe` (Kokoro + whisper.cpp)",
38
+ re: /\bwhisper\w*|\bkokoro\w*/i,
39
+ },
40
+ ];
41
+ const GENERIC_TOOLS = "the native media tools (`image_generate`, `video_generate`, `image_analyze`, `video_analyze`, `voice_speak`, `voice_transcribe`)";
42
+ const WEIGHT_EXT = /\.(?:safetensors|gguf|ckpt)\b/i;
43
+ const HF_RESOLVE = /huggingface\.co\/\S*\/resolve\//i;
44
+ const HF_PY = /\b(?:snapshot_download|hf_hub_download)\b/;
45
+ // Install/launch verbs, anchored at the START of a command segment (after wrappers are stripped) — so
46
+ // `grep "pip install whisper" docs/` and `echo "brew install llava"` (verb not at command position) pass.
47
+ const INSTALL_VERBS = [
48
+ /^(?:pip3?|python3?(?:\.\d+)?\s+-m\s+pip|uv\s+pip|conda|mamba|micromamba)\s+(?:-\S+\s+)*install\b/,
49
+ /^pipx\s+(?:-\S+\s+)*(?:install|run)\b/,
50
+ /^uv\s+(?:add|tool\s+(?:install|run))\b/,
51
+ /^(?:uvx|npx|poetry\s+add)\b/,
52
+ /^(?:brew|port|apt(?:-get)?|dnf|yum|apk)\s+(?:-\S+\s+)*(?:install|reinstall)\b/,
53
+ /^(?:npm|pnpm|bun)\s+(?:-\S+\s+)*(?:i|install|add)\b/,
54
+ /^yarn\s+(?:global\s+)?add\b/,
55
+ /^(?:cargo|go)\s+install\b/,
56
+ /^git\s+clone\b/,
57
+ /^docker\s+(?:pull|run)\b/,
58
+ /^ollama\s+pull\b/,
59
+ /^(?:comfy(?:-cli)?|infsh)\b/,
60
+ ];
61
+ // Leading wrappers that do not change what actually runs: sudo/nohup/time/env FOO=x, a subshell paren, a
62
+ // VAR=val prefix, `bash -c '`, an opening quote.
63
+ const WRAPPER = /^(?:[({!]\s*|(?:sudo|nohup|time|exec|command|builtin|caffeinate|env|xargs)\s+(?:-\S+\s+)*|\w+=\S*\s+|(?:ba|z)?sh\s+-\w*c\s+|eval\s+|["'`]\s*)/;
64
+ function segments(command) {
65
+ const flat = command.replace(/\d*>&\d*/g, " ").replace(/&>>?/g, " ");
66
+ return flat.split(/&&|\|\||;|\n|&(?=\s|$)/).map((s) => s.trim()).filter(Boolean);
67
+ }
68
+ function unwrap(seg) {
69
+ let s = seg;
70
+ for (let i = 0; i < 8; i++) {
71
+ const m = WRAPPER.exec(s);
72
+ if (!m?.[0])
73
+ break;
74
+ s = s.slice(m[0].length);
75
+ }
76
+ return s;
77
+ }
78
+ const curlDownloads = (s) => /(?:^|\s)(?:-[A-Za-z]*[oO][A-Za-z]*|--output(?:-dir)?|--remote-name(?:-all)?)(?=\s|=|$)/.test(s) || />\s*\S/.test(s) || /\|\s*(?:sudo\s+)?(?:ba|z)?sh\b/.test(s);
79
+ function stackOf(text) {
80
+ return STACKS.find((st) => st.re.test(text));
81
+ }
82
+ const blockMsg = (tools) => `error: not run — downloading model weights onto this box is blocked. It pins GBs on the machine that serves ` +
83
+ `your own model (decode collapses to ~1 tok/s) and rebuilds a capability you ALREADY HAVE: ${tools}. Use ` +
84
+ `that. If it errors, the worker is DOWN (an outage): retry once, then TELL THE OWNER — never install ` +
85
+ `tooling or fetch weights to substitute. Only the owner can lift this block.`;
86
+ const redirectMsg = (tools) => `error: not run — you already have ${tools} for this — use it. A worker being down is an outage to ` +
87
+ `retry/report to the owner, not something to install or rebuild. If this install is genuinely unrelated ` +
88
+ `to image/video/vision/voice, re-issue the identical command once and it will run.`;
89
+ /** Classify one shell command. Pure: no env, no state. null = nothing to say. */
90
+ export function classifyToolingCommand(command) {
91
+ // An inline python snippet (-c, or a heredoc whose lines are not command-position) that calls the HF
92
+ // weight downloaders is a weight download however it is split.
93
+ if (/^\s*python3?\b/.test(command) && HF_PY.test(command)) {
94
+ const stack = stackOf(command);
95
+ return { kind: "block", capability: stack?.name ?? "media", message: blockMsg(stack?.tools ?? GENERIC_TOOLS) };
96
+ }
97
+ let redirect = null;
98
+ for (const raw of segments(command)) {
99
+ const seg = unwrap(raw);
100
+ const stack = stackOf(seg);
101
+ const hfDownload = /^(?:hf|huggingface-cli)\s+(?:-\S+\s+)*download\b/.test(seg) && !/--repo-type[=\s]+(?:dataset|space)\b/.test(seg);
102
+ const fetcher = /^(?:wget|aria2c|axel)\b/.test(seg) || (/^curl\b/.test(seg) && curlDownloads(seg)) || /^git\s+lfs\s+(?:pull|clone)\b/.test(seg);
103
+ if (hfDownload || (fetcher && (WEIGHT_EXT.test(seg) || HF_RESOLVE.test(seg)))) {
104
+ const tools = stack?.tools ?? GENERIC_TOOLS;
105
+ return { kind: "block", capability: stack?.name ?? "media", message: blockMsg(tools) };
106
+ }
107
+ if (!redirect && stack && (fetcher || INSTALL_VERBS.some((re) => re.test(seg)))) {
108
+ redirect = { kind: "redirect", capability: stack.name, message: redirectMsg(stack.tools) };
109
+ }
110
+ }
111
+ return redirect;
112
+ }
113
+ // Flag- and pip-spelling-insensitive identity of a command: `pip install -U comfy-cli`, `pip3 install comfy-cli
114
+ // --break-system-packages` and `python3 -m pip install comfy-cli` are the same attempt, so the second is the
115
+ // "identical" one that passes.
116
+ function attemptKey(command) {
117
+ return command.toLowerCase().replace(/\s+/g, " ")
118
+ .replace(/\b(?:python3?(?:\.\d+)? -m pip3?|uv pip|pip3)\b/g, "pip")
119
+ .replace(/(?:^| )--?[a-z][\w-]*(?:=\S+)?(?= |$)/g, "").trim();
120
+ }
121
+ const SEEN_MAX = 200;
122
+ /** The shell hook. Returns the tool-result string to answer with INSTEAD of running the command, or null to
123
+ * run it. `seen` carries the redirect-once memory (the caller owns it, module-level in registry.ts). */
124
+ export function nativeToolRedirect(command, seen, env = process.env) {
125
+ const allow = env.KODE_ALLOW_TOOLING_INSTALL;
126
+ if (allow === "1" || allow === "true")
127
+ return null;
128
+ const v = classifyToolingCommand(command);
129
+ if (!v)
130
+ return null;
131
+ if (v.kind === "block")
132
+ return v.message;
133
+ const key = attemptKey(command);
134
+ if (seen.has(key))
135
+ return null; // the identical attempt, re-issued after the redirect: let it through
136
+ if (seen.size >= SEEN_MAX)
137
+ seen.clear();
138
+ seen.add(key);
139
+ return v.message;
140
+ }
@@ -17,3 +17,157 @@ export function expandPath(p) {
17
17
  return home + p.slice(1);
18
18
  return p;
19
19
  }
20
+ /** Strip trailing slashes; keep "/" as "/". */
21
+ function normPath(p) {
22
+ if (!p)
23
+ return p;
24
+ const n = p.replace(/\/+$/, "");
25
+ return n || "/";
26
+ }
27
+ /**
28
+ * Resolve a search path to an absolute string without node:path (neutral-safe).
29
+ * Relative paths join against `cwd` (default process.cwd()).
30
+ */
31
+ export function resolveSearchPath(raw, cwd) {
32
+ const expanded = expandPath(raw || ".");
33
+ if (expanded.startsWith("/") || /^[A-Za-z]:[\\/]/.test(expanded))
34
+ return normPath(expanded);
35
+ const base = normPath(cwd ?? (typeof process !== "undefined" ? process.cwd() : ".")) || ".";
36
+ if (expanded === "." || expanded === "")
37
+ return base;
38
+ return normPath(`${base}/${expanded}`);
39
+ }
40
+ /**
41
+ * Hard gate for content search (`search` / `grep_search`): refuse scopes that walk the whole
42
+ * machine or the owner's home. Soft "never search ~" in the tool description is not enough —
43
+ * models still pass path=$HOME and hang for minutes (300s tool timeout). Point them at
44
+ * find_files (name index) or a narrow subtree instead.
45
+ *
46
+ * Returns an error string to surface to the model, or null when the scope is OK.
47
+ */
48
+ export function assertContentSearchScope(rawPath, cwd) {
49
+ const abs = resolveSearchPath(rawPath || ".", cwd);
50
+ const home = normPath(process.env.HOME ?? process.env.USERPROFILE ?? "");
51
+ // Top-level roots that always mean "search the world".
52
+ const BROAD = new Set([
53
+ "/",
54
+ "/Users",
55
+ "/home",
56
+ "/private",
57
+ "/var",
58
+ "/private/var",
59
+ "/tmp",
60
+ "/private/tmp",
61
+ "/System",
62
+ "/Library",
63
+ "/Applications",
64
+ ]);
65
+ if (BROAD.has(abs) || (home && abs === home)) {
66
+ return (`error: content search refused at "${rawPath || "."}" (resolves to ${abs}) — that scope is ` +
67
+ `home/machine-wide and will hang for minutes. For a FILE/PROJECT NAME use find_files ` +
68
+ `{"query":"…"}. For CONTENT, pass a narrow path (cwd subtree, ~/work/<repo>, ~/.axe/repos/<name>). ` +
69
+ `For a fleet host/box (e.g. acropolis): read ~/.ssh/config, \`gh repo view\`, AGENTS.md — ` +
70
+ `do not ripgrep $HOME.`);
71
+ }
72
+ return null;
73
+ }
74
+ const SHELL_REFUSE_MSG = "error: shell content search refused — recursive grep/rg/find over $HOME (or /, /Users, /tmp) " +
75
+ "hangs for minutes. Use the `search` tool with a narrow path, or find_files for names. " +
76
+ "For a fleet host/box: ~/.ssh/config → gh repo view → AGENTS.md. Do not bypass with bash.";
77
+ /**
78
+ * Heuristic gate for bash / shell_exec / shell_background: catch the common bypass where the model
79
+ * is refused by `search(path=$HOME)` and retries via `grep -r … ~` / `rg … $HOME` / unbounded `find ~`.
80
+ * Conservative — only fires on recursive content tools aimed at a broad root, not every mention of HOME.
81
+ */
82
+ export function assertShellHomeWideSearch(command) {
83
+ if (!command || !command.trim())
84
+ return null;
85
+ const home = normPath(process.env.HOME ?? process.env.USERPROFILE ?? "");
86
+ // Normalize absolute home + ~ + $HOME spellings so one matcher covers all.
87
+ let c = command;
88
+ if (home) {
89
+ // Escape for literal split; replace all occurrences with the token $HOME.
90
+ c = c.split(home).join("$HOME");
91
+ }
92
+ c = c.replace(/(^|[\s"'=(])~(?=\/|[\s"')|;&#`$]|$)/g, "$1$HOME");
93
+ c = c.replace(/\$\{HOME\}/g, "$HOME");
94
+ // Bare roots only — `$HOME/work/foo` must still be allowed. Trailing slash OK; a subpath is not.
95
+ // (Avoid template literals here — a backtick in the char class would terminate them.)
96
+ const bare = "(?:^|[\\s\"'=(])(?:\\$HOME|/Users|/home|/tmp|/)\\/?(?=[\\s\"')|;&#`]|$)";
97
+ const hasBareBroadRoot = new RegExp(bare).test(c);
98
+ if (!hasBareBroadRoot)
99
+ return null;
100
+ // grep/egrep/fgrep with a recursive flag (-r, -R, or combined like -rn / -rni).
101
+ const isRecursiveGrep = /\b(?:grep|egrep|fgrep|ggrep)\b/.test(c) &&
102
+ /(?:^|\s)-[A-Za-z0-9-]*[rR][A-Za-z0-9-]*(?:\s|=|$)/.test(c);
103
+ const isRg = /\brg\b/.test(c);
104
+ // find $HOME / /Users without a tight maxdepth (1–3) — the classic hang.
105
+ const isFindHome = /\bfind\b/.test(c) &&
106
+ !/-maxdepth\s+[123]\b/.test(c);
107
+ if (isRecursiveGrep || isRg || isFindHome)
108
+ return SHELL_REFUSE_MSG;
109
+ return null;
110
+ }
111
+ export const BLIND_DOWNLOAD_MSG = "refused: `curl … -o/-O <file>` without `-f` / `--fail` (or `--fail-with-body`) saves an HTTP error page as if it were the file, and " +
112
+ "a silent `-s` hides that it happened — the 0-byte / HTML-instead-of-ISO failure. Use the `download_file` tool " +
113
+ "(fails loudly, resumes, retries, verifies size/sha256, never leaves a half file), or add `-f` (and `--retry 5 -C -`) " +
114
+ "and check the size afterwards. Run long transfers with shell_background or download_file, one at a time.";
115
+ /**
116
+ * Hard gate for bash / shell_exec / shell_background: a `curl` that writes to a FILE without failing on HTTP errors.
117
+ * Soft persona guidance did not stop the model; this does. Conservative — a response written to /dev/null or stdout,
118
+ * `curl … | jq`, and `curl -fsSL -o f url` all pass.
119
+ */
120
+ export function assertShellBlindDownload(command) {
121
+ if (!command || !/\bcurl\b/.test(command))
122
+ return null;
123
+ // Split on shell separators so `curl -f … && curl -o f …` is judged per command.
124
+ for (const seg of command.split(/&&|\|\||;|\||\n|&(?!>)/)) {
125
+ const toks = seg.trim().split(/\s+/);
126
+ // The command word: skip `VAR=val`, sudo/nohup/time/command and `timeout N` so `echo "use curl -o x"` is not mistaken for a curl.
127
+ let k = 0;
128
+ while (k < toks.length && (/^[A-Za-z_][A-Za-z0-9_]*=/.test(toks[k]) || ["sudo", "nohup", "time", "command", "exec"].includes(toks[k]) || (toks[k] === "timeout" && ++k)))
129
+ k++;
130
+ if (!(toks[k] === "curl" || toks[k]?.endsWith("/curl")))
131
+ continue;
132
+ const args = toks.slice(k + 1);
133
+ let toFile = false;
134
+ let fails = false;
135
+ for (let i = 0; i < args.length; i++) {
136
+ const a = args[i];
137
+ if (a === "-O" || a === "--remote-name" || a === "--remote-name-all") {
138
+ toFile = true;
139
+ continue;
140
+ }
141
+ if (a === "-o" || a === "--output") {
142
+ const target = args[i + 1] ?? "";
143
+ if (target !== "/dev/null" && target !== "-" && target !== "NUL")
144
+ toFile = true;
145
+ i++;
146
+ continue;
147
+ }
148
+ if (/^--output=/.test(a)) {
149
+ if (!/^--output=(\/dev\/null|-|NUL)$/.test(a))
150
+ toFile = true;
151
+ continue;
152
+ }
153
+ if (a === "--fail" || a === "--fail-with-body" || a === "--fail-early") {
154
+ fails = true;
155
+ continue;
156
+ }
157
+ // combined short flags: -fsSL, -sfL, -fL … ; an arg that is -o/-O fused (-sLo file) also writes to a file
158
+ if (/^-[A-Za-z]+$/.test(a)) {
159
+ if (a.includes("f"))
160
+ fails = true;
161
+ if (a.includes("O"))
162
+ toFile = true;
163
+ if (a.endsWith("o") && a.length > 2) {
164
+ toFile = !/^(\/dev\/null|-|NUL)$/.test(args[i + 1] ?? "");
165
+ i++;
166
+ }
167
+ }
168
+ }
169
+ if (toFile && !fails)
170
+ return BLIND_DOWNLOAD_MSG;
171
+ }
172
+ return null;
173
+ }