@promptdriven/runtime-client 1.1.0 → 1.1.2

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.
@@ -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
+ }