diff --git a/.changeset/trust-refresh-admission.md b/.changeset/trust-refresh-admission.md new file mode 100644 index 0000000000..a653fbf8e3 --- /dev/null +++ b/.changeset/trust-refresh-admission.md @@ -0,0 +1,5 @@ +--- +"@schlessera/brain-ui-server": patch +--- + +Keep turn admission independent of a running index when the verified policy hash already matches the healthy durable trust projection. Changed or unavailable policy still refreshes under the cancellable brain lock. diff --git a/docs/integration-contract/package-api.md b/docs/integration-contract/package-api.md index cd478456e6..12c98a5009 100644 --- a/docs/integration-contract/package-api.md +++ b/docs/integration-contract/package-api.md @@ -5699,6 +5699,8 @@ Exhausted revisions refuse both new reviews and publication of retained reviews before any protected policy write; request revisions above uint64 also refuse. The composed host shares its live trust revision with proxy and backend admission. The protected policy proposal publisher refreshes the same projection under the shared brain lock before reporting updated; qualified turn preparation refreshes it too. A synchronous anchored trust-list check refuses stale disk/projection matches during that refresh gap. Invalid policy retains last-valid rules with an error. Ordinary turn budgets, commands and profiles retain their admitted snapshots. +`OriginTrust.refresh` returns without the brain lock when the observed policy hash equals the durable projection's hash and the projection carries no error; otherwise it queues the lock as before, cancellable on turn abort. + The HTTP boundary admits at most 32 concurrent requests per principal and 256 for the served brain, including lock waiters; excess requests return retryable `busy` (503) before joining another wait and recover as earlier requests finish. @@ -6043,7 +6045,7 @@ approval changes are `safety_changed`, denied recipient/destination policy is client cancellation is `cancelled`. Their statuses are 403, 403, 503 and 499 respectively; disconnected transports may have no response status. Inference/provider uploads, headers, streams and managed GitHub wire metering consume this result. `OriginTrust.refresh(signal?)` cancels a -queued brain-lock acquisition on turn abort; it never releases another holder. +queued brain-lock acquisition on turn abort; it never releases another holder. `OriginTrust.refresh` returns without the brain lock when the observed policy hash equals the durable projection's hash and the projection carries no error; otherwise it queues the lock as before, cancellable on turn abort. Turn startup rechecks abort before creating egress resources and normal turn teardown releases its own governed hold and concurrency slot. diff --git a/packages/ui-server/src/brain/trust-settings.ts b/packages/ui-server/src/brain/trust-settings.ts index e1a4ecf3c1..8244bdad15 100644 --- a/packages/ui-server/src/brain/trust-settings.ts +++ b/packages/ui-server/src/brain/trust-settings.ts @@ -159,7 +159,18 @@ export function createTrustSettings(options: { liveCache = { observation, base, value }; return value; }; const live = { read: freshProjection, revision: () => freshProjection().revision, - refresh: (signal?: AbortSignal) => options.lock.withLock(BRAIN_LOCK_KEY, () => { signal?.throwIfAborted(); return refresh(); }, { signal }), subscribe(changed: () => void) { listeners.add(changed); return () => { listeners.delete(changed); }; } }; + refresh: async (signal?: AbortSignal) => { + signal?.throwIfAborted(); + try { + const value = observePolicy(), row = projection(); + // A missing row's synthetic empty projection is not durable proof. + const exists = options.db.query("SELECT 1 FROM trust_settings_projection WHERE brain_id=?").get(brain) !== null; + if (value.kind === "valid" && exists && row.error === null && row.policyHash === value.policyHash) return freshProjection(); + } catch (error) { + if (!(error instanceof TrustSettingsError) || error.code !== "stale_revision") throw error; + } + return options.lock.withLock(BRAIN_LOCK_KEY, () => { signal?.throwIfAborted(); return refresh(); }, { signal }); + }, subscribe(changed: () => void) { listeners.add(changed); return () => { listeners.delete(changed); }; } }; const refresh = async (): Promise => { const old = projection(), value = observePolicy(); if (value.kind === "unavailable") { diff --git a/packages/ui-server/tests/trust-settings.test.ts b/packages/ui-server/tests/trust-settings.test.ts index fc7f78242d..e73958b8e3 100644 --- a/packages/ui-server/tests/trust-settings.test.ts +++ b/packages/ui-server/tests/trust-settings.test.ts @@ -492,7 +492,7 @@ test('review5: malformed live policy reports an error before any refresh',async( for(const refreshing of [false,true])test(`review5: removal during anchored verification fences ${refreshing?'refresh':'read'}`,async()=>{ const f=await fixture({originEnforcement:true});await f.commit(await f.prepare());writeFileSync(f.path,readFileSync(f.path,'utf8')+'\n');const read=fs.readSync;let swapped=false; const spy=spyOn(fs,'readSync').mockImplementation(((...args:Parameters)=>{const n=Reflect.apply(read,fs,args);if(n>0&&!swapped){swapped=true;renameSync(f.path,f.path+'.old');writeFileSync(f.path,initial);}return n;}) as typeof fs.readSync); - try{if(refreshing)await expect(f.settings.live.refresh(),'CHANGED_DURING_VERIFICATION_NEVER_RETAINS_RULE').rejects.toThrow();else expect(()=>f.settings.live.read(),'CHANGED_DURING_VERIFICATION_NEVER_RETAINS_RULE').toThrow();expect(swapped).toBe(true);}finally{spy.mockRestore();} + try{if(refreshing){const current=await f.settings.live.refresh();expect(current.rules,'LOCKED_RETRY_NEVER_RETAINS_REMOVED_RULE').toEqual([]);expect(current.revision).toBe('2');}else expect(()=>f.settings.live.read(),'CHANGED_DURING_VERIFICATION_NEVER_RETAINS_RULE').toThrow();expect(swapped).toBe(true);}finally{spy.mockRestore();} }); test('review5: unchanged large policy uses a cheap per-chunk revision fence',async()=>{ const f=await fixture({originEnforcement:true});await f.commit(await f.prepare());writeFileSync(f.path,readFileSync(f.path,'utf8')+'\n'+ 'Ithaca. '.repeat(12000));const p=f.settings.live.read();const read=fs.readSync;let reads=0; @@ -516,8 +516,74 @@ test('review7: a second reader recovers after the writer commits the same file g expect(second.live.read().rules,'SECOND_READER_RECOVERS_WITHOUT_EXPLICIT_REFRESH').toEqual([]);expect(second.live.revision()).toBe('2'); }); +test("unchanged durable trust refresh does not queue the held brain lock", async () => { + const f = await fixture({ originEnforcement: true }); + await f.commit(await f.prepare()); + const committed = f.settings.live.read(); + const release = await f.lock.acquire(BRAIN_LOCK_KEY); + const original = f.lock.withLock.bind(f.lock); + let calls = 0; + f.lock.withLock = (key, fn, options) => { if (key === BRAIN_LOCK_KEY) calls++; return original(key, fn, options); }; + let pending: ReturnType | undefined; + try { + pending = f.settings.live.refresh(); + expect(calls, "UNCHANGED_REFRESH_NEVER_QUEUES_BRAIN_LOCK").toBe(0); + expect(await pending).toEqual(committed); + expect(f.lock.heldKeys).toContain(BRAIN_LOCK_KEY); + } finally { release(); await pending; } +}); + +test("diverged trust refresh queues the held brain lock and publishes a new revision after release", async () => { + const f = await fixture({ originEnforcement: true }); + await f.commit(await f.prepare()); + const before = f.settings.live.read(); + writeFileSync(f.path, initial); + const release = await f.lock.acquire(BRAIN_LOCK_KEY); + const original = f.lock.withLock.bind(f.lock); + let calls = 0, completed = false; + f.lock.withLock = (key, fn, options) => { if (key === BRAIN_LOCK_KEY) calls++; return original(key, fn, options); }; + const pending = f.settings.live.refresh().then(value => { completed = true; return value; }); + try { + expect(calls, "CHANGED_POLICY_STILL_QUEUES_BRAIN_LOCK").toBe(1); + await Promise.resolve(); + expect(completed).toBe(false); + expect(f.lock.heldKeys).toContain(BRAIN_LOCK_KEY); + } finally { release(); } + const after = await pending; + expect(after.rules).toEqual([]); + expect(BigInt(after.revision)).toBe(BigInt(before.revision) + 1n); +}); + +test("ADMISSION_NEVER_WAITS_ON_HELD_INDEX_LOCK", async () => { + const f = await fixture({ originEnforcement: true }); + await f.commit(await f.prepare()); + const { WsHost } = await import("../src/ws/host"); + const { runSession } = await import("../src/ws/run-session"); + const { createSessionCatalog } = await import("../src/ws/session-catalog"); + const { createStaticBackendRegistry } = await import("../src/agent/backend"); + const { makeFakeBackend } = await import("./helpers/fake-backend"); + const { testAuthorization } = await import("./helpers/principal"); + let started = 0, queued!: () => void; + const blocked = new Promise(resolve => { queued = resolve; }); + const original = f.lock.withLock.bind(f.lock); + f.lock.withLock = (key, fn, options) => { if (key === BRAIN_LOCK_KEY) queued(); return original(key, fn, options); }; + const policy = createGovernancePolicyReader(f.root).load(); + const backend = makeFakeBackend({ id: "pi", async startTurn() { started++; } }); + const host = new WsHost({ registry: createStaticBackendRegistry([backend], "pi"), catalog: createSessionCatalog(() => f.db), originTrust: f.settings.live, + governedTurns: { async begin() { return { sandbox: { profile: "strict", noteFacts: {} }, policy, async end() { return null; } }; }, refresh() {}, async close() {}, async bindSession() {}, async exposures() { return []; }, facts() { return { policyProfile: "strict", policy }; } } as never }); + const release = await f.lock.acquire(BRAIN_LOCK_KEY); + const running = runSession(host, { authorization: testAuthorization(), text: "Plan Ithaca", attachments: [] }); + try { + const finished = await Promise.race([running.then(() => true), blocked.then(() => false)]); + expect(started, "ADMISSION_NEVER_WAITS_ON_HELD_INDEX_LOCK").toBe(1); + expect(finished, "SESSION_FINISHES_BEFORE_INDEX_LOCK_RELEASE").toBe(true); + expect(f.lock.heldKeys).toContain(BRAIN_LOCK_KEY); + } finally { release(); await running; host.close(); } +}); + for(const timeout of [false,true])test(`review10: turn trust refresh releases governed resources before the lock holder on ${timeout?'timeout':'cancel'}`,async()=>{ const f=await fixture({originEnforcement:true});const {WsHost}=await import('../src/ws/host');const {runSession}=await import('../src/ws/run-session');const {createSessionCatalog}=await import('../src/ws/session-catalog');const {createStaticBackendRegistry}=await import('../src/agent/backend');const {makeFakeBackend}=await import('./helpers/fake-backend');const {testAuthorization}=await import('./helpers/principal'); + await f.commit(await f.prepare());writeFileSync(f.path,initial); let held=0,entered!:()=>void,started=0;const reached=new Promise(r=>{entered=r;});const original=f.lock.withLock.bind(f.lock);f.lock.withLock=(key,fn,opts)=>{if(key===BRAIN_LOCK_KEY)entered();return original(key,fn,opts);}; const policy=createGovernancePolicyReader(f.root).load();const backend=makeFakeBackend({id:'pi',async startTurn(){started++;}});const host=new WsHost({registry:createStaticBackendRegistry([backend],'pi'),catalog:createSessionCatalog(()=>f.db),turnTimeoutMs:5000,originTrust:f.settings.live,governedTurns:{async begin(){held++;return {sandbox:{profile:'strict',noteFacts:{}},policy,async end(){held--;return null;}};},refresh(){},async close(){},async bindSession(){},async exposures(){return [];},facts(){return {policyProfile:'strict',policy};}} as never}); const release=await f.lock.acquire(BRAIN_LOCK_KEY);let running:Promise|undefined;