Q61: a failed status check no longer ends recovery of a background response

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ZacharyZhang-NYandClaude Opus 5.5 committed 2026-10-08 10:42:57 -04:00
1 parent 88463e4396
commit 46cd167c2c
6 files changed
+93 -2

No files matched your search

+1 -1
View File
@@ -24,7 +24,7 @@ describe("domain services", () => {
const connection = createConnection();
const rows = connection.db.prepare("select version from schema_migrations order by version asc").all() as Array<{ version: number }>;
expect(rows.map((row) => row.version)).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70]);
expect(rows.map((row) => row.version)).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71]);
connection.close();
});
@@ -178,6 +178,65 @@ describe("provider background service", () => {
});
});
describe("background polling failures", () => {
test("a failed status check keeps the request recoverable; only repeated failures give up", async () => {
const connection = createConnection();
const organization = new OrganizationService(connection.db);
let pollFailures = 2;
let status = "completed";
const base = backgroundAdapters("completed", "Late result.");
const adapters: ProviderAdapterRegistry = {
...base,
openai: {
...base.openai,
retrieveResponse: async (input) => {
if (pollFailures > 0) {
pollFailures -= 1;
throw new Error("fetch failed: ECONNRESET");
}
const result = await base.openai.retrieveResponse(input);
return { ...result, status };
},
},
};
const providers = new ProviderService(connection.db, adapters, testVault);
const background = new ProviderBackgroundService(connection.db, adapters, testVault);
let orgId = "";
let openAiProviderId = "";
connection.transaction(() => {
orgId = createOrganization(organization);
openAiProviderId = providerId(connection, orgId, "openai");
providers.saveCredential({ orgId, providerId: openAiProviderId, secret: "sk-live-provider", model: "gpt-5.4" });
providers.updatePolicy({
orgId, providerId: openAiProviderId, dataPolicy: PROVIDER_DATA_POLICY.STANDARD, backgroundEnabled: true,
monthlyRequestLimit: 10, workspaceScope: "organization", contextWindowTokens: 128_000,
});
});
const requestId = await background.create({ orgId, providerId: openAiProviderId, prompt: "Research", instructions: null, executionMode: PROVIDER_EXECUTION_MODE.BACKGROUND });
const row = () => connection.db.prepare("select state, poll_failures, last_poll_error, response_text from provider_requests where id = ?").get(requestId);
expect(await background.recoverBackgroundResponses()).toBe(1);
expect(row()).toMatchObject({ state: PROVIDER_REQUEST_STATE.PENDING, poll_failures: 1, last_poll_error: "fetch failed: ECONNRESET" });
await background.recoverBackgroundResponses();
await background.recoverBackgroundResponses();
expect(row()).toMatchObject({ state: PROVIDER_REQUEST_STATE.SUCCEEDED, poll_failures: 0, response_text: "Late result." });
pollFailures = 100;
const doomed = await background.create({ orgId, providerId: openAiProviderId, prompt: "Research again", instructions: null, executionMode: PROVIDER_EXECUTION_MODE.BACKGROUND });
for (let attempt = 0; attempt < 10; attempt += 1) await background.poll({ orgId, requestId: doomed });
expect(connection.db.prepare("select state, error_message from provider_requests where id = ?").get(doomed))
.toEqual({ state: PROVIDER_REQUEST_STATE.FAILED, error_message: "Gave up after 10 failed status checks: fetch failed: ECONNRESET" });
// An answer that is not a valid result (unknown status) also counts, so it cannot stay pending forever.
pollFailures = 0;
status = "mystery";
const odd = await background.create({ orgId, providerId: openAiProviderId, prompt: "Odd", instructions: null, executionMode: PROVIDER_EXECUTION_MODE.BACKGROUND });
for (let attempt = 0; attempt < 10; attempt += 1) await background.poll({ orgId, requestId: odd });
expect(connection.db.prepare("select state from provider_requests where id = ?").get(odd)).toEqual({ state: PROVIDER_REQUEST_STATE.FAILED });
connection.close();
});
});
function backgroundAdapters(status: string, text: string | null): ProviderAdapterRegistry {
return {
...providerAdapters,