@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,30 @@
1
+ export declare const LIMITS: {
2
+ readonly path: 300;
3
+ readonly label: 120;
4
+ readonly words: 300;
5
+ readonly files: 200;
6
+ readonly hunks: 200;
7
+ readonly plan: 50;
8
+ readonly argv: 30;
9
+ };
10
+ export declare const WITHHELD_PATH = "[path withheld]";
11
+ export declare const WITHHELD_WORDS = "[words withheld]";
12
+ export type Category = 'lifecycle' | 'data' | 'control' | 'network' | 'policy' | 'approval';
13
+ export declare const TYPE_CATEGORY: Record<string, Category>;
14
+ export declare function isStorablePath(p: string): boolean;
15
+ export declare function storablePath(p: string): string;
16
+ export declare function argvShapeOf(words: readonly string[]): string[];
17
+ export declare function flagsOfCommand(command: string): string[];
18
+ export declare function storableWords(text: string, max?: number): string | null;
19
+ export type SensitiveClass = 'secret_file' | 'ci_config' | 'migration' | 'lockfile' | 'dependency_manifest' | 'infra';
20
+ export declare function sensitiveClassOf(path: string): SensitiveClass | undefined;
21
+ export declare function isTestPath(path: string): boolean;
22
+ export type Risk = 'low' | 'med' | 'high';
23
+ export declare function riskOf(type: string, opts?: {
24
+ sensitive?: boolean;
25
+ write?: boolean;
26
+ }): Risk;
27
+ export declare function hostOf(url: string): {
28
+ host: string;
29
+ port?: number;
30
+ } | null;
@@ -0,0 +1,158 @@
1
+ import { stripCode, textCarriesSecret } from '../keep-local.js';
2
+ export const LIMITS = {
3
+ path: 300,
4
+ label: 120,
5
+ words: 300,
6
+ files: 200,
7
+ hunks: 200,
8
+ plan: 50,
9
+ argv: 30,
10
+ };
11
+ export const WITHHELD_PATH = '[path withheld]';
12
+ export const WITHHELD_WORDS = '[words withheld]';
13
+ export const TYPE_CATEGORY = {
14
+ 'run.started': 'lifecycle',
15
+ 'setup.ran': 'lifecycle',
16
+ 'engine.started': 'lifecycle',
17
+ 'engine.ended': 'lifecycle',
18
+ 'run.ended': 'lifecycle',
19
+ gap: 'lifecycle',
20
+ 'file.read': 'data',
21
+ 'file.edited': 'data',
22
+ 'file.changed': 'data',
23
+ search: 'data',
24
+ 'skill.read': 'data',
25
+ exec: 'control',
26
+ 'tool.call': 'control',
27
+ 'subagent.started': 'control',
28
+ 'plan.updated': 'control',
29
+ 'git.commit': 'control',
30
+ 'git.push': 'control',
31
+ 'pr.opened': 'control',
32
+ 'web.fetch': 'network',
33
+ 'web.search': 'network',
34
+ 'policy.decision': 'policy',
35
+ 'approval.requested': 'approval',
36
+ 'approval.answered': 'approval',
37
+ 'question.asked': 'approval',
38
+ 'question.answered': 'approval',
39
+ 'plan.approved': 'approval',
40
+ 'criterion.reported': 'approval',
41
+ 'rollback.action': 'control',
42
+ };
43
+ export function isStorablePath(p) {
44
+ if (!p || p.length > LIMITS.path)
45
+ return false;
46
+ if (/[\n\r\t\0]/.test(p))
47
+ return false;
48
+ if (/^(?:\/|[A-Za-z]:[\\/]|~)/.test(p))
49
+ return false;
50
+ if (p.startsWith('…/'))
51
+ return true;
52
+ return !textCarriesSecret(p);
53
+ }
54
+ export function storablePath(p) {
55
+ return isStorablePath(p) ? p : WITHHELD_PATH;
56
+ }
57
+ export function argvShapeOf(words) {
58
+ const out = [];
59
+ for (const w of words) {
60
+ const m = /^(--?[A-Za-z][\w-]{0,40})(?:=.*)?$/.exec(w);
61
+ if (m)
62
+ out.push(m[1]);
63
+ if (out.length >= LIMITS.argv)
64
+ break;
65
+ }
66
+ return out;
67
+ }
68
+ export function flagsOfCommand(command) {
69
+ const words = command.replace(/'[^']*'|"(?:\\.|[^"\\])*"/g, ' ').split(/\s+/);
70
+ return [...new Set(argvShapeOf(words))];
71
+ }
72
+ export function storableWords(text, max = LIMITS.label) {
73
+ const one = stripCode(text).replace(/\s+/g, ' ').trim();
74
+ if (!one)
75
+ return null;
76
+ if (textCarriesSecret(one))
77
+ return WITHHELD_WORDS;
78
+ return one.length > max ? `${one.slice(0, max - 1)}…` : one;
79
+ }
80
+ export function sensitiveClassOf(path) {
81
+ const p = path.replace(/\\/g, '/').toLowerCase();
82
+ const base = p.split('/').pop() ?? p;
83
+ if (/^\.env(?:\..+)?$/.test(base) ||
84
+ /\.(?:pem|key|p12|pfx|jks|keystore)$/.test(base) ||
85
+ /^id_(?:rsa|dsa|ecdsa|ed25519)$/.test(base) ||
86
+ /(?:^|\/)(?:secrets?|credentials?)(?:\/|$)/.test(p) ||
87
+ base === '.npmrc' ||
88
+ base === '.netrc')
89
+ return 'secret_file';
90
+ if (p.startsWith('.github/workflows/') ||
91
+ base === '.gitlab-ci.yml' ||
92
+ p.startsWith('.circleci/') ||
93
+ p.startsWith('.buildkite/') ||
94
+ base === 'jenkinsfile' ||
95
+ base === 'azure-pipelines.yml')
96
+ return 'ci_config';
97
+ if (/(?:^|\/)(?:migrations?|migrate|db\/migrate|alembic\/versions)\//.test(p))
98
+ return 'migration';
99
+ if ([
100
+ 'package-lock.json',
101
+ 'pnpm-lock.yaml',
102
+ 'yarn.lock',
103
+ 'bun.lockb',
104
+ 'cargo.lock',
105
+ 'go.sum',
106
+ 'poetry.lock',
107
+ 'pipfile.lock',
108
+ 'gemfile.lock',
109
+ 'composer.lock',
110
+ 'uv.lock',
111
+ ].includes(base))
112
+ return 'lockfile';
113
+ if ([
114
+ 'package.json',
115
+ 'cargo.toml',
116
+ 'go.mod',
117
+ 'pyproject.toml',
118
+ 'requirements.txt',
119
+ 'gemfile',
120
+ 'composer.json',
121
+ 'pom.xml',
122
+ 'build.gradle',
123
+ ].includes(base))
124
+ return 'dependency_manifest';
125
+ if (/\.(?:tf|tfvars|hcl)$/.test(base) ||
126
+ base === 'dockerfile' ||
127
+ /^docker-compose.*\.ya?ml$/.test(base) ||
128
+ /(?:^|\/)(?:k8s|kubernetes|helm|charts|infrastructure|infra)\//.test(p))
129
+ return 'infra';
130
+ return undefined;
131
+ }
132
+ export function isTestPath(path) {
133
+ return /(?:^|\/)(?:__tests__|tests?|spec|e2e)\/|\.(?:test|spec)\.[a-z0-9]+$|_test\.(?:go|py)$|(?:^|\/)test_[^/]+\.py$/i.test(path);
134
+ }
135
+ export function riskOf(type, opts = {}) {
136
+ if (type === 'git.push' || type === 'pr.opened' || type === 'rollback.action')
137
+ return 'high';
138
+ if (type === 'tool.call')
139
+ return opts.write ? 'high' : 'low';
140
+ if (type === 'file.edited' || type === 'file.changed' || type === 'exec')
141
+ return opts.sensitive ? 'high' : 'med';
142
+ if (type === 'web.fetch')
143
+ return 'med';
144
+ return 'low';
145
+ }
146
+ const HOST = /^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)*[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/i;
147
+ export function hostOf(url) {
148
+ try {
149
+ const u = new URL(url);
150
+ if (!HOST.test(u.hostname))
151
+ return null;
152
+ return { host: u.hostname, ...(u.port ? { port: Number(u.port) } : {}) };
153
+ }
154
+ catch {
155
+ return null;
156
+ }
157
+ }
158
+ //# sourceMappingURL=rules.js.map
@@ -0,0 +1,41 @@
1
+ export type ToolInput = {
2
+ file_path?: string;
3
+ notebook_path?: string;
4
+ path?: string;
5
+ command?: string;
6
+ run_in_background?: boolean;
7
+ url?: string;
8
+ query?: boolean;
9
+ subagent_type?: string;
10
+ skill?: string;
11
+ todos?: Array<{
12
+ content: string;
13
+ status: string;
14
+ }>;
15
+ pattern_is_glob?: boolean;
16
+ };
17
+ export type TrailStreamEvent = {
18
+ kind: 'init';
19
+ model?: string;
20
+ version?: string;
21
+ } | {
22
+ kind: 'tool_use';
23
+ id: string;
24
+ name: string;
25
+ input: ToolInput;
26
+ parentId?: string;
27
+ } | {
28
+ kind: 'tool_result';
29
+ id: string;
30
+ isError: boolean;
31
+ parentId?: string;
32
+ } | {
33
+ kind: 'result';
34
+ durationMs?: number;
35
+ turns?: number;
36
+ isError?: boolean;
37
+ tokensIn?: number;
38
+ tokensOut?: number;
39
+ tokensCache?: number;
40
+ };
41
+ export declare function makeTrailStreamParser(onEvent: (ev: TrailStreamEvent) => void): (chunk: string) => void;
@@ -0,0 +1,86 @@
1
+ const str = (v) => (typeof v === 'string' && v ? v : undefined);
2
+ const num = (v) => typeof v === 'number' && Number.isFinite(v) && v >= 0 ? Math.round(v) : undefined;
3
+ function inputOf(raw) {
4
+ const todos = Array.isArray(raw.todos)
5
+ ? raw.todos.flatMap((t) => {
6
+ const o = t;
7
+ return o && typeof o.content === 'string' && typeof o.status === 'string'
8
+ ? [{ content: o.content, status: o.status }]
9
+ : [];
10
+ })
11
+ : undefined;
12
+ return {
13
+ ...(str(raw.file_path) ? { file_path: str(raw.file_path) } : {}),
14
+ ...(str(raw.notebook_path) ? { notebook_path: str(raw.notebook_path) } : {}),
15
+ ...(str(raw.path) ? { path: str(raw.path) } : {}),
16
+ ...(str(raw.command) ? { command: str(raw.command) } : {}),
17
+ ...(raw.run_in_background === true ? { run_in_background: true } : {}),
18
+ ...(str(raw.url) ? { url: str(raw.url) } : {}),
19
+ ...(str(raw.query) ? { query: true } : {}),
20
+ ...(str(raw.subagent_type) ? { subagent_type: str(raw.subagent_type) } : {}),
21
+ ...(str(raw.skill) ? { skill: str(raw.skill) } : {}),
22
+ ...(todos ? { todos } : {}),
23
+ };
24
+ }
25
+ export function makeTrailStreamParser(onEvent) {
26
+ let buf = '';
27
+ return (chunk) => {
28
+ buf += chunk;
29
+ let nl = buf.indexOf('\n');
30
+ while (nl >= 0) {
31
+ const raw = buf.slice(0, nl).trim();
32
+ buf = buf.slice(nl + 1);
33
+ nl = buf.indexOf('\n');
34
+ if (!raw || raw[0] !== '{')
35
+ continue;
36
+ let line;
37
+ try {
38
+ line = JSON.parse(raw);
39
+ }
40
+ catch {
41
+ continue;
42
+ }
43
+ const parentId = str(line.parent_tool_use_id);
44
+ if (line.type === 'system' && line.subtype === 'init') {
45
+ onEvent({ kind: 'init', model: str(line.model), version: str(line.claude_code_version) });
46
+ }
47
+ else if (line.type === 'assistant' && Array.isArray(line.message?.content)) {
48
+ for (const c of line.message.content) {
49
+ if (c?.type === 'tool_use' && typeof c.name === 'string' && typeof c.id === 'string') {
50
+ onEvent({
51
+ kind: 'tool_use',
52
+ id: c.id,
53
+ name: c.name,
54
+ input: inputOf((c.input ?? {})),
55
+ ...(parentId ? { parentId } : {}),
56
+ });
57
+ }
58
+ }
59
+ }
60
+ else if (line.type === 'user' && Array.isArray(line.message?.content)) {
61
+ for (const c of line.message.content) {
62
+ if (c?.type === 'tool_result' && typeof c.tool_use_id === 'string') {
63
+ onEvent({
64
+ kind: 'tool_result',
65
+ id: c.tool_use_id,
66
+ isError: c.is_error === true,
67
+ ...(parentId ? { parentId } : {}),
68
+ });
69
+ }
70
+ }
71
+ }
72
+ else if (line.type === 'result') {
73
+ onEvent({
74
+ kind: 'result',
75
+ durationMs: num(line.duration_ms),
76
+ turns: num(line.num_turns),
77
+ isError: line.is_error === true,
78
+ tokensIn: num(line.usage?.input_tokens),
79
+ tokensOut: num(line.usage?.output_tokens),
80
+ tokensCache: num(line.usage?.cache_read_input_tokens),
81
+ });
82
+ }
83
+ }
84
+ };
85
+ }
86
+ //# sourceMappingURL=stream.js.map
@@ -0,0 +1,26 @@
1
+ export type TransportState = {
2
+ failingSince: string | null;
3
+ failures: number;
4
+ lastError: string | null;
5
+ rebuilds: number;
6
+ lastRebuildAt: string | null;
7
+ };
8
+ export type TransportDeps = {
9
+ rebuild: () => boolean;
10
+ now: () => number;
11
+ log: (line: string) => void;
12
+ };
13
+ export declare function isTransportFailure(err: unknown): boolean;
14
+ export declare function describeTransportError(err: unknown): string;
15
+ export declare function rebuildGlobalDispatcher(graceMs?: number): boolean;
16
+ export declare class TransportWatchdog {
17
+ private readonly threshold;
18
+ private readonly deps;
19
+ private readonly state;
20
+ constructor(threshold: number, deps: TransportDeps);
21
+ ok(): void;
22
+ failed(err: unknown): boolean;
23
+ snapshot(): TransportState;
24
+ }
25
+ export declare function backoffAfterFailure(backoffMs: number, rebuilt: boolean, rebuildsThisOutage: number): number;
26
+ export declare const TRANSPORT_REBUILD_AFTER = 3;
@@ -0,0 +1,118 @@
1
+ import { isNetworkError } from './network.js';
2
+ const GLOBAL_DISPATCHER = Symbol.for('undici.globalDispatcher.1');
3
+ export function isTransportFailure(err) {
4
+ if (err instanceof Error && (err.name === 'AbortError' || err.name === 'TimeoutError')) {
5
+ return true;
6
+ }
7
+ if (isNetworkError(err))
8
+ return true;
9
+ const code = causeCode(err);
10
+ return !!code && /^(?:UND_ERR_|ECONN|EPIPE|ETIMEDOUT|ENOTFOUND|EAI_AGAIN|EHOST|ENET)/.test(code);
11
+ }
12
+ function causeCode(err) {
13
+ const cause = err?.cause;
14
+ if (!cause)
15
+ return null;
16
+ if (typeof cause.code === 'string')
17
+ return cause.code;
18
+ const first = Array.isArray(cause.errors) ? cause.errors[0]?.code : undefined;
19
+ return typeof first === 'string' ? first : null;
20
+ }
21
+ export function describeTransportError(err) {
22
+ const msg = err instanceof Error ? err.message : String(err ?? 'unknown error');
23
+ const code = causeCode(err);
24
+ const causeMsg = err?.cause?.message;
25
+ const under = code ?? (typeof causeMsg === 'string' && causeMsg ? causeMsg : null);
26
+ const text = under && !msg.includes(under) ? `${msg} (${under})` : msg;
27
+ return text.length > 200 ? `${text.slice(0, 199)}…` : text;
28
+ }
29
+ export function rebuildGlobalDispatcher(graceMs = 30_000) {
30
+ const g = globalThis;
31
+ const old = g[GLOBAL_DISPATCHER];
32
+ if (!old || typeof old.constructor !== 'function')
33
+ return false;
34
+ let next;
35
+ try {
36
+ next = new old.constructor();
37
+ }
38
+ catch {
39
+ return false;
40
+ }
41
+ g[GLOBAL_DISPATCHER] = next;
42
+ try {
43
+ void Promise.resolve(old.close?.()).catch(() => { });
44
+ }
45
+ catch {
46
+ }
47
+ const t = setTimeout(() => {
48
+ try {
49
+ void Promise.resolve(old.destroy?.()).catch(() => { });
50
+ }
51
+ catch {
52
+ }
53
+ }, graceMs);
54
+ t.unref?.();
55
+ return true;
56
+ }
57
+ export class TransportWatchdog {
58
+ threshold;
59
+ deps;
60
+ state = {
61
+ failingSince: null,
62
+ failures: 0,
63
+ lastError: null,
64
+ rebuilds: 0,
65
+ lastRebuildAt: null,
66
+ };
67
+ constructor(threshold, deps) {
68
+ this.threshold = threshold;
69
+ this.deps = deps;
70
+ if (!Number.isInteger(threshold) || threshold < 1) {
71
+ throw new Error(`transport watchdog threshold must be a positive integer (got ${threshold})`);
72
+ }
73
+ }
74
+ ok() {
75
+ if (this.state.failures > 0) {
76
+ const since = this.state.failingSince ? Date.parse(this.state.failingSince) : NaN;
77
+ const mins = Number.isNaN(since) ? null : Math.round((this.deps.now() - since) / 60_000);
78
+ this.deps.log(`FireFunc reachable again after ${this.state.failures} failed poll${this.state.failures === 1 ? '' : 's'}` +
79
+ (mins !== null ? ` (${mins} min)` : '') +
80
+ (this.state.rebuilds > 0
81
+ ? `; the connection was rebuilt ${this.state.rebuilds} time${this.state.rebuilds === 1 ? '' : 's'}`
82
+ : ''));
83
+ }
84
+ this.state.failures = 0;
85
+ this.state.failingSince = null;
86
+ this.state.lastError = null;
87
+ this.state.rebuilds = 0;
88
+ }
89
+ failed(err) {
90
+ if (!isTransportFailure(err))
91
+ return false;
92
+ const at = new Date(this.deps.now()).toISOString();
93
+ if (this.state.failures === 0)
94
+ this.state.failingSince = at;
95
+ this.state.failures += 1;
96
+ this.state.lastError = describeTransportError(err);
97
+ if (this.state.failures % this.threshold !== 0)
98
+ return false;
99
+ const replaced = this.deps.rebuild();
100
+ if (replaced) {
101
+ this.state.rebuilds += 1;
102
+ this.state.lastRebuildAt = at;
103
+ this.deps.log(`${this.state.failures} polls in a row failed (${this.state.lastError}) — dropped the HTTP connections and reconnecting with a fresh client`);
104
+ }
105
+ else {
106
+ this.deps.log(`${this.state.failures} polls in a row failed (${this.state.lastError}) — the HTTP client could not be rebuilt here; if this continues while FireFunc answers elsewhere, restart the runner (firefunc-runner stop, then firefunc-runner start-service)`);
107
+ }
108
+ return replaced;
109
+ }
110
+ snapshot() {
111
+ return { ...this.state };
112
+ }
113
+ }
114
+ export function backoffAfterFailure(backoffMs, rebuilt, rebuildsThisOutage) {
115
+ return rebuilt && rebuildsThisOutage === 1 ? 2_000 : backoffMs;
116
+ }
117
+ export const TRANSPORT_REBUILD_AFTER = 3;
118
+ //# sourceMappingURL=transport.js.map
@@ -0,0 +1,11 @@
1
+ export declare function setWorkDir(dir: string | null | undefined): void;
2
+ export declare function workDir(): string | null;
3
+ export declare function workRoot(): string;
4
+ export declare function workDirFailure(): string | null;
5
+ export declare function workDirProblem(): string | null;
6
+ export declare function workRoots(): string[];
7
+ export declare function workTempEnv(): Record<string, string>;
8
+ export declare function workDirWords(dir: string | undefined, disk: (dir: string) => {
9
+ freeBytes: number;
10
+ totalBytes: number;
11
+ } | null, problem?: string | null): string;
@@ -0,0 +1,69 @@
1
+ import { mkdirSync } from 'node:fs';
2
+ import { tmpdir } from 'node:os';
3
+ import { join } from 'node:path';
4
+ let configured = null;
5
+ let problem = null;
6
+ export function setWorkDir(dir) {
7
+ configured = typeof dir === 'string' && dir.trim() ? dir.trim() : null;
8
+ problem = null;
9
+ }
10
+ export function workDir() {
11
+ return configured;
12
+ }
13
+ export function workRoot() {
14
+ if (!configured)
15
+ return tmpdir();
16
+ try {
17
+ mkdirSync(configured, { recursive: true });
18
+ problem = null;
19
+ return configured;
20
+ }
21
+ catch (err) {
22
+ problem = err instanceof Error ? err.message : String(err);
23
+ return tmpdir();
24
+ }
25
+ }
26
+ export function workDirFailure() {
27
+ if (!configured)
28
+ return null;
29
+ workRoot();
30
+ return problem;
31
+ }
32
+ export function workDirProblem() {
33
+ const why = workDirFailure();
34
+ return why
35
+ ? `the work folder ${configured} cannot be made (${why}), so runs work in the OS temp folder ${tmpdir()} instead. Fix: connect the drive or pick a folder this user can write, with connect --work-dir <dir>.`
36
+ : null;
37
+ }
38
+ export function workRoots() {
39
+ const roots = [tmpdir()];
40
+ if (configured)
41
+ roots.push(configured, join(configured, 'tmp'));
42
+ return [...new Set(roots)];
43
+ }
44
+ export function workTempEnv() {
45
+ if (!configured)
46
+ return {};
47
+ const tmp = join(configured, 'tmp');
48
+ try {
49
+ mkdirSync(tmp, { recursive: true });
50
+ }
51
+ catch {
52
+ return {};
53
+ }
54
+ return { TMPDIR: tmp, TEMP: tmp, TMP: tmp };
55
+ }
56
+ export function workDirWords(dir, disk, problem = null) {
57
+ const where = dir?.trim() ? dir.trim() : tmpdir();
58
+ if (dir?.trim() && problem) {
59
+ const t = disk(tmpdir());
60
+ const tFree = t ? `${(t.freeBytes / 1024 ** 3).toFixed(1)} GB free` : 'free space unknown';
61
+ return `${where} cannot be made (${problem}) — runs work in ${tmpdir()} (${tFree}) instead; connect the drive or set another with connect --work-dir <dir>`;
62
+ }
63
+ const d = disk(where);
64
+ const free = d ? `${(d.freeBytes / 1024 ** 3).toFixed(1)} GB free` : 'free space unknown';
65
+ return dir?.trim()
66
+ ? `${where} (${free}) — worktrees and the engine's temp folder`
67
+ : `${where} (${free}) — the OS temp folder; set one with connect --work-dir <dir>`;
68
+ }
69
+ //# sourceMappingURL=work-dir.js.map
@@ -0,0 +1,10 @@
1
+ export type WorktreeState = 'live' | 'kept' | 'gone';
2
+ export type WorktreeReport = {
3
+ path: string;
4
+ state: WorktreeState;
5
+ };
6
+ export declare function worktreeReport(path: string | null | undefined, state: WorktreeState): WorktreeReport | null;
7
+ export declare function endedState(path: string, kept: boolean): 'kept' | 'gone';
8
+ export declare function reportWorktree(api: {
9
+ event: (runId: string, event: 'worktree', detail?: Record<string, unknown>) => Promise<void>;
10
+ }, runId: string, path: string | null | undefined, state: WorktreeState): Promise<void>;
@@ -0,0 +1,17 @@
1
+ import { existsSync } from 'node:fs';
2
+ import { isAbsolute } from 'node:path';
3
+ export function worktreeReport(path, state) {
4
+ if (!path || !isAbsolute(path) || path.length > 1024)
5
+ return null;
6
+ return { path, state };
7
+ }
8
+ export function endedState(path, kept) {
9
+ return kept && existsSync(path) ? 'kept' : 'gone';
10
+ }
11
+ export async function reportWorktree(api, runId, path, state) {
12
+ const detail = worktreeReport(path, state);
13
+ if (!detail)
14
+ return;
15
+ await api.event(runId, 'worktree', detail).catch(() => { });
16
+ }
17
+ //# sourceMappingURL=worktree-report.js.map
@@ -1,5 +1,10 @@
1
1
  export declare function canonicalPath(path: string): string;
2
- export declare function isRunnerWorktree(path: string): boolean;
2
+ export declare function canonicalPathOn(path: string, platform: NodeJS.Platform): string;
3
+ export declare function isRunnerWorktree(path: string, opts?: {
4
+ platform?: NodeJS.Platform;
5
+ tmp?: string;
6
+ roots?: readonly string[];
7
+ }): boolean;
3
8
  export type WorktreeEntry = {
4
9
  path: string;
5
10
  branch?: string;
@@ -7,6 +12,16 @@ export type WorktreeEntry = {
7
12
  };
8
13
  export declare function worktreesOf(checkout: string): Promise<WorktreeEntry[]>;
9
14
  export declare function staleHolder(stderr: string): string | null;
15
+ export type BranchHolder = {
16
+ kind: 'live';
17
+ runId: string | null;
18
+ } | {
19
+ kind: 'kept';
20
+ runId: string;
21
+ parked: boolean;
22
+ };
23
+ export declare function branchHolderOf(runnerDir: string, worktree: string): BranchHolder | null;
24
+ export declare function branchHeldSentence(branch: string, holder: BranchHolder): string;
10
25
  export declare function removeRunnerWorktree(checkout: string, path: string, log: (line: string) => void): Promise<void>;
11
26
  export declare function sweepLeftoverWorktrees(checkouts: string[], opts: {
12
27
  live: Set<string>;