lazyclaw 4.2.2 → 5.0.0

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 (67) hide show
  1. package/README.ko.md +44 -0
  2. package/README.md +172 -353
  3. package/agents.mjs +19 -3
  4. package/channels/handoff.mjs +41 -0
  5. package/channels/loader.mjs +124 -0
  6. package/channels/matrix.mjs +417 -0
  7. package/channels/telegram.mjs +362 -0
  8. package/channels/threads.mjs +116 -0
  9. package/cli.mjs +730 -27
  10. package/daemon.mjs +111 -0
  11. package/gateway/device_auth.mjs +664 -0
  12. package/gateway/http_gateway.mjs +304 -0
  13. package/mas/agent_memory.mjs +35 -34
  14. package/mas/agent_turn.mjs +30 -1
  15. package/mas/confidence.mjs +108 -0
  16. package/mas/index_db.mjs +242 -0
  17. package/mas/mention_router.mjs +75 -4
  18. package/mas/nudge.mjs +97 -0
  19. package/mas/prompt_stack.mjs +80 -0
  20. package/mas/provider_adapters.mjs +83 -0
  21. package/mas/redact.mjs +46 -0
  22. package/mas/skill_synth.mjs +331 -0
  23. package/mas/tool_runner.mjs +19 -48
  24. package/mas/tools/browser.mjs +77 -0
  25. package/mas/tools/clarify.mjs +36 -0
  26. package/mas/tools/coding.mjs +109 -0
  27. package/mas/tools/delegation.mjs +53 -0
  28. package/mas/tools/edit.mjs +36 -0
  29. package/mas/tools/git.mjs +110 -0
  30. package/mas/tools/ha.mjs +34 -0
  31. package/mas/tools/learning.mjs +168 -0
  32. package/mas/tools/media.mjs +105 -0
  33. package/mas/tools/os.mjs +152 -0
  34. package/mas/tools/patch.mjs +91 -0
  35. package/mas/tools/recall.mjs +103 -0
  36. package/mas/tools/registry.mjs +93 -0
  37. package/mas/tools/scheduling.mjs +62 -0
  38. package/mas/tools/skill_view.mjs +43 -0
  39. package/mas/tools/web.mjs +137 -0
  40. package/mas/toolsets.mjs +64 -0
  41. package/mas/trajectory_export.mjs +169 -0
  42. package/mas/trajectory_store.mjs +179 -0
  43. package/mas/user_modeler.mjs +108 -0
  44. package/package.json +22 -3
  45. package/providers/codex_cli.mjs +200 -0
  46. package/providers/gemini_cli.mjs +179 -0
  47. package/providers/registry.mjs +61 -1
  48. package/sandbox/base.mjs +82 -0
  49. package/sandbox/confiners/bubblewrap.mjs +21 -0
  50. package/sandbox/confiners/firejail.mjs +16 -0
  51. package/sandbox/confiners/landlock.mjs +14 -0
  52. package/sandbox/confiners/seatbelt.mjs +28 -0
  53. package/sandbox/daytona.mjs +37 -0
  54. package/sandbox/docker.mjs +91 -0
  55. package/sandbox/index.mjs +67 -0
  56. package/sandbox/local.mjs +59 -0
  57. package/sandbox/modal.mjs +53 -0
  58. package/sandbox/singularity.mjs +39 -0
  59. package/sandbox/ssh.mjs +56 -0
  60. package/sandbox.mjs +11 -127
  61. package/scripts/hermes-import.mjs +111 -0
  62. package/scripts/migrate-v5.mjs +342 -0
  63. package/scripts/openclaw-import.mjs +71 -0
  64. package/sessions.mjs +20 -1
  65. package/skills.mjs +101 -8
  66. package/skills_curator.mjs +323 -0
  67. package/workspace.mjs +18 -3
