@0perator/operator 0.1.321 → 0.1.322

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.
@@ -1,140 +0,0 @@
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
- }