@principles/codex-adapter 0.14.0 → 0.15.0
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.
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { TargetTurnEvidenceRead } from '@principles/core/runtime';
|
|
1
2
|
import { type PainReceiptRecoveryOutcome } from '@principles/core/runtime';
|
|
2
3
|
/** The MCP tool id this fallback processes (the server gate lives in the
|
|
3
4
|
* codec parser — exact match on both namespace segments, never a pattern). */
|
|
@@ -25,12 +26,38 @@ export interface PainReceiptFallbackInput {
|
|
|
25
26
|
readonly minimalMetadataGranted: boolean;
|
|
26
27
|
readonly now?: () => Date;
|
|
27
28
|
}
|
|
29
|
+
/**
|
|
30
|
+
* PRI-1273 (A2): the structured target-turn evidence read the worker's
|
|
31
|
+
* waiting-task reconcile consumes. This is the EXACT-turn authority for the
|
|
32
|
+
* Codex host boundary:
|
|
33
|
+
*
|
|
34
|
+
* 1. the source-material consent ledger is consulted FIRST (PRI-1283 R2
|
|
35
|
+
* parity with the submit path): a missing / revoked / not-granted /
|
|
36
|
+
* unreadable decision returns `blocked_by_consent` BEFORE any store
|
|
37
|
+
* query — a denial is never mixed into the waiting set;
|
|
38
|
+
* 2. the governance store is queried for the EXACT (rollout identity, host
|
|
39
|
+
* turn id) — never a session scan, never the submission turn, never a
|
|
40
|
+
* recent-turn fallback;
|
|
41
|
+
* 3. rows keep the same retention/excerpt contract the submit path's
|
|
42
|
+
* enrichment uses (user/assistant turns, operational|promoted retention,
|
|
43
|
+
* `governance_observation:<logicalKey>` sourceRef, bounded note) so the
|
|
44
|
+
* backfilled entries are indistinguishable from submit-time evidence.
|
|
45
|
+
*
|
|
46
|
+
* Expired/purged rows (retention outside operational|promoted, empty text)
|
|
47
|
+
* simply do not qualify — an honest not_found until retention re-promotes
|
|
48
|
+
* or the turn is re-collected.
|
|
49
|
+
*/
|
|
50
|
+
export declare function readCodexTargetTurnEvidence(workspaceDir: string, sessionId: string, turnId: string): TargetTurnEvidenceRead;
|
|
28
51
|
/**
|
|
29
52
|
* P1-3: does the authorized governance store already hold readable text for
|
|
30
53
|
* the EXACT (session, turn)? Shared by the receipt fallback (disposition
|
|
31
54
|
* choice) and the workspace worker's waiting-task reconcile (the flip) —
|
|
32
55
|
* the diagnosis itself re-reads the turn through the same store.
|
|
33
|
-
* Unreadable ⇒ missing.
|
|
56
|
+
* Unreadable ⇒ missing. Since PRI-1273 this boolean is the `available`
|
|
57
|
+
* projection of the consent-gated structured read above: a consent denial
|
|
58
|
+
* reads as "not available", so the receipt fallback's waiting disposition
|
|
59
|
+
* is reclassified to `blocked_by_source_material_consent` by the worker
|
|
60
|
+
* reconcile on its next bounded pass.
|
|
34
61
|
*/
|
|
35
62
|
export declare function codexTargetTurnTextAvailable(workspaceDir: string, sessionId: string, turnId: string): boolean;
|
|
36
63
|
/**
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
* `tool_call` rows, or look-alike strings (SPEC TD §1.3).
|
|
29
29
|
*/
|
|
30
30
|
import * as path from 'node:path';
|
|
31
|
-
import {
|
|
31
|
+
import { listGovernanceConversationTextsForHostTurn, readSourceMaterialAuthorization } from '@principles/host-runtime';
|
|
32
32
|
import { isSentinelSessionId } from '@principles/core/runtime';
|
|
33
33
|
import { recoverTaskIdentityFromReceipt, validatePainSubmitReceipt, } from '@principles/core/runtime';
|
|
34
34
|
import { isPainSubmitPostToolUseToolName } from '../codec/input-decoder.js';
|
|
@@ -80,32 +80,68 @@ function normalizeEventId(value, opts) {
|
|
|
80
80
|
return id.length > 0 && id.length <= 200 && !isSentinelSessionId(id) ? id : null;
|
|
81
81
|
}
|
|
82
82
|
/**
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
*
|
|
86
|
-
*
|
|
87
|
-
*
|
|
83
|
+
* PRI-1273 (A2): the structured target-turn evidence read the worker's
|
|
84
|
+
* waiting-task reconcile consumes. This is the EXACT-turn authority for the
|
|
85
|
+
* Codex host boundary:
|
|
86
|
+
*
|
|
87
|
+
* 1. the source-material consent ledger is consulted FIRST (PRI-1283 R2
|
|
88
|
+
* parity with the submit path): a missing / revoked / not-granted /
|
|
89
|
+
* unreadable decision returns `blocked_by_consent` BEFORE any store
|
|
90
|
+
* query — a denial is never mixed into the waiting set;
|
|
91
|
+
* 2. the governance store is queried for the EXACT (rollout identity, host
|
|
92
|
+
* turn id) — never a session scan, never the submission turn, never a
|
|
93
|
+
* recent-turn fallback;
|
|
94
|
+
* 3. rows keep the same retention/excerpt contract the submit path's
|
|
95
|
+
* enrichment uses (user/assistant turns, operational|promoted retention,
|
|
96
|
+
* `governance_observation:<logicalKey>` sourceRef, bounded note) so the
|
|
97
|
+
* backfilled entries are indistinguishable from submit-time evidence.
|
|
98
|
+
*
|
|
99
|
+
* Expired/purged rows (retention outside operational|promoted, empty text)
|
|
100
|
+
* simply do not qualify — an honest not_found until retention re-promotes
|
|
101
|
+
* or the turn is re-collected.
|
|
88
102
|
*/
|
|
89
|
-
export function
|
|
103
|
+
export function readCodexTargetTurnEvidence(workspaceDir, sessionId, turnId) {
|
|
104
|
+
const authorization = readSourceMaterialAuthorization({ workspaceDir, hostKind: 'codex' });
|
|
105
|
+
if (authorization.status === 'blocked') {
|
|
106
|
+
return { status: 'blocked_by_consent', reason: authorization.reason };
|
|
107
|
+
}
|
|
90
108
|
try {
|
|
91
|
-
const result =
|
|
109
|
+
const result = listGovernanceConversationTextsForHostTurn({
|
|
92
110
|
workspaceDir,
|
|
93
111
|
hostKind: 'codex',
|
|
94
112
|
rolloutIdentity: sessionId,
|
|
95
113
|
hostTurnId: turnId,
|
|
96
|
-
limit:
|
|
114
|
+
limit: 12,
|
|
97
115
|
});
|
|
98
|
-
if (!result.ok)
|
|
99
|
-
return
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
116
|
+
if (!result.ok) {
|
|
117
|
+
return { status: 'read_failed', reason: result.reason };
|
|
118
|
+
}
|
|
119
|
+
const entries = result.rows.map((row) => ({
|
|
120
|
+
sourceRef: 'governance_observation:' + row.logicalKey,
|
|
121
|
+
note: row.visibleText.slice(0, 2000),
|
|
122
|
+
}));
|
|
123
|
+
return entries.length > 0
|
|
124
|
+
? { status: 'available', entries }
|
|
125
|
+
: { status: 'not_found' };
|
|
104
126
|
}
|
|
105
|
-
catch {
|
|
106
|
-
return
|
|
127
|
+
catch (error) {
|
|
128
|
+
return { status: 'read_failed', reason: error instanceof Error ? error.message.slice(0, 120) : 'store_exception' };
|
|
107
129
|
}
|
|
108
130
|
}
|
|
131
|
+
/**
|
|
132
|
+
* P1-3: does the authorized governance store already hold readable text for
|
|
133
|
+
* the EXACT (session, turn)? Shared by the receipt fallback (disposition
|
|
134
|
+
* choice) and the workspace worker's waiting-task reconcile (the flip) —
|
|
135
|
+
* the diagnosis itself re-reads the turn through the same store.
|
|
136
|
+
* Unreadable ⇒ missing. Since PRI-1273 this boolean is the `available`
|
|
137
|
+
* projection of the consent-gated structured read above: a consent denial
|
|
138
|
+
* reads as "not available", so the receipt fallback's waiting disposition
|
|
139
|
+
* is reclassified to `blocked_by_source_material_consent` by the worker
|
|
140
|
+
* reconcile on its next bounded pass.
|
|
141
|
+
*/
|
|
142
|
+
export function codexTargetTurnTextAvailable(workspaceDir, sessionId, turnId) {
|
|
143
|
+
return readCodexTargetTurnEvidence(workspaceDir, sessionId, turnId).status === 'available';
|
|
144
|
+
}
|
|
109
145
|
/**
|
|
110
146
|
* Attempt the receipt fallback for one PostToolUse event. Never throws —
|
|
111
147
|
* every failure is a structured result the hook surfaces as one bounded
|
|
@@ -30,7 +30,7 @@ import path from 'node:path';
|
|
|
30
30
|
import { computeFeatureFlagsFromConfig, createRuntimeStateHandle, createPainSignalBridge, isRetryWaitBackoffElapsed, pickDiagnosticianCandidate, reconcileWaitingTargetEvidenceTasks, PrincipleTreeLedgerAdapter, resolveDiagnosticianCapability, } from '@principles/core/runtime';
|
|
31
31
|
import { createWorkspaceBehaviorEvidencePort, createCodexArtificerTrustedFactsPort, loadPdConfigForPlugin, loadFeatureFlagFromConfig, reconcileGovernanceContinuation, runInternalizationConsumerCycle, runStage2CorrectionCycle, createSignalClassifierForWorkspace, createGovernanceTrajectoryTurnReader, createConsentLedgerSourceMaterialAuthorization, createHostIntentDocReader, isAutomaticPainSubmissionAuthorized, saveHostToolDeclaration, HOST_TOOL_DECLARATION_VERSION, } from '@principles/host-runtime';
|
|
32
32
|
import { catchUpCodexIngestion } from '../ingestion/catch-up.js';
|
|
33
|
-
import {
|
|
33
|
+
import { readCodexTargetTurnEvidence } from '../ingestion/pain-receipt-fallback.js';
|
|
34
34
|
import { CODEX_TOOL_SEMANTIC_MAPPINGS, CODEX_TOOL_SEMANTICS } from '../tool-semantics.js';
|
|
35
35
|
const WORKER_OWNER = 'companion-worker';
|
|
36
36
|
const DEFAULT_DIAG_CANDIDATE_LIMIT = 5;
|
|
@@ -299,55 +299,69 @@ export async function runCodexWorkspaceWorkerCycle(options) {
|
|
|
299
299
|
// and the picker's SQL excludes anything still waiting. Bounded work
|
|
300
300
|
// over the existing task rows + the shared authorized-store probe;
|
|
301
301
|
// no new queue, no scheduler, no scanning of general history.
|
|
302
|
+
// PRI-1273 (A2): the probe is the consent-gated EXACT-turn read —
|
|
303
|
+
// the flip CAS backfills the arrived evidence + evidenceClass onto
|
|
304
|
+
// the SAME task, and a source-material denial reclassifies the task
|
|
305
|
+
// to blocked_by_source_material_consent instead of waiting.
|
|
306
|
+
// SPEC TD §2.2 ruling 4: the SAME consent ledger is read once more
|
|
307
|
+
// right before the evidence write (the existing PRI-1283 R2 port), so
|
|
308
|
+
// a revocation landing between the read and the write persists nothing.
|
|
302
309
|
targetEvidenceReconcile = await reconcileWaitingTargetEvidenceTasks({
|
|
303
310
|
stateManager: handle.stateManager,
|
|
304
|
-
|
|
311
|
+
readTargetTurnEvidence: (sessionId, turnId) => readCodexTargetTurnEvidence(path.resolve(workspaceDir), sessionId, turnId),
|
|
312
|
+
sourceMaterialAuthorization: createConsentLedgerSourceMaterialAuthorization(path.resolve(workspaceDir)),
|
|
305
313
|
nowIso: new Date().toISOString(),
|
|
306
314
|
});
|
|
307
|
-
if (targetEvidenceReconcile.flipped > 0 || targetEvidenceReconcile.errors.length > 0) {
|
|
315
|
+
if (targetEvidenceReconcile.flipped > 0 || targetEvidenceReconcile.blocked > 0 || targetEvidenceReconcile.errors.length > 0) {
|
|
308
316
|
emitEvent('CODEX_WORKER_TARGET_EVIDENCE_RECONCILE', JSON.stringify(targetEvidenceReconcile));
|
|
309
317
|
}
|
|
310
318
|
const candidate = await pickDiagnosticianCandidate(handle.stateManager, options.diagnosticianCandidateLimit ?? DEFAULT_DIAG_CANDIDATE_LIMIT, new Date().toISOString());
|
|
311
319
|
if (candidate !== null) {
|
|
312
320
|
const stateDir = path.join(workspaceDir, '.state');
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
.
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
321
|
+
let releaseBridge;
|
|
322
|
+
try {
|
|
323
|
+
const bridge = await createPainSignalBridge({
|
|
324
|
+
workspaceDir,
|
|
325
|
+
stateDir,
|
|
326
|
+
ledgerAdapter: new PrincipleTreeLedgerAdapter({ stateDir }),
|
|
327
|
+
owner: WORKER_OWNER,
|
|
328
|
+
effectiveConfig: config.effective,
|
|
329
|
+
getEnvVar: (name) => process.env[name],
|
|
330
|
+
// PRI-1233 / SPEC §9.2: this worker is where Stage A actually runs
|
|
331
|
+
// for governance-admitted Codex pains — the neutral host reader
|
|
332
|
+
// must ride along (flag-off = zero filesystem access inside it).
|
|
333
|
+
intentDocReader: createHostIntentDocReader({ workspaceDir, effectiveConfig: config.effective }),
|
|
334
|
+
// PRI-1265: the worker's Stage A gets the bound session's ORIGINAL
|
|
335
|
+
// turns from the workspace's own consent-tiered governance store.
|
|
336
|
+
trajectoryTurnReader: createGovernanceTrajectoryTurnReader({ workspaceDir, sourceMaterialAuthorization: createConsentLedgerSourceMaterialAuthorization(workspaceDir) }),
|
|
337
|
+
// PRI-1283 R2: latest-consent gate consulted per assembly / model call.
|
|
338
|
+
sourceMaterialAuthorization: createConsentLedgerSourceMaterialAuthorization(workspaceDir),
|
|
339
|
+
// SPEC §9.6: lazy probe — a revoked flag stops persisted
|
|
340
|
+
// automatic_hook tasks at the bridge's execution boundary.
|
|
341
|
+
isAutoPainSubmissionAuthorized: () => isAutomaticPainSubmissionAuthorized(workspaceDir),
|
|
342
|
+
}, (release) => { releaseBridge = release; });
|
|
343
|
+
const executed = await bridge.executePendingDiagnosis({ taskId: candidate.taskId });
|
|
344
|
+
// PRI-917 v0.3.3 review fix (P2-2): forward the parked-candidate
|
|
345
|
+
// dispositions so the Owner sees the pending items instead of a bare
|
|
346
|
+
// degraded status with no subject.
|
|
347
|
+
const reuseReviewRequired = (executed.candidateOutcomes ?? [])
|
|
348
|
+
.filter((outcome) => outcome.reason === 'reuse_review_required')
|
|
349
|
+
.map((outcome) => outcome.candidateId);
|
|
350
|
+
diagnostician = {
|
|
351
|
+
taskId: candidate.taskId,
|
|
352
|
+
status: executed.status,
|
|
353
|
+
...(executed.message !== undefined ? { message: executed.message.slice(0, 200) } : {}),
|
|
354
|
+
...(executed.errorCategory !== undefined ? { errorCategory: executed.errorCategory } : {}),
|
|
355
|
+
...(reuseReviewRequired.length > 0 ? { reuseReviewRequiredCandidateIds: reuseReviewRequired } : {}),
|
|
356
|
+
};
|
|
357
|
+
// The bridge stays cached per workspace (bounded: one per workspace
|
|
358
|
+
// per worker process, exactly like the OpenClaw plugin host) — no
|
|
359
|
+
// per-cycle dispose, so a concurrent cycle can never hit a disposed
|
|
360
|
+
// bridge. The factory self-disposes losing concurrent constructions.
|
|
361
|
+
}
|
|
362
|
+
finally {
|
|
363
|
+
await releaseBridge?.();
|
|
364
|
+
}
|
|
351
365
|
}
|
|
352
366
|
else {
|
|
353
367
|
// No eligible candidate, but a retry_wait task may still be inside
|
|
@@ -405,32 +419,38 @@ export async function runCodexWorkspaceWorkerCycle(options) {
|
|
|
405
419
|
const resumedTaskIds = new Set();
|
|
406
420
|
if (backlog.length > 0) {
|
|
407
421
|
const stateDir = path.join(workspaceDir, '.state');
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
422
|
+
let releaseBridge;
|
|
423
|
+
try {
|
|
424
|
+
const bridge = await createPainSignalBridge({
|
|
425
|
+
workspaceDir,
|
|
426
|
+
stateDir,
|
|
427
|
+
ledgerAdapter: new PrincipleTreeLedgerAdapter({ stateDir }),
|
|
428
|
+
owner: WORKER_OWNER,
|
|
429
|
+
effectiveConfig: config.effective,
|
|
430
|
+
getEnvVar: (name) => process.env[name],
|
|
431
|
+
// PRI-1265: post-processing resumes see the same authorized
|
|
432
|
+
// trajectory context as the initial diagnosis run.
|
|
433
|
+
trajectoryTurnReader: createGovernanceTrajectoryTurnReader({ workspaceDir, sourceMaterialAuthorization: createConsentLedgerSourceMaterialAuthorization(workspaceDir) }),
|
|
434
|
+
// PRI-1283 R2: latest-consent gate consulted per assembly / model call.
|
|
435
|
+
sourceMaterialAuthorization: createConsentLedgerSourceMaterialAuthorization(workspaceDir),
|
|
436
|
+
// SPEC §9.6: lazy probe — automatic_hook backlog resumes honor
|
|
437
|
+
// the Workspace authorization too.
|
|
438
|
+
isAutoPainSubmissionAuthorized: () => isAutomaticPainSubmissionAuthorized(workspaceDir),
|
|
439
|
+
}, (release) => { releaseBridge = release; });
|
|
440
|
+
for (const item of backlog) {
|
|
441
|
+
const resumed = await bridge.resumeSucceededDiagnosisPostProcessing({ taskId: item.taskId });
|
|
442
|
+
if (resumed.status === 'resumed') {
|
|
443
|
+
resumedTaskIds.add(item.taskId);
|
|
444
|
+
}
|
|
445
|
+
else if (resumed.status === 'blocked') {
|
|
446
|
+
blocked.push({ candidateId: item.candidateId, reason: resumed.reason ?? 'blocked' });
|
|
447
|
+
}
|
|
448
|
+
for (const skip of resumed.skipped ?? [])
|
|
449
|
+
blocked.push(skip);
|
|
428
450
|
}
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
for (const skip of resumed.skipped ?? [])
|
|
433
|
-
blocked.push(skip);
|
|
451
|
+
}
|
|
452
|
+
finally {
|
|
453
|
+
await releaseBridge?.();
|
|
434
454
|
}
|
|
435
455
|
}
|
|
436
456
|
postProcessing = {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@principles/codex-adapter",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.15.0",
|
|
4
4
|
"description": "Codex CLI host adapter for Principles Disciple — implements HostAdapter interface for OpenAI Codex CLI's stdin/stdout JSON hook model (ADR-0020).",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -33,9 +33,9 @@
|
|
|
33
33
|
"lint": "eslint \"src/**/*.ts\""
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@principles/core": "^8.
|
|
37
|
-
"@principles/host-runtime": "^4.
|
|
38
|
-
"@principles/install-layout": "^0.
|
|
36
|
+
"@principles/core": "^8.2.0",
|
|
37
|
+
"@principles/host-runtime": "^4.7.0",
|
|
38
|
+
"@principles/install-layout": "^0.11.0"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"@types/node": "^26.6.6",
|
|
@@ -52,5 +52,5 @@
|
|
|
52
52
|
"publishConfig": {
|
|
53
53
|
"access": "public"
|
|
54
54
|
},
|
|
55
|
-
"gitHead": "
|
|
55
|
+
"gitHead": "06a20cb7842f41a6510937f92340343158164b9f"
|
|
56
56
|
}
|