chompyy 0.3.0 → 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/README.md +6 -3
- package/THIRD_PARTY_NOTICES.md +1 -1
- package/dist/appstate.js +81 -5
- package/dist/away.js +93 -16
- package/dist/blueprint.js +102 -7
- package/dist/board.js +110 -12
- package/dist/catalog.js +81 -0
- package/dist/cli.js +444 -56
- package/dist/coldstart.js +2 -1
- package/dist/config.js +7 -7
- package/dist/deps.js +124 -0
- package/dist/detect.js +511 -0
- package/dist/dream.js +422 -0
- package/dist/duration.js +3 -3
- package/dist/gate.js +11 -5
- package/dist/live.js +255 -0
- package/dist/livequota.js +27 -4
- package/dist/macapp.js +25 -2
- package/dist/pacing.js +5 -0
- package/dist/permissions.js +106 -0
- package/dist/picture.js +13 -6
- package/dist/picturetype.js +208 -0
- package/dist/preflight.js +168 -0
- package/dist/premise.js +122 -0
- package/dist/projects.js +3 -2
- package/dist/refresh.js +290 -0
- package/dist/report.js +3 -0
- package/dist/review.js +66 -9
- package/dist/runner.js +56 -8
- package/dist/taste.js +34 -13
- package/dist/tokens.js +75 -0
- package/dist/tokenstore.js +61 -0
- package/dist/trace.js +284 -0
- package/dist/triggers.js +16 -5
- package/dist/usage.js +467 -0
- package/dist/vendor/codeburn/prices.js +45 -0
- package/dist/verify.js +24 -5
- package/dist/verifycard.js +146 -0
- package/dist/workspace.js +132 -18
- package/package.json +3 -2
package/dist/live.js
ADDED
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
// The live copy (2026-11-19): the Chompyy you use runs from its own copy of the repo, ~/.chompyy/live, that
|
|
2
|
+
// follows main and that nobody works in. After each Keep that lands on main, Chompyy brings the copy up to
|
|
3
|
+
// main, rebuilds the CLI, and rebuilds + restarts the Mac app when its code changed. So a kept card shows up
|
|
4
|
+
// in the real app within a couple of minutes, and switching branches in a work folder can't take it away.
|
|
5
|
+
//
|
|
6
|
+
// Why: on 2026-11-19 four kept cards never reached the running app. The app ran from a work folder
|
|
7
|
+
// (token-saving-research-board) that had been switched to another branch, and nothing rebuilt after Keep.
|
|
8
|
+
import { execFileSync, spawn } from 'node:child_process';
|
|
9
|
+
import { createHash } from 'node:crypto';
|
|
10
|
+
import { closeSync, existsSync, mkdirSync, openSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
|
11
|
+
import { homedir } from 'node:os';
|
|
12
|
+
import { basename, dirname, join, resolve } from 'node:path';
|
|
13
|
+
import { chompyyHome, homePath, readJson, writeJson } from './home.js';
|
|
14
|
+
export const livePath = () => homePath('live.json');
|
|
15
|
+
export const liveDir = () => join(chompyyHome(), 'live');
|
|
16
|
+
export const loadLive = () => readJson(livePath(), null);
|
|
17
|
+
const git = (cwd, args) => execFileSync('git', ['-C', cwd, ...args], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] }).trim();
|
|
18
|
+
/** The repo's shared .git folder: the same for every worktree of one repo. */
|
|
19
|
+
export function commonDir(dir) {
|
|
20
|
+
try {
|
|
21
|
+
return resolve(dir, git(dir, ['rev-parse', '--git-common-dir']));
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/** Does a Keep in `cardRepo` onto `branch` change what the live copy follows? */
|
|
28
|
+
export function keepAffectsLive(live, cardRepo, branch) {
|
|
29
|
+
if (!live || !branch || branch !== live.branch)
|
|
30
|
+
return false;
|
|
31
|
+
const a = commonDir(cardRepo);
|
|
32
|
+
return !!a && a === commonDir(live.repo);
|
|
33
|
+
}
|
|
34
|
+
export function planUpdate(live, target, depsHash, appExists, macChanged) {
|
|
35
|
+
return {
|
|
36
|
+
target,
|
|
37
|
+
installDeps: live.depsHash !== depsHash,
|
|
38
|
+
buildCli: live.builtCommit !== target,
|
|
39
|
+
buildApp: !appExists || !live.appCommit || (live.appCommit !== target && macChanged(live.appCommit, target)),
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
const defaultDeps = (log) => ({
|
|
43
|
+
run: (cmd, args, cwd) => {
|
|
44
|
+
execFileSync(cmd, args, { cwd, stdio: ['ignore', 'pipe', 'pipe'], maxBuffer: 64 * 1024 * 1024 });
|
|
45
|
+
},
|
|
46
|
+
log,
|
|
47
|
+
appRunning: () => {
|
|
48
|
+
try {
|
|
49
|
+
execFileSync('pgrep', ['-x', 'Chompyy'], { stdio: 'ignore' });
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
sleep: (ms) => Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms),
|
|
57
|
+
node: process.execPath,
|
|
58
|
+
});
|
|
59
|
+
/** Tries before giving up on opening the new app, one second apart. */
|
|
60
|
+
export const OPEN_TRIES = 3;
|
|
61
|
+
export const appBundle = (dir) => join(dir, 'mac', 'build', 'Chompyy.app');
|
|
62
|
+
/**
|
|
63
|
+
* Where the app is installed so you can open it like any Mac app (Spotlight, Launchpad, the Dock), without a
|
|
64
|
+
* terminal: ~/Applications/Chompyy.app. No admin rights needed. CHOMPYY_APPLICATIONS moves it (tests).
|
|
65
|
+
*/
|
|
66
|
+
export const installedApp = () => join(process.env.CHOMPYY_APPLICATIONS || join(homedir(), 'Applications'), 'Chompyy.app');
|
|
67
|
+
const lockHash = (dir) => {
|
|
68
|
+
const p = join(dir, 'cli', 'package-lock.json');
|
|
69
|
+
return existsSync(p) ? createHash('sha256').update(readFileSync(p)).digest('hex').slice(0, 16) : 'none';
|
|
70
|
+
};
|
|
71
|
+
/** Point the app at the live CLI, and mark it so other copies of the CLI don't take it over (recordCliLocation). */
|
|
72
|
+
export function pinCli(dir, node) {
|
|
73
|
+
writeJson(homePath('cli.json'), { node, script: join(dir, 'cli', 'dist', 'cli.js'), live: true });
|
|
74
|
+
}
|
|
75
|
+
/** Make the live copy: a worktree of `repo` at `branch`, detached so the branch stays free for your own folders. */
|
|
76
|
+
export function liveSet(repo, branch = 'main') {
|
|
77
|
+
const root = git(repo, ['rev-parse', '--show-toplevel']);
|
|
78
|
+
const dir = liveDir();
|
|
79
|
+
if (!existsSync(join(dir, '.git')))
|
|
80
|
+
git(root, ['worktree', 'add', '--quiet', '--detach', dir, branch]);
|
|
81
|
+
const state = { repo: root, branch, dir };
|
|
82
|
+
writeJson(livePath(), state);
|
|
83
|
+
return state;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Bring the live copy to the latest `branch` and rebuild what changed. A failed step leaves the last good build
|
|
87
|
+
* running (the CLI is only repointed after a build succeeds) and records the error for `chompyy live`.
|
|
88
|
+
*/
|
|
89
|
+
export function liveUpdate(deps = defaultDeps((l) => console.log(l))) {
|
|
90
|
+
const live = loadLive();
|
|
91
|
+
if (!live)
|
|
92
|
+
throw new Error('No live copy yet: run `chompyy live set <your Chompyy checkout>` once.');
|
|
93
|
+
const { dir } = live;
|
|
94
|
+
let appClosed = false;
|
|
95
|
+
try {
|
|
96
|
+
const target = git(live.repo, ['rev-parse', live.branch]);
|
|
97
|
+
git(dir, ['checkout', '--quiet', '--force', '--detach', target]);
|
|
98
|
+
const plan = planUpdate(live, target, lockHash(dir), existsSync(appBundle(dir)), (from, to) => {
|
|
99
|
+
try {
|
|
100
|
+
git(dir, ['diff', '--quiet', from, to, '--', 'mac']);
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
catch {
|
|
104
|
+
return true;
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
const cli = join(dir, 'cli');
|
|
108
|
+
if (plan.installDeps) {
|
|
109
|
+
deps.log('installing the CLI\'s dependencies (the lockfile changed)…');
|
|
110
|
+
deps.run('npm', ['ci', '--no-audit', '--no-fund'], cli);
|
|
111
|
+
live.depsHash = lockHash(dir);
|
|
112
|
+
}
|
|
113
|
+
if (plan.buildCli || plan.installDeps) {
|
|
114
|
+
deps.log(`building the CLI at ${target.slice(0, 7)}…`);
|
|
115
|
+
deps.run('npm', ['run', 'build'], cli);
|
|
116
|
+
live.builtCommit = target;
|
|
117
|
+
}
|
|
118
|
+
pinCli(dir, deps.node);
|
|
119
|
+
const installed = installedApp();
|
|
120
|
+
// Also when only the installed copy is missing or behind (an older download sat there: 0.1.0 on 2026-11-22).
|
|
121
|
+
const install = plan.buildApp || !existsSync(installed) || live.installedCommit !== live.appCommit;
|
|
122
|
+
if (plan.buildApp) {
|
|
123
|
+
deps.log('building the Mac app (its code changed)…');
|
|
124
|
+
deps.run(join(dir, 'mac', 'Scripts', 'build-app.sh'), [], join(dir, 'mac'));
|
|
125
|
+
live.appCommit = target;
|
|
126
|
+
}
|
|
127
|
+
if (install) {
|
|
128
|
+
const wasRunning = deps.appRunning();
|
|
129
|
+
if (wasRunning) {
|
|
130
|
+
try {
|
|
131
|
+
deps.run('pkill', ['-x', 'Chompyy'], dir);
|
|
132
|
+
}
|
|
133
|
+
catch { }
|
|
134
|
+
// pkill only asks it to quit. Opening while the old app is still closing fails (2026-11-21): wait up to 10 s.
|
|
135
|
+
for (let i = 0; i < 50 && deps.appRunning(); i++)
|
|
136
|
+
deps.sleep(200);
|
|
137
|
+
}
|
|
138
|
+
// Replace the copy in Applications with this build, then open that copy.
|
|
139
|
+
if (basename(installed) !== 'Chompyy.app')
|
|
140
|
+
throw new Error(`refusing to replace ${installed}`);
|
|
141
|
+
mkdirSync(dirname(installed), { recursive: true });
|
|
142
|
+
rmSync(installed, { recursive: true, force: true });
|
|
143
|
+
deps.run('ditto', [appBundle(dir), installed], dir);
|
|
144
|
+
live.installedCommit = live.appCommit;
|
|
145
|
+
let failed;
|
|
146
|
+
for (let i = 0; i < OPEN_TRIES; i++) {
|
|
147
|
+
try {
|
|
148
|
+
// In the background and marked as an update: the app comes back quietly to the menu bar.
|
|
149
|
+
deps.run('open', ['-g', installed, '--args', '--updated'], dir);
|
|
150
|
+
failed = undefined;
|
|
151
|
+
break;
|
|
152
|
+
}
|
|
153
|
+
catch (err) {
|
|
154
|
+
failed = err;
|
|
155
|
+
if (i < OPEN_TRIES - 1)
|
|
156
|
+
deps.sleep(1000);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
if (failed) {
|
|
160
|
+
appClosed = wasRunning;
|
|
161
|
+
throw new Error(`the new app is built but didn't open: open ${installed} by hand (${failed.message.split('\n')[0]})`);
|
|
162
|
+
}
|
|
163
|
+
deps.log(`${wasRunning ? 'restarted the app with the new build' : 'opened the new app'} (installed in ${installed.replace(homedir(), '~')}).`);
|
|
164
|
+
}
|
|
165
|
+
delete live.error;
|
|
166
|
+
if (!plan.buildCli && !install && !plan.installDeps)
|
|
167
|
+
deps.log(`already up to date at ${target.slice(0, 7)}.`);
|
|
168
|
+
}
|
|
169
|
+
catch (err) {
|
|
170
|
+
const e = err;
|
|
171
|
+
live.error = `${e.message ?? err}${e.stderr ? `\n${String(e.stderr).slice(-2000)}` : ''}`.trim();
|
|
172
|
+
// Say what is really running: once the old app was closed, nothing is.
|
|
173
|
+
deps.log(appClosed ? `update failed and the app is closed: ${live.error.split('\n')[0]}` : `update failed, the last good build keeps running: ${live.error.split('\n')[0]}`);
|
|
174
|
+
}
|
|
175
|
+
live.at = new Date().toISOString();
|
|
176
|
+
writeJson(livePath(), live);
|
|
177
|
+
return live;
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Run updates one at a time. Keeping two cards in a row asks twice: the second ask leaves a note, and the
|
|
181
|
+
* update that is already running goes round again so it ends at the newest main.
|
|
182
|
+
*/
|
|
183
|
+
export function liveUpdateLocked(deps) {
|
|
184
|
+
const lock = homePath('live.lock');
|
|
185
|
+
const again = homePath('live.again');
|
|
186
|
+
let fd;
|
|
187
|
+
try {
|
|
188
|
+
fd = openSync(lock, 'wx');
|
|
189
|
+
}
|
|
190
|
+
catch {
|
|
191
|
+
const pid = Number(readFileSync(lock, 'utf8')) || 0;
|
|
192
|
+
let alive = false;
|
|
193
|
+
try {
|
|
194
|
+
if (pid)
|
|
195
|
+
process.kill(pid, 0);
|
|
196
|
+
alive = pid > 0;
|
|
197
|
+
}
|
|
198
|
+
catch { }
|
|
199
|
+
if (alive) {
|
|
200
|
+
writeFileSync(again, '');
|
|
201
|
+
return null;
|
|
202
|
+
}
|
|
203
|
+
rmSync(lock, { force: true }); // left by an update that died
|
|
204
|
+
fd = openSync(lock, 'wx');
|
|
205
|
+
}
|
|
206
|
+
try {
|
|
207
|
+
writeFileSync(fd, String(process.pid));
|
|
208
|
+
let state;
|
|
209
|
+
do {
|
|
210
|
+
rmSync(again, { force: true });
|
|
211
|
+
state = liveUpdate(deps);
|
|
212
|
+
} while (existsSync(again));
|
|
213
|
+
return state;
|
|
214
|
+
}
|
|
215
|
+
finally {
|
|
216
|
+
closeSync(fd);
|
|
217
|
+
rmSync(lock, { force: true });
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
/** Start an update in the background, so Keep returns at once and the update outlives an app restart. */
|
|
221
|
+
export function startLiveUpdate(node = process.execPath, script = process.argv[1]) {
|
|
222
|
+
const out = openSync(homePath('live.log'), 'a');
|
|
223
|
+
const child = spawn(node, [script, 'live', 'update'], { detached: true, stdio: ['ignore', out, out] });
|
|
224
|
+
child.unref();
|
|
225
|
+
closeSync(out);
|
|
226
|
+
}
|
|
227
|
+
/** Stop following: the app goes back to whichever CLI runs next (the live folder stays until you delete it). */
|
|
228
|
+
export function liveOff() {
|
|
229
|
+
rmSync(livePath(), { force: true });
|
|
230
|
+
const cli = homePath('cli.json');
|
|
231
|
+
const have = readJson(cli, {});
|
|
232
|
+
if (have.live) {
|
|
233
|
+
delete have.live;
|
|
234
|
+
writeJson(cli, have);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
/** For `chompyy live`: where the copy is, and whether it has everything that's on main. */
|
|
238
|
+
export function formatLive(live) {
|
|
239
|
+
if (!live)
|
|
240
|
+
return 'No live copy. Set it up once with `chompyy live set <your Chompyy checkout>`; after that every Keep on main reaches the app by itself.';
|
|
241
|
+
let behind = '';
|
|
242
|
+
try {
|
|
243
|
+
const n = Number(git(live.repo, ['rev-list', '--count', `${live.builtCommit ?? live.branch}..${live.branch}`]));
|
|
244
|
+
behind = n ? `${n} commit(s) on ${live.branch} not built yet` : `up to date with ${live.branch}`;
|
|
245
|
+
}
|
|
246
|
+
catch { }
|
|
247
|
+
return [
|
|
248
|
+
`live copy ${live.dir}`,
|
|
249
|
+
`follows ${live.branch} of ${live.repo}`,
|
|
250
|
+
`CLI built ${live.builtCommit?.slice(0, 7) ?? 'never'}${behind ? ` (${behind})` : ''}`,
|
|
251
|
+
`app built ${live.appCommit?.slice(0, 7) ?? 'never'}`,
|
|
252
|
+
...(live.at ? [`updated ${live.at}`] : []),
|
|
253
|
+
...(live.error ? [`last error ${live.error.split('\n')[0]} (details: ~/.chompyy/live.log)`] : []),
|
|
254
|
+
].join('\n');
|
|
255
|
+
}
|
package/dist/livequota.js
CHANGED
|
@@ -3,8 +3,29 @@
|
|
|
3
3
|
// It sends no message, so it never starts a 5-hour window. Chosen over the status-line
|
|
4
4
|
// snapshot on 2026-11-15 because it is fresh even when no Claude Code session is open.
|
|
5
5
|
import { fetchClaudeQuota } from './vendor/codeburn/claude.js';
|
|
6
|
+
import { setConfig } from './config.js';
|
|
6
7
|
import { homePath, readJson, writeJson } from './home.js';
|
|
7
8
|
import { loadSnapshot, mergeSnapshot, saveSnapshot } from './quota.js';
|
|
9
|
+
/** What the app and `chompyy quota` say while the Keychain read waits for your OK. */
|
|
10
|
+
export const KEYCHAIN_ASK = "Live quota needs your OK to read Claude Code's saved login from your Keychain (only to ask Anthropic how much is left; it sends no message). Run `chompyy quota --allow-keychain`, or press Allow.";
|
|
11
|
+
/** What they say once you turned the Keychain read off. */
|
|
12
|
+
export const KEYCHAIN_OFF = "Keychain reading is off, as you chose. Quota comes from Claude Code's status line; `chompyy quota --allow-keychain` turns live quota back on.";
|
|
13
|
+
/**
|
|
14
|
+
* Reading the Keychain is opt-in (#24). Allowed once you say yes. A Mac where an earlier check already read the
|
|
15
|
+
* login (you chose Always Allow before Chompyy asked first) counts as a yes, and that is saved.
|
|
16
|
+
*/
|
|
17
|
+
export function keychainAllowed(config) {
|
|
18
|
+
if (config.allowKeychain !== null && config.allowKeychain !== undefined)
|
|
19
|
+
return config.allowKeychain;
|
|
20
|
+
if (loadLiveState().connection !== 'connected')
|
|
21
|
+
return false;
|
|
22
|
+
setConfig('allowKeychain', true);
|
|
23
|
+
return true;
|
|
24
|
+
}
|
|
25
|
+
/** Whether to show the Allow button: a Mac, live quota on, never asked, and the check found nothing without it. */
|
|
26
|
+
export function keychainAsk(config, platform = process.platform) {
|
|
27
|
+
return platform === 'darwin' && config.quotaSource === 'api' && (config.allowKeychain ?? null) === null && loadLiveState().connection === 'keychainUnchecked';
|
|
28
|
+
}
|
|
8
29
|
/** Don't ask the endpoint more often than this. */
|
|
9
30
|
export const MIN_INTERVAL_MS = 5 * 60_000;
|
|
10
31
|
const statePath = () => homePath('livequota.json');
|
|
@@ -40,7 +61,7 @@ export function snapshotFromProvider(provider, now) {
|
|
|
40
61
|
}
|
|
41
62
|
/**
|
|
42
63
|
* Refresh the snapshot from the endpoint. `force` skips the 5-minute spacing (not a 429 backoff).
|
|
43
|
-
* `allowKeychain` lets macOS
|
|
64
|
+
* `allowKeychain` lets it read the Keychain (macOS shows its one-time "allow access?" prompt); off unless you allowed it.
|
|
44
65
|
*/
|
|
45
66
|
export async function refreshLiveQuota(opts = {}) {
|
|
46
67
|
const now = opts.now ?? new Date();
|
|
@@ -53,7 +74,7 @@ export async function refreshLiveQuota(opts = {}) {
|
|
|
53
74
|
return { ok: true, snapshot: current, message: 'Fresh enough.' };
|
|
54
75
|
}
|
|
55
76
|
const fetchQuota = opts.fetchQuota ?? ((o) => fetchClaudeQuota(o));
|
|
56
|
-
const result = await fetchQuota({ allowKeychain: opts.allowKeychain ??
|
|
77
|
+
const result = await fetchQuota({ allowKeychain: opts.allowKeychain ?? false });
|
|
57
78
|
const next = {
|
|
58
79
|
lastTryAt: now.toISOString(),
|
|
59
80
|
connection: result.quota.connection,
|
|
@@ -63,12 +84,13 @@ export async function refreshLiveQuota(opts = {}) {
|
|
|
63
84
|
next.retryAfterAt = new Date(now.getTime() + result.retryAfterSeconds * 1000).toISOString();
|
|
64
85
|
const snap = snapshotFromProvider(result.quota, now);
|
|
65
86
|
if (!snap) {
|
|
66
|
-
|
|
87
|
+
const ask = opts.keychainOff ? KEYCHAIN_OFF : KEYCHAIN_ASK;
|
|
88
|
+
next.note = result.quota.connection === 'keychainUnchecked' ? ask : result.quota.footerLines.join(' ') || result.quota.connection;
|
|
67
89
|
writeJson(statePath(), next);
|
|
68
90
|
// A 429: other tools signed in with the same Claude login (or a recent check) used up the allowance.
|
|
69
91
|
if (next.retryAfterAt)
|
|
70
92
|
return { ok: false, snapshot: current, message: `Anthropic limits how often quota can be checked; next check after ${hhmm(next.retryAfterAt)}.` };
|
|
71
|
-
return { ok: false, snapshot: current, message: explain(result.quota) };
|
|
93
|
+
return { ok: false, snapshot: current, message: result.quota.connection === 'keychainUnchecked' ? ask : explain(result.quota) };
|
|
72
94
|
}
|
|
73
95
|
const merged = mergeSnapshot(current, snap);
|
|
74
96
|
saveSnapshot(merged);
|
|
@@ -82,6 +104,7 @@ function explain(p) {
|
|
|
82
104
|
case 'accessDenied':
|
|
83
105
|
return `macOS didn't allow reading the Claude Code login.${extra}`;
|
|
84
106
|
case 'keychainUnchecked':
|
|
107
|
+
return KEYCHAIN_ASK;
|
|
85
108
|
case 'disconnected':
|
|
86
109
|
return 'No Claude Code login found. Sign in to Claude Code once, then try again.';
|
|
87
110
|
case 'terminalFailure':
|
package/dist/macapp.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
// `chompyy app`: download the Mac app from this version's GitHub Release into ~/Applications and open it.
|
|
2
|
-
// The app is a thin window over this CLI, so the two versions are kept in step.
|
|
2
|
+
// The app is a thin window over this CLI, so the two versions are kept in step. The download is checked against
|
|
3
|
+
// the SHA-256 published next to it, and the app's code signature is checked, before anything is opened (#24).
|
|
3
4
|
import { execFileSync } from 'node:child_process';
|
|
5
|
+
import { createHash } from 'node:crypto';
|
|
4
6
|
import { existsSync, mkdirSync, mkdtempSync, readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs';
|
|
5
7
|
import { homedir, tmpdir } from 'node:os';
|
|
6
8
|
import { dirname, join } from 'node:path';
|
|
@@ -14,6 +16,13 @@ export function cliVersion() {
|
|
|
14
16
|
export function appZipUrl(version) {
|
|
15
17
|
return `https://github.com/${REPO}/releases/download/v${version}/Chompyy-${version}.zip`;
|
|
16
18
|
}
|
|
19
|
+
/** The release's checksum file: `<sha256> Chompyy-<version>.zip`, written by mac/Scripts/build-app.sh --release. */
|
|
20
|
+
export const checksumUrl = (version) => `${appZipUrl(version)}.sha256`;
|
|
21
|
+
/** The hash in a `shasum -a 256` line, or null when the text isn't one. */
|
|
22
|
+
export function parseChecksum(text) {
|
|
23
|
+
const m = text.trim().match(/^([0-9a-f]{64})\b/i);
|
|
24
|
+
return m ? m[1].toLowerCase() : null;
|
|
25
|
+
}
|
|
17
26
|
const defaultDeps = () => ({
|
|
18
27
|
fetch: (url) => fetch(url),
|
|
19
28
|
exec: (cmd, args) => {
|
|
@@ -31,14 +40,28 @@ export async function installApp(opts = {}, deps = defaultDeps()) {
|
|
|
31
40
|
const res = await deps.fetch(url);
|
|
32
41
|
if (!res.ok)
|
|
33
42
|
throw new Error(`Couldn't download the app (HTTP ${res.status}) from ${url}`);
|
|
43
|
+
const sumRes = await deps.fetch(checksumUrl(version));
|
|
44
|
+
const want = sumRes.ok ? parseChecksum(Buffer.from(await sumRes.arrayBuffer()).toString('utf8')) : null;
|
|
45
|
+
if (!want)
|
|
46
|
+
throw new Error(`The release has no checksum (${checksumUrl(version)}), so Chompyy won't open its download. Nothing was installed.`);
|
|
47
|
+
const bytes = Buffer.from(await res.arrayBuffer());
|
|
48
|
+
const got = createHash('sha256').update(bytes).digest('hex');
|
|
49
|
+
if (got !== want)
|
|
50
|
+
throw new Error(`The download doesn't match the release's checksum (got ${got.slice(0, 12)}…, expected ${want.slice(0, 12)}…). Nothing was installed.`);
|
|
34
51
|
const tmp = mkdtempSync(join(tmpdir(), 'chompyy-app-'));
|
|
35
52
|
try {
|
|
36
53
|
const zip = join(tmp, 'Chompyy.zip');
|
|
37
|
-
writeFileSync(zip,
|
|
54
|
+
writeFileSync(zip, bytes);
|
|
38
55
|
deps.exec('ditto', ['-x', '-k', zip, tmp]);
|
|
39
56
|
const unpacked = join(tmp, 'Chompyy.app');
|
|
40
57
|
if (!existsSync(unpacked))
|
|
41
58
|
throw new Error('The download had no Chompyy.app in it.');
|
|
59
|
+
try {
|
|
60
|
+
deps.exec('codesign', ['--verify', '--deep', '--strict', unpacked]);
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
throw new Error("The app's code signature doesn't check out (codesign --verify), so it wasn't installed.");
|
|
64
|
+
}
|
|
42
65
|
mkdirSync(deps.appsDir, { recursive: true });
|
|
43
66
|
const target = join(deps.appsDir, 'Chompyy.app');
|
|
44
67
|
try {
|
package/dist/pacing.js
CHANGED
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
// The burn rate comes from a small history of quota readings (quota-history.json), saved whenever the snapshot
|
|
10
10
|
// changes, so no extra calls are made.
|
|
11
11
|
import { homePath, readJson, writeJson } from './home.js';
|
|
12
|
+
import { archiveWindowEnds } from './premise.js';
|
|
12
13
|
import { windowView } from './quota.js';
|
|
13
14
|
export const FIVE_HOURS = 5 * 3600_000;
|
|
14
15
|
export const WEEK = 7 * 24 * 3600_000;
|
|
@@ -34,6 +35,10 @@ export function recordSample(snap, nowMs = Date.parse(snap.capturedAt)) {
|
|
|
34
35
|
return false; // out of order
|
|
35
36
|
samples.push({ at: nowMs, ...(snap.fiveHour ? { five: snap.fiveHour } : {}), ...(snap.sevenDay ? { week: snap.sevenDay } : {}) });
|
|
36
37
|
writeJson(historyPath(), { samples: samples.filter((s) => s.at > nowMs - KEEP_MS) });
|
|
38
|
+
try {
|
|
39
|
+
archiveWindowEnds(samples, nowMs); // windows that ended, kept beyond the 8 days (premise.ts)
|
|
40
|
+
}
|
|
41
|
+
catch { }
|
|
37
42
|
return true;
|
|
38
43
|
}
|
|
39
44
|
// ---------- pace delta ----------
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
// The permission plan (research/away-readiness.md, step 3): an away run can't ask for approval at 3 AM, so the
|
|
2
|
+
// approval stage asks instead. A card's plan lists what its run may need beyond the box's defaults, from three
|
|
3
|
+
// sources: what earlier runs in this repo were blocked on, what preflight found, and what the blueprint itself
|
|
4
|
+
// says it needs. You tick what to allow when you approve; nothing is allowed by default, and allowed commands
|
|
5
|
+
// still run inside the box.
|
|
6
|
+
export const permissionId = (kind, value) => `${kind}:${value}`;
|
|
7
|
+
export function parsePermission(id) {
|
|
8
|
+
const m = id.match(/^(network|command):(.+)$/);
|
|
9
|
+
if (!m)
|
|
10
|
+
return null;
|
|
11
|
+
const value = m[2].trim();
|
|
12
|
+
if (m[1] === 'network' && !/^[a-z0-9.-]+\.[a-z]{2,}$/i.test(value))
|
|
13
|
+
return null; // a plain domain, nothing else
|
|
14
|
+
if (m[1] === 'command' && (!value || /[;&|`$<>]/.test(value)))
|
|
15
|
+
return null; // one command, no shell tricks
|
|
16
|
+
return { kind: m[1], value };
|
|
17
|
+
}
|
|
18
|
+
/** A blocked command as a pattern to allow: its program name plus anything after ("swiftc *"). */
|
|
19
|
+
export function commandPattern(command) {
|
|
20
|
+
const first = command.trim().split(/\s+/)[0]?.replace(/^.*\//, '');
|
|
21
|
+
if (!first || /[;&|`$<>()]/.test(first))
|
|
22
|
+
return null;
|
|
23
|
+
return `${first} *`;
|
|
24
|
+
}
|
|
25
|
+
/** Hosts named in network errors ("ENOTFOUND registry.npmjs.org", "request to https://x/…"). */
|
|
26
|
+
export function hostsInErrors(text) {
|
|
27
|
+
if (!/ENOTFOUND|EAI_AGAIN|getaddrinfo|Could not resolve host|ECONNREFUSED|network request|unable to access/i.test(text))
|
|
28
|
+
return [];
|
|
29
|
+
const hosts = new Set();
|
|
30
|
+
for (const m of text.matchAll(/(?:ENOTFOUND|EAI_AGAIN|getaddrinfo \w+|Could not resolve host:?)\s+([a-z0-9.-]+\.[a-z]{2,})/gi))
|
|
31
|
+
hosts.add(m[1].toLowerCase());
|
|
32
|
+
for (const m of text.matchAll(/https?:\/\/([a-z0-9.-]+\.[a-z]{2,})/gi))
|
|
33
|
+
hosts.add(m[1].toLowerCase());
|
|
34
|
+
return [...hosts].filter((h) => h !== 'localhost').slice(0, 10);
|
|
35
|
+
}
|
|
36
|
+
/** The "## Needs" section a blueprint may carry: "- network: docs.swift.org: why" or "- command: swiftc: why". */
|
|
37
|
+
export function blueprintNeeds(blueprint) {
|
|
38
|
+
if (!blueprint)
|
|
39
|
+
return [];
|
|
40
|
+
const section = blueprint.split(/^## /m).find((s) => /^Needs\b/i.test(s));
|
|
41
|
+
if (!section)
|
|
42
|
+
return [];
|
|
43
|
+
const needs = [];
|
|
44
|
+
for (const line of section.split('\n').slice(1)) {
|
|
45
|
+
const m = line.match(/^\s*[-*]\s*(network|command)\s*:\s*`?([^`:]+?)`?\s*(?::|—|-)\s*(.+)$/i);
|
|
46
|
+
if (!m)
|
|
47
|
+
continue;
|
|
48
|
+
const kind = m[1].toLowerCase();
|
|
49
|
+
const raw = m[2].trim();
|
|
50
|
+
const value = kind === 'command' ? (raw.endsWith('*') ? raw : `${raw.split(/\s+/)[0]} *`) : raw.toLowerCase();
|
|
51
|
+
if (parsePermission(permissionId(kind, value)))
|
|
52
|
+
needs.push({ kind, value, why: m[3].trim() });
|
|
53
|
+
}
|
|
54
|
+
return needs;
|
|
55
|
+
}
|
|
56
|
+
/** Merge one run's blocked needs into the repo's record. */
|
|
57
|
+
export function recordNeeds(existing, run, cardId, at) {
|
|
58
|
+
const out = existing.map((n) => ({ ...n }));
|
|
59
|
+
const add = (kind, value) => {
|
|
60
|
+
const id = permissionId(kind, value);
|
|
61
|
+
if (!parsePermission(id))
|
|
62
|
+
return;
|
|
63
|
+
const hit = out.find((n) => n.id === id);
|
|
64
|
+
if (hit)
|
|
65
|
+
Object.assign(hit, { runs: hit.runs + 1, lastCard: cardId, lastAt: at });
|
|
66
|
+
else
|
|
67
|
+
out.push({ id, kind, value, runs: 1, lastCard: cardId, lastAt: at });
|
|
68
|
+
};
|
|
69
|
+
for (const c of new Set(run.commands))
|
|
70
|
+
add('command', c);
|
|
71
|
+
for (const h of new Set(run.hosts))
|
|
72
|
+
add('network', h);
|
|
73
|
+
return out;
|
|
74
|
+
}
|
|
75
|
+
/** The plan for a card: earlier runs' blocks, preflight's network needs, and the blueprint's own Needs. */
|
|
76
|
+
export function planFor(blueprint, recorded, preflightHosts = []) {
|
|
77
|
+
const plan = [];
|
|
78
|
+
const push = (p) => {
|
|
79
|
+
if (!plan.some((q) => q.id === p.id))
|
|
80
|
+
plan.push(p);
|
|
81
|
+
};
|
|
82
|
+
for (const n of blueprintNeeds(blueprint))
|
|
83
|
+
push({ id: permissionId(n.kind, n.value), kind: n.kind, value: n.value, why: n.why, source: 'blueprint' });
|
|
84
|
+
for (const n of recorded) {
|
|
85
|
+
const what = n.kind === 'network' ? `reaching ${n.value}` : `running ${n.value.replace(/ \*$/, '')}`;
|
|
86
|
+
push({ id: n.id, kind: n.kind, value: n.value, why: `an earlier run here was blocked ${what} (${n.runs}×, last on #${n.lastCard})`, source: 'earlier runs' });
|
|
87
|
+
}
|
|
88
|
+
for (const h of preflightHosts)
|
|
89
|
+
push({ id: permissionId('network', h), kind: 'network', value: h, why: "the repo's checks reach it", source: 'preflight' });
|
|
90
|
+
return plan;
|
|
91
|
+
}
|
|
92
|
+
/** What a run gets from the permissions you allowed: extra tool rules and sandbox domains. */
|
|
93
|
+
export function applyPermissions(allowed) {
|
|
94
|
+
const tools = [];
|
|
95
|
+
const domains = [];
|
|
96
|
+
for (const id of allowed ?? []) {
|
|
97
|
+
const p = parsePermission(id);
|
|
98
|
+
if (!p)
|
|
99
|
+
continue;
|
|
100
|
+
if (p.kind === 'network')
|
|
101
|
+
domains.push(p.value);
|
|
102
|
+
else
|
|
103
|
+
tools.push(`Bash(${p.value})`);
|
|
104
|
+
}
|
|
105
|
+
return { allowedTools: tools, domains };
|
|
106
|
+
}
|
package/dist/picture.js
CHANGED
|
@@ -7,11 +7,12 @@ import { existsSync, writeFileSync } from 'node:fs';
|
|
|
7
7
|
import { dirname, join } from 'node:path';
|
|
8
8
|
import { fileURLToPath } from 'node:url';
|
|
9
9
|
import { homePath } from './home.js';
|
|
10
|
+
import { chosenType, suggestTypes, typeHint } from './picturetype.js';
|
|
10
11
|
export function skillDir() {
|
|
11
12
|
return join(dirname(fileURLToPath(import.meta.url)), '..', 'vendor', 'diagram-design');
|
|
12
13
|
}
|
|
13
14
|
export const blueprintHash = (text) => createHash('sha256').update(text ?? '').digest('hex').slice(0, 16);
|
|
14
|
-
export function picturePrompt(card) {
|
|
15
|
+
export function picturePrompt(card, suggestions = suggestTypes(card)) {
|
|
15
16
|
return `You are drawing the picture for a task card in Chompyy, unattended. Nobody can answer questions,
|
|
16
17
|
so don't ask any: decide and draw.
|
|
17
18
|
|
|
@@ -21,11 +22,12 @@ onboarding, and take every color from it (never the colors in the example files)
|
|
|
21
22
|
type and read only that type's reference, plus references/primitives-core.md or
|
|
22
23
|
references/layout-budget.md if you need exact markup. Start from assets/template.html.
|
|
23
24
|
|
|
25
|
+
Which type: the card's text was checked for signals (below). They are hints, not orders.
|
|
26
|
+
${typeHint(suggestions)}
|
|
27
|
+
|
|
24
28
|
Who looks at it: the person deciding in ten seconds whether to approve this task. The picture must show
|
|
25
29
|
what the task will DO and WHY it helps: the thing it produces or changes and how it fits, not the
|
|
26
|
-
blueprint's headings. Don't draw four boxes called Goal / Changes / Won't touch / Done when.
|
|
27
|
-
are usually Steps, Flowchart, Data flow, Architecture or Before/After (Architecture delta), but choose
|
|
28
|
-
what fits. Put the accent on the 1 or 2 things this task actually creates or changes. Keep it small:
|
|
30
|
+
blueprint's headings. Don't draw four boxes called Goal / Changes / Won't touch / Done when. Put the accent on the 1 or 2 things this task actually creates or changes. Keep it small:
|
|
29
31
|
at most 7 nodes. Title (h1): the task in plain words. Eyebrow: "<Type> · Card #${card.id}".
|
|
30
32
|
|
|
31
33
|
Rendering rules for Chompyy:
|
|
@@ -103,15 +105,20 @@ export async function drawPicture(card, runner, opts) {
|
|
|
103
105
|
throw new Error(`Card #${card.id} has no blueprint to draw yet.`);
|
|
104
106
|
if (!existsSync(join(skillDir(), 'SKILL.md')))
|
|
105
107
|
throw new Error('The diagram-design skill is missing from this install.');
|
|
106
|
-
const
|
|
108
|
+
const suggested = suggestTypes(card);
|
|
109
|
+
const result = await runner.run({ prompt: picturePrompt(card, suggested), cwd: skillDir(), model: opts.model, tools: 'Read,Glob,Grep', timeoutMs: opts.timeoutMs ?? 300_000 });
|
|
107
110
|
if (result.timedOut)
|
|
108
111
|
throw new Error('Drawing the picture timed out.');
|
|
109
112
|
if (result.exitCode !== 0)
|
|
110
113
|
throw new Error(`claude exited with ${result.exitCode}: ${result.stderr.trim().slice(0, 300)}`);
|
|
111
114
|
const html = cleanPicture(extractHtml(result.output));
|
|
115
|
+
const chose = chosenType(html);
|
|
112
116
|
const path = homePath('pictures', `${card.id}.html`);
|
|
113
117
|
const darkPath = homePath('pictures', `${card.id}-dark.html`);
|
|
114
118
|
writeFileSync(path, html);
|
|
115
119
|
writeFileSync(darkPath, darkPicture(html));
|
|
116
|
-
return {
|
|
120
|
+
return {
|
|
121
|
+
path, darkPath, at: opts.now.toISOString(), blueprintHash: blueprintHash(card.blueprint), check: selfCheck(path),
|
|
122
|
+
type: chose?.type ?? null, why: chose?.why ?? null, suggested,
|
|
123
|
+
};
|
|
117
124
|
}
|