pi-daddy 0.50.0 → 0.51.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.
Files changed (39) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/README.md +16 -2
  3. package/dist/governance/initiative-coordinator.d.ts +5 -0
  4. package/dist/governance/initiative-coordinator.d.ts.map +1 -1
  5. package/dist/governance/initiative-coordinator.js +16 -0
  6. package/dist/governance/initiative-coordinator.js.map +1 -1
  7. package/dist/products/dashboard-cli.d.ts.map +1 -1
  8. package/dist/products/dashboard-cli.js +9 -1
  9. package/dist/products/dashboard-cli.js.map +1 -1
  10. package/dist/products/dashboard-display-controls.d.ts.map +1 -1
  11. package/dist/products/dashboard-display-controls.js +43 -3
  12. package/dist/products/dashboard-display-controls.js.map +1 -1
  13. package/dist/products/dashboard-layout.d.ts +6 -0
  14. package/dist/products/dashboard-layout.d.ts.map +1 -0
  15. package/dist/products/dashboard-layout.js +45 -0
  16. package/dist/products/dashboard-layout.js.map +1 -0
  17. package/dist/products/dashboard-panels.d.ts +4 -0
  18. package/dist/products/dashboard-panels.d.ts.map +1 -1
  19. package/dist/products/dashboard-panels.js +220 -80
  20. package/dist/products/dashboard-panels.js.map +1 -1
  21. package/dist/products/dashboard-recap.d.ts +4 -0
  22. package/dist/products/dashboard-recap.d.ts.map +1 -0
  23. package/dist/products/dashboard-recap.js +47 -0
  24. package/dist/products/dashboard-recap.js.map +1 -0
  25. package/dist/products/dashboard-screen.d.ts +6 -1
  26. package/dist/products/dashboard-screen.d.ts.map +1 -1
  27. package/dist/products/dashboard-screen.js +151 -45
  28. package/dist/products/dashboard-screen.js.map +1 -1
  29. package/extensions/delegation-artifacts.ts +8 -0
  30. package/extensions/initiative-handoff.ts +59 -0
  31. package/extensions/initiative-runtime.ts +39 -1
  32. package/package.json +1 -1
  33. package/src/governance/initiative-coordinator.ts +16 -0
  34. package/src/products/dashboard-cli.ts +9 -1
  35. package/src/products/dashboard-display-controls.ts +37 -3
  36. package/src/products/dashboard-layout.ts +50 -0
  37. package/src/products/dashboard-panels.ts +261 -99
  38. package/src/products/dashboard-recap.ts +48 -0
  39. package/src/products/dashboard-screen.ts +181 -48
@@ -149,6 +149,14 @@ async function compactFrame(options: DashboardFrameOptions): Promise<string> {
149
149
  items: [],
150
150
  problem: "INCOMPATIBLE dashboard protocol; relink this package.",
151
151
  });
