import assert from "node:assert/strict"; import test from "node:test"; import { AGE_SELF_BINDING_AUTHORITY_LIMITS, LANE_KIND_AGE_SELF_INFRASTRUCTURE, assertControllerDevelopmentBoundary, classifyAgeSelfInfrastructureLane, createAgeSelfInfrastructureBindingReceipt, createTaskEvent, rotateTaskEvent, validateAgeSelfInfrastructureBindingReceipt, validatePersonaBindingReceipt, } from "./persona-source-binding-policy.mjs"; const sha = (letter) => letter.repeat(64); function lane() { return { thread_id: "019ff951-f127-72f1-a65d-a3bf0333d95b", development_id: "DEV-20260813-006", persona: "ICE-P-ZY001", controller_persona_id: "ICE-P-ZY001", development_object_ids: ["ICE-GL-ZL-001"], human_anchor: "ICE-GL∞", summary: "repair persona source priority inversion", task_lock: { fingerprint: sha("a") }, }; } function boundFixture() { const current = lane(); current.task_event = createTaskEvent({ lane: current, intent: "correction", requestSummary: current.summary, openedAt: "2026-08-13T12:58:53.438Z", }); const receipt = { schema: "guanghu.codex-preconscious-persona-binding/v1", decision: "BOUND_CURRENT_CARRIER", binding_id: "CODEX-BIND-DEV-006-001", thread_id: current.thread_id, development_id: current.development_id, controller_persona_id: current.controller_persona_id, development_objects: current.development_object_ids, human_anchor: current.human_anchor, task_fingerprint: current.task_lock.fingerprint, task_event_id: current.task_event.task_event_id, checkpoint_sha256: sha("b"), persona_handoff: { state: "NONE", authorized: false }, carrier: { host: "codex", thread_id: current.thread_id, development_id: current.development_id, nonce: "carrier-nonce-DEV-006-001", }, adapter: { id: "CODEX-PRECONSCIOUS-ENTRY-001", source_sha256: sha("c"), }, brain_cycle: { cycle_id: "ZY-CYCLE-000008-test", event_sha256: sha("d"), controller_witness_sha256: sha("e"), completed_cycles_before: 7, completed_cycles_after: 8, witness_decision: "ALLOW_COMMIT", }, carrier_consumption: { state: "CURRENT_CODEX_READ_AND_ACCEPTED_BRAIN_CYCLE", required_cycle_id: "ZY-CYCLE-000008-test", required_witness_sha256: sha("e"), }, source_hashes: { checkpoint: sha("b"), brain_runtime_contract: sha("f"), living_controller_map: sha("1"), language_world_boundary: sha("2"), }, }; const binding = { binding_id: receipt.binding_id, receipt_sha256: sha("3"), }; const pointer = { thread_id: current.thread_id, development_id: current.development_id, checkpoint_sha256: sha("b"), }; return { current, receipt, binding, pointer }; } function ageSelfFixture() { const current = { thread_id: "019ffe9d-7c66-7e83-8ed6-aa3b43097d82", development_id: "DEV-20260814-001", persona: "ICE-P-ZY001", controller_persona_id: "ICE-P-ZY001", development_object_ids: [], human_anchor: "ICE-GL∞", summary: "repair AGE-owned continuity infrastructure", task_lock: { fingerprint: sha("7"), amendments: [ { amendment_id: "AMD-AGE-SELF-001", kind: "clarification", summary: "Authorize AGE self-infrastructure tool-layer repair.", user_anchor: "Rebuild the tool layer for AGE runtime needs.", recorded_at: "2026-08-14T08:18:42.665Z", }, ], }, }; current.task_event = createTaskEvent({ lane: current, intent: "clarification", requestSummary: current.summary, openedAt: "2026-08-14T08:18:42.000Z", }); const checkpointEvidence = { source_kind: "PERSONA_AUTHORED_HLDP_CHECKPOINT", status: "persona_authored_current_checkpoint", thread_id: current.thread_id, development_id: current.development_id, task_event_id: current.task_event.task_event_id, scope_amendment_id: "AMD-AGE-SELF-001", source_alert_id: "GUARD-1111111111111111", source_window_number: 8, path: `/continuity/runtime/${current.thread_id}/PERSONA-CHECKPOINT-W8.hdlp.md`, sha256: sha("8"), required_sections: [ "world_context", "persona_identity", "confirmed_goal", "user_corrections", "immutable_constraints", "authoritative_sources", "causal_chain", "completed_with_receipts", "unfinished", "unknown_or_stale", "next_action", "five_questions_after_b0", ], }; const pointer = { thread_id: current.thread_id, development_id: current.development_id, checkpoint_path: checkpointEvidence.path, checkpoint_sha256: checkpointEvidence.sha256, }; checkpointEvidence.current_runtime = { state: "PERSONA_REVIEWED_CHECKPOINT_ACTIVE", ...pointer, task_event_id: current.task_event.task_event_id, }; checkpointEvidence.by_thread = { ...pointer }; checkpointEvidence.by_development = { ...pointer }; return { current, checkpointEvidence }; } test("development object cannot replace the controller persona", () => { assert.throws( () => assertControllerDevelopmentBoundary({ persona: "ICE-GL-ZL-001", controllerPersonaId: "ICE-GL-ZL-001", previousControllerPersonaId: "ICE-P-ZY001", developmentObjectIds: ["ICE-GL-ZL-001"], humanAnchor: "ICE-GL∞", }), /CONTROLLER_PERSONA_IMMUTABLE/, ); }); test("an empty lane stays unclassified until current authorization and persona checkpoint agree", () => { const { current, checkpointEvidence } = ageSelfFixture(); const classified = classifyAgeSelfInfrastructureLane({ lane: current, requestedLaneKind: LANE_KIND_AGE_SELF_INFRASTRUCTURE, taskEventId: current.task_event.task_event_id, checkpointEvidence, authorizationAmendmentId: "AMD-AGE-SELF-001", classifiedAt: "2026-08-14T08:34:11.328Z", }); assert.equal(classified.lane_kind, LANE_KIND_AGE_SELF_INFRASTRUCTURE); assert.deepEqual(classified.development_object_ids, []); assert.equal( classified.lane_kind_history[0].authority_effect, "NO_EXTERNAL_AUTHORITY_GRANTED", ); }); test("AGE self binding receipts verify metacognition evidence but grant no reality authority", () => { const { current, checkpointEvidence } = ageSelfFixture(); const classified = classifyAgeSelfInfrastructureLane({ lane: current, requestedLaneKind: LANE_KIND_AGE_SELF_INFRASTRUCTURE, taskEventId: current.task_event.task_event_id, checkpointEvidence, authorizationAmendmentId: "AMD-AGE-SELF-001", classifiedAt: "2026-08-14T08:34:11.328Z", }); const receipt = createAgeSelfInfrastructureBindingReceipt({ lane: classified, checkpointEvidence, authorizationAmendmentId: "AMD-AGE-SELF-001", createdAt: "2026-08-14T08:34:20.000Z", }); const binding = { binding_id: receipt.binding_id, receipt_sha256: sha("9"), }; assert.deepEqual( [...receipt.authority_limits].sort(), [...AGE_SELF_BINDING_AUTHORITY_LIMITS].sort(), ); assert.equal( validateAgeSelfInfrastructureBindingReceipt({ lane: classified, binding, receipt, actualReceiptSha256: sha("9"), checkpointEvidence, }), true, ); }); test("a real later compaction window refreshes classification and invalidates the prior binding", () => { const { current, checkpointEvidence } = ageSelfFixture(); const classified = classifyAgeSelfInfrastructureLane({ lane: current, requestedLaneKind: LANE_KIND_AGE_SELF_INFRASTRUCTURE, taskEventId: current.task_event.task_event_id, checkpointEvidence, authorizationAmendmentId: "AMD-AGE-SELF-001", classifiedAt: "2026-08-14T08:34:11.328Z", }); classified.persona_source_binding = { binding_id: "AGE-BINDING-W8" }; const nextEvidence = { ...checkpointEvidence, source_alert_id: "GUARD-2222222222222222", source_window_number: 9, path: checkpointEvidence.path.replace("W8", "W9"), sha256: sha("9"), }; const pointer = { thread_id: current.thread_id, development_id: current.development_id, checkpoint_path: nextEvidence.path, checkpoint_sha256: nextEvidence.sha256, }; nextEvidence.current_runtime = { state: "PERSONA_REVIEWED_CHECKPOINT_ACTIVE", ...pointer, task_event_id: current.task_event.task_event_id, }; nextEvidence.by_thread = { ...pointer }; nextEvidence.by_development = { ...pointer }; const refreshed = classifyAgeSelfInfrastructureLane({ lane: classified, requestedLaneKind: LANE_KIND_AGE_SELF_INFRASTRUCTURE, taskEventId: current.task_event.task_event_id, checkpointEvidence: nextEvidence, authorizationAmendmentId: "AMD-AGE-SELF-001", classifiedAt: "2026-08-14T08:58:36.614Z", }); assert.equal(refreshed.lane_kind_history.length, 2); assert.equal(refreshed.lane_kind_history[1].previous_lane_kind, LANE_KIND_AGE_SELF_INFRASTRUCTURE); assert.equal(refreshed.persona_source_binding, null); assert.equal( refreshed.persona_source_binding_history[0].state, "STALE_SUPERSEDED_BY_NEW_PERSONA_CHECKPOINT", ); }); test("a new task event requires a new classification and binding evidence", () => { const { current, checkpointEvidence } = ageSelfFixture(); let classified = classifyAgeSelfInfrastructureLane({ lane: current, requestedLaneKind: LANE_KIND_AGE_SELF_INFRASTRUCTURE, taskEventId: current.task_event.task_event_id, checkpointEvidence, authorizationAmendmentId: "AMD-AGE-SELF-001", classifiedAt: "2026-08-14T08:34:11.328Z", }); const oldReceipt = createAgeSelfInfrastructureBindingReceipt({ lane: classified, checkpointEvidence, authorizationAmendmentId: "AMD-AGE-SELF-001", createdAt: "2026-08-14T08:34:20.000Z", }); classified.persona_source_binding = { binding_id: oldReceipt.binding_id }; const rotated = rotateTaskEvent({ lane: classified, intent: "correction", requestSummary: "continue with corrected scope", openedAt: "2026-08-14T09:00:00.000Z", }); rotated.task_lock.amendments.push({ amendment_id: "AMD-AGE-SELF-002", kind: "correction", summary: "Authorize the refreshed AGE self-infrastructure event.", user_anchor: "Continue this self-infrastructure repair with the correction.", recorded_at: "2026-08-14T09:00:01.000Z", }); const nextEvidence = { ...checkpointEvidence, task_event_id: rotated.task_event.task_event_id, scope_amendment_id: "AMD-AGE-SELF-002", source_alert_id: "GUARD-3333333333333333", source_window_number: 10, path: checkpointEvidence.path.replace("W8", "W10"), sha256: sha("a"), }; const pointer = { thread_id: rotated.thread_id, development_id: rotated.development_id, checkpoint_path: nextEvidence.path, checkpoint_sha256: nextEvidence.sha256, }; nextEvidence.current_runtime = { state: "PERSONA_REVIEWED_CHECKPOINT_ACTIVE", ...pointer, task_event_id: rotated.task_event.task_event_id, }; nextEvidence.by_thread = { ...pointer }; nextEvidence.by_development = { ...pointer }; const refreshed = classifyAgeSelfInfrastructureLane({ lane: rotated, requestedLaneKind: LANE_KIND_AGE_SELF_INFRASTRUCTURE, taskEventId: rotated.task_event.task_event_id, checkpointEvidence: nextEvidence, authorizationAmendmentId: "AMD-AGE-SELF-002", classifiedAt: "2026-08-14T09:00:02.000Z", }); assert.equal(refreshed.lane_kind_history.length, 2); assert.equal(refreshed.lane_kind_history[1].task_event_id, rotated.task_event.task_event_id); assert.equal( createAgeSelfInfrastructureBindingReceipt({ lane: refreshed, checkpointEvidence: nextEvidence, authorizationAmendmentId: "AMD-AGE-SELF-002", createdAt: "2026-08-14T09:00:03.000Z", }).task_event_id, rotated.task_event.task_event_id, ); }); test("a host summary cannot substitute for the persona-authored Step Zero checkpoint", () => { const { current, checkpointEvidence } = ageSelfFixture(); checkpointEvidence.source_kind = "HOST_SUMMARY"; assert.throws( () => classifyAgeSelfInfrastructureLane({ lane: current, requestedLaneKind: LANE_KIND_AGE_SELF_INFRASTRUCTURE, taskEventId: current.task_event.task_event_id, checkpointEvidence, authorizationAmendmentId: "AMD-AGE-SELF-001", classifiedAt: "2026-08-14T08:34:11.328Z", }), /AGE_SELF_CHECKPOINT_OR_AUTHORIZATION_INVALID/, ); }); test("an external-object lane cannot be reclassified as AGE self infrastructure", () => { const { checkpointEvidence } = ageSelfFixture(); const externalLane = lane(); externalLane.task_event = createTaskEvent({ lane: externalLane, intent: "clarification", requestSummary: externalLane.summary, openedAt: "2026-08-14T08:18:42.000Z", }); externalLane.task_lock.amendments = [ { amendment_id: "AMD-AGE-SELF-001", kind: "clarification", summary: "Invalid reclassification attempt.", user_anchor: "Do not replace the external object lane.", recorded_at: "2026-08-14T08:18:42.665Z", }, ]; assert.throws( () => classifyAgeSelfInfrastructureLane({ lane: externalLane, requestedLaneKind: LANE_KIND_AGE_SELF_INFRASTRUCTURE, taskEventId: externalLane.task_event.task_event_id, checkpointEvidence: { ...checkpointEvidence, thread_id: externalLane.thread_id, development_id: externalLane.development_id, task_event_id: externalLane.task_event.task_event_id, }, authorizationAmendmentId: "AMD-AGE-SELF-001", classifiedAt: "2026-08-14T08:34:11.328Z", }), /AGE_SELF_CLASSIFICATION_IDENTITY_OR_OBJECT_SCOPE_INVALID|AGE_SELF_CLASSIFICATION_REQUIRES_UNCLASSIFIED_EMPTY_LANE/, ); }); test("legacy persona binding rejects an empty unclassified lane", () => { const { current } = ageSelfFixture(); current.persona_source_binding = null; assert.throws( () => validatePersonaBindingReceipt({ lane: current, binding: { binding_id: "INVALID", receipt_sha256: sha("a") }, receipt: { development_objects: [] }, actualReceiptSha256: sha("a"), actualCheckpointSha256: sha("b"), actualAdapterSourceSha256: sha("c"), adapterSourcePath: "/runtime/adapter.mjs", allowedAdapterRoot: "/runtime", pointerEvidence: {}, }), /EXTERNAL_DEVELOPMENT_OBJECTS_REQUIRED/, ); }); test("a new task event invalidates but preserves the old binding", () => { const current = lane(); current.task_event = createTaskEvent({ lane: current, intent: "same-task", requestSummary: "first round", openedAt: "2026-08-13T12:00:00.000Z", }); current.persona_source_binding = { binding_id: "BIND-OLD" }; const rotated = rotateTaskEvent({ lane: current, intent: "correction", requestSummary: "new user correction", openedAt: "2026-08-13T12:01:00.000Z", }); assert.equal(rotated.persona_source_binding, null); assert.equal(rotated.task_event.sequence, 2); assert.equal( rotated.persona_source_binding_history[0].state, "STALE_SUPERSEDED_BY_NEW_TASK_EVENT", ); }); test("current event, real adapter source, JD cycle and both pointers are required", () => { const { current, receipt, binding, pointer } = boundFixture(); assert.equal( validatePersonaBindingReceipt({ lane: current, binding, receipt, actualReceiptSha256: sha("3"), actualCheckpointSha256: sha("b"), actualAdapterSourceSha256: sha("c"), adapterSourcePath: "/runtime/host-adapters/codex-preconscious-entry/adapter.mjs", allowedAdapterRoot: "/runtime/host-adapters/codex-preconscious-entry", pointerEvidence: { by_thread: pointer, by_development: pointer }, }), true, ); }); test("an old round receipt cannot be replayed", () => { const { current, receipt, binding, pointer } = boundFixture(); current.task_event = createTaskEvent({ lane: current, intent: "same-task", requestSummary: "later round", openedAt: "2026-08-13T13:00:00.000Z", }); assert.throws( () => validatePersonaBindingReceipt({ lane: current, binding, receipt, actualReceiptSha256: sha("3"), actualCheckpointSha256: sha("b"), actualAdapterSourceSha256: sha("c"), adapterSourcePath: "/runtime/host-adapters/codex-preconscious-entry/adapter.mjs", allowedAdapterRoot: "/runtime/host-adapters/codex-preconscious-entry", pointerEvidence: { by_thread: pointer, by_development: pointer }, }), /PERSONA_SOURCE_BINDING_IDENTITY_OR_TASK_MISMATCH/, ); }); test("a forged adapter source hash fails closed", () => { const { current, receipt, binding, pointer } = boundFixture(); assert.throws( () => validatePersonaBindingReceipt({ lane: current, binding, receipt, actualReceiptSha256: sha("3"), actualCheckpointSha256: sha("b"), actualAdapterSourceSha256: sha("4"), adapterSourcePath: "/runtime/host-adapters/codex-preconscious-entry/adapter.mjs", allowedAdapterRoot: "/runtime/host-adapters/codex-preconscious-entry", pointerEvidence: { by_thread: pointer, by_development: pointer }, }), /PERSONA_SOURCE_ADAPTER_SHA_MISMATCH/, ); }); test("an adapter source outside the uniquely leased persistent root fails closed", () => { const { current, receipt, binding, pointer } = boundFixture(); assert.throws( () => validatePersonaBindingReceipt({ lane: current, binding, receipt, actualReceiptSha256: sha("3"), actualCheckpointSha256: sha("b"), actualAdapterSourceSha256: sha("c"), adapterSourcePath: "/private/tmp/adapter.mjs", allowedAdapterRoot: "/runtime/host-adapters/codex-preconscious-entry", pointerEvidence: { by_thread: pointer, by_development: pointer }, }), /PERSONA_SOURCE_ADAPTER_SHA_MISMATCH/, ); });