@fitlab-ai/agent-infra 0.9.3 → 0.9.4
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.
- package/README.md +2 -0
- package/README.zh-CN.md +2 -0
- package/bin/cli.ts +12 -0
- package/dist/bin/cli.js +14 -0
- package/dist/bin/internal-cli.js +134 -123
- package/dist/lib/agent-clients/adapter.js +92 -2
- package/dist/lib/{sandbox/credentials.js → agent-clients/adapters/claude-code-credentials.js} +4 -14
- package/dist/lib/agent-clients/adapters/claude-code-refresh.js +85 -0
- package/dist/lib/agent-clients/adapters/claude-code-sandbox.js +247 -0
- package/dist/lib/agent-clients/adapters/claude-code.js +17 -3
- package/dist/lib/agent-clients/adapters/opencode-sandbox.js +108 -0
- package/dist/lib/agent-clients/adapters/opencode.js +15 -3
- package/dist/lib/agent-clients/adapters/runtimes/claude-code.dockerfile +65 -0
- package/dist/lib/agent-clients/registry.js +9 -1
- package/dist/lib/internal/sandbox-control.js +24 -0
- package/dist/lib/internal/task-activity.js +45 -13
- package/dist/lib/internal/task-ledger.js +12 -3
- package/dist/lib/internal/task-lifecycle.js +19 -1
- package/dist/lib/internal/task-orchestration.js +122 -18
- package/dist/lib/platform/github-client.js +19 -2
- package/dist/lib/platform/pr-checks.js +1 -1
- package/dist/lib/platform/pull-requests.js +47 -1
- package/dist/lib/platform/releases.js +13 -5
- package/dist/lib/process-data/index.js +239 -0
- package/dist/lib/process-data/normalize.js +198 -0
- package/dist/lib/process-data/privacy.js +33 -0
- package/dist/lib/process-data/reconcile.js +95 -0
- package/dist/lib/process-data/repair.js +58 -0
- package/dist/lib/process-data/sources.js +345 -0
- package/dist/lib/process-data/store.js +282 -0
- package/dist/lib/process-data/types.js +2 -0
- package/dist/lib/run/index.js +4 -3
- package/dist/lib/run/tui.js +7 -8
- package/dist/lib/sandbox/agent-client-reconciler.js +26 -2
- package/dist/lib/sandbox/capture.js +7 -2
- package/dist/lib/sandbox/commands/create.js +109 -228
- package/dist/lib/sandbox/commands/enter.js +15 -47
- package/dist/lib/sandbox/commands/list-running.js +16 -6
- package/dist/lib/sandbox/commands/ls.js +10 -6
- package/dist/lib/sandbox/commands/rebuild.js +4 -3
- package/dist/lib/sandbox/commands/refresh.js +4 -84
- package/dist/lib/sandbox/commands/rm.js +24 -3
- package/dist/lib/sandbox/commands/show.js +12 -3
- package/dist/lib/sandbox/commands/start.js +7 -4
- package/dist/lib/sandbox/config.js +2 -0
- package/dist/lib/sandbox/constants.js +6 -0
- package/dist/lib/sandbox/control/client.js +44 -0
- package/dist/lib/sandbox/control/protocol.js +35 -0
- package/dist/lib/sandbox/control/server.js +109 -0
- package/dist/lib/sandbox/dockerfile.js +36 -13
- package/dist/lib/sandbox/mounts.js +17 -4
- package/dist/lib/sandbox/recovery.js +128 -5
- package/dist/lib/sandbox/redaction.js +12 -0
- package/dist/lib/sandbox/runtimes/base.dockerfile +24 -74
- package/dist/lib/sandbox/workspace-identity.js +95 -0
- package/dist/lib/sandbox/workspace-view.js +103 -0
- package/dist/lib/task/activity-intent.js +257 -51
- package/dist/lib/task/commit-finalization.js +267 -0
- package/dist/lib/task/commit-intent.js +165 -0
- package/dist/lib/task/decision-intents.js +31 -17
- package/dist/lib/task/delegation-receipts.js +2 -2
- package/dist/lib/task/implementation-inputs.js +54 -7
- package/dist/lib/task/ledger-intents.js +35 -1
- package/dist/lib/task/orchestration.js +363 -28
- package/dist/lib/task/resolve-ref.js +41 -1
- package/lib/agent-clients/adapter.ts +126 -2
- package/lib/{sandbox/credentials.ts → agent-clients/adapters/claude-code-credentials.ts} +3 -18
- package/lib/agent-clients/adapters/claude-code-refresh.ts +128 -0
- package/lib/agent-clients/adapters/claude-code-sandbox.ts +295 -0
- package/lib/agent-clients/adapters/claude-code.ts +21 -3
- package/lib/agent-clients/adapters/opencode-sandbox.ts +136 -0
- package/lib/agent-clients/adapters/opencode.ts +18 -3
- package/lib/agent-clients/adapters/runtimes/claude-code.dockerfile +65 -0
- package/lib/agent-clients/registry.ts +9 -1
- package/lib/internal/sandbox-control.ts +24 -0
- package/lib/internal/task-activity.ts +50 -14
- package/lib/internal/task-ledger.ts +12 -3
- package/lib/internal/task-lifecycle.ts +22 -1
- package/lib/internal/task-orchestration.ts +81 -11
- package/lib/platform/github-client.ts +20 -2
- package/lib/platform/pr-checks.ts +1 -1
- package/lib/platform/pull-requests.ts +49 -1
- package/lib/platform/releases.ts +16 -4
- package/lib/process-data/index.ts +224 -0
- package/lib/process-data/normalize.ts +202 -0
- package/lib/process-data/privacy.ts +41 -0
- package/lib/process-data/reconcile.ts +110 -0
- package/lib/process-data/repair.ts +64 -0
- package/lib/process-data/sources.ts +354 -0
- package/lib/process-data/store.ts +315 -0
- package/lib/process-data/types.ts +135 -0
- package/lib/run/index.ts +5 -3
- package/lib/run/tui.ts +11 -8
- package/lib/sandbox/agent-client-reconciler.ts +44 -2
- package/lib/sandbox/capture.ts +9 -2
- package/lib/sandbox/commands/create.ts +126 -260
- package/lib/sandbox/commands/enter.ts +13 -59
- package/lib/sandbox/commands/list-running.ts +23 -6
- package/lib/sandbox/commands/ls.ts +22 -6
- package/lib/sandbox/commands/rebuild.ts +4 -3
- package/lib/sandbox/commands/refresh.ts +4 -126
- package/lib/sandbox/commands/rm.ts +24 -3
- package/lib/sandbox/commands/show.ts +29 -3
- package/lib/sandbox/commands/start.ts +6 -4
- package/lib/sandbox/config.ts +4 -0
- package/lib/sandbox/constants.ts +8 -0
- package/lib/sandbox/control/client.ts +55 -0
- package/lib/sandbox/control/protocol.ts +75 -0
- package/lib/sandbox/control/server.ts +117 -0
- package/lib/sandbox/dockerfile.ts +66 -13
- package/lib/sandbox/mounts.ts +24 -4
- package/lib/sandbox/recovery.ts +163 -6
- package/lib/sandbox/redaction.ts +14 -0
- package/lib/sandbox/runtimes/base.dockerfile +24 -74
- package/lib/sandbox/tool-types.ts +16 -0
- package/lib/sandbox/workspace-identity.ts +123 -0
- package/lib/sandbox/workspace-view.ts +143 -0
- package/lib/task/activity-intent.ts +350 -61
- package/lib/task/commit-finalization.ts +356 -0
- package/lib/task/commit-intent.ts +230 -0
- package/lib/task/decision-intents.ts +32 -12
- package/lib/task/delegation-receipts.ts +2 -2
- package/lib/task/implementation-inputs.ts +62 -8
- package/lib/task/ledger-intents.ts +43 -3
- package/lib/task/orchestration.ts +501 -33
- package/lib/task/resolve-ref.ts +46 -1
- package/package.json +1 -1
- package/templates/.agents/rules/README.en.md +1 -1
- package/templates/.agents/rules/README.zh-CN.md +1 -1
- package/templates/.agents/rules/human-decision-context.en.md +11 -2
- package/templates/.agents/rules/human-decision-context.zh-CN.md +11 -2
- package/templates/.agents/rules/pr-checks-commands.en.md +1 -1
- package/templates/.agents/rules/pr-checks-commands.github.en.md +1 -1
- package/templates/.agents/rules/pr-checks-commands.github.zh-CN.md +2 -2
- package/templates/.agents/rules/pr-checks-commands.zh-CN.md +1 -1
- package/templates/.agents/rules/review-handshake.zh-CN.md +1 -1
- package/templates/.agents/rules/task-management.en.md +2 -0
- package/templates/.agents/rules/task-management.zh-CN.md +2 -0
- package/templates/.agents/skills/code-task/SKILL.en.md +1 -1
- package/templates/.agents/skills/code-task/SKILL.zh-CN.md +1 -1
- package/templates/.agents/skills/commit/SKILL.en.md +21 -11
- package/templates/.agents/skills/commit/SKILL.zh-CN.md +27 -11
- package/templates/.agents/skills/commit/reference/commit-orchestration.en.md +49 -0
- package/templates/.agents/skills/commit/reference/commit-orchestration.zh-CN.md +55 -0
- package/templates/.agents/skills/commit/reference/task-status-update.en.md +4 -4
- package/templates/.agents/skills/commit/reference/task-status-update.zh-CN.md +4 -4
- package/templates/.agents/skills/complete-task/SKILL.en.md +1 -1
- package/templates/.agents/skills/complete-task/SKILL.zh-CN.md +1 -1
- package/templates/.agents/skills/create-pr/SKILL.en.md +4 -4
- package/templates/.agents/skills/create-pr/SKILL.zh-CN.md +4 -4
- package/templates/.agents/skills/create-task/reference/context-capture.en.md +34 -0
- package/templates/.agents/skills/create-task/reference/context-capture.zh-CN.md +34 -0
- package/templates/.agents/skills/review-code/SKILL.en.md +2 -2
- package/templates/.agents/skills/review-code/SKILL.zh-CN.md +2 -2
- package/templates/.agents/skills/review-code/reference/output-templates.en.md +1 -1
- package/templates/.agents/skills/review-code/reference/output-templates.zh-CN.md +1 -1
- package/templates/.agents/skills/review-pr/SKILL.en.md +36 -8
- package/templates/.agents/skills/review-pr/SKILL.zh-CN.md +36 -8
- package/templates/.agents/skills/review-pr/reference/report-template.en.md +1 -1
- package/templates/.agents/skills/review-pr/reference/report-template.zh-CN.md +1 -1
- package/templates/.agents/skills/update-agent-infra/scripts/sync-templates.js +67 -50
- package/templates/.agents/skills/watch-pr/SKILL.en.md +4 -4
- package/templates/.agents/skills/watch-pr/SKILL.zh-CN.md +4 -4
- package/templates/.agents/skills/watch-pr/reference/monitor-and-heal.en.md +2 -2
- package/templates/.agents/skills/watch-pr/reference/monitor-and-heal.zh-CN.md +2 -2
- package/templates/.agents/templates/task.en.md +1 -1
- package/templates/.agents/templates/task.zh-CN.md +1 -1
- package/templates/.claude/commands/watch-pr.en.md +1 -1
- package/templates/.claude/commands/watch-pr.zh-CN.md +1 -1
- package/templates/.claude/rules/tool-preferences.en.md +20 -0
- package/templates/.claude/rules/tool-preferences.zh-CN.md +20 -0
- package/templates/.opencode/commands/watch-pr.en.md +1 -1
- package/templates/.opencode/commands/watch-pr.zh-CN.md +1 -1
- package/templates/.gemini/commands/_project_/review-pr.en.toml +0 -10
- package/templates/.gemini/commands/_project_/review-pr.zh-CN.toml +0 -10
|
@@ -1,101 +1,368 @@
|
|
|
1
1
|
import fs from 'node:fs';
|
|
2
|
-
import path from 'node:path';
|
|
3
2
|
|
|
3
|
+
import { normalizeAgentToken } from '../agent-clients/tokens.ts';
|
|
4
4
|
import { resolveTaskRef } from './resolve-ref.ts';
|
|
5
5
|
import { locateActivityLog, appendActivityEntry } from './activity-log.ts';
|
|
6
|
-
import {
|
|
6
|
+
import {
|
|
7
|
+
assertWritableInventory,
|
|
8
|
+
buildArtifactLinkSection,
|
|
9
|
+
inspectTaskArtifacts,
|
|
10
|
+
parseArtifactName
|
|
11
|
+
} from './artifact-lifecycle.ts';
|
|
12
|
+
import type { ArtifactIdentity } from './artifact-lifecycle.ts';
|
|
13
|
+
import { TaskExecutionLockError, withTaskExecutionLock } from './task-execution-lock.ts';
|
|
14
|
+
import type { TaskExecutionLockOptions } from './task-execution-lock.ts';
|
|
7
15
|
import { captureTaskWriteMetadata, writeTask } from './write.ts';
|
|
8
16
|
import type { TaskMutation, TaskOperationSummary, TaskWriteOptions } from './write.ts';
|
|
9
17
|
|
|
10
|
-
|
|
11
|
-
|
|
18
|
+
type PrReviewVerdict = 'approved' | 'changes-requested' | 'commented';
|
|
19
|
+
type PrReviewOutcome = 'aborted' | 'superseded';
|
|
20
|
+
type PrReviewArtifactStatus = 'pending' | 'applied' | 'no-op' | 'blocked' | 'failed' | 'aborted' | 'superseded';
|
|
21
|
+
|
|
22
|
+
export type PrReviewInspectIntent = {
|
|
23
|
+
kind: 'pr-review-inspect';
|
|
24
|
+
taskRef: string;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export type PrReviewStartIntent = {
|
|
28
|
+
kind: 'pr-review-start';
|
|
12
29
|
taskRef: string;
|
|
13
|
-
step: string;
|
|
14
30
|
agent: string;
|
|
15
|
-
|
|
16
|
-
|
|
31
|
+
artifact: string;
|
|
32
|
+
head: string;
|
|
17
33
|
dryRun?: boolean;
|
|
18
34
|
};
|
|
19
35
|
|
|
20
|
-
export type
|
|
36
|
+
export type PrReviewCompleteIntent = {
|
|
37
|
+
kind: 'pr-review-complete';
|
|
38
|
+
taskRef: string;
|
|
39
|
+
agent: string;
|
|
40
|
+
artifact: string;
|
|
41
|
+
head: string;
|
|
42
|
+
verdict: PrReviewVerdict;
|
|
43
|
+
blockers: number;
|
|
44
|
+
major: number;
|
|
45
|
+
minor: number;
|
|
46
|
+
dryRun?: boolean;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export type PrReviewTerminateIntent = {
|
|
50
|
+
kind: 'pr-review-terminate';
|
|
51
|
+
taskRef: string;
|
|
52
|
+
agent: string;
|
|
53
|
+
artifact: string;
|
|
54
|
+
head: string;
|
|
55
|
+
outcome: PrReviewOutcome;
|
|
56
|
+
reason: string;
|
|
57
|
+
dryRun?: boolean;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export type PrReviewActivityIntent = PrReviewStartIntent | PrReviewCompleteIntent | PrReviewTerminateIntent;
|
|
61
|
+
|
|
62
|
+
type PrReviewIdentity = {
|
|
63
|
+
round: number;
|
|
64
|
+
artifact: string;
|
|
65
|
+
head: string;
|
|
66
|
+
agent: string | null;
|
|
67
|
+
artifactStatus: PrReviewArtifactStatus;
|
|
68
|
+
note: string | null;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
export type PrReviewActivitySnapshot = {
|
|
72
|
+
nextRound: number;
|
|
73
|
+
prepared: PrReviewIdentity | null;
|
|
74
|
+
open: PrReviewIdentity | null;
|
|
75
|
+
latestTerminal: PrReviewIdentity | null;
|
|
76
|
+
latestSuccessful: PrReviewIdentity | null;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
export type PrReviewActivityResult = {
|
|
21
80
|
status: 'planned' | 'applied' | 'no-op' | 'failed';
|
|
22
81
|
changed: boolean;
|
|
23
|
-
intent: '
|
|
82
|
+
intent: PrReviewActivityIntent['kind'];
|
|
24
83
|
taskId: string | null;
|
|
25
84
|
artifact: string | null;
|
|
26
85
|
operations: readonly TaskOperationSummary[];
|
|
27
86
|
error: { code: string; message: string } | null;
|
|
28
87
|
};
|
|
29
88
|
|
|
30
|
-
|
|
89
|
+
export type PrReviewInspectionResult = {
|
|
90
|
+
status: 'ready' | 'failed';
|
|
91
|
+
changed: false;
|
|
92
|
+
intent: 'pr-review-inspect';
|
|
93
|
+
taskId: string | null;
|
|
94
|
+
snapshot: PrReviewActivitySnapshot | null;
|
|
95
|
+
error: { code: string; message: string } | null;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
type ActivityIntentOptions = TaskWriteOptions & { lockOptions?: TaskExecutionLockOptions };
|
|
99
|
+
type ArtifactRecord = { identity: ArtifactIdentity; head: string; status: PrReviewArtifactStatus };
|
|
100
|
+
type RoundState = ArtifactRecord & {
|
|
101
|
+
startedAgent: string | null;
|
|
102
|
+
terminalAgent: string | null;
|
|
103
|
+
terminalNote: string | null;
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
const HEAD_RE = /^[0-9a-f]{40}$/;
|
|
107
|
+
const REVIEW_ACTION_RE = /^Review PR \(Round ([1-9]\d*)\)( \[started\])?$/;
|
|
108
|
+
const VERDICTS: ReadonlySet<string> = new Set(['approved', 'changes-requested', 'commented']);
|
|
109
|
+
const OUTCOMES: ReadonlySet<string> = new Set(['aborted', 'superseded']);
|
|
110
|
+
const SUCCESS_STATUSES: ReadonlySet<PrReviewArtifactStatus> = new Set(['applied', 'no-op']);
|
|
111
|
+
const ARTIFACT_STATUSES: ReadonlySet<string> = new Set([
|
|
112
|
+
'pending', 'applied', 'no-op', 'blocked', 'failed', 'aborted', 'superseded'
|
|
113
|
+
]);
|
|
114
|
+
|
|
115
|
+
function failed(
|
|
116
|
+
intent: PrReviewActivityIntent,
|
|
117
|
+
code: string,
|
|
118
|
+
message: string,
|
|
119
|
+
taskId: string | null = null
|
|
120
|
+
): PrReviewActivityResult {
|
|
31
121
|
return {
|
|
32
|
-
status: 'failed', changed: false, intent:
|
|
33
|
-
artifact: intent.artifact
|
|
122
|
+
status: 'failed', changed: false, intent: intent.kind, taskId,
|
|
123
|
+
artifact: intent.artifact, operations: [], error: { code, message }
|
|
34
124
|
};
|
|
35
125
|
}
|
|
36
126
|
|
|
37
|
-
function
|
|
38
|
-
return
|
|
127
|
+
function inspectionFailed(code: string, message: string, taskId: string | null = null): PrReviewInspectionResult {
|
|
128
|
+
return {
|
|
129
|
+
status: 'failed', changed: false, intent: 'pr-review-inspect', taskId,
|
|
130
|
+
snapshot: null, error: { code, message }
|
|
131
|
+
};
|
|
39
132
|
}
|
|
40
133
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
if (intent.kind !== 'append') return failed(intent, 'ACTIVITY_INTENT_INVALID', 'intent kind must be append');
|
|
48
|
-
const resolved = resolveTaskRef(intent.taskRef, { repoRoot: options.repoRoot });
|
|
49
|
-
if (!resolved.ok) return failed(intent, resolved.code, resolved.message, resolved.taskId);
|
|
50
|
-
if (resolved.state !== 'active') {
|
|
51
|
-
return failed(intent, 'TASK_STATE_MISMATCH', `task ${resolved.taskId} is ${resolved.state}, expected active`, resolved.taskId);
|
|
134
|
+
function parseArtifactRecord(identity: ArtifactIdentity): ArtifactRecord | { code: string; message: string } {
|
|
135
|
+
let content: string;
|
|
136
|
+
try {
|
|
137
|
+
content = fs.readFileSync(identity.path, 'utf8');
|
|
138
|
+
} catch (error) {
|
|
139
|
+
return { code: 'ACTIVITY_ARTIFACT_READ_FAILED', message: error instanceof Error ? error.message : String(error) };
|
|
52
140
|
}
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
if (!step || !agent || !note || /[\r\n]/.test(intent.step) || /[\r\n]/.test(intent.agent) || /[\r\n]/.test(intent.note)) {
|
|
57
|
-
return failed(intent, 'ACTIVITY_INTENT_INVALID', 'step, agent and note must be non-empty single-line values', resolved.taskId);
|
|
141
|
+
const heads = [...content.matchAll(/^- \*\*(?:被审 head SHA|Reviewed Head SHA)\*\*[::]\s*([0-9a-f]{40})\s*$/gm)];
|
|
142
|
+
if (heads.length !== 1) {
|
|
143
|
+
return { code: 'ACTIVITY_ARTIFACT_INVALID', message: `${identity.name} must contain exactly one reviewed head SHA` };
|
|
58
144
|
}
|
|
145
|
+
const statuses = [...content.matchAll(/^- \*\*(?:正式 Review 状态|Formal Review Status)\*\*[::][ \t]*([a-z-]+)[^\r\n]*$/gm)];
|
|
146
|
+
const status = statuses[0]?.[1];
|
|
147
|
+
if (statuses.length !== 1 || !status || !ARTIFACT_STATUSES.has(status)) {
|
|
148
|
+
return { code: 'ACTIVITY_ARTIFACT_INVALID', message: `${identity.name} must contain exactly one supported formal review status` };
|
|
149
|
+
}
|
|
150
|
+
return { identity, head: heads[0]![1]!, status: status as PrReviewArtifactStatus };
|
|
151
|
+
}
|
|
59
152
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
153
|
+
function toIdentity(state: RoundState, terminal = false): PrReviewIdentity {
|
|
154
|
+
return {
|
|
155
|
+
round: state.identity.round,
|
|
156
|
+
artifact: state.identity.name,
|
|
157
|
+
head: state.head,
|
|
158
|
+
agent: terminal ? state.terminalAgent : state.startedAgent,
|
|
159
|
+
artifactStatus: state.status,
|
|
160
|
+
note: terminal ? state.terminalNote : null
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function readSnapshot(taskRef: string, repoRoot?: string):
|
|
165
|
+
| { ok: true; taskId: string; taskDir: string; taskMdPath: string; repoRoot: string; content: string; states: RoundState[]; snapshot: PrReviewActivitySnapshot }
|
|
166
|
+
| { ok: false; code: string; message: string; taskId: string | null } {
|
|
167
|
+
const resolved = resolveTaskRef(taskRef, { repoRoot });
|
|
168
|
+
if (!resolved.ok) return { ok: false, code: resolved.code, message: resolved.message, taskId: resolved.taskId };
|
|
169
|
+
if (resolved.state !== 'active') {
|
|
170
|
+
return { ok: false, code: 'TASK_STATE_MISMATCH', message: `task ${resolved.taskId} is ${resolved.state}, expected active`, taskId: resolved.taskId };
|
|
171
|
+
}
|
|
172
|
+
const inventory = inspectTaskArtifacts(taskRef, 'pr-review', { repoRoot: resolved.repoRoot });
|
|
173
|
+
if (inventory.status === 'failed') {
|
|
174
|
+
return { ok: false, code: inventory.error?.code ?? 'ACTIVITY_ARTIFACT_INVALID', message: inventory.error?.message ?? 'artifact inspection failed', taskId: resolved.taskId };
|
|
66
175
|
}
|
|
176
|
+
const topology = assertWritableInventory(inventory);
|
|
177
|
+
if (topology) return { ok: false, code: topology.code, message: topology.message, taskId: resolved.taskId };
|
|
67
178
|
|
|
68
179
|
let content: string;
|
|
69
180
|
try {
|
|
70
181
|
content = fs.readFileSync(resolved.taskMdPath, 'utf8');
|
|
71
182
|
} catch (error) {
|
|
72
|
-
return
|
|
183
|
+
return { ok: false, code: 'TASK_READ_FAILED', message: error instanceof Error ? error.message : String(error), taskId: resolved.taskId };
|
|
73
184
|
}
|
|
74
|
-
const
|
|
75
|
-
if (!
|
|
76
|
-
|
|
185
|
+
const activity = locateActivityLog(content);
|
|
186
|
+
if (!activity) return { ok: false, code: 'ACTIVITY_SECTION_MISSING', message: 'task has no unique Activity Log section', taskId: resolved.taskId };
|
|
187
|
+
|
|
188
|
+
const knownRounds = new Set(inventory.artifacts.map((artifact) => artifact.round));
|
|
189
|
+
for (const entry of activity.entries) {
|
|
190
|
+
const match = REVIEW_ACTION_RE.exec(entry.step);
|
|
191
|
+
if (match && !knownRounds.has(Number(match[1]))) {
|
|
192
|
+
return { ok: false, code: 'ACTIVITY_STATE_CONFLICT', message: `Activity Log round ${match[1]} has no canonical pr-review artifact`, taskId: resolved.taskId };
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const states: RoundState[] = [];
|
|
197
|
+
for (const identity of inventory.artifacts) {
|
|
198
|
+
const parsed = parseArtifactRecord(identity);
|
|
199
|
+
if ('code' in parsed) return { ok: false, ...parsed, taskId: resolved.taskId };
|
|
200
|
+
const base = `Review PR (Round ${identity.round})`;
|
|
201
|
+
const started = activity.entries.filter((entry) => entry.step === `${base} [started]`);
|
|
202
|
+
const terminal = activity.entries.filter((entry) => entry.step === base);
|
|
203
|
+
if (started.length > 1 || terminal.length > 1) {
|
|
204
|
+
return { ok: false, code: 'ACTIVITY_STATE_CONFLICT', message: `${base} has duplicate started or terminal rows`, taskId: resolved.taskId };
|
|
205
|
+
}
|
|
206
|
+
if (terminal.length === 1 && started.length === 0 && !terminal[0]!.note.startsWith('receipt ')) {
|
|
207
|
+
return { ok: false, code: 'ACTIVITY_STATE_CONFLICT', message: `${base} has a terminal row without a matching started row`, taskId: resolved.taskId };
|
|
208
|
+
}
|
|
209
|
+
if (terminal.length === 1) {
|
|
210
|
+
const note = terminal[0]!.note;
|
|
211
|
+
const terminalMatchesStatus =
|
|
212
|
+
(note.startsWith('Verdict: ') && SUCCESS_STATUSES.has(parsed.status))
|
|
213
|
+
|| (note.startsWith('Outcome: Aborted, ') && parsed.status === 'aborted')
|
|
214
|
+
|| (note.startsWith('Outcome: Superseded, ') && parsed.status === 'superseded')
|
|
215
|
+
|| (note.startsWith('receipt ') && SUCCESS_STATUSES.has(parsed.status));
|
|
216
|
+
if (!terminalMatchesStatus) {
|
|
217
|
+
return { ok: false, code: 'ACTIVITY_STATE_CONFLICT', message: `${base} terminal note conflicts with artifact status ${parsed.status}`, taskId: resolved.taskId };
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
states.push({
|
|
221
|
+
...parsed,
|
|
222
|
+
startedAgent: started[0]?.agent ?? null,
|
|
223
|
+
terminalAgent: terminal[0]?.agent ?? null,
|
|
224
|
+
terminalNote: terminal[0]?.note ?? null
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
const openStates = states.filter((state) => state.startedAgent !== null && state.terminalNote === null);
|
|
229
|
+
const preparedStates = states.filter((state) => state.status === 'pending' && state.startedAgent === null && state.terminalNote === null);
|
|
230
|
+
if (openStates.length > 1 || preparedStates.length > 1) {
|
|
231
|
+
return { ok: false, code: 'ACTIVITY_STATE_CONFLICT', message: 'multiple prepared or open PR review rounds exist', taskId: resolved.taskId };
|
|
232
|
+
}
|
|
233
|
+
const terminalStates = states.filter((state) => state.terminalNote !== null);
|
|
234
|
+
const successfulStates = states.filter((state) => SUCCESS_STATUSES.has(state.status));
|
|
235
|
+
const latestTerminal = terminalStates.at(-1) ?? null;
|
|
236
|
+
const latestSuccessful = successfulStates.at(-1) ?? null;
|
|
237
|
+
return {
|
|
238
|
+
ok: true,
|
|
239
|
+
taskId: resolved.taskId,
|
|
240
|
+
taskDir: resolved.taskDir,
|
|
241
|
+
taskMdPath: resolved.taskMdPath,
|
|
242
|
+
repoRoot: resolved.repoRoot,
|
|
243
|
+
content,
|
|
244
|
+
states,
|
|
245
|
+
snapshot: {
|
|
246
|
+
nextRound: inventory.next?.round ?? 1,
|
|
247
|
+
prepared: preparedStates[0] ? toIdentity(preparedStates[0]) : null,
|
|
248
|
+
open: openStates[0] ? toIdentity(openStates[0]) : null,
|
|
249
|
+
latestTerminal: latestTerminal ? toIdentity(latestTerminal, true) : null,
|
|
250
|
+
latestSuccessful: latestSuccessful ? toIdentity(latestSuccessful, true) : null
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
export function inspectPrReviewActivity(intent: PrReviewInspectIntent, options: Pick<ActivityIntentOptions, 'repoRoot'> = {}): PrReviewInspectionResult {
|
|
256
|
+
const state = readSnapshot(intent.taskRef, options.repoRoot);
|
|
257
|
+
if (!state.ok) return inspectionFailed(state.code, state.message, state.taskId);
|
|
258
|
+
return { status: 'ready', changed: false, intent: intent.kind, taskId: state.taskId, snapshot: state.snapshot, error: null };
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function validateIntent(intent: PrReviewActivityIntent): { agent: string; artifact: { round: number; name: string } } | { code: string; message: string } {
|
|
262
|
+
const agent = normalizeAgentToken(intent.agent);
|
|
263
|
+
if (!agent) return { code: 'ACTIVITY_INTENT_INVALID', message: 'agent must be a supported collaborator token' };
|
|
264
|
+
const artifact = parseArtifactName(intent.artifact);
|
|
265
|
+
if (!artifact || artifact.family !== 'pr-review') {
|
|
266
|
+
return { code: 'ACTIVITY_ARTIFACT_INVALID', message: `artifact '${intent.artifact}' is not a canonical pr-review artifact` };
|
|
267
|
+
}
|
|
268
|
+
if (!HEAD_RE.test(intent.head)) return { code: 'ACTIVITY_INTENT_INVALID', message: 'head must be a lowercase 40-character SHA' };
|
|
269
|
+
if (intent.kind === 'pr-review-complete') {
|
|
270
|
+
if (!VERDICTS.has(intent.verdict)) return { code: 'ACTIVITY_INTENT_INVALID', message: 'verdict is invalid' };
|
|
271
|
+
if (![intent.blockers, intent.major, intent.minor].every((value) => Number.isSafeInteger(value) && value >= 0)) {
|
|
272
|
+
return { code: 'ACTIVITY_INTENT_INVALID', message: 'finding counts must be non-negative safe integers' };
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
if (intent.kind === 'pr-review-terminate') {
|
|
276
|
+
if (!OUTCOMES.has(intent.outcome)) return { code: 'ACTIVITY_INTENT_INVALID', message: 'outcome is invalid' };
|
|
277
|
+
if (!intent.reason.trim() || /[\r\n]/.test(intent.reason) || intent.reason !== intent.reason.trim()) {
|
|
278
|
+
return { code: 'ACTIVITY_INTENT_INVALID', message: 'reason must be a non-empty trimmed single-line value' };
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
return { agent, artifact };
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
function verdictLabel(verdict: PrReviewVerdict): string {
|
|
285
|
+
if (verdict === 'approved') return 'Approved';
|
|
286
|
+
if (verdict === 'changes-requested') return 'Changes Requested';
|
|
287
|
+
return 'Commented';
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
function outcomeLabel(outcome: PrReviewOutcome): string {
|
|
291
|
+
return outcome === 'aborted' ? 'Aborted' : 'Superseded';
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
function terminalNote(intent: PrReviewCompleteIntent | PrReviewTerminateIntent): string {
|
|
295
|
+
if (intent.kind === 'pr-review-complete') {
|
|
296
|
+
return `Verdict: ${verdictLabel(intent.verdict)}, blockers: ${intent.blockers}, major: ${intent.major}, minor: ${intent.minor} → ${intent.artifact}`;
|
|
297
|
+
}
|
|
298
|
+
return `Outcome: ${outcomeLabel(intent.outcome)}, reason: ${intent.reason} → ${intent.artifact}`;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
function applyLocked(intent: PrReviewActivityIntent, normalizedAgent: string, options: ActivityIntentOptions): PrReviewActivityResult {
|
|
302
|
+
const state = readSnapshot(intent.taskRef, options.repoRoot);
|
|
303
|
+
if (!state.ok) return failed(intent, state.code, state.message, state.taskId);
|
|
304
|
+
const parsed = parseArtifactName(intent.artifact)!;
|
|
305
|
+
const roundState = state.states.find((item) => item.identity.name === parsed.name);
|
|
306
|
+
if (!roundState) return failed(intent, 'ACTIVITY_ARTIFACT_INVALID', `artifact '${intent.artifact}' is not landed`, state.taskId);
|
|
307
|
+
if (roundState.head !== intent.head) {
|
|
308
|
+
return failed(intent, 'ACTIVITY_IDENTITY_MISMATCH', `head does not match ${intent.artifact}`, state.taskId);
|
|
309
|
+
}
|
|
310
|
+
const base = `Review PR (Round ${parsed.round})`;
|
|
311
|
+
let step: string;
|
|
312
|
+
let note: string;
|
|
313
|
+
let linkArtifact = false;
|
|
314
|
+
|
|
315
|
+
if (intent.kind === 'pr-review-start') {
|
|
316
|
+
if (roundState.terminalNote !== null) {
|
|
317
|
+
return failed(intent, 'ACTIVITY_STATE_CONFLICT', `${base} is already terminal`, state.taskId);
|
|
318
|
+
}
|
|
319
|
+
if (roundState.startedAgent !== null) {
|
|
320
|
+
return { status: 'no-op', changed: false, intent: intent.kind, taskId: state.taskId, artifact: intent.artifact, operations: [], error: null };
|
|
321
|
+
}
|
|
322
|
+
if (roundState.status !== 'pending') {
|
|
323
|
+
return failed(intent, 'ACTIVITY_STATE_CONFLICT', `${intent.artifact} must be pending before start`, state.taskId);
|
|
324
|
+
}
|
|
325
|
+
if (state.snapshot.open && state.snapshot.open.artifact !== intent.artifact) {
|
|
326
|
+
return failed(intent, 'ACTIVITY_STATE_CONFLICT', `another PR review round is open: ${state.snapshot.open.artifact}`, state.taskId);
|
|
327
|
+
}
|
|
328
|
+
step = `${base} [started]`;
|
|
329
|
+
note = 'started';
|
|
330
|
+
} else {
|
|
331
|
+
note = terminalNote(intent);
|
|
332
|
+
if (roundState.terminalNote !== null) {
|
|
333
|
+
if (roundState.terminalNote === note) {
|
|
334
|
+
return { status: 'no-op', changed: false, intent: intent.kind, taskId: state.taskId, artifact: intent.artifact, operations: [], error: null };
|
|
335
|
+
}
|
|
336
|
+
return failed(intent, 'ACTIVITY_STATE_CONFLICT', `${base} has a different terminal payload`, state.taskId);
|
|
337
|
+
}
|
|
338
|
+
if (roundState.startedAgent === null) {
|
|
339
|
+
return failed(intent, 'ACTIVITY_STATE_CONFLICT', `${base} has no matching started row`, state.taskId);
|
|
340
|
+
}
|
|
341
|
+
if (intent.kind === 'pr-review-complete' && !SUCCESS_STATUSES.has(roundState.status)) {
|
|
342
|
+
return failed(intent, 'ACTIVITY_STATE_CONFLICT', `${intent.artifact} must be applied or no-op before completion`, state.taskId);
|
|
343
|
+
}
|
|
344
|
+
if (intent.kind === 'pr-review-terminate' && roundState.status !== intent.outcome) {
|
|
345
|
+
return failed(intent, 'ACTIVITY_STATE_CONFLICT', `${intent.artifact} status must match outcome ${intent.outcome}`, state.taskId);
|
|
346
|
+
}
|
|
347
|
+
step = base;
|
|
348
|
+
linkArtifact = true;
|
|
77
349
|
}
|
|
78
350
|
|
|
79
351
|
let metadata;
|
|
80
352
|
try {
|
|
81
353
|
metadata = (options.metadataProvider ?? captureTaskWriteMetadata)();
|
|
82
354
|
} catch (error) {
|
|
83
|
-
return failed(intent, 'METADATA_CAPTURE_FAILED', error instanceof Error ? error.message : String(error),
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
const mutations: TaskMutation[] = [
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
path: path.join(resolved.taskDir, artifact.name),
|
|
93
|
-
size: 0,
|
|
94
|
-
mtimeMs: 0
|
|
95
|
-
});
|
|
355
|
+
return failed(intent, 'METADATA_CAPTURE_FAILED', error instanceof Error ? error.message : String(error), state.taskId);
|
|
356
|
+
}
|
|
357
|
+
const section = locateActivityLog(state.content)!;
|
|
358
|
+
const mutations: TaskMutation[] = [{
|
|
359
|
+
kind: 'section', aliases: ['活动日志', 'Activity Log'], heading: section.heading,
|
|
360
|
+
body: appendActivityEntry(section, { time: metadata.timestamp, step, agent: normalizedAgent, note })
|
|
361
|
+
}];
|
|
362
|
+
if (linkArtifact) {
|
|
363
|
+
const link = buildArtifactLinkSection(state.content, roundState.identity);
|
|
96
364
|
mutations.push({ kind: 'section', aliases: link.aliases, heading: link.heading, body: link.body });
|
|
97
365
|
}
|
|
98
|
-
|
|
99
366
|
const writeResult = writeTask({
|
|
100
367
|
taskRef: intent.taskRef,
|
|
101
368
|
expectedState: 'active',
|
|
@@ -104,10 +371,10 @@ export function applyActivityAppendIntent(intent: ActivityAppendIntent, options:
|
|
|
104
371
|
}, {
|
|
105
372
|
...options,
|
|
106
373
|
taskLocation: {
|
|
107
|
-
repoRoot:
|
|
108
|
-
taskId:
|
|
109
|
-
taskMdPath:
|
|
110
|
-
state:
|
|
374
|
+
repoRoot: state.repoRoot,
|
|
375
|
+
taskId: state.taskId,
|
|
376
|
+
taskMdPath: state.taskMdPath,
|
|
377
|
+
state: 'active'
|
|
111
378
|
},
|
|
112
379
|
metadataProvider: () => metadata
|
|
113
380
|
});
|
|
@@ -117,12 +384,34 @@ export function applyActivityAppendIntent(intent: ActivityAppendIntent, options:
|
|
|
117
384
|
return {
|
|
118
385
|
status: writeResult.status,
|
|
119
386
|
changed: writeResult.changed,
|
|
120
|
-
intent:
|
|
387
|
+
intent: intent.kind,
|
|
121
388
|
taskId: writeResult.taskId,
|
|
122
|
-
artifact: intent.artifact
|
|
389
|
+
artifact: intent.artifact,
|
|
123
390
|
operations: writeResult.operations,
|
|
124
391
|
error: null
|
|
125
392
|
};
|
|
126
393
|
}
|
|
127
394
|
|
|
128
|
-
export
|
|
395
|
+
export function applyPrReviewActivityIntent(intent: PrReviewActivityIntent, options: ActivityIntentOptions = {}): PrReviewActivityResult {
|
|
396
|
+
const validated = validateIntent(intent);
|
|
397
|
+
if ('code' in validated) return failed(intent, validated.code, validated.message);
|
|
398
|
+
const resolved = resolveTaskRef(intent.taskRef, { repoRoot: options.repoRoot });
|
|
399
|
+
if (!resolved.ok) return failed(intent, resolved.code, resolved.message, resolved.taskId);
|
|
400
|
+
if (resolved.state !== 'active') {
|
|
401
|
+
return failed(intent, 'TASK_STATE_MISMATCH', `task ${resolved.taskId} is ${resolved.state}, expected active`, resolved.taskId);
|
|
402
|
+
}
|
|
403
|
+
try {
|
|
404
|
+
return withTaskExecutionLock(
|
|
405
|
+
resolved.repoRoot,
|
|
406
|
+
resolved.taskId,
|
|
407
|
+
`task-activity:${intent.kind}`,
|
|
408
|
+
() => applyLocked(intent, validated.agent, { ...options, repoRoot: resolved.repoRoot }),
|
|
409
|
+
options.lockOptions
|
|
410
|
+
);
|
|
411
|
+
} catch (error) {
|
|
412
|
+
if (error instanceof TaskExecutionLockError) return failed(intent, error.code, error.message, resolved.taskId);
|
|
413
|
+
return failed(intent, 'ACTIVITY_LOCK_FAILED', error instanceof Error ? error.message : String(error), resolved.taskId);
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
export type { ActivityIntentOptions };
|