@trillboards/edge-sdk 0.2.260 → 0.2.263
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/windows/ci/test-published-install.ps1 +10 -1
- package/dist/CloudSensingManager.d.ts +36 -0
- package/dist/CloudSensingManager.d.ts.map +1 -1
- package/dist/CloudSensingManager.js +147 -20
- package/dist/CloudSensingManager.js.map +1 -1
- package/dist/EdgeAgent.js +1 -1
- package/dist/VideoSegmentUploader.d.ts +4 -0
- package/dist/VideoSegmentUploader.d.ts.map +1 -1
- package/dist/VideoSegmentUploader.js +21 -1
- package/dist/VideoSegmentUploader.js.map +1 -1
- package/dist/autoSense.d.ts +5 -4
- package/dist/autoSense.d.ts.map +1 -1
- package/dist/autoSense.js +24 -9
- package/dist/autoSense.js.map +1 -1
- package/dist/config.d.ts +9 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +15 -0
- package/dist/config.js.map +1 -1
- package/dist/diagnose.d.ts +1 -1
- package/dist/diagnose.js +1 -1
- package/dist/sensingRunMode.d.ts +8 -0
- package/dist/sensingRunMode.d.ts.map +1 -1
- package/dist/sensingRunMode.js +59 -11
- package/dist/sensingRunMode.js.map +1 -1
- package/package.json +5 -4
- package/retained/CapturedTheftProfile.cjs +83 -0
- package/retained/ExperimentalTheftConnector.cjs +392 -0
- package/retained/FrameTheftRuntime.cjs +111 -0
- package/retained/FrameTheftWorker.py +136 -0
- package/retained/InstalledTheftLifecycle.cjs +165 -0
- package/retained/InstalledTheftPackage.cjs +91 -0
- package/retained/LiveTheftBinding.cjs +55 -0
- package/retained/ObservedReviewCaseEvidence.py +188 -0
- package/retained/ObservedSameWristPolicy.py +205 -0
- package/retained/OrderedPerceptionDelivery.cjs +72 -0
- package/retained/PairedTheftRoute.cjs +402 -0
- package/retained/ReplayRpc.cjs +157 -0
- package/retained/RetainedCaptureClient.cjs +63 -16
- package/retained/SavedTheftSession.cjs +110 -0
- package/retained/TheftCaseContract.cjs +36 -0
- package/retained/TheftCaseDelivery.cjs +81 -0
- package/retained/TheftDeliveryPump.cjs +41 -0
- package/retained/TheftMovementOwner.py +234 -0
- package/retained/TheftReviewPolicy.cjs +8 -0
- package/retained/__tests__/paired-theft-manager.test.cjs +509 -0
- package/retained/__tests__/support/live-shelf-api.cjs +139 -0
- package/retained/__tests__/support/load-sdk-sources.cjs +41 -0
- package/retained/__tests__/support/loopback-proxy.cjs +33 -0
- package/retained/__tests__/support/pinned-api-loader.cjs +11 -0
- package/retained/__tests__/support/theft-builder.cjs +61 -0
- package/retained/__tests__/support/theft-packet-fixture.cjs +58 -0
- package/retained/__tests__/support/theft-readiness.cjs +51 -0
- package/retained/__tests__/test_live_theft_input.py +103 -0
- package/retained/__tests__/theft-case-api.test.cjs +135 -0
- package/retained/__tests__/theft-ci-regression.test.cjs +92 -0
- package/retained/__tests__/theft-config.test.cjs +84 -0
- package/retained/__tests__/theft-entry.test.cjs +76 -0
- package/retained/__tests__/theft-native-recovery.test.cjs +90 -0
- package/retained/__tests__/theft-readiness.test.cjs +164 -0
- package/retained/manager.cjs +61 -7
- package/retained/pilot-profile-client.cjs +4 -0
|
@@ -0,0 +1,392 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// Explicit private entry point. The ordinary 237 manager never imports this.
|
|
3
|
+
const assert = require('node:assert/strict');
|
|
4
|
+
const fs = require('node:fs');
|
|
5
|
+
const path = require('node:path');
|
|
6
|
+
const crypto = require('node:crypto');
|
|
7
|
+
const {RetainedCaptureClient} = require('./RetainedCaptureClient.cjs');
|
|
8
|
+
const {ActorPerceptionClient} = require('../dist/ActorPerception');
|
|
9
|
+
const {actorProofHash} = require('../dist/ActorTracking');
|
|
10
|
+
const {HUMAN_ONLY,OPTIONAL_GEMMA}=require('./TheftReviewPolicy.cjs');
|
|
11
|
+
const RF = '8e8197112112b79d7cc5d0a94ecef659c05317a658aadc7c7643374c539d7615';
|
|
12
|
+
const POSE = '9aeb635b83f86aea45cf45d85798f7eba1a162de8e0d721c44e54fe5eebaf47d';
|
|
13
|
+
const sha = b => crypto.createHash('sha256').update(b).digest('hex');
|
|
14
|
+
const ref = f => [f.sourceIndex, f.capturedAtMs, f.sha256];
|
|
15
|
+
const clone = structuredClone;
|
|
16
|
+
const LIMIT = 90000, COUNT = 13;
|
|
17
|
+
|
|
18
|
+
/** Python JSON can carry -0.0; JavaScript durable JSON writes 0. Normalize
|
|
19
|
+
* that finite representation at the lab RPC boundary, before both the actor
|
|
20
|
+
* proof and sidecar enter the unchanged exact save/read and receipt checks.
|
|
21
|
+
* Reject all other lossy values; never call getters or custom serializers. */
|
|
22
|
+
function nativeJsonReply(value) {
|
|
23
|
+
const ancestors = new Set();
|
|
24
|
+
function copy(v, depth = 0) {
|
|
25
|
+
assert(depth <= 128, 'Native perception JSON nesting exceeds bound');
|
|
26
|
+
if (v === null || typeof v === 'string' || typeof v === 'boolean') return v;
|
|
27
|
+
if (typeof v === 'number') {
|
|
28
|
+
assert(Number.isFinite(v), 'Native perception JSON requires finite numbers');
|
|
29
|
+
return Object.is(v, -0) ? 0 : v;
|
|
30
|
+
}
|
|
31
|
+
assert(v && typeof v === 'object', 'Native perception JSON cannot omit or coerce values');
|
|
32
|
+
const array = Array.isArray(v);
|
|
33
|
+
assert(Object.getPrototypeOf(v) === (array ? Array.prototype : Object.prototype),
|
|
34
|
+
'Native perception JSON requires plain objects and arrays');
|
|
35
|
+
assert(!ancestors.has(v), 'Native perception JSON cannot contain cycles');
|
|
36
|
+
ancestors.add(v);
|
|
37
|
+
const descriptors = Object.getOwnPropertyDescriptors(v), keys = Reflect.ownKeys(descriptors);
|
|
38
|
+
if (array) assert(keys.length === v.length + 1 &&
|
|
39
|
+
keys.every(k => k === 'length' || typeof k === 'string' && /^(0|[1-9][0-9]*)$/.test(k) && Number(k) < v.length),
|
|
40
|
+
'Native perception JSON arrays cannot have holes or extra properties');
|
|
41
|
+
const result = array ? [] : {};
|
|
42
|
+
for (const key of keys) {
|
|
43
|
+
if (array && key === 'length') continue;
|
|
44
|
+
const d = descriptors[key];
|
|
45
|
+
assert(typeof key === 'string' && d.enumerable && Object.hasOwn(d, 'value'),
|
|
46
|
+
'Native perception JSON requires enumerable string data properties');
|
|
47
|
+
Object.defineProperty(result, key, {value: copy(d.value, depth + 1), enumerable: true, writable: true, configurable: true});
|
|
48
|
+
}
|
|
49
|
+
ancestors.delete(v); return result;
|
|
50
|
+
}
|
|
51
|
+
const normalized = copy(value), text = JSON.stringify(normalized);
|
|
52
|
+
assert(text !== undefined, 'Structured native perception JSON required');
|
|
53
|
+
const saved = JSON.parse(text);
|
|
54
|
+
assert.deepEqual(saved, normalized, 'Native perception JSON must survive exact storage');
|
|
55
|
+
return saved;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function commit(file, value) {
|
|
59
|
+
const bytes = Buffer.from(JSON.stringify(value));
|
|
60
|
+
if (fs.existsSync(file)) {
|
|
61
|
+
assert(fs.readFileSync(file).equals(bytes), 'Conflicting immutable private receipt');
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
const fd = fs.openSync(file, 'wx', 0o600);
|
|
65
|
+
try { fs.writeFileSync(fd, bytes); fs.fsyncSync(fd); } finally { fs.closeSync(fd); }
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
class PrivateAlertReceiver {
|
|
69
|
+
constructor(directory) {
|
|
70
|
+
fs.mkdirSync(directory, {recursive: true}); this.directory = directory;
|
|
71
|
+
}
|
|
72
|
+
receive(packet) {
|
|
73
|
+
assert(packet.customerMessageSent === false && packet.pictures.length === COUNT);
|
|
74
|
+
const file = path.join(this.directory, packet.receipt.caseId + '-' + packet.receipt.snapshotAnchorMs + '.json');
|
|
75
|
+
commit(file, packet);
|
|
76
|
+
const saved = JSON.parse(fs.readFileSync(file));
|
|
77
|
+
assert.deepEqual(saved, packet, 'Private alert readback differs');
|
|
78
|
+
return {accepted: true, file, sha256: sha(fs.readFileSync(file)), customerMessageSent: false};
|
|
79
|
+
}
|
|
80
|
+
savedReceiptNames() {
|
|
81
|
+
const names = fs.readdirSync(this.directory);
|
|
82
|
+
assert(names.every(name => /^[a-f0-9]{64}-(?:[0-9]+|opinion)\.json$/.test(name)), 'Unrecognized private receipt file');
|
|
83
|
+
return names;
|
|
84
|
+
}
|
|
85
|
+
savedPackets() {
|
|
86
|
+
return this.savedReceiptNames().filter(name => /^[a-f0-9]{64}-[0-9]+\.json$/.test(name)).sort().map(name => {
|
|
87
|
+
const packet = JSON.parse(fs.readFileSync(path.join(this.directory, name)));
|
|
88
|
+
assert.equal(name, packet.receipt.caseId + '-' + packet.receipt.snapshotAnchorMs + '.json', 'Saved alert filename changed');
|
|
89
|
+
return packet;
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
savedOpinions() {
|
|
93
|
+
return new Map(this.savedReceiptNames().filter(name => /^[a-f0-9]{64}-opinion\.json$/.test(name))
|
|
94
|
+
.map(name => [name.slice(0, 64), JSON.parse(fs.readFileSync(path.join(this.directory, name)))]));
|
|
95
|
+
}
|
|
96
|
+
opinion(id, outcome) {
|
|
97
|
+
assert(/^[a-f0-9]{64}$/.test(id));
|
|
98
|
+
commit(path.join(this.directory, id + '-opinion.json'), outcome);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** Uses real 237 RF/tracking validation. S pose stays an explicitly labelled
|
|
103
|
+
* experimental sidecar; never masquerades as production's selected M model. */
|
|
104
|
+
class LabPerceiver extends ActorPerceptionClient {
|
|
105
|
+
constructor(scope, rpc) {
|
|
106
|
+
super({version: 1, cameraId: scope.cameraId, sourceId: scope.sourceId,
|
|
107
|
+
minConfidence: .1, detector: {modelId: 'rfdetr_medium', sha256: RF}});
|
|
108
|
+
this.scope = scope; this.rpc = rpc; this.frames = new Map(); this.sidecars = new Map();
|
|
109
|
+
}
|
|
110
|
+
async perceive(input, policy = this.policy) {
|
|
111
|
+
const frame = this.frames.get(input.frameId);
|
|
112
|
+
assert(frame && frame.sha256 === sha(input.jpeg) && frame.capturedAtMs === input.capturedAtMs);
|
|
113
|
+
const wire = nativeJsonReply(await this.rpc.call('detect', {image_b64: input.jpeg.toString('base64'),
|
|
114
|
+
expected_model: policy.detector, classes: ['person'], min_confidence: .1,
|
|
115
|
+
tracking: {version: 1, cameraId: policy.cameraId, sourceId: policy.sourceId,
|
|
116
|
+
frameId: input.frameId, capturedAtMs: input.capturedAtMs},
|
|
117
|
+
native_scope: this.scope, native_frame: frame}));
|
|
118
|
+
const side = wire.lab_sidecar, bind = side?.inputBinding;
|
|
119
|
+
assert(bind && bind.testOnly === false, 'Actual native execution required');
|
|
120
|
+
for (const k of ['cameraId', 'sourceId']) assert.equal(bind[k], this.scope[k]);
|
|
121
|
+
assert.equal(bind.frameId, input.frameId); assert.equal(bind.capturedAtMs, input.capturedAtMs);
|
|
122
|
+
assert.equal(bind.imageSha256, frame.sha256); assert.equal(bind.poseModelSha256, POSE);
|
|
123
|
+
assert.equal(bind.trackingSha256, wire.tracking.sha256);
|
|
124
|
+
assert.deepEqual(bind.nativeScope, this.scope);
|
|
125
|
+
assert.deepEqual(bind.modelHashes, {rfdetr_medium: RF});
|
|
126
|
+
const result = this.validate({version: 1, policySha256: actorProofHash(policy),
|
|
127
|
+
contextSha256: input.contextSha256, result: {detections: wire.detections,
|
|
128
|
+
inferenceMs: wire.inference_ms, model: wire.model, modelHashes: wire.model_hashes,
|
|
129
|
+
tracking: wire.tracking}}, input, policy);
|
|
130
|
+
assert(!this.sidecars.has(input.frameId), 'Duplicate native inference');
|
|
131
|
+
this.sidecars.set(input.frameId, clone(side)); this.frames.delete(input.frameId);
|
|
132
|
+
return result;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const OBSERVED_TIMING=Object.freeze({version:'observed_capture_clock/v1',maximumGapMs:750,minimumSpanMs:500,maximumSpanMs:1500});
|
|
137
|
+
|
|
138
|
+
function validatePacket(packet, scope) {
|
|
139
|
+
const r = packet.receipt;
|
|
140
|
+
const observedTiming=r.captureTimingPolicy!==undefined;
|
|
141
|
+
if(observedTiming)assert.deepEqual(r.captureTimingPolicy,OBSERVED_TIMING,'Unknown capture timing policy');
|
|
142
|
+
assert.deepEqual(packet.scope, scope);
|
|
143
|
+
assert(/^[a-f0-9]{64}$/.test(r.caseId) && /^[a-f0-9]{64}$/.test(r.actor));
|
|
144
|
+
assert(Number.isSafeInteger(r.firstCaptureMs) && r.deadlineMs === r.firstCaptureMs + LIMIT);
|
|
145
|
+
assert(['open_case', 'add_case_evidence'].includes(r.kind));
|
|
146
|
+
assert(packet.pictures.length === COUNT && packet.observations.length === COUNT);
|
|
147
|
+
assert.equal(new Set(packet.pictures.map(p => p.sha256)).size, COUNT);
|
|
148
|
+
assert.deepEqual(r.pictures, packet.pictures.map(ref));
|
|
149
|
+
assert.equal(r.snapshotAnchorMs, packet.pictures.at(-1).capturedAtMs);
|
|
150
|
+
const keys = new Set(r.pictures.map(p => JSON.stringify(p)));
|
|
151
|
+
packet.pictures.forEach((p, i) => {
|
|
152
|
+
assert.equal(sha(Buffer.from(p.jpegBase64, 'base64')), p.sha256);
|
|
153
|
+
if(observedTiming){
|
|
154
|
+
assert(Number.isSafeInteger(p.capturedAtMs)&&Number.isSafeInteger(p.sourceIndex)&&p.sourceIndex>=0);
|
|
155
|
+
if(i){const previous=packet.pictures[i-1],gap=p.capturedAtMs-previous.capturedAtMs;
|
|
156
|
+
assert(gap>0&&gap<=OBSERVED_TIMING.maximumGapMs,'Observed picture gap exceeds native timing policy');
|
|
157
|
+
assert(p.sourceIndex>previous.sourceIndex,'Observed capture order must advance');}
|
|
158
|
+
}else if(i)assert.equal(p.capturedAtMs-packet.pictures[i-1].capturedAtMs,500);
|
|
159
|
+
const o = packet.observations[i];
|
|
160
|
+
assert.deepEqual(o.captureTimingPolicy,r.captureTimingPolicy,'Receipt/observation capture timing mismatch');
|
|
161
|
+
assert.equal(o.inputBinding.imageSha256, p.sha256);
|
|
162
|
+
assert.equal(o.inputBinding.capturedAtMs, p.capturedAtMs);
|
|
163
|
+
assert.deepEqual(o.inputBinding.nativeScope, scope);
|
|
164
|
+
assert.equal(o.inputBinding.poseModelSha256, POSE);
|
|
165
|
+
assert(['initial', 'stable_hypothesis'].includes(o.cameraEvidence.guard));
|
|
166
|
+
assert.equal(o.cameraEvidence.trackerResetDiagnostic, false);
|
|
167
|
+
assert.notEqual(o.cameraEvidence.resetMovementEvidence, true);
|
|
168
|
+
});
|
|
169
|
+
assert(Array.isArray(r.episodes) && r.episodes.length > 0);
|
|
170
|
+
for (const e of r.episodes) {
|
|
171
|
+
assert(['left', 'right'].includes(e.side) && e.originMs < e.arrivalMs);
|
|
172
|
+
if(observedTiming)assert(e.arrivalMs-e.originMs>=OBSERVED_TIMING.minimumSpanMs,'Observed movement is below the native minimum span');
|
|
173
|
+
assert(e.support.length >= 2 && e.support.every(p => keys.has(JSON.stringify(p))));
|
|
174
|
+
assert.equal(e.support[0][1], e.originMs); assert.equal(e.support.at(-1)[1], e.arrivalMs);
|
|
175
|
+
}
|
|
176
|
+
assert.equal(packet.customerMessageSent, false);
|
|
177
|
+
return packet;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** Consumes an existing camera owner's durable originals and perception.
|
|
181
|
+
* No camera, decoder, model process, capture queue or archive is created here.
|
|
182
|
+
* One instance belongs to one captured profile generation/context. */
|
|
183
|
+
class ExperimentalTheftEvidence {
|
|
184
|
+
constructor({directory, scope, environment, archive, receiver, reviewer = null, now = Date.now,
|
|
185
|
+
captureContext, profileBinding, movementDirectory = path.join(directory, 'movement')}) {
|
|
186
|
+
assert(scope.profileGeneration >= 1);
|
|
187
|
+
if (scope.sourceKind === 'live_shelf_review_v1') {
|
|
188
|
+
assert(environment.kind === scope.sourceKind && scope.sourceId === environment.sourceId
|
|
189
|
+
&& scope.cameraId === environment.cameraId && !Object.hasOwn(scope, 'sourceVideoSha256'),
|
|
190
|
+
'Live evidence requires its verified camera identity');
|
|
191
|
+
} else {
|
|
192
|
+
assert.equal(scope.sourceVideoSha256, environment.sourceVideoSha256);
|
|
193
|
+
}
|
|
194
|
+
assert.equal(scope.regionConfigurationSha256, environment.regionConfigurationSha256);
|
|
195
|
+
assert(archive instanceof RetainedCaptureClient, 'Use the existing retained camera archive');
|
|
196
|
+
this.capturedProfile = profileBinding === undefined ? null
|
|
197
|
+
: new (require('./CapturedTheftProfile.cjs').CapturedTheftProfile)(archive, scope, profileBinding, captureContext);
|
|
198
|
+
this.reviewMode=this.capturedProfile?.reviewMode??OPTIONAL_GEMMA;
|
|
199
|
+
assert(this.reviewMode!==HUMAN_ONLY||reviewer===null,'Human-only evidence must not carry a reviewer');
|
|
200
|
+
this.archiveCameraId = this.capturedProfile?.binding.archiveCameraId ?? scope.cameraId;
|
|
201
|
+
assert.equal(archive.cameraId, this.archiveCameraId, 'Camera archive belongs to another camera');
|
|
202
|
+
assert.equal(archive.intervalMs, 500, 'Preserve the existing capture interval');
|
|
203
|
+
assert(archive.actorPerceiver && typeof archive.actorPerceiver.validate === 'function',
|
|
204
|
+
'The camera owner must supply its original perception validator');
|
|
205
|
+
this.scope = clone(scope); this.environment = clone(environment); this.now = now;
|
|
206
|
+
this.captureContext = clone(captureContext ?? {version: 1,
|
|
207
|
+
privateProfile: 'experimental_theft_same_wrist', scope});
|
|
208
|
+
this.receiver = receiver; this.reviewer = reviewer; this.directory = directory;
|
|
209
|
+
this.archive = archive; this.movementDirectory = movementDirectory;
|
|
210
|
+
fs.mkdirSync(movementDirectory, {recursive: true});
|
|
211
|
+
this.rows = []; this.pending = new Map(); this.delivered = new Set();
|
|
212
|
+
this.closed = false; this.failed = false; this.lastAccepted = null;
|
|
213
|
+
this.reviewStarts = path.join(directory, 'review-starts');
|
|
214
|
+
this.recoveredReceipts = new Map(); this.recoveredInterrupted = new Set(); this.restoredSession = false;
|
|
215
|
+
this.stats = {frames: 0, movementCues: [], alerts: 0, attachments: 0, expired: 0,
|
|
216
|
+
opinion: {}, originalPictures: COUNT, queueInclusiveLimitMs: LIMIT};
|
|
217
|
+
}
|
|
218
|
+
acceptCapture(frameId, metadata, side) {
|
|
219
|
+
assert(!this.closed, 'Evidence consumer is closed');
|
|
220
|
+
assert(!this.failed, 'Evidence consumer failed; preserve its error before recovery');
|
|
221
|
+
try { return this.acceptDurable(frameId, metadata, side); }
|
|
222
|
+
catch (error) { this.failed = true; throw error; }
|
|
223
|
+
}
|
|
224
|
+
readDurable(frameId, metadata, side) {
|
|
225
|
+
assert(/^[a-f0-9]{32}$/.test(frameId), 'Original archive frame ID required');
|
|
226
|
+
assert(metadata && metadata.jpegBase64 === undefined, 'Read pictures from the camera archive only');
|
|
227
|
+
const row = this.archive.recordFor(frameId);
|
|
228
|
+
assert(row && row.id === frameId && row.cameraId === this.archiveCameraId, 'Original camera record required');
|
|
229
|
+
const disk = JSON.parse(fs.readFileSync(this.archive.file(frameId)));
|
|
230
|
+
for (const key of ['id', 'cameraId', 'capturedAt', 'imageSha256', 'imageBytes',
|
|
231
|
+
'actorPerceptionPolicy', 'actorPerception']) assert.deepEqual(row[key], disk[key], 'Original durable ' + key + ' changed');
|
|
232
|
+
assert.deepEqual(row.candidate.reviewContext, disk.candidate.reviewContext, 'Original durable context changed');
|
|
233
|
+
if (this.capturedProfile) {
|
|
234
|
+
this.capturedProfile.assertContext(disk.candidate.reviewContext, disk.capturedAt);
|
|
235
|
+
this.capturedProfile.assertPolicy(disk.actorPerceptionPolicy);
|
|
236
|
+
} else assert.deepEqual(disk.candidate.reviewContext, this.captureContext, 'Captured profile context changed');
|
|
237
|
+
assert(disk.actorPerception && disk.actorPerceptionPolicy, 'Original perception is not yet durable');
|
|
238
|
+
const input = this.archive.perceptionInput(row);
|
|
239
|
+
const native = this.archive.actorPerceiver.validate(disk.actorPerception, input, disk.actorPerceptionPolicy);
|
|
240
|
+
assert.equal(metadata.capturedAtMs, row.capturedAt, 'Original capture time changed');
|
|
241
|
+
assert.equal(metadata.sha256, row.imageSha256, 'Original picture reference changed');
|
|
242
|
+
assert.deepEqual([metadata.width, metadata.height], this.environment.frameSize);
|
|
243
|
+
assert(Number.isSafeInteger(metadata.sourceIndex) && metadata.sourceIndex >= 0, 'Original source index required');
|
|
244
|
+
const bind = side?.inputBinding;
|
|
245
|
+
assert(bind && bind.testOnly === false, 'Actual native execution binding required');
|
|
246
|
+
assert.deepEqual(bind.nativeScope, this.scope, 'Native scope changed');
|
|
247
|
+
for (const key of ['cameraId', 'sourceId']) assert.equal(bind[key], this.scope[key]);
|
|
248
|
+
assert.equal(bind.frameId, frameId); assert.equal(bind.capturedAtMs, row.capturedAt);
|
|
249
|
+
assert.equal(bind.imageSha256, row.imageSha256); assert.equal(bind.poseModelSha256, POSE);
|
|
250
|
+
assert.equal(bind.trackingSha256, native.result.tracking.sha256);
|
|
251
|
+
assert.deepEqual(bind.modelHashes, {rfdetr_medium: RF});
|
|
252
|
+
const original = input.jpeg; assert.equal(sha(original), metadata.sha256);
|
|
253
|
+
const meta = clone(metadata), observation = clone(side);
|
|
254
|
+
const fingerprint = sha(Buffer.from(JSON.stringify({metadata: meta, side: observation})));
|
|
255
|
+
return {row, meta, observation, original, fingerprint};
|
|
256
|
+
}
|
|
257
|
+
recoverSavedSession(consumerId = 'private_theft_same_wrist_v1') {
|
|
258
|
+
try { return require('./SavedTheftSession.cjs').recover(this, validatePacket, consumerId, commit); }
|
|
259
|
+
catch (error) {
|
|
260
|
+
this.failed = true;
|
|
261
|
+
const record = require('./OrderedPerceptionDelivery.cjs').errorRecord(error);
|
|
262
|
+
try { commit(path.join(this.directory, 'recovery-error-' + crypto.randomUUID() + '.json'), record); }
|
|
263
|
+
catch (writeError) { throw new AggregateError([error, writeError], 'Recovery and diagnostic persistence failed'); }
|
|
264
|
+
throw error;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
reviewStart(packet) {
|
|
268
|
+
return {version: 1, scope: this.scope, caseId: packet.receipt.caseId,
|
|
269
|
+
snapshotAnchorMs: packet.receipt.snapshotAnchorMs, originalDeadlineMs: packet.receipt.deadlineMs,
|
|
270
|
+
packetSha256: sha(Buffer.from(JSON.stringify(packet)))};
|
|
271
|
+
}
|
|
272
|
+
acceptDurable(frameId, metadata, side) {
|
|
273
|
+
const {row, meta, observation, original, fingerprint} = this.readDurable(frameId, metadata, side);
|
|
274
|
+
if (this.lastAccepted?.frameId === frameId) {
|
|
275
|
+
assert.equal(fingerprint, this.lastAccepted.fingerprint, 'Previously consumed evidence changed');
|
|
276
|
+
return {...this.lastAccepted.result, duplicate: true};
|
|
277
|
+
}
|
|
278
|
+
assert(!this.lastAccepted || row.capturedAt > this.lastAccepted.capturedAtMs, 'Capture order must advance');
|
|
279
|
+
const rows = [...this.rows, {row, frame: {...meta, jpegBase64: original.toString('base64')}, side: observation}].slice(-COUNT);
|
|
280
|
+
const packets = observation.movementObservation.caseEvidenceReceipts.map(receipt => validatePacket({
|
|
281
|
+
receipt, scope: this.scope, environment: this.environment,
|
|
282
|
+
pictures: rows.map(p => p.frame), observations: rows.map(p => p.side),
|
|
283
|
+
message: this.reviewMode===HUMAN_ONLY?'Possible item movement—please review.':
|
|
284
|
+
'Movement from the marked shelf toward upper clothing. Please review the pictures. This is not a confirmed theft.',
|
|
285
|
+
customerMessageSent: false, opinionRole: this.reviewMode===HUMAN_ONLY?'not_requested_human_review':'optional_second_opinion_no_veto'}, this.scope));
|
|
286
|
+
commit(path.join(this.movementDirectory, frameId + '-movement.json'), observation);
|
|
287
|
+
this.rows = rows; this.stats.frames++;
|
|
288
|
+
this.stats.movementCues.push(...observation.movementObservation.cues.map(c => ({sourceIndex: meta.sourceIndex,
|
|
289
|
+
side: c.side, route: c.support.movementConfirmation.route, cueId: c.id})));
|
|
290
|
+
for (const packet of packets) {
|
|
291
|
+
const receipt = packet.receipt, key = receipt.caseId + ':' + receipt.snapshotAnchorMs;
|
|
292
|
+
if (this.delivered.has(key)) {
|
|
293
|
+
const restored = this.recoveredReceipts.get(key);
|
|
294
|
+
assert(restored, 'Duplicate case evidence');
|
|
295
|
+
assert.deepEqual(packet, restored, 'Previously delivered recovery packet changed');
|
|
296
|
+
continue;
|
|
297
|
+
}
|
|
298
|
+
if (this.now() >= receipt.deadlineMs) { this.stats.expired++; continue; }
|
|
299
|
+
const ack = this.receiver.receive(packet); assert.equal(ack.accepted, true);
|
|
300
|
+
this.delivered.add(key);
|
|
301
|
+
if (receipt.kind === 'open_case') { this.stats.alerts++;
|
|
302
|
+
if(this.reviewMode!==HUMAN_ONLY)this.pending.set(receipt.caseId, packet); }
|
|
303
|
+
else this.stats.attachments++;
|
|
304
|
+
}
|
|
305
|
+
const result = {frameId, cues: observation.movementObservation.cues.length};
|
|
306
|
+
this.lastAccepted = {frameId, capturedAtMs: row.capturedAt, fingerprint, result};
|
|
307
|
+
return result;
|
|
308
|
+
}
|
|
309
|
+
async reviewOne({cameraBusy = false} = {}) {
|
|
310
|
+
assert(!this.closed); const next = this.pending.entries().next(); if (next.done) return null;
|
|
311
|
+
const [id, packet] = next.value, now = this.now();
|
|
312
|
+
if (cameraBusy && now < packet.receipt.deadlineMs) return {status: 'paused_for_camera'};
|
|
313
|
+
fs.mkdirSync(this.reviewStarts, {recursive: true});
|
|
314
|
+
commit(path.join(this.reviewStarts, id + '.json'), this.reviewStart(packet));
|
|
315
|
+
this.pending.delete(id);
|
|
316
|
+
let outcome;
|
|
317
|
+
if (now >= packet.receipt.deadlineMs) outcome = {status: 'expired', originalDeadlineMs: packet.receipt.deadlineMs};
|
|
318
|
+
else if (this.recoveredInterrupted.has(id)) outcome = {status: 'unavailable',
|
|
319
|
+
reason: 'review_interrupted_before_saved_outcome', originalDeadlineMs: packet.receipt.deadlineMs,
|
|
320
|
+
nativeReviewStarted: false, engineeringFailure: true, previousNativeCleanup: 'unverified'};
|
|
321
|
+
else if (!this.reviewer) outcome = {status: 'unavailable', reason: 'reviewer_not_admitted'};
|
|
322
|
+
else {
|
|
323
|
+
// The reviewer owns cancellation/cleanup; never cancel by losing a Promise.
|
|
324
|
+
try { outcome = await this.reviewer.review(packet, {deadlineMs: packet.receipt.deadlineMs}); }
|
|
325
|
+
catch (error) {
|
|
326
|
+
const record = e => ({name: e?.name ?? 'Error', message: e?.message ?? String(e),
|
|
327
|
+
code: e?.code ?? null, stack: e?.stack ?? null});
|
|
328
|
+
const expired = error?.code === 'GEMMA_REVIEW_EXPIRED' || this.now() >= packet.receipt.deadlineMs;
|
|
329
|
+
outcome = {status: expired ? 'expired' : 'unavailable',
|
|
330
|
+
reason: expired ? 'review_deadline_expired' : 'reviewer_failed',
|
|
331
|
+
originalDeadlineMs: packet.receipt.deadlineMs,
|
|
332
|
+
engineeringFailure: !!error?.cleanupError || !['GEMMA_REVIEW_EXPIRED', 'GEMMA_UNAVAILABLE'].includes(error?.code),
|
|
333
|
+
diagnostics: {primary: record(error), cleanup: error?.cleanup ?? null,
|
|
334
|
+
cleanupError: error?.cleanupError ? record(error.cleanupError) : null}};
|
|
335
|
+
}
|
|
336
|
+
if (this.now() >= packet.receipt.deadlineMs && outcome.status === 'completed')
|
|
337
|
+
outcome = {status: 'expired', reason: 'late_opinion_not_presented'};
|
|
338
|
+
}
|
|
339
|
+
this.receiver.opinion(id, {...outcome, primaryAlertUnchanged: true, canVeto: false});
|
|
340
|
+
this.stats.opinion[outcome.status] = (this.stats.opinion[outcome.status] ?? 0) + 1;
|
|
341
|
+
return outcome;
|
|
342
|
+
}
|
|
343
|
+
close() { assert.equal(this.pending.size, 0, 'Resolve queued second opinions before close'); this.closed = true; }
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/** Existing private replay entry, using the same evidence consumer as an
|
|
347
|
+
* externally owned archive. The ordinary production manager is unchanged. */
|
|
348
|
+
class ExperimentalTheftConnector extends ExperimentalTheftEvidence {
|
|
349
|
+
constructor({directory, scope, environment, rpc, receiver, reviewer = null, now = Date.now, maxCaptures = 300}) {
|
|
350
|
+
const perceiver = new LabPerceiver(scope, rpc), deliveryMetadata = new Map();
|
|
351
|
+
const {receipt} = require('./OrderedPerceptionDelivery.cjs');
|
|
352
|
+
let consumer;
|
|
353
|
+
const perceptionSink = {id: 'private_theft_same_wrist_v1',
|
|
354
|
+
handlesContext: context => context?.privateProfile === 'experimental_theft_same_wrist',
|
|
355
|
+
prepare({record}) {
|
|
356
|
+
const metadata = deliveryMetadata.get(record.id), side = perceiver.sidecars.get(record.id);
|
|
357
|
+
assert(metadata && side, 'Exact original movement payload required before handoff');
|
|
358
|
+
return {metadata: clone(metadata), side: clone(side)};
|
|
359
|
+
},
|
|
360
|
+
deliver({record, payload, payloadSha256}) {
|
|
361
|
+
assert(consumer, 'Evidence owner must exist before handoff');
|
|
362
|
+
consumer.acceptCapture(record.id, payload.metadata, payload.side);
|
|
363
|
+
return receipt(perceptionSink.id, record.id, payloadSha256);
|
|
364
|
+
}};
|
|
365
|
+
const archive = new RetainedCaptureClient({getEndpoint: () => 'http://127.0.0.1:1/private-unused'},
|
|
366
|
+
path.join(directory, 'captures'), {cameraId: scope.cameraId, intervalMs: 500,
|
|
367
|
+
maxCaptures, maxBytes: 100 * 1024 ** 2, actorPerceiver: perceiver, perceptionSink});
|
|
368
|
+
super({directory, scope, environment, archive, receiver, reviewer, now,
|
|
369
|
+
movementDirectory: path.join(directory, 'captures')});
|
|
370
|
+
this.perceiver = perceiver; this.deliveryMetadata = deliveryMetadata; consumer = this;
|
|
371
|
+
this.recovery = this.recoverSavedSession();
|
|
372
|
+
}
|
|
373
|
+
async capture(frame) {
|
|
374
|
+
assert(!this.closed && !this.failed, 'Private replay evidence consumer is closed or failed');
|
|
375
|
+
assert(!this.restoredSession, 'Recovered session may finish saved deliveries only; new input requires a fresh tracking epoch');
|
|
376
|
+
const {jpegBase64, ...meta} = frame;
|
|
377
|
+
const jpeg = Buffer.from(jpegBase64, 'base64'); assert.equal(sha(jpeg), frame.sha256);
|
|
378
|
+
assert.deepEqual([frame.width, frame.height], this.environment.frameSize);
|
|
379
|
+
const candidate = {name: this.scope.cameraId, enqueuedAtMs: frame.capturedAtMs, jpeg,
|
|
380
|
+
reviewContext: clone(this.captureContext)};
|
|
381
|
+
this.archive.retainOrderedCapture(candidate);
|
|
382
|
+
const row = this.archive.previousCaptureRecord;
|
|
383
|
+
assert.equal(row.capturedAt, frame.capturedAtMs);
|
|
384
|
+
this.perceiver.frames.set(row.id, meta); this.deliveryMetadata.set(row.id, meta);
|
|
385
|
+
assert.equal(await this.archive.perceiveNextOrderedCapture(), true);
|
|
386
|
+
assert.equal(this.lastAccepted?.frameId, row.id, 'The exact analyzed frame must finish handoff');
|
|
387
|
+
this.perceiver.sidecars.delete(row.id); this.deliveryMetadata.delete(row.id);
|
|
388
|
+
return this.lastAccepted.result;
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
module.exports = {ExperimentalTheftConnector, ExperimentalTheftEvidence, PrivateAlertReceiver, LabPerceiver, validatePacket, RF, POSE};
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const assert=require('node:assert/strict');
|
|
3
|
+
const fs=require('node:fs'),path=require('node:path'),crypto=require('node:crypto');
|
|
4
|
+
const {ReplayRpc,nativeErrorDetails:details}=require('./ReplayRpc.cjs');
|
|
5
|
+
const OBSERVED_TIMING=Object.freeze({version:'observed_capture_clock/v1',maximumGapMs:750,minimumSpanMs:500,maximumSpanMs:1500});
|
|
6
|
+
const RF='8e8197112112b79d7cc5d0a94ecef659c05317a658aadc7c7643374c539d7615';
|
|
7
|
+
const POSE='9aeb635b83f86aea45cf45d85798f7eba1a162de8e0d721c44e54fe5eebaf47d';
|
|
8
|
+
const hash=raw=>crypto.createHash('sha256').update(raw).digest('hex');
|
|
9
|
+
|
|
10
|
+
/** Explicit frame-only owner for PairedTheftRoute's runtime interface.
|
|
11
|
+
* File/model provenance is checked, never discovered or installed. Final
|
|
12
|
+
* package transitive-dependency and interpreter qualification remain the
|
|
13
|
+
* launcher's job; a protocol handshake is not a native-performance claim. */
|
|
14
|
+
class FrameTheftRuntime {
|
|
15
|
+
constructor({command,workerFile,configFile,configSha256,pinnedFiles,directory,reviewer=null}) {
|
|
16
|
+
assert([command,workerFile,configFile,directory].every(p=>typeof p==='string'&&path.isAbsolute(p)),
|
|
17
|
+
'Explicit absolute interpreter, worker, configuration and owned output paths required');
|
|
18
|
+
assert(Array.isArray(pinnedFiles)&&pinnedFiles.length>=5,'Complete direct worker/source pins required');
|
|
19
|
+
this.spec={command,workerFile,configFile,configSha256,pinnedFiles:structuredClone(pinnedFiles),directory};
|
|
20
|
+
this.state='prepared';this.transport=null;this.rpc=this;this.reviewer=reviewer;
|
|
21
|
+
this.closePromise=null;this.primaryError=null;this.cleanupResult=null;this.cleanupError=null;
|
|
22
|
+
this.verifyFiles();
|
|
23
|
+
fs.mkdirSync(directory,{recursive:false});this.save();
|
|
24
|
+
}
|
|
25
|
+
verifyFiles() {
|
|
26
|
+
const s=this.spec,pins=new Map();
|
|
27
|
+
for(const pin of s.pinnedFiles){
|
|
28
|
+
assert(pin&&Object.keys(pin).sort().join(',')==='path,sha256'&&path.isAbsolute(pin.path)&&/^[a-f0-9]{64}$/.test(pin.sha256));
|
|
29
|
+
assert(!pins.has(pin.path),'Duplicate worker source pin');
|
|
30
|
+
assert(fs.lstatSync(pin.path).isFile()&&!fs.lstatSync(pin.path).isSymbolicLink(),'Regular pinned worker source required');
|
|
31
|
+
assert.equal(hash(fs.readFileSync(pin.path)),pin.sha256,'Frame runtime source changed: '+pin.path);pins.set(pin.path,pin.sha256);
|
|
32
|
+
}
|
|
33
|
+
assert(pins.has(s.workerFile)&&pins.has(path.join(path.dirname(s.workerFile),'TheftMovementOwner.py')),'Worker and movement-owner pins required');
|
|
34
|
+
const raw=fs.readFileSync(s.configFile);assert(raw.length<=1024**2&&hash(raw)===s.configSha256,'Frame runtime configuration changed');
|
|
35
|
+
const config=JSON.parse(raw);
|
|
36
|
+
assert([1,2,3].includes(config.version)&&Object.keys(config).sort().join(',')===(config.version>=2?'captureTimingPolicy,nativeConfig,sources,version':'nativeConfig,sources,version'));
|
|
37
|
+
if(config.version>=2)assert.deepEqual(config.captureTimingPolicy,OBSERVED_TIMING,'Frozen observed timing policy required');
|
|
38
|
+
assert.deepEqual(Object.keys(config.sources).sort(),['case','native','wrist']);
|
|
39
|
+
for(const source of Object.values(config.sources))assert.equal(pins.get(source.path),source.sha256,'Native source must be included in the verified pin set');
|
|
40
|
+
const c=config.nativeConfig;
|
|
41
|
+
assert(c.frameRate===2&&c.rfdetrThreads===4&&c.poseThreads===2,'Frozen cadence/RF/pose settings changed');
|
|
42
|
+
assert(Number.isSafeInteger(c.maxCameras)&&c.maxCameras>0&&Object.keys(c.cameraEnvironments).length===c.maxCameras);
|
|
43
|
+
this.configuration=config;
|
|
44
|
+
}
|
|
45
|
+
save() {
|
|
46
|
+
const output={state:this.state,workerFile:this.spec.workerFile,configSha256:this.spec.configSha256,
|
|
47
|
+
nativeReady:this.nativeReady??null,primaryError:this.primaryError,cleanup:this.cleanupResult,cleanupError:this.cleanupError,
|
|
48
|
+
mediaReader:false,reviewPictures:13,queueInclusiveReviewLimitMs:90000};
|
|
49
|
+
const target=path.join(this.spec.directory,'FRAME-RUNTIME.json'),temporary=target+'.tmp';
|
|
50
|
+
const fd=fs.openSync(temporary,'w',0o600);
|
|
51
|
+
try{fs.writeFileSync(fd,JSON.stringify(output,null,2)+'\n');fs.fsyncSync(fd);}finally{fs.closeSync(fd);}
|
|
52
|
+
fs.renameSync(temporary,target);
|
|
53
|
+
}
|
|
54
|
+
async start({availableBytes}) {
|
|
55
|
+
assert(this.state==='prepared'&&!this.transport,'Frame runtime starts once');
|
|
56
|
+
assert(Number.isSafeInteger(availableBytes)&&availableBytes>=4500000000,'RF/pose requires 4.5 GB free before startup');
|
|
57
|
+
try{
|
|
58
|
+
// Recheck final bytes immediately before starting the owned interpreter.
|
|
59
|
+
this.verifyFiles();this.state='starting';this.save();
|
|
60
|
+
const s=this.spec;
|
|
61
|
+
this.transport=new ReplayRpc(s.command,['-B',s.workerFile,'--config',s.configFile,'--config-sha256',s.configSha256],
|
|
62
|
+
{cwd:path.dirname(s.workerFile),errorFile:path.join(s.directory,'frame-worker.stderr.log')});
|
|
63
|
+
const ready=await this.transport.next();
|
|
64
|
+
assert(ready.sameWristLab===true&&ready.collarCheckLab===true&&ready.inputMode==='caller_supplied_original_frames'&&ready.mediaReader===false,
|
|
65
|
+
'Actual frame-only movement protocol required');
|
|
66
|
+
assert(ready.nativeOwners?.detectorInstances===1&&ready.nativeOwners?.poseInstances===1,'Exactly one RF and pose owner required');
|
|
67
|
+
assert.deepEqual(ready.nativeOwners.modelHashes,{rfdetr_medium:RF,old_lab_pose:POSE},'Frozen RF and pose identities required');
|
|
68
|
+
assert.deepEqual(ready.captureTimingPolicy,this.configuration.captureTimingPolicy,'Configured/native capture timing mismatch');
|
|
69
|
+
this.nativeReady=structuredClone(ready);this.state='ready';this.save();return this.nativeReady;
|
|
70
|
+
}catch(error){return this.retireAfter(error);}
|
|
71
|
+
}
|
|
72
|
+
describeOriginal({record,input}) {
|
|
73
|
+
const metadata=record?.candidate?.privateOriginalFrame;
|
|
74
|
+
assert(record?.candidate?.privateOriginalFrameKind==='captured_archive_ordinal/v1'&&metadata&&Object.keys(metadata).sort().join(',')==='capturedAtMs,height,sceneId,sha256,sourceIndex,sourceTimeMs,width',
|
|
75
|
+
'Original captured-frame metadata is missing; do not invent a video position or clock');
|
|
76
|
+
assert(record.id===input.frameId&&record.capturedAt===input.capturedAtMs&&metadata.capturedAtMs===record.capturedAt,
|
|
77
|
+
'Exact original source metadata required; capture identity or clock changed');
|
|
78
|
+
assert(metadata.sha256===record.imageSha256&&hash(input.jpeg)===record.imageSha256,'Original frame bytes changed');
|
|
79
|
+
assert(metadata.sourceTimeMs===null&&['sourceIndex','sceneId','capturedAtMs','width','height'].every(k=>Number.isSafeInteger(metadata[k])));
|
|
80
|
+
assert(metadata.sourceIndex>=0&&metadata.sceneId>=0&&metadata.width>0&&metadata.height>0);
|
|
81
|
+
return structuredClone(metadata);
|
|
82
|
+
}
|
|
83
|
+
async call(method,body) {
|
|
84
|
+
assert(this.state==='ready'&&this.transport,'Frame runtime is not ready or is retired');
|
|
85
|
+
assert(['detect','/movement13'].includes(method),'Frame runtime cannot read videos or run unlisted operations');
|
|
86
|
+
try{return await this.transport.call(method,body);}
|
|
87
|
+
catch(error){return this.retireAfter(error);}
|
|
88
|
+
}
|
|
89
|
+
async retireAfter(error) {
|
|
90
|
+
this.primaryError??=details(error);this.state='failed';
|
|
91
|
+
try{await this.close();}catch(cleanupError){error.cleanupError=details(cleanupError);}
|
|
92
|
+
this.state='failed';
|
|
93
|
+
try{this.save();}catch(saveError){throw new AggregateError([error,saveError],'Frame failure and failure-report save error',{cause:error});}
|
|
94
|
+
throw error;
|
|
95
|
+
}
|
|
96
|
+
close() {
|
|
97
|
+
if(!this.closePromise)this.closePromise=this.closeOwned();
|
|
98
|
+
return this.closePromise;
|
|
99
|
+
}
|
|
100
|
+
async closeOwned() {
|
|
101
|
+
this.state='closing';
|
|
102
|
+
try{
|
|
103
|
+
this.cleanupResult=this.transport?await this.transport.close():{exited:true,started:false};
|
|
104
|
+
this.state=this.primaryError?'failed':'closed';this.save();return this.cleanupResult;
|
|
105
|
+
}catch(error){this.cleanupError=details(error);this.state='failed';
|
|
106
|
+
try{this.save();}catch(saveError){throw new AggregateError([error,saveError],'Frame cleanup and report-save failure',{cause:error});}
|
|
107
|
+
throw error;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
module.exports={FrameTheftRuntime,OBSERVED_TIMING};
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
"""Private frame-only native launcher. No dataset reader or model downloader.
|
|
2
|
+
|
|
3
|
+
The package/config verifier remains responsible for the complete dependency
|
|
4
|
+
closure. This entry additionally pins the three direct runtime sources and
|
|
5
|
+
preserves the existing RF/pose settings. Native startup is not locally proven.
|
|
6
|
+
"""
|
|
7
|
+
import argparse
|
|
8
|
+
import contextlib
|
|
9
|
+
import hashlib
|
|
10
|
+
import importlib.util
|
|
11
|
+
import json
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
import re
|
|
14
|
+
import sys
|
|
15
|
+
|
|
16
|
+
from TheftMovementOwner import (movement_owner_type, serve_frames, timing_contract, validate_native_timing,
|
|
17
|
+
OBSERVED_TIMING, LIVE_KIND, LiveMovementAdapter, prepare_live_cameras)
|
|
18
|
+
|
|
19
|
+
RF = '8e8197112112b79d7cc5d0a94ecef659c05317a658aadc7c7643374c539d7615'
|
|
20
|
+
POSE = '9aeb635b83f86aea45cf45d85798f7eba1a162de8e0d721c44e54fe5eebaf47d'
|
|
21
|
+
MODULES = {'native': 'arm_area_native_perception', 'case': 'review_case_evidence', 'wrist': 'same_wrist_policy'}
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def read_configuration(filename, expected_hash):
|
|
25
|
+
raw = Path(filename).read_bytes()
|
|
26
|
+
if len(raw) > 1024 * 1024 or not re.fullmatch('[a-f0-9]{64}', expected_hash):
|
|
27
|
+
raise ValueError('Bounded hash-pinned frame-worker configuration required')
|
|
28
|
+
if hashlib.sha256(raw).hexdigest() != expected_hash:
|
|
29
|
+
raise ValueError('Frame-worker configuration hash changed')
|
|
30
|
+
config = json.loads(raw)
|
|
31
|
+
fields = {'version', 'nativeConfig', 'sources'}
|
|
32
|
+
if config.get('version') in (2, 3):
|
|
33
|
+
fields.add('captureTimingPolicy')
|
|
34
|
+
if config.get('captureTimingPolicy') != OBSERVED_TIMING:
|
|
35
|
+
raise ValueError('Explicit unchanged observed timing policy required')
|
|
36
|
+
if set(config) != fields or type(config.get('version')) is not int or config['version'] not in (1, 2, 3):
|
|
37
|
+
raise ValueError('Explicit frame-worker configuration required')
|
|
38
|
+
native = config['nativeConfig']
|
|
39
|
+
keys = {'labRoot', 'journalPath', 'frameRate', 'maxCameras', 'rfdetrThreads', 'poseThreads', 'cameraEnvironments'}
|
|
40
|
+
if not isinstance(native, dict) or set(native) != keys:
|
|
41
|
+
raise ValueError('Complete unchanged native settings required')
|
|
42
|
+
if any(type(native[k]) is not int or native[k] != v
|
|
43
|
+
for k, v in {'frameRate': 2, 'rfdetrThreads': 4, 'poseThreads': 2}.items()):
|
|
44
|
+
raise ValueError('Existing RF/pose/cadence settings changed')
|
|
45
|
+
if not isinstance(native['cameraEnvironments'], dict) or not native['cameraEnvironments']:
|
|
46
|
+
raise ValueError('Explicit original camera environment required')
|
|
47
|
+
if type(native['maxCameras']) is not int or native['maxCameras'] != len(native['cameraEnvironments']):
|
|
48
|
+
raise ValueError('Camera capacity must match configured environments')
|
|
49
|
+
if config['version'] == 3:
|
|
50
|
+
prepare_live_cameras(native)
|
|
51
|
+
elif any(isinstance(row, dict) and row.get('kind') == LIVE_KIND for row in native['cameraEnvironments'].values()):
|
|
52
|
+
raise ValueError('Live camera input requires a version 3 frame configuration')
|
|
53
|
+
if any(not isinstance(native[k], str) or not Path(native[k]).is_absolute() for k in ('labRoot', 'journalPath')):
|
|
54
|
+
raise ValueError('Explicit absolute lab and owned journal paths required')
|
|
55
|
+
if not isinstance(config['sources'], dict) or set(config['sources']) != set(MODULES):
|
|
56
|
+
raise ValueError('All three runtime source pins required')
|
|
57
|
+
return config
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
@contextlib.contextmanager
|
|
61
|
+
def runtime_types(sources):
|
|
62
|
+
"""Verify every direct source before executing any; restore import state."""
|
|
63
|
+
verified = {}
|
|
64
|
+
for kind in MODULES:
|
|
65
|
+
pin = sources[kind]
|
|
66
|
+
if not isinstance(pin, dict) or set(pin) != {'path', 'sha256'}:
|
|
67
|
+
raise ValueError('Exact runtime path/hash pair required')
|
|
68
|
+
file = Path(pin['path'])
|
|
69
|
+
if not file.is_absolute() or file.is_symlink() or not re.fullmatch('[a-f0-9]{64}', pin['sha256']):
|
|
70
|
+
raise ValueError('Explicit regular pinned runtime source required')
|
|
71
|
+
raw = file.read_bytes()
|
|
72
|
+
if len(raw) > 1024 * 1024 or hashlib.sha256(raw).hexdigest() != pin['sha256']:
|
|
73
|
+
raise ValueError('Runtime source changed: ' + kind)
|
|
74
|
+
verified[kind] = (file, raw)
|
|
75
|
+
old_path = sys.path[:]
|
|
76
|
+
old_modules = {name: sys.modules.get(name) for name in MODULES.values()}
|
|
77
|
+
try:
|
|
78
|
+
sys.path[:0] = list(dict.fromkeys(str(file.parent) for file, _ in verified.values()))
|
|
79
|
+
loaded = {}
|
|
80
|
+
for kind, name in MODULES.items():
|
|
81
|
+
file, raw = verified[kind]
|
|
82
|
+
spec = importlib.util.spec_from_file_location(name, file)
|
|
83
|
+
module = importlib.util.module_from_spec(spec)
|
|
84
|
+
sys.modules[name] = module
|
|
85
|
+
# Execute the exact bytes that were hashed, not a later file read.
|
|
86
|
+
exec(compile(raw, str(file), 'exec'), module.__dict__)
|
|
87
|
+
loaded[kind] = module
|
|
88
|
+
native = loaded['native']
|
|
89
|
+
if native.RF_SHA256 != RF or native.POSE_SHA256 != POSE:
|
|
90
|
+
raise ValueError('Native model pins differ from the frozen pipeline')
|
|
91
|
+
yield native.NativePerception, loaded['case'].ReviewCaseEvidence, loaded['wrist'].SameWristLab
|
|
92
|
+
finally:
|
|
93
|
+
sys.path[:] = old_path
|
|
94
|
+
for name, old in old_modules.items():
|
|
95
|
+
if old is None:
|
|
96
|
+
sys.modules.pop(name, None)
|
|
97
|
+
else:
|
|
98
|
+
sys.modules[name] = old
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def run(config, input_stream, output_stream, error_stream):
|
|
102
|
+
# Hold the verified import context for the full owned lifetime. Model-source
|
|
103
|
+
# imports/inference may refer to these modules after construction.
|
|
104
|
+
native_config = config['nativeConfig']
|
|
105
|
+
bindings = None
|
|
106
|
+
if config['version'] == 3:
|
|
107
|
+
native_config, bindings = prepare_live_cameras(native_config)
|
|
108
|
+
with contextlib.redirect_stdout(error_stream), runtime_types(config['sources']) as types:
|
|
109
|
+
if timing_contract(types[1], types[2]) != config.get('captureTimingPolicy'):
|
|
110
|
+
raise ValueError('Configured source timing contract mismatch before model startup')
|
|
111
|
+
def create_owner():
|
|
112
|
+
import cv2
|
|
113
|
+
import psutil
|
|
114
|
+
if psutil.virtual_memory().available < 4_500_000_000:
|
|
115
|
+
raise RuntimeError('Native RF/pose admission needs 4.5 GB free')
|
|
116
|
+
cv2.setNumThreads(2)
|
|
117
|
+
owner = movement_owner_type(*types)(native_config)
|
|
118
|
+
return LiveMovementAdapter(owner, bindings) if bindings else owner
|
|
119
|
+
def validate_owner(owner):
|
|
120
|
+
if owner.test_only or owner.native_owners['modelHashes'] != {'rfdetr_medium': RF, 'old_lab_pose': POSE}:
|
|
121
|
+
raise RuntimeError('Actual frozen native RF/pose owners were not established')
|
|
122
|
+
validate_native_timing(owner, config.get('captureTimingPolicy'))
|
|
123
|
+
return serve_frames(create_owner, input_stream, output_stream, error_stream, validate_owner=validate_owner)
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def main():
|
|
127
|
+
parser = argparse.ArgumentParser()
|
|
128
|
+
parser.add_argument('--config', required=True)
|
|
129
|
+
parser.add_argument('--config-sha256', required=True)
|
|
130
|
+
args = parser.parse_args()
|
|
131
|
+
config = read_configuration(args.config, args.config_sha256)
|
|
132
|
+
run(config, sys.stdin, sys.stdout, sys.stderr)
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
if __name__ == '__main__':
|
|
136
|
+
main()
|