@fitlab-ai/agent-infra 0.9.5 → 0.9.6

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 (106) hide show
  1. package/dist/bin/internal-cli.js +3 -3
  2. package/dist/lib/agent-clients/adapters/codex-lifecycle/app-server.js +126 -16
  3. package/dist/lib/agent-clients/adapters/codex-lifecycle/evidence.js +14 -2
  4. package/dist/lib/agent-clients/adapters/codex-lifecycle/store.js +48 -4
  5. package/dist/lib/agent-clients/adapters/codex.js +1 -1
  6. package/dist/lib/git/workflow.js +0 -1
  7. package/dist/lib/internal/codex-lifecycle.js +100 -2
  8. package/dist/lib/internal/release-workflow.js +108 -11
  9. package/dist/lib/internal/sandbox-control.js +43 -6
  10. package/dist/lib/internal/task-create.js +12 -4
  11. package/dist/lib/internal/task-orchestration.js +5 -4
  12. package/dist/lib/platform/issue-comments.js +2 -1
  13. package/dist/lib/release/workflow.js +29 -21
  14. package/dist/lib/sandbox/commands/create.js +15 -16
  15. package/dist/lib/sandbox/commands/prune.js +40 -16
  16. package/dist/lib/sandbox/commands/rm.js +318 -89
  17. package/dist/lib/sandbox/control/client.js +138 -22
  18. package/dist/lib/sandbox/control/executor.js +136 -0
  19. package/dist/lib/sandbox/control/lifecycle.js +348 -0
  20. package/dist/lib/sandbox/control/protocol.js +62 -26
  21. package/dist/lib/sandbox/control/server.js +222 -102
  22. package/dist/lib/sandbox/control/state.js +181 -0
  23. package/dist/lib/sandbox/index.js +2 -2
  24. package/dist/lib/sandbox/managed-fs.js +14 -2
  25. package/dist/lib/sandbox/mounts.js +35 -24
  26. package/dist/lib/sandbox/recovery.js +262 -45
  27. package/dist/lib/sandbox/workspace-view.js +73 -10
  28. package/dist/lib/sandbox/worktree-safety.js +196 -0
  29. package/dist/lib/server/process-control.js +6 -0
  30. package/dist/lib/task/artifact-lifecycle.js +5 -4
  31. package/dist/lib/task/codex-orchestration.js +161 -0
  32. package/dist/lib/task/commands/status.js +3 -0
  33. package/dist/lib/task/commands/validate.js +289 -0
  34. package/dist/lib/task/commit-finalization.js +36 -9
  35. package/dist/lib/task/delegation-receipts.js +25 -0
  36. package/dist/lib/task/events.js +10 -4
  37. package/dist/lib/task/finalization-artifacts.js +2 -1
  38. package/dist/lib/task/implementation-inputs.js +1 -7
  39. package/dist/lib/task/index.js +9 -0
  40. package/dist/lib/task/orchestration.js +133 -4
  41. package/dist/lib/task/verification-engine.js +38 -6
  42. package/dist/lib/task/verification.js +1 -0
  43. package/lib/agent-clients/adapters/codex-lifecycle/app-server.ts +142 -15
  44. package/lib/agent-clients/adapters/codex-lifecycle/evidence.ts +19 -3
  45. package/lib/agent-clients/adapters/codex-lifecycle/store.ts +51 -3
  46. package/lib/agent-clients/adapters/codex.ts +1 -1
  47. package/lib/git/workflow.ts +0 -1
  48. package/lib/internal/codex-lifecycle.ts +107 -3
  49. package/lib/internal/release-workflow.ts +107 -11
  50. package/lib/internal/sandbox-control.ts +38 -6
  51. package/lib/internal/task-create.ts +16 -4
  52. package/lib/internal/task-orchestration.ts +4 -4
  53. package/lib/platform/issue-comments.ts +2 -1
  54. package/lib/release/workflow.ts +69 -20
  55. package/lib/sandbox/commands/create.ts +17 -21
  56. package/lib/sandbox/commands/prune.ts +53 -15
  57. package/lib/sandbox/commands/rm.ts +368 -103
  58. package/lib/sandbox/control/client.ts +152 -28
  59. package/lib/sandbox/control/executor.ts +163 -0
  60. package/lib/sandbox/control/lifecycle.ts +342 -0
  61. package/lib/sandbox/control/protocol.ts +96 -69
  62. package/lib/sandbox/control/server.ts +239 -110
  63. package/lib/sandbox/control/state.ts +196 -0
  64. package/lib/sandbox/index.ts +2 -2
  65. package/lib/sandbox/managed-fs.ts +17 -2
  66. package/lib/sandbox/mounts.ts +31 -13
  67. package/lib/sandbox/recovery.ts +301 -60
  68. package/lib/sandbox/workspace-view.ts +81 -11
  69. package/lib/sandbox/worktree-safety.ts +234 -0
  70. package/lib/server/process-control.ts +11 -0
  71. package/lib/task/artifact-lifecycle.ts +5 -4
  72. package/lib/task/codex-orchestration.ts +233 -0
  73. package/lib/task/commands/status.ts +2 -0
  74. package/lib/task/commands/validate.ts +282 -0
  75. package/lib/task/commit-finalization.ts +52 -9
  76. package/lib/task/delegation-receipts.ts +52 -3
  77. package/lib/task/events.ts +9 -4
  78. package/lib/task/finalization-artifacts.ts +2 -1
  79. package/lib/task/implementation-inputs.ts +1 -5
  80. package/lib/task/index.ts +8 -0
  81. package/lib/task/orchestration.ts +199 -4
  82. package/lib/task/verification-engine.ts +42 -8
  83. package/lib/task/verification.ts +2 -1
  84. package/package.json +1 -1
  85. package/templates/.agents/hooks/lifecycle-delegation.js +48 -12
  86. package/templates/.agents/rules/lifecycle-orchestration.en.md +1 -1
  87. package/templates/.agents/rules/lifecycle-orchestration.zh-CN.md +1 -1
  88. package/templates/.agents/rules/no-mid-flow-questions.en.md +1 -0
  89. package/templates/.agents/rules/no-mid-flow-questions.zh-CN.md +1 -0
  90. package/templates/.agents/skills/post-release/SKILL.en.md +32 -6
  91. package/templates/.agents/skills/post-release/SKILL.zh-CN.md +32 -6
  92. package/templates/.agents/skills/run-manual-validation/SKILL.en.md +42 -0
  93. package/templates/.agents/skills/run-manual-validation/SKILL.zh-CN.md +42 -0
  94. package/templates/.agents/skills/run-manual-validation/config/verify.en.json +14 -0
  95. package/templates/.agents/skills/run-manual-validation/config/verify.zh-CN.json +14 -0
  96. package/templates/.agents/skills/run-manual-validation/reference/report-template.en.md +44 -0
  97. package/templates/.agents/skills/run-manual-validation/reference/report-template.zh-CN.md +44 -0
  98. package/templates/.agents/skills/run-task/SKILL.en.md +2 -2
  99. package/templates/.agents/skills/run-task/SKILL.zh-CN.md +2 -2
  100. package/templates/.agents/skills/run-task/reference/host-validation.en.md +3 -3
  101. package/templates/.agents/skills/run-task/reference/host-validation.zh-CN.md +3 -3
  102. package/templates/.claude/commands/run-manual-validation.en.md +9 -0
  103. package/templates/.claude/commands/run-manual-validation.zh-CN.md +9 -0
  104. package/templates/.codex/hooks.json +8 -8
  105. package/templates/.opencode/commands/run-manual-validation.en.md +11 -0
  106. package/templates/.opencode/commands/run-manual-validation.zh-CN.md +11 -0
