🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
✅ test(database): 100% coverage for models/agentIntervention (#21830)
Arvin Xud61d210dd416
diff --git a/packages/database/src/models/__tests__/agentIntervention.coverage.test.ts b/packages/database/src/models/__tests__/agentIntervention.coverage.test.tsnew file mode 100644index 00000000000..c43802c28c4--- /dev/null+++ b/packages/database/src/models/__tests__/agentIntervention.coverage.test.ts@@ -0,0 +1,584 @@+// @vitest-environment node+import { randomUUID } from 'node:crypto';++import { eq, inArray, sql } from 'drizzle-orm';+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';++import { getTestDB } from '../../core/getTestDB';+import type { AgentInterventionItem } from '../../schemas';+import {+ agentInterventionResolutions,+ agentInterventions,+ agentOperations,+ users,+ workspaces,+} from '../../schemas';+import type { LobeChatDatabase } from '../../type';+import {+ AGENT_INTERVENTION_IDENTITY_CONFLICT,+ AGENT_INTERVENTION_INVALID_ACTION,+ AGENT_INTERVENTION_INVALID_BATCH,+ AGENT_INTERVENTION_INVALID_REQUEST_REVISION_HASH,+ AGENT_INTERVENTION_INVALID_REVIEW_TOKEN_HASH,+ AGENT_INTERVENTION_SOURCE_TRANSITION_MISMATCH,+ AgentInterventionModel,+} from '../agentIntervention';+import type { AgentInterventionContext } from '../agentIntervention/context';+import {+ markRowsTerminalInTransaction,+ withLazyTimeout,+ withOwnerLazyTimeout,+} from '../agentIntervention/lookup';+import { lockAndTransitionResolution } from '../agentIntervention/resolution';+import { createProxyTestDB } from './_proxyTestDB';++const serverDB: LobeChatDatabase = await getTestDB();++const ownerId = 't874-ai-owner';+const workspaceId = 't874-ai-workspace';+const operationId = 't874-ai-operation';+const personalOperationId = 't874-ai-personal-operation';++const model = new AgentInterventionModel(serverDB, ownerId, workspaceId);+const personalModel = new AgentInterventionModel(serverDB, ownerId);+const workspaceCtx: AgentInterventionContext = { db: serverDB, userId: ownerId, workspaceId };+const personalCtx: AgentInterventionContext = { db: serverDB, userId: ownerId };++let hashSequence = 0;+const nextHash = () => (++hashSequence).toString(16).padStart(64, 'b');++type CreateParams = Parameters<typeof model.createBatch>[0];+type CreateItem = CreateParams['items'][number];++const questionItem = (overrides: Partial<CreateItem> = {}): CreateItem => ({+ allowedActions: ['submit_answers', 'skip_interaction', 'cancel_interaction'],+ interactionKind: 'question',+ requestRevisionHash: nextHash(),+ reviewContext: { title: 'Agent needs your input' },+ reviewTokenHash: nextHash(),+ sanitizedRequest: {+ apiName: 'askUserQuestion',+ questions: [{ id: 'mode', options: [{ id: 'safe', label: 'Safe' }], question: 'Mode?' }],+ },+ surface: 'form',+ toolCallId: `t874-question-${hashSequence}`,+ ...overrides,+});++const customItem = (): CreateItem =>+ questionItem({+ allowedActions: ['submit_custom'],+ interactionKind: 'custom',+ sanitizedRequest: {+ apiName: 'installMarketplaceItem',+ fields: [{ id: 'confirm', label: 'Install', required: true, type: 'boolean' }],+ },+ });++const batchParams = (overrides: Partial<CreateParams> = {}): CreateParams => ({+ activityKey: `t874-activity-${hashSequence + 1}`,+ batchId: `t874-batch-${hashSequence + 1}`,+ deadline: new Date(Date.now() + 10 * 60_000),+ items: [questionItem()],+ operationId,+ provider: 'claude-code',+ source: 'heterogeneous',+ stepIndex: 0,+ systemActionEligibility: 'review_only',+ ...overrides,+});++const createBatch = (overrides: Partial<CreateParams> = {}, target = model) =>+ target.createBatch(batchParams(overrides));++const claim = (+ rows: AgentInterventionItem[],+ action: Parameters<typeof model.claimBatch>[0]['action'],+ target = model,+ resolutionRequestId: string = randomUUID(),+) =>+ target.claimBatch({+ action,+ actorId: ownerId,+ batchId: rows[0].batchId,+ expectedItemCount: rows.length,+ expectedRequestRevisionHashes: Object.fromEntries(+ rows.map((row) => [row.id, row.requestRevisionHash]),+ ),+ expectedVersions: Object.fromEntries(rows.map((row) => [row.id, row.version])),+ operationId: rows[0].operationId,+ resolutionRequestId,+ scope: 'single',+ selectedInterventionIds: [rows[0].id],+ });++const answer = { answers: { mode: 'safe' }, type: 'submit_answers' } as const;++/** A claimed question decision; returns its still-`resolving` resolution. */+const createAnsweredResolution = async (overrides: Partial<CreateParams> = {}, target = model) => {+ const rows = await createBatch(overrides, target);+ const claimed = await claim(rows, answer, target);+ if (claimed.outcome !== 'applied') throw new Error('question claim failed');+ return { resolution: claimed.resolution, rows };+};++/** A claimed custom decision whose execution is still `pending`. */+const createCustomResolution = async () => {+ const rows = await createBatch({ items: [customItem()] });+ const claimed = await claim(rows, {+ expectedRevisionHash: rows[0].requestRevisionHash,+ result: { confirm: true },+ type: 'submit_custom',+ });+ if (claimed.outcome !== 'applied') throw new Error('custom claim failed');+ return { resolution: claimed.resolution, rows };+};++const claimLease = (resolutionRequestId: string, inputHash = 'c'.repeat(64)) =>+ model.claimCustomExecution({ inputHash, leaseDurationMs: 60_000, resolutionRequestId });++const parkOperation = (id: string) =>+ serverDB+ .update(agentOperations)+ .set({ status: 'waiting_for_human' })+ .where(eq(agentOperations.id, id));++const stubTransition = vi.fn(async (_tx: unknown, resolution: unknown, rows: unknown) => ({+ interventions: rows as AgentInterventionItem[],+ outcome: 'idempotent' as const,+ resolution: resolution as never,+}));++beforeEach(async () => {+ await serverDB.insert(users).values({ id: ownerId });+ await serverDB.insert(workspaces).values({+ id: workspaceId,+ name: 'T874 intervention workspace',+ primaryOwnerId: ownerId,+ slug: 't874-ai-workspace',+ });+ await serverDB.insert(agentOperations).values([+ { id: operationId, status: 'running', userId: ownerId, workspaceId },+ { id: personalOperationId, status: 'running', userId: ownerId },+ ]);+});++afterEach(async () => {+ vi.restoreAllMocks();+ stubTransition.mockClear();+ // agent_operations.user_id does not cascade; interventions and resolutions cascade from it.+ await serverDB+ .delete(agentOperations)+ .where(inArray(agentOperations.id, [operationId, personalOperationId]));+ await serverDB.delete(workspaces).where(eq(workspaces.id, workspaceId));+ await serverDB.delete(users).where(eq(users.id, ownerId));+});++describe('AgentInterventionModel coverage', () => {+ describe('personal scope', () => {+ it('creates and reads a batch with no workspace, invisible to the workspace scope', async () => {+ const [row] = await createBatch({ operationId: personalOperationId }, personalModel);++ expect(row.workspaceId).toBeNull();+ expect(await personalModel.findById(row.id)).toMatchObject({ id: row.id, status: 'pending' });+ expect(await model.findById(row.id)).toBeUndefined();+ });++ it('scopes resolutions to the personal owner', async () => {+ const { resolution } = await createAnsweredResolution(+ { operationId: personalOperationId },+ personalModel,+ );++ expect(+ await personalModel.findResolutionByRequestId(resolution.resolutionRequestId),+ ).toMatchObject({ id: resolution.id, workspaceId: null });+ expect(await model.findResolutionByRequestId(resolution.resolutionRequestId)).toBeUndefined();+ });++ it('expires an overdue personal row through the owner-only cold lookup', async () => {+ const [row] = await createBatch(+ { deadline: new Date(Date.now() - 1000), operationId: personalOperationId },+ personalModel,+ );++ expect(await personalModel.findByIdForOwner(row.id)).toMatchObject({+ id: row.id,+ status: 'timed_out',+ });+ });+ });++ describe('createBatch', () => {+ it('rejects an operation the caller does not own', async () => {+ await expect(createBatch({ operationId: 't874-ai-missing-operation' })).rejects.toThrow(+ AGENT_INTERVENTION_IDENTITY_CONFLICT,+ );+ await expect(+ createBatch({ operationId: personalOperationId }), // personal op, workspace caller+ ).rejects.toThrow(AGENT_INTERVENTION_IDENTITY_CONFLICT);+ });++ it('maps a review-token collision with another batch to an identity conflict', async () => {+ const [first] = await createBatch();++ const error = await createBatch({+ items: [questionItem({ reviewTokenHash: first.reviewTokenHash })],+ }).catch((caught: Error) => caught);++ expect(error).toBeInstanceOf(Error);+ expect((error as Error).message).toBe(AGENT_INTERVENTION_IDENTITY_CONFLICT);+ expect((error as Error).cause).toBeDefined();+ });++ it('rethrows an insert failure that is not a unique violation', async () => {+ const error = await createBatch({ deadline: new Date(Number.NaN) }).catch(+ (caught: Error) => caught,+ );++ expect(error).toBeInstanceOf(Error);+ expect((error as Error).message).not.toBe(AGENT_INTERVENTION_IDENTITY_CONFLICT);+ expect(+ await serverDB+ .select()+ .from(agentInterventions)+ .where(eq(agentInterventions.operationId, operationId)),+ ).toEqual([]);+ });++ it.each([+ ['an empty batch id', () => batchParams({ batchId: '' }), AGENT_INTERVENTION_INVALID_BATCH],+ [+ 'a malformed review token hash',+ () => batchParams({ items: [questionItem({ reviewTokenHash: 'nope' })] }),+ AGENT_INTERVENTION_INVALID_REVIEW_TOKEN_HASH,+ ],+ [+ 'a malformed request revision hash',+ () => batchParams({ items: [questionItem({ requestRevisionHash: 'nope' })] }),+ AGENT_INTERVENTION_INVALID_REQUEST_REVISION_HASH,+ ],+ [+ 'a duplicated tool call id',+ () =>+ batchParams({+ items: [+ questionItem({ toolCallId: 't874-same-call' }),+ questionItem({ toolCallId: 't874-same-call' }),+ ],+ }),+ AGENT_INTERVENTION_INVALID_BATCH,+ ],+ [+ 'approve-remember without a canonical tool key',+ () =>+ batchParams({+ items: [+ questionItem({+ allowedActions: ['approve', 'approve_remember'],+ interactionKind: 'tool_approval',+ sanitizedRequest: { apiName: 'writeFile', identifier: 'filesystem' },+ surface: 'binary',+ }),+ ],+ }),+ AGENT_INTERVENTION_INVALID_ACTION,+ ],+ [+ 'a heterogeneous item with no provider',+ () => batchParams({ provider: undefined }),+ AGENT_INTERVENTION_INVALID_BATCH,+ ],+ ])('rejects %s before touching the database', async (_label, build, message) => {+ const transaction = vi.spyOn(serverDB, 'transaction');++ await expect(model.createBatch(build())).rejects.toThrow(message);+ expect(transaction).not.toHaveBeenCalled();+ });+ });++ describe('locators', () => {+ it('returns undefined for malformed or blank locator input', async () => {+ expect(+ await AgentInterventionModel.locateByReviewTokenHash(serverDB, 'nope'),+ ).toBeUndefined();+ expect(+ await AgentInterventionModel.locateByOperationAndToolCall(serverDB, ' ', 'call'),+ ).toBeUndefined();+ expect(+ await AgentInterventionModel.locateByResolutionRequestId(serverDB, 'not-a-uuid'),+ ).toBeUndefined();+ });+ });++ describe('lookups', () => {+ it('returns undefined for malformed or unknown ids and hashes', async () => {+ expect(await model.findByIdForOwner('not-a-uuid')).toBeUndefined();+ expect(await model.findByReviewTokenHash('nope')).toBeUndefined();+ expect(await model.findByReviewTokenHash('f'.repeat(64))).toBeUndefined();+ expect(await model.findResolutionByRequestId('not-a-uuid')).toBeUndefined();+ });++ it('finds an owned row by its review token hash', async () => {+ const [row] = await createBatch();++ expect(await model.findByReviewTokenHash(row.reviewTokenHash)).toMatchObject({+ id: row.id,+ status: 'pending',+ });+ });++ it('times out an overdue batch while reading it', async () => {+ const [row] = await createBatch({ deadline: new Date(Date.now() - 1000) });++ const state = await model.findBatch(operationId, row.batchId);++ expect(state.interventions).toEqual([+ expect.objectContaining({ id: row.id, status: 'timed_out' }),+ ]);+ expect(state.resolutions).toEqual([]);+ });++ it('falls back to the stale row when it vanished before the lazy timeout reload', async () => {+ const [row] = await createBatch({ deadline: new Date(Date.now() - 1000) });+ await serverDB.delete(agentInterventions).where(eq(agentInterventions.id, row.id));++ expect(await withLazyTimeout(workspaceCtx, row)).toBe(row);+ expect(await withOwnerLazyTimeout(workspaceCtx, row)).toBe(row);+ });+ });++ describe('markBatchTerminal', () => {+ it('returns an empty list for a batch that does not exist', async () => {+ expect(await model.markBatchTerminal(operationId, 't874-no-batch', 'session_ended')).toEqual(+ [],+ );+ });++ it('leaves a batch that is not yet due untouched on timeout', async () => {+ const [row] = await createBatch();++ const rows = await model.markBatchTerminal(operationId, row.batchId, 'timed_out');++ expect(rows).toEqual([+ expect.objectContaining({ id: row.id, status: 'pending', version: 1 }),+ ]);+ });++ it('is a no-op once every item is already terminal', async () => {+ const [row] = await createBatch();+ const [ended] = await model.markBatchTerminal(operationId, row.batchId, 'session_ended');++ const again = await model.markBatchTerminal(operationId, row.batchId, 'session_ended');++ expect(ended.status).toBe('session_ended');+ expect(again).toEqual([+ expect.objectContaining({ status: 'session_ended', version: ended.version }),+ ]);+ });++ it('keeps an item whose custom execution is in flight open', async () => {+ const { resolution, rows } = await createCustomResolution();+ expect((await claimLease(resolution.resolutionRequestId)).outcome).toBe('applied');++ const result = await model.markBatchTerminal(operationId, rows[0].batchId, 'session_ended');++ expect(result).toEqual([expect.objectContaining({ id: rows[0].id, status: 'resolving' })]);+ expect(await model.findResolutionByRequestId(resolution.resolutionRequestId)).toMatchObject({+ customExecutionState: 'executing',+ status: 'resolving',+ });+ });++ it('returns rows unchanged when none of them is open', async () => {+ const [row] = await createBatch();+ const [ended] = await model.markBatchTerminal(operationId, row.batchId, 'session_ended');++ const result = await serverDB.transaction((tx) =>+ markRowsTerminalInTransaction(workspaceCtx, tx, [ended], 'timed_out', new Date()),+ );++ expect(result).toEqual([ended]);+ expect(await model.findById(row.id)).toMatchObject({ status: 'session_ended' });+ });+ });++ describe('custom execution', () => {+ it('refuses a completed execution that stored a JSON null result', async () => {+ const { resolution } = await createCustomResolution();+ const lease = await claimLease(resolution.resolutionRequestId);+ expect(lease.outcome).toBe('applied');+ await serverDB+ .update(agentInterventionResolutions)+ .set({ customExecutionResult: sql`'null'::jsonb`, customExecutionState: 'completed' })+ .where(eq(agentInterventionResolutions.id, resolution.id));++ expect(await claimLease(resolution.resolutionRequestId)).toEqual({ outcome: 'conflict' });+ });++ it('claims a legacy submit-custom resolution that predates execution tracking', async () => {+ const { resolution } = await createCustomResolution();+ await serverDB+ .update(agentInterventionResolutions)+ .set({ customExecutionAttempt: null, customExecutionState: null })+ .where(eq(agentInterventionResolutions.id, resolution.id));++ const lease = await claimLease(resolution.resolutionRequestId);++ expect(lease).toMatchObject({ attempt: 1, outcome: 'applied' });+ });++ it('rejects a completion with a malformed request id', async () => {+ expect(+ await model.completeCustomExecution({+ inputHash: 'c'.repeat(64),+ leaseToken: randomUUID(),+ resolutionRequestId: 'not-a-uuid',+ result: { content: 'done', pluginState: {} },+ }),+ ).toEqual({ outcome: 'conflict' });+ });++ it('rejects a completion once the resolution has left resolving', async () => {+ const { resolution } = await createCustomResolution();+ const lease = await claimLease(resolution.resolutionRequestId);+ if (lease.outcome !== 'applied') throw new Error('lease claim failed');+ expect((await model.markResolutionPublished(resolution.resolutionRequestId)).outcome).toBe(+ 'applied',+ );++ const completion = await model.completeCustomExecution({+ inputHash: 'c'.repeat(64),+ leaseToken: lease.leaseToken,+ resolutionRequestId: resolution.resolutionRequestId,+ result: { content: 'done', pluginState: {} },+ });++ expect(completion).toEqual({ outcome: 'conflict' });+ expect(await model.findResolutionByRequestId(resolution.resolutionRequestId)).toMatchObject({+ customExecutionResult: null,+ customExecutionState: 'executing',+ status: 'published',+ });+ });+ });++ describe('resolution transitions', () => {+ it('reports not_found for an unknown resolution request', async () => {+ expect(await model.markResolutionPublished(randomUUID())).toEqual({ outcome: 'not_found' });+ });++ it('refuses to publish a resolution its batch already ended', async () => {+ const { resolution, rows } = await createAnsweredResolution();+ await model.markBatchTerminal(operationId, rows[0].batchId, 'session_ended');++ const result = await model.markResolutionPublished(resolution.resolutionRequestId);++ expect(result).toMatchObject({+ outcome: 'conflict',+ resolution: { id: resolution.id, status: 'session_ended' },+ });+ });++ it('refuses a producer ACK for a runtime resolution', async () => {+ await parkOperation(operationId);+ const { resolution } = await createAnsweredResolution({+ provider: undefined,+ source: 'runtime',+ });++ await expect(model.recordProducerAck(resolution.resolutionRequestId)).rejects.toThrow(+ AGENT_INTERVENTION_SOURCE_TRANSITION_MISMATCH,+ );+ });++ it('conflicts on a producer ACK before the resolution ended', async () => {+ const { resolution } = await createAnsweredResolution();++ expect(await model.recordProducerAck(resolution.resolutionRequestId)).toMatchObject({+ outcome: 'conflict',+ resolution: { producerAckAt: null, status: 'resolving' },+ });+ });++ it('records a late producer ACK once and replays it idempotently', async () => {+ const { resolution, rows } = await createAnsweredResolution();+ await model.markBatchTerminal(operationId, rows[0].batchId, 'session_ended');+ const ackAt = new Date('2026-01-01T00:00:00.000Z');++ const first = await model.recordProducerAck(resolution.resolutionRequestId, ackAt);+ const second = await model.recordProducerAck(resolution.resolutionRequestId);++ expect(first.outcome).toBe('applied');+ expect(second).toMatchObject({+ outcome: 'idempotent',+ resolution: { producerAckAt: ackAt, status: 'session_ended' },+ });+ });++ it('locks the personal operation before running the transition', async () => {+ const { resolution } = await createAnsweredResolution(+ { operationId: personalOperationId },+ personalModel,+ );++ const result = await serverDB.transaction((tx) =>+ lockAndTransitionResolution(+ personalCtx,+ tx,+ resolution.resolutionRequestId,+ stubTransition,+ { lockOperation: true },+ ),+ );++ expect(result.outcome).toBe('idempotent');+ expect(stubTransition).toHaveBeenCalledWith(+ expect.anything(),+ expect.objectContaining({ id: resolution.id }),+ [expect.objectContaining({ resolutionId: resolution.id })],+ );+ });++ it('reports not_found when the operation disappears before it is locked', async () => {+ const { resolution } = await createAnsweredResolution();+ // Simulates a concurrent delete: the unlocked candidate read succeeds, the operation lock finds nothing.+ const tx = createProxyTestDB(serverDB, (query, result) =>+ query.includes('from "agent_operations"') && query.includes('for update')+ ? []+ : result.rows,+ );++ const result = await lockAndTransitionResolution(+ workspaceCtx,+ tx as never,+ resolution.resolutionRequestId,+ stubTransition,+ { lockOperation: true },+ );++ expect(result).toEqual({ outcome: 'not_found' });+ expect(stubTransition).not.toHaveBeenCalled();+ });++ it('reports not_found when the resolution disappears before it is locked', async () => {+ const { resolution } = await createAnsweredResolution();+ // Simulates a concurrent delete between the unlocked candidate read and the row lock.+ const tx = createProxyTestDB(serverDB, (query, result) =>+ query.includes('from "agent_intervention_resolutions"') && query.includes('for update')+ ? []+ : result.rows,+ );++ const result = await lockAndTransitionResolution(+ workspaceCtx,+ tx as never,+ resolution.resolutionRequestId,+ stubTransition,+ );++ expect(result).toEqual({ outcome: 'not_found' });+ expect(stubTransition).not.toHaveBeenCalled();+ });+ });+});diff --git a/packages/database/src/models/agentIntervention/__tests__/utils.coverage.test.ts b/packages/database/src/models/agentIntervention/__tests__/utils.coverage.test.tsnew file mode 100644index 00000000000..68cf34e37e1--- /dev/null+++ b/packages/database/src/models/agentIntervention/__tests__/utils.coverage.test.ts@@ -0,0 +1,36 @@+import type { AgentInterventionSanitizedRequest } from '@lobechat/types';+import { describe, expect, it } from 'vitest';++import { canonicalJson, isCanonicalJsonSafe, providerOptionIds } from '../utils';++describe('agentIntervention utils guards', () => {+ describe('canonicalJson', () => {+ it('rejects a sparse array whose hole is masked by an extra named property', () => {+ // A hole alone already fails the own-key count; the named property keeps+ // both key counts consistent so only the per-index hole check can catch it.+ const holes: string[] = [];+ holes[1] = 'b';+ const sparse = Object.assign(holes, { extra: 'x' });++ expect(() => canonicalJson(sparse)).toThrow('Sparse array is not JSON-safe');+ expect(isCanonicalJsonSafe(sparse)).toBe(false);+ });+ });++ describe('providerOptionIds', () => {+ it('skips field options that carry no id', () => {+ const request = {+ fields: [+ {+ id: 'mode',+ label: 'Mode',+ options: [{ label: 'Unnamed' }, { id: 'safe', label: 'Safe' }],+ type: 'select',+ },+ ],+ } as unknown as AgentInterventionSanitizedRequest;++ expect(providerOptionIds(request)).toEqual(new Set(['safe']));+ });+ });+});diff --git a/packages/database/src/models/agentIntervention/__tests__/validation.coverage.test.ts b/packages/database/src/models/agentIntervention/__tests__/validation.coverage.test.tsnew file mode 100644index 00000000000..842a13e3b98--- /dev/null+++ b/packages/database/src/models/agentIntervention/__tests__/validation.coverage.test.ts@@ -0,0 +1,116 @@+import { describe, expect, it } from 'vitest';++import type { AgentInterventionItem } from '../../../schemas';+import { AGENT_INTERVENTION_INVALID_ACTION } from '../constants';+import type { AgentInterventionContext } from '../context';+import type { ClaimAgentInterventionBatchParams } from '../types';+import { hasValidSealedSnapshot, hasValidSelection, validateAction } from '../validation';++const ctx = { userId: 'owner' } as AgentInterventionContext;++const row = (id: string, overrides: Partial<AgentInterventionItem> = {}) =>+ ({+ allowedActions: ['approve', 'reject_continue', 'stop'],+ canonicalToolKey: 'filesystem/writeFile',+ id,+ itemCount: 2,+ itemIndex: 0,+ requestRevisionHash: `hash-${id}`,+ sealed: true,+ status: 'pending',+ version: 1,+ ...overrides,+ }) as AgentInterventionItem;++const rows = [row('a'), row('b', { itemIndex: 1 })];++const params = (+ overrides: Partial<ClaimAgentInterventionBatchParams> = {},+): ClaimAgentInterventionBatchParams => ({+ action: { type: 'approve' },+ actorId: 'owner',+ batchId: 'batch',+ expectedItemCount: 2,+ expectedRequestRevisionHashes: { a: 'hash-a', b: 'hash-b' },+ expectedVersions: { a: 1, b: 1 },+ operationId: 'operation',+ resolutionRequestId: 'request',+ scope: 'single',+ selectedInterventionIds: ['a'],+ ...overrides,+});++describe('agentIntervention validation guards', () => {+ describe('hasValidSealedSnapshot', () => {+ it('accepts the exact sealed snapshot', () => {+ expect(hasValidSealedSnapshot(rows, params())).toBe(true);+ });++ it('rejects a snapshot whose item count no longer matches the batch', () => {+ expect(hasValidSealedSnapshot(rows, params({ expectedItemCount: 3 }))).toBe(false);+ });+ });++ describe('hasValidSelection', () => {+ it('rejects an empty selection', () => {+ expect(hasValidSelection(rows, [], params({ selectedInterventionIds: [] }), [])).toBe(false);+ });++ it('rejects a single-scope claim that selects more than one item', () => {+ expect(hasValidSelection(rows, rows, params(), ['a', 'b'])).toBe(false);+ });++ it('rejects an operation-wide stop while a sibling decision is still in flight', () => {+ const inFlight = [row('a'), row('b', { itemIndex: 1, status: 'resolving' })];++ expect(+ hasValidSelection(+ inFlight,+ [inFlight[0]],+ params({ action: { haltScope: 'operation', type: 'stop' }, scope: 'all' }),+ ['a'],+ ),+ ).toBe(false);+ });+ });++ describe('validateAction', () => {+ it('rejects an action that a target does not allow', () => {+ expect(() =>+ validateAction(ctx, [row('a')], params({ action: { type: 'skip_interaction' } })),+ ).toThrow(AGENT_INTERVENTION_INVALID_ACTION);+ });++ it('rejects edited arguments outside a single-item claim', () => {+ const target = row('a', { allowedActions: ['approve', 'edit_arguments'] });++ expect(() =>+ validateAction(+ ctx,+ [target],+ params({ action: { editedArguments: { path: '/tmp' }, type: 'approve' }, scope: 'all' }),+ ),+ ).toThrow(AGENT_INTERVENTION_INVALID_ACTION);+ });++ it('rejects approve-remember from an actor who is not the owner', () => {+ const target = row('a', { allowedActions: ['approve_remember'] });++ expect(() =>+ validateAction(+ ctx,+ [target],+ params({ action: { type: 'approve_remember' }, actorId: 'collaborator' }),+ ),+ ).toThrow(AGENT_INTERVENTION_INVALID_ACTION);+ });++ it('accepts approve-remember from the owner on a single remembered tool', () => {+ const target = row('a', { allowedActions: ['approve_remember'] });++ expect(() =>+ validateAction(ctx, [target], params({ action: { type: 'approve_remember' } })),+ ).not.toThrow();+ });+ });+});diff --git a/packages/database/src/models/agentIntervention/customExecution.ts b/packages/database/src/models/agentIntervention/customExecution.tsindex 913948319d3..82b97e227dd 100644--- a/packages/database/src/models/agentIntervention/customExecution.ts+++ b/packages/database/src/models/agentIntervention/customExecution.ts@@ -71,6 +71,7 @@ export const claimCustomExecution = async (}if (resolution.customExecutionState === 'executing') {+ /* v8 ignore if -- unreachable: the custom_execution_state CHECK requires an executing row to carry a lease token, a lease expiry and an attempt > 0 */if (!resolution.customExecutionLeaseToken ||!resolution.customExecutionLeaseExpiresAt ||@@ -85,12 +86,14 @@ export const claimCustomExecution = async (outcome: 'in_progress',};}+ /* v8 ignore start -- unreachable: the custom_execution_state CHECK only admits NULL, pending, executing and completed, and executing/completed are handled above */} else if (resolution.customExecutionState !== 'pending' &&resolution.customExecutionState !== null) {return { outcome: 'conflict' };}+ /* v8 ignore stop */const attempt = (resolution.customExecutionAttempt ?? 0) + 1;const leaseExpiresAt = new Date(now.getTime() + params.leaseDurationMs);@@ -117,7 +120,9 @@ export const claimCustomExecution = async (.returning({ id: agentInterventionResolutions.id });return claimed? { attempt, leaseExpiresAt, leaseToken, outcome: 'applied' }- : { outcome: 'conflict' };+ : /* v8 ignore next -- unreachable: the row was locked FOR UPDATE in this transaction with the same ownership and was just checked to be resolving, so the UPDATE by its id always returns it */ {+ outcome: 'conflict',+ };});};@@ -189,6 +194,8 @@ export const completeCustomExecution = async (.returning({ result: agentInterventionResolutions.customExecutionResult });return completed?.result? { outcome: 'completed', result: completed.result }- : { outcome: 'conflict' };+ : /* v8 ignore next -- unreachable: the row was locked FOR UPDATE in this transaction and just matched every filter of this UPDATE, which writes the validated non-null result */ {+ outcome: 'conflict',+ };});};