@howaboua/pi-subagent-review 0.1.52 → 0.2.0

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.
@@ -1,44 +1,103 @@
1
1
  import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
2
+ import {
3
+ createChildRunDetails,
4
+ isSubagentFailure,
5
+ resolveReviewConfig,
6
+ } from "./config.js";
2
7
  import { REVIEW_COMMAND } from "./constants.js";
3
- import { createChildRunDetails, isSubagentFailure, resolveReviewConfig } from "./config.js";
4
8
  import { buildReviewConversationSummary } from "./conversation-summary.js";
5
- import { buildReviewTask, buildReviewUserMessage, detectReviewContext } from "./review.js";
9
+ import { buildReviewUserMessage, sendReviewPrefaceOnce } from "./messages.js";
10
+ import { buildReviewTask, detectReviewContext } from "./review.js";
11
+ import {
12
+ applyReviewLoopMarker,
13
+ getSemanticLeafId,
14
+ parseReviewArgs,
15
+ readReviewLoopState,
16
+ summarizeReviewLoopIncrement,
17
+ } from "./review-loop.js";
6
18
  import { getFinalOutput, runReviewSubagent } from "./subagent.js";
7
19
 
8
20
  export function registerReviewCommand(pi: ExtensionAPI) {
9
21
  pi.registerCommand(REVIEW_COMMAND, {
10
- description: "Run an isolated code-review subagent against the current repo and send the findings back as a user message",
22
+ description:
23
+ "Run an isolated code-review subagent against the current repo and send the findings back as a user message",
11
24
  handler: async (args, ctx) => {
25
+ const parsedArgs = parseReviewArgs(args);
26
+ const reviewPrefaceDetails: { markerId?: string } = {};
12
27
  const setReviewWidget = (message?: string) => {
13
28
  ctx.ui.setWidget(
14
29
  REVIEW_COMMAND,
15
30
  message
16
31
  ? [
17
- ctx.ui.theme.fg("accent", "╭─ Review"),
18
- `${ctx.ui.theme.fg("muted", "│")} ${message}`,
19
- ctx.ui.theme.fg("muted", "╰─ Please wait"),
20
- ]
32
+ ctx.ui.theme.fg("accent", "╭─ Review"),
33
+ `${ctx.ui.theme.fg("muted", "│")} ${message}`,
34
+ ctx.ui.theme.fg("muted", "╰─ Please wait"),
35
+ ]
21
36
  : undefined,
22
37
  { placement: "aboveEditor" },
23
38
  );
24
39
  };
25
40
 
26
41
  if (!ctx.isIdle()) {
27
- ctx.ui.notify(`Waiting for the current turn to finish before running /${REVIEW_COMMAND}...`, "info");
42
+ ctx.ui.notify(
43
+ `Waiting for the current turn to finish before running /${REVIEW_COMMAND}...`,
44
+ "info",
45
+ );
28
46
  await ctx.waitForIdle();
29
47
  }
30
48
 
49
+ if (!parsedArgs.startLoop) {
50
+ const markerId = readReviewLoopState(ctx)?.markerId;
51
+ if (markerId) {
52
+ const loopResult = await summarizeReviewLoopIncrement(
53
+ pi,
54
+ ctx,
55
+ markerId,
56
+ );
57
+ if (loopResult === "cancelled") {
58
+ ctx.ui.notify("/review cancelled", "warning");
59
+ return;
60
+ }
61
+ }
62
+ }
63
+
31
64
  let review;
32
65
  try {
33
66
  review = await detectReviewContext(pi, ctx.cwd);
34
67
  } catch (error) {
35
- ctx.ui.notify(error instanceof Error ? error.message : String(error), "error");
68
+ ctx.ui.notify(
69
+ error instanceof Error ? error.message : String(error),
70
+ "error",
71
+ );
36
72
  return;
37
73
  }
38
74
 
75
+ if (parsedArgs.startLoop) {
76
+ const targetId = getSemanticLeafId(ctx);
77
+ if (targetId) {
78
+ reviewPrefaceDetails.markerId = targetId;
79
+ applyReviewLoopMarker(
80
+ pi,
81
+ ctx,
82
+ targetId,
83
+ readReviewLoopState(ctx)?.markerId,
84
+ );
85
+ ctx.ui.notify("Review loop marker set", "info");
86
+ }
87
+ }
88
+ sendReviewPrefaceOnce(pi, ctx, reviewPrefaceDetails);
89
+
39
90
  if (!review.hasAnyChanges) {
40
- pi.sendUserMessage(buildReviewUserMessage(review, "No changes found relative to the selected base branch."));
41
- ctx.ui.notify(`No changes found relative to ${review.baseBranch}; sent summary to the agent.`, "info");
91
+ pi.sendUserMessage(
92
+ buildReviewUserMessage(
93
+ review,
94
+ "No changes found relative to the selected base branch.",
95
+ ),
96
+ );
97
+ ctx.ui.notify(
98
+ `No changes found relative to ${review.baseBranch}; sent summary to the agent.`,
99
+ "info",
100
+ );
42
101
  return;
43
102
  }
44
103
 
@@ -48,31 +107,61 @@ export function registerReviewCommand(pi: ExtensionAPI) {
48
107
  try {
49
108
  try {
50
109
  if (reviewConfig.summary.enabled) {
51
- if (reviewConfig.summary.source === "current") ctx.ui.notify(`Configured summary model unavailable; falling back to current session model ${reviewConfig.summary.model}.`, "warning");
110
+ if (reviewConfig.summary.source === "current")
111
+ ctx.ui.notify(
112
+ `Configured summary model unavailable; falling back to current session model ${reviewConfig.summary.model}.`,
113
+ "warning",
114
+ );
52
115
  setReviewWidget("Preparing review context…");
53
- conversationSummary = await buildReviewConversationSummary(ctx, reviewConfig);
116
+ conversationSummary = await buildReviewConversationSummary(
117
+ ctx,
118
+ reviewConfig,
119
+ );
54
120
  }
55
121
  } catch (error) {
56
122
  if (ctx.signal?.aborted) return;
57
- ctx.ui.notify(`Conversation summary unavailable: ${error instanceof Error ? error.message : String(error)}; continuing with diff-only review.`, "warning");
123
+ ctx.ui.notify(
124
+ `Conversation summary unavailable: ${error instanceof Error ? error.message : String(error)}; continuing with diff-only review.`,
125
+ "warning",
126
+ );
58
127
  }
59
128
 
60
- const task = buildReviewTask(review, args, conversationSummary);
129
+ const task = buildReviewTask(
130
+ review,
131
+ parsedArgs.focus,
132
+ conversationSummary,
133
+ );
61
134
  details = createChildRunDetails(task, review.repoRoot, reviewConfig);
62
135
  if (reviewConfig.source === "current") {
63
- ctx.ui.notify(`Configured review model unavailable; falling back to current session model ${reviewConfig.model}.`, "warning");
136
+ ctx.ui.notify(
137
+ `Configured review model unavailable; falling back to current session model ${reviewConfig.model}.`,
138
+ "warning",
139
+ );
64
140
  }
65
141
 
66
142
  setReviewWidget("Reviewing changes…");
67
- details = await runReviewSubagent(task, review.repoRoot, reviewConfig, ctx.signal);
143
+ details = await runReviewSubagent(
144
+ task,
145
+ review.repoRoot,
146
+ reviewConfig,
147
+ ctx.signal,
148
+ );
68
149
  if (ctx.signal?.aborted) return;
69
- const finalOutput = getFinalOutput(details.messages).trim() || "No actionable issues found.";
70
- if (isSubagentFailure(details)) throw new Error(details.errorMessage || details.stderr || finalOutput);
150
+ const finalOutput =
151
+ getFinalOutput(details.messages).trim() ||
152
+ "No actionable issues found.";
153
+ if (isSubagentFailure(details))
154
+ throw new Error(
155
+ details.errorMessage || details.stderr || finalOutput,
156
+ );
71
157
 
72
158
  const message = buildReviewUserMessage(review, finalOutput);
73
159
  if (ctx.isIdle()) pi.sendUserMessage(message);
74
160
  else pi.sendUserMessage(message, { deliverAs: "followUp" });
75
- ctx.ui.notify(`Review findings sent back to the main agent from /${REVIEW_COMMAND}.`, "info");
161
+ ctx.ui.notify(
162
+ `Review findings sent back to the main agent from /${REVIEW_COMMAND}.`,
163
+ "info",
164
+ );
76
165
  } catch (error) {
77
166
  const message = error instanceof Error ? error.message : String(error);
78
167
  details.exitCode = details.exitCode || 1;
@@ -0,0 +1,152 @@
1
+ import type {
2
+ ExtensionAPI,
3
+ ExtensionCommandContext,
4
+ } from "@earendil-works/pi-coding-agent";
5
+
6
+ export const REVIEW_LOOP_STATE_ENTRY = "subagent-review-loop-state";
7
+ export const REVIEW_LOOP_MARKER_LABEL = "review";
8
+ export const REVIEW_LOOP_WIDGET = "subagent-review-loop";
9
+ export const REVIEW_LOOP_SUMMARY_PROMPT = [
10
+ "Treat this as a completed review-fix increment that should become durable context before the next isolated review pass.",
11
+ "Focus on the final accepted outcome, not dead ends or step-by-step implementation noise.",
12
+ "Capture which review findings were addressed, which were intentionally skipped or deferred, concrete files changed, key decisions, tests/checks run, and any remaining risks that matter for the next review.",
13
+ "Mention relevant commands, commits, PR outcomes, or review feedback only when they change future work.",
14
+ "Omit temporary debugging details, abandoned attempts, incidental churn, and repetitive review transcript noise.",
15
+ "Write the summary so the main agent can continue from this compacted review-loop point and immediately run the next review pass.",
16
+ ].join("\n");
17
+
18
+ interface ReviewLoopState {
19
+ version: 1;
20
+ markerId: string;
21
+ }
22
+
23
+ export interface ParsedReviewArgs {
24
+ startLoop: boolean;
25
+ focus: string;
26
+ }
27
+
28
+ function isReviewLoopState(value: unknown): value is ReviewLoopState {
29
+ if (typeof value !== "object" || value === null) return false;
30
+ const candidate = value as { version?: unknown; markerId?: unknown };
31
+ return candidate.version === 1 && typeof candidate.markerId === "string";
32
+ }
33
+
34
+ export function parseReviewArgs(args: string): ParsedReviewArgs {
35
+ const trimmed = args.trim();
36
+ if (!trimmed) return { startLoop: false, focus: "" };
37
+
38
+ const match = /^(\S+)(?:\s+([\s\S]*))?$/.exec(trimmed);
39
+ const firstWord = match?.[1] ?? "";
40
+ if (firstWord.toLowerCase() !== "loop") {
41
+ return { startLoop: false, focus: trimmed };
42
+ }
43
+
44
+ return { startLoop: true, focus: (match?.[2] ?? "").trim() };
45
+ }
46
+
47
+ export function readReviewLoopState(
48
+ ctx: ExtensionCommandContext,
49
+ ): ReviewLoopState | undefined {
50
+ let state: ReviewLoopState | undefined;
51
+
52
+ for (const entry of ctx.sessionManager.getBranch()) {
53
+ if (entry.type !== "custom" || entry.customType !== REVIEW_LOOP_STATE_ENTRY)
54
+ continue;
55
+ if (isReviewLoopState(entry.data)) state = entry.data;
56
+ }
57
+
58
+ return state;
59
+ }
60
+
61
+ export function getSemanticLeafId(
62
+ ctx: ExtensionCommandContext,
63
+ ): string | undefined {
64
+ let currentId = ctx.sessionManager.getLeafId();
65
+
66
+ while (currentId) {
67
+ const entry = ctx.sessionManager.getEntry(currentId);
68
+ if (!entry) return undefined;
69
+
70
+ if (entry.type === "custom" || entry.type === "label") {
71
+ currentId = entry.parentId;
72
+ continue;
73
+ }
74
+
75
+ return currentId;
76
+ }
77
+
78
+ return undefined;
79
+ }
80
+
81
+ export function applyReviewLoopMarker(
82
+ pi: ExtensionAPI,
83
+ ctx: ExtensionCommandContext,
84
+ nextMarkerId: string,
85
+ previousMarkerId?: string,
86
+ ): void {
87
+ if (
88
+ previousMarkerId &&
89
+ previousMarkerId !== nextMarkerId &&
90
+ ctx.sessionManager.getLabel(previousMarkerId) === REVIEW_LOOP_MARKER_LABEL
91
+ ) {
92
+ pi.setLabel(previousMarkerId, undefined);
93
+ }
94
+
95
+ const existingLabel = ctx.sessionManager.getLabel(nextMarkerId);
96
+ if (
97
+ existingLabel === undefined ||
98
+ existingLabel === REVIEW_LOOP_MARKER_LABEL
99
+ ) {
100
+ pi.setLabel(nextMarkerId, REVIEW_LOOP_MARKER_LABEL);
101
+ }
102
+
103
+ pi.appendEntry(REVIEW_LOOP_STATE_ENTRY, {
104
+ version: 1,
105
+ markerId: nextMarkerId,
106
+ } satisfies ReviewLoopState);
107
+ }
108
+
109
+ export async function summarizeReviewLoopIncrement(
110
+ pi: ExtensionAPI,
111
+ ctx: ExtensionCommandContext,
112
+ markerId: string,
113
+ ): Promise<"summarized" | "skipped" | "cancelled"> {
114
+ if (!ctx.sessionManager.getEntry(markerId)) return "skipped";
115
+
116
+ const currentSemanticLeafId = getSemanticLeafId(ctx);
117
+ if (!currentSemanticLeafId || currentSemanticLeafId === markerId) {
118
+ return "skipped";
119
+ }
120
+
121
+ const clearLoopFeedback = () => {
122
+ if (ctx.hasUI) ctx.ui.setWidget(REVIEW_LOOP_WIDGET, undefined);
123
+ ctx.ui.setWorkingMessage();
124
+ };
125
+
126
+ ctx.ui.setWorkingMessage(ctx.ui.theme.fg("dim", "Summarizing review loop…"));
127
+ if (ctx.hasUI) {
128
+ ctx.ui.setWidget(
129
+ REVIEW_LOOP_WIDGET,
130
+ [ctx.ui.theme.fg("dim", "Summarizing review loop increment...")],
131
+ { placement: "aboveEditor" },
132
+ );
133
+ }
134
+
135
+ let result: Awaited<ReturnType<typeof ctx.navigateTree>>;
136
+ try {
137
+ result = await ctx.navigateTree(markerId, {
138
+ summarize: true,
139
+ customInstructions: REVIEW_LOOP_SUMMARY_PROMPT,
140
+ replaceInstructions: false,
141
+ });
142
+ } finally {
143
+ clearLoopFeedback();
144
+ }
145
+
146
+ if (result.cancelled) return "cancelled";
147
+
148
+ const nextMarkerId = getSemanticLeafId(ctx);
149
+ if (!nextMarkerId) return "skipped";
150
+ applyReviewLoopMarker(pi, ctx, nextMarkerId, markerId);
151
+ return "summarized";
152
+ }