@aibyzero/cm-workflow 0.16.7 → 0.16.9
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/.codex-plugin/plugin.json +1 -1
- package/CHANGELOG.md +37 -0
- package/README.md +39 -3
- package/VERSION +1 -1
- package/assets/readme/workflow-recovery-detailed.png +0 -0
- package/assets/readme/workflow-stages-detailed.png +0 -0
- package/compat/claude-commands/cm-notify.md +3 -0
- package/docs/installation.md +1 -1
- package/docs/js-workflow-control.md +61 -4
- package/docs/next-steps.md +8 -2
- package/docs/user-guide.md +204 -0
- package/package.json +2 -1
- package/runtime/js/cm-ai/claude-review-stream.mjs +49 -13
- package/runtime/js/cm-ai/cm-ai-admission.mjs +3 -1
- package/runtime/js/cm-ai/cm-ai-conversation-entry.mjs +138 -26
- package/runtime/js/cm-ai/cm-ai-qa-log.mjs +142 -21
- package/runtime/js/cm-ai/code-projects.mjs +4 -1
- package/runtime/js/cm-ai/codex-review-adapter.mjs +2 -1
- package/runtime/js/cm-ai/developer-adapter.mjs +7 -0
- package/runtime/js/cm-ai/drive-core.mjs +26 -4
- package/runtime/js/cm-ai/durable-runner-state.mjs +951 -43
- package/runtime/js/cm-ai/effect-abandon-log.mjs +5 -2
- package/runtime/js/cm-ai/effect-contract.mjs +6 -1
- package/runtime/js/cm-ai/execution-store.mjs +4 -2
- package/runtime/js/cm-ai/external-run-guard.mjs +18 -7
- package/runtime/js/cm-ai/host-check.mjs +4 -1
- package/runtime/js/cm-ai/host-conversation-execution.mjs +11 -4
- package/runtime/js/cm-ai/host-documentation.mjs +53 -12
- package/runtime/js/cm-ai/host-project-execution.mjs +2 -2
- package/runtime/js/cm-ai/host-qa-executor.mjs +86 -17
- package/runtime/js/cm-ai/host-qa-fix-owner.mjs +44 -9
- package/runtime/js/cm-ai/host-session.mjs +47 -7
- package/runtime/js/cm-ai/host-tool-bridge.mjs +55 -6
- package/runtime/js/cm-ai/host-workflow-capabilities.mjs +4 -2
- package/runtime/js/cm-ai/operator-guidance.mjs +112 -3
- package/runtime/js/cm-ai/provider-review-observation.mjs +50 -0
- package/runtime/js/cm-ai/qa-resource-release.mjs +46 -0
- package/runtime/js/cm-ai/qa-round-budget.mjs +13 -0
- package/runtime/js/cm-ai/review-dispatch-limits.mjs +18 -0
- package/runtime/js/cm-ai/review-package.mjs +10 -1
- package/runtime/js/cm-ai/reviewed-deliveries.mjs +29 -6
- package/runtime/js/cm-ai/reviewed-evidence-supersede.mjs +1 -1
- package/runtime/js/cm-ai/task-commit.mjs +39 -0
- package/runtime/js/cm-ai/task-runner.mjs +636 -65
- package/runtime/js/cm-ai/worker-claude-developer.mjs +9 -0
- package/runtime/js/cm-ai/worker-claude.mjs +10 -3
- package/runtime/js/cm-ai/worker-process-identity.mjs +43 -0
- package/runtime/js/cm-fix/cause-package.mjs +37 -8
- package/runtime/js/cm-fix/diagnosis.mjs +43 -0
- package/runtime/js/cm-fix/execution.mjs +356 -187
- package/runtime/js/cm-fix/host.mjs +2 -1
- package/runtime/js/cm-fix/investigation.mjs +25 -9
- package/runtime/js/cm-fix/pinned-baseline.mjs +24 -0
- package/runtime/js/cm-fix/progress.mjs +15 -3
- package/runtime/js/cm-fix/protected-edits.mjs +10 -3
- package/runtime/js/cm-fix/repair.mjs +11 -3
- package/runtime/js/cm-fix/test-author.mjs +10 -3
- package/runtime/js/cm-idea/session.mjs +67 -4
- package/runtime/js/cm-prd/review-correction.mjs +10 -4
- package/runtime/js/cm-prd/session.mjs +43 -0
- package/runtime/js/cm-refactor/records.mjs +98 -8
- package/runtime/js/cm-refactor/workflow.mjs +79 -9
- package/runtime/js/cm-test/host.mjs +10 -3
- package/runtime/js/cm-test/session.mjs +45 -6
- package/runtime/js/notify-run.mjs +26 -0
- package/runtime/js/notify-safe-read.mjs +49 -0
- package/runtime/js/notify-send.mjs +312 -0
- package/runtime/js/notify.mjs +492 -0
- package/runtime/js/policy-binding-compat.mjs +54 -0
- package/runtime/model-efficiency.md +17 -0
- package/scripts/ci-test-shards.mjs +67 -0
- package/scripts/ci-test-shards.test.mjs +64 -0
- package/scripts/ci-test-weights.json +222 -0
- package/scripts/cm-ai-abandon-effect-position.test.mjs +70 -2
- package/scripts/cm-ai-answer-gap-dispatch.test.mjs +86 -0
- package/scripts/cm-ai-answer-gap-driver.test.mjs +51 -0
- package/scripts/cm-ai-answer-gap-fixture.mjs +96 -0
- package/scripts/cm-ai-answer-gap-recheck.test.mjs +70 -0
- package/scripts/cm-ai-answer-gap-redo.test.mjs +103 -0
- package/scripts/cm-ai-batch-drive.mjs +55 -10
- package/scripts/cm-ai-batch-drive.test.mjs +65 -0
- package/scripts/cm-ai-batch-host.mjs +132 -13
- package/scripts/cm-ai-batch-host.test.mjs +255 -7
- package/scripts/cm-ai-batch-run-fixture.mjs +499 -0
- package/scripts/cm-ai-batch-run-member-stops.test.mjs +38 -0
- package/scripts/cm-ai-batch-run-parallel.test.mjs +7 -0
- package/scripts/cm-ai-batch-run-reschedule.test.mjs +17 -0
- package/scripts/cm-ai-batch-run.mjs +189 -22
- package/scripts/cm-ai-batch-run.test.mjs +22 -334
- package/scripts/cm-ai-develop-answer-invalid.test.mjs +104 -0
- package/scripts/cm-ai-develop-call-timeout.test.mjs +153 -0
- package/scripts/cm-ai-develop-driver-gaps.test.mjs +4 -3
- package/scripts/cm-ai-documentation-sync-gap.test.mjs +424 -0
- package/scripts/cm-ai-drive.mjs +68 -23
- package/scripts/cm-ai-drive.test.mjs +37 -2
- package/scripts/cm-ai-fix-run-delivery.test.mjs +138 -0
- package/scripts/cm-ai-host-cli-workflow.test.mjs +271 -0
- package/scripts/cm-ai-host-fixture.mjs +268 -0
- package/scripts/cm-ai-host-protected.test.mjs +251 -0
- package/scripts/cm-ai-host.mjs +28 -16
- package/scripts/cm-ai-host.test.mjs +17 -759
- package/scripts/cm-ai-light-modules.test.mjs +57 -0
- package/scripts/cm-ai-operator-guidance.test.mjs +30 -5
- package/scripts/cm-ai-project-qa-gate.test.mjs +71 -4
- package/scripts/cm-ai-protected-scope.test.mjs +7 -3
- package/scripts/cm-ai-qa-answer-gaps.test.mjs +99 -0
- package/scripts/cm-ai-qa-fix-cross-session.test.mjs +78 -11
- package/scripts/cm-ai-qa-rerun-timeouts.test.mjs +127 -8
- package/scripts/cm-ai-review-dispatch-exit.test.mjs +475 -0
- package/scripts/cm-ai-review-failure.test.mjs +124 -13
- package/scripts/cm-ai-review-timeout.test.mjs +188 -10
- package/scripts/cm-ai-run.mjs +15 -6
- package/scripts/cm-ai-supersede.test.mjs +486 -41
- package/scripts/cm-check-drive-files.mjs +8 -0
- package/scripts/cm-check-drive.test.mjs +2 -5
- package/scripts/cm-check-runtime.sh +4 -2
- package/scripts/cm-claude-developer-worker.test.mjs +15 -0
- package/scripts/cm-claude-review-stream.test.mjs +21 -16
- package/scripts/cm-claude-review-synthetic.test.mjs +155 -0
- package/scripts/cm-claude-worker.test.mjs +18 -2
- package/scripts/cm-external-model-recovery.test.mjs +29 -3
- package/scripts/cm-fix-abandon.test.mjs +249 -10
- package/scripts/cm-fix-cause-package.test.mjs +33 -0
- package/scripts/cm-fix-drive-unprotected.test.mjs +37 -0
- package/scripts/cm-fix-drive.mjs +34 -8
- package/scripts/cm-fix-host.mjs +8 -7
- package/scripts/cm-fix-rediagnosis.test.mjs +71 -4
- package/scripts/cm-fix-review-budget.test.mjs +13 -0
- package/scripts/cm-fix-review-recovery.test.mjs +35 -0
- package/scripts/cm-host-answer-timeout.test.mjs +65 -0
- package/scripts/cm-host-call-kinds.test.mjs +28 -0
- package/scripts/cm-host-documentation.test.mjs +49 -0
- package/scripts/cm-host-qa-executor.test.mjs +143 -10
- package/scripts/cm-idea-answer-gaps.test.mjs +164 -0
- package/scripts/cm-idea-drive.mjs +49 -11
- package/scripts/cm-idea-drive.test.mjs +66 -0
- package/scripts/cm-idea-host.mjs +106 -16
- package/scripts/cm-init-answer-gaps.test.mjs +258 -0
- package/scripts/cm-init-drive.mjs +41 -7
- package/scripts/cm-init-host.mjs +61 -12
- package/scripts/cm-log-event.mjs +49 -12
- package/scripts/cm-notify-command.test.mjs +900 -0
- package/scripts/cm-notify-read-drift.test.mjs +205 -0
- package/scripts/cm-notify.mjs +469 -0
- package/scripts/cm-notify.test.mjs +685 -0
- package/scripts/cm-patrol.mjs +83 -0
- package/scripts/cm-patrol.test.mjs +161 -0
- package/scripts/cm-prd-answer-gaps.test.mjs +216 -0
- package/scripts/cm-prd-drive.mjs +18 -5
- package/scripts/cm-prd-drive.test.mjs +15 -0
- package/scripts/cm-prd-host.mjs +37 -4
- package/scripts/cm-prd-review-disposition.test.mjs +6 -1
- package/scripts/cm-prd-review-gate.mjs +56 -0
- package/scripts/cm-refactor-answer-gaps.test.mjs +206 -0
- package/scripts/cm-refactor-drive.mjs +52 -8
- package/scripts/cm-refactor-drive.test.mjs +44 -0
- package/scripts/cm-refactor-host.test.mjs +10 -6
- package/scripts/cm-release-smoke.sh +2 -0
- package/scripts/cm-test-answer-gaps.test.mjs +151 -0
- package/scripts/cm-test-drive.mjs +21 -5
- package/scripts/cm-token-report.mjs +497 -0
- package/scripts/cm-token-report.test.mjs +346 -0
- package/scripts/fixtures/claude-review-process.mjs +1 -1
- package/scripts/policy-binding-compat.test.mjs +99 -0
- package/scripts/scan-public-safety.py +4 -0
- package/scripts/validate-public-repo.py +4 -0
- package/skills/cm-ai/SKILL.md +7 -3
- package/skills/cm-ai/references/N6-qa-eval.md +2 -2
- package/skills/cm-ai/references/js-host-modes.md +90 -0
- package/skills/cm-ai/references/js-host-qa-recovery.md +136 -0
- package/skills/cm-ai/references/js-host-recovery.md +311 -0
- package/skills/cm-ai/references/js-host.md +76 -331
- package/skills/cm-check/SKILL.md +3 -3
- package/skills/cm-fix/SKILL.md +2 -0
- package/skills/cm-fix/references/js-host.md +56 -10
- package/skills/cm-idea/references/js-host.md +27 -2
- package/skills/cm-init/references/js-host.md +16 -3
- package/skills/cm-notify/SKILL.md +34 -0
- package/skills/cm-prd/SKILL.md +2 -0
- package/skills/cm-prd/references/js-change-recovery.md +35 -1
- package/skills/cm-refactor/references/js-host.md +17 -0
- package/skills/cm-test/references/js-host.md +14 -1
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
import test, {after} from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import fs from 'node:fs';
|
|
4
|
+
import os from 'node:os';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import {spawnSync} from 'node:child_process';
|
|
7
|
+
import {fileURLToPath} from 'node:url';
|
|
8
|
+
import {buildReport, collectClaude, collectCodex, readSpecsLog, renderMarkdown, parseArgs, main, lines, UNATTRIBUTED} from './cm-token-report.mjs';
|
|
9
|
+
|
|
10
|
+
const script = fileURLToPath(new URL('./cm-token-report.mjs', import.meta.url));
|
|
11
|
+
const home = fs.mkdtempSync(path.join(os.tmpdir(), 'cm-token-report-home-'));
|
|
12
|
+
process.env.CM_WORKFLOW_HOME = path.join(home, 'home');
|
|
13
|
+
process.env.CM_WORKFLOW_LOG_HOME = path.join(home, 'logs');
|
|
14
|
+
after(() => fs.rmSync(home, {recursive: true, force: true}));
|
|
15
|
+
|
|
16
|
+
const CANARY = 'SECRET-CANARY-正文不该出现';
|
|
17
|
+
const jl = (rows) => rows.map((r) => JSON.stringify(r)).join('\n') + '\n';
|
|
18
|
+
|
|
19
|
+
function claudeLine({id, t, cwd, usage, model = 'claude-test-1', side = false, session = 's1', text = CANARY, type = 'assistant'}) {
|
|
20
|
+
return {type, timestamp: t, cwd, sessionId: session, isSidechain: side, requestId: 'req_' + id,
|
|
21
|
+
message: {id, model, role: 'assistant', content: [{type: 'text', text}, {type: 'tool_use', name: 'Bash', input: {command: CANARY}}], usage}};
|
|
22
|
+
}
|
|
23
|
+
const u = (input, cw, cr, out) => ({input_tokens: input, cache_creation_input_tokens: cw, cache_read_input_tokens: cr, output_tokens: out});
|
|
24
|
+
const cmRow = (at, workflow, event, phase, run_id, extra = {}) => ({schema_version: 1, at, workflow, event, phase, run_id, ...extra});
|
|
25
|
+
|
|
26
|
+
function fixture(t) {
|
|
27
|
+
const root = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), 'cm-token-report-')));
|
|
28
|
+
t.after(() => fs.rmSync(root, {recursive: true, force: true}));
|
|
29
|
+
const project = path.join(root, 'proj');
|
|
30
|
+
const specs = path.join(project, 'specs');
|
|
31
|
+
fs.mkdirSync(specs, {recursive: true});
|
|
32
|
+
// CM 日志:develop 10:00-10:05;review 10:10-10:15;qa 10:20-10:25;同一运行事件间隔 <30 分钟所以外壳 10:00-10:25
|
|
33
|
+
const run = 'feat-T-001';
|
|
34
|
+
fs.writeFileSync(path.join(specs, '运行日志.jsonl'), jl([
|
|
35
|
+
cmRow('2026-10-01T10:00:00+00:00', 'cm-ai', 'progress', 'start', run, {node: 'N3', phase_name: 'developing', operation_id: 'op-dev'}),
|
|
36
|
+
// 时区写法不同:03:05-07:00 == 10:05Z
|
|
37
|
+
cmRow('2026-10-01T03:05:00-07:00', 'cm-ai', 'progress', 'complete', run, {node: 'N3', phase_name: 'developing', operation_id: 'op-dev', outcome: 'returned'}),
|
|
38
|
+
cmRow('2026-10-01T10:10:00+00:00', 'cm-ai', 'progress', 'start', run, {node: 'N4', phase_name: 'review_starting', operation_id: 'op-rev'}),
|
|
39
|
+
cmRow('2026-10-01T10:15:00+00:00', 'cm-ai', 'progress', 'complete', run, {node: 'N4', phase_name: 'review_starting', operation_id: 'op-rev'}),
|
|
40
|
+
cmRow('2026-10-01T10:20:00+00:00', 'cm-ai', 'test_run', 'start', run, {node: 'N6', operation_id: 'qa-1'}),
|
|
41
|
+
cmRow('2026-10-01T10:25:00+00:00', 'cm-ai', 'test_run', 'complete', run, {node: 'N6', operation_id: 'qa-1'}),
|
|
42
|
+
// 一个只有开始没有结束的 check(不确定窗口,只延伸到下一条事件 10:41)
|
|
43
|
+
cmRow('2026-10-01T10:40:00+00:00', 'cm-ai', 'progress', 'start', run, {node: 'N3', phase_name: 'checking', operation_id: 'op-chk'}),
|
|
44
|
+
cmRow('2026-10-01T10:41:00+00:00', 'cm-ai', 'decision', 'route', run, {node: 'N3'}),
|
|
45
|
+
].concat([
|
|
46
|
+
cmRow('2026-10-01T12:00:00+00:00', 'cm-fix', 'run_start', '', 'fix-1', {node: 'FIX'}),
|
|
47
|
+
cmRow('2026-10-01T12:10:00+00:00', 'cm-fix', 'run_done', '', 'fix-1', {node: 'FIX'}),
|
|
48
|
+
])));
|
|
49
|
+
const enc = project.replace(/[^A-Za-z0-9]/g, '-');
|
|
50
|
+
const claude = path.join(root, 'claude-projects');
|
|
51
|
+
const dir = path.join(claude, enc);
|
|
52
|
+
fs.mkdirSync(path.join(dir, 's1', 'subagents'), {recursive: true});
|
|
53
|
+
const T = (hm) => `2026-10-01T${hm}:00.000Z`;
|
|
54
|
+
fs.writeFileSync(path.join(dir, 's1.jsonl'), jl([
|
|
55
|
+
{type: 'user', timestamp: T('10:01'), cwd: project, message: {role: 'user', content: CANARY}},
|
|
56
|
+
// develop 窗口内,同一 message.id 三行(流式分块),应只算一次(用量相同)
|
|
57
|
+
claudeLine({id: 'm1', t: T('10:01'), cwd: project, usage: u(10, 100, 1000, 50)}),
|
|
58
|
+
claudeLine({id: 'm1', t: T('10:01'), cwd: project, usage: u(10, 100, 1000, 50)}),
|
|
59
|
+
claudeLine({id: 'm1', t: T('10:01'), cwd: project, usage: u(10, 100, 1000, 50)}),
|
|
60
|
+
// review 窗口内
|
|
61
|
+
claudeLine({id: 'm2', t: T('10:12'), cwd: project, usage: u(1, 0, 5000, 20)}),
|
|
62
|
+
// qa 窗口内,子代理
|
|
63
|
+
claudeLine({id: 'm3', t: T('10:22'), cwd: path.join(project, '.work', 'x'), usage: u(2, 10, 300, 5), side: true}),
|
|
64
|
+
// 窗口之外:10:30 距上一事件(10:25)与下一事件(10:40)都在外壳内?外壳 10:00-10:41 连续,所以归 other
|
|
65
|
+
claudeLine({id: 'm4', t: T('10:30'), cwd: project, usage: u(3, 0, 7, 1)}),
|
|
66
|
+
// 完全未归属:当天 15:00
|
|
67
|
+
claudeLine({id: 'm5', t: T('15:00'), cwd: project, usage: u(7, 7, 7, 7)}),
|
|
68
|
+
// 合成消息、cwd 不在项目内、字段不全:都不计
|
|
69
|
+
claudeLine({id: 'm6', t: T('10:02'), cwd: project, usage: u(99, 99, 99, 99), model: '<synthetic>'}),
|
|
70
|
+
claudeLine({id: 'm7', t: T('10:02'), cwd: path.join(root, 'other'), usage: u(99, 99, 99, 99)}),
|
|
71
|
+
claudeLine({id: 'm8', t: T('10:02'), cwd: project, usage: {input_tokens: 5}}),
|
|
72
|
+
// fix 窗口
|
|
73
|
+
claudeLine({id: 'm9', t: T('12:05'), cwd: project, usage: u(4, 4, 4, 4)}),
|
|
74
|
+
]));
|
|
75
|
+
// 子代理转录:m10 在 develop 窗口;m1 在这里又出现一次(续跑复制),应跨文件去重
|
|
76
|
+
fs.writeFileSync(path.join(dir, 's1', 'subagents', 'agent-a.jsonl'), jl([
|
|
77
|
+
claudeLine({id: 'm10', t: T('10:03'), cwd: project, usage: u(1, 2, 3, 4), side: true, session: 's1'}),
|
|
78
|
+
claudeLine({id: 'm1', t: T('10:01'), cwd: project, usage: u(10, 100, 1000, 50), side: true}),
|
|
79
|
+
]));
|
|
80
|
+
// Codex
|
|
81
|
+
const codex = path.join(root, 'codex-sessions');
|
|
82
|
+
const cdir = path.join(codex, '2026', '10', '01');
|
|
83
|
+
fs.mkdirSync(cdir, {recursive: true});
|
|
84
|
+
const tc = (t, total, last) => ({timestamp: t, type: 'event_msg', payload: {type: 'token_count', info: total === null ? null : {total_token_usage: {total_tokens: total}, last_token_usage: last}}});
|
|
85
|
+
const cu = (inp, cached, out) => ({input_tokens: inp, cached_input_tokens: cached, cache_write_input_tokens: 0, output_tokens: out, reasoning_output_tokens: 3, total_tokens: inp + out});
|
|
86
|
+
fs.writeFileSync(path.join(cdir, 'rollout-a.jsonl'), jl([
|
|
87
|
+
{timestamp: T('10:00'), type: 'session_meta', payload: {id: 'cx1', cwd: project, base_instructions: CANARY}},
|
|
88
|
+
{timestamp: T('10:00'), type: 'turn_context', payload: {cwd: project, model: 'gpt-test'}},
|
|
89
|
+
tc(T('10:01'), null),
|
|
90
|
+
tc(T('10:02'), 1100, cu(1000, 800, 100)),
|
|
91
|
+
tc(T('10:02'), 1100, cu(1000, 800, 100)), // 同一累计值重复事件
|
|
92
|
+
tc(T('15:30'), 2300, cu(1100, 1000, 100)),
|
|
93
|
+
{timestamp: T('10:03'), type: 'response_item', payload: {type: 'message', content: CANARY}},
|
|
94
|
+
]));
|
|
95
|
+
fs.writeFileSync(path.join(cdir, 'rollout-b.jsonl'), jl([
|
|
96
|
+
{timestamp: T('10:00'), type: 'session_meta', payload: {id: 'cx2', cwd: path.join(root, 'elsewhere')}},
|
|
97
|
+
tc(T('10:02'), 500, cu(400, 0, 100)),
|
|
98
|
+
]));
|
|
99
|
+
return {root, project, specs, claude, codex};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const get = (rep, wf, kind) => {
|
|
103
|
+
const w = rep.by_workflow.find((x) => x.workflow === wf);
|
|
104
|
+
if (!w) return null;
|
|
105
|
+
return kind ? w.steps.find((s) => s.kind === kind) || null : w;
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
test('按时间窗口归属:innermost 步骤优先,其次运行外壳,窗口外=未归属;跨时区时间戳对齐', async (t) => {
|
|
109
|
+
const f = fixture(t);
|
|
110
|
+
const rep = await buildReport({project: f.project, specsDirs: [f.specs], claudeProjects: f.claude, codexSessions: f.codex});
|
|
111
|
+
// develop 10:00-10:05:m1(10,100,1000,50)去重后一次 + m10(1,2,3,4) + codex 10:02 (未缓存 200,读 800,出 100)
|
|
112
|
+
const dev = get(rep, 'cm-ai', 'develop');
|
|
113
|
+
assert.deepEqual([dev.calls, dev.input, dev.cache_write, dev.cache_read, dev.output], [3, 10 + 1 + 200, 100 + 2, 1000 + 3 + 800, 50 + 4 + 100]);
|
|
114
|
+
const rev = get(rep, 'cm-ai', 'review');
|
|
115
|
+
assert.deepEqual([rev.calls, rev.input, rev.cache_read], [1, 1, 5000]);
|
|
116
|
+
const qa = get(rep, 'cm-ai', 'qa');
|
|
117
|
+
assert.deepEqual([qa.calls, qa.input, qa.cache_write], [1, 2, 10]);
|
|
118
|
+
assert.equal(get(rep, 'cm-ai', 'other').calls, 1, '10:30 在步骤窗口外但在运行外壳内');
|
|
119
|
+
assert.equal(get(rep, 'cm-fix', 'fix-diagnose').calls, 1);
|
|
120
|
+
const un = get(rep, UNATTRIBUTED);
|
|
121
|
+
// m5(15:00) + codex 15:30
|
|
122
|
+
assert.equal(un.calls, 2);
|
|
123
|
+
assert.equal(un.input, 7 + 100);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
test('重复 message.id(流式分块、子代理转录复制)只计一次,统计里如实报告', async (t) => {
|
|
127
|
+
const f = fixture(t);
|
|
128
|
+
const c = await collectClaude(f.claude, f.project);
|
|
129
|
+
const m1 = c.records.filter((r) => r.input === 10 && r.cache_read === 1000);
|
|
130
|
+
assert.equal(m1.length, 1);
|
|
131
|
+
assert.equal(c.stats.lines_with_usage, 13, '主转录 11 行 + 子代理转录 2 行(user 行不算)');
|
|
132
|
+
assert.equal(c.stats.duplicates_removed, 3, 'm1 共 4 行(主转录 3 行流式分块 + 子代理转录 1 行),去掉 3 行');
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
test('合成消息、cwd 不在项目内、字段不全不计入,且不估算', async (t) => {
|
|
136
|
+
const f = fixture(t);
|
|
137
|
+
const c = await collectClaude(f.claude, f.project);
|
|
138
|
+
assert.equal(c.stats.synthetic_skipped, 1);
|
|
139
|
+
assert.equal(c.stats.outside_cwd, 1);
|
|
140
|
+
assert.equal(c.stats.incomplete_usage, 1);
|
|
141
|
+
assert.ok(!c.records.some((r) => r.input === 99 || r.input === 5));
|
|
142
|
+
assert.equal(c.records.length, 7); // m1 m2 m3 m4 m5 m9 m10
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
test('Codex:input 拆成未缓存+缓存读,空 info 与重复累计值不计,其他 cwd 的会话跳过', async (t) => {
|
|
146
|
+
const f = fixture(t);
|
|
147
|
+
const x = await collectCodex(f.codex, f.project);
|
|
148
|
+
assert.equal(x.records.length, 2);
|
|
149
|
+
assert.equal(x.stats.null_info, 1);
|
|
150
|
+
assert.equal(x.stats.duplicates_removed, 1);
|
|
151
|
+
assert.equal(x.stats.sessions_matched, 1);
|
|
152
|
+
const r = x.records[0];
|
|
153
|
+
assert.deepEqual([r.input, r.cache_read, r.output, r.model], [200, 800, 100, 'gpt-test']);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
test('--since 同时过滤用量与窗口', async (t) => {
|
|
157
|
+
const f = fixture(t);
|
|
158
|
+
const rep = await buildReport({project: f.project, specsDirs: [f.specs], sinceIso: '2026-10-01T14:00:00Z', claudeProjects: f.claude, codexSessions: f.codex});
|
|
159
|
+
assert.equal(rep.total.calls, 2);
|
|
160
|
+
assert.equal(get(rep, UNATTRIBUTED).calls, 2);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
test('未配对的开始标不确定;窗口计数与不确定调用数如实给出', async (t) => {
|
|
164
|
+
const f = fixture(t);
|
|
165
|
+
const rep = await buildReport({project: f.project, specsDirs: [f.specs], claudeProjects: f.claude, codexSessions: f.codex});
|
|
166
|
+
assert.equal(rep.windows.unpaired_start, 1);
|
|
167
|
+
assert.equal(rep.windows.orphan_complete, 0);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
test('最重调用榜按加权排序,只含元数据;输出里没有任何正文', async (t) => {
|
|
171
|
+
const f = fixture(t);
|
|
172
|
+
const rep = await buildReport({project: f.project, specsDirs: [f.specs], claudeProjects: f.claude, codexSessions: f.codex});
|
|
173
|
+
// 加权最大的是 codex 10:02 那次(未缓存 200、读 800、出 100)
|
|
174
|
+
assert.equal(rep.top_calls[0].src, 'codex');
|
|
175
|
+
assert.equal(rep.top_calls[0].cache_read, 800);
|
|
176
|
+
const weights = rep.weights;
|
|
177
|
+
assert.equal(rep.top_calls[0].weighted, 200 * weights.input + 800 * weights.cache_read + 100 * weights.output);
|
|
178
|
+
for (let i = 1; i < rep.top_calls.length; i++) assert.ok(rep.top_calls[i - 1].weighted >= rep.top_calls[i].weighted);
|
|
179
|
+
const md = renderMarkdown(rep);
|
|
180
|
+
assert.ok(!md.includes(CANARY) && !JSON.stringify(rep).includes(CANARY));
|
|
181
|
+
assert.match(md, /加权合计的权重/);
|
|
182
|
+
assert.match(md, new RegExp(UNATTRIBUTED.replace(/[()]/g, '.')));
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
test('CLI:markdown 与 --json 输出,缺参数返回 2,不写任何文件', async (t) => {
|
|
186
|
+
const f = fixture(t);
|
|
187
|
+
const before = fs.readdirSync(f.root).sort().join();
|
|
188
|
+
const run = (args) => spawnSync(process.execPath, [script, ...args], {encoding: 'utf8', env: process.env});
|
|
189
|
+
const ok = run(['--project', f.project, '--specs', f.specs, '--claude-projects', f.claude, '--codex-sessions', f.codex]);
|
|
190
|
+
assert.equal(ok.status, 0, ok.stderr);
|
|
191
|
+
assert.match(ok.stdout, /# CM token 用量报告/);
|
|
192
|
+
const js = run(['--project', f.project, '--specs', f.specs, '--claude-projects', f.claude, '--codex-sessions', f.codex, '--json', '--weights', 'in=1,cw=2,cr=0.5,out=4']);
|
|
193
|
+
assert.equal(js.status, 0, js.stderr);
|
|
194
|
+
const parsed = JSON.parse(js.stdout);
|
|
195
|
+
assert.deepEqual(parsed.weights, {input: 1, cache_write: 2, cache_read: 0.5, output: 4});
|
|
196
|
+
assert.ok(Array.isArray(parsed.top_calls));
|
|
197
|
+
assert.equal(run(['--project', f.project]).status, 2);
|
|
198
|
+
assert.equal(run(['--bogus']).status, 2);
|
|
199
|
+
assert.equal(fs.readdirSync(f.root).sort().join(), before);
|
|
200
|
+
assert.throws(() => parseArgs(['--weights', 'in=x']), /--weights/);
|
|
201
|
+
const io = {stdout: {write() {}}, stderr: {write() {}}};
|
|
202
|
+
assert.equal(await main(['--project', f.project, '--specs', f.specs, '--since', 'not-a-date', '--claude-projects', f.claude, '--codex-sessions', f.codex], io), 1);
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
test('目录不存在或日志缺失:如实标注,不报错也不估算', async (t) => {
|
|
206
|
+
const f = fixture(t);
|
|
207
|
+
const rep = await buildReport({project: f.project, specsDirs: [path.join(f.root, 'nope')], claudeProjects: path.join(f.root, 'no-claude'), codexSessions: path.join(f.root, 'no-codex')});
|
|
208
|
+
assert.equal(rep.total.calls, 0);
|
|
209
|
+
assert.equal(rep.cm_logs[0].missing, true);
|
|
210
|
+
assert.equal(rep.claude_stats.dir_missing, true);
|
|
211
|
+
assert.equal(rep.codex_stats.dir_missing, true);
|
|
212
|
+
assert.match(renderMarkdown(rep), /文件不存在/);
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
// ---------- 第一轮审查修复的回归测试 ----------
|
|
216
|
+
const run1 = (f, extra = {}) => buildReport({project: f.project, specsDirs: [f.specs], claudeProjects: f.claude, codexSessions: f.codex, ...extra});
|
|
217
|
+
const projDir = (f) => path.join(f.claude, f.project.replace(/[^A-Za-z0-9]/g, '-'));
|
|
218
|
+
|
|
219
|
+
test('隐私:model 是数组/对象/过长/带空格时不进入输出(只放行短的模型标识符)', async (t) => {
|
|
220
|
+
const f = fixture(t);
|
|
221
|
+
fs.appendFileSync(path.join(projDir(f), 's1.jsonl'), jl([
|
|
222
|
+
claudeLine({id: 'p1', t: '2026-10-01T10:03:00.000Z', cwd: f.project, usage: u(1, 1, 1, 1), model: ['BODY-CANARY']}),
|
|
223
|
+
claudeLine({id: 'p2', t: '2026-10-01T10:03:00.000Z', cwd: f.project, usage: u(1, 1, 1, 1), model: {nested: 'BODY-CANARY'}}),
|
|
224
|
+
claudeLine({id: 'p3', t: '2026-10-01T10:03:00.000Z', cwd: f.project, usage: u(1, 1, 1, 1), model: 'BODY CANARY with spaces'}),
|
|
225
|
+
claudeLine({id: 'p4', t: '2026-10-01T10:03:00.000Z', cwd: f.project, usage: u(1, 1, 1, 1), model: 'x'.repeat(101)}),
|
|
226
|
+
{...claudeLine({id: 'p5', t: '2026-10-01T10:03:00.000Z', cwd: f.project, usage: u(1, 1, 1, 1)}), sessionId: ['BODY-CANARY']},
|
|
227
|
+
]));
|
|
228
|
+
const cdir = path.join(f.codex, '2026', '10', '01');
|
|
229
|
+
fs.appendFileSync(path.join(cdir, 'rollout-a.jsonl'), jl([
|
|
230
|
+
{timestamp: '2026-10-01T10:04:00.000Z', type: 'turn_context', payload: {cwd: f.project, model: ['BODY-CANARY']}},
|
|
231
|
+
{timestamp: '2026-10-01T10:04:01.000Z', type: 'event_msg', payload: {type: 'token_count', info: {total_token_usage: {total_tokens: 9000}, last_token_usage: {input_tokens: 10, cached_input_tokens: 0, cache_write_input_tokens: 0, output_tokens: 1}}}},
|
|
232
|
+
]));
|
|
233
|
+
const rep = await run1(f);
|
|
234
|
+
const out = renderMarkdown(rep) + JSON.stringify(rep);
|
|
235
|
+
assert.ok(!out.includes('BODY-CANARY') && !out.includes('BODY CANARY') && !out.includes('xxxxxxxxxx'));
|
|
236
|
+
assert.equal(rep.by_model['未知'].calls, 4); // p1..p4;Codex 的非法 model 保留上一个合法值
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
test('安全:运行日志是指向 specs 之外的符号链接时不读取;转录目录里指向外部的链接跳过并计数', async (t) => {
|
|
240
|
+
const f = fixture(t);
|
|
241
|
+
const outside = path.join(f.root, 'outside.jsonl');
|
|
242
|
+
fs.writeFileSync(outside, jl([cmRow('2026-10-01T10:00:00Z', 'cm-ai', 'run_start', '', 'evil-run')]));
|
|
243
|
+
const spec2 = path.join(f.project, 'specs2');
|
|
244
|
+
fs.mkdirSync(spec2);
|
|
245
|
+
fs.symlinkSync(outside, path.join(spec2, '运行日志.jsonl'));
|
|
246
|
+
const lg = await readSpecsLog(spec2);
|
|
247
|
+
assert.equal(lg.unsafe, true);
|
|
248
|
+
assert.equal(lg.rows.length, 0);
|
|
249
|
+
const evil = path.join(f.root, 'evil-transcript.jsonl');
|
|
250
|
+
fs.writeFileSync(evil, jl([claudeLine({id: 'evil', t: '2026-10-01T10:01:00.000Z', cwd: f.project, usage: u(777, 0, 0, 0)})]));
|
|
251
|
+
fs.symlinkSync(evil, path.join(projDir(f), 'link.jsonl'));
|
|
252
|
+
const c = await collectClaude(f.claude, f.project);
|
|
253
|
+
assert.equal(c.stats.unsafe_links_skipped, 1);
|
|
254
|
+
assert.ok(!c.records.some((r) => r.input === 777));
|
|
255
|
+
fs.symlinkSync(evil, path.join(f.codex, '2026', '10', '01', 'link.jsonl'));
|
|
256
|
+
assert.equal((await collectCodex(f.codex, f.project)).stats.unsafe_links_skipped, 1);
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
test('--since 按事件时间戳过滤 Codex,不按日期目录跳过', async (t) => {
|
|
260
|
+
const f = fixture(t);
|
|
261
|
+
const old = path.join(f.codex, '2020', '01', '01');
|
|
262
|
+
fs.mkdirSync(old, {recursive: true});
|
|
263
|
+
const ev = (t2, total) => ({timestamp: t2, type: 'event_msg', payload: {type: 'token_count', info: {total_token_usage: {total_tokens: total}, last_token_usage: {input_tokens: 10, cached_input_tokens: 0, cache_write_input_tokens: 0, output_tokens: 1}}}});
|
|
264
|
+
fs.writeFileSync(path.join(old, 'rollout-old-dir.jsonl'), jl([
|
|
265
|
+
{timestamp: '2026-10-01T09:00:00Z', type: 'session_meta', payload: {id: 'cx9', cwd: f.project}},
|
|
266
|
+
ev('2026-10-01T09:30:00Z', 11), ev('2026-10-01T10:30:00Z', 22),
|
|
267
|
+
]));
|
|
268
|
+
// 文件修改时间早于 since 也不能整份跳过。
|
|
269
|
+
fs.utimesSync(path.join(old, 'rollout-old-dir.jsonl'), new Date('2026-10-01T09:00:00Z'), new Date('2026-10-01T09:00:00Z'));
|
|
270
|
+
const x = await collectCodex(f.codex, f.project, {sinceMs: Date.parse('2026-10-01T10:00:00Z')});
|
|
271
|
+
assert.ok(x.records.some((r) => r.session === 'cx9' && r.t === Date.parse('2026-10-01T10:30:00Z')));
|
|
272
|
+
assert.ok(!x.records.some((r) => r.session === 'cx9' && r.t === Date.parse('2026-10-01T09:30:00Z')));
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
test('缺失的缓存字段不补 0:缺 cached_input_tokens 的 Codex 记录整条排除并计数,缺 cache_write 记为未知', async (t) => {
|
|
276
|
+
const f = fixture(t);
|
|
277
|
+
const ev = (t2, total, last) => ({timestamp: t2, type: 'event_msg', payload: {type: 'token_count', info: {total_token_usage: {total_tokens: total}, last_token_usage: last}}});
|
|
278
|
+
fs.appendFileSync(path.join(f.codex, '2026', '10', '01', 'rollout-a.jsonl'), jl([
|
|
279
|
+
ev('2026-10-01T10:05:00Z', 5000, {input_tokens: 900, output_tokens: 10}), // 缺 cached
|
|
280
|
+
ev('2026-10-01T10:06:00Z', 5100, {input_tokens: 50, cached_input_tokens: 10, output_tokens: 1}), // 缺 cache_write
|
|
281
|
+
ev('2026-10-01T10:07:00Z', 5200, {input_tokens: 5, cached_input_tokens: 10, output_tokens: 1}), // cached > input:不自洽
|
|
282
|
+
]));
|
|
283
|
+
const x = await collectCodex(f.codex, f.project);
|
|
284
|
+
assert.equal(x.stats.incomplete_usage, 2);
|
|
285
|
+
assert.equal(x.stats.cache_write_unknown, 1);
|
|
286
|
+
assert.ok(!x.records.some((r) => r.input === 900));
|
|
287
|
+
const unknown = x.records.find((r) => r.cache_write === null);
|
|
288
|
+
assert.deepEqual([unknown.input, unknown.cache_read], [40, 10]);
|
|
289
|
+
const rep = await run1(f);
|
|
290
|
+
assert.equal(rep.flags.cache_write_unknown_calls, 1);
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
test('读取提前结束或遇到超长行时:底层流被销毁,超长行被跳过并计数', async (t) => {
|
|
294
|
+
const f = fixture(t);
|
|
295
|
+
const file = path.join(f.root, 'long.jsonl');
|
|
296
|
+
fs.writeFileSync(file, 'a\n' + 'x'.repeat(5000) + '\nb\n');
|
|
297
|
+
const got = [];
|
|
298
|
+
for await (const l of lines(file, 100)) got.push(typeof l === 'symbol' ? 'OVERLONG' : l);
|
|
299
|
+
assert.deepEqual(got, ['a', 'OVERLONG', 'b']);
|
|
300
|
+
const streams = [];
|
|
301
|
+
const orig = fs.createReadStream;
|
|
302
|
+
t.mock.method(fs, 'createReadStream', (...a) => { const s = orig(...a); streams.push(s); return s; });
|
|
303
|
+
for await (const l of lines(file)) { void l; break; }
|
|
304
|
+
assert.equal(streams.length, 1);
|
|
305
|
+
assert.equal(streams[0].destroyed, true);
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
test('非对象的合法 JSON 行(null、数组、字符串)不抛错,只计坏行;超长日志行被跳过', async (t) => {
|
|
309
|
+
const f = fixture(t);
|
|
310
|
+
fs.appendFileSync(path.join(f.specs, '运行日志.jsonl'), 'null\n[1]\n"usage"\n' + JSON.stringify({at: '2026-10-01T10:00:00Z', run_id: 'big', pad: 'y'.repeat(1100000)}) + '\n');
|
|
311
|
+
const lg = await readSpecsLog(f.specs);
|
|
312
|
+
assert.equal(lg.bad, 3);
|
|
313
|
+
assert.equal(lg.overlong, 1);
|
|
314
|
+
fs.appendFileSync(path.join(projDir(f), 's1.jsonl'), 'null\n"usage"\n[{"usage":1}]\n{"type":"assistant","message":null,"usage":1}\n{"type":"assistant","message":{"usage":"x"}}\n');
|
|
315
|
+
fs.appendFileSync(path.join(f.codex, '2026', '10', '01', 'rollout-a.jsonl'), 'null\n{"type":"event_msg","payload":"token_count"}\n[\"token_count\"]\n');
|
|
316
|
+
const rep = await run1(f);
|
|
317
|
+
assert.equal(rep.claude_stats.bad_lines, 2); // "usage" 与 [{"usage":1}];null 行不含 usage 字样被预过滤
|
|
318
|
+
assert.equal(rep.codex_stats.bad_lines, 1); // ["token_count"];null 行不含 token_count 字样被预过滤
|
|
319
|
+
assert.equal(rep.total.calls, 7 + 2);
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
test('十几万条记录时时间范围不抛 RangeError(增量求最值)', async (t) => {
|
|
323
|
+
const f = fixture(t);
|
|
324
|
+
const big = path.join(projDir(f), 'big.jsonl');
|
|
325
|
+
const parts = [];
|
|
326
|
+
for (let i = 0; i < 150000; i++) parts.push(`{"type":"assistant","timestamp":"2026-10-02T00:00:00.000Z","cwd":${JSON.stringify(f.project)},"message":{"id":"b${i}","model":"m","usage":{"input_tokens":1,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"output_tokens":1}}}`);
|
|
327
|
+
fs.writeFileSync(big, parts.join('\n') + '\n');
|
|
328
|
+
const rep = await run1(f);
|
|
329
|
+
assert.ok(rep.total.calls >= 150000);
|
|
330
|
+
assert.ok(rep.span.to >= rep.span.from);
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
test('会话按完整 ID 分组;目录或模型名叫 constructor 时照常统计', async (t) => {
|
|
334
|
+
const f = fixture(t);
|
|
335
|
+
const sub = path.join(f.project, 'constructor');
|
|
336
|
+
fs.writeFileSync(path.join(projDir(f), 'two.jsonl'), jl([
|
|
337
|
+
claudeLine({id: 'p1', t: '2026-10-02T00:00:00Z', cwd: sub, usage: u(10, 0, 0, 1), session: '019c7714-aaaa', model: 'constructor'}),
|
|
338
|
+
claudeLine({id: 'p2', t: '2026-10-02T00:01:00Z', cwd: sub, usage: u(100, 0, 0, 1), session: '019c7714-bbbb', model: 'constructor'}),
|
|
339
|
+
]));
|
|
340
|
+
const rep = await run1(f);
|
|
341
|
+
const sessions = rep.by_session_top.filter((d) => d.session.includes('019c7714'));
|
|
342
|
+
assert.equal(sessions.length, 2);
|
|
343
|
+
assert.deepEqual(sessions.map((d) => d.input).sort((a, b) => a - b), [10, 100]);
|
|
344
|
+
assert.ok(rep.by_dir_top.some((d) => d.dir === 'constructor' && d.calls === 2));
|
|
345
|
+
assert.ok(!renderMarkdown(rep).includes(CANARY));
|
|
346
|
+
});
|
|
@@ -39,5 +39,5 @@ for(const event of [
|
|
|
39
39
|
{type:'system',subtype:'init',session_id},
|
|
40
40
|
{type:'assistant',session_id,parent_tool_use_id:null,
|
|
41
41
|
message:{role:'assistant',content:[{type:'text',text:JSON.stringify(value)}]}},
|
|
42
|
-
{type:'result',subtype:'success',session_id,is_error:false,num_turns:1,result:JSON.stringify(value)},
|
|
42
|
+
{type:'result',subtype:'success',session_id,is_error:false,num_turns:1,structured_output:value,result:JSON.stringify(value)},
|
|
43
43
|
]) process.stdout.write(JSON.stringify(event)+'\n');
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
// Sessions created by origin/main 0b3040e (before batch 3 changed the bound
|
|
2
|
+
// cm-init and cm-test documents) keep working under their original binding;
|
|
3
|
+
// any other drift still refuses. Fixtures carry the exact 0b3040e binding.
|
|
4
|
+
import test from 'node:test';
|
|
5
|
+
import assert from 'node:assert/strict';
|
|
6
|
+
import fs from 'node:fs';
|
|
7
|
+
import os from 'node:os';
|
|
8
|
+
import path from 'node:path';
|
|
9
|
+
import {spawn,spawnSync} from 'node:child_process';
|
|
10
|
+
import {createInterface} from 'node:readline';
|
|
11
|
+
import {once} from 'node:events';
|
|
12
|
+
import {fileURLToPath} from 'node:url';
|
|
13
|
+
import {digest} from '../runtime/js/cm-ai/effect-contract.mjs';
|
|
14
|
+
import {LEGACY_INIT_POLICIES,LEGACY_TEST_POLICIES,POLICY_DOC_SUCCESSORS,legacyInitBindings,sha256} from '../runtime/js/policy-binding-compat.mjs';
|
|
15
|
+
const root=fileURLToPath(new URL('..',import.meta.url));
|
|
16
|
+
const legacyInit=LEGACY_INIT_POLICIES[0],legacyTest=LEGACY_TEST_POLICIES[0];
|
|
17
|
+
|
|
18
|
+
test('legacy digests are exactly the 0b3040e policy documents (when that commit is available)',t=>{
|
|
19
|
+
const show=file=>{const out=spawnSync('git',['show',`0b3040e:${file}`],{cwd:root,encoding:'utf8'});return out.status===0?out.stdout:undefined;};
|
|
20
|
+
if(show('skills/cm-init/SKILL.md')===undefined){t.skip('0b3040e not in this clone');return;}
|
|
21
|
+
const files=Object.keys(legacyInit.files);
|
|
22
|
+
assert.equal(digest(files.map(file=>[file,show(file)])),legacyInit.policyDigest);
|
|
23
|
+
for(const file of files)assert.equal(sha256(show(file)),legacyInit.files[file],file);
|
|
24
|
+
assert.equal(digest(show('skills/cm-test/references/js-host.md')),legacyTest.policy);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test('the current bound documents are listed as known successors (update the list when they change)',()=>{
|
|
28
|
+
for(const [file,list] of Object.entries(POLICY_DOC_SUCCESSORS))
|
|
29
|
+
assert.ok(list.includes(sha256(fs.readFileSync(path.join(root,file),'utf8'))),`${file} changed: add its digest to POLICY_DOC_SUCCESSORS`);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test('legacy init binding is offered only when every other bound file is byte-identical',()=>{
|
|
33
|
+
const files=Object.keys(legacyInit.files).map(file=>[file,fs.readFileSync(path.join(root,file),'utf8')]);
|
|
34
|
+
assert.equal(legacyInitBindings({project:'/p',workflowRoot:'/w',files}).length,1);
|
|
35
|
+
assert.deepEqual(legacyInitBindings({project:'/p',workflowRoot:'/w',files:files.map(([file,content])=>[file,file.endsWith('testing.md')?content+'x':content])}),[]);
|
|
36
|
+
assert.deepEqual(legacyInitBindings({project:'/p',workflowRoot:'/w',files:files.map(([file,content])=>[file,file===legacyInit.changed?content+'x':content])}),[]);
|
|
37
|
+
assert.deepEqual(legacyInitBindings({project:'/p',workflowRoot:'/w',files:files.slice(1)}),[]);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
async function serve(args,{cwd,operations}){
|
|
41
|
+
const child=spawn(process.execPath,args,{cwd,stdio:['pipe','pipe','pipe']});let stderr='',sessionId,index=0;const results=[];
|
|
42
|
+
child.stderr.on('data',chunk=>{stderr+=chunk;});const closed=once(child,'close');
|
|
43
|
+
for await(const line of createInterface({input:child.stdout})){const message=JSON.parse(line);
|
|
44
|
+
if(message.type==='host_ready'){sessionId=message.sessionId;}
|
|
45
|
+
else if(message.requestId)results.push(message.result??message.error);
|
|
46
|
+
if(message.type==='host_ready'||message.requestId){
|
|
47
|
+
if(index<operations.length)child.stdin.write(JSON.stringify({requestId:`r${++index}`,...operations[index-1]})+'\n');
|
|
48
|
+
else child.stdin.write(JSON.stringify({type:'host_close',sessionId})+'\n');}
|
|
49
|
+
}
|
|
50
|
+
const [code]=await closed;return {code,results,stderr};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
test('real cm-init host: a session bound by 0b3040e opens, reports status and keeps its binding; other digests refuse',{timeout:30000},async t=>{
|
|
54
|
+
const dir=fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(),'cm-policy-compat-'))),project=path.join(dir,'project'),file=path.join(dir,'init.json');
|
|
55
|
+
t.after(()=>fs.rmSync(dir,{recursive:true,force:true}));fs.mkdirSync(project);fs.writeFileSync(path.join(project,'README.md'),'# Synthetic\n');
|
|
56
|
+
const args=[path.join(root,'scripts/cm-init-host.mjs'),'serve','--skill-dir',path.join(root,'skills/cm-init'),'--project',project,
|
|
57
|
+
'--host-context','author-a','--session-file',file];
|
|
58
|
+
// The fixture is the exact shape 0b3040e wrote for a fresh session at this workflow root.
|
|
59
|
+
const legacy={version:1,workflow:'cm-init',binding:{project,workflowRoot:root.replace(/\/$/,''),policyDigest:legacyInit.policyDigest},
|
|
60
|
+
checkpoint:{stage:'ready',result:null,verification:null,selection:null,confirmation:null,review:null,writeResult:null,reviewPackage:null,
|
|
61
|
+
reviewEvidence:null,analysisResult:null,revisionHistory:[],authorContexts:['author-a'],authorContextId:'author-a'},pending:null,cancelled:false};
|
|
62
|
+
fs.writeFileSync(file,JSON.stringify(legacy),{mode:0o600});
|
|
63
|
+
let run=await serve(args,{cwd:project,operations:[{operation:'status'},{operation:'cancel'}]});
|
|
64
|
+
assert.equal(run.code,0,run.stderr);assert.equal(run.results[0].stage,'ready');assert.equal(run.results[1].stage,'cancelled');
|
|
65
|
+
assert.equal(JSON.parse(fs.readFileSync(file,'utf8')).binding.policyDigest,legacyInit.policyDigest,'the original binding is kept');
|
|
66
|
+
fs.writeFileSync(file,JSON.stringify({...legacy,binding:{...legacy.binding,policyDigest:'0'.repeat(64)}}),{mode:0o600});
|
|
67
|
+
run=await serve(args,{cwd:project,operations:[]});
|
|
68
|
+
assert.notEqual(run.code,0);assert.match(run.stderr,/idea_session_binding_changed/);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test('real cm-test host: a journal bound by 0b3040e opens and replays; another policy digest refuses',{timeout:30000},async t=>{
|
|
72
|
+
const temp=fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(),'cm-policy-compat-test-'))),project=path.join(temp,'project');
|
|
73
|
+
t.after(()=>fs.rmSync(temp,{recursive:true,force:true}));fs.mkdirSync(project);
|
|
74
|
+
fs.writeFileSync(path.join(project,'input.mjs'),'export const valid = value => value !== null;\n');fs.writeFileSync(path.join(project,'AGENTS.md'),'Synthetic\n');
|
|
75
|
+
const contract={schemaVersion:'1.0',feature:'example',cases:[{id:'TC-001',kind:'logic',origin:'user',blocking:true,acIds:[],taskIds:[],
|
|
76
|
+
title:'Synthetic',preconditions:[],steps:['Observe'],expected:['Expected'],cleanup:[]}]};
|
|
77
|
+
const cases=path.join(temp,'cases.json');fs.writeFileSync(cases,JSON.stringify(contract));
|
|
78
|
+
const config={skillDir:path.join(root,'skills/cm-test'),project,runtime:'codex',arguments:{cases,logic:true},sources:['input.mjs','AGENTS.md'],
|
|
79
|
+
commands:[],environment:{scope:'local',kind:'web',carrier:'browser',target:'http://127.0.0.1:3000'},logHome:path.join(temp,'logs')};
|
|
80
|
+
const configPath=path.join(temp,'config.json'),session=path.join(temp,'session');fs.writeFileSync(configPath,JSON.stringify(config));
|
|
81
|
+
const args=[path.join(root,'scripts/cm-test-host.mjs'),'serve','--config',configPath,'--session-dir',session];
|
|
82
|
+
// Create a run whose qa_logic answer is lost, then rebind its context to the 0b3040e policy (re-chaining hashes).
|
|
83
|
+
const child=spawn(process.execPath,args,{stdio:['pipe','pipe','pipe']});const closed=once(child,'close');
|
|
84
|
+
createInterface({input:child.stdout}).on('line',line=>{const message=JSON.parse(line);
|
|
85
|
+
if(message.type==='host_ready')child.stdin.write(JSON.stringify({requestId:'s',operation:'start'})+'\n');
|
|
86
|
+
if(message.type==='host_request')child.kill('SIGKILL');});
|
|
87
|
+
await closed;
|
|
88
|
+
const journal=path.join(session,'execution.jsonl');
|
|
89
|
+
const rebind=policy=>{let previous=null;const rows=fs.readFileSync(journal,'utf8').trimEnd().split('\n').map(line=>JSON.parse(line)).map(({hash,...row})=>{
|
|
90
|
+
const body={...row,...(row.type==='context'?{value:{...row.value,binding:{...row.value.binding,policy}}}:{}),previous};
|
|
91
|
+
previous=digest(body);return {...body,hash:previous};});
|
|
92
|
+
fs.writeFileSync(journal,rows.map(row=>JSON.stringify(row)).join('\n')+'\n',{mode:0o600});};
|
|
93
|
+
rebind(legacyTest.policy);
|
|
94
|
+
let run=await serve(args,{cwd:temp,operations:[{operation:'status'}]});
|
|
95
|
+
assert.equal(run.code,0,run.stderr);assert.equal(run.results[0].stage,'interrupted');assert.equal(run.results[0].recovery.unknown[0].callKind,'qa_logic');
|
|
96
|
+
rebind('1'.repeat(64));
|
|
97
|
+
run=await serve(args,{cwd:temp,operations:[]});
|
|
98
|
+
assert.notEqual(run.code,0);assert.match(run.stderr,/cm_test_session_binding_changed/);
|
|
99
|
+
});
|
|
@@ -39,14 +39,18 @@ ALLOW_LOOPBACK_ENDPOINT_FILES = {
|
|
|
39
39
|
Path("runtime/js/cm-ai/claude-tool-preview.mjs"),
|
|
40
40
|
Path("scripts/cm-ai-batch-host.test.mjs"),
|
|
41
41
|
Path("scripts/cm-ai-host.test.mjs"),
|
|
42
|
+
Path("scripts/cm-ai-host-cli-workflow.test.mjs"),
|
|
42
43
|
Path("scripts/cm-ai-multi-root.test.mjs"),
|
|
43
44
|
Path("scripts/cm-ai-nested-execution.test.mjs"),
|
|
44
45
|
Path("scripts/cm-claude-probe.test.mjs"),
|
|
45
46
|
Path("scripts/cm-fix-visual-bare.test.mjs"),
|
|
46
47
|
Path("scripts/cm-fix-walkthrough.test.mjs"),
|
|
48
|
+
Path("scripts/cm-notify-command.test.mjs"),
|
|
47
49
|
Path("scripts/cm-host-qa-executor.test.mjs"),
|
|
48
50
|
Path("scripts/cm-test-host.test.mjs"),
|
|
51
|
+
Path("scripts/cm-test-answer-gaps.test.mjs"),
|
|
49
52
|
Path("scripts/cm-test-session.test.mjs"),
|
|
53
|
+
Path("scripts/policy-binding-compat.test.mjs"),
|
|
50
54
|
}
|
|
51
55
|
URL_AUTHORITY = re.compile(r"https?://([^/\s?#'\"`<>]+)")
|
|
52
56
|
LOOPBACK_AUTHORITY = re.compile(
|
|
@@ -21,6 +21,7 @@ CORE_SKILLS = (
|
|
|
21
21
|
"cm-refactor",
|
|
22
22
|
"cm-check",
|
|
23
23
|
"cm-runtime",
|
|
24
|
+
"cm-notify",
|
|
24
25
|
)
|
|
25
26
|
CM_AI_RUNTIME = (
|
|
26
27
|
"cm-ai-admission.mjs",
|
|
@@ -98,6 +99,9 @@ REQUIRED = (
|
|
|
98
99
|
"scripts/cm-runtime-edit.mjs",
|
|
99
100
|
"scripts/cm-runtime-install.mjs",
|
|
100
101
|
"scripts/cm-runtime.test.mjs",
|
|
102
|
+
"scripts/cm-notify.mjs",
|
|
103
|
+
"scripts/cm-notify-command.test.mjs",
|
|
104
|
+
"runtime/js/notify-send.mjs",
|
|
101
105
|
"scripts/cm-workflow-config.mjs",
|
|
102
106
|
"scripts/cm-workflow-config.test.mjs",
|
|
103
107
|
"scripts/cm_workflow_config.py",
|
package/skills/cm-ai/SKILL.md
CHANGED
|
@@ -29,9 +29,11 @@ $cm-ai ~/projects/specs 前端~/code/fe 后端~/code/api
|
|
|
29
29
|
|
|
30
30
|
先按下列顺序选择执行方式;用户无需额外说“使用 JS workflow”。路由选择不替代规格审批或实际调用授权。
|
|
31
31
|
|
|
32
|
-
1. **恢复已有运行**:先读取原运行记录。已有 JS host/batch 沿原身份与配置恢复;已配置 QA 因命令配置错误卡住时,按 `references/js-host.md` 的“QA 配置修订”显式授权,不重开已完成任务。
|
|
32
|
+
1. **恢复已有运行**:先读取原运行记录。已有 JS host/batch 沿原身份与配置恢复;已配置 QA 因命令配置错误卡住时,按 `references/js-host-qa-recovery.md` 的“QA 配置修订”显式授权,不重开已完成任务。
|
|
33
33
|
已确认的旧兼容任务沿原流程续接,不因升级迁移状态。记录缺失、冲突或无法确定归属时只读核对,不能猜测或另开运行绕过历史。
|
|
34
|
-
2.
|
|
34
|
+
2. **新任务**:默认只读取 `references/js-host.md`(正常路径),由共享 JS 入口驱动阶段;当前会话只执行其工具请求。
|
|
35
|
+
启动条件命中外部模型配置、bootstrap 规范任务、多代码根或同仓 specs 受保护执行/文本提案模式时,启动前先读 `references/js-host-modes.md` 对应一节(见 js-host.md“启动前”第 6 项)。
|
|
36
|
+
运行停在阻断、unknown、待审失败或需异常恢复时,才按 js-host.md“文档分工与停机路由”表读取 `js-host-recovery.md` 或 `js-host-qa-recovery.md` 的对应一节。
|
|
35
37
|
只有用户明确选择旧兼容流程、且确认不是已有 JS 运行时,才进入下文兼容执行步骤;不新增 CLI 参数。
|
|
36
38
|
3. **JS 准入失败或不支持**:报告具体宿主、Node 版本、目录、配置或业务能力缺口并停止依赖该能力的执行;
|
|
37
39
|
不静默回退兼容流程,不改 runId、runtime 或手工完成路径规避阻断。原生 Windows 尚不支持,Linux 支持声明不等于实机验收。
|
|
@@ -41,7 +43,7 @@ JS 路由中的状态、日志、handoff、Review 凭证与任务勾选由原 JS
|
|
|
41
43
|
|
|
42
44
|
### N1–N8 业务概览与兼容执行步骤
|
|
43
45
|
|
|
44
|
-
下列流程图说明共同业务顺序;JS 的执行操作以 `references/js-host.md`
|
|
46
|
+
下列流程图说明共同业务顺序;JS 的执行操作以 `references/js-host.md` 及其路由到的恢复/条件文档为准。
|
|
45
47
|
仅已选定兼容流程时,按下文及 `references/` 节点执行手工步骤;入口更新不代表已安装副本或全阶段实机验收。
|
|
46
48
|
|
|
47
49
|
```text
|
|
@@ -109,6 +111,8 @@ detail 用一句大白话;不直接拼 JSON,避免跨会话格式漂移。
|
|
|
109
111
|
测试/审查/外部回答只写专项凭证,不灌主日志。长步骤和临时资源严格按
|
|
110
112
|
`runtime/logging.md` 配对,禁止用后台心跳制造虚假活跃。
|
|
111
113
|
|
|
114
|
+
**会话边界(省 token):** 单任务运行以 `run_done` 收尾、准备开始下一个单任务时,建议用户在**新会话**里开始(新会话作为新运行的 `--host-context`),以减少历史上下文的重复读入。批次不在此列:批次运行器在同一次 `advance` 里交接下一任务,批次宿主必须沿用原 `hostContext`,批次内部照常继续,只在整个批次结束后给出这条建议。在一个真实项目的样本(aihot-dev 两周 8417 次调用)中,一个长会话占加权用量 89%,每次调用重读约 48 万 token 历史;这是单一样本的观测,用于说明换会话的潜在收益。单任务运行中途换会话按 `--original-host-context` 规则接手。不得为换会话改写记录、跳过审查或重置授权。
|
|
115
|
+
|
|
112
116
|
**角色路由投影:** N1 用代码项目根读取有效配置;N3 每个实现任务解析 `coder`,N3
|
|
113
117
|
任务检查解析 `tester`,N4 解析 `reviewer`,并在 N6 QA 解析 `tester`。使用:
|
|
114
118
|
|
|
@@ -12,9 +12,9 @@ commands/logic 及 API、数据库回归;`web-frontend` 优先 commands/browse
|
|
|
12
12
|
类型;`test-cases.json` 中 blocking case 即使类型未列出也必须执行,环境不可用则
|
|
13
13
|
`BLOCKED`,不得把配置当作跳过已审批验收的许可。
|
|
14
14
|
|
|
15
|
-
feature 未完成时,只执行 taskIds 全部已完成且未 dropped 的用例,其余记入 `deferred_cases`,延后到 feature 收尾;延后用例不计入执行数量,也不派发宿主请求。feature 中途无可执行项时产生一条显式 `BLOCKED` 行,id 为 `no-applicable-cases`,不得以零测试宣称通过。
|
|
15
|
+
taskIds 全部是 `[DROPPED]` 任务的用例(blocking 与否)不适用:不执行、不派发宿主请求、不计数,记入 `dropped_task_cases` 与报告 `not_applicable`(`NOT_APPLICABLE`)。feature 未完成时,只执行 taskIds 全部已完成且未 dropped 的用例,其余记入 `deferred_cases`,延后到 feature 收尾;延后用例不计入执行数量,也不派发宿主请求。feature 中途无可执行项时产生一条显式 `BLOCKED` 行,id 为 `no-applicable-cases`,不得以零测试宣称通过。
|
|
16
16
|
|
|
17
|
-
最新已 complete 的 QA 若为纯宿主/环境证据 BLOCKED、failed=0 且 qaRound<3,可按 [JS
|
|
17
|
+
最新已 complete 的 QA 若为纯宿主/环境证据 BLOCKED、failed=0 且 qaRound<3,可按 [JS 宿主 QA 恢复](js-host-qa-recovery.md#已-complete-的宿主证据或环境阻断) 显式 `--rerun-blocked-qa --allow-qa` 恢复,以 superseded 关联下一轮全量重跑;产品 FAIL、commands 阻断与未 complete 不适用,完成后同步 N6 状态镜像。
|
|
18
18
|
|
|
19
19
|
## 评分(1-5 分,总分 ≥ 8 触发)
|
|
20
20
|
|