gcf-board 0.18.5 → 0.18.8
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/dist/server/usage.js +29 -10
- package/dist/web/assets/{index-CeIBZt2A.css → index-BckF9M34.css} +1 -1
- package/dist/web/assets/index-DqvwRLye.js +88 -0
- package/dist/web/index.html +2 -2
- package/node_modules/gcf-session/lib/chat.js +2 -1
- package/node_modules/gcf-session/package.json +1 -1
- package/package.json +1 -1
- package/dist/web/assets/index-Pv8r1y-f.js +0 -88
package/dist/server/usage.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// The account's usage limits, asked of Claude's `/usage` (Board-PRD decision 39): what the top
|
|
2
2
|
// bar shows over the last limit a run reported. The command is free (no model call) and answers
|
|
3
3
|
// in a second or two; it still starts a claude process, so it is asked only while a page is open.
|
|
4
|
-
import { exec } from 'node:child_process';
|
|
4
|
+
import { exec, spawnSync } from 'node:child_process';
|
|
5
5
|
import os from 'node:os';
|
|
6
6
|
import { broadcast, watched } from './events.js';
|
|
7
7
|
/** How often an open page's bar is read again, and how long one ask may take. */
|
|
@@ -13,15 +13,20 @@ const ASK_TIMEOUT_MS = 30_000;
|
|
|
13
13
|
const FLAGS = '-p "/usage" --output-format stream-json --verbose --no-session-persistence ' +
|
|
14
14
|
'--setting-sources project --strict-mcp-config';
|
|
15
15
|
let command = null;
|
|
16
|
+
let askTimeout = ASK_TIMEOUT_MS;
|
|
16
17
|
let last = null;
|
|
17
18
|
let asking = null;
|
|
18
19
|
let askedAt = 0;
|
|
19
20
|
// An ask still going when the board is configured again writes nothing into the new setup.
|
|
20
21
|
let generation = 0;
|
|
21
|
-
/**
|
|
22
|
-
|
|
22
|
+
/**
|
|
23
|
+
* Ask with `agent` (claude, or a stand-in in the tests); null asks nothing, as in the tests.
|
|
24
|
+
* `timeoutMs` is the tests' own, shorter wait.
|
|
25
|
+
*/
|
|
26
|
+
export function configureUsage(agent, timeoutMs = ASK_TIMEOUT_MS) {
|
|
23
27
|
generation += 1;
|
|
24
28
|
command = agent;
|
|
29
|
+
askTimeout = timeoutMs;
|
|
25
30
|
last = null;
|
|
26
31
|
asking = null;
|
|
27
32
|
askedAt = 0;
|
|
@@ -53,16 +58,19 @@ export function limitsOf(out) {
|
|
|
53
58
|
return null;
|
|
54
59
|
}
|
|
55
60
|
/**
|
|
56
|
-
* Why an ask failed: the last two lines of its stderr (cmd's "not recognized"
|
|
57
|
-
*
|
|
61
|
+
* Why an ask failed: a timeout, else the last two lines of its stderr (cmd's "not recognized"
|
|
62
|
+
* spans two), else the error itself.
|
|
58
63
|
*/
|
|
59
64
|
export function reasonOf(e, err) {
|
|
60
65
|
if (!e)
|
|
61
66
|
return 'claude answered /usage without the plan limits (an API key has none)';
|
|
67
|
+
// A stuck ask is told by its timeout, not by a warning it printed before it hung.
|
|
68
|
+
if (e.killed)
|
|
69
|
+
return `no answer in ${askTimeout / 1000} s`;
|
|
62
70
|
const tail = err.trim();
|
|
63
71
|
if (tail)
|
|
64
72
|
return tail.split(/\r?\n/).slice(-2).join(' ').slice(0, 300);
|
|
65
|
-
return e.
|
|
73
|
+
return e.message.slice(0, 300);
|
|
66
74
|
}
|
|
67
75
|
/**
|
|
68
76
|
* Ask `/usage` now, once at a time: a second ask while one goes waits for it. A reading tells
|
|
@@ -75,22 +83,33 @@ export function askUsage() {
|
|
|
75
83
|
return asking;
|
|
76
84
|
askedAt = Date.now();
|
|
77
85
|
const mine = generation;
|
|
78
|
-
// shortcut: a timeout kills the shell, not a claude it started on Windows; one stuck claude
|
|
79
|
-
// then lingers until it ends itself. Kill the tree (gcf-session's killTree) if that is seen.
|
|
80
86
|
asking = new Promise((resolve) => {
|
|
81
|
-
|
|
87
|
+
let late = false;
|
|
88
|
+
const child = exec(`${command} ${FLAGS}`, { cwd: os.tmpdir(), windowsHide: true, maxBuffer: 4 << 20 }, (e, out, err) => {
|
|
89
|
+
clearTimeout(timer);
|
|
82
90
|
if (mine !== generation)
|
|
83
91
|
return resolve();
|
|
84
92
|
const limits = limitsOf(out);
|
|
85
93
|
const at = new Date().toISOString();
|
|
94
|
+
const why = e && late ? Object.assign(e, { killed: true }) : e;
|
|
86
95
|
if (limits)
|
|
87
96
|
last = { at, limits };
|
|
88
97
|
else
|
|
89
|
-
last = { ...(last ?? { at: null, limits: [] }), failed: { at, error: reasonOf(
|
|
98
|
+
last = { ...(last ?? { at: null, limits: [] }), failed: { at, error: reasonOf(why, err) } };
|
|
90
99
|
asking = null;
|
|
91
100
|
broadcast({ type: 'quota' });
|
|
92
101
|
resolve();
|
|
93
102
|
});
|
|
103
|
+
// Not exec's own timeout: on Windows that kills the shell, and the claude under it lingers.
|
|
104
|
+
// shortcut: off Windows the shell gets the signal; a claude it forked (not exec'd) lingers,
|
|
105
|
+
// spawn it detached and signal its group if that is seen.
|
|
106
|
+
const timer = setTimeout(() => {
|
|
107
|
+
late = true;
|
|
108
|
+
if (process.platform === 'win32' && child.pid)
|
|
109
|
+
spawnSync('taskkill', ['/pid', String(child.pid), '/T', '/F'], { stdio: 'ignore' });
|
|
110
|
+
else
|
|
111
|
+
child.kill();
|
|
112
|
+
}, askTimeout).unref();
|
|
94
113
|
});
|
|
95
114
|
return asking;
|
|
96
115
|
}
|