Merge bounded company review deadline lookup into sleep-21-voice-session-recovery

Preserve the additive index migration and generate the combined snapshot for this PR schema; keep existing migration history intact.

Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
DottaandPaperclip committed 2026-10-27 00:59:42 -05:00
commit cecbb98d81
10 files changed
+3860 -3628

No files matched your search

@@ -60,8 +60,15 @@ describe.skipIf(!supported)("gateway credential expiry", () => {
async function settle(ms = 60_000) {
await vi.advanceTimersByTimeAsync(ms);
if (!draining) await vi.waitFor(async () => {
// SQL may finish after the clock jump and arm a coalesced follow-up pass.
if (idleWorkSnapshot().active > 0) await vi.advanceTimersByTimeAsync(30_000);
// Let real SQL settle before advancing only to an actually armed follow-up.
// Repeated blind clock jumps can expire the future credential under test.
if (idleWorkSnapshot().active > 0) {
await db.execute(sql`select pg_sleep(0.01)`);
const next = coordinator!.nextWakeAt();
if (next !== null && next <= Date.now() + 30_000) {
await vi.advanceTimersByTimeAsync(Math.max(0, next - Date.now()));
}
}
expect(idleWorkSnapshot().active).toBe(0);
}, { timeout: 5000 });
}
@@ -1330,7 +1330,7 @@ describeEmbeddedPostgres("tool gateway service", () => {
const debug = db.$client.options.debug;
db.$client.options.debug = (_id, query, params) => {
if (/^select/i.test(query) && query.includes('"tool_action_requests"') &&
(query.includes('min(') || query.includes('"expires_at" <='))) reads.push({ query, params: [...params] });
(query.includes('min(') || query.includes('"expires_at" <=') || query.includes('"expires_at" is not null'))) reads.push({ query, params: [...params] });
};
try {
for (let index = 0; index < 3; index++) {
@@ -1350,6 +1350,60 @@ describeEmbeddedPostgres("tool gateway service", () => {
expect(coordinator!.nextWakeAt()).toBe(foreign.expiresAt!.getTime());
});
it("reads one indexed deadline from a large company backlog on repeated reloads", async () => {
const pending = await request();
const foreignContext = await createRunFixture(db);
await db.insert(toolPolicies).values(reviewNotePolicyRow(foreignContext.company));
const foreign = await requestWithExpiry(new Date(Date.now() + 1000), foreignContext);
await db.execute(sql`insert into tool_action_requests
(company_id, invocation_id, canonical_arguments_hash, canonical_arguments_summary, expires_at)
select ${fixture.company.id}, ${pending.invocationId}, 'backlog', '{}'::jsonb,
${pending.expiresAt!.toISOString()}::timestamptz + n * interval '1 second'
from generate_series(1, 12000) n`);
await db.execute(sql`insert into tool_action_requests
(company_id, invocation_id, canonical_arguments_hash, canonical_arguments_summary, expires_at)
select ${foreignContext.company.id}, ${foreign.invocationId}, 'foreign backlog', '{}'::jsonb,
${foreign.expiresAt!.toISOString()}::timestamptz - n * interval '1 second'
from generate_series(1, 12000) n`);
await db.insert(toolActionRequests).values([
{ ...pending, id: randomUUID(), interactionId: null, expiresAt: null },
{ ...pending, id: randomUUID(), interactionId: null, status: "rejected", expiresAt: new Date(0) },
]);
await db.execute(sql`analyze tool_action_requests`);
const reads: Array<{ query: string; params: unknown[] }> = [];
const debug = db.$client.options.debug;
db.$client.options.debug = (_id, query, params) => {
if (/^select/i.test(query) && query.includes('"tool_action_requests"')) reads.push({ query, params: [...params] });
};
try {
for (let i = 0; i < 3; i++) {
expect(await gateway.actionReviewExpiryDeadlines(fixture.company.id)).toEqual([
{ companyId: fixture.company.id, dueAt: pending.expiresAt!.getTime() },
]);
}
} finally { db.$client.options.debug = debug; }
expect(reads).toHaveLength(3);
type Plan = { "Node Type": string; "Actual Rows": number; "Actual Loops": number;
"Rows Removed by Filter"?: number; "Rows Removed by Index Recheck"?: number; Plans?: Plan[] };
const scannedRows = (plan: Plan): number =>
(/Scan/.test(plan["Node Type"]) ? (plan["Actual Rows"] + (plan["Rows Removed by Filter"] ?? 0)
+ (plan["Rows Removed by Index Recheck"] ?? 0)) * plan["Actual Loops"] : 0)
+ (plan.Plans ?? []).reduce((count, child) => count + scannedRows(child), 0);
for (const read of reads) {
const [result] = await db.$client.unsafe(
`explain (analyze, buffers, format json) ${read.query}`,
read.params as Parameters<typeof db.$client.unsafe>[1],
);
const plan = result["QUERY PLAN"][0].Plan as Plan;
expect(scannedRows(plan), JSON.stringify(plan)).toBeLessThanOrEqual(1);
}
expect(await gateway.actionReviewExpiryDeadlines(randomUUID())).toEqual([]);
expect(await gateway.actionReviewExpiryDeadlines()).toEqual(expect.arrayContaining([
{ companyId: fixture.company.id, dueAt: pending.expiresAt!.getTime() },
{ companyId: foreignContext.company.id, dueAt: foreign.expiresAt!.getTime() - 12_000_000 },
]));
});
it("preserves a five-second deadline and an earlier committed replacement", async () => {
await start(); await quiet();
const later = await requestWithExpiry(new Date(Date.now() + 120_000)); await settle(1);
@@ -1371,10 +1425,10 @@ describeEmbeddedPostgres("tool gateway service", () => {
const debug = db.$client.options.debug;
db.$client.options.debug = (_id, query, params) => {
if (/^select/i.test(query) && query.includes('"tool_action_requests"') &&
(query.includes('min(') || query.includes('"expires_at" <='))) reads.push({ query, params: [...params] });
(query.includes('min(') || query.includes('"expires_at" <=') || query.includes('"expires_at" is not null'))) reads.push({ query, params: [...params] });
};
try { await settle(6000); } finally { db.$client.options.debug = debug; }
expect(reads.some(read => read.query.includes('"expires_at" <='))).toBe(true);
expect(reads.some(read => read.query.includes('"expires_at" <=') || query.includes('"expires_at" is not null'))).toBe(true);
for (const read of reads) {
expect(read.query).toContain('"tool_action_requests"."company_id" =');
expect(read.params).toContain(fixture.company.id);
+9 -6
View File
@@ -18,12 +18,15 @@ export function toolReviewExpiryService(db: Db, onExpired: (input: {
sweep,
sweepForCompany: (companyId: string) => sweep(new Date(), companyId, 1),
async deadlines(companyId?: string) {
const rows = await db.select({ companyId: toolActionRequests.companyId,
at: sql<string | Date>`min(${toolActionRequests.expiresAt})` })
.from(toolActionRequests).where(and(eq(toolActionRequests.status, "pending"),
isNotNull(toolActionRequests.expiresAt),
companyId === undefined ? undefined : eq(toolActionRequests.companyId, companyId)))
.groupBy(toolActionRequests.companyId);
const pendingExpiry = and(eq(toolActionRequests.status, "pending"), isNotNull(toolActionRequests.expiresAt));
const rows = companyId === undefined
? await db.select({ companyId: toolActionRequests.companyId,
at: sql<string | Date>`min(${toolActionRequests.expiresAt})` })
.from(toolActionRequests).where(pendingExpiry).groupBy(toolActionRequests.companyId)
: await db.select({ companyId: toolActionRequests.companyId,
at: sql<Date>`${toolActionRequests.expiresAt}` })
.from(toolActionRequests).where(and(pendingExpiry, eq(toolActionRequests.companyId, companyId)))
.orderBy(asc(toolActionRequests.expiresAt)).limit(1);
return rows.map(row => ({ companyId: row.companyId, dueAt: new Date(row.at).getTime() }));
},
};