opencode-cache-engine 0.3.6 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/docs/cache-policy-inventory.md +313 -0
- package/package.json +1 -1
- package/src/cache-engine-core.mjs +16 -50
- package/src/cache-engine.ts +60 -49
- package/src/cache-policy-core.mjs +512 -0
- package/test/cache-engine.test.mjs +425 -0
|
@@ -48,6 +48,15 @@ import {
|
|
|
48
48
|
toolFingerprint,
|
|
49
49
|
toolWireFingerprint,
|
|
50
50
|
} from "../src/cache-engine-core.mjs"
|
|
51
|
+
import {
|
|
52
|
+
BASELINES,
|
|
53
|
+
MODEL_ALIASES,
|
|
54
|
+
OVERLAYS,
|
|
55
|
+
POLICY_REGISTRY,
|
|
56
|
+
resolveLegacyFamily,
|
|
57
|
+
resolvePolicy,
|
|
58
|
+
resolveRuntimePolicy,
|
|
59
|
+
} from "../src/cache-policy-core.mjs"
|
|
51
60
|
|
|
52
61
|
const asst = (id, read, write) => ({
|
|
53
62
|
info: { id, role: "assistant", tokens: { cache: { read, write } } },
|
|
@@ -1316,3 +1325,419 @@ test("affinity telemetry preserves non-OpenRouter families and reports provider
|
|
|
1316
1325
|
assert.equal(glmChange.to.providerID, "zai")
|
|
1317
1326
|
assert.equal(glmChange.policy, POLICY_GLM53)
|
|
1318
1327
|
})
|
|
1328
|
+
|
|
1329
|
+
// ===========================================================================
|
|
1330
|
+
// v0.4.0 policy registry + resolvePolicy() (research-backed, pure, unwired)
|
|
1331
|
+
//
|
|
1332
|
+
// The inventory (docs/cache-policy-inventory.md) is the authority. These tests
|
|
1333
|
+
// assert the structured resolution layer and that detectPolicy() remains
|
|
1334
|
+
// byte-compatible with its pre-v0.4.0 behavior.
|
|
1335
|
+
// ===========================================================================
|
|
1336
|
+
|
|
1337
|
+
const overlayIds = (r) => r.overlays.map((o) => o.id)
|
|
1338
|
+
const baseId = (r) => (r.baseline ? r.baseline.id : null)
|
|
1339
|
+
|
|
1340
|
+
test("resolvePolicy: GPT-5.6 exact + inventory aliases resolve to the gpt-5.6 family", () => {
|
|
1341
|
+
const exact = resolvePolicy(M("openai", "gpt-5.6-sol"))
|
|
1342
|
+
assert.equal(exact.creator, "openai")
|
|
1343
|
+
assert.equal(exact.family, "gpt-5.6")
|
|
1344
|
+
assert.equal(exact.matchType, "exact")
|
|
1345
|
+
assert.equal(baseId(exact), "openai.gpt56.cache")
|
|
1346
|
+
assert.deepEqual(overlayIds(exact), ["gpt56.prompt-cache-options"])
|
|
1347
|
+
assert.equal(exact.transport.kind, "direct")
|
|
1348
|
+
|
|
1349
|
+
// Inventory alias: gpt-5.6 -> gpt-5.6-sol
|
|
1350
|
+
const alias = resolvePolicy(M("openai", "gpt-5.6"))
|
|
1351
|
+
assert.equal(alias.family, "gpt-5.6")
|
|
1352
|
+
assert.equal(alias.matchType, "exact")
|
|
1353
|
+
assert.ok(alias.matchReason.startsWith("alias:gpt-5.6"))
|
|
1354
|
+
assert.deepEqual(overlayIds(alias), ["gpt56.prompt-cache-options"])
|
|
1355
|
+
|
|
1356
|
+
// OpenRouter-prefixed documented variant resolves by exact id (prefix stripped)
|
|
1357
|
+
const orVariant = resolvePolicy(M("openrouter", "openai/gpt-5.6-luna"))
|
|
1358
|
+
assert.equal(orVariant.family, "gpt-5.6")
|
|
1359
|
+
assert.equal(orVariant.matchType, "exact")
|
|
1360
|
+
})
|
|
1361
|
+
|
|
1362
|
+
test("resolvePolicy: GPT-6 resolves via explicit documented inheritance, with no overlay", () => {
|
|
1363
|
+
const r = resolvePolicy(M("openrouter", "openai/gpt-6-luna"))
|
|
1364
|
+
assert.equal(r.creator, "openai")
|
|
1365
|
+
assert.equal(r.family, "gpt-6")
|
|
1366
|
+
assert.equal(baseId(r), "openai.gpt56.cache")
|
|
1367
|
+
assert.deepEqual(overlayIds(r), [])
|
|
1368
|
+
assert.equal(resolvePolicy(M("openai", "gpt-6-astra")).family, "gpt-6")
|
|
1369
|
+
|
|
1370
|
+
// Inheritance is explicit, not "newer means same": the registry records it.
|
|
1371
|
+
const gpt6 = POLICY_REGISTRY.find((e) => e.family === "gpt-6")
|
|
1372
|
+
assert.equal(gpt6.inheritsFrom, "gpt-5.6")
|
|
1373
|
+
assert.ok(gpt6.inventoryRef)
|
|
1374
|
+
// And no overlay is implied by that inheritance.
|
|
1375
|
+
assert.deepEqual(gpt6.overlays, [])
|
|
1376
|
+
})
|
|
1377
|
+
|
|
1378
|
+
test("resolvePolicy: GPT-6 is not classified by the legacy detectPolicy wrapper", () => {
|
|
1379
|
+
// Intentional: the new layer knows gpt-6, the compatibility wrapper does not.
|
|
1380
|
+
assert.equal(detectPolicy(M("openai", "gpt-6-astra")), POLICY_NEUTRAL)
|
|
1381
|
+
assert.equal(resolvePolicy(M("openai", "gpt-6-astra")).family, "gpt-6")
|
|
1382
|
+
})
|
|
1383
|
+
|
|
1384
|
+
test("resolvePolicy: pre-5.6 GPT negative controls are neutral with no overlays", () => {
|
|
1385
|
+
for (const id of ["gpt-5.5", "gpt-5.4", "gpt-5.2", "gpt-5.1", "gpt-5", "gpt-4.1", "gpt-4o"]) {
|
|
1386
|
+
const r = resolvePolicy(M("openai", id))
|
|
1387
|
+
assert.equal(r.family, "neutral", `${id} must be neutral`)
|
|
1388
|
+
assert.deepEqual(overlayIds(r), [])
|
|
1389
|
+
assert.equal(r.matchType, "neutral")
|
|
1390
|
+
}
|
|
1391
|
+
})
|
|
1392
|
+
|
|
1393
|
+
test("resolvePolicy: DeepSeek V4 / V4.1 resolve to the creator baseline (passive, no overlay)", () => {
|
|
1394
|
+
const v4 = resolvePolicy(M("deepseek", "deepseek-v4-pro"))
|
|
1395
|
+
assert.equal(v4.creator, "deepseek")
|
|
1396
|
+
assert.equal(v4.family, "deepseek")
|
|
1397
|
+
assert.equal(v4.matchType, "creator")
|
|
1398
|
+
assert.equal(baseId(v4), "deepseek.kv-cache")
|
|
1399
|
+
assert.deepEqual(overlayIds(v4), [])
|
|
1400
|
+
|
|
1401
|
+
assert.equal(resolvePolicy(M("deepseek", "deepseek-flash")).family, "deepseek")
|
|
1402
|
+
|
|
1403
|
+
// Inventory alias: retired deepseek-v4-flash -> deepseek-flash
|
|
1404
|
+
const legacy = resolvePolicy(M("deepseek", "deepseek-v4-flash"))
|
|
1405
|
+
assert.equal(legacy.family, "deepseek")
|
|
1406
|
+
assert.ok(legacy.matchReason.startsWith("alias:deepseek-v4-flash"))
|
|
1407
|
+
|
|
1408
|
+
// pre-V4 negative control still resolves to the creator baseline
|
|
1409
|
+
assert.equal(resolvePolicy(M("deepseek", "deepseek-chat")).family, "deepseek")
|
|
1410
|
+
})
|
|
1411
|
+
|
|
1412
|
+
test("resolvePolicy: GLM-5.3 + documented 5.3 variants carry the overlay; 5.2 and earlier do not", () => {
|
|
1413
|
+
const r = resolvePolicy(M("zai", "glm-5.3"))
|
|
1414
|
+
assert.equal(r.creator, "z.ai")
|
|
1415
|
+
assert.equal(r.family, "glm-5.3")
|
|
1416
|
+
assert.equal(baseId(r), "zai.implicit-cache")
|
|
1417
|
+
assert.deepEqual(overlayIds(r), ["glm53.env-relocation"])
|
|
1418
|
+
|
|
1419
|
+
assert.equal(resolvePolicy(M("zai", "glm-5.3-flash")).family, "glm-5.3")
|
|
1420
|
+
assert.equal(resolvePolicy(M("z-ai", "glm-5.3-flashx")).family, "glm-5.3")
|
|
1421
|
+
|
|
1422
|
+
for (const id of ["glm-5.2", "glm-5.1", "glm-5", "glm-4.7", "glm-4.6", "glm-4.5"]) {
|
|
1423
|
+
const g = resolvePolicy(M("zai", id))
|
|
1424
|
+
assert.equal(g.family, "neutral", `${id} must be neutral`)
|
|
1425
|
+
assert.deepEqual(overlayIds(g), [])
|
|
1426
|
+
}
|
|
1427
|
+
})
|
|
1428
|
+
|
|
1429
|
+
test("resolvePolicy: MiMo V2.6 Flash/Pro carry the overlay; Pro UltraSpeed is an explicit no-overlay series member", () => {
|
|
1430
|
+
const flash = resolvePolicy(M("xiaomi", "mimo-v2.6-flash"))
|
|
1431
|
+
assert.equal(flash.creator, "xiaomi")
|
|
1432
|
+
assert.equal(flash.family, "mimo-v2.6")
|
|
1433
|
+
assert.equal(baseId(flash), "xiaomi.implicit-cache")
|
|
1434
|
+
assert.deepEqual(overlayIds(flash), ["mimo26.env-relocation"])
|
|
1435
|
+
|
|
1436
|
+
const pro = resolvePolicy(M("xiaomi", "mimo-v2.6-pro"))
|
|
1437
|
+
assert.equal(pro.family, "mimo-v2.6")
|
|
1438
|
+
assert.deepEqual(overlayIds(pro), ["mimo26.env-relocation"])
|
|
1439
|
+
|
|
1440
|
+
// Documented as the same V2.6 series but with NO registered overlay.
|
|
1441
|
+
const ultraspeed = resolvePolicy(M("xiaomi", "mimo-v2.6-pro-ultraspeed"))
|
|
1442
|
+
assert.equal(ultraspeed.family, "mimo-v2.6")
|
|
1443
|
+
assert.equal(ultraspeed.matchType, "exact")
|
|
1444
|
+
assert.deepEqual(overlayIds(ultraspeed), [])
|
|
1445
|
+
const uEntry = POLICY_REGISTRY.find((e) => e.id === "xiaomi.mimo-v2.6-pro-ultraspeed")
|
|
1446
|
+
assert.equal(uEntry.policyStatus, "documented-series-member-without-registered-overlay")
|
|
1447
|
+
|
|
1448
|
+
const v25 = resolvePolicy(M("xiaomi", "mimo-v2.5"))
|
|
1449
|
+
assert.equal(v25.family, "neutral")
|
|
1450
|
+
assert.deepEqual(overlayIds(v25), [])
|
|
1451
|
+
})
|
|
1452
|
+
|
|
1453
|
+
test("resolvePolicy: overlays are never implied by creator/family classification", () => {
|
|
1454
|
+
// Being GLM/MiMo/DeepSeek does not by itself grant a transformation overlay.
|
|
1455
|
+
assert.deepEqual(overlayIds(resolvePolicy(M("zai", "glm-4.6"))), [])
|
|
1456
|
+
assert.deepEqual(overlayIds(resolvePolicy(M("xiaomi", "mimo-v2.5"))), [])
|
|
1457
|
+
assert.deepEqual(overlayIds(resolvePolicy(M("deepseek", "deepseek-v4-pro"))), [])
|
|
1458
|
+
// A hypothetical future GLM does not silently inherit the 5.3 overlay.
|
|
1459
|
+
assert.deepEqual(overlayIds(resolvePolicy(M("zai", "glm-5.4"))), [])
|
|
1460
|
+
})
|
|
1461
|
+
|
|
1462
|
+
test("resolvePolicy: malformed and unknown model ids resolve safely", () => {
|
|
1463
|
+
for (const bad of [undefined, null, {}, 42, "gpt-5.6", [], true]) {
|
|
1464
|
+
const r = resolvePolicy(bad)
|
|
1465
|
+
assert.equal(r.family, "neutral")
|
|
1466
|
+
assert.equal(r.matchType, "neutral")
|
|
1467
|
+
assert.equal(baseId(r), "neutral.none")
|
|
1468
|
+
assert.deepEqual(r.overlays, [])
|
|
1469
|
+
}
|
|
1470
|
+
// A provider with no model identity gets no family.
|
|
1471
|
+
assert.equal(resolvePolicy({ providerID: "openai" }).family, "neutral")
|
|
1472
|
+
})
|
|
1473
|
+
|
|
1474
|
+
test("resolvePolicy: unknown providers/creators do not gain policy from transport identity", () => {
|
|
1475
|
+
const orUnknown = resolvePolicy(M("openrouter", "acme/mystery-model-9"))
|
|
1476
|
+
assert.equal(orUnknown.family, "neutral")
|
|
1477
|
+
assert.deepEqual(orUnknown.overlays, [])
|
|
1478
|
+
assert.equal(orUnknown.transport.kind, "openrouter")
|
|
1479
|
+
assert.equal(orUnknown.transport.sessionAffinityHeader, "x-session-id")
|
|
1480
|
+
|
|
1481
|
+
// gpt-5.6 on a non-OpenAI gateway stays neutral even though the id looks OpenAI
|
|
1482
|
+
assert.equal(resolvePolicy(M("some-gateway", "gpt-5.6")).family, "neutral")
|
|
1483
|
+
assert.equal(resolvePolicy(M("xiaomi", "not-a-mimo")).family, "neutral")
|
|
1484
|
+
})
|
|
1485
|
+
|
|
1486
|
+
test("resolvePolicy: transport is computed independently from cache policy", () => {
|
|
1487
|
+
const ds = resolvePolicy(M("deepseek", "deepseek-v4-pro"))
|
|
1488
|
+
assert.equal(ds.transport.kind, "direct")
|
|
1489
|
+
assert.equal(ds.transport.sessionAffinityHeader, null)
|
|
1490
|
+
|
|
1491
|
+
const or = resolvePolicy(M("openrouter", "openai/gpt-5.6-luna"))
|
|
1492
|
+
assert.equal(or.family, "gpt-5.6")
|
|
1493
|
+
assert.equal(or.transport.kind, "openrouter")
|
|
1494
|
+
assert.equal(or.transport.sessionAffinityHeader, "x-session-id")
|
|
1495
|
+
|
|
1496
|
+
const zai = resolvePolicy(M("zai", "glm-5.3"))
|
|
1497
|
+
assert.equal(zai.transport.kind, "direct")
|
|
1498
|
+
assert.equal(zai.transport.sessionAffinityHeader, null)
|
|
1499
|
+
|
|
1500
|
+
const noProvider = resolvePolicy({ modelID: "gpt-6-astra" })
|
|
1501
|
+
assert.equal(noProvider.transport.kind, "unknown")
|
|
1502
|
+
})
|
|
1503
|
+
|
|
1504
|
+
test("resolvePolicy is pure and does not mutate its input", () => {
|
|
1505
|
+
const model = Object.freeze({ providerID: "openai", modelID: "gpt-5.6-sol" })
|
|
1506
|
+
const r = resolvePolicy(model)
|
|
1507
|
+
assert.equal(r.family, "gpt-5.6")
|
|
1508
|
+
assert.equal(model.modelID, "gpt-5.6-sol")
|
|
1509
|
+
})
|
|
1510
|
+
|
|
1511
|
+
test("detectPolicy remains compatible with the legacy family resolution", () => {
|
|
1512
|
+
const legacyMap = {
|
|
1513
|
+
"gpt-5.6": POLICY_GPT56,
|
|
1514
|
+
"glm-5.3": POLICY_GLM53,
|
|
1515
|
+
"mimo-v2.6": POLICY_MIMO26,
|
|
1516
|
+
deepseek: POLICY_DEEPSEEK,
|
|
1517
|
+
}
|
|
1518
|
+
const samples = [
|
|
1519
|
+
M("openrouter", "openai/gpt-5.6-luna"),
|
|
1520
|
+
M("openai", "gpt-5.6"),
|
|
1521
|
+
M("openai-compatible", "gpt-5.6"),
|
|
1522
|
+
M("openai", "gpt-5.5"),
|
|
1523
|
+
M("zai", "glm-5.3-flash"),
|
|
1524
|
+
M("zai", "glm-4.6"),
|
|
1525
|
+
M("xiaomi", "mimo-v2.6-flash"),
|
|
1526
|
+
M("xiaomi", "mimo-v2.6-pro-ultraspeed"),
|
|
1527
|
+
M("xiaomi", "mimo-v2.5"),
|
|
1528
|
+
M("deepseek", "deepseek-chat"),
|
|
1529
|
+
M("openrouter", "x-ai/grok-4"),
|
|
1530
|
+
M("anthropic", "claude-sonnet-4-5"),
|
|
1531
|
+
{},
|
|
1532
|
+
null,
|
|
1533
|
+
undefined,
|
|
1534
|
+
42,
|
|
1535
|
+
]
|
|
1536
|
+
for (const m of samples) {
|
|
1537
|
+
const expected = legacyMap[resolveLegacyFamily(m)] ?? POLICY_NEUTRAL
|
|
1538
|
+
assert.equal(detectPolicy(m), expected)
|
|
1539
|
+
}
|
|
1540
|
+
})
|
|
1541
|
+
|
|
1542
|
+
test("registry is traceable and internally consistent", () => {
|
|
1543
|
+
for (const entry of POLICY_REGISTRY) {
|
|
1544
|
+
assert.ok(entry.inventoryRef, `${entry.id} must cite the inventory`)
|
|
1545
|
+
assert.ok(BASELINES[entry.baseline], `${entry.id} baseline must exist`)
|
|
1546
|
+
for (const overlayId of entry.overlays) {
|
|
1547
|
+
assert.ok(OVERLAYS[overlayId], `${entry.id} overlay ${overlayId} must exist`)
|
|
1548
|
+
}
|
|
1549
|
+
}
|
|
1550
|
+
for (const [id, alias] of Object.entries(MODEL_ALIASES)) {
|
|
1551
|
+
assert.ok(alias.inventoryRef, `alias ${id} must cite the inventory`)
|
|
1552
|
+
assert.ok(alias.family)
|
|
1553
|
+
}
|
|
1554
|
+
})
|
|
1555
|
+
|
|
1556
|
+
// ===========================================================================
|
|
1557
|
+
// v0.4.1 runtime policy migration (behavior preservation)
|
|
1558
|
+
//
|
|
1559
|
+
// The runtime now classifies via resolveRuntimePolicy(). These tests prove the
|
|
1560
|
+
// resolved policy equals the legacy detectPolicy() string for every supported
|
|
1561
|
+
// model, and that the hook-observable behavior (GPT cache options, <env>
|
|
1562
|
+
// relocation, OpenRouter affinity) is unchanged. Newer/unknown models must
|
|
1563
|
+
// gain no mutation.
|
|
1564
|
+
// ===========================================================================
|
|
1565
|
+
|
|
1566
|
+
const policyCoreURL = new URL("../src/cache-policy-core.mjs", import.meta.url).href
|
|
1567
|
+
|
|
1568
|
+
test("v0.4.1: resolveRuntimePolicy.policy matches detectPolicy across a broad matrix", () => {
|
|
1569
|
+
const samples = [
|
|
1570
|
+
M("openai", "gpt-5.6"),
|
|
1571
|
+
M("openai", "gpt-5.6-sol"),
|
|
1572
|
+
M("openrouter", "openai/gpt-5.6-luna"),
|
|
1573
|
+
M("openai-compatible", "gpt-5.6"),
|
|
1574
|
+
M("openai", "gpt-6-astra"),
|
|
1575
|
+
M("openai", "gpt-5.5"),
|
|
1576
|
+
M("openai", "gpt-daybreak-blue-latest"),
|
|
1577
|
+
M("openai", "gpt-daybreak-red-latest"),
|
|
1578
|
+
M("zai", "glm-5.3"),
|
|
1579
|
+
M("zai", "glm-5.3-flash"),
|
|
1580
|
+
M("zai", "glm-5.2"),
|
|
1581
|
+
M("xiaomi", "mimo-v2.6-flash"),
|
|
1582
|
+
M("xiaomi", "mimo-v2.6-pro"),
|
|
1583
|
+
M("xiaomi", "mimo-v2.6-pro-ultraspeed"),
|
|
1584
|
+
M("xiaomi", "mimo-v2.5"),
|
|
1585
|
+
M("deepseek", "deepseek-v4-pro"),
|
|
1586
|
+
M("deepseek", "deepseek-flash"),
|
|
1587
|
+
M("deepseek", "deepseek-v4-flash"),
|
|
1588
|
+
M("deepseek", "deepseek-chat"),
|
|
1589
|
+
M("openrouter", "x-ai/grok-4"),
|
|
1590
|
+
{},
|
|
1591
|
+
null,
|
|
1592
|
+
undefined,
|
|
1593
|
+
42,
|
|
1594
|
+
]
|
|
1595
|
+
for (const m of samples) {
|
|
1596
|
+
assert.equal(resolveRuntimePolicy(m).policy, detectPolicy(m))
|
|
1597
|
+
}
|
|
1598
|
+
})
|
|
1599
|
+
|
|
1600
|
+
test("v0.4.1: GPT-5.6 alias keeps the overlay but a newer alias stays runtime-neutral", () => {
|
|
1601
|
+
// Legacy alias: gpt-5.6 -> gpt-5.6-sol (was matched by the old regex).
|
|
1602
|
+
const legacy = resolveRuntimePolicy(M("openai", "gpt-5.6"))
|
|
1603
|
+
assert.equal(legacy.policy, "gpt56")
|
|
1604
|
+
assert.equal(legacy.gptCacheMetadata, true)
|
|
1605
|
+
// Newer documented alias that the old classifier did NOT match: no mutation.
|
|
1606
|
+
const newer = resolveRuntimePolicy(M("openai", "gpt-daybreak-blue-latest"))
|
|
1607
|
+
assert.equal(newer.policy, "neutral")
|
|
1608
|
+
assert.equal(newer.gptCacheMetadata, false)
|
|
1609
|
+
assert.equal(resolvePolicy(M("openai", "gpt-daybreak-blue-latest")).family, "gpt-5.6")
|
|
1610
|
+
})
|
|
1611
|
+
|
|
1612
|
+
async function runPolicyMigrationProbe() {
|
|
1613
|
+
const home = mkdtempSync(join(tmpdir(), "ce-policy-migration-"))
|
|
1614
|
+
const pluginURL = new URL("../src/cache-engine.ts", import.meta.url).href
|
|
1615
|
+
const coreURL = new URL("../src/cache-engine-core.mjs", import.meta.url).href
|
|
1616
|
+
const script = `
|
|
1617
|
+
import assert from "node:assert/strict"
|
|
1618
|
+
process.env.CACHE_ENGINE_METRICS_FILE = process.env.HOME + "/policy-migration.jsonl"
|
|
1619
|
+
const { CacheEngine } = await import(${JSON.stringify(pluginURL)})
|
|
1620
|
+
const { detectPolicy } = await import(${JSON.stringify(coreURL)})
|
|
1621
|
+
const { resolvePolicy, resolveRuntimePolicy } = await import(${JSON.stringify(policyCoreURL)})
|
|
1622
|
+
const client = {
|
|
1623
|
+
app: { log: async () => ({}) },
|
|
1624
|
+
session: { get: async () => ({ data: { parentID: null } }) },
|
|
1625
|
+
tool: { list: async () => ({ data: [] }) },
|
|
1626
|
+
}
|
|
1627
|
+
const hooks = await CacheEngine({ client, directory: process.env.HOME })
|
|
1628
|
+
assert.equal(typeof hooks["chat.params"], "function")
|
|
1629
|
+
assert.equal(typeof hooks["chat.headers"], "function")
|
|
1630
|
+
assert.equal(typeof hooks["experimental.chat.system.transform"], "function")
|
|
1631
|
+
const SYS = ["A: keep1", "B: You are powered by the model named x. The exact model ID is acme/x", "C: <env>", "D: Today's date: 2026-08-17", "E: </env>", "F: keep2"].join("\\n")
|
|
1632
|
+
const CASES = [
|
|
1633
|
+
{ name: "gpt-5.6", model: { providerID: "openai", id: "gpt-5.6", api: { id: "gpt-5.6", npm: "@ai-sdk/openai" } }, expect: { policy: "gpt56", env: false, gpt: true, header: false } },
|
|
1634
|
+
{ name: "gpt-5.6-openrouter", model: { providerID: "openrouter", id: "openai/gpt-5.6-sol", api: { id: "openai/gpt-5.6-sol" } }, expect: { policy: "gpt56", env: false, gpt: true, header: false } },
|
|
1635
|
+
{ name: "gpt-6-astra", model: { providerID: "openai", id: "gpt-6-astra", api: { id: "gpt-6-astra", npm: "@ai-sdk/openai" } }, expect: { policy: "neutral", env: false, gpt: false, header: false } },
|
|
1636
|
+
{ name: "gpt-5.5", model: { providerID: "openai", id: "gpt-5.5", api: { id: "gpt-5.5", npm: "@ai-sdk/openai" } }, expect: { policy: "neutral", env: false, gpt: false, header: false } },
|
|
1637
|
+
{ name: "gpt-daybreak-alias", model: { providerID: "openai", id: "gpt-daybreak-blue-latest", api: { id: "gpt-daybreak-blue-latest", npm: "@ai-sdk/openai" } }, expect: { policy: "neutral", env: false, gpt: false, header: false } },
|
|
1638
|
+
{ name: "deepseek-v4-pro", model: { providerID: "deepseek", id: "deepseek-v4-pro", api: { id: "deepseek-v4-pro" } }, expect: { policy: "deepseek", env: false, gpt: false, header: false } },
|
|
1639
|
+
{ name: "deepseek-flash", model: { providerID: "deepseek", id: "deepseek-flash", api: { id: "deepseek-flash" } }, expect: { policy: "deepseek", env: false, gpt: false, header: false } },
|
|
1640
|
+
{ name: "glm-5.3-direct", model: { providerID: "zai", id: "glm-5.3", api: { id: "glm-5.3" } }, expect: { policy: "glm53", env: true, gpt: false, header: false } },
|
|
1641
|
+
{ name: "glm-5.3-openrouter", model: { providerID: "openrouter", id: "z-ai/glm-5.3-flash", api: { id: "z-ai/glm-5.3-flash" } }, expect: { policy: "glm53", env: true, gpt: false, header: true } },
|
|
1642
|
+
{ name: "glm-5.2", model: { providerID: "zai", id: "glm-5.2", api: { id: "glm-5.2" } }, expect: { policy: "neutral", env: false, gpt: false, header: false } },
|
|
1643
|
+
{ name: "mimo-v2.6-flash-direct", model: { providerID: "xiaomi", id: "mimo-v2.6-flash", api: { id: "mimo-v2.6-flash" } }, expect: { policy: "mimo26", env: true, gpt: false, header: false } },
|
|
1644
|
+
{ name: "mimo-v2.6-pro-openrouter", model: { providerID: "openrouter", id: "xiaomi/mimo-v2.6-pro", api: { id: "xiaomi/mimo-v2.6-pro" } }, expect: { policy: "mimo26", env: true, gpt: false, header: true } },
|
|
1645
|
+
{ name: "mimo-v2.6-pro-ultraspeed", model: { providerID: "xiaomi", id: "mimo-v2.6-pro-ultraspeed", api: { id: "mimo-v2.6-pro-ultraspeed" } }, expect: { policy: "neutral", env: false, gpt: false, header: false } },
|
|
1646
|
+
{ name: "mimo-v2.5", model: { providerID: "xiaomi", id: "mimo-v2.5", api: { id: "mimo-v2.5" } }, expect: { policy: "neutral", env: false, gpt: false, header: false } },
|
|
1647
|
+
{ name: "unknown-provider", model: { providerID: "mystery-provider", id: "xiaomi/mimo-v2.6-flash", api: { id: "xiaomi/mimo-v2.6-flash" } }, expect: { policy: "mimo26", env: true, gpt: false, header: false } },
|
|
1648
|
+
{ name: "unknown-openrouter", model: { providerID: "openrouter", id: "acme/mystery-9", api: { id: "acme/mystery-9" } }, expect: { policy: "neutral", env: false, gpt: false, header: false } },
|
|
1649
|
+
]
|
|
1650
|
+
const results = []
|
|
1651
|
+
for (const c of CASES) {
|
|
1652
|
+
const model = c.model
|
|
1653
|
+
const sid = "ses_" + c.name
|
|
1654
|
+
const provider = { source: "config", info: { id: String(model.providerID ?? "") }, options: {} }
|
|
1655
|
+
const existing = { "User-Agent": "preserve", "x-custom": "preserve" }
|
|
1656
|
+
const paramsOut = { options: {} }
|
|
1657
|
+
const headersOut = { headers: { ...existing } }
|
|
1658
|
+
const sysOut = { system: [SYS] }
|
|
1659
|
+
await hooks["chat.params"]({ sessionID: sid, agent: "build", model, provider, message: { id: "msg-" + c.name, sessionID: sid, role: "user", content: "probe" } }, paramsOut)
|
|
1660
|
+
await hooks["experimental.chat.system.transform"]({ sessionID: sid, model, provider }, sysOut)
|
|
1661
|
+
await hooks["chat.headers"]({ sessionID: sid, agent: "build", model, provider, message: { id: "msg-" + c.name, sessionID: sid, role: "user", content: "probe" } }, headersOut)
|
|
1662
|
+
const rt = resolveRuntimePolicy(model)
|
|
1663
|
+
const existingHeadersPreserved = Object.entries(existing).every(([k, v]) => headersOut.headers[k] === v)
|
|
1664
|
+
results.push({
|
|
1665
|
+
name: c.name,
|
|
1666
|
+
runtimePolicy: rt.policy,
|
|
1667
|
+
detectPolicy: detectPolicy(model),
|
|
1668
|
+
richFamily: resolvePolicy(model).family,
|
|
1669
|
+
overlays: resolvePolicy(model).overlays.map((o) => o.id),
|
|
1670
|
+
systemRelocated: sysOut.system[0] !== SYS,
|
|
1671
|
+
gptOptionInjected: paramsOut.options.promptCacheKey !== undefined,
|
|
1672
|
+
gptOptions: paramsOut.options.promptCacheOptions ?? null,
|
|
1673
|
+
affinityHeaderAttached: headersOut.headers["x-session-id"] !== undefined,
|
|
1674
|
+
existingHeadersPreserved,
|
|
1675
|
+
expect: c.expect,
|
|
1676
|
+
})
|
|
1677
|
+
}
|
|
1678
|
+
process.stdout.write(JSON.stringify({ results }))
|
|
1679
|
+
`
|
|
1680
|
+
const stdout = execFileSync(process.execPath, ["--experimental-strip-types", "--input-type=module", "-e", script], {
|
|
1681
|
+
cwd: process.cwd(),
|
|
1682
|
+
env: { ...process.env, HOME: home },
|
|
1683
|
+
encoding: "utf8",
|
|
1684
|
+
})
|
|
1685
|
+
return JSON.parse(stdout.trim())
|
|
1686
|
+
}
|
|
1687
|
+
|
|
1688
|
+
let policyMigrationProbe
|
|
1689
|
+
const policyMigrationResults = async () => (policyMigrationProbe ??= runPolicyMigrationProbe())
|
|
1690
|
+
|
|
1691
|
+
test("v0.4.1: runtime policy is the resolver's and stays equal to detectPolicy (hook path)", async () => {
|
|
1692
|
+
const { results } = await policyMigrationResults()
|
|
1693
|
+
assert.ok(results.length >= 15)
|
|
1694
|
+
for (const r of results) {
|
|
1695
|
+
assert.equal(r.runtimePolicy, r.detectPolicy, `${r.name}: resolver policy must equal legacy detectPolicy`)
|
|
1696
|
+
assert.equal(r.runtimePolicy, r.expect.policy, `${r.name}: unexpected policy`)
|
|
1697
|
+
}
|
|
1698
|
+
})
|
|
1699
|
+
|
|
1700
|
+
test("v0.4.1: every supported model keeps its pre-migration hook behavior", async () => {
|
|
1701
|
+
const { results } = await policyMigrationResults()
|
|
1702
|
+
for (const r of results) {
|
|
1703
|
+
assert.equal(r.systemRelocated, r.expect.env, `${r.name}: <env> relocation`)
|
|
1704
|
+
assert.equal(r.gptOptionInjected, r.expect.gpt, `${r.name}: GPT cache-options injection`)
|
|
1705
|
+
assert.equal(r.affinityHeaderAttached, r.expect.header, `${r.name}: OpenRouter affinity header`)
|
|
1706
|
+
// No provider in the matrix mutates or drops pre-existing headers.
|
|
1707
|
+
assert.equal(r.existingHeadersPreserved, true, `${r.name}: existing headers preserved`)
|
|
1708
|
+
}
|
|
1709
|
+
})
|
|
1710
|
+
|
|
1711
|
+
test("v0.4.1: GPT-5.6 keeps promptCacheOptions implicit/30m through the resolver", async () => {
|
|
1712
|
+
const { results } = await policyMigrationResults()
|
|
1713
|
+
const gpt = results.find((r) => r.name === "gpt-5.6")
|
|
1714
|
+
assert.deepEqual(gpt.gptOptions, { mode: "implicit", ttl: "30m" })
|
|
1715
|
+
})
|
|
1716
|
+
|
|
1717
|
+
test("v0.4.1: future-looking and unknown models gain no mutation", async () => {
|
|
1718
|
+
const { results } = await policyMigrationResults()
|
|
1719
|
+
const noMutation = [
|
|
1720
|
+
"gpt-6-astra",
|
|
1721
|
+
"gpt-daybreak-alias",
|
|
1722
|
+
"gpt-5.5",
|
|
1723
|
+
"glm-5.2",
|
|
1724
|
+
"mimo-v2.6-pro-ultraspeed",
|
|
1725
|
+
"mimo-v2.5",
|
|
1726
|
+
"unknown-openrouter",
|
|
1727
|
+
]
|
|
1728
|
+
for (const name of noMutation) {
|
|
1729
|
+
const r = results.find((x) => x.name === name)
|
|
1730
|
+
assert.ok(r, `${name} present`)
|
|
1731
|
+
assert.equal(r.gptOptionInjected, false, `${name}: no GPT options`)
|
|
1732
|
+
assert.equal(r.systemRelocated, false, `${name}: no <env> relocation`)
|
|
1733
|
+
assert.equal(r.affinityHeaderAttached, false, `${name}: no affinity header`)
|
|
1734
|
+
}
|
|
1735
|
+
})
|
|
1736
|
+
|
|
1737
|
+
test("v0.4.1: non-OpenRouter models never receive the OpenRouter header", async () => {
|
|
1738
|
+
const { results } = await policyMigrationResults()
|
|
1739
|
+
for (const name of ["glm-5.3-direct", "mimo-v2.6-flash-direct", "unknown-provider", "gpt-5.6", "deepseek-v4-pro"]) {
|
|
1740
|
+
const r = results.find((x) => x.name === name)
|
|
1741
|
+
assert.equal(r.affinityHeaderAttached, false, `${name}: no affinity header off OpenRouter`)
|
|
1742
|
+
}
|
|
1743
|
+
})
|