@@ -2,10 +2,9 @@ import fs from 'node:fs';
2
2
  import os from 'node:os';
3
3
  import path from 'node:path';
4
4
  import { createHash } from 'node:crypto';
5
- import { execFileSync, spawn } from 'node:child_process';
5
+ import { execFileSync } from 'node:child_process';
6
6
  import type { ExecFileSyncOptions, StdioOptions } from 'node:child_process';
7
7
  import { parseArgs } from 'node:util';
8
- import { fileURLToPath } from 'node:url';
9
8
  import * as p from '@clack/prompts';
10
9
  import pc from 'picocolors';
11
10
  import { listAgentClientAdapters } from '../../agent-clients/registry.ts';
@@ -67,14 +66,16 @@ import type { SandboxTool, TmpfsSeedEntry } from '../tools.ts';
67
66
  import {
68
67
  assertFreshSandboxReady,
69
68
  hydrateTmpfsSeedEntries,
70
- prepareTmpfsMounts
69
+ prepareTmpfsMounts,
70
+ startSandboxControlBroker
71
71
  } from '../recovery.ts';
72
72
  import { hostJoin, toEnginePath, volumeArg } from '../engines/wsl2-paths.ts';
73
73
  import { sandboxCoreBindMounts } from '../mounts.ts';
