pi-straw-boss 0.81.0 → 0.81.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-straw-boss",
3
- "version": "0.81.0",
3
+ "version": "0.81.2",
4
4
  "description": "Straw Boss coordination for pi in Herdr: sessions join a tab's net by choice, the first joiner is its main agent, a workspace tower opens on demand with a live monitoring dashboard, and boss_broadcast reaches the rest",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/src/briefing.ts CHANGED
@@ -1,18 +1,23 @@
1
+ import { createHash } from "node:crypto";
2
+ import type { SessionEntry } from "@earendil-works/pi-coding-agent";
1
3
  import type { Graph } from "./duty.ts";
2
4
  import type { MainDuty } from "./mainduty.ts";
3
5
  import { cliCommand } from "./server/version.ts";
4
6
  import { ENTRY, type Kit } from "./state.ts";
5
7
  import { type PromptSet, promptSet } from "./trainswitch.ts";
6
8
 
9
+ const LABEL_GRACE_MS = 10_000;
10
+ const LOCATION_LABELS = ["tab", "workspace"] as const;
7
11
  const PROMPTS = ["main", "tower", "member", "member-no-main", "train", "integrator", "dispatch", "duty-standing", "graph-fan-out", "graph-orchestrator-worker"];
8
12
  /** The addendum a graph adds to the dispatch rules: `single-loop` adds none (docs/specs/2026-11-09-agent-kinds.md §2a). */
9
13
  const GRAPH_ADDENDUM: Partial<Record<Graph, string>> = { "fan-out/fan-in": "graph-fan-out", "orchestrator-worker": "graph-orchestrator-worker" };
10
14
 
11
15
  /** What a run was briefed with: the role, a main's kind, and the graph a coordinator's plan named (an integrator and a standing main carry `dispatch.md` whatever their plan says, so they have none here). */
12
16
  interface Briefed { name: string | undefined; duty: string; graph: Graph | "" }
13
- /** A steer's `details`: what it tells, and what it was written against (`null`: it replaces the whole role prompt, whatever the model held). */
14
- interface Told { briefed: Briefed; base: Briefed | null }
17
+ /** A steer's `details`: what it tells, and what it was written against (`null`: it replaces the whole role prompt, whatever the model held); a whole one also signs the role prompt it carries (`sig`). */
18
+ interface Told { briefed: Briefed; base: Briefed | null; provisional?: true; sig?: string }
15
19
  const same = (a: Briefed, b: Briefed) => a.name === b.name && a.duty === b.duty && a.graph === b.graph;
20
+ const sign = (text: string) => createHash("sha256").update(text).digest("hex").slice(0, 16);
16
21
 
17
22
  /** The role prompt: a system prompt section, set when a run starts, and a steer when the role, the kind, or a coordinator's plan changes during one. */
