@yu_robotics/remote-cli 1.6.190 → 1.6.194

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 (114) hide show
  1. package/README.md +129 -14
  2. package/dist/client/MessageHandler.d.ts.map +1 -1
  3. package/dist/client/MessageHandler.js +2 -1
  4. package/dist/client/MessageHandler.js.map +1 -1
  5. package/dist/commands/delegationStorage.d.ts +15 -0
  6. package/dist/commands/delegationStorage.d.ts.map +1 -0
  7. package/dist/commands/delegationStorage.js +59 -0
  8. package/dist/commands/delegationStorage.js.map +1 -0
  9. package/dist/commands/start.d.ts +2 -0
  10. package/dist/commands/start.d.ts.map +1 -1
  11. package/dist/commands/start.js +42 -18
  12. package/dist/commands/start.js.map +1 -1
  13. package/dist/delegation/DelegatedWorkerSessionStore.d.ts +6 -0
  14. package/dist/delegation/DelegatedWorkerSessionStore.d.ts.map +1 -1
  15. package/dist/delegation/DelegatedWorkerSessionStore.js +25 -2
  16. package/dist/delegation/DelegatedWorkerSessionStore.js.map +1 -1
  17. package/dist/delegation/DelegatedWorkspaceManager.d.ts +119 -2
  18. package/dist/delegation/DelegatedWorkspaceManager.d.ts.map +1 -1
  19. package/dist/delegation/DelegatedWorkspaceManager.js +560 -65
  20. package/dist/delegation/DelegatedWorkspaceManager.js.map +1 -1
  21. package/dist/delegation/DelegationManager.d.ts +20 -0
  22. package/dist/delegation/DelegationManager.d.ts.map +1 -1
  23. package/dist/delegation/DelegationManager.js +267 -41
  24. package/dist/delegation/DelegationManager.js.map +1 -1
  25. package/dist/delegation/DelegationOwnership.d.ts +33 -0
  26. package/dist/delegation/DelegationOwnership.d.ts.map +1 -0
  27. package/dist/delegation/DelegationOwnership.js +397 -0
  28. package/dist/delegation/DelegationOwnership.js.map +1 -0
  29. package/dist/delegation/GitCheckpoint.d.ts.map +1 -1
  30. package/dist/delegation/GitCheckpoint.js +14 -5
  31. package/dist/delegation/GitCheckpoint.js.map +1 -1
  32. package/dist/delegation/LegacyWorkspaceCleanup.d.ts +100 -0
  33. package/dist/delegation/LegacyWorkspaceCleanup.d.ts.map +1 -0
  34. package/dist/delegation/LegacyWorkspaceCleanup.js +864 -0
  35. package/dist/delegation/LegacyWorkspaceCleanup.js.map +1 -0
  36. package/dist/delegation/LfsCheckout.d.ts +11 -0
  37. package/dist/delegation/LfsCheckout.d.ts.map +1 -0
  38. package/dist/delegation/LfsCheckout.js +135 -0
  39. package/dist/delegation/LfsCheckout.js.map +1 -0
  40. package/dist/delegation/OwnershipRuntime.d.ts +50 -0
  41. package/dist/delegation/OwnershipRuntime.d.ts.map +1 -0
  42. package/dist/delegation/OwnershipRuntime.js +411 -0
  43. package/dist/delegation/OwnershipRuntime.js.map +1 -0
  44. package/dist/delegation/WorkspaceDisposal.d.ts +23 -0
  45. package/dist/delegation/WorkspaceDisposal.d.ts.map +1 -0
  46. package/dist/delegation/WorkspaceDisposal.js +212 -0
  47. package/dist/delegation/WorkspaceDisposal.js.map +1 -0
  48. package/dist/delegation/contract.d.ts +1 -1
  49. package/dist/delegation/contract.d.ts.map +1 -1
  50. package/dist/delegation/contract.js +2 -1
  51. package/dist/delegation/contract.js.map +1 -1
  52. package/dist/executor/AcpExecutor.d.ts +2 -0
  53. package/dist/executor/AcpExecutor.d.ts.map +1 -1
  54. package/dist/executor/AcpExecutor.js +14 -1
  55. package/dist/executor/AcpExecutor.js.map +1 -1
  56. package/dist/executor/AgyExecutor.d.ts +2 -0
  57. package/dist/executor/AgyExecutor.d.ts.map +1 -1
  58. package/dist/executor/AgyExecutor.js +11 -1
  59. package/dist/executor/AgyExecutor.js.map +1 -1
  60. package/dist/executor/ClaudePersistentExecutor.d.ts +2 -0
  61. package/dist/executor/ClaudePersistentExecutor.d.ts.map +1 -1
  62. package/dist/executor/ClaudePersistentExecutor.js +13 -3
  63. package/dist/executor/ClaudePersistentExecutor.js.map +1 -1
  64. package/dist/executor/CodexAppServerClient.d.ts +4 -0
  65. package/dist/executor/CodexAppServerClient.d.ts.map +1 -1
  66. package/dist/executor/CodexAppServerClient.js +15 -3
  67. package/dist/executor/CodexAppServerClient.js.map +1 -1
  68. package/dist/executor/CodexAppServerExecutor.d.ts +2 -0
  69. package/dist/executor/CodexAppServerExecutor.d.ts.map +1 -1
  70. package/dist/executor/CodexAppServerExecutor.js +7 -0
  71. package/dist/executor/CodexAppServerExecutor.js.map +1 -1
  72. package/dist/executor/DshExecutor.d.ts.map +1 -1
  73. package/dist/executor/DshExecutor.js +2 -1
  74. package/dist/executor/DshExecutor.js.map +1 -1
  75. package/dist/executor/IExecutor.d.ts +5 -1
  76. package/dist/executor/IExecutor.d.ts.map +1 -1
  77. package/dist/executor/ManagedProcessGroups.d.ts +30 -0
  78. package/dist/executor/ManagedProcessGroups.d.ts.map +1 -0
  79. package/dist/executor/ManagedProcessGroups.js +144 -0
  80. package/dist/executor/ManagedProcessGroups.js.map +1 -0
  81. package/dist/executor/PiExecutor.d.ts +2 -0
  82. package/dist/executor/PiExecutor.d.ts.map +1 -1
  83. package/dist/executor/PiExecutor.js +15 -0
  84. package/dist/executor/PiExecutor.js.map +1 -1
  85. package/dist/executor/ZCodeExecutor.d.ts.map +1 -1
  86. package/dist/executor/ZCodeExecutor.js +1 -0
  87. package/dist/executor/ZCodeExecutor.js.map +1 -1
  88. package/dist/executor/acp/AcpClient.d.ts +5 -0
  89. package/dist/executor/acp/AcpClient.d.ts.map +1 -1
  90. package/dist/executor/acp/AcpClient.js +13 -2
  91. package/dist/executor/acp/AcpClient.js.map +1 -1
  92. package/dist/executor/dsh/DshAcpClient.d.ts +1 -1
  93. package/dist/executor/dsh/DshAcpClient.d.ts.map +1 -1
  94. package/dist/executor/dsh/DshAcpClient.js +2 -2
  95. package/dist/executor/dsh/DshAcpClient.js.map +1 -1
  96. package/dist/executor/pi/PiClient.d.ts +2 -0
  97. package/dist/executor/pi/PiClient.d.ts.map +1 -1
  98. package/dist/executor/pi/PiClient.js +12 -2
  99. package/dist/executor/pi/PiClient.js.map +1 -1
  100. package/dist/executor/pi/PiTypes.d.ts +1 -0
  101. package/dist/executor/pi/PiTypes.d.ts.map +1 -1
  102. package/dist/executor/pi/PiTypes.js.map +1 -1
  103. package/dist/executor/zcode/ZCodeClient.d.ts +4 -0
  104. package/dist/executor/zcode/ZCodeClient.d.ts.map +1 -1
  105. package/dist/executor/zcode/ZCodeClient.js +13 -2
  106. package/dist/executor/zcode/ZCodeClient.js.map +1 -1
  107. package/dist/index.js +13 -5
  108. package/dist/index.js.map +1 -1
  109. package/dist/maintenance/release-notes.json +1 -1
  110. package/dist/utils/ProcessShutdown.d.ts +25 -0
  111. package/dist/utils/ProcessShutdown.d.ts.map +1 -0
  112. package/dist/utils/ProcessShutdown.js +54 -0
  113. package/dist/utils/ProcessShutdown.js.map +1 -0
  114. package/package.json +5 -4
