fix(domain): keep worker, SQLite, and sealing invariants owned (#14770)

* fix(domain): keep worker, SQLite, and sealing invariants owned

- Retain a late caller cancellation when SQLite keeps an unresolved worker
  owner instead of finalizing through runOwned.
- Key retained JavaScript static checkpoints by source identity so a
  discovered preload read with another package type releases its entry
  instead of leaking it across parked-worker reuse.
- Keep a parked JavaScript worker owned by its scope until its close
  succeeds.
- Borrow only sealed JSON members that sealing visited, and seal a shared
  subtree once instead of once per path.
- Refuse borrowed Evidence declarations whose resolution depends on the
  union branch their own frame parse selected.

* test(domain): bound shared JSON sealing inspections

A synchronous seal cannot be interrupted by the test timeout, so a lost
visited set would expand every shared path on the worker. Stop descriptor
reads after one per owned edge of this call and assert that count.

Co-authored-by: morluto <morluto@users.noreply.github.com>

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: morluto <morluto@users.noreply.github.com>
This commit is contained in:
authored and GitHub committed 2026-11-26 16:53:46 +00:00
1 parent aad296795c
commit 9e3a2bbc09
9 files changed
+274 -28

No files matched your search

@@ -90,36 +90,59 @@ export interface RetainedStaticCheckpoint extends ReusedJavaScriptStatic {
* the key, so a reused checkpoint is byte-identical to a recomputed one. The
* store keeps at most one eighth of the V8 heap limit and stays empty where
* the runtime does not report one; a miss recomputes.
*
* Entries are keyed by source identity. The normal pass analyzes each source
* once and always removes its entry; it reuses the checkpoint only when the
* package type also matches. A discovered preload script is read as CommonJS
* after discovery read its package as ESM, and that entry must not outlive
* the pass that can no longer use it.
*/
const retainedStatics = new Map<string, RetainedStaticCheckpoint>();
interface RetainedStaticEntry {
readonly packageType: string;
readonly checkpoint: RetainedStaticCheckpoint;
}
const retainedStatics = new Map<string, RetainedStaticEntry>();
let retainedStaticBytes = 0;
/** Which source a checkpoint describes, and the package type it was read as. */
export interface JavaScriptStaticReuseKey {
readonly source: string;
readonly packageType: string;
}
export const javaScriptStaticReuseKey = (
request: ReturnType<typeof javaScriptWorkerRequestSchema.parse>,
): string =>
JSON.stringify([
): JavaScriptStaticReuseKey => ({
source: JSON.stringify([
request.file.container_sha256,
request.file.path,
request.text_sha256,
request.package_type,
request.semantic_precision ?? null,
request.member_mutation_allowance,
]);
]),
packageType: request.package_type,
});
const staticReuseCapacityBytes = (): number =>
javaScriptRuntime(process.versions).id === "node"
? Math.floor(getHeapStatistics().heap_size_limit / 8)
: 0;
const releaseRetainedStatic = (
source: string,
): RetainedStaticEntry | undefined => {
const previous = retainedStatics.get(source);
if (previous === undefined) return undefined;
retainedStatics.delete(source);
retainedStaticBytes -= previous.checkpoint.retainedBytes;
return previous;
};
export const retainJavaScriptStaticCheckpoint = (
key: string,
key: JavaScriptStaticReuseKey,
facts: JavaScriptStaticAnalysis,
): void => {
const previous = retainedStatics.get(key);
if (previous !== undefined) {
retainedStatics.delete(key);
retainedStaticBytes -= previous.retainedBytes;
}
releaseRetainedStatic(key.source);
// Skip encoding where reuse is disabled or no capacity remains.
const capacity = staticReuseCapacityBytes();
if (retainedStaticBytes >= capacity) return;
@@ -137,23 +160,25 @@ export const retainJavaScriptStaticCheckpoint = (
return;
}
if (retainedStaticBytes + retainedBytes > capacity) return;
retainedStatics.set(key, {
text,
retainedBytes,
parse_status: facts.parse_status,
call_paths: facts.electron.call_paths,
retainedStatics.set(key.source, {
packageType: key.packageType,
checkpoint: {
text,
retainedBytes,
parse_status: facts.parse_status,
call_paths: facts.electron.call_paths,
},
});
retainedStaticBytes += retainedBytes;
};
export const takeJavaScriptStaticCheckpoint = (
key: string,
key: JavaScriptStaticReuseKey,
): RetainedStaticCheckpoint | undefined => {
const retained = retainedStatics.get(key);
if (retained === undefined) return undefined;
retainedStatics.delete(key);
retainedStaticBytes -= retained.retainedBytes;
return retained;
const retained = releaseRetainedStatic(key.source);
return retained?.packageType === key.packageType
? retained.checkpoint
: undefined;
};
/** Validate, fit and bound static facts as the checkpoint the parent reads. */
@@ -207,8 +207,14 @@ class ScopeJavaScriptSourceAnalysis implements JavaScriptSourceAnalysisPort {
current.busy = false;
return;
}
if (current?.worker === this.worker) retained.delete(this.scope);
// Release the scope's ownership only after the process stopped; a
// failed close stays owned by the scope's close hook.
await this.worker.close();
if (
current?.worker === this.worker &&
retained.get(this.scope) === current
)
retained.delete(this.scope);
} catch (cause) {
this.#closed = false;
throw cause;
@@ -66,7 +66,6 @@ it("retains a discovery checkpoint once for the exact request, including sources
const base = request(randomUUID(), plain, "analysis");
retainJavaScriptStaticCheckpoint(javaScriptStaticReuseKey(base), facts);
for (const other of [
request(randomUUID(), plain, "analysis", { package_type: "module" }),
request(randomUUID(), plain, "analysis", { path: "other.js" }),
request(randomUUID(), `${plain}\n`, "analysis"),
])
@@ -80,6 +79,29 @@ it("retains a discovery checkpoint once for the exact request, including sources
).toBe(encodeJavaScriptStaticCheckpoint(facts));
});
it("drops a checkpoint the normal pass reads with another package type", () => {
// Discovery reads an ESM package's preload as a module; the normal pass
// reads the discovered preload as CommonJS and cannot reuse that checkpoint.
const preload = "export const ready = true;";
const facts = analyzeJavaScriptStaticSource(preload);
const discovered = request(randomUUID(), preload, "preload-discovery", {
path: "preload.js",
package_type: "module",
});
const normal = request(randomUUID(), preload, "analysis", {
path: "preload.js",
package_type: "commonjs",
});
retainJavaScriptStaticCheckpoint(javaScriptStaticReuseKey(discovered), facts);
expect(
takeJavaScriptStaticCheckpoint(javaScriptStaticReuseKey(normal)),
).toBeUndefined();
// The mismatched take released the entry rather than parking it forever.
expect(
takeJavaScriptStaticCheckpoint(javaScriptStaticReuseKey(discovered)),
).toBeUndefined();
});
/** Start the production worker entrypoint in its own private root. */
const startWorker = async () => {
const root = await createTestTempDirectory("rea-js-static-reuse-");