pi-odd-worktree 2.0.0 → 3.0.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/README.md +54 -311
- package/extensions/odd-worktree-tools.ts +4149 -2447
- package/lib/bash-activity.js +1164 -0
- package/lib/bash-design.js +684 -0
- package/lib/bash-destructive.js +559 -0
- package/lib/bash-frame.js +205 -0
- package/lib/card-content.js +23 -0
- package/lib/card-style.js +175 -0
- package/lib/checkout-facts.js +30 -70
- package/lib/codegraph-card-preview.js +628 -0
- package/lib/codegraph-card.js +515 -0
- package/lib/codegraph-compaction.js +76 -0
- package/lib/codegraph-daemon.js +246 -0
- package/lib/codegraph-edit-sync.js +182 -0
- package/lib/codegraph-guard-target.js +337 -0
- package/lib/codegraph-guard.js +211 -0
- package/lib/codegraph-index.js +1020 -0
- package/lib/codegraph-lock.js +607 -0
- package/lib/codegraph-mcp-config.js +134 -0
- package/lib/codegraph-mcp.js +123 -0
- package/lib/codegraph-paths.js +69 -0
- package/lib/codegraph-prompt/explore.md +11 -0
- package/lib/codegraph-prompt/fix.md +6 -0
- package/lib/codegraph-prompt/generic.md +8 -0
- package/lib/codegraph-prompt/judge.md +9 -0
- package/lib/codegraph-prompt/orchestrator.md +17 -0
- package/lib/codegraph-prompt/verify.md +10 -0
- package/lib/codegraph-prompt/worker.md +7 -0
- package/lib/codegraph-role.js +55 -0
- package/lib/codegraph-rules.js +146 -0
- package/lib/codegraph-session.js +303 -383
- package/lib/codegraph-warmup.js +139 -0
- package/lib/file-card-markdown.js +141 -0
- package/lib/file-card-resolver.js +245 -0
- package/lib/file-card.js +659 -0
- package/lib/files-index.js +387 -0
- package/lib/files-insert.js +81 -0
- package/lib/files-orders.js +326 -0
- package/lib/files-picker-view.js +1416 -0
- package/lib/files-preview.js +338 -0
- package/lib/files-scope-detail.js +171 -0
- package/lib/files-scope-view.js +507 -0
- package/lib/gh-issues.js +308 -14
- package/lib/issue-preview.js +26 -7
- package/lib/issues-bash-mutation.js +206 -0
- package/lib/issues-cache-store.js +261 -0
- package/lib/issues-section.js +710 -0
- package/lib/issues-view.js +649 -52
- package/lib/odd-prompt/odd-worktrees.md +21 -0
- package/lib/odd-worktrees-prompt.js +10 -28
- package/lib/related-issues-cache.js +210 -0
- package/lib/shell-card.js +825 -0
- package/lib/skill-odd-card.js +183 -0
- package/lib/tui-paint.js +4 -5
- package/lib/warp-create-panel.js +538 -250
- package/lib/warp-create-picker.js +56 -9
- package/lib/warp-delete-confirm.js +19 -28
- package/lib/warp-selector-view.js +160 -82
- package/lib/which-command.js +90 -0
- package/lib/worktree-card.js +267 -897
- package/lib/worktree-cards.js +383 -0
- package/lib/worktree-create.js +287 -0
- package/lib/worktree-git.js +3 -3
- package/lib/worktree-guard.js +548 -0
- package/lib/worktree-hooks.js +421 -711
- package/lib/worktree-list.js +120 -0
- package/lib/worktree-registry.js +162 -879
- package/lib/{worktree-close-herdr.js → worktree-remove.js} +9 -11
- package/lib/worktree-setup-runner.js +376 -0
- package/lib/worktree-setup.js +140 -0
- package/lib/worktree-warp-busy.js +462 -85
- package/lib/worktree-warp-sessions.js +1 -2
- package/package.json +2 -2
- package/tools/bash-activity.test.js +1419 -0
- package/tools/bash-design.test.js +1091 -0
- package/tools/bash-destructive.test.js +139 -0
- package/tools/card-content.test.js +44 -0
- package/tools/card-style.test.js +298 -0
- package/tools/codegraph-card-preview.test.js +254 -0
- package/tools/codegraph-card.test.js +1080 -0
- package/tools/codegraph-compaction.test.js +378 -0
- package/tools/codegraph-daemon.test.js +248 -0
- package/tools/codegraph-edit-sync.test.js +295 -0
- package/tools/codegraph-guard-target.test.js +174 -0
- package/tools/codegraph-guard.test.js +238 -0
- package/tools/codegraph-index.test.js +1948 -0
- package/tools/codegraph-lock.test.js +932 -0
- package/tools/codegraph-mcp-config.test.js +181 -0
- package/tools/codegraph-mcp.test.js +194 -0
- package/tools/codegraph-paths.test.js +44 -0
- package/tools/codegraph-role.test.js +30 -0
- package/tools/codegraph-rules.test.js +250 -0
- package/tools/codegraph-session.test.js +660 -391
- package/tools/codegraph-warmup.test.js +316 -0
- package/tools/commands.test.js +510 -31
- package/tools/file-card-markdown.test.js +188 -0
- package/tools/file-card.test.js +1289 -0
- package/tools/files-bridge.test.js +575 -0
- package/tools/files-command.test.js +610 -0
- package/tools/files-index.test.js +473 -0
- package/tools/files-insert.test.js +139 -0
- package/tools/files-orders.test.js +597 -0
- package/tools/files-picker-view.test.js +1871 -0
- package/tools/files-preview.test.js +452 -0
- package/tools/files-scope-detail.test.js +264 -0
- package/tools/files-scope-view.test.js +614 -0
- package/tools/fixtures/card-codegraph-outputs.js +110 -0
- package/tools/fixtures/codegraph-cli-stub.js +16 -0
- package/tools/fixtures/codegraph-init-process.js +28 -0
- package/tools/fixtures/codegraph-lock-worker.js +117 -0
- package/tools/fixtures/daemon-fake.js +98 -0
- package/tools/fixtures/daemon-mcp-server.js +39 -0
- package/tools/fixtures/daemon-warmup-worker.js +12 -0
- package/tools/fixtures/load-extension.js +136 -3
- package/tools/gh-issues-feedback.test.js +31 -7
- package/tools/gh-issues.test.js +391 -1
- package/tools/issue-preview.test.js +44 -7
- package/tools/issues-bash-mutation.test.js +101 -0
- package/tools/issues-cache-store.test.js +222 -0
- package/tools/issues-command.test.js +1512 -66
- package/tools/issues-open-guard.test.js +1096 -0
- package/tools/issues-section.test.js +4195 -0
- package/tools/issues-seed-query-regression.test.js +67 -0
- package/tools/issues-view.test.js +1489 -60
- package/tools/odd-worktrees-prompt.test.js +98 -65
- package/tools/package-surface.test.js +431 -36
- package/tools/related-issues-cache.test.js +324 -0
- package/tools/shell-card.test.js +834 -0
- package/tools/skill-odd-card.test.js +200 -0
- package/tools/t5-session-identity.test.js +44 -22
- package/tools/warp-busy-guard.test.js +2569 -308
- package/tools/warp-create-panel.test.js +499 -74
- package/tools/warp-create-picker.test.js +101 -5
- package/tools/warp-delete-confirm.test.js +49 -15
- package/tools/warp-empty-session.test.js +0 -1
- package/tools/warp-fallback-regression.test.js +533 -0
- package/tools/warp-selector-view.test.js +460 -196
- package/tools/which-command.test.js +126 -0
- package/tools/worktree-card.test.js +551 -0
- package/tools/worktree-cards.test.js +845 -0
- package/tools/worktree-changes.test.js +4 -5
- package/tools/worktree-create.test.js +489 -0
- package/tools/worktree-guard.test.js +823 -0
- package/tools/worktree-hooks.test.js +905 -0
- package/tools/worktree-list.test.js +170 -0
- package/tools/worktree-registry.test.js +89 -230
- package/tools/worktree-remove-async.test.js +318 -0
- package/tools/worktree-setup-runner.test.js +276 -0
- package/tools/worktree-setup.test.js +140 -0
- package/tools/worktree-tools.test.js +477 -93
- package/lib/codegraph-commands.js +0 -165
- package/lib/odd-prompt/odd-worktrees.with-codegraph.md +0 -19
- package/lib/odd-prompt/odd-worktrees.without-codegraph.md +0 -15
- package/lib/worktree-model-result.js +0 -291
- package/tools/codegraph-commands.test.js +0 -371
- package/tools/herdr-free-create.test.js +0 -161
- package/tools/worktree-close-herdr-async.test.js +0 -204
- package/tools/worktree-odd-migration.test.js +0 -224
- package/tools/worktree-registry-cache.test.js +0 -163
- package/tools/worktree-registry-odd.test.js +0 -231
|
@@ -0,0 +1,515 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// S10: the CodeGraph card — one identity for the three shapes a session reaches
|
|
3
|
+
// CodeGraph through: the native `codegraph` tool, the MCP
|
|
4
|
+
// `mcp__codegraph__codegraph_explore` tool, and a `bash` call whose command runs
|
|
5
|
+
// the same program (`codegraph init`, `sync`, `query …`, `explore …`).
|
|
6
|
+
//
|
|
7
|
+
// The design is the user's own PR #74 (`jonasotoaguilar/gentle-shell`, branch
|
|
8
|
+
// `cards-bash-v2`, copied read-only under `odd/reference/pr74-card/`): the
|
|
9
|
+
// CodeGraph mark and its title role, the operation (with the query it named) as
|
|
10
|
+
// the heading detail, one collapsed row that states only what the program printed,
|
|
11
|
+
// and the whole CLI output once the reader expands the card. What this module adds
|
|
12
|
+
// is the surface (the MCP explore tool) and the drawing: the card is composed here
|
|
13
|
+
// and painted by this package's own substrate (`lib/shell-card.js`), in the shell's
|
|
14
|
+
// two styles, from the shell's theme roles.
|
|
15
|
+
//
|
|
16
|
+
// What the card never decides:
|
|
17
|
+
// * COLOUR. Every cell is a theme role (`titleRole`, `border`, `warning`, `error`,
|
|
18
|
+
// `muted`, `toolOutput`, `text`) handed to the injected `theme.fg`; no hex
|
|
19
|
+
// value, no palette and no raw escape sequence is written down here.
|
|
20
|
+
// * MEASUREMENT. The substrate wraps, clips and pads the rows as PLAIN text and
|
|
21
|
+
// paints each one afterwards, so nothing painted is ever measured twice.
|
|
22
|
+
// * STATE. The tone follows the host's own flags (`isPartial`, `isError`), the
|
|
23
|
+
// expanded view is the host's own `expanded`, and the elapsed time is the
|
|
24
|
+
// host's own `durationMs`.
|
|
25
|
+
// * THE TOOL. Nothing is registered, executed or shadowed: the resolver only
|
|
26
|
+
// chooses renderers. `createCodegraphCardResolver` registers through the
|
|
27
|
+
// host's `registerToolRenderer` hook and returns EVERY tool it does not own
|
|
28
|
+
// exactly as the rest of the chain resolved it, so no load order and no other
|
|
29
|
+
// extension's drawing depends on this package (S1).
|
|
30
|
+
// * THE CONTENT LEVEL. It is read at paint time from the injected reader, whose
|
|
31
|
+
// default is the process-wide slot gentle-pi publishes
|
|
32
|
+
// (`Symbol.for("gentle-pi.card-content")`). A level switched while the
|
|
33
|
+
// transcript is on screen therefore repaints the card already on screen, with
|
|
34
|
+
// no rebuild.
|
|
35
|
+
//
|
|
36
|
+
// Rendering never throws: a theme, a host renderer or an argument that fails draws
|
|
37
|
+
// no rows instead of breaking the session's transcript.
|
|
38
|
+
|
|
39
|
+
const {
|
|
40
|
+
CARD_TONE,
|
|
41
|
+
cardAwaitingResult,
|
|
42
|
+
cardBodyRows,
|
|
43
|
+
cardBottom,
|
|
44
|
+
cardElapsed,
|
|
45
|
+
cardRunningLine,
|
|
46
|
+
cardTopLine,
|
|
47
|
+
cardTopRows,
|
|
48
|
+
floatRows,
|
|
49
|
+
markCardResult,
|
|
50
|
+
} = require('./shell-card.js');
|
|
51
|
+
// The conversation card content level is one reader for every card of this package
|
|
52
|
+
// (lib/card-content.js): the process-wide slot gentle-pi publishes, read at paint
|
|
53
|
+
// time. It is re-exported below so the callers of this module keep their imports.
|
|
54
|
+
const { CARD_CONTENT, CARD_CONTENT_SLOT, cardContentLevel } = require('./card-content.js');
|
|
55
|
+
const {
|
|
56
|
+
CODEGRAPH_MARK,
|
|
57
|
+
CODEGRAPH_MCP_TOOL,
|
|
58
|
+
CODEGRAPH_TITLE_ROLE,
|
|
59
|
+
CODEGRAPH_TOOL,
|
|
60
|
+
cardText,
|
|
61
|
+
codeGraphCollapsedRows,
|
|
62
|
+
codeGraphInitRows,
|
|
63
|
+
codeGraphPreview,
|
|
64
|
+
codeGraphWholeOutput,
|
|
65
|
+
codegraphCallDetail,
|
|
66
|
+
codegraphCallFromCommand,
|
|
67
|
+
projectPathSegment,
|
|
68
|
+
} = require('./codegraph-card-preview.js');
|
|
69
|
+
|
|
70
|
+
// The MCP tool arrives as its bare name or as the prefixed form the host reports.
|
|
71
|
+
// Both are the same program, so both wear the same card; no other prefixed name is
|
|
72
|
+
// claimed, because a server this package does not know is not this card's (S1).
|
|
73
|
+
const CODEGRAPH_MCP_PREFIXED_TOOL = 'mcp__codegraph__codegraph_explore';
|
|
74
|
+
// The shell tool a CodeGraph command can be run through. `bash` is decided per CALL,
|
|
75
|
+
// never per tool: a command that is not CodeGraph keeps the host's own card.
|
|
76
|
+
const BASH_TOOL = 'bash';
|
|
77
|
+
const SUBTITLE_ROLE = 'muted';
|
|
78
|
+
// The role a card body keeps when the reader expanded it: the shell's own tool
|
|
79
|
+
// output slot, so the program's text reads as output and never as chrome.
|
|
80
|
+
const OUTPUT_ROLE = 'toolOutput';
|
|
81
|
+
const ERROR_ROLE = 'error';
|
|
82
|
+
|
|
83
|
+
// The host keybinding the expand affordance is bound to, and the two verbs the
|
|
84
|
+
// heading offers for it.
|
|
85
|
+
const EXPAND_KEYBINDING = 'app.tools.expand';
|
|
86
|
+
const EXPAND_TEXT = { collapsed: 'to expand', expanded: 'to collapse' };
|
|
87
|
+
|
|
88
|
+
/** The conversation card content level gentle-pi keeps for the whole process. */
|
|
89
|
+
// `CARD_CONTENT`, `CARD_CONTENT_SLOT` and `cardContentLevel` are the shared reader's
|
|
90
|
+
// (lib/card-content.js): the level is read on every paint, so a level switched while
|
|
91
|
+
// the transcript is on screen repaints the card already there.
|
|
92
|
+
|
|
93
|
+
const METRICS_FUNCTIONS = ['visibleWidth', 'truncateToWidth', 'wrapTextWithAnsi'];
|
|
94
|
+
|
|
95
|
+
const asObject = (value) => (value !== null && typeof value === 'object' ? value : {});
|
|
96
|
+
const hasFunction = (value, name) => value !== null && typeof value === 'object' && typeof value[name] === 'function';
|
|
97
|
+
|
|
98
|
+
/** A component that draws nothing: the answer of every path that cannot draw. */
|
|
99
|
+
const EMPTY_COMPONENT = { render: () => [], invalidate() {} };
|
|
100
|
+
/** No injected theme, and a host that passed none: text is drawn plain rather than not at all. */
|
|
101
|
+
const PLAIN_THEME = { fg: (_role, text) => text };
|
|
102
|
+
|
|
103
|
+
function assertMetrics(value) {
|
|
104
|
+
if (!hasFunction(value, METRICS_FUNCTIONS[0])) {
|
|
105
|
+
throw new TypeError('createCodegraphCardResolver requires an injected metrics object ({visibleWidth, truncateToWidth, wrapTextWithAnsi})');
|
|
106
|
+
}
|
|
107
|
+
for (const name of METRICS_FUNCTIONS) {
|
|
108
|
+
if (!hasFunction(value, name)) throw new TypeError(`createCodegraphCardResolver requires metrics.${name} to be a function`);
|
|
109
|
+
}
|
|
110
|
+
return value;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* The measurement seam a caller injected, validated once at construction: the
|
|
115
|
+
* object itself, or a function the factory calls on every paint (the host's TUI
|
|
116
|
+
* package is resolved inside the hook, never at load).
|
|
117
|
+
*/
|
|
118
|
+
function metricsSeam(value) {
|
|
119
|
+
if (typeof value === 'function') return value;
|
|
120
|
+
assertMetrics(value);
|
|
121
|
+
return () => value;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** The cwd seam: a string or a function asked on every paint, and no answer is undefined. */
|
|
125
|
+
function cwdSeam(value) {
|
|
126
|
+
return () => {
|
|
127
|
+
try {
|
|
128
|
+
const cwd = typeof value === 'function' ? value() : value;
|
|
129
|
+
return typeof cwd === 'string' && cwd.length > 0 ? cwd : undefined;
|
|
130
|
+
} catch {
|
|
131
|
+
return undefined;
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Whether a value is a usable text-measurement seam. */
|
|
137
|
+
function usableMetrics(value) {
|
|
138
|
+
return METRICS_FUNCTIONS.every((name) => hasFunction(value, name)) ? value : undefined;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// ---- the content level ----
|
|
142
|
+
|
|
143
|
+
// `cardContentLevel(host)` is re-exported from lib/card-content.js: the level in
|
|
144
|
+
// force is `minimal` (a card drawn with no result rows until it is expanded) or
|
|
145
|
+
// `default` (the bounded preview), and the host without gentle-shell leaves it
|
|
146
|
+
// unset, which is the safe `default`.
|
|
147
|
+
|
|
148
|
+
// ---- which call is a CodeGraph call ----
|
|
149
|
+
|
|
150
|
+
function stringArg(args, key) {
|
|
151
|
+
const value = asObject(args)[key];
|
|
152
|
+
return typeof value === 'string' && value.length > 0 ? cardText(value) : undefined;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** The command text with its leading program word removed, so a heading that names the program does not say it twice. */
|
|
156
|
+
function withoutLeadingProgram(text, program) {
|
|
157
|
+
if (text === program) return '';
|
|
158
|
+
const head = `${program} `;
|
|
159
|
+
return text.startsWith(head) ? text.slice(head.length).trimStart() : text;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* The CodeGraph call a tool call makes, or undefined when it makes none: an
|
|
164
|
+
* unrelated tool, or a `bash` command that runs something else. That undefined is
|
|
165
|
+
* what keeps every ordinary shell card byte-identical to the one the host drew.
|
|
166
|
+
*/
|
|
167
|
+
function codegraphShape(toolName, args, cwd) {
|
|
168
|
+
if (typeof toolName !== 'string') return undefined;
|
|
169
|
+
if (toolName === CODEGRAPH_TOOL) {
|
|
170
|
+
const operation = stringArg(args, 'operation');
|
|
171
|
+
const query = stringArg(args, 'query');
|
|
172
|
+
// Until the call's arguments stream in the operation is not known: the card
|
|
173
|
+
// says so rather than guessing one.
|
|
174
|
+
return { surface: 'tool', operation: operation === undefined ? '...' : operation, ...(query === undefined ? {} : { query }) };
|
|
175
|
+
}
|
|
176
|
+
if (toolName === CODEGRAPH_MCP_TOOL || toolName === CODEGRAPH_MCP_PREFIXED_TOOL) {
|
|
177
|
+
const query = stringArg(args, 'query');
|
|
178
|
+
// The explored checkout is named only when it is not this session's own (S6); a
|
|
179
|
+
// cwd the seam cannot answer still shows the caller's explicit argument.
|
|
180
|
+
const project = projectPathSegment(asObject(args).projectPath, cwd);
|
|
181
|
+
return { surface: 'mcp', operation: 'explore', ...(query === undefined ? {} : { query }), ...(project === undefined ? {} : { project }) };
|
|
182
|
+
}
|
|
183
|
+
if (toolName !== BASH_TOOL) return undefined;
|
|
184
|
+
const command = asObject(args).command;
|
|
185
|
+
const call = codegraphCallFromCommand(command);
|
|
186
|
+
// The command itself is the card's detail, so a bash call that runs the program is
|
|
187
|
+
// this card's whatever the operation it names, and a command that only mentions it
|
|
188
|
+
// stays the host's own row.
|
|
189
|
+
return call === undefined ? undefined : { surface: 'bash', command: cardText(command), ...call };
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** Whether this tool's drawing belongs to the card. `bash` is decided per call, not per tool. */
|
|
193
|
+
function isCodeGraphCardTool(toolName) {
|
|
194
|
+
return toolName === CODEGRAPH_TOOL || toolName === CODEGRAPH_MCP_TOOL || toolName === CODEGRAPH_MCP_PREFIXED_TOOL;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// ---- the rows ----
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* The heading both the tool rows and the card use: the CodeGraph mark and name, and
|
|
201
|
+
* its detail — the operation and the query the native and MCP tools named, or the
|
|
202
|
+
* command a `bash` call ran. The same heading on all three surfaces, so the tool the
|
|
203
|
+
* model reached and the shell it ran read as one card.
|
|
204
|
+
*
|
|
205
|
+
* The whole box wears the CodeGraph role too (`railRole`): the card's corners, rules
|
|
206
|
+
* and rail are painted in it, so the box reads as part of the label it belongs to.
|
|
207
|
+
* The substrate keeps the call's own status tone on the box while the call is
|
|
208
|
+
* running or has failed, so an identity colour never hides a pending call.
|
|
209
|
+
*/
|
|
210
|
+
function codegraphCallIdent(shape) {
|
|
211
|
+
const call = asObject(shape);
|
|
212
|
+
const operation = typeof call.operation === 'string' && call.operation.length > 0 ? call.operation : '...';
|
|
213
|
+
const query = typeof call.query === 'string' && call.query.length > 0 ? call.query : undefined;
|
|
214
|
+
const command = typeof call.command === 'string' ? call.command : '';
|
|
215
|
+
// A bash call names the input after the program word: the heading already says
|
|
216
|
+
// `codegraph`, so `codegraph status .` reads `· status .` (S5). Only a LEADING
|
|
217
|
+
// program word goes, exactly as the bash activity card's own detail does, so a
|
|
218
|
+
// launcher or a `cd X &&` chain keeps its text.
|
|
219
|
+
const detail = command.length > 0 ? withoutLeadingProgram(command, CODEGRAPH_TOOL) : '';
|
|
220
|
+
const project = typeof call.project === 'string' && call.project.length > 0 ? call.project : undefined;
|
|
221
|
+
const base = command.length > 0
|
|
222
|
+
? detail
|
|
223
|
+
: codegraphCallDetail({ operation, ...(query === undefined ? {} : { query }) });
|
|
224
|
+
const subtitle = project === undefined
|
|
225
|
+
? (base.length > 0 ? base : undefined)
|
|
226
|
+
: (base.length > 0 ? `${base} · ${project}` : project);
|
|
227
|
+
return {
|
|
228
|
+
glyph: CODEGRAPH_MARK,
|
|
229
|
+
title: CODEGRAPH_TOOL,
|
|
230
|
+
subtitle,
|
|
231
|
+
titleRole: CODEGRAPH_TITLE_ROLE,
|
|
232
|
+
railRole: CODEGRAPH_TITLE_ROLE,
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* The heading rows: the identity, then the call's detail. Plain text plus the theme
|
|
238
|
+
* role that paints it — the card itself paints these same two values through the
|
|
239
|
+
* substrate's top rule, which is what `codegraphCallIdent` holds for both.
|
|
240
|
+
*/
|
|
241
|
+
function codegraphCallRows(shape) {
|
|
242
|
+
const ident = codegraphCallIdent(shape);
|
|
243
|
+
return [
|
|
244
|
+
{ text: `${ident.glyph} ${ident.title}`, role: CODEGRAPH_TITLE_ROLE },
|
|
245
|
+
{ text: ident.subtitle, role: SUBTITLE_ROLE },
|
|
246
|
+
];
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/** Everything the result rows are decided from. */
|
|
250
|
+
function codegraphResultRows(input) {
|
|
251
|
+
const value = asObject(input);
|
|
252
|
+
const shape = asObject(value.shape);
|
|
253
|
+
const operation = typeof shape.operation === 'string' && shape.operation.length > 0 ? shape.operation : '...';
|
|
254
|
+
const failed = value.failed === true;
|
|
255
|
+
const partial = value.isPartial === true;
|
|
256
|
+
const expanded = value.expanded === true;
|
|
257
|
+
const withheld = value.content === CARD_CONTENT.MINIMAL;
|
|
258
|
+
const output = cardText(value.text);
|
|
259
|
+
// Expanding is the reader's own ask, so it shows everything at either level —
|
|
260
|
+
// except an init, whose whole CLI log is replaced by its outcome and index facts
|
|
261
|
+
// (S4). A failed init keeps its own error and nothing else.
|
|
262
|
+
if (expanded) {
|
|
263
|
+
if (!failed && operation === 'init') {
|
|
264
|
+
const rows = codeGraphInitRows(output);
|
|
265
|
+
return rows.length > 0 ? [{ text: rows.join('\n'), role: failed ? ERROR_ROLE : OUTPUT_ROLE }] : [];
|
|
266
|
+
}
|
|
267
|
+
const whole = codeGraphWholeOutput(value.text);
|
|
268
|
+
return whole.length > 0 ? [{ text: whole, role: failed ? ERROR_ROLE : OUTPUT_ROLE }] : [];
|
|
269
|
+
}
|
|
270
|
+
// A minimized card draws the call alone; only a failure still says why — and
|
|
271
|
+
// an init keeps its one outcome row at either level (S4), because that row is
|
|
272
|
+
// what the call DID rather than the body of its output.
|
|
273
|
+
if (withheld && !failed && !(operation === 'init' && !partial)) return [];
|
|
274
|
+
if (partial) {
|
|
275
|
+
const preview = codeGraphPreview(output);
|
|
276
|
+
return preview.length > 0 ? [{ text: preview, role: failed ? ERROR_ROLE : SUBTITLE_ROLE }] : [];
|
|
277
|
+
}
|
|
278
|
+
const rows = codeGraphCollapsedRows(operation, output, failed, withheld);
|
|
279
|
+
return rows === undefined ? [] : [{ text: rows.text, role: failed ? ERROR_ROLE : SUBTITLE_ROLE }];
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// ---- the drawing ----
|
|
283
|
+
|
|
284
|
+
/** The theme a render is painted with: the host's own when it has one, else the injected fallback. */
|
|
285
|
+
function themeFor(theme, fallback) {
|
|
286
|
+
if (hasFunction(theme, 'fg')) return theme;
|
|
287
|
+
if (hasFunction(fallback, 'fg')) return fallback;
|
|
288
|
+
return PLAIN_THEME;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/** The text of a tool result: the host's `content` parts, or the `{text}` a forwarded result carries. */
|
|
292
|
+
function resultText(result) {
|
|
293
|
+
const value = asObject(result);
|
|
294
|
+
if (typeof value.text === 'string') return value.text;
|
|
295
|
+
const parts = Array.isArray(value.content) ? value.content : [];
|
|
296
|
+
return parts
|
|
297
|
+
.map((part) => (typeof asObject(part).text === 'string' ? asObject(part).text : ''))
|
|
298
|
+
.filter((text) => text.length > 0)
|
|
299
|
+
.join('\n');
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
function optionFlag(options, context, key) {
|
|
303
|
+
return asObject(options)[key] === true || asObject(context)[key] === true;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/** The right-aligned dim hint the heading offers: the host's own keybinding text for expanding (or collapsing) the card. */
|
|
307
|
+
function expandHint(context, expanded) {
|
|
308
|
+
const keyHint = asObject(context).keyHint;
|
|
309
|
+
if (typeof keyHint !== 'function') return undefined;
|
|
310
|
+
try {
|
|
311
|
+
const text = keyHint(EXPAND_KEYBINDING, expanded ? EXPAND_TEXT.expanded : EXPAND_TEXT.collapsed);
|
|
312
|
+
return typeof text === 'string' && text.length > 0 ? text : undefined;
|
|
313
|
+
} catch {
|
|
314
|
+
return undefined;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* A component whose rows are built again on every paint, so the host's own level,
|
|
320
|
+
* style and width all apply to the card already on screen. Rendering never throws: a
|
|
321
|
+
* failure draws no rows instead of breaking the transcript.
|
|
322
|
+
*/
|
|
323
|
+
function component(build) {
|
|
324
|
+
return {
|
|
325
|
+
render(width) {
|
|
326
|
+
try {
|
|
327
|
+
return build(Math.max(0, Math.floor(Number.isFinite(width) ? width : 0)));
|
|
328
|
+
} catch {
|
|
329
|
+
return [];
|
|
330
|
+
}
|
|
331
|
+
},
|
|
332
|
+
invalidate() {
|
|
333
|
+
// Nothing is cached: there is no measurement and no row to invalidate.
|
|
334
|
+
},
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/** One slot of the host's own drawing, called exactly as the host would have called it. */
|
|
339
|
+
function forwardedComponent(inherited, slot, args) {
|
|
340
|
+
try {
|
|
341
|
+
const renderer = asObject(inherited)[slot];
|
|
342
|
+
if (typeof renderer !== 'function') return EMPTY_COMPONENT;
|
|
343
|
+
const drawn = renderer(...args);
|
|
344
|
+
return hasFunction(drawn, 'render') ? drawn : EMPTY_COMPONENT;
|
|
345
|
+
} catch {
|
|
346
|
+
return EMPTY_COMPONENT;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/** The renderers the rest of the resolver chain would use for this row, resolved once. */
|
|
351
|
+
function inheritedRenderers(next) {
|
|
352
|
+
try {
|
|
353
|
+
return typeof next === 'function' ? next() : undefined;
|
|
354
|
+
} catch {
|
|
355
|
+
return undefined;
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/** The call slot: the CodeGraph heading, or the host's own drawing when this call is not CodeGraph's. */
|
|
360
|
+
function callSlot(toolName, inherited, seams) {
|
|
361
|
+
return (args, theme, context) => {
|
|
362
|
+
try {
|
|
363
|
+
const ctx = asObject(context);
|
|
364
|
+
const shape = codegraphShape(toolName, args === undefined ? ctx.args : args, seams.cwd());
|
|
365
|
+
if (shape === undefined) return forwardedComponent(inherited, 'renderCall', [args, theme, context]);
|
|
366
|
+
const metrics = seams.metrics();
|
|
367
|
+
if (metrics === undefined) return forwardedComponent(inherited, 'renderCall', [args, theme, context]);
|
|
368
|
+
const paint = themeFor(theme, seams.theme);
|
|
369
|
+
const pending = ctx.isPartial !== false;
|
|
370
|
+
const failed = ctx.isError === true;
|
|
371
|
+
const tone = failed ? CARD_TONE.ERROR : pending ? CARD_TONE.WARNING : CARD_TONE.INFO;
|
|
372
|
+
const expanded = ctx.expanded === true;
|
|
373
|
+
const hint = pending || failed ? undefined : expandHint(ctx, expanded);
|
|
374
|
+
const card = { ...codegraphCallIdent(shape), tone };
|
|
375
|
+
return component((width) => floatRows(tone, paint, width, (inner) => {
|
|
376
|
+
const head = expanded
|
|
377
|
+
? cardTopRows(card, paint, inner, hint, metrics)
|
|
378
|
+
: [cardTopLine(card, paint, inner, hint, undefined, metrics)];
|
|
379
|
+
const body = pending && !failed ? [cardRunningLine(tone, paint, inner, metrics)] : [];
|
|
380
|
+
// The result that follows owns the closing rule; a call still waiting for it
|
|
381
|
+
// closes its own frame, so a running row never floats outside one.
|
|
382
|
+
const bottom = cardAwaitingResult(ctx) ? cardBottom(tone, paint, inner, undefined, metrics) : undefined;
|
|
383
|
+
return { head, body, bottom };
|
|
384
|
+
}, card.railRole));
|
|
385
|
+
} catch {
|
|
386
|
+
return EMPTY_COMPONENT;
|
|
387
|
+
}
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/** The result slot: the rows CodeGraph's own output supports, or the host's own drawing for any other call. */
|
|
392
|
+
function resultSlot(toolName, inherited, seams) {
|
|
393
|
+
return (result, options, theme, context) => {
|
|
394
|
+
try {
|
|
395
|
+
const ctx = asObject(context);
|
|
396
|
+
const shape = codegraphShape(toolName, ctx.args, seams.cwd());
|
|
397
|
+
if (shape === undefined) return forwardedComponent(inherited, 'renderResult', [result, options, theme, context]);
|
|
398
|
+
const metrics = seams.metrics();
|
|
399
|
+
if (metrics === undefined) return forwardedComponent(inherited, 'renderResult', [result, options, theme, context]);
|
|
400
|
+
const paint = themeFor(theme, seams.theme);
|
|
401
|
+
const failed = asObject(result).isError === true || optionFlag(options, ctx, 'isError');
|
|
402
|
+
const partial = optionFlag(options, ctx, 'isPartial');
|
|
403
|
+
const expanded = optionFlag(options, ctx, 'expanded');
|
|
404
|
+
const tone = failed ? CARD_TONE.ERROR : partial ? CARD_TONE.WARNING : CARD_TONE.INFO;
|
|
405
|
+
const text = resultText(result);
|
|
406
|
+
// One frame: from here on the call above is a heading and this card closes it.
|
|
407
|
+
markCardResult(ctx.state);
|
|
408
|
+
return component((width) => floatRows(tone, paint, width, (inner) => {
|
|
409
|
+
// The level is asked for on every paint, so a preference switched on screen
|
|
410
|
+
// repaints the rows it decides.
|
|
411
|
+
const rows = codegraphResultRows({ shape, text, expanded, isPartial: partial, failed, content: seams.content() });
|
|
412
|
+
const role = rows.length > 0 ? rows[0].role : undefined;
|
|
413
|
+
const body = role === undefined
|
|
414
|
+
? []
|
|
415
|
+
: cardBodyRows(rows.map((row) => row.text), tone, paint, inner, { expanded: true, role }, metrics);
|
|
416
|
+
// A streamed result is not finished: the closing rule (and the time on it)
|
|
417
|
+
// belongs to the final one, which the host renders when the call settles.
|
|
418
|
+
const bottom = partial ? undefined : cardBottom(tone, paint, inner, cardElapsed(ctx.durationMs), metrics);
|
|
419
|
+
// The call above owns the heading, so the float style still sets this body
|
|
420
|
+
// off from it with its separator row. The result continues the heading's card,
|
|
421
|
+
// so its sides and closing rule keep the CodeGraph role the box above wears.
|
|
422
|
+
return { body, bottom, afterHeading: true };
|
|
423
|
+
}, CODEGRAPH_TITLE_ROLE));
|
|
424
|
+
} catch {
|
|
425
|
+
return EMPTY_COMPONENT;
|
|
426
|
+
}
|
|
427
|
+
};
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
/**
|
|
431
|
+
* S10: the CodeGraph card, for the native `codegraph` tool, the MCP explore tool and
|
|
432
|
+
* a `bash` call whose command runs the same program.
|
|
433
|
+
*
|
|
434
|
+
* `createCodegraphCardResolver({ metrics, registerToolRenderer, theme, cardContent })`
|
|
435
|
+
* takes every host seam as an option:
|
|
436
|
+
* * `metrics` (required) — the text-measurement seam, either the object itself or a
|
|
437
|
+
* function returning it, so a runtime lookup (the host's TUI package) is resolved
|
|
438
|
+
* per paint and never at load;
|
|
439
|
+
* * `registerToolRenderer` — the host hook this factory registers through. Absent,
|
|
440
|
+
* or a hook that throws, is no hook: the factory reports `registered: false` and
|
|
441
|
+
* nothing of ours is drawn, which is what an older host needs;
|
|
442
|
+
* * `theme` — the theme used when a render arrives without one;
|
|
443
|
+
* * `cardContent` — the level reader, defaulting to the process-wide slot above.
|
|
444
|
+
*
|
|
445
|
+
* It returns `{ registered, resolver }`; `resolver(toolName, next)` is the function
|
|
446
|
+
* the host hook receives, and it hands every tool it does not own back exactly as
|
|
447
|
+
* `next()` resolved it.
|
|
448
|
+
*/
|
|
449
|
+
function createCodegraphCardResolver(options) {
|
|
450
|
+
const opts = asObject(options);
|
|
451
|
+
const metrics = metricsSeam(opts.metrics);
|
|
452
|
+
const seams = {
|
|
453
|
+
theme: opts.theme,
|
|
454
|
+
cwd: cwdSeam(opts.cwd),
|
|
455
|
+
metrics() {
|
|
456
|
+
try {
|
|
457
|
+
return usableMetrics(metrics());
|
|
458
|
+
} catch {
|
|
459
|
+
return undefined;
|
|
460
|
+
}
|
|
461
|
+
},
|
|
462
|
+
content() {
|
|
463
|
+
try {
|
|
464
|
+
const reader = opts.cardContent;
|
|
465
|
+
if (typeof reader !== 'function') return cardContentLevel();
|
|
466
|
+
return reader() === CARD_CONTENT.MINIMAL ? CARD_CONTENT.MINIMAL : CARD_CONTENT.DEFAULT;
|
|
467
|
+
} catch {
|
|
468
|
+
return CARD_CONTENT.DEFAULT;
|
|
469
|
+
}
|
|
470
|
+
},
|
|
471
|
+
};
|
|
472
|
+
const resolver = (toolName, next) => {
|
|
473
|
+
const inherited = inheritedRenderers(next);
|
|
474
|
+
if (typeof toolName !== 'string') return inherited;
|
|
475
|
+
// Every tool the card does not own goes back untouched: those rows are the
|
|
476
|
+
// chain's, byte for byte, whatever this card would have drawn.
|
|
477
|
+
if (!isCodeGraphCardTool(toolName) && toolName !== BASH_TOOL) return inherited;
|
|
478
|
+
// This card frames itself, so the host must not frame it too: the host wraps every tool whose definition does not say
|
|
479
|
+
// `renderShell: 'self'` in its own filled box, and a card drawn inside that box is two designs at once.
|
|
480
|
+
if (toolName === BASH_TOOL) {
|
|
481
|
+
// A bash row inside the host's own box stays the host's, as does a host with no bash renderer at all: with no
|
|
482
|
+
// renderer to hand a non-CodeGraph command back to, every bash row would be drawn blank.
|
|
483
|
+
if (inherited === undefined || inherited.renderShell !== 'self') return inherited;
|
|
484
|
+
}
|
|
485
|
+
return {
|
|
486
|
+
renderShell: 'self',
|
|
487
|
+
renderCall: callSlot(toolName, inherited, seams),
|
|
488
|
+
renderResult: resultSlot(toolName, inherited, seams),
|
|
489
|
+
};
|
|
490
|
+
};
|
|
491
|
+
const register = opts.registerToolRenderer;
|
|
492
|
+
if (typeof register !== 'function') return { registered: false, resolver };
|
|
493
|
+
try {
|
|
494
|
+
register(resolver);
|
|
495
|
+
return { registered: true, resolver };
|
|
496
|
+
} catch {
|
|
497
|
+
return { registered: false, resolver };
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
module.exports = {
|
|
502
|
+
CARD_CONTENT,
|
|
503
|
+
CARD_CONTENT_SLOT,
|
|
504
|
+
CODEGRAPH_MARK,
|
|
505
|
+
CODEGRAPH_MCP_PREFIXED_TOOL,
|
|
506
|
+
CODEGRAPH_MCP_TOOL,
|
|
507
|
+
CODEGRAPH_TITLE_ROLE,
|
|
508
|
+
CODEGRAPH_TOOL,
|
|
509
|
+
cardContentLevel,
|
|
510
|
+
codegraphCallRows,
|
|
511
|
+
codegraphResultRows,
|
|
512
|
+
codegraphShape,
|
|
513
|
+
createCodegraphCardResolver,
|
|
514
|
+
isCodeGraphCardTool,
|
|
515
|
+
};
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// The sync one compaction asks for (S2, S3): after a compaction the index is as stale as the session was long, so
|
|
3
|
+
// this requests ONE background sync of the checkout. It only ever syncs an index that already exists — never
|
|
4
|
+
// initialises a project — never notifies the session, and never throws: its outcome goes to the caller's log.
|
|
5
|
+
//
|
|
6
|
+
// It is the same coordinated request the session start and the post-init hook use (`lib/codegraph-index.js`), so the
|
|
7
|
+
// sync runs under the per-project lease and the package's index lock, and a request that finds an init running is
|
|
8
|
+
// kept and served once the init ends, not dropped. A subagent child, a read-only role and a machine without the
|
|
9
|
+
// `codegraph` CLI start nothing at all.
|
|
10
|
+
|
|
11
|
+
const nodeFs = require('node:fs');
|
|
12
|
+
|
|
13
|
+
const { realInitLock, realSyncLease } = require('./codegraph-lock.js');
|
|
14
|
+
const {
|
|
15
|
+
CHILD_ENV,
|
|
16
|
+
cliAvailable,
|
|
17
|
+
planIndexAction,
|
|
18
|
+
requestCoordinatedSync,
|
|
19
|
+
runCodegraph,
|
|
20
|
+
} = require('./codegraph-index.js');
|
|
21
|
+
const { mayInitiateIndexWrite, sessionIndexRole } = require('./codegraph-role.js');
|
|
22
|
+
|
|
23
|
+
/** Whether this session may write the index at all: a caller-supplied `mayWrite`/`role` wins, else the child env. */
|
|
24
|
+
function mayWriteHere(input) {
|
|
25
|
+
if (typeof input.mayWrite === 'boolean') return input.mayWrite;
|
|
26
|
+
if (input.role !== undefined) return mayInitiateIndexWrite(input.role);
|
|
27
|
+
return mayInitiateIndexWrite(sessionIndexRole(input.env || process.env));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async function decide(input) {
|
|
31
|
+
const env = input.env || {};
|
|
32
|
+
if (env[CHILD_ENV] === '1') return 'skipped-child';
|
|
33
|
+
if (!mayWriteHere(input)) return 'skipped-read-only';
|
|
34
|
+
if (!cliAvailable({ hasBinary: input.hasBinary })) return 'skipped-no-cli';
|
|
35
|
+
const exists = typeof input.exists === 'function' ? input.exists : (p) => nodeFs.existsSync(p);
|
|
36
|
+
if (planIndexAction(input.cwd, exists) !== 'sync') return 'skipped-no-index';
|
|
37
|
+
// The same coordinated request the startup and post-init syncs use: one lease, the index lock for the run.
|
|
38
|
+
const outcome = await requestCoordinatedSync({
|
|
39
|
+
project: input.cwd,
|
|
40
|
+
sync: input.sync === undefined ? realSyncLease : input.sync || undefined,
|
|
41
|
+
lock: input.lock === undefined ? realInitLock : input.lock || undefined,
|
|
42
|
+
run: input.run === undefined ? runCodegraph : input.run,
|
|
43
|
+
});
|
|
44
|
+
if (outcome === 'synced') return 'synced';
|
|
45
|
+
if (outcome === 'coalesced') return 'coalesced';
|
|
46
|
+
if (outcome === 'pending') return 'queued';
|
|
47
|
+
if (outcome === 'unavailable') return 'skipped-coordination-unavailable';
|
|
48
|
+
return 'failed';
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Requests one coordinated background sync after a compaction and returns what it achieved: `synced` only for a run
|
|
53
|
+
* that came back here, `coalesced` only for another session's run that started after this request and completed,
|
|
54
|
+
* `queued` for a request that is recorded and still waiting for the index to be free, and the `skipped-*` outcomes
|
|
55
|
+
* for a session that may not write. Never throws; a diagnostic line goes to the caller's `log` seam alone.
|
|
56
|
+
*/
|
|
57
|
+
async function compactionSync(input = {}) {
|
|
58
|
+
const now = typeof input.now === 'function' ? input.now : Date.now;
|
|
59
|
+
const started = now();
|
|
60
|
+
let outcome;
|
|
61
|
+
try {
|
|
62
|
+
outcome = await decide(input);
|
|
63
|
+
} catch {
|
|
64
|
+
outcome = 'failed';
|
|
65
|
+
}
|
|
66
|
+
try {
|
|
67
|
+
if (typeof input.log === 'function') input.log({ cwd: input.cwd, outcome, ms: Math.max(0, now() - started) });
|
|
68
|
+
} catch {
|
|
69
|
+
// A log is never worth failing the session over.
|
|
70
|
+
}
|
|
71
|
+
return { outcome };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
module.exports = {
|
|
75
|
+
compactionSync,
|
|
76
|
+
};
|