@firefunc-agent/runner 0.9.0 → 0.9.4
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 +90 -28
- package/dist/activity.d.ts +4 -1
- package/dist/activity.js +94 -8
- package/dist/api.d.ts +24 -0
- package/dist/api.js +74 -5
- package/dist/brief.d.ts +5 -0
- package/dist/brief.js +21 -0
- package/dist/build-info.json +2 -2
- package/dist/cli.js +10 -2
- package/dist/config.d.ts +2 -0
- package/dist/cut.d.ts +45 -0
- package/dist/cut.js +105 -0
- package/dist/daemon.d.ts +16 -0
- package/dist/daemon.js +156 -35
- package/dist/declined.d.ts +11 -0
- package/dist/declined.js +27 -0
- package/dist/engines/claude.js +129 -1
- package/dist/engines/types.d.ts +10 -0
- package/dist/exec.d.ts +3 -0
- package/dist/exec.js +48 -5
- package/dist/firefunc/agents.js +59 -7
- package/dist/firefunc/client.d.ts +1 -0
- package/dist/firefunc/client.js +3 -0
- package/dist/firefunc/configure.d.ts +6 -0
- package/dist/firefunc/configure.js +409 -0
- package/dist/firefunc/main.js +45 -7
- package/dist/firefunc/output.d.ts +1 -0
- package/dist/firefunc/output.js +24 -1
- package/dist/firefunc/tasks.js +32 -4
- package/dist/firefunc/version.js +2 -17
- package/dist/held.d.ts +18 -0
- package/dist/held.js +95 -0
- package/dist/job.d.ts +3 -0
- package/dist/job.js +437 -76
- package/dist/keep-local.d.ts +6 -1
- package/dist/keep-local.js +171 -4
- package/dist/limit.d.ts +30 -0
- package/dist/limit.js +258 -0
- package/dist/locks.d.ts +14 -0
- package/dist/locks.js +164 -0
- package/dist/mcp-proxy.d.ts +21 -0
- package/dist/mcp-proxy.js +180 -0
- package/dist/mcp.d.ts +2 -0
- package/dist/mcp.js +31 -2
- package/dist/orphans.d.ts +40 -0
- package/dist/orphans.js +235 -0
- package/dist/pool.d.ts +7 -0
- package/dist/pool.js +57 -9
- package/dist/pr-title.d.ts +11 -0
- package/dist/pr-title.js +63 -0
- package/dist/recover.d.ts +10 -0
- package/dist/recover.js +104 -0
- package/dist/session-stream.d.ts +8 -1
- package/dist/session-stream.js +9 -1
- package/dist/skills.d.ts +11 -0
- package/dist/skills.js +164 -0
- package/dist/stopped.d.ts +3 -0
- package/dist/stopped.js +10 -0
- package/dist/usage.d.ts +49 -0
- package/dist/usage.js +389 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.js +17 -0
- package/dist/worktrees.d.ts +15 -0
- package/dist/worktrees.js +96 -0
- package/package.json +1 -1
- package/skill/firefunc/SKILL.md +63 -8
package/dist/skills.js
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { execFileSync } from 'node:child_process';
|
|
2
|
+
import { existsSync, lstatSync, mkdirSync, realpathSync, statSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { isAbsolute, join, relative, sep } from 'node:path';
|
|
4
|
+
const NONE = { written: [], pathspecs: [] };
|
|
5
|
+
const SKILL_GITIGNORE = '# FireFunc: a skill written for one run, never committed\n*\n';
|
|
6
|
+
export function writtenSkillPaths(staged, w) {
|
|
7
|
+
const fold = (p) => (w.ignoreCase ? p.toLowerCase() : p);
|
|
8
|
+
const mine = w.written.map(fold);
|
|
9
|
+
return staged.filter((l) => {
|
|
10
|
+
const f = fold(l);
|
|
11
|
+
return mine.some((p) => f === p || f.startsWith(`${p}/`));
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
function git(cwd, args) {
|
|
15
|
+
try {
|
|
16
|
+
return execFileSync('git', ['-C', cwd, ...args], {
|
|
17
|
+
encoding: 'utf8',
|
|
18
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
19
|
+
}).trim();
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
function insideTop(top, real) {
|
|
26
|
+
const rel = relative(top, real);
|
|
27
|
+
if (!rel || rel === '..' || rel.startsWith(`..${sep}`) || isAbsolute(rel))
|
|
28
|
+
return null;
|
|
29
|
+
return rel.split(sep).join('/');
|
|
30
|
+
}
|
|
31
|
+
export function writeSkills(worktree, skills, log) {
|
|
32
|
+
const seen = new Set();
|
|
33
|
+
const wanted = [];
|
|
34
|
+
for (const s of skills ?? []) {
|
|
35
|
+
if (!s || typeof s.slug !== 'string' || typeof s.content !== 'string')
|
|
36
|
+
continue;
|
|
37
|
+
const slug = s.slug.replace(/[^a-z0-9-]/gi, '-').toLowerCase();
|
|
38
|
+
if (!slug || seen.has(slug))
|
|
39
|
+
continue;
|
|
40
|
+
seen.add(slug);
|
|
41
|
+
wanted.push({ slug, content: s.content });
|
|
42
|
+
}
|
|
43
|
+
if (wanted.length === 0)
|
|
44
|
+
return NONE;
|
|
45
|
+
let top;
|
|
46
|
+
try {
|
|
47
|
+
top = realpathSync(worktree);
|
|
48
|
+
}
|
|
49
|
+
catch (e) {
|
|
50
|
+
log(`could not write skills: ${e.message}`);
|
|
51
|
+
return NONE;
|
|
52
|
+
}
|
|
53
|
+
let dir = top;
|
|
54
|
+
for (const part of ['.claude', 'skills']) {
|
|
55
|
+
const next = join(dir, part);
|
|
56
|
+
const shown = relative(top, next).split(sep).join('/') || part;
|
|
57
|
+
if (!lstatExists(next)) {
|
|
58
|
+
try {
|
|
59
|
+
mkdirSync(next);
|
|
60
|
+
}
|
|
61
|
+
catch (e) {
|
|
62
|
+
log(`could not write skills: ${e.message}`);
|
|
63
|
+
return NONE;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
else if (!existsSync(next)) {
|
|
67
|
+
log(`skills not written: ${shown} is a link to a place that does not exist in this checkout. ` +
|
|
68
|
+
`Fix: point it at a folder inside the repository, or remove the link.`);
|
|
69
|
+
return NONE;
|
|
70
|
+
}
|
|
71
|
+
let real;
|
|
72
|
+
try {
|
|
73
|
+
real = realpathSync(next);
|
|
74
|
+
}
|
|
75
|
+
catch (e) {
|
|
76
|
+
log(`could not write skills: ${e.message}`);
|
|
77
|
+
return NONE;
|
|
78
|
+
}
|
|
79
|
+
if (insideTop(top, real) === null) {
|
|
80
|
+
log(`skills not written: ${shown} leads to ${real}, which is not a folder inside this checkout; ` +
|
|
81
|
+
`FireFunc writes only inside the worktree. ` +
|
|
82
|
+
`Fix: point ${shown} at a folder inside the repository.`);
|
|
83
|
+
return NONE;
|
|
84
|
+
}
|
|
85
|
+
if (!statSync(real).isDirectory()) {
|
|
86
|
+
log(`skills not written: ${shown} is a file, not a folder. ` +
|
|
87
|
+
`Fix: make ${shown} a folder (or a link to one inside the repository).`);
|
|
88
|
+
return NONE;
|
|
89
|
+
}
|
|
90
|
+
dir = real;
|
|
91
|
+
}
|
|
92
|
+
const skillsRel = insideTop(top, dir);
|
|
93
|
+
const gitTop = git(top, ['rev-parse', '--show-toplevel']);
|
|
94
|
+
let isRepo = false;
|
|
95
|
+
if (gitTop) {
|
|
96
|
+
try {
|
|
97
|
+
isRepo = realpathSync(gitTop) === top;
|
|
98
|
+
}
|
|
99
|
+
catch {
|
|
100
|
+
isRepo = false;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
const ignoreCase = isRepo && git(top, ['config', '--bool', 'core.ignorecase']) === 'true';
|
|
104
|
+
const fold = (x) => (ignoreCase ? x.toLowerCase() : x);
|
|
105
|
+
const tracked = isRepo
|
|
106
|
+
? (git(top, ['ls-files', '-z', '--', `:(literal${ignoreCase ? ',icase' : ''})${skillsRel}`]) ??
|
|
107
|
+
'')
|
|
108
|
+
.split('\0')
|
|
109
|
+
.filter(Boolean)
|
|
110
|
+
.map(fold)
|
|
111
|
+
: [];
|
|
112
|
+
const written = [];
|
|
113
|
+
for (const s of wanted) {
|
|
114
|
+
const p = `${skillsRel}/${s.slug}`;
|
|
115
|
+
const folded = fold(p);
|
|
116
|
+
if (tracked.some((t) => t === folded || t.startsWith(`${folded}/`))) {
|
|
117
|
+
log(`skill ${s.slug} not written: the repository tracks its own ${p}, and FireFunc never ` +
|
|
118
|
+
`overwrites a repository's files — this run sees the repository's version. ` +
|
|
119
|
+
`Fix: rename one of the two skills.`);
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
const slugDir = join(dir, s.slug);
|
|
123
|
+
try {
|
|
124
|
+
if (lstatExists(slugDir)) {
|
|
125
|
+
log(`skill ${s.slug} not written: ${p} is already in the checkout (possibly under another ` +
|
|
126
|
+
`case), and FireFunc never writes into the repository's own files — this run sees ` +
|
|
127
|
+
`what is there. Fix: rename the skill, or remove ${p} from the repository.`);
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
mkdirSync(slugDir);
|
|
131
|
+
writeFileSync(join(slugDir, 'SKILL.md'), s.content);
|
|
132
|
+
written.push(p);
|
|
133
|
+
try {
|
|
134
|
+
writeFileSync(join(slugDir, '.gitignore'), SKILL_GITIGNORE);
|
|
135
|
+
}
|
|
136
|
+
catch (e) {
|
|
137
|
+
log(`could not keep skill ${s.slug} out of git status: ${e.message}`);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
catch (e) {
|
|
141
|
+
log(`could not write skill ${s.slug}: ${e.message}`);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
if (written.length === 0)
|
|
145
|
+
return NONE;
|
|
146
|
+
const where = skillsRel === '.claude/skills' ? '.claude/skills' : `.claude/skills (${skillsRel})`;
|
|
147
|
+
log(`wrote ${written.length} skill(s) into ${where}: ` +
|
|
148
|
+
written.map((p) => p.slice(skillsRel.length + 1)).join(', '));
|
|
149
|
+
return {
|
|
150
|
+
written,
|
|
151
|
+
pathspecs: written.map((p) => `:(exclude,literal${ignoreCase ? ',icase' : ''})${p}`),
|
|
152
|
+
...(ignoreCase ? { ignoreCase } : {}),
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
function lstatExists(p) {
|
|
156
|
+
try {
|
|
157
|
+
lstatSync(p);
|
|
158
|
+
return true;
|
|
159
|
+
}
|
|
160
|
+
catch {
|
|
161
|
+
return false;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
//# sourceMappingURL=skills.js.map
|
package/dist/stopped.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export function endedBySignal(code) {
|
|
2
|
+
return code === 143 ? 'SIGTERM' : code === 137 ? 'SIGKILL' : null;
|
|
3
|
+
}
|
|
4
|
+
export function stoppedSentence(engineName, code, signal, sessionLog) {
|
|
5
|
+
return (`${engineName} was ended by a signal (${signal}, exit ${code}) before it finished — by a person, ` +
|
|
6
|
+
'the operating system or a time limit on the machine, not by the agent. The runner pushed ' +
|
|
7
|
+
`nothing and opened no pull request. Its output stayed on the machine${sessionLog ? ` — the session log is logs/${sessionLog} in the runner’s folder` : ''}. ` +
|
|
8
|
+
'Re-run the task to start a new attempt.');
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=stopped.js.map
|
package/dist/usage.d.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
export type ModelUsage = {
|
|
2
|
+
input_tokens: number;
|
|
3
|
+
output_tokens: number;
|
|
4
|
+
cache_creation_input_tokens?: number;
|
|
5
|
+
cache_read_input_tokens?: number;
|
|
6
|
+
thinking_tokens?: number;
|
|
7
|
+
cost_usd?: number;
|
|
8
|
+
};
|
|
9
|
+
export type UsageLimits = {
|
|
10
|
+
five_hour_pct_before?: number;
|
|
11
|
+
five_hour_pct_after?: number;
|
|
12
|
+
weekly_pct_before?: number;
|
|
13
|
+
weekly_pct_after?: number;
|
|
14
|
+
source: 'rate_limit_event';
|
|
15
|
+
};
|
|
16
|
+
export type RunUsage = {
|
|
17
|
+
input_tokens: number;
|
|
18
|
+
output_tokens: number;
|
|
19
|
+
cache_creation_input_tokens?: number;
|
|
20
|
+
cache_read_input_tokens?: number;
|
|
21
|
+
thinking_tokens?: number;
|
|
22
|
+
total_cost_usd?: number;
|
|
23
|
+
num_turns?: number;
|
|
24
|
+
duration_ms?: number;
|
|
25
|
+
duration_api_ms?: number;
|
|
26
|
+
models?: Record<string, ModelUsage>;
|
|
27
|
+
cost_basis?: 'list' | 'managed';
|
|
28
|
+
auth?: 'subscription' | 'api_key';
|
|
29
|
+
overage?: boolean;
|
|
30
|
+
engine_version?: string;
|
|
31
|
+
sessions?: number;
|
|
32
|
+
incomplete?: boolean;
|
|
33
|
+
limits?: UsageLimits;
|
|
34
|
+
};
|
|
35
|
+
export declare function readClaudeUsage(stdout: string): RunUsage | null;
|
|
36
|
+
export declare function combineUsage(sessions: Array<RunUsage | null>): RunUsage | null;
|
|
37
|
+
export declare function carriesEarlierSpend(u: RunUsage): boolean;
|
|
38
|
+
export declare function continueUsage(prev: RunUsage | null, next: RunUsage | null): RunUsage | null;
|
|
39
|
+
export declare function usageSince(next: RunUsage | null, baseline: RunUsage | undefined): RunUsage | null;
|
|
40
|
+
export type AttemptSession = {
|
|
41
|
+
usage: RunUsage | null;
|
|
42
|
+
session: string | null;
|
|
43
|
+
continued: boolean;
|
|
44
|
+
};
|
|
45
|
+
export declare function attemptUsage(sessions: AttemptSession[], cut?: {
|
|
46
|
+
session: string;
|
|
47
|
+
baseline?: RunUsage;
|
|
48
|
+
}): RunUsage | null;
|
|
49
|
+
export declare function describeUsage(u: RunUsage): string;
|
package/dist/usage.js
ADDED
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
const isObj = (v) => typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
2
|
+
const count = (v) => typeof v === 'number' && Number.isFinite(v) && v >= 0 ? v : undefined;
|
|
3
|
+
const plus = (a, b) => a === undefined && b === undefined ? undefined : (a ?? 0) + (b ?? 0);
|
|
4
|
+
function tidy(o) {
|
|
5
|
+
for (const k of Object.keys(o))
|
|
6
|
+
if (o[k] === undefined)
|
|
7
|
+
delete o[k];
|
|
8
|
+
return o;
|
|
9
|
+
}
|
|
10
|
+
function modelLine(raw) {
|
|
11
|
+
if (!isObj(raw))
|
|
12
|
+
return null;
|
|
13
|
+
const input = count(raw.inputTokens);
|
|
14
|
+
const output = count(raw.outputTokens);
|
|
15
|
+
if (input === undefined || output === undefined)
|
|
16
|
+
return null;
|
|
17
|
+
const basis = raw.costBasis === undefined || raw.costBasis === 'list'
|
|
18
|
+
? 'list'
|
|
19
|
+
: raw.costBasis === 'managed'
|
|
20
|
+
? 'managed'
|
|
21
|
+
: 'unknown';
|
|
22
|
+
return {
|
|
23
|
+
line: tidy({
|
|
24
|
+
input_tokens: input,
|
|
25
|
+
output_tokens: output,
|
|
26
|
+
cache_creation_input_tokens: count(raw.cacheCreationInputTokens),
|
|
27
|
+
cache_read_input_tokens: count(raw.cacheReadInputTokens),
|
|
28
|
+
thinking_tokens: count(raw.thinkingTokens),
|
|
29
|
+
cost_usd: count(raw.costUSD),
|
|
30
|
+
}),
|
|
31
|
+
basis,
|
|
32
|
+
provider: raw.provider,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
function runBasis(bases) {
|
|
36
|
+
if (bases.includes('unknown'))
|
|
37
|
+
return 'unknown';
|
|
38
|
+
const managed = bases.filter((b) => b === 'managed').length;
|
|
39
|
+
if (managed === 0)
|
|
40
|
+
return 'list';
|
|
41
|
+
return managed === bases.length ? 'managed' : 'mixed';
|
|
42
|
+
}
|
|
43
|
+
function reportCounts(ev) {
|
|
44
|
+
const out = [ev.total_cost_usd];
|
|
45
|
+
if (isObj(ev.usage)) {
|
|
46
|
+
const u = ev.usage;
|
|
47
|
+
out.push(u.input_tokens, u.output_tokens, u.cache_creation_input_tokens, u.cache_read_input_tokens);
|
|
48
|
+
}
|
|
49
|
+
if (isObj(ev.modelUsage)) {
|
|
50
|
+
for (const m of Object.values(ev.modelUsage)) {
|
|
51
|
+
if (!isObj(m))
|
|
52
|
+
continue;
|
|
53
|
+
out.push(m.inputTokens, m.outputTokens, m.cacheCreationInputTokens, m.cacheReadInputTokens, m.costUSD);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return out.filter((v) => typeof v === 'number');
|
|
57
|
+
}
|
|
58
|
+
function isZeroedCrash(ev) {
|
|
59
|
+
return ev.subtype === 'error_during_execution' && reportCounts(ev).every((v) => v === 0);
|
|
60
|
+
}
|
|
61
|
+
function sumModels(models) {
|
|
62
|
+
let acc = { input_tokens: 0, output_tokens: 0 };
|
|
63
|
+
for (const m of models)
|
|
64
|
+
acc = addModel(acc, m);
|
|
65
|
+
return acc;
|
|
66
|
+
}
|
|
67
|
+
function addModel(a, b) {
|
|
68
|
+
return tidy({
|
|
69
|
+
input_tokens: a.input_tokens + b.input_tokens,
|
|
70
|
+
output_tokens: a.output_tokens + b.output_tokens,
|
|
71
|
+
cache_creation_input_tokens: plus(a.cache_creation_input_tokens, b.cache_creation_input_tokens),
|
|
72
|
+
cache_read_input_tokens: plus(a.cache_read_input_tokens, b.cache_read_input_tokens),
|
|
73
|
+
thinking_tokens: plus(a.thinking_tokens, b.thinking_tokens),
|
|
74
|
+
cost_usd: plus(a.cost_usd, b.cost_usd),
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
export function readClaudeUsage(stdout) {
|
|
78
|
+
try {
|
|
79
|
+
let result = null;
|
|
80
|
+
const stretches = [];
|
|
81
|
+
let crashedAfter = false;
|
|
82
|
+
let floor = false;
|
|
83
|
+
let overage = false;
|
|
84
|
+
let apiKeySource;
|
|
85
|
+
let version;
|
|
86
|
+
const first = {
|
|
87
|
+
five_hour: undefined,
|
|
88
|
+
seven_day: undefined,
|
|
89
|
+
};
|
|
90
|
+
const last = { ...first };
|
|
91
|
+
for (const line of stdout.split('\n')) {
|
|
92
|
+
const t = line.trim();
|
|
93
|
+
if (!t.startsWith('{'))
|
|
94
|
+
continue;
|
|
95
|
+
if (!t.includes('"result"') && !t.includes('rate_limit_event') && !t.includes('"init"'))
|
|
96
|
+
continue;
|
|
97
|
+
let ev;
|
|
98
|
+
try {
|
|
99
|
+
ev = JSON.parse(t);
|
|
100
|
+
}
|
|
101
|
+
catch {
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
if (!isObj(ev))
|
|
105
|
+
continue;
|
|
106
|
+
if (ev.type === 'result') {
|
|
107
|
+
if (isZeroedCrash(ev)) {
|
|
108
|
+
crashedAfter = true;
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
result = ev;
|
|
112
|
+
stretches.push(ev);
|
|
113
|
+
crashedAfter = false;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
else if (ev.type === 'system' && ev.subtype === 'init') {
|
|
117
|
+
apiKeySource = ev.apiKeySource;
|
|
118
|
+
version = ev.claude_code_version;
|
|
119
|
+
}
|
|
120
|
+
else if (ev.type === 'rate_limit_event' && isObj(ev.rate_limit_info)) {
|
|
121
|
+
const info = ev.rate_limit_info;
|
|
122
|
+
if (info.isUsingOverage === true || info.overageInUse === true)
|
|
123
|
+
overage = true;
|
|
124
|
+
const kind = info.rateLimitType ?? info.rate_limit_type;
|
|
125
|
+
const u = count(info.utilization);
|
|
126
|
+
if ((kind === 'five_hour' || kind === 'seven_day') && u !== undefined && u <= 1) {
|
|
127
|
+
const pct = Math.round(u * 1000) / 10;
|
|
128
|
+
if (first[kind] === undefined)
|
|
129
|
+
first[kind] = pct;
|
|
130
|
+
last[kind] = pct;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
if (!result)
|
|
135
|
+
return null;
|
|
136
|
+
const lines = [];
|
|
137
|
+
if (isObj(result.modelUsage)) {
|
|
138
|
+
for (const [id, raw] of Object.entries(result.modelUsage)) {
|
|
139
|
+
const m = modelLine(raw);
|
|
140
|
+
if (m)
|
|
141
|
+
lines.push([id, m]);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
const basis = lines.length > 0 ? runBasis(lines.map(([, m]) => m.basis)) : 'list';
|
|
145
|
+
const priced = basis === 'list' || basis === 'managed';
|
|
146
|
+
const models = {};
|
|
147
|
+
for (const [id, m] of lines) {
|
|
148
|
+
const keep = m.basis === 'list' || (m.basis === 'managed' && basis === 'managed');
|
|
149
|
+
models[id] = keep ? m.line : tidy({ ...m.line, cost_usd: undefined });
|
|
150
|
+
}
|
|
151
|
+
const perModel = Object.values(models);
|
|
152
|
+
let totals;
|
|
153
|
+
const s = sumModels(perModel);
|
|
154
|
+
if (perModel.length > 0) {
|
|
155
|
+
totals = tidy({
|
|
156
|
+
input_tokens: s.input_tokens,
|
|
157
|
+
output_tokens: s.output_tokens,
|
|
158
|
+
cache_creation_input_tokens: s.cache_creation_input_tokens,
|
|
159
|
+
cache_read_input_tokens: s.cache_read_input_tokens,
|
|
160
|
+
thinking_tokens: s.thinking_tokens,
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
let loop = null;
|
|
165
|
+
let unreadStretch = false;
|
|
166
|
+
for (const r of stretches) {
|
|
167
|
+
const u = isObj(r.usage) ? r.usage : null;
|
|
168
|
+
const input = count(u?.input_tokens);
|
|
169
|
+
const output = count(u?.output_tokens);
|
|
170
|
+
if (input === undefined || output === undefined) {
|
|
171
|
+
unreadStretch = true;
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
174
|
+
const details = isObj(u?.output_tokens_details) ? u.output_tokens_details : null;
|
|
175
|
+
const line = tidy({
|
|
176
|
+
input_tokens: input,
|
|
177
|
+
output_tokens: output,
|
|
178
|
+
cache_creation_input_tokens: count(u?.cache_creation_input_tokens),
|
|
179
|
+
cache_read_input_tokens: count(u?.cache_read_input_tokens),
|
|
180
|
+
thinking_tokens: count(details?.thinking_tokens),
|
|
181
|
+
});
|
|
182
|
+
loop = loop ? addModel(loop, line) : line;
|
|
183
|
+
}
|
|
184
|
+
if (!loop)
|
|
185
|
+
return null;
|
|
186
|
+
if (unreadStretch)
|
|
187
|
+
floor = true;
|
|
188
|
+
totals = tidy({
|
|
189
|
+
input_tokens: loop.input_tokens,
|
|
190
|
+
output_tokens: loop.output_tokens,
|
|
191
|
+
cache_creation_input_tokens: loop.cache_creation_input_tokens,
|
|
192
|
+
cache_read_input_tokens: loop.cache_read_input_tokens,
|
|
193
|
+
thinking_tokens: loop.thinking_tokens,
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
const stretchSum = (k) => stretches.reduce((acc, r) => plus(acc, count(r[k])), undefined);
|
|
197
|
+
const limits = tidy({
|
|
198
|
+
five_hour_pct_before: first.five_hour,
|
|
199
|
+
five_hour_pct_after: last.five_hour,
|
|
200
|
+
weekly_pct_before: first.seven_day,
|
|
201
|
+
weekly_pct_after: last.seven_day,
|
|
202
|
+
});
|
|
203
|
+
const firstParty = lines.length > 0 && lines.every(([, m]) => m.provider === 'firstParty');
|
|
204
|
+
return tidy({
|
|
205
|
+
...totals,
|
|
206
|
+
total_cost_usd: priced ? (count(result.total_cost_usd) ?? s.cost_usd) : undefined,
|
|
207
|
+
cost_basis: priced && basis === 'managed' ? 'managed' : undefined,
|
|
208
|
+
num_turns: stretchSum('num_turns'),
|
|
209
|
+
duration_ms: stretchSum('duration_ms'),
|
|
210
|
+
duration_api_ms: count(result.duration_api_ms),
|
|
211
|
+
models: perModel.length > 0 ? models : undefined,
|
|
212
|
+
auth: apiKeySource === 'none'
|
|
213
|
+
? firstParty
|
|
214
|
+
? 'subscription'
|
|
215
|
+
: undefined
|
|
216
|
+
: typeof apiKeySource === 'string' && apiKeySource
|
|
217
|
+
? 'api_key'
|
|
218
|
+
: undefined,
|
|
219
|
+
overage: overage ? true : undefined,
|
|
220
|
+
engine_version: typeof version === 'string' && version ? version : undefined,
|
|
221
|
+
sessions: 1,
|
|
222
|
+
incomplete: crashedAfter || floor ? true : undefined,
|
|
223
|
+
limits: Object.keys(limits).length > 0
|
|
224
|
+
? { ...limits, source: 'rate_limit_event' }
|
|
225
|
+
: undefined,
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
catch {
|
|
229
|
+
return null;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
export function combineUsage(sessions) {
|
|
233
|
+
const read = sessions.filter((s) => s !== null);
|
|
234
|
+
if (read.length === 0)
|
|
235
|
+
return null;
|
|
236
|
+
if (read.length === 1 && sessions.length === 1)
|
|
237
|
+
return read[0];
|
|
238
|
+
const models = {};
|
|
239
|
+
for (const s of read) {
|
|
240
|
+
for (const [id, m] of Object.entries(s.models ?? {})) {
|
|
241
|
+
models[id] = models[id] ? addModel(models[id], m) : m;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
const withLimits = read.filter((s) => s.limits);
|
|
245
|
+
const firstL = withLimits[0]?.limits;
|
|
246
|
+
const lastL = withLimits[withLimits.length - 1]?.limits;
|
|
247
|
+
const limits = tidy({
|
|
248
|
+
five_hour_pct_before: firstL?.five_hour_pct_before,
|
|
249
|
+
five_hour_pct_after: lastL?.five_hour_pct_after,
|
|
250
|
+
weekly_pct_before: firstL?.weekly_pct_before,
|
|
251
|
+
weekly_pct_after: lastL?.weekly_pct_after,
|
|
252
|
+
});
|
|
253
|
+
const sum = (k) => read.reduce((acc, s) => plus(acc, s[k]), undefined);
|
|
254
|
+
const latest = read[read.length - 1];
|
|
255
|
+
const priced = read.every((s) => s.total_cost_usd !== undefined) &&
|
|
256
|
+
read.every((s) => s.cost_basis === latest.cost_basis);
|
|
257
|
+
return tidy({
|
|
258
|
+
input_tokens: sum('input_tokens') ?? 0,
|
|
259
|
+
output_tokens: sum('output_tokens') ?? 0,
|
|
260
|
+
cache_creation_input_tokens: sum('cache_creation_input_tokens'),
|
|
261
|
+
cache_read_input_tokens: sum('cache_read_input_tokens'),
|
|
262
|
+
thinking_tokens: sum('thinking_tokens'),
|
|
263
|
+
total_cost_usd: priced ? sum('total_cost_usd') : undefined,
|
|
264
|
+
cost_basis: priced ? latest.cost_basis : undefined,
|
|
265
|
+
num_turns: sum('num_turns'),
|
|
266
|
+
duration_ms: sum('duration_ms'),
|
|
267
|
+
duration_api_ms: sum('duration_api_ms'),
|
|
268
|
+
models: Object.keys(models).length > 0 ? models : undefined,
|
|
269
|
+
auth: latest.auth,
|
|
270
|
+
overage: read.some((s) => s.overage) ? true : undefined,
|
|
271
|
+
engine_version: latest.engine_version,
|
|
272
|
+
sessions: read.length,
|
|
273
|
+
incomplete: read.length < sessions.length || read.some((s) => s.incomplete) ? true : undefined,
|
|
274
|
+
limits: Object.keys(limits).length > 0
|
|
275
|
+
? { ...limits, source: 'rate_limit_event' }
|
|
276
|
+
: undefined,
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
export function carriesEarlierSpend(u) {
|
|
280
|
+
if (!u.models || !u.engine_version)
|
|
281
|
+
return false;
|
|
282
|
+
const m = /^(\d+)\.(\d+)\.(\d+)/.exec(u.engine_version);
|
|
283
|
+
if (!m)
|
|
284
|
+
return false;
|
|
285
|
+
const [maj, min, pat] = [Number(m[1]), Number(m[2]), Number(m[3])];
|
|
286
|
+
return maj > 2 || (maj === 2 && (min > 1 || (min === 1 && pat >= 277)));
|
|
287
|
+
}
|
|
288
|
+
export function continueUsage(prev, next) {
|
|
289
|
+
if (!next)
|
|
290
|
+
return prev ? { ...prev, incomplete: true } : null;
|
|
291
|
+
if (!prev)
|
|
292
|
+
return { ...next, incomplete: true };
|
|
293
|
+
if (!carriesEarlierSpend(next))
|
|
294
|
+
return combineUsage([prev, next]);
|
|
295
|
+
const limits = tidy({
|
|
296
|
+
five_hour_pct_before: prev.limits?.five_hour_pct_before ?? next.limits?.five_hour_pct_before,
|
|
297
|
+
five_hour_pct_after: next.limits?.five_hour_pct_after ?? prev.limits?.five_hour_pct_after,
|
|
298
|
+
weekly_pct_before: prev.limits?.weekly_pct_before ?? next.limits?.weekly_pct_before,
|
|
299
|
+
weekly_pct_after: next.limits?.weekly_pct_after ?? prev.limits?.weekly_pct_after,
|
|
300
|
+
});
|
|
301
|
+
return tidy({
|
|
302
|
+
...next,
|
|
303
|
+
num_turns: plus(prev.num_turns, next.num_turns),
|
|
304
|
+
duration_ms: plus(prev.duration_ms, next.duration_ms),
|
|
305
|
+
overage: prev.overage || next.overage ? true : undefined,
|
|
306
|
+
sessions: prev.sessions ?? 1,
|
|
307
|
+
incomplete: prev.incomplete || next.incomplete ? true : undefined,
|
|
308
|
+
limits: Object.keys(limits).length > 0
|
|
309
|
+
? { ...limits, source: 'rate_limit_event' }
|
|
310
|
+
: undefined,
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
export function usageSince(next, baseline) {
|
|
314
|
+
if (!next || !carriesEarlierSpend(next))
|
|
315
|
+
return next;
|
|
316
|
+
if (!baseline)
|
|
317
|
+
return null;
|
|
318
|
+
const minus = (a, b) => a === undefined ? undefined : Math.max(0, a - (b ?? 0));
|
|
319
|
+
const models = {};
|
|
320
|
+
for (const [id, m] of Object.entries(next.models ?? {})) {
|
|
321
|
+
const was = baseline.models?.[id];
|
|
322
|
+
models[id] = tidy({
|
|
323
|
+
input_tokens: minus(m.input_tokens, was?.input_tokens) ?? 0,
|
|
324
|
+
output_tokens: minus(m.output_tokens, was?.output_tokens) ?? 0,
|
|
325
|
+
cache_creation_input_tokens: minus(m.cache_creation_input_tokens, was?.cache_creation_input_tokens),
|
|
326
|
+
cache_read_input_tokens: minus(m.cache_read_input_tokens, was?.cache_read_input_tokens),
|
|
327
|
+
thinking_tokens: minus(m.thinking_tokens, was?.thinking_tokens),
|
|
328
|
+
cost_usd: minus(m.cost_usd, was?.cost_usd),
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
const s = sumModels(Object.values(models));
|
|
332
|
+
return tidy({
|
|
333
|
+
...next,
|
|
334
|
+
input_tokens: s.input_tokens,
|
|
335
|
+
output_tokens: s.output_tokens,
|
|
336
|
+
cache_creation_input_tokens: s.cache_creation_input_tokens,
|
|
337
|
+
cache_read_input_tokens: s.cache_read_input_tokens,
|
|
338
|
+
thinking_tokens: s.thinking_tokens,
|
|
339
|
+
total_cost_usd: next.total_cost_usd !== undefined && baseline.total_cost_usd !== undefined
|
|
340
|
+
? Math.max(0, next.total_cost_usd - baseline.total_cost_usd)
|
|
341
|
+
: undefined,
|
|
342
|
+
duration_api_ms: minus(next.duration_api_ms, baseline.duration_api_ms),
|
|
343
|
+
models,
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
export function attemptUsage(sessions, cut) {
|
|
347
|
+
const groups = [];
|
|
348
|
+
for (const s of sessions) {
|
|
349
|
+
const own = cut && s.session === cut.session ? usageSince(s.usage, cut.baseline) : s.usage;
|
|
350
|
+
if (s.continued && groups.length > 0) {
|
|
351
|
+
groups[groups.length - 1] = continueUsage(groups[groups.length - 1] ?? null, own);
|
|
352
|
+
}
|
|
353
|
+
else {
|
|
354
|
+
groups.push(own);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
return combineUsage(groups);
|
|
358
|
+
}
|
|
359
|
+
const n = (v) => Math.round(v).toLocaleString('en-US');
|
|
360
|
+
const secs = (ms) => {
|
|
361
|
+
const s = Math.round(ms / 1000);
|
|
362
|
+
return s >= 60 ? `${Math.floor(s / 60)}m${String(s % 60).padStart(2, '0')}s` : `${s}s`;
|
|
363
|
+
};
|
|
364
|
+
export function describeUsage(u) {
|
|
365
|
+
const total = u.input_tokens + u.output_tokens + (u.cache_creation_input_tokens ?? 0);
|
|
366
|
+
const parts = [
|
|
367
|
+
`${u.incomplete ? 'at least ' : ''}${n(total)} tokens (${n(u.input_tokens)} in · ${n(u.output_tokens)} out` +
|
|
368
|
+
`${u.cache_creation_input_tokens !== undefined ? ` · ${n(u.cache_creation_input_tokens)} cache written` : ''})` +
|
|
369
|
+
`${u.cache_read_input_tokens !== undefined ? ` + ${n(u.cache_read_input_tokens)} cache read` : ''}`,
|
|
370
|
+
];
|
|
371
|
+
if (u.num_turns !== undefined)
|
|
372
|
+
parts.push(`${n(u.num_turns)} turns`);
|
|
373
|
+
if (u.duration_ms !== undefined)
|
|
374
|
+
parts.push(secs(u.duration_ms));
|
|
375
|
+
if (u.total_cost_usd !== undefined) {
|
|
376
|
+
const rate = u.cost_basis === 'managed' ? "at your organization's configured rates" : 'at API list prices';
|
|
377
|
+
const why = u.overage
|
|
378
|
+
? " (the plan's extra usage was drawn on, so part of it may have been billed)"
|
|
379
|
+
: u.auth === 'subscription'
|
|
380
|
+
? ' (a subscription run is not billed per token)'
|
|
381
|
+
: '';
|
|
382
|
+
parts.push(`≈$${u.total_cost_usd.toFixed(2)} ${rate}${why}`);
|
|
383
|
+
}
|
|
384
|
+
else if (u.overage) {
|
|
385
|
+
parts.push("the plan's extra usage was drawn on, so part of it may have been billed");
|
|
386
|
+
}
|
|
387
|
+
return parts.join(' · ');
|
|
388
|
+
}
|
|
389
|
+
//# sourceMappingURL=usage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const RUNNER_VERSION: string;
|
package/dist/version.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
import { dirname, join } from 'node:path';
|
|
3
|
+
import { fileURLToPath } from 'node:url';
|
|
4
|
+
export const RUNNER_VERSION = (() => {
|
|
5
|
+
const here = dirname(fileURLToPath(import.meta.url));
|
|
6
|
+
for (const file of [join(here, 'build-info.json'), join(here, '..', 'package.json')]) {
|
|
7
|
+
try {
|
|
8
|
+
const v = JSON.parse(readFileSync(file, 'utf8')).version;
|
|
9
|
+
if (v)
|
|
10
|
+
return v;
|
|
11
|
+
}
|
|
12
|
+
catch {
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
return '0.0.0';
|
|
16
|
+
})();
|
|
17
|
+
//# sourceMappingURL=version.js.map
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export declare function canonicalPath(path: string): string;
|
|
2
|
+
export declare function isRunnerWorktree(path: string): boolean;
|
|
3
|
+
export type WorktreeEntry = {
|
|
4
|
+
path: string;
|
|
5
|
+
branch?: string;
|
|
6
|
+
head?: string;
|
|
7
|
+
};
|
|
8
|
+
export declare function worktreesOf(checkout: string): Promise<WorktreeEntry[]>;
|
|
9
|
+
export declare function staleHolder(stderr: string): string | null;
|
|
10
|
+
export declare function removeRunnerWorktree(checkout: string, path: string, log: (line: string) => void): Promise<void>;
|
|
11
|
+
export declare function sweepLeftoverWorktrees(checkouts: string[], opts: {
|
|
12
|
+
live: Set<string>;
|
|
13
|
+
keep: Set<string>;
|
|
14
|
+
log: (line: string) => void;
|
|
15
|
+
}): Promise<number>;
|