@firefunc-agent/runner 0.9.7 → 0.9.34

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.
Files changed (178) hide show
  1. package/README.md +169 -10
  2. package/dist/activity.d.ts +1 -0
  3. package/dist/activity.js +1 -1
  4. package/dist/alive.d.ts +40 -0
  5. package/dist/alive.js +138 -0
  6. package/dist/api-fallback.d.ts +11 -0
  7. package/dist/api-fallback.js +80 -0
  8. package/dist/api.d.ts +186 -3
  9. package/dist/api.js +311 -6
  10. package/dist/ask/index.d.ts +20 -0
  11. package/dist/ask/index.js +64 -0
  12. package/dist/ask-first.d.ts +8 -0
  13. package/dist/brief-file.d.ts +15 -0
  14. package/dist/brief-file.js +36 -0
  15. package/dist/brief.d.ts +5 -0
  16. package/dist/brief.js +19 -3
  17. package/dist/build-caches.d.ts +7 -0
  18. package/dist/build-caches.js +63 -0
  19. package/dist/build-info.json +2 -2
  20. package/dist/checkpoint.d.ts +40 -0
  21. package/dist/checkpoint.js +98 -0
  22. package/dist/cli.js +184 -18
  23. package/dist/cloud-boot.d.ts +41 -0
  24. package/dist/cloud-boot.js +239 -0
  25. package/dist/command-activity.d.ts +23 -0
  26. package/dist/command-activity.js +89 -0
  27. package/dist/command-step.d.ts +58 -0
  28. package/dist/command-step.js +248 -0
  29. package/dist/config.d.ts +40 -0
  30. package/dist/config.js +23 -0
  31. package/dist/cut.d.ts +2 -1
  32. package/dist/cut.js +19 -5
  33. package/dist/daemon.js +375 -14
  34. package/dist/device-lease.d.ts +15 -0
  35. package/dist/device-lease.js +608 -0
  36. package/dist/editor/fs-server.d.ts +31 -0
  37. package/dist/editor/fs-server.js +159 -0
  38. package/dist/editor/relay-crypto.d.ts +68 -0
  39. package/dist/editor/relay-crypto.js +191 -0
  40. package/dist/editor/sessions.d.ts +50 -0
  41. package/dist/editor/sessions.js +279 -0
  42. package/dist/editor/worktree-of.d.ts +1 -0
  43. package/dist/editor/worktree-of.js +23 -0
  44. package/dist/editor/ws-client.d.ts +26 -0
  45. package/dist/editor/ws-client.js +227 -0
  46. package/dist/engine-account.d.ts +24 -0
  47. package/dist/engine-account.js +118 -0
  48. package/dist/engine-login.d.ts +12 -0
  49. package/dist/engine-login.js +88 -0
  50. package/dist/engine-models.d.ts +13 -0
  51. package/dist/engine-models.js +71 -0
  52. package/dist/engine-watch.d.ts +1 -0
  53. package/dist/engine-watch.js +22 -6
  54. package/dist/engines/antigravity.js +19 -4
  55. package/dist/engines/claude.js +15 -8
  56. package/dist/engines/codex.js +16 -2
  57. package/dist/engines/copilot.js +12 -0
  58. package/dist/engines/crush.d.ts +2 -0
  59. package/dist/engines/crush.js +58 -0
  60. package/dist/engines/cursor.js +12 -0
  61. package/dist/engines/gemini.js +14 -9
  62. package/dist/engines/index.d.ts +8 -0
  63. package/dist/engines/index.js +73 -1
  64. package/dist/engines/kilo.d.ts +2 -0
  65. package/dist/engines/kilo.js +63 -0
  66. package/dist/engines/opencode.d.ts +9 -0
  67. package/dist/engines/opencode.js +234 -0
  68. package/dist/engines/openhands.d.ts +10 -0
  69. package/dist/engines/openhands.js +271 -0
  70. package/dist/engines/pi.d.ts +10 -0
  71. package/dist/engines/pi.js +248 -0
  72. package/dist/engines/qwen.d.ts +8 -0
  73. package/dist/engines/qwen.js +153 -0
  74. package/dist/engines/sessions.d.ts +6 -0
  75. package/dist/engines/sessions.js +68 -0
  76. package/dist/engines/types.d.ts +3 -3
  77. package/dist/exec.d.ts +1 -0
  78. package/dist/exec.js +10 -4
  79. package/dist/firefunc/agents.d.ts +8 -1
  80. package/dist/firefunc/agents.js +53 -3
  81. package/dist/firefunc/auth.js +72 -19
  82. package/dist/firefunc/cli.js +25 -0
  83. package/dist/firefunc/client.d.ts +5 -1
  84. package/dist/firefunc/client.js +12 -2
  85. package/dist/firefunc/config.d.ts +11 -1
  86. package/dist/firefunc/config.js +6 -4
  87. package/dist/firefunc/io.d.ts +1 -0
  88. package/dist/firefunc/main.js +8 -1
  89. package/dist/firefunc/oauth-login.d.ts +23 -0
  90. package/dist/firefunc/oauth-login.js +304 -0
  91. package/dist/firefunc/tasks.d.ts +4 -0
  92. package/dist/firefunc/tasks.js +89 -2
  93. package/dist/fleet-words.d.ts +2 -0
  94. package/dist/fleet-words.js +7 -0
  95. package/dist/git-auth.d.ts +16 -0
  96. package/dist/git-auth.js +48 -2
  97. package/dist/held.d.ts +3 -0
  98. package/dist/job.d.ts +26 -3
  99. package/dist/job.js +1109 -132
  100. package/dist/keep-awake.d.ts +17 -0
  101. package/dist/keep-awake.js +90 -0
  102. package/dist/keep-local.d.ts +2 -1
  103. package/dist/keep-local.js +8 -3
  104. package/dist/limit.d.ts +10 -1
  105. package/dist/limit.js +264 -4
  106. package/dist/local-checks.d.ts +19 -0
  107. package/dist/local-checks.js +79 -0
  108. package/dist/locks.d.ts +21 -1
  109. package/dist/locks.js +279 -66
  110. package/dist/machine-commands.d.ts +21 -0
  111. package/dist/machine-commands.js +107 -0
  112. package/dist/machine-profile.d.ts +18 -0
  113. package/dist/machine-profile.js +69 -0
  114. package/dist/mcp-init.d.ts +2 -0
  115. package/dist/mcp-init.js +28 -0
  116. package/dist/mcp.d.ts +13 -1
  117. package/dist/mcp.js +23 -2
  118. package/dist/network.js +3 -2
  119. package/dist/orphans.d.ts +4 -0
  120. package/dist/orphans.js +96 -4
  121. package/dist/plan-phase/index.d.ts +11 -0
  122. package/dist/plan-phase/index.js +46 -0
  123. package/dist/pool.d.ts +1 -0
  124. package/dist/pool.js +29 -4
  125. package/dist/pr-title.d.ts +15 -0
  126. package/dist/pr-title.js +33 -0
  127. package/dist/recover.d.ts +3 -1
  128. package/dist/recover.js +27 -7
  129. package/dist/reference-files.d.ts +27 -0
  130. package/dist/reference-files.js +202 -0
  131. package/dist/release-draft.d.ts +79 -0
  132. package/dist/release-draft.js +301 -0
  133. package/dist/resources.d.ts +86 -0
  134. package/dist/resources.js +217 -0
  135. package/dist/restart-idle.d.ts +23 -0
  136. package/dist/restart-idle.js +67 -0
  137. package/dist/run-attachments.d.ts +84 -0
  138. package/dist/run-attachments.js +281 -0
  139. package/dist/run-stops.d.ts +15 -0
  140. package/dist/run-stops.js +90 -0
  141. package/dist/run-temp.d.ts +12 -0
  142. package/dist/run-temp.js +57 -0
  143. package/dist/secret-scan.d.ts +6 -1
  144. package/dist/secret-scan.js +33 -0
  145. package/dist/secret-values.d.ts +17 -0
  146. package/dist/secret-values.js +81 -0
  147. package/dist/self-upgrade.d.ts +36 -0
  148. package/dist/self-upgrade.js +78 -0
  149. package/dist/service.d.ts +7 -1
  150. package/dist/service.js +50 -7
  151. package/dist/sign-in.d.ts +41 -0
  152. package/dist/sign-in.js +239 -0
  153. package/dist/tool-call.d.ts +59 -0
  154. package/dist/tool-call.js +375 -0
  155. package/dist/trail/chain.d.ts +12 -0
  156. package/dist/trail/chain.js +89 -0
  157. package/dist/trail/deltas.d.ts +34 -0
  158. package/dist/trail/deltas.js +206 -0
  159. package/dist/trail/index.d.ts +27 -0
  160. package/dist/trail/index.js +128 -0
  161. package/dist/trail/keys.d.ts +14 -0
  162. package/dist/trail/keys.js +61 -0
  163. package/dist/trail/recorder.d.ts +118 -0
  164. package/dist/trail/recorder.js +697 -0
  165. package/dist/trail/rules.d.ts +30 -0
  166. package/dist/trail/rules.js +158 -0
  167. package/dist/trail/stream.d.ts +41 -0
  168. package/dist/trail/stream.js +86 -0
  169. package/dist/transport.d.ts +26 -0
  170. package/dist/transport.js +118 -0
  171. package/dist/work-dir.d.ts +11 -0
  172. package/dist/work-dir.js +69 -0
  173. package/dist/worktree-report.d.ts +10 -0
  174. package/dist/worktree-report.js +17 -0
  175. package/dist/worktrees.d.ts +16 -1
  176. package/dist/worktrees.js +62 -17
  177. package/package.json +4 -4
  178. package/skill/firefunc/SKILL.md +4 -3
