@hugesb/dsh-stream-agent 0.0.0-stage → 0.3.2
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/CHANGELOG.md +622 -0
- package/CONTRIBUTING.md +72 -0
- package/LICENSE +202 -0
- package/README.en.md +349 -0
- package/README.md +300 -3
- package/cordis.patch.yml +77 -0
- package/dist/entry/action-gate.js +206 -0
- package/dist/entry/adapter-kit.js +192 -0
- package/dist/entry/context-render.js +119 -0
- package/dist/entry/control-bar-cli.js +289 -0
- package/dist/entry/control-bar.js +123 -0
- package/dist/entry/control-session.js +115 -0
- package/dist/entry/control-state.js +125 -0
- package/dist/entry/cordis-compat.js +95 -0
- package/dist/entry/eval-probe.js +112 -0
- package/dist/entry/intent-log.js +121 -0
- package/dist/entry/json-persistence.js +306 -0
- package/dist/entry/loop.js +4163 -0
- package/dist/entry/mock-provider.js +549 -0
- package/dist/entry/obs-broker.js +178 -0
- package/dist/entry/ptc-bridge.js +405 -0
- package/dist/entry/state-handle.js +217 -0
- package/dist/entry/takeover-registry.js +60 -0
- package/dist/kernel/control-bar.js +559 -0
- package/dist/kernel/final.js +62 -0
- package/dist/kernel/gate.js +464 -0
- package/dist/kernel/modes.js +253 -0
- package/dist/kernel/obs.js +517 -0
- package/dist/kernel/parser.js +1081 -0
- package/dist/kernel/render.js +466 -0
- package/dist/kernel/state.js +416 -0
- package/dist/version.js +9 -0
- package/docs/TEST-PROPOSITIONS.md +139 -0
- package/docs/adr-001-segment-boundary.md +61 -0
- package/docs/architecture.md +104 -0
- package/docs/desktop-ui-feasibility.md +277 -0
- package/docs/disabled-lines-audit.md +53 -0
- package/docs/limitations.md +181 -0
- package/docs/protocol.md +99 -0
- package/docs/v0.4.0-plan.md +50 -0
- package/eval/METRICS.md +128 -0
- package/eval/RESULTS.md +246 -0
- package/eval/tasks/fixtures/big-report.md +298 -0
- package/eval/tasks/fixtures/build-manifest.txt +5 -0
- package/eval/tasks/fixtures/deploy.yaml +4 -0
- package/eval/tasks/fixtures/error.log +200 -0
- package/eval/tasks/fixtures/features.yaml +4 -0
- package/eval/tasks/fixtures/pipeline.yaml +4 -0
- package/eval/tasks/fixtures/sales-q1.csv +61 -0
- package/eval/tasks/fixtures/settings.yaml +4 -0
- package/eval/tasks/fixtures/users-roster.json +304 -0
- package/eval/tasks/fixtures/users-scores.json +20 -0
- package/eval/tasks/gen-fixtures.cjs +80 -0
- package/eval/tasks/tasks.json +150 -0
- package/eval/tools/f6-kill-drill.cjs +176 -0
- package/eval/tools/f6-kill-drill.worker.cjs +131 -0
- package/eval/tools/gen-results.cjs +191 -0
- package/eval/tools/machine-test.cjs +200 -0
- package/eval/tools/manual-code-scenarios.md +59 -0
- package/eval/tools/replay-check.cjs +311 -0
- package/eval/tools/runner.cjs +219 -0
- package/eval/tools/session-log.cjs +106 -0
- package/eval/tools/summarize.cjs +185 -0
- package/eval/tools/t3-cells.cjs +173 -0
- package/package.json +73 -6
- package/patches/adapters-smoke.yml +41 -0
- package/patches/eval-account.yml +36 -0
- package/patches/eval-baseline.yml +33 -0
- package/patches/eval-stream-code-off.yml +46 -0
- package/patches/eval-stream-code-on.yml +47 -0
- package/patches/eval-stream.yml +54 -0
- package/patches/takeover.yml +42 -0
- package/patches/test-preset.yml +23 -0
- package/preset/stream/cordis.patch.yml +39 -0
- package/preset/stream/package.json +13 -0
- package/preset.yml +56 -0
- package/profiles/stream/README.md +66 -0
- package/profiles/stream/cordis.patch.yml +12 -0
- package/profiles/stream/package.json +17 -0
- package/profiles/stream-headless/package.json +16 -0
- package/scripts/build.mjs +103 -0
- package/scripts/check-consistency.mjs +146 -0
- package/scripts/gate.mjs +138 -0
- package/scripts/install-check.mjs +116 -0
- package/scripts/install-cycle.mjs +88 -0
- package/scripts/release.mjs +428 -0
- package/scripts/repair-session-logs.mjs +498 -0
- package/src/entry/action-gate.ts +206 -0
- package/src/entry/adapter-kit.ts +192 -0
- package/src/entry/context-render.ts +119 -0
- package/src/entry/control-bar-cli.ts +289 -0
- package/src/entry/control-bar.ts +123 -0
- package/src/entry/control-session.ts +115 -0
- package/src/entry/control-state.ts +125 -0
- package/src/entry/cordis-compat.ts +95 -0
- package/src/entry/eval-probe.ts +112 -0
- package/src/entry/intent-log.ts +121 -0
- package/src/entry/json-persistence.ts +306 -0
- package/src/entry/loop.ts +4163 -0
- package/src/entry/mock-provider.ts +549 -0
- package/src/entry/obs-broker.ts +178 -0
- package/src/entry/ptc-bridge.ts +405 -0
- package/src/entry/state-handle.ts +217 -0
- package/src/entry/takeover-registry.ts +60 -0
- package/src/kernel/control-bar.ts +559 -0
- package/src/kernel/final.ts +62 -0
- package/src/kernel/gate.ts +464 -0
- package/src/kernel/modes.ts +253 -0
- package/src/kernel/obs.ts +517 -0
- package/src/kernel/parser.ts +1081 -0
- package/src/kernel/render.ts +466 -0
- package/src/kernel/state.ts +416 -0
- package/src/version.ts +9 -0
- package/test/activation.test.ts +187 -0
- package/test/adapters.test.ts +676 -0
- package/test/control-bar.test.ts +486 -0
- package/test/desktop-ui.test.ts +359 -0
- package/test/eval-probe.test.ts +139 -0
- package/test/f5-plan.test.ts +221 -0
- package/test/f6-intent.test.ts +168 -0
- package/test/f7-segment.test.ts +190 -0
- package/test/fix-limits.test.ts +599 -0
- package/test/gate.test.ts +380 -0
- package/test/helpers/stub-loop.ts +240 -0
- package/test/host-conformance.test.ts +418 -0
- package/test/kernel/obs.test.ts +329 -0
- package/test/kernel/parser.test.ts +451 -0
- package/test/loop.test.ts +1393 -0
- package/test/mock-provider.test.ts +386 -0
- package/test/ptc-bridge.test.ts +356 -0
- package/test/render.test.ts +418 -0
- package/test/s2-install-check.test.ts +46 -0
- package/test/scenarios/3-file-basic.json +13 -0
- package/test/scenarios/5-file-basic.json +22 -0
- package/test/scenarios/burn-more.json +9 -0
- package/test/scenarios/degraded-final.json +10 -0
- package/test/scenarios/fault-abort-xml.json +11 -0
- package/test/scenarios/fault-malformed-command.json +12 -0
- package/test/scenarios/fault-midstream-abort.json +11 -0
- package/test/scenarios/fault-saturating-output.json +9 -0
- package/test/scenarios/fault-timeout.json +9 -0
- package/test/scenarios/peek-first.json +11 -0
- package/test/scenarios/peek-second.json +11 -0
- package/test/scenarios/peek-third.json +10 -0
- package/test/scenarios/recover-final.json +10 -0
- package/test/scenarios/reroute-first.json +17 -0
- package/test/scenarios/reroute-second.json +16 -0
- package/test/scenarios/starvation-final.json +10 -0
- package/test/scenarios/starvation-promote.json +11 -0
- package/test/scenarios/wait-tools-continue.json +10 -0
- package/test/scenarios/wait-tools-xml.json +22 -0
- package/test/scenarios/wait-tools.json +15 -0
- package/test/session-format.test.ts +234 -0
- package/test/state.test.ts +335 -0
- package/test/stream-v02.test.ts +264 -0
- package/test/v031-display.test.ts +333 -0
- package/test/v032-conflict.test.ts +191 -0
- package/test/v032-truncation.test.ts +291 -0
- package/ui/client.js +576 -0
- package/ui/index.js +282 -0
- package/ui/package.json +26 -0
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
import { test } from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import {
|
|
4
|
+
ObservationLedger,
|
|
5
|
+
InMemoryBlobSink,
|
|
6
|
+
classify,
|
|
7
|
+
classifyTool,
|
|
8
|
+
renderSkeleton,
|
|
9
|
+
skeletonToText,
|
|
10
|
+
UNCHANGED_TEXT,
|
|
11
|
+
type ObservationInput,
|
|
12
|
+
} from '../../src/kernel/obs.ts';
|
|
13
|
+
|
|
14
|
+
function obs(partial: Partial<ObservationInput>): ObservationInput {
|
|
15
|
+
return { tool: 'tf-read', ok: true, key: 'a.ts', content: 'x', ...partial };
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// ---------------------------------------------------------------------------
|
|
19
|
+
// 分类器四条规则(每条 ≥2 用例)
|
|
20
|
+
// ---------------------------------------------------------------------------
|
|
21
|
+
|
|
22
|
+
test('规则1: read/grep 族首次输出 = high', () => {
|
|
23
|
+
const ledger = new ObservationLedger();
|
|
24
|
+
const r1 = ledger.observe(obs({ tool: 'tf-read', key: 'src/a.ts', content: 'line1\nline2' }));
|
|
25
|
+
assert.equal(r1.info_gain, 'high');
|
|
26
|
+
const r2 = ledger.observe(obs({ tool: 'grep', key: 'TODO', content: 'a.ts:1: TODO x' }));
|
|
27
|
+
assert.equal(r2.info_gain, 'high');
|
|
28
|
+
assert.equal(r2.payload.kind, 'skeleton');
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test('规则1(变体): web/test 首次输出 = high', () => {
|
|
32
|
+
const ledger = new ObservationLedger();
|
|
33
|
+
assert.equal(ledger.observe(obs({ tool: 'web_fetch', key: 'http://x', content: '<html>hi</html>' })).info_gain, 'high');
|
|
34
|
+
assert.equal(ledger.observe(obs({ tool: 'test', key: 'npm test', content: 'all green' })).info_gain, 'high');
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test('规则2: write/append/mkdir 成功回执 = low(句柄化不注入)', () => {
|
|
38
|
+
const ledger = new ObservationLedger();
|
|
39
|
+
const w = ledger.observe(obs({ tool: 'tf-write', key: 'src/a.ts', content: 'written' }));
|
|
40
|
+
assert.equal(w.info_gain, 'low');
|
|
41
|
+
assert.equal(w.payload.kind, 'receipt');
|
|
42
|
+
const m = ledger.observe(obs({ tool: 'mkdir', key: 'src/lib', content: 'ok' }));
|
|
43
|
+
assert.equal(m.info_gain, 'low');
|
|
44
|
+
const ap = ledger.observe(obs({ tool: 'append', key: 'log.txt', content: 'ok' }));
|
|
45
|
+
assert.equal(ap.info_gain, 'low');
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test('规则2(变体): bash 成功 = low(README §5:成功记 ledger),bash 失败见规则4', () => {
|
|
49
|
+
const ledger = new ObservationLedger();
|
|
50
|
+
assert.equal(ledger.observe(obs({ tool: 'tf-bash', key: 'npm test', content: 'ok' })).info_gain, 'low');
|
|
51
|
+
assert.equal(ledger.observe(obs({ tool: 'tf-bash', key: 'npm build', content: 'ok' })).info_gain, 'low');
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test('规则3: 重复观测(内容哈希未变)= none', () => {
|
|
55
|
+
const ledger = new ObservationLedger();
|
|
56
|
+
ledger.observe(obs({ tool: 'tf-read', key: 'a.ts', content: 'same' }));
|
|
57
|
+
const again = ledger.observe(obs({ tool: 'tf-read', key: 'a.ts', content: 'same' }));
|
|
58
|
+
assert.equal(again.info_gain, 'none');
|
|
59
|
+
assert.equal(again.reason, 'duplicate-observation');
|
|
60
|
+
const third = ledger.observe(obs({ tool: 'tf-read', key: 'a.ts', content: 'same' }));
|
|
61
|
+
assert.equal(third.info_gain, 'none');
|
|
62
|
+
assert.deepEqual(third.payload, { kind: 'unchanged', text: UNCHANGED_TEXT });
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test('规则3(变体): 空输出 = none,且句柄目录不更新', () => {
|
|
66
|
+
const ledger = new ObservationLedger();
|
|
67
|
+
const e = ledger.observe(obs({ tool: 'grep', key: 'nothing', content: '' }));
|
|
68
|
+
assert.equal(e.info_gain, 'none');
|
|
69
|
+
assert.equal(e.handle, null);
|
|
70
|
+
// 空输出不产生目录条目
|
|
71
|
+
assert.deepEqual(ledger.directoryDelta(0), { added: [], changed: [], stale: [] });
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test('规则4: 一切执行失败 = high(含 write/bash 失败)', () => {
|
|
75
|
+
const ledger = new ObservationLedger();
|
|
76
|
+
const f1 = ledger.observe(obs({ tool: 'tf-read', key: 'missing.ts', ok: false, content: 'ENOENT' }));
|
|
77
|
+
assert.equal(f1.info_gain, 'high');
|
|
78
|
+
const f2 = ledger.observe(obs({ tool: 'tf-bash', key: 'npm test', ok: false, content: 'exit 1' }));
|
|
79
|
+
assert.equal(f2.info_gain, 'high');
|
|
80
|
+
const f3 = ledger.observe(obs({ tool: 'tf-write', key: 'ro/a.ts', ok: false, content: 'EACCES' }));
|
|
81
|
+
assert.equal(f3.info_gain, 'high');
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
test('规则4(变体): 重复读但内容失败 = high;classify 纯函数可直接用', () => {
|
|
85
|
+
const ledger = new ObservationLedger();
|
|
86
|
+
ledger.observe(obs({ tool: 'tf-read', key: 'a.ts', content: 'v1' }));
|
|
87
|
+
const fail = ledger.observe(obs({ tool: 'tf-read', key: 'a.ts', ok: false, content: 'EIO' }));
|
|
88
|
+
assert.equal(fail.info_gain, 'high');
|
|
89
|
+
// 内容变化的新版本也是 high
|
|
90
|
+
const changed = ledger.observe(obs({ tool: 'tf-read', key: 'a.ts', content: 'v2' }));
|
|
91
|
+
assert.equal(changed.info_gain, 'high');
|
|
92
|
+
assert.equal(changed.reason, 'content-changed:read');
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
test('classify 纯函数:不依赖 ledger 状态即可判定', () => {
|
|
96
|
+
assert.equal(classify(obs({ content: 'x' })).info_gain, 'high'); // 无历史 → 首次
|
|
97
|
+
assert.equal(classify(obs({ ok: false, content: 'boom' })).info_gain, 'high');
|
|
98
|
+
assert.equal(classify(obs({ tool: 'tf-write', content: 'ok' })).info_gain, 'low');
|
|
99
|
+
assert.equal(classify(obs({ content: '' })).info_gain, 'none');
|
|
100
|
+
assert.equal(classify(obs({ tool: 'unknown_thing', content: 'x' })).info_gain, 'low');
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
test('classifyTool:tf-* 与常见工具名映射正确,未知归 other', () => {
|
|
104
|
+
assert.equal(classifyTool('tf-read'), 'read');
|
|
105
|
+
assert.equal(classifyTool('tf-write'), 'write');
|
|
106
|
+
assert.equal(classifyTool('tf-bash'), 'bash');
|
|
107
|
+
assert.equal(classifyTool('tf-peek'), 'read');
|
|
108
|
+
assert.equal(classifyTool('grep'), 'grep');
|
|
109
|
+
assert.equal(classifyTool('web_fetch'), 'web');
|
|
110
|
+
assert.equal(classifyTool('some_custom_tool'), 'other');
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
// ---------------------------------------------------------------------------
|
|
114
|
+
// 行骨架(O1)
|
|
115
|
+
// ---------------------------------------------------------------------------
|
|
116
|
+
|
|
117
|
+
function bigFile(): string {
|
|
118
|
+
const lines: string[] = [
|
|
119
|
+
"import { foo } from './foo';",
|
|
120
|
+
"const bar = require('bar');",
|
|
121
|
+
'',
|
|
122
|
+
'class Widget {',
|
|
123
|
+
' // padding comment with long body --------------------------------------------------------',
|
|
124
|
+
' render() { return 1; }',
|
|
125
|
+
'}',
|
|
126
|
+
'',
|
|
127
|
+
'function helper(a, b) {',
|
|
128
|
+
...Array.from({ length: 180 }, (_, i) => ` const pad${i} = '${'x'.repeat(80)}'; // body filler line ${i}`),
|
|
129
|
+
' return 0;',
|
|
130
|
+
'}',
|
|
131
|
+
'# Title',
|
|
132
|
+
'## Error handling',
|
|
133
|
+
'failed step detected here',
|
|
134
|
+
];
|
|
135
|
+
return lines.join('\n');
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
test('renderSkeleton:totalLines / nonEmptyRanges / keyLines 正确', () => {
|
|
139
|
+
const s = renderSkeleton('import x\n\nclass A {}\n\ndef f():\n pass\n# Heading\nerror: boom\n');
|
|
140
|
+
assert.equal(s.totalLines, 8); // 末尾换行产生的空行不计
|
|
141
|
+
assert.deepEqual(s.nonEmptyRanges, [[1, 1], [3, 3], [5, 8]]);
|
|
142
|
+
const tags = s.keyLines.map((k) => [k.line, k.tag]);
|
|
143
|
+
assert.deepEqual(tags, [[1, 'import'], [3, 'class'], [5, 'def'], [7, 'heading'], [8, 'error']]);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
test('行骨架注入 token < 原文 10%(O1 断言)', () => {
|
|
147
|
+
const text = bigFile();
|
|
148
|
+
const skeleton = renderSkeleton(text);
|
|
149
|
+
const injected = skeletonToText(skeleton);
|
|
150
|
+
assert.ok(text.length > 10000, `fixture too small: ${text.length}`);
|
|
151
|
+
assert.ok(
|
|
152
|
+
injected.length < text.length * 0.1,
|
|
153
|
+
`skeleton ${injected.length} chars vs raw ${text.length} chars`,
|
|
154
|
+
);
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
test('重复读:第二次注入("内容未变化"字面量)< 首次骨架 10%', () => {
|
|
158
|
+
const ledger = new ObservationLedger();
|
|
159
|
+
const text = bigFile();
|
|
160
|
+
const first = ledger.observe(obs({ key: 'big.ts', content: text }));
|
|
161
|
+
assert.equal(first.payload.kind, 'skeleton');
|
|
162
|
+
const second = ledger.observe(obs({ key: 'big.ts', content: text }));
|
|
163
|
+
assert.equal(second.payload.kind, 'unchanged');
|
|
164
|
+
assert.ok(
|
|
165
|
+
UNCHANGED_TEXT.length < (first.payload as { rendered: string }).rendered.length * 0.1,
|
|
166
|
+
);
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
// ---------------------------------------------------------------------------
|
|
170
|
+
// tf-peek(O2)
|
|
171
|
+
// ---------------------------------------------------------------------------
|
|
172
|
+
|
|
173
|
+
test('peek:按行段取回,1-based 闭区间', () => {
|
|
174
|
+
const ledger = new ObservationLedger();
|
|
175
|
+
const r = ledger.observe(obs({ key: 'a.ts', content: 'l1\nl2\nl3\nl4\nl5' }));
|
|
176
|
+
const handle = r.handle!;
|
|
177
|
+
assert.equal(ledger.peek(handle, 2, 4), 'l2\nl3\nl4');
|
|
178
|
+
assert.equal(ledger.peek(handle, 1, 1), 'l1');
|
|
179
|
+
assert.equal(ledger.peek(handle, 5, 5), 'l5');
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
test('peek:越界收敛;空段返回空串;未知句柄返回 null', () => {
|
|
183
|
+
const ledger = new ObservationLedger();
|
|
184
|
+
const r = ledger.observe(obs({ key: 'a.ts', content: 'l1\nl2\nl3' }));
|
|
185
|
+
const handle = r.handle!;
|
|
186
|
+
assert.equal(ledger.peek(handle, -5, 99), 'l1\nl2\nl3');
|
|
187
|
+
assert.equal(ledger.peek(handle, 7, 9), '');
|
|
188
|
+
assert.equal(ledger.peek('obs:read:nope@v1', 1, 2), null);
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
// ---------------------------------------------------------------------------
|
|
192
|
+
// 句柄目录差分(O3)
|
|
193
|
+
// ---------------------------------------------------------------------------
|
|
194
|
+
|
|
195
|
+
test('directoryDelta:added / changed / stale 三列表', () => {
|
|
196
|
+
const ledger = new ObservationLedger();
|
|
197
|
+
const r1 = ledger.observe(obs({ tool: 'tf-write', key: 'a.ts', content: 'v1' }));
|
|
198
|
+
const delta1 = ledger.directoryDelta(0);
|
|
199
|
+
assert.deepEqual(delta1.added.map((e) => e.key), ['a.ts']);
|
|
200
|
+
assert.deepEqual(delta1.changed, []);
|
|
201
|
+
assert.deepEqual(delta1.stale, []);
|
|
202
|
+
|
|
203
|
+
ledger.observe(obs({ tool: 'tf-read', key: 'b.ts', content: 'read v1' })); // b.ts 新增
|
|
204
|
+
ledger.observe(obs({ tool: 'tf-write', key: 'a.ts', content: 'v2' })); // a.ts 变更
|
|
205
|
+
const staleSeq = ledger.markStale('b.ts');
|
|
206
|
+
assert.notEqual(staleSeq, null);
|
|
207
|
+
assert.equal(ledger.markStale('no-such-key'), null); // 未知 key 返回 null
|
|
208
|
+
|
|
209
|
+
const delta = ledger.directoryDelta(r1.seq);
|
|
210
|
+
// 每个条目只按"当前状态"出现在一个列表里:b.ts 新增后又失效 → 记 stale
|
|
211
|
+
assert.deepEqual(delta.added.map((e) => e.key), []);
|
|
212
|
+
assert.deepEqual(delta.changed.map((e) => e.key), ['a.ts']);
|
|
213
|
+
assert.deepEqual(delta.stale.map((e) => e.key), ['b.ts']);
|
|
214
|
+
// 游标推进到 staleSeq 之后无新增/变更/失效
|
|
215
|
+
const empty = ledger.directoryDelta(staleSeq!);
|
|
216
|
+
assert.deepEqual(empty.added, []);
|
|
217
|
+
assert.deepEqual(empty.changed, []);
|
|
218
|
+
assert.deepEqual(empty.stale, []);
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
test('句柄版本语义:同 key 新内容版本递增,handle 形如 obs:<family>:<callId>@v<n>', () => {
|
|
222
|
+
const ledger = new ObservationLedger();
|
|
223
|
+
const r1 = ledger.observe(obs({ tool: 'tf-write', key: 'a.ts', callId: 'c1', content: 'v1' }));
|
|
224
|
+
const r2 = ledger.observe(obs({ tool: 'tf-write', key: 'a.ts', callId: 'c2', content: 'v2' }));
|
|
225
|
+
assert.equal(r1.handle, 'obs:write:c1@v1');
|
|
226
|
+
assert.equal(r2.handle, 'obs:write:c2@v2');
|
|
227
|
+
assert.notEqual(r1.handle, r2.handle);
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
// ---------------------------------------------------------------------------
|
|
231
|
+
// 饥饿防线(M2 关键防线)
|
|
232
|
+
// ---------------------------------------------------------------------------
|
|
233
|
+
|
|
234
|
+
test('饥饿防线:连续 5 步无 high 注入,第 5 步触发升级注入', () => {
|
|
235
|
+
const ledger = new ObservationLedger({ starvationThreshold: 5 });
|
|
236
|
+
const write = ledger.observe(obs({ tool: 'tf-write', key: 'out.ts', content: 'result\n'.repeat(20) }));
|
|
237
|
+
assert.equal(write.info_gain, 'low');
|
|
238
|
+
|
|
239
|
+
for (let step = 1; step <= 4; step++) {
|
|
240
|
+
const promoted = ledger.noteStep(false);
|
|
241
|
+
assert.equal(promoted, null, `step ${step} 不应触发`);
|
|
242
|
+
}
|
|
243
|
+
const fifth = ledger.noteStep(false);
|
|
244
|
+
assert.ok(fifth, '第 5 步必须触发升级注入');
|
|
245
|
+
assert.equal(fifth!.reason, 'starvation-promotion');
|
|
246
|
+
assert.equal(fifth!.info_gain, 'high');
|
|
247
|
+
assert.equal(fifth!.handle, write.handle);
|
|
248
|
+
assert.equal(fifth!.payload.kind, 'skeleton');
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
test('饥饿防线:有 high 注入的步清零计数;触发后重新计数', () => {
|
|
252
|
+
const ledger = new ObservationLedger({ starvationThreshold: 3 });
|
|
253
|
+
ledger.observe(obs({ tool: 'tf-write', key: 'a', content: 'x1' }));
|
|
254
|
+
ledger.noteStep(false);
|
|
255
|
+
ledger.noteStep(false);
|
|
256
|
+
ledger.noteStep(true); // high 注入 → 清零
|
|
257
|
+
assert.equal(ledger.noteStep(false), null);
|
|
258
|
+
assert.equal(ledger.noteStep(false), null);
|
|
259
|
+
const third = ledger.noteStep(false);
|
|
260
|
+
assert.ok(third, '清零后重新累计到阈值应再次触发');
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
test('饥饿防线:无 low 观测时不触发也不清零', () => {
|
|
264
|
+
const ledger = new ObservationLedger({ starvationThreshold: 2 });
|
|
265
|
+
assert.equal(ledger.noteStep(false), null);
|
|
266
|
+
assert.equal(ledger.noteStep(false), null); // 无 low 可升级 → 不触发
|
|
267
|
+
ledger.observe(obs({ tool: 'tf-write', key: 'a', content: 'x' }));
|
|
268
|
+
// drySteps 已是 2 ≥ 阈值,下一个 dry step 应立即升级
|
|
269
|
+
assert.ok(ledger.noteStep(false));
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
test('饥饿防线阈值可配置且校验非法值', () => {
|
|
273
|
+
assert.throws(() => new ObservationLedger({ starvationThreshold: 0 }), RangeError);
|
|
274
|
+
assert.throws(() => new ObservationLedger({ starvationThreshold: 1.5 }), RangeError);
|
|
275
|
+
const ledger = new ObservationLedger({ starvationThreshold: 1 });
|
|
276
|
+
ledger.observe(obs({ tool: 'tf-write', key: 'a', content: 'x' }));
|
|
277
|
+
assert.ok(ledger.noteStep(false)); // 阈值 1:一步即触发
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
// ---------------------------------------------------------------------------
|
|
281
|
+
// 计数器与 injected_ratio(08 号 METRICS.md 口径)
|
|
282
|
+
// ---------------------------------------------------------------------------
|
|
283
|
+
|
|
284
|
+
test('injected_ratio:模拟 10 次 write 任务,ratio ≤ 0.2', () => {
|
|
285
|
+
const ledger = new ObservationLedger(); // 默认阈值 5
|
|
286
|
+
for (let i = 1; i <= 10; i++) {
|
|
287
|
+
ledger.observe(obs({ tool: 'tf-write', key: `f${i}.ts`, content: `content ${i}\n`.repeat(10) }));
|
|
288
|
+
ledger.noteStep(false); // write 任务无 high 注入
|
|
289
|
+
}
|
|
290
|
+
const c = ledger.counters();
|
|
291
|
+
assert.equal(c.produced, 10);
|
|
292
|
+
assert.equal(c.injected, 2); // 第 5、10 步各升级一次
|
|
293
|
+
assert.equal(c.peeked, 0);
|
|
294
|
+
assert.equal(ledger.injectedRatio(), 0.2);
|
|
295
|
+
assert.ok(ledger.injectedRatio() <= 0.2);
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
test('injected_ratio:produced=0 时为 0;peek 计入 peeked 与 injected', () => {
|
|
299
|
+
const ledger = new ObservationLedger();
|
|
300
|
+
assert.equal(ledger.injectedRatio(), 0);
|
|
301
|
+
const r = ledger.observe(obs({ key: 'a.ts', content: 'l1\nl2' }));
|
|
302
|
+
ledger.peek(r.handle!, 1, 1);
|
|
303
|
+
const c = ledger.counters();
|
|
304
|
+
assert.equal(c.peeked, 1);
|
|
305
|
+
assert.equal(c.produced, 1);
|
|
306
|
+
assert.equal(c.injected, 2); // high 骨架 1 + peek 片段 1
|
|
307
|
+
assert.equal(ledger.injectedRatio(), 2);
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
test('reset:全部状态清零', () => {
|
|
311
|
+
const ledger = new ObservationLedger();
|
|
312
|
+
ledger.observe(obs({ key: 'a.ts', content: 'x' }));
|
|
313
|
+
ledger.noteStep(false);
|
|
314
|
+
ledger.reset();
|
|
315
|
+
assert.deepEqual(ledger.counters(), { injected: 0, produced: 0, peeked: 0 });
|
|
316
|
+
assert.deepEqual(ledger.records(), []);
|
|
317
|
+
assert.deepEqual(ledger.directoryDelta(0), { added: [], changed: [], stale: [] });
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
// ---------------------------------------------------------------------------
|
|
321
|
+
// BlobSink 注入(红线:IO 只经 sink 出口)
|
|
322
|
+
// ---------------------------------------------------------------------------
|
|
323
|
+
|
|
324
|
+
test('BlobSink:内容经注入的 sink 落盘,内核自身无 IO 副作用', () => {
|
|
325
|
+
const sink = new InMemoryBlobSink();
|
|
326
|
+
const ledger = new ObservationLedger({ sink });
|
|
327
|
+
const r = ledger.observe(obs({ key: 'a.ts', content: 'blob-content' }));
|
|
328
|
+
assert.equal(sink.get(r.handle!), 'blob-content');
|
|
329
|
+
});
|