@trillboards/edge-sdk 0.2.197 → 0.2.200
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/deploy/detector/pyproject.toml +1 -1
- package/deploy/detector/tests/test_colors_v2.py +79 -0
- package/deploy/detector/tests/test_detection_colors_wire.py +25 -0
- package/deploy/detector/tests/test_held_phone_customer_packaging.py +102 -0
- package/deploy/detector/trillboards_detector/_version.py +2 -2
- package/deploy/detector/trillboards_detector/colors.py +30 -11
- package/deploy/detector/trillboards_detector/server.py +1 -1
- package/deploy/macos/build-macos-pkg.sh +14 -2
- package/deploy/windows/build-windows.ps1 +12 -2
- package/deploy/windows/ci/test-published-install.ps1 +167 -8
- package/deploy/windows/setup-detector.ps1 +4 -1
- package/dist/DetectionColors.d.ts +11 -4
- package/dist/DetectionColors.d.ts.map +1 -1
- package/dist/DetectionColors.js +5 -2
- package/dist/DetectionColors.js.map +1 -1
- package/dist/DetectorFields.d.ts +138 -4
- package/dist/DetectorFields.d.ts.map +1 -1
- package/dist/DetectorFields.js +101 -11
- package/dist/DetectorFields.js.map +1 -1
- package/dist/EdgeAgent.js +1 -1
- package/dist/LiteRtVlmProvider.d.ts.map +1 -1
- package/dist/LiteRtVlmProvider.js +10 -1
- package/dist/LiteRtVlmProvider.js.map +1 -1
- package/dist/PhoneEmbeddingClassifier.d.ts +28 -0
- package/dist/PhoneEmbeddingClassifier.d.ts.map +1 -0
- package/dist/PhoneEmbeddingClassifier.js +40 -0
- package/dist/PhoneEmbeddingClassifier.js.map +1 -0
- package/dist/RetainedFieldContract.d.ts +9 -1
- package/dist/RetainedFieldContract.d.ts.map +1 -1
- package/dist/RetainedFieldContract.js +19 -3
- package/dist/RetainedFieldContract.js.map +1 -1
- package/dist/ScopedVlmReview.d.ts +6 -2
- package/dist/ScopedVlmReview.d.ts.map +1 -1
- package/dist/ScopedVlmReview.js +55 -5
- package/dist/ScopedVlmReview.js.map +1 -1
- package/dist/SensingProfileClient.d.ts +6 -0
- package/dist/SensingProfileClient.d.ts.map +1 -1
- package/dist/SensingProfileClient.js +7 -0
- package/dist/SensingProfileClient.js.map +1 -1
- package/dist/diagnose.d.ts +1 -1
- package/dist/diagnose.js +1 -1
- package/dist/localSensing.d.ts +5 -2
- package/dist/localSensing.d.ts.map +1 -1
- package/dist/localSensing.js +56 -8
- package/dist/localSensing.js.map +1 -1
- package/package.json +3 -3
- package/retained/ProfileAuthorityClient.cjs +1 -1
- package/retained/RetainedCaptureClient.cjs +2 -1
- package/retained/WebsiteAdmissionCaptureClient.cjs +478 -0
- package/retained/WebsiteAdmissionState.cjs +75 -0
- package/retained/__tests__/actor-perception.test.cjs +1 -1
- package/retained/__tests__/actor-window.test.cjs +1 -1
- package/retained/__tests__/archive-lifecycle.test.cjs +1 -1
- package/retained/__tests__/runtime.test.cjs +30 -2
- package/retained/__tests__/website-admission-realtime.test.cjs +503 -0
- package/retained/__tests__/website-admission.test.cjs +806 -0
- package/retained/manager.cjs +4 -1
- package/retained/pilot-profile-client.cjs +2 -1
|
@@ -0,0 +1,806 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { test } = require('node:test');
|
|
3
|
+
const assert = require('node:assert/strict');
|
|
4
|
+
const fs = require('node:fs'), path = require('node:path'), os = require('node:os'), crypto = require('node:crypto');
|
|
5
|
+
const { WebsiteAdmissionCaptureClient, readAdmission, advanceAdmission, admissionContextHash } = require('../WebsiteAdmissionCaptureClient.cjs');
|
|
6
|
+
const { RetainedCaptureClient } = require('../RetainedCaptureClient.cjs');
|
|
7
|
+
const api = require('../../../../../trillboard-api/services/retainedWebsiteReviewAdmission');
|
|
8
|
+
const { nextReviewTiming } = require('../../../../../trillboard-api/services/fein/routeReviewTiming');
|
|
9
|
+
const { alertFocusFieldKey } = require('../../../../../trillboard-api/services/fein/alertFocusFields');
|
|
10
|
+
const digest = bytes => crypto.createHash('sha256').update(bytes).digest('hex');
|
|
11
|
+
const hash = value => digest(JSON.stringify(value));
|
|
12
|
+
const clone = value => JSON.parse(JSON.stringify(value));
|
|
13
|
+
const screenId = 'fixture_camera_7';
|
|
14
|
+
function route(extra = {}) {
|
|
15
|
+
return { routeId: 'fixture-phone', enabled: true, profileName: 'Phone staff area', mode: 'threshold',
|
|
16
|
+
condition: { field: 'phone_use', op: 'eq', value: true }, reviewTimingMode: 'wait_recheck', reviewWaitSeconds: 10,
|
|
17
|
+
sustainWindowSeconds: 0, sustainMinSamples: 1, reFirePolicy: 'once_per_episode',
|
|
18
|
+
updatedAt: '2026-10-20T12:00:00.000Z', screenMongoIds: [screenId], ...extra };
|
|
19
|
+
}
|
|
20
|
+
function input(extra = {}) {
|
|
21
|
+
return { screenId, profileId: 'fixture-profile', profileName: 'Phone staff area', defaultConfirmSamples: 1, issuedAtMs: 0,
|
|
22
|
+
fields: [{ field: 'phone_use', type: 'boolean' }], routes: [route()], ...extra };
|
|
23
|
+
}
|
|
24
|
+
function context(admission = api.waitEndpointAdmission(input())) {
|
|
25
|
+
return { version: 1, profileState: 'active', prompt: 'Synthetic fixture, not model evidence',
|
|
26
|
+
schema: { type: 'object', properties: { phone_use: { type: 'boolean' } }, required: ['phone_use'] },
|
|
27
|
+
retainedFieldContract: { version: 1, required: [{ field: 'phone_use', type: 'boolean' }] },
|
|
28
|
+
absenceWatchedFields: [], websiteReviewAdmission: admission };
|
|
29
|
+
}
|
|
30
|
+
function row(at, value, c = context()) {
|
|
31
|
+
const jpeg = Buffer.from('synthetic-protocol-image-' + at);
|
|
32
|
+
return { id: digest(jpeg).slice(0, 32), capturedAt: at, imageSha256: digest(jpeg), candidate: { reviewContext: c },
|
|
33
|
+
result: { evidenceSha256: digest(jpeg), structured: { phone_use: value } } };
|
|
34
|
+
}
|
|
35
|
+
function fixture(t, extra = {}) {
|
|
36
|
+
const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'website-admission-fixture-'));
|
|
37
|
+
const absolute = path.resolve(directory);
|
|
38
|
+
t.after(() => {
|
|
39
|
+
assert.equal(path.dirname(absolute), path.resolve(os.tmpdir()));
|
|
40
|
+
assert(path.basename(absolute).startsWith('website-admission-fixture-'));
|
|
41
|
+
fs.rmSync(absolute, { recursive: true, force: true });
|
|
42
|
+
});
|
|
43
|
+
let answer = true, accepted = true, decisionAtMs = 0, currentContext;
|
|
44
|
+
const client = {
|
|
45
|
+
getEndpoint: () => 'http://127.0.0.1:18791/v2/earner/audience-analyze',
|
|
46
|
+
captureReviewContext: () => currentContext,
|
|
47
|
+
async reviewCandidate(req) {
|
|
48
|
+
req.checkpointAnalysis({ evidenceSha256: digest(req.jpeg), structured: { phone_use: answer } });
|
|
49
|
+
},
|
|
50
|
+
async analyze(req) { return { ok: accepted, body: { accepted, evidenceId: req.evidenceId,
|
|
51
|
+
evidenceReview: { version: 1, capturedAtMs: req.capturedAt } } }; },
|
|
52
|
+
};
|
|
53
|
+
const options = { cameraId: 'camera7', pairedCameraId: screenId, offerWindow: 16,
|
|
54
|
+
maxCaptures: 200, maxBytes: 1000000, admissionNowMs: () => decisionAtMs, ...extra };
|
|
55
|
+
let archive = new WebsiteAdmissionCaptureClient(client, directory, options);
|
|
56
|
+
return { directory, options, client, get archive() { return archive; },
|
|
57
|
+
answer: v => { answer = v; }, accepted: v => { accepted = v; },
|
|
58
|
+
now: v => { decisionAtMs = v; }, current: v => { currentContext = v; },
|
|
59
|
+
recover: () => { archive = new WebsiteAdmissionCaptureClient(client, directory, options); return archive; },
|
|
60
|
+
capture(at, c = context()) {
|
|
61
|
+
decisionAtMs = at; currentContext = c;
|
|
62
|
+
archive.retainOrderedCapture({ name: 'camera7', enqueuedAtMs: at, jpeg: Buffer.from('synthetic-fixture-' + at),
|
|
63
|
+
captureMode: 'periodic', reason: 'motion', signature: String(at), reviewKind: 'event', reviewContext: c });
|
|
64
|
+
return archive.previousCaptureRecord;
|
|
65
|
+
},
|
|
66
|
+
async deliver(candidate = archive.pendingOrderedCandidates()[0]) {
|
|
67
|
+
assert(candidate, 'A selected fixture candidate is required');
|
|
68
|
+
return archive.analyze({ evidenceId: candidate.retainedId, capturedAt: candidate.enqueuedAtMs,
|
|
69
|
+
jpeg: candidate.jpeg, reviewContext: candidate.reviewContext, checkpointAnalysis() {} });
|
|
70
|
+
} };
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
test('strict reader supplies the exact observed lasso field; no whole-frame veto', () => {
|
|
74
|
+
const r = route({ screenFocusRegions: [{ screenMongoId: screenId, x: .3, y: .1, width: .48, height: .8 }] });
|
|
75
|
+
const focused = alertFocusFieldKey(r.routeId);
|
|
76
|
+
const a = api.waitEndpointAdmission(input({ routes: [r], fields: [...input().fields, { field: focused, type: 'boolean' }] }));
|
|
77
|
+
assert.equal(a.rules[0].field, focused);
|
|
78
|
+
const c = context(a); c.retainedFieldContract.required.push({ field: focused, type: 'boolean' });
|
|
79
|
+
const first = row(1000, false, c); first.result.structured[focused] = true;
|
|
80
|
+
assert.equal(advanceAdmission(a, first).anchors[r.routeId].evidenceId, first.id);
|
|
81
|
+
assert.equal(first.result.structured.phone_use, false);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
for (const [name, change] of [
|
|
85
|
+
['immediate route', { reviewTimingMode: 'immediate', reviewWaitSeconds: null }],
|
|
86
|
+
['unknown route mode', { mode: 'sequence' }], ['disabled route', { enabled: false }],
|
|
87
|
+
['absence rule', { condition: { field: 'phone_use', op: 'eq', value: false } }],
|
|
88
|
+
['unknown comparator', { condition: { field: 'phone_use', op: 'ne', value: false } }],
|
|
89
|
+
['count rule', { condition: { field: 'person_count', op: 'gte', value: 1 } }],
|
|
90
|
+
['missing wait', { reviewWaitSeconds: null }], ['fractional wait', { reviewWaitSeconds: 10.5 }],
|
|
91
|
+
['too long wait', { reviewWaitSeconds: 601 }], ['sustain samples', { sustainMinSamples: 2 }],
|
|
92
|
+
['sustain window', { sustainWindowSeconds: 10 }], ['corroboration', { confirmSamples: 2 }],
|
|
93
|
+
['dwell', { entityMinDwellSeconds: 10 }], ['repeated dwell', { reFirePolicy: 'nag' }],
|
|
94
|
+
['missing revision', { updatedAt: null }], ['wrong camera', { screenMongoIds: ['another_camera'] }],
|
|
95
|
+
]) test(`${name}: preserve normal changed-frame review`, () => {
|
|
96
|
+
assert.equal(api.waitEndpointAdmission(input({ routes: [route(), route({ routeId: 'fixture-other', ...change })] })), null);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
test('unreadable/missing/global corroboration does not silently thin intermediate samples', () => {
|
|
100
|
+
assert.equal(api.waitEndpointAdmission(input({ defaultConfirmSamples: 2 })), null);
|
|
101
|
+
assert.equal(api.waitEndpointAdmission(input({ defaultConfirmSamples: undefined })), null);
|
|
102
|
+
assert(api.waitEndpointAdmission(input({ defaultConfirmSamples: 2, routes: [route({ confirmSamples: 1 })] })));
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
test('non-threshold consumers and failed extra reads veto admission; no unnecessary DB request', async () => {
|
|
106
|
+
assert.equal(await api.resolveWaitEndpointAdmission(input(), async () => true), null);
|
|
107
|
+
assert.equal(await api.resolveWaitEndpointAdmission(input(), async () => null), null);
|
|
108
|
+
assert.equal(await api.resolveWaitEndpointAdmission(input(), async () => { throw Error('fixture unavailable'); }), null);
|
|
109
|
+
assert(await api.resolveWaitEndpointAdmission(input(), async id => { assert.equal(id, screenId); return false; }));
|
|
110
|
+
assert.equal(await api.resolveWaitEndpointAdmission(input({ routes: [] }), () => { throw Error('must not read'); }), null);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
test('a route bound to a different active profile is dormant, never a guessed consumer', () => {
|
|
114
|
+
assert(api.waitEndpointAdmission(input({ routes: [route(), route({ profileName: 'Other profile', reviewTimingMode: 'immediate' })] })));
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
test('native parser treats missing/tampered/unbound metadata as full review', () => {
|
|
118
|
+
const c = context(); assert(readAdmission(c, screenId, 1000));
|
|
119
|
+
assert.equal(readAdmission(c, 'other', 1000), null);
|
|
120
|
+
const altered = clone(c); altered.websiteReviewAdmission.rules[0].waitMs = 1;
|
|
121
|
+
assert.equal(readAdmission(altered, screenId, 1000), null);
|
|
122
|
+
delete c.websiteReviewAdmission; assert.equal(readAdmission(c, screenId, 1000), null);
|
|
123
|
+
const absent = context(); absent.absenceWatchedFields = ['phone_use'];
|
|
124
|
+
assert.equal(readAdmission(absent, screenId, 1000), null);
|
|
125
|
+
const unknown = context(); unknown.retainedFieldContract.required = [];
|
|
126
|
+
assert.equal(readAdmission(unknown, screenId, 1000), null);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
for (const field of ['phone_use', 'door_open', 'spill_present', 'person_present']) {
|
|
130
|
+
test(`${field}: endpoint clock agrees with server through early false/unknown and exact deadline`, () => {
|
|
131
|
+
const f = field, r = route({ condition: { field: f, op: 'eq', value: true } });
|
|
132
|
+
const a = api.waitEndpointAdmission(input({ fields: [{ field: f, type: 'boolean' }], routes: [r] }));
|
|
133
|
+
const c = context(a); c.retainedFieldContract.required = [{ field: f, type: 'boolean' }];
|
|
134
|
+
let state, server;
|
|
135
|
+
for (const [at, value] of [[1000, true], [2000, false], [10999, null], [11000, true], [11500, false]]) {
|
|
136
|
+
const sample = row(at, value, c); sample.result.structured = { [f]: value };
|
|
137
|
+
state = advanceAdmission(a, sample, state);
|
|
138
|
+
const next = nextReviewTiming(r, { capturedAtMs: at, evidenceId: sample.id,
|
|
139
|
+
matches: value, contextId: hash(c) }, server);
|
|
140
|
+
server = next.state;
|
|
141
|
+
assert.equal(state.anchors[r.routeId]?.capturedAt ?? null, server.anchorCapturedAtMs);
|
|
142
|
+
if (at === 11000) assert.equal(state.anchors[r.routeId].confirmed, true);
|
|
143
|
+
}
|
|
144
|
+
assert.equal(state.anchors[r.routeId], null);
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
test('capture originals first; skip only the interior; mandatory separate 10-second follow-up is urgent', async t => {
|
|
149
|
+
const f = fixture(t);
|
|
150
|
+
const first = f.capture(1000); await f.deliver();
|
|
151
|
+
for (let at = 1500; at <= 11000; at += 500) f.capture(at);
|
|
152
|
+
assert.equal(f.archive.stats().pending, 20); // deferred is real backlog, not hidden
|
|
153
|
+
const [followUp] = f.archive.pendingOrderedCandidates();
|
|
154
|
+
assert.equal(followUp.enqueuedAtMs, 11000); assert.equal(followUp.reviewPriority, 'urgent');
|
|
155
|
+
assert.notEqual(followUp.retainedId, first.id);
|
|
156
|
+
assert.equal(f.archive.stats().retainedWaitWindowCaptures, 19);
|
|
157
|
+
assert.equal(f.archive.stats().reviewed, 1);
|
|
158
|
+
assert.equal(f.archive.stats().selectedPending, 1);
|
|
159
|
+
for (const r of f.archive.records.values()) assert.equal(digest(fs.readFileSync(f.archive.image(r))), r.imageSha256);
|
|
160
|
+
await f.deliver(followUp);
|
|
161
|
+
assert.equal(f.archive.stats().pending, 0); assert.equal(f.archive.stats().reviewed, 2);
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
test('slow inference never adds duration, and a negative follow-up clears the anchor', async t => {
|
|
165
|
+
const f = fixture(t); f.capture(1000);
|
|
166
|
+
for (let at = 1500; at <= 12000; at += 500) f.capture(at);
|
|
167
|
+
await f.deliver(); // completion wall time is deliberately unrelated to capture time
|
|
168
|
+
assert.equal(f.archive.pendingOrderedCandidates()[0].enqueuedAtMs, 11000);
|
|
169
|
+
f.answer(false); await f.deliver();
|
|
170
|
+
f.answer(true); const next = f.archive.pendingOrderedCandidates()[0];
|
|
171
|
+
assert.equal(next.enqueuedAtMs, 11500); await f.deliver(next);
|
|
172
|
+
assert.equal([...f.archive.states.values()][0].anchors['fixture-phone'].capturedAt, 11500);
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
test('unknown endpoint retains the anchor and reviews the next frame, not a fabricated negative', async t => {
|
|
176
|
+
const f = fixture(t); f.capture(1000); await f.deliver(); f.capture(11000); f.capture(11500);
|
|
177
|
+
f.answer(null); await f.deliver();
|
|
178
|
+
assert.equal([...f.archive.states.values()][0].anchors['fixture-phone'].capturedAt, 1000);
|
|
179
|
+
assert.equal(f.archive.pendingOrderedCandidates()[0].enqueuedAtMs, 11500);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
test('multiple wait rules use the earliest needed deadline, never the longest timer', async t => {
|
|
183
|
+
const a = api.waitEndpointAdmission(input({ routes: [route(), route({ routeId: 'fixture-long', reviewWaitSeconds: 20 })] }));
|
|
184
|
+
const c = context(a), f = fixture(t); f.capture(1000, c); await f.deliver();
|
|
185
|
+
f.capture(10999, c); f.capture(11000, c); f.capture(21000, c);
|
|
186
|
+
assert.equal(f.archive.pendingOrderedCandidates()[0].enqueuedAtMs, 11000);
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
test('a profile/timer/model/zone context edit starts fresh and does not inherit an old anchor', async t => {
|
|
190
|
+
const f = fixture(t); f.capture(1000); await f.deliver();
|
|
191
|
+
const edited = context(); edited.regions = [['phone_use', { x: .1, y: .1, width: .8, height: .8 }]];
|
|
192
|
+
f.capture(1500, edited); assert.equal(f.archive.pendingOrderedCandidates()[0].enqueuedAtMs, 1500);
|
|
193
|
+
await f.deliver(); assert.equal(f.archive.states.size, 2);
|
|
194
|
+
assert.equal(f.archive.states.get(admissionContextHash(edited)).anchors['fixture-phone'].capturedAt, 1500);
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
test('delivery retry and restart preserve the exact selected evidence and deferred queue', async t => {
|
|
198
|
+
const f = fixture(t); f.capture(1000); f.capture(1500);
|
|
199
|
+
const [first] = f.archive.pendingOrderedCandidates(); f.accepted(false); await f.deliver(first);
|
|
200
|
+
f.recover(); assert.equal(f.archive.stats().pending, 2);
|
|
201
|
+
assert.equal(f.archive.pendingOrderedCandidates()[0].retainedId, first.retainedId);
|
|
202
|
+
assert.equal(f.archive.pendingOrderedCandidates().length, 1);
|
|
203
|
+
f.accepted(true); await f.deliver();
|
|
204
|
+
assert.deepEqual(f.archive.pendingOrderedCandidates(), []);
|
|
205
|
+
assert.equal(f.archive.stats().retainedWaitWindowCaptures, 1);
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
test('upgrade drains existing selected jobs unchanged; no reclassification to make backlog look small', async t => {
|
|
209
|
+
const f = fixture(t), old = new RetainedCaptureClient(f.client, f.directory, f.options);
|
|
210
|
+
for (const at of [1000, 1500, 2000]) old.retainOrderedCapture({ name: 'camera7', enqueuedAtMs: at,
|
|
211
|
+
jpeg: Buffer.from('synthetic-upgrade-' + at), reviewContext: context() });
|
|
212
|
+
f.recover(); assert.equal(f.archive.stats().pending, 3);
|
|
213
|
+
assert.equal(f.archive.pendingOrderedCandidates().length, 3);
|
|
214
|
+
const before = [...old.records.values()].map(r => [r.id, r.imageSha256, r.selected]);
|
|
215
|
+
assert.deepEqual([...f.archive.records.values()].map(r => [r.id, r.imageSha256, r.selected]), before);
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
test('archive pressure pauses visibly instead of deleting an unresolved original', async t => {
|
|
219
|
+
const f = fixture(t, { maxCaptures: 3, archiveRetentionMs: 500 });
|
|
220
|
+
const first = f.capture(1000); f.capture(1500); f.capture(2000);
|
|
221
|
+
assert.throws(() => f.capture(2500), /CAPTURE_STORAGE_EXHAUSTED/);
|
|
222
|
+
assert.equal(f.archive.stats().pending, 3);
|
|
223
|
+
assert.equal(digest(fs.readFileSync(f.archive.image(first))), first.imageSha256);
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
test('selected endpoint pins original anchor even when model says false and delivery retries past retention', async t => {
|
|
227
|
+
const f = fixture(t, { archiveRetentionMs: 500 });
|
|
228
|
+
const first = f.capture(1000); await f.deliver(); f.capture(11000);
|
|
229
|
+
f.answer(false); f.accepted(false); await f.deliver();
|
|
230
|
+
f.capture(20000); f.capture(21000);
|
|
231
|
+
assert(fs.existsSync(f.archive.image(first))); assert(fs.existsSync(f.archive.file(first.id)));
|
|
232
|
+
const original = fs.readFileSync(f.archive.image(first));
|
|
233
|
+
fs.writeFileSync(f.archive.image(first), 'synthetic-corruption');
|
|
234
|
+
await assert.rejects(f.deliver(), /anchor missing or changed/);
|
|
235
|
+
fs.writeFileSync(f.archive.image(first), original);
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
test('malformed/missing hint reviews every normal changed frame in capture order', async t => {
|
|
239
|
+
const f = fixture(t), c = context(); delete c.websiteReviewAdmission;
|
|
240
|
+
for (const at of [1000, 1500, 2000]) f.capture(at, c);
|
|
241
|
+
assert.equal(f.archive.stats().pending, 3);
|
|
242
|
+
for (const at of [1000, 1500, 2000]) {
|
|
243
|
+
const selected = f.archive.pendingOrderedCandidates();
|
|
244
|
+
assert.equal(selected.length, 1); assert.equal(selected[0].enqueuedAtMs, at);
|
|
245
|
+
await f.deliver(selected[0]);
|
|
246
|
+
}
|
|
247
|
+
assert.equal(f.archive.stats().reviewed, 3);
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
test('expired, future-skewed or malformed lease means full changed review, not silence', async t => {
|
|
251
|
+
const f = fixture(t); f.capture(1000); await f.deliver(); f.capture(1500); f.capture(30000);
|
|
252
|
+
assert.equal(f.archive.pendingOrderedCandidates()[0].enqueuedAtMs, 30000);
|
|
253
|
+
assert.equal(f.archive.stats().retainedWaitWindowCaptures, 1);
|
|
254
|
+
assert.equal(readAdmission(context(), screenId, 30000), null);
|
|
255
|
+
assert.equal(readAdmission(context(api.waitEndpointAdmission(input({ issuedAtMs: 10000 }))), screenId, 1000), null);
|
|
256
|
+
const malformed = context(); delete malformed.websiteReviewAdmission.lease;
|
|
257
|
+
assert.equal(readAdmission(malformed, screenId, 1000), null);
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
test('an immediate-rule edit cannot overtake older deferred evidence', async t => {
|
|
261
|
+
const f = fixture(t); f.capture(1000);
|
|
262
|
+
const immediate = context(); delete immediate.websiteReviewAdmission;
|
|
263
|
+
f.capture(1500, immediate);
|
|
264
|
+
assert.equal(f.archive.pendingOrderedCandidates()[0].enqueuedAtMs, 1000);
|
|
265
|
+
await f.deliver(); assert.equal(f.archive.pendingOrderedCandidates()[0].enqueuedAtMs, 1500);
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
test('lease renewal does not reset the endpoint timer or change the original contexts', async t => {
|
|
269
|
+
const f = fixture(t), first = f.capture(1000); await f.deliver();
|
|
270
|
+
const renewed = context(api.waitEndpointAdmission(input({ issuedAtMs: 5000 })));
|
|
271
|
+
assert.notDeepEqual(renewed, first.candidate.reviewContext);
|
|
272
|
+
assert.equal(admissionContextHash(renewed), admissionContextHash(first.candidate.reviewContext));
|
|
273
|
+
f.capture(6000, renewed); f.capture(11000, renewed);
|
|
274
|
+
const next = f.archive.pendingOrderedCandidates()[0];
|
|
275
|
+
assert.equal(next.enqueuedAtMs, 11000); assert.equal(next.reviewPriority, 'urgent');
|
|
276
|
+
await f.deliver(next);
|
|
277
|
+
assert.equal([...f.archive.states.values()][0].anchors['fixture-phone'].capturedAt, 1000);
|
|
278
|
+
assert.equal(first.candidate.reviewContext.websiteReviewAdmission.lease.issuedAtMs, 0);
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
test('a reviewed historical capture uses its captured lease, never completion wall time', async t => {
|
|
282
|
+
const f = fixture(t); f.capture(1000); f.capture(11000);
|
|
283
|
+
assert(Date.now() > 30000);
|
|
284
|
+
await f.deliver(); assert.equal(f.archive.pendingOrderedCandidates()[0].enqueuedAtMs, 11000);
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
for (const type of ['list', 'counter']) {
|
|
288
|
+
test('a companion ' + type + ' metric keeps full review even with only a boolean wait route', () => {
|
|
289
|
+
assert.equal(api.waitEndpointAdmission(input({ fields: [...input().fields,
|
|
290
|
+
{ field: 'companion_metric', type }] })), null);
|
|
291
|
+
});
|
|
292
|
+
test('the durable reader rejects an old hint when the captured contract includes a ' + type, () => {
|
|
293
|
+
const c = context();
|
|
294
|
+
c.retainedFieldContract.required.push({ field: 'companion_metric', type });
|
|
295
|
+
c.schema.properties.companion_metric = type === 'list' ? { type: 'array', items: { type: 'string' } } : { type: 'integer' };
|
|
296
|
+
c.schema.required.push('companion_metric');
|
|
297
|
+
assert.equal(readAdmission(c, screenId, 0), null);
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// Explicit phone-only admission: search spacing is separate from rule wait.
|
|
302
|
+
function phoneContext(at = 0, { intervalMs = 10000, waitSeconds = 10, ...extra } = {}) {
|
|
303
|
+
const issuedAtMs = Math.floor(at / 20000) * 20000;
|
|
304
|
+
return Object.assign(context(api.waitEndpointAdmission(input({ issuedAtMs, phoneSearchIntervalMs: intervalMs,
|
|
305
|
+
routes: [route({ reviewWaitSeconds: waitSeconds })] }))), extra);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
test('phone search is explicit and canonical; default0 keeps the legacy hint', () => {
|
|
309
|
+
const legacy = api.waitEndpointAdmission(input());
|
|
310
|
+
assert.equal(legacy.phoneSearch, undefined);
|
|
311
|
+
const configured = api.waitEndpointAdmission(input({ phoneSearchIntervalMs: 10000 }));
|
|
312
|
+
assert.deepEqual(configured.phoneSearch, { version: 1, kind: 'phone_only_search', intervalMs: 10000,
|
|
313
|
+
canonicalField: 'phone_use', fields: ['phone_use'] });
|
|
314
|
+
assert.equal(configured.rules[0].waitMs, 10000);
|
|
315
|
+
assert.equal(api.waitEndpointAdmission(input({ phoneSearchIntervalMs: 10000,
|
|
316
|
+
routes: [route({ reviewWaitSeconds: 60 })] })).rules[0].waitMs, 60000);
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
for (const interval of [0, -1, 499, 600001, 1000.25, NaN, Infinity, '10000garbage', '10000', null]) {
|
|
320
|
+
test('invalid/off phone search interval '+String(interval)+' preserves legacy admission', () => {
|
|
321
|
+
assert.equal(api.waitEndpointAdmission(input({ phoneSearchIntervalMs: interval })).phoneSearch, undefined);
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
for (const field of ['door_open', 'spill_present', 'person_present']) {
|
|
325
|
+
test(field+' is never phone search by profile name or boolean shape', () => {
|
|
326
|
+
const result = api.waitEndpointAdmission(input({ phoneSearchIntervalMs: 10000,
|
|
327
|
+
fields: [{ field, type: 'boolean' }], routes: [route({ condition: { field, op: 'eq', value: true } })] }));
|
|
328
|
+
assert(result); assert.equal(result.phoneSearch, undefined);
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
test('an unrelated required primary boolean vetoes phone search and canonical focus fields remain eligible', () => {
|
|
332
|
+
const mixed = api.waitEndpointAdmission(input({ phoneSearchIntervalMs: 10000,
|
|
333
|
+
fields: [...input().fields, { field: 'door_open', type: 'boolean' }] }));
|
|
334
|
+
assert.equal(mixed.phoneSearch, undefined);
|
|
335
|
+
const focusedRoute = route({ screenFocusRegions: [{ screenMongoId: screenId, x: 0, y: 0, width: 1, height: 1 }] });
|
|
336
|
+
const focused = alertFocusFieldKey(focusedRoute.routeId);
|
|
337
|
+
const eligible = api.waitEndpointAdmission(input({ phoneSearchIntervalMs: 500,
|
|
338
|
+
fields: [...input().fields, { field: focused, type: 'boolean' }], routes: [focusedRoute] }));
|
|
339
|
+
assert.deepEqual(eligible.phoneSearch.fields, [focused, 'phone_use'].sort());
|
|
340
|
+
});
|
|
341
|
+
test('a mixed phone/non-phone consumer vetoes phone search even when both have endpoint waits', () => {
|
|
342
|
+
const mixed = api.waitEndpointAdmission(input({ phoneSearchIntervalMs: 10000,
|
|
343
|
+
fields: [...input().fields, { field: 'door_open', type: 'boolean' }],
|
|
344
|
+
routes: [route(), route({ routeId: 'door', condition: { field: 'door_open', op: 'eq', value: true } })] }));
|
|
345
|
+
assert(mixed); assert.equal(mixed.phoneSearch, undefined);
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
test('phone search selects a current capture from2730eligible deferred originals without inventing reviews', t => {
|
|
349
|
+
const f = fixture(t, { maxCaptures: 3000 });
|
|
350
|
+
let latest;
|
|
351
|
+
for (let index = 0; index < 2730; index++) latest = f.capture(1000 + index * 500, phoneContext(1000 + index * 500));
|
|
352
|
+
const candidate = f.archive.pendingOrderedCandidates()[0];
|
|
353
|
+
assert.equal(candidate.retainedId, latest.id);
|
|
354
|
+
assert.equal(f.archive.stats().reviewed, 0); assert.equal(f.archive.stats().selectedPending, 1);
|
|
355
|
+
assert(latest.phoneAdmissionDecision, 'A durable policy/provenance decision must precede promotion');
|
|
356
|
+
for (let pass = 0; pass < 100; pass++) f.archive.pendingOrderedCandidates();
|
|
357
|
+
assert.equal(f.archive.stats().pendingAdmission, 0);
|
|
358
|
+
assert.equal(f.archive.stats().retainedPhoneSearchUnreviewed, 2729);
|
|
359
|
+
assert.equal(f.archive.records.size, 2730);
|
|
360
|
+
for (const r of f.archive.records.values()) {
|
|
361
|
+
assert.equal(digest(fs.readFileSync(f.archive.image(r))), r.imageSha256);
|
|
362
|
+
assert.equal(r.result, null); assert.equal(r.acknowledged, false);
|
|
363
|
+
}
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
test('first eligible confirmation beyond first32 keeps exact positive anchor and configured60second wait', async t => {
|
|
367
|
+
const f = fixture(t, { maxCaptures: 200 });
|
|
368
|
+
const first = f.capture(1000, phoneContext(1000, { waitSeconds: 60 })); await f.deliver();
|
|
369
|
+
for (let at = 1500; at <= 65000; at += 500) f.capture(at, phoneContext(at, { waitSeconds: 60 }));
|
|
370
|
+
const followUp = f.archive.pendingOrderedCandidates()[0];
|
|
371
|
+
assert.equal(followUp.enqueuedAtMs, 61000); assert.equal(followUp.reviewPriority, 'urgent');
|
|
372
|
+
const selected = f.archive.records.get(followUp.retainedId);
|
|
373
|
+
assert.equal(selected.ruleSelection.anchors[0].evidenceId, first.id);
|
|
374
|
+
assert.equal(selected.ruleSelection.anchors[0].imageSha256, first.imageSha256);
|
|
375
|
+
assert.equal(selected.ruleSelection.anchors[0].capturedAt, 1000);
|
|
376
|
+
assert.equal(selected.candidate.reviewContext.websiteReviewAdmission.rules[0].waitMs, 60000);
|
|
377
|
+
});
|
|
378
|
+
test('negative result resumes configured search spacing without changing business wait', async t => {
|
|
379
|
+
const f = fixture(t); f.answer(false); f.capture(1000, phoneContext(1000, { waitSeconds: 60 })); await f.deliver();
|
|
380
|
+
for (let at = 1500; at <= 10000; at += 500) f.capture(at, phoneContext(at, { waitSeconds: 60 }));
|
|
381
|
+
assert.deepEqual(f.archive.pendingOrderedCandidates(), []);
|
|
382
|
+
const latest = f.capture(11500, phoneContext(11500, { waitSeconds: 60 }));
|
|
383
|
+
assert.equal(f.archive.pendingOrderedCandidates()[0].retainedId, latest.id);
|
|
384
|
+
});
|
|
385
|
+
test('unknown due endpoint retains the exact anchor and prioritises the next capture', async t => {
|
|
386
|
+
const f = fixture(t); const first = f.capture(1000, phoneContext(1000)); await f.deliver();
|
|
387
|
+
f.capture(11000, phoneContext(11000)); f.answer(null); await f.deliver();
|
|
388
|
+
const next = f.capture(11500, phoneContext(11500));
|
|
389
|
+
const candidate = f.archive.pendingOrderedCandidates()[0];
|
|
390
|
+
assert.equal(candidate.retainedId, next.id); assert.equal(candidate.reviewPriority, 'urgent');
|
|
391
|
+
assert.equal(f.archive.records.get(next.id).ruleSelection.anchors[0].evidenceId, first.id);
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
test('explicit current phone policy migrates only matching legacy backlog with original contexts intact', t => {
|
|
395
|
+
const f = fixture(t, { maxCaptures: 3000 }); const snapshots = new Map();
|
|
396
|
+
for (let index = 0; index < 2730; index++) {
|
|
397
|
+
const at = 1000 + index * 500;
|
|
398
|
+
const legacy = context(api.waitEndpointAdmission(input({ issuedAtMs: Math.floor(at / 20000) * 20000 })));
|
|
399
|
+
const captured = f.capture(at, legacy); snapshots.set(captured.id, JSON.stringify(captured.candidate.reviewContext));
|
|
400
|
+
}
|
|
401
|
+
f.recover();
|
|
402
|
+
const current = f.capture(1366000, phoneContext(1366000));
|
|
403
|
+
assert.equal(f.archive.pendingOrderedCandidates()[0].retainedId, current.id);
|
|
404
|
+
const decision = f.archive.records.get(current.id).phoneAdmissionDecision;
|
|
405
|
+
assert.equal(decision.policy.evidenceId, current.id);
|
|
406
|
+
assert.equal(decision.policy.imageSha256, current.imageSha256);
|
|
407
|
+
for (const [id, original] of snapshots) assert.equal(JSON.stringify(f.archive.records.get(id).candidate.reviewContext), original);
|
|
408
|
+
f.recover(); assert.equal(f.archive.pendingOrderedCandidates()[0].retainedId, current.id);
|
|
409
|
+
assert.equal(f.archive.stats().selectedPending, 1);
|
|
410
|
+
});
|
|
411
|
+
|
|
412
|
+
for (const [name, extra] of [
|
|
413
|
+
['geometry', { regions: [['phone_use', { x: .1, y: .2, width: .6, height: .7 }]] }],
|
|
414
|
+
['model', { modelSelection: { selectionId: 'original-captured-model' } }],
|
|
415
|
+
['prompt', { prompt: 'Original captured question' }],
|
|
416
|
+
['actor dependency', { actorReview: { version: 1, source: 'captured-actor-window' } }],
|
|
417
|
+
]) test('nonmatching legacy '+name+' remains an explicit FIFO dependency barrier', t => {
|
|
418
|
+
const f = fixture(t); const older = f.capture(1000, Object.assign(context(), extra));
|
|
419
|
+
f.capture(11000, phoneContext(11000));
|
|
420
|
+
assert.equal(f.archive.pendingOrderedCandidates()[0].retainedId, older.id);
|
|
421
|
+
assert.equal(older.result, null); assert.equal(f.archive.stats().pendingAdmission, 1);
|
|
422
|
+
});
|
|
423
|
+
test('without explicit current phone policy, restart retains old v1 queue interpretation', t => {
|
|
424
|
+
const f = fixture(t); const first = f.capture(1000); f.capture(11000); f.recover();
|
|
425
|
+
assert.equal(f.archive.pendingOrderedCandidates()[0].retainedId, first.id);
|
|
426
|
+
assert.equal(f.archive.records.get(first.id).phoneAdmissionDecision, undefined);
|
|
427
|
+
});
|
|
428
|
+
test('failed atomic promotion leaves memory and disk unadvanced, then restart selects the same current evidence', t => {
|
|
429
|
+
const f = fixture(t); f.capture(1000, phoneContext(1000)); const latest = f.capture(11000, phoneContext(11000));
|
|
430
|
+
const filename = f.archive.file(latest.id), original = fs.readFileSync(filename), rename = fs.renameSync;
|
|
431
|
+
t.mock.method(fs, 'renameSync', (from, to) => { if (to === filename) throw Error('fixture_atomic_promotion_failure'); return rename(from, to); });
|
|
432
|
+
assert.throws(() => f.archive.pendingOrderedCandidates(), /fixture_atomic_promotion_failure/);
|
|
433
|
+
assert.deepEqual(fs.readFileSync(filename), original); assert.equal(latest.selected, false);
|
|
434
|
+
assert.equal(f.archive.stats().selectedPending, 0); assert.equal(f.archive.stats().pendingAdmission, 2);
|
|
435
|
+
t.mock.restoreAll(); f.recover(); assert.equal(f.archive.pendingOrderedCandidates()[0].retainedId, latest.id);
|
|
436
|
+
});
|
|
437
|
+
test('restart recovers durable selection before bounded omission writes and retries failed omission commits', t => {
|
|
438
|
+
const f = fixture(t); const first = f.capture(1000, phoneContext(1000)); const last = f.capture(11000, phoneContext(11000));
|
|
439
|
+
assert.equal(f.archive.pendingOrderedCandidates()[0].retainedId, last.id);
|
|
440
|
+
f.recover(); const filename = f.archive.file(first.id), original = fs.readFileSync(filename), rename = fs.renameSync;
|
|
441
|
+
t.mock.method(fs, 'renameSync', (from, to) => { if (to === filename) throw Error('fixture_atomic_omission_failure'); return rename(from, to); });
|
|
442
|
+
assert.throws(() => f.archive.pendingOrderedCandidates(), /fixture_atomic_omission_failure/);
|
|
443
|
+
assert.deepEqual(fs.readFileSync(filename), original); assert.equal(f.archive.stats().selectedPending, 1);
|
|
444
|
+
t.mock.restoreAll(); f.recover(); assert.equal(f.archive.pendingOrderedCandidates()[0].retainedId, last.id);
|
|
445
|
+
assert.equal(f.archive.records.get(first.id).result, null); assert.equal(f.archive.records.get(first.id).acknowledged, false);
|
|
446
|
+
});
|
|
447
|
+
|
|
448
|
+
test('unknown phone policy extensions fail closed even with a valid hint hash', () => {
|
|
449
|
+
const c = phoneContext(1000); c.websiteReviewAdmission.phoneSearch.unknownConsumer = true;
|
|
450
|
+
const { revision: _revision, lease: _lease, ...identity } = c.websiteReviewAdmission;
|
|
451
|
+
c.websiteReviewAdmission.revision = hash(identity);
|
|
452
|
+
assert.equal(readAdmission(c, screenId, 1000), null);
|
|
453
|
+
});
|
|
454
|
+
test('inactive captures cannot authorize migration of old active legacy work', t => {
|
|
455
|
+
const f = fixture(t, { reviewStage: at => at < 2000 ? 'active' : null });
|
|
456
|
+
const first = f.capture(1000); f.capture(11000, phoneContext(11000));
|
|
457
|
+
assert.equal(f.archive.pendingOrderedCandidates()[0].retainedId, first.id);
|
|
458
|
+
assert.equal(f.archive.records.get(first.id).phoneAdmissionDecision, undefined);
|
|
459
|
+
});
|
|
460
|
+
test('policy provenance corruption remains visibly pending after restart', async t => {
|
|
461
|
+
const f = fixture(t); const first = f.capture(1000); const current = f.capture(11000, phoneContext(11000));
|
|
462
|
+
const [selected] = f.archive.pendingOrderedCandidates(); assert.equal(selected.retainedId, current.id);
|
|
463
|
+
const data = JSON.parse(fs.readFileSync(f.archive.file(current.id)));
|
|
464
|
+
data.phoneAdmissionDecision.policy.contextSha256 = '0'.repeat(64);
|
|
465
|
+
fs.writeFileSync(f.archive.file(current.id), JSON.stringify(data)); f.recover();
|
|
466
|
+
await assert.rejects(f.deliver(), /policy provenance missing or changed/);
|
|
467
|
+
assert.equal(f.archive.records.get(current.id).acknowledged, false);
|
|
468
|
+
assert.equal(f.archive.records.get(first.id).result, null);
|
|
469
|
+
});
|
|
470
|
+
test('the selected scope/model/person-view context is unchanged by search admission', async t => {
|
|
471
|
+
const f = fixture(t);
|
|
472
|
+
const extras = { regions: [['phone_use', { x: 0, y: 0, width: 1, height: 1 }]],
|
|
473
|
+
phonePersonViews: { version: 1, method: 'native_person_views_v1', maxImages: 5, minConfidence: .25 },
|
|
474
|
+
modelSelection: { selectionId: 'captured-immutable-models', models: ['gemma', 'person-proposals'] } };
|
|
475
|
+
f.capture(1000, phoneContext(1000, extras)); const latest = f.capture(11000, phoneContext(11000, extras));
|
|
476
|
+
const original = JSON.stringify(latest.candidate.reviewContext);
|
|
477
|
+
f.client.reviewCandidate = async req => {
|
|
478
|
+
assert.equal(JSON.stringify(req.reviewContext), original);
|
|
479
|
+
req.checkpointAnalysis({ evidenceSha256: digest(req.jpeg), structured: { phone_use: false } });
|
|
480
|
+
};
|
|
481
|
+
await f.deliver(); assert.equal(JSON.stringify(f.archive.records.get(latest.id).candidate.reviewContext), original);
|
|
482
|
+
});
|
|
483
|
+
|
|
484
|
+
for (const missing of [undefined, null]) test('legacy '+String(missing)+' context remains a startup-safe FIFO barrier', t => {
|
|
485
|
+
const f = fixture(t), legacy = new RetainedCaptureClient(f.client, f.directory, f.options);
|
|
486
|
+
legacy.retainOrderedCapture({ name: 'camera7', enqueuedAtMs: 1000,
|
|
487
|
+
jpeg: Buffer.from('legacy-no-context'), ...(missing === null ? { reviewContext: null } : {}) });
|
|
488
|
+
const first = legacy.previousCaptureRecord;
|
|
489
|
+
first.selected = false; first.disposition = 'awaiting_rule_decision';
|
|
490
|
+
fs.writeFileSync(legacy.file(first.id), JSON.stringify(first));
|
|
491
|
+
f.recover(); f.capture(11000, phoneContext(11000));
|
|
492
|
+
assert.equal(f.archive.pendingOrderedCandidates()[0].retainedId, first.id);
|
|
493
|
+
assert.equal(f.archive.records.get(first.id).phoneAdmissionDecision, undefined);
|
|
494
|
+
assert.equal(f.archive.records.get(first.id).candidate.reviewContext, missing);
|
|
495
|
+
assert.equal(f.archive.stats().pendingAdmission, 1);
|
|
496
|
+
});
|
|
497
|
+
|
|
498
|
+
for (const kind of ['corrupt', 'missing']) {
|
|
499
|
+
for (const mode of ['search authority', 'separate due authority']) {
|
|
500
|
+
test(kind+' '+mode+' JPEG cannot authorize pending omissions before prepare or restart', async t => {
|
|
501
|
+
const f = fixture(t); let first, authority, selected;
|
|
502
|
+
if (mode === 'search authority') {
|
|
503
|
+
first = f.capture(1000); authority = f.capture(11000, phoneContext(11000));
|
|
504
|
+
[selected] = f.archive.pendingOrderedCandidates();
|
|
505
|
+
} else {
|
|
506
|
+
f.capture(1000, phoneContext(1000)); await f.deliver();
|
|
507
|
+
for (let at = 1500; at <= 12500; at += 500) {
|
|
508
|
+
const r = f.capture(at, phoneContext(at)); first ??= r; authority = r;
|
|
509
|
+
}
|
|
510
|
+
[selected] = f.archive.pendingOrderedCandidates();
|
|
511
|
+
assert.notEqual(selected.retainedId, authority.id);
|
|
512
|
+
}
|
|
513
|
+
const filename = f.archive.file(first.id), original = fs.readFileSync(filename);
|
|
514
|
+
const jpegFile = f.archive.image(authority), jpeg = fs.readFileSync(jpegFile);
|
|
515
|
+
if (kind === 'missing') fs.unlinkSync(jpegFile); else fs.writeFileSync(jpegFile, 'changed-policy-image');
|
|
516
|
+
try { f.archive.pendingOrderedCandidates(); } catch (error) { assert.equal(kind, 'missing'); assert.equal(error.code, 'ENOENT'); }
|
|
517
|
+
assert.deepEqual(fs.readFileSync(filename), original, 'No omission may commit before actual policy JPEG proof');
|
|
518
|
+
assert.equal(f.archive.records.get(first.id).disposition, 'awaiting_rule_decision');
|
|
519
|
+
await assert.rejects(f.deliver(selected), /policy provenance missing or changed|policy image missing or changed|ENOENT/);
|
|
520
|
+
assert.throws(() => f.recover(), /ENOENT|Expected values to be strictly equal/);
|
|
521
|
+
assert.deepEqual(fs.readFileSync(filename), original, 'Restart failure must leave older originals pending');
|
|
522
|
+
fs.writeFileSync(jpegFile, jpeg); f.recover();
|
|
523
|
+
assert.equal(f.archive.records.get(first.id).disposition, 'awaiting_rule_decision');
|
|
524
|
+
assert.equal(f.archive.stats().selectedPending, 1);
|
|
525
|
+
});
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
test('new omissions require actual policy JPEG proof even without a promotion receipt', async t => {
|
|
530
|
+
const f = fixture(t); f.answer(false); f.capture(1000, phoneContext(1000)); await f.deliver();
|
|
531
|
+
const next = f.capture(1500, phoneContext(1500)), filename = f.archive.file(next.id);
|
|
532
|
+
const original = fs.readFileSync(filename), jpeg = f.archive.image(next);
|
|
533
|
+
fs.writeFileSync(jpeg, 'changed-policy-image');
|
|
534
|
+
f.archive.pendingOrderedCandidates();
|
|
535
|
+
// Normal FIFO selection remains allowed; new phone-specific omission does not.
|
|
536
|
+
assert.equal(f.archive.records.get(next.id).phoneAdmissionOmission, undefined);
|
|
537
|
+
assert.notEqual(f.archive.records.get(next.id).disposition, 'archived_phone_search_unreviewed');
|
|
538
|
+
assert.equal(JSON.parse(original).result, null);
|
|
539
|
+
});
|
|
540
|
+
|
|
541
|
+
test('proof authority, selected decision and exact anchor stay pinned until covered omissions commit', async t => {
|
|
542
|
+
const f = fixture(t, { archiveRetentionMs: 500 });
|
|
543
|
+
const first = f.capture(1000, phoneContext(1000, { waitSeconds: 60 })); await f.deliver();
|
|
544
|
+
let authority;
|
|
545
|
+
for (let at = 1500; at <= 65000; at += 500) authority = f.capture(at, phoneContext(at, { waitSeconds: 60 }));
|
|
546
|
+
const [selected] = f.archive.pendingOrderedCandidates();
|
|
547
|
+
f.archive.pendingOrderedCandidates(); // only32 omissions per pump
|
|
548
|
+
f.capture(66000, phoneContext(66000, { waitSeconds: 60 }));
|
|
549
|
+
f.capture(66500, phoneContext(66500, { waitSeconds: 60 }));
|
|
550
|
+
assert(f.archive.stats().pendingAdmission > 32);
|
|
551
|
+
for (const id of [first.id, authority.id, selected.retainedId]) {
|
|
552
|
+
assert(f.archive.archivePins().has(id)); assert(f.archive.records.has(id));
|
|
553
|
+
assert(fs.existsSync(f.archive.file(id))); assert(fs.existsSync(f.archive.image(f.archive.records.get(id))));
|
|
554
|
+
}
|
|
555
|
+
f.recover(); assert.equal(f.archive.stats().selectedPending, 1);
|
|
556
|
+
});
|
|
557
|
+
|
|
558
|
+
test('metadata cache follows retained rows and lifetime counts survive phone omission pruning/restart', async t => {
|
|
559
|
+
const f = fixture(t, { archiveRetentionMs: 500, maxCaptures: 12 }); f.answer(false);
|
|
560
|
+
for (let index = 0; index < 40; index++) {
|
|
561
|
+
const at = 1000 + index * 500; f.capture(at, phoneContext(at, { intervalMs: 1000 }));
|
|
562
|
+
const [selected] = f.archive.pendingOrderedCandidates(); if (selected) await f.deliver(selected);
|
|
563
|
+
assert(f.archive.admissionMetadata.size <= f.archive.records.size, 'Cache cannot retain retired capture metadata');
|
|
564
|
+
for (const id of f.archive.admissionMetadata.keys()) assert(f.archive.records.has(id));
|
|
565
|
+
}
|
|
566
|
+
const before = f.archive.stats(); assert(before.prunedCaptures > 0);
|
|
567
|
+
assert.equal(before.captures, 40); assert.equal(before.reviewed, before.selected);
|
|
568
|
+
assert.equal(before.archivedBetweenReviews, before.captures - before.selected);
|
|
569
|
+
assert.equal(before.retainedPhoneSearchUnreviewed,
|
|
570
|
+
[...f.archive.records.values()].filter(r => r.disposition === 'archived_phone_search_unreviewed').length);
|
|
571
|
+
f.recover(); const after = f.archive.stats();
|
|
572
|
+
for (const key of ['captures', 'selected', 'reviewed', 'received', 'archivedBetweenReviews', 'retainedPhoneSearchUnreviewed']) {
|
|
573
|
+
assert.equal(after[key], before[key], key+' must survive restart without reclassifying unreviewed frames');
|
|
574
|
+
}
|
|
575
|
+
});
|
|
576
|
+
|
|
577
|
+
test('stopped capture cannot newly migrate work after its authority lease expires', t => {
|
|
578
|
+
const f = fixture(t); const first = f.capture(1000); f.capture(11000, phoneContext(11000));
|
|
579
|
+
f.now(31000);
|
|
580
|
+
assert.equal(f.archive.pendingOrderedCandidates()[0].retainedId, first.id);
|
|
581
|
+
assert.equal(f.archive.records.get(first.id).phoneAdmissionDecision, undefined);
|
|
582
|
+
});
|
|
583
|
+
test('current disabled, unreadable or changed policy vetoes new migration before another frame', t => {
|
|
584
|
+
for (const change of [() => context(), () => null, () => phoneContext(11000, { intervalMs: 20000 })]) {
|
|
585
|
+
const f = fixture(t); const first = f.capture(1000); f.capture(11000, phoneContext(11000));
|
|
586
|
+
f.current(change());
|
|
587
|
+
assert.equal(f.archive.pendingOrderedCandidates()[0].retainedId, first.id);
|
|
588
|
+
assert.equal(f.archive.records.get(first.id).phoneAdmissionDecision, undefined);
|
|
589
|
+
}
|
|
590
|
+
});
|
|
591
|
+
test('a durable phone decision remains valid after lease expiry and current disable on restart', t => {
|
|
592
|
+
const f = fixture(t); const first = f.capture(1000); const latest = f.capture(11000, phoneContext(11000));
|
|
593
|
+
assert.equal(f.archive.pendingOrderedCandidates()[0].retainedId, latest.id);
|
|
594
|
+
f.now(90000); f.current(context()); f.recover();
|
|
595
|
+
assert.equal(f.archive.pendingOrderedCandidates()[0].retainedId, latest.id);
|
|
596
|
+
assert.equal(f.archive.records.get(first.id).disposition, 'archived_phone_search_unreviewed');
|
|
597
|
+
assert.equal(f.archive.records.get(first.id).result, null);
|
|
598
|
+
});
|
|
599
|
+
|
|
600
|
+
for (const [name, legacy] of [
|
|
601
|
+
['route revision', () => context(api.waitEndpointAdmission(input({ routes: [route({ updatedAt: '2026-10-19T00:00:00Z' })] })))],
|
|
602
|
+
['business wait', () => context(api.waitEndpointAdmission(input({ routes: [route({ reviewWaitSeconds: 60 })] })))],
|
|
603
|
+
['profile identity', () => context(api.waitEndpointAdmission(input({ profileId: 'original-legacy-profile' })))],
|
|
604
|
+
['schema', () => { const c = context(); c.schema.properties.phone_use.description = 'Original captured question'; return c; }],
|
|
605
|
+
]) test('legacy '+name+' dependency is never normalized away during migration', t => {
|
|
606
|
+
const f = fixture(t), first = f.capture(1000, legacy()); f.capture(11000, phoneContext(11000));
|
|
607
|
+
assert.equal(f.archive.pendingOrderedCandidates()[0].retainedId, first.id);
|
|
608
|
+
assert.equal(f.archive.records.get(first.id).phoneAdmissionDecision, undefined);
|
|
609
|
+
assert.equal(f.archive.stats().pendingAdmission, 1);
|
|
610
|
+
});
|
|
611
|
+
|
|
612
|
+
|
|
613
|
+
// Compose the actual archive and disk spool with the server's real timer logic.
|
|
614
|
+
// Only model answers and the external response are controlled fixture boundaries.
|
|
615
|
+
const { DurableVlmCandidateSpool } = require('../../dist/DurableVlmCandidateSpool');
|
|
616
|
+
function spooledFixture(t, { timerRule = route() } = {}) {
|
|
617
|
+
const f = fixture(t);
|
|
618
|
+
let now = 1000, calls = 0, server;
|
|
619
|
+
const localReview = f.client.reviewCandidate;
|
|
620
|
+
f.client.reviewCandidate = async req => {
|
|
621
|
+
if (req.cachedAnalysis) req.checkpointAnalysis(req.cachedAnalysis);
|
|
622
|
+
else { calls++; await localReview(req); }
|
|
623
|
+
};
|
|
624
|
+
let queue;
|
|
625
|
+
const recoverQueue = () => {
|
|
626
|
+
queue = new DurableVlmCandidateSpool({ directory: path.join(f.directory, 'spool'),
|
|
627
|
+
reviewWindowMs: 150000, retentionMs: 86400000, preserveArchivedPending: true,
|
|
628
|
+
maxFrames: 32, maxPerFeed: 32, maxBytes: 1000000, retryInitialMs: 500, now: () => now });
|
|
629
|
+
queue.startRun(); return queue;
|
|
630
|
+
};
|
|
631
|
+
recoverQueue();
|
|
632
|
+
const request = item => ({ evidenceId: item.spoolId, capturedAt: item.enqueuedAtMs,
|
|
633
|
+
jpeg: item.jpeg, reviewContext: item.reviewContext, cachedAnalysis: item.cachedAnalysis,
|
|
634
|
+
checkpointAnalysis: saved => queue.checkpoint(item, saved) });
|
|
635
|
+
return { f, get queue() { return queue; }, get calls() { return calls; },
|
|
636
|
+
now: value => { now = value; },
|
|
637
|
+
recover() { f.recover(); recoverQueue(); },
|
|
638
|
+
pump() {
|
|
639
|
+
for (const candidate of f.archive.pendingOrderedCandidates()) {
|
|
640
|
+
assert(['buffered', 'coalesced'].includes(queue.offer(candidate, now)));
|
|
641
|
+
}
|
|
642
|
+
},
|
|
643
|
+
async review() {
|
|
644
|
+
const item = queue.take(now, 'review'); assert(item, 'Actual spool must contain review work');
|
|
645
|
+
await f.archive.reviewCandidate(request(item)); queue.readyForDelivery(item); return item;
|
|
646
|
+
},
|
|
647
|
+
async upload() {
|
|
648
|
+
const item = queue.take(now, 'delivery'); assert(item, 'Actual spool must contain saved delivery work');
|
|
649
|
+
const response = await f.archive.analyze(request(item));
|
|
650
|
+
const receipt = response.body?.evidenceReview;
|
|
651
|
+
const accepted = response.ok && response.body?.accepted === true &&
|
|
652
|
+
response.body.evidenceId === item.spoolId && receipt?.version === 1 &&
|
|
653
|
+
receipt.capturedAtMs === item.enqueuedAtMs;
|
|
654
|
+
if (accepted) {
|
|
655
|
+
const next = nextReviewTiming(timerRule, { capturedAtMs: item.enqueuedAtMs,
|
|
656
|
+
evidenceId: item.spoolId, matches: item.cachedAnalysis.structured.phone_use,
|
|
657
|
+
contextId: 'unchanged-spooled-fixture' }, server);
|
|
658
|
+
server = next.state; queue.complete(item);
|
|
659
|
+
} else queue.retryDelivery(item, now);
|
|
660
|
+
return { item, accepted, server };
|
|
661
|
+
},
|
|
662
|
+
};
|
|
663
|
+
}
|
|
664
|
+
for (const receiptVersion of [undefined, 0, 2]) {
|
|
665
|
+
test('actual spool and archive both keep an unsupported receipt pending: version=' + String(receiptVersion), async t => {
|
|
666
|
+
const r = spooledFixture(t), first = r.f.capture(1000);
|
|
667
|
+
r.pump(); await r.review();
|
|
668
|
+
const response = r.f.client.analyze;
|
|
669
|
+
r.f.client.analyze = async req => {
|
|
670
|
+
const result = await response(req);
|
|
671
|
+
result.body.evidenceReview.version = receiptVersion; return result;
|
|
672
|
+
};
|
|
673
|
+
assert.equal((await r.upload()).accepted, false);
|
|
674
|
+
assert.equal(r.queue.getStats().completed, 0);
|
|
675
|
+
assert.equal(r.f.archive.records.get(first.id).acknowledged, false,
|
|
676
|
+
'Archive must not claim success when the worker rejects the same receipt');
|
|
677
|
+
assert.equal(r.f.archive.stats().received, 0);
|
|
678
|
+
r.recover(); r.now(1500);
|
|
679
|
+
r.f.client.analyze = response;
|
|
680
|
+
assert.equal((await r.upload()).accepted, true);
|
|
681
|
+
assert.equal(r.calls, 1, 'Receipt repair and restart must reuse completed inference');
|
|
682
|
+
assert.equal(r.f.archive.stats().pending, 0);
|
|
683
|
+
});
|
|
684
|
+
}
|
|
685
|
+
test('actual spool preserves a due endpoint, original anchor and cached answer through failed delivery/restart', async t => {
|
|
686
|
+
const r = spooledFixture(t), first = r.f.capture(1000);
|
|
687
|
+
r.pump(); await r.review(); r.f.accepted(false);
|
|
688
|
+
assert.equal((await r.upload()).accepted, false);
|
|
689
|
+
r.f.capture(1500); const due = r.f.capture(11000);
|
|
690
|
+
r.pump();
|
|
691
|
+
assert.equal(r.queue.take(11000, 'review'), null, 'Do not overtake the unaccepted anchor');
|
|
692
|
+
r.recover(); r.now(11000); r.f.accepted(true);
|
|
693
|
+
assert.equal((await r.upload()).server.anchorCapturedAtMs, 1000);
|
|
694
|
+
r.pump(); const followUp = await r.review();
|
|
695
|
+
assert.equal(followUp.spoolId, due.id);
|
|
696
|
+
assert.equal(followUp.enqueuedAtMs, 11000);
|
|
697
|
+
assert.equal(followUp.reviewPriority, 'urgent');
|
|
698
|
+
assert.equal(r.f.archive.records.get(due.id).ruleSelection.anchors[0].evidenceId, first.id);
|
|
699
|
+
const delivered = await r.upload();
|
|
700
|
+
assert.equal(delivered.server.anchorCapturedAtMs, 1000);
|
|
701
|
+
assert.equal(r.calls, 2, 'Only the two selected captures require controlled inference');
|
|
702
|
+
assert.equal(r.f.archive.stats().retainedWaitWindowCaptures, 1);
|
|
703
|
+
assert.equal(r.f.archive.stats().pending, 0);
|
|
704
|
+
assert.equal(r.queue.getStats().buffered, 0);
|
|
705
|
+
for (const row of r.f.archive.records.values()) {
|
|
706
|
+
assert.equal(digest(fs.readFileSync(r.f.archive.image(row))), row.imageSha256);
|
|
707
|
+
}
|
|
708
|
+
});
|
|
709
|
+
test('actual spool does not admit a later observation while the first model result is unfinished', async t => {
|
|
710
|
+
const r = spooledFixture(t); r.f.capture(1000); r.pump();
|
|
711
|
+
const review = r.f.client.reviewCandidate;
|
|
712
|
+
let resume;
|
|
713
|
+
const held = new Promise(resolve => { resume = resolve; });
|
|
714
|
+
r.f.client.reviewCandidate = async req => { await held; return review(req); };
|
|
715
|
+
const work = r.review();
|
|
716
|
+
r.f.capture(11000); r.pump();
|
|
717
|
+
assert.equal(r.queue.take(11000, 'review'), null);
|
|
718
|
+
assert.equal(r.f.archive.stats().reviewed, 0);
|
|
719
|
+
resume(); await work;
|
|
720
|
+
assert.equal(r.calls, 1);
|
|
721
|
+
assert.equal((await r.upload()).accepted, true);
|
|
722
|
+
r.pump(); await r.review(); await r.upload();
|
|
723
|
+
assert.equal(r.calls, 2);
|
|
724
|
+
assert.equal(r.f.archive.stats().pending, 0);
|
|
725
|
+
});
|
|
726
|
+
|
|
727
|
+
for (const edit of ['region', 'profile', 'model']) {
|
|
728
|
+
test('actual spool isolates a ' + edit + ' revision instead of inheriting an earlier positive', async t => {
|
|
729
|
+
const r = spooledFixture(t); const first = r.f.capture(1000);
|
|
730
|
+
r.pump(); await r.review(); await r.upload();
|
|
731
|
+
const changed = context();
|
|
732
|
+
if (edit === 'region') changed.regions = [['phone_use', { x: .1, y: .1, width: .8, height: .8 }]];
|
|
733
|
+
if (edit === 'profile') changed.prompt = 'Different captured lab question';
|
|
734
|
+
if (edit === 'model') changed.modelSelection = { version: 1, selectionId: 'new-lab-selection',
|
|
735
|
+
models: [{ modelId: 'gemma_4_e2b', format: 'litert-lm',
|
|
736
|
+
sha256: 'ab7838cdfc8f77e54d8ca45eadceb20452d9f01e4bfade03e5dce27911b27e42' }] };
|
|
737
|
+
const second = r.f.capture(1500, changed); r.now(1500); r.pump();
|
|
738
|
+
assert.equal((await r.review()).spoolId, second.id);
|
|
739
|
+
assert.equal(r.f.archive.states.size, 2);
|
|
740
|
+
assert.equal(r.f.archive.states.get(admissionContextHash(changed)).anchors['fixture-phone'].evidenceId, second.id);
|
|
741
|
+
assert.equal(r.f.archive.records.get(first.id).capturedAt, 1000);
|
|
742
|
+
await r.upload(); assert.equal(r.calls, 2);
|
|
743
|
+
});
|
|
744
|
+
}
|
|
745
|
+
test('actual spool preserves a sixty-second capture-time deadline through lease renewal', async t => {
|
|
746
|
+
const timerRule = route({ reviewWaitSeconds: 60 });
|
|
747
|
+
const retained = issuedAtMs => context(api.waitEndpointAdmission(input({ issuedAtMs, routes: [timerRule] })));
|
|
748
|
+
const r = spooledFixture(t, { timerRule }), first = r.f.capture(1000, retained(0));
|
|
749
|
+
r.pump(); await r.review(); await r.upload();
|
|
750
|
+
r.f.capture(1500, retained(0)); r.f.capture(30000, retained(25000));
|
|
751
|
+
r.f.capture(60500, retained(60000)); const due = r.f.capture(61000, retained(60000));
|
|
752
|
+
r.now(61000); r.pump(); const next = await r.review();
|
|
753
|
+
assert.equal(next.spoolId, due.id);
|
|
754
|
+
assert.equal(next.reviewPriority, 'urgent');
|
|
755
|
+
assert.equal(r.f.archive.states.size, 1, 'Lease renewal is not a new model/profile/area identity');
|
|
756
|
+
assert.equal(r.f.archive.records.get(due.id).ruleSelection.anchors[0].evidenceId, first.id);
|
|
757
|
+
assert.equal((await r.upload()).server.anchorCapturedAtMs, 1000);
|
|
758
|
+
assert.equal(r.calls, 2); assert.equal(r.f.archive.stats().pending, 0);
|
|
759
|
+
assert.equal(r.f.archive.stats().retainedWaitWindowCaptures, 3);
|
|
760
|
+
});
|
|
761
|
+
|
|
762
|
+
|
|
763
|
+
for (const example of require('../../../../../trillboard-api/tests/fixtures/review-timing-anchor-expiry.json')) {
|
|
764
|
+
test(`shared server/connector anchor expiry: ${example.name}`,()=>{
|
|
765
|
+
const r=route({reviewWaitSeconds:example.waitSeconds});
|
|
766
|
+
const a=api.waitEndpointAdmission(input({routes:[r]}));
|
|
767
|
+
const c=context(a);let state,server;
|
|
768
|
+
for(const [at,value] of example.sequence){
|
|
769
|
+
const sample=row(at,value,c);state=advanceAdmission(a,sample,state);
|
|
770
|
+
server=nextReviewTiming(r,{capturedAtMs:at,evidenceId:sample.id,matches:value,contextId:hash(c)},server).state;
|
|
771
|
+
assert.equal(state.anchors[r.routeId]?.capturedAt??null,server.anchorCapturedAtMs);
|
|
772
|
+
}
|
|
773
|
+
assert.equal(state.anchors[r.routeId]?.capturedAt??null,example.anchor);
|
|
774
|
+
assert.equal(Boolean(state.anchors[r.routeId]?.confirmed),example.confirmed);
|
|
775
|
+
});
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
|
|
779
|
+
for(const [name,times,kind,capture] of [
|
|
780
|
+
['expired-only frame',[121001],'search',121001],
|
|
781
|
+
['gap across confirmation window',[60000,121001],'search',121001],
|
|
782
|
+
['preserve a saved on-time confirmation',[61000,121001],'confirmation',61000],
|
|
783
|
+
])test(`phone scheduling uses a live confirmation anchor: ${name}`,()=>{
|
|
784
|
+
const {choosePhoneReview}=require('../WebsiteAdmissionState.cjs');
|
|
785
|
+
const admission={rules:[{routeId:'r',waitMs:60000}]};
|
|
786
|
+
const state={capturedAt:1000,anchors:{r:{capturedAt:1000,confirmed:false}}};
|
|
787
|
+
const choice=choosePhoneReview(times.map(capturedAt=>({capturedAt})),admission,state,10000);
|
|
788
|
+
assert.equal(choice.kind,kind);assert.equal(choice.row.capturedAt,capture);
|
|
789
|
+
if(kind==='search')assert.equal(choice.anchors[0].anchor,null);
|
|
790
|
+
});
|
|
791
|
+
|
|
792
|
+
|
|
793
|
+
test('ordinary admission does not mark an expired anchor as an urgent confirmation',async t=>{
|
|
794
|
+
const f=fixture(t);const r=route({reviewWaitSeconds:60});
|
|
795
|
+
const renewed=at=>context(api.waitEndpointAdmission(input({routes:[r],issuedAtMs:at})));
|
|
796
|
+
const first=f.capture(1000,renewed(1000));await f.deliver();
|
|
797
|
+
assert.equal(f.archive.states.get(admissionContextHash(first.candidate.reviewContext)).anchors[r.routeId].capturedAt,1000);
|
|
798
|
+
const later=f.capture(121001,renewed(121001));
|
|
799
|
+
f.archive.pendingOrderedCandidates();
|
|
800
|
+
const selected=f.archive.records.get(later.id);
|
|
801
|
+
assert.equal(selected.selected,true);
|
|
802
|
+
assert.equal(selected.selectionReason,'website_initial_or_context_change');
|
|
803
|
+
assert.deepEqual(selected.ruleSelection.anchors,[]);
|
|
804
|
+
assert.notEqual(selected.scheduledReviewPriority,'urgent');
|
|
805
|
+
assert.equal(selected.candidate.reviewContext.websiteReviewAdmission.phoneSearch,undefined);
|
|
806
|
+
});
|