mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-01 09:39:53 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work. > - Agents call connector tools through the tool gateway. The gateway writes a `tool_call_events` audit row for each step of a call. > - The audit insert has foreign keys to `companies`, `agents`, `heartbeat_runs` and `issues`. Postgres takes a key-share lock on each parent row, in trigger order: company, agent, run, issue. > - Other writers lock the issue row and then the run row. When such a transaction holds the issue and then asks for the run, while the audit insert holds the run and waits for the issue, Postgres detects a deadlock and aborts the audit insert (`40P01`). > - The gateway then returns the raw `Failed query: insert into "tool_call_events" ...` error to the agent. The agent does not get the tool result. The same call works when the agent sends it again. > - This pull request runs the audit insert through the shared bounded retry that #20971 added for the same lock cycle on `workspace_operations`. > - The benefit is that a short lock conflict no longer fails a connector call or loses its audit row. ## Linked Issues or Issue Description **What happened?** Some connector calls fail with `Failed query: insert into "tool_call_events" ...`. This happens most often when an agent sends several tool calls in parallel in one run. The failed call waits for some seconds, then fails. The other parallel calls succeed. The same call succeeds when the agent sends it again. The Postgres cause is `deadlock detected` (`40P01`). **Expected behavior** A transient lock conflict does not fail the tool call. The gateway writes the audit row and returns the tool result. **Steps to reproduce** 1. Transaction A: lock the issue row `FOR UPDATE`. 2. Call a connector tool for a run on that issue. Its `tool_call_events` insert takes key-share on the company, agent and run rows, then waits for the issue. 3. Transaction A: lock the run row `FOR UPDATE`. 4. Postgres reports a deadlock. Before this change the audit insert is the victim, and the tool call fails with the database error. **Additional context** The new test reproduces this with two real transactions on embedded Postgres. Transaction A gets a longer `deadlock_timeout`, so the audit insert is always the victim, as in production. The failing statement in the test has the same column list and the same placeholder shape as the production error. Refs #20971 Refs #21703 Open PRs #15830 and #16596 make the audit insert non-fatal. That approach drops the audit row. This change keeps the row. ## What Changed - `server/src/services/tool-gateway.ts`: `writeToolCallEvent` runs its `tool_call_events` insert through `retryTransientLockContention`. The helper retries only lock timeout, deadlock and serialization failure (`55P03`, `40P01`, `40001`): 5 attempts, with 100 ms × attempt backoff. Other errors fail as before. - `server/src/__tests__/tool-gateway-connector-telemetry.test.ts`: new test with a real two-transaction deadlock between an issue-then-run locker and the audit insert of a real `executeTool` call. The fixture now also returns the issue. ## Verification - Failing before the fix: `cd server && npx vitest run src/__tests__/tool-gateway-connector-telemetry.test.ts -t "loses a deadlock"` failed. `executeTool` rejected with `Failed query: insert into "tool_call_events" ...`, cause `deadlock detected`, code `40P01`. - Passing after the fix: the same test passes. I ran it 3 times. - The full `tool-gateway-connector-telemetry.test.ts` file passes (9 tests). - `tool-gateway-service.test.ts` passes (194 tests). - Not run: `pnpm verify` and the server typecheck. The local sandbox has not enough memory and disk for them. CI runs both. ## Risks - Low risk. Only the `tool_call_events` insert retries, and only on the three contention codes. - Each attempt is a new single-statement write. A deadlock victim's statement rolls back completely, so a retry cannot write a duplicate row. - The worst added delay is about 1 s of backoff across 5 attempts, and only when the lock conflict continues. - Authorization, screening and approval checks do not change. ## Model Used - Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, used through Claude Code with tool use (shell, file edits, local test execution on embedded Postgres). ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [ ] All Paperclip CI gates are green - [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
632 lines
20 KiB
TypeScript
632 lines
20 KiB
TypeScript
import { randomUUID } from "node:crypto";
|
|
import { and, eq, sql } from "drizzle-orm";
|
|
import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from "vitest";
|
|
import {
|
|
activityLog,
|
|
agents,
|
|
companies,
|
|
companyMemberships,
|
|
connectionGrants,
|
|
createDb,
|
|
heartbeatRuns,
|
|
issues,
|
|
projects,
|
|
toolAccessAuditEvents,
|
|
toolActionRequests,
|
|
toolApplications,
|
|
toolCallEvents,
|
|
toolCatalogEntries,
|
|
toolConnections,
|
|
toolGatewayRateLimitCounters,
|
|
toolGatewaySessions,
|
|
toolInvocations,
|
|
toolPolicies,
|
|
toolProfileBindings,
|
|
toolProfiles,
|
|
toolRuntimeSlots,
|
|
} from "@paperclipai/db";
|
|
import {
|
|
getEmbeddedPostgresTestSupport,
|
|
startEmbeddedPostgresTestDatabase,
|
|
} from "./helpers/embedded-postgres.js";
|
|
import { scopedTelemetryCompanyId } from "../telemetry-company-scope.js";
|
|
|
|
// Proposal wrappers drop unregistered names inside the real client, so the
|
|
// integration assertions use a client double that reports the proposed event
|
|
// as registered. `track` observes exactly what the emitter would hand the
|
|
// real client after schema adoption.
|
|
const track = vi.fn((..._args: unknown[]) => scopedTelemetryCompanyId());
|
|
const isRegisteredEventName = vi.fn(() => true);
|
|
vi.mock("../telemetry.js", () => ({
|
|
getTelemetryClient: () => ({ track, isRegisteredEventName }),
|
|
}));
|
|
|
|
// Deterministic reproduction of "post-execution bookkeeping failure": the
|
|
// gateway's success path awaits writeAudit -> logActivity after the succeeded
|
|
// save; failing that call lands in the catch that overwrites the row to
|
|
// failed.
|
|
const failLogActivityForAction = { current: null as string | null };
|
|
vi.mock("../services/activity-log.js", async (importOriginal) => {
|
|
const actual =
|
|
await importOriginal<typeof import("../services/activity-log.js")>();
|
|
return {
|
|
...actual,
|
|
logActivity: vi.fn(
|
|
async (...args: Parameters<typeof actual.logActivity>) => {
|
|
const input = args[1] as { action?: string };
|
|
if (
|
|
failLogActivityForAction.current &&
|
|
input?.action === failLogActivityForAction.current
|
|
) {
|
|
throw new Error(`forced activity write failure: ${input.action}`);
|
|
}
|
|
return actual.logActivity(...args);
|
|
},
|
|
),
|
|
};
|
|
});
|
|
|
|
const { createToolGatewayService } = await import(
|
|
"../services/tool-gateway.js"
|
|
);
|
|
|
|
const embeddedPostgresSupport = await getEmbeddedPostgresTestSupport();
|
|
const describeEmbeddedPostgres = embeddedPostgresSupport.supported
|
|
? describe
|
|
: describe.skip;
|
|
|
|
type Db = ReturnType<typeof createDb>;
|
|
|
|
const SIGNING_SECRET = "connector-telemetry-signing-secret";
|
|
|
|
function mcpSuccessResponse(init: RequestInit): Response {
|
|
const body = JSON.parse(String(init.body ?? "{}"));
|
|
return new Response(
|
|
JSON.stringify({
|
|
jsonrpc: "2.0",
|
|
id: body?.id ?? "fixture",
|
|
result: { content: [{ type: "text", text: "ok" }] },
|
|
}),
|
|
{ status: 200, headers: { "content-type": "application/json" } },
|
|
);
|
|
}
|
|
|
|
async function createCompany(db: Db) {
|
|
return db
|
|
.insert(companies)
|
|
.values({
|
|
name: `Connector telemetry ${randomUUID()}`,
|
|
issuePrefix: `CT${randomUUID().slice(0, 6).toUpperCase()}`,
|
|
})
|
|
.returning()
|
|
.then((rows) => rows[0]!);
|
|
}
|
|
|
|
async function createAgent(db: Db, companyId: string) {
|
|
return db
|
|
.insert(agents)
|
|
.values({
|
|
companyId,
|
|
name: `Agent ${randomUUID()}`,
|
|
role: "engineer",
|
|
adapterType: "process",
|
|
adapterConfig: {},
|
|
runtimeConfig: {},
|
|
permissions: {},
|
|
})
|
|
.returning()
|
|
.then((rows) => rows[0]!);
|
|
}
|
|
|
|
async function createIssueAndRun(db: Db, companyId: string, agentId: string) {
|
|
const project = await db
|
|
.insert(projects)
|
|
.values({ companyId, name: `Project ${randomUUID()}` })
|
|
.returning()
|
|
.then((rows) => rows[0]!);
|
|
const issue = await db
|
|
.insert(issues)
|
|
.values({
|
|
companyId,
|
|
projectId: project.id,
|
|
title: `Telemetry issue ${randomUUID()}`,
|
|
status: "in_progress",
|
|
assigneeAgentId: agentId,
|
|
})
|
|
.returning()
|
|
.then((rows) => rows[0]!);
|
|
const run = await db
|
|
.insert(heartbeatRuns)
|
|
.values({
|
|
companyId,
|
|
agentId,
|
|
invocationSource: "assignment",
|
|
status: "running",
|
|
contextSnapshot: { issueId: issue.id, projectId: project.id },
|
|
})
|
|
.returning()
|
|
.then((rows) => rows[0]!);
|
|
return { issue, run };
|
|
}
|
|
|
|
async function allowAllToolsForAgent(db: Db, companyId: string, agentId: string) {
|
|
const profile = await db
|
|
.insert(toolProfiles)
|
|
.values({
|
|
companyId,
|
|
profileKey: `telemetry-all-${randomUUID()}`,
|
|
name: `Telemetry profile ${randomUUID()}`,
|
|
defaultAction: "allow",
|
|
})
|
|
.returning()
|
|
.then((rows) => rows[0]!);
|
|
await db.insert(toolProfileBindings).values({
|
|
companyId,
|
|
profileId: profile.id,
|
|
targetType: "agent",
|
|
targetId: agentId,
|
|
});
|
|
}
|
|
|
|
async function createRemoteMcpTool(db: Db, companyId: string) {
|
|
const [application] = await db
|
|
.insert(toolApplications)
|
|
.values({
|
|
companyId,
|
|
applicationKey: `telemetry-app-${randomUUID().slice(0, 8)}`,
|
|
name: `Remote app ${randomUUID()}`,
|
|
type: "mcp_http",
|
|
status: "active",
|
|
})
|
|
.returning();
|
|
const config = {
|
|
url: "https://mcp.telemetry.test/mcp",
|
|
sourceTemplateKey: "github",
|
|
};
|
|
const [connection] = await db
|
|
.insert(toolConnections)
|
|
.values({
|
|
companyId,
|
|
applicationId: application!.id,
|
|
name: `Remote connection ${randomUUID()}`,
|
|
uid: `test/${randomUUID()}`,
|
|
transport: "mcp_remote",
|
|
status: "active",
|
|
enabled: true,
|
|
healthStatus: "ok",
|
|
config,
|
|
transportConfig: config,
|
|
credentialRefs: [],
|
|
credentialSecretRefs: [],
|
|
})
|
|
.returning();
|
|
await db.insert(connectionGrants).values({
|
|
companyId,
|
|
connectionId: connection!.id,
|
|
kind: "organization",
|
|
credentialSecretRefs: [],
|
|
status: "active",
|
|
isDefault: true,
|
|
});
|
|
const [catalogEntry] = await db
|
|
.insert(toolCatalogEntries)
|
|
.values({
|
|
companyId,
|
|
applicationId: application!.id,
|
|
connectionId: connection!.id,
|
|
entryKind: "tool",
|
|
name: `kv_set-${randomUUID()}`,
|
|
toolName: "kv_set",
|
|
title: "KV Set",
|
|
description: "Set a value",
|
|
inputSchema: {
|
|
type: "object",
|
|
properties: { key: { type: "string" }, value: { type: "string" } },
|
|
required: ["key", "value"],
|
|
additionalProperties: false,
|
|
},
|
|
annotations: { readOnlyHint: false },
|
|
riskLevel: "write",
|
|
isReadOnly: false,
|
|
isWrite: true,
|
|
isDestructive: false,
|
|
status: "active",
|
|
versionHash: randomUUID(),
|
|
})
|
|
.returning();
|
|
return { application: application!, connection: connection!, catalogEntry: catalogEntry! };
|
|
}
|
|
|
|
function scopesOf(name: string) {
|
|
return track.mock.calls.flatMap(([eventName], index) =>
|
|
eventName === name ? [track.mock.results[index]?.value] : []);
|
|
}
|
|
|
|
async function settledEvents(expectedCount: number) {
|
|
await vi.waitFor(() => {
|
|
expect(
|
|
track.mock.calls.filter(
|
|
([name]) => name === "connection.invoked",
|
|
).length,
|
|
).toBeGreaterThanOrEqual(expectedCount);
|
|
});
|
|
// The emitter runs as detached background work; give any surplus emission a
|
|
// chance to land before asserting the exact count.
|
|
await new Promise((resolve) => setTimeout(resolve, 150));
|
|
return track.mock.calls.filter(
|
|
([name]) => name === "connection.invoked",
|
|
);
|
|
}
|
|
|
|
describeEmbeddedPostgres("gateway connector invocation telemetry", () => {
|
|
let db!: Db;
|
|
let tempDb: Awaited<ReturnType<typeof startEmbeddedPostgresTestDatabase>> | null =
|
|
null;
|
|
|
|
beforeAll(async () => {
|
|
tempDb = await startEmbeddedPostgresTestDatabase(
|
|
"paperclip-connector-telemetry-",
|
|
);
|
|
db = createDb(tempDb.connectionString);
|
|
}, 30_000);
|
|
|
|
afterEach(async () => {
|
|
vi.useRealTimers();
|
|
track.mockClear();
|
|
failLogActivityForAction.current = null;
|
|
await db.delete(activityLog);
|
|
await db.delete(toolCallEvents);
|
|
await db.delete(toolRuntimeSlots);
|
|
await db.delete(toolGatewaySessions);
|
|
await db.delete(toolGatewayRateLimitCounters);
|
|
await db.delete(toolActionRequests);
|
|
await db.delete(toolInvocations);
|
|
await db.delete(toolAccessAuditEvents);
|
|
await db.delete(toolPolicies);
|
|
await db.delete(toolProfileBindings);
|
|
await db.delete(toolProfiles);
|
|
await db.delete(toolCatalogEntries);
|
|
await db.delete(connectionGrants);
|
|
await db.delete(toolConnections);
|
|
await db.delete(toolApplications);
|
|
await db.delete(heartbeatRuns);
|
|
await db.delete(issues);
|
|
await db.delete(projects);
|
|
await db.delete(agents);
|
|
await db.delete(companyMemberships);
|
|
await db.delete(companies);
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await tempDb?.cleanup();
|
|
});
|
|
|
|
async function fixture(options: Parameters<typeof createToolGatewayService>[1] = {}) {
|
|
const company = await createCompany(db);
|
|
const agent = await createAgent(db, company.id);
|
|
const { issue, run } = await createIssueAndRun(db, company.id, agent.id);
|
|
const remoteTool = await createRemoteMcpTool(db, company.id);
|
|
await allowAllToolsForAgent(db, company.id, agent.id);
|
|
const gateway = createToolGatewayService(db, {
|
|
toolActionSigningSecret: SIGNING_SECRET,
|
|
remoteHttpRequest: async (_url, init) => mcpSuccessResponse(init),
|
|
...options,
|
|
});
|
|
const session = await gateway.createSession({
|
|
companyId: company.id,
|
|
agentId: agent.id,
|
|
runId: run.id,
|
|
});
|
|
const tool = (await gateway.listToolsForSession(session.token)).find(
|
|
(candidate) => candidate.connectionId === remoteTool.connection.id,
|
|
);
|
|
expect(tool).toBeDefined();
|
|
return { company, agent, issue, run, remoteTool, gateway, session, tool: tool! };
|
|
}
|
|
|
|
it("emits exactly one succeeded event for a successful agent execution", async () => {
|
|
const f = await fixture();
|
|
await expect(
|
|
f.gateway.executeTool({
|
|
sessionToken: f.session.token,
|
|
tool: f.tool.name,
|
|
parameters: { key: "a", value: "1" },
|
|
}),
|
|
).resolves.toMatchObject({ status: "completed" });
|
|
|
|
const events = await settledEvents(1);
|
|
expect(events).toHaveLength(1);
|
|
expect(events[0]?.[1]).toMatchObject({
|
|
connector_key: "github",
|
|
transport: "mcp_remote",
|
|
status: "succeeded",
|
|
origin: "agent",
|
|
});
|
|
expect(scopesOf("connection.invoked")).toEqual([f.company.id]);
|
|
}, 30_000);
|
|
|
|
it("keeps the result and its audit row when the audit insert loses a deadlock to an issue-then-run locker", async () => {
|
|
const locker = createDb(tempDb!.connectionString);
|
|
let startLocker!: () => void, issueHeld!: () => void, lockRun!: () => void;
|
|
const lockerStart = new Promise<void>(resolve => { startLocker = resolve; });
|
|
const issueLocked = new Promise<void>(resolve => { issueHeld = resolve; });
|
|
const runGate = new Promise<void>(resolve => { lockRun = resolve; });
|
|
const f = await fixture({
|
|
remoteHttpRequest: async (_url, init) => {
|
|
if (JSON.parse(String(init.body ?? "{}"))?.method === "tools/call") {
|
|
startLocker();
|
|
await issueLocked;
|
|
}
|
|
return mcpSuccessResponse(init);
|
|
},
|
|
});
|
|
const lockerDone = lockerStart.then(() => locker.transaction(async tx => {
|
|
await tx.execute(sql`set local deadlock_timeout = '20s'`);
|
|
await tx.select({ id: issues.id }).from(issues).where(eq(issues.id, f.issue.id)).for("update");
|
|
issueHeld();
|
|
await runGate;
|
|
await tx.select({ id: heartbeatRuns.id }).from(heartbeatRuns).where(eq(heartbeatRuns.id, f.run.id)).for("update");
|
|
}));
|
|
try {
|
|
const call = f.gateway.executeTool({
|
|
sessionToken: f.session.token,
|
|
tool: f.tool.name,
|
|
parameters: { key: "a", value: "1" },
|
|
}).then(value => ({ value }), error => ({ error }));
|
|
await vi.waitFor(async () => {
|
|
const [waiting] = await locker.execute<{ count: number }>(sql`select count(*)::int as count from pg_stat_activity
|
|
where wait_event_type = 'Lock' and query like 'insert into "tool_call_events"%'`);
|
|
expect(waiting?.count).toBe(1);
|
|
}, { timeout: 10_000, interval: 50 });
|
|
lockRun();
|
|
await lockerDone;
|
|
const outcome = await call;
|
|
expect(outcome).not.toHaveProperty("error");
|
|
expect(outcome).toMatchObject({ value: { status: "completed" } });
|
|
expect(await db.select({ id: toolCallEvents.id }).from(toolCallEvents).where(and(
|
|
eq(toolCallEvents.runId, f.run.id), eq(toolCallEvents.eventType, "call_completed")))).toHaveLength(1);
|
|
} finally {
|
|
startLocker();
|
|
issueHeld();
|
|
lockRun();
|
|
await lockerDone.catch(() => undefined);
|
|
await locker.$client.end({ timeout: 1 });
|
|
}
|
|
}, 30_000);
|
|
|
|
it("emits a single failed event when post-success audit work throws (no contradictory pair)", async () => {
|
|
const f = await fixture();
|
|
failLogActivityForAction.current = "tool_gateway.call_completed";
|
|
|
|
await expect(
|
|
f.gateway.executeTool({
|
|
sessionToken: f.session.token,
|
|
tool: f.tool.name,
|
|
parameters: { key: "a", value: "1" },
|
|
}),
|
|
).rejects.toThrow(/forced activity write failure/);
|
|
|
|
const [invocation] = await db.select().from(toolInvocations);
|
|
expect(invocation?.status).toBe("failed");
|
|
|
|
const events = await settledEvents(1);
|
|
expect(events).toHaveLength(1);
|
|
expect(events[0]?.[1]).toMatchObject({ status: "failed", origin: "agent" });
|
|
expect(
|
|
events.some(([, dims]) => (dims as { status: string }).status === "succeeded"),
|
|
).toBe(false);
|
|
}, 30_000);
|
|
|
|
it("emits one failed event when the remote execution itself fails", async () => {
|
|
const f = await fixture({
|
|
remoteHttpRequest: async () =>
|
|
new Response("upstream exploded", { status: 500 }),
|
|
});
|
|
await expect(
|
|
f.gateway.executeTool({
|
|
sessionToken: f.session.token,
|
|
tool: f.tool.name,
|
|
parameters: { key: "a", value: "1" },
|
|
}),
|
|
).rejects.toThrow();
|
|
|
|
const events = await settledEvents(1);
|
|
expect(events).toHaveLength(1);
|
|
expect(events[0]?.[1]).toMatchObject({ status: "failed" });
|
|
}, 30_000);
|
|
|
|
it("emits one denied event for a policy block and one rate_limited event past the limit", async () => {
|
|
// Both calls must land in the same policy window, even across a minute boundary.
|
|
vi.useFakeTimers({ toFake: ["Date"] });
|
|
vi.setSystemTime(new Date("2026-10-06T12:00:30Z"));
|
|
const f = await fixture();
|
|
await db.insert(toolPolicies).values({
|
|
companyId: f.company.id,
|
|
name: "Block telemetry tool",
|
|
policyType: "block",
|
|
selectors: { connectionId: f.remoteTool.connection.id },
|
|
priority: 1,
|
|
});
|
|
await expect(
|
|
f.gateway.executeTool({
|
|
sessionToken: f.session.token,
|
|
tool: f.tool.name,
|
|
parameters: { key: "a", value: "1" },
|
|
}),
|
|
).rejects.toMatchObject({ reasonCode: expect.any(String) });
|
|
|
|
let events = await settledEvents(1);
|
|
expect(events).toHaveLength(1);
|
|
expect(events[0]?.[1]).toMatchObject({ status: "denied", origin: "agent" });
|
|
|
|
track.mockClear();
|
|
await db.delete(toolPolicies);
|
|
await db.insert(toolPolicies).values({
|
|
companyId: f.company.id,
|
|
name: "One call only",
|
|
policyType: "rate_limit",
|
|
selectors: { connectionId: f.remoteTool.connection.id },
|
|
config: { limit: 1, windowSeconds: 60 },
|
|
priority: 1,
|
|
});
|
|
await expect(
|
|
f.gateway.executeTool({
|
|
sessionToken: f.session.token,
|
|
tool: f.tool.name,
|
|
parameters: { key: "rate", value: "first" },
|
|
}),
|
|
).resolves.toMatchObject({ status: "completed" });
|
|
await expect(
|
|
f.gateway.executeTool({
|
|
sessionToken: f.session.token,
|
|
tool: f.tool.name,
|
|
parameters: { key: "rate", value: "second" },
|
|
}),
|
|
).rejects.toMatchObject({ status: 429 });
|
|
|
|
events = await settledEvents(2);
|
|
expect(events).toHaveLength(2);
|
|
expect(events.map(([, dims]) => (dims as { status: string }).status).sort()).toEqual(
|
|
["rate_limited", "succeeded"],
|
|
);
|
|
}, 30_000);
|
|
|
|
it("approval flow: pending emits nothing, decline emits denied, approve emits succeeded", async () => {
|
|
const f = await fixture();
|
|
await db.insert(toolPolicies).values({
|
|
companyId: f.company.id,
|
|
name: "Review telemetry tool",
|
|
policyType: "require_approval",
|
|
selectors: { connectionId: f.remoteTool.connection.id },
|
|
priority: 1,
|
|
});
|
|
|
|
await f.gateway
|
|
.executeTool({
|
|
sessionToken: f.session.token,
|
|
tool: f.tool.name,
|
|
parameters: { key: "a", value: "declined" },
|
|
})
|
|
.then(
|
|
() => {
|
|
throw new Error("expected approval_required");
|
|
},
|
|
(error) => expect(error).toMatchObject({ reasonCode: "approval_required" }),
|
|
);
|
|
// Awaiting approval is in-flight: no completion event may exist yet. The
|
|
// approval card itself emits the registered interaction.created event, so
|
|
// filter to the proposed connector event.
|
|
await new Promise((resolve) => setTimeout(resolve, 150));
|
|
expect(
|
|
track.mock.calls.filter(([name]) => name === "connection.invoked"),
|
|
).toHaveLength(0);
|
|
|
|
const [pendingRequest] = await db.select().from(toolActionRequests);
|
|
await f.gateway.declineActionRequest({
|
|
companyId: f.company.id,
|
|
actionRequestId: pendingRequest!.id,
|
|
actor: { userId: "board" },
|
|
});
|
|
let events = await settledEvents(1);
|
|
expect(events).toHaveLength(1);
|
|
expect(events[0]?.[1]).toMatchObject({ status: "denied", origin: "agent" });
|
|
|
|
track.mockClear();
|
|
await f.gateway
|
|
.executeTool({
|
|
sessionToken: f.session.token,
|
|
tool: f.tool.name,
|
|
parameters: { key: "a", value: "approved" },
|
|
})
|
|
.then(
|
|
() => {
|
|
throw new Error("expected approval_required");
|
|
},
|
|
(error) => expect(error).toMatchObject({ reasonCode: "approval_required" }),
|
|
);
|
|
const approvable = await db
|
|
.select()
|
|
.from(toolActionRequests)
|
|
.where(eq(toolActionRequests.status, "pending"))
|
|
.then((rows) => rows[0]!);
|
|
await f.gateway.approveActionRequest({
|
|
companyId: f.company.id,
|
|
actionRequestId: approvable.id,
|
|
actor: { userId: "board" },
|
|
});
|
|
events = await settledEvents(1);
|
|
expect(events).toHaveLength(1);
|
|
expect(events[0]?.[1]).toMatchObject({ status: "succeeded", origin: "agent" });
|
|
}, 40_000);
|
|
|
|
it("labels connection-test executions as setup_test", async () => {
|
|
const f = await fixture();
|
|
await db.insert(companyMemberships).values({
|
|
companyId: f.company.id,
|
|
principalType: "user",
|
|
principalId: "board-user",
|
|
status: "active",
|
|
membershipRole: "member",
|
|
});
|
|
const testCall = await f.gateway.executeTestCall({
|
|
companyId: f.company.id,
|
|
connectionId: f.remoteTool.connection.id,
|
|
agentId: f.agent.id,
|
|
userId: "board-user",
|
|
toolName: f.tool.name,
|
|
parameters: { key: "t", value: "1" },
|
|
});
|
|
expect(testCall).toMatchObject({ decision: "allowed" });
|
|
expect(testCall).not.toHaveProperty("error");
|
|
|
|
const events = await settledEvents(1);
|
|
expect(events).toHaveLength(1);
|
|
expect(events[0]?.[1]).toMatchObject({
|
|
status: "succeeded",
|
|
origin: "setup_test",
|
|
});
|
|
}, 30_000);
|
|
|
|
it("an idempotent replay returns the stored result without a second event", async () => {
|
|
const f = await fixture();
|
|
const idempotencyKey = `telemetry-replay-${randomUUID()}`;
|
|
await expect(
|
|
f.gateway.executeTool({
|
|
sessionToken: f.session.token,
|
|
tool: f.tool.name,
|
|
parameters: { key: "a", value: "1" },
|
|
idempotencyKey,
|
|
}),
|
|
).resolves.toMatchObject({ status: "completed" });
|
|
await settledEvents(1);
|
|
await expect(
|
|
f.gateway.executeTool({
|
|
sessionToken: f.session.token,
|
|
tool: f.tool.name,
|
|
parameters: { key: "a", value: "1" },
|
|
idempotencyKey,
|
|
}),
|
|
).resolves.toMatchObject({ status: "replayed" });
|
|
|
|
const events = await settledEvents(1);
|
|
expect(events).toHaveLength(1);
|
|
}, 30_000);
|
|
|
|
it("telemetry sink failures never fail the tool call", async () => {
|
|
const f = await fixture();
|
|
track.mockImplementation(() => {
|
|
throw new Error("sink offline");
|
|
});
|
|
await expect(
|
|
f.gateway.executeTool({
|
|
sessionToken: f.session.token,
|
|
tool: f.tool.name,
|
|
parameters: { key: "a", value: "1" },
|
|
}),
|
|
).resolves.toMatchObject({ status: "completed" });
|
|
const [invocation] = await db.select().from(toolInvocations);
|
|
expect(invocation?.status).toBe("succeeded");
|
|
track.mockReset();
|
|
}, 30_000);
|
|
});
|