Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions packages/eve/src/harness/approval-candidates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface ApprovalCandidateAuditRecord {
readonly status: ApprovalCandidateStatus;
readonly createdAt: number;
readonly completedAt?: number;
readonly eventEmitted?: boolean;
readonly expiresAt?: number;
readonly provider?: string;
readonly runtimeRevision?: string;
Expand Down Expand Up @@ -135,6 +136,25 @@ export function markApprovalCandidatePendingEventEmitted(input: {
});
}

/** Marks a terminal candidate history event as emitted. */
export function markApprovalCandidateHistoryEventEmitted(input: {
readonly candidateId: string;
readonly state: SessionStateMap | undefined;
}): SessionStateMap | undefined {
const approvalState = readApprovalState(input.state);
let changed = false;
const candidateHistory = approvalState.candidateHistory.map((candidate) => {
if (candidate.candidateId !== input.candidateId || candidate.eventEmitted === true) {
return candidate;
}
changed = true;
return { ...candidate, eventEmitted: true };
});
return changed
? writeApprovalState(input.state, { ...approvalState, candidateHistory })
: input.state;
}

/** Marks a terminal settlement event as emitted. */
export function markApprovalSettlementEventEmitted(input: {
readonly requestId: string;
Expand Down
67 changes: 39 additions & 28 deletions packages/eve/src/harness/tool-loop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ import { createToolResultMessagePartFromToolError } from "#harness/action-result
import { buildTelemetryRuntimeContext } from "#harness/instrumentation-runtime-context.js";
import {
getApprovalAuditState,
markApprovalCandidateHistoryEventEmitted,
markApprovalCandidatePendingEventEmitted,
markApprovalSettlementEventEmitted,
} from "#harness/approval-candidates.js";
Expand Down Expand Up @@ -585,50 +586,60 @@ export function createToolLoopHarness(config: ToolLoopHarnessConfig): StepFn {
const pendingCandidateEvent = getApprovalAuditState(session.state).activeCandidates.find(
(candidate) => candidate.pendingEventEmitted !== true,
);
if (emit && pendingCandidateEvent !== undefined) {
await emit(
createApprovalCandidateEvent({
candidateId: pendingCandidateEvent.candidateId,
outcome: "pending",
requestId: pendingCandidateEvent.requestId,
responderPrincipalId: pendingCandidateEvent.responder.principalId,
sequence: emissionState.sequence,
stepIndex: emissionState.stepIndex,
turnId: emissionState.turnId,
}),
);
if (pendingCandidateEvent !== undefined) {
if (emit) {
await emit(
createApprovalCandidateEvent({
candidateId: pendingCandidateEvent.candidateId,
outcome: "pending",
requestId: pendingCandidateEvent.requestId,
responderPrincipalId: pendingCandidateEvent.responder.principalId,
sequence: emissionState.sequence,
stepIndex: emissionState.stepIndex,
turnId: emissionState.turnId,
}),
);
}
session = {
...session,
state: markApprovalCandidatePendingEventEmitted({
candidateId: pendingCandidateEvent.candidateId,
state: session.state,
}),
};
return { next: runStep, session };
}

if (
emit &&
authorized.kind !== "continue" &&
authorized.kind !== "duplicate" &&
authorized.kind !== "authorization-required"
) {
const pendingCandidateHistoryEvent = getApprovalAuditState(session.state).candidateHistory.find(
(candidate) =>
candidate.eventEmitted !== true &&
candidate.status !== "allowed" &&
candidate.status !== "authorization-required",
);
if (emit && pendingCandidateHistoryEvent !== undefined) {
await emit(
createApprovalCandidateEvent({
candidateId: authorized.candidateId ?? `stale:${authorized.requestId}`,
outcome: authorized.kind,
requestId: authorized.requestId,
responderPrincipalId:
[
...getApprovalAuditState(session.state).activeCandidates,
...getApprovalAuditState(session.state).candidateHistory,
].find((candidate) => candidate.candidateId === authorized.candidateId)?.responder
.principalId ?? "unknown",
safeReason: "safeReason" in authorized ? authorized.safeReason : undefined,
candidateId: pendingCandidateHistoryEvent.candidateId,
outcome: pendingCandidateHistoryEvent.status as Exclude<
typeof pendingCandidateHistoryEvent.status,
"allowed" | "authorization-required"
>,
requestId: pendingCandidateHistoryEvent.requestId,
responderPrincipalId: pendingCandidateHistoryEvent.responder.principalId,
safeReason: pendingCandidateHistoryEvent.safeReason,
sequence: emissionState.sequence,
stepIndex: emissionState.stepIndex,
turnId: emissionState.turnId,
}),
);
session = {
...session,
state: markApprovalCandidateHistoryEventEmitted({
candidateId: pendingCandidateHistoryEvent.candidateId,
state: session.state,
}),
};
return { next: runStep, session };
}
if (authorized.kind === "authorization-required") {
const { challenges } = authorized.authorization;
Expand Down