74
74
  import {
75
75
  assertSandboxTaskSource,
76
76
  materializeSandboxControl,
77
- materializeSandboxWorkspaceView
77
+ materializeSandboxWorkspaceView,
78
+ prepareSandboxWorkspaceMountTargets
78
79
  } from '../workspace-view.ts';
79
80
  import { clipboardHostDir, CONTAINER_CLIPBOARD_MOUNT } from '../clipboard/paths.ts';
80
81
  import { validateSelinuxDisableEnv } from '../engines/selinux.ts';
@@ -157,12 +158,6 @@ type HostShellConfig = {
157
158
  mounts: Array<{ hostPath: string; containerPath: string }>;
158
159
  };
159
160
 
160
- function internalCliPath(): string {
161
- const directory = path.dirname(fileURLToPath(import.meta.url));
162
- const extension = path.extname(fileURLToPath(import.meta.url));
163
- return path.resolve(directory, '..', '..', '..', 'bin', `internal-cli${extension}`);
164
- }
165
-
166
161
  function resolveToolDirs(config: Pick<SandboxCreateConfig, 'project'>, tools: SandboxTool[], branch: string): ResolvedTool[] {
167
162
  return tools.map((tool) => {
168
163
  const candidates = toolConfigDirCandidates(tool, config.project, branch);
@@ -1254,9 +1249,13 @@ export async function create(args: string[]): Promise<void> {
1254
1249
  const requestedDescription = target.workspace.mode === 'task-bound'
1255
1250
  ? `task-bound:${target.workspace.taskId}`
1256
1251
  : target.workspace.mode;
1252
+ const recoveryTarget = target.workspace.mode === 'task-bound'
1253
+ ? target.workspace.taskId
1254
+ : branch;
1257
1255
  throw new Error(
1258
1256
  `SANDBOX_WORKSPACE_IDENTITY_CONFLICT: container '${name}' is ${existingDescription}, `
1259
- + `but this request is ${requestedDescription}. Run 'ai sandbox rm ${branch}' and retry.`
1257
+ + `but this request is ${requestedDescription}. `
1258
+ + `Run 'ai sandbox start --recreate ${recoveryTarget}' on the host, then retry.`
1260
1259
  );
1261
1260
  }
1262
1261
  }
@@ -1342,6 +1341,7 @@ export async function create(args: string[]): Promise<void> {
1342
1341
  container,
1343
1342
  identity: target.workspace
1344
1343
  });
1344
+ prepareSandboxWorkspaceMountTargets(worktree);
1345
1345
  const control = materializeSandboxControl({
1346
1346
  base: effectiveConfig.controlBase,
1347
1347
  repoRoot: effectiveConfig.repoRoot,
@@ -1362,6 +1362,7 @@ export async function create(args: string[]): Promise<void> {
1362
1362
  shellConfigHostDir: hostShellConfig.hostDir,
1363
1363
  workspaceViewRoot: workspaceView.root,
1364
1364
  controlDir: control.channelDir,
1365
+ controlStatusDir: control.statusDir,
1365
1366
  ...(target.workspace.mode === 'task-bound'
1366
1367
  ? {
1367
1368
  taskSource: assertSandboxTaskSource(
@@ -1445,7 +1446,11 @@ export async function create(args: string[]): Promise<void> {
1445
1446
  '-e',
1446
1447
  `AGENT_INFRA_CONTROL_TOKEN=${control.token}`,
1447
1448
  '-e',
1449
+ `AGENT_INFRA_CONTROL_GENERATION=${control.generation}`,
1450
+ '-e',
1448
1451
  'AGENT_INFRA_CONTROL_DIR=/run/agent-infra/control',
1452
+ '-e',
1453
+ 'AGENT_INFRA_CONTROL_STATUS_DIR=/run/agent-infra/control-status',
1449
1454
  '--label',
1450
1455
  `${sandboxRuntimeCapabilityLabel(effectiveConfig)}=${capabilityPlan.runtimeSignature}`,
1451
1456
  ...coreVolumes,
@@ -1467,16 +1472,7 @@ export async function create(args: string[]): Promise<void> {
1467
1472
  '/workspace',
1468
1473
  effectiveConfig.imageName
1469
1474
  ]);
1470
- const broker = spawn(
1471
- process.execPath,
1472
- [internalCliPath(), 'sandbox-control', 'serve', '--manifest', control.manifestPath],
1473
- { cwd: effectiveConfig.repoRoot, detached: true, stdio: 'ignore' }
1474
- );
1475
- broker.once('error', () => {
1476
- // Fresh readiness verification below remains the source of truth;
1477
- // consume detached spawn errors so they cannot crash the command.
1478
- });
1479
- broker.unref();
1475
+ await startSandboxControlBroker(effectiveConfig.repoRoot, control.manifestPath);
1480
1476
  createdTmpfsSeedPlan = tmpfsSeedPlan;
1481
1477
  } finally {
1482
1478
  envFile.cleanup();
@@ -6,16 +6,27 @@ import pc from 'picocolors';
6
6
  import { loadConfig } from '../config.ts';
7
7
  import type { SandboxConfig } from '../config.ts';
8
8
  import { safeNameCandidates, sandboxBranchLabel, sandboxLabel } from '../constants.ts';
9
- import { detectEngine } from '../engine.ts';
9
+ import { detectEngine, ENGINES } from '../engine.ts';
10
10
  import { hostJoin } from '../engines/wsl2-paths.ts';
11
11
  import { removeManagedDir, removeWorktreeDir } from '../managed-fs.ts';
12
12
  import { parseLabels } from './ls.ts';
13
13
  import { runEngine, runSafe } from '../shell.ts';
14
14
  import { createSandboxCapabilityPlan } from '../agent-client-reconciler.ts';
15
15
  import type { SandboxTool } from '../tools.ts';
16
+ import { createCleanPermit, formatWorktreeSnapshot, inspectWorktrees } from '../worktree-safety.ts';
17
+ import type { WorktreeRemovalPermit } from '../worktree-safety.ts';
16
18
 
17
19
  const USAGE = `Usage: ai sandbox prune [--dry-run]`;
18
20
 
21
+ type PruneDependencies = {
22
+ config?: SandboxConfig;
23
+ tools?: SandboxTool[];
24
+ engine?: string;
25
+ labelsOutput?: string;
26
+ confirm?: typeof p.confirm;
27
+ isCancel?: typeof p.isCancel;
28
+ };
29
+
19
30
  type OrphanKind = 'shell' | 'worktree' | 'share' | 'tool';
20
31
 
21
32
  export type OrphanGroup = {
@@ -107,12 +118,19 @@ export function collectOrphanGroups(
107
118
  return groups;
108
119
  }
109
120
 
110
- export function removeOrphanGroups(config: SandboxConfig, groups: OrphanGroup[]): boolean {
121
+ export function removeOrphanGroups(
122
+ config: SandboxConfig,
123
+ groups: OrphanGroup[],
124
+ permits: ReadonlyMap<string, WorktreeRemovalPermit>,
125
+ allowRegisteredPathFallback = false
126
+ ): boolean {
111
127
  let removedWorktrees = false;
112
128
  for (const group of groups) {
113
129
  for (const dir of group.dirs) {
114
130
  if (group.kind === 'worktree') {
115
- removeWorktreeDir(config.repoRoot, group.base, dir);
131
+ const permit = permits.get(path.resolve(dir));
132
+ if (!permit) throw new Error(`Missing worktree removal permit: ${dir}`);
133
+ removeWorktreeDir(config.repoRoot, group.base, dir, permit, { allowRegisteredPathFallback });
116
134
  removedWorktrees = true;
117
135
  } else {
118
136
  removeManagedDir(group.base, dir);
@@ -142,7 +160,7 @@ function writeGroups(groups: OrphanGroup[]): void {
142
160
  }
143
161
  }
144
162
 
145
- export async function prune(args: string[]): Promise<void> {
163
+ export async function prune(args: string[], dependencies: PruneDependencies = {}): Promise<void> {
146
164
  const { values } = parseArgs({
147
165
  args,
148
166
  strict: true,
@@ -157,9 +175,9 @@ export async function prune(args: string[]): Promise<void> {
157
175
  return;
158
176
  }
159
177
 
160
- const config = loadConfig();
161
- const tools = [...createSandboxCapabilityPlan(config).cleanupInventory];
162
- const engine = detectEngine(config);
178
+ const config = dependencies.config ?? loadConfig();
179
+ const tools = dependencies.tools ?? [...createSandboxCapabilityPlan(config).cleanupInventory];
180
+ const engine = dependencies.engine ?? detectEngine(config);
163
181
  const psArgs = [
164
182
  'ps',
165
183
  '-a',
@@ -168,14 +186,25 @@ export async function prune(args: string[]): Promise<void> {
168
186
  '--format',
169
187
  '{{.Labels}}'
170
188
  ];
171
- let labelsOutput: string;
172
- try {
173
- labelsOutput = runEngine(engine, 'docker', psArgs);
174
- } catch {
175
- throw new Error('Unable to determine active sandbox branches: docker ps failed');
189
+ let labelsOutput = dependencies.labelsOutput;
190
+ if (labelsOutput === undefined) {
191
+ try {
192
+ labelsOutput = runEngine(engine, 'docker', psArgs);
193
+ } catch {
194
+ throw new Error('Unable to determine active sandbox branches: docker ps failed');
195
+ }
176
196
  }
197
+ const confirm = dependencies.confirm ?? p.confirm;
198
+ const isCancel = dependencies.isCancel ?? p.isCancel;
177
199
  const groups = collectOrphanGroups(config, tools, activeBranchesFromLabels(config, labelsOutput));
178
200
  const count = orphanCount(groups);
201
+ const worktrees = groups.filter((group) => group.kind === 'worktree').flatMap((group) => group.dirs);
202
+ const inspections = inspectWorktrees(worktrees);
203
+ const blockers = inspections.filter((inspection) => inspection.status !== 'clean');
204
+ const permits = new Map<string, WorktreeRemovalPermit>();
205
+ for (const inspection of inspections) {
206
+ if (inspection.status === 'clean') permits.set(inspection.snapshot.worktree, createCleanPermit(inspection.snapshot));
207
+ }
179
208
 
180
209
  p.intro(pc.cyan(`Pruning orphaned sandbox state for ${config.project}`));
181
210
 
@@ -186,23 +215,32 @@ export async function prune(args: string[]): Promise<void> {
186
215
  }
187
216
 
188
217
  writeGroups(groups);
218
+ for (const blocker of blockers) {
219
+ p.log.error(blocker.status === 'failed'
220
+ ? `${JSON.stringify(blocker.worktree)}: ${blocker.message}`
221
+ : formatWorktreeSnapshot(blocker.snapshot));
222
+ }
189
223
 
190
224
  if (values['dry-run']) {
191
225
  p.outro(pc.green('Dry run complete'));
192
226
  return;
193
227
  }
194
228
 
195
- const shouldRemove = await p.confirm({
229
+ if (blockers.length > 0) {
230
+ throw new Error('Refusing to prune because worktree preflight found blocker(s); no orphan state was deleted');
231
+ }
232
+
233
+ const shouldRemove = await confirm({
196
234
  message: `Remove ${count} orphaned sandbox state dirs?`,
197
235
  initialValue: true
198
236
  });
199
237
 
200
- if (p.isCancel(shouldRemove) || !shouldRemove) {
238
+ if (isCancel(shouldRemove) || !shouldRemove) {
201
239
  p.outro('Cancelled');
202
240
  return;
203
241
  }
204
242
 
205
- const removedWorktrees = removeOrphanGroups(config, groups);
243
+ const removedWorktrees = removeOrphanGroups(config, groups, permits, engine === ENGINES.WSL2);
206
244
  if (removedWorktrees) {
207
245
  runSafe('git', ['-C', config.repoRoot, 'worktree', 'prune']);
208
246
  }