groove-dev 0.27.181 → 0.27.182
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/CLAUDE.md +7 -0
- package/node_modules/@groove-dev/cli/package.json +1 -1
- package/node_modules/@groove-dev/daemon/package.json +1 -1
- package/node_modules/@groove-dev/daemon/scripts/repair-tokens.mjs +78 -0
- package/node_modules/@groove-dev/daemon/src/api.js +1 -1
- package/node_modules/@groove-dev/daemon/src/index.js +1 -0
- package/node_modules/@groove-dev/daemon/src/journalist.js +15 -8
- package/node_modules/@groove-dev/daemon/src/process.js +58 -33
- package/node_modules/@groove-dev/daemon/src/providers/claude-code.js +9 -1
- package/node_modules/@groove-dev/daemon/src/rotator.js +62 -5
- package/node_modules/@groove-dev/daemon/src/tokentracker.js +46 -6
- package/node_modules/@groove-dev/daemon/test/claude-code-provider.test.js +52 -0
- package/node_modules/@groove-dev/daemon/test/journalist.test.js +4 -2
- package/node_modules/@groove-dev/daemon/test/rotator.test.js +131 -0
- package/node_modules/@groove-dev/daemon/test/tokentracker.test.js +4 -0
- package/node_modules/@groove-dev/gui/package.json +1 -1
- package/package.json +1 -1
- package/packages/cli/package.json +1 -1
- package/packages/daemon/package.json +1 -1
- package/packages/daemon/scripts/repair-tokens.mjs +78 -0
- package/packages/daemon/src/api.js +1 -1
- package/packages/daemon/src/index.js +1 -0
- package/packages/daemon/src/journalist.js +15 -8
- package/packages/daemon/src/process.js +58 -33
- package/packages/daemon/src/providers/claude-code.js +9 -1
- package/packages/daemon/src/rotator.js +62 -5
- package/packages/daemon/src/tokentracker.js +46 -6
- package/packages/gui/package.json +1 -1
package/CLAUDE.md
CHANGED
|
@@ -295,3 +295,10 @@ Audit-driven release. Multi-agent orchestration system with 7 coordination layer
|
|
|
295
295
|
- Dashboard: routing donut, cache panel, context health gauges
|
|
296
296
|
- Monitor/QC agent mode (stay active, loop)
|
|
297
297
|
- Distribution: demo video, HN launch, Twitter content
|
|
298
|
+
|
|
299
|
+
<!-- GROOVE:START -->
|
|
300
|
+
## GROOVE Orchestration (auto-injected)
|
|
301
|
+
Active agents: 0
|
|
302
|
+
See AGENTS_REGISTRY.md for full agent state.
|
|
303
|
+
**Memory policy:** GROOVE manages project memory automatically. Do not read or write MEMORY.md or .groove/memory/ files directly.
|
|
304
|
+
<!-- GROOVE:END -->
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// FSL-1.1-Apache-2.0 — see LICENSE
|
|
3
|
+
//
|
|
4
|
+
// One-off ledger repair for ~/.groove/tokens.json (2026-07 efficiency audit).
|
|
5
|
+
//
|
|
6
|
+
// Entry c3789b6f recorded $20,536.57 from a duplicate-ingestion loop (4,631
|
|
7
|
+
// session records in 380s, payloads duplicated up to 10×). The ingestion bug
|
|
8
|
+
// is fixed in claude-code.js, but the poisoned data still dominates every cost
|
|
9
|
+
// dashboard: recorded all-time spend ~$24K vs ~$3.5K real. This script:
|
|
10
|
+
// 1. Sets the poisoned entry's totalCostUsd to its audited real value (~$325)
|
|
11
|
+
// 2. Truncates every entry's sessions array to the last 500 records
|
|
12
|
+
// (matches SESSION_CAP in tokentracker.js; aggregates are untouched)
|
|
13
|
+
// 3. Writes a timestamped .bak of the original file first
|
|
14
|
+
//
|
|
15
|
+
// MUST run with the daemon STOPPED — the daemon holds the ledger in memory and
|
|
16
|
+
// rewrites the file, so a live edit gets clobbered. The script checks and aborts.
|
|
17
|
+
//
|
|
18
|
+
// Usage: node repair-tokens.mjs [path-to-tokens.json] [--entry c3789b6f] [--cost 325]
|
|
19
|
+
|
|
20
|
+
import { readFileSync, writeFileSync, copyFileSync, existsSync } from 'fs';
|
|
21
|
+
import { resolve } from 'path';
|
|
22
|
+
import { homedir } from 'os';
|
|
23
|
+
|
|
24
|
+
const args = process.argv.slice(2);
|
|
25
|
+
const flag = (name, dflt) => {
|
|
26
|
+
const i = args.indexOf(`--${name}`);
|
|
27
|
+
return i !== -1 && args[i + 1] ? args[i + 1] : dflt;
|
|
28
|
+
};
|
|
29
|
+
const tokensPath = args[0] && !args[0].startsWith('--')
|
|
30
|
+
? resolve(args[0])
|
|
31
|
+
: resolve(homedir(), '.groove', 'tokens.json');
|
|
32
|
+
const entryId = flag('entry', 'c3789b6f');
|
|
33
|
+
const correctedCost = Number(flag('cost', '325'));
|
|
34
|
+
const SESSION_CAP = 500;
|
|
35
|
+
|
|
36
|
+
// Refuse to edit under a live daemon
|
|
37
|
+
try {
|
|
38
|
+
const res = await fetch('http://127.0.0.1:31415/api/health', { signal: AbortSignal.timeout(1500) });
|
|
39
|
+
if (res.ok) {
|
|
40
|
+
console.error('ABORT: GROOVE daemon is running on :31415. Stop it first (groove stop) — it rewrites tokens.json and will clobber this repair.');
|
|
41
|
+
process.exit(1);
|
|
42
|
+
}
|
|
43
|
+
} catch { /* no daemon — safe to proceed */ }
|
|
44
|
+
|
|
45
|
+
if (!existsSync(tokensPath)) {
|
|
46
|
+
console.error(`ABORT: ${tokensPath} not found. Pass the path explicitly: node repair-tokens.mjs /path/to/tokens.json`);
|
|
47
|
+
process.exit(1);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const backupPath = `${tokensPath}.${new Date().toISOString().replace(/[:.]/g, '-')}.bak`;
|
|
51
|
+
copyFileSync(tokensPath, backupPath);
|
|
52
|
+
console.log(`Backup written: ${backupPath}`);
|
|
53
|
+
|
|
54
|
+
const data = JSON.parse(readFileSync(tokensPath, 'utf8'));
|
|
55
|
+
const usage = data.usage || data;
|
|
56
|
+
|
|
57
|
+
const target = usage[entryId];
|
|
58
|
+
if (target) {
|
|
59
|
+
const before = target.totalCostUsd;
|
|
60
|
+
target.totalCostUsd = correctedCost;
|
|
61
|
+
target.note = `totalCostUsd corrected ${before} -> ${correctedCost} on ${new Date().toISOString().slice(0, 10)} (duplicate-ingestion inflation, see 2026-07 efficiency audit)`;
|
|
62
|
+
console.log(`Entry ${entryId}: totalCostUsd ${before} -> ${correctedCost}`);
|
|
63
|
+
} else {
|
|
64
|
+
console.warn(`Entry ${entryId} not found — skipping cost correction.`);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
let trimmed = 0;
|
|
68
|
+
for (const entry of Object.values(usage)) {
|
|
69
|
+
if (Array.isArray(entry?.sessions) && entry.sessions.length > SESSION_CAP) {
|
|
70
|
+
trimmed += entry.sessions.length - SESSION_CAP;
|
|
71
|
+
entry.sessions.splice(0, entry.sessions.length - SESSION_CAP);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
console.log(`Trimmed ${trimmed} session records across all entries (cap ${SESSION_CAP}).`);
|
|
75
|
+
|
|
76
|
+
writeFileSync(tokensPath, JSON.stringify(data, null, 2));
|
|
77
|
+
const sizeMb = (Buffer.byteLength(JSON.stringify(data)) / 1024 / 1024).toFixed(1);
|
|
78
|
+
console.log(`Repaired ledger written: ${tokensPath} (${sizeMb}MB)`);
|
|
@@ -1438,7 +1438,7 @@ Keep responses concise. Help them think, don't lecture them about the system the
|
|
|
1438
1438
|
'port', 'journalistInterval', 'rotationThreshold', 'autoRotation',
|
|
1439
1439
|
'qcThreshold', 'maxAgents', 'defaultProvider', 'defaultWorkingDir',
|
|
1440
1440
|
'onboardingDismissed', 'defaultModel', 'defaultChatProvider', 'defaultChatModel',
|
|
1441
|
-
'dataSharingDismissed',
|
|
1441
|
+
'dataSharingDismissed', 'replayCeiling', 'velocityCeiling', 'journalistModelTier',
|
|
1442
1442
|
];
|
|
1443
1443
|
for (const key of Object.keys(req.body)) {
|
|
1444
1444
|
if (!ALLOWED_KEYS.includes(key)) {
|
|
@@ -836,6 +836,7 @@ export class Daemon {
|
|
|
836
836
|
// Persist state before shutdown
|
|
837
837
|
this.state.set('agents', this.registry.getAll());
|
|
838
838
|
await this.state.save();
|
|
839
|
+
try { this.tokens.flush(); } catch { /* best-effort */ }
|
|
839
840
|
|
|
840
841
|
// Stop background services
|
|
841
842
|
await this.gateways.stop();
|
|
@@ -5,6 +5,7 @@ import { readFileSync, writeFileSync, existsSync, mkdirSync, statSync } from 'fs
|
|
|
5
5
|
import { resolve } from 'path';
|
|
6
6
|
import { execFile, spawn as cpSpawn } from 'child_process';
|
|
7
7
|
import { getProvider, getInstalledProviders, resolveProviderCommand } from './providers/index.js';
|
|
8
|
+
import { agentLogPath } from './process.js';
|
|
8
9
|
|
|
9
10
|
const DEFAULT_INTERVAL = 300_000; // 5 minutes (safety-net fallback; event-driven triggers handle the normal case)
|
|
10
11
|
const MAX_LOG_CHARS = 100_000; // ~25k tokens budget for synthesis input (captures 80-90% of recent activity)
|
|
@@ -168,7 +169,7 @@ export class Journalist {
|
|
|
168
169
|
|
|
169
170
|
hasNewActivity(agents) {
|
|
170
171
|
for (const agent of agents) {
|
|
171
|
-
const logPath =
|
|
172
|
+
const logPath = agentLogPath(this.daemon.grooveDir, agent);
|
|
172
173
|
if (!existsSync(logPath)) continue;
|
|
173
174
|
try {
|
|
174
175
|
const size = statSync(logPath).size;
|
|
@@ -182,7 +183,7 @@ export class Journalist {
|
|
|
182
183
|
const result = {};
|
|
183
184
|
|
|
184
185
|
for (const agent of agents) {
|
|
185
|
-
const logPath =
|
|
186
|
+
const logPath = agentLogPath(this.daemon.grooveDir, agent);
|
|
186
187
|
if (!existsSync(logPath)) {
|
|
187
188
|
result[agent.id] = { agent, entries: [], explorationEntries: [] };
|
|
188
189
|
continue;
|
|
@@ -574,9 +575,15 @@ export class Journalist {
|
|
|
574
575
|
const provider = getProvider(providerId);
|
|
575
576
|
if (!provider) continue;
|
|
576
577
|
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
578
|
+
// Digests are light-tier work (Haiku-shaped): default internal overhead to
|
|
579
|
+
// the cheapest tier, escalate via config only on evidence of quality issues.
|
|
580
|
+
// On Claude, medium→light cuts the journalist's marginal cost ~90%.
|
|
581
|
+
const models = provider.constructor.models || [];
|
|
582
|
+
const preferredTier = this.daemon.config?.journalistModelTier || 'light';
|
|
583
|
+
const selectedModel = models.find((m) => m.tier === preferredTier)
|
|
584
|
+
|| models.find((m) => m.tier === 'light')
|
|
585
|
+
|| models.find((m) => m.tier === 'medium')
|
|
586
|
+
|| models[0];
|
|
580
587
|
const modelId = selectedModel?.id || null;
|
|
581
588
|
|
|
582
589
|
// Try CLI headless command first
|
|
@@ -1055,7 +1062,7 @@ export class Journalist {
|
|
|
1055
1062
|
* Budget: keeps recent turns verbatim, summarizes oldest if over maxChars.
|
|
1056
1063
|
*/
|
|
1057
1064
|
extractConversationThread(agent, { maxChars = 60000 } = {}) {
|
|
1058
|
-
const logPath =
|
|
1065
|
+
const logPath = agentLogPath(this.daemon.grooveDir, agent);
|
|
1059
1066
|
if (!existsSync(logPath)) return null;
|
|
1060
1067
|
|
|
1061
1068
|
let content;
|
|
@@ -1297,7 +1304,7 @@ export class Journalist {
|
|
|
1297
1304
|
* Used by the Introducer to tell new agents what their teammates built.
|
|
1298
1305
|
*/
|
|
1299
1306
|
getAgentFiles(agent) {
|
|
1300
|
-
const logPath =
|
|
1307
|
+
const logPath = agentLogPath(this.daemon.grooveDir, agent);
|
|
1301
1308
|
if (!existsSync(logPath)) return [];
|
|
1302
1309
|
|
|
1303
1310
|
try {
|
|
@@ -1335,7 +1342,7 @@ export class Journalist {
|
|
|
1335
1342
|
* Used to capture planner conclusions, build summaries, etc.
|
|
1336
1343
|
*/
|
|
1337
1344
|
getAgentResult(agent) {
|
|
1338
|
-
const logPath =
|
|
1345
|
+
const logPath = agentLogPath(this.daemon.grooveDir, agent);
|
|
1339
1346
|
if (!existsSync(logPath)) return '';
|
|
1340
1347
|
|
|
1341
1348
|
try {
|
|
@@ -324,6 +324,15 @@ function sanitizeFilename(name) {
|
|
|
324
324
|
return String(name).replace(/[^a-zA-Z0-9_-]/g, '_').slice(0, 64);
|
|
325
325
|
}
|
|
326
326
|
|
|
327
|
+
// Agent log files are keyed by sanitized agent NAME, not id — a rotated agent
|
|
328
|
+
// keeps its name and appends to the same file, preserving conversation history
|
|
329
|
+
// across rotations. Every reader must resolve paths through this helper: an
|
|
330
|
+
// id-based path silently misses the file and downstream consumers (handoff
|
|
331
|
+
// briefs, conversation resume, synthesis) fall back to empty context.
|
|
332
|
+
export function agentLogPath(grooveDir, agent) {
|
|
333
|
+
return resolve(grooveDir, 'logs', `${sanitizeFilename(agent.name)}.log`);
|
|
334
|
+
}
|
|
335
|
+
|
|
327
336
|
// Apply Claude Code billing mode to a spawn env, in place.
|
|
328
337
|
// The `claude` CLI bills to usage credits when ANTHROPIC_API_KEY is present and
|
|
329
338
|
// to the OAuth subscription otherwise. 'subscription' (default) strips any
|
|
@@ -1146,7 +1155,7 @@ For normal file edits within your scope, proceed without review.
|
|
|
1146
1155
|
// Set up log capture (shared between CLI and agent loop paths)
|
|
1147
1156
|
const logDir = resolve(this.daemon.grooveDir, 'logs');
|
|
1148
1157
|
mkdirSync(logDir, { recursive: true });
|
|
1149
|
-
const logPath =
|
|
1158
|
+
const logPath = agentLogPath(this.daemon.grooveDir, agent);
|
|
1150
1159
|
const logStream = createWriteStream(logPath, { flags: 'a', mode: 0o600 });
|
|
1151
1160
|
|
|
1152
1161
|
// Inject API key from credential store for agent-loop providers
|
|
@@ -1567,38 +1576,54 @@ For normal file edits within your scope, proceed without review.
|
|
|
1567
1576
|
|
|
1568
1577
|
// Session result data (cost, duration, turns)
|
|
1569
1578
|
if (output.type === 'result') {
|
|
1570
|
-
//
|
|
1571
|
-
//
|
|
1572
|
-
//
|
|
1573
|
-
//
|
|
1574
|
-
//
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
//
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
if (
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1579
|
+
// Did this result represent a turn that actually ran? CLI >= 2.1.212
|
|
1580
|
+
// emits a 0-turn no-op `result` during --resume reconciliation, before
|
|
1581
|
+
// the real turn starts. Both it and an aborted turn report
|
|
1582
|
+
// duration_api_ms === 0 — the model was never called. Providers that
|
|
1583
|
+
// don't report an API duration (codex, gemini, ollama) are unaffected.
|
|
1584
|
+
const reachedModel = output.apiDurationMs === undefined || output.apiDurationMs > 0;
|
|
1585
|
+
|
|
1586
|
+
// Turn aborted before the model was called — re-deliver the message.
|
|
1587
|
+
// Deliberately does NOT return: session id persistence and the GUI
|
|
1588
|
+
// broadcast below still need to run, and the retry is scheduled async.
|
|
1589
|
+
if (!reachedModel && output.isError) {
|
|
1590
|
+
this._retryDroppedTurn(agentId, agent, output);
|
|
1591
|
+
}
|
|
1592
|
+
|
|
1593
|
+
// Everything below is completion bookkeeping and must only run for a
|
|
1594
|
+
// turn that actually executed. Running it for the resume handshake is
|
|
1595
|
+
// what killed the process mid-turn.
|
|
1596
|
+
if (reachedModel) {
|
|
1597
|
+
// The user's message is safely delivered.
|
|
1598
|
+
this._pendingUserMessage.delete(agentId);
|
|
1599
|
+
|
|
1600
|
+
tokens.recordResult(agentId, {
|
|
1601
|
+
costUsd: output.cost, durationMs: output.duration, turns: output.turns,
|
|
1602
|
+
});
|
|
1603
|
+
if (output.cost) updates.costUsd = (agent.costUsd || 0) + output.cost;
|
|
1604
|
+
if (output.duration) updates.durationMs = output.duration;
|
|
1605
|
+
if (output.turns) updates.turns = output.turns;
|
|
1606
|
+
|
|
1607
|
+
// Claude Code sometimes hangs after emitting the result event — the
|
|
1608
|
+
// process stays alive instead of exiting. Record that the result
|
|
1609
|
+
// arrived so exit handlers know this was a successful completion even
|
|
1610
|
+
// if we have to SIGTERM the process. After a 5s grace period, force-
|
|
1611
|
+
// kill any process that hasn't exited on its own.
|
|
1612
|
+
//
|
|
1613
|
+
// MUST stay behind the reachedModel guard. CLI >= 2.1.212 emits a
|
|
1614
|
+
// 0-turn no-op result ~180ms into --resume; arming this on that result
|
|
1615
|
+
// SIGTERMs the process ~5.2s later, mid-turn, before a token is
|
|
1616
|
+
// produced (fable TTFT observed at 34s). The CLI records the signal as
|
|
1617
|
+
// "[Request interrupted by user]" and the user's message is destroyed.
|
|
1618
|
+
this._resultReceived.add(agentId);
|
|
1619
|
+
const handle = this.handles.get(agentId);
|
|
1620
|
+
if (handle?.proc && typeof handle.proc.kill === 'function') {
|
|
1621
|
+
setTimeout(() => {
|
|
1622
|
+
if (this.handles.has(agentId) && this._resultReceived.has(agentId)) {
|
|
1623
|
+
try { handle.proc.kill('SIGTERM'); } catch {}
|
|
1624
|
+
}
|
|
1625
|
+
}, 5_000);
|
|
1626
|
+
}
|
|
1602
1627
|
}
|
|
1603
1628
|
}
|
|
1604
1629
|
|
|
@@ -275,7 +275,15 @@ export class ClaudeCodeProvider extends Provider {
|
|
|
275
275
|
|
|
276
276
|
// Merge events: prefer content-bearing events (activity/result) over usage/session.
|
|
277
277
|
// Accumulate token counts across all events in this chunk.
|
|
278
|
-
|
|
278
|
+
// Prefer an error result over a success one when a single chunk carries
|
|
279
|
+
// both. During --resume the CLI emits a 0-turn no-op success result
|
|
280
|
+
// immediately before the real turn's result; taking the first would hide a
|
|
281
|
+
// subsequent abort and the failure would look like a clean completion.
|
|
282
|
+
const resultEvents = events.filter((e) => e.type === 'result');
|
|
283
|
+
let content = resultEvents.find((e) => e.isError)
|
|
284
|
+
|| resultEvents[0]
|
|
285
|
+
|| events.find((e) => e.type === 'activity')
|
|
286
|
+
|| events[events.length - 1];
|
|
279
287
|
const merged = { ...content };
|
|
280
288
|
|
|
281
289
|
let totalTokens = 0;
|
|
@@ -18,6 +18,9 @@ const SCORE_HISTORY_MAX = 40; // ~10 min at 15s intervals
|
|
|
18
18
|
const COOLDOWN_MS = 5 * 60 * 1000; // 5 minutes between rotations per agent
|
|
19
19
|
const QUALITY_COOLDOWN_MS = 2 * 60 * 1000; // 2 minutes for quality degradation rotations
|
|
20
20
|
const TOKEN_CEILING = 5_000_000; // 5M tokens per agent (non-self-managing only)
|
|
21
|
+
const REPLAY_CEILING = 0.50; // Self-managing providers: rotate idle agents above this context usage
|
|
22
|
+
const VELOCITY_CEILING = 250_000; // New (non-cache) tokens per window before force rotation
|
|
23
|
+
const VELOCITY_WINDOW_MS = 5 * 60_000;
|
|
21
24
|
const ROLE_MULTIPLIERS = {
|
|
22
25
|
planner: 2,
|
|
23
26
|
fullstack: 4,
|
|
@@ -201,13 +204,34 @@ export class Rotator extends EventEmitter {
|
|
|
201
204
|
const singleTask = providerInstance?.constructor?.singleTask ?? false;
|
|
202
205
|
if (singleTask) continue;
|
|
203
206
|
|
|
204
|
-
// Skip agents idle for over 60s — no point scoring them every 15s
|
|
205
|
-
const idleMs = this._idleMs(agent);
|
|
206
|
-
if (idleMs > 60_000 && agent.contextUsage < HARD_CEILING) continue;
|
|
207
|
-
|
|
208
207
|
// Determine if provider manages its own context (e.g. Claude Code compacts internally)
|
|
209
208
|
const selfManagesContext = providerInstance?.constructor?.managesOwnContext ?? false;
|
|
210
209
|
|
|
210
|
+
// Velocity safety — new-token burn rate, all provider types. tokensUsed
|
|
211
|
+
// excludes cache reads, so this measures real generation. Caps worst-case
|
|
212
|
+
// runaway burn; bypasses cooldown (a runaway must be stopped, not waited out).
|
|
213
|
+
const velCeiling = this.daemon.config?.velocityCeiling ?? VELOCITY_CEILING;
|
|
214
|
+
const velocity = this.daemon.tokens?.getVelocity?.(agent.id, VELOCITY_WINDOW_MS) ?? 0;
|
|
215
|
+
if (velCeiling > 0 && velocity >= velCeiling) {
|
|
216
|
+
console.warn(` Rotator: ${agent.name} burned ${velCeiling.toLocaleString()}+ new tokens in 5min — FORCE rotating (velocity)`);
|
|
217
|
+
await this.rotate(agent.id, { reason: 'velocity_ceiling' });
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// Replay ceiling — self-managing providers only. Internal compaction keeps
|
|
222
|
+
// sessions functional but lets context ride at 60-85% indefinitely, and every
|
|
223
|
+
// subsequent message replays that much context on EVERY API call in the agent
|
|
224
|
+
// loop. A handoff-brief rotation costs ~10K tokens once; riding high costs
|
|
225
|
+
// 150-300K per call. Computed here because the idle-skip guard below must
|
|
226
|
+
// not hide the exact agents this targets (idle, high-context).
|
|
227
|
+
const replayCeiling = this.daemon.config?.replayCeiling ?? REPLAY_CEILING;
|
|
228
|
+
const overReplayCeiling = selfManagesContext && replayCeiling > 0
|
|
229
|
+
&& agent.contextUsage >= replayCeiling;
|
|
230
|
+
|
|
231
|
+
// Skip agents idle for over 60s — no point scoring them every 15s
|
|
232
|
+
const idleMs = this._idleMs(agent);
|
|
233
|
+
if (idleMs > 60_000 && agent.contextUsage < HARD_CEILING && !overReplayCeiling) continue;
|
|
234
|
+
|
|
211
235
|
if (!selfManagesContext) {
|
|
212
236
|
// Non-Claude: threshold + ceiling + quality rotation
|
|
213
237
|
// These providers fill up linearly and degrade without external rotation
|
|
@@ -260,6 +284,13 @@ export class Rotator extends EventEmitter {
|
|
|
260
284
|
continue;
|
|
261
285
|
}
|
|
262
286
|
}
|
|
287
|
+
} else if (overReplayCeiling && !this._isOnCooldown(agent.id) && idleMs > 10_000) {
|
|
288
|
+
// Rotate while idle only — never mid-task — and respect the cooldown.
|
|
289
|
+
// Continuity comes from the conversation-thread resume; rotate() blocks
|
|
290
|
+
// the rotation entirely if the thread can't be extracted for a chat agent.
|
|
291
|
+
console.log(` Rotator: ${agent.name} at ${Math.round(agent.contextUsage * 100)}% — rotating (replay ceiling)`);
|
|
292
|
+
await this.rotate(agent.id, { reason: 'replay_ceiling' });
|
|
293
|
+
continue;
|
|
263
294
|
}
|
|
264
295
|
|
|
265
296
|
// --- Change 4: Truncation-triggered rotation ---
|
|
@@ -409,6 +440,7 @@ export class Rotator extends EventEmitter {
|
|
|
409
440
|
// empty or the thread is too short to be useful.
|
|
410
441
|
let brief;
|
|
411
442
|
let usedConversationThread = false;
|
|
443
|
+
const isAutoRotation = options.reason && options.reason !== 'manual';
|
|
412
444
|
if (typeof journalist.buildConversationResumePrompt === 'function') {
|
|
413
445
|
const conversationPrompt = journalist.buildConversationResumePrompt(
|
|
414
446
|
agent,
|
|
@@ -421,6 +453,22 @@ export class Rotator extends EventEmitter {
|
|
|
421
453
|
}
|
|
422
454
|
}
|
|
423
455
|
if (!usedConversationThread) {
|
|
456
|
+
// Chat-continuity protection: an agent with a live user conversation must
|
|
457
|
+
// carry that dialogue into the fresh session. If the thread can't be
|
|
458
|
+
// extracted, a boilerplate brief would wipe the agent's memory of the
|
|
459
|
+
// conversation — skip the auto-rotation instead (context stays intact,
|
|
460
|
+
// worst case is status quo). Manual rotations proceed; the user accepts the risk.
|
|
461
|
+
if (isAutoRotation && this.userMessageTimes.has(agentId)) {
|
|
462
|
+
console.warn(` Rotator: ${agent.name} has chat history but no extractable conversation thread — skipping rotation`);
|
|
463
|
+
this.rotating.delete(agentId);
|
|
464
|
+
this.daemon.broadcast({
|
|
465
|
+
type: 'rotation:blocked',
|
|
466
|
+
agentId,
|
|
467
|
+
agentName: agent.name,
|
|
468
|
+
reason: 'no_conversation_thread',
|
|
469
|
+
});
|
|
470
|
+
return null;
|
|
471
|
+
}
|
|
424
472
|
brief = await journalist.generateHandoffBrief(agent, {
|
|
425
473
|
reason: options.reason,
|
|
426
474
|
qualityScore: options.qualityScore,
|
|
@@ -435,11 +483,14 @@ export class Rotator extends EventEmitter {
|
|
|
435
483
|
// with too little meaningful content, the new agent will arrive blind.
|
|
436
484
|
// Block it rather than spawning an agent that can't continue the work.
|
|
437
485
|
// Manual rotations (user-requested) are never blocked — the user accepts the risk.
|
|
438
|
-
const isAutoRotation = options.reason && options.reason !== 'manual';
|
|
439
486
|
if (isAutoRotation && !usedConversationThread) {
|
|
440
487
|
const briefContent = brief
|
|
441
488
|
.replace(/^#.*$/gm, '') // strip markdown headers
|
|
442
489
|
.replace(/^[-*].*role:.*$/gim, '') // strip metadata lines
|
|
490
|
+
// Strip the static Instructions boilerplate generateHandoffBrief always
|
|
491
|
+
// appends (~400 chars) — padding must not defeat the content threshold
|
|
492
|
+
.replace(/^- (Do NOT|If the task).*$/gm, '')
|
|
493
|
+
.replace(/^Continue and finish the in-progress task.*$/gm, '')
|
|
443
494
|
.replace(/^\s*$/gm, '') // strip blank lines
|
|
444
495
|
.trim();
|
|
445
496
|
if (briefContent.length < 200) {
|
|
@@ -716,6 +767,8 @@ export class Rotator extends EventEmitter {
|
|
|
716
767
|
const hardCeilingRotations = this.rotationHistory.filter((r) => r.reason === 'hard_ceiling').length;
|
|
717
768
|
const tokenCeilingRotations = this.rotationHistory.filter((r) => r.reason === 'token_ceiling').length;
|
|
718
769
|
const estimatedCeilingRotations = this.rotationHistory.filter((r) => r.reason === 'estimated_context_ceiling').length;
|
|
770
|
+
const replayCeilingRotations = this.rotationHistory.filter((r) => r.reason === 'replay_ceiling').length;
|
|
771
|
+
const velocityCeilingRotations = this.rotationHistory.filter((r) => r.reason === 'velocity_ceiling').length;
|
|
719
772
|
return {
|
|
720
773
|
enabled: this.enabled,
|
|
721
774
|
totalRotations,
|
|
@@ -726,6 +779,8 @@ export class Rotator extends EventEmitter {
|
|
|
726
779
|
hardCeilingRotations,
|
|
727
780
|
tokenCeilingRotations,
|
|
728
781
|
estimatedCeilingRotations,
|
|
782
|
+
replayCeilingRotations,
|
|
783
|
+
velocityCeilingRotations,
|
|
729
784
|
rotating: Array.from(this.rotating),
|
|
730
785
|
liveScores: this.liveScores,
|
|
731
786
|
scoreHistory: this.scoreHistory,
|
|
@@ -734,6 +789,8 @@ export class Rotator extends EventEmitter {
|
|
|
734
789
|
qualityThreshold: QUALITY_THRESHOLD,
|
|
735
790
|
cooldownMs: COOLDOWN_MS,
|
|
736
791
|
tokenCeiling: TOKEN_CEILING,
|
|
792
|
+
replayCeiling: this.daemon.config?.replayCeiling ?? REPLAY_CEILING,
|
|
793
|
+
velocityCeiling: this.daemon.config?.velocityCeiling ?? VELOCITY_CEILING,
|
|
737
794
|
roleMultipliers: ROLE_MULTIPLIERS,
|
|
738
795
|
};
|
|
739
796
|
}
|
|
@@ -12,6 +12,14 @@ const COLD_START_PER_FILE = 15;
|
|
|
12
12
|
const COLD_START_PER_DIR = 40;
|
|
13
13
|
// Estimated tokens wasted per file conflict (agent discovers, backs off, retries)
|
|
14
14
|
const CONFLICT_OVERHEAD = 500;
|
|
15
|
+
// Max per-agent session records kept. Aggregates are cumulative and unaffected;
|
|
16
|
+
// windowed queries (getVelocity/getTokensInWindow) only need the recent tail,
|
|
17
|
+
// which 500 records covers by orders of magnitude. Uncapped, tokens.json grew
|
|
18
|
+
// to 13.8MB and was rewritten on every assistant stream event.
|
|
19
|
+
const SESSION_CAP = 500;
|
|
20
|
+
// Debounce window for persisting to disk. record() fires per assistant event —
|
|
21
|
+
// writing the full ledger each time is a hot-path full-file rewrite.
|
|
22
|
+
const SAVE_DEBOUNCE_MS = 5_000;
|
|
15
23
|
|
|
16
24
|
export class TokenTracker {
|
|
17
25
|
constructor(grooveDir) {
|
|
@@ -23,6 +31,7 @@ export class TokenTracker {
|
|
|
23
31
|
this.coldStartsSkipped = 0;
|
|
24
32
|
this.projectFiles = 0; // Set from indexer stats
|
|
25
33
|
this.projectDirs = 0;
|
|
34
|
+
this._saveTimer = null;
|
|
26
35
|
this.load();
|
|
27
36
|
}
|
|
28
37
|
|
|
@@ -47,6 +56,7 @@ export class TokenTracker {
|
|
|
47
56
|
if (!entry.totalDurationMs) entry.totalDurationMs = 0;
|
|
48
57
|
if (!entry.totalTurns) entry.totalTurns = 0;
|
|
49
58
|
if (!entry.modelDistribution) entry.modelDistribution = {};
|
|
59
|
+
this._capSessions(entry);
|
|
50
60
|
}
|
|
51
61
|
} catch {
|
|
52
62
|
this.usage = {};
|
|
@@ -55,6 +65,10 @@ export class TokenTracker {
|
|
|
55
65
|
}
|
|
56
66
|
|
|
57
67
|
save() {
|
|
68
|
+
if (this._saveTimer) {
|
|
69
|
+
clearTimeout(this._saveTimer);
|
|
70
|
+
this._saveTimer = null;
|
|
71
|
+
}
|
|
58
72
|
writeFileSync(this.path, JSON.stringify({
|
|
59
73
|
usage: this.usage,
|
|
60
74
|
rotationSavings: this.rotationSavings,
|
|
@@ -64,6 +78,30 @@ export class TokenTracker {
|
|
|
64
78
|
}, null, 2));
|
|
65
79
|
}
|
|
66
80
|
|
|
81
|
+
// Debounced persistence for hot-path recording. In-memory state is always
|
|
82
|
+
// current; at most SAVE_DEBOUNCE_MS of records are at risk on a crash.
|
|
83
|
+
_scheduleSave() {
|
|
84
|
+
if (this._saveTimer) return;
|
|
85
|
+
this._saveTimer = setTimeout(() => {
|
|
86
|
+
this._saveTimer = null;
|
|
87
|
+
try { this.save(); } catch { /* best-effort */ }
|
|
88
|
+
}, SAVE_DEBOUNCE_MS);
|
|
89
|
+
this._saveTimer.unref?.();
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Write any pending debounced state to disk. Call on daemon shutdown.
|
|
93
|
+
flush() {
|
|
94
|
+
if (this._saveTimer) {
|
|
95
|
+
try { this.save(); } catch { /* best-effort */ }
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
_capSessions(entry) {
|
|
100
|
+
if (Array.isArray(entry.sessions) && entry.sessions.length > SESSION_CAP) {
|
|
101
|
+
entry.sessions.splice(0, entry.sessions.length - SESSION_CAP);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
67
105
|
_initAgent(agentId) {
|
|
68
106
|
if (!this.usage[agentId]) {
|
|
69
107
|
this.usage[agentId] = {
|
|
@@ -89,7 +127,8 @@ export class TokenTracker {
|
|
|
89
127
|
if (typeof detail === 'number') {
|
|
90
128
|
entry.total += detail;
|
|
91
129
|
entry.sessions.push({ tokens: detail, timestamp: new Date().toISOString() });
|
|
92
|
-
this.
|
|
130
|
+
this._capSessions(entry);
|
|
131
|
+
this._scheduleSave();
|
|
93
132
|
return;
|
|
94
133
|
}
|
|
95
134
|
|
|
@@ -121,8 +160,9 @@ export class TokenTracker {
|
|
|
121
160
|
projectRoot: detail.projectRoot || null,
|
|
122
161
|
timestamp: new Date().toISOString(),
|
|
123
162
|
});
|
|
163
|
+
this._capSessions(entry);
|
|
124
164
|
|
|
125
|
-
this.
|
|
165
|
+
this._scheduleSave();
|
|
126
166
|
}
|
|
127
167
|
|
|
128
168
|
// Sum tokens recorded for an agent since a given timestamp.
|
|
@@ -152,25 +192,25 @@ export class TokenTracker {
|
|
|
152
192
|
if (costUsd) entry.totalCostUsd += costUsd;
|
|
153
193
|
if (durationMs) entry.totalDurationMs += durationMs;
|
|
154
194
|
if (turns) entry.totalTurns += turns;
|
|
155
|
-
this.
|
|
195
|
+
this._scheduleSave();
|
|
156
196
|
}
|
|
157
197
|
|
|
158
198
|
// Record that a rotation saved context tokens
|
|
159
199
|
recordRotation(agentId, tokensBefore) {
|
|
160
200
|
this.rotationSavings += Math.round(tokensBefore * 0.3); // ~30% of context was degraded
|
|
161
|
-
this.
|
|
201
|
+
this._scheduleSave();
|
|
162
202
|
}
|
|
163
203
|
|
|
164
204
|
// Record that a conflict was prevented (scope enforcement)
|
|
165
205
|
recordConflictPrevented() {
|
|
166
206
|
this.conflictsPrevented++;
|
|
167
|
-
this.
|
|
207
|
+
this._scheduleSave();
|
|
168
208
|
}
|
|
169
209
|
|
|
170
210
|
// Record that a cold-start was skipped (Journalist provided context)
|
|
171
211
|
recordColdStartSkipped() {
|
|
172
212
|
this.coldStartsSkipped++;
|
|
173
|
-
this.
|
|
213
|
+
this._scheduleSave();
|
|
174
214
|
}
|
|
175
215
|
|
|
176
216
|
// Set project size from indexer for dynamic cold-start estimation
|
|
@@ -69,6 +69,58 @@ describe('ClaudeCodeProvider result parsing', () => {
|
|
|
69
69
|
});
|
|
70
70
|
});
|
|
71
71
|
|
|
72
|
+
describe('ClaudeCodeProvider resume handshake (CLI >= 2.1.212)', () => {
|
|
73
|
+
// The 0-turn no-op result the CLI emits ~180ms into --resume reconciliation.
|
|
74
|
+
const HANDSHAKE = JSON.stringify({
|
|
75
|
+
type: 'result', subtype: 'success', is_error: false,
|
|
76
|
+
duration_ms: 186, duration_api_ms: 0, num_turns: 0, total_cost_usd: 0,
|
|
77
|
+
});
|
|
78
|
+
const ABORT = JSON.stringify({
|
|
79
|
+
type: 'result', subtype: 'error_during_execution', is_error: true,
|
|
80
|
+
duration_ms: 5126, duration_api_ms: 0, num_turns: 2, total_cost_usd: 0,
|
|
81
|
+
terminal_reason: 'aborted_streaming',
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
// This is the gate the daemon uses to decide whether a result means "the
|
|
85
|
+
// turn completed". Treating the handshake as a completion arms a 5s
|
|
86
|
+
// force-kill that SIGTERMs the process mid-turn.
|
|
87
|
+
const reachedModel = (o) => o.apiDurationMs === undefined || o.apiDurationMs > 0;
|
|
88
|
+
|
|
89
|
+
it('does not count the resume handshake as a completed turn', () => {
|
|
90
|
+
const out = provider.parseOutput(HANDSHAKE);
|
|
91
|
+
assert.equal(out.isError, false);
|
|
92
|
+
assert.equal(reachedModel(out), false,
|
|
93
|
+
'handshake must not arm the force-kill or clear the queued message');
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it('counts a real turn as completed', () => {
|
|
97
|
+
const out = provider.parseOutput(JSON.stringify({
|
|
98
|
+
type: 'result', subtype: 'success', is_error: false,
|
|
99
|
+
duration_ms: 17850, duration_api_ms: 17672, num_turns: 3, total_cost_usd: 0.42,
|
|
100
|
+
}));
|
|
101
|
+
assert.equal(reachedModel(out), true);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('providers without an API duration still count as completed', () => {
|
|
105
|
+
// codex / gemini / ollama don't report duration_api_ms — they must be
|
|
106
|
+
// unaffected by the guard.
|
|
107
|
+
const out = provider.parseOutput(JSON.stringify({
|
|
108
|
+
type: 'result', subtype: 'success', duration_ms: 1200, num_turns: 1,
|
|
109
|
+
}));
|
|
110
|
+
assert.equal(out.apiDurationMs, undefined);
|
|
111
|
+
assert.equal(reachedModel(out), true);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
// Regression: taking the first result in a chunk let the handshake mask a
|
|
115
|
+
// subsequent abort, so the failure looked like a clean completion.
|
|
116
|
+
it('surfaces the abort when a chunk carries handshake + abort together', () => {
|
|
117
|
+
const out = provider.parseOutput(`${HANDSHAKE}\n${ABORT}`);
|
|
118
|
+
assert.equal(out.isError, true, 'abort must win over the no-op success result');
|
|
119
|
+
assert.equal(out.terminalReason, 'aborted_streaming');
|
|
120
|
+
assert.equal(reachedModel(out), false);
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
|
|
72
124
|
describe('ClaudeCodeProvider models', () => {
|
|
73
125
|
it('offers Fable 5', () => {
|
|
74
126
|
assert.ok(ClaudeCodeProvider.models.some((m) => m.id === 'claude-fable-5'));
|