zcode-agent 0.0.0-stage → 1.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.
- package/LICENSE +21 -0
- package/README.md +418 -2
- package/assets/executor-agents.md +10 -0
- package/bin/zcode-agent.js +418 -0
- package/lib/appserver.js +516 -0
- package/lib/bootstrap.js +70 -0
- package/lib/brief.js +109 -0
- package/lib/checks.js +104 -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 +329 -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 +128 -0
- package/lib/isolation.js +621 -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 +52 -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/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,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
|
+
};
|