@fitlab-ai/agent-infra 0.9.8 → 0.9.10

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 (182) hide show
  1. package/README.md +1 -1
  2. package/README.zh-CN.md +1 -1
  3. package/dist/bin/internal-cli.js +11 -1
  4. package/dist/lib/agent-clients/adapters/codex-lifecycle/app-server.js +58 -21
  5. package/dist/lib/agent-clients/adapters/codex-lifecycle/build-identity.js +25 -11
  6. package/dist/lib/agent-clients/adapters/codex-lifecycle/capability-store.js +173 -10
  7. package/dist/lib/agent-clients/adapters/codex-lifecycle/controller-context.js +127 -0
  8. package/dist/lib/agent-clients/adapters/codex-lifecycle/evidence.js +14 -0
  9. package/dist/lib/agent-clients/adapters/codex-lifecycle/sandbox-controller.js +63 -92
  10. package/dist/lib/agent-clients/adapters/codex-sandbox.js +72 -11
  11. package/dist/lib/agent-clients/adapters/index.js +3 -1
  12. package/dist/lib/agent-clients/adapters/traecli.js +66 -0
  13. package/dist/lib/agent-clients/tokens.js +2 -2
  14. package/dist/lib/agent-clients/types.js +2 -1
  15. package/dist/lib/defaults.json +5 -0
  16. package/dist/lib/git/workflow.js +10 -1
  17. package/dist/lib/internal/codex-lifecycle.js +20 -6
  18. package/dist/lib/internal/codex-sandbox-controller.js +9 -15
  19. package/dist/lib/internal/git-workflow.js +5 -7
  20. package/dist/lib/internal/platform-pr.js +20 -7
  21. package/dist/lib/internal/release-workflow.js +2 -1
  22. package/dist/lib/internal/sandbox-control.js +79 -2
  23. package/dist/lib/internal/task-finalization.js +54 -0
  24. package/dist/lib/internal/task-orchestration.js +156 -291
  25. package/dist/lib/{task/commands/validate.js → internal/task-validate.js} +75 -42
  26. package/dist/lib/platform/pr-summary.js +48 -10
  27. package/dist/lib/platform/pull-requests.js +205 -41
  28. package/dist/lib/run/tui.js +5 -2
  29. package/dist/lib/sandbox/commands/rm.js +316 -55
  30. package/dist/lib/sandbox/control/client.js +190 -6
  31. package/dist/lib/sandbox/control/controller-registration.js +275 -0
  32. package/dist/lib/sandbox/control/executor.js +140 -5
  33. package/dist/lib/sandbox/control/lifecycle.js +3 -1
  34. package/dist/lib/sandbox/control/protocol.js +67 -6
  35. package/dist/lib/sandbox/index.js +5 -3
  36. package/dist/lib/sandbox/managed-fs.js +5 -0
  37. package/dist/lib/sandbox/task-resolver.js +50 -6
  38. package/dist/lib/sandbox/workspace-identity.js +27 -0
  39. package/dist/lib/sandbox/worktree-safety.js +215 -2
  40. package/dist/lib/task/codex-orchestration.js +161 -39
  41. package/dist/lib/task/commands/status.js +59 -1
  42. package/dist/lib/task/commit-operation.js +335 -0
  43. package/dist/lib/task/commit-policy.js +18 -0
  44. package/dist/lib/task/delegation-receipts.js +248 -6
  45. package/dist/lib/task/finalization.js +593 -0
  46. package/dist/lib/task/index.js +0 -9
  47. package/dist/lib/task/operation-outcome.js +33 -0
  48. package/dist/lib/task/orchestration.js +187 -761
  49. package/dist/lib/task/short-id.js +59 -1
  50. package/dist/lib/task/task-execution-lock.js +6 -1
  51. package/dist/lib/task/verification-engine.js +110 -60
  52. package/dist/lib/task/verification.js +2 -1
  53. package/dist/lib/task/workflow-warning-intents.js +50 -1
  54. package/dist/lifecycle-build-manifest.json +3 -3
  55. package/lib/agent-clients/adapters/codex-lifecycle/app-server.ts +61 -19
  56. package/lib/agent-clients/adapters/codex-lifecycle/build-identity.ts +37 -10
  57. package/lib/agent-clients/adapters/codex-lifecycle/capability-store.ts +276 -12
  58. package/lib/agent-clients/adapters/codex-lifecycle/controller-context.ts +163 -0
  59. package/lib/agent-clients/adapters/codex-lifecycle/evidence.ts +19 -0
  60. package/lib/agent-clients/adapters/codex-lifecycle/manifest-files.json +9 -1
  61. package/lib/agent-clients/adapters/codex-lifecycle/sandbox-controller.ts +91 -118
  62. package/lib/agent-clients/adapters/codex-sandbox.ts +89 -14
  63. package/lib/agent-clients/adapters/index.ts +3 -1
  64. package/lib/agent-clients/adapters/traecli.ts +67 -0
  65. package/lib/agent-clients/tokens.ts +2 -2
  66. package/lib/agent-clients/types.ts +2 -1
  67. package/lib/defaults.json +5 -0
  68. package/lib/git/workflow.ts +11 -2
  69. package/lib/internal/codex-lifecycle.ts +19 -6
  70. package/lib/internal/codex-sandbox-controller.ts +9 -14
  71. package/lib/internal/git-workflow.ts +5 -6
  72. package/lib/internal/platform-pr.ts +14 -7
  73. package/lib/internal/release-workflow.ts +2 -1
  74. package/lib/internal/sandbox-control.ts +92 -2
  75. package/lib/internal/task-finalization.ts +68 -0
  76. package/lib/internal/task-orchestration.ts +20 -103
  77. package/lib/{task/commands/validate.ts → internal/task-validate.ts} +75 -40
  78. package/lib/platform/pr-summary.ts +55 -11
  79. package/lib/platform/pull-requests.ts +211 -41
  80. package/lib/run/tui.ts +5 -3
  81. package/lib/sandbox/commands/rm.ts +413 -61
  82. package/lib/sandbox/control/client.ts +255 -6
  83. package/lib/sandbox/control/controller-registration.ts +352 -0
  84. package/lib/sandbox/control/executor.ts +169 -5
  85. package/lib/sandbox/control/lifecycle.ts +2 -1
  86. package/lib/sandbox/control/protocol.ts +80 -7
  87. package/lib/sandbox/index.ts +5 -3
  88. package/lib/sandbox/managed-fs.ts +5 -0
  89. package/lib/sandbox/task-resolver.ts +58 -7
  90. package/lib/sandbox/workspace-identity.ts +50 -2
  91. package/lib/sandbox/workspace-view.ts +3 -3
  92. package/lib/sandbox/worktree-safety.ts +236 -2
  93. package/lib/task/codex-orchestration.ts +168 -40
  94. package/lib/task/commands/status.ts +78 -1
  95. package/lib/task/commit-operation.ts +430 -0
  96. package/lib/task/commit-policy.ts +30 -0
  97. package/lib/task/delegation-receipts.ts +263 -4
  98. package/lib/task/finalization.ts +761 -0
  99. package/lib/task/index.ts +0 -8
  100. package/lib/task/operation-outcome.ts +48 -0
  101. package/lib/task/orchestration.ts +218 -982
  102. package/lib/task/short-id.ts +65 -1
  103. package/lib/task/task-execution-lock.ts +10 -1
  104. package/lib/task/verification-engine.ts +115 -63
  105. package/lib/task/verification.ts +3 -2
  106. package/lib/task/workflow-warning-intents.ts +57 -2
  107. package/package.json +2 -2
  108. package/templates/.agents/QUICKSTART.en.md +3 -4
  109. package/templates/.agents/QUICKSTART.zh-CN.md +3 -4
  110. package/templates/.agents/hooks/lifecycle-delegation.js +1 -21
  111. package/templates/.agents/rules/README.en.md +1 -0
  112. package/templates/.agents/rules/README.zh-CN.md +1 -0
  113. package/templates/.agents/rules/compatibility-policy.en.md +39 -0
  114. package/templates/.agents/rules/compatibility-policy.zh-CN.md +39 -0
  115. package/templates/.agents/rules/lifecycle-orchestration.en.md +4 -4
  116. package/templates/.agents/rules/lifecycle-orchestration.zh-CN.md +4 -4
  117. package/templates/.agents/rules/pr-sync.en.md +1 -1
  118. package/templates/.agents/rules/pr-sync.github.en.md +2 -1
  119. package/templates/.agents/rules/pr-sync.github.zh-CN.md +2 -0
  120. package/templates/.agents/rules/pr-sync.zh-CN.md +1 -0
  121. package/templates/.agents/rules/review-method.en.md +1 -0
  122. package/templates/.agents/rules/review-method.zh-CN.md +1 -0
  123. package/templates/.agents/rules/task-management.en.md +2 -2
  124. package/templates/.agents/rules/task-management.zh-CN.md +2 -2
  125. package/templates/.agents/skills/analyze-task/SKILL.en.md +1 -0
  126. package/templates/.agents/skills/analyze-task/SKILL.zh-CN.md +1 -0
  127. package/templates/.agents/skills/block-task/SKILL.en.md +2 -4
  128. package/templates/.agents/skills/block-task/SKILL.zh-CN.md +2 -4
  129. package/templates/.agents/skills/cancel-task/SKILL.en.md +3 -3
  130. package/templates/.agents/skills/cancel-task/SKILL.zh-CN.md +3 -3
  131. package/templates/.agents/skills/close-codescan/SKILL.en.md +6 -2
  132. package/templates/.agents/skills/close-codescan/SKILL.zh-CN.md +6 -2
  133. package/templates/.agents/skills/close-dependabot/SKILL.en.md +6 -2
  134. package/templates/.agents/skills/close-dependabot/SKILL.zh-CN.md +6 -2
  135. package/templates/.agents/skills/code-task/SKILL.en.md +1 -0
  136. package/templates/.agents/skills/code-task/SKILL.zh-CN.md +2 -0
  137. package/templates/.agents/skills/commit/SKILL.en.md +41 -125
  138. package/templates/.agents/skills/commit/SKILL.zh-CN.md +52 -123
  139. package/templates/.agents/skills/commit/reference/commit-message.en.md +1 -1
  140. package/templates/.agents/skills/commit/reference/commit-message.zh-CN.md +1 -1
  141. package/templates/.agents/skills/commit/reference/commit-orchestration.en.md +14 -43
  142. package/templates/.agents/skills/commit/reference/commit-orchestration.zh-CN.md +14 -49
  143. package/templates/.agents/skills/commit/reference/pr-summary-sync.en.md +1 -1
  144. package/templates/.agents/skills/commit/reference/pr-summary-sync.zh-CN.md +1 -1
  145. package/templates/.agents/skills/commit/reference/task-status-update.en.md +2 -35
  146. package/templates/.agents/skills/commit/reference/task-status-update.zh-CN.md +2 -35
  147. package/templates/.agents/skills/complete-task/SKILL.en.md +31 -38
  148. package/templates/.agents/skills/complete-task/SKILL.zh-CN.md +31 -38
  149. package/templates/.agents/skills/complete-task/config/verify.en.json +1 -0
  150. package/templates/.agents/skills/complete-task/config/verify.zh-CN.json +1 -0
  151. package/templates/.agents/skills/complete-task/reference/external-delivery.en.md +1 -1
  152. package/templates/.agents/skills/complete-task/reference/external-delivery.zh-CN.md +1 -1
  153. package/templates/.agents/skills/create-pr/SKILL.en.md +17 -9
  154. package/templates/.agents/skills/create-pr/SKILL.zh-CN.md +17 -9
  155. package/templates/.agents/skills/create-pr/reference/change-report.en.md +41 -0
  156. package/templates/.agents/skills/create-pr/reference/change-report.zh-CN.md +41 -0
  157. package/templates/.agents/skills/create-pr/reference/comment-publish.en.md +2 -2
  158. package/templates/.agents/skills/create-pr/reference/comment-publish.zh-CN.md +2 -2
  159. package/templates/.agents/skills/plan-task/SKILL.en.md +2 -1
  160. package/templates/.agents/skills/plan-task/SKILL.zh-CN.md +2 -1
  161. package/templates/.agents/skills/review-code/reference/migration-risks.en.md +8 -6
  162. package/templates/.agents/skills/review-code/reference/migration-risks.zh-CN.md +8 -6
  163. package/templates/.agents/skills/run-manual-validation/SKILL.en.md +3 -3
  164. package/templates/.agents/skills/run-manual-validation/SKILL.zh-CN.md +3 -3
  165. package/templates/.agents/skills/run-manual-validation/reference/discovery-and-execution.en.md +1 -1
  166. package/templates/.agents/skills/run-manual-validation/reference/discovery-and-execution.zh-CN.md +1 -1
  167. package/templates/.agents/skills/run-manual-validation/reference/report-template.en.md +2 -2
  168. package/templates/.agents/skills/run-manual-validation/reference/report-template.zh-CN.md +2 -2
  169. package/templates/.agents/skills/run-task/SKILL.en.md +6 -6
  170. package/templates/.agents/skills/run-task/SKILL.zh-CN.md +6 -6
  171. package/templates/.agents/skills/run-task/reference/host-validation.en.md +2 -2
  172. package/templates/.agents/skills/run-task/reference/host-validation.zh-CN.md +2 -2
  173. package/templates/.agents/skills/update-agent-infra/scripts/sync-templates.js +24 -0
  174. package/templates/.agents/skills/watch-pr/reference/monitor-and-heal.en.md +1 -1
  175. package/templates/.agents/skills/watch-pr/reference/monitor-and-heal.zh-CN.md +1 -1
  176. package/templates/.codex/hooks.json +4 -4
  177. package/templates/.trae/skills/update-agent-infra.en.md +8 -0
  178. package/templates/.trae/skills/update-agent-infra.zh-CN.md +8 -0
  179. package/dist/lib/task/commit-finalization.js +0 -305
  180. package/dist/lib/task/commit-intent.js +0 -165
  181. package/lib/task/commit-finalization.ts +0 -419
  182. package/lib/task/commit-intent.ts +0 -230
