pi-odd-worktree 3.1.1 → 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 {
@@ -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
@@ -1,27 +1,43 @@
1
1
  'use strict';
2
- // The `## ODD worktrees` section of the primary session's system prompt (S7).
2
+ // The `## ODD worktrees` section of the primary session's system prompt (S7), and
3
+ // (T3, S5) the one short block a CHILD that writes gets.
3
4
  //
4
5
  // It is appended to `systemPromptOptions.appendSystemPrompt` and nowhere else:
5
6
  // a handler-returned `systemPrompt` is dropped by pi-claude-bridge, and
6
7
  // replacing the option would erase the harness's own text. The block is only
7
8
  // ever added after the existing text, is skipped when it is already there, and
8
- // reaches a primary session only — a named agent or a `GENTLE_PI_AGENTS_CHILD`
9
- // worker is left untouched. `ODD_WORKTREE_PROMPT=0` turns it off.
9
+ // reaches a primary session only — a named agent gets the primary text of its
10
+ // own session, never this one.
11
+ //
12
+ // S5: a `GENTLE_PI_AGENTS_CHILD` session is not skipped any more. A child that
13
+ // WRITES — `gentle-ai-worker`, `jd-fix-agent`, and any child whose agent name the
14
+ // argv cannot name, which is the generic child — gets one short line telling it to
15
+ // work in its workspace root's worktree and to stop and report on the primary. The
16
+ // children that only look or judge (explore, verify, the two judges, every
17
+ // `review-*`) get nothing, exactly as before. The agent name comes from
18
+ // `lib/codegraph-rules.js` (`agentNameFromArgv`), which already reads it from the
19
+ // child's own argv; the argv parsing is never duplicated here.
20
+ //
21
+ // `ODD_WORKTREE_PROMPT=0` turns both blocks off.
10
22
 
11
23
  const fs = require('node:fs');
12
24
  const path = require('node:path');
25
+ const { agentNameFromArgv, variantFor } = require('./codegraph-rules.js');
13
26
 
14
27
  // The heading is the identity of the block, so an already-injected section is
15
28
  // never added twice.
16
29
  const SECTION_MARKER = '## ODD worktrees';
17
30
 
18
31
  // The section text lives in its own Markdown file so it can be read, reviewed
19
- // and edited as prose: `lib/odd-prompt/odd-worktrees.md`. A missing or empty
20
- // file falls back to the heading alone: the block still injects, never crashes
21
- // the session, and the idempotency marker still works.
32
+ // and edited as prose: `lib/odd-prompt/odd-worktrees.md`, and — one short line for a
33
+ // child that writes — `lib/odd-prompt/odd-worktrees-child.md`. A missing or empty
34
+ // file falls back to the heading alone: the block still injects, never crashes the
35
+ // session, and the idempotency marker still works.
22
36
  const PROMPT_DIR = path.join(__dirname, 'odd-prompt');
23
37
  const PROMPT_FILE = path.join(PROMPT_DIR, 'odd-worktrees.md');
38
+ const CHILD_PROMPT_FILE = path.join(PROMPT_DIR, 'odd-worktrees-child.md');
24
39
  const FALLBACK = `${SECTION_MARKER}\n\nEach ODD feature has its own worktree.`;
40
+ const CHILD_FALLBACK = `${SECTION_MARKER}\n`;
25
41
 
26
42
  function readPromptFile(file) {
27
43
  try {
@@ -32,7 +48,31 @@ function readPromptFile(file) {
32
48
  }
33
49
  }
34
50
 
35
- const SECTION = readPromptFile(PROMPT_FILE) || FALLBACK;
51
+ // The block one file contributes, or the fallback when the file cannot be used.
52
+ function sectionFromFile(file, fallback) {
53
+ return readPromptFile(file) || fallback;
54
+ }
55
+
56
+ const SECTION = sectionFromFile(PROMPT_FILE, FALLBACK);
57
+ const CHILD_SECTION = sectionFromFile(CHILD_PROMPT_FILE, CHILD_FALLBACK);
58
+
59
+ // S5: the children that WRITE, by the name `agentNameFromArgv` reports for them.
60
+ const CHILD_WRITER_AGENTS = new Set(['gentle-ai-worker', 'jd-fix-agent']);
61
+ const REVIEWER_PREFIX = 'review-';
62
+
63
+ /**
64
+ * S5: whether this child gets the block. A worker or the fix agent does; a child whose
65
+ * name cannot be read is the generic one and does too; a child that only looks or
66
+ * judges — explore, verify, the judges, every `review-*` — never does. An unknown but
67
+ * READ name is generic exactly as `lib/codegraph-rules.js` decides it, so the two
68
+ * blocks can never call the same child two different things.
69
+ */
70
+ function childGetsSection(agent) {
71
+ if (typeof agent !== 'string' || agent.length === 0) return true;
72
+ if (CHILD_WRITER_AGENTS.has(agent)) return true;
73
+ if (agent.startsWith(REVIEWER_PREFIX)) return false;
74
+ return variantFor({ child: true, agent }) === 'generic';
75
+ }
36
76
 
37
77
  // The names a named-agent event may carry. `agent` itself is listed because a
38
78
  // plain string there is a named agent too; an object is read through `.name`.
@@ -57,26 +97,35 @@ function separatorFor(existing) {
57
97
  return '\n\n';
58
98
  }
59
99
 
60
- // Append the section to `event.systemPromptOptions.appendSystemPrompt`.
61
- // Returns nothing: the mutation is the whole contract, so a host that awaits
62
- // the handler receives no replacement prompt.
100
+ // Append the section for this session to
101
+ // `event.systemPromptOptions.appendSystemPrompt`. Returns nothing: the mutation
102
+ // is the whole contract, so a host that awaits the handler receives no
103
+ // replacement prompt. `deps.env` and `deps.argv` are the seams: the environment the
104
+ // session runs with, and the argv a child's agent name is read from.
63
105
  function appendOddWorktreesSection(event, deps = {}) {
64
106
  try {
65
107
  const effectiveDeps = deps || {};
66
108
  const env = effectiveDeps.env || process.env;
67
109
  if (env.ODD_WORKTREE_PROMPT === '0') return;
68
- if (env.GENTLE_PI_AGENTS_CHILD === '1') return;
69
- if (namedAgentOf(event) !== null) return;
110
+
111
+ // S5: a child is judged by the agent name its own argv carries, and gets the child
112
+ // block only when it writes. A primary session keeps the one section it always had,
113
+ // and a named-agent event still gets none of it.
114
+ const child = env.GENTLE_PI_AGENTS_CHILD === '1';
115
+ if (child) {
116
+ if (!childGetsSection(agentNameFromArgv(effectiveDeps.argv || process.argv))) return;
117
+ } else if (namedAgentOf(event) !== null) return;
70
118
 
71
119
  const options = event && event.systemPromptOptions;
72
120
  if (!options || typeof options !== 'object') return;
73
121
 
122
+ const section = child ? CHILD_SECTION : SECTION;
74
123
  const existing = typeof options.appendSystemPrompt === 'string' ? options.appendSystemPrompt : '';
75
124
  if (existing.includes(SECTION_MARKER)) return;
76
125
  // Append-only: every existing byte stays the prefix of the new value.
77
126
  options.appendSystemPrompt = existing.length === 0
78
- ? SECTION
79
- : `${existing}${separatorFor(existing)}${SECTION}`;
127
+ ? section
128
+ : `${existing}${separatorFor(existing)}${section}`;
80
129
  } catch {
81
130
  // The prompt block must never break session start.
82
131
  }
@@ -84,5 +133,10 @@ function appendOddWorktreesSection(event, deps = {}) {
84
133
 
85
134
  module.exports = {
86
135
  appendOddWorktreesSection,
136
+ childGetsSection,
137
+ readPromptFile,
138
+ sectionFromFile,
87
139
  SECTION,
140
+ CHILD_SECTION,
141
+ CHILD_FALLBACK,
88
142
  };