@danceiny/gotry 0.0.1-rc.11 → 0.0.1-rc.13
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 +111 -146
- package/cordis.gotry-patch.yml +14 -7
- package/dist/capabilities/flyai.js +135 -30
- package/dist/capabilities/hbcli.js +14 -2
- package/dist/capabilities/session/benchmark.js +252 -0
- package/dist/capabilities/session/transport.js +25 -10
- package/dist/capabilities/session-consent.js +82 -0
- package/dist/capabilities/session-login.js +119 -0
- package/dist/capabilities/session-search.js +7 -4
- package/dist/capabilities/weather.js +72 -17
- package/dist/scripts/agent-reach-wrapper-tests.js +4 -0
- package/dist/scripts/async-collect.js +33 -5
- package/dist/scripts/hbcli-tests.js +25 -4
- package/dist/scripts/ledger-tests.js +134 -4
- package/dist/scripts/memory-value-report.js +379 -0
- package/dist/scripts/product-metrics.js +569 -0
- package/dist/scripts/session-benchmark.js +261 -0
- package/dist/scripts/session-login.js +28 -0
- package/dist/scripts/session-tests.js +262 -23
- package/dist/scripts/smoke.js +112 -14
- package/dist/scripts/weather-tests.js +8 -1
- package/dist/src/index.js +124 -5
- package/dist/src/loop.js +65 -14
- package/dist/src/state-ledger.js +30 -4
- package/package.json +4 -2
- package/ts/capabilities/flyai.ts +177 -22
- package/ts/capabilities/hbcli.ts +16 -4
- package/ts/capabilities/session/benchmark.ts +273 -0
- package/ts/capabilities/session/transport.ts +41 -9
- package/ts/capabilities/session-consent.ts +127 -0
- package/ts/capabilities/session-login.ts +146 -0
- package/ts/capabilities/session-search.ts +13 -5
- package/ts/capabilities/weather.ts +84 -19
- package/ts/scripts/async-collect.ts +48 -7
- package/ts/src/index.ts +107 -11
- package/ts/src/loop.ts +85 -12
- package/ts/src/state-ledger.ts +48 -4
|
@@ -1,7 +1,30 @@
|
|
|
1
1
|
import { appendFileSync } from 'node:fs';
|
|
2
|
-
import { collectDeepPlanning, loadAsyncTicket, makeJournaledSolvePort, settleAsyncTicket } from '../src/loop.js';
|
|
2
|
+
import { ASYNC_TERMINAL_SCHEMA, collectDeepPlanning, loadAsyncTicket, makeJournaledSolvePort, settleAsyncTicket } from '../src/loop.js';
|
|
3
3
|
import { solveUnified } from '../src/unified.js';
|
|
4
4
|
import { openLedgerIfExists } from '../src/state-ledger.js';
|
|
5
|
+
function isTerminalOutcome(value) {
|
|
6
|
+
return value?.['schema'] === ASYNC_TERMINAL_SCHEMA && (value['status'] === 'succeeded' || value['status'] === 'failed') && typeof value['passed'] === 'number' && value['total'] === 4 && Array.isArray(value['failed_checks']);
|
|
7
|
+
}
|
|
8
|
+
function legacyTerminalOutcome(ticket, status, deliverable) {
|
|
9
|
+
const succeeded = status === 'settled' && deliverable.includes('不失望四条:4/4 ✅');
|
|
10
|
+
return {
|
|
11
|
+
schema: ASYNC_TERMINAL_SCHEMA,
|
|
12
|
+
ticket_id: ticket,
|
|
13
|
+
status: succeeded ? 'succeeded' : 'failed',
|
|
14
|
+
passed: succeeded ? 4 : 0,
|
|
15
|
+
total: 4,
|
|
16
|
+
checks: {
|
|
17
|
+
legacy_human_marker_only: succeeded
|
|
18
|
+
},
|
|
19
|
+
failed_checks: succeeded ? [] : [
|
|
20
|
+
'legacy_unstructured_terminal'
|
|
21
|
+
]
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
function emitTerminal(outcome) {
|
|
25
|
+
console.log(JSON.stringify(outcome));
|
|
26
|
+
process.exit(outcome.status === 'succeeded' ? 0 : 2);
|
|
27
|
+
}
|
|
5
28
|
const ticketId = process.argv[2];
|
|
6
29
|
const stateRoot = process.argv[3] ?? '.';
|
|
7
30
|
if (!ticketId) {
|
|
@@ -15,9 +38,12 @@ if (!loaded) {
|
|
|
15
38
|
}
|
|
16
39
|
const ledger = openLedgerIfExists(stateRoot);
|
|
17
40
|
const run = ledger?.getWorkflowRun(ticketId);
|
|
18
|
-
if (run?.status === 'settled' && run.deliverable) {
|
|
41
|
+
if ((run?.status === 'settled' || run?.status === 'failed') && run.deliverable) {
|
|
42
|
+
const storedOutcome = ledger?.getWorkflowTerminalOutcome(ticketId) ?? null;
|
|
43
|
+
const outcome = isTerminalOutcome(storedOutcome) ? storedOutcome : legacyTerminalOutcome(ticketId, run.status, run.deliverable);
|
|
44
|
+
await settleAsyncTicket(ticketId, run.deliverable, stateRoot, outcome);
|
|
19
45
|
console.log(`工单 ${ticketId} 已交付(账本终态,复诵不重算):\n\n${run.deliverable}`);
|
|
20
|
-
|
|
46
|
+
emitTerminal(outcome);
|
|
21
47
|
}
|
|
22
48
|
if (!ledger) {
|
|
23
49
|
console.error(`工单 ${ticketId}:stateRoot(${stateRoot})无账本——持久化先于回收,不应发生`);
|
|
@@ -29,9 +55,11 @@ const solve = makeJournaledSolvePort(ledger, ticketId, solveUnified, {
|
|
|
29
55
|
if (f) appendFileSync(f, 'solve\n');
|
|
30
56
|
}
|
|
31
57
|
});
|
|
32
|
-
const { reply } = await collectDeepPlanning(loaded.state, loaded.ticket, solve);
|
|
33
|
-
const out = await settleAsyncTicket(ticketId, reply, stateRoot);
|
|
58
|
+
const { reply, outcome } = await collectDeepPlanning(loaded.state, loaded.ticket, solve);
|
|
59
|
+
const out = await settleAsyncTicket(ticketId, reply, stateRoot, outcome);
|
|
34
60
|
console.log(`交付物已落盘:${out}\n\n${reply}`);
|
|
61
|
+
console.log(JSON.stringify(outcome));
|
|
62
|
+
process.exitCode = outcome.status === 'succeeded' ? 0 : 2;
|
|
35
63
|
|
|
36
64
|
|
|
37
65
|
//# sourceURL=/Users/bytedance/work/gotry/ts/scripts/async-collect.ts
|
|
@@ -62,7 +62,12 @@ await writeFile(fallback, JSON.stringify({
|
|
|
62
62
|
meta: 'fake',
|
|
63
63
|
stays: [
|
|
64
64
|
{
|
|
65
|
-
id: 's1'
|
|
65
|
+
id: 's1',
|
|
66
|
+
note: '普吉岛 workation 两周(13 晚)'
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
id: 's2',
|
|
70
|
+
note: '曼谷周末(2 晚)'
|
|
66
71
|
}
|
|
67
72
|
]
|
|
68
73
|
}));
|
|
@@ -83,19 +88,35 @@ await writeFile(fallback, JSON.stringify({
|
|
|
83
88
|
}
|
|
84
89
|
console.log('5. v0.3.0 旗标(--destination-name/--room-occupancies)对齐 OK');
|
|
85
90
|
}const r4 = await searchHotels({
|
|
86
|
-
destination: '
|
|
91
|
+
destination: '普吉岛'
|
|
87
92
|
}, {
|
|
88
93
|
hbcliBin: failBin2,
|
|
89
94
|
fallbackPath: fallback
|
|
90
95
|
});
|
|
91
96
|
assert.equal(r4.via, 'hbcli-error', 'searchHotels: fails to hbcli → fallback');
|
|
92
97
|
assert.equal(r4.summary.includes('降级到静态包'), true, 'summary 指明降级');
|
|
93
|
-
assert.
|
|
98
|
+
assert.deepEqual(r4.hotels, {
|
|
99
|
+
stays: [
|
|
100
|
+
{
|
|
101
|
+
id: 's1',
|
|
102
|
+
note: '普吉岛 workation 两周(13 晚)'
|
|
103
|
+
}
|
|
104
|
+
]
|
|
105
|
+
}, 'hotels 只含目的地命中的住宿块');
|
|
106
|
+
assert.equal(r4.summary.includes('命中 1 个住宿块'), true, 'summary 指明命中块数');
|
|
107
|
+
const r5 = await searchHotels({
|
|
108
|
+
destination: '巴黎'
|
|
109
|
+
}, {
|
|
110
|
+
hbcliBin: failBin2,
|
|
111
|
+
fallbackPath: fallback
|
|
112
|
+
});
|
|
113
|
+
assert.equal(r5.hotels ?? null, null, '无目的地命中时 hotels 为 null(不整包倾倒)');
|
|
114
|
+
assert.match(r5.summary, /无「巴黎」住宿数据/, 'summary 明示静态包无该目的地');
|
|
94
115
|
await rm(tmp, {
|
|
95
116
|
recursive: true,
|
|
96
117
|
force: true
|
|
97
118
|
});
|
|
98
|
-
console.log('HBCLI TESTS:
|
|
119
|
+
console.log('HBCLI TESTS: 6/6 OK (happy / error / no-binary / fallback-filter / fallback-no-match / v0.3.0 旗标回归)');
|
|
99
120
|
|
|
100
121
|
|
|
101
122
|
//# sourceURL=/Users/bytedance/work/gotry/ts/scripts/hbcli-tests.ts
|
|
@@ -3,7 +3,9 @@ import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync, mkdirSync
|
|
|
3
3
|
import { tmpdir } from 'node:os';
|
|
4
4
|
import { join } from 'node:path';
|
|
5
5
|
import { spawnSync } from 'node:child_process';
|
|
6
|
+
import { persistAsyncTicket } from '../src/loop.js';
|
|
6
7
|
import { ensureLedger, openLedgerIfExists } from '../src/state-ledger.js';
|
|
8
|
+
import { parseFlightPackToSpec } from '../src/unified.js';
|
|
7
9
|
let pass = 0;
|
|
8
10
|
let fail = 0;
|
|
9
11
|
function assert(cond, msg) {
|
|
@@ -15,6 +17,17 @@ function assert(cond, msg) {
|
|
|
15
17
|
console.error(` FAIL - ${msg}`);
|
|
16
18
|
}
|
|
17
19
|
}
|
|
20
|
+
function terminalOutcomeOf(stdout) {
|
|
21
|
+
const line = stdout.trim().split('\n').filter(Boolean).at(-1);
|
|
22
|
+
if (!line) return null;
|
|
23
|
+
try {
|
|
24
|
+
const value = JSON.parse(line);
|
|
25
|
+
if (value.schema !== 'gotry_async_terminal.v1' || typeof value.ticket_id !== 'string' || value.status !== 'succeeded' && value.status !== 'failed' || typeof value.passed !== 'number' || typeof value.total !== 'number' || typeof value.checks !== 'object' || !Array.isArray(value.failed_checks)) return null;
|
|
26
|
+
return value;
|
|
27
|
+
} catch {
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
18
31
|
const root = mkdtempSync(join(tmpdir(), 'gotry-ledger-'));
|
|
19
32
|
const ledger = ensureLedger(root);
|
|
20
33
|
const r1 = ledger.appendMotivationPatch({
|
|
@@ -276,11 +289,16 @@ const resume = spawnSync('npx', [
|
|
|
276
289
|
GOTRY_SOLVE_COUNT_FILE: countFile
|
|
277
290
|
}
|
|
278
291
|
});
|
|
279
|
-
|
|
292
|
+
const failedOutcome = terminalOutcomeOf(resume.stdout ?? '');
|
|
293
|
+
assert(resume.status === 2, `非4/4 恢复进程 exit 2(实际 ${resume.status}:${(resume.stderr ?? '').slice(0, 300)})`);
|
|
294
|
+
assert(failedOutcome?.status === 'failed' && failedOutcome.ticket_id === 'dp-crash1' && failedOutcome.passed < failedOutcome.total && failedOutcome.failed_checks.length > 0, '非4/4 输出 gotry_async_terminal.v1/failed 与失败项');
|
|
280
295
|
const count2 = readFileSync(countFile, 'utf-8').split('\n').filter(Boolean).length;
|
|
281
296
|
assert(count2 === 1, '恢复时 done 步骤零重算(exactly-once:求解计数仍 1,不重复花钱)');
|
|
282
|
-
|
|
283
|
-
assert(
|
|
297
|
+
const failedDeliverablePath = join(wroot, 'gotry-state', 'async', 'dp-crash1.deliverable.md');
|
|
298
|
+
assert(existsSync(failedDeliverablePath), '交付物视图已落盘(.deliverable.md)');
|
|
299
|
+
const failedLedger = openLedgerIfExists(wroot);
|
|
300
|
+
assert(failedLedger?.getWorkflowRun('dp-crash1')?.status === 'failed', '非4/4 账本终态 failed');
|
|
301
|
+
assert(JSON.stringify(failedLedger?.getWorkflowTerminalOutcome('dp-crash1')) === JSON.stringify(failedOutcome), '非4/4 结构化终态随 async.failed 事件落账');
|
|
284
302
|
const replay = spawnSync('npx', [
|
|
285
303
|
'tsx',
|
|
286
304
|
'scripts/async-collect.ts',
|
|
@@ -293,9 +311,117 @@ const replay = spawnSync('npx', [
|
|
|
293
311
|
GOTRY_SOLVE_COUNT_FILE: countFile
|
|
294
312
|
}
|
|
295
313
|
});
|
|
296
|
-
assert(replay.status ===
|
|
314
|
+
assert(replay.status === 2 && JSON.stringify(terminalOutcomeOf(replay.stdout ?? '')) === JSON.stringify(failedOutcome), 'failed 终态复诵保持 exit 2 与同一结构化结果');
|
|
297
315
|
const count3 = readFileSync(countFile, 'utf-8').split('\n').filter(Boolean).length;
|
|
298
316
|
assert(count3 === 1, '终态复诵零重算');
|
|
317
|
+
const failedDeliverable = readFileSync(failedDeliverablePath, 'utf-8');
|
|
318
|
+
rmSync(failedDeliverablePath);
|
|
319
|
+
const replayAfterMissingView = spawnSync('npx', [
|
|
320
|
+
'tsx',
|
|
321
|
+
'scripts/async-collect.ts',
|
|
322
|
+
'dp-crash1',
|
|
323
|
+
wroot
|
|
324
|
+
], {
|
|
325
|
+
encoding: 'utf-8',
|
|
326
|
+
env: {
|
|
327
|
+
...process.env,
|
|
328
|
+
GOTRY_SOLVE_COUNT_FILE: countFile
|
|
329
|
+
}
|
|
330
|
+
});
|
|
331
|
+
assert(replayAfterMissingView.status === 2 && JSON.stringify(terminalOutcomeOf(replayAfterMissingView.stdout ?? '')) === JSON.stringify(failedOutcome), 'failed 终态缺失视图时仍复诵同一 exit 2 与结构化结果');
|
|
332
|
+
assert(existsSync(failedDeliverablePath) && readFileSync(failedDeliverablePath, 'utf-8') === failedDeliverable, 'failed 终态复诵重建缺失 .deliverable.md 视图');
|
|
333
|
+
const countAfterViewRepair = readFileSync(countFile, 'utf-8').split('\n').filter(Boolean).length;
|
|
334
|
+
assert(countAfterViewRepair === 1, '终态视图修复零重算');
|
|
335
|
+
const tickRoot = mkdtempSync(join(tmpdir(), 'gotry-wf-tick-failed-'));
|
|
336
|
+
const tickTicket = {
|
|
337
|
+
id: 'dp-tick-failed1',
|
|
338
|
+
objective: 'state-cli 非4/4 机器终态回归',
|
|
339
|
+
requestedAt: new Date().toISOString(),
|
|
340
|
+
etaLabel: '秒级'
|
|
341
|
+
};
|
|
342
|
+
const tickState = {
|
|
343
|
+
calendar: {
|
|
344
|
+
year: 2026,
|
|
345
|
+
assertedWeekdays: {}
|
|
346
|
+
},
|
|
347
|
+
profile: {},
|
|
348
|
+
gates: [],
|
|
349
|
+
wishes: []
|
|
350
|
+
};
|
|
351
|
+
await persistAsyncTicket(tickTicket, tickState, tickRoot);
|
|
352
|
+
const tick = spawnSync('npx', [
|
|
353
|
+
'tsx',
|
|
354
|
+
'scripts/state-cli.ts',
|
|
355
|
+
'tick',
|
|
356
|
+
tickRoot
|
|
357
|
+
], {
|
|
358
|
+
encoding: 'utf-8'
|
|
359
|
+
});
|
|
360
|
+
assert(tick.status === 0, `state-cli tick 完成回收(实际 ${tick.status}:${(tick.stderr ?? '').slice(0, 200)})`);
|
|
361
|
+
const tickLedger = openLedgerIfExists(tickRoot);
|
|
362
|
+
const tickOutcome = tickLedger?.getWorkflowTerminalOutcome(tickTicket.id);
|
|
363
|
+
assert(tickLedger?.getWorkflowRun(tickTicket.id)?.status === 'failed' && tickOutcome?.schema === 'gotry_async_terminal.v1' && tickOutcome.status === 'failed' && tickOutcome.passed === 0 && tickOutcome.failed_checks.length === 4, 'state-cli tick 未透传 outcome 时仍从权威账本恢复非4/4 failed 终态');
|
|
364
|
+
const tickReplay = spawnSync('npx', [
|
|
365
|
+
'tsx',
|
|
366
|
+
'scripts/async-collect.ts',
|
|
367
|
+
tickTicket.id,
|
|
368
|
+
tickRoot
|
|
369
|
+
], {
|
|
370
|
+
encoding: 'utf-8'
|
|
371
|
+
});
|
|
372
|
+
assert(tickReplay.status === 2 && JSON.stringify(terminalOutcomeOf(tickReplay.stdout ?? '')) === JSON.stringify(tickOutcome), 'state-cli 写入的 failed 终态可由 collector 幂等复诵为同一 outcome/exit 2');
|
|
373
|
+
const successRoot = mkdtempSync(join(tmpdir(), 'gotry-wf-success-'));
|
|
374
|
+
const successCountFile = join(successRoot, 'solve-count.txt');
|
|
375
|
+
writeFileSync(successCountFile, '');
|
|
376
|
+
const successTicket = {
|
|
377
|
+
id: 'dp-success1',
|
|
378
|
+
objective: '4/4 机器终态回归',
|
|
379
|
+
requestedAt: new Date().toISOString(),
|
|
380
|
+
etaLabel: '秒级'
|
|
381
|
+
};
|
|
382
|
+
const successSpec = parseFlightPackToSpec(JSON.parse(readFileSync(join('..', 'data', 'flights_2026.json'), 'utf-8')));
|
|
383
|
+
successSpec.budgetCny = 9000;
|
|
384
|
+
const successState = {
|
|
385
|
+
calendar: {
|
|
386
|
+
year: 2026,
|
|
387
|
+
assertedWeekdays: {}
|
|
388
|
+
},
|
|
389
|
+
profile: {},
|
|
390
|
+
gates: [],
|
|
391
|
+
wishes: [],
|
|
392
|
+
spec: successSpec
|
|
393
|
+
};
|
|
394
|
+
await persistAsyncTicket(successTicket, successState, successRoot);
|
|
395
|
+
const success = spawnSync('npx', [
|
|
396
|
+
'tsx',
|
|
397
|
+
'scripts/async-collect.ts',
|
|
398
|
+
successTicket.id,
|
|
399
|
+
successRoot
|
|
400
|
+
], {
|
|
401
|
+
encoding: 'utf-8',
|
|
402
|
+
env: {
|
|
403
|
+
...process.env,
|
|
404
|
+
GOTRY_SOLVE_COUNT_FILE: successCountFile
|
|
405
|
+
}
|
|
406
|
+
});
|
|
407
|
+
const successOutcome = terminalOutcomeOf(success.stdout ?? '');
|
|
408
|
+
assert(success.status === 0 && successOutcome?.status === 'succeeded' && successOutcome.ticket_id === successTicket.id && successOutcome.passed === 4 && successOutcome.total === 4 && successOutcome.failed_checks.length === 0, `4/4 输出 gotry_async_terminal.v1/succeeded 且 exit 0(实际 ${success.status})`);
|
|
409
|
+
const successLedger = openLedgerIfExists(successRoot);
|
|
410
|
+
assert(successLedger?.getWorkflowRun(successTicket.id)?.status === 'settled' && successLedger.getWorkflowTerminalOutcome(successTicket.id)?.['status'] === 'succeeded', '4/4 账本终态 settled 且结构化结果随 async.settled 落账');
|
|
411
|
+
const successReplay = spawnSync('npx', [
|
|
412
|
+
'tsx',
|
|
413
|
+
'scripts/async-collect.ts',
|
|
414
|
+
successTicket.id,
|
|
415
|
+
successRoot
|
|
416
|
+
], {
|
|
417
|
+
encoding: 'utf-8',
|
|
418
|
+
env: {
|
|
419
|
+
...process.env,
|
|
420
|
+
GOTRY_SOLVE_COUNT_FILE: successCountFile
|
|
421
|
+
}
|
|
422
|
+
});
|
|
423
|
+
const successCount = readFileSync(successCountFile, 'utf-8').split('\n').filter(Boolean).length;
|
|
424
|
+
assert(successReplay.status === 0 && successCount === 1 && JSON.stringify(terminalOutcomeOf(successReplay.stdout ?? '')) === JSON.stringify(successOutcome), 'succeeded 终态复诵保持 exit 0、同一结构化结果且零重算');
|
|
299
425
|
const pw1 = ledger.requestPendingWrite({
|
|
300
426
|
idemKey: 'booking:demo-1',
|
|
301
427
|
seam: 'flight-order-confirm',
|
|
@@ -358,6 +484,10 @@ rmSync(wroot, {
|
|
|
358
484
|
recursive: true,
|
|
359
485
|
force: true
|
|
360
486
|
});
|
|
487
|
+
rmSync(successRoot, {
|
|
488
|
+
recursive: true,
|
|
489
|
+
force: true
|
|
490
|
+
});
|
|
361
491
|
console.log(`\nLEDGER TESTS: ${pass} ok, ${fail} fail`);
|
|
362
492
|
if (fail > 0) process.exit(1);
|
|
363
493
|
|
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
import { resolve } from 'node:path';
|
|
3
|
+
import { pathToFileURL } from 'node:url';
|
|
4
|
+
function isObject(value) {
|
|
5
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
6
|
+
}
|
|
7
|
+
function nonEmptyString(value) {
|
|
8
|
+
return typeof value === 'string' && value.trim().length > 0;
|
|
9
|
+
}
|
|
10
|
+
function finiteNumber(value) {
|
|
11
|
+
return typeof value === 'number' && Number.isFinite(value);
|
|
12
|
+
}
|
|
13
|
+
function round(value) {
|
|
14
|
+
return Math.round((value + Number.EPSILON) * 1_000_000) / 1_000_000;
|
|
15
|
+
}
|
|
16
|
+
function nearestRank(values, percentile) {
|
|
17
|
+
if (values.length === 0) return 0;
|
|
18
|
+
const sorted = [
|
|
19
|
+
...values
|
|
20
|
+
].sort((a, b)=>a - b);
|
|
21
|
+
const index = Math.max(0, Math.ceil(percentile * sorted.length) - 1);
|
|
22
|
+
return round(sorted[index]);
|
|
23
|
+
}
|
|
24
|
+
function parseTimestamp(value, path, errors) {
|
|
25
|
+
if (!nonEmptyString(value)) {
|
|
26
|
+
errors.push(`${path} must be a non-empty ISO timestamp`);
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
const parsed = Date.parse(value);
|
|
30
|
+
if (!Number.isFinite(parsed)) {
|
|
31
|
+
errors.push(`${path} is not a valid ISO timestamp`);
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
return parsed;
|
|
35
|
+
}
|
|
36
|
+
function scoreFlow(value, path, expectedEligibleIndex, allowedWaitCodes, errors) {
|
|
37
|
+
if (!isObject(value)) {
|
|
38
|
+
errors.push(`${path} must be an object`);
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
const flowId = value.flow_id;
|
|
42
|
+
if (!nonEmptyString(flowId)) errors.push(`${path}.flow_id must be non-empty`);
|
|
43
|
+
if (value.eligible !== true) errors.push(`${path}.eligible must be true`);
|
|
44
|
+
if (value.status !== 'completed') errors.push(`${path}.status must be completed`);
|
|
45
|
+
if (value.eligible_planning_index !== expectedEligibleIndex) {
|
|
46
|
+
errors.push(`${path}.eligible_planning_index must be ${expectedEligibleIndex}`);
|
|
47
|
+
}
|
|
48
|
+
const startedAtMs = parseTimestamp(value.started_at, `${path}.started_at`, errors);
|
|
49
|
+
const completedAtMs = parseTimestamp(value.completed_at, `${path}.completed_at`, errors);
|
|
50
|
+
if (startedAtMs === null || completedAtMs === null) return null;
|
|
51
|
+
if (completedAtMs <= startedAtMs) {
|
|
52
|
+
errors.push(`${path}.completed_at must be after started_at`);
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
const rawWaits = value.external_waits;
|
|
56
|
+
if (!Array.isArray(rawWaits)) {
|
|
57
|
+
errors.push(`${path}.external_waits must be an array`);
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
const waits = [];
|
|
61
|
+
for (const [index, rawWait] of rawWaits.entries()){
|
|
62
|
+
const waitPath = `${path}.external_waits[${index}]`;
|
|
63
|
+
if (!isObject(rawWait)) {
|
|
64
|
+
errors.push(`${waitPath} must be an object`);
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
const code = rawWait.code;
|
|
68
|
+
if (!nonEmptyString(code) || !allowedWaitCodes.has(code)) {
|
|
69
|
+
errors.push(`${waitPath}.code must be predeclared by measurement_policy`);
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
const waitStartedAtMs = parseTimestamp(rawWait.started_at, `${waitPath}.started_at`, errors);
|
|
73
|
+
const waitCompletedAtMs = parseTimestamp(rawWait.completed_at, `${waitPath}.completed_at`, errors);
|
|
74
|
+
if (waitStartedAtMs === null || waitCompletedAtMs === null) continue;
|
|
75
|
+
if (waitCompletedAtMs <= waitStartedAtMs) {
|
|
76
|
+
errors.push(`${waitPath}.completed_at must be after started_at`);
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
if (waitStartedAtMs < startedAtMs || waitCompletedAtMs > completedAtMs) {
|
|
80
|
+
errors.push(`${waitPath} must stay inside the planning flow`);
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
waits.push({
|
|
84
|
+
code,
|
|
85
|
+
startedAtMs: waitStartedAtMs,
|
|
86
|
+
completedAtMs: waitCompletedAtMs
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
waits.sort((a, b)=>a.startedAtMs - b.startedAtMs);
|
|
90
|
+
for(let index = 1; index < waits.length; index += 1){
|
|
91
|
+
if (waits[index].startedAtMs < waits[index - 1].completedAtMs) {
|
|
92
|
+
errors.push(`${path}.external_waits must not overlap`);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
const externalWaitMs = waits.reduce((total, wait)=>total + wait.completedAtMs - wait.startedAtMs, 0);
|
|
96
|
+
const activeSeconds = (completedAtMs - startedAtMs - externalWaitMs) / 1_000;
|
|
97
|
+
if (activeSeconds <= 0) errors.push(`${path} must have positive active planning duration`);
|
|
98
|
+
return nonEmptyString(flowId) ? {
|
|
99
|
+
activeSeconds: round(activeSeconds),
|
|
100
|
+
completedAtMs,
|
|
101
|
+
flowId,
|
|
102
|
+
startedAtMs
|
|
103
|
+
} : null;
|
|
104
|
+
}
|
|
105
|
+
function invalidReport(errors, schema = '', evidenceKind = '') {
|
|
106
|
+
return {
|
|
107
|
+
schema: 'memory_value_report.v1',
|
|
108
|
+
contract_valid: false,
|
|
109
|
+
errors,
|
|
110
|
+
source: {
|
|
111
|
+
fixture_schema: schema,
|
|
112
|
+
evidence_kind: evidenceKind,
|
|
113
|
+
quantile_method: 'nearest_rank',
|
|
114
|
+
active_duration_rule: 'wall_clock_minus_non_overlapping_predeclared_external_waits'
|
|
115
|
+
},
|
|
116
|
+
cohort: {
|
|
117
|
+
eligible_pair_count: 0,
|
|
118
|
+
first_active_seconds: {
|
|
119
|
+
p50: 0,
|
|
120
|
+
p75: 0
|
|
121
|
+
},
|
|
122
|
+
returning_active_seconds: {
|
|
123
|
+
p50: 0,
|
|
124
|
+
p75: 0
|
|
125
|
+
},
|
|
126
|
+
paired_reduction_ratio: {
|
|
127
|
+
p50: 0,
|
|
128
|
+
p75: 0
|
|
129
|
+
},
|
|
130
|
+
target_median_reduction_ratio: 0,
|
|
131
|
+
target_met: false,
|
|
132
|
+
minimum_pair_count_for_exit: 0,
|
|
133
|
+
sample_size_met: false
|
|
134
|
+
},
|
|
135
|
+
experience_reflux: {
|
|
136
|
+
recalled_experience_count: 0,
|
|
137
|
+
verified_experience_count: 0,
|
|
138
|
+
baseline: null,
|
|
139
|
+
baseline_available: false,
|
|
140
|
+
real_evidence: false
|
|
141
|
+
},
|
|
142
|
+
preference_assertions: {
|
|
143
|
+
total_count: 0,
|
|
144
|
+
traceable_count: 0,
|
|
145
|
+
traceable_ratio: 0,
|
|
146
|
+
hard_filter_violation_count: 0,
|
|
147
|
+
contract_met: false
|
|
148
|
+
},
|
|
149
|
+
p4: {
|
|
150
|
+
state: 'unknown',
|
|
151
|
+
trigger_observed: false,
|
|
152
|
+
contract_met: false
|
|
153
|
+
},
|
|
154
|
+
exit_evidence_eligible: false,
|
|
155
|
+
exit_ready: false
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
export function scoreMemoryValue(input) {
|
|
159
|
+
const errors = [];
|
|
160
|
+
if (!isObject(input)) return invalidReport([
|
|
161
|
+
'root must be an object'
|
|
162
|
+
]);
|
|
163
|
+
const schema = nonEmptyString(input.schema) ? input.schema : '';
|
|
164
|
+
const evidenceKind = nonEmptyString(input.evidence_kind) ? input.evidence_kind : '';
|
|
165
|
+
if (schema !== 'memory_value_fixture.v1') errors.push('schema must be memory_value_fixture.v1');
|
|
166
|
+
if (![
|
|
167
|
+
'synthetic_fixture',
|
|
168
|
+
'observed_private'
|
|
169
|
+
].includes(evidenceKind)) {
|
|
170
|
+
errors.push('evidence_kind must be synthetic_fixture or observed_private');
|
|
171
|
+
}
|
|
172
|
+
const policy = input.measurement_policy;
|
|
173
|
+
if (!isObject(policy)) return invalidReport([
|
|
174
|
+
...errors,
|
|
175
|
+
'measurement_policy must be an object'
|
|
176
|
+
], schema, evidenceKind);
|
|
177
|
+
if (policy.quantile_method !== 'nearest_rank') errors.push('measurement_policy.quantile_method must be nearest_rank');
|
|
178
|
+
const rawWaitCodes = policy.predeclared_external_wait_codes;
|
|
179
|
+
const allowedWaitCodes = new Set();
|
|
180
|
+
if (!Array.isArray(rawWaitCodes) || rawWaitCodes.length === 0) {
|
|
181
|
+
errors.push('measurement_policy.predeclared_external_wait_codes must be a non-empty array');
|
|
182
|
+
} else {
|
|
183
|
+
for (const code of rawWaitCodes){
|
|
184
|
+
if (nonEmptyString(code)) allowedWaitCodes.add(code);
|
|
185
|
+
else errors.push('measurement_policy.predeclared_external_wait_codes must contain non-empty strings');
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
const minimumPairCount = policy.minimum_pair_count_for_exit;
|
|
189
|
+
const targetReduction = policy.target_median_reduction_ratio;
|
|
190
|
+
if (!Number.isInteger(minimumPairCount) || minimumPairCount <= 0) {
|
|
191
|
+
errors.push('measurement_policy.minimum_pair_count_for_exit must be a positive integer');
|
|
192
|
+
}
|
|
193
|
+
if (!finiteNumber(targetReduction) || targetReduction < 0 || targetReduction > 1) {
|
|
194
|
+
errors.push('measurement_policy.target_median_reduction_ratio must be between 0 and 1');
|
|
195
|
+
}
|
|
196
|
+
const rawPairs = input.pairs;
|
|
197
|
+
if (!Array.isArray(rawPairs) || rawPairs.length === 0) {
|
|
198
|
+
errors.push('pairs must be a non-empty array');
|
|
199
|
+
}
|
|
200
|
+
const firstDurations = [];
|
|
201
|
+
const returningDurations = [];
|
|
202
|
+
const reductions = [];
|
|
203
|
+
const pairIds = new Set();
|
|
204
|
+
const subjectRefs = new Set();
|
|
205
|
+
const flowIds = new Set();
|
|
206
|
+
for (const [index, rawPair] of (Array.isArray(rawPairs) ? rawPairs : []).entries()){
|
|
207
|
+
const pairPath = `pairs[${index}]`;
|
|
208
|
+
if (!isObject(rawPair)) {
|
|
209
|
+
errors.push(`${pairPath} must be an object`);
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
if (!nonEmptyString(rawPair.pair_id)) errors.push(`${pairPath}.pair_id must be non-empty`);
|
|
213
|
+
else if (pairIds.has(rawPair.pair_id)) errors.push(`${pairPath}.pair_id must be unique`);
|
|
214
|
+
else pairIds.add(rawPair.pair_id);
|
|
215
|
+
if (!nonEmptyString(rawPair.subject_ref)) {
|
|
216
|
+
errors.push(`${pairPath}.subject_ref must be a non-empty pseudonymous reference`);
|
|
217
|
+
} else if (subjectRefs.has(rawPair.subject_ref)) {
|
|
218
|
+
errors.push(`${pairPath}.subject_ref must be unique across the paired cohort`);
|
|
219
|
+
} else {
|
|
220
|
+
subjectRefs.add(rawPair.subject_ref);
|
|
221
|
+
}
|
|
222
|
+
const first = scoreFlow(rawPair.first, `${pairPath}.first`, 1, allowedWaitCodes, errors);
|
|
223
|
+
const returning = scoreFlow(rawPair.returning, `${pairPath}.returning`, 2, allowedWaitCodes, errors);
|
|
224
|
+
for (const flow of [
|
|
225
|
+
first,
|
|
226
|
+
returning
|
|
227
|
+
]){
|
|
228
|
+
if (!flow) continue;
|
|
229
|
+
if (flowIds.has(flow.flowId)) errors.push(`${pairPath} flow_id values must be globally unique`);
|
|
230
|
+
flowIds.add(flow.flowId);
|
|
231
|
+
}
|
|
232
|
+
if (!first || !returning || first.activeSeconds <= 0 || returning.activeSeconds <= 0) continue;
|
|
233
|
+
if (returning.startedAtMs <= first.completedAtMs) {
|
|
234
|
+
errors.push(`${pairPath}.returning must start after the first completed flow`);
|
|
235
|
+
}
|
|
236
|
+
firstDurations.push(first.activeSeconds);
|
|
237
|
+
returningDurations.push(returning.activeSeconds);
|
|
238
|
+
reductions.push(round((first.activeSeconds - returning.activeSeconds) / first.activeSeconds));
|
|
239
|
+
}
|
|
240
|
+
const rawRefluxEvents = input.experience_reflux_events;
|
|
241
|
+
if (!Array.isArray(rawRefluxEvents)) errors.push('experience_reflux_events must be an array');
|
|
242
|
+
const recalled = new Set();
|
|
243
|
+
const verified = new Set();
|
|
244
|
+
for (const [index, rawEvent] of (Array.isArray(rawRefluxEvents) ? rawRefluxEvents : []).entries()){
|
|
245
|
+
const eventPath = `experience_reflux_events[${index}]`;
|
|
246
|
+
if (!isObject(rawEvent)) {
|
|
247
|
+
errors.push(`${eventPath} must be an object`);
|
|
248
|
+
continue;
|
|
249
|
+
}
|
|
250
|
+
if (!nonEmptyString(rawEvent.experience_id)) {
|
|
251
|
+
errors.push(`${eventPath}.experience_id must be non-empty`);
|
|
252
|
+
continue;
|
|
253
|
+
}
|
|
254
|
+
if (!nonEmptyString(rawEvent.evidence_ref)) errors.push(`${eventPath}.evidence_ref must be non-empty`);
|
|
255
|
+
if (rawEvent.kind === 'recalled') recalled.add(rawEvent.experience_id);
|
|
256
|
+
else if (rawEvent.kind === 'verified_outcome') verified.add(rawEvent.experience_id);
|
|
257
|
+
else errors.push(`${eventPath}.kind must be recalled or verified_outcome`);
|
|
258
|
+
}
|
|
259
|
+
for (const experienceId of verified){
|
|
260
|
+
if (!recalled.has(experienceId)) errors.push(`verified experience ${experienceId} must have a recalled event`);
|
|
261
|
+
}
|
|
262
|
+
const verifiedRecalledCount = [
|
|
263
|
+
...verified
|
|
264
|
+
].filter((id)=>recalled.has(id)).length;
|
|
265
|
+
const refluxBaseline = recalled.size > 0 ? round(verifiedRecalledCount / recalled.size) : null;
|
|
266
|
+
const rawAssertions = input.preference_assertions;
|
|
267
|
+
if (!Array.isArray(rawAssertions)) errors.push('preference_assertions must be an array');
|
|
268
|
+
let traceableCount = 0;
|
|
269
|
+
let hardFilterViolationCount = 0;
|
|
270
|
+
const assertions = Array.isArray(rawAssertions) ? rawAssertions : [];
|
|
271
|
+
for (const [index, rawAssertion] of assertions.entries()){
|
|
272
|
+
const assertionPath = `preference_assertions[${index}]`;
|
|
273
|
+
if (!isObject(rawAssertion)) {
|
|
274
|
+
errors.push(`${assertionPath} must be an object`);
|
|
275
|
+
continue;
|
|
276
|
+
}
|
|
277
|
+
if (!nonEmptyString(rawAssertion.assertion_id)) errors.push(`${assertionPath}.assertion_id must be non-empty`);
|
|
278
|
+
if (nonEmptyString(rawAssertion.evidence_ref)) traceableCount += 1;
|
|
279
|
+
if (rawAssertion.hard_filter === true || rawAssertion.consumer === 'hard_filter') hardFilterViolationCount += 1;
|
|
280
|
+
}
|
|
281
|
+
const traceableRatio = assertions.length > 0 ? round(traceableCount / assertions.length) : 0;
|
|
282
|
+
const assertionContractMet = assertions.length > 0 && traceableRatio === 1 && hardFilterViolationCount === 0;
|
|
283
|
+
const rawP4 = input.p4;
|
|
284
|
+
let p4State = 'unknown';
|
|
285
|
+
let p4TriggerObserved = false;
|
|
286
|
+
if (!isObject(rawP4) || !isObject(rawP4.triggers)) {
|
|
287
|
+
errors.push('p4.state and p4.triggers must be declared');
|
|
288
|
+
} else {
|
|
289
|
+
if (nonEmptyString(rawP4.state)) p4State = rawP4.state;
|
|
290
|
+
else errors.push('p4.state must be non-empty');
|
|
291
|
+
p4TriggerObserved = rawP4.triggers.real_usage === true || rawP4.triggers.multi_user === true;
|
|
292
|
+
}
|
|
293
|
+
const p4ContractMet = p4TriggerObserved || p4State === 'closed';
|
|
294
|
+
if (errors.length > 0) return invalidReport(errors, schema, evidenceKind);
|
|
295
|
+
const medianReduction = nearestRank(reductions, 0.5);
|
|
296
|
+
const numericMinimumPairCount = minimumPairCount;
|
|
297
|
+
const numericTargetReduction = targetReduction;
|
|
298
|
+
const sampleSizeMet = firstDurations.length >= numericMinimumPairCount;
|
|
299
|
+
const targetMet = medianReduction >= numericTargetReduction;
|
|
300
|
+
const exitEvidenceEligible = evidenceKind === 'observed_private';
|
|
301
|
+
const baselineAvailable = refluxBaseline !== null;
|
|
302
|
+
const exitReady = exitEvidenceEligible && sampleSizeMet && targetMet && baselineAvailable && assertionContractMet && p4ContractMet;
|
|
303
|
+
return {
|
|
304
|
+
schema: 'memory_value_report.v1',
|
|
305
|
+
contract_valid: true,
|
|
306
|
+
errors: [],
|
|
307
|
+
source: {
|
|
308
|
+
fixture_schema: schema,
|
|
309
|
+
evidence_kind: evidenceKind,
|
|
310
|
+
quantile_method: 'nearest_rank',
|
|
311
|
+
active_duration_rule: 'wall_clock_minus_non_overlapping_predeclared_external_waits'
|
|
312
|
+
},
|
|
313
|
+
cohort: {
|
|
314
|
+
eligible_pair_count: firstDurations.length,
|
|
315
|
+
first_active_seconds: {
|
|
316
|
+
p50: nearestRank(firstDurations, 0.5),
|
|
317
|
+
p75: nearestRank(firstDurations, 0.75)
|
|
318
|
+
},
|
|
319
|
+
returning_active_seconds: {
|
|
320
|
+
p50: nearestRank(returningDurations, 0.5),
|
|
321
|
+
p75: nearestRank(returningDurations, 0.75)
|
|
322
|
+
},
|
|
323
|
+
paired_reduction_ratio: {
|
|
324
|
+
p50: medianReduction,
|
|
325
|
+
p75: nearestRank(reductions, 0.75)
|
|
326
|
+
},
|
|
327
|
+
target_median_reduction_ratio: numericTargetReduction,
|
|
328
|
+
target_met: targetMet,
|
|
329
|
+
minimum_pair_count_for_exit: numericMinimumPairCount,
|
|
330
|
+
sample_size_met: sampleSizeMet
|
|
331
|
+
},
|
|
332
|
+
experience_reflux: {
|
|
333
|
+
recalled_experience_count: recalled.size,
|
|
334
|
+
verified_experience_count: verifiedRecalledCount,
|
|
335
|
+
baseline: refluxBaseline,
|
|
336
|
+
baseline_available: baselineAvailable,
|
|
337
|
+
real_evidence: exitEvidenceEligible
|
|
338
|
+
},
|
|
339
|
+
preference_assertions: {
|
|
340
|
+
total_count: assertions.length,
|
|
341
|
+
traceable_count: traceableCount,
|
|
342
|
+
traceable_ratio: traceableRatio,
|
|
343
|
+
hard_filter_violation_count: hardFilterViolationCount,
|
|
344
|
+
contract_met: assertionContractMet
|
|
345
|
+
},
|
|
346
|
+
p4: {
|
|
347
|
+
state: p4State,
|
|
348
|
+
trigger_observed: p4TriggerObserved,
|
|
349
|
+
contract_met: p4ContractMet
|
|
350
|
+
},
|
|
351
|
+
exit_evidence_eligible: exitEvidenceEligible,
|
|
352
|
+
exit_ready: exitReady
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
function main() {
|
|
356
|
+
const inputPath = process.argv[2];
|
|
357
|
+
if (!inputPath) {
|
|
358
|
+
console.error('usage: npx tsx scripts/memory-value-report.ts <fixture-or-private-manifest.json>');
|
|
359
|
+
process.exitCode = 2;
|
|
360
|
+
return;
|
|
361
|
+
}
|
|
362
|
+
let input;
|
|
363
|
+
try {
|
|
364
|
+
input = JSON.parse(readFileSync(resolve(inputPath), 'utf8'));
|
|
365
|
+
} catch (error) {
|
|
366
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
367
|
+
console.error(`cannot read memory value input: ${message}`);
|
|
368
|
+
process.exitCode = 2;
|
|
369
|
+
return;
|
|
370
|
+
}
|
|
371
|
+
const report = scoreMemoryValue(input);
|
|
372
|
+
console.log(JSON.stringify(report, null, 2));
|
|
373
|
+
if (!report.contract_valid) process.exitCode = 2;
|
|
374
|
+
}
|
|
375
|
+
const invokedPath = process.argv[1] ? pathToFileURL(resolve(process.argv[1])).href : '';
|
|
376
|
+
if (import.meta.url === invokedPath) main();
|
|
377
|
+
|
|
378
|
+
|
|
379
|
+
//# sourceURL=/Users/bytedance/work/gotry/ts/scripts/memory-value-report.ts
|