pi-daddy 0.55.0 → 0.56.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 (41) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/README.md +1 -1
  3. package/dist/kernel/dashboard-choice.d.ts +58 -0
  4. package/dist/kernel/dashboard-choice.d.ts.map +1 -0
  5. package/dist/kernel/dashboard-choice.js +34 -0
  6. package/dist/kernel/dashboard-choice.js.map +1 -0
  7. package/dist/products/dashboard-choice-picker.d.ts +22 -0
  8. package/dist/products/dashboard-choice-picker.d.ts.map +1 -0
  9. package/dist/products/dashboard-choice-picker.js +219 -0
  10. package/dist/products/dashboard-choice-picker.js.map +1 -0
  11. package/dist/products/dashboard-cli.d.ts.map +1 -1
  12. package/dist/products/dashboard-cli.js +16 -4
  13. package/dist/products/dashboard-cli.js.map +1 -1
  14. package/dist/products/dashboard-components.d.ts.map +1 -1
  15. package/dist/products/dashboard-components.js +21 -1
  16. package/dist/products/dashboard-components.js.map +1 -1
  17. package/dist/products/dashboard-display-controls.d.ts +1 -1
  18. package/dist/products/dashboard-display-controls.d.ts.map +1 -1
  19. package/dist/products/dashboard-display-controls.js +6 -1
  20. package/dist/products/dashboard-display-controls.js.map +1 -1
  21. package/dist/products/dashboard-screen.d.ts +5 -1
  22. package/dist/products/dashboard-screen.d.ts.map +1 -1
  23. package/dist/products/dashboard-screen.js +17 -6
  24. package/dist/products/dashboard-screen.js.map +1 -1
  25. package/dist/products/dashboard-session-client.d.ts +18 -5
  26. package/dist/products/dashboard-session-client.d.ts.map +1 -1
  27. package/dist/products/dashboard-session-client.js +92 -14
  28. package/dist/products/dashboard-session-client.js.map +1 -1
  29. package/extensions/dashboard-choice-controls.ts +152 -0
  30. package/extensions/dashboard-session-server.ts +92 -17
  31. package/extensions/dashboard-sources.ts +12 -0
  32. package/extensions/runtime-snapshot.ts +1 -0
  33. package/extensions/session.ts +1 -0
  34. package/package.json +1 -1
  35. package/src/kernel/dashboard-choice.ts +73 -0
  36. package/src/products/dashboard-choice-picker.ts +228 -0
  37. package/src/products/dashboard-cli.ts +18 -3
  38. package/src/products/dashboard-components.ts +21 -1
  39. package/src/products/dashboard-display-controls.ts +7 -1
  40. package/src/products/dashboard-screen.ts +19 -6
  41. package/src/products/dashboard-session-client.ts +129 -19
@@ -1,3 +1,9 @@
1
+ import {
2
+ validChoiceDetail,
3
+ type DashboardChoiceAction,
4
+ type DashboardChoiceDetail,
5
+ type DashboardChoiceTarget,
6
+ } from "../kernel/dashboard-choice.ts";
1
7
  import type { DashboardOwnerObservation } from "../kernel/dashboard-observation.ts";
2
8
  import type { DashboardSettingsSnapshot, JevEditAction } from "./dashboard-settings.ts";
3
9
  import type { EcosystemVersions } from "./ecosystem-versions.ts";
