@nexum-hub/agent 0.0.0-stage → 0.0.1

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 (343) hide show
  1. package/README.md +129 -2
  2. package/contracts/v2/common.schema.json +262 -0
  3. package/contracts/v2/fixtures/inputs.json +244 -0
  4. package/contracts/v2/fixtures/wire.json +6493 -0
  5. package/contracts/v2/model/agent.close.input.schema.json +14 -0
  6. package/contracts/v2/model/agent.close.output.schema.json +101 -0
  7. package/contracts/v2/model/agent.list.input.schema.json +57 -0
  8. package/contracts/v2/model/agent.list.output.schema.json +220 -0
  9. package/contracts/v2/model/agent.open.input.schema.json +35 -0
  10. package/contracts/v2/model/agent.open.output.schema.json +41 -0
  11. package/contracts/v2/model/agent.run.cancel.input.schema.json +18 -0
  12. package/contracts/v2/model/agent.run.cancel.output.schema.json +100 -0
  13. package/contracts/v2/model/agent.run.get.input.schema.json +41 -0
  14. package/contracts/v2/model/agent.run.get.output.schema.json +100 -0
  15. package/contracts/v2/model/agent.run.input.input.schema.json +30 -0
  16. package/contracts/v2/model/agent.run.input.output.schema.json +100 -0
  17. package/contracts/v2/model/agent.run.list.input.schema.json +47 -0
  18. package/contracts/v2/model/agent.run.list.output.schema.json +41 -0
  19. package/contracts/v2/model/agent.run.start.input.schema.json +73 -0
  20. package/contracts/v2/model/agent.run.start.output.schema.json +100 -0
  21. package/contracts/v2/wire/bodies.schema.json +383 -0
  22. package/contracts/v2/wire/envelope.schema.json +2280 -0
  23. package/contracts/v2/wire/events.schema.json +242 -0
  24. package/contracts/v2/wire/maintenance.schema.json +308 -0
  25. package/contracts/v2/wire/operations.schema.json +1791 -0
  26. package/contracts/v2/wire/stream.schema.json +74 -0
  27. package/contracts/v2/wire/types.schema.json +2616 -0
  28. package/dist/adapters/mcp-http.d.ts +22 -0
  29. package/dist/adapters/mcp-http.js +472 -0
  30. package/dist/adapters/mcp-http.js.map +1 -0
  31. package/dist/adapters/mcp-oauth.d.ts +29 -0
  32. package/dist/adapters/mcp-oauth.js +653 -0
  33. package/dist/adapters/mcp-oauth.js.map +1 -0
  34. package/dist/adapters/mcp-wire.d.ts +21 -0
  35. package/dist/adapters/mcp-wire.js +307 -0
  36. package/dist/adapters/mcp-wire.js.map +1 -0
  37. package/dist/adapters/mcp.d.ts +56 -0
  38. package/dist/adapters/mcp.js +1989 -0
  39. package/dist/adapters/mcp.js.map +1 -0
  40. package/dist/adapters/openapi-http.d.ts +29 -0
  41. package/dist/adapters/openapi-http.js +504 -0
  42. package/dist/adapters/openapi-http.js.map +1 -0
  43. package/dist/adapters/openapi-request.d.ts +26 -0
  44. package/dist/adapters/openapi-request.js +597 -0
  45. package/dist/adapters/openapi-request.js.map +1 -0
  46. package/dist/adapters/openapi-schema.d.ts +22 -0
  47. package/dist/adapters/openapi-schema.js +471 -0
  48. package/dist/adapters/openapi-schema.js.map +1 -0
  49. package/dist/adapters/openapi-typegen.d.ts +11 -0
  50. package/dist/adapters/openapi-typegen.js +185 -0
  51. package/dist/adapters/openapi-typegen.js.map +1 -0
  52. package/dist/adapters/openapi-types.d.ts +99 -0
  53. package/dist/adapters/openapi-types.js +2 -0
  54. package/dist/adapters/openapi-types.js.map +1 -0
  55. package/dist/adapters/openapi.d.ts +7 -0
  56. package/dist/adapters/openapi.js +768 -0
  57. package/dist/adapters/openapi.js.map +1 -0
  58. package/dist/application/activation.d.ts +28 -0
  59. package/dist/application/activation.js +289 -0
  60. package/dist/application/activation.js.map +1 -0
  61. package/dist/application/attachments.d.ts +24 -0
  62. package/dist/application/attachments.js +87 -0
  63. package/dist/application/attachments.js.map +1 -0
  64. package/dist/application/authority.d.ts +32 -0
  65. package/dist/application/authority.js +213 -0
  66. package/dist/application/authority.js.map +1 -0
  67. package/dist/application/catalog.d.ts +11 -0
  68. package/dist/application/catalog.js +177 -0
  69. package/dist/application/catalog.js.map +1 -0
  70. package/dist/application/config.d.ts +24 -0
  71. package/dist/application/config.js +140 -0
  72. package/dist/application/config.js.map +1 -0
  73. package/dist/application/connection.d.ts +35 -0
  74. package/dist/application/connection.js +465 -0
  75. package/dist/application/connection.js.map +1 -0
  76. package/dist/application/data-plane.d.ts +35 -0
  77. package/dist/application/data-plane.js +437 -0
  78. package/dist/application/data-plane.js.map +1 -0
  79. package/dist/application/diagnostics.d.ts +4 -0
  80. package/dist/application/diagnostics.js +65 -0
  81. package/dist/application/diagnostics.js.map +1 -0
  82. package/dist/application/dispatcher.d.ts +4 -0
  83. package/dist/application/dispatcher.js +16 -0
  84. package/dist/application/dispatcher.js.map +1 -0
  85. package/dist/application/main.d.ts +12 -0
  86. package/dist/application/main.js +167 -0
  87. package/dist/application/main.js.map +1 -0
  88. package/dist/application/maintenance.d.ts +27 -0
  89. package/dist/application/maintenance.js +244 -0
  90. package/dist/application/maintenance.js.map +1 -0
  91. package/dist/application/packages.d.ts +25 -0
  92. package/dist/application/packages.js +208 -0
  93. package/dist/application/packages.js.map +1 -0
  94. package/dist/application/processes.d.ts +49 -0
  95. package/dist/application/processes.js +180 -0
  96. package/dist/application/processes.js.map +1 -0
  97. package/dist/application/run-view.d.ts +17 -0
  98. package/dist/application/run-view.js +286 -0
  99. package/dist/application/run-view.js.map +1 -0
  100. package/dist/application/runtime.d.ts +78 -0
  101. package/dist/application/runtime.js +1747 -0
  102. package/dist/application/runtime.js.map +1 -0
  103. package/dist/application/tasks.d.ts +38 -0
  104. package/dist/application/tasks.js +23 -0
  105. package/dist/application/tasks.js.map +1 -0
  106. package/dist/application/user-state.d.ts +20 -0
  107. package/dist/application/user-state.js +52 -0
  108. package/dist/application/user-state.js.map +1 -0
  109. package/dist/application/work-git.d.ts +14 -0
  110. package/dist/application/work-git.js +67 -0
  111. package/dist/application/work-git.js.map +1 -0
  112. package/dist/cli.d.ts +2 -0
  113. package/dist/cli.js +40 -0
  114. package/dist/cli.js.map +1 -0
  115. package/dist/contracts/caller.d.ts +51 -0
  116. package/dist/contracts/caller.js +95 -0
  117. package/dist/contracts/caller.js.map +1 -0
  118. package/dist/contracts/error-data.d.ts +6 -0
  119. package/dist/contracts/error-data.js +51 -0
  120. package/dist/contracts/error-data.js.map +1 -0
  121. package/dist/contracts/errors.d.ts +10 -0
  122. package/dist/contracts/errors.js +28 -0
  123. package/dist/contracts/errors.js.map +1 -0
  124. package/dist/contracts/json.d.ts +9 -0
  125. package/dist/contracts/json.js +104 -0
  126. package/dist/contracts/json.js.map +1 -0
  127. package/dist/contracts/types.d.ts +122 -0
  128. package/dist/contracts/types.js +19 -0
  129. package/dist/contracts/types.js.map +1 -0
  130. package/dist/contracts/validation.d.ts +4 -0
  131. package/dist/contracts/validation.js +157 -0
  132. package/dist/contracts/validation.js.map +1 -0
  133. package/dist/contracts/wire-validation.d.ts +9 -0
  134. package/dist/contracts/wire-validation.js +399 -0
  135. package/dist/contracts/wire-validation.js.map +1 -0
  136. package/dist/contracts/wire.d.ts +1083 -0
  137. package/dist/contracts/wire.js +62 -0
  138. package/dist/contracts/wire.js.map +1 -0
  139. package/dist/extension-api.d.ts +12 -0
  140. package/dist/extension-api.js +2 -0
  141. package/dist/extension-api.js.map +1 -0
  142. package/dist/extensions/api.d.ts +72 -0
  143. package/dist/extensions/api.js +2 -0
  144. package/dist/extensions/api.js.map +1 -0
  145. package/dist/extensions/host-requests.d.ts +23 -0
  146. package/dist/extensions/host-requests.js +60 -0
  147. package/dist/extensions/host-requests.js.map +1 -0
  148. package/dist/extensions/materials.d.ts +37 -0
  149. package/dist/extensions/materials.js +691 -0
  150. package/dist/extensions/materials.js.map +1 -0
  151. package/dist/extensions/package-loader.d.ts +28 -0
  152. package/dist/extensions/package-loader.js +177 -0
  153. package/dist/extensions/package-loader.js.map +1 -0
  154. package/dist/extensions/packages.d.ts +73 -0
  155. package/dist/extensions/packages.js +441 -0
  156. package/dist/extensions/packages.js.map +1 -0
  157. package/dist/extensions/registry.d.ts +92 -0
  158. package/dist/extensions/registry.js +1540 -0
  159. package/dist/extensions/registry.js.map +1 -0
  160. package/dist/extensions/resources.d.ts +105 -0
  161. package/dist/extensions/resources.js +595 -0
  162. package/dist/extensions/resources.js.map +1 -0
  163. package/dist/extensions/state.d.ts +25 -0
  164. package/dist/extensions/state.js +10 -0
  165. package/dist/extensions/state.js.map +1 -0
  166. package/dist/extensions/transfer.d.ts +81 -0
  167. package/dist/extensions/transfer.js +16 -0
  168. package/dist/extensions/transfer.js.map +1 -0
  169. package/dist/index.d.ts +7 -0
  170. package/dist/index.js +25 -0
  171. package/dist/index.js.map +1 -0
  172. package/dist/maintenance/configuration.d.ts +79 -0
  173. package/dist/maintenance/configuration.js +1503 -0
  174. package/dist/maintenance/configuration.js.map +1 -0
  175. package/dist/maintenance/credentials.d.ts +23 -0
  176. package/dist/maintenance/credentials.js +177 -0
  177. package/dist/maintenance/credentials.js.map +1 -0
  178. package/dist/maintenance/files.d.ts +2 -0
  179. package/dist/maintenance/files.js +40 -0
  180. package/dist/maintenance/files.js.map +1 -0
  181. package/dist/maintenance/npm.d.ts +71 -0
  182. package/dist/maintenance/npm.js +1013 -0
  183. package/dist/maintenance/npm.js.map +1 -0
  184. package/dist/maintenance/oauth.d.ts +45 -0
  185. package/dist/maintenance/oauth.js +686 -0
  186. package/dist/maintenance/oauth.js.map +1 -0
  187. package/dist/maintenance/requests.d.ts +347 -0
  188. package/dist/maintenance/requests.js +959 -0
  189. package/dist/maintenance/requests.js.map +1 -0
  190. package/dist/maintenance/work-git.d.ts +29 -0
  191. package/dist/maintenance/work-git.js +776 -0
  192. package/dist/maintenance/work-git.js.map +1 -0
  193. package/dist/process/backend.d.ts +54 -0
  194. package/dist/process/backend.js +39 -0
  195. package/dist/process/backend.js.map +1 -0
  196. package/dist/process/environment-windows.d.ts +15 -0
  197. package/dist/process/environment-windows.js +151 -0
  198. package/dist/process/environment-windows.js.map +1 -0
  199. package/dist/process/environment.d.ts +28 -0
  200. package/dist/process/environment.js +410 -0
  201. package/dist/process/environment.js.map +1 -0
  202. package/dist/process/launch.d.ts +31 -0
  203. package/dist/process/launch.js +281 -0
  204. package/dist/process/launch.js.map +1 -0
  205. package/dist/process/managed.d.ts +36 -0
  206. package/dist/process/managed.js +459 -0
  207. package/dist/process/managed.js.map +1 -0
  208. package/dist/process/output-log.d.ts +50 -0
  209. package/dist/process/output-log.js +552 -0
  210. package/dist/process/output-log.js.map +1 -0
  211. package/dist/process/pipes.d.ts +16 -0
  212. package/dist/process/pipes.js +588 -0
  213. package/dist/process/pipes.js.map +1 -0
  214. package/dist/process/pty.d.ts +22 -0
  215. package/dist/process/pty.js +512 -0
  216. package/dist/process/pty.js.map +1 -0
  217. package/dist/process/windows-conout-worker.d.ts +1 -0
  218. package/dist/process/windows-conout-worker.js +90 -0
  219. package/dist/process/windows-conout-worker.js.map +1 -0
  220. package/dist/process/windows-conout.d.ts +46 -0
  221. package/dist/process/windows-conout.js +140 -0
  222. package/dist/process/windows-conout.js.map +1 -0
  223. package/dist/process/windows-helper.d.ts +43 -0
  224. package/dist/process/windows-helper.js +255 -0
  225. package/dist/process/windows-helper.js.map +1 -0
  226. package/dist/process/windows-job.d.ts +39 -0
  227. package/dist/process/windows-job.js +224 -0
  228. package/dist/process/windows-job.js.map +1 -0
  229. package/dist/process/windows-pipes.d.ts +16 -0
  230. package/dist/process/windows-pipes.js +475 -0
  231. package/dist/process/windows-pipes.js.map +1 -0
  232. package/dist/results/artifacts.d.ts +118 -0
  233. package/dist/results/artifacts.js +873 -0
  234. package/dist/results/artifacts.js.map +1 -0
  235. package/dist/results/encoder.d.ts +57 -0
  236. package/dist/results/encoder.js +1179 -0
  237. package/dist/results/encoder.js.map +1 -0
  238. package/dist/results/mcp.d.ts +95 -0
  239. package/dist/results/mcp.js +656 -0
  240. package/dist/results/mcp.js.map +1 -0
  241. package/dist/results/media.d.ts +19 -0
  242. package/dist/results/media.js +301 -0
  243. package/dist/results/media.js.map +1 -0
  244. package/dist/results/normalize.d.ts +29 -0
  245. package/dist/results/normalize.js +464 -0
  246. package/dist/results/normalize.js.map +1 -0
  247. package/dist/runtime/control-scripts.d.ts +23 -0
  248. package/dist/runtime/control-scripts.js +242 -0
  249. package/dist/runtime/control-scripts.js.map +1 -0
  250. package/dist/runtime/engine.d.ts +177 -0
  251. package/dist/runtime/engine.js +1159 -0
  252. package/dist/runtime/engine.js.map +1 -0
  253. package/dist/runtime/globals.d.ts +1 -0
  254. package/dist/runtime/globals.js +31 -0
  255. package/dist/runtime/globals.js.map +1 -0
  256. package/dist/runtime/material-retention.d.ts +34 -0
  257. package/dist/runtime/material-retention.js +671 -0
  258. package/dist/runtime/material-retention.js.map +1 -0
  259. package/dist/runtime/modules.d.ts +50 -0
  260. package/dist/runtime/modules.js +320 -0
  261. package/dist/runtime/modules.js.map +1 -0
  262. package/dist/runtime/names.d.ts +2 -0
  263. package/dist/runtime/names.js +22 -0
  264. package/dist/runtime/names.js.map +1 -0
  265. package/dist/runtime/operations.d.ts +20 -0
  266. package/dist/runtime/operations.js +137 -0
  267. package/dist/runtime/operations.js.map +1 -0
  268. package/dist/runtime/paths.d.ts +3 -0
  269. package/dist/runtime/paths.js +38 -0
  270. package/dist/runtime/paths.js.map +1 -0
  271. package/dist/runtime/promises.d.ts +9 -0
  272. package/dist/runtime/promises.js +78 -0
  273. package/dist/runtime/promises.js.map +1 -0
  274. package/dist/runtime/retention.d.ts +88 -0
  275. package/dist/runtime/retention.js +735 -0
  276. package/dist/runtime/retention.js.map +1 -0
  277. package/dist/runtime/run-log.d.ts +30 -0
  278. package/dist/runtime/run-log.js +207 -0
  279. package/dist/runtime/run-log.js.map +1 -0
  280. package/dist/runtime/scripts.d.ts +84 -0
  281. package/dist/runtime/scripts.js +387 -0
  282. package/dist/runtime/scripts.js.map +1 -0
  283. package/dist/runtime/tasks.d.ts +54 -0
  284. package/dist/runtime/tasks.js +290 -0
  285. package/dist/runtime/tasks.js.map +1 -0
  286. package/dist/sdk/maintenance.d.ts +3 -0
  287. package/dist/sdk/maintenance.js +80 -0
  288. package/dist/sdk/maintenance.js.map +1 -0
  289. package/dist/sdk/output-extension.d.ts +3 -0
  290. package/dist/sdk/output-extension.js +44 -0
  291. package/dist/sdk/output-extension.js.map +1 -0
  292. package/dist/sdk/output.d.ts +52 -0
  293. package/dist/sdk/output.js +350 -0
  294. package/dist/sdk/output.js.map +1 -0
  295. package/dist/sdk/runtime.d.ts +3 -0
  296. package/dist/sdk/runtime.js +26 -0
  297. package/dist/sdk/runtime.js.map +1 -0
  298. package/dist/sdk/sh.d.ts +3 -0
  299. package/dist/sdk/sh.js +384 -0
  300. package/dist/sdk/sh.js.map +1 -0
  301. package/dist/sdk/state.d.ts +2 -0
  302. package/dist/sdk/state.js +69 -0
  303. package/dist/sdk/state.js.map +1 -0
  304. package/dist/sdk/transfer.d.ts +3 -0
  305. package/dist/sdk/transfer.js +141 -0
  306. package/dist/sdk/transfer.js.map +1 -0
  307. package/dist/services/extensions.d.ts +19 -0
  308. package/dist/services/extensions.js +183 -0
  309. package/dist/services/extensions.js.map +1 -0
  310. package/dist/services/state.d.ts +37 -0
  311. package/dist/services/state.js +125 -0
  312. package/dist/services/state.js.map +1 -0
  313. package/dist/services/transfer.d.ts +86 -0
  314. package/dist/services/transfer.js +948 -0
  315. package/dist/services/transfer.js.map +1 -0
  316. package/dist/storage/schema.d.ts +8 -0
  317. package/dist/storage/schema.js +241 -0
  318. package/dist/storage/schema.js.map +1 -0
  319. package/dist/storage/sqlite.d.ts +241 -0
  320. package/dist/storage/sqlite.js +1855 -0
  321. package/dist/storage/sqlite.js.map +1 -0
  322. package/dist/storage/transfers.d.ts +90 -0
  323. package/dist/storage/transfers.js +2 -0
  324. package/dist/storage/transfers.js.map +1 -0
  325. package/dist/transfer/io.d.ts +74 -0
  326. package/dist/transfer/io.js +425 -0
  327. package/dist/transfer/io.js.map +1 -0
  328. package/dist/transfer/offers.d.ts +72 -0
  329. package/dist/transfer/offers.js +380 -0
  330. package/dist/transfer/offers.js.map +1 -0
  331. package/dist/transfer/stream-slots.d.ts +12 -0
  332. package/dist/transfer/stream-slots.js +28 -0
  333. package/dist/transfer/stream-slots.js.map +1 -0
  334. package/dist/work/catalog.d.ts +43 -0
  335. package/dist/work/catalog.js +198 -0
  336. package/dist/work/catalog.js.map +1 -0
  337. package/dist/work/discovery.d.ts +70 -0
  338. package/dist/work/discovery.js +465 -0
  339. package/dist/work/discovery.js.map +1 -0
  340. package/dist/work/service.d.ts +83 -0
  341. package/dist/work/service.js +703 -0
  342. package/dist/work/service.js.map +1 -0
  343. package/package.json +66 -4
