@howaboua/pi-agent-board 0.0.4 → 0.0.6

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 (64) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/README.md +3 -1
  3. package/dist/viewer/archive.js +24 -17
  4. package/dist/viewer/web/posts.js +0 -1
  5. package/index.ts +5 -69
  6. package/package.json +15 -14
  7. package/runtime.ts +2 -0
  8. package/scripts/build-viewer.mjs +9 -0
  9. package/src/board/AGENTS.md +12 -0
  10. package/src/{archive.ts → board/archive.ts} +17 -7
  11. package/src/{awareness.ts → board/awareness.ts} +6 -11
  12. package/src/board/briefing.ts +25 -0
  13. package/src/{contract.ts → board/contract.ts} +39 -29
  14. package/src/board/feature.ts +91 -0
  15. package/src/{identity.ts → board/identity.ts} +121 -37
  16. package/src/{integration.ts → board/integration.ts} +1 -0
  17. package/src/board/protocol.ts +117 -0
  18. package/src/{queries.ts → board/queries.ts} +9 -11
  19. package/src/{response.ts → board/response.ts} +7 -1
  20. package/src/board/runtime/activation.ts +50 -0
  21. package/src/{runtime → board/runtime}/coordinator.ts +76 -43
  22. package/src/board/runtime/engine.ts +40 -0
  23. package/src/board/runtime/index.ts +63 -0
  24. package/src/{runtime → board/runtime}/owner.ts +44 -15
  25. package/src/{runtime → board/runtime}/routing.ts +19 -4
  26. package/src/{runtime → board/runtime}/turns.ts +9 -1
  27. package/src/{search.ts → board/search.ts} +11 -9
  28. package/src/board/settings/panel.ts +193 -0
  29. package/src/{store.ts → board/store.ts} +45 -9
  30. package/src/{summarizer.ts → board/summarizer.ts} +3 -2
  31. package/src/{tool.ts → board/tool.ts} +5 -0
  32. package/src/board/viewer/AGENTS.md +1 -0
  33. package/src/{viewer → board/viewer}/archive.ts +24 -17
  34. package/src/{viewer → board/viewer}/index.ts +5 -1
  35. package/src/{viewer → board/viewer}/web/posts.js +0 -1
  36. package/src/guidance.ts +121 -0
  37. package/src/settings-navigation.ts +11 -0
  38. package/src/timestamps.ts +3 -0
  39. package/tsconfig.json +6 -2
  40. package/tsconfig.viewer.json +11 -0
  41. package/changelog.ts +0 -337
  42. package/src/briefing.ts +0 -32
  43. package/src/protocol.ts +0 -78
  44. package/src/runtime/index.ts +0 -113
  45. package/src/settings/panel.ts +0 -87
  46. /package/src/{activity.ts → board/activity.ts} +0 -0
  47. /package/src/{config.ts → board/config.ts} +0 -0
  48. /package/src/{consolidation.ts → board/consolidation.ts} +0 -0
  49. /package/src/{context-briefing.ts → board/context-briefing.ts} +0 -0
  50. /package/src/{delivery.ts → board/delivery.ts} +0 -0
  51. /package/src/{paging.ts → board/paging.ts} +0 -0
  52. /package/src/{runtime → board/runtime}/catchup.ts +0 -0
  53. /package/src/{summaries.ts → board/summaries.ts} +0 -0
  54. /package/src/{viewer → board/viewer}/config.ts +0 -0
  55. /package/src/{viewer → board/viewer}/contracts.ts +0 -0
  56. /package/src/{viewer → board/viewer}/server.ts +0 -0
  57. /package/src/{viewer → board/viewer}/source.ts +0 -0
  58. /package/src/{viewer → board/viewer}/web/app.js +0 -0
  59. /package/src/{viewer → board/viewer}/web/index.html +0 -0
  60. /package/src/{viewer → board/viewer}/web/navigation.js +0 -0
  61. /package/src/{viewer → board/viewer}/web/render.js +0 -0
  62. /package/src/{viewer → board/viewer}/web/session.js +0 -0
  63. /package/src/{viewer → board/viewer}/web/style.css +0 -0
  64. /package/src/{viewer → board/viewer}/web/updates.js +0 -0
