pi-odd-worktree 3.1.0 → 3.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.
package/README.md CHANGED
@@ -71,10 +71,11 @@ The full rules of each step, including every refusal, are in [Create, list, move
71
71
  - **Cards follow your shell.** Tool and warp cards use the card style Gentle Shell publishes, and a click expands that card only. See [card style](docs/reference.md#card-style-follows-your-shell).
72
72
  - **Tool headings can name the activity.** With `activity` chosen on the `Card tags` line, a bash card whose heading shows the whole command is headed with the family its command names (mark and word) instead of `$`, and a `grep`, `find` or `ls` card is headed `⌕ exploring` instead of its own glyph and name, while a `read` — which runs no shell command — keeps the host's own card, colour and width; a bash command the classifier cannot name, or a heading that hides part of it, keeps the host's `$`. See [the cards the `activity` design paints](docs/reference.md#the-cards-the-activity-design-paints).
73
73
  - **File calls on a skill or an ODD are cards too.** While `Card tags` answers with `activity` for the tool being painted, a `read`, `edit` or `write` call whose path names a skill (anything below a `skills` folder) or an ODD (anything below an `odd` folder) heads as that file, with the tool, the path and a read's line range; its Markdown body — the whole document when expanded, the first rows when collapsed — is drawn by Pi's own renderer in the theme's Markdown roles, and an edit always keeps its diff. Any other file call, and every call while that line answers anything else, is the host's own row. See [the file cards](docs/reference.md#the-file-cards).
74
+ - **`edit` and `write` cannot land in the principal checkout's default branch.** A write whose path is inside the principal checkout of the session's repository, while that checkout is on the default branch, is refused with one line pointing at `worktree_list` and `worktree_create`. Paths outside the repository, anything under the OS temp directories, other repositories and linked worktrees are never blocked. See [the guard](docs/reference.md#the-edit-and-write-guard).
74
75
  - **`bash` cannot leave the principal checkout's default branch.** A `git worktree add` or `git worktree list` is refused in any cwd and pointed at `worktree_create` and `worktree_list`; in the principal checkout, a switch that would leave the branch it is on is refused while that branch is the repository's default, and creating a branch is not blocked. See [the guards](docs/reference.md#the-bash-guards).
75
76
  - **Two blocks in the system prompt.** `## ODD worktrees` tells the agent how worktrees and ODDs work. `## CodeGraph` tells a session that already holds a CodeGraph tool how to use it, with one text per role. Both are appended after the host's own text, and `ODD_WORKTREE_PROMPT=0` suppresses them. See [the prompt blocks](docs/reference.md#the--odd-worktrees-block-in-the-system-prompt).
76
77
  - **CodeGraph stays fresh, only when its CLI is installed.** At session start the package creates or syncs the checkout's index and warms its daemon, and makes sure your global `mcp.json` has the `codegraph` entry (it adds it or fixes only the differing fields and keeps the rest). It also syncs after a compaction, after a native `codegraph init` and after an `edit` or `write`, in the background and without ever blocking you. See [CodeGraph](docs/reference.md#codegraph-index-daemon-and-mcp-entry). Its tool rows are drawn as a card.
77
- - **Two reminders keep a session on the CodeGraph rule.** The first `grep`, `find`, `ls`, `read` or `bash` search in a checkout gets one sentence appended to its result: use `codegraph` first, or — for another checkout — call `mcp__codegraph__codegraph_explore` with that path as `projectPath`. Once per checkout, once per session, never a block. See [the reminders](docs/reference.md#the-two-codegraph-reminders).
78
+ - **Two reminders keep a session on the CodeGraph rule.** The first `grep`, `find`, `ls`, `read` or `bash` search in a checkout gets one sentence appended to its result: use `codegraph` first, or — for another checkout — call `mcp__codegraph__codegraph_explore` with that path as `projectPath`. Each message that starts with a search instead of the graph gets the one about your own checkout once, on its first search, and no later search of it repeats it (a new request re-arms it, a compaction starts it from zero); the one about another checkout stays once per checkout; neither is ever a block. See [the reminders](docs/reference.md#the-two-codegraph-reminders).
78
79
  - **Deliberately absent.** No customize panel, no plugin-tools toggles, no companion-app integration and no worktree-to-issue association. There is nothing to configure here: the tools design — and, with it, the skill/ODD file cards — is chosen on the bridge's own `Card tags` line (or `Card bash`, on an older bridge), so this package owns no preference of its own.
79
80
 
80
81
  ## Documentation
@@ -3099,6 +3099,51 @@ function guardWorktreeBashCall(event, ctx) {
3099
3099
  }
3100
3100
  }
3101
3101
 
3102
+ // S4 (T3): the WRITE guard's `tool_call` handler, a sibling of the bash one on the same
3103
+ // hook. An `edit` or `write` whose path lies inside the primary checkout, while that
3104
+ // checkout is on the repository's default branch, is refused with one sentence (L1:
3105
+ // the human chose a block, not a warning). Every other tool, and every path outside the
3106
+ // session's own repository — tmp, another repository, a linked worktree (L2) — passes,
3107
+ // and the same `ODD_WORKTREE_GUARD=0` switch turns it off with the bash guards.
3108
+ //
3109
+ // It reads the touched paths the naming handler already reads (`oddTouchedPaths` over
3110
+ // the call's own arguments) and its cwd the same way (`oddEventCwd`), and hands the
3111
+ // guard the SAME memoized identity/default-branch seams, so the two guards can never
3112
+ // disagree about which checkout the session stands in. The current branch is read
3113
+ // fresh for every judgement: the checkout's own answer, never a remembered one.
3114
+ const WRITE_GUARD_TOOLS = new Set(["edit", "write"]);
3115
+
3116
+ function guardWorktreeWriteCall(event, ctx) {
3117
+ try {
3118
+ if (process.env.ODD_WORKTREE_GUARD === "0") return undefined;
3119
+ const toolName = oddToolNameOf(event);
3120
+ if (!WRITE_GUARD_TOOLS.has(toolName)) return undefined;
3121
+ const paths = oddTouchedPaths(oddArgsOf(event));
3122
+ if (paths.length === 0) return undefined;
3123
+ const guard = libRequire("worktree-guard.js");
3124
+ if (!guard || typeof guard.decideWriteGuard !== "function") return undefined;
3125
+ const git = libRequire("worktree-git.js");
3126
+ const decision = guard.decideWriteGuard({
3127
+ toolName,
3128
+ paths,
3129
+ cwd: oddEventCwd(event, ctx),
3130
+ repoIdentity: (cwd) => cachedRepoIdentity(git, cwd),
3131
+ // The owner of a written path's directory is read live (only for a path already
3132
+ // inside the principal), because a nested worktree can appear during the session.
3133
+ ownerIdentity: (dir) => git.repoIdentity(String(dir)),
3134
+ defaultBranch: (root) => cachedDefaultBranch(git, root),
3135
+ currentBranch: (root) => readCurrentBranch(git, root),
3136
+ });
3137
+ if (decision && decision.block === true && typeof decision.reason === "string") {
3138
+ return { block: true, reason: decision.reason };
3139
+ }
3140
+ return undefined;
3141
+ } catch {
3142
+ // S8: the guard never throws into the host, and a call it cannot judge passes.
3143
+ return undefined;
3144
+ }
3145
+ }
3146
+
3102
3147
  // ---------------------------------------------------------------------------
3103
3148
  // `/warp <name>` (S23): move the SESSION to another
3104
3149
  // checkout by NAME.
@@ -9781,6 +9826,11 @@ function registerOddWorktreeSurfaceEntries(pi) {
9781
9826
  // the only handler that returns a `block` answer; every other tool call is left
9782
9827
  // exactly as the host proposed it. Nothing about it is callable by the agent (S7).
9783
9828
  pi.on("tool_call", (event, ctx) => guardWorktreeBashCall(event, ctx));
9829
+ // S4 (T3): the write guard, a second `tool_call` observation that refuses an `edit`
9830
+ // or `write` into the primary checkout while it sits on the default branch. It is a
9831
+ // sibling of the bash guard, not a part of it: the two read different inputs, and a
9832
+ // `bash` call and a file write are refused for different reasons.
9833
+ pi.on("tool_call", (event, ctx) => guardWorktreeWriteCall(event, ctx));
9784
9834
  pi.on("tool_result", (event, ctx) => handleWorktreeOddEvent(event, ctx, pi, true));
9785
9835
  // S10 (T9): ONE `tool_result` observation for the rail's forced refresh. It
9786
9836
  // answers the `bash` tool only — every other tool returns before the classifier
@@ -1,17 +1,18 @@
1
1
  'use strict';
2
- // T1 (S1, S2, S5, S6, S7): the two CodeGraph reminders, decided from a finished tool call.
2
+ // T1 (S1, S2, S5, S6, S7) and T6 (S3): the two CodeGraph reminders, decided from a finished tool call.
3
3
  //
4
4
  // Guard 1 says a call went to ANOTHER checkout and points the session at
5
5
  // `mcp__codegraph__codegraph_explore` with that checkout as `projectPath`; it belongs to the primary
6
6
  // session and fires once per checkout per session. Guard 2 says to reach CodeGraph first inside the
7
7
  // session's OWN checkout; it belongs to the primary session and to every child except the `review-*`
8
- // agents, and its key is the checkout, so a `/warp` re-arms it.
8
+ // agents, and it reminds once on the first search of a checkout, while the graph is unused in that
9
+ // request, and then never again for that checkout in that request (S3, S7).
9
10
  //
10
- // Both are precise: a checkout the session already explored with the MCP tool silences guard 1, a
11
- // session that already used CodeGraph silences guard 2, and a reminder that does not fire uses up no
12
- // slot. A call that failed is no subject at all: the model got no result to read, so nothing is
13
- // reminded, spent or recorded from it. A compaction forgets everything, and so does a new session in
14
- // the same process.
11
+ // Both are precise: a checkout the session already explored with the MCP tool silences guard 1, and a
12
+ // reminder that does not fire uses up no slot. A call that failed is no subject at all: the model got no
13
+ // result to read, so nothing is reminded, spent or recorded from it. A new request forgets guard 2's
14
+ // first-search slots and `used` flag; a compaction forgets everything, and so does a new session in the
15
+ // same process.
15
16
  //
16
17
  // The reminder is only a sentence. Appending it to the tool result is the wiring's job
17
18
  // (`lib/codegraph-session.js`); nothing here blocks, notifies or writes. Git is reached through one
@@ -40,8 +41,8 @@ function sameCheckoutReminder(input = {}) {
40
41
  const variant = input.variant;
41
42
  const tools = Array.isArray(input.tools) ? input.tools : [];
42
43
  return NATIVE_VARIANTS.includes(variant) && tools.includes(NATIVE_TOOL)
43
- ? 'Follow the CodeGraph rule: run `codegraph` `query` or `explore` first, if it applies.'
44
- : `Follow the CodeGraph rule: call \`${MCP_TOOL}\` first, if it applies.`;
44
+ ? 'Follow the CodeGraph rule: run `codegraph` `query` or `explore` first.'
45
+ : `Follow the CodeGraph rule: call \`${MCP_TOOL}\` first.`;
45
46
  }
46
47
 
47
48
  function isExploreCall(event) {
@@ -52,13 +53,13 @@ function isCodegraphCall(event) {
52
53
  return Boolean(event) && (event.toolName === NATIVE_TOOL || isExploreCall(event));
53
54
  }
54
55
 
55
- /** The once-per-key slots and what the session already used, forgotten together. */
56
+ /** The once-per-key slots and what the request already used, forgotten together. */
56
57
  function createGuardState() {
57
58
  return {
58
- checkouts: new Set(), // guard 1: the other checkouts already reminded
59
- own: new Set(), // guard 2: the session checkouts already reminded
59
+ checkouts: new Set(), // guard 1: the other checkouts already reminded, for the whole session
60
+ own: new Set(), // guard 2: the checkouts already reminded on their first search, for this request
60
61
  explored: new Set(), // the absolute projectPath of every MCP explore call this session made
61
- used: false, // the session already used CodeGraph at all
62
+ used: false, // this request already used CodeGraph
62
63
  };
63
64
  }
64
65
 
@@ -69,6 +70,15 @@ function resetGuardState(state) {
69
70
  state.used = false;
70
71
  }
71
72
 
73
+ /**
74
+ * S7: a new request forgets guard 2 alone — its first-search slots and `used`. Guard 1's slots and the
75
+ * explored checkouts are the session's, so they stay.
76
+ */
77
+ function forgetRequestState(state) {
78
+ state.own.clear();
79
+ state.used = false;
80
+ }
81
+
72
82
  /**
73
83
  * What one finished call says about the session's use of CodeGraph: the native tool or the MCP tool is a
74
84
  * use (S5), and an explore call aimed at a `projectPath` records that checkout. Returns whether this call
@@ -97,7 +107,8 @@ async function exploredCheckout(state, root, resolver) {
97
107
  /**
98
108
  * The reminder one call earns, or undefined. It marks the slot it used, so the same key is never
99
109
  * reminded twice; a call whose reminder would duplicate one, or whose checkout the session already
100
- * explored, marks nothing.
110
+ * explored, marks nothing. Guard 2 (S3, S7) reminds once on the first search of a checkout in a request
111
+ * while the graph is unused, and never again for that checkout in that request.
101
112
  */
102
113
  async function decideGuard(input) {
103
114
  const { state, call, ownRoot, tools, variant, resolver } = input;
@@ -112,7 +123,8 @@ async function decideGuard(input) {
112
123
  state.checkouts.add(checkout.root);
113
124
  return { kind: 'another-checkout', checkout: checkout.root, text: anotherCheckoutReminder(checkout.root) };
114
125
  }
115
- if (state.own.has(checkout.root) || state.used) return undefined;
126
+ // The first search of a checkout is reminded at once, and only while the graph is still unused.
127
+ if (state.used !== false || state.own.has(checkout.root)) return undefined;
116
128
  state.own.add(checkout.root);
117
129
  return { kind: 'same-checkout', checkout: checkout.root, text: sameCheckoutReminder({ variant, tools }) };
118
130
  }
@@ -125,10 +137,10 @@ function sessionCwd(event, ctx) {
125
137
 
126
138
  /**
127
139
  * The two guards of one session, with their own state. `observeTools` takes the session's tool list from
128
- * the host's `before_agent_start` event (the only source of it); `onToolResult` decides one reminder from
129
- * one finished call; `onCompacted` and `onNewSession` forget everything. Every collaborator is injectable:
130
- * `env`, `argv` (the child's agent name), `toplevel` (the cached git read) and `isDirectory` (whether a path
131
- * is a directory, so a file costs one read).
140
+ * the host's `before_agent_start` event (the only source of it); `onNewRequest` re-arms guard 2 for a new
141
+ * request; `onToolResult` decides one reminder from one finished call; `onCompacted` and `onNewSession`
142
+ * forget everything. Every collaborator is injectable: `env`, `argv` (the child's agent name), `toplevel`
143
+ * (the cached git read) and `isDirectory` (whether a path is a directory, so a file costs one read).
132
144
  */
133
145
  function createCodegraphGuard(deps = {}) {
134
146
  const env = deps.env || process.env;
@@ -147,6 +159,11 @@ function createCodegraphGuard(deps = {}) {
147
159
  selected = Array.isArray(selectedTools) ? selectedTools.filter((tool) => typeof tool === 'string') : [];
148
160
  },
149
161
 
162
+ /** A new request re-arms guard 2: its first-search slots and what it used (S7). */
163
+ onNewRequest() {
164
+ forgetRequestState(state);
165
+ },
166
+
150
167
  /** A compaction forgets what was reminded and what was used (S7). */
151
168
  onCompacted() {
152
169
  resetGuardState(state);
@@ -2,7 +2,7 @@
2
2
 
3
3
  Run one call to the CodeGraph tool you hold before you search or read code — `fd`, `find`, `rg`, `grep`,
4
4
  `read` or `cat` — when the task needs structure rather than a lookup: where something lives, who calls it,
5
- what a change reaches. Exact strings, filenames and a known file may skip it, and the other tools stay
5
+ what a change reaches. Skip it only for an exact string or a file path you were given. The other tools stay
6
6
  available after the first call.
7
7
 
8
8
  The answer is a map, not proof: confirm it in the source, and read a file before editing it.
@@ -1,12 +1,12 @@
1
1
  ## CodeGraph
2
2
 
3
- Run a `codegraph` `query` for symbols or `explore` for source plus call paths before you search or read
4
- code — `fd`, `find`, `rg`, `grep`, `read` or `cat` — and before you plan or size a change that one read
5
- batch does not already explain: one `explore` on the change's subject shows the files and callers it
6
- reaches, which is what scope, size and any handoff depend on, and `query` locates a symbol. Exact strings,
7
- a file or location you already know and a run that only executes commands or tests need no call. The call
8
- comes first, not instead: the other tools stay for what the graph does not index, and its answer is a map
9
- — a verdict comes from the source and from tests.
3
+ Before you `grep`, `rg`, `fd`, `find`, `read` or `cat` to answer where code lives, who calls it, how it
4
+ works, or what a change reaches, call CodeGraph first: `codegraph` with `operation: "explore"` and
5
+ `query: "<symbols or the question>"` returns the source and the call paths in one call, and it is
6
+ also what scope, size and any handoff depend on. Use `operation: "query"` only to find where one
7
+ symbol is defined. Skip it only for an exact string or a file path the user gave you. The other tools stay for
8
+ what the graph does not index. Its answer is a map: confirm it in the source, and a verdict comes
9
+ from the source and from tests.
10
10
 
11
11
  `codegraph` answers only for the checkout the session runs in, and each worktree is its own workspace with
12
12
  its own index. An ODD works in its own worktree: when the session runs elsewhere, look at that worktree, at
@@ -10,7 +10,8 @@
10
10
  // - before a CodeGraph search: a bounded join of the index work this process already has in flight for that checkout
11
11
  // (never a new one, and nothing at all when nothing is in flight);
12
12
  // - a search or read call into another checkout, or into the session's own one: one reminder appended to
13
- // that call's result (never a block, never a separate message), forgotten on a compaction and a new session;
13
+ // that call's result (never a block, never a separate message), re-armed at every new request and
14
+ // forgotten on a compaction and a new session;
14
15
  // - the CodeGraph tool card, through the host's renderer hook;
15
16
  // - shutdown: every pending window is cleared.
16
17
  //
@@ -299,8 +300,15 @@ function registerCodegraph(pi, deps = {}) {
299
300
  });
300
301
 
301
302
  // The session's tools reach the guards from the ONE source that has them: the prompt options of
302
- // `before_agent_start`. Nothing here touches the prompt itself.
303
+ // `before_agent_start`. S7: that same event is a new request, so guard 2 is re-armed there (its
304
+ // first-search slots and `used`), while guard 1's slots stay per session. Nothing here touches the
305
+ // prompt itself.
303
306
  pi.on('before_agent_start', (event) => {
307
+ try {
308
+ guards.onNewRequest();
309
+ } catch {
310
+ // Re-arming a reminder never breaks a prompt.
311
+ }
304
312
  try {
305
313
  guards.observeTools(event && event.systemPromptOptions && event.systemPromptOptions.selectedTools);
306
314
  } catch {
@@ -35,11 +35,15 @@
35
35
  // design only ever sees the body there) gets the whole pane from the call half instead, and the
36
36
  // host's own hint row, when it drew one, follows the fold row unchanged. No hint is ever
37
37
  // invented and none is ever dropped.
38
- // * `edit` — the host's own diff, recoloured: additions in the theme's `toolDiffAdded` role,
39
- // deletions in `toolDiffRemoved`, hunk headers and context in `muted`/`text`, and the code of a
40
- // row highlighted with the language `args.path` names. The row's text is never changed, so its
41
- // width survives, and the transform is idempotent: a row already wearing this paint comes back
42
- // identical.
38
+ // * `edit` — the host's own diff, recoloured and divided: every row that states a line gets the
39
+ // same `N │ code` gutter the write pane draws, in the write pane's own role for the rule, and a
40
+ // line the call DELETED is painted whole in the theme's `toolDiffRemoved` role and a line it
41
+ // ADDED whole in `toolDiffAdded` — marker, number, divider and code — as `git diff` paints
42
+ // them, so a changed row carries no syntax colour of its own. A context row keeps the gutter,
43
+ // the divider and the code highlighted with the language `args.path` names; a hunk header is
44
+ // `muted`. Every row is measured as PLAIN text and its code clipped to what the divider leaves,
45
+ // so the row keeps the width the host gave it, and the transform is idempotent: a row already
46
+ // wearing this paint — divider included — comes back byte-identical.
43
47
  //
44
48
  // S6: every bridge member stays optional and defensive. A bridge without `registerToolDesign`, or
45
49
  // one older than the release that can take a second line on these two tools (0.9.0, whose per-
@@ -405,19 +409,53 @@ function writeCallRows(lines, ctx, render, seam) {
405
409
  // ---- the `edit` diff, recoloured ----
406
410
 
407
411
  const HUNK = /^@@/u;
408
- const ADDED = /^\+(\s*\d*)\s/u;
409
- const REMOVED = /^-(\s*\d*)\s/u;
412
+ // The marker and the line number of a row the host numbered. A NUMBER is required: `renderDiff`
413
+ // always states one, and a row the host wrapped onto a continuation row states neither (the wrap
414
+ // carries on at the row's own left column). A continuation is recognised only when its own text
415
+ // happens to read as `<number> ` — the one ambiguity a plain-text layout cannot answer.
416
+ const ADDED = /^\+(\s*\d+)\s/u;
417
+ const REMOVED = /^-(\s*\d+)\s/u;
410
418
  const CONTEXT = /^(\d+)\s/u;
411
419
  const HUNK_TOKEN = /^@@.*?@@/u;
420
+ // The rule between a line-number gutter and its code, exactly the one the write pane's `codeRow`
421
+ // draws: `${number} │ ${code}`. It is read back off a row's PLAIN text, so a row this design already
422
+ // divided is divided no further (S12/S14) — which is also this design's one plain-text ambiguity: a
423
+ // row whose own code begins with `│ ` is read as one that already wears the divider.
424
+ const DIVIDER = '│';
425
+ const DIVIDER_TAIL = `${DIVIDER} `;
426
+ const DIVIDER_RUN = ` ${DIVIDER_TAIL}`;
412
427
  // The cells a diff row's own text is padded by on the left: the frame's sides and its spaces.
413
428
  const LEADING_CHROME = new Set([' ', '│', '▎', '|', '┃', '║']);
414
- // A run of SGR sequences at one EDGE of the bytes a row keeps: the paint standing immediately
415
- // before the span this design repaints, and the one immediately after it. Both govern only that
416
- // span, which is rebuilt from the row's plain text, so dropping them is what makes a second pass a
417
- // no-op (S8b) — and a row that never wore this paint loses nothing but the colour the host had just
418
- // opened for the marker, which is repainted in this design's own role.
429
+ // The paint that governs the span this design repaints stands at that span's two edges: the escapes
430
+ // that OPEN it are the last ones before the span, and the RESET that closes it is the first one
431
+ // after. Both belong to a span that is rebuilt from the row's plain text, so dropping them is what
432
+ // makes a second pass a no-op (S8b) instead of a row carrying every escape twice. The opening run is
433
+ // dropped whole, which is what a row this design already painted needs: its own last escape there.
434
+ // The closing run is dropped only while it CLEARS a state, because a frame cell's own paint starts
435
+ // there too (`\u001b[32m│`) and is the host's to keep.
419
436
  const TRAILING_SGR = /(?:\u001b\[[0-9;]*m)+$/u;
420
- const LEADING_SGR = /^(?:\u001b\[[0-9;]*m)+/u;
437
+ const SGR_CLEARS = new Set(['0', '22', '23', '24', '25', '27', '28', '29', '39', '49', '55', '59']);
438
+
439
+ /** Whether one SGR escape CLEARS a state rather than opening one. */
440
+ function clearsSgr(params) {
441
+ return params === '' || params.split(';').some((code) => SGR_CLEARS.has(code));
442
+ }
443
+
444
+ /**
445
+ * `text` with the RESET that closes the paint of the span this design repaints dropped from its
446
+ * START, and nothing that OPENS a paint: the frame cell that follows the span is already painted.
447
+ */
448
+ function withoutClosingPaint(text) {
449
+ const one = /\u001b\[([0-9;]*)m/uy;
450
+ let index = 0;
451
+ while (index < text.length) {
452
+ one.lastIndex = index;
453
+ const escape = one.exec(text);
454
+ if (escape === null || !clearsSgr(escape[1])) break;
455
+ index += escape[0].length;
456
+ }
457
+ return text.slice(index);
458
+ }
421
459
 
422
460
  /** The last character of a row's own TEXT, or `-1` when the row is chrome alone. */
423
461
  function contentLast(chars) {
@@ -451,18 +489,47 @@ function diffStart(chars) {
451
489
  }
452
490
 
453
491
  /**
454
- * One diff row with this design's paint on it: the marker and the line number in the role its kind
455
- * names, the code in the highlighter's own roles (or the theme's `text`), and every other cell of
456
- * the host's row — the frame, its padding, the background — byte for byte. A row that is not a diff
457
- * row, and one whose content this cannot read, come back untouched.
492
+ * The bytes a row keeps AFTER its own text, split into the padding it may give back to the gutter and
493
+ * the host's own tail (its frame cells and the reset they carry). `columns` is the padding's width,
494
+ * which is what a row with no slack needs to know before it can be divided.
495
+ */
496
+ function afterChrome(row, last) {
497
+ const after = withoutClosingPaint(row.slice(last.raw + last.char.length));
498
+ const chars = visibleChars(after);
499
+ let pad = 0;
500
+ while (pad < chars.length && chars[pad].char === ' ') pad += 1;
501
+ const cut = pad === 0 ? 0 : chars[pad - 1].raw + chars[pad - 1].char.length;
502
+ return { padding: after.slice(0, cut), rest: after.slice(cut), columns: pad };
503
+ }
504
+
505
+ /** `padding` with `drop` of its trailing space columns given back, every other byte untouched. */
506
+ function giveBack(padding, drop) {
507
+ if (drop <= 0) return padding;
508
+ const chars = visibleChars(padding);
509
+ if (chars.length <= drop) return '';
510
+ return padding.slice(0, chars[chars.length - drop].raw);
511
+ }
512
+
513
+ /**
514
+ * One diff row with this design's drawing on it: the line-number gutter, the divider the write pane
515
+ * draws, and the code. A row that states a DELETION is painted whole in `toolDiffRemoved` and one
516
+ * that states an ADDITION whole in `toolDiffAdded` — marker, number, divider and code — because the
517
+ * role's colour is the whole point (S13), so neither carries a syntax colour of its own. A CONTEXT
518
+ * row keeps the gutter, the divider in the role the write pane paints its own, and the code in the
519
+ * highlighter's roles. A hunk header states no line, so it has no gutter to divide and keeps today's
520
+ * drawing. Every other cell of the host's row — the frame, its padding, the background — is the
521
+ * host's, byte for byte, and a row that is not a diff row comes back untouched.
458
522
  *
459
- * S8b: the row is RECOGNISED and MEASURED through its plain text (`visibleChars` drops every SGR
460
- * sequence), and the span that carries the marker and the code is rebuilt from that plain text. The
461
- * two SGR runs that stand immediately beside that span therefore belong to text this function
462
- * replaces, and are dropped: one is the host's own paint of the marker, the other is this design's
463
- * own `theme.fg` codes when the row it is handed was already painted. Nothing this design added
464
- * survives into the rewritten row, which is what makes a second pass a byte-identical no-op instead
465
- * of a row carrying every escape twice.
523
+ * S12: the divider costs two columns, and they come out of the row's own padding, so the row keeps
524
+ * the width the host gave it; a row with no padding to give back loses the tail of its CODE to a
525
+ * PLAIN clip (never a cut through a painted string, invariant 17), never the frame.
526
+ *
527
+ * S8b/S14: the row is RECOGNISED and MEASURED through its plain text (`visibleChars` drops every SGR
528
+ * sequence), the span that carries the gutter and the code is rebuilt from that plain text, and the
529
+ * two SGR runs that stand immediately beside that span are dropped — one is the host's own paint of
530
+ * that span, the other is this design's own `theme.fg` codes when the row it is handed was already
531
+ * painted. The divider is read back off the same plain text, so it is never added twice, and nothing
532
+ * this design added survives into the rewritten row: a second pass is a byte-identical no-op.
466
533
  */
467
534
  function recolourDiffRow(row, spec) {
468
535
  const chars = visibleChars(row);
@@ -471,33 +538,52 @@ function recolourDiffRow(row, spec) {
471
538
  const last = contentLast(chars);
472
539
  if (last < start) return row;
473
540
  const rest = chars.slice(start, last + 1).map((entry) => entry.char).join('');
474
- let token;
475
- let role;
476
- let code;
541
+ const before = row.slice(0, chars[start].raw).replace(TRAILING_SGR, '');
542
+ const after = withoutClosingPaint(row.slice(chars[last].raw + chars[last].char.length));
477
543
  const hunk = HUNK_TOKEN.exec(rest);
478
544
  if (HUNK.test(rest)) {
479
- token = hunk === null ? '@@' : hunk[0];
480
- role = 'muted';
481
- code = rest.slice(token.length);
545
+ const token = hunk === null ? '@@' : hunk[0];
546
+ const code = rest.slice(token.length);
547
+ const paintedCode = code === '' ? '' : highlightRow(code, 'muted', spec.language, spec.seam, spec.theme);
548
+ return `${before}${paint(spec.theme, 'muted', token)}${paintedCode}${after}`;
549
+ }
550
+ const match = ADDED.exec(rest) ?? REMOVED.exec(rest) ?? CONTEXT.exec(rest);
551
+ if (match === null) return row;
552
+ // `undefined` marks a context row, whose marker is a space and already part of the row's chrome.
553
+ const added = ADDED.test(rest);
554
+ const role = added ? 'toolDiffAdded' : REMOVED.test(rest) ? 'toolDiffRemoved' : undefined;
555
+ const gutter = role === undefined ? match[1] : `${rest[0]}${match[1]}`;
556
+ const head = `${gutter}${DIVIDER_RUN}`;
557
+ const headColumns = textColumns(head);
558
+ const tail = afterChrome(row, chars[last]);
559
+ // The row's own drawing width: its text plus the padding the divider may take from.
560
+ const room = textColumns(rest) + tail.columns;
561
+ // Too narrow to hold even the gutter: the host's own drawing (whole row in its role) stays.
562
+ if (room - headColumns < 1) return row;
563
+ const tailText = rest.slice(match[0].length);
564
+ // A row this design already divided wears the rule right after its number: read it back, so the
565
+ // second pass rebuilds the same row instead of adding a second divider (S14).
566
+ const code = tailText.startsWith(DIVIDER_TAIL) ? tailText.slice(DIVIDER_TAIL.length) : tailText;
567
+ const clipped = headColumns + textColumns(code) > room;
568
+ const shown = clipped ? clipPlain(code, room - headColumns) : code;
569
+ let body;
570
+ if (role !== undefined) {
571
+ body = paint(spec.theme, role, `${head}${shown}`);
482
572
  } else {
483
- const match = ADDED.exec(rest) ?? REMOVED.exec(rest) ?? CONTEXT.exec(rest);
484
- if (match === null) return row;
485
- token = match[0];
486
- role = ADDED.test(rest) ? 'toolDiffAdded' : REMOVED.test(rest) ? 'toolDiffRemoved' : 'muted';
487
- code = rest.slice(token.length);
573
+ const paintedCode = shown === ''
574
+ ? ''
575
+ : clipped
576
+ ? paint(spec.theme, 'text', shown)
577
+ : highlightRow(shown, 'text', spec.language, spec.seam, spec.theme);
578
+ body = `${paint(spec.theme, 'muted', gutter)} ${paint(spec.theme, 'dim', DIVIDER)} ${paintedCode}`;
488
579
  }
489
- const language = spec.language;
490
- const paintedCode = code === ''
491
- ? ''
492
- : highlightRow(code, role, language, spec.seam, spec.theme);
493
- const before = row.slice(0, chars[start].raw).replace(TRAILING_SGR, '');
494
- const after = row.slice(chars[last].raw + chars[last].char.length).replace(LEADING_SGR, '');
495
- return `${before}${paint(spec.theme, role, token)}${paintedCode}${after}`;
580
+ const slack = Math.max(0, room - headColumns - textColumns(shown));
581
+ return `${before}${body}${giveBack(tail.padding, tail.columns - slack)}${tail.rest}`;
496
582
  }
497
583
 
498
584
  /**
499
- * The code of one diff row: the host's highlighter when it paints this exact line, else the plain
500
- * text in `fallback` (the diff's own role for an addition or a deletion, `text` for context).
585
+ * The code of one row whose whole line the highlighter can paint: the host's highlighter when it
586
+ * paints this exact line, else the plain text in `fallback`.
501
587
  */
502
588
  function highlightRow(code, fallback, language, seam, theme) {
503
589
  if (language !== undefined) {
@@ -508,9 +594,10 @@ function highlightRow(code, fallback, language, seam, theme) {
508
594
  }
509
595
 
510
596
  /**
511
- * Every row of an `edit` card, recoloured. Idempotent (S8b): each row is read as its PLAIN text and
512
- * rebuilt from it, so a row already wearing this paint states the same visible characters and comes
513
- * back byte for byte — never with a second copy of an escape or a reset.
597
+ * Every row of an `edit` card, recoloured and divided. Idempotent (S8b/S14): each row is read as its
598
+ * PLAIN text and rebuilt from it, divider included, so a row already wearing this drawing states the
599
+ * same visible characters and comes back byte for byte — never with a second copy of an escape, a
600
+ * reset or the divider.
514
601
  */
515
602
  function editRows(lines, render, ctx, seam) {
516
603
  const language = languageOf(callPath(render?.args), seam);
@@ -0,0 +1,3 @@
1
+ ## ODD worktrees
2
+
3
+ Work only under the worktree of your workspace root: if that is the primary checkout on the default branch, stop and report instead of writing.
@@ -1,13 +1,17 @@
1
1
  ## ODD worktrees
2
2
 
3
- An ODD writes only in its own worktree, never in the primary checkout or on the default
4
- branch; the feature branch lives there. Reading anywhere is fine, and a request that stays
5
- read-only needs no worktree.
3
+ Every write goes in a worktree, whatever the size of the task, a one-line fix included. Never write
4
+ in the primary checkout or on the default branch.
6
5
 
7
- A worktree lives at `<repo-parent>/<repo-name>-worktrees/<odd>`: the directory IS the ODD
8
- it serves. Before the ODD's first write, call `worktree_list`: if it already has a
9
- worktree, work there; if not, create it with `worktree_create` (use `copy_changes: true`
10
- if the pending changes of the current checkout belong to that work).
6
+ A worktree lives at `<repo-parent>/<repo-name>-worktrees/<odd>`: the directory IS the ODD it serves, and
7
+ its feature branch lives there. Before your first `edit` or `write`:
8
+
9
+ 1. Run `worktree_list`.
10
+ 2. If the ODD has a worktree, work there.
11
+ 3. If not, run `worktree_create` (`copy_changes: true` if the pending changes of the current checkout
12
+ belong to that work).
13
+
14
+ Reading anywhere is fine, and a request that stays read-only needs no worktree.
11
15
 
12
16
  One ODD, one worktree. An ODD is one request and its follow-ups, big or small: everything
13
17
  that belongs to it goes into the same ODD and worktree, and an unrelated request is another