@@ -0,0 +1,8 @@
1
+ ---
2
+ description: "更新项目 AI 协作配置"
3
+ disable-model-invocation: true
4
+ ---
5
+
6
+ 读取并执行 `.agents/skills/update-agent-infra/SKILL.md` 中的 update-agent-infra 技能。
7
+
8
+ 严格按照技能中定义的所有步骤执行,包括先从 `templates/` 渲染,再应用 managed/merged 更新策略。
@@ -1,305 +0,0 @@
1
- import { execFileSync, spawnSync } from 'node:child_process';
2
- import fs from 'node:fs';
3
- import path from 'node:path';
4
- import { locateActivityLog, appendActivityEntry, pairEntries, parseCommitAttemptStarted } from './activity-log.js';
5
- import { commitIntentPath, digest, readCommitIntent } from './commit-intent.js';
6
- import { parseTypedTaskFrontmatter } from './frontmatter.js';
7
- import { artifactName, maxRound, parseReviewSummary } from './review-artifacts.js';
8
- import { extractReviewBaseline, extractReviewedSnapshotTree, findAuthoritativeReviewCodeArtifact, parseReviewedGitTree } from './review-fingerprint.js';
9
- const SHA_RE = /^[a-f0-9]{40,64}$/;
10
- function git(repoRoot, args) {
11
- return execFileSync('git', args, {
12
- cwd: repoRoot,
13
- encoding: 'utf8',
14
- stdio: ['ignore', 'pipe', 'pipe']
15
- }).trim();
16
- }
17
- function commitExists(repoRoot, value) {
18
- return SHA_RE.test(value) && spawnSync('git', ['cat-file', '-e', `${value}^{commit}`], {
19
- cwd: repoRoot,
20
- stdio: 'ignore'
21
- }).status === 0;
22
- }
23
- function isAncestor(repoRoot, ancestor, descendant) {
24
- return spawnSync('git', ['merge-base', '--is-ancestor', ancestor, descendant], {
25
- cwd: repoRoot,
26
- stdio: 'ignore'
27
- }).status === 0;
28
- }
29
- function hasMatchingApprovedReview(taskDir, beforeRound, baseline, tree) {
30
- const entries = fs.readdirSync(taskDir);
31
- for (let round = Math.min(beforeRound - 1, maxRound(entries, 'review-code')); round >= 1; round -= 1) {
32
- const file = path.join(taskDir, artifactName('review-code', round));
33
- if (!fs.existsSync(file))
34
- continue;
35
- const content = fs.readFileSync(file, 'utf8');
36
- const summary = parseReviewSummary(content);
37
- if (summary.ok
38
- && summary.summary.verdict === 'Approved'
39
- && extractReviewBaseline(content) === baseline
40
- && extractReviewedSnapshotTree(content) === tree)
41
- return true;
42
- }
43
- return false;
44
- }
45
- function outcome(disposition, overrides = {}) {
46
- return {
47
- disposition,
48
- code: null,
49
- message: disposition,
50
- currentHead: '',
51
- committedHead: null,
52
- commitNote: null,
53
- activityNote: null,
54
- needsAnchor: false,
55
- needsLog: false,
56
- intent: null,
57
- intentDigest: null,
58
- attempt: null,
59
- ...overrides
60
- };
61
- }
62
- function invalid(message, currentHead = '', code = 'COMMIT_FINALIZATION_EVIDENCE_INVALID', overrides = {}) {
63
- return outcome('invalid', { code, message, currentHead, ...overrides });
64
- }
65
- function inspectCommitFinalization(taskDir, repoRoot, taskId) {
66
- let currentHead;
67
- let taskContent;
68
- let metadata;
69
- try {
70
- currentHead = git(repoRoot, ['rev-parse', 'HEAD']);
71
- taskContent = fs.readFileSync(path.join(taskDir, 'task.md'), 'utf8');
72
- metadata = parseTypedTaskFrontmatter(taskContent);
73
- }
74
- catch (error) {
75
- return invalid(error instanceof Error ? error.message : String(error));
76
- }
77
- const activity = locateActivityLog(taskContent);
78
- if (!activity)
79
- return invalid('task activity log is missing or ambiguous', currentHead);
80
- const commitRows = pairEntries(activity.entries).filter((row) => row.step === 'Commit');
81
- const openRows = commitRows.filter((row) => row.started !== '' && row.done === '');
82
- const file = commitIntentPath(taskDir);
83
- if (!fs.existsSync(file)) {
84
- if (openRows.length === 1) {
85
- const attempt = parseCommitAttemptStarted(openRows[0].note);
86
- if (attempt && attempt.baseline === currentHead) {
87
- return outcome('retryable-start', {
88
- message: 'a structured Commit attempt can be safely reused',
89
- currentHead,
90
- attempt
91
- });
92
- }
93
- }
94
- if (openRows.length > 0) {
95
- return outcome('orphaned-start', {
96
- code: 'COMMIT_FINALIZATION_EVIDENCE_MISSING',
97
- message: 'an open Commit activity entry exists without a commit intent',
98
- currentHead
99
- });
100
- }
101
- return outcome('idle', { message: 'no active commit finalization exists', currentHead });
102
- }
103
- let raw;
104
- let intent;
105
- try {
106
- raw = fs.readFileSync(file, 'utf8');
107
- intent = readCommitIntent(taskDir, taskId);
108
- }
109
- catch (error) {
110
- return invalid(error instanceof Error ? error.message : String(error), currentHead);
111
- }
112
- const intentDigest = digest(raw);
113
- const shared = { currentHead, intent, intentDigest, committedHead: intent.committedHead };
114
- if (!commitExists(repoRoot, intent.baselineHead)) {
115
- return invalid('commit intent baseline is not a valid Git commit', currentHead, 'COMMIT_FINALIZATION_EVIDENCE_INVALID', shared);
116
- }
117
- if (intent.phase === 'prepared') {
118
- if (intent.committedHead !== null || intent.pushEvidence !== null) {
119
- return invalid('prepared commit intent contains commit or push evidence', currentHead, 'COMMIT_FINALIZATION_EVIDENCE_INVALID', shared);
120
- }
121
- if (currentHead !== intent.baselineHead || openRows.length !== 1) {
122
- return outcome('conflict', {
123
- ...shared,
124
- code: 'COMMIT_FINALIZATION_CONFLICT',
125
- message: 'prepared commit intent does not match HEAD and exactly one open Commit entry'
126
- });
127
- }
128
- return outcome('prepared', { ...shared, message: 'prepared commit intent can be safely retried' });
129
- }
130
- if (!intent.committedHead || !commitExists(repoRoot, intent.committedHead)) {
131
- return invalid('committed intent head is missing or invalid', currentHead, 'COMMIT_FINALIZATION_EVIDENCE_INVALID', shared);
132
- }
133
- if (intent.phase === 'committed' && intent.pushEvidence !== null) {
134
- return invalid('committed intent contains premature push evidence', currentHead, 'COMMIT_FINALIZATION_EVIDENCE_INVALID', shared);
135
- }
136
- if (intent.phase === 'pushed'
137
- && (intent.pushEvidence === null
138
- || intent.pushEvidence.head !== intent.committedHead
139
- || !intent.pushEvidence.remote
140
- || !intent.pushEvidence.ref)) {
141
- return invalid('pushed intent evidence is missing or inconsistent', currentHead, 'COMMIT_FINALIZATION_EVIDENCE_INVALID', shared);
142
- }
143
- if (currentHead !== intent.committedHead || !isAncestor(repoRoot, intent.baselineHead, intent.committedHead)) {
144
- return outcome('conflict', {
145
- ...shared,
146
- code: 'COMMIT_FINALIZATION_CONFLICT',
147
- message: 'repository HEAD or ancestry conflicts with the commit intent'
148
- });
149
- }
150
- const commitNote = git(repoRoot, ['show', '-s', '--format=%h%x20%s', intent.committedHead]);
151
- const matchingDone = commitRows.filter((row) => row.done !== '' && row.note === commitNote);
152
- const pushOnlyRetry = intent.phase === 'pushed'
153
- && intent.baselineHead === intent.committedHead
154
- && matchingDone.length === 1
155
- && openRows.length === 1;
156
- const review = findAuthoritativeReviewCodeArtifact(taskDir);
157
- let needsAnchor = false;
158
- if (review.ok && review.path) {
159
- let reviewContent;
160
- try {
161
- reviewContent = fs.readFileSync(review.path, 'utf8');
162
- }
163
- catch (error) {
164
- return invalid(error instanceof Error ? error.message : String(error), currentHead, 'COMMIT_FINALIZATION_EVIDENCE_MISSING', shared);
165
- }
166
- const summary = parseReviewSummary(reviewContent);
167
- const reviewBaseline = extractReviewBaseline(reviewContent);
168
- const reviewTree = extractReviewedSnapshotTree(reviewContent);
169
- if (!summary.ok || summary.summary.verdict !== 'Approved' || !commitExists(repoRoot, reviewBaseline) || !parseReviewedGitTree(reviewTree)) {
170
- return invalid('authoritative review-code evidence is not a valid approved snapshot', currentHead, 'COMMIT_FINALIZATION_EVIDENCE_INVALID', shared);
171
- }
172
- const committedTree = git(repoRoot, ['rev-parse', `${intent.committedHead}^{tree}`]);
173
- const matchingCurrentReview = reviewBaseline === intent.baselineHead && reviewTree === committedTree;
174
- if (!matchingCurrentReview
175
- && !hasMatchingApprovedReview(taskDir, review.round, intent.baselineHead, committedTree)) {
176
- return outcome('conflict', {
177
- ...shared,
178
- code: 'COMMIT_FINALIZATION_CONFLICT',
179
- message: 'review baseline or snapshot tree conflicts with the committed intent'
180
- });
181
- }
182
- const anchor = String(metadata.last_reviewed_commit ?? '').trim();
183
- if (anchor && anchor !== intent.committedHead && anchor !== intent.baselineHead) {
184
- return outcome('conflict', {
185
- ...shared,
186
- code: 'COMMIT_FINALIZATION_CONFLICT',
187
- message: 'task review anchor conflicts with the committed intent'
188
- });
189
- }
190
- needsAnchor = anchor !== intent.committedHead;
191
- }
192
- const needsLog = (matchingDone.length === 0 && openRows.length === 1) || pushOnlyRetry;
193
- if (!(needsLog || (matchingDone.length === 1 && openRows.length === 0))) {
194
- return outcome('conflict', {
195
- ...shared,
196
- code: 'COMMIT_FINALIZATION_CONFLICT',
197
- message: 'Commit activity identity is missing, duplicated, or ambiguous',
198
- commitNote
199
- });
200
- }
201
- return outcome('recoverable', {
202
- ...shared,
203
- message: 'commit finalization can be recovered',
204
- commitNote,
205
- activityNote: pushOnlyRetry ? `Pushed ${commitNote}` : commitNote,
206
- needsAnchor,
207
- needsLog
208
- });
209
- }
210
- function planCommitTaskFinalization(taskDir, inspection, agent, timestamp) {
211
- if (inspection.disposition !== 'recoverable'
212
- || !inspection.committedHead
213
- || !inspection.commitNote
214
- || !inspection.activityNote) {
215
- throw new Error('commit finalization task mutations require a recoverable inspection');
216
- }
217
- const taskContent = fs.readFileSync(path.join(taskDir, 'task.md'), 'utf8');
218
- const frontmatter = { assigned_to: agent };
219
- if (inspection.needsAnchor)
220
- frontmatter.last_reviewed_commit = inspection.committedHead;
221
- const mutations = [{
222
- kind: 'frontmatter',
223
- set: frontmatter
224
- }];
225
- if (inspection.needsLog) {
226
- const activity = locateActivityLog(taskContent);
227
- if (!activity)
228
- throw new Error('task activity log is missing or ambiguous');
229
- mutations.push({
230
- kind: 'section',
231
- aliases: ['活动日志', 'Activity Log'],
232
- heading: activity.heading,
233
- body: appendActivityEntry(activity, {
234
- time: timestamp,
235
- step: 'Commit',
236
- agent,
237
- note: inspection.activityNote
238
- })
239
- });
240
- }
241
- return { mutations, commitNote: inspection.commitNote };
242
- }
243
- function inspectCreatePrCommitGate(taskDir, repoRoot, taskId) {
244
- const lifecycle = inspectCommitFinalization(taskDir, repoRoot, taskId);
245
- if (lifecycle.disposition !== 'idle') {
246
- const pending = lifecycle.disposition === 'prepared' || lifecycle.disposition === 'recoverable';
247
- return {
248
- allowed: false,
249
- code: pending ? 'COMMIT_FINALIZATION_PENDING' : lifecycle.code ?? 'COMMIT_FINALIZATION_CONFLICT',
250
- message: pending ? 'commit finalization must complete before creating a pull request' : lifecycle.message,
251
- disposition: lifecycle.disposition,
252
- action: 'rerun-commit'
253
- };
254
- }
255
- const review = findAuthoritativeReviewCodeArtifact(taskDir);
256
- if (!review.ok || !review.path) {
257
- return { allowed: true, code: null, message: 'no review-code history requires an anchor', disposition: 'idle', action: 'continue' };
258
- }
259
- try {
260
- const content = fs.readFileSync(review.path, 'utf8');
261
- const summary = parseReviewSummary(content);
262
- if (!summary.ok || summary.summary.verdict !== 'Approved') {
263
- return {
264
- allowed: false,
265
- code: 'COMMIT_FINALIZATION_EVIDENCE_INVALID',
266
- message: 'latest authoritative review-code is not approved',
267
- disposition: 'idle',
268
- action: 'rerun-review-code'
269
- };
270
- }
271
- const task = parseTypedTaskFrontmatter(fs.readFileSync(path.join(taskDir, 'task.md'), 'utf8'));
272
- const currentHead = git(repoRoot, ['rev-parse', 'HEAD']);
273
- const anchor = String(task.last_reviewed_commit ?? '').trim();
274
- if (!anchor || !commitExists(repoRoot, anchor)) {
275
- return {
276
- allowed: false,
277
- code: 'COMMIT_FINALIZATION_EVIDENCE_MISSING',
278
- message: 'approved review snapshot is not anchored to a Git commit',
279
- disposition: 'idle',
280
- action: 'rerun-commit'
281
- };
282
- }
283
- if (anchor !== currentHead || extractReviewedSnapshotTree(content) !== git(repoRoot, ['rev-parse', 'HEAD^{tree}'])) {
284
- return {
285
- allowed: false,
286
- code: 'COMMIT_FINALIZATION_CONFLICT',
287
- message: 'approved review anchor or tree does not match repository HEAD',
288
- disposition: 'idle',
289
- action: 'rerun-review-code'
290
- };
291
- }
292
- return { allowed: true, code: null, message: 'commit finalization evidence is complete', disposition: 'idle', action: 'continue' };
293
- }
294
- catch (error) {
295
- return {
296
- allowed: false,
297
- code: 'COMMIT_FINALIZATION_EVIDENCE_INVALID',
298
- message: error instanceof Error ? error.message : String(error),
299
- disposition: 'idle',
300
- action: 'rerun-review-code'
301
- };
302
- }
303
- }
304
- export { inspectCommitFinalization, inspectCreatePrCommitGate, planCommitTaskFinalization };
305
- //# sourceMappingURL=commit-finalization.js.map
@@ -1,165 +0,0 @@
1
- import { createHash, randomBytes, randomUUID, timingSafeEqual } from 'node:crypto';
2
- import fs from 'node:fs';
3
- import path from 'node:path';
4
- class CommitIntentError extends Error {
5
- code;
6
- constructor(code, message) {
7
- super(message);
8
- this.name = 'CommitIntentError';
9
- this.code = code;
10
- }
11
- }
12
- function commitIntentPath(taskDir) {
13
- return path.join(taskDir, 'commit-intent.json');
14
- }
15
- function digest(value) {
16
- return createHash('sha256').update(value).digest('hex');
17
- }
18
- function serialize(value) {
19
- return `${JSON.stringify(value, null, 2)}\n`;
20
- }
21
- function atomicWrite(file, value) {
22
- const temp = `${file}.${process.pid}.${randomUUID()}.tmp`;
23
- try {
24
- fs.writeFileSync(temp, serialize(value), { encoding: 'utf8', flag: 'wx', mode: 0o600 });
25
- const handle = fs.openSync(temp, 'r+');
26
- try {
27
- fs.fsyncSync(handle);
28
- }
29
- finally {
30
- fs.closeSync(handle);
31
- }
32
- fs.renameSync(temp, file);
33
- fs.chmodSync(file, 0o600);
34
- try {
35
- const directory = fs.openSync(path.dirname(file), 'r');
36
- try {
37
- fs.fsyncSync(directory);
38
- }
39
- finally {
40
- fs.closeSync(directory);
41
- }
42
- }
43
- catch (error) {
44
- const code = error.code;
45
- if (!['EACCES', 'EISDIR', 'EINVAL', 'EPERM'].includes(code ?? ''))
46
- throw error;
47
- }
48
- }
49
- finally {
50
- if (fs.existsSync(temp))
51
- fs.unlinkSync(temp);
52
- }
53
- }
54
- function isRecord(value) {
55
- return typeof value === 'object' && value !== null && !Array.isArray(value);
56
- }
57
- function keysEqual(value, expected) {
58
- return Object.keys(value).sort().join('\0') === [...expected].sort().join('\0');
59
- }
60
- function validPushEvidence(value) {
61
- return value === null || (isRecord(value)
62
- && keysEqual(value, ['remote', 'ref', 'head'])
63
- && ['remote', 'ref', 'head'].every((key) => typeof value[key] === 'string' && value[key] !== ''));
64
- }
65
- function validOrchestration(value) {
66
- if (value === null)
67
- return true;
68
- if (!isRecord(value) || !keysEqual(value, [
69
- 'runId', 'receiptId', 'authorizationIssuedAt', 'sourceRunDigest', 'plannedRunDigest',
70
- 'completionUpdatedAt', 'plannedReceipt'
71
- ]))
72
- return false;
73
- return [
74
- 'runId', 'receiptId', 'authorizationIssuedAt', 'sourceRunDigest', 'plannedRunDigest', 'completionUpdatedAt'
75
- ].every((key) => typeof value[key] === 'string' && value[key] !== '') && isRecord(value.plannedReceipt);
76
- }
77
- function parseIntent(raw, taskId) {
78
- let value;
79
- try {
80
- value = JSON.parse(raw);
81
- }
82
- catch {
83
- throw new CommitIntentError('ORCHESTRATION_COMMIT_INTENT_INVALID', 'commit intent is not valid JSON');
84
- }
85
- if (!isRecord(value) || !keysEqual(value, [
86
- 'schemaVersion', 'taskId', 'mode', 'phase', 'tokenDigest', 'baselineHead', 'committedHead',
87
- 'pushEvidence', 'orchestration', 'createdAt', 'updatedAt'
88
- ])) {
89
- throw new CommitIntentError('ORCHESTRATION_COMMIT_INTENT_INVALID', 'commit intent has an invalid schema');
90
- }
91
- if (value.schemaVersion !== 1
92
- || value.taskId !== taskId
93
- || !['standalone', 'orchestrated'].includes(String(value.mode))
94
- || !['prepared', 'committed', 'pushed'].includes(String(value.phase))
95
- || typeof value.tokenDigest !== 'string'
96
- || !/^[a-f0-9]{64}$/.test(value.tokenDigest)
97
- || typeof value.baselineHead !== 'string'
98
- || (value.committedHead !== null && typeof value.committedHead !== 'string')
99
- || !validPushEvidence(value.pushEvidence)
100
- || !validOrchestration(value.orchestration)
101
- || typeof value.createdAt !== 'string'
102
- || typeof value.updatedAt !== 'string'
103
- || (value.mode === 'standalone') !== (value.orchestration === null)) {
104
- throw new CommitIntentError('ORCHESTRATION_COMMIT_INTENT_INVALID', 'commit intent has invalid field values');
105
- }
106
- return value;
107
- }
108
- function tokenMatches(actualDigest, token) {
109
- const actual = Buffer.from(actualDigest, 'hex');
110
- const supplied = Buffer.from(digest(token), 'hex');
111
- return actual.length === supplied.length && timingSafeEqual(actual, supplied);
112
- }
113
- function readCommitIntent(taskDir, taskId, token) {
114
- const file = commitIntentPath(taskDir);
115
- const intent = parseIntent(fs.readFileSync(file, 'utf8'), taskId);
116
- if (token !== undefined && !tokenMatches(intent.tokenDigest, token)) {
117
- throw new CommitIntentError('ORCHESTRATION_COMMIT_INTENT_TOKEN_MISMATCH', 'commit intent token does not match');
118
- }
119
- return intent;
120
- }
121
- function createCommitIntent(taskDir, input, options = {}) {
122
- const file = commitIntentPath(taskDir);
123
- if (fs.existsSync(file)) {
124
- throw new CommitIntentError('ORCHESTRATION_COMMIT_INTENT_BUSY', 'an active commit intent already exists');
125
- }
126
- const token = (options.token ?? (() => randomBytes(32).toString('base64url')))();
127
- const intent = Object.freeze({ schemaVersion: 1, ...input, tokenDigest: digest(token) });
128
- atomicWrite(file, intent);
129
- return { intent, token };
130
- }
131
- function updateCommitIntent(taskDir, taskId, token, updates) {
132
- const current = readCommitIntent(taskDir, taskId, token);
133
- const nextPhase = updates.phase ?? current.phase;
134
- const transitions = {
135
- prepared: ['prepared', 'committed', 'pushed'],
136
- committed: ['committed', 'pushed'],
137
- pushed: ['pushed']
138
- };
139
- if (!transitions[current.phase].includes(nextPhase)) {
140
- throw new CommitIntentError('ORCHESTRATION_COMMIT_INTENT_STATE_INVALID', `cannot move commit intent from ${current.phase} to ${nextPhase}`);
141
- }
142
- const updated = Object.freeze({ ...current, ...updates, phase: nextPhase });
143
- atomicWrite(commitIntentPath(taskDir), updated);
144
- return updated;
145
- }
146
- function removeCommitIntent(taskDir, taskId, token) {
147
- readCommitIntent(taskDir, taskId, token);
148
- fs.unlinkSync(commitIntentPath(taskDir));
149
- }
150
- function removeCommitIntentByDigest(taskDir, taskId, expectedDigest) {
151
- const file = commitIntentPath(taskDir);
152
- const raw = fs.readFileSync(file);
153
- parseIntent(raw.toString('utf8'), taskId);
154
- const actual = digest(raw);
155
- const expected = Buffer.from(expectedDigest, 'hex');
156
- const observed = Buffer.from(actual, 'hex');
157
- if (!/^[a-f0-9]{64}$/.test(expectedDigest)
158
- || expected.length !== observed.length
159
- || !timingSafeEqual(expected, observed)) {
160
- throw new CommitIntentError('ORCHESTRATION_COMMIT_RECOVERY_REQUIRED', 'commit intent changed after recovery inspection');
161
- }
162
- fs.unlinkSync(file);
163
- }
164
- export { CommitIntentError, commitIntentPath, createCommitIntent, digest, readCommitIntent, removeCommitIntent, removeCommitIntentByDigest, serialize, updateCommitIntent };
165
- //# sourceMappingURL=commit-intent.js.map