@promptdriven/runtime-client 1.0.0 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/runtime/agent-chat.mjs +55 -8
- package/src/runtime/index.mjs +41 -1
- package/src/runtime/live-local-bridge.mjs +178 -0
- package/src/runtime/live-local.mjs +181 -0
- package/src/runtime/run-program.mjs +798 -0
- package/src/runtime/runtime-bindings.mjs +28 -18
- package/src/runtime/runtime-check-plan.mjs +19 -27
- package/src/runtime/runtime-check-reconcile.mjs +328 -0
- package/src/runtime/runtime-client.mjs +2797 -345
- package/src/runtime/runtime-outcome.mjs +66 -2
- package/src/runtime/runtime-output.mjs +202 -15
- package/src/runtime/runtime-preflight.mjs +994 -87
- package/src/runtime/runtime-slots.mjs +75 -4
- package/src/runtime/runtime-stream.mjs +125 -0
- package/src/runtime/scenario-json.mjs +167 -1
- package/src/runtime/secret-redaction.mjs +3 -2
- package/src/runtime/try-doors.mjs +426 -59
- package/src/runtime/try-http.mjs +49 -15
- package/src/runtime/try-update-policy.mjs +43 -0
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Generated from prompts/cli/runtime-bindings.prompt.md (sha256:
|
|
1
|
+
// Generated from prompts/cli/runtime-bindings.prompt.md (sha256: cfbb0656bad59b35043dfcc88ad843a90f907a05da99980046d9085514891d85).
|
|
2
2
|
import {loadRuntimeWorkspace,allowsRuntimePath} from './runtime-workspace.mjs';
|
|
3
3
|
import { lstatSync } from 'node:fs';
|
|
4
4
|
import { join } from 'node:path';
|
|
@@ -10,20 +10,21 @@ export function bindingCoverage({ root, found, selected, includeRuntime = false,
|
|
|
10
10
|
const runtime = includeRuntime ? {runtime:runtimeCoverage({root,allowMissingInterfaces})} : {};
|
|
11
11
|
const unavailable = { available: false, bound: null, total: null, modules: [], ...runtime };
|
|
12
12
|
const fail = (path, why) => { throw Object.assign(new PreflightError(4, `pdd-bindings.json: ${path}: ${why}`), { bindingInventory: true }); };
|
|
13
|
-
const regular = path => {
|
|
14
|
-
if (typeof path !== 'string' || !path || path.includes('\\') || path.startsWith('/') || path.split('/').some(p => !p || p === '.' || p === '..')) fail(String(path), 'use an existing repository-relative regular Git-visible path');
|
|
13
|
+
const regular = (path, control = false) => {
|
|
14
|
+
if (typeof path !== 'string' || !path || path.includes('\\') || path.startsWith('/') || path.split('/').some(p => !p || p === '.' || p === '..' || p.toLowerCase() === '.git')) fail(String(path), 'use an existing repository-relative regular Git-visible path');
|
|
15
15
|
if (isSecretPath(path)) fail(path, 'secret-looking paths are not allowed');
|
|
16
|
+
if (!control && !allowsRuntimePath(workspace, path)) fail(path, 'outside pdd-workspace.json allowed_paths');
|
|
16
17
|
let full = root;
|
|
17
18
|
for (const part of path.split('/')) {
|
|
18
19
|
full = join(full, part);
|
|
19
20
|
let stat; try { stat = lstatSync(full); } catch { fail(path, 'file does not exist'); }
|
|
20
21
|
if (stat.isSymbolicLink()) fail(path, 'symlinks are not allowed');
|
|
21
22
|
}
|
|
22
|
-
if (!lstatSync(full).isFile() || !(
|
|
23
|
+
if (!lstatSync(full).isFile() || !(control ? controlVisible : found.visible).has(path)) fail(path, 'must be a regular Git-visible file');
|
|
23
24
|
return path;
|
|
24
25
|
};
|
|
25
26
|
try { lstatSync(join(root, 'pdd-bindings.json')); } catch (error) { if (error.code === 'ENOENT') return unavailable; throw error; }
|
|
26
|
-
regular('pdd-bindings.json');
|
|
27
|
+
regular('pdd-bindings.json', true);
|
|
27
28
|
const files = sourceFilePort(root);
|
|
28
29
|
if (files.stat({path:'pdd-bindings.json'}).size > 65536) fail('inventory', 'exceeds 64 KiB');
|
|
29
30
|
let inventory; try { inventory = JSON.parse(files.read({path:'pdd-bindings.json'}).content); } catch { fail('inventory', 'invalid JSON'); }
|
|
@@ -32,28 +33,30 @@ export function bindingCoverage({ root, found, selected, includeRuntime = false,
|
|
|
32
33
|
if (!keys(inventory, ['schema_version', 'modules']) || inventory.schema_version !== 'pdd.bindings.v1' || !Array.isArray(inventory.modules)) fail('inventory', 'expected schema_version pdd.bindings.v1 and modules array only');
|
|
33
34
|
if (inventory.modules.length > 1000) fail('inventory', 'exceeds 1000 module declarations');
|
|
34
35
|
const stories = new Set(found.stories.map(s => s.path)), chosen = new Set(selected.map(t => t.storyPath)), seen = new Set(), modules = [];
|
|
35
|
-
const reached = new Set(),
|
|
36
|
+
const reached = new Set(), context = new Set(), visited = new Set();
|
|
37
|
+
const queue = selected.flatMap(t => (Array.isArray(t.doc?.steps) ? t.doc.steps : []).map(s => ({path:s?.entry_path, executable:true})));
|
|
36
38
|
while (queue.length) {
|
|
37
|
-
const path = queue.shift()
|
|
38
|
-
|
|
39
|
+
const {path,executable} = queue.shift(), identity = `${executable ? 'call' : 'context'}:${path}`;
|
|
40
|
+
if (typeof path !== 'string' || visited.has(identity)) continue;
|
|
41
|
+
visited.add(identity);
|
|
42
|
+
(executable ? reached : context).add(path);
|
|
39
43
|
const prompt = readPrompt(root, found.visible, path, workspace);
|
|
40
|
-
for (const call of Array.isArray(prompt?.interface?.calls) ? prompt.interface.calls : []) if (typeof call?.target === 'string') queue.push(call.target);
|
|
41
|
-
|
|
42
|
-
for (const target of promptMarkdownTargets(prompt.text, path, found.visible)) queue.push(target);
|
|
43
|
-
}
|
|
44
|
+
for (const call of Array.isArray(prompt?.interface?.calls) ? prompt.interface.calls : []) if (typeof call?.target === 'string') queue.push({path:call.target,executable});
|
|
45
|
+
for (const target of promptMarkdownTargets(prompt.text, path, found.visible)) queue.push({path:target,executable:false});
|
|
44
46
|
}
|
|
45
47
|
for (const item of inventory.modules) {
|
|
46
48
|
const path = item?.module ?? 'module';
|
|
47
49
|
if (!object(item)) fail(path, 'expected module declaration');
|
|
48
50
|
const bound = Object.hasOwn(item, 'prompt');
|
|
49
|
-
|
|
50
|
-
if(!
|
|
51
|
-
regular(item.module); if (seen.has(item.module)) fail(path, 'duplicate module'); seen.add(item.module);
|
|
52
|
-
|
|
53
|
-
|
|
51
|
+
const hasStories = Object.hasOwn(item, 'stories');
|
|
52
|
+
if (!keys(item, bound ? (hasStories ? ['module', 'stories', 'prompt'] : ['module', 'prompt']) : ['module', 'stories', 'reason', 'port_attempt'])) fail(path, 'declare prompt or both reason and port_attempt, with no extra fields');
|
|
53
|
+
regular(item.module, true); if (seen.has(item.module)) fail(path, 'duplicate module'); seen.add(item.module);
|
|
54
|
+
const itemStories = bound && !hasStories ? [] : item.stories;
|
|
55
|
+
if (!Array.isArray(itemStories) || (!bound && !itemStories.length) || new Set(itemStories).size !== itemStories.length) fail(path, bound ? 'stories must be an array of unique Story paths' : 'stories must be nonempty and unique');
|
|
56
|
+
for (const story of itemStories) { regular(story); if (!stories.has(story)) fail(path, `unknown Story ${story}`); }
|
|
54
57
|
if (bound) { regular(item.prompt); if (!item.prompt.endsWith('.prompt.md')) fail(path, `${item.prompt} must be a *.prompt.md file`); }
|
|
55
58
|
else for (const key of ['reason', 'port_attempt']) if (typeof item[key] !== 'string' || !item[key].trim()) fail(path, `${key} must be nonblank`);
|
|
56
|
-
if (!
|
|
59
|
+
if (!itemStories.some(story => chosen.has(story))) continue;
|
|
57
60
|
if (bound) {
|
|
58
61
|
const prompt = readPrompt(root, found.visible, item.prompt, workspace);
|
|
59
62
|
const nativeProblem = (prompt.frontmatter || prompt?.interface?.kind === 'native-only') ? promptRefusal(prompt, 'interface') : null;
|
|
@@ -63,6 +66,13 @@ export function bindingCoverage({ root, found, selected, includeRuntime = false,
|
|
|
63
66
|
continue;
|
|
64
67
|
}
|
|
65
68
|
const lazyMissing = allowMissingInterfaces && prompt.error === 'interface-missing';
|
|
69
|
+
if ((lazyMissing && reached.has(item.prompt)) || (!reached.has(item.prompt) && context.has(item.prompt) && prompt.error === 'interface-missing')) {
|
|
70
|
+
const pending = reached.has(item.prompt);
|
|
71
|
+
modules.push({path:item.module,state:'unbound',prompt:item.prompt,
|
|
72
|
+
reason:pending ? 'Runtime interface pending' : 'Context-only module contract',
|
|
73
|
+
port_attempt:pending ? 'lazy interface synthesis pending' : 'no executable Runtime call'});
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
66
76
|
if (!reached.has(item.prompt) || (!lazyMissing && (prompt.error || !Array.isArray(prompt?.interface?.entries) || !prompt.interface.entries.length || prompt.interface.entries.some(entry => !entry || typeof entry.operation !== 'string' || !entry.operation.trim() || !object(entry.input_schema) || !object(entry.result_schema))))) fail(path, `${item.prompt} must be reached and have an executable Runtime interface`);
|
|
67
77
|
}
|
|
68
78
|
modules.push({ path: item.module, state: bound ? 'bound' : 'unbound', ...(bound ? {} : { reason: item.reason, port_attempt: item.port_attempt }) });
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
// Generated from prompts/cli/runtime-check-plan.prompt.md (sha256:
|
|
1
|
+
// Generated from prompts/cli/runtime-check-plan.prompt.md (sha256: 081b45f925041a79c1e7ecf6b9b36ff3c4b10166ae0faecccfc138c2c0904780).
|
|
2
2
|
import { spawnSync } from 'node:child_process';
|
|
3
3
|
import { isAbsolute, posix } from 'node:path';
|
|
4
|
-
import { captureSource, readPrompt, sourceFilePort,
|
|
4
|
+
import { captureSource, readPrompt, sourceFilePort, analyzeStepCallCost, hasSimulationBindings, LAZY_INTERFACE_SYNTHESIS_CALLS } from './runtime-preflight.mjs';
|
|
5
5
|
import { resultOutcome } from './runtime-outcome.mjs';
|
|
6
6
|
import { allowsRuntimePath, loadRuntimeWorkspace } from './runtime-workspace.mjs';
|
|
7
7
|
|
|
@@ -118,37 +118,23 @@ export function buildCheckPlan({ root, found, selected, rows = [], full, base =
|
|
|
118
118
|
const key = `${code}\0${path}\0${field}`;
|
|
119
119
|
if (!seenProblems.has(key)) { seenProblems.add(key); prerequisites.push({code,path,field}); }
|
|
120
120
|
};
|
|
121
|
-
const
|
|
122
|
-
if (stack.has(path)) { if (!proseOnly) problem('recursive-call-bound', path, '/calls'); return 0; }
|
|
121
|
+
const getPrompt = path => {
|
|
123
122
|
let prompt = prompts.get(path);
|
|
124
123
|
if (!prompt) {
|
|
125
124
|
try { prompt = readPrompt(root, found.visible, path, found.workspace); }
|
|
126
125
|
catch { prompt = {error:'interface-unavailable'}; }
|
|
127
126
|
prompts.set(path, prompt);
|
|
128
127
|
}
|
|
129
|
-
|
|
130
|
-
if (!iface) {
|
|
131
|
-
if (allowMissingInterfaces && prompt.error === 'interface-missing') {
|
|
132
|
-
const next = new Set([...stack, path]);
|
|
133
|
-
let count = 1;
|
|
134
|
-
for (const target of promptMarkdownTargets(prompt.text, path, found.visible)) count += cost(target, next, true);
|
|
135
|
-
return count;
|
|
136
|
-
}
|
|
137
|
-
problem(prompt.error ?? 'interface-missing', path, '/interface');
|
|
138
|
-
return 0;
|
|
139
|
-
}
|
|
140
|
-
if (iface.kind === 'native-only') return 0;
|
|
141
|
-
if (!Number.isSafeInteger(iface.limits?.max_model_calls) || iface.limits.max_model_calls < 1)
|
|
142
|
-
problem('undefined-call-bound', path, '/limits/max_model_calls');
|
|
143
|
-
const next = new Set([...stack, path]);
|
|
144
|
-
let count = 1 + (Array.isArray(iface.resources) ? iface.resources : []).reduce((sum, resource) => sum + (Array.isArray(resource.operations) ? resource.operations.length : 0), 0);
|
|
145
|
-
for (const [index, call] of (Array.isArray(iface.calls) ? iface.calls : []).entries()) {
|
|
146
|
-
if (!Number.isSafeInteger(call?.max_calls) || call.max_calls < 1) problem('undefined-call-bound', path, `/calls/${index}/max_calls`);
|
|
147
|
-
const nested = cost(call?.target, next);
|
|
148
|
-
if (Number.isSafeInteger(call?.max_calls) && call.max_calls >= 1 && nested > 0) count += call.max_calls * (nested + 1);
|
|
149
|
-
}
|
|
150
|
-
return count;
|
|
128
|
+
return prompt;
|
|
151
129
|
};
|
|
130
|
+
const cost = (path, { hasSimulationPorts = false, missingInterfaces = null } = {}) => analyzeStepCallCost(path, {
|
|
131
|
+
getPrompt,
|
|
132
|
+
visible: found.visible,
|
|
133
|
+
allowMissingInterfaces,
|
|
134
|
+
hasSimulationPorts,
|
|
135
|
+
onProblem: problem,
|
|
136
|
+
missingInterfaces,
|
|
137
|
+
});
|
|
152
138
|
const groups = new Map();
|
|
153
139
|
for (const test of selected) {
|
|
154
140
|
const key = test.storyPath ?? test.path;
|
|
@@ -254,9 +240,15 @@ export function buildCheckPlan({ root, found, selected, rows = [], full, base =
|
|
|
254
240
|
}
|
|
255
241
|
let estimated = 0;
|
|
256
242
|
const dispatched = new Set(selectCheckTests({stories},{affected}).tests), budgetRows = [];
|
|
243
|
+
const docsByTest = new Map(selected.map(test => [test.path, test.doc]));
|
|
257
244
|
for (const row of rows.filter(row=>!affected || dispatched.has(row.test))) {
|
|
258
245
|
// Inspect interfaces even when a different local rule won preflight precedence.
|
|
259
|
-
const
|
|
246
|
+
const doc = docsByTest.get(row.test), missingInterfaces = new Set();
|
|
247
|
+
const stepSum = (row.steps ?? []).reduce((sum, step) => sum + cost(step?.entry_path, {
|
|
248
|
+
hasSimulationPorts: hasSimulationBindings(doc, step),
|
|
249
|
+
missingInterfaces,
|
|
250
|
+
}), 0);
|
|
251
|
+
const count = stepSum + missingInterfaces.size * LAZY_INTERFACE_SYNTHESIS_CALLS;
|
|
260
252
|
if (row.state !== 'ready') continue;
|
|
261
253
|
const validBound = Number.isSafeInteger(row.max_calls) && row.max_calls >= 1;
|
|
262
254
|
if (!validBound) problem('undefined-call-bound',row.test,'/max_calls');
|
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
// Generated from prompts/cli/runtime-check-reconcile.prompt.md
|
|
2
|
+
const SCHEMA_VERSION = 'pdd-runtime.check-reconciliation.v1';
|
|
3
|
+
const VALID_STATES = new Set(['queued', 'running', 'passed', 'failed', 'refused', 'unknown']);
|
|
4
|
+
const TERMINAL_STATES = new Set(['passed', 'failed', 'refused', 'unknown']);
|
|
5
|
+
const VALID_DISPATCH = new Set(['not_started', 'dispatched', 'unproved', 'contradictory']);
|
|
6
|
+
const VALID_ACCOUNTING = new Set(['complete_zero', 'complete_dispatched', 'unknown']);
|
|
7
|
+
const VALID_REPORT_STATUS = new Set(['present', 'missing', 'invalid', 'expired']);
|
|
8
|
+
const VALID_REPORT_DISPATCH = new Set(['not_started', 'started', 'completed', 'unknown']);
|
|
9
|
+
const VALID_PREPARED_STATUS = new Set(['valid', 'absent', 'invalid']);
|
|
10
|
+
const VALID_LEDGER_STATUS = new Set(['trusted', 'absent', 'untrusted']);
|
|
11
|
+
const VALID_PROVISION_PHASES = new Set([null, 'never_provisioned', 'provisioning', 'dispatched', 'invalid']);
|
|
12
|
+
const VALID_CUSTODY = new Set([null, 'protected', 'invalid']);
|
|
13
|
+
const SAFE_CODE_RE = /^[a-z0-9][a-z0-9._:-]{0,95}$/;
|
|
14
|
+
|
|
15
|
+
const isPlainObject = value => value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
16
|
+
const isNonNegInt = value => Number.isSafeInteger(value) && value >= 0;
|
|
17
|
+
const boundedString = (value, max) => (typeof value === 'string' && value.length > 0 && value.length <= max ? value : null);
|
|
18
|
+
const safeCode = value => (typeof value === 'string' && SAFE_CODE_RE.test(value) ? value : null);
|
|
19
|
+
const safeCodeList = value => (Array.isArray(value) && value.every(item => safeCode(item) !== null) ? [...value] : null);
|
|
20
|
+
const safeSourceList = value =>
|
|
21
|
+
Array.isArray(value) && value.every(item => typeof item === 'string' && item.length > 0 && item.length <= 64 && !/[\\/]/.test(item))
|
|
22
|
+
? [...value]
|
|
23
|
+
: [];
|
|
24
|
+
|
|
25
|
+
const DEFAULT_UNKNOWN_ACTION =
|
|
26
|
+
'Retained evidence is unavailable, incomplete, or contradictory. Retain unknown usage and keep the retry block; do not replay the original request or release its reservation automatically.';
|
|
27
|
+
|
|
28
|
+
function unknownProjection({ request_id, status, error_code, contradiction, safe_next_action, body = null }) {
|
|
29
|
+
const disp = isPlainObject(body?.disposition) ? body.disposition : null;
|
|
30
|
+
const prov = isPlainObject(body?.provenance) ? body.provenance : null;
|
|
31
|
+
const ev = isPlainObject(body?.evidence) ? body.evidence : null;
|
|
32
|
+
const acct = isPlainObject(body?.accounting) ? body.accounting : null;
|
|
33
|
+
const state = disp && VALID_STATES.has(disp.state) ? disp.state : 'unknown';
|
|
34
|
+
const contradictions = safeCodeList(ev?.contradictions) ?? [];
|
|
35
|
+
if (contradiction && !contradictions.includes(contradiction)) contradictions.push(contradiction);
|
|
36
|
+
return {
|
|
37
|
+
schema_version: SCHEMA_VERSION,
|
|
38
|
+
read_only: true,
|
|
39
|
+
available: false,
|
|
40
|
+
http_status: Number.isSafeInteger(status) ? status : 0,
|
|
41
|
+
error_code,
|
|
42
|
+
request_id,
|
|
43
|
+
job_id: boundedString(body?.job_id, 36),
|
|
44
|
+
command: 'check',
|
|
45
|
+
disposition: {
|
|
46
|
+
state,
|
|
47
|
+
verdict: disp && TERMINAL_STATES.has(disp.verdict) ? disp.verdict : null,
|
|
48
|
+
terminal: TERMINAL_STATES.has(state),
|
|
49
|
+
refusal_code: safeCode(disp?.refusal_code),
|
|
50
|
+
created_at: boundedString(disp?.created_at, 64),
|
|
51
|
+
finished_at: boundedString(disp?.finished_at, 64),
|
|
52
|
+
expired: status === 410 || disp?.expired === true,
|
|
53
|
+
purged: disp?.purged === true,
|
|
54
|
+
},
|
|
55
|
+
dispatch: {
|
|
56
|
+
status: isPlainObject(body?.dispatch) && VALID_DISPATCH.has(body.dispatch.status) && body.dispatch.status === 'dispatched'
|
|
57
|
+
? 'dispatched'
|
|
58
|
+
: isPlainObject(body?.dispatch) && body.dispatch.status === 'contradictory'
|
|
59
|
+
? 'contradictory'
|
|
60
|
+
: 'unproved',
|
|
61
|
+
proven: isPlainObject(body?.dispatch) && body.dispatch.status === 'dispatched' && body.dispatch.proven === true,
|
|
62
|
+
},
|
|
63
|
+
accounting: {
|
|
64
|
+
status: 'unknown',
|
|
65
|
+
accounting_complete: false,
|
|
66
|
+
calls: 'unknown',
|
|
67
|
+
known_calls: isNonNegInt(acct?.known_calls) && acct.known_calls > 0 ? acct.known_calls : null,
|
|
68
|
+
unknown_calls: isNonNegInt(acct?.unknown_calls) && acct.unknown_calls > 0 ? acct.unknown_calls : 'unknown',
|
|
69
|
+
cap: isNonNegInt(acct?.cap) ? acct.cap : null,
|
|
70
|
+
},
|
|
71
|
+
retry: {
|
|
72
|
+
blocked: true,
|
|
73
|
+
safe_next_action: safe_next_action || DEFAULT_UNKNOWN_ACTION,
|
|
74
|
+
},
|
|
75
|
+
evidence: {
|
|
76
|
+
proof_basis: [],
|
|
77
|
+
contradictions,
|
|
78
|
+
ledger_row_present: ev?.ledger_row_present === true,
|
|
79
|
+
report_status: ev && VALID_REPORT_STATUS.has(ev.report_status) ? ev.report_status : (status === 410 ? 'expired' : 'missing'),
|
|
80
|
+
report_dispatch: VALID_REPORT_DISPATCH.has(ev?.report_dispatch) ? ev.report_dispatch : null,
|
|
81
|
+
report_accounting_complete: typeof ev?.report_accounting_complete === 'boolean' ? ev.report_accounting_complete : null,
|
|
82
|
+
prepared_receipt_status: ev && VALID_PREPARED_STATUS.has(ev.prepared_receipt_status) ? ev.prepared_receipt_status : 'absent',
|
|
83
|
+
provision_phase: ev && VALID_PROVISION_PHASES.has(ev.provision_phase) ? ev.provision_phase : null,
|
|
84
|
+
accounting_custody: ev && VALID_CUSTODY.has(ev.accounting_custody) ? ev.accounting_custody : null,
|
|
85
|
+
call_records_count: isNonNegInt(ev?.call_records_count) ? ev.call_records_count : 0,
|
|
86
|
+
call_ledger_status: ev && VALID_LEDGER_STATUS.has(ev.call_ledger_status) ? ev.call_ledger_status : 'absent',
|
|
87
|
+
call_ledger_started: typeof ev?.call_ledger_started === 'boolean' ? ev.call_ledger_started : null,
|
|
88
|
+
call_ledger_counts: null,
|
|
89
|
+
},
|
|
90
|
+
provenance: {
|
|
91
|
+
source_sha256: boundedString(prov?.source_sha256, 64),
|
|
92
|
+
server_commit: boundedString(prov?.server_commit, 128),
|
|
93
|
+
engine_sha: boundedString(prov?.engine_sha, 128),
|
|
94
|
+
sources_consulted: safeSourceList(prov?.sources_consulted),
|
|
95
|
+
},
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function projectCheckReconciliation({ request_id, status, body }) {
|
|
100
|
+
const reqId = boundedString(request_id, 36);
|
|
101
|
+
if (!reqId) {
|
|
102
|
+
throw Object.assign(new Error('A canonical request_id is required.'), { code: 'invalid-request-id' });
|
|
103
|
+
}
|
|
104
|
+
if (status === 404) {
|
|
105
|
+
return unknownProjection({
|
|
106
|
+
request_id: reqId,
|
|
107
|
+
status,
|
|
108
|
+
error_code: 'run-not-found',
|
|
109
|
+
contradiction: 'run-not-found',
|
|
110
|
+
safe_next_action: `No CHECK request "${reqId}" was found for this owner. Retain unknown usage and keep the retry block; do not replay or release reservations automatically.`,
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
if (status === 410) {
|
|
114
|
+
return unknownProjection({
|
|
115
|
+
request_id: reqId,
|
|
116
|
+
status,
|
|
117
|
+
error_code: 'result-expired',
|
|
118
|
+
contradiction: 'evidence-expired',
|
|
119
|
+
safe_next_action: `Retained evidence for CHECK "${reqId}" has expired. Retain unknown usage and keep the retry block; inspect a pre-expiry operator backup snapshot if available, and do not replay or release reservations automatically.`,
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
if (status !== 200 || !isPlainObject(body)) {
|
|
123
|
+
return unknownProjection({
|
|
124
|
+
request_id: reqId,
|
|
125
|
+
status,
|
|
126
|
+
error_code: 'reconciliation-unavailable',
|
|
127
|
+
contradiction: 'reconciliation-unavailable',
|
|
128
|
+
safe_next_action: `Reconciliation lookup for CHECK "${reqId}" is unavailable (HTTP ${status || 0}). Retain unknown usage and keep the retry block; do not replay or release reservations automatically.`,
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const jobId = boundedString(body.job_id, 36);
|
|
133
|
+
const disp = body.disposition;
|
|
134
|
+
const dispatch = body.dispatch;
|
|
135
|
+
const acct = body.accounting;
|
|
136
|
+
const retry = body.retry;
|
|
137
|
+
const ev = body.evidence;
|
|
138
|
+
const prov = body.provenance;
|
|
139
|
+
const baseValid =
|
|
140
|
+
body.schema_version === SCHEMA_VERSION &&
|
|
141
|
+
body.read_only === true &&
|
|
142
|
+
body.request_id === reqId &&
|
|
143
|
+
body.command === 'check' &&
|
|
144
|
+
jobId !== null &&
|
|
145
|
+
isPlainObject(disp) &&
|
|
146
|
+
VALID_STATES.has(disp.state) &&
|
|
147
|
+
typeof disp.terminal === 'boolean' &&
|
|
148
|
+
typeof disp.expired === 'boolean' &&
|
|
149
|
+
typeof disp.purged === 'boolean' &&
|
|
150
|
+
isPlainObject(dispatch) &&
|
|
151
|
+
VALID_DISPATCH.has(dispatch.status) &&
|
|
152
|
+
typeof dispatch.proven === 'boolean' &&
|
|
153
|
+
isPlainObject(acct) &&
|
|
154
|
+
VALID_ACCOUNTING.has(acct.status) &&
|
|
155
|
+
typeof acct.accounting_complete === 'boolean' &&
|
|
156
|
+
isPlainObject(retry) &&
|
|
157
|
+
typeof retry.blocked === 'boolean' &&
|
|
158
|
+
typeof retry.safe_next_action === 'string' &&
|
|
159
|
+
retry.safe_next_action.length > 0 &&
|
|
160
|
+
retry.safe_next_action.length <= 512 &&
|
|
161
|
+
isPlainObject(ev) &&
|
|
162
|
+
VALID_REPORT_STATUS.has(ev.report_status) &&
|
|
163
|
+
VALID_PREPARED_STATUS.has(ev.prepared_receipt_status) &&
|
|
164
|
+
VALID_LEDGER_STATUS.has(ev.call_ledger_status) &&
|
|
165
|
+
VALID_PROVISION_PHASES.has(ev.provision_phase) &&
|
|
166
|
+
VALID_CUSTODY.has(ev.accounting_custody) &&
|
|
167
|
+
isNonNegInt(ev.call_records_count) &&
|
|
168
|
+
isPlainObject(prov);
|
|
169
|
+
|
|
170
|
+
const proofBasis = baseValid ? safeCodeList(ev.proof_basis) : null;
|
|
171
|
+
const contradictions = baseValid ? safeCodeList(ev.contradictions) : null;
|
|
172
|
+
|
|
173
|
+
if (!baseValid || proofBasis === null || contradictions === null) {
|
|
174
|
+
return unknownProjection({
|
|
175
|
+
request_id: reqId,
|
|
176
|
+
status,
|
|
177
|
+
error_code: 'contradictory-reconciliation',
|
|
178
|
+
contradiction: 'invalid-reconciliation-payload',
|
|
179
|
+
safe_next_action: DEFAULT_UNKNOWN_ACTION,
|
|
180
|
+
body,
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
let ledgerCounts = null;
|
|
185
|
+
if (ev.call_ledger_counts !== null && ev.call_ledger_counts !== undefined) {
|
|
186
|
+
if (
|
|
187
|
+
!isPlainObject(ev.call_ledger_counts) ||
|
|
188
|
+
!isNonNegInt(ev.call_ledger_counts.known) ||
|
|
189
|
+
!isNonNegInt(ev.call_ledger_counts.unknown) ||
|
|
190
|
+
!isNonNegInt(ev.call_ledger_counts.unused)
|
|
191
|
+
) {
|
|
192
|
+
return unknownProjection({
|
|
193
|
+
request_id: reqId,
|
|
194
|
+
status,
|
|
195
|
+
error_code: 'contradictory-reconciliation',
|
|
196
|
+
contradiction: 'invalid-reconciliation-payload',
|
|
197
|
+
safe_next_action: DEFAULT_UNKNOWN_ACTION,
|
|
198
|
+
body,
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
ledgerCounts = {
|
|
202
|
+
known: ev.call_ledger_counts.known,
|
|
203
|
+
unknown: ev.call_ledger_counts.unknown,
|
|
204
|
+
unused: ev.call_ledger_counts.unused,
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
if (acct.status === 'complete_zero') {
|
|
209
|
+
const validZero =
|
|
210
|
+
disp.state === 'refused' &&
|
|
211
|
+
disp.terminal === true &&
|
|
212
|
+
disp.expired === false &&
|
|
213
|
+
disp.purged === false &&
|
|
214
|
+
dispatch.status === 'not_started' &&
|
|
215
|
+
dispatch.proven === true &&
|
|
216
|
+
acct.accounting_complete === true &&
|
|
217
|
+
acct.calls === 0 &&
|
|
218
|
+
acct.known_calls === 0 &&
|
|
219
|
+
acct.unknown_calls === 0 &&
|
|
220
|
+
ev.call_records_count === 0 &&
|
|
221
|
+
proofBasis.length > 0 &&
|
|
222
|
+
contradictions.length === 0;
|
|
223
|
+
if (!validZero) {
|
|
224
|
+
return unknownProjection({
|
|
225
|
+
request_id: reqId,
|
|
226
|
+
status,
|
|
227
|
+
error_code: 'contradictory-reconciliation',
|
|
228
|
+
contradiction: 'unproved-complete-zero',
|
|
229
|
+
safe_next_action: DEFAULT_UNKNOWN_ACTION,
|
|
230
|
+
body,
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
} else if (acct.status === 'complete_dispatched') {
|
|
234
|
+
const validDispatched =
|
|
235
|
+
disp.terminal === true &&
|
|
236
|
+
disp.expired === false &&
|
|
237
|
+
disp.purged === false &&
|
|
238
|
+
dispatch.status === 'dispatched' &&
|
|
239
|
+
dispatch.proven === true &&
|
|
240
|
+
acct.accounting_complete === true &&
|
|
241
|
+
isNonNegInt(acct.calls) &&
|
|
242
|
+
acct.known_calls === acct.calls &&
|
|
243
|
+
acct.unknown_calls === 0 &&
|
|
244
|
+
retry.blocked === true &&
|
|
245
|
+
proofBasis.length > 0 &&
|
|
246
|
+
contradictions.length === 0;
|
|
247
|
+
if (!validDispatched) {
|
|
248
|
+
return unknownProjection({
|
|
249
|
+
request_id: reqId,
|
|
250
|
+
status,
|
|
251
|
+
error_code: 'contradictory-reconciliation',
|
|
252
|
+
contradiction: 'unproved-dispatched-accounting',
|
|
253
|
+
safe_next_action: DEFAULT_UNKNOWN_ACTION,
|
|
254
|
+
body,
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
} else {
|
|
258
|
+
// Unknown accounting must always keep accounting_complete=false, calls='unknown', retry.blocked=true.
|
|
259
|
+
if (acct.accounting_complete !== false || acct.calls !== 'unknown' || retry.blocked !== true) {
|
|
260
|
+
return unknownProjection({
|
|
261
|
+
request_id: reqId,
|
|
262
|
+
status,
|
|
263
|
+
error_code: 'contradictory-reconciliation',
|
|
264
|
+
contradiction: 'contradictory-unknown-accounting',
|
|
265
|
+
safe_next_action: DEFAULT_UNKNOWN_ACTION,
|
|
266
|
+
body,
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
return {
|
|
272
|
+
schema_version: SCHEMA_VERSION,
|
|
273
|
+
read_only: true,
|
|
274
|
+
available: true,
|
|
275
|
+
http_status: 200,
|
|
276
|
+
error_code: null,
|
|
277
|
+
request_id: reqId,
|
|
278
|
+
job_id: jobId,
|
|
279
|
+
command: 'check',
|
|
280
|
+
disposition: {
|
|
281
|
+
state: disp.state,
|
|
282
|
+
verdict: TERMINAL_STATES.has(disp.verdict) ? disp.verdict : null,
|
|
283
|
+
terminal: disp.terminal,
|
|
284
|
+
refusal_code: safeCode(disp.refusal_code),
|
|
285
|
+
created_at: boundedString(disp.created_at, 64),
|
|
286
|
+
finished_at: boundedString(disp.finished_at, 64),
|
|
287
|
+
expired: disp.expired,
|
|
288
|
+
purged: disp.purged,
|
|
289
|
+
},
|
|
290
|
+
dispatch: {
|
|
291
|
+
status: dispatch.status,
|
|
292
|
+
proven: dispatch.proven,
|
|
293
|
+
},
|
|
294
|
+
accounting: {
|
|
295
|
+
status: acct.status,
|
|
296
|
+
accounting_complete: acct.accounting_complete,
|
|
297
|
+
calls: acct.calls,
|
|
298
|
+
known_calls: isNonNegInt(acct.known_calls) ? acct.known_calls : null,
|
|
299
|
+
unknown_calls: isNonNegInt(acct.unknown_calls) ? acct.unknown_calls : 'unknown',
|
|
300
|
+
cap: isNonNegInt(acct.cap) ? acct.cap : null,
|
|
301
|
+
},
|
|
302
|
+
retry: {
|
|
303
|
+
blocked: retry.blocked,
|
|
304
|
+
safe_next_action: retry.safe_next_action,
|
|
305
|
+
},
|
|
306
|
+
evidence: {
|
|
307
|
+
proof_basis: proofBasis,
|
|
308
|
+
contradictions,
|
|
309
|
+
ledger_row_present: ev.ledger_row_present === true,
|
|
310
|
+
report_status: ev.report_status,
|
|
311
|
+
report_dispatch: VALID_REPORT_DISPATCH.has(ev.report_dispatch) ? ev.report_dispatch : null,
|
|
312
|
+
report_accounting_complete: typeof ev.report_accounting_complete === 'boolean' ? ev.report_accounting_complete : null,
|
|
313
|
+
prepared_receipt_status: ev.prepared_receipt_status,
|
|
314
|
+
provision_phase: ev.provision_phase,
|
|
315
|
+
accounting_custody: ev.accounting_custody,
|
|
316
|
+
call_records_count: ev.call_records_count,
|
|
317
|
+
call_ledger_status: ev.call_ledger_status,
|
|
318
|
+
call_ledger_started: typeof ev.call_ledger_started === 'boolean' ? ev.call_ledger_started : null,
|
|
319
|
+
call_ledger_counts: ledgerCounts,
|
|
320
|
+
},
|
|
321
|
+
provenance: {
|
|
322
|
+
source_sha256: boundedString(prov.source_sha256, 64),
|
|
323
|
+
server_commit: boundedString(prov.server_commit, 128),
|
|
324
|
+
engine_sha: boundedString(prov.engine_sha, 128),
|
|
325
|
+
sources_consulted: safeSourceList(prov.sources_consulted),
|
|
326
|
+
},
|
|
327
|
+
};
|
|
328
|
+
}
|