@@ -0,0 +1,17 @@
1
+ import { type ChildProcess } from 'node:child_process';
2
+ export type KeepAwakeDeps = {
3
+ platform: NodeJS.Platform;
4
+ pid: number;
5
+ spawn: (cmd: string, args: string[]) => ChildProcess | null;
6
+ log: (line: string) => void;
7
+ };
8
+ export declare function windowsKeepAwakeScript(pid: number): string;
9
+ export declare function keepAwakeCommand(platform: NodeJS.Platform, pid: number): {
10
+ cmd: string;
11
+ args: string[];
12
+ } | null;
13
+ export declare function keepAwake(d: KeepAwakeDeps): {
14
+ hold: () => void;
15
+ release: () => void;
16
+ };
17
+ export declare function defaultKeepAwakeDeps(log: (line: string) => void): KeepAwakeDeps;
@@ -0,0 +1,90 @@
1
+ import { spawn } from 'node:child_process';
2
+ export function windowsKeepAwakeScript(pid) {
3
+ return ('Add-Type -Name P -Namespace W -MemberDefinition \'[DllImport("kernel32.dll")] public static extern uint SetThreadExecutionState(uint f);\';' +
4
+ ' [W.P]::SetThreadExecutionState([uint32]2147483649) | Out-Null;' +
5
+ ` while (Get-Process -Id ${Math.trunc(pid)} -ErrorAction SilentlyContinue) { Start-Sleep -Seconds 30 }`);
6
+ }
7
+ export function keepAwakeCommand(platform, pid) {
8
+ if (platform === 'darwin')
9
+ return { cmd: 'caffeinate', args: ['-i', '-w', String(pid)] };
10
+ if (platform === 'win32') {
11
+ return {
12
+ cmd: 'powershell.exe',
13
+ args: [
14
+ '-NoProfile',
15
+ '-NonInteractive',
16
+ '-WindowStyle',
17
+ 'Hidden',
18
+ '-EncodedCommand',
19
+ Buffer.from(windowsKeepAwakeScript(pid), 'utf16le').toString('base64'),
20
+ ],
21
+ };
22
+ }
23
+ if (platform === 'linux') {
24
+ return {
25
+ cmd: 'systemd-inhibit',
26
+ args: [
27
+ '--what=idle:sleep',
28
+ '--who=firefunc-runner',
29
+ '--why=A FireFunc run is in progress',
30
+ '--mode=block',
31
+ 'sh',
32
+ '-c',
33
+ `while kill -0 ${pid} 2>/dev/null && kill -0 $PPID 2>/dev/null; do sleep 5; done`,
34
+ ],
35
+ };
36
+ }
37
+ return null;
38
+ }
39
+ export function keepAwake(d) {
40
+ let held = 0;
41
+ let child = null;
42
+ let toldMissing = false;
43
+ const start = () => {
44
+ const c = keepAwakeCommand(d.platform, d.pid);
45
+ if (!c)
46
+ return;
47
+ try {
48
+ child = d.spawn(c.cmd, c.args);
49
+ }
50
+ catch {
51
+ child = null;
52
+ }
53
+ child?.on('error', () => {
54
+ child = null;
55
+ if (toldMissing)
56
+ return;
57
+ toldMissing = true;
58
+ d.log(`keep-awake: ${c.cmd} is not available here, so this machine may sleep during a run — keep it awake yourself, or install ${c.cmd}.`);
59
+ });
60
+ };
61
+ const stop = () => {
62
+ try {
63
+ child?.kill();
64
+ }
65
+ catch {
66
+ }
67
+ child = null;
68
+ };
69
+ return {
70
+ hold: () => {
71
+ held += 1;
72
+ if (held === 1)
73
+ start();
74
+ },
75
+ release: () => {
76
+ held = Math.max(0, held - 1);
77
+ if (held === 0)
78
+ stop();
79
+ },
80
+ };
81
+ }
82
+ export function defaultKeepAwakeDeps(log) {
83
+ return {
84
+ platform: process.platform,
85
+ pid: process.pid,
86
+ spawn: (cmd, args) => spawn(cmd, args, { stdio: 'ignore', windowsHide: true }),
87
+ log,
88
+ };
89
+ }
90
+ //# sourceMappingURL=keep-awake.js.map
@@ -1,12 +1,13 @@
1
1
  export declare const CODE_NOTE = "code kept on the machine";
