zhiqu 0.1.0 → 0.2.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/README.md +45 -10
- package/package.json +2 -1
- package/skills/code-audit/SKILL.md +49 -0
- package/skills/codebase-tour/SKILL.md +65 -0
- package/skills/commit/SKILL.md +72 -0
- package/skills/debug/SKILL.md +49 -0
- package/skills/perf/SKILL.md +42 -0
- package/skills/refactor/SKILL.md +40 -0
- package/skills/security-review/SKILL.md +71 -0
- package/skills/test-first/SKILL.md +50 -0
- package/src/agent.js +242 -26
- package/src/cli.js +137 -23
- package/src/commands.js +1 -0
- package/src/config.js +34 -3
- package/src/desktop.js +25 -46
- package/src/direct.js +274 -0
- package/src/host/code-host.js +154 -25
- package/src/host/sandbox-launch.js +1 -1
- package/src/prompt.js +2 -2
- package/src/replay.js +8 -3
- package/src/review.js +5 -2
- package/src/selfupdate.js +112 -0
- package/src/skills.js +12 -3
- package/src/steps.js +219 -0
- package/src/subagents.js +475 -0
- package/src/tools/drafts.js +45 -0
- package/src/tools/local.js +6 -3
- package/src/tools/sandbox.js +20 -15
- package/src/ui.js +69 -4
- package/src/update.js +2 -2
package/src/tools/sandbox.js
CHANGED
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
// 规则(execrules.js)拦不住一段脚本 —— 白名单里有 python3 就等于允许任意代码。这一层是操作系统在拦:
|
|
4
4
|
// - 写:工作区 + 这个进程自己的临时目录。.git、.zhiqu 不能写:写一个 hook、改一下设置,下次就跑到沙箱外面去了。
|
|
5
5
|
// - 读:家目录默认不让读,只放工具链和工作区。读到的东西会原样进模型上下文,发给模型服务。
|
|
6
|
-
// -
|
|
6
|
+
// - 网:默认开着(2026-11-10 用户定的「所有人都可以联网」;原来是全断,因为本机的数据库可能没有密码、Docker 的 socket 也在本机)。
|
|
7
|
+
// 关掉用 --sandbox-network off,只放本机用 local。写范围和读范围不跟着变。
|
|
7
8
|
// - 系统服务:mach-lookup 只放一小撮。放开的话 open、osascript、launchctl 能让系统替它在沙箱外跑东西,读剪贴板、钥匙串。
|
|
8
9
|
// 探测(detectSandbox)是真跑一次:嵌套沙箱、用户命名空间被关时 sandbox-exec / bwrap 起不来,当作没有,退回白名单。
|
|
9
10
|
import { spawnSync } from 'node:child_process';
|
|
@@ -185,13 +186,13 @@ export const shellQuote = (s) => `'${String(s).replace(/'/g, `'\\''`)}'`;
|
|
|
185
186
|
* home、env 能传进来:判据用假的家目录,不碰用户真的 ~/.ssh。
|
|
186
187
|
*/
|
|
187
188
|
export function sandboxedLaunch(box, { command, root, cwd, policy = WORKSPACE_WRITE, network: wantedNetwork = 'off', home = os.homedir(), env = process.env, privateDir = childHome(), hosted = false }) {
|
|
189
|
+
// 认不出的一律断网:两边都是「不是 off 就开网」,写错一个字不能把网打开
|
|
190
|
+
const network = NETWORK_MODES.includes(wantedNetwork) ? wantedNetwork : 'off';
|
|
188
191
|
if (hosted) {
|
|
189
|
-
// 服务器上只有这一种:没有 bubblewrap
|
|
192
|
+
// 服务器上只有这一种:没有 bubblewrap 就是不跑,不能退回上面那份(那份的根目录是整个系统)
|
|
190
193
|
if (box.kind !== 'bwrap') throw new Error('服务器上的沙箱要 bubblewrap');
|
|
191
|
-
return hostedLaunch(box, { command, root, cwd, policy, privateDir });
|
|
194
|
+
return hostedLaunch(box, { command, root, cwd, policy, privateDir, network });
|
|
192
195
|
}
|
|
193
|
-
// 认不出的一律断网:bwrap 那边「不是 off 就开网」,写错一个字不能把网打开
|
|
194
|
-
const network = NETWORK_MODES.includes(wantedNetwork) ? wantedNetwork : 'off';
|
|
195
196
|
const realHome = real(home);
|
|
196
197
|
const realRoot = real(root);
|
|
197
198
|
const priv = real(privateDir);
|
|
@@ -226,12 +227,14 @@ const HOSTED_LIMITS = 'ulimit -c 0 && ulimit -u 128 && ulimit -n 256 && ulimit -
|
|
|
226
227
|
const HOSTED_PATH = '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin';
|
|
227
228
|
const HOSTED_WORK = '/work';
|
|
228
229
|
|
|
229
|
-
function hostedBwrapArgs({ root, cwd, policy, privateDir }) {
|
|
230
|
+
function hostedBwrapArgs({ root, cwd, policy, privateDir, network = 'off' }) {
|
|
230
231
|
// 不用 --unshare-all:它对 user、cgroup 是「能开就开」,开不了就悄悄不开。这里开不了就该起不来
|
|
231
232
|
// 不要 --as-pid-1:那样 1 号进程是 exec 出来的命令,AppArmor 切配置时内核把 --die-with-parent 设的 pdeathsig 清了(第一百一十九轮在真服务器上撞到,容器里没有
|
|
232
233
|
// AppArmor 看不到):外层一死,里面的进程就成了没人管的孤儿。1 号留给 bwrap,它没有 exec 过,外层一死它就死,整个 PID 命名空间跟着没了
|
|
233
234
|
const args = ['--die-with-parent', '--new-session', '--unshare-user', '--disable-userns', '--unshare-pid', '--unshare-net', '--unshare-ipc', '--unshare-uts', '--unshare-cgroup',
|
|
234
235
|
'--uid', '1000', '--gid', '1000', '--hostname', 'sandbox', '--cap-drop', 'ALL', '--clearenv'];
|
|
236
|
+
// 网络:先 --unshare-net 再 --share-net(bwrap 按顺序认,后面的盖前面的)。bwrap 分不出本机和外网,所以 local 在这里等于 on
|
|
237
|
+
if (network !== 'off') args.push('--share-net');
|
|
235
238
|
args.push('--ro-bind', '/usr', '/usr');
|
|
236
239
|
// /bin、/lib 这些在 merged-usr 上是软链,老系统上是目录
|
|
237
240
|
for (const name of ['bin', 'sbin', 'lib', 'lib32', 'lib64', 'libx32']) {
|
|
@@ -269,12 +272,12 @@ function hostedBwrapArgs({ root, cwd, policy, privateDir }) {
|
|
|
269
272
|
* 外面包一层 sh:优先级最低(后端和 MySQL 先)、内存不够时先杀它(oom_score_adj 只能往上调,里面的进程调不回来)。
|
|
270
273
|
* 限额设不上就不跑:一条没限额的命令不该跑在这台机器上。
|
|
271
274
|
*/
|
|
272
|
-
function hostedLaunch(box, { command, root, cwd, policy, privateDir }) {
|
|
275
|
+
function hostedLaunch(box, { command, root, cwd, policy, privateDir, network = 'off' }) {
|
|
273
276
|
const inner = `${HOSTED_LIMITS} || { echo '限额设不上,这条命令没有运行' >&2; exit 126; }; ${reapJobs(command)}`;
|
|
274
277
|
// 1 号进程是 bwrap,里面的命令读得到它的 /proc/1/cmdline 和 environ:参数放文件里(--args,打开就删,bwrap 读完就关),不写在命令行上(服务器上的真路径、账号 id);
|
|
275
278
|
// env -i:它的环境是空的,调用方带了什么都进不来
|
|
276
279
|
const argsFile = path.join(real(privateDir), `hosted-args-${crypto.randomBytes(8).toString('hex')}`);
|
|
277
|
-
const argv = hostedBwrapArgs({ root: real(root), cwd: real(cwd), policy, privateDir: real(privateDir) });
|
|
280
|
+
const argv = hostedBwrapArgs({ root: real(root), cwd: real(cwd), policy, privateDir: real(privateDir), network });
|
|
278
281
|
fs.writeFileSync(argsFile, `${argv.join('\0')}\0`, { mode: 0o600 });
|
|
279
282
|
return {
|
|
280
283
|
binary: '/bin/sh',
|
|
@@ -284,8 +287,10 @@ function hostedLaunch(box, { command, root, cwd, policy, privateDir }) {
|
|
|
284
287
|
};
|
|
285
288
|
}
|
|
286
289
|
|
|
287
|
-
// 探测不只看起不起得来:边界得真的在(Ubuntu 的 AppArmor
|
|
288
|
-
|
|
290
|
+
// 探测不只看起不起得来:边界得真的在(Ubuntu 的 AppArmor 规则、内核开关都可能让它起不来或者不完整)。
|
|
291
|
+
// 网络这一条只在「应该断网」时验:允许联网时服务器本来就可能没有外网,拿它当判据会把好沙箱判死。
|
|
292
|
+
export const hostedProbe = (network) => 'echo x > /work/.probe && rm /work/.probe && [ ! -e /home ] && [ ! -e /root ] && [ ! -e /var ] && [ ! -e /etc/shadow ]'
|
|
293
|
+
+ (network === 'off' ? ' && ! (exec 3<>/dev/tcp/1.1.1.1/53) 2>/dev/null' : '');
|
|
289
294
|
|
|
290
295
|
/**
|
|
291
296
|
* 命令一结束,它起的后台进程一起结束(第一百零九轮):不收的话命令行要等它结束才回话,网页把它留在外面接着跑。
|
|
@@ -318,8 +323,8 @@ export function plainLaunch({ command, home = os.homedir(), env = process.env, p
|
|
|
318
323
|
* 这台机器有没有能用的系统沙箱。不只看文件在不在:真跑一次。
|
|
319
324
|
* 返回 { kind, binary } 或 null(调用方退回白名单)。
|
|
320
325
|
*/
|
|
321
|
-
export function detectSandbox({ platform = process.platform, env = process.env, probe = true, hosted = false } = {}) {
|
|
322
|
-
if (hosted) return detectHostedSandbox({ platform, env, probe });
|
|
326
|
+
export function detectSandbox({ platform = process.platform, env = process.env, probe = true, hosted = false, network = 'on' } = {}) {
|
|
327
|
+
if (hosted) return detectHostedSandbox({ platform, env, probe, network });
|
|
323
328
|
let box = null;
|
|
324
329
|
if (platform === 'darwin' && fs.existsSync('/usr/bin/sandbox-exec')) box = { kind: 'seatbelt', binary: '/usr/bin/sandbox-exec' };
|
|
325
330
|
else if (platform === 'linux') {
|
|
@@ -337,7 +342,7 @@ export function detectSandbox({ platform = process.platform, env = process.env,
|
|
|
337
342
|
}
|
|
338
343
|
}
|
|
339
344
|
|
|
340
|
-
function detectHostedSandbox({ platform, env, probe }) {
|
|
345
|
+
function detectHostedSandbox({ platform, env, probe, network = 'on' }) {
|
|
341
346
|
const binary = platform === 'linux' ? resolveOnPath('bwrap', env) : null;
|
|
342
347
|
if (!binary) return null;
|
|
343
348
|
const box = { kind: 'bwrap', binary };
|
|
@@ -345,7 +350,7 @@ function detectHostedSandbox({ platform, env, probe }) {
|
|
|
345
350
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'zhiqu-probe-'));
|
|
346
351
|
const priv = fs.mkdtempSync(path.join(os.tmpdir(), 'zhiqu-probe-'));
|
|
347
352
|
try {
|
|
348
|
-
const launch = hostedLaunch(box, { command:
|
|
353
|
+
const launch = hostedLaunch(box, { command: hostedProbe(network), root: dir, cwd: dir, policy: WORKSPACE_WRITE, privateDir: priv, network });
|
|
349
354
|
const r = spawnSync(launch.binary, launch.args, { stdio: 'ignore', timeout: 5000, cwd: dir, env: launch.env });
|
|
350
355
|
return r.status === 0 ? box : null;
|
|
351
356
|
} catch { return null; } finally {
|
|
@@ -366,7 +371,7 @@ export function looksBlocked(output) {
|
|
|
366
371
|
/** 给人和模型看的一句话:现在关在什么里面。 */
|
|
367
372
|
export function describeBoundary({ policy = WORKSPACE_WRITE, network = 'off', hosted = false } = {}) {
|
|
368
373
|
if (hosted) {
|
|
369
|
-
return `服务器上的隔离环境,工作目录是 ${HOSTED_WORK}(就是工作区);${policy === READ_ONLY ? '工作区只读,只能写 /tmp' : '只能写工作区和 /tmp'}
|
|
374
|
+
return `服务器上的隔离环境,工作目录是 ${HOSTED_WORK}(就是工作区);${policy === READ_ONLY ? '工作区只读,只能写 /tmp' : '只能写工作区和 /tmp'};${network === 'off' ? '不能联网' : '能联网'};只有系统目录,读不到别的文件;`
|
|
370
375
|
+ '一条命令最多 60 秒,内存和进程数有限,超了会被杀;不能起后台进程(background 用不了);每条命令是一个新的 bash,/tmp 也不留到下一条(要留的放工作区)';
|
|
371
376
|
}
|
|
372
377
|
const write = policy === READ_ONLY ? '只读(只能写临时目录 $TMPDIR)' : '能写工作区(.git、.zhiqu 除外)和临时目录 $TMPDIR,别处都不能写';
|
package/src/ui.js
CHANGED
|
@@ -80,6 +80,8 @@ export class Ui {
|
|
|
80
80
|
this.paint = painter(this.color);
|
|
81
81
|
this.queue = [];
|
|
82
82
|
this.waiters = [];
|
|
83
|
+
this.chooseChain = Promise.resolve();
|
|
84
|
+
this.chooseEpoch = 0;
|
|
83
85
|
this.closed = false;
|
|
84
86
|
this.onInterrupt = null;
|
|
85
87
|
this.interactive = interactive ?? Boolean(input.isTTY && output.isTTY);
|
|
@@ -450,9 +452,44 @@ export class Ui {
|
|
|
450
452
|
});
|
|
451
453
|
}
|
|
452
454
|
|
|
453
|
-
/**
|
|
454
|
-
|
|
455
|
-
|
|
455
|
+
/**
|
|
456
|
+
* 问一个单字母的选择;输入结束、或者回答不在选项里时返回 fallback。
|
|
457
|
+
* 一次只问一个(第一百二十轮):几个子 agent 同时要确认时,原来两个问题一起挂着 —— 屏幕上是后一个,回车交给的是前一个,
|
|
458
|
+
* 看着「写入 b.js?」按 y,批准的是 a.js。排队一个一个问;Ctrl+C 作废问题时,还排着没问出来的也一并按不同意。
|
|
459
|
+
*/
|
|
460
|
+
choose(question, options, fallback, { signal } = {}) {
|
|
461
|
+
const epoch = this.chooseEpoch;
|
|
462
|
+
const live = () => epoch === this.chooseEpoch && !(signal && signal.aborted);
|
|
463
|
+
const turn = this.chooseChain.then(() => (live() ? this.chooseNow(question, options, fallback, { signal }) : fallback));
|
|
464
|
+
this.chooseChain = turn.catch(() => {});
|
|
465
|
+
return turn;
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
/**
|
|
469
|
+
* signal:问的那一方停了(子 agent 被停掉,第一百二十六轮 review)—— 正问着的收起来、按 fallback,排着的轮到时不问。
|
|
470
|
+
* 原来只有 Ctrl+C 作废问题:子 agent 被 close_agent / 一句话结束停掉以后,它的问题照样挂着,主 agent 收不了尾,
|
|
471
|
+
* 按 y 也算不同意,接着打的下一句话被当成回答吃掉。
|
|
472
|
+
*/
|
|
473
|
+
async chooseNow(question, options, fallback, { signal } = {}) {
|
|
474
|
+
const asked = this.ask(`${question} ${options.map((o) => this.paint.bold(`[${o.key}]`) + ` ${o.label}`).join(' ')} › `, { fresh: true });
|
|
475
|
+
let onAbort = null;
|
|
476
|
+
if (signal) {
|
|
477
|
+
const waiter = this.waiters[this.waiters.length - 1];
|
|
478
|
+
onAbort = () => {
|
|
479
|
+
const at = this.waiters.indexOf(waiter);
|
|
480
|
+
if (at < 0) return; // 已经答了
|
|
481
|
+
this.waiters.splice(at, 1);
|
|
482
|
+
this.dropQuestion('(不用回答了:问的那一方已经停下)');
|
|
483
|
+
waiter.resolve(null);
|
|
484
|
+
};
|
|
485
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
486
|
+
}
|
|
487
|
+
let line;
|
|
488
|
+
try {
|
|
489
|
+
line = await asked;
|
|
490
|
+
} finally {
|
|
491
|
+
if (onAbort) signal.removeEventListener('abort', onAbort);
|
|
492
|
+
}
|
|
456
493
|
if (line == null) return fallback;
|
|
457
494
|
const k = line.trim().toLowerCase();
|
|
458
495
|
const hit = options.find((o) => o.key === k || (o.aliases || []).includes(k));
|
|
@@ -465,6 +502,7 @@ export class Ui {
|
|
|
465
502
|
* 用户接着打的下一句话被当成对这个问题的回答吃掉(第十三轮真终端实测:「还在吗」→「用户没有同意,没写」,那句话没了)。
|
|
466
503
|
*/
|
|
467
504
|
cancelQuestion() {
|
|
505
|
+
this.chooseEpoch += 1; // 排着还没问出来的确认:轮到它们时直接按不同意(见 choose)
|
|
468
506
|
const fresh = this.waiters.filter((w) => w.fresh);
|
|
469
507
|
if (!fresh.length) return;
|
|
470
508
|
this.waiters = this.waiters.filter((w) => !w.fresh);
|
|
@@ -587,10 +625,37 @@ export class Ui {
|
|
|
587
625
|
this.renderThinking();
|
|
588
626
|
}
|
|
589
627
|
|
|
628
|
+
/**
|
|
629
|
+
* 在跑命令(第一百二十九轮):状态行从「思考中」换成「运行中 N 秒 · 命令」。
|
|
630
|
+
* 用户原来只看得到「思考中 45s」——分不清是在等模型还是在等命令,以为卡住了(用户:「我以为一直在思考,卡住」)。
|
|
631
|
+
* 计时从命令开始算,不是从这一轮开始算。
|
|
632
|
+
*/
|
|
633
|
+
startRun(label) {
|
|
634
|
+
if (!this.thinking) return;
|
|
635
|
+
this.thinking.phase = 'run';
|
|
636
|
+
this.thinking.runStarted = monotonic();
|
|
637
|
+
this.thinking.runLabel = label ? termSafe(String(label).split('\n')[0], { singleLine: true }).replace(/\s+/g, ' ').slice(0, 60) : '';
|
|
638
|
+
this.renderThinking();
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
endRun() {
|
|
642
|
+
if (!this.thinking) return;
|
|
643
|
+
this.thinking.phase = 'think';
|
|
644
|
+
this.thinking.runStarted = null;
|
|
645
|
+
this.thinking.runLabel = '';
|
|
646
|
+
this.renderThinking();
|
|
647
|
+
}
|
|
648
|
+
|
|
590
649
|
renderThinking() {
|
|
591
650
|
const t = this.thinking;
|
|
592
|
-
const secs = Math.floor((monotonic() - t.started) / 1000);
|
|
593
651
|
const room = this.cols() - 16;
|
|
652
|
+
if (t.phase === 'run') {
|
|
653
|
+
const secs = Math.floor((monotonic() - (t.runStarted || t.started)) / 1000);
|
|
654
|
+
const label = t.runLabel ? ` · ${t.runLabel}` : '';
|
|
655
|
+
this.setStatus(`${this.paint.cyan(pixelFrame(t.tick))} ${this.paint.dim(fit(`运行中 ${secs}s${label}`, Math.max(10, room)))}`);
|
|
656
|
+
return;
|
|
657
|
+
}
|
|
658
|
+
const secs = Math.floor((monotonic() - t.started) / 1000);
|
|
594
659
|
const act = t.activity ? ` · ${t.activity}` : '';
|
|
595
660
|
this.setStatus(`${this.paint.cyan(pixelFrame(t.tick))} ${this.paint.dim(fit(`思考中 ${secs}s${act}`, Math.max(10, room)))}`);
|
|
596
661
|
}
|
package/src/update.js
CHANGED
|
@@ -44,8 +44,8 @@ export async function checkForUpdate(api, { force = false, now = Date.now() } =
|
|
|
44
44
|
|
|
45
45
|
export function updateMessage(v) {
|
|
46
46
|
if (v.level === 'required') {
|
|
47
|
-
return `这个版本(${VERSION})太旧了,服务器要求至少 ${v.minimum}
|
|
47
|
+
return `这个版本(${VERSION})太旧了,服务器要求至少 ${v.minimum}。运行 zhiqu update 更新(或者 npm i -g zhiqu)。`;
|
|
48
48
|
}
|
|
49
|
-
if (v.level === 'available') return `有新版本 ${v.latest}(你现在是 ${VERSION}
|
|
49
|
+
if (v.level === 'available') return `有新版本 ${v.latest}(你现在是 ${VERSION}):运行 zhiqu update 更新`;
|
|
50
50
|
return null;
|
|
51
51
|
}
|