limitcast 0.18.2 → 0.19.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/README.md +2 -0
- package/bin/limitcast.mjs +28 -5
- package/package.json +1 -1
- package/src/apply.mjs +21 -22
- package/src/capabilities.mjs +125 -0
- package/src/flags.mjs +3 -0
- package/src/grok.mjs +9 -21
- package/src/install.mjs +64 -3
- package/src/model-optimize.mjs +195 -0
- package/src/optimize-cli.mjs +128 -0
- package/src/optimize.mjs +397 -0
- package/src/optimizer-hosts.mjs +54 -0
- package/src/pi-extension.mjs +79 -0
- package/src/plan.mjs +2 -9
- package/src/repomap.mjs +53 -0
- package/src/store.mjs +60 -4
- package/src/subagents.mjs +9 -28
package/src/store.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// @ts-check
|
|
2
|
-
import {
|
|
2
|
+
import { randomUUID } from 'node:crypto';
|
|
3
|
+
import { appendFileSync, closeSync, existsSync, mkdirSync, openSync, readFileSync, renameSync, rmSync, statSync, writeFileSync } from 'node:fs';
|
|
3
4
|
import { homedir } from 'node:os';
|
|
4
5
|
import { join } from 'node:path';
|
|
5
6
|
|
|
@@ -14,9 +15,64 @@ export function stateDir() {
|
|
|
14
15
|
|
|
15
16
|
/** Write through a temp file and rename, so a hook reading at the same moment never sees a half-written file. */
|
|
16
17
|
export function writeAtomic(/** @type {string} */ path, /** @type {string} */ text) {
|
|
17
|
-
const tmp = `${path}.${process.pid}.tmp`;
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
const tmp = `${path}.${process.pid}.${randomUUID()}.tmp`;
|
|
19
|
+
const mode = existsSync(path) ? statSync(path).mode & 0o777 : 0o600;
|
|
20
|
+
const fd = openSync(tmp, 'wx', mode);
|
|
21
|
+
let closed = false;
|
|
22
|
+
try {
|
|
23
|
+
writeFileSync(fd, text);
|
|
24
|
+
closeSync(fd);
|
|
25
|
+
closed = true;
|
|
26
|
+
renameSync(tmp, path);
|
|
27
|
+
} finally {
|
|
28
|
+
if (!closed) closeSync(fd);
|
|
29
|
+
rmSync(tmp, { force: true });
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const LOCK_STALE_MS = 2_000; // the lock is held for one read and one write; older than this means its owner died
|
|
34
|
+
const LOCK_WAIT_MS = 5_000;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Change config.json under a lock: read it, let `change` return the new settings (or the same object to leave the file
|
|
38
|
+
* alone), write it. Without the lock, two commands that each read the file and then each write it lose one change.
|
|
39
|
+
* shortcut: two waiters that break the same stale lock in the same instant can both write; fix with a lockfile library if seen.
|
|
40
|
+
* @param {(cfg: any) => any} change
|
|
41
|
+
*/
|
|
42
|
+
export function updateConfig(change) {
|
|
43
|
+
const dir = stateDir();
|
|
44
|
+
mkdirSync(dir, { recursive: true });
|
|
45
|
+
const file = join(dir, 'config.json');
|
|
46
|
+
const lock = `${file}.lock`;
|
|
47
|
+
const start = Date.now();
|
|
48
|
+
for (;;) {
|
|
49
|
+
try {
|
|
50
|
+
mkdirSync(lock); // atomic: exactly one process creates it
|
|
51
|
+
break;
|
|
52
|
+
} catch (e) {
|
|
53
|
+
if (/** @type {NodeJS.ErrnoException} */ (e).code !== 'EEXIST') throw e;
|
|
54
|
+
try {
|
|
55
|
+
if (Date.now() - statSync(lock).mtimeMs > LOCK_STALE_MS) rmSync(lock, { recursive: true, force: true });
|
|
56
|
+
} catch {
|
|
57
|
+
/* released while we looked */
|
|
58
|
+
}
|
|
59
|
+
if (Date.now() - start > LOCK_WAIT_MS) throw new Error(`could not lock ${file}: another limitcast command is changing settings`);
|
|
60
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 20);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
try {
|
|
64
|
+
let cfg = {};
|
|
65
|
+
try {
|
|
66
|
+
cfg = JSON.parse(readFileSync(file, 'utf8'));
|
|
67
|
+
} catch {
|
|
68
|
+
/* new or corrupt: start fresh */
|
|
69
|
+
}
|
|
70
|
+
if (!cfg || typeof cfg !== 'object') cfg = {};
|
|
71
|
+
const next = change(cfg);
|
|
72
|
+
if (next !== cfg) writeAtomic(file, JSON.stringify(next));
|
|
73
|
+
} finally {
|
|
74
|
+
rmSync(lock, { recursive: true, force: true });
|
|
75
|
+
}
|
|
20
76
|
}
|
|
21
77
|
|
|
22
78
|
/**
|
package/src/subagents.mjs
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { appendFileSync, existsSync, mkdirSync, readFileSync } from 'node:fs';
|
|
3
3
|
import { homedir } from 'node:os';
|
|
4
4
|
import { join } from 'node:path';
|
|
5
|
-
import { stateDir,
|
|
5
|
+
import { stateDir, updateConfig } from './store.mjs';
|
|
6
6
|
|
|
7
7
|
// Subagent receipts and budgets. A SubagentStop hook prices each finished subagent against the 5-hour window
|
|
8
8
|
// (from the user's own calibration) and logs it; the guard holds new subagents once a window's budget is spent.
|
|
@@ -121,14 +121,7 @@ export function getBudget() {
|
|
|
121
121
|
|
|
122
122
|
/** @param {number|null} pct */
|
|
123
123
|
export function setBudget(pct) {
|
|
124
|
-
|
|
125
|
-
try {
|
|
126
|
-
cfg = JSON.parse(readFileSync(configFile(), 'utf8'));
|
|
127
|
-
} catch {
|
|
128
|
-
/* new */
|
|
129
|
-
}
|
|
130
|
-
mkdirSync(stateDir(), { recursive: true });
|
|
131
|
-
writeAtomic(configFile(), JSON.stringify({ ...cfg, subagentBudget: pct }));
|
|
124
|
+
updateConfig((cfg) => ({ ...cfg, subagentBudget: pct }));
|
|
132
125
|
}
|
|
133
126
|
|
|
134
127
|
/** Whether the guard holds new subagents once today's share of a weekly window is used up (`limitcast budget today on`). */
|
|
@@ -142,14 +135,7 @@ export function getTodayHold() {
|
|
|
142
135
|
|
|
143
136
|
/** @param {boolean} on */
|
|
144
137
|
export function setTodayHold(on) {
|
|
145
|
-
|
|
146
|
-
try {
|
|
147
|
-
cfg = JSON.parse(readFileSync(configFile(), 'utf8'));
|
|
148
|
-
} catch {
|
|
149
|
-
/* new */
|
|
150
|
-
}
|
|
151
|
-
mkdirSync(stateDir(), { recursive: true });
|
|
152
|
-
writeAtomic(configFile(), JSON.stringify({ ...cfg, todayHold: on }));
|
|
138
|
+
updateConfig((cfg) => ({ ...cfg, todayHold: on }));
|
|
153
139
|
}
|
|
154
140
|
|
|
155
141
|
/** A model a subagent type can be pinned to: sonnet, opus, haiku, a claude-... id, or a grok-... id. */
|
|
@@ -183,17 +169,12 @@ export function getSubagentModels() {
|
|
|
183
169
|
* @param {string|null} model
|
|
184
170
|
*/
|
|
185
171
|
export function setSubagentModel(type, model) {
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
}
|
|
192
|
-
const models = { ...cfg.subagentModels };
|
|
193
|
-
if (model === null) delete models[type];
|
|
194
|
-
else models[type] = model;
|
|
195
|
-
mkdirSync(stateDir(), { recursive: true });
|
|
196
|
-
writeAtomic(configFile(), JSON.stringify({ ...cfg, subagentModels: models }));
|
|
172
|
+
updateConfig((cfg) => {
|
|
173
|
+
const models = { ...cfg.subagentModels };
|
|
174
|
+
if (model === null) delete models[type];
|
|
175
|
+
else models[type] = model;
|
|
176
|
+
return { ...cfg, subagentModels: models };
|
|
177
|
+
});
|
|
197
178
|
}
|
|
198
179
|
|
|
199
180
|
/**
|