dsh-lean-router 2.0.0 → 2.1.1
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/docs/adr/0001-token-ledger-durable-usage.md +11 -0
- package/docs/changelog.en.md +30 -1
- package/docs/changelog.md +30 -1
- package/lib/bands.js +4 -2
- package/lib/lean/output-slim.js +271 -0
- package/lib/token-ledger/index.js +147 -0
- package/lib/token-ledger/live.js +184 -0
- package/lib/toolface/executor.js +76 -0
- package/lib/toolface/rules.js +21 -24
- package/lib/toolface/transport.js +7 -9
- package/package.json +4 -2
- package/test/output-slim.test.mjs +212 -0
- package/test/router-bands.test.mjs +26 -0
- package/test/token-ledger-live.test.mjs +351 -0
- package/test/token-ledger.test.mjs +264 -0
- package/test/toolface-executor.test.mjs +95 -0
- package/test/toolface-rules.test.mjs +15 -0
- package/test/toolface-runtime.test.mjs +72 -0
- package/test/toolface-wiring.test.mjs +16 -0
|
@@ -205,3 +205,98 @@ test('a caller-provided native module builds the delegate through it', () => {
|
|
|
205
205
|
executor.describe([{ name: 'pwsh' }]);
|
|
206
206
|
assert.deepEqual(constructed, [ctx], 'one delegate instance is built on the provided module');
|
|
207
207
|
});
|
|
208
|
+
|
|
209
|
+
// ===== 输出压缩(conservative output compaction)=====
|
|
210
|
+
|
|
211
|
+
/** 40 行相同、每行 60 字符 = 2439 字符,超过默认 2000 阈值。 */
|
|
212
|
+
const SLIM_RULE = 'a'.repeat(60);
|
|
213
|
+
const LONG_OUTPUT = Array(40).fill(SLIM_RULE).join('\n');
|
|
214
|
+
|
|
215
|
+
/** 定义一个工具,可选地带上原 finalizeContent。 */
|
|
216
|
+
function definitionWith(finalizeContent) {
|
|
217
|
+
const definition = { name: 'pwsh', description: 'd', parameters: {} };
|
|
218
|
+
if (finalizeContent !== undefined) definition.finalizeContent = finalizeContent;
|
|
219
|
+
return definition;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** describe 一遍,让 executor 用包装后的定义替换掉原 finalizeContent。 */
|
|
223
|
+
function described(definition) {
|
|
224
|
+
const executor = createCodemodeExecutor(EXEC_CTX, options, { createDelegate: fakeDelegate });
|
|
225
|
+
assert.deepEqual(executor.describe([definition]).schemas.map((schema) => schema.name), ['pwsh'], '包装不改变请求面');
|
|
226
|
+
return definition;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
test('output compaction: the original finalizeContent still runs, its content is compacted, value untouched', () => {
|
|
230
|
+
const seen = [];
|
|
231
|
+
const definition = described(definitionWith(function (exec, result) {
|
|
232
|
+
seen.push({ callId: exec.callId, content: result.content });
|
|
233
|
+
return [{ type: 'text', text: LONG_OUTPUT }];
|
|
234
|
+
}));
|
|
235
|
+
const result = { isError: false, value: { ok: true }, content: [{ type: 'text', text: 'renderer' }] };
|
|
236
|
+
|
|
237
|
+
const content = definition.finalizeContent({ callId: 'c1' }, result);
|
|
238
|
+
|
|
239
|
+
assert.equal(seen.length, 1, '原实现被调用且只调用一次');
|
|
240
|
+
assert.equal(seen[0].callId, 'c1', 'exec 原样透传');
|
|
241
|
+
assert.equal(content.length, 1);
|
|
242
|
+
assert.ok(content[0].text.length < LONG_OUTPUT.length, 'content 被压缩');
|
|
243
|
+
assert.match(content[0].text, /\[\.\.\. 39 identical lines collapsed\]/);
|
|
244
|
+
assert.match(content[0].text, /chars saved \.\.\.\]/);
|
|
245
|
+
assert.deepEqual(result.value, { ok: true }, 'value 不被压缩器改写');
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
test('output compaction: a short content passes through as the very array the original returned', () => {
|
|
249
|
+
const original = [{ type: 'text', text: 'short output' }];
|
|
250
|
+
const definition = described(definitionWith(() => original));
|
|
251
|
+
const result = { isError: false, value: 1, content: [{ type: 'text', text: 'renderer' }] };
|
|
252
|
+
assert.equal(definition.finalizeContent({ callId: 'c1' }, result), original, '无变化时原样返回原实现的数组');
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
test('output compaction: an undefined original return keeps the renderer content and compacts that', () => {
|
|
256
|
+
const definition = described(definitionWith(() => undefined));
|
|
257
|
+
const result = { isError: false, value: 1, content: [{ type: 'text', text: LONG_OUTPUT }] };
|
|
258
|
+
const content = definition.finalizeContent({ callId: 'c1' }, result);
|
|
259
|
+
assert.ok(Array.isArray(content), 'undefined 表示「保留渲染器输出」,压缩后必须显式返回新 content');
|
|
260
|
+
assert.match(content[0].text, /\[\.\.\. 39 identical lines collapsed\]/);
|
|
261
|
+
assert.deepEqual(result.value, 1);
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
test('output compaction: a tool without finalizeContent is compacted too, and returns undefined when nothing changes', () => {
|
|
265
|
+
const definition = described(definitionWith(undefined));
|
|
266
|
+
const long = { isError: false, value: { n: 1 }, content: [{ type: 'text', text: LONG_OUTPUT }] };
|
|
267
|
+
const compacted = definition.finalizeContent({ callId: 'c1' }, long);
|
|
268
|
+
assert.equal(compacted.length, 1);
|
|
269
|
+
assert.match(compacted[0].text, /identical lines collapsed/);
|
|
270
|
+
assert.deepEqual(long.value, { n: 1 });
|
|
271
|
+
|
|
272
|
+
const short = { isError: false, value: { n: 1 }, content: [{ type: 'text', text: 'short' }] };
|
|
273
|
+
assert.equal(definition.finalizeContent({ callId: 'c1' }, short), undefined, '未变化时返回 undefined 保留原 content');
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
test('output compaction: an original finalizer that throws still throws (never swallowed)', () => {
|
|
277
|
+
const definition = described(definitionWith(() => { throw new Error('finalizer boom'); }));
|
|
278
|
+
assert.throws(() => definition.finalizeContent({ callId: 'c1' }, { content: [] }), /finalizer boom/);
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
test('output compaction: repeated describe() wraps once (idempotent)', () => {
|
|
282
|
+
let calls = 0;
|
|
283
|
+
const definition = definitionWith(() => { calls += 1; return [{ type: 'text', text: LONG_OUTPUT }]; });
|
|
284
|
+
const executor = createCodemodeExecutor(EXEC_CTX, options, { createDelegate: fakeDelegate });
|
|
285
|
+
executor.describe([definition]);
|
|
286
|
+
executor.describe([definition]);
|
|
287
|
+
const content = definition.finalizeContent({ callId: 'c1' }, { content: [] });
|
|
288
|
+
assert.equal(calls, 1, '原实现每次执行只调用一次');
|
|
289
|
+
assert.equal((content[0].text.match(/identical lines collapsed/g) ?? []).length, 1, 'marker 不叠加');
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
test('output compaction: non-text blocks and other block fields survive untouched', () => {
|
|
293
|
+
const blocks = [
|
|
294
|
+
{ type: 'image', source: { type: 'base64', data: 'AAAA' } },
|
|
295
|
+
{ type: 'text', text: LONG_OUTPUT, name: 'pwsh' },
|
|
296
|
+
];
|
|
297
|
+
const definition = described(definitionWith(() => blocks));
|
|
298
|
+
const content = definition.finalizeContent({ callId: 'c1' }, { content: [] });
|
|
299
|
+
assert.equal(content[0], blocks[0], '图片块原引用');
|
|
300
|
+
assert.equal(content[1].name, 'pwsh', 'text 块的其他字段保留');
|
|
301
|
+
assert.ok(content[1].text.length < LONG_OUTPUT.length);
|
|
302
|
+
});
|
|
@@ -148,6 +148,21 @@ test('buildCodemodeSectionText documents discovery without leaking hidden names'
|
|
|
148
148
|
assert.doesNotMatch(text, /mcp__fake__get_ticker/, 'no hidden tool’s own name leaks into the section');
|
|
149
149
|
});
|
|
150
150
|
|
|
151
|
+
test('the section keeps the model-critical contract after the prose trim', () => {
|
|
152
|
+
const text = buildCodemodeSectionText({
|
|
153
|
+
runToolName: 'code_run',
|
|
154
|
+
metaTools: DEFAULT_META_TOOLS,
|
|
155
|
+
hidden: { total: 3, namespaces: [{ namespace: 'fake', count: 3, kind: 'mcp' }] },
|
|
156
|
+
searchLimit: 8,
|
|
157
|
+
programFormat: 'function-body',
|
|
158
|
+
});
|
|
159
|
+
assert.match(text, /reaches the conversation/, 'the output rule stays advertised');
|
|
160
|
+
assert.match(text, /is rejected by the harness/, 'the model-direct denial stays advertised');
|
|
161
|
+
assert.match(text, /only callable from inside/, 'discovery tools stay program-only');
|
|
162
|
+
assert.match(text, /Never guess a masked tool's name or arguments/, 'the search-first rule stays advertised');
|
|
163
|
+
assert.match(text, /hits\[0\]\.parameters/, 'the parameter lookup step stays in the example');
|
|
164
|
+
});
|
|
165
|
+
|
|
151
166
|
test('buildCodemodeSectionText degrades cleanly with nothing hidden', () => {
|
|
152
167
|
const text = buildCodemodeSectionText({});
|
|
153
168
|
assert.match(text, /No masked tools are connected right now/);
|
|
@@ -238,6 +238,78 @@ test('the transport registers under code_run, never the reserved run_code', () =
|
|
|
238
238
|
assert.equal(normalizeConfig({ runToolName: 'run_code' }).runToolName, 'run_code', 'config is normalized; the registry itself rejects the reserved name at register time');
|
|
239
239
|
});
|
|
240
240
|
|
|
241
|
+
/**
|
|
242
|
+
* The machine-readable contract, read off the real schema *before* the prose
|
|
243
|
+
* trim: property names, required list, types, and the absence of enums. Prose
|
|
244
|
+
* is deliberately not frozen here — only what a caller's arguments must match.
|
|
245
|
+
*/
|
|
246
|
+
const SCHEMA_SHAPE = {
|
|
247
|
+
type: 'object',
|
|
248
|
+
additionalProperties: false,
|
|
249
|
+
required: ['code', 'description'],
|
|
250
|
+
properties: {
|
|
251
|
+
code: { type: 'string' },
|
|
252
|
+
description: { type: 'string' },
|
|
253
|
+
timeoutMs: { type: 'number' },
|
|
254
|
+
},
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
/** Project a schema onto its machine-readable shape, refusing any enum. */
|
|
258
|
+
function shapeOf(schema) {
|
|
259
|
+
const properties = {};
|
|
260
|
+
for (const [name, property] of Object.entries(schema.properties ?? {})) {
|
|
261
|
+
assert.equal(Object.hasOwn(property, 'enum'), false, `${name} carries no enum, before or after the trim`);
|
|
262
|
+
assert.deepEqual(Object.keys(property).sort(), ['description', 'type'], `${name} exposes exactly a type and a description`);
|
|
263
|
+
properties[name] = { type: property.type };
|
|
264
|
+
}
|
|
265
|
+
return {
|
|
266
|
+
type: schema.type,
|
|
267
|
+
additionalProperties: schema.additionalProperties,
|
|
268
|
+
required: [...(schema.required ?? [])],
|
|
269
|
+
properties,
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
test('the parameter schema contract is frozen: names, required, types, no enums', () => {
|
|
274
|
+
const full = parameterSchema({ programFormat: 'function-body', timeout: { defaultMs: 120000, maxMs: 600000 } });
|
|
275
|
+
assert.deepEqual(shapeOf(full), SCHEMA_SHAPE);
|
|
276
|
+
// The runtime's numbers are semantic defaults, not decoration.
|
|
277
|
+
assert.match(full.properties.timeoutMs.description, /Default 120000/);
|
|
278
|
+
assert.match(full.properties.timeoutMs.description, /capped at 600000/);
|
|
279
|
+
|
|
280
|
+
const bare = parameterSchema({ programFormat: 'function-body' });
|
|
281
|
+
assert.deepEqual(shapeOf(bare), {
|
|
282
|
+
...SCHEMA_SHAPE,
|
|
283
|
+
properties: { code: { type: 'string' }, description: { type: 'string' } },
|
|
284
|
+
}, 'no descriptor, no timeoutMs — and nothing else moves');
|
|
285
|
+
|
|
286
|
+
const repl = parameterSchema({ programFormat: 'repl' });
|
|
287
|
+
assert.deepEqual(Object.keys(repl.properties), ['code', 'description']);
|
|
288
|
+
assert.match(repl.properties.code.description, /REPL/);
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
test('the transport description keeps the constraints a program depends on', () => {
|
|
292
|
+
const { tool } = buildTransport();
|
|
293
|
+
const description = tool.description;
|
|
294
|
+
// Call convention, including the quoted form for names a dot cannot carry.
|
|
295
|
+
assert.match(description, /tools\.name\(args\)/);
|
|
296
|
+
assert.match(description, /tools\["[^"]+"\]\(args\)/, 'quoted access for names a dot cannot carry');
|
|
297
|
+
// Failure surface: the error class and the property that names the call.
|
|
298
|
+
assert.match(description, /ToolCallError/);
|
|
299
|
+
assert.match(description, /toolName/);
|
|
300
|
+
// Output rule: only returned or printed values leave the program.
|
|
301
|
+
assert.match(description, /program output/);
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
test('the transport definition keeps its registry-facing shape', () => {
|
|
305
|
+
const { tool } = buildTransport();
|
|
306
|
+
assert.equal(tool.name, 'code_run');
|
|
307
|
+
assert.equal(tool.isConcurrencySafe(), false, 'programs stay exclusive');
|
|
308
|
+
assert.equal(tool.output.schema.type, 'object');
|
|
309
|
+
assert.deepEqual(Object.keys(tool.parameters).sort(), ['additionalProperties', 'properties', 'required', 'type']);
|
|
310
|
+
assert.equal(tool.execute.length, 2);
|
|
311
|
+
});
|
|
312
|
+
|
|
241
313
|
test('parameterSchema advertises timeoutMs only when the runtime reports a descriptor', () => {
|
|
242
314
|
const plain = parameterSchema({ programFormat: 'function-body' });
|
|
243
315
|
assert.deepEqual(Object.keys(plain.properties).sort(), ['code', 'description']);
|
|
@@ -254,3 +254,19 @@ test('the built bundle would keep base then frag order (in-memory concat, no wri
|
|
|
254
254
|
assert.equal(topOps[0], '- insert:', 'the base preset row leads');
|
|
255
255
|
assert.ok(topOps.some(l => l === '- id: preset-lean-router'), 'the frag override follows as a sibling op');
|
|
256
256
|
});
|
|
257
|
+
|
|
258
|
+
test('output compaction is wired onto the visible code_run definition (content only, value untouched)', async () => {
|
|
259
|
+
const { describeFor } = await harness();
|
|
260
|
+
describeFor();
|
|
261
|
+
const transport = DEFINITIONS.get('code_run');
|
|
262
|
+
assert.equal(typeof transport.finalizeContent, 'function', 'code_run 的可见定义获得了 finalizeContent 包装');
|
|
263
|
+
|
|
264
|
+
const long = Array(40).fill('a'.repeat(60)).join('\n');
|
|
265
|
+
const value = { logs: ['same line', 'same line', 'same line'], result: 1 };
|
|
266
|
+
const result = { isError: false, value, content: [{ type: 'text', text: long }] };
|
|
267
|
+
const content = transport.finalizeContent({ callId: 'c1' }, result);
|
|
268
|
+
|
|
269
|
+
assert.match(content[0].text, /\[\.\.\. 39 identical lines collapsed\]/, 'code_run 的输出被保守压缩');
|
|
270
|
+
assert.equal(content[0].text.length < long.length, true);
|
|
271
|
+
assert.equal(result.value, value, 'value 原引用未动,程序内子调用 settled.value 不受影响');
|
|
272
|
+
});
|