@@ -3,7 +3,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.DelegatedWorkspaceManager = void 0;
6
+ exports.DelegatedWorkspaceManager = exports.WORKSPACE_INVENTORY_PAGE_LIMIT = void 0;
7
+ const async_hooks_1 = require("async_hooks");
7
8
  const crypto_1 = require("crypto");
8
9
  const fs_1 = require("fs");
9
10
  const promises_1 = __importDefault(require("fs/promises"));
@@ -11,8 +12,16 @@ const os_1 = __importDefault(require("os"));
11
12
  const path_1 = __importDefault(require("path"));
12
13
  const GitCheckpoint_1 = require("./GitCheckpoint");
13
14
  const BoundedFileChecks_1 = require("./BoundedFileChecks");
15
+ const LfsCheckout_1 = require("./LfsCheckout");
16
+ const LegacyWorkspaceCleanup_1 = require("./LegacyWorkspaceCleanup");
17
+ const WorkspaceDisposal_1 = require("./WorkspaceDisposal");
14
18
  const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
15
19
  const RECORD_BYTES = 128 * 1024;
20
+ const INVENTORY_LIMIT = 10_000;
21
+ /** At most one bounded artifact listing plus the exact tasks of the bounded lane listing. */
22
+ exports.WORKSPACE_INVENTORY_PAGE_LIMIT = INVENTORY_LIMIT * 2;
23
+ const INVENTORY_BYTES = 32 * 1024 * 1024;
24
+ const CLEANUP_ATTEMPTS = 3;
16
25
  function inside(root, child) {
17
26
  const relative = path_1.default.relative(root, child);
18
27
  return relative === '' || (!path_1.default.isAbsolute(relative) && relative !== '..' && !relative.startsWith(`..${path_1.default.sep}`));
@@ -20,9 +29,18 @@ function inside(root, child) {
20
29
  /** Worktrees isolate checkout files, not process permissions. Artifacts are never expired with task diagnostics. */
21
30
  class DelegatedWorkspaceManager {
22
31
  directory;
32
+ disposalSupported;
23
33
  static operations = new Map();
24
- constructor(directory = path_1.default.join(os_1.default.homedir(), '.remote-cli', 'delegation-workspaces')) {
34
+ ownership;
35
+ beforeMutation;
36
+ repositoryContext = new async_hooks_1.AsyncLocalStorage();
37
+ setOwnership(ownership, beforeMutation) {
38
+ this.ownership = ownership;
39
+ this.beforeMutation = beforeMutation;
40
+ }
41
+ constructor(directory = path_1.default.join(os_1.default.homedir(), '.remote-cli', 'delegation-workspaces'), disposalSupported = process.platform === 'linux') {
25
42
  this.directory = directory;
43
+ this.disposalSupported = disposalSupported;
26
44
  }
27
45
  async discover(cwd) {
28
46
  // Avoid starting Git for ordinary directories (including environments without Git).
@@ -69,7 +87,8 @@ class DelegatedWorkspaceManager {
69
87
  continue;
70
88
  try {
71
89
  const record = await this.read('lanes', id);
72
- if (!record || record.blocked || record.pendingTaskId)
90
+ if (!record || record.blocked || record.pendingTaskId || record.cleanup
91
+ && !(record.checkoutState === 'reclaiming' && record.cleanup.retryable))
73
92
  blocked.add(id);
74
93
  else
75
94
  this.assertLifecycle(record);
@@ -81,7 +100,7 @@ class DelegatedWorkspaceManager {
81
100
  return blocked;
82
101
  }
83
102
  async prepare(lane, source, input, taskId) {
84
- return this.withLane(lane.id, () => this.prepareLocked(lane, source, input, taskId));
103
+ return this.withLane(lane.id, () => this.withRepository(source, () => this.prepareLocked(lane, source, input, taskId)));
85
104
  }
86
105
  async prepareLocked(lane, source, input, taskId) {
87
106
  this.assertId(taskId);
@@ -97,7 +116,9 @@ class DelegatedWorkspaceManager {
97
116
  if (previous) {
98
117
  await this.validateWorkspaceRecord(previous);
99
118
  if (previous.laneId !== lane.id || previous.threadId !== lane.threadId || previous.workspaceGeneration !== lane.workspaceGeneration
100
- || previous.source.cwd !== source.cwd || previous.blocked || previous.pendingTaskId) {
119
+ || previous.source.cwd !== source.cwd || previous.blocked || previous.pendingTaskId
120
+ || previous.disposalPolicy === 1 && previous.cleanup
121
+ && !(previous.checkoutState === 'reclaiming' && previous.cleanup.retryable)) {
101
122
  throw new Error('Worker workspace has unresolved artifacts or a changed identity');
102
123
  }
103
124
  previous = await this.resolveReclamation(previous);
@@ -132,57 +153,115 @@ class DelegatedWorkspaceManager {
132
153
  }
133
154
  await (0, GitCheckpoint_1.runGit)(source.root, ['update-ref', this.inputRef(taskId), input.commit]);
134
155
  await (0, GitCheckpoint_1.runGit)(source.root, ['update-ref', taskRef, input.commit]);
156
+ const identity = await promises_1.default.lstat(directory);
157
+ // Reusing a legacy checkout must not adopt its pre-existing ignored files as
158
+ // disposable data. Only a newly created checkout opts into the new policy.
159
+ const disposalPolicy = this.disposalSupported
160
+ ? previous && previous.checkoutState !== 'reclaimed' ? previous.disposalPolicy : 1 : undefined;
135
161
  const workspace = { laneId: lane.id, threadId: lane.threadId,
136
162
  workspaceGeneration: lane.workspaceGeneration, source, directory,
137
- cwd: path_1.default.join(directory, source.relativeCwd), taskId, taskRef, input, checkoutState: 'present' };
163
+ cwd: path_1.default.join(directory, source.relativeCwd), taskId, taskRef, input, checkoutState: 'present',
164
+ disposalPolicy, checkoutIdentity: { dev: identity.dev, ino: identity.ino } };
138
165
  await this.write('lanes', lane.id, workspace);
139
166
  await this.validateWorkspace(workspace);
140
167
  return workspace;
141
168
  }
142
- async collect(workspace, successful) {
143
- return this.withLane(workspace.laneId, () => this.collectLocked(workspace, successful));
169
+ async collect(workspace, successful, options = {}) {
170
+ return this.withLane(workspace.laneId, async () => {
171
+ try {
172
+ return await this.collectLocked(workspace, successful, options);
173
+ }
174
+ catch (error) {
175
+ await this.recordHold(workspace.laneId, workspace.taskId, error, 'capture-failed');
176
+ throw error;
177
+ }
178
+ });
144
179
  }
145
- async collectLocked(workspace, successful) {
180
+ async collectLocked(workspace, successful, options) {
146
181
  const record = await this.read('lanes', workspace.laneId);
147
182
  if (!record || record.taskId !== workspace.taskId || record.blocked
148
183
  || record.checkoutState && record.checkoutState !== 'present') {
149
184
  throw new Error('Worker workspace task changed before collection');
150
185
  }
151
186
  await this.validateWorkspace(workspace);
152
- const checkpoint = await (0, GitCheckpoint_1.captureCheckpoint)(workspace.directory);
153
- const opaque = (await (0, GitCheckpoint_1.runGit)(workspace.directory, ['ls-tree', '-r', '-z', workspace.input.tree]))
154
- .toString('utf8').split('\0').filter(entry => entry.startsWith('160000 commit '))
155
- .map(entry => entry.slice(entry.indexOf('\t') + 1));
156
- const changed = (await (0, GitCheckpoint_1.runGit)(workspace.directory, ['diff', '--name-only', '--no-renames', '-z',
157
- workspace.input.tree, checkpoint.tree, '--'])).toString('utf8').split('\0').filter(Boolean);
158
- const opaqueChange = opaque.find(name => changed.some(file => file === name || file.startsWith(`${name}/`)));
159
- const nestedChange = checkpoint.repositories?.[0]?.path;
160
- const unsupported = nestedChange !== undefined || opaqueChange !== undefined;
161
- const output = await (0, GitCheckpoint_1.createCheckpointCommit)(workspace.directory, checkpoint.tree, workspace.input.commit, `Remote CLI worker artifact ${workspace.taskId}`);
162
- await (0, GitCheckpoint_1.runGit)(workspace.directory, ['update-ref', this.outputRef(workspace.taskId), output]);
163
- await (0, GitCheckpoint_1.runGit)(workspace.directory, ['update-ref', `refs/remote-cli/worker-history/${workspace.taskId}`, checkpoint.head]);
164
- const artifact = { taskId: workspace.taskId, laneId: workspace.laneId,
165
- threadId: workspace.threadId, workspaceGeneration: workspace.workspaceGeneration,
166
- source: workspace.source, input: workspace.input, output, tree: checkpoint.tree,
167
- originalHead: checkpoint.head, successful: successful && !unsupported,
168
- disposition: !unsupported && checkpoint.tree === workspace.input.tree ? 'applied' : 'pending', createdAt: Date.now() };
169
- // Durable artifact + refs precede any index/branch update in the owned checkout.
170
- await this.write('artifacts', artifact.taskId, artifact);
171
- if (unsupported) {
172
- await this.write('lanes', workspace.laneId, { ...workspace, blocked: true, pendingTaskId: artifact.taskId });
173
- throw new Error(`${nestedChange !== undefined ? 'Worker created nested Git metadata' : 'Worker changed an uninitialized submodule'} at ${JSON.stringify(nestedChange ?? opaqueChange)}; files and history were retained for manual recovery`);
174
- }
175
- const currentBranch = await (0, GitCheckpoint_1.gitText)(workspace.directory, ['branch', '--show-current']);
176
- if (currentBranch || checkpoint.head !== await (0, GitCheckpoint_1.gitText)(workspace.directory, ['rev-parse', 'HEAD'])) {
177
- await this.write('lanes', workspace.laneId, { ...workspace, blocked: true, pendingTaskId: artifact.taskId });
178
- throw new Error('Worker changed its branch during execution; the artifact is preserved for manual recovery');
179
- }
180
- // Mixed reset only updates OUR index/branch to the preserved working files. Never touches the parent index.
181
- await (0, GitCheckpoint_1.runGit)(workspace.directory, ['reset', '--mixed', output]);
182
- await (0, GitCheckpoint_1.runGit)(workspace.directory, ['update-ref', workspace.taskRef, output]);
183
- await this.assertClean(workspace.directory);
184
- await this.write('lanes', workspace.laneId, { ...workspace,
185
- pendingTaskId: artifact.disposition === 'pending' ? artifact.taskId : undefined });
187
+ const saved = await this.read('artifacts', workspace.taskId);
188
+ if (saved && record.disposalPolicy === 1) {
189
+ await this.validateArtifact(saved, { threadId: record.threadId, cwd: record.source.cwd, workspaceGeneration: record.workspaceGeneration });
190
+ if (saved.laneId !== record.laneId || saved.successful !== successful
191
+ || await (0, GitCheckpoint_1.gitText)(record.source.root, ['rev-parse', record.taskRef]) !== saved.output
192
+ || await (0, GitCheckpoint_1.gitText)(record.source.root, ['rev-parse', `refs/remote-cli/worker-history/${saved.taskId}`]) !== saved.originalHead) {
193
+ throw new WorkspaceDisposal_1.WorkspaceCleanupError('capture-receipt-incomplete');
194
+ }
195
+ // Retrying proof/export must never rewrite immutable output or native history refs.
196
+ return this.finishCollection(record, saved, options);
197
+ }
198
+ const artifact = await this.withRepository(workspace.source, async () => {
199
+ const checkpoint = await (0, GitCheckpoint_1.captureCheckpoint)(workspace.directory);
200
+ const opaque = (await (0, GitCheckpoint_1.runGit)(workspace.directory, ['ls-tree', '-r', '-z', workspace.input.tree]))
201
+ .toString('utf8').split('\0').filter(entry => entry.startsWith('160000 commit '))
202
+ .map(entry => entry.slice(entry.indexOf('\t') + 1));
203
+ const changed = (await (0, GitCheckpoint_1.runGit)(workspace.directory, ['diff', '--name-only', '--no-renames', '-z',
204
+ workspace.input.tree, checkpoint.tree, '--'])).toString('utf8').split('\0').filter(Boolean);
205
+ const opaqueChange = opaque.find(name => changed.some(file => file === name || file.startsWith(`${name}/`)));
206
+ const nestedChange = checkpoint.repositories?.[0]?.path;
207
+ const unsupported = nestedChange !== undefined || opaqueChange !== undefined;
208
+ const output = await (0, GitCheckpoint_1.createCheckpointCommit)(workspace.directory, checkpoint.tree, workspace.input.commit, `Remote CLI worker artifact ${workspace.taskId}`);
209
+ await (0, GitCheckpoint_1.runGit)(workspace.directory, ['update-ref', this.outputRef(workspace.taskId), output]);
210
+ await (0, GitCheckpoint_1.runGit)(workspace.directory, ['update-ref', `refs/remote-cli/worker-history/${workspace.taskId}`, checkpoint.head]);
211
+ const artifact = { taskId: workspace.taskId, laneId: workspace.laneId,
212
+ threadId: workspace.threadId, workspaceGeneration: workspace.workspaceGeneration,
213
+ source: workspace.source, input: workspace.input, output, tree: checkpoint.tree,
214
+ originalHead: checkpoint.head, successful: successful && !unsupported,
215
+ disposition: !unsupported && checkpoint.tree === workspace.input.tree ? 'applied' : 'pending', createdAt: Date.now(),
216
+ ...(record.disposalPolicy === 1 ? { disposalPolicy: 1,
217
+ ...(options.exitConfirmed === true ? { closeout: { taskId: workspace.taskId, output, tree: checkpoint.tree,
218
+ exitConfirmedAt: Date.now(), recoverable: false } } : {}) } : {}) };
219
+ // Durable artifact + refs precede any index/branch update in the owned checkout.
220
+ await this.write('artifacts', artifact.taskId, artifact);
221
+ if (unsupported) {
222
+ await this.write('lanes', workspace.laneId, { ...workspace, blocked: true, pendingTaskId: artifact.taskId });
223
+ throw new WorkspaceDisposal_1.WorkspaceCleanupError('nested-repository', false, `${nestedChange !== undefined ? 'Worker created nested Git metadata' : 'Worker changed an uninitialized submodule'}; files and history were retained for manual recovery`);
224
+ }
225
+ const currentBranch = await (0, GitCheckpoint_1.gitText)(workspace.directory, ['branch', '--show-current']);
226
+ if (currentBranch || checkpoint.head !== await (0, GitCheckpoint_1.gitText)(workspace.directory, ['rev-parse', 'HEAD'])) {
227
+ await this.write('lanes', workspace.laneId, { ...workspace, blocked: true, pendingTaskId: artifact.taskId });
228
+ throw new Error('Worker changed its branch during execution; the artifact is preserved for manual recovery');
229
+ }
230
+ // Mixed reset only updates OUR index/branch to the preserved working files. Never touches the parent index.
231
+ await (0, GitCheckpoint_1.runGit)(workspace.directory, ['reset', '--mixed', output]);
232
+ await (0, GitCheckpoint_1.runGit)(workspace.directory, ['update-ref', workspace.taskRef, output]);
233
+ await this.assertClean(workspace.directory);
234
+ // Persist pending before proof/export, so a failed proof cannot hide changed output.
235
+ await this.write('lanes', workspace.laneId, { ...record,
236
+ pendingTaskId: artifact.disposition === 'pending' ? artifact.taskId : undefined });
237
+ return artifact;
238
+ });
239
+ return this.finishCollection(record, artifact, options);
240
+ }
241
+ async finishCollection(record, saved, options) {
242
+ let artifact = saved;
243
+ if (artifact.disposalPolicy === 1 && options.exitConfirmed === true && !artifact.closeout) {
244
+ artifact = { ...artifact, closeout: { taskId: artifact.taskId, output: artifact.output, tree: artifact.tree,
245
+ exitConfirmedAt: Date.now(), recoverable: false } };
246
+ await this.write('artifacts', artifact.taskId, artifact);
247
+ }
248
+ if (artifact.disposalPolicy === 1 && artifact.closeout) {
249
+ const closeout = artifact.closeout;
250
+ if (options.exports !== undefined) {
251
+ const directory = await this.exportDirectory(artifact.taskId, true);
252
+ artifact = { ...artifact, exports: await (0, WorkspaceDisposal_1.exportWorkspaceFiles)(record.directory, directory, options.exports) };
253
+ await this.write('artifacts', artifact.taskId, artifact);
254
+ }
255
+ await this.verifyCheckout(record, artifact);
256
+ artifact = { ...artifact, closeout: { ...closeout, recoverable: true },
257
+ disposition: !artifact.successful && artifact.disposition === 'pending' ? 'retained' : artifact.disposition };
258
+ await this.write('artifacts', artifact.taskId, artifact);
259
+ await this.write('lanes', record.laneId, { ...record, cleanup: undefined,
260
+ pendingTaskId: artifact.disposition === 'pending' ? artifact.taskId : undefined });
261
+ }
262
+ else if (artifact.disposalPolicy === 1) {
263
+ await this.recordHold(record.laneId, record.taskId, new WorkspaceDisposal_1.WorkspaceCleanupError('child-exit-unconfirmed'));
264
+ }
186
265
  return this.view(artifact);
187
266
  }
188
267
  async integrate(owner, taskId, action, expectedRevision) {
@@ -190,8 +269,8 @@ class DelegatedWorkspaceManager {
190
269
  const artifact = await this.read('artifacts', taskId);
191
270
  if (!artifact)
192
271
  throw new Error('Worker artifact not found');
193
- const result = await this.withLane(artifact.laneId, () => this.integrateLocked(owner, taskId, action, expectedRevision));
194
- if (action === 'apply' || action === 'inspect' && result.deliveredAtHead) {
272
+ const result = await this.withLane(artifact.laneId, () => this.withRepository(artifact.source, () => this.integrateLocked(owner, taskId, action, expectedRevision)));
273
+ if (action === 'apply' || action === 'retain' || action === 'inspect' && result.deliveredAtHead) {
195
274
  await this.reclaim(taskId);
196
275
  }
197
276
  return result;
@@ -242,7 +321,7 @@ class DelegatedWorkspaceManager {
242
321
  return view;
243
322
  if (action === 'retain') {
244
323
  owner.checkActive?.();
245
- // Intentional non-integration is not deletion. The immutable artifact remains inspectable.
324
+ // Retain preserves the immutable artifact. Opted-in disposable checkouts can close out.
246
325
  await this.resolveArtifact(artifact, 'retained');
247
326
  return this.view({ ...artifact, disposition: 'retained' });
248
327
  }
@@ -338,6 +417,290 @@ class DelegatedWorkspaceManager {
338
417
  const artifact = await this.read('artifacts', taskId);
339
418
  return artifact ? this.view(artifact) : undefined;
340
419
  }
420
+ /** Metadata inventory only: no mkdir, worker-store initialization, Git commands or record repair. */
421
+ async enumerateArtifacts(options = {}) {
422
+ const records = new Map();
423
+ const workspaces = new Map();
424
+ const missingArtifacts = new Set();
425
+ let incomplete = false;
426
+ let bytes = 0;
427
+ const matches = (record) => (options.threadId === undefined || record.threadId === options.threadId)
428
+ && (options.cwd === undefined || record.source?.cwd === options.cwd)
429
+ && (options.workspaceGeneration === undefined || record.workspaceGeneration === options.workspaceGeneration)
430
+ && (options.laneId === undefined || record.laneId === options.laneId);
431
+ // Read current ownership before historical artifacts can exhaust the global budget.
432
+ // Scope filtering bounds retained memory, while the read budget still bounds total I/O.
433
+ for (const kind of ['lanes', 'artifacts']) {
434
+ const listing = await this.snapshotNames(kind);
435
+ incomplete ||= listing.incomplete;
436
+ bytes = 0;
437
+ const charge = (size) => {
438
+ bytes += size;
439
+ if (bytes > INVENTORY_BYTES)
440
+ throw new Error('Inventory byte budget exhausted');
441
+ };
442
+ for (const name of listing.names) {
443
+ try {
444
+ if (kind === 'artifacts') {
445
+ if (records.has(name))
446
+ continue;
447
+ const artifact = await this.readSnapshot(kind, name, charge);
448
+ if (!artifact || artifact.taskId !== name || !this.validSummaryArtifact(artifact))
449
+ throw new Error('Invalid artifact');
450
+ if (matches(artifact))
451
+ records.set(name, artifact);
452
+ }
453
+ else {
454
+ const workspace = await this.readSnapshot(kind, name, charge);
455
+ if (!workspace || workspace.laneId !== name || !UUID.test(workspace.taskId)
456
+ || typeof workspace.threadId !== 'string' || typeof workspace.source?.cwd !== 'string'
457
+ || !Number.isSafeInteger(workspace.workspaceGeneration))
458
+ throw new Error('Invalid workspace');
459
+ if (matches(workspace))
460
+ workspaces.set(workspace.taskId, workspace);
461
+ }
462
+ }
463
+ catch {
464
+ if (kind === 'artifacts')
465
+ missingArtifacts.delete(name);
466
+ incomplete = true;
467
+ if (bytes > INVENTORY_BYTES)
468
+ break;
469
+ }
470
+ }
471
+ if (kind === 'lanes') {
472
+ // Exact current task IDs remain obligations even if historical enumeration
473
+ // is truncated. This bounded pass never scans the entire store again.
474
+ let currentBytes = 0;
475
+ for (const workspace of workspaces.values()) {
476
+ try {
477
+ const artifact = await this.readSnapshot('artifacts', workspace.taskId, size => {
478
+ currentBytes += size;
479
+ if (currentBytes > INVENTORY_BYTES)
480
+ throw new Error('Current artifact byte budget exhausted');
481
+ });
482
+ if (!artifact) {
483
+ missingArtifacts.add(workspace.taskId);
484
+ continue;
485
+ }
486
+ if (!this.validSummaryArtifact(artifact) || artifact.taskId !== workspace.taskId
487
+ || artifact.laneId !== workspace.laneId || !matches(artifact))
488
+ throw new Error('Invalid current artifact');
489
+ records.set(artifact.taskId, artifact);
490
+ }
491
+ catch {
492
+ incomplete = true;
493
+ if (currentBytes > INVENTORY_BYTES)
494
+ break;
495
+ }
496
+ }
497
+ }
498
+ }
499
+ const items = [];
500
+ for (const taskId of new Set([...records.keys(), ...workspaces.keys()])) {
501
+ const artifact = records.get(taskId);
502
+ const workspace = workspaces.get(taskId);
503
+ const item = this.summary(artifact, workspace);
504
+ if (!artifact && workspace && missingArtifacts.has(taskId)) {
505
+ item.discarded = await (0, LegacyWorkspaceCleanup_1.completedLegacyDiscard)(this.directory, workspace.laneId, taskId);
506
+ }
507
+ if (options.threadId !== undefined && item.threadId !== options.threadId
508
+ || options.cwd !== undefined && item.cwd !== options.cwd
509
+ || options.workspaceGeneration !== undefined && item.workspaceGeneration !== options.workspaceGeneration)
510
+ continue;
511
+ if (options.laneId !== undefined && item.laneId !== options.laneId)
512
+ continue;
513
+ if (options.unresolvedOnly && item.disposition !== 'pending' && !item.recovery
514
+ && (item.disposition !== undefined || item.discarded))
515
+ continue;
516
+ items.push(item);
517
+ }
518
+ items.sort((a, b) => a.taskId.localeCompare(b.taskId));
519
+ const offset = Number.isSafeInteger(options.offset) ? Math.max(0, options.offset) : 0;
520
+ const limit = Number.isSafeInteger(options.limit) ? Math.max(0, Math.min(exports.WORKSPACE_INVENTORY_PAGE_LIMIT, options.limit)) : 100;
521
+ return { items: items.slice(offset, offset + limit), total: items.length, incomplete };
522
+ }
523
+ /** A caller must still hold its admission reservation; this is not an inactivity proof. */
524
+ async getLaneCloseout(laneId) {
525
+ const workspace = await this.readSnapshot('lanes', laneId);
526
+ if (!workspace)
527
+ return undefined;
528
+ const artifact = await this.readSnapshot('artifacts', workspace.taskId);
529
+ if (artifact && (!this.validSummaryArtifact(artifact) || artifact.laneId !== laneId))
530
+ throw new Error('Invalid lane artifact');
531
+ const item = this.summary(artifact, workspace);
532
+ if (!artifact)
533
+ item.discarded = await (0, LegacyWorkspaceCleanup_1.completedLegacyDiscard)(this.directory, laneId, workspace.taskId);
534
+ return item;
535
+ }
536
+ async getArtifactExports(taskId) {
537
+ const artifact = await this.readSnapshot('artifacts', taskId);
538
+ if (!artifact || !this.validSummaryArtifact(artifact))
539
+ throw new Error('Artifact is unavailable');
540
+ const directory = await this.exportDirectory(taskId);
541
+ await (0, WorkspaceDisposal_1.verifyWorkspaceExports)(directory, artifact.exports ?? []);
542
+ return (artifact.exports ?? []).map(entry => ({ ...entry, file: path_1.default.join(directory, entry.sha256) }));
543
+ }
544
+ async enumerateIntegrationScratch() {
545
+ const listing = await this.snapshotNames('scratch');
546
+ const items = [];
547
+ let incomplete = listing.incomplete;
548
+ for (const id of listing.names) {
549
+ try {
550
+ const record = await this.readSnapshot('scratch', id);
551
+ if (!record || record.id !== id || !UUID.test(record.taskId) || !UUID.test(record.laneId)
552
+ || record.directory !== path_1.default.join(this.directory, `integration-${id}`)
553
+ || !['allocating', 'present', 'conflict', 'cleanup-pending', 'reclaimed'].includes(record.state))
554
+ throw new Error('Invalid scratch receipt');
555
+ items.push(record);
556
+ }
557
+ catch {
558
+ incomplete = true;
559
+ }
560
+ }
561
+ return { items, incomplete };
562
+ }
563
+ /** Owner-only retry. The owner must reserve these lanes and exclude every live/setup lane. */
564
+ async retryReclamation(options = {}) {
565
+ const inventory = await this.enumerateArtifacts({ limit: INVENTORY_LIMIT });
566
+ const result = { attempted: 0, reclaimed: 0, held: 0, incomplete: inventory.incomplete };
567
+ if (inventory.total > inventory.items.length)
568
+ result.incomplete = true;
569
+ if (result.incomplete)
570
+ return result;
571
+ const limit = Number.isSafeInteger(options.limit) ? Math.max(0, Math.min(100, options.limit)) : 10;
572
+ for (const item of inventory.items) {
573
+ if (result.attempted >= limit)
574
+ break;
575
+ if (item.disposalPolicy !== 1 || !item.exitConfirmed || !item.recoverable || item.blocked
576
+ || item.pendingTaskId || item.recovery || !['applied', 'retained'].includes(item.disposition ?? '')
577
+ || item.checkoutState === undefined || item.checkoutState === 'reclaimed' || options.excludedLaneIds?.has(item.laneId)
578
+ || item.cleanup && (!item.cleanup.retryable || item.cleanup.attempts >= CLEANUP_ATTEMPTS
579
+ || (item.cleanup.nextRetryAt ?? 0) > Date.now()))
580
+ continue;
581
+ result.attempted++;
582
+ if (await this.reclaim(item.taskId))
583
+ result.reclaimed++;
584
+ else
585
+ result.held++;
586
+ }
587
+ return result;
588
+ }
589
+ validSummaryArtifact(artifact) {
590
+ return UUID.test(artifact.taskId) && UUID.test(artifact.laneId) && typeof artifact.threadId === 'string'
591
+ && Number.isSafeInteger(artifact.workspaceGeneration) && typeof artifact.source?.cwd === 'string'
592
+ && GitCheckpoint_1.GIT_OID.test(artifact.input?.commit) && GitCheckpoint_1.GIT_OID.test(artifact.input?.tree) && GitCheckpoint_1.GIT_OID.test(artifact.output)
593
+ && GitCheckpoint_1.GIT_OID.test(artifact.tree) && typeof artifact.successful === 'boolean'
594
+ && ['pending', 'applied', 'retained'].includes(artifact.disposition);
595
+ }
596
+ summary(artifact, workspace) {
597
+ const owner = artifact ?? workspace;
598
+ return { taskId: owner.taskId, laneId: owner.laneId, threadId: owner.threadId,
599
+ workspaceGeneration: owner.workspaceGeneration, cwd: owner.source.cwd,
600
+ disposition: artifact?.disposition, successful: artifact?.successful,
601
+ outputCommit: artifact?.output, baseCommit: artifact?.input?.commit, exports: artifact?.exports,
602
+ disposalPolicy: workspace?.disposalPolicy, checkoutState: workspace?.checkoutState,
603
+ blocked: workspace?.blocked, pendingTaskId: workspace?.pendingTaskId,
604
+ exitConfirmed: artifact?.closeout?.taskId === artifact?.taskId && Number.isFinite(artifact?.closeout?.exitConfirmedAt),
605
+ recoverable: artifact?.closeout?.recoverable === true, recovery: artifact?.recovery,
606
+ cleanup: workspace?.cleanup,
607
+ holdReason: workspace?.cleanup?.reason ?? (workspace?.blocked ? 'preserved-for-recovery'
608
+ : !artifact ? 'artifact-unavailable' : !workspace ? 'workspace-unavailable' : undefined) };
609
+ }
610
+ async snapshotNames(kind) {
611
+ const names = [];
612
+ try {
613
+ const directory = path_1.default.join(this.directory, kind);
614
+ if (await promises_1.default.realpath(directory) !== directory || await promises_1.default.realpath(this.directory) !== this.directory) {
615
+ return { names, incomplete: true };
616
+ }
617
+ const handle = await promises_1.default.opendir(directory);
618
+ let count = 0;
619
+ let incomplete = false;
620
+ for await (const entry of handle) {
621
+ if (++count > INVENTORY_LIMIT) {
622
+ incomplete = true;
623
+ break;
624
+ }
625
+ if (!entry.name.endsWith('.json'))
626
+ continue;
627
+ const id = entry.name.slice(0, -5);
628
+ if (!UUID.test(id) || !entry.isFile() || entry.isSymbolicLink()) {
629
+ incomplete = true;
630
+ continue;
631
+ }
632
+ names.push(id);
633
+ }
634
+ return { names: names.sort(), incomplete };
635
+ }
636
+ catch (error) {
637
+ return { names, incomplete: error.code !== 'ENOENT' };
638
+ }
639
+ }
640
+ async readSnapshot(kind, id, charge) {
641
+ this.assertId(id);
642
+ const directory = path_1.default.join(this.directory, kind);
643
+ try {
644
+ if (await promises_1.default.realpath(this.directory) !== this.directory || await promises_1.default.realpath(directory) !== directory)
645
+ throw new Error('Unsafe inventory path');
646
+ const file = path_1.default.join(directory, `${id}.json`);
647
+ const stat = await promises_1.default.lstat(file);
648
+ if (!stat.isFile() || stat.isSymbolicLink() || stat.size > RECORD_BYTES)
649
+ throw new Error('Invalid inventory record');
650
+ charge?.(stat.size);
651
+ const handle = await promises_1.default.open(file, fs_1.constants.O_RDONLY | (fs_1.constants.O_NOFOLLOW ?? 0));
652
+ try {
653
+ const opened = await handle.stat();
654
+ if (opened.ino !== stat.ino || opened.dev !== stat.dev || opened.size !== stat.size)
655
+ throw new Error('Inventory changed');
656
+ const buffer = Buffer.alloc(RECORD_BYTES + 1);
657
+ const { bytesRead } = await handle.read(buffer, 0, buffer.length, 0);
658
+ const after = await handle.stat();
659
+ if (bytesRead !== stat.size || after.size !== stat.size || after.mtimeMs !== stat.mtimeMs)
660
+ throw new Error('Inventory changed');
661
+ return JSON.parse(buffer.subarray(0, bytesRead).toString('utf8'));
662
+ }
663
+ finally {
664
+ await handle.close();
665
+ }
666
+ }
667
+ catch (error) {
668
+ if (error.code === 'ENOENT')
669
+ return undefined;
670
+ throw error;
671
+ }
672
+ }
673
+ async exportDirectory(taskId, create = false) {
674
+ this.assertId(taskId);
675
+ const root = path_1.default.join(this.directory, 'exports');
676
+ const directory = path_1.default.join(root, taskId);
677
+ if (create) {
678
+ await this.initialize();
679
+ await this.safeDirectory(root);
680
+ await this.safeDirectory(directory);
681
+ }
682
+ return directory;
683
+ }
684
+ async recordHold(laneId, taskId, error, fallback = 'verification-failed') {
685
+ try {
686
+ const workspace = await this.read('lanes', laneId);
687
+ if (!workspace || workspace.taskId !== taskId || workspace.disposalPolicy !== 1)
688
+ return;
689
+ const detail = error instanceof Error ? error.message : '';
690
+ const reason = error instanceof WorkspaceDisposal_1.WorkspaceCleanupError ? error.reason
691
+ : /checkout file|Saved checkout|file mode|blob|bytes/i.test(detail) ? 'tracked-files-changed'
692
+ : /identity|reference|registration|receipt|locked|no longer/.test(detail) ? 'identity-or-registration-changed' : fallback;
693
+ if (reason === 'preserved-for-recovery' && workspace.cleanup)
694
+ return;
695
+ const attempts = Math.min(CLEANUP_ATTEMPTS, (workspace.cleanup?.attempts ?? 0) + 1);
696
+ const retryable = error instanceof WorkspaceDisposal_1.WorkspaceCleanupError && error.retryable;
697
+ await this.write('lanes', laneId, { ...workspace, cleanup: { reason, attempts, retryable, updatedAt: Date.now(),
698
+ ...(retryable && attempts < CLEANUP_ATTEMPTS ? { nextRetryAt: Date.now() + 1000 * 2 ** attempts } : {}) } });
699
+ }
700
+ catch {
701
+ console.warn('[Delegation] Cleanup hold could not be persisted; the checkout remains protected');
702
+ }
703
+ }
341
704
  /** Closeout needs a fresh, owned receipt, not a cached result or an absent record. */
342
705
  async inspectCloseout(owner, taskId) {
343
706
  const saved = await this.read('artifacts', taskId);
@@ -350,6 +713,11 @@ class DelegatedWorkspaceManager {
350
713
  throw new Error('Invalid worker artifact receipt');
351
714
  await this.validateArtifact(artifact, owner);
352
715
  owner.checkActive?.();
716
+ if (artifact.disposalPolicy === 1 && (!artifact.closeout?.recoverable
717
+ || artifact.closeout.taskId !== artifact.taskId || artifact.closeout.output !== artifact.output
718
+ || artifact.closeout.tree !== artifact.tree || !Number.isFinite(artifact.closeout.exitConfirmedAt))) {
719
+ throw new WorkspaceDisposal_1.WorkspaceCleanupError('recovery-unverified');
720
+ }
353
721
  const workspace = await this.read('lanes', artifact.laneId);
354
722
  if (artifact.disposition !== 'retained' && workspace?.taskId === taskId && workspace.blocked) {
355
723
  throw new Error('Worker artifact collection requires recovery');
@@ -367,8 +735,10 @@ class DelegatedWorkspaceManager {
367
735
  async preserveLane(laneId) {
368
736
  await this.withLane(laneId, async () => {
369
737
  const workspace = await this.read('lanes', laneId);
370
- if (workspace)
738
+ if (workspace) {
371
739
  await this.write('lanes', laneId, { ...workspace, blocked: true });
740
+ await this.recordHold(laneId, workspace.taskId, new WorkspaceDisposal_1.WorkspaceCleanupError('preserved-for-recovery'));
741
+ }
372
742
  });
373
743
  }
374
744
  /** Called only after worker exit or a durable delivery receipt; never races a cleanup deadline. */
@@ -378,7 +748,15 @@ class DelegatedWorkspaceManager {
378
748
  const artifact = await this.read('artifacts', taskId);
379
749
  if (!artifact)
380
750
  return false;
381
- return await this.withLane(artifact.laneId, () => this.reclaimLocked(taskId, artifact.laneId));
751
+ return await this.withLane(artifact.laneId, async () => {
752
+ try {
753
+ return await this.reclaimLocked(taskId, artifact.laneId);
754
+ }
755
+ catch (error) {
756
+ await this.recordHold(artifact.laneId, taskId, error, 'verification-failed');
757
+ return false;
758
+ }
759
+ });
382
760
  }
383
761
  catch {
384
762
  // Physical cleanup is optional. Do not turn a delivered result into a failed task.
@@ -389,12 +767,26 @@ class DelegatedWorkspaceManager {
389
767
  async reclaimLocked(taskId, laneId) {
390
768
  const workspace = await this.read('lanes', laneId);
391
769
  const artifact = await this.read('artifacts', taskId);
392
- if (!workspace || workspace.taskId !== taskId || !artifact || workspace.blocked || workspace.pendingTaskId
393
- || artifact.successful !== true || artifact.disposition !== 'applied' || artifact.recovery)
770
+ if (!workspace || workspace.taskId !== taskId || !artifact)
394
771
  return false;
395
- await this.validateReclamationArtifact(workspace, artifact);
396
772
  if (workspace.checkoutState === 'reclaimed')
397
773
  return false;
774
+ if (!this.disposalSupported && (workspace.disposalPolicy === 1 || artifact.disposalPolicy === 1)) {
775
+ throw new WorkspaceDisposal_1.WorkspaceCleanupError('disposal-platform-unsupported');
776
+ }
777
+ if (workspace.blocked)
778
+ throw new WorkspaceDisposal_1.WorkspaceCleanupError('preserved-for-recovery');
779
+ if (artifact.recovery)
780
+ throw new WorkspaceDisposal_1.WorkspaceCleanupError('parent-application-unresolved');
781
+ if (workspace.pendingTaskId || artifact.disposition === 'pending')
782
+ return false;
783
+ await this.validateReclamationArtifact(workspace, artifact);
784
+ if (workspace.checkoutState === 'reclaiming' && !await this.pathExists(workspace.directory)) {
785
+ if (await this.registration(workspace.source, workspace.directory))
786
+ throw new WorkspaceDisposal_1.WorkspaceCleanupError('partial-registration');
787
+ await this.write('lanes', laneId, { ...workspace, checkoutState: 'reclaimed', cleanup: undefined });
788
+ return true;
789
+ }
398
790
  await this.verifyCheckout(workspace, artifact);
399
791
  const intent = { ...workspace, checkoutState: 'reclaiming',
400
792
  reclamation: { taskId, output: artifact.output, tree: artifact.tree } };
@@ -409,11 +801,22 @@ class DelegatedWorkspaceManager {
409
801
  if (!registered || registered.locked || registered.head !== artifact.output) {
410
802
  throw new Error('Worker registration changed before removal');
411
803
  }
412
- await (0, GitCheckpoint_1.runGit)(workspace.source.root, ['worktree', 'remove', workspace.directory]);
804
+ try {
805
+ await (0, GitCheckpoint_1.runGit)(workspace.source.root, ['worktree', 'remove',
806
+ ...(workspace.disposalPolicy === 1 ? ['--force'] : []), workspace.directory]);
807
+ }
808
+ catch {
809
+ throw new WorkspaceDisposal_1.WorkspaceCleanupError('removal-failed', true);
810
+ }
413
811
  if (await this.pathExists(workspace.directory) || await this.registration(workspace.source, workspace.directory)) {
414
812
  throw new Error('Worker checkout removal was not confirmed');
415
813
  }
416
- await this.write('lanes', laneId, { ...intent, checkoutState: 'reclaimed' });
814
+ try {
815
+ await this.write('lanes', laneId, { ...intent, checkoutState: 'reclaimed', cleanup: undefined });
816
+ }
817
+ catch {
818
+ throw new WorkspaceDisposal_1.WorkspaceCleanupError('receipt-write-failed', true);
819
+ }
417
820
  return true;
418
821
  });
419
822
  }
@@ -421,9 +824,30 @@ class DelegatedWorkspaceManager {
421
824
  await this.validateWorkspaceRecord(workspace);
422
825
  await this.validateArtifact(artifact, { threadId: workspace.threadId, cwd: workspace.source.cwd,
423
826
  workspaceGeneration: workspace.workspaceGeneration });
827
+ if (workspace.disposalPolicy === 1 || artifact.disposalPolicy !== undefined) {
828
+ if (workspace.disposalPolicy !== 1 || artifact.disposalPolicy !== 1)
829
+ throw new WorkspaceDisposal_1.WorkspaceCleanupError('policy-mismatch');
830
+ if (!artifact.closeout)
831
+ throw new WorkspaceDisposal_1.WorkspaceCleanupError('child-exit-unconfirmed');
832
+ const receipt = artifact.closeout;
833
+ if (receipt.taskId !== artifact.taskId || receipt.output !== artifact.output || receipt.tree !== artifact.tree
834
+ || !Number.isFinite(receipt.exitConfirmedAt) || receipt.exitConfirmedAt <= 0)
835
+ throw new WorkspaceDisposal_1.WorkspaceCleanupError('invalid-exit-receipt');
836
+ if (receipt.recoverable !== true)
837
+ throw new WorkspaceDisposal_1.WorkspaceCleanupError('recovery-unverified');
838
+ if (artifact.disposition !== 'applied' && artifact.disposition !== 'retained')
839
+ throw new WorkspaceDisposal_1.WorkspaceCleanupError('delivery-pending');
840
+ if (!artifact.successful && artifact.disposition === 'applied' && artifact.tree !== artifact.input.tree) {
841
+ throw new WorkspaceDisposal_1.WorkspaceCleanupError('invalid-failed-disposition');
842
+ }
843
+ await (0, WorkspaceDisposal_1.verifyWorkspaceExports)(await this.exportDirectory(artifact.taskId), artifact.exports ?? []);
844
+ }
845
+ else if (artifact.successful !== true || artifact.disposition !== 'applied') {
846
+ throw new WorkspaceDisposal_1.WorkspaceCleanupError('legacy-policy');
847
+ }
424
848
  if (artifact.laneId !== workspace.laneId || artifact.taskId !== workspace.taskId
425
849
  || artifact.input.commit !== workspace.input.commit || artifact.input.tree !== workspace.input.tree
426
- || artifact.successful !== true || artifact.disposition !== 'applied' || artifact.recovery
850
+ || artifact.recovery
427
851
  || workspace.blocked || workspace.pendingTaskId
428
852
  || !GitCheckpoint_1.GIT_OID.test(artifact.originalHead)
429
853
  || await (0, GitCheckpoint_1.gitText)(workspace.source.root, ['rev-parse', workspace.taskRef]) !== artifact.output
@@ -446,14 +870,14 @@ class DelegatedWorkspaceManager {
446
870
  const exists = await this.pathExists(workspace.directory);
447
871
  const registered = await this.registration(workspace.source, workspace.directory);
448
872
  if (!exists && !registered) {
449
- const reclaimed = { ...workspace, checkoutState: 'reclaimed' };
873
+ const reclaimed = { ...workspace, checkoutState: 'reclaimed', cleanup: undefined };
450
874
  await this.write('lanes', workspace.laneId, reclaimed);
451
875
  return reclaimed;
452
876
  }
453
877
  if (workspace.checkoutState === 'reclaiming' && exists && registered) {
454
878
  // A fully intact checkout can cancel an interrupted intent. Partial/foreign paths cannot.
455
879
  await this.verifyCheckout(workspace, artifact);
456
- const present = { ...workspace, checkoutState: 'present', reclamation: undefined };
880
+ const present = { ...workspace, checkoutState: 'present', reclamation: undefined, cleanup: undefined };
457
881
  await this.write('lanes', workspace.laneId, present);
458
882
  return present;
459
883
  }
@@ -461,6 +885,7 @@ class DelegatedWorkspaceManager {
461
885
  }
462
886
  async verifyCheckout(workspace, artifact) {
463
887
  await this.validateWorkspace(workspace);
888
+ const disposable = workspace.disposalPolicy === 1 && artifact.disposalPolicy === 1;
464
889
  const registered = await this.registration(workspace.source, workspace.directory);
465
890
  if (!registered || registered.locked || registered.head !== artifact.output
466
891
  || await (0, GitCheckpoint_1.gitText)(workspace.directory, ['branch', '--show-current'])
@@ -468,7 +893,16 @@ class DelegatedWorkspaceManager {
468
893
  || (await (0, GitCheckpoint_1.runGit)(workspace.directory, ['ls-files', '-u', '-z'])).length) {
469
894
  throw new Error('Worker checkout is locked, unresolved, or no longer at its saved output');
470
895
  }
471
- await this.assertClean(workspace.directory);
896
+ if (disposable) {
897
+ if ((await (0, GitCheckpoint_1.runGit)(workspace.directory, ['status', '--porcelain=v1', '-z', '--untracked-files=no'])).length) {
898
+ throw new WorkspaceDisposal_1.WorkspaceCleanupError('tracked-files-changed');
899
+ }
900
+ const entries = (await (0, GitCheckpoint_1.runGit)(workspace.directory, ['ls-files', '-v', '-z'])).toString('utf8').split('\0').filter(Boolean);
901
+ if (entries.some(entry => !entry.startsWith('H ')))
902
+ throw new WorkspaceDisposal_1.WorkspaceCleanupError('hidden-index-state');
903
+ }
904
+ else
905
+ await this.assertClean(workspace.directory);
472
906
  // Git status and a copied index can hide changed bytes behind assume-unchanged,
473
907
  // skip-worktree, or stat-cache flags. Hash actual bytes without filters or index metadata.
474
908
  const files = new Map();
@@ -493,6 +927,20 @@ class DelegatedWorkspaceManager {
493
927
  parent = path_1.default.posix.dirname(parent);
494
928
  }
495
929
  }
930
+ const lfsPaths = new Set();
931
+ const lfs = new LfsCheckout_1.LfsCheckoutVerifier(workspace.directory, workspace.source.root, this.directory);
932
+ const attributes = (await (0, GitCheckpoint_1.runGit)(workspace.directory, ['check-attr', '-z', '--stdin', 'filter'], Buffer.from([...files.keys()].join('\0') + '\0'))).toString('utf8').split('\0');
933
+ for (let index = 2; index < attributes.length; index += 3) {
934
+ if (attributes[index] === 'lfs')
935
+ lfsPaths.add(attributes[index - 2]);
936
+ else if (disposable && !['unspecified', 'unset'].includes(attributes[index])) {
937
+ throw new WorkspaceDisposal_1.WorkspaceCleanupError('unsupported-filter');
938
+ }
939
+ }
940
+ if (disposable) {
941
+ await (0, WorkspaceDisposal_1.assertDisposableTree)(workspace.directory);
942
+ await (0, WorkspaceDisposal_1.verifyWorkspaceExports)(await this.exportDirectory(artifact.taskId), artifact.exports ?? [], workspace.directory);
943
+ }
496
944
  const walk = async (relative) => {
497
945
  const absolute = path_1.default.join(workspace.directory, relative);
498
946
  if (await promises_1.default.realpath(absolute) !== absolute)
@@ -513,12 +961,19 @@ class DelegatedWorkspaceManager {
513
961
  files.delete(child);
514
962
  return;
515
963
  }
964
+ if (!expected && disposable)
965
+ return; // The complete boundary walk above validated disposable extras.
516
966
  if (!expected || (expected.mode === '120000' ? !stat.isSymbolicLink() : !stat.isFile())) {
517
967
  throw new Error('Checkout contains unknown, ignored, or unsupported files');
518
968
  }
519
969
  if (process.platform !== 'win32' && expected.mode !== '120000'
520
970
  && Boolean(stat.mode & 0o111) !== (expected.mode === '100755'))
521
971
  throw new Error('Checkout file mode changed');
972
+ if (lfsPaths.has(child)) {
973
+ await lfs.verify(file, expected.oid, expected.mode);
974
+ files.delete(child);
975
+ return;
976
+ }
522
977
  const hash = (0, crypto_1.createHash)(expected.oid.length === 64 ? 'sha256' : 'sha1');
523
978
  hash.update(`blob ${stat.size}\0`);
524
979
  if (stat.isSymbolicLink())
@@ -627,12 +1082,21 @@ class DelegatedWorkspaceManager {
627
1082
  }
628
1083
  // Compatible with Git 2.34: merge in an owned scratch checkout, never in the user's worktree.
629
1084
  const wrapper = await (0, GitCheckpoint_1.createCheckpointCommit)(artifact.source.root, current.tree, artifact.input.commit, `Remote CLI integration input ${artifact.taskId}`);
630
- const integrationDirectory = await promises_1.default.mkdtemp(path_1.default.join(this.directory, 'integration-'));
1085
+ const scratchId = (0, crypto_1.randomUUID)();
1086
+ const integrationDirectory = path_1.default.join(this.directory, `integration-${scratchId}`);
1087
+ const scratch = { id: scratchId, taskId: artifact.taskId, laneId: artifact.laneId,
1088
+ directory: integrationDirectory, source: artifact.source, state: 'allocating', updatedAt: Date.now() };
1089
+ await this.write('scratch', scratchId, scratch);
1090
+ const recordScratch = async (state, holdReason) => {
1091
+ await this.write('scratch', scratchId, { ...scratch, state, holdReason, updatedAt: Date.now() });
1092
+ };
1093
+ await promises_1.default.mkdir(integrationDirectory, { mode: 0o700 });
631
1094
  let created = false;
632
1095
  let preserve = false;
633
1096
  try {
634
1097
  await this.withRepository(artifact.source, () => (0, GitCheckpoint_1.runGit)(artifact.source.root, ['worktree', 'add', '--detach', integrationDirectory, wrapper]));
635
1098
  created = true;
1099
+ await recordScratch('present');
636
1100
  try {
637
1101
  await (0, GitCheckpoint_1.runGit)(integrationDirectory, ['-c', 'user.name=Remote CLI', '-c', 'user.email=remote-cli@example.com',
638
1102
  'merge', '--no-commit', '--no-ff', '--no-edit', artifact.output]);
@@ -643,6 +1107,7 @@ class DelegatedWorkspaceManager {
643
1107
  if (!conflicts.length)
644
1108
  throw error;
645
1109
  preserve = true;
1110
+ await recordScratch('conflict', 'integration-conflict');
646
1111
  return { ...await this.view(artifact), applied: false, conflicts: conflicts.slice(0, 60),
647
1112
  conflictsTruncated: conflicts.length > 60, recoveryDirectory: integrationDirectory,
648
1113
  detail: 'Merge conflicts are isolated here. The delivery workspace has not been changed.' };
@@ -681,13 +1146,25 @@ class DelegatedWorkspaceManager {
681
1146
  finally {
682
1147
  if (created && !preserve) {
683
1148
  // This directory contains only captured integration input and a reproducible merge result.
684
- await this.withRepository(artifact.source, () => (0, GitCheckpoint_1.runGit)(artifact.source.root, ['worktree', 'remove', '--force', integrationDirectory]))
685
- .catch(() => console.warn('[Delegation] Integration scratch worktree retained for manual cleanup'));
1149
+ try {
1150
+ await this.withRepository(artifact.source, () => (0, GitCheckpoint_1.runGit)(artifact.source.root, ['worktree', 'remove', '--force', integrationDirectory]));
1151
+ await recordScratch('reclaimed');
1152
+ }
1153
+ catch {
1154
+ await recordScratch('cleanup-pending', 'scratch-removal-failed').catch(() => undefined);
1155
+ console.warn('[Delegation] Integration scratch worktree retained for manual cleanup');
1156
+ }
686
1157
  }
687
1158
  else if (!created) {
688
1159
  // Failed setup can leave partial files; preserve them without masking the setup error.
689
- await promises_1.default.rmdir(integrationDirectory)
690
- .catch(() => console.warn('[Delegation] Partial integration directory retained for manual cleanup'));
1160
+ try {
1161
+ await promises_1.default.rmdir(integrationDirectory);
1162
+ await recordScratch('reclaimed');
1163
+ }
1164
+ catch {
1165
+ await recordScratch('cleanup-pending', 'scratch-setup-incomplete').catch(() => undefined);
1166
+ console.warn('[Delegation] Partial integration directory retained for manual cleanup');
1167
+ }
691
1168
  }
692
1169
  }
693
1170
  }
@@ -705,7 +1182,8 @@ class DelegatedWorkspaceManager {
705
1182
  baseCommit: artifact.input.commit, outputCommit: artifact.output,
706
1183
  changedFiles: files.slice(0, 60), filesTruncated: files.length > 60,
707
1184
  ...(artifact.deliveredAtHead ? { deliveredAtHead: artifact.deliveredAtHead } : {}),
708
- ...(artifact.recovery ? { recovery: artifact.recovery } : {}) };
1185
+ ...(artifact.recovery ? { recovery: artifact.recovery } : {}),
1186
+ ...(artifact.exports ? { exports: artifact.exports } : {}) };
709
1187
  }
710
1188
  async assertSelectedScope(artifact) {
711
1189
  // Rename detection hides the removed source path from --name-only; validate BOTH endpoints.
@@ -750,9 +1228,18 @@ class DelegatedWorkspaceManager {
750
1228
  }
751
1229
  if (!inside(workspace.directory, await promises_1.default.realpath(workspace.cwd)))
752
1230
  throw new Error('Worker directory escapes its worktree');
1231
+ if (workspace.disposalPolicy === 1) {
1232
+ const stat = await promises_1.default.lstat(workspace.directory);
1233
+ if (!stat.isDirectory() || stat.isSymbolicLink() || !workspace.checkoutIdentity
1234
+ || workspace.checkoutIdentity.dev !== stat.dev || workspace.checkoutIdentity.ino !== stat.ino) {
1235
+ throw new WorkspaceDisposal_1.WorkspaceCleanupError('checkout-identity-changed');
1236
+ }
1237
+ }
753
1238
  }
754
1239
  assertLifecycle(workspace) {
755
1240
  const state = workspace.checkoutState ?? 'present';
1241
+ if (workspace.disposalPolicy !== undefined && workspace.disposalPolicy !== 1)
1242
+ throw new Error('Unknown workspace disposal policy');
756
1243
  if (!['present', 'reclaiming', 'reclaimed'].includes(state)
757
1244
  || (state === 'present' ? workspace.reclamation !== undefined : !workspace.reclamation)) {
758
1245
  throw new Error('Invalid worker checkout lifecycle');
@@ -790,7 +1277,14 @@ class DelegatedWorkspaceManager {
790
1277
  return this.serialize(`lane:${path_1.default.resolve(this.directory)}:${laneId}`, operation);
791
1278
  }
792
1279
  withRepository(source, operation) {
793
- return this.serialize(`repository:${source.commonDirectory}`, operation);
1280
+ const held = this.repositoryContext.getStore();
1281
+ if (held?.has(source.commonDirectory))
1282
+ return operation();
1283
+ return this.serialize(`repository:${source.commonDirectory}`, async () => {
1284
+ await this.beforeMutation?.();
1285
+ const run = () => this.repositoryContext.run(new Set([...(held ?? []), source.commonDirectory]), operation);
1286
+ return this.ownership ? this.ownership.withRepository(source.commonDirectory, run) : run();
1287
+ });
794
1288
  }
795
1289
  async serialize(key, operation) {
796
1290
  const previous = DelegatedWorkspaceManager.operations.get(key) ?? Promise.resolve();
@@ -808,8 +1302,9 @@ class DelegatedWorkspaceManager {
808
1302
  }
809
1303
  }
810
1304
  async initialize() {
1305
+ await this.beforeMutation?.();
811
1306
  await this.safeDirectory(this.directory);
812
- await Promise.all(['lanes', 'artifacts', 'trees'].map(name => this.safeDirectory(path_1.default.join(this.directory, name))));
1307
+ await Promise.all(['lanes', 'artifacts', 'trees', 'scratch'].map(name => this.safeDirectory(path_1.default.join(this.directory, name))));
813
1308
  }
814
1309
  async safeDirectory(directory) {
815
1310
  await promises_1.default.mkdir(directory, { recursive: true, mode: 0o700 });