@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
@@ -0,0 +1,181 @@
1
+ import { execFileSync } from 'node:child_process';
2
+ import fs from 'node:fs';
3
+ import path from 'node:path';
4
+ import { randomUUID } from 'node:crypto';
5
+ import { buildProcessTreeStopCommand } from '../../server/process-control.js';
6
+ import { processIdentityMatches } from '../../server/process-state.js';
7
+ export const SANDBOX_CONTROL_AUDIT_MAX_BYTES = 1024 * 1024;
8
+ export const SANDBOX_CONTROL_EXECUTION_STOP_MS = 2_000;
9
+ function assertRegularFile(filePath) {
10
+ const stat = fs.lstatSync(filePath);
11
+ if (!stat.isFile() || stat.isSymbolicLink())
12
+ throw new Error('SANDBOX_CONTROL_STATE_INVALID');
13
+ }
14
+ export function atomicWriteJson(filePath, value, mode = 0o600, createParent = true) {
15
+ if (createParent)
16
+ fs.mkdirSync(path.dirname(filePath), { recursive: true, mode: 0o700 });
17
+ const temporary = path.join(path.dirname(filePath), `.${path.basename(filePath)}.${process.pid}.${randomUUID()}.tmp`);
18
+ try {
19
+ fs.writeFileSync(temporary, `${JSON.stringify(value)}\n`, { mode, flag: 'wx' });
20
+ fs.renameSync(temporary, filePath);
21
+ }
22
+ finally {
23
+ fs.rmSync(temporary, { force: true });
24
+ }
25
+ }
26
+ export function readJsonFile(filePath) {
27
+ assertRegularFile(filePath);
28
+ return JSON.parse(fs.readFileSync(filePath, 'utf8'));
29
+ }
30
+ export function statusPath(manifest) {
31
+ return path.join(manifest.publicStatusDir, 'status.json');
32
+ }
33
+ export function writeSandboxControlStatus(manifest, broker, state, reasonCode, activeRequestId, now = Date.now()) {
34
+ const status = {
35
+ version: 1, generation: manifest.generation, broker, state, reasonCode, activeRequestId, updatedAt: now
36
+ };
37
+ atomicWriteJson(statusPath(manifest), status, 0o600, false);
38
+ return status;
39
+ }
40
+ export function parseSandboxControlStatus(value) {
41
+ const status = value;
42
+ if (!status || status.version !== 1 || typeof status.generation !== 'string'
43
+ || !status.broker || !Number.isSafeInteger(status.broker.pid) || typeof status.broker.startTime !== 'string'
44
+ || !['starting', 'healthy', 'busy', 'parked'].includes(status.state ?? '')
45
+ || (status.reasonCode !== null && typeof status.reasonCode !== 'string')
46
+ || (status.activeRequestId !== null && typeof status.activeRequestId !== 'string')
47
+ || !Number.isSafeInteger(status.updatedAt))
48
+ throw new Error('SANDBOX_CONTROL_STATUS_INVALID');
49
+ return status;
50
+ }
51
+ export function readSandboxControlStatus(directory) {
52
+ return parseSandboxControlStatus(readJsonFile(path.join(directory, 'status.json')));
53
+ }
54
+ export function readActiveLease(manifest, now = Date.now()) {
55
+ const filePath = path.join(path.dirname(manifest.publicStatusDir), 'lease.json');
56
+ if (!fs.existsSync(filePath))
57
+ return null;
58
+ const lease = readJsonFile(filePath);
59
+ if (!lease || lease.version !== 1
60
+ || typeof lease.nonce !== 'string' || !lease.owner || !Number.isSafeInteger(lease.owner.pid)
61
+ || typeof lease.owner.startTime !== 'string' || !Number.isSafeInteger(lease.issuedAt)
62
+ || !Number.isSafeInteger(lease.expiresAt) || typeof lease.generation !== 'string'
63
+ || (lease.taskId !== null && typeof lease.taskId !== 'string')
64
+ || typeof lease.branch !== 'string' || typeof lease.reason !== 'string') {
65
+ throw new Error('SANDBOX_CONTROL_LEASE_INVALID');
66
+ }
67
+ if (lease.generation !== manifest.generation || lease.expiresAt <= now
68
+ || lease.taskId !== manifest.taskId || lease.branch !== manifest.branch)
69
+ return null;
70
+ return lease;
71
+ }
72
+ export function cleanupStaleSandboxControlLease(manifest, now = Date.now()) {
73
+ const filePath = path.join(path.dirname(manifest.publicStatusDir), 'lease.json');
74
+ if (!fs.existsSync(filePath))
75
+ return false;
76
+ const raw = fs.readFileSync(filePath, 'utf8');
77
+ const lease = JSON.parse(raw);
78
+ if (!lease || lease.version !== 1
79
+ || typeof lease.nonce !== 'string' || !lease.owner || !Number.isSafeInteger(lease.owner.pid)
80
+ || typeof lease.owner.startTime !== 'string' || !Number.isSafeInteger(lease.issuedAt)
81
+ || !Number.isSafeInteger(lease.expiresAt) || typeof lease.generation !== 'string'
82
+ || (lease.taskId !== null && typeof lease.taskId !== 'string')
83
+ || typeof lease.branch !== 'string' || typeof lease.reason !== 'string') {
84
+ throw new Error('SANDBOX_CONTROL_LEASE_INVALID');
85
+ }
86
+ const stale = lease.generation !== manifest.generation || lease.taskId !== manifest.taskId
87
+ || lease.branch !== manifest.branch || lease.expiresAt <= now || !processIdentityMatches({
88
+ version: 1, pid: lease.owner.pid, startTime: lease.owner.startTime
89
+ });
90
+ if (!stale)
91
+ return false;
92
+ if (fs.readFileSync(filePath, 'utf8') !== raw)
93
+ throw new Error('SANDBOX_CONTROL_LEASE_OWNERSHIP_LOST');
94
+ fs.unlinkSync(filePath);
95
+ return true;
96
+ }
97
+ export function appendSandboxControlAudit(manifest, event, fields = {}, now = Date.now()) {
98
+ const auditPath = path.join(path.dirname(manifest.publicStatusDir), 'audit.ndjson');
99
+ if (fs.existsSync(auditPath) && fs.statSync(auditPath).size >= SANDBOX_CONTROL_AUDIT_MAX_BYTES) {
100
+ fs.rmSync(`${auditPath}.1`, { force: true });
101
+ fs.renameSync(auditPath, `${auditPath}.1`);
102
+ }
103
+ fs.appendFileSync(auditPath, `${JSON.stringify({ version: 1, event, generation: manifest.generation, at: now, ...fields })}\n`, {
104
+ encoding: 'utf8', mode: 0o600
105
+ });
106
+ }
107
+ export function executionPath(manifest, requestId) {
108
+ return path.join(manifest.processingDir, requestId, 'execution.json');
109
+ }
110
+ export function readExecution(filePath) {
111
+ const execution = readJsonFile(filePath);
112
+ if (!execution || execution.version !== 1 || typeof execution.generation !== 'string'
113
+ || typeof execution.requestId !== 'string' || typeof execution.nonce !== 'string'
114
+ || !execution.child || !Number.isSafeInteger(execution.child.pid) || typeof execution.child.startTime !== 'string'
115
+ || !['prepared', 'running', 'terminating'].includes(execution.phase ?? '')
116
+ || !Number.isSafeInteger(execution.updatedAt))
117
+ throw new Error('SANDBOX_CONTROL_EXECUTION_INVALID');
118
+ return execution;
119
+ }
120
+ export function terminateSandboxControlExecution(execution, options = {}) {
121
+ const platform = options.platform ?? process.platform;
122
+ const processGroupId = execution.child.processGroupId;
123
+ const processGroupAlive = (groupId) => {
124
+ try {
125
+ const rows = execFileSync('ps', ['-axo', 'pgid=,stat='], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] });
126
+ return rows.split('\n').some((row) => {
127
+ const match = row.trim().match(/^(\d+)\s+(\S+)/);
128
+ return Number(match?.[1]) === groupId && !match?.[2]?.startsWith('Z');
129
+ });
130
+ }
131
+ catch {
132
+ try {
133
+ process.kill(-groupId, 0);
134
+ return true;
135
+ }
136
+ catch (error) {
137
+ return error.code === 'EPERM';
138
+ }
139
+ }
140
+ };
141
+ const treeAlive = () => {
142
+ if (platform === 'win32' || !processGroupId) {
143
+ return processIdentityMatches({ version: 1, pid: execution.child.pid, startTime: execution.child.startTime });
144
+ }
145
+ return processGroupAlive(processGroupId);
146
+ };
147
+ if (!treeAlive())
148
+ return true;
149
+ const command = buildProcessTreeStopCommand(processGroupId ?? execution.child.pid, platform);
150
+ try {
151
+ if (command.kind === 'exec')
152
+ execFileSync(command.command, command.args, { stdio: 'ignore' });
153
+ else
154
+ process.kill(command.pid, command.signal);
155
+ }
156
+ catch {
157
+ // The identity check below distinguishes an already-exited tree from a failed termination.
158
+ }
159
+ const deadline = Date.now() + (options.timeoutMs ?? SANDBOX_CONTROL_EXECUTION_STOP_MS);
160
+ while (Date.now() < deadline) {
161
+ if (!treeAlive())
162
+ return true;
163
+ Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 25);
164
+ }
165
+ if (platform !== 'win32') {
166
+ try {
167
+ process.kill(processGroupId ? -processGroupId : execution.child.pid, 'SIGKILL');
168
+ }
169
+ catch {
170
+ // The final identity check handles an already-exited tree.
171
+ }
172
+ const killDeadline = Date.now() + (options.timeoutMs ?? SANDBOX_CONTROL_EXECUTION_STOP_MS);
173
+ while (Date.now() < killDeadline) {
174
+ if (!treeAlive())
175
+ return true;
176
+ Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 25);
177
+ }
178
+ }
179
+ return !treeAlive();
180
+ }
181
+ //# sourceMappingURL=state.js.map
@@ -15,9 +15,9 @@ Commands:
15
15
  rebuild [--quiet] [--refresh]
