specdojo 0.3.0 → 0.3.1
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/CHANGELOG.md +18 -0
- package/dist/catalog-build-references.js +144 -0
- package/dist/catalog-build-render.js +144 -0
- package/dist/catalog-build.js +13 -290
- package/dist/catalog-generate.js +6 -9
- package/dist/catalog-plan-generate.js +2 -2
- package/dist/catalog-plan.js +6 -6
- package/dist/catalog-scaffold.js +4 -4
- package/dist/catalog.js +2 -2
- package/dist/dashboard-exec-records.js +105 -0
- package/dist/dashboard-routines.js +255 -0
- package/dist/dashboard-shared.js +14 -0
- package/dist/dashboard-timeline.js +175 -0
- package/dist/dashboard.js +10 -544
- package/dist/deliverable-trash.js +3 -3
- package/dist/doc-index.js +3 -3
- package/dist/exec-commands.js +557 -0
- package/dist/exec-core.js +457 -0
- package/dist/exec-plans-core.js +693 -0
- package/dist/exec-plans-generation.js +206 -0
- package/dist/exec-plans-storage.js +131 -0
- package/dist/exec-plans.js +3 -1018
- package/dist/exec-repo-integration-git.js +23 -0
- package/dist/exec-repo-integration-sync.js +264 -0
- package/dist/exec-repo-integration.js +4 -281
- package/dist/exec-reporter-output.js +206 -0
- package/dist/exec-reporter.js +2 -206
- package/dist/exec-results.js +22 -15
- package/dist/exec-run-agent-pipeline.js +256 -0
- package/dist/exec-run-agent.js +531 -0
- package/dist/exec-run-in-place.js +541 -0
- package/dist/exec-run-job.js +717 -0
- package/dist/exec-run-lifecycle-commands.js +522 -0
- package/dist/exec-run-mode-shared.js +140 -0
- package/dist/exec-run-register-command.js +244 -0
- package/dist/exec-run-register-core.js +386 -0
- package/dist/exec-run-register-mode.js +206 -0
- package/dist/exec-run-register-resume.js +393 -0
- package/dist/exec-run-register-state.js +322 -0
- package/dist/exec-run-register-worktree.js +509 -0
- package/dist/exec-run-schedule-mode.js +294 -0
- package/dist/exec-run-schedule-prepare.js +353 -0
- package/dist/exec-run-schedule-task.js +572 -0
- package/dist/exec-run-shared.js +399 -0
- package/dist/exec-run.js +11 -6182
- package/dist/exec-shared.js +2 -2
- package/dist/exec-trial-commands.js +558 -0
- package/dist/exec-trial-core.js +371 -0
- package/dist/exec-trial.js +2 -921
- package/dist/exec-worktree-ops-commit.js +707 -0
- package/dist/exec-worktree-ops-lifecycle.js +292 -0
- package/dist/exec-worktree-ops-merge.js +303 -0
- package/dist/exec-worktree-ops.js +3 -1292
- package/dist/exec-worktree.js +32 -4
- package/dist/exec.js +2 -1006
- package/dist/frontmatter-namespace.js +2 -2
- package/dist/grade-apply.js +734 -0
- package/dist/grade-command-apply.js +250 -0
- package/dist/grade-command-inspect.js +138 -0
- package/dist/grade-command-plan.js +101 -0
- package/dist/grade-command-shared.js +68 -0
- package/dist/grade-core.js +307 -0
- package/dist/grade-markdown.js +274 -0
- package/dist/grade-pipeline-state.js +184 -0
- package/dist/grade-plan.js +507 -0
- package/dist/grade-result.js +5 -5
- package/dist/grade-target-discovery.js +265 -0
- package/dist/grade-targets.js +432 -0
- package/dist/grade.js +14 -3188
- package/dist/init-command.js +7 -7
- package/dist/job-definition.js +670 -0
- package/dist/job.js +9 -673
- package/dist/kata-command.js +2 -2
- package/dist/register-command-types.js +1 -0
- package/dist/register-core.js +363 -0
- package/dist/register-create-commands.js +245 -0
- package/dist/register-events.js +4 -4
- package/dist/register-item.js +6 -6
- package/dist/register-maintenance-commands.js +187 -0
- package/dist/register-migration.js +268 -0
- package/dist/register-renumber.js +314 -0
- package/dist/register-transition-commands.js +248 -0
- package/dist/register-update.js +230 -0
- package/dist/register-views.js +256 -0
- package/dist/register.js +61 -2069
- package/dist/review-plan.js +3 -3
- package/dist/routine-definition.js +401 -0
- package/dist/routine.js +6 -402
- package/dist/schedule-approach.js +2 -2
- package/dist/schedule-strategy-generate-document.js +273 -0
- package/dist/schedule-strategy-generate-scope.js +199 -0
- package/dist/schedule-strategy-generate.js +9 -473
- package/dist/schedule.js +4 -4
- package/dist/specdojo-config.js +3 -4
- package/dist/viewpoint-document-kinds-check.js +3 -3
- package/dist/yaml-pages.js +4 -4
- package/dist/yaml.js +28 -0
- package/docs/ja/specdojo/guides/exec-config-guide.md +6 -6
- package/docs/ja/specdojo/guides/orchestrator-operation-guide.md +20 -18
- package/docs/ja/specdojo/guides/routine-operation-guide.md +1 -1
- package/package.json +4 -5
- package/templates/antigravity/README.md +3 -3
- package/templates/antigravity/exec-defaults-snippet.yaml +6 -4
- package/templates/init-exec-defaults.yaml +3 -1
|
@@ -0,0 +1,541 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
3
|
+
import { join, relative, resolve, sep } from "node:path";
|
|
4
|
+
import { loadExecDefaultsConfig, resolveMemberCommand, } from "./exec-agent-config.js";
|
|
5
|
+
import { activateResolvedProjectPaths, resolveProjectPaths } from "./exec-project.js";
|
|
6
|
+
import { randomHex } from "./exec-shared.js";
|
|
7
|
+
import { specdojoRootDir } from "./specdojo-config.js";
|
|
8
|
+
import { extractLocalId } from "./schedule-phase-sets.js";
|
|
9
|
+
import { archivePlan, buildInPlaceStem, generateDeliverablePlan, generateSinglePlan, parsePlanTaskIdentity, resolveDeliverableTarget, finalizeResultSectionsForDeliverable, targetDocIdsForScheduledTask, stemFromPlanPath, } from "./exec-plans.js";
|
|
10
|
+
import { buildTaskView } from "./exec-task-view.js";
|
|
11
|
+
import { isResultUnfilled, readResultFrontmatterSnapshot, renderReporterResult, scaffoldResult, updateResultStatus, } from "./exec-results.js";
|
|
12
|
+
import { gitEnvironment } from "./exec-worktree.js";
|
|
13
|
+
import { agentProtectedConfigViolation, captureAgentProtectedConfigSnapshot, changedAgentProtectedConfigPaths, } from "./exec-agent-protected-config.js";
|
|
14
|
+
import { agentGitStateViolation, captureAgentGitStateSnapshot, changedAgentGitStateFields, } from "./exec-agent-git-state.js";
|
|
15
|
+
import { recordGitStateBlock, recordProtectedConfigBlock } from "./exec-protection-handoff.js";
|
|
16
|
+
import { recordExecutorEvidence, recordReporterFailureOutput } from "./exec-evidence.js";
|
|
17
|
+
import { deriveReporterFacts, evidenceForReporter, runReporterWithFormatRetry, } from "./exec-reporter.js";
|
|
18
|
+
import { createPipelineState, pipelineStateLocation, updatePipelineStage, writePipelineState, } from "./exec-pipeline-state.js";
|
|
19
|
+
import { pinnedExecutor, pinnedReporter, resolveExecDefaultsPath, selectCandidates, } from "./exec-run-shared.js";
|
|
20
|
+
import { appendParentValidationsToExecutorEvidence, buildExecutorPrompt, executorRequirements, expandPromptRefs, extractBlockReason, loadRosterForExecutionPath, reportProtectionHandoffRecord, resolveReporterAgentCandidates, runWithRetry, } from "./exec-run-agent.js";
|
|
21
|
+
import { gradeBeforeReviewPlan } from "./exec-run-schedule-prepare.js";
|
|
22
|
+
import { pipelineRecoveryMeta, spawnBlock, spawnClaim, spawnComplete, } from "./exec-run-mode-shared.js";
|
|
23
|
+
// Resolve the agent command and the actor for an in-place run, ignoring task state. Unlike the
|
|
24
|
+
// worktree flow, this never requires the task to be claimable. The actor is derived the same way
|
|
25
|
+
// as the worktree path (`prepareTask`): explicit --by wins, otherwise the resolved member's
|
|
26
|
+
// nickname. This lets --track-state record events without forcing --by.
|
|
27
|
+
export function resolveInPlaceCommand(task, roster, opts, execDefaults = {}) {
|
|
28
|
+
if (!task?.agent_pipeline && (opts.executorBy || opts.reporterBy)) {
|
|
29
|
+
throw new Error("--executor-by / --reporter-by require an agent_pipeline task.");
|
|
30
|
+
}
|
|
31
|
+
// A task definition can pin the delegated agent by nickname (Job `task.agent`). An explicit
|
|
32
|
+
// --by still wins so an operator can redirect a single run without editing the definition.
|
|
33
|
+
const by = (task?.agent_pipeline
|
|
34
|
+
? (opts.executorBy ?? opts.by ?? pinnedExecutor(task))
|
|
35
|
+
: (opts.by ?? pinnedExecutor(task)))?.trim();
|
|
36
|
+
if (by) {
|
|
37
|
+
const member = roster?.members.find((m) => m.nickname === by && m.type === "agent");
|
|
38
|
+
if (task?.agent_pipeline && member?.stage_role !== "executor") {
|
|
39
|
+
throw new Error(`Agent must have stage_role: executor for this pipeline stage: ${by}`);
|
|
40
|
+
}
|
|
41
|
+
const command = member ? resolveMemberCommand(execDefaults, member) : undefined;
|
|
42
|
+
if (!command)
|
|
43
|
+
throw new Error(`Agent command not found for actor: ${by}`);
|
|
44
|
+
return {
|
|
45
|
+
command,
|
|
46
|
+
actor: by,
|
|
47
|
+
...(member?.provider ? { provider: member.provider } : {}),
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
if (task && (task.execution ?? "agent") === "human") {
|
|
51
|
+
throw new Error(`Task requires human execution. Use --by <nickname> to override: ${task.id}`);
|
|
52
|
+
}
|
|
53
|
+
const candidates = selectCandidates((task && executorRequirements(task)) ?? {
|
|
54
|
+
capabilities: task?.capabilities ?? [],
|
|
55
|
+
proficiency: task?.proficiency,
|
|
56
|
+
}, roster, task?.mode ?? "edit", undefined, execDefaults);
|
|
57
|
+
const candidate = candidates[0];
|
|
58
|
+
const command = candidate ? resolveMemberCommand(execDefaults, candidate) : undefined;
|
|
59
|
+
if (!candidate || !command)
|
|
60
|
+
throw new Error("No agent found. Specify --by <nickname>.");
|
|
61
|
+
return {
|
|
62
|
+
command,
|
|
63
|
+
actor: candidate.nickname,
|
|
64
|
+
...(candidate.provider ? { provider: candidate.provider } : {}),
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
// Default two-stage pipeline for in-place runs whose task has no schedule definition (a
|
|
68
|
+
// bring-your-own --plan or --deliverable). Stage requirements are left open so explicit
|
|
69
|
+
// --executor-by / --reporter-by nicknames (or stage_role-based auto selection) decide the agents.
|
|
70
|
+
const DEFAULT_IN_PLACE_AGENT_PIPELINE = {
|
|
71
|
+
stages: [{ stage_role: "executor" }, { stage_role: "reporter" }],
|
|
72
|
+
};
|
|
73
|
+
// Decide whether an in-place run without a schedule-defined task (--plan / --deliverable) uses
|
|
74
|
+
// the executor/reporter pipeline. --executor-by / --reporter-by opt in to the pipeline. A --by
|
|
75
|
+
// nickname that names a pipeline stage agent is rejected as a configuration error: an executor
|
|
76
|
+
// stage agent never writes the result, so a single-agent run would always end with an unfilled
|
|
77
|
+
// result instead of a meaningful outcome.
|
|
78
|
+
export function resolveInPlaceAgentPipeline(task, roster, opts, source) {
|
|
79
|
+
if (task?.agent_pipeline)
|
|
80
|
+
return task.agent_pipeline;
|
|
81
|
+
const flag = source === "plan" ? "--plan" : "--deliverable";
|
|
82
|
+
if (opts.executorBy || opts.reporterBy) {
|
|
83
|
+
if (opts.by) {
|
|
84
|
+
throw new Error("--by cannot be combined with --executor-by / --reporter-by.");
|
|
85
|
+
}
|
|
86
|
+
if (!task) {
|
|
87
|
+
throw new Error(`${flag} pipeline execution requires a plan with task_id in its frontmatter (the reporter fills the scaffolded result).`);
|
|
88
|
+
}
|
|
89
|
+
return DEFAULT_IN_PLACE_AGENT_PIPELINE;
|
|
90
|
+
}
|
|
91
|
+
const by = opts.by?.trim();
|
|
92
|
+
if (!by)
|
|
93
|
+
return undefined;
|
|
94
|
+
const member = roster?.members.find((m) => m.type === "agent" && m.nickname === by);
|
|
95
|
+
if (member?.stage_role === "executor") {
|
|
96
|
+
throw new Error(`--by ${by} is a pipeline executor agent (stage_role: executor) and does not write the result. ` +
|
|
97
|
+
`Run ${flag} with --executor-by ${by} --reporter-by <reporter> instead.`);
|
|
98
|
+
}
|
|
99
|
+
if (member?.stage_role === "reporter") {
|
|
100
|
+
throw new Error(`--by ${by} is a pipeline reporter agent (stage_role: reporter) and cannot run a plan by itself. ` +
|
|
101
|
+
`Run ${flag} with --executor-by <executor> --reporter-by ${by} instead.`);
|
|
102
|
+
}
|
|
103
|
+
return undefined;
|
|
104
|
+
}
|
|
105
|
+
export async function spawnAgentInPlace(command, prompt, cwd, schedulePath, executionPath, resultPath) {
|
|
106
|
+
const protectedConfigBefore = captureAgentProtectedConfigSnapshot(cwd);
|
|
107
|
+
const gitStateBefore = captureAgentGitStateSnapshot(cwd);
|
|
108
|
+
const child = spawn(command, {
|
|
109
|
+
cwd,
|
|
110
|
+
env: {
|
|
111
|
+
...gitEnvironment(),
|
|
112
|
+
SPECDOJO_SCHEDULE_PATH: schedulePath,
|
|
113
|
+
SPECDOJO_EXECUTION_PATH: executionPath,
|
|
114
|
+
},
|
|
115
|
+
shell: true,
|
|
116
|
+
stdio: ["pipe", "inherit", "inherit"],
|
|
117
|
+
});
|
|
118
|
+
// executeAgent と同じ理由で、stdin を読まないコマンドの EPIPE は無視する。
|
|
119
|
+
child.stdin.on("error", () => undefined);
|
|
120
|
+
child.stdin.end(prompt);
|
|
121
|
+
const exitCode = await new Promise((resolveExit) => {
|
|
122
|
+
child.once("error", () => resolveExit(1));
|
|
123
|
+
child.once("close", (code) => resolveExit(code ?? 1));
|
|
124
|
+
});
|
|
125
|
+
const protectedConfigChanges = changedAgentProtectedConfigPaths(cwd, protectedConfigBefore);
|
|
126
|
+
if (protectedConfigChanges.length > 0) {
|
|
127
|
+
const reason = agentProtectedConfigViolation(protectedConfigChanges);
|
|
128
|
+
process.stderr.write(`blocked: ${reason}\n`);
|
|
129
|
+
reportProtectionHandoffRecord(recordProtectedConfigBlock({
|
|
130
|
+
resultPath,
|
|
131
|
+
repoRoot: cwd,
|
|
132
|
+
paths: protectedConfigChanges,
|
|
133
|
+
reason,
|
|
134
|
+
}), resultPath);
|
|
135
|
+
return 1;
|
|
136
|
+
}
|
|
137
|
+
const gitStateChanges = changedAgentGitStateFields(cwd, gitStateBefore);
|
|
138
|
+
if (gitStateChanges.length > 0) {
|
|
139
|
+
const reason = agentGitStateViolation(gitStateChanges);
|
|
140
|
+
process.stderr.write(`blocked: ${reason}\n`);
|
|
141
|
+
reportProtectionHandoffRecord(recordGitStateBlock({
|
|
142
|
+
resultPath,
|
|
143
|
+
repoRoot: cwd,
|
|
144
|
+
before: gitStateBefore,
|
|
145
|
+
fields: gitStateChanges,
|
|
146
|
+
reason,
|
|
147
|
+
}), resultPath);
|
|
148
|
+
return 1;
|
|
149
|
+
}
|
|
150
|
+
return exitCode;
|
|
151
|
+
}
|
|
152
|
+
// Default run path: generate the plan on demand and run the agent in the current
|
|
153
|
+
// repository. No worktree, and no claim/complete events unless --track-state.
|
|
154
|
+
export async function runInPlaceMode(opts) {
|
|
155
|
+
const resolvedPaths = resolveProjectPaths({ project: opts.project });
|
|
156
|
+
activateResolvedProjectPaths(resolvedPaths);
|
|
157
|
+
const { schedulePath, executionPath, catalogPath, rolesPath, viewpointsPath, projectContext } = resolvedPaths;
|
|
158
|
+
const repoRoot = specdojoRootDir();
|
|
159
|
+
const projectId = resolvedPaths.projectId ?? opts.project ?? process.env.SPECDOJO_PROJECT ?? "";
|
|
160
|
+
const roster = loadRosterForExecutionPath(executionPath);
|
|
161
|
+
const execDefaults = loadExecDefaultsConfig(resolveExecDefaultsPath(opts, schedulePath), executionPath);
|
|
162
|
+
// --track-state controls whether this run records claim/complete events; it no longer affects
|
|
163
|
+
// file naming. Task-identity runs (--task) always use the fixed `<task-id>` stem (shared with
|
|
164
|
+
// claim / run --track-state), so an in-place result can be adopted by claim/complete without
|
|
165
|
+
// renaming; re-runs overwrite the same plan/result and git history is the audit trail. Only
|
|
166
|
+
// --deliverable / ad-hoc runs (no task identity) use a unique stem to avoid id collisions.
|
|
167
|
+
const trackState = !!opts.trackState;
|
|
168
|
+
const plansDir = join(executionPath, "exec", "plans");
|
|
169
|
+
let planPath;
|
|
170
|
+
let slug;
|
|
171
|
+
// Shared plan/result stem. For --task it is the fixed task id; for bring-your-own --plan it is
|
|
172
|
+
// recovered from the plan filename so re-running the same plan overwrites the tied result; for
|
|
173
|
+
// --deliverable it is unique per run (`<slug>-<UTC>-<rand>`).
|
|
174
|
+
let stem;
|
|
175
|
+
let task = null;
|
|
176
|
+
// null target = bring-your-own --plan (not generated, not archived).
|
|
177
|
+
let target = null;
|
|
178
|
+
let planProjectId = "";
|
|
179
|
+
// bring-your-own --plan の frontmatter から復元した targets。scaffold 時に catalog 再解決より優先する。
|
|
180
|
+
let planTargets;
|
|
181
|
+
if (opts.plan) {
|
|
182
|
+
planPath = resolve(opts.plan);
|
|
183
|
+
if (!existsSync(planPath))
|
|
184
|
+
throw new Error(`Plan not found: ${planPath}`);
|
|
185
|
+
stem = stemFromPlanPath(planPath);
|
|
186
|
+
// Recover task identity from the plan frontmatter so the result is scaffolded
|
|
187
|
+
// with complete frontmatter (id/mode/plan_ref/started_at), matching the --task
|
|
188
|
+
// path. Ad-hoc plans without a task_id keep the previous no-scaffold behavior.
|
|
189
|
+
const identity = parsePlanTaskIdentity(readFileSync(planPath, "utf8"));
|
|
190
|
+
if (identity) {
|
|
191
|
+
slug = identity.taskId;
|
|
192
|
+
planProjectId = identity.projectId;
|
|
193
|
+
planTargets = identity.targets;
|
|
194
|
+
task = {
|
|
195
|
+
id: identity.taskId,
|
|
196
|
+
// Derive the catalog local_id so a review result can resolve done_criteria. A scheduled
|
|
197
|
+
// task id (T-<track>-<local_id>-NNN) yields the local_id; a deliverable plan's task_id is
|
|
198
|
+
// already the local_id (slug).
|
|
199
|
+
local_id: extractLocalId(identity.taskId) ?? identity.taskId,
|
|
200
|
+
mode: identity.mode,
|
|
201
|
+
approach: identity.approach,
|
|
202
|
+
schedule_file: "",
|
|
203
|
+
fifo_rank: 0,
|
|
204
|
+
critical_first_rank: 0,
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
else if (opts.task) {
|
|
209
|
+
const taskId = opts.task.trim();
|
|
210
|
+
task = buildTaskView(schedulePath, executionPath, taskId);
|
|
211
|
+
slug = taskId;
|
|
212
|
+
stem = taskId;
|
|
213
|
+
planPath = join(plansDir, `${stem}-plan.md`);
|
|
214
|
+
}
|
|
215
|
+
else {
|
|
216
|
+
target = resolveDeliverableTarget(catalogPath ?? "", opts.deliverable.trim());
|
|
217
|
+
slug = target.slug;
|
|
218
|
+
stem = buildInPlaceStem(slug);
|
|
219
|
+
planPath = join(plansDir, `${stem}-plan.md`);
|
|
220
|
+
task = {
|
|
221
|
+
id: slug,
|
|
222
|
+
local_id: target.localId,
|
|
223
|
+
mode: "edit",
|
|
224
|
+
schedule_file: "",
|
|
225
|
+
fifo_rank: 0,
|
|
226
|
+
critical_first_rank: 0,
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
// --plan / --deliverable tasks carry no schedule-defined agent_pipeline. Opt in to the
|
|
230
|
+
// executor/reporter pipeline via --executor-by / --reporter-by, and reject a pipeline stage
|
|
231
|
+
// agent passed to --by before it runs (it would leave the result unfilled).
|
|
232
|
+
if (!opts.task) {
|
|
233
|
+
const pipeline = resolveInPlaceAgentPipeline(task, roster, opts, opts.plan ? "plan" : "deliverable");
|
|
234
|
+
if (pipeline && task)
|
|
235
|
+
task.agent_pipeline = pipeline;
|
|
236
|
+
}
|
|
237
|
+
const { command, actor, provider } = resolveInPlaceCommand(task, roster, opts, execDefaults);
|
|
238
|
+
const reporterCandidates = task?.agent_pipeline && task
|
|
239
|
+
? resolveReporterAgentCandidates(task, roster, execDefaults, undefined, opts.reporterBy ?? pinnedReporter(task))
|
|
240
|
+
: undefined;
|
|
241
|
+
if (task?.agent_pipeline && !reporterCandidates?.length) {
|
|
242
|
+
throw new Error("No agent found for reporter pipeline stage.");
|
|
243
|
+
}
|
|
244
|
+
const label = slug ?? planPath;
|
|
245
|
+
if (opts.dryRun) {
|
|
246
|
+
process.stdout.write(`[dry-run] target: ${label} (state ignored)\n`);
|
|
247
|
+
process.stdout.write(`[dry-run] agent: ${actor}\n`);
|
|
248
|
+
process.stdout.write(`[dry-run] command: ${command}\n`);
|
|
249
|
+
if (reporterCandidates?.[0]) {
|
|
250
|
+
process.stdout.write(`[dry-run] reporter agent: ${reporterCandidates[0].actor ?? "-"}\n`);
|
|
251
|
+
process.stdout.write(`[dry-run] reporter command: ${reporterCandidates[0].command}\n`);
|
|
252
|
+
}
|
|
253
|
+
process.stdout.write(`[dry-run] cwd: ${repoRoot}\n`);
|
|
254
|
+
process.stdout.write(`[dry-run] plan: ${planPath}\n`);
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
// Generate the plan on demand (skip for bring-your-own --plan).
|
|
258
|
+
const generatedPlan = !opts.plan;
|
|
259
|
+
if (opts.task && task) {
|
|
260
|
+
const reviewGradeOutcome = await gradeBeforeReviewPlan({
|
|
261
|
+
task,
|
|
262
|
+
projectId,
|
|
263
|
+
catalogPath,
|
|
264
|
+
repoRoot,
|
|
265
|
+
dryRun: false,
|
|
266
|
+
});
|
|
267
|
+
await generateSinglePlan({
|
|
268
|
+
executionPath,
|
|
269
|
+
projectId,
|
|
270
|
+
catalogPath: catalogPath ?? "",
|
|
271
|
+
rolesPath,
|
|
272
|
+
viewpointsPath,
|
|
273
|
+
projectContext,
|
|
274
|
+
task,
|
|
275
|
+
...(stem ? { stem } : {}),
|
|
276
|
+
...(reviewGradeOutcome ? { reviewGradeOutcome } : {}),
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
else if (target) {
|
|
280
|
+
await generateDeliverablePlan({
|
|
281
|
+
executionPath,
|
|
282
|
+
projectId,
|
|
283
|
+
catalogPath: catalogPath ?? "",
|
|
284
|
+
rolesPath,
|
|
285
|
+
viewpointsPath,
|
|
286
|
+
projectContext,
|
|
287
|
+
target,
|
|
288
|
+
...(stem ? { stem } : {}),
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
const planPrompt = expandPromptRefs(readFileSync(planPath, "utf8"));
|
|
292
|
+
const prompt = task?.agent_pipeline
|
|
293
|
+
? buildExecutorPrompt(planPrompt, execDefaults.pipeline?.parent_validations)
|
|
294
|
+
: planPrompt;
|
|
295
|
+
if (trackState) {
|
|
296
|
+
if (!opts.task)
|
|
297
|
+
throw new Error("--track-state requires --task.");
|
|
298
|
+
if (!spawnClaim(projectId, opts.task.trim(), actor)) {
|
|
299
|
+
throw new Error(`Claim failed for ${opts.task.trim()} (omit --track-state to run without state).`);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
// Scaffold the result so the agent fills in a frontmatter-complete file (mirrors on-demand plan
|
|
303
|
+
// generation). Skipped for bring-your-own --plan, where no managed task identity exists.
|
|
304
|
+
// Idempotent: never clobbers an existing result (e.g. one created by claim above).
|
|
305
|
+
let resultPath;
|
|
306
|
+
let resultScaffold;
|
|
307
|
+
if (task && slug) {
|
|
308
|
+
const finalizeSections = task.approach === "finalize" || task.approach === "bootstrap-finalize"
|
|
309
|
+
? finalizeResultSectionsForDeliverable(catalogPath ?? "", task.local_id, task.approach)
|
|
310
|
+
: undefined;
|
|
311
|
+
const targets = planTargets ??
|
|
312
|
+
targetDocIdsForScheduledTask(catalogPath ?? "", task, projectId || planProjectId);
|
|
313
|
+
resultPath = (await scaffoldResult({
|
|
314
|
+
executionPath,
|
|
315
|
+
taskId: slug,
|
|
316
|
+
mode: task.mode ?? "edit",
|
|
317
|
+
projectId: projectId || planProjectId,
|
|
318
|
+
planRef: `exec/plans/${stem ?? slug}-plan.md`,
|
|
319
|
+
agent: actor,
|
|
320
|
+
startedAt: new Date().toISOString(),
|
|
321
|
+
...(stem ? { stem } : {}),
|
|
322
|
+
...(task.approach ? { approach: task.approach } : {}),
|
|
323
|
+
...(targets ? { targets } : {}),
|
|
324
|
+
...(finalizeSections ? { finalizeSections } : {}),
|
|
325
|
+
})).resultPath;
|
|
326
|
+
resultScaffold = readResultFrontmatterSnapshot(resultPath);
|
|
327
|
+
}
|
|
328
|
+
process.stdout.write(`Running ${label} in place: ${command}\n`);
|
|
329
|
+
let exitCode;
|
|
330
|
+
let pipelineBlockReason;
|
|
331
|
+
let pipelineEvidenceRef;
|
|
332
|
+
let pipelineStateRef;
|
|
333
|
+
let pipelineRunId;
|
|
334
|
+
let pipelineFailureStage = "executor";
|
|
335
|
+
if (task?.agent_pipeline) {
|
|
336
|
+
const startedAt = new Date().toISOString();
|
|
337
|
+
pipelineRunId = `${new Date().toISOString().replace(/[-:.]/g, "")}-${randomHex(4)}`;
|
|
338
|
+
const stateLocation = pipelineStateLocation({
|
|
339
|
+
repoRoot,
|
|
340
|
+
worktreePath: repoRoot,
|
|
341
|
+
executionPath,
|
|
342
|
+
taskId: task.id,
|
|
343
|
+
runId: pipelineRunId,
|
|
344
|
+
});
|
|
345
|
+
pipelineStateRef = stateLocation.ref;
|
|
346
|
+
let pipelineState = createPipelineState({
|
|
347
|
+
taskId: task.id,
|
|
348
|
+
runId: pipelineRunId,
|
|
349
|
+
updatedAt: startedAt,
|
|
350
|
+
executorActor: actor,
|
|
351
|
+
reporterActor: reporterCandidates?.[0]?.actor,
|
|
352
|
+
});
|
|
353
|
+
pipelineState = updatePipelineStage(pipelineState, "executor", { status: "running", started_at: startedAt }, startedAt);
|
|
354
|
+
writePipelineState(stateLocation.path, pipelineState);
|
|
355
|
+
const outcome = await runWithRetry([{ command, actor, provider }], prompt, execDefaults, repoRoot, {
|
|
356
|
+
...gitEnvironment(),
|
|
357
|
+
SPECDOJO_SCHEDULE_PATH: schedulePath,
|
|
358
|
+
SPECDOJO_EXECUTION_PATH: executionPath,
|
|
359
|
+
}, resultPath);
|
|
360
|
+
const recorded = recordExecutorEvidence({
|
|
361
|
+
repoRoot,
|
|
362
|
+
worktreePath: repoRoot,
|
|
363
|
+
executionPath,
|
|
364
|
+
taskId: task.id,
|
|
365
|
+
runId: pipelineRunId,
|
|
366
|
+
actor,
|
|
367
|
+
status: outcome.result === "success"
|
|
368
|
+
? "succeeded"
|
|
369
|
+
: outcome.result === "rate_limit"
|
|
370
|
+
? "rate_limited"
|
|
371
|
+
: "failed",
|
|
372
|
+
startedAt,
|
|
373
|
+
completedAt: new Date().toISOString(),
|
|
374
|
+
exitCode: outcome.exitCode,
|
|
375
|
+
attempts: outcome.attempts,
|
|
376
|
+
stdout: outcome.stdout,
|
|
377
|
+
stderr: outcome.stderr,
|
|
378
|
+
});
|
|
379
|
+
pipelineEvidenceRef = relative(repoRoot, recorded.evidencePath).split(sep).join("/");
|
|
380
|
+
const executorCompletedAt = new Date().toISOString();
|
|
381
|
+
pipelineState = updatePipelineStage(pipelineState, "executor", {
|
|
382
|
+
status: outcome.result === "success"
|
|
383
|
+
? "succeeded"
|
|
384
|
+
: outcome.protectionBlock
|
|
385
|
+
? "blocked"
|
|
386
|
+
: outcome.result === "rate_limit"
|
|
387
|
+
? "rate_limited"
|
|
388
|
+
: "failed",
|
|
389
|
+
attempts: outcome.attempts,
|
|
390
|
+
completed_at: executorCompletedAt,
|
|
391
|
+
artifact_ref: pipelineEvidenceRef,
|
|
392
|
+
}, executorCompletedAt);
|
|
393
|
+
writePipelineState(stateLocation.path, pipelineState);
|
|
394
|
+
process.stdout.write(`Executor evidence: ${pipelineEvidenceRef}\n`);
|
|
395
|
+
if (outcome.result === "success") {
|
|
396
|
+
recorded.evidence = await appendParentValidationsToExecutorEvidence({
|
|
397
|
+
execDefaults,
|
|
398
|
+
cwd: repoRoot,
|
|
399
|
+
evidence: recorded.evidence,
|
|
400
|
+
evidencePath: recorded.evidencePath,
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
if (outcome.result === "success") {
|
|
404
|
+
pipelineFailureStage = "reporter";
|
|
405
|
+
const reporterEvidence = evidenceForReporter(recorded.evidence, {
|
|
406
|
+
repoRoot,
|
|
407
|
+
worktreePath: repoRoot,
|
|
408
|
+
executionPath,
|
|
409
|
+
...(resultPath ? { resultPath } : {}),
|
|
410
|
+
});
|
|
411
|
+
const reporterStartedAt = new Date().toISOString();
|
|
412
|
+
pipelineState = updatePipelineStage(pipelineState, "reporter", {
|
|
413
|
+
status: "running",
|
|
414
|
+
actor: reporterCandidates?.[0]?.actor ?? null,
|
|
415
|
+
started_at: reporterStartedAt,
|
|
416
|
+
}, reporterStartedAt);
|
|
417
|
+
writePipelineState(stateLocation.path, pipelineState);
|
|
418
|
+
let reporterAttempts = 0;
|
|
419
|
+
process.stdout.write(`Running reporter: ${reporterCandidates?.[0]?.command ?? ""}\n`);
|
|
420
|
+
const reporter = await runReporterWithFormatRetry({
|
|
421
|
+
plan: planPrompt,
|
|
422
|
+
evidence: reporterEvidence,
|
|
423
|
+
mode: task.mode ?? "edit",
|
|
424
|
+
invoke: async (reporterPrompt) => {
|
|
425
|
+
const reporterOutcome = await runWithRetry(reporterCandidates ?? [], reporterPrompt, execDefaults, repoRoot, {
|
|
426
|
+
...gitEnvironment(),
|
|
427
|
+
SPECDOJO_SCHEDULE_PATH: schedulePath,
|
|
428
|
+
SPECDOJO_EXECUTION_PATH: executionPath,
|
|
429
|
+
}, resultPath);
|
|
430
|
+
reporterAttempts += reporterOutcome.attempts;
|
|
431
|
+
return {
|
|
432
|
+
result: reporterOutcome.result,
|
|
433
|
+
stdout: reporterOutcome.stdout,
|
|
434
|
+
stderr: reporterOutcome.stderr,
|
|
435
|
+
};
|
|
436
|
+
},
|
|
437
|
+
});
|
|
438
|
+
if (reporter.result === "success" && resultPath) {
|
|
439
|
+
try {
|
|
440
|
+
await renderReporterResult(resultPath, reporter.output, reporterEvidence);
|
|
441
|
+
const facts = deriveReporterFacts(reporterEvidence, { mode: task.mode ?? "edit" });
|
|
442
|
+
exitCode = facts.outcome === "complete" ? 0 : 1;
|
|
443
|
+
pipelineBlockReason = facts.block_reason || undefined;
|
|
444
|
+
process.stdout.write(`Reporter complete: ${reporterCandidates?.[0]?.actor ?? "reporter"} (format attempts: ${reporter.formatAttempts})\n`);
|
|
445
|
+
}
|
|
446
|
+
catch (error) {
|
|
447
|
+
exitCode = 1;
|
|
448
|
+
pipelineBlockReason =
|
|
449
|
+
error instanceof Error
|
|
450
|
+
? `reporter result rendering failed: ${error.message}`
|
|
451
|
+
: String(error);
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
else if (reporter.result === "success") {
|
|
455
|
+
exitCode = 1;
|
|
456
|
+
pipelineBlockReason = "reporter result path is unavailable";
|
|
457
|
+
}
|
|
458
|
+
else {
|
|
459
|
+
exitCode = 1;
|
|
460
|
+
pipelineBlockReason = reporter.reason;
|
|
461
|
+
recordReporterFailureOutput({
|
|
462
|
+
worktreePath: repoRoot,
|
|
463
|
+
evidencePath: recorded.evidencePath,
|
|
464
|
+
evidence: recorded.evidence,
|
|
465
|
+
invocationOutputs: reporter.invocationOutputs,
|
|
466
|
+
});
|
|
467
|
+
}
|
|
468
|
+
const reporterCompletedAt = new Date().toISOString();
|
|
469
|
+
pipelineState = updatePipelineStage(pipelineState, "reporter", {
|
|
470
|
+
status: exitCode === 0
|
|
471
|
+
? "succeeded"
|
|
472
|
+
: reporter.result === "rate_limit"
|
|
473
|
+
? "rate_limited"
|
|
474
|
+
: "failed",
|
|
475
|
+
attempts: reporterAttempts,
|
|
476
|
+
completed_at: reporterCompletedAt,
|
|
477
|
+
artifact_ref: reporter.result === "success" && resultPath
|
|
478
|
+
? relative(repoRoot, resultPath).split(sep).join("/")
|
|
479
|
+
: null,
|
|
480
|
+
}, reporterCompletedAt);
|
|
481
|
+
writePipelineState(stateLocation.path, pipelineState);
|
|
482
|
+
}
|
|
483
|
+
else {
|
|
484
|
+
pipelineBlockReason =
|
|
485
|
+
outcome.result === "rate_limit"
|
|
486
|
+
? "executor rate limit reached"
|
|
487
|
+
: extractBlockReason(outcome.stderr);
|
|
488
|
+
exitCode = outcome.exitCode && outcome.exitCode !== 0 ? outcome.exitCode : 1;
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
else {
|
|
492
|
+
exitCode = await spawnAgentInPlace(command, prompt, repoRoot, schedulePath, executionPath, resultPath);
|
|
493
|
+
}
|
|
494
|
+
// Some agents (notably `claude -p`) exit 0 even when they conclude they are blocked: a
|
|
495
|
+
// permission-denied tool call is fed back as a tool error and the model ends its turn
|
|
496
|
+
// normally. The agent's core duty is to fill the result while preserving its scaffold
|
|
497
|
+
// frontmatter, so treat an incomplete or modified result as a block even on exit 0.
|
|
498
|
+
let effectiveExit = exitCode;
|
|
499
|
+
let blockReason = pipelineBlockReason;
|
|
500
|
+
if (exitCode === 0 &&
|
|
501
|
+
resultPath &&
|
|
502
|
+
task &&
|
|
503
|
+
isResultUnfilled(resultPath, task.mode ?? "edit", resultScaffold)) {
|
|
504
|
+
effectiveExit = 1;
|
|
505
|
+
blockReason =
|
|
506
|
+
"agent exited 0 but result is incomplete or its frontmatter differs from the scaffold (treated as blocked)";
|
|
507
|
+
process.stdout.write(`run blocked: ${label} (result incomplete or frontmatter changed despite exit 0)\n`);
|
|
508
|
+
}
|
|
509
|
+
// In-place runs do not write claim/complete events unless --track-state, but the result file's
|
|
510
|
+
// own status is a file-level field, so reflect the outcome into it regardless.
|
|
511
|
+
if (resultPath) {
|
|
512
|
+
await updateResultStatus(resultPath, effectiveExit === 0 ? "complete" : "blocked", new Date().toISOString(), blockReason);
|
|
513
|
+
}
|
|
514
|
+
if (trackState && opts.task) {
|
|
515
|
+
if (effectiveExit === 0)
|
|
516
|
+
spawnComplete(projectId, opts.task.trim(), actor);
|
|
517
|
+
else
|
|
518
|
+
spawnBlock(projectId, opts.task.trim(), actor, blockReason ?? "agent exited with non-zero code", {
|
|
519
|
+
limit_deferred: "false",
|
|
520
|
+
...(pipelineRunId
|
|
521
|
+
? pipelineRecoveryMeta({
|
|
522
|
+
stage: pipelineFailureStage,
|
|
523
|
+
evidenceRef: pipelineEvidenceRef,
|
|
524
|
+
stateRef: pipelineStateRef,
|
|
525
|
+
runId: pipelineRunId,
|
|
526
|
+
})
|
|
527
|
+
: {}),
|
|
528
|
+
});
|
|
529
|
+
}
|
|
530
|
+
if (effectiveExit !== 0) {
|
|
531
|
+
process.exitCode = effectiveExit;
|
|
532
|
+
process.stdout.write(`run failed: ${label} (exit ${effectiveExit})\n`);
|
|
533
|
+
return;
|
|
534
|
+
}
|
|
535
|
+
if (opts.archiveOnSuccess && generatedPlan && (stem ?? slug)) {
|
|
536
|
+
const archived = archivePlan({ executionPath, slug: stem ?? slug });
|
|
537
|
+
if (archived.to)
|
|
538
|
+
process.stdout.write(`Archived plan: ${archived.to}\n`);
|
|
539
|
+
}
|
|
540
|
+
process.stdout.write(`run done: ${label}\n`);
|
|
541
|
+
}
|