18
23
  export function createBriefing({ pi, cfg, s, role, guiDir, tell }: Kit, mainDuty: MainDuty) {
@@ -28,29 +33,40 @@ export function createBriefing({ pi, cfg, s, role, guiDir, tell }: Kit, mainDuty
28
33
  const current = role(), duty = current === "main" ? mainDuty.now() : "";
29
34
  return { name: current === "member" && !s.mainId ? "member-no-main" : current, duty, graph: duty === "coordinator" ? (mainDuty.graph() ?? "") : "" };
30
35
  };
31
- // A rewritten section invalidates the system prompt cache. Pin the rendered bytes at the first run, except for an explicit boss_duty change.
36
+ // A rewritten section invalidates the system prompt cache. Once the role's location labels are ready, pin the rendered bytes, except for an explicit boss_duty change.
32
37
  // The section never contains a coordinator's graph. Role and graph changes, including ones on a different branch, reach the agent as messages.
33
38
  // `told` is what the agent holds: the section, then each steer pi delivered (its `message_end`), never one only sent, since pi's queue can drop it first (Esc, the dequeue key, the sb GUI's Edit).
34
39
  // One steer waits in the queue at a time (`flight`), and a change meanwhile is written once it arrived, against what it told. A steer an abort left queued arrives at the next run's start, after that run's
35
40
  // own was written against the older state: `told` is unknown (`undefined`) then, and the next steer is the whole prompt.
36
41
  let idle = () => false, told: Briefed | undefined, flight: Told | undefined, running = false, rewriteKind = false;
37
- let section: { base: Briefed; text: string | undefined } | undefined, labels: typeof s.info | undefined;
38
- // Keep the first labels for custom templates as well as the shipped ones. Live labels come from boss_status.
39
- const compose = ({ name, duty, graph }: Briefed, parts = name ? partsOf(name, duty, graph) : []) => parts.map((part) => prompts[part]).join("\n\n").replace(/\{\{(\w+)\}\}/g, (_, key: string) => ({ dir: files ? files.dir : cfg.promptDir, state: guiDir, cli: cliCommand(), ...labels, duty } as Record<string, string | undefined>)[key] || "none yet");
42
+ let section: { base: Briefed; text: string } | undefined, labels: typeof s.info | undefined, labelDeadline: number | undefined;
43
+ // Keep the first role's labels for custom templates too. A steer before pinning uses the same fields, read live.
44
+ const labelsOf = () => ({ tab: s.info.tab, workspace: s.info.workspace, head: s.info.head });
45
+ // Empty labels are valid for unnamed tabs. The optional {{head}} is not a gate, and stock train prompts have no location slots.
46
+ const labelled = (name: string, duty: string) => LOCATION_LABELS.every((key) => s.info[key] !== undefined || !partsOf(name, duty, "").some((part) => prompts[part]?.includes(`{{${key}}}`)));
47
+ const readyToPin = (name: string, duty: string) => {
48
+ if (labelled(name, duty)) return true;
49
+ // Give the asynchronous Herdr look time, but never keep a known role unpinned forever if it fails.
50
+ const now = cfg.now();
51
+ return now >= (labelDeadline ??= now + LABEL_GRACE_MS);
52
+ };
53
+ const compose = ({ name, duty, graph }: Briefed, parts = name ? partsOf(name, duty, graph) : []) => parts.map((part) => prompts[part]).join("\n\n").replace(/\{\{(\w+)\}\}/g, (_, key: string) => ({ dir: files ? files.dir : cfg.promptDir, state: guiDir, cli: cliCommand(), ...(labels ?? labelsOf()), duty } as Record<string, string | undefined>)[key] || "none yet");
40
54
  // A compaction that retries no turn gets the message stored, not steered: a steer queued then would make pi continue a run that had ended (runmode.ts does the same with a run's brief).
41
55
  let quiet = false;
42
56
  // A stored message is in the transcript at the turn's end, and pi reports it to no extension: it is told as it is sent. So is a steer sent while pi is idle (awaiting `agent_settled` handlers, or after a run
43
57
  // that failed to start past `before_agent_start`): pi stores it at once (`_appendCustomMessage`), with no `message_end`.
44
- const steer = (text: string, base: Briefed | null, briefed: Briefed) => {
45
- const details: Told = { briefed, base };
58
+ const steer = (text: string, base: Briefed | null, briefed: Briefed, sig?: string) => {
59
+ const details: Told = { briefed, base, ...(!section && briefed.name && !labelled(briefed.name, briefed.duty) ? { provisional: true } : {}), ...(sig ? { sig } : {}) };
46
60
  pi.sendMessage({ customType: ENTRY, content: `<straw-boss>\n${text}\n</straw-boss>`, display: true, details }, quiet ? { triggerTurn: false } : { deliverAs: "steer" });
47
61
  if (quiet || idle()) told = briefed;
48
62
  else flight = details;
49
63
  };
64
+ /** The role prompt a whole steer carries for `now`. */
65
+ const wholeText = (now: Briefed) => compose(now) || "No Straw Boss role applies.";
50
66
  /** Steers what changed from `was` to `now`, if anything: a role or a kind gets its whole prompt, which replaces the section's, as does an unknown `was`; a coordinator's graph only what it adds, replaces, or drops (§2a). */
51
67
  const steerChange = (was: Briefed | undefined, now: Briefed) => {
52
68
  if ((was && same(was, now)) || (!now.name && !section?.base.name && !was?.name)) return;
53
- const whole = (lead: string) => steer(`${lead} Follow only these Straw Boss role instructions, replacing the system section and any earlier role updates:\n\n${compose(now) || "No Straw Boss role applies."}`, null, now);
69
+ const text = wholeText(now), whole = (lead: string) => steer(`${lead} Follow only these Straw Boss role instructions, replacing the system section and any earlier role updates:\n\n${text}`, null, now, sign(text));
54
70
  if (!was) return whole("Some of your Straw Boss role updates may be missing or out of order.");
55
71
  if (now.name !== was.name) return whole("Your Straw Boss role changed.");
56
72
  if (now.duty !== was.duty) return whole(`Your kind is now ${now.duty}.`);
@@ -68,7 +84,7 @@ export function createBriefing({ pi, cfg, s, role, guiDir, tell }: Kit, mainDuty
68
84
  idle = isIdle;
69
85
  files = promptSet({ promptDir: cfg.promptDir, stateDir: guiDir, env: cfg.env, warn: (text) => tell(text, "warning") });
70
86
  prompts = Object.fromEntries(PROMPTS.map((name) => [name, files!.read(name)]));
71
- [section, labels, told, flight, running, rewriteKind] = [undefined, undefined, undefined, undefined, false, false];
87
+ [section, labels, labelDeadline, told, flight, running, rewriteKind] = [undefined, undefined, undefined, undefined, undefined, false, false];
72
88
  },
73
89
  /** Whether the loaded prompts have integration off; before they are loaded, the switch is read as `promptSet` will read it. */
74
90
  off: () => files?.off,
@@ -80,7 +96,7 @@ export function createBriefing({ pi, cfg, s, role, guiDir, tell }: Kit, mainDuty
80
96
  delivered(message: { role?: string; customType?: string; details?: unknown }) {
81
97
  const d = message.details as Told | undefined;
82
98
  if (message.role !== "custom" || message.customType !== ENTRY || !d?.briefed) return;
83
- const fits = d.base === null || (told !== undefined && (same(d.base, told) || same(d.briefed, told)));
99
+ const fits = !(section && d.provisional) && (d.base === null || (told !== undefined && (same(d.base, told) || same(d.briefed, told))));
84
100
  [told, flight] = [fits ? d.briefed : undefined, flight === d ? undefined : flight];
85
101
  rebrief();
86
102
  },
@@ -90,16 +106,25 @@ export function createBriefing({ pi, cfg, s, role, guiDir, tell }: Kit, mainDuty
90
106
  rebrief();
91
107
  quiet = false;
92
108
  },
93
- begin(sections: Record<string, string>) {
109
+ begin(sections: Record<string, string>, branch: () => readonly SessionEntry[]) {
94
110
  const now = stateOf();
95
- if (!section || (rewriteKind && now.name === "main" && section.base.duty !== now.duty)) {
96
- labels ??= { tab: s.info.tab, workspace: s.info.workspace, head: s.info.head };
111
+ if (now.name && (section ? rewriteKind && now.name === "main" && section.base.duty !== now.duty : readyToPin(now.name, now.duty))) {
112
+ labels ??= labelsOf();
97
113
  const base: Briefed = { ...now, graph: "" };
98
- if (told === section?.base && !flight) told = base; // no earlier steer to supersede the new section
99
- section = { base, text: base.name ? compose(base) : undefined };
114
+ if (!section) {
115
+ // A whole steer with the text this one would carry supersedes everything before it, while the model sees it: pi's context drops what precedes the last compaction's first kept
116
+ // entry (`buildContextEntries`), and a summary may keep older role text. A delta may have lost its base; a provisional steer may lack labels; an unsigned one (0.81.1 and earlier) may hold older prompts.
117
+ const path = branch(), at = path.findLastIndex((e) => e.type === "custom_message" && e.customType === ENTRY && (e.details as Partial<Told> | undefined)?.briefed);
118
+ const cut = path.findLastIndex((e) => e.type === "compaction"), compaction = path[cut], prior = path[at]?.type === "custom_message" ? path[at].details as Told : undefined;
119
+ const seen = compaction?.type !== "compaction" || path.some((e, i) => i <= at && i < cut && e.id === compaction.firstKeptEntryId) || at > cut;
120
+ if (flight) told = undefined;
121
+ else if (prior) told = seen && prior.base === null && !prior.provisional && same(prior.briefed, now) && prior.sig === sign(wholeText(now)) ? prior.briefed : undefined;
122
+ else told = told === undefined ? base : undefined;
123
+ } else if (told === section.base && !flight) told = base;
124
+ section = { base, text: compose(base) };
100
125
  }
101
- if (section.text === undefined) delete sections[ENTRY];
102
- else sections[ENTRY] = section.text;
126
+ if (section) sections[ENTRY] = section.text;
127
+ else delete sections[ENTRY];
103
128
  // An undelivered steer was dropped or waits for this run's start: if it arrives, `delivered` weighs it against what the agent holds.
104
129
  [running, flight, rewriteKind] = [true, undefined, false];
105
130
  rebrief();
package/src/runmode.ts CHANGED
@@ -505,7 +505,7 @@ export function createRunMode(kit: Kit, look: ServerLook | undefined, deps: RunS
505
505
  });
506
506
  // A compaction folds the brief into its summary with the rest: the session is given it again, behind the summary (stored without a turn of its own; steered into the turn only when the compaction retries one).
507
507
  pi.on("session_compact", (event) => { const brief = currentBrief(); if (brief) pi.sendMessage(liveBrief(brief), event.willRetry ? { deliverAs: "steer" } : { triggerTurn: false }); });
508
- pi.on("session_tree", () => void (briefSent = undefined));
508
+ pi.on("session_tree", () => { briefSent = undefined; keep(); });
509
509
  /** A pane the server owns: this pi quits with the server's reason. */
510
510
  const quitPane = (context: ExtensionContext, reason?: string) => {
511
511
  mode = "run";
package/src/session.ts CHANGED
@@ -281,11 +281,11 @@ export function registerSession(kit: Kit, { mainDuty, runMode, briefing, roles,
281
281
  });
282
282
 
283
283
  // The tower's run starts with what happened in its workspace since its last run, and the digest is cleared as it is read.
284
- pi.on("before_agent_start", async (event) => {
284
+ pi.on("before_agent_start", async (event, ctx) => {
285
285
  if (runMode.active()) return;
286
286
  const sections = event.systemPromptOptions.sections;
287
287
  mainDuty.migrate(pi.getSessionName()); // a main's signs: its name and its train todos, read before the prompt is built
288
- briefing.begin(sections);
288
+ briefing.begin(sections, () => ctx.sessionManager.getBranch());
289
289
  if (!s.holdsTower || !sections[ENTRY]) return;
290
290
  const lines = await digest.take(s.at.workspaceId).catch(() => []);
291
291
  if (lines.length) return { message: { customType: ENTRY, content: `Since your last run:\n${lines.map((line) => `- ${line}`).join("\n")}`, display: false } };