16
16
  Rebuild the sandbox image (--refresh pulls base + tools)
17
17
  refresh Sync host Claude Code credentials to all sandbox copies
18
- rm <branch> | --all | --purge
18
+ rm <branch> | --unbound | --purge
19
19
  Remove one sandbox, all sandboxes not bound to an
20
- active task (--all), or tear down everything (--purge)
20
+ active task (--unbound), or tear down everything (--purge)
21
21
  start [--recreate] <branch | TASK-id | N>
22
22
  Verify or recover an existing sandbox container;
23
23
  optionally replace only the container on failure
@@ -1,6 +1,7 @@
1
1
  import path from 'node:path';
2
2
  import { run, runSafe } from './shell.js';
3
3
  import { removeDirRecursive } from '../remove-dir.js';
4
+ import { verifyWorktreePermit } from './worktree-safety.js';
4
5
  export function assertManagedPath(root, target) {
5
6
  const resolvedRoot = path.resolve(root);
6
7
  const resolvedTarget = path.resolve(target);
@@ -14,15 +15,26 @@ export function removeManagedDir(root, dir) {
14
15
  assertManagedPath(root, dir);
15
16
  removeDirRecursive(dir);
16
17
  }
17
- export function removeWorktreeDir(repoRoot, worktreeBase, dir, { runFn = run, runSafeFn = runSafe } = {}) {
18
+ export function removeWorktreeDir(repoRoot, worktreeBase, dir, permit, { runFn = run, runSafeFn = runSafe, allowRegisteredPathFallback = false } = {}) {
19
+ assertManagedPath(worktreeBase, dir);
20
+ if (path.resolve(permit.snapshot.worktree) !== path.resolve(dir)) {
21
+ throw new Error(`Worktree permit target mismatch: ${dir}`);
22
+ }
23
+ verifyWorktreePermit(permit);
18
24
  try {
19
25
  runFn('git', ['-C', repoRoot, 'worktree', 'remove', dir, '--force']);
20
26
  }
21
- catch {
27
+ catch (error) {
28
+ const message = error instanceof Error ? error.message : String(error);
29
+ const registeredPathMismatch = /(?:is not|not a valid) working tree|not a working tree/i.test(message);
30
+ if (!allowRegisteredPathFallback || !registeredPathMismatch) {
31
+ throw error;
32
+ }
22
33
  // On WSL2 the worktree is registered under its `/mnt/<drive>/...` path, so
23
34
  // `git worktree remove <windows-path>` cannot match it. Delete the managed
24
35
  // directory directly, then prune the now-dangling worktree metadata so the
25
36
  // branch is no longer reported as checked out.
37
+ verifyWorktreePermit(permit);
26
38
  removeManagedDir(worktreeBase, dir);
27
39
  runSafeFn('git', ['-C', repoRoot, 'worktree', 'prune']);
28
40
  }
@@ -1,6 +1,19 @@
1
1
  import path from 'node:path';
2
2
  import { shareBranchDir, shareCommonDir, shellConfigDirCandidates, worktreeDirCandidates } from './constants.js';
3
+ import { sandboxWorkspaceViewStatePaths } from './workspace-view.js';
3
4
  export function sandboxCoreBindMounts(config, branch, overrides) {
5
+ const taskBound = Boolean(overrides.taskSource && overrides.taskId);
6
+ const workspaceMounts = sandboxWorkspaceViewStatePaths(overrides.workspaceViewRoot).map(({ state, hostPath }) => taskBound && state === 'active'
7
+ ? {
8
+ hostPaths: [path.join(hostPath, '.short-ids.json')],
9
+ containerPath: '/workspace/.agents/workspace/active/.short-ids.json',
10
+ readOnly: true
11
+ }
12
+ : {
13
+ hostPaths: [hostPath],
14
+ containerPath: path.posix.join('/workspace/.agents/workspace', state),
15
+ readOnly: true
16
+ });
4
17
  const mounts = [
5
18
  {
6
19
  hostPaths: overrides.worktree
@@ -9,37 +22,35 @@ export function sandboxCoreBindMounts(config, branch, overrides) {
9
22
  containerPath: '/workspace',
10
23
  readOnly: false
11
24
  },
12
- {
13
- hostPaths: [overrides.workspaceViewRoot],
14
- containerPath: '/workspace/.agents/workspace',
15
- readOnly: true
16
- },
17
- {
18
- hostPaths: [shareCommonDir(config)],
19
- containerPath: '/share/common',
20
- readOnly: false
21
- },
22
- {
23
- hostPaths: [shareBranchDir(config, branch)],
24
- containerPath: '/share/branch',
25
- readOnly: false
26
- },
27
- {
28
- hostPaths: overrides.shellConfigHostDir
29
- ? [overrides.shellConfigHostDir]
30
- : shellConfigDirCandidates(config, branch),
31
- containerPath: '/home/devuser/.host-shell-config',
32
- readOnly: true
33
- }
25
+ ...workspaceMounts,
34
26
  ];
35
- if (overrides.taskSource && overrides.taskId) {
36
- mounts.splice(2, 0, {
27
+ if (taskBound) {
28
+ mounts.push({
37
29
  hostPaths: [overrides.taskSource],
38
30
  containerPath: path.posix.join('/workspace/.agents/workspace/active', overrides.taskId),
39
31
  readOnly: false
40
32
  });
41
33
  }
42
34
  mounts.push({
35
+ hostPaths: [shareCommonDir(config)],
36
+ containerPath: '/share/common',
37
+ readOnly: false
38
+ }, {
39
+ hostPaths: [shareBranchDir(config, branch)],
40
+ containerPath: '/share/branch',
41
+ readOnly: false
42
+ }, {
43
+ hostPaths: overrides.shellConfigHostDir
44
+ ? [overrides.shellConfigHostDir]
45
+ : shellConfigDirCandidates(config, branch),
46
+ containerPath: '/home/devuser/.host-shell-config',
47
+ readOnly: true
48
+ });
49
+ mounts.push({
50
+ hostPaths: [overrides.controlStatusDir],
51
+ containerPath: '/run/agent-infra/control-status',
52
+ readOnly: true
53
+ }, {
43
54
  hostPaths: [overrides.controlDir],
44
55
  containerPath: '/run/agent-infra/control',
45
56
  readOnly: false