pi-straw-boss 0.81.1 → 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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/briefing.ts +34 -12
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-straw-boss",
3
- "version": "0.81.1",
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,3 +1,4 @@
1
+ import { createHash } from "node:crypto";
1
2
  import type { SessionEntry } from "@earendil-works/pi-coding-agent";
2
3
  import type { Graph } from "./duty.ts";
3
4
  import type { MainDuty } from "./mainduty.ts";
@@ -5,15 +6,18 @@ import { cliCommand } from "./server/version.ts";
5
6
  import { ENTRY, type Kit } from "./state.ts";
6
7
  import { type PromptSet, promptSet } from "./trainswitch.ts";
7
8
 
9
+ const LABEL_GRACE_MS = 10_000;
10
+ const LOCATION_LABELS = ["tab", "workspace"] as const;
8
11
  const PROMPTS = ["main", "tower", "member", "member-no-main", "train", "integrator", "dispatch", "duty-standing", "graph-fan-out", "graph-orchestrator-worker"];
9
12
  /** The addendum a graph adds to the dispatch rules: `single-loop` adds none (docs/specs/2026-11-09-agent-kinds.md §2a). */
10
13
  const GRAPH_ADDENDUM: Partial<Record<Graph, string>> = { "fan-out/fan-in": "graph-fan-out", "orchestrator-worker": "graph-orchestrator-worker" };
11
14
 
12
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). */
13
16
  interface Briefed { name: string | undefined; duty: string; graph: Graph | "" }
14
- /** A steer's `details`: what it tells, and what it was written against (`null`: it replaces the whole role prompt, whatever the model held). */
15
- 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 }
16
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);
17
21
 
18
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. */
19
23
  export function createBriefing({ pi, cfg, s, role, guiDir, tell }: Kit, mainDuty: MainDuty) {
@@ -29,30 +33,40 @@ export function createBriefing({ pi, cfg, s, role, guiDir, tell }: Kit, mainDuty
29
33
  const current = role(), duty = current === "main" ? mainDuty.now() : "";
30
34
  return { name: current === "member" && !s.mainId ? "member-no-main" : current, duty, graph: duty === "coordinator" ? (mainDuty.graph() ?? "") : "" };
31
35
  };
32
- // A rewritten section invalidates the system prompt cache. Pin the rendered bytes at the first run with a role, 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.
33
37
  // The section never contains a coordinator's graph. Role and graph changes, including ones on a different branch, reach the agent as messages.
34
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).
35
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
36
40
  // own was written against the older state: `told` is unknown (`undefined`) then, and the next steer is the whole prompt.
37
41
  let idle = () => false, told: Briefed | undefined, flight: Told | undefined, running = false, rewriteKind = false;
38
- let section: { base: Briefed; text: string } | undefined, labels: typeof s.info | undefined;
42
+ let section: { base: Briefed; text: string } | undefined, labels: typeof s.info | undefined, labelDeadline: number | undefined;
39
43
  // Keep the first role's labels for custom templates too. A steer before pinning uses the same fields, read live.
40
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
+ };
41
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");
42
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).
43
55
  let quiet = false;
44
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
45
57
  // that failed to start past `before_agent_start`): pi stores it at once (`_appendCustomMessage`), with no `message_end`.
46
- const steer = (text: string, base: Briefed | null, briefed: Briefed) => {
47
- 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 } : {}) };
48
60
  pi.sendMessage({ customType: ENTRY, content: `<straw-boss>\n${text}\n</straw-boss>`, display: true, details }, quiet ? { triggerTurn: false } : { deliverAs: "steer" });
49
61
  if (quiet || idle()) told = briefed;
50
62
  else flight = details;
51
63
  };
64
+ /** The role prompt a whole steer carries for `now`. */
65
+ const wholeText = (now: Briefed) => compose(now) || "No Straw Boss role applies.";
52
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). */
53
67
  const steerChange = (was: Briefed | undefined, now: Briefed) => {
54
68
  if ((was && same(was, now)) || (!now.name && !section?.base.name && !was?.name)) return;
55
- 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));
56
70
  if (!was) return whole("Some of your Straw Boss role updates may be missing or out of order.");
57
71
  if (now.name !== was.name) return whole("Your Straw Boss role changed.");
58
72
  if (now.duty !== was.duty) return whole(`Your kind is now ${now.duty}.`);
@@ -70,7 +84,7 @@ export function createBriefing({ pi, cfg, s, role, guiDir, tell }: Kit, mainDuty
70
84
  idle = isIdle;
71
85
  files = promptSet({ promptDir: cfg.promptDir, stateDir: guiDir, env: cfg.env, warn: (text) => tell(text, "warning") });
72
86
  prompts = Object.fromEntries(PROMPTS.map((name) => [name, files!.read(name)]));
73
- [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];
74
88
  },
75
89
  /** Whether the loaded prompts have integration off; before they are loaded, the switch is read as `promptSet` will read it. */
76
90
  off: () => files?.off,
@@ -82,7 +96,7 @@ export function createBriefing({ pi, cfg, s, role, guiDir, tell }: Kit, mainDuty
82
96
  delivered(message: { role?: string; customType?: string; details?: unknown }) {
83
97
  const d = message.details as Told | undefined;
84
98
  if (message.role !== "custom" || message.customType !== ENTRY || !d?.briefed) return;
85
- 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))));
86
100
  [told, flight] = [fits ? d.briefed : undefined, flight === d ? undefined : flight];
87
101
  rebrief();
88
102
  },
@@ -94,11 +108,19 @@ export function createBriefing({ pi, cfg, s, role, guiDir, tell }: Kit, mainDuty
94
108
  },
95
109
  begin(sections: Record<string, string>, branch: () => readonly SessionEntry[]) {
96
110
  const now = stateOf();
97
- if (now.name && (!section || (rewriteKind && now.name === "main" && section.base.duty !== now.duty))) {
111
+ if (now.name && (section ? rewriteKind && now.name === "main" && section.base.duty !== now.duty : readyToPin(now.name, now.duty))) {
98
112
  labels ??= labelsOf();
99
113
  const base: Briefed = { ...now, graph: "" };
100
- // On the first pin, an earlier instance's steer may still override the section. Leave `told` unknown so we replace it in a message.
101
- if (told === section?.base && !flight && (section || !branch().some((e) => e.type === "custom_message" && e.customType === ENTRY && (e.details as Partial<Told> | undefined)?.briefed))) told = base;
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;
102
124
  section = { base, text: compose(base) };
103
125
  }
104
126
  if (section) sections[ENTRY] = section.text;