fix(plugin-ai): validate context references before publishing turns

This commit is contained in:
陈奇 committed 2026-10-26 14:46:30 +00:00
1 parent 6392ac9532
commit c3a5f810ce
4 files changed
+47 -4

No files matched your search

@@ -147,7 +147,11 @@ class SelectedContextAuthority {
}
release(handle) {
if (!this.handles.delete(handle)) return;
for (const listener of this.listeners) listener(handle);
for (const listener of [...this.listeners]) {
// Revocation must finish even when an observer fails. Never log selected
// text or let an observer exception retain another runtime handle.
try { listener(handle); } catch {}
}
}
releaseRuntime(runtimeId) {
for (const [handle, entry] of this.handles) if (entry.runtimeId === runtimeId) this.release(handle);
@@ -75,3 +75,13 @@ test("content reader has a deadline and respects request cancellation", async ()
const controller = new AbortController(); const reading = authority.read(note, { ...context, signal: controller.signal });
controller.abort(); await assert.rejects(reading); authority.shutdown();
});
test("one failing invalidation listener cannot interrupt other listeners or handle cleanup", async () => {
const f = fixture(); const first = await f.authority.share(f.selection, f.context); const second = await f.authority.share(f.selection, f.context);
const notified = [];
f.authority.onDidInvalidate(() => { throw Error("listener failed"); });
f.authority.onDidInvalidate((handle) => notified.push(handle));
assert.doesNotThrow(() => f.authority.releaseRuntime("r1"));
assert.equal(f.authority.handles.size, 0);
assert.deepEqual(notified, [first.handle, second.handle]);
});