Files
jason.woltje c012b2e0a3 docs(row333): R1 repair packet, receipts and author log appends (#1831)
Commits agents/codex/work/queue-333/r1-repair/ (packet, receipts, review and author files) and the append-only BUILD-LOG.md and docs/SESSIONS.md entries. The packet INDEX.json sha256 is 3eb9b8d10445aa328be3b854385e4792dda2c5712b19d443d485d1b00a18d0f8 and source-tests.patch is a78bc385801b64be270fb9d6e84672c443738a5fce8eb68f7d2a01359a57a770; the product commit for these bytes is 2627321f. Two packet tarballs are deliberately not committed: packet/baseline-input-bytes.tar.gz and packet/fixed-input-bytes.tar.gz. The secret check reported a -----BEGIN marker in each (members packages/bus/src/credentials.mjs and packages/discord/src/tools.mjs, byte-identical to 503b91e56 and with no PEM body). They stay untracked until the lead rules on them.
2026-11-02 13:37:52 -06:00

312 lines
21 KiB
Diff

diff --git a/packages/bus/src/limits.mjs b/packages/bus/src/limits.mjs
index cb47c5df6c97fad2a18cf6a482b6976376ea7fa7..c4bb3a75ecd47b94f83dcc4df28f11e252d5f460 100644
--- a/packages/bus/src/limits.mjs
+++ b/packages/bus/src/limits.mjs
@@ -12,7 +12,7 @@
//
// refresh, mayLaunch and mustStop are async: they re-read the file and reload first. report is
// sync, so the organization verbs stay sync; it uses the modules the last refresh loaded.
-import { createLoader } from '../../plugins/src/loader.mjs';
+import { createLoader, isDailyModelQuota } from '../../plugins/src/loader.mjs';
import { turnRecords } from './usage.mjs';
const KIND = 'limit-check';
@@ -122,8 +122,9 @@ export function createLimits({ store, read, log, makeLoader = createLoader, keyF
log({ event: 'limit-check-failed', business, check: st.missing, export: name, code: 'not-loaded' });
return halt({ check: st.missing, code: 'not-loaded' });
}
+ if (!st.checks.length) return { [key]: go };
const leaves = limitLeaves(st.limits);
- if (Object.keys(st.limits?.tokens?.keys ?? {}).some(k => /^[a-f0-9]{12}$/.test(k))) {
+ if (Object.entries(st.limits?.tokens?.keys ?? {}).some(([k, entry]) => isDailyModelQuota(k, entry))) {
let modelKeyId;
try {
if (name === 'mustStop' && (typeof context?.run !== 'string' || !context.run)) throw new Failure('proxy-key-refused');
diff --git a/packages/bus/src/model-keys.mjs b/packages/bus/src/model-keys.mjs
index 560c8474838ca69a88539ecbc44961c2f157a84b..a998576434fa604912732b0131b9e31ca62f08b4 100644
--- a/packages/bus/src/model-keys.mjs
+++ b/packages/bus/src/model-keys.mjs
@@ -36,6 +36,17 @@ export class ModelKeys {
if (this.#record(instance)?.state === 'revoking') fail('proxy-key-revoking');
if (!this.#ready) fail(this.#failure);
}
+ // P5's destructive queue read needs this instance's current identity, not
+ // an issued agent credential. A sentinel refusal stays closed until explicit
+ // reconcile; telemetry never retries around it or performs reconciliation.
+ verifyIdentity() {
+ return this.#exclusive(async () => {
+ if (!this.#ready) fail(this.#failure);
+ let keys;
+ try { keys = await this.#client.getKeys(); } catch { fail('proxy-down'); }
+ this.#sentinel(keys);
+ });
+ }
async #replace(before, after) {
this.#sentinel(before); this.#sentinel(after);
await this.#client.putKeys(after);
diff --git a/packages/bus/src/proxy-usage.mjs b/packages/bus/src/proxy-usage.mjs
index 71d9a72fa348cf24003e02d36ebe0c1280597f30..754c8c022185014c39183dae0456ac6235ff87a9 100644
--- a/packages/bus/src/proxy-usage.mjs
+++ b/packages/bus/src/proxy-usage.mjs
@@ -28,7 +28,7 @@ export function createProxyUsage({ store, read, client, records, log, now = Date
due = now() + Math.min(...enabled.map(b => b.pollSeconds)) * 1000;
try {
await loader.load({ usage: 'cliproxy-queue' });
- const module = loader.modules('usage')[0], management = client();
+ const module = loader.modules('usage')[0], management = await client();
if (!module || !management) throw Error('unavailable');
const events = await module.read({ client: management });
const owners = new Map();
diff --git a/packages/bus/src/runtime.mjs b/packages/bus/src/runtime.mjs
index c21103a447c2e77fa1b8e487fef1a7c4a0dba6b2..8bb0b0585df10f38d3bf563f3bb6b4dc15ef33ab 100644
--- a/packages/bus/src/runtime.mjs
+++ b/packages/bus/src/runtime.mjs
@@ -384,7 +384,11 @@ export async function startBroker({
const keyForRun = (business, run) => modelRuns.get(business + ':' + run)?.keyId ?? null;
const usage = await createUsage({ store, dataRoot, businesses, log: usageLog, keyForRun,
proxyView: business => proxyUsage?.view(business) ?? null });
- proxyUsage = createProxyUsage({ store, log: usageLog, client: () => modelProxyClient,
+ proxyUsage = createProxyUsage({ store, log: usageLog, client: async () => {
+ if (!modelKeys) return null;
+ await modelKeys.verifyIdentity();
+ return modelProxyClient;
+ },
records: () => credentials.status(),
read: () => Object.fromEntries(Object.keys(businesses).map(id => [id, settings ? settings.current(id) : businesses[id]])) });
// Row 125 (#1619, REQ-LIMIT-3, REQ-LIMIT-4): the limit checks read the organization file at
@@ -862,9 +866,10 @@ export async function startBroker({
}
// One bounded queue read at a time. The service applies the configured
// 1..59 second poll deadline; this tick only checks whether a read is due.
- usageTimer = setInterval(() => { proxyUsage.tick().catch(() => usageLog({ action: 'proxy.usage', code: 'proxy-usage-read-failed' })); }, 1000);
+ const usageTick = () => { proxyUsage.tick().catch(() => usageLog({ action: 'proxy.usage', code: 'proxy-usage-read-failed' })); };
+ usageTimer = setInterval(usageTick, 1000);
usageTimer.unref();
- await proxyUsage.tick();
+ usageTick(); // Optional telemetry never blocks the native ready answer.
return {
broker,
credentials,
diff --git a/packages/bus/tests/limit-checks.test.mjs b/packages/bus/tests/limit-checks.test.mjs
index b9b7660723e4e2fe115cc786b8af2121b23e2403..a0264cc77cb2f9802d42adf4df5dbb01165a5bcc 100644
--- a/packages/bus/tests/limit-checks.test.mjs
+++ b/packages/bus/tests/limit-checks.test.mjs
@@ -327,7 +327,7 @@ test('REQ-LIMIT-3 core reaches limit checks only through the plug-in loader', ()
test('P5 missing, ambiguous or invalid model key lookup fails closed without leaking a lookup error', async t => {
const root=mkdtempSync(join(tmpdir(),'p5-limit-')),store=new Store(root),log=[];t.after(()=>{store.close();rmSync(root,{recursive:true,force:true});});
- const key='012345abcdef',doc={plugins:{'limit-check':['token-limit']},launch:{limits:{tokens:{keys:{[key]:{limit:5,since:SINCE}}}}}};
+ const key='012345abcdef',doc={plugins:{'limit-check':['daily-quota']},launch:{limits:{tokens:{keys:{[key]:{limit:5,since:SINCE,reset:'00:00',timeZone:'UTC'}}}}}};
for(const lookup of [()=>null,()=>['012345abcdef','abcdef012345'],()=> 'msk-private-lookup',()=>{throw Error('PRIVATE-LOOKUP-CANARY');}]) {
const l=createLimits({store,read:()=>doc,log:e=>log.push(e),keyForSubject:lookup});
assert.deepEqual(await l.mayLaunch('demo',SUBJECT),{allow:false,check:null,code:'proxy-key-refused'});
@@ -337,7 +337,7 @@ test('P5 missing, ambiguous or invalid model key lookup fails closed without lea
});
test('P5 trusted lookup uses the retiring run key rather than the agent current key',async t=>{
const root=mkdtempSync(join(tmpdir(),'p5-old-key-')),store=new Store(root);t.after(()=>{store.close();rmSync(root,{recursive:true,force:true});});
- const old='012345abcdef',current='abcdef012345',doc={plugins:{'limit-check':['token-limit']},launch:{limits:{tokens:{keys:{[old]:{limit:0,since:SINCE}}}}}};
+ const old='012345abcdef',current='abcdef012345',doc={plugins:{'limit-check':['daily-quota']},launch:{limits:{tokens:{keys:{[old]:{limit:0,since:SINCE,reset:'00:00',timeZone:'UTC'}}}}}};
const l=createLimits({store,read:()=>doc,log:()=>{},keyForSubject:(_,s)=>s.run==='old-run'?old:current});
assert.equal((await l.mayLaunch('demo',SUBJECT)).allow,true);
assert.equal((await l.mustStop('demo',SUBJECT,{run:'old-run'})).stop,true);
diff --git a/packages/bus/tests/p5-key-context.test.mjs b/packages/bus/tests/p5-key-context.test.mjs
index 8b2ebe0947591589f83d5c89b49bbd8fcf261e10..46b7a87c8e9cbfe4515e35be768f619dffc4a2b6 100644
--- a/packages/bus/tests/p5-key-context.test.mjs
+++ b/packages/bus/tests/p5-key-context.test.mjs
@@ -22,7 +22,7 @@ test('P5 broker preserves original plugin subject and derives internal run conte
});
test('P5 configured key quotas never use a supplied safe-looking ID when trusted metadata lookup is missing',async t=>{
const root=mkdtempSync(join(tmpdir(),'p5-no-bypass-')),store=new Store(root);t.after(()=>{store.close();rmSync(root,{recursive:true,force:true});});
- const doc={plugins:{'limit-check':['token-limit']},launch:{limits:{tokens:{keys:{[key]:{limit:10,since:'2026-01-01T00:00:00.000Z'}}}}}};
+ const doc={plugins:{'limit-check':['daily-quota']},launch:{limits:{tokens:{keys:{[key]:{limit:10,since:'2026-01-01T00:00:00.000Z',reset:'00:00',timeZone:'UTC'}}}}}};
const limits=createLimits({store,read:()=>doc,log:()=>{},keyForSubject:()=>null});
assert.deepEqual(await limits.mustStop('demo',{instance:'coder',modelKeyId:key},{run:'forged-run'}),{stop:true,check:null,code:'proxy-key-refused'});
});
@@ -38,7 +38,7 @@ test('P5 internal context resolves exactly current or retained generation; wrong
test('P5 model-key mustStop without native run context fails closed even when the current key resolves',async t=>{
const root=mkdtempSync(join(tmpdir(),'p5-missing-context-')),store=new Store(root);t.after(()=>{store.close();rmSync(root,{recursive:true,force:true});});
- const doc={plugins:{'limit-check':['token-limit']},launch:{limits:{tokens:{keys:{[key]:{limit:10,since:'2026-01-01T00:00:00.000Z'}}}}}};
+ const doc={plugins:{'limit-check':['daily-quota']},launch:{limits:{tokens:{keys:{[key]:{limit:10,since:'2026-01-01T00:00:00.000Z',reset:'00:00',timeZone:'UTC'}}}}}};
const limits=createLimits({store,read:()=>doc,log:()=>{},keyForSubject:()=>key});
assert.deepEqual(await limits.mustStop('demo',{instance:'coder'}),{stop:true,check:null,code:'proxy-key-refused'});
});
diff --git a/packages/business/src/launch.mjs b/packages/business/src/launch.mjs
index 4ee37c5c6514bdf541b20e7001873fb5d8e878f3..7871fdda595d23c3645b417dbf96783faac9047e 100644
--- a/packages/business/src/launch.mjs
+++ b/packages/business/src/launch.mjs
@@ -1,5 +1,6 @@
// REQ-LIMIT-1: editable concurrency policy, never a compiled-in capacity ceiling.
import { requireObject, requireId, requireString, rejectUnknownKeys } from "./util.mjs";
+import { isDailyModelQuota } from "../../plugins/src/model-quota.mjs";
import { refuse } from "./errors.mjs";
const name = (value, at) => requireString(value, at, { max: 200 });
@@ -67,12 +68,16 @@ export function checkTokenLimits(value, where = "launch.limits.tokens") {
if (Object.hasOwn(value, "models")) {
out.models = tokenMap(value.models, `${where}.models`, name, (models, at) => tokenMap(models, at, name, tokenLimit));
}
- // P5: model key IDs are 12 hex characters (including digit-leading IDs).
- // A model key may use the daily quota shape; existing account limits retain theirs.
- if (Object.hasOwn(value, "keys")) out.keys = tokenMap(value.keys, `${where}.keys`,
- (key, at) => /^[a-f0-9]{12}$/.test(key) ? key : requireId(key, at),
- (entry, at) => Object.hasOwn(entry ?? {}, "reset") && /^[a-f0-9]{12}$/.test(at.split('.').at(-1))
- ? dailyQuota(entry, at) : tokenLimit(entry, at));
+ // P5 adds daily model leaves, including digit-leading IDs; plain keys keep
+ // their existing account-name schema and cumulative account semantics.
+ if (Object.hasOwn(value, "keys")) {
+ requireObject(value.keys, `${where}.keys`);
+ out.keys = Object.fromEntries(Object.entries(value.keys).map(([key, entry]) => {
+ const at = `${where}.keys.${key}`;
+ if (isDailyModelQuota(key, entry)) return [key, dailyQuota(entry, at)];
+ return [requireId(key, `${where}.keys`), tokenLimit(entry, at)];
+ }));
+ }
// One quota per account; the business read checks each key names launch.account.
if (Object.hasOwn(value, "daily")) out.daily = tokenMap(value.daily, `${where}.daily`, requireId, dailyQuota);
return out;
diff --git a/packages/business/src/organization.mjs b/packages/business/src/organization.mjs
index 6d358631d81464dcac98ec0db2388f08e9eceb39..2acb92e417f32f9080396b41d1a2f565272a01d4 100644
--- a/packages/business/src/organization.mjs
+++ b/packages/business/src/organization.mjs
@@ -8,6 +8,7 @@ import { join, dirname } from "node:path";
import { randomUUID } from "node:crypto";
import { businessFilePath, validateBusinessDocument } from "./business.mjs";
import { requireId, sha256, canonicalJson } from "./util.mjs";
+import { isDailyModelQuota } from "../../plugins/src/model-quota.mjs";
import { checkReplyLimits } from "../../library/src/limits.mjs";
export class OrganizationError extends Error {
@@ -84,7 +85,7 @@ function changes(old, next, path = "") {
// P5/Q222: every writer protects per-model-key quotas, including removal/reset.
function modelQuotaFence(old, document, actor) {
const keys = doc => Object.fromEntries(Object.entries(doc?.launch?.limits?.tokens?.keys ?? {})
- .filter(([key]) => /^[a-f0-9]{12}$/.test(key)));
+ .filter(([key, entry]) => isDailyModelQuota(key, entry)));
if (canonicalJson(keys(old)) === canonicalJson(keys(document))) return;
const agents = Object.entries(old.roles ?? {}).flatMap(([instance, entry]) => [instance, `${entry?.definition}.${instance}`]);
if (actor === old.human && actor !== "tester" && !agents.includes(actor)) return;
diff --git a/packages/plugins/limit-check/daily-quota/index.mjs b/packages/plugins/limit-check/daily-quota/index.mjs
index 38b445803d13a05803ec91e11556346e868a2437..096aa2ead37a18cfa17b2e2d43e19fb5604350f6 100644
--- a/packages/plugins/limit-check/daily-quota/index.mjs
+++ b/packages/plugins/limit-check/daily-quota/index.mjs
@@ -4,6 +4,7 @@
// timeZone}. A count is every kind summed over the account's known records since the later of the
// day's reset and the leaf's `since`; an unknown turn adds nothing and is counted on its own.
// It copies token-limit's tally helpers rather than importing a sibling check, so each loads alone.
+import { isDailyModelQuota } from '../../src/model-quota.mjs';
export const interfaceVersion = 2;
const KINDS = ['input', 'output', 'cacheWrite', 'cacheRead'];
@@ -63,7 +64,7 @@ function windowStart(entry, now) {
function leaves(limits) {
const daily = limits?.tokens?.daily ?? {};
const entries = [...Object.entries(daily).map(([account, entry]) => ({ path: ['tokens', 'daily', account], entry, match: x => x.account === account })),
- ...Object.entries(limits?.tokens?.keys ?? {}).filter(([key, entry]) => /^[a-f0-9]{12}$/.test(key) && Object.hasOwn(entry, 'reset'))
+ ...Object.entries(limits?.tokens?.keys ?? {}).filter(([key, entry]) => isDailyModelQuota(key, entry))
.map(([key, entry]) => ({ path: ['tokens', 'keys', key], entry, match: x => x.modelKeyId === key }))];
return entries.map(({ path, entry, match }) => {
if (!Number.isSafeInteger(entry?.limit) || entry.limit < 0 || typeof entry.reset !== 'string' || !RESET.test(entry.reset) ||
diff --git a/packages/plugins/limit-check/token-limit/index.mjs b/packages/plugins/limit-check/token-limit/index.mjs
index 8f26fcb9c7cbcae4c6cf953a6827af53590771be..b20cea356ca7081a6cf571949a0f6aa31dfec89d 100644
--- a/packages/plugins/limit-check/token-limit/index.mjs
+++ b/packages/plugins/limit-check/token-limit/index.mjs
@@ -3,6 +3,7 @@
// launch limits and its per-turn usage records, and nothing here reads a file
// or a store. A count is every kind summed over the known records since the
// limit's `since`; an unknown turn adds nothing and is counted on its own.
+import { isDailyModelQuota } from '../../src/model-quota.mjs';
export const interfaceVersion = 2;
const KINDS = ['input', 'output', 'cacheWrite', 'cacheRead'];
@@ -31,8 +32,8 @@ function leaves(limits) {
// A key is the record's account. Accounts are null until row 126 names them,
// so a key limit matches nothing until then.
for (const [k, entry] of Object.entries(tokens.keys ?? {}))
- if (!Object.hasOwn(entry, 'reset')) out.push({ path: ['tokens', 'keys', k], entry,
- match: x => /^[a-f0-9]{12}$/.test(k) ? x.modelKeyId === k : x.account === k, key: true });
+ if (!isDailyModelQuota(k, entry)) out.push({ path: ['tokens', 'keys', k], entry,
+ match: x => x.account === k, key: true });
return out;
}
@@ -73,7 +74,7 @@ export function mustStop(args) {
// while no record names an account (lead decision 150, Q1).
export function report({ limits, records }) {
if (!Array.isArray(records)) refuse('bad-records');
- const named = records.some(r => r.account != null || r.modelKeyId != null);
+ const named = records.some(r => r.account != null);
return leaves(limits).map(leaf => {
const { path, entry: { limit, since } } = leaf;
if (leaf.key && !named) return { path, limit, since, notEnforced: 'no records name a key' };
diff --git a/packages/plugins/src/loader.mjs b/packages/plugins/src/loader.mjs
index 04731ee91b04329a3956fd751f4d7fb215f845c3..5634a90664ea98022074cd9fff9bbfbd279692e8 100644
--- a/packages/plugins/src/loader.mjs
+++ b/packages/plugins/src/loader.mjs
@@ -162,3 +162,6 @@ export function createLoader({ log, barrier = () => {} }) {
return { load, loaded, modules, snapshot };
}
+
+// Shared pure plug-in setting classification; no check implementation is loaded.
+export { isDailyModelQuota } from "./model-quota.mjs";
diff --git a/packages/plugins/src/model-quota.mjs b/packages/plugins/src/model-quota.mjs
new file mode 100644
index 0000000000000000000000000000000000000000..1350eb56b5947e759ec8d32f0c429813c352f2a9
--- /dev/null
+++ b/packages/plugins/src/model-quota.mjs
@@ -0,0 +1,17 @@
+// The new P5 namespace is the validated DAILY leaf, never a twelve-hex name
+// alone. Plain {limit,since} keys remain legacy account limits.
+const ID = /^[a-f0-9]{12}$/;
+const RESET = /^([01]\d|2[0-3]):[0-5]\d$/;
+const ZONE = /^[A-Za-z][A-Za-z0-9_+-]*(\/[A-Za-z0-9_+-]+)*$/;
+const SINCE = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/;
+export function isDailyModelQuota(key, value) {
+ if (typeof key !== 'string' || !ID.test(key) || !value || typeof value !== 'object' || Array.isArray(value)
+ || Object.keys(value).some(k => !['limit','since','reset','timeZone'].includes(k))
+ || !Number.isSafeInteger(value.limit) || value.limit < 0
+ || typeof value.since !== 'string' || !SINCE.test(value.since) || Number.isNaN(Date.parse(value.since))
+ || new Date(value.since).toISOString() !== value.since
+ || typeof value.reset !== 'string' || !RESET.test(value.reset)
+ || typeof value.timeZone !== 'string' || !ZONE.test(value.timeZone)) return false;
+ try { new Intl.DateTimeFormat('en-US', { timeZone:value.timeZone }); return true; }
+ catch { return false; }
+}
diff --git a/packages/plugins/tests/token-limit.test.mjs b/packages/plugins/tests/token-limit.test.mjs
index d8f8d3db7928f034d7126d479f4255637d0f8646..2c08be007564e5d4b223a7f93bcf4b36535fe739 100644
--- a/packages/plugins/tests/token-limit.test.mjs
+++ b/packages/plugins/tests/token-limit.test.mjs
@@ -150,11 +150,11 @@ test("REQ-LIMIT-3 token-limit ignores tokens.daily, which the daily-quota check
assert.deepEqual(check.report(a), []);
});
-test('P5 a model key ID limit counts stack records by key ID without an account', () => {
- const keyId = '012345abcdef';
+test('P5 plain twelve-hex keys keep legacy account matching rather than cumulative model matching', () => {
+ const keyId = 'abcdef012345';
const args = { business: 'demo', limits: { tokens: { keys: { [keyId]: { limit: 5, since: '2026-10-18T00:00:00.000Z' } } } },
- subject: { instance: 'coder', modelKeyId: keyId, account: null }, records: [{ at: '2026-10-18T01:00:00.000Z',
- run: 'run', turn: 'turn', modelKeyId: keyId, account: null, status: 'known', input: 5, output: 0, cacheWrite: 0, cacheRead: 0 }] };
+ subject: { instance: 'coder', modelKeyId: '012345abcdef', account: keyId }, records: [{ at: '2026-10-18T01:00:00.000Z',
+ run: 'run', turn: 'turn', modelKeyId: '012345abcdef', account: keyId, status: 'known', input: 5, output: 0, cacheWrite: 0, cacheRead: 0 }] };
assert.equal(check.mayLaunch(args).allow, false);
assert.equal(check.report(args)[0].count, 5);
});
diff --git a/packages/plugins/usage/cliproxy-queue/README.md b/packages/plugins/usage/cliproxy-queue/README.md
index 98aa6e8a2757e170dc2a27752e0b474a9fb3fab3..7e1b0c1996b536deb0013d4d58c8ee1d2697ad8e 100644
--- a/packages/plugins/usage/cliproxy-queue/README.md
+++ b/packages/plugins/usage/cliproxy-queue/README.md
@@ -35,8 +35,9 @@ usage cursor, preserved at turn begin and end. The Console exposes `byKey`.
A business can set a daily per-key quota at
`launch.limits.tokens.keys.<12-hex-key-id>` with `{limit, since, reset, timeZone}`;
the human writer stamps `since`. Enable `limit-check: ["daily-quota"]` to apply
-that quota. Cumulative model-key limits retain `{limit,since}` and use
-`token-limit`. Omitting a limit means no quota. Existing named account keys and
+that quota. Plain `{limit,since}` keys leaves remain cumulative legacy account limits, even
+when the name is twelve hex characters. They use `token-limit` and never resolve
+model metadata. Omitting a limit means no quota. Existing named account keys and
`tokens.daily.<account>` retain their existing behavior. Model-key limits and
resets are human-only through every organization writer. Missing, ambiguous or
foreign run-key metadata fails closed with `proxy-key-refused`; a retiring run