152
+ if (options.screen?.view === "recap")
153
+ return renderDashboardScreen({
154
+ ...options,
155
+ width: options.width ?? 80,
156
+ height: options.height!,
157
+ screen: options.screen,
158
+ items: [],
159
+ });
152
160
  try {
153
161
  const text = options.session?.observation ? await dashboardFile(path) : await readFile(path, "utf8");
154
162
  if (!options.ledgerPath || isActivityTimelineText(text)) {
@@ -218,7 +226,7 @@ async function compactFrame(options: DashboardFrameOptions): Promise<string> {
218
226
  /* The owner identity remains useful when its local timeline is unavailable. */
219
227
  }
220
228
  }
221
- if (options.activityDetail) {
229
+ if (options.activityDetail && options.screen?.view === "details") {
222
230
  try {
223
231
  const requested = aliases.resolve(options.activityDetail.taskKey) ?? options.activityDetail.taskKey;
224
232
  const key = items.find((value) => value.taskKey === requested)?.taskKey;
@@ -15,17 +15,46 @@ export function createDashboardDisplayControls(details: boolean, ledger: boolean
15
15
  const aliases = new ActivityTimelineAliases();
16
16
  const screen = dashboardScreenState();
17
17
  if (details) screen.view = "details";
18
+ let returnFromReading: Pick<typeof screen, "view" | "offset" | "manualScroll" | "selectedKey"> | undefined;
18
19
  return {
19
20
  state,
20
21
  screen,
21
22
  key(key: string): "auto" | "quit" | "command" | "setting" | undefined {
23
+ if (key === "?" || key === "r") {
24
+ const reading = key === "?" ? "help" : "recap";
25
+ if (screen.view === reading) key = "escape";
26
+ else {
27
+ returnFromReading ??= {
28
+ view: screen.view,
29
+ offset: screen.offset,
30
+ manualScroll: screen.manualScroll,
31
+ selectedKey: screen.selectedKey,
32
+ };
33
+ screen.view = reading;
34
+ screen.offset = 0;
35
+ screen.manualScroll = false;
36
+ return;
37
+ }
38
+ }
39
+ if (screen.view === "help" || screen.view === "recap") {
40
+ if (key === "escape" && returnFromReading) {
41
+ Object.assign(screen, returnFromReading);
42
+ returnFromReading = undefined;
43
+ return;
44
+ }
45
+ if (key === "return" || key === "d" || key === "tab" || key === "p" || key === "f") return;
46
+ if (/^[1-6mvs]$/.test(key)) returnFromReading = undefined;
47
+ }
22
48
  if (/^[1-6]$/.test(key)) {
23
49
  screen.selections ??= {};
24
50
  if (screen.selectedKey) screen.selections[screen.destination ?? "now"] = screen.selectedKey;
51
+ if ((screen.destination ?? "now") !== DASHBOARD_DESTINATIONS[Number(key) - 1] && state.filter)
52
+ state.filter = "everything";
25
53
  screen.destination = DASHBOARD_DESTINATIONS[Number(key) - 1];
26
54
  screen.selectedKey = screen.selections[screen.destination!];
27
55
  screen.view = screen.destination === "settings" ? "settings" : "main";
28
56
  screen.offset = 0;
57
+ screen.manualScroll = false;
29
58
  state.activityDetail = undefined;
30
59
  return;
31
60
  }
@@ -35,7 +64,10 @@ export function createDashboardDisplayControls(details: boolean, ledger: boolean
35
64
  return;
36
65
  }
37
66
  if (key === "x") {
38
- screen.expanded = !screen.expanded;
67
+ if (screen.view === "details") {
68
+ screen.sourceDetails = !screen.sourceDetails;
69
+ screen.offset = 0;
70
+ } else if (screen.view === "main") screen.expanded = !screen.expanded;
39
71
  return;
40
72
  }
41
73
  if (key === "a") return "auto";
@@ -47,13 +79,15 @@ export function createDashboardDisplayControls(details: boolean, ledger: boolean
47
79
  if (screen.view === "main" && screen.destination === "settings") screen.destination = "now";
48
80
  state.activityDetail = undefined;
49
81
  screen.offset = 0;
50
- } else if (key === "?" || key === "m" || key === "v" || key === "s") {
51
- screen.view = key === "?" ? "help" : key === "v" ? "versions" : key === "s" ? "settings" : "models";
82
+ } else if (key === "m" || key === "v" || key === "s") {
83
+ screen.view = key === "v" ? "versions" : key === "s" ? "settings" : "models";
52
84
  if (key === "s") screen.destination = "settings";
53
85
  state.activityDetail = undefined;
54
86
  screen.offset = 0;
55
87
  } else if (key === "return" || key === "d") {
88
+ if (screen.view !== "versions" && (screen.view !== "main" || !screen.selectedKey)) return;
56
89
  screen.view = screen.view === "versions" ? "version-details" : "details";
90
+ screen.sourceDetails = false;
57
91
  screen.offset = 0;
58
92
  } else if (["up", "down", "j", "k"].includes(key)) {
59
93
  screen.manualScroll = false;
@@ -0,0 +1,50 @@
1
+ import { cellWidth, truncate } from "./dashboard-render.ts";
2
+
3
+ /** Keep related content together on wide terminals without filling the entire row with decoration. */
4
+ export function sectionHeading(title: string, width: number): string {
5
+ const remaining = Math.min(width, 100) - cellWidth(title) - 2;
6
+ return remaining >= 3 ? title + " " + "─".repeat(remaining) : truncate(title, width);
7
+ }
8
+
9
+ export function previewWidth(destination: string, expanded: boolean, width: number): number | undefined {
10
+ if (width < 96 || (!expanded && (width < 110 || !["now", "work", "reviews"].includes(destination)))) return;
11
+ return Math.min(56, Math.floor(width * 0.45));
12
+ }
13
+
14
+ const sgr = (value: string, code: string) => (value ? `\x1b[${code}m${value}\x1b[0m` : "");
15
+
16
+ /** Apply terminal styles only to already-sanitized and measured display text. */
17
+ export function colorSessionLine(line: string, role: "header" | "navigation" | "footer" | "body"): string {
18
+ if (role === "header") {
19
+ const parts = /^(PI DADDY)(.*?)( · Connected)?$/.exec(line);
20
+ return parts ? sgr(parts[1]!, "1;36") + parts[2] + sgr(parts[3] ?? "", "32") : line;
21
+ }
22
+ if (role === "navigation")
23
+ return line.includes("[") ? line.replace(/\[[^\]]+\]/, (s) => sgr(s, "1;7")) : sgr(line, "1");
24
+ if (role === "footer")
25
+ return line.replace(
26
+ /(^| · )(↑↓|Enter|Esc|PgUp\/PgDn|1–6|[?rqxpfs])/g,
27
+ (_, space: string, key: string) => space + sgr(key, "1;36"),
28
+ );
29
+ const divider = line.indexOf(" │ ");
30
+ if (divider >= 0) return paintCell(line.slice(0, divider)) + sgr(" │ ", "36") + paintCell(line.slice(divider + 3));
31
+ return paintCell(line);
32
+ }
33
+
34
+ function paintCell(cell: string): string {
35
+ const text = cell.trimEnd();
36
+ const padding = cell.slice(text.length);
37
+ if (text.startsWith("> ")) return sgr(text, "1;7") + padding;
38
+ const rule = /^(.*?) (─{3,})$/.exec(text);
39
+ if (rule) {
40
+ const title = rule[1]!;
41
+ const attention =
42
+ /^(?:SESSION PRIORITY|YOUR DECISIONS|PERMISSION REQUESTS|NEXT ACTION|NEEDS ATTENTION|AWAITING REVIEW)/.test(
43
+ title,
44
+ );
45
+ return sgr(title, attention ? "1;33" : "1;36") + " " + sgr(rule[2]!, "36") + padding;
46
+ }
47
+ if (text === "/bookie-work review") return sgr(text, "1;36") + padding;
48
+ if (/^(?:!|LAST OBSERVED|Disconnected)/.test(text)) return sgr(text, "1;33") + padding;
49
+ return cell;
50
+ }
@@ -1,9 +1,9 @@
1
1
  /** Six current-session destinations share stable row identities; contributions are labels, never authority. */
2
- import type { DashboardContribution } from "../kernel/dashboard-observation.ts";
2
+ import type { DashboardContribution, DashboardSourceSnapshot } from "../kernel/dashboard-observation.ts";
3
3
  import type { DashboardItem, DashboardScreenOptions } from "./dashboard-screen.ts";
4
- import { dashboardText } from "./dashboard-screen.ts";
4
+ import { dashboardText, friendlyStatus } from "./dashboard-screen.ts";
5
+ import { previewWidth, sectionHeading } from "./dashboard-layout.ts";
5
6
  import { activityTaskKey } from "./activity-timeline.ts";
6
- import { truncate, cellWidth } from "./dashboard-render.ts";
7
7
  export type DashboardDestination = "now" | "work" | "reviews" | "activity" | "evidence" | "settings";
8
8
  export const DASHBOARD_DESTINATIONS: DashboardDestination[] = [
9
9
  "now",
@@ -14,27 +14,194 @@ export const DASHBOARD_DESTINATIONS: DashboardDestination[] = [
14
14
  "settings",
15
15
  ];
16
16
  const clean = (s: string) => dashboardText(s).replace(/\s+/g, " ").trim();
17
- const row = (item: DashboardContribution, key: string, source: string, stale = false): DashboardItem => ({
18
- key,
19
- label: item.label,
20
- status: stale ? "last-observed" : item.state,
21
- elapsed: "",
22
- agent: item.kind === "work",
23
- details: [
24
- `Source: ${source}`,
25
- `Observed state: ${item.state}${stale ? " (stale/disconnected)" : ""}`,
26
- ...(item.details ?? []),
27
- ...(item.reference ? [`Reference: ${item.reference}`] : []),
28
- ...(item.kind === "review" ? ["A reported judgment is not execution authority or overall task acceptance."] : []),
29
- "Reported observations do not grant execution authority or task approval.",
30
- ...(stale ? ["Current liveness/applicability unknown; no automatic retry."] : []),
31
- ],
32
- });
17
+ export function staleSource(source: DashboardSourceSnapshot, options: DashboardScreenOptions): boolean {
18
+ return (
19
+ options.connected === false ||
20
+ source.availability === "closed" ||
21
+ (options.now?.getTime() ?? Date.now()) - Date.parse(source.observedAt) > source.staleAfterMs
22
+ );
23
+ }
24
+
25
+ const sourceKey = (source: DashboardSourceSnapshot, item: DashboardContribution) =>
26
+ `source:${source.producer}:${source.generation}:${item.id}`;
27
+
28
+ /** A current request for review takes precedence over general workflow suggestions. */
29
+ function sessionNext(options: DashboardScreenOptions) {
30
+ const current = options.session!.observation!.sources.filter((source) => !staleSource(source, options));
31
+ const recommendations = current.flatMap((source) =>
32
+ source.items.filter((item) => item.kind === "next").map((item) => ({ item, source })),
33
+ );
34
+ for (const next of recommendations) {
35
+ if (next.item.state !== "approval-wait") continue;
36
+ const reviews = next.source.items.filter(
37
+ (item) =>
38
+ item.kind === "review" &&
39
+ item.state === "approval-wait" &&
40
+ !(next.source.producer === "bookie" && reviewTotals(item)) &&
41
+ (!next.item.reference || item.reference === next.item.reference),
42
+ );
43
+ if (reviews.length === 1) return { ...next, reviewKey: sourceKey(next.source, reviews[0]!) };
44
+ }
45
+ const general = recommendations.filter(({ item }) => item.state !== "approval-wait" || !item.reference);
46
+ const next =
47
+ general.find(({ item }) => ["dependency-wait", "failed", "unknown", "unavailable"].includes(item.state)) ??
48
+ general[0];
49
+ return next && { ...next, reviewKey: undefined };
50
+ }
51
+
52
+ export function sessionNextLines(options: DashboardScreenOptions): [string, string] {
53
+ if (options.connected === false) return ["NEXT", "Reconnect to the parent Pi session for current work."];
54
+ if (options.session?.pendingApprovals.length)
55
+ return ["NEXT · Permission", "Respond to the permission request in the parent Pi session."];
56
+ const next = sessionNext(options);
57
+ if (!next) {
58
+ const sources = options.session!.observation!.sources;
59
+ if (sources.length && sources.every((source) => staleSource(source, options)))
60
+ return ["NEXT", "Waiting for a current status from Pi."];
61
+ }
62
+ return [
63
+ next?.reviewKey ? "NEXT · [3] Reviews" : "NEXT",
64
+ next ? clean(next.item.label) : "No current next step reported.",
65
+ ];
66
+ }
67
+ function contributionDetails(item: DashboardContribution, source: string, stale: boolean): string[] {
68
+ const details = item.details ?? [];
69
+ if (!source.startsWith("bookie@") || item.kind !== "review" || item.state !== "approval-wait") return details;
70
+ const open = details.find((line) => line.startsWith("Open /bookie-work review"));
71
+ if (stale)
72
+ return details.filter(
73
+ (line) => !line.startsWith("Open /bookie-work review") && !line.startsWith("Record your review decision"),
74
+ );
75
+ return open ? [open, ...details.filter((line) => line !== open)] : details;
76
+ }
77
+ const row = (
78
+ item: DashboardContribution,
79
+ key: string,
80
+ source: string,
81
+ stale = false,
82
+ label = item.label,
83
+ taskReview?: DashboardContribution,
84
+ ): DashboardItem => {
85
+ const details = contributionDetails(item, source, stale);
86
+ const handoff = taskReview ?? item;
87
+ const pendingTask = source.startsWith("bookie@") && item.kind === "work" && item.state === "approval-wait";
88
+ const currentHandoff =
89
+ !stale &&
90
+ source.startsWith("bookie@") &&
91
+ handoff.kind === "review" &&
92
+ handoff.state === "approval-wait" &&
93
+ !reviewTotals(handoff);
94
+ const brief = currentHandoff
95
+ ? contributionDetails(handoff, source, stale).filter(
96
+ (line) =>
97
+ !/^(?:\/|[A-Za-z]:[\\/]|(?:Reference|Source|Observed at):)/i.test(line) &&
98
+ !/\b[a-f0-9]{64}\b/i.test(line) &&
99
+ line !== "Output: handoff",
100
+ )
101
+ : [];
102
+ const openInstruction = brief.indexOf("Open /bookie-work review to read the saved handoff.");
103
+ if (openInstruction >= 0) {
104
+ const choice = taskReview && (label === item.label ? label : item.label.split("/").at(-1));
105
+ brief.splice(
106
+ openInstruction,
107
+ 1,
108
+ "In your main Pi chat, run",
109
+ "/bookie-work review",
110
+ ...(choice ? [`If asked, select ${choice}.`] : []),
111
+ );
112
+ const decision = brief.indexOf("Record your review decision for this output in the selected project.");
113
+ if (decision >= 0) brief[decision] = "Read the output and record your decision.";
114
+ }
115
+ if (currentHandoff && !brief.length) brief.push("Awaiting a review decision for this output.");
116
+ if (!currentHandoff && pendingTask)
117
+ brief.push(
118
+ stale
119
+ ? "Last seen awaiting a decision. Check the parent Pi session for current status."
120
+ : "A decision is pending. Check the selected task in the parent Pi session for its review evidence.",
121
+ );
122
+ return {
123
+ key,
124
+ label,
125
+ status: stale ? "last-observed" : item.state,
126
+ elapsed: "",
127
+ agent: item.kind === "work",
128
+ ...(source.startsWith("bookie@") && reviewTotals(item) ? { reviewSummary: true } : {}),
129
+ ...(brief.length ? { briefDetails: brief } : {}),
130
+ details: [
131
+ ...(stale ? ["Last recorded details. Current state is unknown."] : []),
132
+ ...details,
133
+ ...(label !== item.label ? [item.label] : []),
134
+ `Observed state: ${item.state}${stale ? " (stale/disconnected)" : ""}`,
135
+ `Source: ${source}`,
136
+ ...(item.reference ? [`Reference: ${item.reference}`] : []),
137
+ ...(taskReview?.reference ? [`Saved handoff reference: ${taskReview.reference}`] : []),
138
+ ...(item.kind === "review"
139
+ ? [
140
+ item.state === "approval-wait"
141
+ ? "The reviewer decides on this output."
142
+ : "A reported judgment is not execution authority or overall task acceptance.",
143
+ ]
144
+ : []),
145
+ "Reported observations do not grant execution authority or task approval.",
146
+ ...(stale ? ["Current liveness/applicability unknown; no automatic retry."] : []),
147
+ ],
148
+ };
149
+ };
150
+
151
+ const reviewTotals = (item: DashboardContribution) =>
152
+ item.kind === "review" && item.id === "reviews" && item.label === "Selected output judgment observations";
153
+
154
+ // Bookie publishes work with its task revision and the corresponding review with its output digest.
155
+ // Only the task ID within this one observation links the two records.
156
+ function reviewForTask(item: DashboardContribution, source: DashboardSourceSnapshot, stale: boolean) {
157
+ if (
158
+ stale ||
159
+ source.producer !== "bookie" ||
160
+ item.kind !== "work" ||
161
+ item.state !== "approval-wait" ||
162
+ !item.reference
163
+ )
164
+ return;
165
+ const matches = source.items.filter(
166
+ (candidate) =>
167
+ candidate.id === `review-${item.id}` &&
168
+ candidate.kind === "review" &&
169
+ candidate.state === "approval-wait" &&
170
+ candidate.reference,
171
+ );
172
+ return matches.length === 1 ? matches[0] : undefined;
173
+ }
174
+
175
+ // A Bookie task can publish its canonical document path as its only label.
176
+ // Abbreviate unambiguous sibling names while keeping the full path in Details.
177
+ function bookieLabel(item: DashboardContribution, source: DashboardSourceSnapshot): string {
178
+ if (reviewTotals(item)) return "Review totals";
179
+ if (item.kind !== "work" || !item.label.startsWith("/")) return item.label;
180
+ const name = (label: string) => label.split("/").at(-1)?.replace(/\.md$/i, "").replace(/[-_]+/g, " ");
181
+ const title = name(item.label);
182
+ if (
183
+ !title ||
184
+ !item.label.endsWith(".md") ||
185
+ source.items.some((other) => other.kind === "work" && other.id !== item.id && name(other.label) === title)
186
+ )
187
+ return item.label;
188
+ return title;
189
+ }
190
+
191
+ function rowText(item: DashboardItem, destination: DashboardDestination, selected: boolean): string {
192
+ const label = clean(item.label);
193
+ const marker = selected ? "> " : " ";
194
+ if (item.status === "last-observed") return `${marker}Last seen · ${label}`;
195
+ if (item.reviewSummary) return marker + label;
196
+ const waitingForReview = destination === "reviews" && item.status === "approval-wait";
197
+ const described = waitingForReview && /^ready for review[:\s]/i.test(label);
198
+ const status = described ? "" : ` · ${waitingForReview ? "Awaiting review" : friendlyStatus(clean(item.status))}`;
199
+ return `${marker}${label}${status}${item.elapsed ? " " + item.elapsed : ""}`;
200
+ }
33
201
  export function sessionDashboardItems(options: DashboardScreenOptions): DashboardItem[] {
34
202
  const observed = options.session!.observation!;
35
203
  const destination = options.screen.destination ?? "now";
36
204
  const staleOwner = options.connected === false;
37
- const now = options.now?.getTime() ?? Date.now();
38
205
  const wanted =
39
206
  destination === "work"
40
207
  ? ["work"]
@@ -50,18 +217,33 @@ export function sessionDashboardItems(options: DashboardScreenOptions): Dashboar
50
217
  .filter(
51
218
  (item) =>
52
219
  wanted.includes(item.kind) &&
220
+ !(
221
+ destination === "reviews" &&
222
+ !options.screen.expanded &&
223
+ source.producer === "bookie" &&
224
+ reviewTotals(item) &&
225
+ source.items.some((other) => other.kind === "review" && other.id !== item.id)
226
+ ) &&
53
227
  !(source.producer === "daddy-initiative" && item.id === "scope" && ["now", "work"].includes(destination)),
54
228
  )
55
- .map((item) =>
56
- row(
229
+ .map((item) => {
230
+ const stale = staleSource(source, options);
231
+ return row(
57
232
  item,
58
- `source:${source.producer}:${source.generation}:${item.id}`,
233
+ sourceKey(source, item),
59
234
  `${source.producer}@${source.producerVersion} · seq ${source.sequence} · ${source.observedAt}`,
60
- staleOwner || source.availability === "closed" || now - Date.parse(source.observedAt) > source.staleAfterMs,
61
- ),
62
- ),
235
+ stale,
236
+ source.producer === "bookie" ? bookieLabel(item, source) : item.label,
237
+ reviewForTask(item, source, stale),
238
+ );
239
+ }),
63
240
  );
64
- if (destination === "reviews") return published;
241
+ if (destination === "reviews") {
242
+ const first = sessionNext(options)?.reviewKey;
243
+ const priority = (item: DashboardItem) =>
244
+ item.key === first ? 2 : Number(item.status === "approval-wait" && !item.reviewSummary);
245
+ return published.sort((a, b) => priority(b) - priority(a));
246
+ }
65
247
  if (destination === "evidence")
66
248
  return [
67
249
  ...(observed.artifacts ?? []).map((a) => ({
@@ -101,110 +283,90 @@ export function sessionDashboardItems(options: DashboardScreenOptions): Dashboar
101
283
  const activity = options.session?.activity;
102
284
  const parentShown =
103
285
  activity && native.some((item) => item.taskKey === activityTaskKey(activity.rootId, activity.taskId));
104
- return [
286
+ const items = [
105
287
  ...(observed.turn && !parentShown
106
288
  ? [row(observed.turn, `owner:${observed.generation}:${observed.turn.id}`, "Pi owner", staleOwner)]
107
289
  : []),
108
290
  ...native,
109
291
  ...published,
110
292
  ];
293
+ items.sort((a, b) => workGroups.indexOf(workGroup(a)) - workGroups.indexOf(workGroup(b)));
294
+ return items;
295
+ }
296
+
297
+ const workGroups = ["PERMISSION REQUESTS", "YOUR DECISIONS", "NEEDS ATTENTION", "IN PROGRESS", "OTHER WORK", "HISTORY"];
298
+ function workGroup(item: DashboardItem): string {
299
+ if (item.approval) return "PERMISSION REQUESTS";
300
+ if (item.status === "approval-wait") return "YOUR DECISIONS";
301
+ if (["needs-you", "failed", "refused", "incomplete", "dependency-wait"].includes(item.status))
302
+ return "NEEDS ATTENTION";
303
+ if (["running", "active", "starting", "authorised", "cancel-requested"].includes(item.status)) return "IN PROGRESS";
304
+ if (["settled", "completed", "finished", "cancelled", "historical", "last-observed"].includes(item.status))
305
+ return "HISTORY";
306
+ return "OTHER WORK";
307
+ }
308
+
309
+ function listHeading(item: DashboardItem, destination: DashboardDestination) {
310
+ if (destination === "now" || destination === "work") return workGroup(item);
311
+ if (destination === "reviews")
312
+ return item.status === "approval-wait" && !item.reviewSummary ? "AWAITING REVIEW" : "REVIEW RECORDS";
313
+ return destination === "evidence" ? "SAVED REFERENCES" : "SESSION ACTIVITY";
111
314
  }
112
315
  export function sessionPanelLines(options: DashboardScreenOptions, items: DashboardItem[], width: number): string[] {
113
316
  const observation = options.session!.observation!,
114
317
  screen = options.screen;
115
318
  const destination = screen.destination ?? "now";
116
319
  const sourceRows = observation.sources.flatMap((s) => s.items.map((item) => ({ source: s, item })));
117
- const lines: string[] = [`${destination.toUpperCase()} · CURRENT SESSION`];
118
- const staleSource = (source: (typeof observation.sources)[number]) =>
119
- options.connected === false ||
120
- source.availability === "closed" ||
121
- (options.now?.getTime() ?? Date.now()) - Date.parse(source.observedAt) > source.staleAfterMs;
320
+ const lines: string[] = [];
122
321
  const initiative = observation.sources.find((source) => source.producer === "daddy-initiative");
123
322
  const scope = initiative?.items.find((item) => item.id === "scope");
124
323
  if (scope && (destination === "now" || destination === "work")) {
125
- lines.push(`SELECTED ${scope.label}${staleSource(initiative!) ? " (last observed)" : ""}`);
324
+ lines.push(`SELECTED ${scope.label}${staleSource(initiative!, options) ? " (last observed)" : ""}`);
126
325
  if (destination === "work" && scope.details?.[1]) lines.push(scope.details[1]);
127
326
  }
128
327
  if (destination === "now") {
129
- const goal = sourceRows.find((x) => x.item.kind === "goal");
130
- lines.push(
131
- "GOAL",
132
- goal
133
- ? `${goal.item.label} (${goal.source.producer}${staleSource(goal.source) ? "; stale" : ""})`
134
- : (options.current ?? "Goal not reported; no inference from a finished step."),
135
- );
136
- const next = sourceRows.find((x) => x.item.kind === "next");
137
- const nextStale =
138
- next &&
139
- (options.connected === false ||
140
- next.source.availability === "closed" ||
141
- (options.now?.getTime() ?? Date.now()) - Date.parse(next.source.observedAt) > next.source.staleAfterMs);
142
- lines.push(
143
- "NEXT",
144
- next
145
- ? `${next.item.label} (${next.source.producer}${nextStale ? "; stale" : ""})`
146
- : "No coordinator recommendation reported.",
147
- );
148
- const attention = items.filter((x) =>
149
- [
150
- "failed",
151
- "refused",
152
- "incomplete",
153
- "unknown",
154
- "unavailable",
155
- "needs-you",
156
- "approval-wait",
157
- "dependency-wait",
158
- "cancel-requested",
159
- ].includes(x.status),
160
- );
161
- if (attention.length) {
162
- lines.push(`ATTENTION · ${attention.length}`);
163
- for (const item of attention.slice(0, 3)) lines.push(`${item.label} · ${item.status}`);
164
- }
165
- const result = [...items].reverse().find((x) => ["settled", "finished", "completed"].includes(x.status));
166
- if (result) lines.push(`LATEST UPDATE · ${result.label} (${result.status})`);
328
+ const goals = sourceRows.filter((x) => x.item.kind === "goal");
329
+ const goal = goals.find((x) => !staleSource(x.source, options)) ?? goals[0];
330
+ if (goal) {
331
+ const name =
332
+ !screen.expanded && goal.source.producer === "bookie"
333
+ ? goal.item.label.replace(/^\/projects\/([^/]+)\/project\.md$/, "$1")
334
+ : goal.item.label;
335
+ lines.push(
336
+ `Goal · ${clean(name)} (${goal.source.producer}${staleSource(goal.source, options) ? "; stale" : ""})`,
337
+ );
338
+ } else if (options.current) lines.push(`Goal · ${clean(options.current)}`);
167
339
  }
168
340
 
169
341
  if (!items.length)
170
342
  lines.push(
171
343
  destination === "reviews"
172
- ? "No review producer observations available."
344
+ ? options.connected === false || observation.sources.some((s) => staleSource(s, options))
345
+ ? "Current reviews unknown. Check the parent Pi session."
346
+ : "No reviews reported. [2] Work shows task progress."
173
347
  : destination === "evidence"
174
348
  ? "No current-session result references observed."
175
349
  : "No observations for this view.",
176
350
  );
177
- const selected = items.find((x) => x.key === screen.selectedKey);
178
- const rows = items.map(
179
- (x) =>
180
- `${x.key === screen.selectedKey ? ">" : " "} ${clean(x.label)} · ${clean(x.status)}${x.elapsed ? " " + x.elapsed : ""}`,
181
- );
182
- const expanded = screen.expanded && width >= 96;
183
- if (expanded && rows.length) {
184
- const left = Math.floor(width * 0.45),
185
- right = width - left - 3;
186
- const details = [selected ? `DETAIL · ${clean(selected.label)}` : "DETAIL", ...(selected?.details ?? [])];
187
- const length = Math.max(rows.length, details.length);
188
- for (let i = 0; i < length; i++) {
189
- const a = truncate(clean(rows[i] ?? ""), left);
190
- lines.push(a + " ".repeat(Math.max(0, left - cellWidth(a))) + " | " + truncate(clean(details[i] ?? ""), right));
191
- }
192
- } else {
193
- lines.push(...rows);
194
- if (screen.expanded) lines.push("Expanded layout needs 96 columns; same selection retained.");
351
+ const left = previewWidth(destination, Boolean(screen.expanded), width);
352
+ const rows: string[] = [];
353
+ for (let start = 0; start < items.length;) {
354
+ const group = listHeading(items[start]!, destination);
355
+ let end = start + 1;
356
+ while (end < items.length && listHeading(items[end]!, destination) === group) end++;
357
+ rows.push(sectionHeading(`${group} · ${end - start}`, left ?? width));
358
+ for (const item of items.slice(start, end)) rows.push(rowText(item, destination, item.key === screen.selectedKey));
359
+ start = end;
195
360
  }
196
- if (destination === "now") {
197
- lines.push("COMPONENTS");
361
+ lines.push(...rows);
362
+ if (screen.expanded && left === undefined) lines.push("Expanded layout needs 96 columns; same selection retained.");
363
+ if (destination === "now" && screen.expanded) {
364
+ lines.push(sectionHeading("COMPONENTS", width));
198
365
  for (const producer of ["principal", "bookie", "scout", "skill-harness"]) {
199
366
  const streams = observation.sources.filter(
200
367
  (x) => x.producer === producer || (producer === "scout" && x.producer.startsWith("scout-")),
201
368
  );
202
- const stale = streams.filter(
203
- (s) =>
204
- options.connected === false ||
205
- s.availability === "closed" ||
206
- (options.now?.getTime() ?? Date.now()) - Date.parse(s.observedAt) > s.staleAfterMs,
207
- ).length;
369
+ const stale = streams.filter((s) => staleSource(s, options)).length;
208
370
  const status = !streams.length
209
371
  ? "status adapter unavailable"
210
372
  : `${streams.length - stale} observed${stale ? `; ${stale} last observed / stale` : ""}`;
@@ -0,0 +1,48 @@
1
+ import type { DashboardContribution } from "../kernel/dashboard-observation.ts";
2
+ import { staleSource } from "./dashboard-panels.ts";
3
+ import { truncate } from "./dashboard-render.ts";
4
+ import { dashboardText, type DashboardScreenOptions } from "./dashboard-screen.ts";
5
+
6
+ function eventLine(event: DashboardContribution): string {
7
+ const label = dashboardText(event.label).replace(/\s+/g, " ").trim();
8
+ if (label === "agent_start") return "Parent turn started";
9
+ if (label === "agent_end") return "Parent turn ended";
10
+ const tool = /^(.*): tool (start|end)$/.exec(label);
11
+ const name = truncate(tool?.[1] || label || "Event", 80);
12
+ if (event.state === "failed") return `${name} failed`;
13
+ return tool ? `${name} ${tool[2] === "start" ? "started" : "finished"}` : name;
14
+ }
15
+
16
+ /** Current owner hook events, newest first. Producer snapshots can describe current state only. */
17
+ export function sessionRecapLines(options: DashboardScreenOptions): string[] {
18
+ const session = options.session;
19
+ const observed = session?.observation;
20
+ if (!session || !observed)
21
+ return [
22
+ options.connected === false
23
+ ? "Disconnected · no observed session activity."
24
+ : "No session activity has been observed.",
25
+ ];
26
+
27
+ const lines: string[] = [];
28
+ if (options.connected === false) {
29
+ lines.push("Disconnected · last observed activity");
30
+ } else {
31
+ const work = observed.sources.find((source) => source.producer === "bookie" && !staleSource(source, options));
32
+ const ready = work?.items.filter(
33
+ (item) => item.kind === "review" && item.id !== "reviews" && item.state === "approval-wait" && item.reference,
34
+ ).length;
35
+ const waiting = work?.items.filter((item) => item.kind === "work" && item.state === "dependency-wait").length;
36
+ if (session.pendingApprovals.length) lines.push(`Pending permissions: ${session.pendingApprovals.length}`);
37
+ if (ready) lines.push(`Ready for review · ${ready} shown by Bookie`);
38
+ if (waiting) lines.push(`Waiting on a dependency · ${waiting} shown by Bookie`);
39
+ }
40
+
41
+ const recent = (observed.activity ?? []).slice(-5).reverse();
42
+ lines.push(
43
+ ...(recent.length
44
+ ? ["Recent activity · newest first", ...recent.map(eventLine)]
45
+ : ["No recent session events recorded."]),
46
+ );
47
+ return lines;
48
+ }