@@ -0,0 +1,200 @@
1
+ // Codex subscription/CLI provider (no API key).
2
+ //
3
+ // Spawns the local `codex` CLI binary (OpenAI's Codex CLI shipped from
4
+ // npm: @openai/codex) in non-interactive mode:
5
+ //
6
+ // codex exec --skip-git-repo-check --json [-m model] "<prompt>"
7
+ //
8
+ // Auth is whatever `codex` is signed into — a ChatGPT Plus/Pro/Business
9
+ // session bound to ~/.codex/auth — so no OPENAI_API_KEY is required and
10
+ // no key lands in the lazyclaw config. This is the CLI counterpart to
11
+ // providers/openai.mjs (which talks to api.openai.com and needs an API
12
+ // key).
13
+ //
14
+ // Output protocol (NDJSON on stdout, one JSON object per line):
15
+ // {"type":"thread.started", "thread_id": "..."}
16
+ // {"type":"turn.started"}
17
+ // {"type":"item.completed", "item": {"type":"agent_message","text":"..."}}
18
+ // {"type":"turn.completed", "usage": {...}}
19
+ //
20
+ // We stream-parse line-by-line and yield the `text` field of every
21
+ // agent_message item. Reasoning items (`item.type === 'reasoning'`)
22
+ // are skipped — they're internal thought summaries, not the visible
23
+ // answer. Usage from turn.completed is surfaced via onUsage.
24
+ //
25
+ // Why `--skip-git-repo-check` is hard-coded: lazyclaw orchestrator
26
+ // runs workers from scratch dirs (often /tmp) that aren't git repos,
27
+ // and codex's default git-repo gate would reject every such spawn.
28
+ // The flag is a UX shim for headless invocation, not a security
29
+ // downgrade — we're handing codex our own prompt, not user-trusted
30
+ // shell input.
31
+
32
+ import { spawnSandboxed } from '../sandbox.mjs';
33
+
34
+ class AbortError extends Error {
35
+ constructor(message = 'aborted') {
36
+ super(message);
37
+ this.name = 'AbortError';
38
+ this.code = 'ABORT';
39
+ }
40
+ }
41
+
42
+ class CliMissingError extends Error {
43
+ constructor() {
44
+ super('codex CLI not found in PATH — install @openai/codex or use the openai API provider');
45
+ this.name = 'CodexCliMissingError';
46
+ this.code = 'CLI_MISSING';
47
+ }
48
+ }
49
+
50
+ class CliExitError extends Error {
51
+ constructor(code, signal, stderr) {
52
+ super(`codex CLI exited ${code ?? signal}: ${String(stderr).slice(0, 400)}`);
53
+ this.name = 'CodexCliExitError';
54
+ this.code = 'CLI_EXIT';
55
+ this.exitCode = code;
56
+ this.signal = signal;
57
+ this.stderr = stderr;
58
+ }
59
+ }
60
+
61
+ // Map a few friendly aliases. Codex CLI accepts the literal model id
62
+ // (e.g. gpt-5-codex) directly, so unknown inputs pass through.
63
+ const _ALIASES = {
64
+ codex: 'gpt-5-codex',
65
+ 'gpt-codex': 'gpt-5-codex',
66
+ };
67
+
68
+ // Drop cross-vendor model ids (claude-*, gemini-*) silently so the
69
+ // CLI falls back to its own default. The orchestrator workflow forwards
70
+ // cfg.model verbatim to every worker, and `providers test` does the
71
+ // same — both would otherwise crash here when cfg.model is Anthropic
72
+ // or Google.
73
+ function resolveModel(model) {
74
+ if (!model) return '';
75
+ const lower = String(model).toLowerCase();
76
+ if (_ALIASES[lower]) return _ALIASES[lower];
77
+ if (
78
+ lower.startsWith('gpt-') ||
79
+ lower.startsWith('o1') ||
80
+ lower.startsWith('o3') ||
81
+ lower.startsWith('o4')
82
+ ) return String(model);
83
+ return '';
84
+ }
85
+
86
+ function buildPrompt(messages, system) {
87
+ const parts = [];
88
+ if (system) parts.push(`[System instructions: ${system}]`);
89
+ for (const m of messages) {
90
+ if (!m || !m.content) continue;
91
+ if (m.role === 'system' && !system) parts.push(`[System instructions: ${m.content}]`);
92
+ else if (m.role === 'user') parts.push(`User: ${m.content}`);
93
+ else if (m.role === 'assistant') parts.push(`Assistant: ${m.content}`);
94
+ }
95
+ return parts.length ? parts.join('\n') + '\n\nAssistant:' : '';
96
+ }
97
+
98
+ // Pull text out of a single NDJSON event. We only surface agent_message
99
+ // items (the visible answer). Reasoning summaries are intentionally
100
+ // dropped so the orchestrator's planner doesn't see the model's
101
+ // internal deliberation as part of "the worker's answer".
102
+ function extractEventText(obj) {
103
+ if (!obj || typeof obj !== 'object') return '';
104
+ if (obj.type !== 'item.completed') return '';
105
+ const item = obj.item || {};
106
+ if (item.type === 'agent_message' && typeof item.text === 'string') return item.text;
107
+ return '';
108
+ }
109
+
110
+ function extractUsage(obj) {
111
+ if (!obj || typeof obj !== 'object' || obj.type !== 'turn.completed') return null;
112
+ const u = obj.usage || {};
113
+ const input = (u.input_tokens ?? 0) + (u.cached_input_tokens ?? 0);
114
+ const output = (u.output_tokens ?? 0) + (u.reasoning_output_tokens ?? 0);
115
+ if (!input && !output) return null;
116
+ return { inputTokens: input, outputTokens: output, totalCostUsd: 0 };
117
+ }
118
+
119
+ export const codexCliProvider = {
120
+ name: 'codex-cli',
121
+ async *sendMessage(messages, opts = {}) {
122
+ const bin = opts.bin || 'codex';
123
+ const prompt = buildPrompt(messages, opts.system || messages.find(m => m.role === 'system')?.content);
124
+ if (!prompt) return;
125
+
126
+ const args = ['exec', '--skip-git-repo-check', '--json'];
127
+ const model = resolveModel(opts.model);
128
+ if (model) args.push('-m', model);
129
+ args.push(prompt);
130
+
131
+ if (opts.signal?.aborted) throw new AbortError('aborted before spawn');
132
+
133
+ let proc;
134
+ try {
135
+ proc = spawnSandboxed(opts.sandbox || null, bin, args, {
136
+ cwd: opts.cwd || process.cwd(),
137
+ stdio: ['ignore', 'pipe', 'pipe'],
138
+ });
139
+ } catch (err) {
140
+ if (err && err.code === 'ENOENT') throw new CliMissingError();
141
+ throw err;
142
+ }
143
+
144
+ const onAbort = () => { try { proc.kill('SIGTERM'); } catch (_) { /* ignore */ } };
145
+ if (opts.signal) opts.signal.addEventListener('abort', onAbort);
146
+
147
+ let stderr = '';
148
+ proc.stderr.setEncoding('utf8');
149
+ proc.stderr.on('data', (chunk) => { stderr += chunk; });
150
+
151
+ proc.stdout.setEncoding('utf8');
152
+ let buffer = '';
153
+ let exitInfo = null;
154
+ const exitPromise = new Promise((resolve) => {
155
+ proc.on('close', (code, signal) => {
156
+ exitInfo = { code, signal };
157
+ resolve();
158
+ });
159
+ });
160
+
161
+ try {
162
+ for await (const chunk of proc.stdout) {
163
+ if (opts.signal?.aborted) throw new AbortError('aborted mid-stream');
164
+ buffer += chunk;
165
+ let nl;
166
+ while ((nl = buffer.indexOf('\n')) >= 0) {
167
+ const line = buffer.slice(0, nl).trim();
168
+ buffer = buffer.slice(nl + 1);
169
+ if (!line) continue;
170
+ let obj;
171
+ try { obj = JSON.parse(line); } catch { continue; }
172
+ const text = extractEventText(obj);
173
+ if (text) yield text;
174
+ const usage = extractUsage(obj);
175
+ if (usage && typeof opts.onUsage === 'function') {
176
+ try { opts.onUsage(usage); } catch (_) { /* never break stream on usage */ }
177
+ }
178
+ }
179
+ }
180
+ if (buffer.trim()) {
181
+ try {
182
+ const obj = JSON.parse(buffer.trim());
183
+ const text = extractEventText(obj);
184
+ if (text) yield text;
185
+ } catch (_) { /* incomplete tail — drop */ }
186
+ }
187
+ await exitPromise;
188
+ if (exitInfo && exitInfo.code !== 0 && !opts.signal?.aborted) {
189
+ throw new CliExitError(exitInfo.code, exitInfo.signal, stderr);
190
+ }
191
+ } finally {
192
+ if (opts.signal) opts.signal.removeEventListener('abort', onAbort);
193
+ if (!proc.killed && exitInfo === null) {
194
+ try { proc.kill('SIGTERM'); } catch (_) { /* ignore */ }
195
+ }
196
+ }
197
+ },
198
+ };
199
+
200
+ export { CliMissingError, CliExitError, AbortError, resolveModel, buildPrompt, extractEventText, extractUsage };
@@ -0,0 +1,179 @@
1
+ // Gemini subscription/CLI provider (no API key).
2
+ //
3
+ // Spawns the local `gemini` CLI binary (the Google Gemini CLI shipped
4
+ // from npm: @google/gemini-cli) in non-interactive mode:
5
+ //
6
+ // gemini --skip-trust -p "<prompt>" -o json [-m model]
7
+ //
8
+ // Auth is whatever `gemini` is already signed into on the host —
9
+ // Google account / Vertex / AI Studio session — so no GEMINI_API_KEY
10
+ // is required and no key lands in the lazyclaw config. This is the
11
+ // CLI counterpart to providers/gemini.mjs (which talks to the
12
+ // Generative Language API and needs an API key).
13
+ //
14
+ // Output shape (one JSON object on stdout):
15
+ // { session_id, response: "<text>", stats: { models: {...} } }
16
+ //
17
+ // We capture stdout in full, parse the JSON once, and yield the
18
+ // `response` string in a single chunk. The orchestrator and chat
19
+ // REPL both treat AsyncIterable<string> as their only contract, so
20
+ // non-streaming is transparent to callers.
21
+ //
22
+ // Why `--skip-trust` is hard-coded: lazyclaw orchestrator / workflow
23
+ // subprocesses commonly run from /tmp or scratch dirs which gemini's
24
+ // trusted-folder policy rejects. We are spawning gemini ourselves with
25
+ // our own prompt, so the policy adds friction without security value.
26
+
27
+ import { spawnSandboxed } from '../sandbox.mjs';
28
+
29
+ class AbortError extends Error {
30
+ constructor(message = 'aborted') {
31
+ super(message);
32
+ this.name = 'AbortError';
33
+ this.code = 'ABORT';
34
+ }
35
+ }
36
+
37
+ class CliMissingError extends Error {
38
+ constructor() {
39
+ super('gemini CLI not found in PATH — install @google/gemini-cli or use the gemini API provider');
40
+ this.name = 'GeminiCliMissingError';
41
+ this.code = 'CLI_MISSING';
42
+ }
43
+ }
44
+
45
+ class CliExitError extends Error {
46
+ constructor(code, signal, stderr) {
47
+ super(`gemini CLI exited ${code ?? signal}: ${String(stderr).slice(0, 400)}`);
48
+ this.name = 'GeminiCliExitError';
49
+ this.code = 'CLI_EXIT';
50
+ this.exitCode = code;
51
+ this.signal = signal;
52
+ this.stderr = stderr;
53
+ }
54
+ }
55
+
56
+ // `gemini -m` accepts the marketing model id directly (e.g. gemini-2.5-pro),
57
+ // so unlike claude_cli no alias map is needed. We still normalize a few
58
+ // shorthand inputs that users have typed in chat config.
59
+ const _ALIASES = {
60
+ pro: 'gemini-2.5-pro',
61
+ flash: 'gemini-2.5-flash',
62
+ 'gemini-pro': 'gemini-2.5-pro',
63
+ 'gemini-flash': 'gemini-2.5-flash',
64
+ };
65
+
66
+ // Drop cross-vendor model ids (claude-*, gpt-*, o1/o3/o4-*) silently so the
67
+ // CLI falls back to its own default. The orchestrator workflow happily
68
+ // dispatches the same cfg.model to every worker, and `providers test`
69
+ // forwards cfg.model verbatim — both would otherwise crash here with
70
+ // "ModelNotFoundError" the moment cfg.model is anything Anthropic/OpenAI.
71
+ function resolveModel(model) {
72
+ if (!model) return '';
73
+ const lower = String(model).toLowerCase();
74
+ if (_ALIASES[lower]) return _ALIASES[lower];
75
+ if (lower.startsWith('gemini') || lower.startsWith('gemma')) return String(model);
76
+ return '';
77
+ }
78
+
79
+ function buildPrompt(messages, system) {
80
+ const parts = [];
81
+ if (system) parts.push(`[System instructions: ${system}]`);
82
+ for (const m of messages) {
83
+ if (!m || !m.content) continue;
84
+ if (m.role === 'system' && !system) parts.push(`[System instructions: ${m.content}]`);
85
+ else if (m.role === 'user') parts.push(`User: ${m.content}`);
86
+ else if (m.role === 'assistant') parts.push(`Assistant: ${m.content}`);
87
+ }
88
+ return parts.length ? parts.join('\n') + '\n\nAssistant:' : '';
89
+ }
90
+
91
+ // Pull usage totals out of the gemini stats block. The shape is
92
+ // `stats.models["<model-id>"].api` + `.tokens`. We sum across all
93
+ // model entries since a single response can route through multiple
94
+ // (e.g. flash for reasoning + pro for the final answer).
95
+ function extractUsage(stats) {
96
+ if (!stats || typeof stats !== 'object') return null;
97
+ const models = stats.models || {};
98
+ let input = 0, output = 0;
99
+ for (const m of Object.values(models)) {
100
+ const t = m?.tokens || {};
101
+ input += (t.prompt ?? t.input ?? 0);
102
+ output += (t.candidates ?? t.output ?? 0);
103
+ }
104
+ if (!input && !output) return null;
105
+ return { inputTokens: input, outputTokens: output, totalCostUsd: 0 };
106
+ }
107
+
108
+ export const geminiCliProvider = {
109
+ name: 'gemini-cli',
110
+ async *sendMessage(messages, opts = {}) {
111
+ const bin = opts.bin || 'gemini';
112
+ const prompt = buildPrompt(messages, opts.system || messages.find(m => m.role === 'system')?.content);
113
+ if (!prompt) return;
114
+
115
+ const args = ['--skip-trust', '-p', prompt, '-o', 'json'];
116
+ const model = resolveModel(opts.model);
117
+ if (model) args.push('-m', model);
118
+
119
+ if (opts.signal?.aborted) throw new AbortError('aborted before spawn');
120
+
121
+ let proc;
122
+ try {
123
+ proc = spawnSandboxed(opts.sandbox || null, bin, args, {
124
+ cwd: opts.cwd || process.cwd(),
125
+ stdio: ['ignore', 'pipe', 'pipe'],
126
+ env: { ...process.env, GEMINI_CLI_TRUST_WORKSPACE: 'true' },
127
+ });
128
+ } catch (err) {
129
+ if (err && err.code === 'ENOENT') throw new CliMissingError();
130
+ throw err;
131
+ }
132
+
133
+ const onAbort = () => { try { proc.kill('SIGTERM'); } catch (_) { /* ignore */ } };
134
+ if (opts.signal) opts.signal.addEventListener('abort', onAbort);
135
+
136
+ let stdout = '';
137
+ let stderr = '';
138
+ proc.stdout.setEncoding('utf8');
139
+ proc.stderr.setEncoding('utf8');
140
+ proc.stdout.on('data', (c) => { stdout += c; });
141
+ proc.stderr.on('data', (c) => { stderr += c; });
142
+
143
+ const exitInfo = await new Promise((resolve) => {
144
+ proc.on('close', (code, signal) => resolve({ code, signal }));
145
+ });
146
+
147
+ try {
148
+ if (opts.signal?.aborted) throw new AbortError('aborted mid-run');
149
+ if (exitInfo.code !== 0) {
150
+ throw new CliExitError(exitInfo.code, exitInfo.signal, stderr || stdout);
151
+ }
152
+ // Gemini emits one JSON document; some installations prefix it
153
+ // with a "Ripgrep is not available. Falling back to GrepTool."
154
+ // line on stderr (harmless) and never touch stdout. Defensive
155
+ // parse: find the first `{` to skip any pre-banner noise.
156
+ const first = stdout.indexOf('{');
157
+ const payload = first >= 0 ? stdout.slice(first) : stdout;
158
+ let parsed;
159
+ try {
160
+ parsed = JSON.parse(payload);
161
+ } catch (err) {
162
+ throw new Error(`gemini CLI returned non-JSON stdout: ${err.message} :: ${payload.slice(0, 200)}`);
163
+ }
164
+ const text = typeof parsed.response === 'string' ? parsed.response : '';
165
+ if (text) yield text;
166
+ const usage = extractUsage(parsed.stats);
167
+ if (usage && typeof opts.onUsage === 'function') {
168
+ try { opts.onUsage(usage); } catch (_) { /* never break stream on usage callback */ }
169
+ }
170
+ } finally {
171
+ if (opts.signal) opts.signal.removeEventListener('abort', onAbort);
172
+ if (!proc.killed && exitInfo === null) {
173
+ try { proc.kill('SIGTERM'); } catch (_) { /* ignore */ }
174
+ }
175
+ }
176
+ },
177
+ };
178
+
179
+ export { CliMissingError, CliExitError, AbortError, resolveModel, buildPrompt, extractUsage };
@@ -61,6 +61,61 @@ export { anthropicProvider, openaiProvider, ollamaProvider, geminiProvider, clau
61
61
  export { makeOpenAICompatProvider, fetchOpenAICompatModels };
62
62
  export { makeOrchestratorProvider };
63
63
 
64
+ // ─── Phase A: trainer resolver (spec §2.3, §2.4, canonical C3/C9) ───
65
+ //
66
+ // resolveTrainer(cfg, opts) returns { provider, model } for synthesis /
67
+ // reflection calls — split from the chat provider so users can route
68
+ // learning to a cheap model or to a CLI-subscription worker.
69
+ //
70
+ // Rules:
71
+ // - cfg.trainer omitted → mirror chat (v4 compat).
72
+ // - cfg.trainer.provider === 'auto' → claude-cli when Pro/Max
73
+ // session detected, else mirror chat (canonical decision C9).
74
+ // - opts.useFallback → parse cfg.trainer.fallback ('provider:model')
75
+ // and use it; missing pieces inherit from chat.
76
+ // - All identifiers MUST be kebab-case in user-facing config (C3).
77
+
78
+ export function parseProviderModel(spec) {
79
+ const s = String(spec || '');
80
+ const i = s.indexOf(':');
81
+ if (i < 0) return { provider: s || null, model: null };
82
+ return { provider: s.slice(0, i) || null, model: s.slice(i + 1) || null };
83
+ }
84
+
85
+ function _defaultDetectClaudeCli() {
86
+ // Phase A stub: real Pro/Max session detection arrives in Phase B
87
+ // (it requires reading the claude-cli OAuth token cache). Until
88
+ // then, treat presence of CLAUDE_CODE_OAUTH_TOKEN as a positive
89
+ // signal so users can opt-in explicitly.
90
+ return Boolean(process.env.CLAUDE_CODE_OAUTH_TOKEN);
91
+ }
92
+
93
+ export function resolveTrainer(cfg, opts = {}) {
94
+ const chatProvider = cfg && cfg.provider;
95
+ const chatModel = cfg && cfg.model;
96
+ const t = cfg && cfg.trainer;
97
+
98
+ if (!t || !t.provider) {
99
+ return { provider: chatProvider, model: chatModel };
100
+ }
101
+
102
+ if (opts.useFallback && t.fallback) {
103
+ const { provider, model } = parseProviderModel(t.fallback);
104
+ return {
105
+ provider: provider || chatProvider,
106
+ model: model || chatModel,
107
+ };
108
+ }
109
+
110
+ if (t.provider === 'auto') {
111
+ const detect = opts.detectClaudeCli || _defaultDetectClaudeCli;
112
+ if (detect()) return { provider: 'claude-cli', model: t.model || chatModel };
113
+ return { provider: chatProvider, model: t.model || chatModel };
114
+ }
115
+
116
+ return { provider: t.provider, model: t.model || chatModel };
117
+ }
118
+
64
119
  // Built-in OpenAI-compatible vendors. Same wire format → one factory call
65
120
  // each. The picker treats these like first-class providers so users don't
66
121
  // have to walk through "+ Add a custom endpoint" for the popular ones.
@@ -431,10 +486,15 @@ export function resolveBuiltinEnvKey(provider) {
431
486
  * Split a unified "provider/model" string (OpenClaw style:
432
487
  * "anthropic/claude-opus-4-7"). Also accepts a bare model id and returns
433
488
  * provider=null so callers can fall back to a separately-stored provider.
489
+ *
490
+ * Renamed from `parseProviderModel` in v5.0 to free that name for the
491
+ * trainer fallback parser (which uses ':' as the separator — see spec
492
+ * §2.4). Callers that still need the slash form should import this.
493
+ *
434
494
  * @param {string} s
435
495
  * @returns {{ provider: string|null, model: string }}
436
496
  */
437
- export function parseProviderModel(s) {
497
+ export function parseSlashProviderModel(s) {
438
498
  if (!s || typeof s !== 'string') return { provider: null, model: '' };
439
499
  const slash = s.indexOf('/');
440
500
  if (slash > 0) {
@@ -0,0 +1,82 @@
1
+ // sandbox/base.mjs — Sandbox + SandboxSession contracts.
2
+ //
3
+ // Spec ref: §0.1 C8 (6-enum), §6 (backend contract).
4
+ // Every backend module (docker/local/ssh/singularity/modal/daytona)
5
+ // exports a class extending Sandbox and returns SandboxSession from
6
+ // open(). The session is the only object that holds resources
7
+ // (sockets, child PIDs, remote workspace ids) — caller MUST call
8
+ // close() in a finally block.
9
+
10
+ export const SANDBOX_KINDS = Object.freeze([
11
+ 'local', 'docker', 'ssh', 'singularity', 'modal', 'daytona',
12
+ ]);
13
+
14
+ export class SandboxError extends Error {
15
+ constructor(message, code) {
16
+ super(message);
17
+ this.name = 'SandboxError';
18
+ this.code = code || 'SANDBOX_ERR';
19
+ }
20
+ toString() {
21
+ return `${this.name}[${this.code}]: ${this.message}`;
22
+ }
23
+ }
24
+
25
+ export class Sandbox {
26
+ /**
27
+ * @param {{kind: string} & Record<string, unknown>} spec
28
+ * @param {{_skipAbstract?: boolean}} [opts]
29
+ */
30
+ constructor(spec, opts = {}) {
31
+ if (new.target === Sandbox && !opts._skipAbstract) {
32
+ throw new SandboxError(
33
+ 'Sandbox is abstract — instantiate a backend subclass',
34
+ 'SANDBOX_ABSTRACT',
35
+ );
36
+ }
37
+ if (!spec || !SANDBOX_KINDS.includes(spec.kind)) {
38
+ throw new SandboxError(
39
+ `unknown sandbox kind "${spec && spec.kind}" — expected one of ${SANDBOX_KINDS.join(', ')}`,
40
+ 'SANDBOX_BAD_KIND',
41
+ );
42
+ }
43
+ this.spec = spec;
44
+ }
45
+
46
+ /**
47
+ * Open a session. Subclasses MUST override.
48
+ * @returns {Promise<SandboxSession>}
49
+ */
50
+ async open() {
51
+ throw new SandboxError(`${this.constructor.name}.open() not implemented`, 'SANDBOX_NOT_IMPL');
52
+ }
53
+
54
+ /** Short human label for `lazyclaw sandbox list`. */
55
+ describe() { return `${this.spec.kind}`; }
56
+ }
57
+
58
+ export class SandboxSession {
59
+ /**
60
+ * Run an argv inside the sandbox.
61
+ * @param {string[]} argv
62
+ * @param {{cwd?: string, env?: Record<string,string>, stdio?: 'pipe'|'inherit', input?: string}} [opts]
63
+ * @returns {Promise<{code: number, stdout: string, stderr: string}>}
64
+ */
65
+ async exec(_argv, _opts) {
66
+ throw new SandboxError(`${this.constructor.name}.exec() not implemented`, 'SANDBOX_NOT_IMPL');
67
+ }
68
+
69
+ /**
70
+ * Spawn a long-running child within the sandbox. Returns a
71
+ * node:child_process-shaped object with stdin/stdout/stderr.
72
+ * Default: synthesise from exec() via streaming if backend allows.
73
+ */
74
+ async spawn(_argv, _opts) {
75
+ throw new SandboxError(`${this.constructor.name}.spawn() not implemented`, 'SANDBOX_NOT_IMPL');
76
+ }
77
+
78
+ /** Release resources. Idempotent. */
79
+ async close() {
80
+ throw new SandboxError(`${this.constructor.name}.close() not implemented`, 'SANDBOX_NOT_IMPL');
81
+ }
82
+ }
@@ -0,0 +1,21 @@
1
+ // sandbox/confiners/bubblewrap.mjs — Linux bwrap wrapper.
2
+
3
+ import { execFileSync } from 'node:child_process';
4
+
5
+ export function available() {
6
+ if (process.platform !== 'linux') return false;
7
+ try { execFileSync('bwrap', ['--version'], { stdio: 'ignore' }); return true; }
8
+ catch { return false; }
9
+ }
10
+
11
+ export function buildArgv(argv, opts = {}) {
12
+ const readOnly = opts.readOnly || ['/usr', '/lib', '/lib64', '/bin', '/etc'];
13
+ const readWrite = opts.readWrite || [process.cwd()];
14
+ const allowNet = opts.allowNet === true;
15
+ const out = ['bwrap', '--die-with-parent', '--proc', '/proc', '--dev', '/dev'];
16
+ for (const p of readOnly) out.push('--ro-bind', p, p);
17
+ for (const p of readWrite) out.push('--bind', p, p);
18
+ if (!allowNet) out.push('--unshare-net');
19
+ out.push('--unshare-pid', '--unshare-ipc', '--unshare-uts');
20
+ return [...out, ...argv];
21
+ }
@@ -0,0 +1,16 @@
1
+ // sandbox/confiners/firejail.mjs — firejail wrapper.
2
+
3
+ import { execFileSync } from 'node:child_process';
4
+
5
+ export function available() {
6
+ if (process.platform !== 'linux') return false;
7
+ try { execFileSync('firejail', ['--version'], { stdio: 'ignore' }); return true; }
8
+ catch { return false; }
9
+ }
10
+
11
+ export function buildArgv(argv, opts = {}) {
12
+ const allowNet = opts.allowNet === true;
13
+ const out = ['firejail', '--quiet', '--private', '--caps.drop=all'];
14
+ out.push(allowNet ? '--net=eth0' : '--net=none');
15
+ return [...out, ...argv];
16
+ }
@@ -0,0 +1,14 @@
1
+ // sandbox/confiners/landlock.mjs — Linux Landlock helper.
2
+ //
3
+ // Landlock is enforced from *inside* the process via the
4
+ // landlock_create_ruleset() syscall. With no native bindings available
5
+ // in plain Node, we currently emit the argv unchanged and let
6
+ // downstream tooling (e.g. a future `lazyclaw-landlock-shim` binary)
7
+ // install the ruleset. Returns argv unchanged on non-Linux.
8
+
9
+ export function available() { return process.platform === 'linux'; }
10
+
11
+ export function buildArgv(argv, _opts = {}) {
12
+ // Pass-through. Spec §0.1 C8 leaves room for a preloader binary.
13
+ return [...argv];
14
+ }
@@ -0,0 +1,28 @@
1
+ // sandbox/confiners/seatbelt.mjs — macOS sandbox-exec wrapper.
2
+ // Spec §0.1 C8: local.confiner sub-option.
3
+
4
+ import { execFileSync } from 'node:child_process';
5
+
6
+ export function available() {
7
+ if (process.platform !== 'darwin') return false;
8
+ try { execFileSync('sandbox-exec', ['-h'], { stdio: 'ignore' }); return true; }
9
+ catch { return false; }
10
+ }
11
+
12
+ export function buildArgv(argv, opts = {}) {
13
+ const readOnly = opts.readOnly || [];
14
+ const readWrite = opts.readWrite || [process.cwd()];
15
+ const allowNet = opts.allowNet === true;
16
+ const profile = [
17
+ '(version 1)',
18
+ '(deny default)',
19
+ '(allow process-fork)',
20
+ '(allow process-exec)',
21
+ '(allow signal)',
22
+ '(allow sysctl-read)',
23
+ allowNet ? '(allow network*)' : '(deny network*)',
24
+ ...readOnly.map(p => `(allow file-read* (subpath "${p}"))`),
25
+ ...readWrite.map(p => `(allow file-read* file-write* (subpath "${p}"))`),
26
+ ].join('\n');
27
+ return ['sandbox-exec', '-p', profile, ...argv];
28
+ }
@@ -0,0 +1,37 @@
1
+ // sandbox/daytona.mjs — Daytona workspace wrapper.
2
+
3
+ import { spawn, spawnSync } from 'node:child_process';
4
+ import { Sandbox, SandboxSession, SandboxError } from './base.mjs';
5
+
6
+ export function buildDaytonaArgv(spec, argv) {
7
+ if (!spec || !spec.workspace) {
8
+ throw new SandboxError('daytona sandbox requires workspace', 'SANDBOX_BAD_SPEC');
9
+ }
10
+ const out = ['daytona', 'ssh', spec.workspace];
11
+ if (spec.persistent === false) out.push('--auto-stop=true');
12
+ out.push('--', ...argv);
13
+ return out;
14
+ }
15
+
16
+ class DaytonaSession extends SandboxSession {
17
+ constructor(spec) { super(); this.spec = spec; }
18
+ async exec(argv, opts = {}) {
19
+ const a = buildDaytonaArgv(this.spec, argv);
20
+ const r = spawnSync(a[0], a.slice(1), {
21
+ input: opts.input, env: { ...process.env, ...(opts.env || {}) },
22
+ stdio: opts.stdio || 'pipe', encoding: 'utf8',
23
+ });
24
+ return { code: r.status ?? -1, stdout: r.stdout || '', stderr: r.stderr || '' };
25
+ }
26
+ async spawn(argv, opts = {}) {
27
+ const a = buildDaytonaArgv(this.spec, argv);
28
+ return spawn(a[0], a.slice(1), opts);
29
+ }
30
+ async close() {}
31
+ }
32
+
33
+ export class DaytonaSandbox extends Sandbox {
34
+ constructor(spec) { super(spec); }
35
+ async open() { return new DaytonaSession(this.spec); }
36
+ describe() { return `daytona · ${this.spec.workspace}`; }
37
+ }