diff --git a/plugins/pardes-pi/docs/MANAGER.md b/plugins/pardes-pi/docs/MANAGER.md index daeaa99..4a0119d 100644 --- a/plugins/pardes-pi/docs/MANAGER.md +++ b/plugins/pardes-pi/docs/MANAGER.md @@ -84,9 +84,16 @@ durable history remains intact. It fails closed on unrelated interruption, and `/pardes stop` synchronously cancels every active report identity and invalidates pre-read lifecycle permits before manager deactivation. Restart advances the permit epoch, so a late pre-stop artifact read cannot create delivery or block a -fresh retrieval. After fixes, call -`verification_refresh({ verificationId })` so the same retained verifier checks -the latest clean HEAD. Verification is advisory and +fresh retrieval. A terminal writer report that advances the reviewed head may +carry a Pardes-derived stale-evidence context on that same inbox row; treat it as +the software-owned signal to refresh, not as a second missing notification. An +`audit repair pending` count is software-owned durable recovery state; leave its +blocked row unacknowledged and let restore/retry settle the exact audit identity. +If storage status reports event corruption, Pardes preserves the damaged bytes, +retains parseable audit records, and reports whether it repaired a trailing +fragment or interior line; inspect the preserved artifact only for operator +diagnosis. After fixes, call `verification_refresh({ verificationId })` so the +same retained verifier checks the latest clean HEAD. Verification is advisory and separate from publication. Use it before publishing meaningful engineering slices and when a concrete review question remains. Skip trivial documentation or test-only maintenance unless risk justifies it. Do not recreate verification @@ -111,6 +118,11 @@ For a completed worker slice: intentionally never force-pushes; 7. leave merges under user control. +Acknowledged conflict attention stays generation-scoped to its audited head and +owner lifecycle. Transient `unknown` or one-off `mergeable` hosted projections do +not re-arm it. A newly audited head/lifecycle or conflict reappearance after +confirmed resolution does; inspect that new generation rather than polling. + Browser handoff is explicit: omit `browserMode` or use `'none'` for no opener, use `'background'` for macOS `open -g` with a portable ordinary-opener fallback elsewhere, and use `'foreground'` for the ordinary platform opener. The legacy diff --git a/plugins/pardes-pi/docs/NORTH_STAR.md b/plugins/pardes-pi/docs/NORTH_STAR.md index b37bce6..2e21f6d 100644 --- a/plugins/pardes-pi/docs/NORTH_STAR.md +++ b/plugins/pardes-pi/docs/NORTH_STAR.md @@ -264,10 +264,19 @@ append-only audit history Pi presentation delivery scheduled for the manager conversation ``` -Actionable events enter the durable inbox before presentation. Tokenized wake -records are presentation cursors, not workflow state. Only one delivered cursor -is active at a time; later durable rows remain queued as a suffix. User-judgment -handoffs are explicit and cursor-scoped. +Actionable events enter the durable inbox before presentation. Causally +derivative software context may be coalesced onto its triggering row: a terminal +worker report that invalidates advisory verification evidence carries that stale +context without a second attention row, while both facts remain externally +visible. Durable exact-event intents idempotently repair either audit append +across failure or manager restart before presentation refinement is released. +Malformed event-log bytes are preserved in a manager-scoped corruption artifact; +all parseable records are retained in a repaired active stream, and bounded +storage status distinguishes trailing fragments from interior corruption. Thus +both facts remain append-only audited. Tokenized wake records are presentation cursors, +not workflow state. Only one delivered cursor is active at a time; later durable +rows remain queued as a suffix. User-judgment handoffs are explicit and +cursor-scoped. ## Retained child model diff --git a/plugins/pardes-pi/extensions/pardes/manager/attention.test.ts b/plugins/pardes-pi/extensions/pardes/manager/attention.test.ts index 773c3d3..fbb301b 100644 --- a/plugins/pardes-pi/extensions/pardes/manager/attention.test.ts +++ b/plugins/pardes-pi/extensions/pardes/manager/attention.test.ts @@ -8,6 +8,7 @@ import { pullRequestNeedsAttention, } from './attention.ts'; import type { AgentRecord, PullRequestObservation, PullRequestRecord } from './domain.ts'; +import { conflictAttentionTransition } from './review-gate-lifecycle.ts'; const createdAt = '2026-06-01T00:00:00.000Z'; @@ -152,6 +153,56 @@ describe('manager attention predicates', () => { expect(pullRequestNeedsAttention(healthy)).toBe(false); }); + test('supersedes an older conflict key after complete mergeability and rearms on later conflict', () => { + const headA = 'a'.repeat(40); + const headB = 'b'.repeat(40); + for (const current of [ + { headSha: headB, lifecycleGeneration: 1 }, + { headSha: headA, lifecycleGeneration: 2 }, + ]) { + const owner = agent({ lifecycleGeneration: current.lifecycleGeneration }); + const gate = pullRequest('open', { + conflictAttention: { + attentionObservedForKey: true, + auditedHeadSha: headA, + generation: 1, + ownerLifecycleGeneration: 1, + phase: 'conflicting', + }, + lastPushedHeadSha: current.headSha, + }); + + const first = conflictAttentionTransition(gate, owner, observation(), true); + expect(first).toMatchObject({ + attention: false, + next: { + attentionObservedForKey: false, + auditedHeadSha: current.headSha, + generation: 1, + ownerLifecycleGeneration: current.lifecycleGeneration, + phase: 'resolution_candidate', + }, + }); + const second = conflictAttentionTransition( + { ...gate, conflictAttention: first.next }, + owner, + observation(), + true, + ); + expect(second).toMatchObject({ attention: false, next: { phase: 'resolved' } }); + const reappeared = conflictAttentionTransition( + { ...gate, conflictAttention: second.next }, + owner, + observation({ mergeable: 'conflicting' }), + true, + ); + expect(reappeared).toMatchObject({ + attention: true, + next: { attentionObservedForKey: true, generation: 2, phase: 'conflicting' }, + }); + } + }); + test('does not count terminal pull-request metadata warnings as open-review attention', () => { for (const status of ['merged', 'closed'] as const) { const terminal = pullRequest(status, { observation: observation({ ci: 'failing', status }) }); diff --git a/plugins/pardes-pi/extensions/pardes/manager/attention.ts b/plugins/pardes-pi/extensions/pardes/manager/attention.ts index 2aa9cbb..14da2e6 100644 --- a/plugins/pardes-pi/extensions/pardes/manager/attention.ts +++ b/plugins/pardes-pi/extensions/pardes/manager/attention.ts @@ -28,6 +28,8 @@ export function hasPullRequestWarningMetadata(pullRequest: PullRequestRecord): b pullRequest.discussionPaginationGaps?.length || pullRequest.observation?.ci === 'failing' || pullRequest.observation?.mergeable === 'conflicting' || + (pullRequest.conflictAttention !== undefined && + pullRequest.conflictAttention.phase !== 'resolved') || pullRequest.observation?.reviewDecision === 'changes_requested', ); } diff --git a/plugins/pardes-pi/extensions/pardes/manager/controller.test.ts b/plugins/pardes-pi/extensions/pardes/manager/controller.test.ts index 080d681..d8adbf4 100644 --- a/plugins/pardes-pi/extensions/pardes/manager/controller.test.ts +++ b/plugins/pardes-pi/extensions/pardes/manager/controller.test.ts @@ -66,7 +66,7 @@ import { } from './controller.ts'; import { currentVerificationAttempt, type PullRequestObservation } from './domain.ts'; import { AgentNotFoundError, formatPardesError } from './errors.ts'; -import { MANAGER_INBOX_WAKE_MAX_ROWS } from './inbox.ts'; +import { MANAGER_INBOX_WAKE_MAX_ROWS, projectInboxAttention } from './inbox.ts'; import { ManagerInputValidationError } from './inputs.ts'; const temporaryDirectories: string[] = []; @@ -5837,7 +5837,7 @@ describe('manager controller', () => { }), ); - expect(controller.snapshot()?.revision).toBe(revisionBeforeReports + 3); + expect(controller.snapshot()?.revision).toBe(revisionBeforeReports + 4); expect(controller.snapshot()?.inbox).toHaveLength(1); expect(controller.snapshot()?.inbox[0]).toMatchObject({ agentId: agent.id, @@ -6025,7 +6025,9 @@ describe('manager controller', () => { 'worker-branch non-merge change candidates unavailable (bounds_exceeded)', ); expect(summary).toContain('Total audited change set: 2 paths'); - expect(summary).toContain('durable report available'); + expect(summary).toContain( + 'canonical full report available via one report_get({ reportId }) call', + ); expect(summary.length).toBeLessThanOrEqual(900); }); @@ -9221,7 +9223,7 @@ describe('manager controller', () => { await Effect.runPromise(controller.shutdown(fixture.ctx)); }); - test('deduplicates equivalent pending conflict attention until acknowledgement rearms a later transition', async () => { + test('keeps conflict attention generation-sticky across acknowledgement, transient mergeability, and restart, then rearms after confirmed resolution', async () => { const repo = fixtureRepository(); const stateRoot = mkdtempSync(join(tmpdir(), 'pardes-state-')); temporaryDirectories.push(stateRoot); @@ -9236,28 +9238,157 @@ describe('manager controller', () => { makeWorkers: workers.makeWorkers, }); await Effect.runPromise(controller.activate(fixture.ctx)); - const { published } = await publishManagedFixture(controller, fixture.ctx, repo); + const { agent, published } = await publishManagedFixture(controller, fixture.ctx, repo); const pullRequestId = published.pullRequest.id; await Effect.runPromise( watcher.observeLifecycle(pullRequestId, observedPullRequest({ mergeable: 'conflicting' })), ); - const conflict = requiredValue( + const firstConflict = requiredValue( controller.snapshot()?.inbox.find(({ type }) => type === 'conflict'), ); - await Effect.runPromise(watcher.observeLifecycle(pullRequestId, observedPullRequest())); + expect(controller.snapshot()?.pullRequests[pullRequestId]?.conflictAttention).toMatchObject({ + generation: 1, + phase: 'conflicting', + }); + await Effect.runPromise(controller.acknowledgeInbox(fixture.ctx, { cursor: firstConflict.id })); + + // Unknown and one-off mergeable projections remain externally visible but + // are not proof of conflict resolution. await Effect.runPromise( - watcher.observeLifecycle(pullRequestId, observedPullRequest({ mergeable: 'conflicting' })), + watcher.observeLifecycle(pullRequestId, observedPullRequest({ mergeable: 'unknown' })), + ); + await Effect.runPromise( + watcher.observeLifecycle(pullRequestId, observedPullRequest({ mergeable: 'mergeable' })), + ); + expect(controller.snapshot()?.pullRequests[pullRequestId]).toMatchObject({ + conflictAttention: { generation: 1, phase: 'conflicting' }, + observation: { mergeable: 'mergeable' }, + }); + await Effect.runPromise( + watcher.observeLifecycle(pullRequestId, observedPullRequest({ mergeable: 'unknown' })), + ); + await Effect.runPromise( + watcher.observeLifecycle(pullRequestId, observedPullRequest({ mergeable: 'mergeable' })), ); - expect(controller.snapshot()?.inbox.filter(({ type }) => type === 'conflict')).toHaveLength(1); - - await Effect.runPromise(controller.acknowledgeInbox(fixture.ctx, { cursor: conflict.id })); - await Effect.runPromise(watcher.observeLifecycle(pullRequestId, observedPullRequest())); await Effect.runPromise( watcher.observeLifecycle(pullRequestId, observedPullRequest({ mergeable: 'conflicting' })), ); - expect(controller.snapshot()?.inbox.filter(({ type }) => type === 'conflict')).toHaveLength(1); + expect(controller.snapshot()?.inbox.filter(({ type }) => type === 'conflict')).toEqual([]); + expect(controller.snapshot()?.pullRequests[pullRequestId]?.conflictAttention).toMatchObject({ + generation: 1, + phase: 'conflicting', + }); + await Effect.runPromise(controller.shutdown(fixture.ctx)); + const restoredWatcher = manualGithubWatcher(); + const restoredGithub = stubGithub(); + const restoredWorkers = stubWorkers(); + const restored = new ManagerController(fixture.pi, { + github: restoredGithub.github, + githubWatcher: restoredWatcher.watcher, + makeWorkers: restoredWorkers.makeWorkers, + }); + await Effect.runPromise(restored.restore(fixture.ctx)); + await Effect.runPromise( + restoredWatcher.observeLifecycle( + pullRequestId, + observedPullRequest({ mergeable: 'unknown' }), + ), + ); + await Effect.runPromise( + restoredWatcher.observeLifecycle( + pullRequestId, + observedPullRequest({ mergeable: 'conflicting' }), + ), + ); + expect(restored.snapshot()?.inbox.filter(({ type }) => type === 'conflict')).toEqual([]); + + // Two consecutive complete mergeable observations confirm resolution; a + // later conflicting observation is a materially new generation. + await Effect.runPromise( + restoredWatcher.observe(pullRequestId, observedPullRequest({ mergeable: 'mergeable' })), + ); + await Effect.runPromise( + restoredWatcher.observe(pullRequestId, observedPullRequest({ mergeable: 'mergeable' })), + ); + expect(restored.snapshot()?.pullRequests[pullRequestId]?.conflictAttention).toMatchObject({ + generation: 1, + phase: 'resolved', + }); + await Effect.runPromise( + restoredWatcher.observeLifecycle( + pullRequestId, + observedPullRequest({ mergeable: 'conflicting' }), + ), + ); + const reappeared = requiredValue( + restored.snapshot()?.inbox.find(({ type }) => type === 'conflict'), + ); + expect(reappeared.id).not.toBe(firstConflict.id); + expect(restored.snapshot()?.pullRequests[pullRequestId]?.conflictAttention).toMatchObject({ + generation: 2, + phase: 'conflicting', + }); + expect( + managerEvents(activationStateDir(fixture.entries)).filter(({ type }) => type === 'conflict'), + ).toHaveLength(2); + + await Effect.runPromise(restored.acknowledgeInbox(fixture.ctx, { cursor: reappeared.id })); + const previousAuditedHead = restored.snapshot()?.pullRequests[pullRequestId]?.lastPushedHeadSha; + const worktree = requiredValue(agent.worktree); + writeFileSync(join(worktree.path, 'new-head.txt'), 'new audited generation\n'); + git(worktree.path, 'add', 'new-head.txt'); + git(worktree.path, 'commit', '-m', 'new audited conflict generation'); + await Effect.runPromise( + restoredWorkers.emit({ + agentId: agent.id, + lifecycleGeneration: restored.snapshot()?.agents[agent.id]?.lifecycleGeneration, + status: 'completed', + summary: 'Published a new audited head.', + type: 'report', + }), + ); + const report = requiredValue( + restored.snapshot()?.inbox.find(({ type }) => type === 'agent_report_completed'), + ); + expect(restoredGithub.syncs).toHaveLength(1); + expect(restored.snapshot()?.pullRequests[pullRequestId]?.lastPushedHeadSha).not.toBe( + previousAuditedHead, + ); + await Effect.runPromise(restored.acknowledgeInbox(fixture.ctx, { cursor: report.id })); + await Effect.runPromise( + restoredWatcher.observe(pullRequestId, observedPullRequest({ mergeable: 'mergeable' })), + ); + expect(restored.snapshot()?.pullRequests[pullRequestId]?.conflictAttention).toMatchObject({ + attentionObservedForKey: false, + generation: 2, + phase: 'resolution_candidate', + }); + await Effect.runPromise( + restoredWatcher.observe(pullRequestId, observedPullRequest({ mergeable: 'mergeable' })), + ); + expect(restored.snapshot()?.pullRequests[pullRequestId]?.conflictAttention).toMatchObject({ + attentionObservedForKey: false, + generation: 2, + phase: 'resolved', + }); + expect(restored.snapshot()?.inbox.filter(({ type }) => type === 'conflict')).toEqual([]); + await Effect.runPromise( + restoredWatcher.observeLifecycle( + pullRequestId, + observedPullRequest({ mergeable: 'conflicting' }), + ), + ); + expect(restored.snapshot()?.pullRequests[pullRequestId]?.conflictAttention).toMatchObject({ + generation: 3, + phase: 'conflicting', + }); + expect(restored.snapshot()?.inbox.filter(({ type }) => type === 'conflict')).toHaveLength(1); + expect( + managerEvents(activationStateDir(fixture.entries)).filter(({ type }) => type === 'conflict'), + ).toHaveLength(3); + await Effect.runPromise(restored.shutdown(fixture.ctx)); }); test('surfaces bounded remote-head divergence once until matching watcher metadata clears its durable warning', async () => { @@ -11285,6 +11416,171 @@ describe('manager controller', () => { ]); }); + test('coalesces terminal-report-derived stale verification context without disturbing a delivered cursor or queued suffix across restart', async () => { + const repo = fixtureRepository(); + const stateRoot = mkdtempSync(join(tmpdir(), 'pardes-state-')); + temporaryDirectories.push(stateRoot); + process.env.PARDES_PI_STATE_DIR = stateRoot; + const fixture = harness(repo); + fixture.setManagerIdle(true); + const workers = stubWorkers(); + const controller = new ManagerController(fixture.pi, { makeWorkers: workers.makeWorkers }); + await Effect.runPromise(controller.activate(fixture.ctx)); + const { agent } = await spawnManagedFixture(controller, fixture.ctx, repo, 'coalesced stale'); + const worktree = requiredValue(agent.worktree); + writeFileSync(join(worktree.path, 'reviewed.txt'), 'reviewed\n'); + git(worktree.path, 'add', 'reviewed.txt'); + git(worktree.path, 'commit', '-m', 'reviewed head'); + const verification = await Effect.runPromise( + controller.requestVerification({ sourceAgentId: agent.id }, fixture.ctx), + ); + + await Effect.runPromise( + workers.emit({ + agentId: agent.id, + question: 'Preserve this delivered cursor?', + type: 'question', + }), + ); + const deliveredQuestion = requiredValue(controller.snapshot()?.inboxWake); + const wakeMessageCount = fixture.messages.length; + const stateDirectory = activationStateDir(fixture.entries); + const eventPath = join(stateDirectory, 'events.jsonl'); + const eventSourceBeforeFailure = readFileSync(eventPath, 'utf8'); + rmSync(eventPath); + mkdirSync(eventPath); + + writeFileSync(join(worktree.path, 'fixed.txt'), 'new source head\n'); + git(worktree.path, 'add', 'fixed.txt'); + git(worktree.path, 'commit', '-m', 'advance reviewed source'); + await withoutConsoleError(() => + Effect.runPromise( + workers.emit({ + agentId: agent.id, + status: 'completed', + summary: 'Implemented the requested fix.', + type: 'report', + }), + ), + ); + + const afterReport = requiredValue(controller.snapshot()); + const report = requiredValue( + afterReport.inbox.find((event) => event.type === 'agent_report_completed'), + ); + expect(afterReport.inbox.map((event) => event.type)).toEqual([ + 'agent_question', + 'agent_report_completed', + ]); + expect(report.summary).toContain('[Pardes-derived context]'); + expect(report).toMatchObject({ + presentationBlocked: true, + presentationBlockedReason: 'verification_reconciliation', + }); + expect(report.coalescedVerificationEvidence).toEqual([ + expect.objectContaining({ + attempt: 1, + staleReasonCode: 'source_head_changed', + verificationId: verification.id, + }), + ]); + expect( + currentVerificationAttempt(requiredValue(afterReport.verifications[verification.id])), + ).toMatchObject({ + evidenceStatus: 'stale', + staleReasonCode: 'source_head_changed', + }); + expect(afterReport.inboxWake).toEqual(deliveredQuestion); + expect(projectInboxAttention(afterReport.inbox, afterReport.inboxWake).queuedSuffixCount).toBe( + 1, + ); + expect(fixture.messages).toHaveLength(wakeMessageCount); + const pendingAudits = Object.values(afterReport.auditIntents ?? {}); + const reportAudit = requiredValue(pendingAudits.find((event) => event.id === report.id)); + const staleAudit = requiredValue( + pendingAudits.find( + (event) => + event.type === 'verification_evidence_stale' && event.verificationId === verification.id, + ), + ); + expect(pendingAudits).toHaveLength(2); + + await Effect.runPromise(controller.shutdown(fixture.ctx)); + // Both append attempts failed after state persistence. Repair the external + // leaf, then restore must append each exact intent once before releasing the row. + rmSync(eventPath, { force: true, recursive: true }); + writeFileSync(eventPath, `${eventSourceBeforeFailure}{"id":"torn-audit`); + const statePath = join(stateDirectory, 'state.json'); + + const restored = new ManagerController(fixture.pi, { + makeWorkers: stubWorkers().makeWorkers, + }); + await Effect.runPromise(restored.restore(fixture.ctx)); + expect(restored.snapshot()?.auditIntents).toEqual({}); + expect(await Effect.runPromise(restored.inspectStorage())).toMatchObject({ + events: { + corruptionKind: 'malformed_trailing_fragment', + corruptionStatus: 'repaired', + }, + }); + expect(restored.snapshot()?.inboxWake).toEqual(deliveredQuestion); + expect(restored.snapshot()?.inbox.find((event) => event.id === report.id)).not.toHaveProperty( + 'presentationBlocked', + ); + expect(restored.snapshot()?.inbox.map((event) => event.id)).toEqual( + afterReport.inbox.map((event) => event.id), + ); + expect( + restored.snapshot()?.inbox.filter((event) => event.type === 'verification_evidence_stale'), + ).toEqual([]); + expect( + managerEvents(stateDirectory).filter((event) => event.id === reportAudit.id), + ).toHaveLength(1); + expect( + managerEvents(stateDirectory).filter((event) => event.id === staleAudit.id), + ).toHaveLength(1); + + await Effect.runPromise(restored.shutdown(fixture.ctx)); + // Simulate exit at both post-append/pre-intent-clear boundaries. Idempotent + // restore observes the existing identities, clears intents, and duplicates none. + const appendedButUncleared = JSON.parse(readFileSync(statePath, 'utf8')) as { + auditIntents?: Record; + }; + appendedButUncleared.auditIntents = { + [reportAudit.id]: reportAudit, + [staleAudit.id]: staleAudit, + }; + writeFileSync(statePath, `${JSON.stringify(appendedButUncleared, null, 2)}\n`); + const reloaded = new ManagerController(fixture.pi, { + makeWorkers: stubWorkers().makeWorkers, + }); + await Effect.runPromise(reloaded.restore(fixture.ctx)); + expect(reloaded.snapshot()?.auditIntents).toEqual({}); + expect( + managerEvents(stateDirectory).filter((event) => event.id === reportAudit.id), + ).toHaveLength(1); + expect( + managerEvents(stateDirectory).filter((event) => event.id === staleAudit.id), + ).toHaveLength(1); + + await Effect.runPromise( + reloaded.acknowledgeInbox(fixture.ctx, { cursor: deliveredQuestion.cursor }), + ); + expect(reloaded.snapshot()?.inbox.map((event) => event.id)).toEqual([report.id]); + expect(reloaded.snapshot()?.inboxWake?.cursor).toBe(report.id); + expect(JSON.stringify(fixture.messages.at(-1)?.message)).toContain( + 'Pardes stale verification context:1', + ); + await Effect.runPromise(reloaded.acknowledgeInbox(fixture.ctx, { cursor: report.id })); + expect(reloaded.snapshot()?.inbox).toEqual([]); + expect( + currentVerificationAttempt( + requiredValue(reloaded.snapshot()?.verifications[verification.id]), + ), + ).toMatchObject({ evidenceStatus: 'stale', staleReasonCode: 'source_head_changed' }); + await Effect.runPromise(reloaded.shutdown(fixture.ctx)); + }); + test('refreshes an idle retained scratch verifier at the latest clean source head with stale attempt lineage', async () => { const repo = fixtureRepository(); const stateRoot = mkdtempSync(join(tmpdir(), 'pardes-state-')); diff --git a/plugins/pardes-pi/extensions/pardes/manager/controller.ts b/plugins/pardes-pi/extensions/pardes/manager/controller.ts index a052ffa..b239ea3 100644 --- a/plugins/pardes-pi/extensions/pardes/manager/controller.ts +++ b/plugins/pardes-pi/extensions/pardes/manager/controller.ts @@ -812,6 +812,32 @@ export class ManagerController { ); }); + /** Append one durable intent idempotently, then clear only that exact persisted identity. */ + private readonly settleAuditIntent = Effect.fnUntraced(function* ( + this: ManagerController, + store: StateStoreShape, + event: ManagerEvent, + ) { + const appended = yield* store.appendEventOnce(event).pipe( + Effect.as(true), + Effect.catch((error) => + Effect.sync(() => { + console.error(`Pardes failed to settle audit intent ${event.type}`, error); + return false; + }), + ), + ); + if (!appended) return false; + yield* store.mutate((state) => { + if (state.auditIntents?.[event.id]?.id !== event.id) + return Effect.succeed([false, state] as const); + const auditIntents = { ...(state.auditIntents ?? {}) }; + delete auditIntents[event.id]; + return Effect.succeed([true, { ...state, auditIntents }] as const); + }); + return true; + }); + private cancelScheduledInboxWakeRelease(): void { if (this.inboxWakeReleaseTimer === undefined) return; clearTimeout(this.inboxWakeReleaseTimer); @@ -1018,6 +1044,7 @@ export class ManagerController { resumeWorkerEvents: (agentId) => { this.ignoredWorkerEvents.delete(agentId); }, + settleAuditIntent: (event) => this.settleAuditIntent(active.store, event), suppressWorkerEvents: (agentId) => { this.ignoredWorkerEvents.add(agentId); }, @@ -1045,13 +1072,15 @@ export class ManagerController { consumeWorkstreamCompletionIntent: (agentId, lifecycleGeneration) => this.consumeWorkstreamCompletionIntent(agentId, lifecycleGeneration), isSuppressed: (agentId) => this.ignoredWorkerEvents.has(agentId), - reconcileVerificationsForSource: (agentId) => verifications.reconcileForSource(agentId), + reconcileVerificationsForSource: (agentId, options) => + verifications.reconcileForSource(agentId, options), refresh: () => this.refreshActiveState(active), releaseInboxWake: () => this.releaseInboxWake(), render: () => this.render(), retryResolvedVerificationRetirementForIdleVerifier: (agentId) => verifications.retryResolvedRetirementForIdleVerifier(agentId), serializeVerificationMutation: (effect) => verifications.serializeMutation(effect), + settleAuditIntent: (event) => this.settleAuditIntent(active.store, event), }, liveRuntimes: this.liveRuntimes, namespace: active, @@ -1459,6 +1488,17 @@ export class ManagerController { workerEvents, }); this.active = active; + for (const event of Object.values(active.state.auditIntents ?? {})) + yield* this.settleAuditIntent(active.store, event); + yield* this.refreshActiveState(active, ctx); + for (const event of active.state.inbox.filter( + (candidate) => candidate.presentationBlockedReason === 'verification_reconciliation', + )) { + if (event.agentId === undefined) continue; + yield* verifications.reconcileForSource(event.agentId, { + coalesceIntoEventId: event.id, + }); + } yield* this.consumeRestoredWorkstreamCompletionIntents(ctx); this.render(ctx); yield* reviewGates.retirePersistedMergedPullRequests(); diff --git a/plugins/pardes-pi/extensions/pardes/manager/domain.ts b/plugins/pardes-pi/extensions/pardes/manager/domain.ts index 8329e2b..d6897f0 100644 --- a/plugins/pardes-pi/extensions/pardes/manager/domain.ts +++ b/plugins/pardes-pi/extensions/pardes/manager/domain.ts @@ -346,6 +346,14 @@ export function currentVerificationTerminalReportStatus( return status === 'completed' || status === 'blocked' ? status : undefined; } +export const CoalescedVerificationEvidenceSchema = Schema.Struct({ + attempt: Schema.Number.check(Schema.isInt(), Schema.isGreaterThan(0)), + staleReason: NonEmptyString.check(Schema.isMaxLength(VERIFICATION_STALE_REASON_MAX_CHARS)), + staleReasonCode: VerificationStaleReasonCodeSchema, + verificationId: NonEmptyString, +}); +export type CoalescedVerificationEvidence = typeof CoalescedVerificationEvidenceSchema.Type; + export const PullRequestObservationSchema = Schema.Struct({ ci: Schema.Literals(['unknown', 'pending', 'passing', 'failing']), mergeable: Schema.Literals(['unknown', 'mergeable', 'conflicting']), @@ -355,9 +363,22 @@ export const PullRequestObservationSchema = Schema.Struct({ }); export type PullRequestObservation = typeof PullRequestObservationSchema.Type; +export const PullRequestConflictAttentionSchema = Schema.Struct({ + /** Optional for snapshots written before key-supersession tracking. */ + attentionObservedForKey: Schema.optionalKey(Schema.Boolean), + auditedHeadSha: Schema.optionalKey(FullCommitShaSchema), + generation: Schema.Number.check(Schema.isInt(), Schema.isGreaterThan(0)), + ownerLifecycleGeneration: Schema.optionalKey( + Schema.Number.check(Schema.isInt(), Schema.isGreaterThan(0)), + ), + phase: Schema.Literals(['conflicting', 'resolution_candidate', 'resolved']), +}); +export type PullRequestConflictAttention = typeof PullRequestConflictAttentionSchema.Type; + export const PullRequestRecordSchema = Schema.Struct({ agentId: NonEmptyString, baseBranch: Schema.optionalKey(NonEmptyString), + conflictAttention: Schema.optionalKey(PullRequestConflictAttentionSchema), createdAt: NonEmptyString, discussionCursor: Schema.optionalKey(GitHubDiscussionCursorSchema), discussionPaginationGaps: Schema.optionalKey(GitHubDiscussionPaginationGapsSchema), @@ -382,6 +403,9 @@ export type PullRequestRecord = typeof PullRequestRecordSchema.Type; export const ManagerEventSchema = Schema.Struct({ agentId: Schema.optionalKey(NonEmptyString), + coalescedVerificationEvidence: Schema.optionalKey( + Schema.Array(CoalescedVerificationEvidenceSchema).check(Schema.isMaxLength(32)), + ), createdAt: NonEmptyString, /** Optional lossless non-report prose. Compact rows expose only a bounded structural pointer. */ details: Schema.optionalKey( @@ -440,6 +464,8 @@ export type InboxHandoff = typeof InboxHandoffSchema.Type; export const ManagerStateSchema = Schema.Struct({ agents: Schema.Record(Schema.String, AgentRecordSchema), + /** Durable exact-event intents repaired idempotently into append-only history. */ + auditIntents: Schema.optionalKey(Schema.Record(Schema.String, ManagerEventSchema)), /** Durable dedupe marker for one controller-scoped GitHub.com rate-metadata warning. */ githubRateMetadataUnavailableAt: Schema.optionalKey(NonEmptyString), inbox: Schema.Array(ManagerEventSchema), @@ -470,6 +496,7 @@ export type ManagerActivation = typeof ManagerActivationSchema.Type; export function initialManagerState(managerId: string, repo: RepoState): ManagerState { return { agents: {}, + auditIntents: {}, inbox: [], managerId, pullRequests: {}, diff --git a/plugins/pardes-pi/extensions/pardes/manager/inbox.test.ts b/plugins/pardes-pi/extensions/pardes/manager/inbox.test.ts index 514675d..1b06759 100644 --- a/plugins/pardes-pi/extensions/pardes/manager/inbox.test.ts +++ b/plugins/pardes-pi/extensions/pardes/manager/inbox.test.ts @@ -187,6 +187,31 @@ describe('manager inbox notification projection', () => { }); }); + test('surfaces coalesced stale-verification context on the causative report wake without adding a row', () => { + const report: ManagerEvent = { + agentId: 'agent-1', + coalescedVerificationEvidence: [ + { + attempt: 2, + staleReason: '[source_head_changed] source head changed', + staleReasonCode: 'source_head_changed', + verificationId: 'verify-1', + }, + ], + createdAt, + id: 'event-report', + reportId: 'report-1', + summary: '[Pardes-derived context] verify-1 evidence is stale. Worker completed.', + type: 'agent_report_completed', + }; + + const message = render([report]); + + expect(message.details.pendingCount).toBe(1); + expect(message.details.queuedSuffixCount).toBe(0); + expect(message.content).toContain('[child summary + Pardes stale verification context:1]'); + }); + test('renders mixed event provenance without injecting unsafe diagnostics or unknown summaries', () => { const message = render([ event('event-1', 'conflict', '#68 for agent-1 has merge conflicts.'), diff --git a/plugins/pardes-pi/extensions/pardes/manager/inbox.ts b/plugins/pardes-pi/extensions/pardes/manager/inbox.ts index a86423b..a146b34 100644 --- a/plugins/pardes-pi/extensions/pardes/manager/inbox.ts +++ b/plugins/pardes-pi/extensions/pardes/manager/inbox.ts @@ -208,7 +208,7 @@ function drillDownPointer(event: ManagerEvent): string { function digestSummary(event: ManagerEvent): string { if (CHILD_SUMMARY_EVENT_TYPES.has(event.type)) - return `[${childDigestLabel(event, 'summary')}] ${drillDownPointer(event)}`; + return `[${childDigestLabel(event, 'summary')}${event.coalescedVerificationEvidence?.length ? ` + Pardes stale verification context:${event.coalescedVerificationEvidence.length}` : ''}] ${drillDownPointer(event)}`; if (event.type === 'agent_question') return `[${childDigestLabel(event, 'question')}] ${drillDownPointer(event)}`; if (GITHUB_METADATA_EVENT_TYPES.has(event.type)) diff --git a/plugins/pardes-pi/extensions/pardes/manager/review-gate-lifecycle.ts b/plugins/pardes-pi/extensions/pardes/manager/review-gate-lifecycle.ts index 668dcd1..3bec48c 100644 --- a/plugins/pardes-pi/extensions/pardes/manager/review-gate-lifecycle.ts +++ b/plugins/pardes-pi/extensions/pardes/manager/review-gate-lifecycle.ts @@ -17,6 +17,7 @@ import type { AgentRecord, ManagerEvent, ManagerState, + PullRequestConflictAttention, PullRequestObservation, PullRequestRecord, } from './domain.ts'; @@ -112,6 +113,91 @@ function monotonicPullRequestObservation( return status === observed.status ? observed : { ...observed, status }; } +interface ConflictAttentionTransition { + readonly attention: boolean; + readonly next: PullRequestConflictAttention | undefined; +} + +/** Keep one conflict generation sticky across transient hosted mergeability projections. */ +export function conflictAttentionTransition( + pullRequest: PullRequestRecord, + owner: AgentRecord | undefined, + observation: PullRequestObservation, + complete: boolean, +): ConflictAttentionTransition { + const previous = pullRequest.conflictAttention; + const sameGeneration = + previous !== undefined && + previous.auditedHeadSha === pullRequest.lastPushedHeadSha && + previous.ownerLifecycleGeneration === owner?.lifecycleGeneration; + if (observation.mergeable === 'conflicting') { + const materiallyNew = + !sameGeneration || + previous?.phase === 'resolved' || + previous?.attentionObservedForKey === false; + return { + attention: materiallyNew, + next: { + attentionObservedForKey: true, + ...(pullRequest.lastPushedHeadSha === undefined + ? {} + : { auditedHeadSha: pullRequest.lastPushedHeadSha }), + generation: materiallyNew ? (previous?.generation ?? 0) + 1 : (previous?.generation ?? 1), + ...(owner?.lifecycleGeneration === undefined + ? {} + : { ownerLifecycleGeneration: owner.lifecycleGeneration }), + phase: 'conflicting', + }, + }; + } + if (previous === undefined) return { attention: false, next: previous }; + if (!sameGeneration) { + if (observation.mergeable !== 'mergeable' || !complete) + return { attention: false, next: previous }; + return { + attention: false, + next: { + attentionObservedForKey: false, + ...(pullRequest.lastPushedHeadSha === undefined + ? {} + : { auditedHeadSha: pullRequest.lastPushedHeadSha }), + generation: previous.generation, + ...(owner?.lifecycleGeneration === undefined + ? {} + : { ownerLifecycleGeneration: owner.lifecycleGeneration }), + phase: 'resolution_candidate', + }, + }; + } + if (observation.mergeable === 'unknown') + return { + attention: false, + next: + previous.phase === 'resolution_candidate' + ? { ...previous, phase: 'conflicting' } + : previous, + }; + if (!complete) return { attention: false, next: previous }; + if (previous.phase === 'conflicting') + return { attention: false, next: { ...previous, phase: 'resolution_candidate' } }; + if (previous.phase === 'resolution_candidate') + return { attention: false, next: { ...previous, phase: 'resolved' } }; + return { attention: false, next: previous }; +} + +function conflictAttentionEqual( + left: PullRequestConflictAttention | undefined, + right: PullRequestConflictAttention | undefined, +): boolean { + return ( + left?.attentionObservedForKey === right?.attentionObservedForKey && + left?.auditedHeadSha === right?.auditedHeadSha && + left?.generation === right?.generation && + left?.ownerLifecycleGeneration === right?.ownerLifecycleGeneration && + left?.phase === right?.phase + ); +} + function watcherEventMatchesAssociation( pullRequest: PullRequestRecord, expectedHeadSha: string | undefined, @@ -853,6 +939,12 @@ export const makeReviewGateLifecycleCoordinator = Effect.fnUntraced(function* ( // bounded discussion surfaces finish without clearing an outage warning. const complete = event.complete !== false; const nextObservation = monotonicPullRequestObservation(known, event.observation); + const nextConflictAttention = conflictAttentionTransition( + known, + namespace.state.agents[known.agentId], + nextObservation, + complete, + ); const discussionPaginationGaps = event.discussion === undefined ? (known.discussionPaginationGaps ?? []) @@ -870,6 +962,7 @@ export const makeReviewGateLifecycleCoordinator = Effect.fnUntraced(function* ( ); const changed = !pullRequestObservationsEqual(known.observation, nextObservation) || + !conflictAttentionEqual(known.conflictAttention, nextConflictAttention.next) || (discussionCursor !== undefined && !githubDiscussionCursorsEqual(known.discussionCursor, discussionCursor)) || !githubDiscussionPaginationGapsEqual( @@ -898,10 +991,39 @@ export const makeReviewGateLifecycleCoordinator = Effect.fnUntraced(function* ( if (!pullRequest || !watcherEventMatchesAssociation(pullRequest, event.expectedHeadSha)) return Effect.succeed([[] as ReadonlyArray, state] as const); const nextObservation = monotonicPullRequestObservation(pullRequest, event.observation); - const nextTransitions = derivePullRequestTransitions( + const conflictTransition = conflictAttentionTransition( + pullRequest, + state.agents[pullRequest.agentId], + nextObservation, + complete, + ); + const derivedTransitions = derivePullRequestTransitions( pullRequest.observation, nextObservation, - ).filter( + ); + const conflictPosition = derivedTransitions.indexOf('conflict'); + const terminalPosition = derivedTransitions.findIndex( + (transition) => transition === 'merged' || transition === 'closed_unmerged', + ); + const transitionsWithConflict = + conflictPosition >= 0 + ? derivedTransitions.flatMap((transition) => + transition === 'conflict' + ? conflictTransition.attention + ? (['conflict'] as const) + : [] + : [transition], + ) + : conflictTransition.attention + ? [ + ...(terminalPosition < 0 + ? derivedTransitions + : derivedTransitions.slice(0, terminalPosition)), + 'conflict' as const, + ...(terminalPosition < 0 ? [] : derivedTransitions.slice(terminalPosition)), + ] + : derivedTransitions; + const nextTransitions = transitionsWithConflict.filter( (transition) => (transition !== 'merged' && transition !== 'closed_unmerged') || pullRequest.status !== nextObservation.status, @@ -951,6 +1073,9 @@ export const makeReviewGateLifecycleCoordinator = Effect.fnUntraced(function* ( } = watcherCleared; const nextPullRequest: PullRequestRecord = { ...withoutDiscussionPaginationGaps, + ...(conflictTransition.next === undefined + ? {} + : { conflictAttention: conflictTransition.next }), number: nextObservation.number, observation: nextObservation, status: nextObservation.status, diff --git a/plugins/pardes-pi/extensions/pardes/manager/verification/contracts.ts b/plugins/pardes-pi/extensions/pardes/manager/verification/contracts.ts index f0f8beb..6408e1b 100644 --- a/plugins/pardes-pi/extensions/pardes/manager/verification/contracts.ts +++ b/plugins/pardes-pi/extensions/pardes/manager/verification/contracts.ts @@ -26,6 +26,7 @@ export interface VerificationLifecycleNamespace extends ManagerNamespaceContext export interface VerificationLifecycleCallbacks { readonly refresh: (ctx?: ExtensionContext) => Effect.Effect; readonly appendEventSafely: (event: ManagerEvent) => Effect.Effect; + readonly settleAuditIntent: (event: ManagerEvent) => Effect.Effect; readonly releaseInboxWake: () => Effect.Effect; readonly defaultModel: (ctx?: ExtensionContext) => string | undefined; readonly defaultThinkingLevel: () => WorkerThinkingLevel; diff --git a/plugins/pardes-pi/extensions/pardes/manager/verification/evidence.ts b/plugins/pardes-pi/extensions/pardes/manager/verification/evidence.ts index 5263072..3fd1690 100644 --- a/plugins/pardes-pi/extensions/pardes/manager/verification/evidence.ts +++ b/plugins/pardes-pi/extensions/pardes/manager/verification/evidence.ts @@ -2,6 +2,7 @@ import { Effect, Exit } from 'effect'; import type { WorktreeInspection } from '../../git/index.ts'; import { type AgentRecord, + type CoalescedVerificationEvidence, currentVerificationAttempt, type VerificationRecord, type VerificationStaleReasonCode, @@ -9,6 +10,7 @@ import { } from '../domain.ts'; import { VerificationRequestRejectedError } from '../errors.ts'; import { managedLeaseOwner, validateRetainedAgentState } from '../namespace.ts'; +import { boundedEventSummary } from '../worker-events.ts'; import type { VerificationLifecycleCoordinatorOptions } from './contracts.ts'; import { makeVerificationEvent, @@ -28,7 +30,10 @@ export interface VerificationEvidenceReconcilerShape { readonly inspectSource: ( sourceAgentId: string, ) => Effect.Effect; - readonly reconcile: (verification: VerificationRecord) => Effect.Effect; + readonly reconcile: ( + verification: VerificationRecord, + options?: { readonly coalesceIntoEventId?: string }, + ) => Effect.Effect; } function rejected(sourceAgentId: string, reason: string): VerificationRequestRejectedError { @@ -65,6 +70,7 @@ export function makeVerificationEvidenceReconciler( verification: VerificationRecord, reasonCode: VerificationStaleReasonCode, detail?: string, + coalesceIntoEventId?: string, ) { if (currentVerificationAttempt(verification).evidenceStatus === 'stale') return; const timestamp = yield* nowIso; @@ -78,15 +84,54 @@ export function makeVerificationEvidenceReconciler( workstreamId: verification.workstreamId, }, ); - const changed = yield* namespace.store.mutate((state) => { + const outcome = yield* namespace.store.mutate< + { readonly changed: boolean; readonly coalesced: boolean }, + never + >((state) => { const current = state.verifications[verification.id]; if (!current || currentVerificationAttempt(current).evidenceStatus === 'stale') - return Effect.succeed([false, state] as const); + return Effect.succeed([{ changed: false, coalesced: false }, state] as const); + const currentAttempt = currentVerificationAttempt(current); + const parentIndex = + coalesceIntoEventId === undefined + ? -1 + : state.inbox.findIndex( + (candidate) => + candidate.id === coalesceIntoEventId && + (candidate.type === 'agent_report_completed' || + candidate.type === 'agent_report_blocked') && + candidate.agentId === verification.sourceAgentId && + (candidate.coalescedVerificationEvidence?.length ?? 0) < 32, + ); + const coalesced = parentIndex >= 0; + const inbox = coalesced + ? state.inbox.map((candidate, index) => { + if (index !== parentIndex) return candidate; + const evidence: CoalescedVerificationEvidence = { + attempt: currentAttempt.attempt, + staleReason: verificationStaleReason(reasonCode, detail), + staleReasonCode: reasonCode, + verificationId: verification.id, + }; + return { + ...candidate, + coalescedVerificationEvidence: [ + ...(candidate.coalescedVerificationEvidence ?? []), + evidence, + ], + summary: boundedEventSummary([ + `[Pardes-derived context] ${verification.id} attempt ${currentAttempt.attempt} evidence is stale: ${evidence.staleReason}.`, + candidate.summary, + ]), + }; + }) + : [...state.inbox, event]; return Effect.succeed([ - true, + { changed: true, coalesced }, { ...state, - inbox: [...state.inbox, event], + auditIntents: { ...(state.auditIntents ?? {}), [event.id]: event }, + inbox, verifications: { ...state.verifications, [verification.id]: withStaleCurrentEvidence(current, reasonCode, timestamp, detail), @@ -94,18 +139,30 @@ export function makeVerificationEvidenceReconciler( }, ] as const); }); - if (!changed) return; - yield* callbacks.appendEventSafely(event); + if (!outcome.changed) return; + // The stale fact remains append-only even when its derivative user attention + // is folded into the causative terminal-report row. Its exact identity stays + // durable until the idempotent append succeeds. + yield* callbacks.settleAuditIntent(event); yield* callbacks.refresh(); - yield* callbacks.releaseInboxWake().pipe(Effect.catch(() => Effect.succeed(false))); + if (!outcome.coalesced) + yield* callbacks.releaseInboxWake().pipe(Effect.catch(() => Effect.succeed(false))); }); - const reconcile = Effect.fnUntraced(function* (verification: VerificationRecord) { + const reconcile = Effect.fnUntraced(function* ( + verification: VerificationRecord, + options?: { readonly coalesceIntoEventId?: string }, + ) { const attempt = currentVerificationAttempt(verification); if (attempt.evidenceStatus === 'stale') return; const sourceResult = yield* inspectSource(verification.sourceAgentId).pipe(Effect.exit); if (Exit.isFailure(sourceResult)) { - yield* markStale(verification, 'source_unverifiable'); + yield* markStale( + verification, + 'source_unverifiable', + undefined, + options?.coalesceIntoEventId, + ); return; } const source = sourceResult.value.inspected; @@ -114,11 +171,12 @@ export function makeVerificationEvidenceReconciler( verification, 'source_head_changed', `from ${attempt.reviewedHeadSha} to ${source.headSha}`, + options?.coalesceIntoEventId, ); return; } if (source.dirty) { - yield* markStale(verification, 'source_dirty'); + yield* markStale(verification, 'source_dirty', undefined, options?.coalesceIntoEventId); return; } const review = yield* worktrees @@ -128,15 +186,30 @@ export function makeVerificationEvidenceReconciler( ) .pipe(Effect.exit); if (Exit.isFailure(review)) { - yield* markStale(verification, 'review_checkout_unverifiable'); + yield* markStale( + verification, + 'review_checkout_unverifiable', + undefined, + options?.coalesceIntoEventId, + ); return; } if (review.value.headSha !== attempt.reviewedHeadSha) { - yield* markStale(verification, 'review_checkout_head_changed'); + yield* markStale( + verification, + 'review_checkout_head_changed', + undefined, + options?.coalesceIntoEventId, + ); return; } if (review.value.dirty) { - yield* markStale(verification, 'review_checkout_dirty'); + yield* markStale( + verification, + 'review_checkout_dirty', + undefined, + options?.coalesceIntoEventId, + ); } }); diff --git a/plugins/pardes-pi/extensions/pardes/manager/verification/index.test.ts b/plugins/pardes-pi/extensions/pardes/manager/verification/index.test.ts index 08a67a6..3d5841d 100644 --- a/plugins/pardes-pi/extensions/pardes/manager/verification/index.test.ts +++ b/plugins/pardes-pi/extensions/pardes/manager/verification/index.test.ts @@ -360,6 +360,7 @@ async function verificationFixture(options: VerificationFixtureOptions = {}) { resumeWorkerEvents: (agentId) => { ignoredEvents.delete(agentId); }, + settleAuditIntent: () => Effect.succeed(true), suppressWorkerEvents: (agentId) => { ignoredEvents.add(agentId); }, diff --git a/plugins/pardes-pi/extensions/pardes/manager/verification/index.ts b/plugins/pardes-pi/extensions/pardes/manager/verification/index.ts index 28023dc..0788a1a 100644 --- a/plugins/pardes-pi/extensions/pardes/manager/verification/index.ts +++ b/plugins/pardes-pi/extensions/pardes/manager/verification/index.ts @@ -36,7 +36,10 @@ export interface VerificationLifecycleCoordinatorShape { verificationId: string, ctx?: ExtensionContext, ) => Effect.Effect; - readonly reconcileForSource: (sourceAgentId: string) => Effect.Effect; + readonly reconcileForSource: ( + sourceAgentId: string, + options?: { readonly coalesceIntoEventId?: string }, + ) => Effect.Effect; readonly retireResolvedForSource: (sourceAgentId: string) => Effect.Effect; readonly retryResolvedRetirementForIdleVerifier: ( verifierAgentId: string, @@ -86,14 +89,46 @@ export const makeVerificationLifecycleCoordinator = Effect.fnUntraced(function* ); const reconcileForSourceUnlocked: VerificationLifecycleCoordinatorShape['reconcileForSource'] = - Effect.fnUntraced(function* (sourceAgentId) { + Effect.fnUntraced(function* (sourceAgentId, reconcileOptions) { yield* callbacks.refresh(); const verifications = Object.values(namespace.state.verifications).filter( (verification) => verification.sourceAgentId === sourceAgentId && currentVerificationAttempt(verification).evidenceStatus === 'current', ); - for (const verification of verifications) yield* evidence.reconcile(verification); + for (const verification of verifications) + yield* evidence.reconcile(verification, reconcileOptions); + if (reconcileOptions?.coalesceIntoEventId !== undefined) { + const refined = yield* namespace.store.mutate((state) => { + let changed = false; + const inbox = state.inbox.map((event) => { + if ( + event.id !== reconcileOptions.coalesceIntoEventId || + event.presentationBlockedReason !== 'verification_reconciliation' + ) + return event; + const pendingAudit = + state.auditIntents?.[event.id] !== undefined || + (event.coalescedVerificationEvidence ?? []).some((evidence) => + Object.values(state.auditIntents ?? {}).some( + (intent) => + intent.type === 'verification_evidence_stale' && + intent.verificationId === evidence.verificationId, + ), + ); + if (pendingAudit) return event; + const { + presentationBlocked: _presentationBlocked, + presentationBlockedReason: _presentationBlockedReason, + ...ready + } = event; + changed = true; + return ready; + }); + return Effect.succeed([changed, changed ? { ...state, inbox } : state] as const); + }); + if (refined) yield* callbacks.refresh(); + } }); const retireResolvedForSourceUnlocked: VerificationLifecycleCoordinatorShape['retireResolvedForSource'] = @@ -132,7 +167,8 @@ export const makeVerificationLifecycleCoordinator = Effect.fnUntraced(function* serializeMutation(retirement.stopIdleForWorkstreamCompletion(verifierAgentId, ctx)); const reconcileForSource: VerificationLifecycleCoordinatorShape['reconcileForSource'] = ( sourceAgentId, - ) => serializeMutation(reconcileForSourceUnlocked(sourceAgentId)); + reconcileOptions, + ) => serializeMutation(reconcileForSourceUnlocked(sourceAgentId, reconcileOptions)); const retireResolvedForSource: VerificationLifecycleCoordinatorShape['retireResolvedForSource'] = (sourceAgentId) => serializeMutation(retireResolvedForSourceUnlocked(sourceAgentId)); diff --git a/plugins/pardes-pi/extensions/pardes/manager/worker-event-coordinator.test.ts b/plugins/pardes-pi/extensions/pardes/manager/worker-event-coordinator.test.ts index 6e6a5fc..5e139d6 100644 --- a/plugins/pardes-pi/extensions/pardes/manager/worker-event-coordinator.test.ts +++ b/plugins/pardes-pi/extensions/pardes/manager/worker-event-coordinator.test.ts @@ -87,6 +87,11 @@ describe('incoming worker-event coordinator', () => { render: () => {}, retryResolvedVerificationRetirementForIdleVerifier: () => Effect.succeed(false), serializeVerificationMutation: (effect) => effect, + settleAuditIntent: (event) => + Effect.sync(() => { + trace.push(`append:${event.type}`); + return true; + }), }, liveRuntimes: new Map(), namespace, @@ -233,6 +238,11 @@ describe('incoming worker-event coordinator', () => { render: () => {}, retryResolvedVerificationRetirementForIdleVerifier: () => Effect.succeed(false), serializeVerificationMutation: (effect) => effect, + settleAuditIntent: (event) => + Effect.sync(() => { + appended.push(event.type); + return true; + }), }, liveRuntimes: new Map(), namespace, @@ -437,6 +447,7 @@ describe('incoming worker-event coordinator', () => { Effect.andThen(effect), ), ), + settleAuditIntent: () => Effect.succeed(true), }, liveRuntimes: new Map(), namespace, @@ -617,6 +628,7 @@ describe('incoming worker-event coordinator', () => { Effect.sync(() => { serializedMutations += 1; }).pipe(Effect.andThen(effect)), + settleAuditIntent: () => Effect.succeed(true), }, liveRuntimes, namespace, @@ -801,6 +813,7 @@ describe('incoming worker-event coordinator', () => { Effect.andThen(Deferred.await(releaseSerialization)), Effect.andThen(effect), ), + settleAuditIntent: () => Effect.succeed(true), }, liveRuntimes: new Map(), namespace, diff --git a/plugins/pardes-pi/extensions/pardes/manager/worker-event-coordinator.ts b/plugins/pardes-pi/extensions/pardes/manager/worker-event-coordinator.ts index f7bf6c2..ef64b54 100644 --- a/plugins/pardes-pi/extensions/pardes/manager/worker-event-coordinator.ts +++ b/plugins/pardes-pi/extensions/pardes/manager/worker-event-coordinator.ts @@ -72,7 +72,10 @@ interface WorkerEventFollowUp { readonly workstreamId: string; }; readonly releaseInboxWake?: boolean; - readonly reconcileVerificationsForSource?: string; + readonly reconcileVerificationsForSource?: { + readonly sourceAgentId: string; + readonly coalesceIntoEventId?: string; + }; readonly syncCompletedReport?: string; } @@ -116,13 +119,17 @@ export class WorkerSupervisorEventCoordinator extends Context.Service< export interface WorkerSupervisorEventCoordinatorCallbacks { readonly refresh: () => Effect.Effect; readonly appendEventSafely: (event: ManagerEvent) => Effect.Effect; + readonly settleAuditIntent: (event: ManagerEvent) => Effect.Effect; readonly releaseInboxWake: () => Effect.Effect; readonly render: () => void; readonly isSuppressed: (agentId: string) => boolean; readonly serializeVerificationMutation: ( effect: Effect.Effect, ) => Effect.Effect; - readonly reconcileVerificationsForSource: (agentId: string) => Effect.Effect; + readonly reconcileVerificationsForSource: ( + agentId: string, + options?: { readonly coalesceIntoEventId?: string }, + ) => Effect.Effect; readonly retryResolvedVerificationRetirementForIdleVerifier: ( agentId: string, ) => Effect.Effect; @@ -305,12 +312,31 @@ export const makeWorkerSupervisorEventCoordinator = Effect.fnUntraced(function* } : {}), }; + const awaitsVerificationReconciliation = + workerEvent.type === 'report' && + workerEvent.status !== 'progress' && + persistedAgent.role === 'worker' && + Object.values(namespace.state.verifications).some( + (verification) => + verification.sourceAgentId === workerEvent.agentId && + currentVerificationAttempt(verification).evidenceStatus === 'current', + ); const attention = event?.actionable ? { ...makeEvent(event.type, event.summary, timestamp, association), ...(event.details === undefined ? {} : { details: event.details }), + ...(awaitsVerificationReconciliation + ? { + presentationBlocked: true, + presentationBlockedReason: 'verification_reconciliation', + } + : {}), } : undefined; + const reportAuditEvent = + workerEvent.type === 'report' && event !== undefined + ? (attention ?? makeEvent(event.type, event.summary, timestamp, association)) + : undefined; const projection = yield* namespace.store.mutate((state) => { const agent = state.agents[workerEvent.agentId]; if (!agent) @@ -416,6 +442,10 @@ export const makeWorkerSupervisorEventCoordinator = Effect.fnUntraced(function* { ...state, agents: { ...state.agents, [agent.id]: nextAgent }, + auditIntents: + event !== undefined && !duplicateAttention && reportAuditEvent !== undefined + ? { ...(state.auditIntents ?? {}), [reportAuditEvent.id]: reportAuditEvent } + : state.auditIntents, inbox: enqueue && attention ? [...state.inbox, attention] : state.inbox, verifications: nextVerification === undefined @@ -426,10 +456,13 @@ export const makeWorkerSupervisorEventCoordinator = Effect.fnUntraced(function* ] as const); }); if (!projection.changed) return; - if (event && projection.append) - yield* callbacks.appendEventSafely( - attention ?? makeEvent(event.type, event.summary, timestamp, association), - ); + if (event && projection.append) { + if (reportAuditEvent) yield* callbacks.settleAuditIntent(reportAuditEvent); + else + yield* callbacks.appendEventSafely( + attention ?? makeEvent(event.type, event.summary, timestamp, association), + ); + } if (projection.cancelledCompletionIntent) yield* callbacks.appendEventSafely( makeEvent( @@ -481,11 +514,18 @@ export const makeWorkerSupervisorEventCoordinator = Effect.fnUntraced(function* ? { releaseInboxWake: true } : {}), ...(workerEvent.type === 'report' && - workerEvent.status === 'completed' && + workerEvent.status !== 'progress' && persistedAgent.role === 'worker' ? { - reconcileVerificationsForSource: workerEvent.agentId, - syncCompletedReport: workerEvent.agentId, + reconcileVerificationsForSource: { + sourceAgentId: workerEvent.agentId, + ...(projection.enqueued && attention !== undefined + ? { coalesceIntoEventId: attention.id } + : {}), + }, + ...(workerEvent.status === 'completed' + ? { syncCompletedReport: workerEvent.agentId } + : {}), } : {}), } satisfies WorkerEventFollowUp; @@ -510,9 +550,18 @@ export const makeWorkerSupervisorEventCoordinator = Effect.fnUntraced(function* yield* callbacks.retryResolvedVerificationRetirementForIdleVerifier(agentId); if (retiredVerifier) yield* reviewGates.retryMergedRetirementForWorkstream(workstreamId); } - if (followUp.releaseInboxWake) yield* callbacks.releaseInboxWake(); if (followUp.reconcileVerificationsForSource) - yield* callbacks.reconcileVerificationsForSource(followUp.reconcileVerificationsForSource); + yield* callbacks.reconcileVerificationsForSource( + followUp.reconcileVerificationsForSource.sourceAgentId, + followUp.reconcileVerificationsForSource.coalesceIntoEventId === undefined + ? undefined + : { + coalesceIntoEventId: followUp.reconcileVerificationsForSource.coalesceIntoEventId, + }, + ); + // Reconcile derivative verification state before releasing the causative + // report cursor so one wake covers the complete software-owned context. + if (followUp.releaseInboxWake) yield* callbacks.releaseInboxWake(); if (followUp.syncCompletedReport) yield* pullRequests.syncCompletedReport(followUp.syncCompletedReport); }); diff --git a/plugins/pardes-pi/extensions/pardes/manager/worker-events.test.ts b/plugins/pardes-pi/extensions/pardes/manager/worker-events.test.ts index 9125374..208162a 100644 --- a/plugins/pardes-pi/extensions/pardes/manager/worker-events.test.ts +++ b/plugins/pardes-pi/extensions/pardes/manager/worker-events.test.ts @@ -476,7 +476,9 @@ describe('manager handoff-audit policy', () => { ); expect(projected?.summary).toContain('Total audited change set: 513 paths'); expect(projected?.summary.length).toBeLessThanOrEqual(900); - expect(projected?.summary).toContain('durable report available'); + expect(projected?.summary).toContain( + 'canonical full report available via one report_get({ reportId }) call', + ); }); test('renders only failed report-persistence suffixes', () => { diff --git a/plugins/pardes-pi/extensions/pardes/storage/event-log.ts b/plugins/pardes-pi/extensions/pardes/storage/event-log.ts new file mode 100644 index 0000000..1e45a00 --- /dev/null +++ b/plugins/pardes-pi/extensions/pardes/storage/event-log.ts @@ -0,0 +1,248 @@ +import { randomUUID } from 'node:crypto'; +import { createReadStream } from 'node:fs'; +import { copyFile, open, readFile, rename, rm, stat, writeFile } from 'node:fs/promises'; +import { basename, dirname, join } from 'node:path'; +import { Effect } from 'effect'; +import { fsPromise } from './filesystem.ts'; +import { STORAGE_EVENT_WRITE_MAX_BYTES } from './state-limits.ts'; + +const PENDING_RAW_SUFFIX = '.corrupt-pending'; +const PENDING_METADATA_SUFFIX = '.corrupt-pending.json'; +export const EVENT_CORRUPTION_STATUS_SUFFIX = '.corruption-status.json'; + +export type EventLogCorruptionKind = 'malformed_trailing_fragment' | 'interior_corruption'; + +interface EventLogRepairMetadata { + readonly classification: EventLogCorruptionKind | 'interrupted_repair'; + readonly malformedLines: number; + readonly originalBytes: number; + readonly repairId: string; + readonly repairedAt: string; + readonly retainedValidLines: number; + readonly version: 1; +} + +export interface EventLogIdentityScan { + readonly exists: boolean; + readonly repair?: { + readonly classification: EventLogCorruptionKind; + readonly malformedLines: number; + readonly originalBytes: number; + readonly retainedValidLines: number; + }; +} + +interface ParsedEventLog { + readonly exists: boolean; + readonly malformedLines: number; + readonly originalBytes: number; + readonly retainedValidLines: number; + readonly trailingFragment: boolean; +} + +function missing(cause: unknown): boolean { + return ( + typeof cause === 'object' && + cause !== null && + 'code' in cause && + (cause.code === 'ENOENT' || cause.code === 'ENOTDIR') + ); +} + +async function parseEventLog( + path: string, + eventId: string, + validLine?: (line: Buffer) => Promise, +): Promise { + let exists = false; + let malformedLines = 0; + let originalBytes = 0; + let retainedValidLines = 0; + let pending = Buffer.alloc(0); + let oversized = false; + let lastNonemptyMalformed = false; + let endedWithNewline = false; + + const consume = async (terminated: boolean) => { + if (pending.length === 0 && !oversized) { + endedWithNewline = terminated; + return; + } + let valid = false; + if (!oversized) { + try { + const decoded: unknown = JSON.parse(pending.toString('utf8')); + if ( + typeof decoded === 'object' && + decoded !== null && + 'id' in decoded && + decoded.id === eventId + ) + exists = true; + valid = true; + } catch { + // Classified below without retaining unbounded malformed input in memory. + } + } + if (valid) { + retainedValidLines += 1; + lastNonemptyMalformed = false; + if (validLine) await validLine(pending); + } else { + malformedLines += 1; + lastNonemptyMalformed = true; + } + pending = Buffer.alloc(0); + oversized = false; + endedWithNewline = terminated; + }; + + try { + for await (const chunk of createReadStream(path)) { + const bytes = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk); + originalBytes += bytes.length; + let offset = 0; + while (offset < bytes.length) { + const newline = bytes.indexOf(10, offset); + const end = newline < 0 ? bytes.length : newline; + if (!oversized && end > offset) { + const nextLength = pending.length + end - offset; + if (nextLength > STORAGE_EVENT_WRITE_MAX_BYTES) { + pending = Buffer.alloc(0); + oversized = true; + } else { + pending = Buffer.concat([pending, bytes.subarray(offset, end)], nextLength); + } + } + if (newline < 0) break; + await consume(true); + offset = newline + 1; + } + } + } catch (cause) { + if (missing(cause)) + return { + exists: false, + malformedLines: 0, + originalBytes: 0, + retainedValidLines: 0, + trailingFragment: false, + }; + throw cause; + } + if (pending.length > 0 || oversized) await consume(false); + return { + exists, + malformedLines, + originalBytes, + retainedValidLines, + trailingFragment: malformedLines === 1 && lastNonemptyMalformed && !endedWithNewline, + }; +} + +function repairPaths(eventPath: string, repairId: string) { + const raw = `${eventPath}.corrupt-${repairId}`; + return { metadata: `${raw}.json`, raw }; +} + +async function finalizePendingRepair(eventPath: string): Promise { + const pendingRaw = `${eventPath}${PENDING_RAW_SUFFIX}`; + const pendingMetadata = `${eventPath}${PENDING_METADATA_SUFFIX}`; + let rawExists = true; + try { + await stat(pendingRaw); + } catch (cause) { + if (!missing(cause)) throw cause; + rawExists = false; + } + if (!rawExists) return; + let metadata: EventLogRepairMetadata; + try { + metadata = JSON.parse(await readFile(pendingMetadata, 'utf8')) as EventLogRepairMetadata; + } catch { + metadata = { + classification: 'interrupted_repair', + malformedLines: 0, + originalBytes: (await stat(pendingRaw)).size, + repairedAt: new Date().toISOString(), + repairId: randomUUID(), + retainedValidLines: 0, + version: 1, + }; + } + const finalized = repairPaths(eventPath, metadata.repairId); + try { + await rename(pendingRaw, finalized.raw); + } catch (cause) { + if (!missing(cause)) throw cause; + } + await writeFile(finalized.metadata, `${JSON.stringify(metadata, null, 2)}\n`, 'utf8'); + await writeFile( + `${eventPath}${EVENT_CORRUPTION_STATUS_SUFFIX}`, + `${JSON.stringify(metadata, null, 2)}\n`, + 'utf8', + ); + await rm(pendingMetadata, { force: true }); +} + +/** + * Scan one JSONL event stream with bounded line memory. If malformed data is + * found, preserve the complete original bytes, rebuild all parseable records, + * and atomically replace the active stream before returning. + */ +export const scanAndRepairEventIdentity = Effect.fnUntraced(function* ( + eventPath: string, + eventId: string, +) { + return yield* fsPromise('scan and repair event identities', eventPath, async () => { + await finalizePendingRepair(eventPath); + const scan = await parseEventLog(eventPath, eventId); + if (scan.malformedLines === 0) return { exists: scan.exists }; + + const temporaryPath = join( + dirname(eventPath), + `.${basename(eventPath)}.${randomUUID()}.repair`, + ); + const output = await open(temporaryPath, 'wx'); + try { + await parseEventLog(eventPath, eventId, async (line) => { + await output.write(line); + await output.write('\n'); + }); + } catch (cause) { + await rm(temporaryPath, { force: true }); + throw cause; + } finally { + await output.close(); + } + + const classification: EventLogCorruptionKind = scan.trailingFragment + ? 'malformed_trailing_fragment' + : 'interior_corruption'; + const repairId = randomUUID(); + const metadata: EventLogRepairMetadata = { + classification, + malformedLines: scan.malformedLines, + originalBytes: scan.originalBytes, + repairedAt: new Date().toISOString(), + repairId, + retainedValidLines: scan.retainedValidLines, + version: 1, + }; + const pendingRaw = `${eventPath}${PENDING_RAW_SUFFIX}`; + const pendingMetadata = `${eventPath}${PENDING_METADATA_SUFFIX}`; + await copyFile(eventPath, pendingRaw); + await writeFile(pendingMetadata, `${JSON.stringify(metadata, null, 2)}\n`, 'utf8'); + await rename(temporaryPath, eventPath); + await finalizePendingRepair(eventPath); + return { + exists: scan.exists, + repair: { + classification, + malformedLines: scan.malformedLines, + originalBytes: scan.originalBytes, + retainedValidLines: scan.retainedValidLines, + }, + }; + }); +}); diff --git a/plugins/pardes-pi/extensions/pardes/storage/inspection.ts b/plugins/pardes-pi/extensions/pardes/storage/inspection.ts index 61c2750..6485e86 100644 --- a/plugins/pardes-pi/extensions/pardes/storage/inspection.ts +++ b/plugins/pardes-pi/extensions/pardes/storage/inspection.ts @@ -3,6 +3,7 @@ import { lstat, open, opendir } from 'node:fs/promises'; import { join } from 'node:path'; import { Effect } from 'effect'; import { storeError } from './errors.ts'; +import { EVENT_CORRUPTION_STATUS_SUFFIX, type EventLogCorruptionKind } from './event-log.ts'; import { fsPromise } from './filesystem.ts'; export const STORAGE_EVENT_SCAN_MAX_BYTES = 64 * 1_024; @@ -29,6 +30,11 @@ export interface StorageLeafObservation { } export interface EventStorageObservation extends StorageLeafObservation { + readonly corruptionStatus?: 'clean' | 'pending' | 'repaired' | 'unavailable'; + readonly corruptionKind?: EventLogCorruptionKind | 'interrupted_repair'; + readonly corruptionMalformedLines?: number; + readonly corruptionPreservedBytes?: number; + readonly corruptionRetainedValidLines?: number; readonly eventLines: number; readonly eventLinesAccuracy: StorageMetricAccuracy; readonly scannedBytes: number; @@ -167,6 +173,67 @@ const scanEventLines = Effect.fnUntraced(function* (path: string) { ); }); +interface EventCorruptionStatus { + readonly corruptionStatus: 'clean' | 'pending' | 'repaired' | 'unavailable'; + readonly corruptionKind?: EventLogCorruptionKind | 'interrupted_repair'; + readonly corruptionMalformedLines?: number; + readonly corruptionPreservedBytes?: number; + readonly corruptionRetainedValidLines?: number; +} + +const observeEventCorruption = Effect.fnUntraced(function* (eventPath: string) { + const pending = yield* observeLeaf(`${eventPath}.corrupt-pending`); + if (pending.kind !== 'missing') + return { + corruptionStatus: pending.kind === 'regular_file' ? 'pending' : 'unavailable', + ...(pending.kind === 'regular_file' ? { corruptionPreservedBytes: pending.bytes ?? 0 } : {}), + } satisfies EventCorruptionStatus; + const statusPath = `${eventPath}${EVENT_CORRUPTION_STATUS_SUFFIX}`; + const statusLeaf = yield* observeLeaf(statusPath); + if (statusLeaf.kind === 'missing') return { corruptionStatus: 'clean' } as const; + if (statusLeaf.kind !== 'regular_file' || (statusLeaf.bytes ?? 0) > 4_096) + return { corruptionStatus: 'unavailable' } as const; + return yield* Effect.acquireUseRelease( + fsPromise('open event corruption status', statusPath, () => + open(statusPath, noFollowReadOnlyFlags()), + ), + (handle) => + fsPromise('read event corruption status', statusPath, async () => { + const source = await handle.readFile('utf8'); + const decoded = JSON.parse(source) as { + readonly classification?: unknown; + readonly malformedLines?: unknown; + readonly originalBytes?: unknown; + readonly retainedValidLines?: unknown; + }; + const kind = decoded.classification; + if ( + kind !== 'malformed_trailing_fragment' && + kind !== 'interior_corruption' && + kind !== 'interrupted_repair' + ) + throw new Error('event corruption status classification is invalid'); + if ( + typeof decoded.malformedLines !== 'number' || + typeof decoded.originalBytes !== 'number' || + typeof decoded.retainedValidLines !== 'number' + ) + throw new Error('event corruption status counts are invalid'); + return { + corruptionKind: kind, + corruptionMalformedLines: decoded.malformedLines, + corruptionPreservedBytes: decoded.originalBytes, + corruptionRetainedValidLines: decoded.retainedValidLines, + corruptionStatus: 'repaired', + } satisfies EventCorruptionStatus; + }), + (handle) => + fsPromise('close event corruption status', statusPath, () => handle.close()).pipe( + Effect.ignore, + ), + ).pipe(Effect.catch(() => Effect.succeed({ corruptionStatus: 'unavailable' as const }))); +}); + const observeEvents = Effect.fnUntraced(function* (path: string) { const leaf = yield* observeLeaf(path); if (leaf.kind === 'missing') @@ -408,6 +475,7 @@ export const inspectFileSystemStorage = Effect.fnUntraced(function* ( } const state = yield* observeLeaf(paths.statePath); const events = yield* observeEvents(paths.eventPath); + const eventCorruption = yield* observeEventCorruption(paths.eventPath); const reports = yield* observeReports(paths.reportsPath); - return inspection(root, state, events, reports); + return inspection(root, state, { ...events, ...eventCorruption }, reports); }); diff --git a/plugins/pardes-pi/extensions/pardes/storage/state-store.test.ts b/plugins/pardes-pi/extensions/pardes/storage/state-store.test.ts index 2cefa88..347cbff 100644 --- a/plugins/pardes-pi/extensions/pardes/storage/state-store.test.ts +++ b/plugins/pardes-pi/extensions/pardes/storage/state-store.test.ts @@ -73,7 +73,8 @@ describe('filesystem state store', () => { summary: 'test event', type: 'test_event', }; - await Effect.runPromise(store.appendEvent(event)); + await Effect.runPromise(store.appendEventOnce(event)); + await Effect.runPromise(store.appendEventOnce(event)); const report: AgentReport = { agentId: 'agent-1', createdAt: '2026-06-01T00:00:00.000Z', @@ -89,6 +90,109 @@ describe('filesystem state store', () => { expect(JSON.parse(await readFile(reportPath, 'utf8'))).toEqual(report); }); + test('repairs a malformed trailing event fragment losslessly and settles one identity across repeated restart', async () => { + const directory = await temporaryDirectory(); + const store = await Effect.runPromise(makeFileSystemStateStore(directory)); + await Effect.runPromise(store.initialize(initialState())); + const prior: ManagerEvent = { + createdAt: '2026-06-01T00:00:00.000Z', + id: 'event-prior', + summary: 'prior valid event', + type: 'test_event', + }; + const repair: ManagerEvent = { + createdAt: '2026-06-01T00:00:01.000Z', + id: 'event-repair', + summary: 'repair intent', + type: 'test_repair', + }; + const damaged = `${JSON.stringify(prior)}\n{"id":"torn"`; + await writeFile(store.eventPath, damaged, 'utf8'); + + await Effect.runPromise(store.appendEventOnce(repair)); + const restarted = await Effect.runPromise(makeFileSystemStateStore(directory)); + await Effect.runPromise(restarted.appendEventOnce(repair)); + + const active = (await readFile(store.eventPath, 'utf8')) + .trim() + .split('\n') + .map((line) => JSON.parse(line) as ManagerEvent); + expect(active.map(({ id }) => id)).toEqual([prior.id, repair.id]); + const entries = await readdir(directory); + const preserved = entries.filter( + (entry) => entry.startsWith('events.jsonl.corrupt-') && !entry.endsWith('.json'), + ); + expect(preserved).toHaveLength(1); + expect(await readFile(join(directory, preserved[0] ?? ''), 'utf8')).toBe(damaged); + expect(await Effect.runPromise(store.inspectStorage())).toMatchObject({ + events: { + corruptionKind: 'malformed_trailing_fragment', + corruptionMalformedLines: 1, + corruptionPreservedBytes: Buffer.byteLength(damaged), + corruptionRetainedValidLines: 1, + corruptionStatus: 'repaired', + eventLines: 2, + }, + }); + }); + + test('surfaces bounded pending event-corruption preservation state', async () => { + const directory = await temporaryDirectory(); + const store = await Effect.runPromise(makeFileSystemStateStore(directory)); + await Effect.runPromise(store.initialize(initialState())); + await writeFile(`${store.eventPath}.corrupt-pending`, 'preserved-damaged-bytes', 'utf8'); + + expect(await Effect.runPromise(store.inspectStorage())).toMatchObject({ + events: { + corruptionPreservedBytes: 23, + corruptionStatus: 'pending', + }, + }); + }); + + test('quarantines interior event corruption while retaining valid records on both sides', async () => { + const directory = await temporaryDirectory(); + const store = await Effect.runPromise(makeFileSystemStateStore(directory)); + await Effect.runPromise(store.initialize(initialState())); + const first: ManagerEvent = { + createdAt: '2026-06-01T00:00:00.000Z', + id: 'event-first', + summary: 'first valid event', + type: 'test_event', + }; + const second: ManagerEvent = { + createdAt: '2026-06-01T00:00:01.000Z', + id: 'event-second', + summary: 'second valid event', + type: 'test_event', + }; + const repair: ManagerEvent = { + createdAt: '2026-06-01T00:00:02.000Z', + id: 'event-interior-repair', + summary: 'repair interior corruption', + type: 'test_repair', + }; + const damaged = `${JSON.stringify(first)}\nnot-json\n${JSON.stringify(second)}\n`; + await writeFile(store.eventPath, damaged, 'utf8'); + + await Effect.runPromise(store.appendEvent(repair)); + + const active = (await readFile(store.eventPath, 'utf8')) + .trim() + .split('\n') + .map((line) => JSON.parse(line) as ManagerEvent); + expect(active.map(({ id }) => id)).toEqual([first.id, second.id, repair.id]); + expect(await Effect.runPromise(store.inspectStorage())).toMatchObject({ + events: { + corruptionKind: 'interior_corruption', + corruptionMalformedLines: 1, + corruptionRetainedValidLines: 2, + corruptionStatus: 'repaired', + eventLines: 3, + }, + }); + }); + test('restores lossless inbox prose and bounded presentation-barrier reasons through schema-v1 storage', async () => { const directory = await temporaryDirectory(); const store = await Effect.runPromise(makeFileSystemStateStore(directory)); diff --git a/plugins/pardes-pi/extensions/pardes/storage/state-store.ts b/plugins/pardes-pi/extensions/pardes/storage/state-store.ts index b4f480a..9480423 100644 --- a/plugins/pardes-pi/extensions/pardes/storage/state-store.ts +++ b/plugins/pardes-pi/extensions/pardes/storage/state-store.ts @@ -5,6 +5,7 @@ import type { ManagerEvent, ManagerState } from '../manager/index.ts'; import type { AgentReport, ReportArtifactError } from '../reporting/index.ts'; import { decodeState, encodeEvent, encodeReport, encodeState, parseStateJson } from './codecs.ts'; import type { StoreError } from './errors.ts'; +import { scanAndRepairEventIdentity } from './event-log.ts'; import { ensureDirectory, fsPromise, writeJsonAtomically } from './filesystem.ts'; import { inspectFileSystemStorage, type StorageInspection } from './inspection.ts'; import { @@ -31,6 +32,7 @@ export interface StateStoreShape { mutation: (state: ManagerState) => Effect.Effect, ) => Effect.Effect; readonly appendEvent: (event: ManagerEvent) => Effect.Effect; + readonly appendEventOnce: (event: ManagerEvent) => Effect.Effect; readonly writeReport: (report: AgentReport) => Effect.Effect; readonly readReport: (reportId: string) => Effect.Effect; readonly inspectStorage: () => Effect.Effect; @@ -82,14 +84,26 @@ export const makeFileSystemStateStore = Effect.fnUntraced(function* (directory: return result; }), ); + const appendEventUnlocked = Effect.fnUntraced(function* (event: ManagerEvent) { + yield* ensureManagerDirectory(); + const encoded = yield* encodeEvent(eventPath, event); + const source = `${JSON.stringify(encoded)}\n`; + yield* validateSerializedEventWrite(eventPath, source); + yield* fsPromise('append event', eventPath, () => appendFile(eventPath, source, 'utf8')); + }); const appendEventToLog = (event: ManagerEvent) => + semaphore.withPermit( + scanAndRepairEventIdentity(eventPath, event.id).pipe( + Effect.andThen(appendEventUnlocked(event)), + ), + ); + /** Serialize an event identity at most once so durable audit intents can repair after a crash. */ + const appendEventOnce = (event: ManagerEvent) => semaphore.withPermit( Effect.gen(function* () { yield* ensureManagerDirectory(); - const encoded = yield* encodeEvent(eventPath, event); - const source = `${JSON.stringify(encoded)}\n`; - yield* validateSerializedEventWrite(eventPath, source); - yield* fsPromise('append event', eventPath, () => appendFile(eventPath, source, 'utf8')); + if ((yield* scanAndRepairEventIdentity(eventPath, event.id)).exists) return; + yield* appendEventUnlocked(event); }), ); const writeReport = (report: AgentReport) => @@ -113,6 +127,7 @@ export const makeFileSystemStateStore = Effect.fnUntraced(function* (directory: return StateStore.of({ appendEvent: appendEventToLog, + appendEventOnce, directory, eventPath, initialize, diff --git a/plugins/pardes-pi/extensions/pardes/tools/projections/inbox.ts b/plugins/pardes-pi/extensions/pardes/tools/projections/inbox.ts index ede7e97..be361b2 100644 --- a/plugins/pardes-pi/extensions/pardes/tools/projections/inbox.ts +++ b/plugins/pardes-pi/extensions/pardes/tools/projections/inbox.ts @@ -85,8 +85,9 @@ function inboxIndexEventLines(event: ManagerEvent): ReadonlyArray { const preview = compactText(event.summary, INBOX_REPORT_PREVIEW_LENGTH); const previewTruncated = event.reportPreviewTruncated === true || preview !== event.summary.replace(/\s+/g, ' ').trim(); + const coalesced = event.coalescedVerificationEvidence?.length ?? 0; return [ - `reportId:${event.reportId} · previewTruncated:${previewTruncated} · artifact: report_get({ reportId })`, + `reportId:${event.reportId} · previewTruncated:${previewTruncated}${coalesced === 0 ? '' : ` · derived-stale-verifications:${coalesced}`} · artifact: report_get({ reportId })`, `↳ ${event.id} [${event.type}] · ${childAuthoredPreviewLabel(event)}${refinement} · ${pointer}`, ]; } @@ -129,11 +130,12 @@ function inboxIndexOmissionLine(omittedCount: number): string { /** Keep authored judgment guidance intact; maxRows targets total rows but never clips fixed guidance or omission metadata. */ export function inboxLines( - state: Pick, + state: Pick, maxRows?: number, ): string { + const auditRepairCount = Object.keys(state.auditIntents ?? {}).length; const authoredLines = [ - `inbox: ${plural(state.inbox.length, 'pending event')} · read and judge one: inbox_get({ eventId })`, + `inbox: ${plural(state.inbox.length, 'pending event')} · audit repair pending:${auditRepairCount} · read and judge one: inbox_get({ eventId })`, inboxDeliveryLine(state), `path autonomous: ${AUTONOMOUS_INBOX_PATH}`, `path judgment: ${USER_JUDGMENT_INBOX_PATH}`, @@ -193,6 +195,7 @@ export interface InboxEventDetailMetadata { readonly verificationId?: string; readonly reportId?: string; readonly reportPreviewTruncated?: boolean; + readonly coalescedVerificationEvidenceCount?: number; } function inboxEventTrust(event: ManagerEvent): InboxEventTrust { @@ -285,6 +288,9 @@ export function inboxEventDetailMetadata( ...(event.reportPreviewTruncated === undefined ? {} : { reportPreviewTruncated: event.reportPreviewTruncated }), + ...(event.coalescedVerificationEvidence === undefined + ? {} + : { coalescedVerificationEvidenceCount: event.coalescedVerificationEvidence.length }), }; } @@ -331,6 +337,17 @@ export function inboxEventDetailLines( `next: inbox_get({ eventId: ${JSON.stringify(metadata.eventId)}, offset: ${metadata.offset + metadata.returnedChars}, maxChars: ${metadata.maxChars} })`, ] : []), + ...(event.coalescedVerificationEvidence + ?.slice(0, 6) + .map( + (evidence) => + `Pardes-derived verification context: verificationId:${JSON.stringify(evidence.verificationId)} · attempt:${evidence.attempt} · evidence:stale · reasonCode:${evidence.staleReasonCode} · reason:${JSON.stringify(evidence.staleReason)}`, + ) ?? []), + ...((event.coalescedVerificationEvidence?.length ?? 0) > 6 + ? [ + `… +${(event.coalescedVerificationEvidence?.length ?? 0) - 6} more coalesced stale verification contexts omitted; inspect pardes_status(view="verifications").`, + ] + : []), ...(metadata.reportId === undefined ? [] : [`durable child artifact: report_get({ reportId: ${JSON.stringify(metadata.reportId)} })`]), diff --git a/plugins/pardes-pi/extensions/pardes/tools/projections/inspections.ts b/plugins/pardes-pi/extensions/pardes/tools/projections/inspections.ts index d544b84..e4bbab0 100644 --- a/plugins/pardes-pi/extensions/pardes/tools/projections/inspections.ts +++ b/plugins/pardes-pi/extensions/pardes/tools/projections/inspections.ts @@ -148,6 +148,12 @@ export function storageLines(storage: StorageInspection, maxRows?: number): stri storage.events.issue ? ` (${storage.events.issue})` : ''; + const eventCorruption = + storage.events.corruptionStatus === undefined || storage.events.corruptionStatus === 'clean' + ? '' + : storage.events.corruptionStatus === 'repaired' + ? ` · corruption:repaired(${storage.events.corruptionKind ?? 'unknown'}, malformed:${storage.events.corruptionMalformedLines ?? 'unknown'}, retained:${storage.events.corruptionRetainedValidLines ?? 'unknown'}, preserved:${storage.events.corruptionPreservedBytes ?? 'unknown'} bytes)` + : ` · corruption:${storage.events.corruptionStatus}${storage.events.corruptionPreservedBytes === undefined ? '' : `(${storage.events.corruptionPreservedBytes} bytes preserved)`}`; const reportIssue = storage.reports.kind === 'directory' && storage.reports.metricsAccuracy === 'unavailable' && @@ -159,7 +165,7 @@ export function storageLines(storage: StorageInspection, maxRows?: number): stri authoredLines: [ `storage: read-only bounded inspection · root ${storageLeafLabel(storage.root, 'directory')}`, `state: ${storageLeafLabel(storage.state, 'regular_file')} · ${storageBytes(storage.state)}`, - `events: ${storageLeafLabel(storage.events, 'regular_file')} · ${storageBytes(storage.events)} · ${storageMetric(storage.events.eventLines, storage.events.eventLinesAccuracy)} event lines${eventIssue}${eventScan}`, + `events: ${storageLeafLabel(storage.events, 'regular_file')} · ${storageBytes(storage.events)} · ${storageMetric(storage.events.eventLines, storage.events.eventLinesAccuracy)} event lines${eventIssue}${eventScan}${eventCorruption}`, `reports: ${storageLeafLabel(storage.reports, 'directory')} · ${storageMetric(storage.reports.reports, storage.reports.metricsAccuracy)} reports · ${storageMetric(storage.reports.reportBytes, storage.reports.metricsAccuracy)} bytes${reportIssue}${otherReports}${reportScan}`, ], retrievalHintLines: [ diff --git a/plugins/pardes-pi/extensions/pardes/tools/projections/reviews.ts b/plugins/pardes-pi/extensions/pardes/tools/projections/reviews.ts index 94bce63..64ea5d8 100644 --- a/plugins/pardes-pi/extensions/pardes/tools/projections/reviews.ts +++ b/plugins/pardes-pi/extensions/pardes/tools/projections/reviews.ts @@ -26,6 +26,13 @@ export function reviewWarningMetadata(pullRequest: PullRequestRecord): ReadonlyA if (pullRequest.headDivergedAt) warnings.push('remote-head'); const paginationGap = discussionPaginationGapMetadata(pullRequest); if (paginationGap) warnings.push(paginationGap); + if ( + pullRequest.conflictAttention?.phase !== undefined && + pullRequest.conflictAttention.phase !== 'resolved' + ) + warnings.push( + `conflict-gen:${pullRequest.conflictAttention.generation}/${pullRequest.conflictAttention.phase}`, + ); return warnings; } diff --git a/plugins/pardes-pi/extensions/pardes/tools/tools.test.ts b/plugins/pardes-pi/extensions/pardes/tools/tools.test.ts index 5849772..e043c74 100644 --- a/plugins/pardes-pi/extensions/pardes/tools/tools.test.ts +++ b/plugins/pardes-pi/extensions/pardes/tools/tools.test.ts @@ -849,6 +849,11 @@ describe('Pardes model-visible tools', () => { }, events: { bytes: 131_072, + corruptionKind: 'interior_corruption', + corruptionMalformedLines: 1, + corruptionPreservedBytes: 132_000, + corruptionRetainedValidLines: 72, + corruptionStatus: 'repaired', eventLines: 73, eventLinesAccuracy: 'lower_bound', kind: 'regular_file', @@ -899,7 +904,7 @@ describe('Pardes model-visible tools', () => { expect(text).toContain('storage: read-only bounded inspection · root directory'); expect(text).toContain('state: regular file · 512 bytes'); expect(text).toContain( - 'events: regular file · 131072 bytes · ≥73 event lines · scan limited [event_scan_byte_limit]: original=131072 shown=65536 omitted=65536 bytes', + 'events: regular file · 131072 bytes · ≥73 event lines · scan limited [event_scan_byte_limit]: original=131072 shown=65536 omitted=65536 bytes · corruption:repaired(interior_corruption, malformed:1, retained:72, preserved:132000 bytes)', ); expect(text).toContain( 'reports: directory · ≥128 reports · ≥4096 bytes · 2 other direct entries observed · scan limited [direct_entry_scan_limit]: shown=128 omitted>=1 direct entries', @@ -1295,6 +1300,7 @@ describe('Pardes model-visible tools', () => { ]; const state = { ...base, + auditIntents: { 'event-1': requiredValue(inbox[0]) }, githubRateMetadataUnavailableAt: createdAt, inbox: [ ...inbox, @@ -1336,7 +1342,7 @@ describe('Pardes model-visible tools', () => { const pending = await status.execute('call-2', { view: 'inbox' }, signal, onUpdate, ctx); expect(pending.content[0]?.text).toContain( - 'inbox: 2 pending events · read and judge one: inbox_get({ eventId })', + 'inbox: 2 pending events · audit repair pending:1 · read and judge one: inbox_get({ eventId })', ); expect(pending.content[0]?.text).toContain('delivery: cursor event-1 · delivered age:'); expect(pending.content[0]?.text).toContain(