@@ -2,8 +2,10 @@ import type {
2
2
  ExtensionAPI,
3
3
  ExtensionContext,
4
4
  } from "@earendil-works/pi-coding-agent";
5
- import { recordBoardActivityMarker } from "../activity.js";
6
- import { BoardAwareness } from "../awareness.js";
5
+ import {
6
+ recordBoardActivityMarker,
7
+ registerBoardActivityRenderer,
8
+ } from "../activity.js";
7
9
  import {
8
10
  type BoardScope,
9
11
  writeBoardConfig,
@@ -20,72 +22,51 @@ import {
20
22
  } from "../identity.js";
21
23
  import type { BoardAdapter } from "../integration.js";
22
24
  import { type BoardEnvelope } from "../protocol.js";
23
- import { BoardSummarizer } from "../summarizer.js";
24
- import { BoardCatchup } from "./catchup.js";
25
+ import type { BoardEngine } from "./engine.js";
25
26
  import { BoardOwner } from "./owner.js";
26
27
  import { BoardRouting } from "./routing.js";
27
- import { BoardTurns } from "./turns.js";
28
28
 
29
29
  export class BoardRuntime {
30
30
  gptPromptMode = false;
31
31
  private preparePromptMode: ((ctx: ExtensionContext) => void) | undefined;
32
32
  private readonly pi: ExtensionAPI;
33
33
  private readonly routing: BoardRouting;
34
- private readonly turns: BoardTurns;
35
34
  private readonly owner: BoardOwner;
36
- private readonly awareness: BoardAwareness;
37
- private readonly catchup: BoardCatchup;
38
- private readonly summarizer = new BoardSummarizer();
35
+ private engine: BoardEngine | undefined;
36
+ private engineReady: Promise<BoardEngine> | undefined;
37
+ private briefingReady: Promise<void> | undefined;
39
38
  private refreshTools: (() => void) | undefined;
40
39
  private lastSetting: { sessionId: string; enabled: boolean } | undefined;
41
40
  private configError: string | undefined;
42
41
  private availabilityWarning: string | undefined;
43
42
  constructor(pi: ExtensionAPI) {
44
43
  this.pi = pi;
44
+ registerBoardActivityRenderer(pi);
45
45
  const availability = (ctx: ExtensionContext, error?: unknown) =>
46
46
  this.reportAvailability(ctx, error);
47
- this.awareness = new BoardAwareness(
48
- pi,
49
- (ctx) => this.population(ctx),
50
- availability,
51
- () => this.routing.adapter?.briefing,
52
- () => this.gptPromptMode,
53
- );
54
- this.catchup = new BoardCatchup(
55
- pi,
56
- this.awareness,
57
- (ctx, request) => this.toOwner(ctx, request),
58
- availability,
59
- () => this.gptPromptMode,
60
- );
61
- this.turns = new BoardTurns(
62
- pi,
63
- (ctx, request) => this.toOwner(ctx, request),
64
- availability,
65
- (ctx, ids) => this.awareness.markSeen(ctx, ids),
66
- );
67
47
  this.owner = new BoardOwner(
68
- this.turns,
69
- this.summarizer,
48
+ () => this.requireEngine().turns,
49
+ () => this.requireEngine().summarizer,
70
50
  () => this.routing.adapter,
71
51
  () => this.routing.requireAdapter(),
72
52
  (ctx, request, signal) => this.handle(ctx, request, signal),
73
53
  );
74
54
  this.routing = new BoardRouting(
75
55
  pi,
76
- this.turns,
56
+ { deliver: (request) => this.engine?.turns.deliver(request) ?? false },
77
57
  this.owner,
78
58
  (ctx) => this.refresh(ctx),
79
59
  availability,
80
60
  );
81
61
  pi.on("session_start", (_event, ctx) => {
82
- this.summarizer.cancelAll();
62
+ this.engine?.summarizer.cancelAll();
83
63
  this.availabilityWarning = undefined;
84
64
  setBoardDefault(ctx, true);
85
65
  return this.refresh(ctx);
86
66
  });
87
- pi.on("session_shutdown", () => this.summarizer.cancelAll());
67
+ pi.on("session_shutdown", () => this.engine?.summarizer.cancelAll());
88
68
  pi.on("before_agent_start", async (_event, ctx) => {
69
+ const prepared = this.preparePromptMode;
89
70
  this.preparePromptMode?.(ctx);
90
71
  // Reconcile resumed/offline children before tool/prompt preparation.
91
72
  const own = binding(ctx);
@@ -111,6 +92,7 @@ export class BoardRuntime {
111
92
  }
112
93
  }
113
94
  await this.refresh(ctx);
95
+ if (this.preparePromptMode !== prepared) this.preparePromptMode?.(ctx);
114
96
  const catchup = await this.promptCatchup(ctx);
115
97
  catchup.accepted();
116
98
  if (catchup.content)
@@ -123,7 +105,7 @@ export class BoardRuntime {
123
105
  };
124
106
  });
125
107
  }
126
- private reportAvailability(ctx: ExtensionContext, error?: unknown): void {
108
+ reportAvailability(ctx: ExtensionContext, error?: unknown): void {
127
109
  const own = binding(ctx);
128
110
  if (error === undefined || !own.enabled) {
129
111
  this.availabilityWarning = undefined;
@@ -146,7 +128,7 @@ export class BoardRuntime {
146
128
  setPromptModePreparation(prepare: (ctx: ExtensionContext) => void) {
147
129
  this.preparePromptMode = prepare;
148
130
  }
149
- private async population(ctx: ExtensionContext) {
131
+ async population(ctx: ExtensionContext) {
150
132
  const own = binding(ctx);
151
133
  if (own.upstream) {
152
134
  const status = await this.toOwner(
@@ -182,11 +164,26 @@ export class BoardRuntime {
182
164
  return ctx ? binding(ctx).enabled : false;
183
165
  }
184
166
  async promptCatchup(ctx: ExtensionContext) {
185
- return this.catchup.read(ctx);
167
+ return this.enabled(ctx)
168
+ ? (await this.ensureEngine()).catchup.read(ctx)
169
+ : { content: "", accepted() {} };
186
170
  }
187
171
  async refresh(ctx: ExtensionContext) {
188
172
  const own = binding(ctx);
189
- this.awareness.refresh(ctx);
173
+ if (own.enabled) await this.ensureEngine();
174
+ // Native Pi also needs projection of saved briefings while Board is off.
175
+ if (
176
+ own.enabled ||
177
+ ctx.sessionManager
178
+ .getEntries()
179
+ .some(
180
+ (entry) =>
181
+ entry.type === "custom" &&
182
+ entry.customType === "codex-context-briefing",
183
+ )
184
+ )
185
+ await this.ensureBriefing();
186
+ this.engine?.awareness.refresh(ctx);
190
187
  const setting = own.upstream ? undefined : rootBoardSetting(ctx);
191
188
  if (setting?.error && setting.error !== this.configError)
192
189
  ctx.ui.notify(`Board disabled: ${setting.error}`, "error");
@@ -196,8 +193,8 @@ export class BoardRuntime {
196
193
  this.pi.setActiveTools(active);
197
194
  this.refreshTools?.();
198
195
  if (!own.enabled) {
199
- this.turns.active.clear();
200
- this.summarizer.cancelAll();
196
+ this.engine?.turns.active.clear();
197
+ this.engine?.summarizer.cancelAll();
201
198
  }
202
199
  const changed =
203
200
  this.lastSetting?.sessionId !== own.sessionId ||
@@ -223,7 +220,7 @@ export class BoardRuntime {
223
220
  }
224
221
  setAutomaticSummaries(ctx: ExtensionContext, automatic: boolean) {
225
222
  writeBoardSummaryAutomatic(ctx.cwd, automatic);
226
- if (!automatic) this.summarizer.cancelAll();
223
+ if (!automatic) this.engine?.summarizer.cancelAll();
227
224
  }
228
225
  async execute(ctx: ExtensionContext, input: unknown, requestId: string) {
229
226
  const params = parseBoardRequest(input);
@@ -245,7 +242,7 @@ export class BoardRuntime {
245
242
  ctx.signal,
246
243
  );
247
244
  if (params.action === "read_thread" || params.action === "read_post")
248
- this.awareness.markRead(ctx, value);
245
+ this.engine?.awareness.markRead(ctx, value);
249
246
  try {
250
247
  recordBoardActivityMarker(this.pi, ctx, params, value, requestId);
251
248
  } catch {
@@ -260,6 +257,7 @@ export class BoardRuntime {
260
257
  : value;
261
258
  }
262
259
  async handle(ctx: ExtensionContext, value: unknown, signal?: AbortSignal) {
260
+ if (this.enabled(ctx)) await this.ensureEngine();
263
261
  return this.routing.handle(ctx, value, signal);
264
262
  }
265
263
  async toOwner(
@@ -267,12 +265,47 @@ export class BoardRuntime {
267
265
  request: BoardEnvelope,
268
266
  signal?: AbortSignal,
269
267
  ) {
268
+ if (this.enabled(ctx)) await this.ensureEngine();
270
269
  return this.routing.toOwner(ctx, request, signal);
271
270
  }
271
+ private ensureEngine() {
272
+ return (this.engineReady ??= import("./engine.js").then(
273
+ async ({ createBoardEngine }) => {
274
+ const engine = await createBoardEngine(this.pi, this);
275
+ this.engine = engine;
276
+ return engine;
277
+ },
278
+ ));
279
+ }
280
+ private ensureBriefing() {
281
+ return (this.briefingReady ??= import("../context-briefing.js").then(
282
+ ({ registerContextBriefing }) => {
283
+ registerContextBriefing(
284
+ this.pi,
285
+ "board",
286
+ (ctx) => this.enabled(ctx),
287
+ async (ctx, windowId) => {
288
+ if (!this.enabled(ctx)) return;
289
+ return (await this.ensureEngine()).awareness.record(ctx, windowId);
290
+ },
291
+ );
292
+ },
293
+ ));
294
+ }
295
+ private requireEngine() {
296
+ if (!this.engine) throw new Error("Board is not ready");
297
+ return this.engine;
298
+ }
299
+ get adapter() {
300
+ return this.routing.adapter;
301
+ }
272
302
  attach(adapter: BoardAdapter) {
273
303
  this.routing.attach(adapter);
274
304
  }
275
305
  get active() {
276
- return this.turns.active;
306
+ return (
307
+ this.engine?.turns.active ??
308
+ new Map<string, { sessionId: string; turnId: string }>()
309
+ );
277
310
  }
278
311
  }
@@ -0,0 +1,40 @@
1
+ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
2
+ import { BoardAwareness } from "../awareness.js";
3
+ import { BoardSummarizer } from "../summarizer.js";
4
+ import { activateBoard } from "./activation.js";
5
+ import { BoardCatchup } from "./catchup.js";
6
+ import type { BoardRuntime } from "./coordinator.js";
7
+ import { BoardTurns } from "./turns.js";
8
+
9
+ /** Optional inference, turn delivery and summary machinery, loaded only when on. */
10
+ export async function createBoardEngine(
11
+ pi: ExtensionAPI,
12
+ runtime: BoardRuntime,
13
+ ) {
14
+ const availability = runtime.reportAvailability.bind(runtime);
15
+ const awareness = new BoardAwareness(
16
+ pi,
17
+ (ctx) => runtime.population(ctx),
18
+ availability,
19
+ () => runtime.adapter?.briefing,
20
+ () => runtime.gptPromptMode,
21
+ );
22
+ const catchup = new BoardCatchup(
23
+ pi,
24
+ awareness,
25
+ (ctx, request) => runtime.toOwner(ctx, request),
26
+ availability,
27
+ () => runtime.gptPromptMode,
28
+ );
29
+ const turns = new BoardTurns(
30
+ pi,
31
+ (ctx, request) => runtime.toOwner(ctx, request),
32
+ availability,
33
+ (ctx, ids) => awareness.markSeen(ctx, ids),
34
+ );
35
+ const summarizer = new BoardSummarizer();
36
+ await activateBoard(pi, runtime);
37
+ return { awareness, catchup, turns, summarizer };
38
+ }
39
+
40
+ export type BoardEngine = Awaited<ReturnType<typeof createBoardEngine>>;
@@ -0,0 +1,63 @@
1
+ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
2
+ import type { BoardRuntime } from "./coordinator.js";
3
+
4
+ export type { BoardRuntime };
5
+
6
+ const CLAIM = "@howaboua/pi-agent-board.owner/v1";
7
+ const AVAILABLE = "@howaboua/pi-agent-board.available/v1";
8
+ interface OwnerClaim {
9
+ runtime: Promise<BoardRuntime>;
10
+ }
11
+
12
+ /** Discover an explicitly loaded owner, including one loaded after this adapter. */
13
+ export function discoverBoard(
14
+ pi: ExtensionAPI,
15
+ attach: (runtime: BoardRuntime) => void,
16
+ ) {
17
+ let attached = false;
18
+ const accept = (runtime: BoardRuntime) => {
19
+ if (attached) return;
20
+ attached = true;
21
+ attach(runtime);
22
+ };
23
+ const request = () =>
24
+ pi.events.emit(CLAIM, {
25
+ accept: (claim: OwnerClaim) => {
26
+ void claim.runtime.then(accept);
27
+ },
28
+ });
29
+ const stop = pi.events.on(AVAILABLE, request);
30
+ request();
31
+ pi.on("session_shutdown", stop);
32
+ }
33
+
34
+ /** Only an explicitly loaded Board extension claims the owner; its engine stays lazy. */
35
+ export async function acquireBoard(pi: ExtensionAPI): Promise<BoardRuntime> {
36
+ let claim: OwnerClaim | undefined;
37
+ pi.events.emit(CLAIM, {
38
+ accept(value: OwnerClaim) {
39
+ claim = value;
40
+ },
41
+ });
42
+ if (!claim) {
43
+ const created: OwnerClaim = {
44
+ runtime: import("./coordinator.js").then(
45
+ ({ BoardRuntime }) => new BoardRuntime(pi),
46
+ ),
47
+ };
48
+ claim = created;
49
+ const stop = pi.events.on(CLAIM, (value) => {
50
+ if (
51
+ value &&
52
+ typeof value === "object" &&
53
+ "accept" in value &&
54
+ typeof value.accept === "function"
55
+ )
56
+ value.accept(created);
57
+ });
58
+ pi.on("session_shutdown", stop);
59
+ }
60
+ const runtime = await claim.runtime;
61
+ pi.events.emit(AVAILABLE, undefined);
62
+ return runtime;
63
+ }
@@ -1,12 +1,11 @@
1
1
  import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
2
- import { executeArchive, readSubscribedUpdates } from "../archive.js";
3
2
  import {
4
3
  agentPath,
5
4
  type BoardParams,
6
5
  boardHelp,
7
6
  parseBoardRequest,
8
7
  } from "../contract.js";
9
- import { binding, members } from "../identity.js";
8
+ import { binding, boardAliases, members } from "../identity.js";
10
9
  import type { BoardAdapter } from "../integration.js";
11
10
  import type { BoardEnvelope } from "../protocol.js";
12
11
  import type { BoardSummarizer } from "../summarizer.js";
@@ -14,8 +13,8 @@ import type { BoardTurns } from "./turns.js";
14
13
 
15
14
  /** Owner execution begins at membership validation; archive commit precedes delivery. */
16
15
  export class BoardOwner {
17
- private readonly turns: BoardTurns;
18
- private readonly summarizer: BoardSummarizer;
16
+ private readonly getTurns: () => BoardTurns;
17
+ private readonly getSummarizer: () => BoardSummarizer;
19
18
  private readonly adapterOrUndefined: () => BoardAdapter | undefined;
20
19
  private readonly adapter: () => BoardAdapter;
21
20
  private readonly notify: (
@@ -24,8 +23,8 @@ export class BoardOwner {
24
23
  signal?: AbortSignal,
25
24
  ) => Promise<unknown>;
26
25
  constructor(
27
- turns: BoardTurns,
28
- summarizer: BoardSummarizer,
26
+ turns: () => BoardTurns,
27
+ summarizer: () => BoardSummarizer,
29
28
  adapterOrUndefined: () => BoardAdapter | undefined,
30
29
  adapter: () => BoardAdapter,
31
30
  notify: (
@@ -34,8 +33,8 @@ export class BoardOwner {
34
33
  signal?: AbortSignal,
35
34
  ) => Promise<unknown>,
36
35
  ) {
37
- this.turns = turns;
38
- this.summarizer = summarizer;
36
+ this.getTurns = turns;
37
+ this.getSummarizer = summarizer;
39
38
  this.adapterOrUndefined = adapterOrUndefined;
40
39
  this.adapter = adapter;
41
40
  this.notify = notify;
@@ -64,11 +63,17 @@ export class BoardOwner {
64
63
  )
65
64
  return this.adapter().commitDirectory(own, directory, caller, request);
66
65
  if (request.operation === "board-active") {
67
- if (own.enabled) this.turns.register(caller, request);
66
+ if (own.enabled) this.getTurns().register(caller, request);
68
67
  return true;
69
68
  }
70
69
  if (request.operation === "board-catchup")
71
- return own.enabled ? readSubscribedUpdates(caller, request.seen) : [];
70
+ return own.enabled
71
+ ? (await import("../archive.js")).readSubscribedUpdates(
72
+ caller,
73
+ request.seen,
74
+ boardAliases(ctx),
75
+ )
76
+ : [];
72
77
  if (request.operation !== "board-call")
73
78
  throw new Error("Invalid board owner operation");
74
79
  const params = parseBoardRequest(request.params);
@@ -80,7 +85,7 @@ export class BoardOwner {
80
85
  : {
81
86
  ...boardHelp.rules,
82
87
  agents:
83
- "Absolute /root/... or child path relative to you; notices require registered members",
88
+ "Agent reference /<Pi session ID>; notices require registered members",
84
89
  },
85
90
  enabled: own.enabled,
86
91
  };
@@ -89,21 +94,29 @@ export class BoardOwner {
89
94
  const prepared: BoardParams =
90
95
  params.author === undefined
91
96
  ? params
92
- : { ...params, author: agentPath(params.author, caller.agentName) };
93
- const active = new Map(this.turns.active);
97
+ : {
98
+ ...params,
99
+ author: agentPath(
100
+ params.author,
101
+ caller.legacyAgentName ?? caller.agentName,
102
+ ),
103
+ };
104
+ const active = new Map(this.getTurns().active);
105
+ const { executeArchive } = await import("../archive.js");
94
106
  const result = await executeArchive(
95
107
  { ...caller, enabled: own.enabled },
96
108
  directory.map((member) => member.agentName),
97
109
  prepared,
98
110
  request.requestId,
111
+ boardAliases(ctx, params.board_id),
99
112
  );
100
113
  if ("notice" in result && result.notice) {
101
114
  // Replayed mutations have no notice: never start or cancel twice for a retry.
102
115
  if (params.action === "set_status") {
103
116
  if (params.status === "open")
104
- this.summarizer.cancel(own.boardId, result.notice.thread_id);
117
+ this.getSummarizer().cancel(own.boardId, result.notice.thread_id);
105
118
  else
106
- this.summarizer.start(
119
+ this.getSummarizer().start(
107
120
  ctx,
108
121
  own,
109
122
  result.notice.thread_id,
@@ -115,6 +128,21 @@ export class BoardOwner {
115
128
  result.recipients.slice(start, start + 16).map(async (target) => {
116
129
  const turn = active.get(target);
117
130
  if (!turn) return;
131
+ const route: string[] = [];
132
+ let member = directory.find(
133
+ (entry) => entry.sessionId === turn.sessionId,
134
+ );
135
+ while (
136
+ member &&
137
+ member.sessionId !== own.sessionId &&
138
+ route.length < directory.length
139
+ ) {
140
+ route.unshift(member.sessionId);
141
+ member = directory.find(
142
+ (entry) => entry.sessionId === member?.parentSessionId,
143
+ );
144
+ }
145
+ if (member?.sessionId !== own.sessionId) return;
118
146
  try {
119
147
  await this.notify(
120
148
  ctx,
@@ -122,6 +150,7 @@ export class BoardOwner {
122
150
  operation: "board-notify",
123
151
  boardId: own.boardId,
124
152
  target,
153
+ route,
125
154
  ...turn,
126
155
  notice: result.notice,
127
156
  },
@@ -2,7 +2,7 @@ import type {
2
2
  ExtensionAPI,
3
3
  ExtensionContext,
4
4
  } from "@earendil-works/pi-coding-agent";
5
- import { binding, saveBinding } from "../identity.js";
5
+ import { binding, parseBinding, saveBinding } from "../identity.js";
6
6
  import type { BoardAdapter } from "../integration.js";
7
7
  import { type BoardEnvelope, parseEnvelope } from "../protocol.js";
8
8
  import type { BoardOwner } from "./owner.js";
@@ -11,7 +11,7 @@ import type { BoardTurns } from "./turns.js";
11
11
  /** Routes membership/setting/notice envelopes; lifecycle refresh stays with the runtime. */
12
12
  export class BoardRouting {
13
13
  private readonly pi: ExtensionAPI;
14
- private readonly turns: BoardTurns;
14
+ private readonly turns: Pick<BoardTurns, "deliver">;
15
15
  private readonly owner: BoardOwner;
16
16
  private readonly refresh: (ctx: ExtensionContext) => Promise<void>;
17
17
  private readonly reportAvailability: (
@@ -21,7 +21,7 @@ export class BoardRouting {
21
21
  adapter: BoardAdapter | undefined;
22
22
  constructor(
23
23
  pi: ExtensionAPI,
24
- turns: BoardTurns,
24
+ turns: Pick<BoardTurns, "deliver">,
25
25
  owner: BoardOwner,
26
26
  refresh: (ctx: ExtensionContext) => Promise<void>,
27
27
  reportAvailability: (ctx: ExtensionContext, error?: unknown) => void,
@@ -40,6 +40,15 @@ export class BoardRouting {
40
40
  const request = parseEnvelope(value);
41
41
  const own = binding(ctx);
42
42
  if (request.operation === "board-bind") {
43
+ parseBinding(request.binding);
44
+ if (own.sessionId === request.binding.sessionId)
45
+ throw new Error(
46
+ "Child and controller have the same Pi session ID; start the child with a different ID",
47
+ );
48
+ if (request.binding.agentName !== `/${request.binding.sessionId}`)
49
+ throw new Error(
50
+ "Restart the controller with updated extensions before spawning shared-board agents",
51
+ );
43
52
  if (
44
53
  !ctx.isIdle() ||
45
54
  ctx.sessionManager
@@ -55,7 +64,13 @@ export class BoardRouting {
55
64
  throw new Error(
56
65
  "Bind board identity only in a fresh idle child before its first task",
57
66
  );
58
- const adopted = { ...request.binding, sessionId: own.sessionId };
67
+ const { legacyAgentName: _legacy, ...proposed } = request.binding;
68
+ const adopted = {
69
+ ...proposed,
70
+ sessionId: own.sessionId,
71
+ agentName: own.agentName,
72
+ parentSessionId: request.binding.sessionId,
73
+ };
59
74
  saveBinding(this.pi, adopted);
60
75
  await this.refresh(ctx);
61
76
  return adopted;
@@ -72,12 +72,20 @@ export class BoardTurns {
72
72
  own.agentName !== request.target
73
73
  )
74
74
  return false;
75
+ const {
76
+ message_id,
77
+ channel_name,
78
+ author,
79
+ thread_id,
80
+ text_preview,
81
+ truncated,
82
+ } = request.notice;
75
83
  // No await between running-turn check and steering. Never use the idle kickoff.
76
84
  sendPolicyMessage(
77
85
  pi,
78
86
  {
79
87
  customType: "shepherdr-board-post",
80
- content: `Board post ${JSON.stringify(request.notice)}`,
88
+ content: `Board post ${JSON.stringify({ message_id, channel_name, author, thread_id, text_preview, truncated })}`,
81
89
  display: true,
82
90
  },
83
91
  { deliverAs: "steer" },
@@ -19,24 +19,26 @@ export function searchBoard(
19
19
  args.push(params.channel_name);
20
20
  }
21
21
  if (params.author !== undefined) {
22
- clauses.push(`(author=? OR (kind='summary' AND EXISTS (
23
- SELECT 1 FROM posts a WHERE a.board_id=c.board_id AND a.root=c.thread_id AND a.author=?)))`);
24
- const author = agentPath(params.author, "/root");
25
- args.push(author, author);
22
+ clauses.push(`(author IN (SELECT value FROM json_each(?)) OR (kind='summary' AND EXISTS (
23
+ SELECT 1 FROM posts a WHERE a.board_id=c.board_id AND a.root=c.thread_id AND a.author IN (SELECT value FROM json_each(?)))))`);
24
+ const authors = JSON.stringify(
25
+ store.agentIdentities(agentPath(params.author, "/root")),
26
+ );
27
+ args.push(authors, authors);
26
28
  }
27
29
  if (params.query !== undefined) {
28
30
  clauses.push("instr(body_search,?)>0");
29
31
  args.push(caseFold(params.query));
30
32
  }
31
33
  if (params.after_message_id !== undefined) {
32
- const after = store.one<{ created_at: string; seq: number }>(
33
- "SELECT created_at,seq FROM posts WHERE board_id=? AND id=?",
34
+ const after = store.one<{ seq: number }>(
35
+ "SELECT seq FROM posts WHERE board_id=? AND id=?",
34
36
  board,
35
37
  params.after_message_id,
36
38
  );
37
39
  if (!after) throw new Error("Post not found in this board");
38
- clauses.push("(created_at,seq)>(?,?)");
39
- args.push(after.created_at, after.seq);
40
+ clauses.push("seq>?");
41
+ args.push(after.seq);
40
42
  }
41
43
  const rows = store.all<
42
44
  | (PostRecord & { kind: "post" })
@@ -54,7 +56,7 @@ export function searchBoard(
54
56
  : ""
55
57
  }
56
58
  ) SELECT * FROM candidates c WHERE ${clauses.join(" AND ")}
57
- ORDER BY created_at DESC,seq DESC LIMIT ? OFFSET ?`,
59
+ ORDER BY seq DESC LIMIT ? OFFSET ?`,
58
60
  ...args,
59
61
  limit + 1,
60
62
  offset,