@xzzpig/pi-subagents 0.24.1 → 0.25.0

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 (263) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/bundle/agent-management.js +4 -4
  3. package/bundle/agents.js +3 -3
  4. package/bundle/child-tool-plan.js +2 -2
  5. package/bundle/chunk-34TUUBRN.js +1107 -0
  6. package/bundle/{chunk-SW2VMNIU.js → chunk-5OD3OWTE.js} +2 -2
  7. package/bundle/{chunk-ZWXU5SPQ.js → chunk-5YEOWNSD.js} +371 -135
  8. package/bundle/{chunk-EUPJB4S7.js → chunk-5Z7JAORM.js} +196 -1219
  9. package/bundle/{chunk-LCDJWBVM.js → chunk-66JNFRFS.js} +1 -1
  10. package/bundle/{chunk-6BJUEES3.js → chunk-6MNRKUHY.js} +23 -12
  11. package/bundle/{chunk-ED55MIC2.js → chunk-CGFF4WA3.js} +42 -19
  12. package/bundle/{chunk-TMHVDPYW.js → chunk-DLVPMML3.js} +263 -154
  13. package/bundle/chunk-HXRB42AF.js +108 -0
  14. package/bundle/{chunk-355ECEP2.js → chunk-IH7LQVEE.js} +29 -14
  15. package/bundle/{chunk-SLNMBXLL.js → chunk-IJWITCC6.js} +411 -249
  16. package/bundle/{chunk-RU522D5W.js → chunk-IKFKH5EL.js} +369 -50
  17. package/bundle/{chunk-Z6TFJ6BR.js → chunk-INWSUNI6.js} +1 -1
  18. package/bundle/{chunk-JEA2ZKUA.js → chunk-IUDEI6FJ.js} +35 -200
  19. package/bundle/{chunk-WW76JRIX.js → chunk-JAXVBXGF.js} +1 -1
  20. package/bundle/{chunk-ICNYIALB.js → chunk-JH5H4CYJ.js} +784 -474
  21. package/bundle/{chunk-35GII5EF.js → chunk-JWFLPSTP.js} +41 -117
  22. package/bundle/{chunk-LMGQAJ3O.js → chunk-KCJFGKVK.js} +350 -123
  23. package/bundle/{chunk-RB4GELQA.js → chunk-LPJEKHOY.js} +1 -1
  24. package/bundle/chunk-LYKG4FKH.js +523 -0
  25. package/bundle/{chunk-X6LOM3Z4.js → chunk-LZ3HBDTR.js} +161 -78
  26. package/bundle/{chunk-FG6SQJAR.js → chunk-OGPOCGWX.js} +24 -6
  27. package/bundle/{chunk-Y6H2G6KX.js → chunk-OSVXFWPG.js} +67 -30
  28. package/bundle/{chunk-FJWI64H7.js → chunk-RMSABUN3.js} +27 -13
  29. package/bundle/{chunk-H36HBZB2.js → chunk-SJPABBZG.js} +2 -2
  30. package/bundle/{chunk-3U633XDZ.js → chunk-U3ZDUUTM.js} +11 -3
  31. package/bundle/{chunk-LVWU2HOO.js → chunk-ULGMXABL.js} +16 -7
  32. package/bundle/{chunk-WPW4V6HM.js → chunk-W7ZA6TYD.js} +2 -2
  33. package/bundle/{chunk-V2KFIGFH.js → chunk-XAC2XNAP.js} +503 -304
  34. package/bundle/{chunk-TGLFTQWJ.js → chunk-YWXILP5N.js} +5 -4
  35. package/bundle/control-channel.js +2 -2
  36. package/bundle/index.js +1 -1
  37. package/bundle/intercom-bridge.js +2 -2
  38. package/bundle/preflight.js +12 -10
  39. package/bundle/project-panes.js +3 -3
  40. package/bundle/workflow-resources.js +1 -1
  41. package/docs/agents.md +27 -6
  42. package/docs/configuration.md +20 -8
  43. package/docs/extension-api.md +18 -1
  44. package/docs/models.md +6 -2
  45. package/docs/observability.md +2 -0
  46. package/docs/tool-reference.md +7 -1
  47. package/docs/workflows.md +30 -2
  48. package/node_modules/@xzzpig/pi-components/CHANGELOG.md +12 -0
  49. package/node_modules/@xzzpig/pi-components/README.md +94 -0
  50. package/node_modules/@xzzpig/pi-components/dist/src/transcript-presenter.d.ts +186 -0
  51. package/node_modules/@xzzpig/pi-components/dist/src/transcript-presenter.js +693 -0
  52. package/node_modules/@xzzpig/pi-components/package.json +5 -1
  53. package/package.json +4 -4
  54. package/skills/pi-subagents/SKILL.md +15 -6
  55. package/skills/pi-subagents/references/prompting-and-roles.md +1 -0
  56. package/src/agents/agent-management.d.ts +1 -0
  57. package/src/agents/agent-management.js +46 -18
  58. package/src/agents/agent-memory.d.ts +1 -4
  59. package/src/agents/agent-memory.js +1 -4
  60. package/src/agents/agent-serializer.d.ts +2 -0
  61. package/src/agents/agent-serializer.js +1 -1
  62. package/src/agents/agents.d.ts +2 -3
  63. package/src/agents/agents.js +46 -20
  64. package/src/agents/frontmatter.js +1 -8
  65. package/src/agents/global-npm-root.d.ts +1 -0
  66. package/src/agents/global-npm-root.js +11 -3
  67. package/src/agents/skills.d.ts +0 -3
  68. package/src/agents/skills.js +0 -3
  69. package/src/api/preflight.d.ts +3 -1
  70. package/src/api/preflight.js +2 -0
  71. package/src/extension/config.d.ts +1 -0
  72. package/src/extension/config.js +13 -1
  73. package/src/extension/herdr-pi-bridge.js +10 -2
  74. package/src/extension/index.d.ts +0 -13
  75. package/src/extension/index.js +51 -37
  76. package/src/extension/public-execution.d.ts +1 -0
  77. package/src/extension/public-execution.js +4 -0
  78. package/src/extension/reply-workflow-script.js +24 -4
  79. package/src/extension/schemas.d.ts +12 -3
  80. package/src/extension/schemas.js +7 -3
  81. package/src/extension/session-switch.d.ts +25 -0
  82. package/src/extension/session-switch.js +138 -0
  83. package/src/extension/subagent-guide.d.ts +1 -1
  84. package/src/extension/subagent-guide.js +1 -1
  85. package/src/extension/subagent-launch-tool.d.ts +25 -0
  86. package/src/extension/subagent-launch-tool.js +48 -0
  87. package/src/extension/tool-activation.js +55 -15
  88. package/src/extension/tool-description.d.ts +1 -1
  89. package/src/extension/tool-description.js +14 -8
  90. package/src/inspectors/herdr/actions.d.ts +0 -1
  91. package/src/inspectors/herdr/actions.js +0 -1
  92. package/src/inspectors/session-roots-codec.d.ts +0 -1
  93. package/src/inspectors/session-roots-codec.js +0 -1
  94. package/src/inspectors/tmux/actions.d.ts +0 -1
  95. package/src/inspectors/tmux/actions.js +0 -1
  96. package/src/integrations/herdr-status.js +3 -8
  97. package/src/intercom/native-supervisor-channel.js +65 -9
  98. package/src/runs/background/async-execution.d.ts +7 -10
  99. package/src/runs/background/async-execution.js +66 -29
  100. package/src/runs/background/async-job-tracker.js +12 -0
  101. package/src/runs/background/async-resume.d.ts +2 -1
  102. package/src/runs/background/async-resume.js +114 -40
  103. package/src/runs/background/async-retention.d.ts +0 -2
  104. package/src/runs/background/async-retention.js +17 -6
  105. package/src/runs/background/async-status.d.ts +2 -0
  106. package/src/runs/background/async-status.js +4 -0
  107. package/src/runs/background/auto-drain.js +2 -1
  108. package/src/runs/background/boot-id.d.ts +1 -3
  109. package/src/runs/background/boot-id.js +1 -3
  110. package/src/runs/background/completion-batcher.d.ts +3 -4
  111. package/src/runs/background/completion-batcher.js +2 -3
  112. package/src/runs/background/control-channel.d.ts +0 -6
  113. package/src/runs/background/control-channel.js +0 -6
  114. package/src/runs/background/fleet-view.js +2 -3
  115. package/src/runs/background/notify.d.ts +0 -2
  116. package/src/runs/background/notify.js +0 -2
  117. package/src/runs/background/result-watcher.d.ts +1 -0
  118. package/src/runs/background/result-watcher.js +59 -29
  119. package/src/runs/background/resume-guidance.js +4 -2
  120. package/src/runs/background/retained-children.js +2 -1
  121. package/src/runs/background/run-child-session.d.ts +2 -3
  122. package/src/runs/background/run-child-session.js +5 -8
  123. package/src/runs/background/run-status.js +9 -7
  124. package/src/runs/background/runner-aliases.js +15 -0
  125. package/src/runs/background/scheduled-runs.d.ts +3 -1
  126. package/src/runs/background/scheduled-runs.js +13 -16
  127. package/src/runs/background/steering.d.ts +2 -11
  128. package/src/runs/background/steering.js +11 -6
  129. package/src/runs/background/subagent-runner.d.ts +2 -6
  130. package/src/runs/background/subagent-runner.js +88 -70
  131. package/src/runs/background/subagent-wait.js +12 -15
  132. package/src/runs/background/wait-config.d.ts +2 -0
  133. package/src/runs/background/wait-config.js +17 -3
  134. package/src/runs/background/wait-subscriptions.js +124 -12
  135. package/src/runs/background/wait-tool.js +4 -2
  136. package/src/runs/background/workflow-terminal-proof.d.ts +0 -1
  137. package/src/runs/background/workflow-terminal-proof.js +0 -1
  138. package/src/runs/foreground/async-steering-action.js +24 -9
  139. package/src/runs/foreground/async-stop-action.d.ts +1 -1
  140. package/src/runs/foreground/async-stop-action.js +9 -1
  141. package/src/runs/foreground/execution.d.ts +0 -3
  142. package/src/runs/foreground/execution.js +56 -24
  143. package/src/runs/foreground/prompt-audit.js +1 -0
  144. package/src/runs/foreground/subagent-executor.d.ts +5 -10
  145. package/src/runs/foreground/subagent-executor.js +164 -49
  146. package/src/runs/shared/acceptance.js +69 -26
  147. package/src/runs/shared/async-status-projection.d.ts +2 -0
  148. package/src/runs/shared/async-status-projection.js +10 -0
  149. package/src/runs/shared/child-commands.js +15 -2
  150. package/src/runs/shared/child-hooks.d.ts +1 -7
  151. package/src/runs/shared/child-hooks.js +13 -5
  152. package/src/runs/shared/child-launch.js +6 -2
  153. package/src/runs/shared/child-runtime-config.d.ts +0 -1
  154. package/src/runs/shared/child-session.d.ts +6 -5
  155. package/src/runs/shared/child-session.js +85 -38
  156. package/src/runs/shared/child-tool-plan.d.ts +0 -1
  157. package/src/runs/shared/child-tool-plan.js +0 -1
  158. package/src/runs/shared/claude-code-adapter.js +11 -0
  159. package/src/runs/shared/external-cli-contract.d.ts +49 -0
  160. package/src/runs/shared/external-cli-contract.js +18 -13
  161. package/src/runs/shared/external-cli-runner.js +26 -6
  162. package/src/runs/shared/herdr-connection.js +2 -1
  163. package/src/runs/shared/herdr-pane-host.d.ts +2 -2
  164. package/src/runs/shared/herdr-pane-host.js +17 -1
  165. package/src/runs/shared/herdr-placed-run.js +51 -24
  166. package/src/runs/shared/lane-metadata.d.ts +0 -1
  167. package/src/runs/shared/lane-metadata.js +0 -1
  168. package/src/runs/shared/mcp-config-sources.d.ts +4 -1
  169. package/src/runs/shared/mcp-config-sources.js +1 -1
  170. package/src/runs/shared/mcp-direct-tool-allowlist.d.ts +0 -3
  171. package/src/runs/shared/mcp-direct-tool-allowlist.js +26 -17
  172. package/src/runs/shared/mcp-direct-tool-grant.js +8 -1
  173. package/src/runs/shared/model-resolution-diagnostic.d.ts +0 -1
  174. package/src/runs/shared/model-resolution.d.ts +20 -5
  175. package/src/runs/shared/model-resolution.js +55 -50
  176. package/src/runs/shared/model-scope.d.ts +1 -5
  177. package/src/runs/shared/model-scope.js +1 -5
  178. package/src/runs/shared/mutation-evidence.d.ts +1 -1
  179. package/src/runs/shared/mutation-evidence.js +52 -7
  180. package/src/runs/shared/nested-events.d.ts +1 -0
  181. package/src/runs/shared/nested-events.js +1 -1
  182. package/src/runs/shared/parallel-utils.d.ts +0 -1
  183. package/src/runs/shared/permissions.js +5 -3
  184. package/src/runs/shared/placement-host.d.ts +5 -1
  185. package/src/runs/shared/rex-pane-host.d.ts +0 -1
  186. package/src/runs/shared/rex-pane-host.js +0 -2
  187. package/src/runs/shared/run-launch-folder.d.ts +12 -0
  188. package/src/runs/shared/run-launch-folder.js +21 -0
  189. package/src/runs/shared/runner-launcher.d.ts +12 -1
  190. package/src/runs/shared/runner-launcher.js +16 -0
  191. package/src/runs/shared/single-output.d.ts +3 -3
  192. package/src/runs/shared/single-output.js +5 -3
  193. package/src/runs/shared/structured-output.d.ts +8 -0
  194. package/src/runs/shared/structured-output.js +40 -5
  195. package/src/runs/shared/subagent-prompt-runtime.d.ts +0 -1
  196. package/src/runs/shared/subagent-prompt-runtime.js +18 -4
  197. package/src/runs/shared/supervisor-wait-timer.d.ts +16 -0
  198. package/src/runs/shared/supervisor-wait-timer.js +61 -0
  199. package/src/runs/shared/tool-timeout.js +2 -1
  200. package/src/runs/shared/worktree-cleanup-plan.d.ts +0 -1
  201. package/src/runs/shared/worktree-cleanup-plan.js +0 -1
  202. package/src/runs/shared/worktree.d.ts +3 -0
  203. package/src/runs/shared/worktree.js +20 -0
  204. package/src/shared/artifacts.d.ts +1 -0
  205. package/src/shared/artifacts.js +132 -22
  206. package/src/shared/capacity-resilient-json.d.ts +1 -1
  207. package/src/shared/child-cache-retention.d.ts +1 -2
  208. package/src/shared/child-cache-retention.js +1 -2
  209. package/src/shared/fleet-transcript-reader.d.ts +51 -0
  210. package/src/shared/fleet-transcript-reader.js +333 -0
  211. package/src/shared/formatters.d.ts +0 -18
  212. package/src/shared/formatters.js +0 -18
  213. package/src/shared/pruned-fork.js +2 -2
  214. package/src/shared/reported-progress.d.ts +1 -0
  215. package/src/shared/reported-progress.js +12 -0
  216. package/src/shared/session-succession.d.ts +16 -1
  217. package/src/shared/session-succession.js +41 -2
  218. package/src/shared/session-warnings.d.ts +7 -0
  219. package/src/shared/session-warnings.js +26 -0
  220. package/src/shared/settings.d.ts +0 -23
  221. package/src/shared/settings.js +3 -35
  222. package/src/shared/status-format.d.ts +1 -0
  223. package/src/shared/status-format.js +1 -1
  224. package/src/shared/types.d.ts +108 -59
  225. package/src/shared/types.js +0 -12
  226. package/src/shared/utils.d.ts +3 -15
  227. package/src/shared/utils.js +4 -22
  228. package/src/shared/wait-tool-name.d.ts +8 -0
  229. package/src/shared/wait-tool-name.js +13 -0
  230. package/src/slash/slash-commands.js +2 -1
  231. package/src/slash/subagent-cost.d.ts +3 -2
  232. package/src/slash/subagent-cost.js +22 -3
  233. package/src/slash/subagents-admin.js +0 -1
  234. package/src/tui/fleet-status.d.ts +0 -5
  235. package/src/tui/fleet-status.js +4 -24
  236. package/src/tui/fleet.js +6 -4
  237. package/src/tui/render.d.ts +0 -9
  238. package/src/tui/render.js +194 -68
  239. package/src/tui/subagent-messages.js +3 -2
  240. package/src/watchdog/model-selection.js +2 -2
  241. package/src/watchdog/permission-arbiter.js +1 -0
  242. package/src/watchdog/register-child.d.ts +2 -3
  243. package/src/watchdog/register-child.js +2 -3
  244. package/src/watchdog/review.d.ts +0 -5
  245. package/src/watchdog/review.js +4 -3
  246. package/src/watchdog/turn-delta.js +2 -1
  247. package/src/workflows/scripted-workflow.d.ts +2 -0
  248. package/src/workflows/scripted-workflow.js +344 -59
  249. package/src/workflows/structured-workflow-scripts.d.ts +4 -3
  250. package/src/workflows/structured-workflow-scripts.js +32 -11
  251. package/src/workflows/workflow-launch-record.d.ts +15 -6
  252. package/src/workflows/workflow-launch-record.js +30 -12
  253. package/src/workflows/workflow-receipt.js +36 -53
  254. package/src/workflows/workflow-resources.js +4 -4
  255. package/src/workflows/workflow-resume-claim.d.ts +2 -0
  256. package/src/workflows/workflow-resume-claim.js +3 -3
  257. package/src/workflows/workflow-resume.d.ts +1 -1
  258. package/src/workflows/workflow-resume.js +13 -8
  259. package/src/workflows/workflow-script-worker.d.ts +13 -0
  260. package/src/workflows/workflow-script-worker.js +81 -0
  261. package/workflow-script-process.cjs +36 -0
  262. package/workflow-script-worker.cjs +4 -1
  263. package/bundle/chunk-U4ODUYAG.js +0 -235
