Files
PaperClipAI/server/src/services/native-runtime/evidence-classifier.ts
T
f237785766 fix(native): save and wake the unblock owner a blocked report names (#22962)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - An agent reports a blocked task with `paperclip_block` and names the
unblock owner in `blocker.owner` (`kind` and `name`).
> - The server dropped an agent owner's name, so current-track blocks
saved `owner: "board"` and task-wide blocks fell back to the reporting
agent or its self-blocker retry path.
> - The named agent never became the unblock owner and never received a
wake, so work waiting on another agent stopped until a person reassigned
the task by hand.
> - PR #21265 fixed agent resolution and wake dispatch, but it
conflicted with newer master changes (named human owners in #22438 and
self-blocker continuations in #22140), carried a stale runner-check
failure from before #21726, and left a Greptile P1 where
external-chat-bound tasks failed finalization with
`chat_binding_agent_locked`.
> - This pull request rebases #21265 cleanly onto master, preserves
#22438 human-owner routing and #22140 self-blocker continuations,
enforces the full assignment/authorization/chat-lock/invokability
eligibility contract (including `tasks:assign` with run `keyScope` and
external `authorizingUserId`), and falls back safely to board ownership
if eligibility changes race before commit.
> - The benefit is that a blocked task moves to the named agent owner
and wakes that owner without manual reassignment, while chat-bound,
unauthorized, and conversation tasks never fail finalization with
`chat_binding_agent_locked` or bypass assignment policy.

## Linked Issues or Issue Description

Refs #21265 (supersedes with clean rebase onto master, Greptile P1
`chat_binding_agent_locked` fix, `tasks:assign` authorization
enforcement, and compatibility with #22438 and #22140).
Related: #20969, #22140, #22438.

**What happened?**
When an agent reports `blocked` with `blocker.owner: { kind: "agent",
name: "Review Lead" }`, the server drops the owner name. Current-track
blocks save `unblockDescriptor.owner` as `"board"`, and task-wide blocks
treat the report as the reporting agent's own blocker (triggering
self-continuation or board handoff). The named agent is never saved as
the unblock owner and never receives a wake.

**What did you expect?**
The server resolves a named agent owner against non-terminated company
agents (exact ID, trimmed NFC case-folded full name, URL key, or
initials), saves that agent in `unblockDescriptor`, hands the task to
it, and wakes it so its run starts. A person owner keeps #22438
company-member routing (or `"board"` when unresolved). A self-owned
blocker keeps #22140 fresh-session continuation. Unknown, ambiguous,
unavailable, unassignable, unauthorized, interaction-denied, or
chat-locked agent handoffs fail at report time with a clear message so
the agent can correct the report.

**Steps to reproduce**
1. Create two agents in one company: a worker and a reviewer (for
example `"Eligible status corpus delegate"`).
2. Assign a task to the worker and report `blocked` with `blocker.owner:
{ kind: "agent", name: "ESCD" }` (initials) or the full name for
`current_track` or `task_wide` scope.
3. Finalize the run and inspect `issue.unblockDescriptor.owner`,
`issue.assigneeAgentId`, and `agent_wakeup_requests`. On `master`,
`current_track` saves `"board"` and `task_wide` schedules a self-retry
for the worker instead of waking the named agent.

## What Changed

- Add leaf `server/src/services/native-runtime/blocker-owner.ts` (zero
service imports) and `blocker-owner.test.ts`:
- `matchBlockerOwnerAgent` resolves an agent by exact ID, trimmed NFC
case-folded full name (supports non-ASCII names such as `王明`), URL key,
then initials without falling through an ambiguous tier.
- `resolveNativeBlockerOwner` queries non-terminated company agents
(supporting `lockSources` shared row locks).
- Add `server/src/services/native-runtime/blocker-owner-handoff.ts`:
- Centralizes `evaluateBlockerAgentHandoff` and `hasExternalChatBinding`
for report-time validation, run finalization, and under-lock commit
rechecks: conversation/external-chat lock (`chatConversations`), agent
invokability, assignability, interaction policy, and
`authorizationService(db).decide({ action: "tasks:assign", ... })` with
run `keyScope` (`nativeRunKeyScope`) and external `authorizingUserId`.
- Add `server/src/modules/external-agents/run-authorizer.ts` (exported
via `modules/external-agents/index.ts`):
- Owns external-agent execution-input payload parsing and pinned
authorizer lookup (`resolveExternalRunAuthorizingUser`), returning a
normalized `{ status: "none" | "authorized" | "unavailable" }` contract
to keep provider-specific parsing inside `modules/external-agents`.
- Update `server/src/services/native-runtime/evidence-classifier.ts`:
- Carry `ownerAgentId` on `NativeEvidenceAssessment["blocker"]` while
preserving `ownerUserId` from `nativeBlockerUserId` (#22438).
- Mark unresolved agent names `boardOwned: true` so they never silently
fall back to the reporting agent.
- Update `server/src/services/native-runtime/status-arbiter.ts` (995
lines):
- Preserve named agent owners via `resolveBlockerHandoff` across
`current_track`, `task_wide`, and `durable_dependency_blocker_bound`
(carrying `handoffAgentId` while preserving `effects: []` until
dependencies resolve).
- Skip #22140 self-blocker recovery (`agentBlockerRecoverable`) and
queued-request self-supersession when the blocker names a *different*
agent.
- Honor `blockerHandoffLocked` by falling back cleanly to `"board"`
without queueing an unwakeable agent wake; strip `handoffAgentId` in
`supersedeHandedOffDecision`.
- Update
`server/src/services/native-runtime/native-completion-feedback.ts`,
`native-run-finalizer.ts`, `status-decision-committer.ts`, and
`server/src/services/chat-control-recovery-stop.ts`:
- Reject invalid, unauthorized, or chat-locked agent handoffs at report
time; resolve and verify eligibility under lock at commit time with safe
fallback to `"board"`; dispatch the named owner wake on handoff.
- Add unit and embedded-Postgres integration tests in
`blocker-owner.test.ts`, `evidence-classifier.test.ts`,
`status-arbiter.test.ts`, and `native-status-arbiter-corpus.test.ts`
(covering initials/full-name `current_track` and `task_wide` handoff +
wake dispatch, human owner preservation,
unknown/chat-locked/dependency-gated/policy-denied/`skill_test`
`keyScope` cases).

## Verification

- **RED-before-fix on base `2d06567795`:**
- `cd server && npx vitest run
src/services/native-runtime/evidence-classifier.test.ts
src/services/native-runtime/status-arbiter.test.ts` → 4 failed | 34
passed (38).
- **Local checks on final implementation:**
  - `node scripts/check-module-boundaries.mjs` → PASS
- `node scripts/check-test-budget.mjs --base
2d065677959b585065ff3fec8718156a4cf6f41d` → PASS (`110 nightly suites
validated, 4 test file(s) checked`)
- `pnpm --filter @paperclipai/server typecheck` → PASS across all 7
layers
- `cd server && npx vitest run
src/services/native-runtime/blocker-owner.test.ts
src/services/native-runtime/evidence-classifier.test.ts
src/services/native-runtime/status-arbiter.test.ts` → 49/49 passed
- `cd server && npx vitest run --hookTimeout=120000
src/__tests__/native-status-arbiter-corpus.test.ts -t "saves the unblock
owner|rejects a blocker owner agent name|rejects handing off an
external-chat-bound task|hands off a dependency-gated blocker"` → 6/6
passed (plus full 291/291 corpus/blocker/chat suite & 39/39
`github-retry-and-recovery.integration.test.ts`)
- **CI on head `aafddae13b`:**
- **70/70** executed checks `SUCCESS` (0 failing, 0 pending, 6 skipped,
2 non-blocking neutral Paperclip Review), including `ci / verify`, `ci /
e2e` + all 16 E2E shards, all 20 General server shards + chat +
workspaces, all 9 Serialized server shards, Verify Paperclip Runner,
Runner support (1–4/4), Build, Typecheck + Release Registry, policy,
`Superagent Security Scan`, `Socket Security`, `Contributor trust`,
`commitperclip`, `Snyk`, and `Greptile Review`.
- **Greptile:** **5/5** (`The PR appears safe to merge, with the
previous module-ownership finding addressed and no new actionable issues
found.`), 0 unresolved Greptile threads.

## Risks

- Low risk: existing `owner: "board"`, `owner: "agent"` (self-owned
blockers with #22140 continuation), and `owner: { kind: "user", ... }`
(#22438 named human owners) paths keep their existing semantics when no
distinct agent is named.
- If a task becomes chat-bound, unassignable, or unauthorized between
report validation and run finalization, finalization degrades safely to
`owner: "board"` instead of failing the commit.
- No database migrations or schema changes are required.

## Model Used

Gemini 3.1 Pro (Antigravity)

## Checklist

- [x] I have tested my changes locally
- [x] I have checked for any regressions
- [x] I have considered and documented any risks above
- [x] All Paperclip CI gates are green
- [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

---------

Co-authored-by: lucktastic <13757243+lucktastic@users.noreply.github.com>
Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-11-25 15:31:46 -06:00

469 lines
21 KiB
TypeScript

import { and, eq, or } from "drizzle-orm";
import type { Db } from "@paperclipai/db";
import { nativeBlockerUserId } from "./native-blocker-owner.js";
import { resolveNativeBlockerOwner } from "./blocker-owner.js";
import {
approvals,
caseHandoffs,
agents,
heartbeatRunEvents,
issueApprovals,
issueAttachments,
issueThreadInteractions,
issueWorkProducts,
} from "@paperclipai/db";
import {
normalizePrpResultSignals,
type PrpIgnoredAttentionRequest,
type PrpNormalizedAttentionRequest,
type PrpVerificationReasonCode,
} from "../../vendor/paperclip-runner/index.js";
export type NativeEvidenceOutcome = "accepted" | "missing" | "rejected" | "unverifiable";
export interface NativeEvidenceRefAssessment {
ref: string;
kind: "event" | "work_product" | "approval" | "interaction" | "attachment" | "case_handoff" | "unknown";
outcome: NativeEvidenceOutcome;
reasonCode: string;
durableRecordId: string | null;
}
export type NativeBlockerOwnerKind = "agent" | "user" | "system" | "external" | "board" | "unspecified";
export function nativeBlockerOwnerKind(owner: Record<string, unknown>): NativeBlockerOwnerKind {
if (owner.owner === "board") return "board";
return ["agent", "user", "system", "external", "board"].includes(String(owner.kind))
? owner.kind as NativeBlockerOwnerKind : "unspecified";
}
export interface NativeEvidenceAssessment {
objectiveClaimSatisfied: boolean;
objectiveSatisfied: boolean;
allCriteriaSatisfied: boolean;
verificationPassed: boolean;
hasFailedVerification: boolean;
hasBlockingRemainingWork: boolean;
reportedDisposition: "done" | "blocked" | "needs_review" | "yielded";
summary: string;
contractRevisionMatches: boolean;
criterionAssessments: Array<{
criterionId: string;
claimStatus: "satisfied" | "not_satisfied" | "unknown" | null;
outcome: NativeEvidenceOutcome;
evidenceRefs: NativeEvidenceRefAssessment[];
reasonCode: string;
}>;
verificationAssessments: Array<{
commandOrCheck: string;
claimStatus: "passed" | "failed" | "not_run";
outcome: NativeEvidenceOutcome;
evidenceRef: NativeEvidenceRefAssessment | null;
reasonCode: string;
reportedReasonCode: PrpVerificationReasonCode | null;
detail: string | null;
}>;
verificationCaveats: Array<{
commandOrCheck: string;
reasonCode: PrpVerificationReasonCode | null;
detail: string | null;
}>;
acceptedEvidenceRefs: string[];
missingRequirements: string[];
rejectedEvidence: Array<{ ref: string; reasonCode: string }>;
unverifiableEvidence: Array<{ ref: string; reasonCode: string }>;
blocker: {
unblockAction: string;
boardOwned: boolean;
ownerKind?: NativeBlockerOwnerKind;
ownerUserId?: string;
/** The agent the report named as unblock owner, when it resolved to one. */
ownerAgentId?: string | null;
scope: "current_track" | "task_wide";
} | null;
continuation: {
kind: "same_agent" | "retry" | "delegated_issue" | "response_wake" | "monitor" | "case_handoff";
summary: string;
idempotencyKey: string;
handoffId?: string;
} | null;
/** Actionable, blocking requests only. */
attentionRequests: PrpNormalizedAttentionRequest[];
ignoredAttentionRequests: PrpIgnoredAttentionRequest[];
}
function record(value: unknown): Record<string, unknown> {
return typeof value === "object" && value !== null && !Array.isArray(value)
? value as Record<string, unknown>
: {};
}
function text(value: unknown): string | null {
return typeof value === "string" && value.trim().length > 0 ? value.trim() : null;
}
function uuidRef(ref: string, prefixes: string[]): string | null {
for (const prefix of prefixes) {
if (!ref.startsWith(`${prefix}:`)) continue;
const value = ref.slice(prefix.length + 1).trim();
// Model-authored evidence often annotates a durable reference, for example
// `interaction:<uuid> (request_confirmation, status accepted)`. Extract the
// canonical UUID only; never pass the descriptive suffix (or arbitrary
// malformed text) to a Postgres UUID comparison.
const match = /^([0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12})(?=$|[^0-9a-f-])/i.exec(value);
return match?.[1] ?? null;
}
return null;
}
export function readNativeEventEvidenceOutcome(value: unknown): "accepted" | "rejected" | null {
const event = record(value);
if (event.sourceKind !== "control_plane") return null;
const outcome = text(record(event.payload).evidenceOutcome);
return outcome === "accepted" || outcome === "rejected" ? outcome : null;
}
async function classifyEvidenceRef(input: {
db: Db;
companyId: string;
issueId: string;
runId: string;
ref: string;
lockSources?: boolean;
}): Promise<NativeEvidenceRefAssessment> {
const handoffId = uuidRef(input.ref, ["case-handoff"]);
if (handoffId) {
const query = input.db.select({ id: caseHandoffs.id, state: caseHandoffs.state }).from(caseHandoffs).where(and(
eq(caseHandoffs.id, handoffId), eq(caseHandoffs.companyId, input.companyId), eq(caseHandoffs.issueId, input.issueId),
eq(caseHandoffs.sourceRunId, input.runId),
)).limit(1);
const [row] = await (input.lockSources ? query.for("share", { noWait: true }) : query);
return { ref: input.ref, kind: "case_handoff", outcome: !row ? "missing" : row.state === "cancelled" ? "rejected" : "accepted",
reasonCode: !row ? "case_handoff_missing" : row.state === "cancelled" ? "case_handoff_cancelled" : "case_handoff_persisted", durableRecordId: row?.id ?? null };
}
const eventMatch = /^event:(\d+)$/.exec(input.ref);
if (eventMatch) {
const cursor = Number(eventMatch[1]);
const query = input.db.select({
id: heartbeatRunEvents.id,
eventType: heartbeatRunEvents.eventType,
payload: heartbeatRunEvents.payload,
})
.from(heartbeatRunEvents)
.where(and(
eq(heartbeatRunEvents.companyId, input.companyId),
eq(heartbeatRunEvents.runId, input.runId),
or(eq(heartbeatRunEvents.seq, cursor), eq(heartbeatRunEvents.sourceSeq, cursor)),
)).limit(1);
const [event] = await (input.lockSources ? query.for("share", { noWait: true }) : query);
if (!event) {
return { ref: input.ref, kind: "event", outcome: "missing", reasonCode: "durable_run_event_missing", durableRecordId: null };
}
const evidenceOutcome = readNativeEventEvidenceOutcome(record(event.payload).prpEvent);
if (evidenceOutcome === "accepted") {
return { ref: input.ref, kind: "event", outcome: "accepted", reasonCode: "control_plane_evidence_accepted", durableRecordId: String(event.id) };
}
if (evidenceOutcome === "rejected") {
return { ref: input.ref, kind: "event", outcome: "rejected", reasonCode: "control_plane_evidence_rejected", durableRecordId: String(event.id) };
}
return {
ref: input.ref,
kind: "event",
outcome: "unverifiable",
reasonCode: event.eventType === "run.result.proposed"
? "model_result_event_is_claim_only"
: "run_event_has_no_authoritative_evidence_verdict",
durableRecordId: String(event.id),
};
}
const workProductId = uuidRef(input.ref, ["work_product", "work-product", "artifact"]);
if (workProductId) {
const query = input.db.select({
id: issueWorkProducts.id,
status: issueWorkProducts.status,
reviewState: issueWorkProducts.reviewState,
}).from(issueWorkProducts).where(and(
eq(issueWorkProducts.id, workProductId),
eq(issueWorkProducts.companyId, input.companyId),
eq(issueWorkProducts.issueId, input.issueId),
)).limit(1);
const [row] = await (input.lockSources ? query.for("share", { noWait: true }) : query);
if (!row) return { ref: input.ref, kind: "work_product", outcome: "missing", reasonCode: "work_product_missing", durableRecordId: null };
if (
["failed", "error", "cancelled", "rejected", "changes_requested"].includes(row.status)
|| ["rejected", "changes_requested"].includes(row.reviewState)
) {
return { ref: input.ref, kind: "work_product", outcome: "rejected", reasonCode: "work_product_rejected", durableRecordId: row.id };
}
if (
row.reviewState === "approved"
|| ["approved", "passed", "succeeded", "completed", "merged", "published"].includes(row.status)
) {
return { ref: input.ref, kind: "work_product", outcome: "accepted", reasonCode: "work_product_authoritatively_accepted", durableRecordId: row.id };
}
return { ref: input.ref, kind: "work_product", outcome: "unverifiable", reasonCode: "work_product_not_yet_accepted", durableRecordId: row.id };
}
const approvalId = uuidRef(input.ref, ["approval"]);
if (approvalId) {
const query = input.db.select({ id: approvals.id, status: approvals.status })
.from(issueApprovals)
.innerJoin(approvals, eq(issueApprovals.approvalId, approvals.id))
.where(and(
eq(issueApprovals.companyId, input.companyId),
eq(issueApprovals.issueId, input.issueId),
eq(approvals.companyId, input.companyId),
eq(approvals.id, approvalId),
)).limit(1);
const [row] = await (input.lockSources ? query.for("share", { noWait: true }) : query);
if (!row) return { ref: input.ref, kind: "approval", outcome: "missing", reasonCode: "approval_missing", durableRecordId: null };
if (row.status === "approved") return { ref: input.ref, kind: "approval", outcome: "accepted", reasonCode: "approval_approved", durableRecordId: row.id };
if (["rejected", "cancelled"].includes(row.status)) return { ref: input.ref, kind: "approval", outcome: "rejected", reasonCode: "approval_rejected", durableRecordId: row.id };
return { ref: input.ref, kind: "approval", outcome: "missing", reasonCode: "approval_pending", durableRecordId: row.id };
}
const interactionId = uuidRef(input.ref, ["interaction"]);
if (interactionId) {
const query = input.db.select({
id: issueThreadInteractions.id,
status: issueThreadInteractions.status,
result: issueThreadInteractions.result,
}).from(issueThreadInteractions).where(and(
eq(issueThreadInteractions.id, interactionId),
eq(issueThreadInteractions.companyId, input.companyId),
eq(issueThreadInteractions.issueId, input.issueId),
)).limit(1);
const [row] = await (input.lockSources ? query.for("share", { noWait: true }) : query);
if (!row) return { ref: input.ref, kind: "interaction", outcome: "missing", reasonCode: "interaction_missing", durableRecordId: null };
const outcome = text(record(row.result).outcome);
if (["accepted", "answered", "resolved"].includes(outcome ?? "") || ["accepted", "answered"].includes(row.status)) {
return { ref: input.ref, kind: "interaction", outcome: "accepted", reasonCode: "interaction_resolved", durableRecordId: row.id };
}
if (["rejected", "expired", "cancelled"].includes(row.status) || ["rejected", "withdrawn", "issue_closed"].includes(outcome ?? "")) {
return { ref: input.ref, kind: "interaction", outcome: "rejected", reasonCode: "interaction_rejected", durableRecordId: row.id };
}
return { ref: input.ref, kind: "interaction", outcome: "missing", reasonCode: "interaction_pending", durableRecordId: row.id };
}
const attachmentId = uuidRef(input.ref, ["attachment"]);
if (attachmentId) {
const query = input.db.select({ id: issueAttachments.id }).from(issueAttachments).where(and(
eq(issueAttachments.id, attachmentId),
eq(issueAttachments.companyId, input.companyId),
eq(issueAttachments.issueId, input.issueId),
)).limit(1);
const [row] = await (input.lockSources ? query.for("share", { noWait: true }) : query);
return row
? { ref: input.ref, kind: "attachment", outcome: "accepted", reasonCode: "attachment_persisted", durableRecordId: row.id }
: { ref: input.ref, kind: "attachment", outcome: "missing", reasonCode: "attachment_missing", durableRecordId: null };
}
return {
ref: input.ref,
kind: "unknown",
outcome: "unverifiable",
reasonCode: "unsupported_evidence_reference",
durableRecordId: null,
};
}
export async function classifyNativeEvidence(input: {
db: Db;
companyId: string;
issueId: string;
runId: string;
contract: Record<string, unknown>;
result: Record<string, unknown>;
lockSources?: boolean;
}): Promise<NativeEvidenceAssessment> {
const claim = record(input.result.completionClaim);
const claimedCriteria = Array.isArray(claim.criteria) ? claim.criteria.map(record) : [];
const contractCriteria = Array.isArray(input.contract.criteria) ? input.contract.criteria.map(record) : [];
const remaining = Array.isArray(claim.remainingWork) ? claim.remainingWork.map(record) : [];
const normalizedSignals = normalizePrpResultSignals(input.result);
const verification = normalizedSignals.verification;
const reported = input.result.reportedWorkDisposition;
const blocker = record(input.result.blocker);
const blockerOwner = record(blocker.owner);
const ownerUserId = reported === "blocked" && typeof blocker.unblockAction === "string"
? await nativeBlockerUserId({ ...input, owner: blockerOwner }) : null;
const continuation = record(input.result.continuation);
if (!["done", "blocked", "needs_review", "yielded"].includes(String(reported))) {
throw new Error("native_result_invalid_disposition");
}
const contractRevision = text(input.contract.revision);
const contractRevisionMatches = contractRevision !== null && text(claim.contractRevision) === contractRevision;
const cache = new Map<string, Promise<NativeEvidenceRefAssessment>>();
const resolveRef = (ref: string) => {
let pending = cache.get(ref);
if (!pending) {
pending = classifyEvidenceRef({ ...input, ref });
cache.set(ref, pending);
}
return pending;
};
const criterionAssessments = await Promise.all(contractCriteria.map(async (criterion) => {
const criterionId = text(criterion.id) ?? "invalid-contract-criterion";
const claimed = claimedCriteria.find((entry) => text(entry.criterionId) === criterionId) ?? null;
const claimStatus = claimed && ["satisfied", "not_satisfied", "unknown"].includes(String(claimed.status))
? claimed.status as "satisfied" | "not_satisfied" | "unknown"
: null;
const refs = claimed && Array.isArray(claimed.evidenceRefs)
? claimed.evidenceRefs.flatMap((value) => text(value) ? [text(value)!] : [])
: [];
const evidenceRefs = await Promise.all(refs.map(resolveRef));
let outcome: NativeEvidenceOutcome;
let reasonCode: string;
if (!contractRevisionMatches) {
outcome = "rejected";
reasonCode = "contract_revision_stale";
} else if (!claimed) {
outcome = "missing";
reasonCode = "criterion_claim_missing";
} else if (claimStatus === "not_satisfied") {
outcome = "rejected";
reasonCode = "criterion_reported_not_satisfied";
} else if (claimStatus !== "satisfied") {
outcome = "unverifiable";
reasonCode = "criterion_claim_not_satisfied";
} else if (refs.length === 0) {
outcome = "missing";
reasonCode = "criterion_evidence_missing";
} else if (evidenceRefs.some((entry) => entry.outcome === "rejected")) {
outcome = "rejected";
reasonCode = "criterion_evidence_rejected";
} else if (evidenceRefs.some((entry) => entry.outcome === "unverifiable")) {
outcome = "unverifiable";
reasonCode = "criterion_evidence_unverifiable";
} else if (evidenceRefs.some((entry) => entry.outcome === "missing")) {
outcome = "missing";
reasonCode = "criterion_evidence_missing";
} else {
outcome = "accepted";
reasonCode = "criterion_evidence_accepted";
}
return { criterionId, claimStatus, outcome, evidenceRefs, reasonCode };
}));
const verificationAssessments = await Promise.all(verification.map(async (entry) => {
const commandOrCheck = entry.commandOrCheck;
const claimStatus = entry.status;
const ref = entry.artifactRef;
const evidenceRef = ref ? await resolveRef(ref) : null;
const common = { commandOrCheck, claimStatus, evidenceRef, reportedReasonCode: entry.reasonCode, detail: entry.detail };
if (claimStatus === "failed") return { ...common, outcome: "rejected" as const, reasonCode: "verification_reported_failed" };
if (claimStatus === "not_run") return { ...common, outcome: "missing" as const, reasonCode: "verification_not_run" };
if (!evidenceRef) return { ...common, outcome: "unverifiable" as const, reasonCode: "verification_has_no_durable_reference" };
return {
...common,
outcome: evidenceRef.outcome,
reasonCode: evidenceRef.outcome === "accepted" ? "verification_evidence_accepted" : evidenceRef.reasonCode,
};
}));
const allRefs = [...cache.keys()].map((ref) => cache.get(ref)!);
const resolvedRefs = await Promise.all(allRefs);
const acceptedEvidenceRefs = resolvedRefs.filter((entry) => entry.outcome === "accepted").map((entry) => entry.ref);
const missingRequirements = [
...criterionAssessments.filter((entry) => entry.outcome === "missing").map((entry) => entry.criterionId),
...verificationAssessments.filter((entry) => entry.outcome === "missing").map((entry) => entry.commandOrCheck),
];
const rejectedEvidence = resolvedRefs.filter((entry) => entry.outcome === "rejected").map((entry) => ({ ref: entry.ref, reasonCode: entry.reasonCode }));
const unverifiableEvidence = resolvedRefs.filter((entry) => entry.outcome === "unverifiable").map((entry) => ({ ref: entry.ref, reasonCode: entry.reasonCode }));
const allCriteriaSatisfied = contractCriteria.length > 0 && criterionAssessments.every((entry) => entry.outcome === "accepted");
const verificationPassed = verificationAssessments.length > 0 && verificationAssessments.every((entry) => entry.outcome === "accepted");
const hasFailedVerification = verificationAssessments.some((entry) => entry.claimStatus === "failed");
const continuationKind = continuation.kind;
const actionableAttentionRequests: PrpNormalizedAttentionRequest[] = [];
const ignoredAttentionRequests = [...normalizedSignals.ignoredAttentionRequests];
for (const request of normalizedSignals.actionableAttentionRequests) {
if (request.ownerClass === "agent" && request.targetAgentId) {
const targetQuery = input.db.select({ id: agents.id }).from(agents).where(and(
eq(agents.id, request.targetAgentId),
eq(agents.companyId, input.companyId),
)).limit(1);
const targets = await (input.lockSources ? targetQuery.for("share", { noWait: true }) : targetQuery);
if (targets.length === 0) {
ignoredAttentionRequests.push({
sourceIndex: request.sourceIndex,
sourceKind: request.sourceKind,
summary: request.summary,
disposition: "rejected",
reasonCode: "attention_request_target_agent_unavailable",
});
continue;
}
}
actionableAttentionRequests.push(request);
}
const resolvedOwner = reported === "blocked" && typeof blocker.unblockAction === "string"
? await resolveNativeBlockerOwner(input.db, {
companyId: input.companyId,
owner: blocker.owner,
lockSources: input.lockSources,
})
: { kind: "unspecified" as const };
return {
objectiveClaimSatisfied: claim.objectiveSatisfied === true,
objectiveSatisfied: claim.objectiveSatisfied === true && allCriteriaSatisfied,
allCriteriaSatisfied,
verificationPassed,
hasFailedVerification,
hasBlockingRemainingWork:
remaining.some((entry) => entry.blocksCompletion === true)
|| (reported === "done" && Object.keys(blocker).length > 0),
reportedDisposition: reported as NativeEvidenceAssessment["reportedDisposition"],
summary: typeof input.result.summary === "string" ? input.result.summary : "Native run completed.",
contractRevisionMatches,
criterionAssessments,
verificationAssessments,
verificationCaveats: verificationAssessments
.filter((entry) => entry.claimStatus === "not_run")
.map((entry) => ({
commandOrCheck: entry.commandOrCheck,
reasonCode: entry.reportedReasonCode,
detail: entry.detail,
})),
acceptedEvidenceRefs,
missingRequirements,
rejectedEvidence,
unverifiableEvidence,
blocker: reported === "blocked" && typeof blocker.unblockAction === "string"
? {
unblockAction: blocker.unblockAction,
// A person owner is acted on by the board (or resolved company member).
// An agent name that cannot be resolved stays visible to the board
// rather than silently falling back to the reporting agent.
boardOwned: resolvedOwner.kind === "board" || resolvedOwner.kind === "unresolved",
...(ownerUserId ? { ownerUserId } : {}),
ownerKind: nativeBlockerOwnerKind(blockerOwner),
ownerAgentId: resolvedOwner.kind === "agent" ? resolvedOwner.agentId : null,
scope: blocker.scope === "task_wide" ? "task_wide" : "current_track",
}
: null,
continuation: reported === "yielded"
&& ["same_agent", "retry", "delegated_issue", "response_wake", "monitor", "case_handoff"].includes(String(continuationKind))
&& text(continuation.summary)
&& text(continuation.idempotencyKey)
? {
kind: continuationKind as NativeEvidenceAssessment["continuation"] extends infer T ? T extends { kind: infer K } ? K : never : never,
summary: text(continuation.summary)!,
idempotencyKey: text(continuation.idempotencyKey)!,
...(continuationKind === "case_handoff" && text(continuation.handoffId) ? { handoffId: text(continuation.handoffId)! } : {}),
}
: null,
attentionRequests: actionableAttentionRequests,
ignoredAttentionRequests,
};
}