mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-17 07:42:23 +00:00
Run document summaries and filing suggestions in the background
The summary and suggest routes accept `Prefer: respond-async`: after the same quick refusals they answer 202 with a run id, and the background document AI worker runs the read under two new run kinds (DOCUMENT_SUMMARY, DOCUMENT_SUGGEST, migration 0370). Without the header both routes answer synchronously as before; both paths run one shared body, so they cannot drift. The worker asks the capability for the record and re-checks the wire for the provider it picks before every read. A summary that is to be stored marks the document PENDING while queued, READY when it lands, and UNAVAILABLE when the run fails or the write does not land. A second request attaches to a live run only when it would produce the same thing. The vault asks for both in the background and shows the same calm waiting line the index uses, worded so a transient read never promises that leaving the page keeps it. The egress guard and the route inventory now hold every run kind to its dispatcher re-check and its capability.
This commit is contained in:
1 parent
7cd323499e
commit
eda5776886
24 files changed
+2061
-800
No files matched your search
@@ -19,6 +19,7 @@ import { describe, expect, it } from "vitest";
|
||||
import { readFileSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
|
||||
import { DOCUMENT_AI_RUN_KINDS } from "@/lib/documents/ai-runs/types";
|
||||
import type { runStatusCompletion } from "@/lib/insights/status-provider";
|
||||
|
||||
import { walkSourceFiles } from "./helpers/source-files";
|
||||
@@ -163,7 +164,7 @@ const EGRESS_SITES: Record<string, EgressSite> = {
|
||||
*/
|
||||
const WORKER_REACHERS: Record<string, string> = {
|
||||
"lib/jobs/document-ai-run.ts":
|
||||
"Background document reads and lab scans (v1.40): `transcribeDocument` and `runOcrExtraction` through the shared run bodies.",
|
||||
"Background document reads and lab scans (v1.40): `transcribeDocument`, `runDocumentSummary`, `runDocumentAssist` and `runOcrExtraction` through the shared run bodies.",
|
||||
};
|
||||
|
||||
const JOB_CAPABILITY = /\baiCapabilityForJob\s*\(/;
|
||||
@@ -190,6 +191,50 @@ describe("workers that reach an admitted helper", () => {
|
||||
});
|
||||
});
|
||||
|
||||
/** The body of one top-level `async function name(` in a source text. */
|
||||
function functionBody(text: string, name: string): string | null {
|
||||
const start = text.search(new RegExp(`async function ${name}\\s*\\(`));
|
||||
if (start < 0) return null;
|
||||
const end = text.indexOf("\n}\n", start);
|
||||
return end < 0 ? null : text.slice(start, end);
|
||||
}
|
||||
|
||||
describe("every kind of background run re-checks before its read", () => {
|
||||
const worker = code("lib/jobs/document-ai-run.ts");
|
||||
const table = /const RUN_DISPATCH[^=]*=\s*\{([\s\S]*?)\};/.exec(worker);
|
||||
const entries = [...(table?.[1] ?? "").matchAll(/(\w+):\s*(\w+),/g)].map(
|
||||
(m) => ({ kind: m[1], dispatcher: m[2] }),
|
||||
);
|
||||
|
||||
it("finds the dispatch table (an empty match is a failure, not a pass)", () => {
|
||||
expect(entries.length).toBeGreaterThanOrEqual(2);
|
||||
});
|
||||
|
||||
it("dispatches every run kind", () => {
|
||||
expect(entries.map((e) => e.kind).sort()).toEqual(
|
||||
[...DOCUMENT_AI_RUN_KINDS].sort(),
|
||||
);
|
||||
});
|
||||
|
||||
it("the shared document pick re-checks the wire on both transports", () => {
|
||||
const pick = functionBody(worker, "pickForDocumentRead") ?? "";
|
||||
expect(pick.match(/\bassertRunMayEgress\s*\(/g)?.length ?? 0).toBe(2);
|
||||
});
|
||||
|
||||
it.each(DOCUMENT_AI_RUN_KINDS)(
|
||||
"%s re-checks the capability and the wire before the body runs",
|
||||
(kind) => {
|
||||
const entry = entries.find((e) => e.kind === kind);
|
||||
const body = entry ? functionBody(worker, entry.dispatcher) : null;
|
||||
expect(body, `${kind}: dispatcher not found`).not.toBeNull();
|
||||
expect(
|
||||
/\b(?:assertRunMayEgress|pickForDocumentRead)\s*\(/.test(body ?? ""),
|
||||
`${kind}: its dispatcher sends to a model without assertRunMayEgress`,
|
||||
).toBe(true);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
describe("AI egress call sites", () => {
|
||||
const found = sourceFiles().filter((rel) => EGRESS_CALL.test(code(rel)));
|
||||
|
||||
|
||||
Reference in new issue
Block a user