sprag-cli 3.129.0 → 3.129.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/package.json +1 -1
- package/src/commands/brief.js +5 -4
- package/src/relax-offer.js +33 -14
- package/src/subagent-context.js +84 -45
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sprag-cli",
|
|
3
|
-
"version": "3.129.
|
|
3
|
+
"version": "3.129.1",
|
|
4
4
|
"description": "The delegation layer for Claude Code and Codex: each task goes to the model and effort that fits it, down to cheaper models for lookups and up to higher effort when a cheaper run would miss, backed by public replay cases, with a live statusline and team rules for Pro and Enterprise.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
package/src/commands/brief.js
CHANGED
|
@@ -109,12 +109,13 @@ export async function run({ hasFlag }) {
|
|
|
109
109
|
const { getSessionModel } = await import('../parser.js');
|
|
110
110
|
pressure = capPressure({ sessionModel: getSessionModel(ctx.transcript_path) });
|
|
111
111
|
// High usage while delegation is not widened: the hint offers
|
|
112
|
-
// `sprag routing relax` once per limit window instead of
|
|
113
|
-
// pressure line.
|
|
114
|
-
//
|
|
112
|
+
// `sprag routing relax` once per limit window and session instead of
|
|
113
|
+
// the plain pressure line. The offer rides on the model relaying it,
|
|
114
|
+
// so every session gets its own chance. Relaxed, or already offered
|
|
115
|
+
// to this session in this window, the plain line stays.
|
|
115
116
|
if (pressure && (pressure.level === 'high' || pressure.level === 'critical') && (match.t2 || match.t1 || match.t1h) && !isRelaxed()) {
|
|
116
117
|
const { claimRelaxOffer } = await import('../relax-offer.js');
|
|
117
|
-
if (claimRelaxOffer(pressure.resetsAt)) pressure = { ...pressure, offer: true };
|
|
118
|
+
if (claimRelaxOffer(pressure.resetsAt, { session: ctx.session_id })) pressure = { ...pressure, offer: true };
|
|
118
119
|
}
|
|
119
120
|
} catch (e) { debug('brief:cap-pressure', e); }
|
|
120
121
|
}
|
package/src/relax-offer.js
CHANGED
|
@@ -2,11 +2,13 @@
|
|
|
2
2
|
* The offer to widen delegation until the usage window resets.
|
|
3
3
|
*
|
|
4
4
|
* When a window runs high the prompt hook tells the model to ask the user once
|
|
5
|
-
* whether to run `sprag routing relax`. "Once" is counted per limit window
|
|
6
|
-
* per session:
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
5
|
+
* whether to run `sprag routing relax`. "Once" is counted per limit window and
|
|
6
|
+
* per session: the offer goes through the model as a context line, and a
|
|
7
|
+
* session that never relays it must not silence every other session for the
|
|
8
|
+
* rest of the window. The window resetting is what makes a new offer possible
|
|
9
|
+
* for a session that was already offered. The last window offered, identified
|
|
10
|
+
* by its resetsAt (epoch seconds), and the sessions offered in it are kept in a
|
|
11
|
+
* small file under the user data directory.
|
|
10
12
|
*/
|
|
11
13
|
|
|
12
14
|
import { readFileSync, existsSync } from 'node:fs';
|
|
@@ -19,6 +21,8 @@ const FILE = 'relax-offer.json';
|
|
|
19
21
|
const SAME_WINDOW_S = 600;
|
|
20
22
|
/** A window with no reset time is bucketed by this span instead. */
|
|
21
23
|
const FALLBACK_BUCKET_S = 5 * 60 * 60;
|
|
24
|
+
/** Sessions remembered per window; the oldest ids drop first. */
|
|
25
|
+
const MAX_SESSIONS = 50;
|
|
22
26
|
|
|
23
27
|
/** The window id used for the claim: its resetsAt, or a 5 hour bucket when the window has none. */
|
|
24
28
|
export function offerWindowId(resetsAt, nowMs = Date.now()) {
|
|
@@ -27,23 +31,38 @@ export function offerWindowId(resetsAt, nowMs = Date.now()) {
|
|
|
27
31
|
}
|
|
28
32
|
|
|
29
33
|
/**
|
|
30
|
-
* True the first time a window is offered, false after.
|
|
31
|
-
*
|
|
32
|
-
*
|
|
34
|
+
* True the first time a (window, session) pair is offered, false after. A call
|
|
35
|
+
* without a session id counts under one shared key, which keeps the old
|
|
36
|
+
* once-per-window behaviour for it. Never throws: when the state file cannot be
|
|
37
|
+
* written the offer is skipped, so a failing disk cannot turn it into a line on
|
|
38
|
+
* every prompt.
|
|
39
|
+
*
|
|
40
|
+
* The file holds { resetsAt, sessions: [ids], offeredAt }. A file in the older
|
|
41
|
+
* shape { resetsAt, offeredAt } reads as the same window with no sessions.
|
|
33
42
|
*
|
|
34
43
|
* @param {number|null} resetsAt epoch seconds of the busiest window's reset
|
|
35
|
-
* @param {{dir?: string, nowMs?: number}} [opts]
|
|
44
|
+
* @param {{dir?: string, nowMs?: number, session?: string}} [opts]
|
|
36
45
|
*/
|
|
37
|
-
export function claimRelaxOffer(resetsAt, { dir, nowMs = Date.now() } = {}) {
|
|
46
|
+
export function claimRelaxOffer(resetsAt, { dir, nowMs = Date.now(), session } = {}) {
|
|
38
47
|
try {
|
|
39
48
|
const file = join(dir || userDataDir(), FILE);
|
|
40
49
|
const id = offerWindowId(resetsAt, nowMs);
|
|
41
|
-
|
|
50
|
+
const key = typeof session === 'string' ? session : '';
|
|
51
|
+
let last = NaN;
|
|
52
|
+
let sessions = [];
|
|
42
53
|
try {
|
|
43
|
-
if (existsSync(file))
|
|
54
|
+
if (existsSync(file)) {
|
|
55
|
+
const saved = JSON.parse(readFileSync(file, 'utf8'));
|
|
56
|
+
last = Number(saved?.resetsAt);
|
|
57
|
+
if (Array.isArray(saved?.sessions)) sessions = saved.sessions.filter((s) => typeof s === 'string');
|
|
58
|
+
}
|
|
44
59
|
} catch { /* unreadable reads as never offered */ }
|
|
45
|
-
|
|
46
|
-
|
|
60
|
+
const sameWindow = Number.isFinite(last) && Math.abs(last - id) <= SAME_WINDOW_S;
|
|
61
|
+
if (sameWindow && sessions.includes(key)) return false;
|
|
62
|
+
// A new window starts a fresh list; the same window keeps its resetsAt so
|
|
63
|
+
// the jitter allowance does not creep forward with every session.
|
|
64
|
+
const kept = sameWindow ? [...sessions, key].slice(-MAX_SESSIONS) : [key];
|
|
65
|
+
writeStateFile(file, JSON.stringify({ resetsAt: sameWindow ? last : id, sessions: kept, offeredAt: nowMs }) + '\n');
|
|
47
66
|
return true;
|
|
48
67
|
} catch {
|
|
49
68
|
return false;
|
package/src/subagent-context.js
CHANGED
|
@@ -13,12 +13,21 @@
|
|
|
13
13
|
* type, so the two sides meet on that pair. The oldest entry for the pair wins:
|
|
14
14
|
* two delegations of one type in a row start in the order they were sent.
|
|
15
15
|
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
16
|
+
* Several Agent calls in one response start several hook processes at once, so
|
|
17
|
+
* the queue needs no lock: each entry is its own file in
|
|
18
|
+
* `<dir>/subagent-context/`, written as a temp file and renamed into place, and
|
|
19
|
+
* a take claims an entry by creating `<entry>.claim` with the exclusive flag
|
|
20
|
+
* ('wx'), which only one process can win. Claiming by renaming the entry away
|
|
21
|
+
* delivered one entry twice on Windows CI (2026-11-15), while an exclusive
|
|
22
|
+
* create is atomic on every platform. The claim file stays until the TTL prune,
|
|
23
|
+
* so a process that read the entry before it was deleted still loses. An entry
|
|
24
|
+
* is therefore delivered at most once, and one process's write cannot drop
|
|
25
|
+
* another's entry. The hook paths must never fail a delegation, so every
|
|
26
|
+
* function swallows its errors.
|
|
19
27
|
*/
|
|
20
28
|
|
|
21
|
-
import { readFileSync, mkdirSync } from 'node:fs';
|
|
29
|
+
import { readFileSync, readdirSync, mkdirSync, openSync, closeSync, unlinkSync } from 'node:fs';
|
|
30
|
+
import { randomBytes } from 'node:crypto';
|
|
22
31
|
import { join } from 'node:path';
|
|
23
32
|
import { userDataDir } from './paths.js';
|
|
24
33
|
import { writeViaTmp } from './state-file.js';
|
|
@@ -28,38 +37,53 @@ export const SUBAGENT_CONTEXT_TTL_MS = 120_000;
|
|
|
28
37
|
/** Newest entries kept. A burst of parallel delegations stays well under this. */
|
|
29
38
|
export const SUBAGENT_CONTEXT_MAX = 20;
|
|
30
39
|
|
|
31
|
-
const
|
|
40
|
+
const DIR_NAME = 'subagent-context';
|
|
41
|
+
/** The single-file queue an older version kept; nothing reads it any more. */
|
|
42
|
+
const OLD_FILE_NAME = 'subagent-context.json';
|
|
43
|
+
const ENTRY_NAME = /^(\d{15})-\d+-[0-9a-f]{8}\.json$/;
|
|
44
|
+
const CLAIM_MARK = '.claim';
|
|
32
45
|
|
|
33
|
-
const
|
|
46
|
+
const queueDir = (dir) => join(dir, DIR_NAME);
|
|
47
|
+
const rand = () => randomBytes(4).toString('hex');
|
|
48
|
+
const rmQuiet = (file) => { try { unlinkSync(file); } catch { /* already gone */ } };
|
|
34
49
|
|
|
35
|
-
/** The
|
|
36
|
-
function
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
50
|
+
/** The `at` encoded in a queue file name (entry, temp or claim file), or null when the name does not parse. */
|
|
51
|
+
function nameAt(name) {
|
|
52
|
+
const m = /^(\d{15})-/.exec(name);
|
|
53
|
+
return m ? Number(m[1]) : null;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const expired = (name, now) => {
|
|
57
|
+
const at = nameAt(name);
|
|
58
|
+
return at === null || now - at > SUBAGENT_CONTEXT_TTL_MS;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const list = (dir) => {
|
|
62
|
+
try { return readdirSync(queueDir(dir)); } catch { return []; }
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Delete expired files (a name that does not parse counts as expired, claim
|
|
67
|
+
* files included), then the oldest entries beyond the cap.
|
|
68
|
+
*/
|
|
69
|
+
function prune(dir, now) {
|
|
70
|
+
const base = queueDir(dir);
|
|
71
|
+
const live = [];
|
|
72
|
+
for (const name of list(dir).sort()) {
|
|
73
|
+
if (expired(name, now)) rmQuiet(join(base, name));
|
|
74
|
+
else if (ENTRY_NAME.test(name)) live.push(name);
|
|
48
75
|
}
|
|
49
|
-
const
|
|
50
|
-
return list
|
|
51
|
-
.filter((e) => e && typeof e === 'object'
|
|
52
|
-
&& typeof e.session === 'string' && typeof e.type === 'string'
|
|
53
|
-
&& typeof e.text === 'string' && Number.isFinite(e.at)
|
|
54
|
-
&& now - e.at <= SUBAGENT_CONTEXT_TTL_MS)
|
|
55
|
-
.sort((a, b) => a.at - b.at)
|
|
56
|
-
.slice(-SUBAGENT_CONTEXT_MAX);
|
|
76
|
+
for (const name of live.slice(0, Math.max(0, live.length - SUBAGENT_CONTEXT_MAX))) rmQuiet(join(base, name));
|
|
57
77
|
}
|
|
58
78
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
79
|
+
/** An entry file's parsed body, or null when it is unreadable or malformed. */
|
|
80
|
+
function readEntry(file) {
|
|
81
|
+
try {
|
|
82
|
+
const e = JSON.parse(readFileSync(file, 'utf8'));
|
|
83
|
+
if (e && typeof e === 'object' && typeof e.session === 'string' && typeof e.type === 'string'
|
|
84
|
+
&& typeof e.text === 'string' && Number.isFinite(e.at)) return e;
|
|
85
|
+
} catch { /* unreadable reads as no entry */ }
|
|
86
|
+
return null;
|
|
63
87
|
}
|
|
64
88
|
|
|
65
89
|
/**
|
|
@@ -70,10 +94,16 @@ export function enqueueSubagentContext(entry, { dir = userDataDir(), now = Date.
|
|
|
70
94
|
try {
|
|
71
95
|
if (!entry || typeof entry.session !== 'string' || !entry.session
|
|
72
96
|
|| typeof entry.type !== 'string' || typeof entry.text !== 'string' || !entry.text) return false;
|
|
73
|
-
const at = Number.isFinite(entry.at) ? entry.at : now;
|
|
74
|
-
const
|
|
75
|
-
|
|
76
|
-
|
|
97
|
+
const at = Number.isFinite(entry.at) && entry.at >= 0 ? Math.floor(entry.at) : now;
|
|
98
|
+
const base = queueDir(dir);
|
|
99
|
+
mkdirSync(base, { recursive: true });
|
|
100
|
+
const name = `${String(at).padStart(15, '0')}-${process.pid}-${rand()}.json`;
|
|
101
|
+
const body = JSON.stringify({ session: entry.session, type: entry.type, at, text: entry.text }) + '\n';
|
|
102
|
+
// The temp name keeps the `at` prefix, so prune treats it like any file of
|
|
103
|
+
// this time and never deletes a write that is still in flight.
|
|
104
|
+
writeViaTmp(join(base, `${name}.tmp`), join(base, name), body);
|
|
105
|
+
prune(dir, now);
|
|
106
|
+
rmQuiet(join(dir, OLD_FILE_NAME));
|
|
77
107
|
return true;
|
|
78
108
|
} catch {
|
|
79
109
|
return false;
|
|
@@ -83,20 +113,29 @@ export function enqueueSubagentContext(entry, { dir = userDataDir(), now = Date.
|
|
|
83
113
|
/**
|
|
84
114
|
* Remove and return the oldest live entry for this session and agent type, or
|
|
85
115
|
* null. Nothing is written when there is no match, so a resumed subagent (which
|
|
86
|
-
* never had an entry) costs one read.
|
|
116
|
+
* never had an entry) costs one directory read.
|
|
87
117
|
*/
|
|
88
118
|
export function takeSubagentContext({ session, type }, { dir = userDataDir(), now = Date.now() } = {}) {
|
|
89
119
|
try {
|
|
90
120
|
if (typeof session !== 'string' || !session || typeof type !== 'string' || !type) return null;
|
|
91
|
-
const
|
|
92
|
-
const
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
121
|
+
const base = queueDir(dir);
|
|
122
|
+
for (const name of list(dir).sort()) {
|
|
123
|
+
if (!ENTRY_NAME.test(name)) continue;
|
|
124
|
+
const file = join(base, name);
|
|
125
|
+
if (expired(name, now)) { rmQuiet(file); continue; }
|
|
126
|
+
const entry = readEntry(file);
|
|
127
|
+
if (!entry || entry.session !== session || entry.type !== type) continue;
|
|
128
|
+
// The exclusive create is the claim: of two processes after the same
|
|
129
|
+
// entry, one gets EEXIST and moves on to the next candidate. The claim
|
|
130
|
+
// file is left for the prune, so a late reader of this entry loses too.
|
|
131
|
+
try { closeSync(openSync(`${file}${CLAIM_MARK}`, 'wx')); } catch { continue; }
|
|
132
|
+
const mine = readEntry(file);
|
|
133
|
+
// Gone between the read and the claim: an old process deleted it. Not ours.
|
|
134
|
+
if (!mine) continue;
|
|
135
|
+
rmQuiet(file);
|
|
136
|
+
return mine;
|
|
137
|
+
}
|
|
138
|
+
return null;
|
|
100
139
|
} catch {
|
|
101
140
|
return null;
|
|
102
141
|
}
|