peaks-loop 4.1.12 → 4.1.13
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/.claude-plugin/marketplace.json +2 -2
- package/CHANGELOG.md +53 -0
- package/dist/cli/commands/_register.js +2 -0
- package/dist/cli/commands/batch-commands.d.ts +6 -0
- package/dist/cli/commands/batch-commands.js +70 -0
- package/dist/cli/commands/batch-diagnose-command.d.ts +10 -0
- package/dist/cli/commands/batch-diagnose-command.js +196 -0
- package/dist/cli/commands/batch-diagnose-envelope.d.ts +25 -0
- package/dist/cli/commands/batch-diagnose-envelope.js +60 -0
- package/dist/cli/commands/batch-diagnose-record.d.ts +21 -0
- package/dist/cli/commands/batch-diagnose-record.js +51 -0
- package/dist/cli/commands/batch-item-input.d.ts +59 -0
- package/dist/cli/commands/batch-item-input.js +113 -0
- package/dist/cli/commands/batch-report-command.d.ts +22 -0
- package/dist/cli/commands/batch-report-command.js +199 -0
- package/dist/cli/commands/core/skill-context-verdict.d.ts +16 -1
- package/dist/cli/commands/core/skill-context-verdict.js +38 -8
- package/dist/cli/commands/lint-commands.js +8 -0
- package/dist/cli/commands/lint-rules-command.d.ts +14 -0
- package/dist/cli/commands/lint-rules-command.js +44 -0
- package/dist/services/batch/batch-active-batch.d.ts +55 -0
- package/dist/services/batch/batch-active-batch.js +127 -0
- package/dist/services/batch/batch-context-reminder.d.ts +85 -0
- package/dist/services/batch/batch-context-reminder.js +201 -0
- package/dist/services/batch/batch-diagnosis-finding.d.ts +25 -0
- package/dist/services/batch/batch-diagnosis-finding.js +227 -0
- package/dist/services/batch/batch-diagnosis-report-store.d.ts +49 -0
- package/dist/services/batch/batch-diagnosis-report-store.js +133 -0
- package/dist/services/batch/batch-diagnosis-summary.d.ts +31 -0
- package/dist/services/batch/batch-diagnosis-summary.js +129 -0
- package/dist/services/batch/batch-diagnosis.d.ts +36 -0
- package/dist/services/batch/batch-diagnosis.js +107 -0
- package/dist/services/batch/batch-entry.d.ts +22 -0
- package/dist/services/batch/batch-entry.js +42 -0
- package/dist/services/batch/batch-fix-agent.d.ts +39 -0
- package/dist/services/batch/batch-fix-agent.js +126 -0
- package/dist/services/batch/batch-fix-types.d.ts +85 -0
- package/dist/services/batch/batch-fix-types.js +11 -0
- package/dist/services/batch/batch-fix.d.ts +15 -0
- package/dist/services/batch/batch-fix.js +229 -0
- package/dist/services/batch/batch-phase-advance.d.ts +45 -0
- package/dist/services/batch/batch-phase-advance.js +105 -0
- package/dist/services/batch/batch-progress-store.d.ts +52 -0
- package/dist/services/batch/batch-progress-store.js +219 -0
- package/dist/services/batch/batch-progress-types.d.ts +79 -0
- package/dist/services/batch/batch-progress-types.js +10 -0
- package/dist/services/batch/batch-recovery.d.ts +5 -0
- package/dist/services/batch/batch-recovery.js +24 -0
- package/dist/services/batch/batch-verify.d.ts +93 -0
- package/dist/services/batch/batch-verify.js +216 -0
- package/dist/services/code/auto-compact-decision.d.ts +17 -0
- package/dist/services/code/auto-compact-decision.js +6 -0
- package/dist/services/code/auto-compact-orchestrator.js +6 -0
- package/dist/services/code/batch-compaction-point.d.ts +45 -0
- package/dist/services/code/batch-compaction-point.js +99 -0
- package/dist/services/context/context-schema.d.ts +1 -1
- package/dist/services/lint/curated-rule-reader.d.ts +37 -0
- package/dist/services/lint/curated-rule-reader.js +179 -0
- package/dist/services/lint/project-lint-declarations.d.ts +27 -0
- package/dist/services/lint/project-lint-declarations.js +217 -0
- package/dist/services/lint/project-lint-tool-detection.d.ts +14 -0
- package/dist/services/lint/project-lint-tool-detection.js +208 -0
- package/dist/services/lint/project-lint-tool-rules.d.ts +19 -0
- package/dist/services/lint/project-lint-tool-rules.js +199 -0
- package/dist/services/lint/rule-pre-read-types.d.ts +66 -0
- package/dist/services/lint/rule-pre-read-types.js +22 -0
- package/dist/services/lint/rule-pre-read.d.ts +20 -0
- package/dist/services/lint/rule-pre-read.js +145 -0
- package/dist/services/skill/skill-search-service.d.ts +3 -3
- package/package.json +5 -5
- package/skills/peaks-code/references/micro-cycle.md +13 -1
- package/skills/peaks-race-code/SKILL.md +40 -1
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
// src/services/batch/batch-fix-agent.ts
|
|
2
|
+
//
|
|
3
|
+
// The agent-facing half of the batch's fix phase: the prompt one fix
|
|
4
|
+
// sub-agent receives, and the request id that agent is dispatched under.
|
|
5
|
+
//
|
|
6
|
+
// WHY THE PROMPT IS BUILT HERE AND NOT IN THE PHASE. The phase's job is
|
|
7
|
+
// ORDER: dispatch, test, flip. The prompt's job is CONTENT: the one item's
|
|
8
|
+
// diagnosis, the rules that apply to its files, and the shared governance
|
|
9
|
+
// prefix. Splitting them keeps each file one thing, and it is the same split
|
|
10
|
+
// the batch record already makes (`batch-progress-types.ts` beside
|
|
11
|
+
// `batch-progress-store.ts`).
|
|
12
|
+
//
|
|
13
|
+
// WHY THE AGENT'S PROMPT IS BUILT FROM THE DIAGNOSIS, NOT FROM THE TREE. A
|
|
14
|
+
// fix agent does not re-read the repository: the diagnosis phase already
|
|
15
|
+
// measured the root cause and the file set, and a second look would produce a
|
|
16
|
+
// second answer to the same question.
|
|
17
|
+
//
|
|
18
|
+
// WHY THE RID IS DERIVED FROM THE ITEM. The worktree branch name is derived
|
|
19
|
+
// from the rid, so two agents sharing a rid would make `git worktree add -b`
|
|
20
|
+
// fail. The derivation therefore ends in the item's id, and a collision is
|
|
21
|
+
// broken with a counter so two items that differ only in characters a rid
|
|
22
|
+
// cannot carry still get two rids.
|
|
23
|
+
import { buildDispatchSystemPrompt } from '../context/build-dispatch-system-prompt.js';
|
|
24
|
+
/** The role the fix sub-agents are dispatched as: the lane's own RD role. */
|
|
25
|
+
export const FIX_ROLE = 'rd';
|
|
26
|
+
/**
|
|
27
|
+
* The memory block a pure prompt builder carries.
|
|
28
|
+
*
|
|
29
|
+
* `buildDispatchSystemPrompt` takes a pre-fetched memory block, and the
|
|
30
|
+
* memory preflight is asynchronous I/O that belongs to the dispatch site, not
|
|
31
|
+
* to a pure prompt builder. This prompt carries no project memory block, and
|
|
32
|
+
* the composer renders its own "no block" form.
|
|
33
|
+
*/
|
|
34
|
+
const NO_MEMORY_BLOCK = { available: false };
|
|
35
|
+
/**
|
|
36
|
+
* Build the prompt one fix sub-agent receives.
|
|
37
|
+
*
|
|
38
|
+
* The prompt carries the ONE item's diagnosis and the rule list, and the
|
|
39
|
+
* shared governance prefix comes from the one composer that owns it — a
|
|
40
|
+
* second hand-rolled copy of the prefix would drift away from it.
|
|
41
|
+
*/
|
|
42
|
+
export function buildFixPrompt(item, diagnosis, rules, rid) {
|
|
43
|
+
const taskBody = [
|
|
44
|
+
`## Batch fix — ${item.label}`,
|
|
45
|
+
'',
|
|
46
|
+
`- Item id: \`${item.id}\``,
|
|
47
|
+
`- Your request id: \`${rid}\``,
|
|
48
|
+
'',
|
|
49
|
+
'You are fixing ONE item of a batch. Other agents are fixing the other',
|
|
50
|
+
'items in parallel, each on its own worktree.',
|
|
51
|
+
'',
|
|
52
|
+
...renderDiagnosis(diagnosis),
|
|
53
|
+
'',
|
|
54
|
+
'## Rules that apply to the files above',
|
|
55
|
+
'',
|
|
56
|
+
...renderRules(rules),
|
|
57
|
+
'',
|
|
58
|
+
...renderBoundaries()
|
|
59
|
+
].join('\n');
|
|
60
|
+
return buildDispatchSystemPrompt({
|
|
61
|
+
taskTitle: FIX_ROLE,
|
|
62
|
+
taskBody,
|
|
63
|
+
memoryBlock: NO_MEMORY_BLOCK
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
/** The diagnosis block: the answer the fix agent must not go looking for again. */
|
|
67
|
+
function renderDiagnosis(diagnosis) {
|
|
68
|
+
const files = diagnosis.files.length > 0
|
|
69
|
+
? diagnosis.files.map((file) => `\`${file}\``).join(', ')
|
|
70
|
+
: '(no file was reported)';
|
|
71
|
+
return [
|
|
72
|
+
'## The diagnosis (authoritative — do not re-read the repository)',
|
|
73
|
+
'',
|
|
74
|
+
`**Root cause (根因)**: ${diagnosis.rootCause}`,
|
|
75
|
+
'',
|
|
76
|
+
`**Expected files (预计文件集)**: ${files}`,
|
|
77
|
+
'',
|
|
78
|
+
`**Risk surface hit (风险面命中)**: ${diagnosis.riskSurface ?? 'none'}`,
|
|
79
|
+
'',
|
|
80
|
+
`**Fix sketch (修复草图)**: ${diagnosis.fixSketch}`,
|
|
81
|
+
'',
|
|
82
|
+
'The diagnosis above IS the answer to the reported symptom. Do NOT',
|
|
83
|
+
're-diagnose it and do NOT re-scan the repository to look for the cause',
|
|
84
|
+
'again — your job is to apply the fix it names.'
|
|
85
|
+
];
|
|
86
|
+
}
|
|
87
|
+
/** The rule list, or the honest note that none was pre-read. */
|
|
88
|
+
function renderRules(rules) {
|
|
89
|
+
if (rules.length === 0)
|
|
90
|
+
return ['- (no rule list was pre-read for this batch)'];
|
|
91
|
+
return rules.map((rule) => `- ${rule}`);
|
|
92
|
+
}
|
|
93
|
+
/** The two things a fix agent must not do. */
|
|
94
|
+
function renderBoundaries() {
|
|
95
|
+
return [
|
|
96
|
+
'## What you must not do',
|
|
97
|
+
'',
|
|
98
|
+
'- Do NOT `git merge`, `git pull`, `git rebase` or release a worktree — the parent session owns the merge.',
|
|
99
|
+
'- Do NOT run the whole test suite; your scoped test is run for you right after you finish.',
|
|
100
|
+
'',
|
|
101
|
+
'Report what you changed, and the test file your fix is scoped to.'
|
|
102
|
+
];
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Derive one agent's rid from the batch's rid and the item's id.
|
|
106
|
+
*
|
|
107
|
+
* The rid becomes a worktree branch name, so two agents may never share one.
|
|
108
|
+
* The item's id is sanitized into rid characters, and a collision (two ids
|
|
109
|
+
* that differ only in characters a rid cannot carry) is broken with a counter.
|
|
110
|
+
*/
|
|
111
|
+
export function deriveAgentRid(baseRid, itemId, used) {
|
|
112
|
+
const root = `${sanitizeRidPart(baseRid)}-fix-${sanitizeRidPart(itemId)}`;
|
|
113
|
+
let candidate = root;
|
|
114
|
+
let counter = 2;
|
|
115
|
+
while (used.has(candidate)) {
|
|
116
|
+
candidate = `${root}-${counter}`;
|
|
117
|
+
counter += 1;
|
|
118
|
+
}
|
|
119
|
+
used.add(candidate);
|
|
120
|
+
return candidate;
|
|
121
|
+
}
|
|
122
|
+
/** The characters a request id may carry, reduced to what a rid accepts. */
|
|
123
|
+
function sanitizeRidPart(raw) {
|
|
124
|
+
const cleaned = raw.replace(/[^A-Za-z0-9._-]/g, '-');
|
|
125
|
+
return cleaned.length > 0 ? cleaned : 'item';
|
|
126
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import type { BatchItem, BatchItemStatus } from './batch-progress-types.js';
|
|
2
|
+
/** What one diagnostician reported about one item. */
|
|
3
|
+
export interface BatchFixDiagnosis {
|
|
4
|
+
/** Why the reported symptom happens. */
|
|
5
|
+
readonly rootCause: string;
|
|
6
|
+
/** The repo-relative paths the fix is expected to touch. */
|
|
7
|
+
readonly files: readonly string[];
|
|
8
|
+
/** The risk surface number the diagnosis hit, when one was reported. */
|
|
9
|
+
readonly riskSurface?: number;
|
|
10
|
+
/** What the fix would look like. */
|
|
11
|
+
readonly fixSketch: string;
|
|
12
|
+
}
|
|
13
|
+
/** The descriptor one fix sub-agent is dispatched with. */
|
|
14
|
+
export interface BatchFixAgentSpec {
|
|
15
|
+
/** The request id this agent owns. Distinct per item, so worktrees do not collide. */
|
|
16
|
+
readonly rid: string;
|
|
17
|
+
readonly role: string;
|
|
18
|
+
readonly itemId: string;
|
|
19
|
+
readonly waveIndex: number;
|
|
20
|
+
/**
|
|
21
|
+
* The text `peaks worktree spawn --purpose` names this agent's worktree by.
|
|
22
|
+
*
|
|
23
|
+
* It is in the spec because the plan's isolation rule is one spawn per
|
|
24
|
+
* agent, and the spawn takes exactly these three values.
|
|
25
|
+
*/
|
|
26
|
+
readonly purpose: string;
|
|
27
|
+
/** The full dispatch prompt, governance prefix included. */
|
|
28
|
+
readonly prompt: string;
|
|
29
|
+
}
|
|
30
|
+
/** What one fix sub-agent reported back. */
|
|
31
|
+
export interface BatchFixAgentOutcome {
|
|
32
|
+
readonly ok: boolean;
|
|
33
|
+
readonly summary: string;
|
|
34
|
+
/** The test file the fix is scoped to, when the agent named one. */
|
|
35
|
+
readonly scopedTestPath?: string;
|
|
36
|
+
}
|
|
37
|
+
/** The verdict of one item's scoped test. */
|
|
38
|
+
export interface BatchFixScopedTest {
|
|
39
|
+
readonly ok: boolean;
|
|
40
|
+
/** The command that was run, for the report and for the failure text. */
|
|
41
|
+
readonly command: string;
|
|
42
|
+
readonly detail?: string;
|
|
43
|
+
}
|
|
44
|
+
/** What the scoped test is run against. */
|
|
45
|
+
export interface BatchFixScopedTestInput {
|
|
46
|
+
readonly item: BatchItem;
|
|
47
|
+
readonly outcome: BatchFixAgentOutcome;
|
|
48
|
+
}
|
|
49
|
+
/** The two effects the caller wires in. */
|
|
50
|
+
export interface BatchFixDependencies {
|
|
51
|
+
/** Dispatch one fix sub-agent and resolve with what it reported. */
|
|
52
|
+
readonly dispatchFixAgent: (spec: BatchFixAgentSpec) => Promise<BatchFixAgentOutcome> | BatchFixAgentOutcome;
|
|
53
|
+
/** Run one item's scoped test and resolve with its verdict. */
|
|
54
|
+
readonly runScopedTest: (input: BatchFixScopedTestInput) => Promise<BatchFixScopedTest> | BatchFixScopedTest;
|
|
55
|
+
}
|
|
56
|
+
/** What the fix phase needs to know. */
|
|
57
|
+
export interface BatchFixOptions {
|
|
58
|
+
readonly projectRoot: string;
|
|
59
|
+
readonly sessionId: string;
|
|
60
|
+
readonly batchId: string;
|
|
61
|
+
/** The batch's request id; every agent's rid is derived from it. */
|
|
62
|
+
readonly rid: string;
|
|
63
|
+
/** The batch's items. Only the `diagnosed` ones are fixed. */
|
|
64
|
+
readonly items: readonly BatchItem[];
|
|
65
|
+
/** Each item's diagnosis, keyed by item id. */
|
|
66
|
+
readonly diagnoses: Readonly<Record<string, BatchFixDiagnosis>>;
|
|
67
|
+
/** The rules that apply to the files being fixed (the pre-read's output). */
|
|
68
|
+
readonly rules: readonly string[];
|
|
69
|
+
}
|
|
70
|
+
/** What one item's fix produced. */
|
|
71
|
+
export interface BatchFixItemResult {
|
|
72
|
+
readonly itemId: string;
|
|
73
|
+
readonly waveIndex: number;
|
|
74
|
+
readonly rid: string;
|
|
75
|
+
/** `fixed` only when the fix and its scoped test both succeeded. */
|
|
76
|
+
readonly status: BatchItemStatus;
|
|
77
|
+
readonly fix: BatchFixAgentOutcome;
|
|
78
|
+
readonly scopedTest: BatchFixScopedTest;
|
|
79
|
+
}
|
|
80
|
+
/** What the fix phase produced. */
|
|
81
|
+
export interface BatchFixResult {
|
|
82
|
+
readonly results: readonly BatchFixItemResult[];
|
|
83
|
+
/** Items with no recorded diagnosis, so no fix agent could be dispatched. */
|
|
84
|
+
readonly undiagnosable: readonly string[];
|
|
85
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// src/services/batch/batch-fix-types.ts
|
|
2
|
+
//
|
|
3
|
+
// The vocabulary of the batch's fix phase: what a diagnosis says, what a fix
|
|
4
|
+
// agent is dispatched with, what it reports, and what the scoped test
|
|
5
|
+
// answers.
|
|
6
|
+
//
|
|
7
|
+
// WHY A SEPARATE MODULE. The fix phase touches the filesystem (the batch
|
|
8
|
+
// record) and the names here are the pure data the phase trades. Keeping the
|
|
9
|
+
// names here means the contract can be read — and typed — without importing
|
|
10
|
+
// any I/O, which is the same split the batch progress record already makes.
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { BatchFixDependencies, BatchFixOptions, BatchFixResult } from './batch-fix-types.js';
|
|
2
|
+
export type { BatchFixAgentOutcome, BatchFixAgentSpec, BatchFixDependencies, BatchFixDiagnosis, BatchFixItemResult, BatchFixOptions, BatchFixResult, BatchFixScopedTest, BatchFixScopedTestInput } from './batch-fix-types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Fix every diagnosed item of a batch, one scoped test at a time.
|
|
5
|
+
*
|
|
6
|
+
* The waves are the unit of parallelism: the agents of one wave are
|
|
7
|
+
* dispatched together, and the wave after it starts only when the previous
|
|
8
|
+
* wave's items have been tested. The scoped test of an item runs before that
|
|
9
|
+
* item's status flips, and a red test leaves the item `diagnosed`.
|
|
10
|
+
*
|
|
11
|
+
* @param options - The batch, its items, their diagnoses and the rule list.
|
|
12
|
+
* @param deps - The dispatch and the scoped-test runner.
|
|
13
|
+
* @returns One result per fixed item, and the items that had no diagnosis.
|
|
14
|
+
*/
|
|
15
|
+
export declare function runBatchFix(options: BatchFixOptions, deps: BatchFixDependencies): Promise<BatchFixResult>;
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
// src/services/batch/batch-fix.ts
|
|
2
|
+
//
|
|
3
|
+
// The batch's fix phase: one fix sub-agent per item, each carrying ONLY its
|
|
4
|
+
// own diagnosis, and a scoped test that decides whether the item counts as
|
|
5
|
+
// fixed.
|
|
6
|
+
//
|
|
7
|
+
// WHY THE SCOPED TEST IS A GATE AND NOT A REPORT. The batch's verification
|
|
8
|
+
// phase runs once, at the end, over the whole tree. When that gate goes red
|
|
9
|
+
// the question "which item broke it?" has no answer — unless every item was
|
|
10
|
+
// checked on its own, immediately after its own fix. So the scoped test runs
|
|
11
|
+
// BETWEEN the fix and the status flip: green ⇒ `appendBatchItem(status:
|
|
12
|
+
// 'fixed')`; red ⇒ the item stays `diagnosed` and the failure travels back
|
|
13
|
+
// in the result. Without that step the final gate cannot tell an integration
|
|
14
|
+
// failure from one broken item.
|
|
15
|
+
//
|
|
16
|
+
// WHY THE DISPATCH AND THE SCOPED TEST ARE INJECTED. Both are effects this
|
|
17
|
+
// library cannot perform on its own: a sub-agent is dispatched by an IDE
|
|
18
|
+
// tool call the LLM executes, and a scoped test is a test runner inside the
|
|
19
|
+
// target project. So `runBatchFix` takes both as dependencies, and the tests
|
|
20
|
+
// pass fakes. The contract that matters — own diagnosis, distinct rid,
|
|
21
|
+
// scoped test before the status flip — is then observable without spawning a
|
|
22
|
+
// real agent or running a real suite.
|
|
23
|
+
//
|
|
24
|
+
// The prompt and the rid live in `batch-fix-agent.ts`; the names live in
|
|
25
|
+
// `batch-fix-types.ts`. This file is the phase itself: the order of the
|
|
26
|
+
// steps, and the one rule that decides a status.
|
|
27
|
+
import { planBatchTopology } from './batch-diagnosis.js';
|
|
28
|
+
import { appendBatchItem, readBatchProgress } from './batch-progress-store.js';
|
|
29
|
+
import { advanceBatchPhase } from './batch-phase-advance.js';
|
|
30
|
+
import { buildFixPrompt, deriveAgentRid, FIX_ROLE } from './batch-fix-agent.js';
|
|
31
|
+
/**
|
|
32
|
+
* Fix every diagnosed item of a batch, one scoped test at a time.
|
|
33
|
+
*
|
|
34
|
+
* The waves are the unit of parallelism: the agents of one wave are
|
|
35
|
+
* dispatched together, and the wave after it starts only when the previous
|
|
36
|
+
* wave's items have been tested. The scoped test of an item runs before that
|
|
37
|
+
* item's status flips, and a red test leaves the item `diagnosed`.
|
|
38
|
+
*
|
|
39
|
+
* @param options - The batch, its items, their diagnoses and the rule list.
|
|
40
|
+
* @param deps - The dispatch and the scoped-test runner.
|
|
41
|
+
* @returns One result per fixed item, and the items that had no diagnosis.
|
|
42
|
+
*/
|
|
43
|
+
export async function runBatchFix(options, deps) {
|
|
44
|
+
const { fixable, undiagnosable } = splitDiagnosed(options);
|
|
45
|
+
const plan = planBatchTopology(fixable);
|
|
46
|
+
const scheduled = scheduleFixes(fixable, plan, options.rid);
|
|
47
|
+
const frame = {
|
|
48
|
+
context: {
|
|
49
|
+
projectRoot: options.projectRoot,
|
|
50
|
+
sessionId: options.sessionId,
|
|
51
|
+
batchId: options.batchId
|
|
52
|
+
},
|
|
53
|
+
deps,
|
|
54
|
+
diagnoses: options.diagnoses,
|
|
55
|
+
rules: options.rules
|
|
56
|
+
};
|
|
57
|
+
stampFixPhase(frame.context, fixable, plan);
|
|
58
|
+
const results = [];
|
|
59
|
+
for (const wave of plan.waves) {
|
|
60
|
+
for (const result of await runWave(frame, wave.waveIndex, scheduled)) {
|
|
61
|
+
results.push(result);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
// The ③→④ boundary: the fix stage is over, whether its scoped tests
|
|
65
|
+
// were green or not. A red test is the UNIFIED GATE's to attribute, and
|
|
66
|
+
// the attribution needs the batch to be in verification — so the advance
|
|
67
|
+
// happens either way, and it is the cheapest moment to compact.
|
|
68
|
+
advanceBatchPhase({
|
|
69
|
+
projectRoot: options.projectRoot,
|
|
70
|
+
sessionId: options.sessionId,
|
|
71
|
+
batchId: options.batchId,
|
|
72
|
+
toPhase: 'verify'
|
|
73
|
+
});
|
|
74
|
+
return { results, undiagnosable };
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Split the batch's items into the ones a fix agent can be dispatched for and
|
|
78
|
+
* the ones whose diagnosis is missing.
|
|
79
|
+
*
|
|
80
|
+
* Only a `diagnosed` item is fixed: a `pending` one has no diagnosis to
|
|
81
|
+
* carry, and a `fixed` or `verified` one is already behind us.
|
|
82
|
+
*/
|
|
83
|
+
function splitDiagnosed(options) {
|
|
84
|
+
const fixable = [];
|
|
85
|
+
const undiagnosable = [];
|
|
86
|
+
for (const item of options.items) {
|
|
87
|
+
if (item.status !== 'diagnosed')
|
|
88
|
+
continue;
|
|
89
|
+
if (options.diagnoses[item.id] === undefined)
|
|
90
|
+
undiagnosable.push(item.id);
|
|
91
|
+
else
|
|
92
|
+
fixable.push(item);
|
|
93
|
+
}
|
|
94
|
+
return { fixable, undiagnosable };
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Give every scheduled item its wave and its own rid.
|
|
98
|
+
*
|
|
99
|
+
* The rid is derived from the batch's rid and the item's id, so two agents
|
|
100
|
+
* of one wave never ask for the same worktree branch.
|
|
101
|
+
*/
|
|
102
|
+
function scheduleFixes(items, plan, baseRid) {
|
|
103
|
+
const byId = new Map(items.map((item) => [item.id, item]));
|
|
104
|
+
const usedRids = new Set();
|
|
105
|
+
const scheduled = [];
|
|
106
|
+
for (const wave of plan.waves) {
|
|
107
|
+
for (const itemId of wave.slices) {
|
|
108
|
+
const item = byId.get(itemId);
|
|
109
|
+
if (item === undefined)
|
|
110
|
+
continue;
|
|
111
|
+
scheduled.push({
|
|
112
|
+
item,
|
|
113
|
+
waveIndex: wave.waveIndex,
|
|
114
|
+
rid: deriveAgentRid(baseRid, itemId, usedRids)
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return scheduled;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Run one wave: dispatch its agents together, then test each item and flip
|
|
122
|
+
* its status if the test was green.
|
|
123
|
+
*/
|
|
124
|
+
async function runWave(frame, waveIndex, scheduled) {
|
|
125
|
+
const entries = scheduled.filter((entry) => entry.waveIndex === waveIndex);
|
|
126
|
+
const dispatched = await Promise.all(entries.map(async (entry) => ({ entry, outcome: await dispatchOne(frame, entry) })));
|
|
127
|
+
const results = [];
|
|
128
|
+
for (const { entry, outcome } of dispatched) {
|
|
129
|
+
results.push(await fixOne(frame, entry, outcome));
|
|
130
|
+
}
|
|
131
|
+
return results;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Dispatch one fix sub-agent.
|
|
135
|
+
*
|
|
136
|
+
* A dispatch that throws is a failed fix, not a failed batch: the item
|
|
137
|
+
* stays `diagnosed` and the batch's other items still run.
|
|
138
|
+
*/
|
|
139
|
+
async function dispatchOne(frame, entry) {
|
|
140
|
+
const diagnosis = frame.diagnoses[entry.item.id];
|
|
141
|
+
if (diagnosis === undefined) {
|
|
142
|
+
return { ok: false, summary: `no diagnosis was recorded for item "${entry.item.id}"` };
|
|
143
|
+
}
|
|
144
|
+
const spec = {
|
|
145
|
+
rid: entry.rid,
|
|
146
|
+
role: FIX_ROLE,
|
|
147
|
+
itemId: entry.item.id,
|
|
148
|
+
waveIndex: entry.waveIndex,
|
|
149
|
+
purpose: `Batch fix: ${entry.item.label} (${entry.item.id})`,
|
|
150
|
+
prompt: buildFixPrompt(entry.item, diagnosis, frame.rules, entry.rid)
|
|
151
|
+
};
|
|
152
|
+
try {
|
|
153
|
+
return await frame.deps.dispatchFixAgent(spec);
|
|
154
|
+
}
|
|
155
|
+
catch (error) {
|
|
156
|
+
return { ok: false, summary: `the fix agent could not be dispatched: ${describe(error)}` };
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Test one fixed item, and flip its status only if the test was green.
|
|
161
|
+
*
|
|
162
|
+
* The order is the whole point: the test runs while the item is still
|
|
163
|
+
* `diagnosed`, so a record read mid-run shows the item before the flip.
|
|
164
|
+
*/
|
|
165
|
+
async function fixOne(frame, entry, outcome) {
|
|
166
|
+
const scopedTest = await runScopedTest(frame, entry.item, outcome);
|
|
167
|
+
const fixed = outcome.ok && scopedTest.ok;
|
|
168
|
+
if (fixed) {
|
|
169
|
+
appendBatchItem(frame.context.projectRoot, frame.context.sessionId, frame.context.batchId, {
|
|
170
|
+
...entry.item,
|
|
171
|
+
status: 'fixed',
|
|
172
|
+
fixedAt: new Date().toISOString()
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
return {
|
|
176
|
+
itemId: entry.item.id,
|
|
177
|
+
waveIndex: entry.waveIndex,
|
|
178
|
+
rid: entry.rid,
|
|
179
|
+
status: fixed ? 'fixed' : 'diagnosed',
|
|
180
|
+
fix: outcome,
|
|
181
|
+
scopedTest
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Run one item's scoped test.
|
|
186
|
+
*
|
|
187
|
+
* A runner that throws is a red test, not a crashed batch: the item keeps
|
|
188
|
+
* its `diagnosed` status and the failure text travels back in the result.
|
|
189
|
+
*/
|
|
190
|
+
async function runScopedTest(frame, item, outcome) {
|
|
191
|
+
try {
|
|
192
|
+
return await frame.deps.runScopedTest({ item, outcome });
|
|
193
|
+
}
|
|
194
|
+
catch (error) {
|
|
195
|
+
return {
|
|
196
|
+
ok: false,
|
|
197
|
+
command: '',
|
|
198
|
+
detail: `the scoped test could not be run: ${describe(error)}`
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Stamp the record with the fix phase and the wave plan.
|
|
204
|
+
*
|
|
205
|
+
* The record is what survives a compaction, and its `phase` is the only
|
|
206
|
+
* field that says a batch is in flight — so entering the fix phase is
|
|
207
|
+
* written down before any agent is dispatched. The items already in the
|
|
208
|
+
* record are kept: the record knows more than this call does.
|
|
209
|
+
*/
|
|
210
|
+
function stampFixPhase(context, items, topology) {
|
|
211
|
+
const current = readBatchProgress(context.projectRoot, context.sessionId, context.batchId);
|
|
212
|
+
const recorded = [...(current?.items ?? [])];
|
|
213
|
+
for (const item of items) {
|
|
214
|
+
if (!recorded.some((candidate) => candidate.id === item.id))
|
|
215
|
+
recorded.push(item);
|
|
216
|
+
}
|
|
217
|
+
advanceBatchPhase({
|
|
218
|
+
projectRoot: context.projectRoot,
|
|
219
|
+
sessionId: context.sessionId,
|
|
220
|
+
batchId: context.batchId,
|
|
221
|
+
toPhase: 'fix',
|
|
222
|
+
items: recorded,
|
|
223
|
+
topology
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
/** An error's message, whether it is an `Error` or not. */
|
|
227
|
+
function describe(error) {
|
|
228
|
+
return error instanceof Error ? error.message : String(error);
|
|
229
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { AutoCompactMode } from '../code/auto-compact-modes.js';
|
|
2
|
+
import { type BatchCompactionPoint } from '../code/batch-compaction-point.js';
|
|
3
|
+
import type { BatchItem, BatchPhase, BatchProgress } from './batch-progress-types.js';
|
|
4
|
+
/** What the caller knows about the advance. */
|
|
5
|
+
export interface BatchPhaseAdvanceInput {
|
|
6
|
+
readonly projectRoot: string;
|
|
7
|
+
readonly sessionId: string;
|
|
8
|
+
readonly batchId: string;
|
|
9
|
+
/** The phase the batch is moving INTO. */
|
|
10
|
+
readonly toPhase: BatchPhase;
|
|
11
|
+
/** The items to record; the record's own items when omitted. */
|
|
12
|
+
readonly items?: readonly BatchItem[];
|
|
13
|
+
/** The wave plan to record; the record's own when omitted. */
|
|
14
|
+
readonly topology?: unknown;
|
|
15
|
+
/** The auto-compact mode; the project's own profile when omitted. */
|
|
16
|
+
readonly mode?: AutoCompactMode;
|
|
17
|
+
}
|
|
18
|
+
/** What the advance did. */
|
|
19
|
+
export interface BatchPhaseAdvanceResult {
|
|
20
|
+
/** The record as it was written. */
|
|
21
|
+
readonly progress: BatchProgress;
|
|
22
|
+
/** The compaction point this advance chose. */
|
|
23
|
+
readonly compaction: BatchCompactionPoint;
|
|
24
|
+
/** The notice recorded, when one was. */
|
|
25
|
+
readonly boundaryNotice?: string;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Advance the batch's phase, and record the boundary notice when a boundary
|
|
29
|
+
* was crossed.
|
|
30
|
+
*
|
|
31
|
+
* The notice is recorded only when a compaction actually happens: it SAYS
|
|
32
|
+
* that a compaction is running, so writing it while the window is still at
|
|
33
|
+
* 40% would be a false fact.
|
|
34
|
+
*
|
|
35
|
+
* @param input - The batch, the phase it is moving into, and what to record.
|
|
36
|
+
*/
|
|
37
|
+
export declare function advanceBatchPhase(input: BatchPhaseAdvanceInput): BatchPhaseAdvanceResult;
|
|
38
|
+
/**
|
|
39
|
+
* The context ratio the compaction point is judged against.
|
|
40
|
+
*
|
|
41
|
+
* The probe is the same one every other context read uses; when it cannot
|
|
42
|
+
* measure, it answers zero, which reads as "there is room" — the same
|
|
43
|
+
* answer today's conservative fallback gives.
|
|
44
|
+
*/
|
|
45
|
+
export declare function readContextRatio(projectRoot: string, sessionId: string): number;
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
// src/services/batch/batch-phase-advance.ts
|
|
2
|
+
//
|
|
3
|
+
// ONE owner for "the batch changed phase".
|
|
4
|
+
//
|
|
5
|
+
// WHY THIS MODULE EXISTS. The batch's four stages end in two compaction
|
|
6
|
+
// boundaries — ①→② (diagnosis → topology) and ③→④ (fix → verify) — and
|
|
7
|
+
// each boundary is the cheapest moment to compact: the stage's results are
|
|
8
|
+
// on disk, its agents are done, and the window is at its heaviest. Both
|
|
9
|
+
// boundaries need the same three things: the phase written down, the
|
|
10
|
+
// compaction point chosen, and the boundary notice recorded. Two writers
|
|
11
|
+
// would drift, and a drifted boundary rule is a batch that compacts at the
|
|
12
|
+
// wrong moment — or never.
|
|
13
|
+
//
|
|
14
|
+
// WHY THE NOTICE IS NOT WRITTEN HERE. The wording belongs to the
|
|
15
|
+
// verification stage (`buildPhaseBoundaryNotice`), and this module CONSUMES
|
|
16
|
+
// it. A second sentence about the same moment would be a rival, and two
|
|
17
|
+
// rivals drift apart.
|
|
18
|
+
//
|
|
19
|
+
// WHY THE COUNTS IN THE NOTICE ARE THE DIAGNOSIS COUNTS. The notice says
|
|
20
|
+
// 诊断完成 — "the diagnosis is complete" — and that is Task 2's wording,
|
|
21
|
+
// which this module does not change. So the numbers it carries have to be
|
|
22
|
+
// the DIAGNOSIS counts at every boundary: a boundary after a red scoped
|
|
23
|
+
// test still reports the diagnosis's counts, because a notice claiming the
|
|
24
|
+
// diagnosis finished 1 of 2 items when it finished 2 of 2 would be a false
|
|
25
|
+
// fact. If the notice should name the stage that finished, that is a
|
|
26
|
+
// change to the producer's wording, and the producer owns it.
|
|
27
|
+
//
|
|
28
|
+
// WHY THE RATIO IS READ HERE. The compaction point and the notice both
|
|
29
|
+
// depend on the window's occupancy, and the two callers that advance a
|
|
30
|
+
// phase must not each read it differently. One read, one place.
|
|
31
|
+
import { readContextPercent } from '../context/auto-compact-reader.js';
|
|
32
|
+
import { resolveOuterSessionId } from '../session/binding-status-service.js';
|
|
33
|
+
import { resolveAutoCompactProfile } from '../mode/mode-status-service.js';
|
|
34
|
+
import { decideBatchCompactionPoint, isBatchPhaseBoundary } from '../code/batch-compaction-point.js';
|
|
35
|
+
import { buildPhaseBoundaryNotice } from './batch-verify.js';
|
|
36
|
+
import { readBatchProgress, writeBatchProgress } from './batch-progress-store.js';
|
|
37
|
+
/**
|
|
38
|
+
* Advance the batch's phase, and record the boundary notice when a boundary
|
|
39
|
+
* was crossed.
|
|
40
|
+
*
|
|
41
|
+
* The notice is recorded only when a compaction actually happens: it SAYS
|
|
42
|
+
* that a compaction is running, so writing it while the window is still at
|
|
43
|
+
* 40% would be a false fact.
|
|
44
|
+
*
|
|
45
|
+
* @param input - The batch, the phase it is moving into, and what to record.
|
|
46
|
+
*/
|
|
47
|
+
export function advanceBatchPhase(input) {
|
|
48
|
+
const { projectRoot, sessionId, batchId, toPhase } = input;
|
|
49
|
+
const current = readBatchProgress(projectRoot, sessionId, batchId);
|
|
50
|
+
const items = input.items ?? current?.items ?? [];
|
|
51
|
+
const topology = input.topology ?? current?.topology;
|
|
52
|
+
const fromPhase = current?.phase ?? 'diagnosis';
|
|
53
|
+
const mode = input.mode ?? resolveAutoCompactProfile(projectRoot);
|
|
54
|
+
const ratio = readContextRatio(projectRoot, sessionId);
|
|
55
|
+
const compaction = decideBatchCompactionPoint({ ratio, fromPhase, toPhase, mode });
|
|
56
|
+
const boundaryNotice = resolveBoundaryNotice({ fromPhase, toPhase, items, ratio, mode, compaction }) ??
|
|
57
|
+
current?.boundaryNotice;
|
|
58
|
+
const progress = {
|
|
59
|
+
batchId,
|
|
60
|
+
phase: toPhase,
|
|
61
|
+
items,
|
|
62
|
+
...(topology === undefined ? {} : { topology }),
|
|
63
|
+
...(boundaryNotice === undefined ? {} : { boundaryNotice })
|
|
64
|
+
};
|
|
65
|
+
writeBatchProgress(projectRoot, sessionId, progress);
|
|
66
|
+
return {
|
|
67
|
+
progress,
|
|
68
|
+
compaction,
|
|
69
|
+
...(boundaryNotice === undefined ? {} : { boundaryNotice })
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* The notice for this advance, when one is due.
|
|
74
|
+
*
|
|
75
|
+
* Three conditions, and each one is a refusal: the phase did not change,
|
|
76
|
+
* the change was not a boundary, or no compaction is actually running (the
|
|
77
|
+
* notice SAYS a compaction is running, so it may not be written when none
|
|
78
|
+
* is).
|
|
79
|
+
*/
|
|
80
|
+
function resolveBoundaryNotice(input) {
|
|
81
|
+
if (input.fromPhase === input.toPhase)
|
|
82
|
+
return undefined;
|
|
83
|
+
if (!isBatchPhaseBoundary(input.fromPhase, input.toPhase))
|
|
84
|
+
return undefined;
|
|
85
|
+
if (!input.compaction.shouldCompact)
|
|
86
|
+
return undefined;
|
|
87
|
+
const diagnosed = input.items.filter((item) => item.status !== 'pending').length;
|
|
88
|
+
return buildPhaseBoundaryNotice({
|
|
89
|
+
done: diagnosed,
|
|
90
|
+
total: input.items.length,
|
|
91
|
+
ratio: input.ratio,
|
|
92
|
+
mode: input.mode
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* The context ratio the compaction point is judged against.
|
|
97
|
+
*
|
|
98
|
+
* The probe is the same one every other context read uses; when it cannot
|
|
99
|
+
* measure, it answers zero, which reads as "there is room" — the same
|
|
100
|
+
* answer today's conservative fallback gives.
|
|
101
|
+
*/
|
|
102
|
+
export function readContextRatio(projectRoot, sessionId) {
|
|
103
|
+
const outerSessionId = resolveOuterSessionId(projectRoot, sessionId, process.env);
|
|
104
|
+
return readContextPercent({ projectRoot, sessionId, outerSessionId, env: process.env }).ratio;
|
|
105
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { BatchItem, BatchProgress } from './batch-progress-types.js';
|
|
2
|
+
/**
|
|
3
|
+
* The record's path for one batch.
|
|
4
|
+
*
|
|
5
|
+
* `batchId` is caller-supplied and becomes a path segment, so it is checked
|
|
6
|
+
* before it is joined: `..` and an absolute path would otherwise let a caller
|
|
7
|
+
* write outside the session's batch directory, and a nested `a/b` would write
|
|
8
|
+
* outside the directory the batch owns.
|
|
9
|
+
*/
|
|
10
|
+
export declare function batchProgressPath(projectRoot: string, sessionId: string, batchId: string): string;
|
|
11
|
+
/**
|
|
12
|
+
* The canonical form of a file set: POSIX separators, no leading `./`, no
|
|
13
|
+
* duplicates, sorted.
|
|
14
|
+
*
|
|
15
|
+
* Two callers that measured the same file differently — one agent on Windows
|
|
16
|
+
* reporting a backslash-spelled path, another reporting the same path with
|
|
17
|
+
* forward slashes — must end up with the same string, or the disjointness
|
|
18
|
+
* check that decides whether two items may run in parallel waves reads them as
|
|
19
|
+
* disjoint and two agents edit one file. Sorting makes the stored set
|
|
20
|
+
* comparable, not just its members.
|
|
21
|
+
*/
|
|
22
|
+
export declare function normalizeFileSet(files: readonly string[]): readonly string[];
|
|
23
|
+
/**
|
|
24
|
+
* Read one batch's record. `null` when the batch has no record yet — the
|
|
25
|
+
* "no progress" answer, distinct from a record that exists but is unreadable.
|
|
26
|
+
*
|
|
27
|
+
* A malformed record throws rather than reading as `null`: the two answers
|
|
28
|
+
* mean opposite things to the caller, and a batch that silently loses its
|
|
29
|
+
* history restarts the work it had already done.
|
|
30
|
+
*/
|
|
31
|
+
export declare function readBatchProgress(projectRoot: string, sessionId: string, batchId: string): BatchProgress | null;
|
|
32
|
+
/**
|
|
33
|
+
* Write one batch's record, atomically.
|
|
34
|
+
*
|
|
35
|
+
* The timestamp is the writer's, not the caller's: the record's value is
|
|
36
|
+
* "when this batch last moved", and a caller that supplied its own clock
|
|
37
|
+
* would make that unanswerable.
|
|
38
|
+
*/
|
|
39
|
+
export declare function writeBatchProgress(projectRoot: string, sessionId: string, progress: BatchProgress): void;
|
|
40
|
+
/**
|
|
41
|
+
* Append one item to a batch's record and write it back.
|
|
42
|
+
*
|
|
43
|
+
* The append is read → merge → write, and the merge is by item id: an item
|
|
44
|
+
* that is already in the record is replaced by the new version, so an item
|
|
45
|
+
* that moves from `pending` to `diagnosed` to `fixed` is one row that changes
|
|
46
|
+
* status rather than three rows. That is what lets the recovery read "which
|
|
47
|
+
* items are still pending" by filtering one field.
|
|
48
|
+
*
|
|
49
|
+
* A batch with no record yet gets one, so the first append does not need a
|
|
50
|
+
* separate create step — the record is written as the batch runs, not before.
|
|
51
|
+
*/
|
|
52
|
+
export declare function appendBatchItem(projectRoot: string, sessionId: string, batchId: string, item: BatchItem): void;
|