@@ -0,0 +1,735 @@
1
+ import path from "node:path";
2
+ import { constants } from "node:fs";
3
+ import { lstat, open, opendir, realpath, rm, rmdir } from "node:fs/promises";
4
+ import { AgentError, requireCondition } from "../contracts/errors.js";
5
+ const GiB = 1024 ** 3, batchSize = 100, scanLimit = 100_000;
6
+ const managedRoots = [
7
+ "state",
8
+ "logs",
9
+ "work/tmp",
10
+ "work/resources",
11
+ "work/docs",
12
+ "work/data",
13
+ "work/scripts",
14
+ "work/skills",
15
+ "work/.git",
16
+ "work/.platform-controls",
17
+ "work/api",
18
+ "work/modules",
19
+ ];
20
+ const managedFiles = [
21
+ "config.json",
22
+ "work/mcp.json",
23
+ "work/package.json",
24
+ "work/package-lock.json",
25
+ "work/.gitignore",
26
+ "work/.npmrc",
27
+ "work/AGENTS.override.md",
28
+ "work/AGENTS.md",
29
+ "work/CLAUDE.md",
30
+ "work/GEMINI.md",
31
+ "work/CONTEXT.md",
32
+ ];
33
+ const workTemporaryFile = /^(?:\.configuration-[0-9a-f]{48}|(?:package\.json|package-lock\.json)\.tmp_[0-9a-f]{32}|\.write_[0-9a-f]{32})$/;
34
+ const resourcePattern = /^res_[0-9a-f]{32}$/;
35
+ const runPattern = /^run_[0-9a-f]{32}$/;
36
+ const absent = (cause) => cause?.code === "ENOENT";
37
+ const failure = (cause) => cause instanceof AgentError ? cause.code : "retention_io_failed";
38
+ /** Cold, profile-bound cleanup and admission. Raw Node/npm/project writes are not intercepted.
39
+ * Callers reserve before real managed allocation and release only after its I/O settles.
40
+ * Pending reservations are counted conservatively even while their writes become visible.
41
+ */
42
+ export class ProfileRetention {
43
+ store;
44
+ artifacts;
45
+ options;
46
+ maxManagedBytes;
47
+ intervalMs;
48
+ controlHeadroomBytes;
49
+ root;
50
+ lastError = null;
51
+ #opened = false;
52
+ #stopped = false;
53
+ #opening;
54
+ #stopping;
55
+ #timer;
56
+ #sweeping;
57
+ #admission = Promise.resolve();
58
+ #pendingAdmissions = 0;
59
+ #reserved = 0;
60
+ #artifactDirectory;
61
+ #artifactIdentity;
62
+ #materialFirst = true;
63
+ #inputs = [];
64
+ #inputCursor;
65
+ constructor(store, artifacts, options = {}) {
66
+ this.store = store;
67
+ this.artifacts = artifacts;
68
+ this.options = options;
69
+ requireCondition(path.isAbsolute(store.root) &&
70
+ path.resolve(store.root) === store.root &&
71
+ artifacts.root === store.root &&
72
+ artifacts.directory === path.join(store.root, "work", "resources"), "retention_profile_mismatch", "Retention must use the same actual Agent/profile and artifact directory.");
73
+ this.root = store.root;
74
+ requireCondition(!options.materialRetention ||
75
+ options.materialRetention.root === this.root, "retention_profile_mismatch", "Material cleanup must share this actual Agent profile.");
76
+ this.maxManagedBytes = options.maxManagedBytes ?? 20 * GiB;
77
+ this.controlHeadroomBytes =
78
+ options.controlHeadroomBytes ??
79
+ Math.min(8 * 1024 ** 2, Math.floor(this.maxManagedBytes / 64));
80
+ this.intervalMs = options.intervalMs ?? 60_000;
81
+ requireCondition(Number.isSafeInteger(this.maxManagedBytes) &&
82
+ this.maxManagedBytes > 0 &&
83
+ Number.isSafeInteger(this.controlHeadroomBytes) &&
84
+ this.controlHeadroomBytes >= 0 &&
85
+ this.controlHeadroomBytes < this.maxManagedBytes &&
86
+ Number.isSafeInteger(this.intervalMs) &&
87
+ this.intervalMs > 0 &&
88
+ this.intervalMs <= 2_147_483_647, "invalid_retention_options", "Managed quota and cleanup interval must be positive bounded integers.");
89
+ }
90
+ get reservedBytes() {
91
+ return this.#reserved;
92
+ }
93
+ #now() {
94
+ const now = this.options.now?.() ?? Date.now();
95
+ requireCondition(Number.isSafeInteger(now) && now >= 0, "invalid_retention_clock", "Retention requires a valid current timestamp.");
96
+ return now;
97
+ }
98
+ #admit() {
99
+ requireCondition(!this.#stopped, "retention_closed", "The profile retention service has stopped.");
100
+ }
101
+ async #root() {
102
+ requireCondition((await lstat(this.root)).isDirectory() &&
103
+ (await realpath(this.root)) === this.root, "storage_path_escape", "The Agent profile root cannot redirect elsewhere.");
104
+ }
105
+ async #directory(directory) {
106
+ await this.#root();
107
+ let current = this.root;
108
+ for (const part of path.relative(this.root, directory).split(path.sep)) {
109
+ requireCondition(part !== ".." && part !== "." && part.length > 0, "storage_path_escape", "Cleanup must stay in its fixed managed directory.");
110
+ current = path.join(current, part);
111
+ let stats;
112
+ try {
113
+ stats = await lstat(current, { bigint: true });
114
+ }
115
+ catch (cause) {
116
+ if (absent(cause))
117
+ return null;
118
+ throw cause;
119
+ }
120
+ requireCondition(stats.isDirectory() && !stats.isSymbolicLink(), "storage_path_escape", "A managed directory cannot be a symbolic link or another file type.");
121
+ }
122
+ try {
123
+ requireCondition((await realpath(directory)) === directory, "storage_path_escape", "A managed directory cannot redirect elsewhere.");
124
+ return await lstat(directory, { bigint: true });
125
+ }
126
+ catch (cause) {
127
+ if (absent(cause))
128
+ return null;
129
+ throw cause;
130
+ }
131
+ }
132
+ async #openDirectory(directory) {
133
+ if (!(await this.#directory(directory)))
134
+ return null;
135
+ try {
136
+ return await opendir(directory);
137
+ }
138
+ catch (cause) {
139
+ // Native Git/material cleanup can remove its own temporary directory
140
+ // after validation. Missing bytes are gone; other I/O/safety errors remain fatal.
141
+ if (absent(cause))
142
+ return null;
143
+ throw cause;
144
+ }
145
+ }
146
+ async open() {
147
+ this.#admit();
148
+ if (this.#opened)
149
+ return;
150
+ this.#opening ??= (async () => {
151
+ await this.#root();
152
+ this.#opened = true;
153
+ })();
154
+ try {
155
+ await this.#opening;
156
+ }
157
+ finally {
158
+ this.#opening = undefined;
159
+ }
160
+ }
161
+ async start() {
162
+ await this.open();
163
+ this.#admit();
164
+ if (this.#timer)
165
+ return;
166
+ this.#timer = setInterval(() => {
167
+ void this.sweep().catch((cause) => {
168
+ this.lastError = cause;
169
+ try {
170
+ this.options.onError?.(cause);
171
+ }
172
+ catch (callback) {
173
+ this.lastError = callback;
174
+ }
175
+ });
176
+ }, this.intervalMs);
177
+ this.#timer.unref();
178
+ }
179
+ stop() {
180
+ if (this.#stopping)
181
+ return this.#stopping;
182
+ this.#stopped = true;
183
+ if (this.#timer)
184
+ clearInterval(this.#timer);
185
+ this.#timer = undefined;
186
+ this.#stopping = this.#finishStop();
187
+ return this.#stopping;
188
+ }
189
+ async #finishStop() {
190
+ await this.#sweeping?.catch(() => { });
191
+ await this.#admission;
192
+ await this.#closeDirectory();
193
+ await this.#closeInputs();
194
+ await this.options.materialRetention?.stop();
195
+ if (this.#inputs.length) {
196
+ this.lastError = new AgentError("retention_cleanup_incomplete", "Temporary input cleanup stopped before its bounded traversal completed.", { count: this.#inputs.length });
197
+ this.options.onError?.(this.lastError);
198
+ }
199
+ }
200
+ async measure() {
201
+ await this.open();
202
+ let bytes = 0, files = 0, directories = 0, symlinks = 0, scanned = 0, complete = true;
203
+ const identities = new Set();
204
+ const countFile = (stats) => {
205
+ // NTFS file identities may exceed the exact integer range of a Number.
206
+ // Round only bounded byte counts, never the identity used for hard-link deduplication.
207
+ const identity = `${stats.dev}:${stats.ino}`;
208
+ if (!identities.has(identity)) {
209
+ const size = Number(stats.size);
210
+ requireCondition(Number.isSafeInteger(size) && Number.isSafeInteger(bytes + size), "storage_measurement_invalid", "The managed byte count exceeds its exact integer range.");
211
+ identities.add(identity);
212
+ bytes += size;
213
+ files++;
214
+ }
215
+ };
216
+ const inspectFile = async (filename) => {
217
+ if (!complete)
218
+ return;
219
+ if (++scanned > scanLimit) {
220
+ complete = false;
221
+ return;
222
+ }
223
+ const parent = path.dirname(filename);
224
+ if (parent !== this.root && !(await this.#directory(parent)))
225
+ return;
226
+ await this.#root();
227
+ let stats;
228
+ try {
229
+ stats = await lstat(filename, { bigint: true });
230
+ }
231
+ catch (cause) {
232
+ if (absent(cause))
233
+ return;
234
+ throw cause;
235
+ }
236
+ if (stats.isSymbolicLink()) {
237
+ symlinks++;
238
+ return;
239
+ }
240
+ requireCondition(stats.isFile(), "storage_measurement_invalid", "A fixed managed record must remain a regular file.");
241
+ countFile(stats);
242
+ };
243
+ const walk = async (directory, depth) => {
244
+ if (!complete)
245
+ return;
246
+ if (depth > 128) {
247
+ complete = false;
248
+ return;
249
+ }
250
+ const dir = await this.#openDirectory(directory);
251
+ if (!dir)
252
+ return;
253
+ try {
254
+ if (!(await this.#directory(directory)))
255
+ return;
256
+ for await (const entry of dir) {
257
+ if (++scanned > scanLimit) {
258
+ complete = false;
259
+ break;
260
+ }
261
+ const filename = path.join(directory, entry.name);
262
+ // npm dependency trees remain the native package environment even
263
+ // while the bounded maintenance planner uses a private temp root.
264
+ const relative = path
265
+ .relative(this.root, filename)
266
+ .split(path.sep)
267
+ .join("/");
268
+ if (/^state\/maintenance\/npm\/plan-[A-Za-z0-9_-]+\/node_modules$/.test(relative))
269
+ continue;
270
+ let stats;
271
+ try {
272
+ stats = await lstat(filename, { bigint: true });
273
+ }
274
+ catch (cause) {
275
+ if (absent(cause))
276
+ continue;
277
+ throw cause;
278
+ }
279
+ if (stats.isSymbolicLink()) {
280
+ symlinks++;
281
+ continue;
282
+ }
283
+ if (stats.isDirectory()) {
284
+ directories++;
285
+ await walk(filename, depth + 1);
286
+ }
287
+ else if (stats.isFile()) {
288
+ countFile(stats);
289
+ }
290
+ if (!complete)
291
+ break;
292
+ }
293
+ }
294
+ finally {
295
+ await dir.close().catch((cause) => {
296
+ if (cause.code !== "ERR_DIR_CLOSED")
297
+ throw cause;
298
+ });
299
+ }
300
+ };
301
+ for (const relative of managedRoots) {
302
+ const directory = path.join(this.root, relative);
303
+ if (await this.#directory(directory)) {
304
+ directories++;
305
+ await walk(directory, 0);
306
+ }
307
+ if (!complete)
308
+ break;
309
+ }
310
+ for (const relative of managedFiles)
311
+ await inspectFile(path.join(this.root, relative));
312
+ // Only these Core-owned siblings are inspected at work's top level. Never
313
+ // recurse through arbitrary projects, package dependencies or raw work files.
314
+ const work = path.join(this.root, "work");
315
+ const directory = complete ? await this.#openDirectory(work) : null;
316
+ if (directory) {
317
+ try {
318
+ if (await this.#directory(work)) {
319
+ for await (const entry of directory) {
320
+ if (++scanned > scanLimit) {
321
+ complete = false;
322
+ break;
323
+ }
324
+ if (workTemporaryFile.test(entry.name))
325
+ await inspectFile(path.join(work, entry.name));
326
+ if (!complete)
327
+ break;
328
+ }
329
+ }
330
+ }
331
+ finally {
332
+ await directory.close().catch((cause) => {
333
+ if (cause.code !== "ERR_DIR_CLOSED")
334
+ throw cause;
335
+ });
336
+ }
337
+ }
338
+ return Object.freeze({
339
+ bytes,
340
+ files,
341
+ directories,
342
+ symlinks,
343
+ observedAt: this.#now(),
344
+ complete,
345
+ });
346
+ }
347
+ async reserve(bytes, options = {}) {
348
+ this.#admit();
349
+ requireCondition(Number.isSafeInteger(bytes) && bytes > 0, "invalid_storage_reservation", "Reserve a positive safe byte count before managed allocation.");
350
+ requireCondition(this.#pendingAdmissions < 256, "storage_reservation_busy", "The bounded managed-space admission queue is full.");
351
+ this.#pendingAdmissions++;
352
+ const maximum = this.maxManagedBytes -
353
+ (options.control === true ? 0 : this.controlHeadroomBytes);
354
+ const admission = this.#admission.then(async () => {
355
+ this.#admit();
356
+ // Earlier writers can finish and release after their path was scanned.
357
+ // Admissions themselves are serial, so this per-scan floor covers exactly
358
+ // the old commitments that may become newly visible bytes during the scan.
359
+ let reservedAtScanStart = this.#reserved;
360
+ let usage = await this.measure();
361
+ requireCondition(usage.complete, "storage_measurement_incomplete", "The managed usage scan could not finish within its safety budget.");
362
+ if (bytes >
363
+ maximum - usage.bytes - Math.max(reservedAtScanStart, this.#reserved)) {
364
+ await this.sweep();
365
+ reservedAtScanStart = this.#reserved;
366
+ usage = await this.measure();
367
+ }
368
+ requireCondition(usage.complete, "storage_measurement_incomplete", "The managed usage scan could not finish within its safety budget.");
369
+ requireCondition(bytes <=
370
+ maximum - usage.bytes - Math.max(reservedAtScanStart, this.#reserved), "storage_quota_exceeded", "Managed storage is full after eligible expired data cleanup.");
371
+ this.#admit();
372
+ this.#reserved += bytes;
373
+ let released = false;
374
+ return Object.freeze({
375
+ release: () => {
376
+ if (!released) {
377
+ released = true;
378
+ this.#reserved -= bytes;
379
+ }
380
+ },
381
+ });
382
+ });
383
+ this.#admission = admission.then(() => { }, () => { });
384
+ try {
385
+ return await admission;
386
+ }
387
+ finally {
388
+ this.#pendingAdmissions--;
389
+ }
390
+ }
391
+ sweep() {
392
+ this.#admit();
393
+ if (this.#sweeping)
394
+ return this.#sweeping;
395
+ const pending = this.#sweep();
396
+ this.#sweeping = pending;
397
+ void pending
398
+ .finally(() => {
399
+ if (this.#sweeping === pending)
400
+ this.#sweeping = undefined;
401
+ })
402
+ .catch(() => { });
403
+ return pending;
404
+ }
405
+ async #closeDirectory() {
406
+ const dir = this.#artifactDirectory;
407
+ this.#artifactDirectory = undefined;
408
+ this.#artifactIdentity = undefined;
409
+ if (dir)
410
+ await dir.close().catch((cause) => {
411
+ if (cause.code !== "ERR_DIR_CLOSED")
412
+ throw cause;
413
+ });
414
+ }
415
+ async #nextArtifact() {
416
+ const stats = await this.#directory(this.artifacts.directory);
417
+ if (!stats) {
418
+ await this.#closeDirectory();
419
+ return null;
420
+ }
421
+ if (this.#artifactIdentity)
422
+ requireCondition(stats.dev === this.#artifactIdentity.dev &&
423
+ stats.ino === this.#artifactIdentity.ino, "storage_path_escape", "The open artifact catalog was replaced.");
424
+ if (!this.#artifactDirectory) {
425
+ this.#artifactDirectory = await opendir(this.artifacts.directory);
426
+ this.#artifactIdentity = stats;
427
+ }
428
+ const entry = await this.#artifactDirectory.read();
429
+ if (!entry) {
430
+ await this.#closeDirectory();
431
+ return null;
432
+ }
433
+ return entry.name;
434
+ }
435
+ #protected(id, originRunId, now) {
436
+ const active = this.artifacts.activeSnapshot();
437
+ return (active.resourceIds.has(id) ||
438
+ active.pendingPaths.has(path.join(this.artifacts.directory, id)) ||
439
+ this.store.protectedArtifactIds(now).has(id) ||
440
+ (originRunId !== null && this.store.activeRunIds().has(originRunId)) ||
441
+ (this.store.taskArtifactRetentionUntil(id, originRunId, now) ?? 0) > now);
442
+ }
443
+ async #removeArtifact(id, now) {
444
+ if (!resourcePattern.test(id) || this.#protected(id, null, now))
445
+ return "protected";
446
+ const directory = path.join(this.artifacts.directory, id);
447
+ if (!(await this.#directory(directory)))
448
+ return "current";
449
+ let record;
450
+ try {
451
+ const handle = await open(path.join(directory, "resource.json"), constants.O_RDONLY |
452
+ (constants.O_NOFOLLOW ?? 0) |
453
+ (constants.O_NONBLOCK ?? 0));
454
+ try {
455
+ const stats = await handle.stat();
456
+ requireCondition(stats.isFile() && stats.size <= 16384, "invalid_resource_metadata", "Only bounded regular artifact metadata can be cleaned.");
457
+ const buffer = Buffer.alloc(16385), read = await handle.read(buffer, 0, buffer.length, 0);
458
+ requireCondition(read.bytesRead <= 16384, "invalid_resource_metadata", "The artifact metadata changed beyond its safe size.");
459
+ record = JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(buffer.subarray(0, read.bytesRead)));
460
+ }
461
+ finally {
462
+ await handle.close();
463
+ }
464
+ }
465
+ catch (cause) {
466
+ if (!absent(cause))
467
+ throw cause;
468
+ }
469
+ let originRunId = null;
470
+ if (record !== undefined) {
471
+ requireCondition(record &&
472
+ record.resourceId === id &&
473
+ record.agentId === this.store.binding.agentId &&
474
+ (record.originRunId === null ||
475
+ (typeof record.originRunId === "string" &&
476
+ runPattern.test(record.originRunId))) &&
477
+ typeof record.expiresAt === "string" &&
478
+ Number.isFinite(Date.parse(record.expiresAt)), "invalid_resource_metadata", "Artifact cleanup requires its actual registered identity and expiry.");
479
+ originRunId = record.originRunId;
480
+ if (Date.parse(record.expiresAt) > now)
481
+ return "current";
482
+ }
483
+ if (this.#protected(id, originRunId, now))
484
+ return "protected";
485
+ await this.#directory(directory);
486
+ if (this.#protected(id, originRunId, now))
487
+ return "protected";
488
+ await rm(directory, { recursive: true, force: true });
489
+ return "removed";
490
+ }
491
+ async #removeFile(candidate) {
492
+ const { kind, ref } = candidate;
493
+ let match;
494
+ if (kind === "run") {
495
+ match = /^work\/tmp\/runs\/(run_[0-9a-f]{32})$/.exec(ref);
496
+ requireCondition(match, "invalid_cleanup_reference", "Run cleanup only accepts a fixed expired run directory.");
497
+ if (this.store.activeRunIds().has(match[1]))
498
+ return 0;
499
+ if (await this.#directory(path.join(this.root, ref)))
500
+ await rm(path.join(this.root, ref), { recursive: true, force: true });
501
+ return 1;
502
+ }
503
+ if (kind === "log") {
504
+ match = /^logs\/runs\/(run_[0-9a-f]{32})\/console\.log$/.exec(ref);
505
+ requireCondition(match, "invalid_cleanup_reference", "Run log cleanup only accepts its fixed console file.");
506
+ if (this.store.activeRunIds().has(match[1]))
507
+ return 0;
508
+ }
509
+ else if (kind === "task")
510
+ requireCondition(/^state\/tasks\/[0-9a-f]{64}\/result_[0-9a-f]{32}\.json$/.test(ref), "invalid_cleanup_reference", "Task cleanup only accepts its fixed retained result file.");
511
+ else if (kind === "process_log")
512
+ requireCondition(/^logs\/processes\/res_[0-9a-f]{32}\.output$/.test(ref), "invalid_cleanup_reference", "Process output cleanup only accepts its fixed managed log.");
513
+ else
514
+ throw new AgentError("invalid_cleanup_reference", "Unknown managed cleanup kind.");
515
+ const filename = path.join(this.root, ref), parent = path.dirname(filename);
516
+ if (!(await this.#directory(parent)))
517
+ return 0;
518
+ const names = kind === "process_log" ? [filename, `${filename}.complete`] : [filename];
519
+ let removed = 0;
520
+ for (const file of names) {
521
+ try {
522
+ const stats = await lstat(file);
523
+ requireCondition(stats.isFile() && !stats.isSymbolicLink(), "storage_path_escape", "A retained file cannot redirect cleanup elsewhere.");
524
+ await rm(file);
525
+ removed++;
526
+ }
527
+ catch (cause) {
528
+ if (!absent(cause))
529
+ throw cause;
530
+ }
531
+ }
532
+ if (kind === "log" || kind === "task")
533
+ await rmdir(parent).catch((cause) => {
534
+ if (!absent(cause) &&
535
+ cause.code !== "ENOTEMPTY")
536
+ throw cause;
537
+ });
538
+ return removed;
539
+ }
540
+ async #closeInputs() {
541
+ const cursor = this.#inputCursor;
542
+ this.#inputCursor = undefined;
543
+ for (const dir of [cursor?.wait, cursor?.root])
544
+ if (dir)
545
+ await dir.close().catch((cause) => {
546
+ if (cause.code !== "ERR_DIR_CLOSED")
547
+ throw cause;
548
+ });
549
+ }
550
+ async #drainInputs(limit, now, errors) {
551
+ let processed = 0, removed = 0;
552
+ while (processed < limit && this.#inputs.length) {
553
+ const candidate = this.#inputs[0];
554
+ try {
555
+ const match = /^work\/tmp\/runs\/(run_[0-9a-f]{32})\/inputs$/.exec(candidate.ref);
556
+ requireCondition(match, "invalid_cleanup_reference", "Input cleanup only accepts the original run's fixed temporary directory.");
557
+ const runId = match[1];
558
+ if (this.store.activeRunIds().has(runId)) {
559
+ processed++;
560
+ break;
561
+ }
562
+ if (!this.#inputCursor) {
563
+ const directory = path.join(this.root, candidate.ref);
564
+ if (!(await this.#directory(directory))) {
565
+ this.store.completeInputsCleanup(runId, now);
566
+ this.#inputs.shift();
567
+ processed++;
568
+ continue;
569
+ }
570
+ this.#inputCursor = {
571
+ ref: candidate.ref,
572
+ runId,
573
+ directory,
574
+ root: await opendir(directory),
575
+ failed: false,
576
+ };
577
+ }
578
+ const cursor = this.#inputCursor;
579
+ requireCondition(cursor.ref === candidate.ref, "invalid_cleanup_reference", "Input traversal cannot be rebound to another run.");
580
+ await this.#directory(cursor.waitPath ?? cursor.directory);
581
+ const entry = await (cursor.wait ?? cursor.root).read();
582
+ if (!entry) {
583
+ if (cursor.wait) {
584
+ await cursor.wait.close();
585
+ cursor.wait = undefined;
586
+ const waitPath = cursor.waitPath;
587
+ cursor.waitPath = undefined;
588
+ await rmdir(waitPath).catch((cause) => {
589
+ if (!absent(cause) &&
590
+ cause.code !== "ENOTEMPTY")
591
+ throw cause;
592
+ });
593
+ }
594
+ else {
595
+ const finished = !cursor.failed;
596
+ await this.#closeInputs();
597
+ processed++;
598
+ if (finished && !this.store.activeRunIds().has(runId))
599
+ this.store.completeInputsCleanup(runId, now);
600
+ this.#inputs.shift();
601
+ await rmdir(cursor.directory).catch((cause) => {
602
+ if (!absent(cause) &&
603
+ cause.code !== "ENOTEMPTY")
604
+ throw cause;
605
+ });
606
+ }
607
+ continue;
608
+ }
609
+ processed++;
610
+ if (!cursor.wait &&
611
+ entry.isDirectory() &&
612
+ /^wait_[0-9a-f]{32}$/.test(entry.name)) {
613
+ const directory = path.join(cursor.directory, entry.name);
614
+ if (await this.#directory(directory)) {
615
+ cursor.wait = await opendir(directory);
616
+ cursor.waitPath = directory;
617
+ }
618
+ }
619
+ else if (/^input_[0-9a-f]{32}\.bin$/.test(entry.name)) {
620
+ const filename = path.join(cursor.waitPath ?? cursor.directory, entry.name);
621
+ const stats = await lstat(filename);
622
+ requireCondition(stats.isFile() && !stats.isSymbolicLink(), "storage_path_escape", "Temporary attachment cleanup cannot follow a link or remove another file type.");
623
+ if (this.store.activeRunIds().has(runId))
624
+ break;
625
+ await rm(filename);
626
+ removed++;
627
+ }
628
+ }
629
+ catch (cause) {
630
+ errors.push({ code: failure(cause), ref: candidate.ref });
631
+ if (this.#inputCursor)
632
+ this.#inputCursor.failed = true;
633
+ else {
634
+ this.#inputs.shift();
635
+ processed++;
636
+ }
637
+ // A failed native directory read cannot be resumed safely by assuming
638
+ // that it advanced. Retry through the Store's still-unfinished fact.
639
+ await this.#closeInputs();
640
+ if (this.#inputs[0] === candidate)
641
+ this.#inputs.shift();
642
+ }
643
+ }
644
+ return { processed, removed };
645
+ }
646
+ async #sweep() {
647
+ await this.open();
648
+ const now = this.#now();
649
+ let processed = 0, removedFiles = 0, removedArtifacts = 0, removedMaterials = 0, protectedArtifacts = 0;
650
+ let deleted = {};
651
+ const errors = [];
652
+ const pending = await this.#drainInputs(batchSize, now, errors);
653
+ processed += pending.processed;
654
+ removedFiles += pending.removed;
655
+ if (!this.#inputs.length && processed < batchSize) {
656
+ const limit = batchSize - processed, batch = this.store.cleanupBatch({ now, limit });
657
+ requireCondition(Number.isSafeInteger(batch.processed) &&
658
+ batch.processed >= 0 &&
659
+ batch.processed <= limit &&
660
+ Array.isArray(batch.files) &&
661
+ batch.files.length <= 2 * limit, "invalid_cleanup_batch", "The Store cleanup batch exceeds its bounded contract.");
662
+ processed += batch.processed;
663
+ deleted = Object.freeze({ ...batch.deleted });
664
+ for (const candidate of batch.files) {
665
+ if (candidate.kind === "inputs") {
666
+ requireCondition(this.#inputs.length < batchSize, "invalid_cleanup_batch", "The bounded input cleanup continuation is full.");
667
+ if (!this.#inputs.some((item) => item.ref === candidate.ref))
668
+ this.#inputs.push(candidate);
669
+ }
670
+ else
671
+ try {
672
+ removedFiles += await this.#removeFile(candidate);
673
+ }
674
+ catch (cause) {
675
+ errors.push({ code: failure(cause), ref: candidate.ref });
676
+ }
677
+ }
678
+ const inputs = await this.#drainInputs(batchSize - processed, now, errors);
679
+ processed += inputs.processed;
680
+ removedFiles += inputs.removed;
681
+ }
682
+ const materials = async () => {
683
+ if (!this.options.materialRetention || processed >= batchSize)
684
+ return;
685
+ const remaining = batchSize - processed, material = await this.options.materialRetention.sweep(remaining);
686
+ requireCondition(Number.isSafeInteger(material.processed) &&
687
+ material.processed >= 0 &&
688
+ material.processed <= remaining, "invalid_cleanup_batch", "Material cleanup exceeded the remaining Core batch budget.");
689
+ processed += material.processed;
690
+ removedMaterials += material.removed;
691
+ errors.push(...material.errors);
692
+ };
693
+ if (this.#materialFirst)
694
+ await materials();
695
+ while (processed < batchSize) {
696
+ const id = await this.#nextArtifact();
697
+ if (id === null)
698
+ break;
699
+ processed++;
700
+ if (!resourcePattern.test(id))
701
+ continue;
702
+ try {
703
+ const result = await this.#removeArtifact(id, now);
704
+ if (result === "removed")
705
+ removedArtifacts++;
706
+ if (result === "protected")
707
+ protectedArtifacts++;
708
+ }
709
+ catch (cause) {
710
+ errors.push({ code: failure(cause), ref: `work/resources/${id}` });
711
+ }
712
+ }
713
+ if (!this.#materialFirst)
714
+ await materials();
715
+ this.#materialFirst = !this.#materialFirst;
716
+ const result = Object.freeze({
717
+ processed,
718
+ deleted,
719
+ removedFiles,
720
+ removedArtifacts,
721
+ removedMaterials,
722
+ protectedArtifacts,
723
+ pendingInputDirectories: this.#inputs.length,
724
+ errors: Object.freeze(errors),
725
+ });
726
+ if (errors.length) {
727
+ this.lastError = new AgentError("retention_cleanup_failed", "Some retained files could not be safely removed.", { count: errors.length });
728
+ this.options.onError?.(this.lastError);
729
+ }
730
+ else
731
+ this.lastError = null;
732
+ return result;
733
+ }
734
+ }
735
+ //# sourceMappingURL=retention.js.map