@@ -1,235 +0,0 @@
1
- import {
2
- getAgentDir
3
- } from "./chunk-V2KFIGFH.js";
4
- import {
5
- TEMP_ARTIFACTS_DIR
6
- } from "./chunk-UQDHXMUG.js";
7
-
8
- // src/shared/artifacts.ts
9
- import * as fs from "node:fs";
10
- import * as path from "node:path";
11
- var CLEANUP_MARKER_FILE = ".last-cleanup";
12
- var PROJECT_SUBAGENTS_RELATIVE_DIR = ".pi/subagents";
13
- var PROJECT_ARTIFACT_PATHS = [
14
- `${PROJECT_SUBAGENTS_RELATIVE_DIR}/artifacts/output.md`,
15
- `${PROJECT_SUBAGENTS_RELATIVE_DIR}/artifacts/run_worker_input.md`,
16
- `${PROJECT_SUBAGENTS_RELATIVE_DIR}/artifacts/run_worker_output.md`,
17
- `${PROJECT_SUBAGENTS_RELATIVE_DIR}/artifacts/run_worker.jsonl`,
18
- `${PROJECT_SUBAGENTS_RELATIVE_DIR}/artifacts/run_worker_transcript.jsonl`,
19
- `${PROJECT_SUBAGENTS_RELATIVE_DIR}/artifacts/run_worker_meta.json`,
20
- `${PROJECT_SUBAGENTS_RELATIVE_DIR}/artifacts/progress/run/progress.md`,
21
- `${PROJECT_SUBAGENTS_RELATIVE_DIR}/artifacts/outputs/output.md`,
22
- `${PROJECT_SUBAGENTS_RELATIVE_DIR}/artifacts/outputs/run/output.md`,
23
- `${PROJECT_SUBAGENTS_RELATIVE_DIR}/chain-runs/run.json`
24
- ];
25
- function globMatchesPath(pattern, filePath) {
26
- let expression = "^";
27
- for (let index = 0; index < pattern.length; index += 1) {
28
- const character = pattern[index];
29
- if (character === void 0) break;
30
- if (character === "*" && pattern[index + 1] === "*") {
31
- index += 1;
32
- if (pattern[index + 1] === "/") {
33
- index += 1;
34
- expression += "(?:.*/)?";
35
- } else {
36
- expression += ".*";
37
- }
38
- } else if (character === "*") {
39
- expression += "[^/]*";
40
- } else if (character === "?") {
41
- expression += "[^/]";
42
- } else if (character === "[") {
43
- const end = pattern.indexOf("]", index + 1);
44
- if (end === -1) expression += "\\[";
45
- else {
46
- expression += pattern.slice(index, end + 1);
47
- index = end;
48
- }
49
- } else {
50
- expression += character.replace(/[|\\{}()[\]^$+?.]/g, "\\$&");
51
- }
52
- }
53
- try {
54
- return new RegExp(`${expression}$`).test(filePath);
55
- } catch {
56
- return false;
57
- }
58
- }
59
- function normalizePattern(pattern) {
60
- return pattern.replace(/^\.\//, "").replace(/^\//, "").replace(/\/+$/, "");
61
- }
62
- function patternMatchesArtifactPath(pattern, artifactPath) {
63
- const normalized = normalizePattern(pattern);
64
- return normalized === PROJECT_SUBAGENTS_RELATIVE_DIR || normalized === "*" || artifactPath.startsWith(`${normalized}/`) || globMatchesPath(normalized, artifactPath);
65
- }
66
- function ignoreFileExcludesProjectArtifacts(filePath) {
67
- if (!fs.existsSync(filePath)) return false;
68
- try {
69
- const excluded = /* @__PURE__ */ new Set();
70
- for (const rawLine of fs.readFileSync(filePath, "utf-8").split(/\r?\n/)) {
71
- const line = rawLine.trim();
72
- if (!line || line.startsWith("#")) continue;
73
- const negated = line.startsWith("!");
74
- const pattern = negated ? line.slice(1) : line;
75
- for (const artifactPath of PROJECT_ARTIFACT_PATHS) {
76
- if (!patternMatchesArtifactPath(pattern, artifactPath)) continue;
77
- if (negated) excluded.delete(artifactPath);
78
- else excluded.add(artifactPath);
79
- }
80
- }
81
- return excluded.size === PROJECT_ARTIFACT_PATHS.length;
82
- } catch {
83
- return false;
84
- }
85
- }
86
- function filesIncludeProjectArtifacts(files) {
87
- if (!Array.isArray(files)) return void 0;
88
- const included = /* @__PURE__ */ new Set();
89
- for (const entry of files) {
90
- if (typeof entry !== "string") continue;
91
- const negated = entry.startsWith("!");
92
- const pattern = negated ? entry.slice(1) : entry;
93
- for (const artifactPath of PROJECT_ARTIFACT_PATHS) {
94
- if (!patternMatchesArtifactPath(pattern, artifactPath)) continue;
95
- if (negated) included.delete(artifactPath);
96
- else included.add(artifactPath);
97
- }
98
- }
99
- return included.size > 0;
100
- }
101
- function getProjectArtifactPackagingWarning(cwd) {
102
- const packagePath = path.join(cwd, "package.json");
103
- if (!fs.existsSync(packagePath)) return void 0;
104
- let packageJson;
105
- try {
106
- const parsed = JSON.parse(fs.readFileSync(packagePath, "utf-8"));
107
- if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return void 0;
108
- packageJson = parsed;
109
- } catch {
110
- return void 0;
111
- }
112
- const filesIncludeArtifacts = filesIncludeProjectArtifacts(packageJson.files);
113
- if (filesIncludeArtifacts === false) return void 0;
114
- const npmIgnorePath = path.join(cwd, ".npmignore");
115
- const ignorePath = fs.existsSync(npmIgnorePath) ? npmIgnorePath : path.join(cwd, ".gitignore");
116
- if (filesIncludeArtifacts === void 0 && ignoreFileExcludesProjectArtifacts(ignorePath)) return void 0;
117
- return "Project-scoped subagent artifacts can be included when this package is published. Add '.pi/subagents/' to .npmignore, restrict package.json files, or set artifactDir to 'session' or 'temp'.";
118
- }
119
- function getProjectSubagentsDir(cwd) {
120
- return path.join(cwd, PROJECT_SUBAGENTS_RELATIVE_DIR);
121
- }
122
- function getProjectArtifactsDir(cwd) {
123
- return path.join(getProjectSubagentsDir(cwd), "artifacts");
124
- }
125
- function getArtifactsDir(sessionFile, projectCwd, dirPreference = "session") {
126
- switch (dirPreference) {
127
- case "session":
128
- if (sessionFile) {
129
- const sessionDir = path.dirname(sessionFile);
130
- return path.join(sessionDir, "subagent-artifacts");
131
- }
132
- return TEMP_ARTIFACTS_DIR;
133
- case "temp":
134
- return TEMP_ARTIFACTS_DIR;
135
- case "project":
136
- if (projectCwd) return getProjectArtifactsDir(projectCwd);
137
- if (sessionFile) {
138
- const sessionDir = path.dirname(sessionFile);
139
- return path.join(sessionDir, "subagent-artifacts");
140
- }
141
- return TEMP_ARTIFACTS_DIR;
142
- default:
143
- throw new Error(`Unsupported artifactDir ${JSON.stringify(dirPreference)}; expected "project", "session", or "temp".`);
144
- }
145
- }
146
- function getArtifactPaths(artifactsDir, runId, agent, index) {
147
- const suffix = index !== void 0 ? `_${index}` : "";
148
- const safeAgent = agent.replace(/[^\w.-]/g, "_");
149
- const base = `${runId}_${safeAgent}${suffix}`;
150
- return {
151
- inputPath: path.join(artifactsDir, `${base}_input.md`),
152
- outputPath: path.join(artifactsDir, `${base}_output.md`),
153
- jsonlPath: path.join(artifactsDir, `${base}.jsonl`),
154
- transcriptPath: path.join(artifactsDir, `${base}_transcript.jsonl`),
155
- metadataPath: path.join(artifactsDir, `${base}_meta.json`)
156
- };
157
- }
158
- function ensureArtifactsDir(dir) {
159
- fs.mkdirSync(dir, { recursive: true });
160
- }
161
- function writeArtifact(filePath, content) {
162
- fs.mkdirSync(path.dirname(filePath), { recursive: true });
163
- fs.writeFileSync(filePath, content, "utf-8");
164
- }
165
- function formatOutputArtifactContent(input) {
166
- if (input.output.trim() || !input.error) return input.output;
167
- const lines = ["Subagent run failed before producing output.", "", "Error:", input.error];
168
- if (input.transcriptPath) lines.push("", `Transcript: ${input.transcriptPath}`);
169
- if (input.metadataPath) lines.push(`Metadata: ${input.metadataPath}`);
170
- return lines.join("\n");
171
- }
172
- function writeMetadata(filePath, metadata) {
173
- fs.mkdirSync(path.dirname(filePath), { recursive: true });
174
- fs.writeFileSync(filePath, JSON.stringify(metadata, null, 2), "utf-8");
175
- }
176
- function appendJsonl(filePath, line) {
177
- fs.appendFileSync(filePath, `${line}
178
- `);
179
- }
180
- function cleanupOldArtifacts(dir, maxAgeDays) {
181
- if (maxAgeDays <= 0 || !fs.existsSync(dir)) return;
182
- const markerPath = path.join(dir, CLEANUP_MARKER_FILE);
183
- const now = Date.now();
184
- if (fs.existsSync(markerPath)) {
185
- const stat = fs.statSync(markerPath);
186
- if (now - stat.mtimeMs < 24 * 60 * 60 * 1e3) return;
187
- }
188
- const maxAgeMs = maxAgeDays * 24 * 60 * 60 * 1e3;
189
- const cutoff = now - maxAgeMs;
190
- for (const file of fs.readdirSync(dir)) {
191
- if (file === CLEANUP_MARKER_FILE) continue;
192
- const filePath = path.join(dir, file);
193
- try {
194
- const stat = fs.statSync(filePath);
195
- if (stat.mtimeMs < cutoff) {
196
- fs.unlinkSync(filePath);
197
- }
198
- } catch {
199
- }
200
- }
201
- fs.writeFileSync(markerPath, String(now));
202
- }
203
- function cleanupAllArtifactDirs(maxAgeDays) {
204
- cleanupOldArtifacts(TEMP_ARTIFACTS_DIR, maxAgeDays);
205
- const sessionsBase = path.join(getAgentDir(), "sessions");
206
- if (!fs.existsSync(sessionsBase)) return;
207
- let dirs;
208
- try {
209
- dirs = fs.readdirSync(sessionsBase);
210
- } catch {
211
- return;
212
- }
213
- for (const dir of dirs) {
214
- const artifactsDir = path.join(sessionsBase, dir, "subagent-artifacts");
215
- try {
216
- cleanupOldArtifacts(artifactsDir, maxAgeDays);
217
- } catch {
218
- }
219
- }
220
- }
221
-
222
- export {
223
- PROJECT_SUBAGENTS_RELATIVE_DIR,
224
- getProjectArtifactPackagingWarning,
225
- getProjectSubagentsDir,
226
- getArtifactsDir,
227
- getArtifactPaths,
228
- ensureArtifactsDir,
229
- writeArtifact,
230
- formatOutputArtifactContent,
231
- writeMetadata,
232
- appendJsonl,
233
- cleanupOldArtifacts,
234
- cleanupAllArtifactDirs
235
- };