pi-straw-boss 0.114.0 → 0.114.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.114.0",
3
+ "version": "0.114.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",
@@ -1,4 +1,4 @@
1
- import { readdirSync } from "node:fs";
1
+ import { readdirSync, statSync } from "node:fs";
2
2
  import { resolve } from "node:path";
3
3
  import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
4
4
  import { isChildBusy } from "@nubl/maestro/core/child-busy";
@@ -13,6 +13,8 @@ import { ACK_MS, REQUEST_MS, ackFile, isRequestId, nudgeDir, nudgeText, readJson
13
13
  // parent has stopped waiting for it (F007).
14
14
 
15
15
  const POLL_MS = 2000;
16
+ /** A folder's mtime this close to the scan that read it may hide a change made in the same tick of the file system's clock: such a scan is not trusted. */
17
+ const RACY_MS = 2000;
16
18
  /** A nudge sent that has not entered the session this long is acked `lost`. */
17
19
  export const ACCEPT_MS = 30_000;
18
20
 
@@ -30,6 +32,8 @@ export interface InboxCore {
30
32
  pi: Pick<ExtensionAPI, "sendUserMessage"> & { events?: ExtensionAPI["events"] };
31
33
  undelivered?: () => boolean;
32
34
  now?: () => number;
35
+ /** The file system calls of a poll; the tests count them. */
36
+ fs?: { statSync: (dir: string, options: { throwIfNoEntry: false }) => { mtimeMs: number } | undefined; readdirSync: (dir: string) => string[] };
33
37
  }
34
38
 
35
39
  export interface Inbox {
@@ -62,7 +66,10 @@ function onLaunchFile(ctx: Ctx | undefined, file: string): boolean {
62
66
  export function createInbox(d: InboxCore): Inbox {
63
67
  const dir = nudgeDir(d.file, d.runId);
64
68
  const now = d.now ?? Date.now;
69
+ const fs = d.fs ?? { statSync, readdirSync };
65
70
  let lastTurnAt = -Infinity;
71
+ /** The folder's mtime at the last scan, and when that scan ran (wall clock, as the mtime is): an idle poll is one stat. */
72
+ let scanned: { mtime: number; at: number } | undefined;
66
73
  let inflight: { id: string; text: string; at: number; requested: number } | undefined;
67
74
  /** `requested` is when the parent wrote the request: past ACK_MS the parent no longer waits, and an ack would only be left behind. */
68
75
  const ack = (id: string, requested: number, outcome: NudgeOutcome) => {
@@ -93,8 +100,18 @@ export function createInbox(d: InboxCore): Inbox {
93
100
  }
94
101
  let names: string[];
95
102
  try {
96
- names = readdirSync(dir);
103
+ // No folder, the common case: no request, and no error built to say so.
104
+ const mtime = fs.statSync(dir, { throwIfNoEntry: false })?.mtimeMs;
105
+ if (mtime === undefined) {
106
+ scanned = undefined;
107
+ return [];
108
+ }
109
+ if (scanned && scanned.mtime === mtime && scanned.at - mtime > RACY_MS) return [];
110
+ const at = Date.now();
111
+ names = fs.readdirSync(dir);
112
+ scanned = { mtime, at };
97
113
  } catch {
114
+ scanned = undefined;
98
115
  return [];
99
116
  }
100
117
  const outcomes: NudgeOutcome[] = [];