2
2
  export declare function codeNote(ref: string | null): string;
3
+ export declare function textCarriesSecret(text: string): boolean;
3
4
  export declare function stripCode(text: string): string;
4
5
  export declare const UNNAMED_COMMAND = "a command";
5
6
  export declare function commandName(command: string): string;
6
7
  export declare function setupCommandName(command: string): string;
7
8
  export declare function errorLine(stderr: string, sendable?: (line: string) => boolean): string | null;
8
9
  export declare function foldHome(text: string, home: string): string;
9
- export declare function lastWords(output: string): string | null;
10
+ export declare function lastWords(output: string, sendable?: (line: string) => boolean): string | null;
10
11
  export declare function setupReason(stderr: string, stdout: string): string | null;
11
12
  export declare function pushRefusal(stderr: string): string | null;
12
13
  export declare function promisesLaterCheck(text: string): boolean;
@@ -89,6 +89,9 @@ const carriesSecret = (inner) => inner.length <= 1000 &&
89
89
  SECRET_ARG.test(inner) ||
90
90
  SECRET_CLI.test(inner) ||
91
91
  SECRET_ENV.test(inner));
92
+ export function textCarriesSecret(text) {
93
+ return carriesSecret(text);
94
+ }
92
95
  function inlineSpan(whole, inner) {
93
96
  const named = /^[\w@./-]+(?::\d+)?$/.test(inner.trim());
94
97
  const code = inner.includes('\n') ||
@@ -626,7 +629,7 @@ export function foldHome(text, home) {
626
629
  const escaped = root.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
627
630
  return text.replace(new RegExp(`${escaped}(?![\\w-])`, 'g'), '~');
628
631
  }
629
- export function lastWords(output) {
632
+ export function lastWords(output, sendable = () => true) {
630
633
  const lines = output
631
634
  .replace(ANSI, '')
632
635
  .split(/\r?\n/)
@@ -646,7 +649,7 @@ export function lastWords(output) {
646
649
  continue;
647
650
  if (isCodeLine(raw) || /[;{]\s*$/.test(line) || (/^\s/.test(raw) && CODE_SHAPE.test(raw)))
648
651
  continue;
649
- if (!plainWords(line))
652
+ if (!plainWords(line) || !sendable(line))
650
653
  continue;
651
654
  const kept = stripCode(line).trim();
652
655
  if (kept && !kept.includes(`[${CODE_NOTE}`))
@@ -655,8 +658,10 @@ export function lastWords(output) {
655
658
  return null;
656
659
  }
657
660
  export function setupReason(stderr, stdout) {
658
- return errorLine(stderr, plainWords) ?? lastWords(stderr) ?? lastWords(stdout);
661
+ const said = (line) => plainWords(line) && !WARNING_LINE.test(line);
662
+ return errorLine(stderr, said) ?? lastWords(stderr, said) ?? lastWords(stdout, said);
659
663
  }
664
+ const WARNING_LINE = /^\s*(?:\[?(?:warn(?:ing)?)\]?[:\s]|[\w@./-]+\s+(?:warn(?:ing)?)\b[:\s]|\S+:\s*warn(?:ing)?:)/i;
660
665
  export function pushRefusal(stderr) {
661
666
  const kept = stderr
662
667
  .split(/\r?\n/)
package/dist/limit.d.ts CHANGED
@@ -5,8 +5,16 @@ export type PlanLimit = {
5
5
  said?: string;
6
6
  };
7
7
  export declare function readClaudeLimit(stdout: string, now?: Date): PlanLimit | null;
8
+ export declare function readCodexLimit(stdout: string, now?: Date): PlanLimit | null;
9
+ export declare function codexResetFrom(text: string, now?: Date): string | null;
10
+ export declare function readGeminiLimit(stdout: string, now?: Date): PlanLimit | null;
11
+ export declare function geminiResetFrom(text: string, now?: Date): string | null;
12
+ export declare function readCopilotLimit(stdout: string, now?: Date, stderr?: string): PlanLimit | null;
13
+ export declare function readWordedLimit(endingError: string | null, stderr: string, now?: Date): PlanLimit | null;
14
+ export declare function readCursorLimit(stdout: string, now?: Date, stderr?: string): PlanLimit | null;
15
+ export declare function readAntigravityLimit(stdout: string, now?: Date, stderr?: string): PlanLimit | null;
8
16
  export declare function resetFromSentence(text: string, now?: Date): string | null;
9
- export declare function limitSentence(engineName: string, limit: PlanLimit, kept: boolean): string;
17
+ export declare function limitSentence(engineName: string, limit: PlanLimit, kept: boolean | 'session' | 'worktree'): string;
10
18
  export type ActiveLimit = {
11
19
  engine: string;
12
20
  resetsAt: string | null;
@@ -20,6 +28,7 @@ export declare class LimitGate {
20
28
  private readonly file;
21
29
  private readonly now;
22
30
  private limits;
31
+ waive: (engine: string) => boolean;
23
32
  constructor(file: string | null, now?: () => number);
24
33
  static fileIn(runnerDir: string): string;
25
34
  set(engine: string, limit: PlanLimit): ActiveLimit;
package/dist/limit.js CHANGED
@@ -75,6 +75,261 @@ export function readClaudeLimit(stdout, now = new Date()) {
75
75
  return null;
76
76
  }
77
77
  }
78
+ export function readCodexLimit(stdout, now = new Date()) {
79
+ try {
80
+ let failure = null;
81
+ for (const line of stdout.split('\n')) {
82
+ const t = line.trim();
83
+ if (!t.startsWith('{'))
84
+ continue;
85
+ if (!t.includes('"type":"turn.failed"') &&
86
+ !t.includes('"type":"error"') &&
87
+ !t.includes('"type":"turn.completed"')) {
88
+ continue;
89
+ }
90
+ let ev;
91
+ try {
92
+ ev = JSON.parse(t);
93
+ }
94
+ catch {
95
+ continue;
96
+ }
97
+ if (!isObj(ev))
98
+ continue;
99
+ if (ev.type === 'turn.completed') {
100
+ failure = null;
101
+ }
102
+ else if (ev.type === 'turn.failed' || ev.type === 'error') {
103
+ const msg = typeof ev.message === 'string'
104
+ ? ev.message
105
+ : isObj(ev.error) && typeof ev.error.message === 'string'
106
+ ? ev.error.message
107
+ : null;
108
+ if (msg && (!failure || !CODEX_LIMIT_WORDS.test(failure)))
109
+ failure = msg.trim();
110
+ }
111
+ }
112
+ if (!failure || !CODEX_LIMIT_WORDS.test(failure))
113
+ return null;
114
+ return {
115
+ kind: 'plan',
116
+ resetsAt: codexResetFrom(failure, now),
117
+ said: failure.slice(0, 200),
118
+ };
119
+ }
120
+ catch {
121
+ return null;
122
+ }
123
+ }
124
+ const CODEX_LIMIT_WORDS = /\bhit your usage limit\b/i;
125
+ export function codexResetFrom(text, now = new Date()) {
126
+ const rel = /\btry again in\s+((?:\d+\s*(?:days?|hours?|minutes?|mins?|seconds?|secs?)[\s,]*(?:and\s+)?)+)/i.exec(text);
127
+ if (rel?.[1]) {
128
+ let ms = 0;
129
+ for (const m of rel[1].matchAll(/(\d+)\s*(day|hour|min|sec)/gi)) {
130
+ const n = Number(m[1]);
131
+ const unit = m[2].toLowerCase();
132
+ ms +=
133
+ n *
134
+ (unit === 'day'
135
+ ? 86_400_000
136
+ : unit === 'hour'
137
+ ? 3_600_000
138
+ : unit === 'min'
139
+ ? 60_000
140
+ : 1000);
141
+ }
142
+ return ms > 0 ? new Date(now.getTime() + ms).toISOString() : null;
143
+ }
144
+ const at = /\btry again at\s+(?:(?<mon>[A-Za-z]{3,9})\.?\s+(?<day>\d{1,2})(?:st|nd|rd|th)?,?\s+(?:\d{4},?\s+)?)?(?<time>\d{1,2}:\d{2}\s*[AaPp][Mm])/.exec(text);
145
+ if (!at?.groups)
146
+ return null;
147
+ const g = at.groups;
148
+ const sentence = g.mon && g.day ? `resets ${g.mon} ${g.day}, ${g.time}` : `resets ${g.time}`;
149
+ return resetFromSentence(sentence, now);
150
+ }
151
+ export function readGeminiLimit(stdout, now = new Date()) {
152
+ try {
153
+ let error = null;
154
+ const take = (ev) => {
155
+ if (!isObj(ev))
156
+ return;
157
+ if (isObj(ev.error))
158
+ error = ev.error;
159
+ else if (typeof ev.response === 'string' && ev.response.trim())
160
+ error = null;
161
+ };
162
+ const whole = stdout.trim();
163
+ let parsed = false;
164
+ if (whole.startsWith('{')) {
165
+ try {
166
+ take(JSON.parse(whole));
167
+ parsed = true;
168
+ }
169
+ catch {
170
+ }
171
+ }
172
+ if (!parsed) {
173
+ for (const line of stdout.split('\n')) {
174
+ const t = line.trim();
175
+ if (!t.startsWith('{') || !t.includes('"error"'))
176
+ continue;
177
+ try {
178
+ take(JSON.parse(t));
179
+ }
180
+ catch {
181
+ }
182
+ }
183
+ }
184
+ const e = error;
185
+ if (!e)
186
+ return null;
187
+ const type = typeof e.type === 'string' ? e.type : '';
188
+ const message = typeof e.message === 'string' ? e.message.trim() : '';
189
+ if (type === 'RetryableQuotaError')
190
+ return null;
191
+ if (type !== 'TerminalQuotaError' && !GEMINI_QUOTA_WORDS.test(message))
192
+ return null;
193
+ return {
194
+ kind: 'plan',
195
+ resetsAt: geminiResetFrom(message, now),
196
+ ...(/\bdaily\b/i.test(message) ? { window: 'daily' } : {}),
197
+ ...(message ? { said: message.slice(0, 200) } : {}),
198
+ };
199
+ }
200
+ catch {
201
+ return null;
202
+ }
203
+ }
204
+ const GEMINI_QUOTA_WORDS = /\bexhausted your (?:[\w-]+ ){0,2}quota\b|\bQUOTA_EXHAUSTED\b/i;
205
+ export function geminiResetFrom(text, now = new Date()) {
206
+ const delay = /\bplease retry in\s+([0-9.]+)\s*(ms|s)\b/i.exec(text) ??
207
+ /\bretry after\s+([0-9.]+)\s*(ms|s)\b/i.exec(text);
208
+ if (delay?.[1]) {
209
+ const n = Number(delay[1]);
210
+ if (Number.isFinite(n) && n > 0) {
211
+ const ms = delay[2].toLowerCase() === 'ms' ? n : n * 1000;
212
+ return new Date(now.getTime() + Math.ceil(ms)).toISOString();
213
+ }
214
+ }
215
+ if (/\bdaily\b/i.test(text)) {
216
+ const zone = 'America/Los_Angeles';
217
+ const today = wallParts(now.getTime(), zone);
218
+ const next = new Date(Date.UTC(today.y, today.mo, today.d + 1));
219
+ return new Date(zonedInstant(next.getUTCFullYear(), next.getUTCMonth(), next.getUTCDate(), 0, 0, zone)).toISOString();
220
+ }
221
+ return null;
222
+ }
223
+ export function readCopilotLimit(stdout, now = new Date(), stderr = '') {
224
+ try {
225
+ const tail = (text) => text
226
+ .split('\n')
227
+ .map((l) => l.trim())
228
+ .filter(Boolean)
229
+ .slice(-4)
230
+ .join('\n');
231
+ const ending = `${tail(stderr)}\n${tail(stdout)}`;
232
+ const said = COPILOT_QUOTA_WORDS.exec(ending);
233
+ if (!said)
234
+ return null;
235
+ const line = ending.split('\n').find((l) => COPILOT_QUOTA_WORDS.test(l)) ?? said[0];
236
+ void now;
237
+ return { kind: 'plan', resetsAt: null, said: line.slice(0, 200) };
238
+ }
239
+ catch {
240
+ return null;
241
+ }
242
+ }
243
+ const COPILOT_QUOTA_WORDS = /\b402\b[^\n]{0,40}\bno quota\b|\bquota_exceeded\b|\bQuota exceeded\b/i;
244
+ export function readWordedLimit(endingError, stderr, now = new Date()) {
245
+ try {
246
+ const tail = stderr
247
+ .split('\n')
248
+ .map((l) => l.trim())
249
+ .filter(Boolean)
250
+ .slice(-4)
251
+ .join('\n');
252
+ const text = [endingError ?? '', tail].filter(Boolean).join('\n');
253
+ if (!text || !WORDED_LIMIT.test(text))
254
+ return null;
255
+ const line = text.split('\n').find((l) => WORDED_LIMIT.test(l)) ?? text;
256
+ return {
257
+ kind: 'plan',
258
+ resetsAt: codexResetFrom(text, now) ?? resetFromSentence(text, now),
259
+ said: line.slice(0, 200),
260
+ };
261
+ }
262
+ catch {
263
+ return null;
264
+ }
265
+ }
266
+ const WORDED_LIMIT = /\b(?:usage|rate|quota|credit|spend(?:ing)?) limit\b|\bhit your (?:[\w-]+ ){0,2}limit\b|\bquota (?:exceeded|exhausted)\b|\bexhausted your (?:[\w-]+ ){0,2}quota\b|\bresource[_ ]exhausted\b|\bout of (?:credits|quota)\b|\btoo many requests\b|\b429\b/i;
267
+ export function readCursorLimit(stdout, now = new Date(), stderr = '') {
268
+ let error = null;
269
+ let succeeded = false;
270
+ try {
271
+ for (const line of stdout.split('\n')) {
272
+ const t = line.trim();
273
+ if (!t.startsWith('{') || !t.includes('"result"'))
274
+ continue;
275
+ try {
276
+ const ev = JSON.parse(t);
277
+ if (ev.type !== 'result')
278
+ continue;
279
+ const failed = ev.is_error === true || (typeof ev.subtype === 'string' && ev.subtype !== 'success');
280
+ succeeded = !failed;
281
+ error = failed
282
+ ? typeof ev.result === 'string'
283
+ ? ev.result
284
+ : typeof ev.error === 'string'
285
+ ? ev.error
286
+ : ''
287
+ : null;
288
+ }
289
+ catch {
290
+ }
291
+ }
292
+ }
293
+ catch {
294
+ error = null;
295
+ }
296
+ if (succeeded)
297
+ return null;
298
+ return readWordedLimit(error, stderr, now);
299
+ }
300
+ export function readAntigravityLimit(stdout, now = new Date(), stderr = '') {
301
+ let error = null;
302
+ let succeeded = false;
303
+ try {
304
+ for (const line of stdout.split('\n')) {
305
+ const t = line.trim();
306
+ if (!t.startsWith('{') || !t.includes('"status"'))
307
+ continue;
308
+ try {
309
+ const ev = JSON.parse(t);
310
+ if (typeof ev.status !== 'string')
311
+ continue;
312
+ succeeded = ev.status === 'SUCCESS';
313
+ error =
314
+ ev.status === 'ERROR'
315
+ ? typeof ev.error === 'string'
316
+ ? ev.error
317
+ : isObj(ev.error) && typeof ev.error.message === 'string'
318
+ ? ev.error.message
319
+ : ''
320
+ : null;
321
+ }
322
+ catch {
323
+ }
324
+ }
325
+ }
326
+ catch {
327
+ error = null;
328
+ }
329
+ if (succeeded)
330
+ return null;
331
+ return readWordedLimit(error, stderr, now);
332
+ }
78
333
  const MONTHS = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'];
79
334
  function knownZone(zone) {
80
335
  if (!zone)
@@ -163,9 +418,11 @@ export function limitSentence(engineName, limit, kept) {
163
418
  return (`${engineName} was stopped by the plan's usage limit — ${when}. ` +
164
419
  `The runner pushed nothing from this attempt and claims no new work for ${engineName} ` +
165
420
  `until the reset. ` +
166
- (kept
167
- ? 'When the task runs again after the reset, this machine continues the same session from where it stopped instead of starting over.'
168
- : 'Run the task again after the reset.'));
421
+ (kept === 'worktree'
422
+ ? 'Its work is kept on this machine: when the task runs again after the reset, a new session goes on from it instead of starting over.'
423
+ : kept
424
+ ? 'When the task runs again after the reset, this machine continues the same session from where it stopped instead of starting over.'
425
+ : 'Run the task again after the reset.'));
169
426
  }
170
427
  export const UNKNOWN_RESET_WAIT_MS = 15 * 60_000;
171
428
  export const RESET_GRACE_MS = 2 * 60_000;
@@ -173,6 +430,7 @@ export class LimitGate {
173
430
  file;
174
431
  now;
175
432
  limits = new Map();
433
+ waive = () => false;
176
434
  constructor(file, now = () => Date.now()) {
177
435
  this.file = file;
178
436
  this.now = now;
@@ -222,11 +480,13 @@ export class LimitGate {
222
480
  }
223
481
  isLimited(engine) {
224
482
  this.prune();
483
+ if (this.waive(engine))
484
+ return null;
225
485
  return this.limits.get(engine) ?? null;
226
486
  }
227
487
  active() {
228
488
  this.prune();
229
- return [...this.limits.values()];
489
+ return [...this.limits.values()].filter((l) => !this.waive(l.engine));
230
490
  }
231
491
  prune() {
232
492
  const nowMs = this.now();
@@ -0,0 +1,19 @@
1
+ export type DeclaredCheck = {
2
+ key: string;
3
+ command: string;
4
+ };
5
+ export type LocalCheckRun = {
6
+ key: string;
7
+ ran: boolean;
8
+ ok: boolean;
9
+ fresh: boolean;
10
+ };
11
+ export type LocalChecksReport = {
12
+ checks: LocalCheckRun[];
13
+ };
14
+ export declare function checkWords(command: string): string;
15
+ export declare function makeLocalChecksReader(declared: readonly DeclaredCheck[]): {
16
+ feed(chunk: string): void;
17
+ report(): LocalChecksReport | null;
18
+ };
19
+ export declare function declaredChecksOf(raw: unknown): DeclaredCheck[];
@@ -0,0 +1,79 @@
1
+ import { commandName } from './keep-local.js';
2
+ import { makeTrailStreamParser } from './trail/stream.js';
3
+ const EDIT_TOOLS = new Set(['Edit', 'MultiEdit', 'Write', 'NotebookEdit']);
4
+ export function checkWords(command) {
5
+ let c = command.trim().replace(/\s+/g, ' ');
6
+ c = c.replace(/^cd \S+ && /, '');
7
+ c = c.replace(/ 2>&1$/, '');
8
+ return c;
9
+ }
10
+ export function makeLocalChecksReader(declared) {
11
+ const wanted = declared
12
+ .map((d) => ({ key: d.key, name: commandName(d.command), words: checkWords(d.command) }))
13
+ .filter((d) => d.name && d.name !== 'a command');
14
+ const pending = new Map();
15
+ const last = new Map();
16
+ const onEvent = (ev) => {
17
+ if (ev.kind === 'tool_use') {
18
+ if (EDIT_TOOLS.has(ev.name)) {
19
+ for (const run of last.values())
20
+ run.fresh = false;
21
+ return;
22
+ }
23
+ if (ev.name !== 'Bash' || !ev.input.command || ev.input.run_in_background)
24
+ return;
25
+ const name = commandName(ev.input.command);
26
+ const words = checkWords(ev.input.command);
27
+ const hit = wanted.find((w) => w.name === name && w.words === words);
28
+ if (!hit)
29
+ return;
30
+ pending.set(ev.id, hit.key);
31
+ last.set(hit.key, { id: ev.id, ok: null, fresh: true });
32
+ }
33
+ else if (ev.kind === 'tool_result') {
34
+ const key = pending.get(ev.id);
35
+ if (!key)
36
+ return;
37
+ pending.delete(ev.id);
38
+ const run = last.get(key);
39
+ if (run && run.id === ev.id)
40
+ run.ok = !ev.isError;
41
+ }
42
+ };
43
+ const parse = makeTrailStreamParser(onEvent);
44
+ return {
45
+ feed(chunk) {
46
+ try {
47
+ parse(chunk);
48
+ }
49
+ catch {
50
+ }
51
+ },
52
+ report() {
53
+ if (declared.length === 0)
54
+ return null;
55
+ return {
56
+ checks: declared.map((d) => {
57
+ const run = last.get(d.key);
58
+ return {
59
+ key: d.key,
60
+ ran: run !== undefined && run.ok !== null,
61
+ ok: run?.ok === true,
62
+ fresh: run?.fresh === true,
63
+ };
64
+ }),
65
+ };
66
+ },
67
+ };
68
+ }
69
+ export function declaredChecksOf(raw) {
70
+ if (!Array.isArray(raw))
71
+ return [];
72
+ return raw.flatMap((c) => {
73
+ const o = c;
74
+ return o && typeof o.key === 'string' && typeof o.command === 'string' && o.command.trim()
75
+ ? [{ key: o.key.slice(0, 40), command: o.command.slice(0, 200) }]
76
+ : [];
77
+ });
78
+ }
79
+ //# sourceMappingURL=local-checks.js.map
package/dist/locks.d.ts CHANGED
@@ -4,15 +4,35 @@ export type KeyedMutex = {
4
4
  export declare function createKeyedMutex(): KeyedMutex;
5
5
  export declare function repoLockKey(repo: string): string;
6
6
  export declare function heavyLockDir(runnerDir: string, repo: string): string;
7
+ export declare function heavySlotsFor(machine?: {
8
+ cores: number;
9
+ memBytes: number;
10
+ }): number;
11
+ export declare function heavySlots(setting?: number | null): number;
12
+ export declare function heavySlotDir(lockDir: string, i: number): string;
13
+ export declare function heavyQueueDir(lockDir: string): string;
14
+ export declare function heavyTag(runId: string): string;
15
+ export type HeavyWait = {
16
+ tag: string;
17
+ place: number;
18
+ running: number;
19
+ since: number;
20
+ };
21
+ export declare function heavyWaits(runnerDir: string): HeavyWait[];
7
22
  export type HeavyLockOptions = {
8
23
  label?: string;
24
+ slots?: number;
9
25
  pollMs?: number;
10
26
  maxWaitMs?: number;
11
- onWait?: (holder: number | null) => void;
27
+ onWait?: (holder: number | null, ahead: number) => void;
28
+ tag?: string;
12
29
  now?: () => number;
13
30
  sleep?: (ms: number) => Promise<void>;
14
31
  };
15
32
  export declare function acquireHeavyLock(lockDir: string, opts?: HeavyLockOptions): Promise<() => void>;
16
33
  export declare function withHeavyLock<T>(lockDir: string, fn: () => Promise<T>, opts?: HeavyLockOptions): Promise<T>;
17
34
  export declare function heavyScript(): string;
35
+ export declare function deviceLockDir(runnerDir: string): string;
36
+ export declare function deviceScript(): string;
37
+ export declare function ensureDeviceScript(runnerDir: string): string | null;
18
38
  export declare function ensureHeavyScript(runnerDir: string): string | null;