@@ -6,6 +12,7 @@ import { createConnection } from "node:net";
6
12
  export interface DashboardSessionSnapshot {
7
13
  rows: Array<{ definition: string; model: string; thinking: string; source: string }>;
8
14
  cost: number | null;
15
+ choiceControls?: { sourceGeneration: string };
9
16
  observation?: DashboardOwnerObservation;
10
17
  activity?: { rootId: string; path: string; taskId: string };
11
18
  versions?: EcosystemVersions;
@@ -14,8 +21,25 @@ export interface DashboardSessionSnapshot {
14
21
  pendingApprovals: Array<{ id: string; subject: string; capability: string }>;
15
22
  }
16
23
 
24
+ interface DashboardSessionReply extends DashboardSessionSnapshot {
25
+ choiceDetail?: DashboardChoiceDetail;
26
+ choiceSaved?: true;
27
+ choiceRefreshRequested?: true;
28
+ }
29
+ class ChoiceDeclined extends Error {
30
+ readonly stale: boolean;
31
+ constructor(stale: boolean) {
32
+ super(
33
+ stale
34
+ ? "Choice is no longer available. Refresh and reopen it."
35
+ : "Bookie declined the answer. Refresh and check the choice and answer.",
36
+ );
37
+ this.stale = stale;
38
+ }
39
+ }
17
40
  export type DashboardSessionAction = (
18
- | { action: "get" }
41
+ | DashboardChoiceAction
42
+ | { action: "get"; refreshChoices?: true }
19
43
  | { action: "set"; edits: string }
20
44
  | { action: "set-auto"; enabled: boolean }
21
45
  | { action: "set-jev"; enabled: boolean }
@@ -27,12 +51,15 @@ export async function dashboardSessionRequest(
27
51
  socketPath: string,
28
52
  token: string,
29
53
  request: DashboardSessionAction,
30
- ): Promise<DashboardSessionSnapshot> {
54
+ ): Promise<DashboardSessionReply> {
31
55
  return new Promise((resolve, reject) => {
32
56
  const socket = createConnection(socketPath);
33
57
  let output = "";
34
58
  socket.setEncoding("utf8");
35
- socket.setTimeout(2_000, () => socket.destroy(new Error("dashboard session request timed out")));
59
+ const choiceAction = request.action === "inspect-choice" || request.action === "answer-choice";
60
+ socket.setTimeout(choiceAction ? 30_000 : 2_000, () =>
61
+ socket.destroy(new Error("dashboard session request timed out")),
62
+ );
36
63
  socket.once("error", reject);
37
64
  socket.on("data", (chunk) => {
38
65
  output += chunk;
@@ -40,8 +67,26 @@ export async function dashboardSessionRequest(
40
67
  });
41
68
  socket.once("end", () => {
42
69
  try {
43
- const parsed = JSON.parse(output) as DashboardSessionSnapshot & { ok?: boolean; error?: string };
44
- if (!parsed.ok) throw new Error(parsed.error ?? "dashboard session request failed");
70
+ const parsed = JSON.parse(output) as DashboardSessionReply & {
71
+ ok?: boolean;
72
+ error?: string;
73
+ choiceOutcome?: string;
74
+ };
75
+ if (!parsed.ok) {
76
+ if (choiceAction) {
77
+ if (parsed.choiceOutcome === "stale" || parsed.choiceOutcome === "refused")
78
+ throw new ChoiceDeclined(parsed.choiceOutcome === "stale");
79
+ throw Error("The owner could not confirm the choice request.");
80
+ }
81
+ throw new Error(parsed.error ?? "dashboard session request failed");
82
+ }
83
+ if (
84
+ request.action === "inspect-choice" &&
85
+ (!validChoiceDetail(parsed.choiceDetail) || parsed.choiceDetail.id !== request.choiceId)
86
+ )
87
+ throw Error("The owner returned an unavailable choice.");
88
+ if (request.action === "answer-choice" && parsed.choiceSaved !== true)
89
+ throw Error("Bookie did not confirm the answer.");
45
90
  if (
46
91
  !parsed.auto ||
47
92
  typeof parsed.auto.enabled !== "boolean" ||
@@ -67,7 +112,7 @@ export type DashboardAcknowledgment = {
67
112
 
68
113
  /** Controls use only a connected owner; last observations remain explicitly disconnected evidence. */
69
114
  export function createDashboardConnection(
70
- request?: (action: DashboardSessionAction) => Promise<DashboardSessionSnapshot>,
115
+ request?: (action: DashboardSessionAction) => Promise<DashboardSessionReply>,
71
116
  ) {
72
117
  const state: {
73
118
  snapshot?: DashboardSessionSnapshot;
@@ -79,6 +124,8 @@ export function createDashboardConnection(
79
124
  let generation = 0,
80
125
  poll = 0,
81
126
  requestedJev: boolean | undefined;
127
+ let choiceInterest = 0,
128
+ consumedInterest = 0;
82
129
  const retired = new Set<string>();
83
130
  const accept = (snapshot: DashboardSessionSnapshot) => {
84
131
  const previous = state.lastObserved?.observation,
@@ -102,14 +149,55 @@ export function createDashboardConnection(
102
149
  state.acknowledgment.state = snapshot.settings.jev.enabled === requestedJev ? "applied" : "refused";
103
150
  return true;
104
151
  };
152
+ const matchesOwner = (snapshot: DashboardSessionSnapshot | undefined, target: DashboardChoiceTarget) => {
153
+ const owner = snapshot?.observation,
154
+ expected = target.expectedOwner;
155
+ return (
156
+ !!owner &&
157
+ owner.sessionId === expected.sessionId &&
158
+ owner.cwd === expected.cwd &&
159
+ owner.generation === expected.generation
160
+ );
161
+ };
162
+ const matches = (snapshot: DashboardSessionSnapshot | undefined, target: DashboardChoiceTarget) => {
163
+ const source = snapshot?.observation?.sources.find(
164
+ (s) => s.producer === "bookie-memory" && s.generation === target.sourceGeneration,
165
+ );
166
+ return (
167
+ matchesOwner(snapshot, target) &&
168
+ snapshot?.choiceControls?.sourceGeneration === target.sourceGeneration &&
169
+ source?.availability === "observed" &&
170
+ source.items.some((i) => i.id === target.choiceId && i.kind === "work" && i.state === "observed")
171
+ );
172
+ };
105
173
  return {
106
174
  state,
107
- async refresh() {
175
+ async inspectChoice(target: DashboardChoiceTarget): Promise<DashboardChoiceDetail> {
176
+ if (!request || state.pending || !matches(state.snapshot, target)) throw new ChoiceDeclined(true);
177
+ const result = await request({ ...target, action: "inspect-choice" });
178
+ if (
179
+ !matches(state.snapshot, target) ||
180
+ !matches(result, target) ||
181
+ !validChoiceDetail(result.choiceDetail) ||
182
+ result.choiceDetail.id !== target.choiceId
183
+ )
184
+ throw new ChoiceDeclined(true);
185
+ return result.choiceDetail;
186
+ },
187
+ requestChoiceRefresh() {
188
+ choiceInterest++;
189
+ },
190
+ async refresh(visibleChoices?: "picker" | "list") {
108
191
  if (!request || state.pending) return;
109
192
  const observed = generation,
110
193
  order = ++poll;
111
194
  try {
112
- const snapshot = await request({ action: "get" });
195
+ const interest = choiceInterest;
196
+ const wanted = visibleChoices === "picker" || (visibleChoices === "list" && interest !== consumedInterest);
197
+ const { choiceRefreshRequested, ...snapshot } = await request(
198
+ wanted ? { action: "get", refreshChoices: true } : { action: "get" },
199
+ );
200
+ if (choiceRefreshRequested) consumedInterest = Math.max(consumedInterest, interest);
113
201
  if (generation === observed && order === poll && !accept(snapshot)) {
114
202
  state.snapshot = undefined;
115
203
  state.error = "Owner observation is stale or unavailable; controls disabled until a fresh reply.";
@@ -121,25 +209,35 @@ export function createDashboardConnection(
121
209
  }
122
210
  }
123
211
  },
124
- async change(action: Exclude<DashboardSessionAction, { action: "get" }>) {
212
+ async change(action: Exclude<DashboardSessionAction, { action: "get" | "inspect-choice" }>) {
125
213
  if (!request || !state.snapshot || state.pending) return false;
214
+ if (action.action === "answer-choice" && !matches(state.snapshot, action)) {
215
+ state.acknowledgment = {
216
+ action: action.action,
217
+ state: "refused",
218
+ effect: "Choice changed. Refresh and reopen it.",
219
+ };
220
+ return false;
221
+ }
126
222
  const observedOwner = state.snapshot.observation;
127
223
  generation++;
128
224
  state.pending = true;
129
225
  requestedJev = action.action === "set-jev" ? action.enabled : undefined;
130
226
  const effect =
131
- action.action === "set-auto"
132
- ? "current permission handling; admitted work unchanged"
133
- : action.action === "edit-jev"
134
- ? "use the main Pi session; current values shown below"
135
- : action.action === "set-jev"
136
- ? "current JEV session; separate paid-call and storage choices"
137
- : "future children; running children unchanged";
227
+ action.action === "answer-choice"
228
+ ? "Save this answer in project memory"
229
+ : action.action === "set-auto"
230
+ ? "current permission handling; admitted work unchanged"
231
+ : action.action === "edit-jev"
232
+ ? "use the main Pi session; current values shown below"
233
+ : action.action === "set-jev"
234
+ ? "current JEV session; separate paid-call and storage choices"
235
+ : "future children; running children unchanged";
138
236
  state.acknowledgment = { action: action.action, state: "pending", effect };
139
237
  try {
140
238
  const snapshot = await request({
141
239
  ...action,
142
- ...(observedOwner
240
+ ...(observedOwner && !action.expectedOwner
143
241
  ? {
144
242
  expectedOwner: {
145
243
  sessionId: observedOwner.sessionId,
@@ -149,7 +247,10 @@ export function createDashboardConnection(
149
247
  }
150
248
  : {}),
151
249
  });
152
- if (!accept(snapshot)) throw Error("owner acknowledgment is stale; reconcile before another action");
250
+ if (action.action === "answer-choice" && (snapshot.choiceSaved !== true || !matchesOwner(snapshot, action)))
251
+ throw Error("Bookie did not confirm the answer.");
252
+ const { choiceDetail: _detail, choiceSaved: _saved, ...observed } = snapshot;
253
+ if (!accept(observed)) throw Error("owner acknowledgment is stale; reconcile before another action");
153
254
  state.acknowledgment.state =
154
255
  action.action === "edit-jev"
155
256
  ? "opened"
@@ -162,9 +263,18 @@ export function createDashboardConnection(
162
263
  : "applied";
163
264
  return true;
164
265
  } catch (error) {
266
+ if (action.action === "answer-choice" && error instanceof ChoiceDeclined) {
267
+ if (error.stale) state.snapshot = undefined;
268
+ state.acknowledgment.state = "refused";
269
+ state.error = error.message;
270
+ return false;
271
+ }
165
272
  state.snapshot = undefined;
166
273
  state.acknowledgment.state = "unconfirmed";
167
- state.error = `Control change not confirmed; inspect owner before retrying: ${error instanceof Error ? error.message : String(error)}`;
274
+ state.error =
275
+ action.action === "answer-choice"
276
+ ? "Save unconfirmed. Check project memory before retrying."
277
+ : `Control change not confirmed; inspect owner before retrying: ${error instanceof Error ? error.message : String(error)}`;
168
278
  return false;
169
279
  } finally {
170
280
  state.pending = false;