pi-bro 0.38.1 → 0.40.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.
- package/CHANGELOG.md +23 -0
- package/README.md +4 -9
- package/docs/README.md +1 -1
- package/docs/feature-map.md +3 -3
- package/docs/features/architecture.md +1 -1
- package/docs/features/btw.md +1 -3
- package/docs/features/elevate.md +1 -1
- package/docs/features/grill-me.md +1 -1
- package/docs/features/guided-review.md +5 -5
- package/docs/features/overthink.md +36 -34
- package/docs/features/remember-me.md +0 -2
- package/docs/overthink.md +2 -2
- package/docs/reference/commands.md +2 -2
- package/docs/reference/data-and-permissions.md +7 -18
- package/docs/reference/settings.md +7 -5
- package/docs/reference/troubleshooting.md +0 -4
- package/package.json +1 -5
- package/src/advisor.ts +1 -1
- package/src/architecture.ts +2 -2
- package/src/backend.ts +12 -31
- package/src/backends/agy.ts +4 -4
- package/src/backends/cli-process.ts +5 -11
- package/src/backends/contract.ts +4 -7
- package/src/bro.ts +1 -1
- package/src/btw.ts +1 -1
- package/src/config-navigation-ui.ts +0 -2
- package/src/config-navigation.ts +3 -6
- package/src/config-ui.ts +34 -19
- package/src/doctor.ts +68 -34
- package/src/explain-modal.ts +2 -2
- package/src/grill.ts +1 -1
- package/src/overthink-state.ts +566 -140
- package/src/overthink.ts +338 -162
- package/src/remember-engine.ts +0 -4
- package/src/settings.ts +98 -81
- package/src/backends/grok.ts +0 -204
- package/src/backends/muse.ts +0 -173
- package/src/overthink-questions-ui.ts +0 -131
package/src/overthink.ts
CHANGED
|
@@ -1,31 +1,34 @@
|
|
|
1
|
-
/** Automatic same-model second look.
|
|
1
|
+
/** Automatic same-model second look. One actionable alert or silence, with inspectable evidence. */
|
|
2
2
|
import type { ExtensionAPI, ExtensionCommandContext, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
3
|
-
import {
|
|
3
|
+
import { copyToClipboard } from "@earendil-works/pi-coding-agent";
|
|
4
4
|
import { normalizeContext } from "@earendil-works/pi-ai";
|
|
5
5
|
import { matchesKey, wrapTextWithAnsi, type Component, type TUI } from "@earendil-works/pi-tui";
|
|
6
6
|
import { modalHeight, openBroOverlay, renderFrame, Scroller, formatNotice, type Theme } from "./modal-kit.ts";
|
|
7
7
|
import { hasBroCustomUi } from "./ui-capabilities.ts";
|
|
8
8
|
import {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
type OverthinkNote,
|
|
9
|
+
allowBranchAutomaticChecks, buildOverthinkReviewContext, formatOverthinkEvidenceLine,
|
|
10
|
+
formatOverthinkFinding, formatOverthinkPreview, overthinkContextPosition, overthinkIsStale,
|
|
11
|
+
overthinkPrompt, overthinkTurnState, parseOverthinkFinding, readGlobalOverthink,
|
|
12
|
+
readOverthinkUsage, resolveOverthinkSession, saveOverthinkFailure, saveOverthinkNote,
|
|
13
|
+
shouldRunAutomaticOverthink,
|
|
14
|
+
type OverthinkFinding, type OverthinkGlobal, type OverthinkNote, type OverthinkPromptOptions, type OverthinkSession,
|
|
15
15
|
} from "./overthink-state.ts";
|
|
16
|
-
import { openOverthinkQuestionEditor } from "./overthink-questions-ui.ts";
|
|
17
16
|
import { readPreferences } from "./settings.ts";
|
|
18
17
|
import { errorMessage } from "./util.ts";
|
|
19
18
|
|
|
20
|
-
const MIN_TOOL_RESULTS = 3;
|
|
19
|
+
export const MIN_TOOL_RESULTS = 3;
|
|
20
|
+
export type OverthinkGate = "all" | "substantive";
|
|
21
|
+
// shortcut: pilot checks all normally settled turns; switch to "substantive" if eval rejects broader coverage.
|
|
22
|
+
export const PILOT_OVERTHINK_GATE: OverthinkGate = "all";
|
|
23
|
+
|
|
21
24
|
const WIDGET = "bro-overthink";
|
|
22
25
|
const NOTE_TIMEOUT_MS = 90_000;
|
|
26
|
+
const MAX_PREVIEW_FINDING_LINES = 6;
|
|
23
27
|
|
|
24
|
-
type ViewerAction = "close" | "edit";
|
|
25
|
-
type CheckResult = OverthinkNote;
|
|
26
28
|
type CheckContext = ExtensionContext | ExtensionCommandContext;
|
|
27
29
|
|
|
28
30
|
export type OverthinkRunEvidence = { toolResults: number; normalFinish: boolean };
|
|
31
|
+
|
|
29
32
|
/** Collect evidence at each agent_end, but never start a check until agent_settled. */
|
|
30
33
|
export function accumulateOverthinkEvidence(
|
|
31
34
|
prior: OverthinkRunEvidence | undefined,
|
|
@@ -38,44 +41,51 @@ export function accumulateOverthinkEvidence(
|
|
|
38
41
|
};
|
|
39
42
|
}
|
|
40
43
|
|
|
41
|
-
export function overthinkReady(
|
|
42
|
-
|
|
44
|
+
export function overthinkReady(
|
|
45
|
+
evidence: OverthinkRunEvidence | undefined,
|
|
46
|
+
aborted: boolean,
|
|
47
|
+
pending: boolean,
|
|
48
|
+
gate: OverthinkGate = PILOT_OVERTHINK_GATE,
|
|
49
|
+
): boolean {
|
|
50
|
+
if (aborted || pending || !evidence?.normalFinish) return false;
|
|
51
|
+
return gate === "substantive" ? evidence.toolResults >= MIN_TOOL_RESULTS : true;
|
|
43
52
|
}
|
|
44
53
|
|
|
54
|
+
function wrapPreviewFinding(text: string, width: number): string[] {
|
|
55
|
+
const lines = wrapTextWithAnsi(text.replace(/\s+/g, " ").trim(), width);
|
|
56
|
+
if (lines.length <= MAX_PREVIEW_FINDING_LINES) return lines;
|
|
57
|
+
const kept = lines.slice(0, MAX_PREVIEW_FINDING_LINES);
|
|
58
|
+
const last = kept[kept.length - 1]!;
|
|
59
|
+
kept[kept.length - 1] = last.length > 1 ? last.slice(0, Math.max(1, width - 1)) + "…" : "…";
|
|
60
|
+
return kept;
|
|
61
|
+
}
|
|
45
62
|
|
|
46
|
-
function noteWidget(ctx: CheckContext,
|
|
63
|
+
function noteWidget(ctx: CheckContext, finding?: OverthinkFinding, sourcePosition?: string): void {
|
|
47
64
|
if (ctx.mode !== "tui") return;
|
|
48
|
-
ctx.ui.setWidget(WIDGET,
|
|
65
|
+
ctx.ui.setWidget(WIDGET, finding
|
|
49
66
|
? (_tui, theme) => ({
|
|
50
67
|
invalidate() {},
|
|
51
68
|
render(width: number) {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
69
|
+
// Hide immediately if compaction, context edits, or later messages moved the branch.
|
|
70
|
+
if (sourcePosition && overthinkContextPosition(ctx.sessionManager.getBranch()) !== sourcePosition) {
|
|
71
|
+
return [];
|
|
72
|
+
}
|
|
73
|
+
const { header } = formatOverthinkPreview(finding);
|
|
74
|
+
const bodyWidth = Math.max(12, width - 2);
|
|
75
|
+
const findingLines = wrapPreviewFinding(finding.finding, bodyWidth);
|
|
76
|
+
const nextMoveLines = wrapTextWithAnsi(finding.nextMove.replace(/\s+/g, " ").trim(), bodyWidth);
|
|
77
|
+
return [
|
|
78
|
+
"",
|
|
79
|
+
theme.fg("warning", header),
|
|
80
|
+
...findingLines.map(line => " " + theme.fg("muted", line)),
|
|
81
|
+
...nextMoveLines.map(line => " " + theme.fg("text", line)),
|
|
82
|
+
theme.fg("dim", " /bro overthink for evidence"),
|
|
83
|
+
];
|
|
57
84
|
},
|
|
58
85
|
})
|
|
59
86
|
: undefined);
|
|
60
87
|
}
|
|
61
88
|
|
|
62
|
-
export function questionStatus(note: OverthinkNote | undefined, questions: string[], position?: string): string {
|
|
63
|
-
if (!note) return "No assessment yet.";
|
|
64
|
-
const changed = note.questionKey !== questionKey(questions) ? " · questions changed" : "";
|
|
65
|
-
const freshness = overthinkIsStale(note, position)
|
|
66
|
-
? (note.sourcePosition ? " · OUTDATED (later work)" : " · source position not recorded")
|
|
67
|
-
: " · current";
|
|
68
|
-
// The full timestamp and provider usage remain in the saved note; show only
|
|
69
|
-
// what helps orient someone opening the viewer after a long work session.
|
|
70
|
-
return "Checked " + note.checkedAt.slice(0, 16).replace("T", " ") + " · " + note.model + freshness + changed;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/** A saved question snapshot keeps old notes aligned even after session edits. */
|
|
74
|
-
export function overthinkQuestionLabel(question: string, position: number): string {
|
|
75
|
-
const index = (FACTORY_QUESTIONS as readonly string[]).indexOf(question);
|
|
76
|
-
return (position + 1) + ". " + (index < 0 ? "Question" : FACTORY_LENSES[index]);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
89
|
/** A reported zero may mean a miss OR a provider that does not support cache accounting. */
|
|
80
90
|
export function usageStatus(note: OverthinkNote | undefined): string[] {
|
|
81
91
|
const u = note?.usage;
|
|
@@ -94,97 +104,218 @@ export function usageStatus(note: OverthinkNote | undefined): string[] {
|
|
|
94
104
|
];
|
|
95
105
|
}
|
|
96
106
|
|
|
107
|
+
function shortDate(iso: string): string {
|
|
108
|
+
return iso.slice(0, 16).replace("T", " ");
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function renderFindingSection(
|
|
112
|
+
add: (value: string, color?: "accent" | "muted") => void,
|
|
113
|
+
finding: OverthinkFinding,
|
|
114
|
+
): void {
|
|
115
|
+
add(finding.label ?? "Finding", "accent");
|
|
116
|
+
add(finding.finding);
|
|
117
|
+
add("");
|
|
118
|
+
add("Next move", "accent");
|
|
119
|
+
add(finding.nextMove);
|
|
120
|
+
add("");
|
|
121
|
+
add("Situation", "accent");
|
|
122
|
+
add(finding.situation);
|
|
123
|
+
add("");
|
|
124
|
+
add("Evidence", "accent");
|
|
125
|
+
for (const item of finding.evidence) {
|
|
126
|
+
add("• " + formatOverthinkEvidenceLine(item));
|
|
127
|
+
}
|
|
128
|
+
add("");
|
|
129
|
+
add("Why it matters", "accent");
|
|
130
|
+
add(finding.whyItMatters);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function renderLegacyNote(
|
|
134
|
+
add: (value: string, color?: "accent" | "muted") => void,
|
|
135
|
+
note: OverthinkNote,
|
|
136
|
+
): void {
|
|
137
|
+
if (note.assessment) {
|
|
138
|
+
add("Earlier assessment (legacy)", "accent");
|
|
139
|
+
add(note.assessment.summary);
|
|
140
|
+
note.assessment.questions.forEach((question, i) => {
|
|
141
|
+
add("");
|
|
142
|
+
add((i + 1) + ". " + question, "muted");
|
|
143
|
+
add(note.assessment!.answers[i] ?? "");
|
|
144
|
+
});
|
|
145
|
+
} else if (note.text) {
|
|
146
|
+
add("Earlier assessment (legacy plain text)", "accent");
|
|
147
|
+
note.text.split("\n").forEach(line => add(line));
|
|
148
|
+
} else {
|
|
149
|
+
add("Earlier assessment (legacy — nothing flagged). Ctrl+R checks this turn with the current reviewer.", "muted");
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function renderPreviousNote(
|
|
154
|
+
add: (value: string, color?: "accent" | "muted") => void,
|
|
155
|
+
note: OverthinkNote,
|
|
156
|
+
position: string | undefined,
|
|
157
|
+
): void {
|
|
158
|
+
const staleReason = overthinkIsStale(note, position)
|
|
159
|
+
? (note.sourcePosition ? "OUTDATED · earlier turn" : "source position not recorded")
|
|
160
|
+
: "earlier check";
|
|
161
|
+
add("");
|
|
162
|
+
add("Earlier check (" + staleReason + " · " + shortDate(note.checkedAt) + " · " + note.model + ")", "muted");
|
|
163
|
+
add("");
|
|
164
|
+
if (note.finding) renderFindingSection(add, note.finding);
|
|
165
|
+
else renderLegacyNote(add, note);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export type OverthinkViewerSnapshot = {
|
|
169
|
+
session: OverthinkSession;
|
|
170
|
+
position?: string;
|
|
171
|
+
checking?: boolean;
|
|
172
|
+
globalAutomatic?: boolean;
|
|
173
|
+
};
|
|
174
|
+
|
|
97
175
|
export function createOverthinkViewer(
|
|
98
|
-
read: () =>
|
|
99
|
-
retry: () => Promise<
|
|
176
|
+
read: () => OverthinkViewerSnapshot,
|
|
177
|
+
retry: () => Promise<OverthinkNote>,
|
|
178
|
+
allowBranch?: () => void,
|
|
100
179
|
) {
|
|
101
|
-
return (tui: TUI, theme: Theme, _keys: unknown, done: (
|
|
180
|
+
return (tui: TUI, theme: Theme, _keys: unknown, done: () => void): Component => {
|
|
102
181
|
let help = false;
|
|
103
182
|
let showUsage = false;
|
|
104
183
|
let busy = false;
|
|
105
184
|
let closed = false;
|
|
106
185
|
let notice = "";
|
|
107
186
|
const scroller = new Scroller(() => tui.requestRender());
|
|
187
|
+
|
|
188
|
+
const copyTarget = (): { note?: OverthinkNote; earlier: boolean } => {
|
|
189
|
+
const { session, position } = read();
|
|
190
|
+
const turn = overthinkTurnState(session, position);
|
|
191
|
+
if (turn.state === "finding" || turn.state === "none" || turn.state === "legacy") {
|
|
192
|
+
return { note: turn.note, earlier: false };
|
|
193
|
+
}
|
|
194
|
+
return { note: turn.previousNote, earlier: true };
|
|
195
|
+
};
|
|
196
|
+
|
|
108
197
|
const copy = async () => {
|
|
109
|
-
const note =
|
|
198
|
+
const { note, earlier } = copyTarget();
|
|
110
199
|
if (!note?.text) { notice = formatNotice(theme, "Nothing to copy yet.", "warning"); tui.requestRender(); return; }
|
|
111
|
-
try {
|
|
200
|
+
try {
|
|
201
|
+
await copyToClipboard(note.text);
|
|
202
|
+
notice = formatNotice(theme, earlier ? "Earlier assessment copied." : "Assessment copied.", "success");
|
|
203
|
+
}
|
|
112
204
|
catch (error) { notice = formatNotice(theme, "Copy failed: " + errorMessage(error), "error"); }
|
|
113
205
|
tui.requestRender();
|
|
114
206
|
};
|
|
207
|
+
|
|
115
208
|
const rerun = async () => {
|
|
116
209
|
if (busy || closed) return;
|
|
117
210
|
busy = true; notice = ""; tui.requestRender();
|
|
118
211
|
try {
|
|
119
212
|
const result = await retry();
|
|
120
|
-
if (!closed) {
|
|
213
|
+
if (!closed) {
|
|
214
|
+
scroller.toStart();
|
|
215
|
+
notice = formatNotice(theme, result.finding ? "Check refreshed." : "Checked — nothing material to flag.", "success");
|
|
216
|
+
}
|
|
121
217
|
} catch (error) {
|
|
122
|
-
if (!closed) notice = formatNotice(theme, "
|
|
218
|
+
if (!closed) notice = formatNotice(theme, "Check failed: " + errorMessage(error), "error");
|
|
123
219
|
} finally { busy = false; if (!closed) tui.requestRender(); }
|
|
124
220
|
};
|
|
221
|
+
|
|
222
|
+
const allow = () => {
|
|
223
|
+
const snapshot = read();
|
|
224
|
+
if (!snapshot.session.branchOptOut || !allowBranch) return;
|
|
225
|
+
allowBranch();
|
|
226
|
+
notice = formatNotice(
|
|
227
|
+
theme,
|
|
228
|
+
snapshot.globalAutomatic
|
|
229
|
+
? "Automatic checks allowed on this branch."
|
|
230
|
+
: "Allowed on this branch (enable global automatic checks in /bro config to run automatically).",
|
|
231
|
+
"success",
|
|
232
|
+
);
|
|
233
|
+
tui.requestRender();
|
|
234
|
+
};
|
|
235
|
+
|
|
125
236
|
return {
|
|
126
237
|
render(width: number) {
|
|
127
|
-
const {
|
|
238
|
+
const { session, position, checking, globalAutomatic } = read();
|
|
239
|
+
const isChecking = busy || Boolean(checking);
|
|
240
|
+
const turn = overthinkTurnState(session, position, isChecking ? position : undefined);
|
|
128
241
|
const inner = Math.max(1, width - 2);
|
|
129
242
|
const lines: string[] = [];
|
|
130
243
|
const add = (value: string, color?: "accent" | "muted") => {
|
|
131
244
|
const styled = color ? theme.fg(color, color === "accent" ? theme.bold(value) : value) : value;
|
|
132
245
|
lines.push(...wrapTextWithAnsi(styled, inner));
|
|
133
246
|
};
|
|
247
|
+
|
|
134
248
|
if (help) {
|
|
135
249
|
[
|
|
136
|
-
"Overthink
|
|
137
|
-
"
|
|
250
|
+
"Overthink gives Pi's latest settled turn one private second look using the active Pi model.",
|
|
251
|
+
"Enable automatic checks in /bro config → Features → Overthink. Notes stay out of Pi's conversation.",
|
|
138
252
|
"",
|
|
139
|
-
"Ctrl+R
|
|
140
|
-
"
|
|
141
|
-
"U Show or hide
|
|
253
|
+
"Ctrl+R Recheck the current turn now",
|
|
254
|
+
...(session.branchOptOut && allowBranch ? ["A Allow automatic checks on this branch (clears legacy empty-list opt-out)"] : []),
|
|
255
|
+
"U Show or hide provider-reported token, cache and estimated-cost details",
|
|
142
256
|
"? Toggle help",
|
|
143
|
-
"Ctrl+C Copy the
|
|
257
|
+
"Ctrl+C Copy the displayed finding as Markdown",
|
|
144
258
|
"Esc Close / back from help",
|
|
145
259
|
].forEach(value => add(value));
|
|
146
260
|
} else {
|
|
147
|
-
|
|
261
|
+
if (turn.state === "checking") {
|
|
262
|
+
add("Checking the current turn…", "muted");
|
|
263
|
+
} else if (turn.state === "failed") {
|
|
264
|
+
add("Check failed · " + shortDate(turn.failure.checkedAt) + " · " + turn.failure.model, "muted");
|
|
265
|
+
} else if (turn.state === "finding" || turn.state === "none") {
|
|
266
|
+
add("Reviewed: current turn · " + shortDate(turn.note.checkedAt) + " · " + turn.note.model, "muted");
|
|
267
|
+
} else if (turn.state === "legacy") {
|
|
268
|
+
add("Earlier assessment (legacy) · " + shortDate(turn.note.checkedAt) + " · " + turn.note.model, "muted");
|
|
269
|
+
} else {
|
|
270
|
+
add("Not checked for this turn.", "muted");
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
if (session.branchOptOut && allowBranch) {
|
|
274
|
+
add("");
|
|
275
|
+
add(
|
|
276
|
+
"Automatic checks are off on this branch (legacy empty question list). Press A to allow automatic checks on this branch." +
|
|
277
|
+
(globalAutomatic ? "" : " Global automatic checks are currently off in /bro config."),
|
|
278
|
+
"muted",
|
|
279
|
+
);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
const usageNote = turn.state === "finding" || turn.state === "none" || turn.state === "legacy"
|
|
283
|
+
? turn.note : turn.previousNote;
|
|
148
284
|
if (showUsage) {
|
|
149
285
|
add("");
|
|
150
286
|
add("Usage details", "accent");
|
|
151
|
-
usageStatus(
|
|
287
|
+
usageStatus(usageNote).forEach(value => add(value));
|
|
152
288
|
}
|
|
289
|
+
|
|
153
290
|
add("");
|
|
154
|
-
if (
|
|
155
|
-
add("Checking the current
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
add(
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
add
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
add(note.assessment!.answers[i] ?? "");
|
|
168
|
-
});
|
|
291
|
+
if (turn.state === "checking") {
|
|
292
|
+
add("Checking the current turn…");
|
|
293
|
+
if (turn.previousNote) renderPreviousNote(add, turn.previousNote, position);
|
|
294
|
+
} else if (turn.state === "failed") {
|
|
295
|
+
add("Check failed", "accent");
|
|
296
|
+
add(turn.failure.error);
|
|
297
|
+
if (turn.previousNote) renderPreviousNote(add, turn.previousNote, position);
|
|
298
|
+
} else if (turn.state === "finding") {
|
|
299
|
+
renderFindingSection(add, turn.finding);
|
|
300
|
+
} else if (turn.state === "none") {
|
|
301
|
+
add("Checked — nothing material to flag.");
|
|
302
|
+
} else if (turn.state === "legacy") {
|
|
303
|
+
renderLegacyNote(add, turn.note);
|
|
169
304
|
} else {
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
add("Earlier assessment (plain text)", "accent");
|
|
173
|
-
note.text.split("\n").forEach(line => add(line));
|
|
305
|
+
add("Not checked for this turn. Ctrl+R checks now.");
|
|
306
|
+
if (turn.previousNote) renderPreviousNote(add, turn.previousNote, position);
|
|
174
307
|
}
|
|
175
308
|
}
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
// notices. Count them before capping or short notes lose their last line.
|
|
309
|
+
|
|
310
|
+
const hints = "↑/↓ scroll · Ctrl+R recheck" +
|
|
311
|
+
(session.branchOptOut && allowBranch ? " · A allow branch" : "") +
|
|
312
|
+
" · U usage · ? help · Ctrl+C copy · Esc close";
|
|
181
313
|
const hintRows = wrapTextWithAnsi(hints, inner).length;
|
|
182
314
|
const noticeRows = notice ? Math.min(3, wrapTextWithAnsi(notice, inner).length) : 0;
|
|
183
315
|
const cap = Math.max(8, lines.length + 5 + hintRows + noticeRows);
|
|
184
316
|
return renderFrame(theme, width, modalHeight(tui, undefined, cap), {
|
|
185
317
|
header: () => theme.fg("accent", theme.bold("Bro · Overthink")) +
|
|
186
|
-
(
|
|
187
|
-
(!help ? " · " + questions.length + (questions.length === 1 ? " question" : " questions") : "") +
|
|
318
|
+
(isChecking ? " · checking" : "") +
|
|
188
319
|
(scroller.indicator() ? " · " + scroller.indicator() : ""),
|
|
189
320
|
body: (rows) => scroller.view(lines, rows),
|
|
190
321
|
notice,
|
|
@@ -196,126 +327,168 @@ export function createOverthinkViewer(
|
|
|
196
327
|
handleInput(data: string) {
|
|
197
328
|
if (matchesKey(data, "escape")) {
|
|
198
329
|
if (help) { help = false; scroller.toStart(); tui.requestRender(); }
|
|
199
|
-
else { closed = true; done(
|
|
330
|
+
else { closed = true; done(); }
|
|
200
331
|
return;
|
|
201
332
|
}
|
|
202
333
|
if (matchesKey(data, "?")) { help = !help; scroller.toStart(); tui.requestRender(); return; }
|
|
203
334
|
if (matchesKey(data, "u") || matchesKey(data, "shift+u")) { showUsage = !showUsage; scroller.toStart(); tui.requestRender(); return; }
|
|
335
|
+
if ((matchesKey(data, "a") || matchesKey(data, "shift+a")) && !help) { allow(); return; }
|
|
204
336
|
if (matchesKey(data, "ctrl+r")) { void rerun(); return; }
|
|
205
337
|
if (matchesKey(data, "ctrl+c")) { void copy(); return; }
|
|
206
|
-
if (matchesKey(data, "ctrl+e") && !busy) { closed = true; done("edit"); return; }
|
|
207
338
|
scroller.handleKey(data);
|
|
208
339
|
},
|
|
209
340
|
};
|
|
210
341
|
};
|
|
211
342
|
}
|
|
212
343
|
|
|
213
|
-
export
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
});
|
|
221
|
-
}
|
|
344
|
+
export type RegisterOverthinkOptions = {
|
|
345
|
+
loadGlobal?: () => Promise<OverthinkGlobal>;
|
|
346
|
+
loadAutomaticChecks?: () => Promise<boolean>;
|
|
347
|
+
gate?: OverthinkGate;
|
|
348
|
+
promptOptions?: Omit<OverthinkPromptOptions, "preferences" | "sourceIndex">;
|
|
349
|
+
timeoutMs?: number;
|
|
350
|
+
};
|
|
222
351
|
|
|
223
|
-
export function registerOverthink(pi: ExtensionAPI,
|
|
352
|
+
export function registerOverthink(pi: ExtensionAPI, options: RegisterOverthinkOptions = {}): {
|
|
224
353
|
open(ctx: ExtensionCommandContext): Promise<void>;
|
|
354
|
+
recheck(ctx: CheckContext): Promise<OverthinkNote>;
|
|
225
355
|
} {
|
|
356
|
+
const loadGlobal: () => Promise<OverthinkGlobal> = options.loadGlobal ?? (options.loadAutomaticChecks
|
|
357
|
+
? async () => {
|
|
358
|
+
const ok = await options.loadAutomaticChecks!();
|
|
359
|
+
return { automatic: ok, savedAutomatic: ok, legacyEmptyOptOut: false };
|
|
360
|
+
}
|
|
361
|
+
: readGlobalOverthink);
|
|
362
|
+
const gate = options.gate ?? PILOT_OVERTHINK_GATE;
|
|
363
|
+
const promptOptions = options.promptOptions ?? {};
|
|
364
|
+
const timeoutMs = options.timeoutMs ?? NOTE_TIMEOUT_MS;
|
|
365
|
+
|
|
226
366
|
let active: AbortController | undefined;
|
|
367
|
+
let checkingPosition: string | undefined;
|
|
227
368
|
let generation = 0;
|
|
228
369
|
let evidence: OverthinkRunEvidence | undefined;
|
|
370
|
+
|
|
229
371
|
const invalidate = (_event: unknown, ctx: ExtensionContext) => {
|
|
230
372
|
generation++;
|
|
231
373
|
evidence = undefined;
|
|
374
|
+
checkingPosition = undefined;
|
|
232
375
|
active?.abort(); active = undefined;
|
|
233
376
|
noteWidget(ctx);
|
|
234
377
|
};
|
|
235
378
|
|
|
236
|
-
|
|
237
|
-
// inherit the nearest private custom entry rather than sharing a project setting.
|
|
238
|
-
pi.on("session_start", async (_event, ctx) => {
|
|
239
|
-
invalidate(_event, ctx);
|
|
240
|
-
try { await ensureOverthinkSession(pi, ctx.sessionManager.getBranch()); }
|
|
241
|
-
catch (error) { if (ctx.mode === "tui") ctx.ui.notify("Overthink settings: " + errorMessage(error), "warning"); }
|
|
242
|
-
});
|
|
379
|
+
pi.on("session_start", invalidate);
|
|
243
380
|
pi.on("session_tree", invalidate);
|
|
244
381
|
pi.on("agent_start", (_event, ctx) => {
|
|
245
382
|
// A continuation or retry can begin a new agent loop before the overall
|
|
246
|
-
// run settles. Cancel in-flight checks and
|
|
383
|
+
// run settles. Cancel in-flight checks and clear any old preview, but keep
|
|
247
384
|
// tool evidence until agent_settled decides whether the full run qualifies.
|
|
248
385
|
generation++;
|
|
386
|
+
checkingPosition = undefined;
|
|
249
387
|
active?.abort(); active = undefined;
|
|
250
388
|
noteWidget(ctx);
|
|
251
389
|
});
|
|
252
|
-
pi.on("session_shutdown",
|
|
390
|
+
pi.on("session_shutdown", invalidate);
|
|
391
|
+
|
|
392
|
+
type Trigger = { generation: number; sessionId: string; position?: string; toolResults?: number };
|
|
253
393
|
|
|
254
|
-
|
|
255
|
-
const run = async (ctx: CheckContext, forced: boolean, trigger?: Trigger): Promise<CheckResult> => {
|
|
394
|
+
const run = async (ctx: CheckContext, forced: boolean, trigger?: Trigger): Promise<OverthinkNote> => {
|
|
256
395
|
if (!ctx.model) throw new Error("No active Pi model; Overthink cannot choose a different one.");
|
|
396
|
+
const model = ctx.model;
|
|
397
|
+
const name = model.provider + "/" + model.id;
|
|
257
398
|
const marker = trigger?.generation ?? generation;
|
|
258
399
|
const sessionId = trigger?.sessionId ?? ctx.sessionManager.getSessionId();
|
|
259
400
|
const position = trigger?.position ?? overthinkContextPosition(ctx.sessionManager.getBranch());
|
|
260
401
|
const stillCurrent = () => marker === generation &&
|
|
261
402
|
sessionId === ctx.sessionManager.getSessionId() &&
|
|
262
|
-
position === overthinkContextPosition(ctx.sessionManager.getBranch())
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
if (!questions.length) throw new Error("No Overthink questions. Edit session questions to enable checks.");
|
|
267
|
-
const preferences = await readPreferences();
|
|
268
|
-
if (!stillCurrent()) throw new Error("Session changed; assessment cancelled.");
|
|
269
|
-
|
|
270
|
-
const projected = ctx.sessionManager.buildSessionProjection().messages;
|
|
271
|
-
if (!projected.length && forced) throw new Error("Nothing in the current conversation to assess.");
|
|
272
|
-
if (!projected.length) throw new Error("No conversation to assess.");
|
|
403
|
+
position === overthinkContextPosition(ctx.sessionManager.getBranch()) &&
|
|
404
|
+
ctx.model?.provider === model.provider &&
|
|
405
|
+
ctx.model?.id === model.id;
|
|
406
|
+
if (!stillCurrent()) throw new Error("Session moved before check could start.");
|
|
273
407
|
|
|
274
408
|
active?.abort();
|
|
275
409
|
const controller = new AbortController();
|
|
276
410
|
active = controller;
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
const
|
|
280
|
-
|
|
281
|
-
|
|
411
|
+
checkingPosition = position;
|
|
412
|
+
let timedOut = false;
|
|
413
|
+
const timer = setTimeout(() => {
|
|
414
|
+
timedOut = true;
|
|
415
|
+
controller.abort();
|
|
416
|
+
}, timeoutMs);
|
|
417
|
+
|
|
282
418
|
try {
|
|
283
|
-
|
|
284
|
-
|
|
419
|
+
const preferences = await readPreferences();
|
|
420
|
+
if (timedOut) throw new Error("Check timed out.");
|
|
421
|
+
if (controller.signal.aborted || !stillCurrent() || active !== controller) {
|
|
422
|
+
throw new Error("Session changed; check cancelled.");
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
const projection = ctx.sessionManager.buildSessionProjection();
|
|
426
|
+
const { messages, sources, sourceIndex } = buildOverthinkReviewContext(projection);
|
|
427
|
+
if (!sources.length) {
|
|
428
|
+
throw new Error(forced ? "Nothing in the current conversation to check." : "No conversation to check.");
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
const effort = pi.getThinkingLevel();
|
|
432
|
+
const ask = {
|
|
433
|
+
role: "user" as const,
|
|
434
|
+
content: [{ type: "text" as const, text: overthinkPrompt({ ...promptOptions, preferences, sourceIndex }) }],
|
|
435
|
+
timestamp: Date.now(),
|
|
436
|
+
};
|
|
285
437
|
const answer = await ctx.modelRegistry.streamSimple(
|
|
286
438
|
model,
|
|
287
|
-
normalizeContext({ messages:
|
|
439
|
+
normalizeContext({ messages: [...messages, ask] }),
|
|
288
440
|
{
|
|
289
441
|
reasoning: model.reasoning && effort !== "off" ? effort : undefined,
|
|
290
|
-
maxTokens:
|
|
442
|
+
maxTokens: 8192,
|
|
291
443
|
sessionId: sessionId ?? undefined,
|
|
292
444
|
signal: controller.signal,
|
|
293
445
|
},
|
|
294
446
|
).result();
|
|
295
|
-
if (
|
|
296
|
-
if (
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
const
|
|
447
|
+
if (timedOut) throw new Error("Check timed out.");
|
|
448
|
+
if (controller.signal.aborted) throw new Error("Check cancelled.");
|
|
449
|
+
if (!stillCurrent() || active !== controller) {
|
|
450
|
+
throw new Error("Session or model changed; stale result discarded.");
|
|
451
|
+
}
|
|
452
|
+
if (answer.stopReason !== "stop") {
|
|
453
|
+
const detail = typeof answer.errorMessage === "string" && answer.errorMessage.trim()
|
|
454
|
+
? ": " + answer.errorMessage.trim() : "";
|
|
455
|
+
throw new Error("Model did not finish its check (" + answer.stopReason + detail + ").");
|
|
456
|
+
}
|
|
457
|
+
const rawText = answer.content.filter(part => part.type === "text").map(part => part.text).join("");
|
|
458
|
+
const finding = parseOverthinkFinding(rawText, sources, { allowLabel: promptOptions.includeLabels ?? true });
|
|
459
|
+
const text = finding ? formatOverthinkFinding(finding) : "";
|
|
306
460
|
const usage = readOverthinkUsage(answer.usage);
|
|
307
461
|
const note: OverthinkNote = {
|
|
308
|
-
text,
|
|
462
|
+
text,
|
|
463
|
+
checkedAt: new Date().toISOString(),
|
|
464
|
+
model: name,
|
|
465
|
+
status: finding ? "finding" : "none",
|
|
309
466
|
...(position ? { sourcePosition: position } : {}),
|
|
310
467
|
...(usage ? { usage } : {}),
|
|
311
|
-
...(
|
|
468
|
+
...(finding ? { finding } : {}),
|
|
469
|
+
...(trigger?.toolResults !== undefined ? { toolResults: trigger.toolResults } : {}),
|
|
312
470
|
};
|
|
313
471
|
saveOverthinkNote(pi, note);
|
|
314
|
-
noteWidget(ctx,
|
|
472
|
+
noteWidget(ctx, finding, position);
|
|
315
473
|
return note;
|
|
474
|
+
} catch (error) {
|
|
475
|
+
const ownedAndCurrent = (!controller.signal.aborted || timedOut) && stillCurrent() && active === controller;
|
|
476
|
+
if (ownedAndCurrent) {
|
|
477
|
+
saveOverthinkFailure(pi, {
|
|
478
|
+
checkedAt: new Date().toISOString(),
|
|
479
|
+
model: name,
|
|
480
|
+
error: timedOut ? "Check timed out." : errorMessage(error),
|
|
481
|
+
...(position ? { sourcePosition: position } : {}),
|
|
482
|
+
});
|
|
483
|
+
noteWidget(ctx);
|
|
484
|
+
}
|
|
485
|
+
throw timedOut ? new Error("Check timed out.") : error;
|
|
316
486
|
} finally {
|
|
317
487
|
clearTimeout(timer);
|
|
318
|
-
if (active === controller)
|
|
488
|
+
if (active === controller) {
|
|
489
|
+
active = undefined;
|
|
490
|
+
checkingPosition = undefined;
|
|
491
|
+
}
|
|
319
492
|
}
|
|
320
493
|
};
|
|
321
494
|
|
|
@@ -330,19 +503,23 @@ export function registerOverthink(pi: ExtensionAPI, loadAutomaticChecks = readAu
|
|
|
330
503
|
const completed = evidence;
|
|
331
504
|
evidence = undefined;
|
|
332
505
|
if (ctx.mode !== "tui" || !ctx.model ||
|
|
333
|
-
!overthinkReady(completed, event.aborted, ctx.hasPendingMessages())) return;
|
|
506
|
+
!overthinkReady(completed, event.aborted, ctx.hasPendingMessages(), gate)) return;
|
|
334
507
|
// Capture BEFORE the asynchronous settings read. A new run, fork or
|
|
335
508
|
// compaction must not turn an older settlement into a request for new work.
|
|
336
|
-
const trigger = {
|
|
337
|
-
generation,
|
|
509
|
+
const trigger: Trigger = {
|
|
510
|
+
generation,
|
|
511
|
+
sessionId: ctx.sessionManager.getSessionId(),
|
|
338
512
|
position: overthinkContextPosition(ctx.sessionManager.getBranch()),
|
|
513
|
+
toolResults: completed?.toolResults,
|
|
339
514
|
};
|
|
340
515
|
void (async () => {
|
|
341
|
-
|
|
516
|
+
const global = await loadGlobal();
|
|
342
517
|
if (trigger.generation !== generation ||
|
|
343
518
|
trigger.sessionId !== ctx.sessionManager.getSessionId() ||
|
|
344
519
|
trigger.position !== overthinkContextPosition(ctx.sessionManager.getBranch()) ||
|
|
345
520
|
ctx.hasPendingMessages()) return;
|
|
521
|
+
const session = resolveOverthinkSession(ctx.sessionManager.getBranch());
|
|
522
|
+
if (!shouldRunAutomaticOverthink(global, session)) return;
|
|
346
523
|
await run(ctx, false, trigger);
|
|
347
524
|
})().catch(() => undefined); // Optional review must never fail the main task.
|
|
348
525
|
});
|
|
@@ -352,25 +529,24 @@ export function registerOverthink(pi: ExtensionAPI, loadAutomaticChecks = readAu
|
|
|
352
529
|
ctx.ui.notify("Use /bro overthink in Pi's interactive UI.", "warning");
|
|
353
530
|
return;
|
|
354
531
|
}
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
});
|
|
373
|
-
}
|
|
532
|
+
let globalAutomatic = false;
|
|
533
|
+
try { globalAutomatic = (await loadGlobal()).automatic; }
|
|
534
|
+
catch { globalAutomatic = false; }
|
|
535
|
+
await openBroOverlay<void>(ctx, createOverthinkViewer(
|
|
536
|
+
() => {
|
|
537
|
+
const branch = ctx.sessionManager.getBranch();
|
|
538
|
+
const position = overthinkContextPosition(branch);
|
|
539
|
+
return {
|
|
540
|
+
session: resolveOverthinkSession(branch),
|
|
541
|
+
position,
|
|
542
|
+
checking: Boolean(active && checkingPosition === position),
|
|
543
|
+
globalAutomatic,
|
|
544
|
+
};
|
|
545
|
+
},
|
|
546
|
+
() => run(ctx, true),
|
|
547
|
+
() => allowBranchAutomaticChecks(pi),
|
|
548
|
+
));
|
|
374
549
|
};
|
|
375
|
-
|
|
550
|
+
|
|
551
|
+
return { open, recheck: (ctx: CheckContext) => run(ctx, true) };
|
|
376
552
|
}
|