zcode-agent 0.0.0-stage → 1.1.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.
- package/LICENSE +21 -0
- package/README.md +468 -2
- package/assets/acp-agents.md +11 -0
- package/assets/executor-agents.md +10 -0
- package/bin/zcode-agent-acp.js +11 -0
- package/bin/zcode-agent.js +442 -0
- package/lib/acp-map.js +543 -0
- package/lib/acp.js +763 -0
- package/lib/appserver.js +546 -0
- package/lib/bootstrap.js +71 -0
- package/lib/brief.js +109 -0
- package/lib/checks.js +104 -0
- package/lib/commands/acp.js +35 -0
- package/lib/commands/cost.js +67 -0
- package/lib/commands/doctor.js +179 -0
- package/lib/commands/exec.js +394 -0
- package/lib/commands/login.js +44 -0
- package/lib/commands/mcp-list.js +12 -0
- package/lib/commands/plan.js +302 -0
- package/lib/commands/sessions.js +333 -0
- package/lib/commands/skills.js +47 -0
- package/lib/contract.js +530 -0
- package/lib/cost.js +278 -0
- package/lib/engine.js +212 -0
- package/lib/executor.js +914 -0
- package/lib/help.js +143 -0
- package/lib/isolation.js +625 -0
- package/lib/key.js +81 -0
- package/lib/models.js +179 -0
- package/lib/plan.js +459 -0
- package/lib/ports.js +45 -0
- package/lib/skills.js +92 -0
- package/lib/store.js +340 -0
- package/lib/taskspec.js +114 -0
- package/lib/ui.js +249 -0
- package/lib/verify.js +100 -0
- package/lib/workspace.js +383 -0
- package/package.json +57 -4
- package/skills/README.md +40 -0
- package/skills/claude-code/zcode-agent/SKILL.md +169 -0
- package/skills/codex/zcode-agent/SKILL.md +169 -0
- package/skills/codex/zcode-agent/agents/openai.yaml +4 -0
package/lib/brief.js
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// The compact handback: what an orchestrator reads by default (`--json`,
|
|
3
|
+
// `zcode result`, `zcode wait --json`). A projection of the full result
|
|
4
|
+
// record (schema zcode.result/1, still what result.json holds on disk and
|
|
5
|
+
// what `--full` prints): decision-relevant fields only, empty keys omitted,
|
|
6
|
+
// long text bounded, passing checks and met visual criteria reduced to a count
|
|
7
|
+
// (checksPassed, visualMet), decisions/files capped, and a pointer (`result`) to the full record for anything else.
|
|
8
|
+
const BRIEF_SCHEMA = 'zcode.brief/1';
|
|
9
|
+
const SUMMARY_MAX = 600;
|
|
10
|
+
const ERROR_MAX = 500;
|
|
11
|
+
const TAIL_MAX = 1500;
|
|
12
|
+
|
|
13
|
+
function clip(text, max) {
|
|
14
|
+
const s = String(text ?? '');
|
|
15
|
+
return s.length <= max ? s : `${s.slice(0, max - 1)}…`;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const empty = (v) =>
|
|
19
|
+
v === undefined || v === null || v === '' || (Array.isArray(v) && v.length === 0);
|
|
20
|
+
|
|
21
|
+
function briefCheck(c) {
|
|
22
|
+
if (c.ok) return { command: c.command, ok: true };
|
|
23
|
+
const out = { command: c.command, ok: false, exitCode: c.exitCode ?? null };
|
|
24
|
+
if (c.timedOut) out.timedOut = true;
|
|
25
|
+
out.tail = String(c.output || '').slice(-TAIL_MAX);
|
|
26
|
+
return out;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const DECISIONS_MAX = 6;
|
|
30
|
+
const FILES_MAX = 40;
|
|
31
|
+
const OPTION_MAX = 300;
|
|
32
|
+
|
|
33
|
+
function briefDecision(d) {
|
|
34
|
+
if (d && typeof d === 'object') return { what: clip(d.what, 160), why: clip(d.why, 120) };
|
|
35
|
+
return clip(d, 160);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function briefVisual(v) {
|
|
39
|
+
return {
|
|
40
|
+
criterion: clip(v.criterion, 160),
|
|
41
|
+
viewport: v.viewport,
|
|
42
|
+
met: v.met === null ? null : false,
|
|
43
|
+
seen: v.seen ? clip(v.seen, 200) : undefined,
|
|
44
|
+
note: clip(v.note, 300),
|
|
45
|
+
shot: v.shot,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Questions stay whole (the decision surface); only option strings are bounded.
|
|
50
|
+
function briefQuestion(q) {
|
|
51
|
+
if (!q || typeof q !== 'object' || !Array.isArray(q.options)) return q;
|
|
52
|
+
return { ...q, options: q.options.map((o) => (typeof o === 'string' ? clip(o, OPTION_MAX) : o)) };
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const ALWAYS = new Set(['type', 'schema', 'status', 'ok', 'id']);
|
|
56
|
+
|
|
57
|
+
function buildBrief(result) {
|
|
58
|
+
// checks_failed restates the failing `checks` entries below: kind only.
|
|
59
|
+
const error = result.error
|
|
60
|
+
? result.error.kind === 'checks_failed'
|
|
61
|
+
? { kind: 'checks_failed' }
|
|
62
|
+
: { kind: result.error.kind, message: clip(result.error.message, ERROR_MAX) }
|
|
63
|
+
: null;
|
|
64
|
+
// The failure message usually repeats as an issue; say it once.
|
|
65
|
+
const issues = (result.issues || []).filter((i) => !(error && i === result.error.message));
|
|
66
|
+
const checks = result.checks || [];
|
|
67
|
+
const failing = checks.filter((c) => !c.ok);
|
|
68
|
+
const passed = checks.length - failing.length;
|
|
69
|
+
const visual = Array.isArray(result.visual) ? result.visual : [];
|
|
70
|
+
const visualUnmet = visual.filter((v) => !(v && v.met === true));
|
|
71
|
+
const decisions = Array.isArray(result.decisions) ? result.decisions : [];
|
|
72
|
+
// Files a concurrent session changed are that session's, not this handback's.
|
|
73
|
+
const files = ((result.changes && result.changes.files) || [])
|
|
74
|
+
.filter((f) => !f.concurrentSession)
|
|
75
|
+
.map((f) => f.path);
|
|
76
|
+
const raw = {
|
|
77
|
+
type: 'result',
|
|
78
|
+
schema: BRIEF_SCHEMA,
|
|
79
|
+
status: result.status,
|
|
80
|
+
ok: Boolean(result.ok),
|
|
81
|
+
id: result.id,
|
|
82
|
+
label: result.label,
|
|
83
|
+
preflight: result.preflight,
|
|
84
|
+
summary: clip(result.summary, SUMMARY_MAX),
|
|
85
|
+
// run/agent: the answer is the product. task: the handback is.
|
|
86
|
+
response: result.kind === 'task' ? undefined : result.response,
|
|
87
|
+
files: files.slice(0, FILES_MAX),
|
|
88
|
+
filesMore: files.length > FILES_MAX ? files.length - FILES_MAX : undefined,
|
|
89
|
+
checks: failing.map(briefCheck),
|
|
90
|
+
checksPassed: passed > 0 ? passed : undefined,
|
|
91
|
+
questions: Array.isArray(result.questions) ? result.questions.map(briefQuestion) : result.questions,
|
|
92
|
+
issues,
|
|
93
|
+
unresolved: result.unresolved,
|
|
94
|
+
scopeViolations: result.scopeViolations,
|
|
95
|
+
touched: result.touched,
|
|
96
|
+
decisions: decisions.slice(0, DECISIONS_MAX).map(briefDecision),
|
|
97
|
+
decisionsMore: decisions.length > DECISIONS_MAX ? decisions.length - DECISIONS_MAX : undefined,
|
|
98
|
+
visual: visualUnmet.map(briefVisual),
|
|
99
|
+
visualMet: visual.length ? `${visual.length - visualUnmet.length}/${visual.length}` : undefined,
|
|
100
|
+
units: result.units,
|
|
101
|
+
error,
|
|
102
|
+
result: result.files && result.files.result,
|
|
103
|
+
};
|
|
104
|
+
const out = {};
|
|
105
|
+
for (const [k, v] of Object.entries(raw)) if (ALWAYS.has(k) || !empty(v)) out[k] = v;
|
|
106
|
+
return out;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
module.exports = { buildBrief, BRIEF_SCHEMA };
|
package/lib/checks.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// Acceptance checks: shell commands from the task contract that zcode runs
|
|
3
|
+
// ITSELF after the model's turn — the model's own claims are never the
|
|
4
|
+
// verification evidence. Each check runs in its own process group (so a
|
|
5
|
+
// timeout or cancel kills the whole tree), with a per-check timeout and a
|
|
6
|
+
// bounded output tail. CI=1 is set when unset so test runners do not enter
|
|
7
|
+
// watch/interactive mode.
|
|
8
|
+
const { spawn } = require('node:child_process');
|
|
9
|
+
|
|
10
|
+
const OUTPUT_TAIL = 4000;
|
|
11
|
+
|
|
12
|
+
function killGroup(child) {
|
|
13
|
+
try {
|
|
14
|
+
process.kill(-child.pid, 'SIGKILL');
|
|
15
|
+
} catch {
|
|
16
|
+
try {
|
|
17
|
+
child.kill('SIGKILL');
|
|
18
|
+
} catch {
|
|
19
|
+
/* gone */
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function runCheck(command, { cwd, timeoutMs = 300000, signal, env = process.env } = {}) {
|
|
25
|
+
env = env || process.env;
|
|
26
|
+
const started = Date.now();
|
|
27
|
+
return new Promise((resolve) => {
|
|
28
|
+
let output = '';
|
|
29
|
+
let timedOut = false;
|
|
30
|
+
let cancelled = false;
|
|
31
|
+
const keep = (chunk) => {
|
|
32
|
+
output += chunk;
|
|
33
|
+
if (output.length > OUTPUT_TAIL * 2) output = output.slice(-OUTPUT_TAIL);
|
|
34
|
+
};
|
|
35
|
+
let child;
|
|
36
|
+
try {
|
|
37
|
+
child = spawn('/bin/sh', ['-c', command], {
|
|
38
|
+
cwd,
|
|
39
|
+
env: { ...env, CI: env.CI ?? '1' },
|
|
40
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
41
|
+
detached: true,
|
|
42
|
+
});
|
|
43
|
+
} catch (err) {
|
|
44
|
+
resolve({ command, ok: false, exitCode: null, timedOut: false, durationMs: 0, output: String(err.message) });
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
child.stdout.setEncoding('utf8');
|
|
48
|
+
child.stderr.setEncoding('utf8');
|
|
49
|
+
child.stdout.on('data', keep);
|
|
50
|
+
child.stderr.on('data', keep);
|
|
51
|
+
const timer = setTimeout(() => {
|
|
52
|
+
timedOut = true;
|
|
53
|
+
killGroup(child);
|
|
54
|
+
}, timeoutMs);
|
|
55
|
+
const onAbort = () => {
|
|
56
|
+
cancelled = true;
|
|
57
|
+
killGroup(child);
|
|
58
|
+
};
|
|
59
|
+
if (signal) {
|
|
60
|
+
if (signal.aborted) onAbort();
|
|
61
|
+
else signal.addEventListener('abort', onAbort, { once: true });
|
|
62
|
+
}
|
|
63
|
+
const finish = (exitCode, errText) => {
|
|
64
|
+
clearTimeout(timer);
|
|
65
|
+
if (signal) signal.removeEventListener('abort', onAbort);
|
|
66
|
+
const tail = (output + (errText ? `\n${errText}` : '')).slice(-OUTPUT_TAIL);
|
|
67
|
+
resolve({
|
|
68
|
+
command,
|
|
69
|
+
ok: exitCode === 0 && !timedOut && !cancelled,
|
|
70
|
+
exitCode,
|
|
71
|
+
timedOut,
|
|
72
|
+
cancelled,
|
|
73
|
+
durationMs: Date.now() - started,
|
|
74
|
+
output: tail,
|
|
75
|
+
});
|
|
76
|
+
};
|
|
77
|
+
child.on('error', (err) => finish(null, err.message));
|
|
78
|
+
child.on('close', (code) => finish(code));
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Runs every check in order (later checks still run when an earlier one
|
|
83
|
+
// fails, so the handback carries complete evidence); stops on cancel.
|
|
84
|
+
async function runChecks(commands, { cwd, timeoutMs, signal, env, onEvent = () => {} } = {}) {
|
|
85
|
+
const results = [];
|
|
86
|
+
for (const command of commands) {
|
|
87
|
+
if (signal && signal.aborted) break;
|
|
88
|
+
onEvent({ type: 'check', phase: 'started', command });
|
|
89
|
+
const r = await runCheck(command, { cwd, timeoutMs, signal, env });
|
|
90
|
+
results.push(r);
|
|
91
|
+
onEvent({
|
|
92
|
+
type: 'check',
|
|
93
|
+
phase: 'finished',
|
|
94
|
+
command,
|
|
95
|
+
ok: r.ok,
|
|
96
|
+
exitCode: r.exitCode,
|
|
97
|
+
timedOut: r.timedOut,
|
|
98
|
+
durationMs: r.durationMs,
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
return results;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
module.exports = { runCheck, runChecks, OUTPUT_TAIL };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// lib/commands/acp.js — `zcode-agent acp`: serve the Agent Client Protocol
|
|
3
|
+
// on stdio (lib/acp.js) until the client closes stdin. Flags set the
|
|
4
|
+
// defaults of every session; clients change mode, model and effort per
|
|
5
|
+
// session through ACP config options.
|
|
6
|
+
const { serve } = require('../acp.js');
|
|
7
|
+
const { MODE_IDS } = require('../acp-map.js');
|
|
8
|
+
|
|
9
|
+
module.exports = function acpCommand(parsed) {
|
|
10
|
+
const { flags, given } = parsed;
|
|
11
|
+
if (parsed.prompts.length) {
|
|
12
|
+
process.stderr.write(`zcode-agent: acp takes no arguments (got "${parsed.prompts.join(' ')}"); it speaks ACP on stdio.\n`);
|
|
13
|
+
process.exit(2);
|
|
14
|
+
}
|
|
15
|
+
const mode = flags.mode || process.env.ZCODE_ACP_MODE || 'build';
|
|
16
|
+
if (!MODE_IDS.includes(mode)) {
|
|
17
|
+
process.stderr.write(`zcode-agent: unknown mode "${mode}". --mode accepts ${MODE_IDS.join(' | ')}.\n`);
|
|
18
|
+
process.exit(2);
|
|
19
|
+
}
|
|
20
|
+
serve({
|
|
21
|
+
version: require('../../package.json').version,
|
|
22
|
+
defaults: {
|
|
23
|
+
model: flags.model,
|
|
24
|
+
effort: flags.effort,
|
|
25
|
+
mode,
|
|
26
|
+
mcpSpecs: flags.mcpSpecs,
|
|
27
|
+
hermetic: flags.hermetic,
|
|
28
|
+
browser: !flags.noBrowser,
|
|
29
|
+
skillsDirs: flags.skillsDirs.map((d) => require('node:path').resolve(d)),
|
|
30
|
+
noSkills: flags.noSkills,
|
|
31
|
+
inheritDesktop: flags.inheritDesktop,
|
|
32
|
+
timeoutMs: given.has('timeoutMs') ? flags.timeoutMs : null,
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
};
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// lib/commands/cost.js — `zcode cost [<dir>] [--app] [--json]`: what the GLM
|
|
3
|
+
// work in a directory cost at API list price, per session and in total.
|
|
4
|
+
// Read-only over the session store and the engine DBs; no engine, no key.
|
|
5
|
+
const path = require('node:path');
|
|
6
|
+
const cost = require('../cost.js');
|
|
7
|
+
|
|
8
|
+
function row(cols, widths) {
|
|
9
|
+
return cols.map((c, i) => (i >= cols.length - 5 ? String(c).padStart(widths[i]) : String(c).padEnd(widths[i]))).join(' ').trimEnd();
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function table(entries, title) {
|
|
13
|
+
const head = [title, 'status', 'model', 'req', 'input', 'cached', 'output', 'cost'];
|
|
14
|
+
const rows = entries.map((e) => {
|
|
15
|
+
const c = e.cost;
|
|
16
|
+
return [
|
|
17
|
+
e.id.length > 40 ? `${e.id.slice(0, 39)}…` : e.id,
|
|
18
|
+
e.status || '',
|
|
19
|
+
e.model || '',
|
|
20
|
+
c ? c.requests : '-',
|
|
21
|
+
c ? cost.formatTokens(c.input) : '-',
|
|
22
|
+
c ? cost.formatTokens(c.cached) : '-',
|
|
23
|
+
c ? cost.formatTokens(c.output) : '-',
|
|
24
|
+
c ? `${cost.formatUsd(c.usd)}${c.estimate ? '~' : ''}` : '-',
|
|
25
|
+
];
|
|
26
|
+
});
|
|
27
|
+
const widths = head.map((h, i) => Math.max(String(h).length, ...rows.map((r) => String(r[i]).length)));
|
|
28
|
+
return [row(head, widths), ...rows.map((r) => row(r, widths))].join('\n');
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
module.exports = async function costCommand(parsed) {
|
|
32
|
+
const { flags, prompts } = parsed;
|
|
33
|
+
if (prompts.length > 1) {
|
|
34
|
+
process.stderr.write('zcode-agent: cost takes at most one directory.\n');
|
|
35
|
+
process.exit(2);
|
|
36
|
+
}
|
|
37
|
+
const dir = path.resolve(prompts[0] || flags.cwd || process.cwd());
|
|
38
|
+
const sessions = cost.dirSessions(dir);
|
|
39
|
+
const app = flags.app ? cost.appSessions(dir) : null;
|
|
40
|
+
const zTotal = cost.sumCosts(sessions.map((s) => s.cost));
|
|
41
|
+
const aTotal = app ? cost.sumCosts(app.map((s) => s.cost)) : null;
|
|
42
|
+
const total = cost.sumCosts([zTotal, aTotal]);
|
|
43
|
+
|
|
44
|
+
if (flags.json) {
|
|
45
|
+
const out = { type: 'cost', dir, currency: 'USD', basis: 'Z.ai API list price', total, sessions };
|
|
46
|
+
if (flags.app) out.app = app === null ? { error: `desktop app DB not readable` } : { total: aTotal, sessions: app };
|
|
47
|
+
process.stdout.write(JSON.stringify(out) + '\n');
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
const lines = [];
|
|
51
|
+
if (sessions.length) lines.push(table(sessions, 'zcode-agent session'));
|
|
52
|
+
else lines.push(`no zcode-agent sessions in ${dir}`);
|
|
53
|
+
if (flags.app) {
|
|
54
|
+
if (app === null) lines.push('', 'desktop app: DB not readable');
|
|
55
|
+
else if (!app.length) lines.push('', `no desktop app sessions in ${dir}`);
|
|
56
|
+
else lines.push('', table(app.map((s) => ({ ...s, id: s.title || s.id, status: 'app' })), 'desktop session'));
|
|
57
|
+
}
|
|
58
|
+
const n = sessions.length + (app ? app.length : 0);
|
|
59
|
+
lines.push(
|
|
60
|
+
'',
|
|
61
|
+
`total ${n} session${n === 1 ? '' : 's'} ${total.requests} requests ` +
|
|
62
|
+
`${cost.formatTokens(total.input)} input (${cost.formatTokens(total.cached)} cached) ` +
|
|
63
|
+
`${cost.formatTokens(total.output)} output ${cost.formatUsd(total.usd)}${total.estimate ? '~' : ''}`,
|
|
64
|
+
`Z.ai API list price${total.estimate ? '; ~ = no cache split, priced as uncached input' : ''}; ZCODE_PRICES overrides`
|
|
65
|
+
);
|
|
66
|
+
process.stdout.write(lines.join('\n') + '\n');
|
|
67
|
+
};
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// lib/commands/doctor.js — engine, key, inherited setup and browser report.
|
|
3
|
+
// Builds a probe engine HOME (<root>/doctor/home) exactly the way a session
|
|
4
|
+
// does, so what doctor reports is what a session gets. Never prints the key.
|
|
5
|
+
// --install-browser installs the Chromium build the default (standalone)
|
|
6
|
+
// Playwright server drives.
|
|
7
|
+
const fs = require('node:fs');
|
|
8
|
+
const os = require('node:os');
|
|
9
|
+
const path = require('node:path');
|
|
10
|
+
const { spawnSync } = require('node:child_process');
|
|
11
|
+
const { sha256File, engineVersion, packagedEngine } = require('../engine.js');
|
|
12
|
+
const { resolveRuntime, ConfigError } = require('../bootstrap.js');
|
|
13
|
+
const {
|
|
14
|
+
buildEngineHome, engineEnv, rootDir, liveDir, liveMcpServers, DEFAULT_BROWSER_SERVER, PLAYWRIGHT_MCP_VERSION,
|
|
15
|
+
} = require('../isolation.js');
|
|
16
|
+
const store = require('../store.js');
|
|
17
|
+
const { modelLevels, modelVision } = require('../models.js');
|
|
18
|
+
|
|
19
|
+
// node:sqlite without a flag (the engine's session DB) arrived in 22.13.
|
|
20
|
+
const MIN_NODE = [22, 13];
|
|
21
|
+
const KEY_SOURCES = {
|
|
22
|
+
env: 'ZAI_API_KEY',
|
|
23
|
+
login: 'zcode-agent login',
|
|
24
|
+
desktop: 'the ZCode app (read in place, never copied)',
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
function nodeOk(version) {
|
|
28
|
+
const [major, minor] = version.split('.').map(Number);
|
|
29
|
+
return major > MIN_NODE[0] || (major === MIN_NODE[0] && minor >= MIN_NODE[1]);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function testedEngineVersion() {
|
|
33
|
+
try {
|
|
34
|
+
return require('@zcode-agent/engine').version;
|
|
35
|
+
} catch {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function playwrightCache(env) {
|
|
41
|
+
if (env.PLAYWRIGHT_BROWSERS_PATH) return env.PLAYWRIGHT_BROWSERS_PATH;
|
|
42
|
+
const home = env.HOME || os.homedir();
|
|
43
|
+
if (process.platform === 'darwin') return path.join(home, 'Library', 'Caches', 'ms-playwright');
|
|
44
|
+
if (process.platform === 'win32') return path.join(env.LOCALAPPDATA || path.join(home, 'AppData', 'Local'), 'ms-playwright');
|
|
45
|
+
return path.join(home, '.cache', 'ms-playwright');
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function hasChromium(env) {
|
|
49
|
+
try {
|
|
50
|
+
return fs.readdirSync(playwrightCache(env)).some((n) => /^chromium(_headless_shell)?-\d+$/.test(n));
|
|
51
|
+
} catch {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// `playwright install chromium` from the exact @playwright/mcp version the
|
|
57
|
+
// default server runs, so the browser revision matches.
|
|
58
|
+
function installBrowser() {
|
|
59
|
+
const npx = process.platform === 'win32' ? 'npx.cmd' : 'npx';
|
|
60
|
+
const res = spawnSync(npx, ['-y', '-p', `@playwright/mcp@${PLAYWRIGHT_MCP_VERSION}`, 'playwright', 'install', 'chromium'], {
|
|
61
|
+
stdio: ['ignore', 'inherit', 'inherit'],
|
|
62
|
+
});
|
|
63
|
+
return res.status === 0;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
module.exports = async function doctor(parsed) {
|
|
67
|
+
const flags = parsed.flags;
|
|
68
|
+
// First, so a machine without a key yet can still get its browser.
|
|
69
|
+
const browserInstalled = flags.installBrowser ? installBrowser() : null;
|
|
70
|
+
let runtime;
|
|
71
|
+
try {
|
|
72
|
+
runtime = resolveRuntime(process.env);
|
|
73
|
+
} catch (err) {
|
|
74
|
+
if (err instanceof ConfigError) {
|
|
75
|
+
process.stderr.write(`zcode-agent: ${err.message}\n`);
|
|
76
|
+
process.exit(err.exitCode);
|
|
77
|
+
}
|
|
78
|
+
throw err;
|
|
79
|
+
}
|
|
80
|
+
const { enginePath, builtinConfig, nodeBin } = runtime;
|
|
81
|
+
const root = rootDir(process.env);
|
|
82
|
+
const live = liveDir(process.env);
|
|
83
|
+
const built = buildEngineHome({
|
|
84
|
+
home: path.join(root, 'doctor', 'home'),
|
|
85
|
+
root,
|
|
86
|
+
key: runtime.key,
|
|
87
|
+
live,
|
|
88
|
+
hermetic: process.env.ZCODE_HERMETIC === '1',
|
|
89
|
+
});
|
|
90
|
+
const version = engineVersion(nodeBin, enginePath, engineEnv({ home: built.home, builtinProviderConfig: builtinConfig }));
|
|
91
|
+
const tested = testedEngineVersion();
|
|
92
|
+
const packaged = packagedEngine();
|
|
93
|
+
const engineSource = process.env.ZAI_CODE_ENGINE
|
|
94
|
+
? 'ZAI_CODE_ENGINE'
|
|
95
|
+
: packaged && path.resolve(packaged) === path.resolve(enginePath)
|
|
96
|
+
? '@zcode-agent/engine'
|
|
97
|
+
: 'the ZCode app';
|
|
98
|
+
const nodeVersion = process.versions.node;
|
|
99
|
+
const sessions = store.listSessions(process.env);
|
|
100
|
+
const liveSet = Object.keys(liveMcpServers(live));
|
|
101
|
+
const servers = Object.keys(built.mcp.servers);
|
|
102
|
+
const browserDef = built.mcp.servers.playwright;
|
|
103
|
+
const defaultBrowser = browserDef === DEFAULT_BROWSER_SERVER;
|
|
104
|
+
|
|
105
|
+
const chromium = hasChromium(process.env);
|
|
106
|
+
|
|
107
|
+
const warnings = [];
|
|
108
|
+
if (!nodeOk(nodeVersion)) warnings.push(`node ${nodeVersion} is too old: the engine needs node >= ${MIN_NODE.join('.')} (node:sqlite)`);
|
|
109
|
+
if (tested && version && version !== tested) warnings.push(`engine ${version} differs from the tested ${tested}`);
|
|
110
|
+
if (defaultBrowser && !chromium) warnings.push('no Playwright Chromium found for the default browser server: run zcode-agent doctor --install-browser');
|
|
111
|
+
if (browserInstalled === false) warnings.push('browser install failed (see the output above)');
|
|
112
|
+
|
|
113
|
+
const report = {
|
|
114
|
+
ok: nodeOk(nodeVersion) && browserInstalled !== false,
|
|
115
|
+
node: { version: nodeVersion, engineBin: nodeBin, minimum: MIN_NODE.join('.') },
|
|
116
|
+
engine: {
|
|
117
|
+
path: enginePath,
|
|
118
|
+
source: engineSource,
|
|
119
|
+
sha256: sha256File(enginePath),
|
|
120
|
+
version,
|
|
121
|
+
tested,
|
|
122
|
+
builtinProviderConfig: builtinConfig,
|
|
123
|
+
},
|
|
124
|
+
key: { configured: true, source: runtime.keySource },
|
|
125
|
+
models: modelLevels({ builtinConfig }),
|
|
126
|
+
vision: modelVision({ builtinConfig }),
|
|
127
|
+
desktop: {
|
|
128
|
+
dir: live,
|
|
129
|
+
found: fs.existsSync(live),
|
|
130
|
+
mcpServers: liveSet.length,
|
|
131
|
+
},
|
|
132
|
+
isolation: {
|
|
133
|
+
root,
|
|
134
|
+
home: built.home,
|
|
135
|
+
sessionsDir: store.sessionsDir(process.env),
|
|
136
|
+
mode: built.mode,
|
|
137
|
+
hermetic: process.env.ZCODE_HERMETIC === '1',
|
|
138
|
+
mcpServers: servers,
|
|
139
|
+
mcpSkippedOAuth: liveSet.filter((n) => !servers.includes(n)),
|
|
140
|
+
skillsInherited: built.skillsCopied,
|
|
141
|
+
},
|
|
142
|
+
browser: {
|
|
143
|
+
server: defaultBrowser ? `@playwright/mcp@${PLAYWRIGHT_MCP_VERSION} (default, headless)` : browserDef ? 'the ZCode app\'s playwright server' : 'none (--no-browser)',
|
|
144
|
+
chromium,
|
|
145
|
+
...(browserInstalled !== null && { installed: browserInstalled }),
|
|
146
|
+
},
|
|
147
|
+
sessions: {
|
|
148
|
+
total: sessions.length,
|
|
149
|
+
running: sessions.filter((s) => s.live).length,
|
|
150
|
+
interrupted: sessions.filter((s) => s.state === 'interrupted').length,
|
|
151
|
+
},
|
|
152
|
+
warnings,
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
if (flags.json) {
|
|
156
|
+
process.stdout.write(JSON.stringify(report) + '\n');
|
|
157
|
+
} else {
|
|
158
|
+
const iso = report.isolation;
|
|
159
|
+
process.stdout.write(
|
|
160
|
+
[
|
|
161
|
+
'zcode-agent doctor',
|
|
162
|
+
` node ${nodeVersion} (${nodeBin})`,
|
|
163
|
+
` engine ${version ?? 'unknown'} from ${engineSource}${tested ? ` (tested: ${tested})` : ''}`,
|
|
164
|
+
` ${enginePath}`,
|
|
165
|
+
` sha256 ${report.engine.sha256}`,
|
|
166
|
+
` api key from ${KEY_SOURCES[runtime.keySource] || runtime.keySource} (never printed)`,
|
|
167
|
+
` models ${Object.entries(report.models).map(([id, l]) => `${id} (effort ${l.join('|')}, ${report.vision[id] ? 'images' : 'no images'})`).join(', ')}`,
|
|
168
|
+
report.desktop.found ? ` ZCode app settings ${live}` : ' ZCode app not found: standalone mode',
|
|
169
|
+
` mcp servers ${servers.length ? servers.join(', ') : 'none'}${iso.mcpSkippedOAuth.length ? ` (skipped, OAuth: ${iso.mcpSkippedOAuth.join(', ')})` : ''}`,
|
|
170
|
+
` skills inherited ${iso.skillsInherited}${iso.hermetic ? ' (ZCODE_HERMETIC=1)' : ''}`,
|
|
171
|
+
` browser ${report.browser.server}; Playwright Chromium ${chromium ? 'found' : 'missing'}`,
|
|
172
|
+
` state ${root} (one engine HOME per session)`,
|
|
173
|
+
` sessions ${report.sessions.total} (${report.sessions.running} running, ${report.sessions.interrupted} interrupted)`,
|
|
174
|
+
...warnings.map((w) => ` ⚠ ${w}`),
|
|
175
|
+
].join('\n') + '\n'
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
process.exit(report.ok ? 0 : 1);
|
|
179
|
+
};
|