|
1 | 1 | import { test, beforeEach, afterEach } from "node:test"; |
2 | 2 | import assert from "node:assert/strict"; |
3 | 3 | import ext, { |
| 4 | + __resetAdvisedForTests, |
4 | 5 | findBetaHeader, |
5 | 6 | parseBetaTokens, |
6 | 7 | planSanitizeBetaHeader, |
@@ -189,3 +190,53 @@ test("onRequest: duplicate `context-1m-2025-08-07` tokens (defensive) — all re |
189 | 190 | "claude-code-20250219, oauth_auth, interleaved-thinking-2025-05-14", |
190 | 191 | ); |
191 | 192 | }); |
| 193 | + |
| 194 | +// --- the advisory is advice, not a per-request fact --- |
| 195 | + |
| 196 | +// The advisory sampler, shared by the two cases below. Forwards what it is not |
| 197 | +// sampling, so it cannot swallow an unrelated line. Resets the latch on both |
| 198 | +// sides -- a spent one makes a case appended later count 0 and read green for |
| 199 | +// the wrong reason. |
| 200 | +async function sampleAdvisories(fn) { |
| 201 | + __resetAdvisedForTests(); |
| 202 | + const seen = []; |
| 203 | + const orig = process.stderr.write; |
| 204 | + process.stderr.write = (s) => { |
| 205 | + if (!String(s).includes("[auto-1m-guard]")) return orig.call(process.stderr, s); |
| 206 | + seen.push(String(s)); |
| 207 | + return true; |
| 208 | + }; |
| 209 | + try { await fn(); } finally { process.stderr.write = orig; __resetAdvisedForTests(); } |
| 210 | + return seen; |
| 211 | +} |
| 212 | + |
| 213 | +test("onRequest: the advisory is written once, but every request is still annotated", async () => { |
| 214 | + const seen = await sampleAdvisories(async () => { |
| 215 | + for (let i = 0; i < 5; i++) { |
| 216 | + const ctx = mkCtx({ headers: { "anthropic-beta": STD_BETAS_WITH_1M }, mode: "warn" }); |
| 217 | + await ext.onRequest(ctx); |
| 218 | + // The latch sits below the annotation, which every request's session JSON needs. |
| 219 | + assert.equal(ctx.meta._auto1mGuard?.auto_1m_detected, true, `request ${i} lost its annotation`); |
| 220 | + } |
| 221 | + }); |
| 222 | + assert.equal(seen.length, 1, `advisory written ${seen.length}x for 5 requests`); |
| 223 | +}); |
| 224 | + |
| 225 | +test("onRequest: the advisory latch spans the process, not one module instance", async () => { |
| 226 | + // loadExtensions cache-busts every import (pipeline.mjs), so this module is |
| 227 | + // re-evaluated inside ONE process on every reload -- and a module-scoped latch |
| 228 | + // re-arms there, returning the advisory this exists to silence. |
| 229 | + const href = new URL("../proxy/extensions/auto-1m-guard.mjs", import.meta.url).href; |
| 230 | + const a = await import(`${href}?latch=a`); |
| 231 | + const b = await import(`${href}?latch=b`); |
| 232 | + assert.notEqual(a.default, b.default, |
| 233 | + "premise: both imports resolved to the same module, so this case proves nothing"); |
| 234 | + |
| 235 | + const seen = await sampleAdvisories(async () => { |
| 236 | + for (const m of [a, b]) { |
| 237 | + await m.default.onRequest(mkCtx({ headers: { "anthropic-beta": STD_BETAS_WITH_1M } })); |
| 238 | + } |
| 239 | + }); |
| 240 | + assert.equal(seen.length, 1, |
| 241 | + `two module instances in one process wrote ${seen.length} advisories`); |
| 242 | +}); |
0 commit comments