mirror of
https://github.com/davidkaya/aryx.git
synced 2026-11-15 22:52:04 +01:00
fix: queue session approvals
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
1 parent
f54cbdb6b1
commit
c6e20da8db
8 files changed
+365
-215
No files matched your search
+66
-20
@@ -26,6 +26,9 @@ import {
|
||||
} from '@shared/domain/pattern';
|
||||
import {
|
||||
approvalPolicyRequiresCheckpoint,
|
||||
dequeuePendingApprovalState,
|
||||
enqueuePendingApprovalState,
|
||||
listPendingApprovals,
|
||||
normalizeApprovalPolicy,
|
||||
resolvePendingApproval,
|
||||
type ApprovalDecision,
|
||||
@@ -512,6 +515,15 @@ export class EryxAppService extends EventEmitter<AppServiceEvents> {
|
||||
const session = this.requireSession(workspace, sessionId);
|
||||
const approval = session.pendingApproval;
|
||||
if (!approval || approval.id !== approvalId) {
|
||||
const queuedApproval = session.pendingApprovalQueue?.some((candidate) => candidate.id === approvalId);
|
||||
if (queuedApproval) {
|
||||
throw new Error(
|
||||
approval
|
||||
? `Approval "${approvalId}" is queued behind "${approval.id}" for session "${sessionId}". Resolve the active approval first.`
|
||||
: `Approval "${approvalId}" is queued but not active for session "${sessionId}".`,
|
||||
);
|
||||
}
|
||||
|
||||
throw new Error(`Approval "${approvalId}" is not pending for session "${sessionId}".`);
|
||||
}
|
||||
|
||||
@@ -522,7 +534,7 @@ export class EryxAppService extends EventEmitter<AppServiceEvents> {
|
||||
|
||||
const resolvedAt = nowIso();
|
||||
const resolvedApproval = resolvePendingApproval(approval, decision, resolvedAt);
|
||||
session.pendingApproval = undefined;
|
||||
this.setSessionPendingApprovalState(session, dequeuePendingApprovalState(session, approvalId));
|
||||
session.updatedAt = resolvedAt;
|
||||
|
||||
const updatedRun = this.updateSessionRun(session, handle.requestId, (run) =>
|
||||
@@ -539,6 +551,11 @@ export class EryxAppService extends EventEmitter<AppServiceEvents> {
|
||||
await Promise.resolve(handle.resolve(decision));
|
||||
} catch (error) {
|
||||
const failedAt = nowIso();
|
||||
this.rejectPendingApprovals(
|
||||
session,
|
||||
failedAt,
|
||||
'Queued approval was cancelled because the run failed before it could resume.',
|
||||
);
|
||||
session.status = 'error';
|
||||
session.lastError = error instanceof Error ? error.message : String(error);
|
||||
session.updatedAt = failedAt;
|
||||
@@ -931,14 +948,10 @@ export class EryxAppService extends EventEmitter<AppServiceEvents> {
|
||||
resolve: (decision: ApprovalDecision) => void | Promise<void>,
|
||||
): Promise<void> {
|
||||
const session = this.requireSession(workspace, sessionId);
|
||||
if (session.pendingApproval) {
|
||||
throw new Error(`Session "${sessionId}" already has a pending approval.`);
|
||||
}
|
||||
|
||||
const pendingApproval =
|
||||
'type' in approval ? this.createPendingApprovalFromSidecarEvent(approval) : approval;
|
||||
|
||||
session.pendingApproval = pendingApproval;
|
||||
this.setSessionPendingApprovalState(session, enqueuePendingApprovalState(session, pendingApproval));
|
||||
session.updatedAt = pendingApproval.requestedAt;
|
||||
|
||||
const updatedRun = this.updateSessionRun(session, requestId, (run) =>
|
||||
@@ -971,6 +984,41 @@ export class EryxAppService extends EventEmitter<AppServiceEvents> {
|
||||
};
|
||||
}
|
||||
|
||||
private setSessionPendingApprovalState(
|
||||
session: SessionRecord,
|
||||
state: {
|
||||
pendingApproval?: PendingApprovalRecord;
|
||||
pendingApprovalQueue?: PendingApprovalRecord[];
|
||||
},
|
||||
): void {
|
||||
session.pendingApproval = state.pendingApproval;
|
||||
session.pendingApprovalQueue = state.pendingApprovalQueue;
|
||||
}
|
||||
|
||||
private rejectPendingApprovals(
|
||||
session: SessionRecord,
|
||||
failedAt: string,
|
||||
error: string,
|
||||
): string[] {
|
||||
const requestIds = new Set<string>();
|
||||
|
||||
for (const pendingApproval of listPendingApprovals(session)) {
|
||||
const requestId = this.findApprovalRequestId(session, pendingApproval.id);
|
||||
const rejectedApproval = resolvePendingApproval(pendingApproval, 'rejected', failedAt, error);
|
||||
|
||||
if (requestId) {
|
||||
requestIds.add(requestId);
|
||||
this.updateSessionRun(session, requestId, (run) =>
|
||||
upsertRunApprovalEvent(run, rejectedApproval));
|
||||
}
|
||||
|
||||
this.pendingApprovalHandles.delete(pendingApproval.id);
|
||||
}
|
||||
|
||||
this.setSessionPendingApprovalState(session, {});
|
||||
return [...requestIds];
|
||||
}
|
||||
|
||||
private async awaitFinalResponseApproval(
|
||||
workspace: WorkspaceState,
|
||||
sessionId: string,
|
||||
@@ -1192,32 +1240,30 @@ export class EryxAppService extends EventEmitter<AppServiceEvents> {
|
||||
let changed = false;
|
||||
|
||||
for (const session of workspace.sessions) {
|
||||
const pendingApproval = session.pendingApproval;
|
||||
if (!pendingApproval) {
|
||||
const pendingApprovals = listPendingApprovals(session);
|
||||
if (pendingApprovals.length === 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
changed = true;
|
||||
const failedAt = nowIso();
|
||||
const error = 'Pending approval was interrupted because Eryx restarted before a decision was recorded.';
|
||||
const requestId = this.findApprovalRequestId(session, pendingApproval.id);
|
||||
const rejectedApproval = resolvePendingApproval(pendingApproval, 'rejected', failedAt, error);
|
||||
|
||||
session.pendingApproval = undefined;
|
||||
const requestIds = this.rejectPendingApprovals(session, failedAt, error);
|
||||
session.status = 'error';
|
||||
session.lastError = error;
|
||||
session.updatedAt = failedAt;
|
||||
|
||||
if (!requestId) {
|
||||
continue;
|
||||
if (requestIds.length === 0) {
|
||||
const fallbackRequestId = session.runs.find((run) => run.status === 'running')?.requestId;
|
||||
if (fallbackRequestId) {
|
||||
requestIds.push(fallbackRequestId);
|
||||
}
|
||||
}
|
||||
|
||||
this.updateSessionRun(session, requestId, (run) =>
|
||||
failSessionRunRecord(
|
||||
upsertRunApprovalEvent(run, rejectedApproval),
|
||||
failedAt,
|
||||
error,
|
||||
));
|
||||
for (const requestId of requestIds) {
|
||||
this.updateSessionRun(session, requestId, (run) =>
|
||||
failSessionRunRecord(run, failedAt, error));
|
||||
}
|
||||
}
|
||||
|
||||
return changed;
|
||||
|
||||
@@ -7,7 +7,7 @@ import { normalizeSessionRunRecords } from '@shared/domain/runTimeline';
|
||||
import { normalizeSessionToolingSelection, normalizeWorkspaceSettings } from '@shared/domain/tooling';
|
||||
import {
|
||||
normalizeApprovalPolicy,
|
||||
normalizePendingApproval,
|
||||
normalizePendingApprovalState,
|
||||
} from '@shared/domain/approval';
|
||||
import { createWorkspaceSeed, type WorkspaceState } from '@shared/domain/workspace';
|
||||
import { nowIso } from '@shared/utils/ids';
|
||||
@@ -72,7 +72,10 @@ export class WorkspaceRepository {
|
||||
...session,
|
||||
runs: normalizeSessionRunRecords(session.runs),
|
||||
tooling: normalizeSessionToolingSelection(session.tooling),
|
||||
pendingApproval: normalizePendingApproval(session.pendingApproval),
|
||||
...normalizePendingApprovalState({
|
||||
pendingApproval: session.pendingApproval,
|
||||
pendingApprovalQueue: session.pendingApprovalQueue,
|
||||
}),
|
||||
})),
|
||||
settings: normalizeWorkspaceSettings(stored.settings),
|
||||
selectedProjectId: projects.some((project) => project.id === stored.selectedProjectId)
|
||||
|
||||
@@ -32,6 +32,11 @@ export interface PendingApprovalRecord {
|
||||
messages?: PendingApprovalMessageRecord[];
|
||||
}
|
||||
|
||||
export interface PendingApprovalState {
|
||||
pendingApproval?: PendingApprovalRecord;
|
||||
pendingApprovalQueue?: PendingApprovalRecord[];
|
||||
}
|
||||
|
||||
const approvalCheckpointKinds: ApprovalCheckpointKind[] = ['tool-call', 'final-response'];
|
||||
const approvalCheckpointKindSet = new Set<ApprovalCheckpointKind>(approvalCheckpointKinds);
|
||||
const approvalStatusSet = new Set<ApprovalStatus>(['pending', 'approved', 'rejected']);
|
||||
@@ -174,6 +179,58 @@ export function resolvePendingApproval(
|
||||
};
|
||||
}
|
||||
|
||||
export function listPendingApprovals(
|
||||
state: Partial<PendingApprovalState>,
|
||||
): PendingApprovalRecord[] {
|
||||
const pendingApprovals: PendingApprovalRecord[] = [];
|
||||
const seenApprovalIds = new Set<string>();
|
||||
|
||||
function appendApproval(approval?: Partial<PendingApprovalRecord>) {
|
||||
const normalized = normalizePendingApproval(approval);
|
||||
if (!normalized || normalized.status !== 'pending' || seenApprovalIds.has(normalized.id)) {
|
||||
return;
|
||||
}
|
||||
|
||||
seenApprovalIds.add(normalized.id);
|
||||
pendingApprovals.push(normalized);
|
||||
}
|
||||
|
||||
appendApproval(state.pendingApproval);
|
||||
for (const queuedApproval of state.pendingApprovalQueue ?? []) {
|
||||
appendApproval(queuedApproval);
|
||||
}
|
||||
|
||||
return pendingApprovals;
|
||||
}
|
||||
|
||||
export function normalizePendingApprovalState(
|
||||
state: Partial<PendingApprovalState>,
|
||||
): PendingApprovalState {
|
||||
return splitPendingApprovalState(listPendingApprovals(state));
|
||||
}
|
||||
|
||||
export function enqueuePendingApprovalState(
|
||||
state: Partial<PendingApprovalState>,
|
||||
approval: PendingApprovalRecord,
|
||||
): PendingApprovalState {
|
||||
return normalizePendingApprovalState({
|
||||
pendingApproval: state.pendingApproval,
|
||||
pendingApprovalQueue: [
|
||||
...(state.pendingApprovalQueue ?? []),
|
||||
approval,
|
||||
],
|
||||
});
|
||||
}
|
||||
|
||||
export function dequeuePendingApprovalState(
|
||||
state: Partial<PendingApprovalState>,
|
||||
approvalId: string,
|
||||
): PendingApprovalState {
|
||||
return splitPendingApprovalState(
|
||||
listPendingApprovals(state).filter((approval) => approval.id !== approvalId),
|
||||
);
|
||||
}
|
||||
|
||||
function normalizePendingApprovalMessages(
|
||||
messages?: ReadonlyArray<Partial<PendingApprovalMessageRecord>>,
|
||||
): PendingApprovalMessageRecord[] | undefined {
|
||||
@@ -198,6 +255,22 @@ function normalizePendingApprovalMessages(
|
||||
return normalized.length > 0 ? normalized : undefined;
|
||||
}
|
||||
|
||||
function splitPendingApprovalState(
|
||||
approvals: readonly PendingApprovalRecord[],
|
||||
): PendingApprovalState {
|
||||
if (approvals.length === 0) {
|
||||
return {
|
||||
pendingApproval: undefined,
|
||||
pendingApprovalQueue: undefined,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
pendingApproval: approvals[0],
|
||||
pendingApprovalQueue: approvals.length > 1 ? approvals.slice(1) : undefined,
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeOptionalString(value: string | undefined): string | undefined {
|
||||
const trimmed = value?.trim();
|
||||
return trimmed ? trimmed : undefined;
|
||||
|
||||
@@ -41,6 +41,7 @@ export interface SessionRecord {
|
||||
scratchpadConfig?: ScratchpadSessionConfig;
|
||||
tooling?: SessionToolingSelection;
|
||||
pendingApproval?: PendingApprovalRecord;
|
||||
pendingApprovalQueue?: PendingApprovalRecord[];
|
||||
runs: SessionRunRecord[];
|
||||
}
|
||||
|
||||
|
||||
@@ -182,6 +182,7 @@ export function duplicateSessionRecord(
|
||||
}
|
||||
: undefined,
|
||||
pendingApproval: undefined,
|
||||
pendingApprovalQueue: undefined,
|
||||
runs: [],
|
||||
messages: session.messages.map((message): ChatMessageRecord => ({
|
||||
...message,
|
||||
|
||||
Reference in new issue
Block a user