pi-dev-flow 0.53.1 → 0.53.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/CHANGELOG.md CHANGED
@@ -4,6 +4,13 @@ Format: newest first. `Unreleased` collects what's merged but not yet tagged.
4
4
 
5
5
  ## [Unreleased]
6
6
 
7
+ ## [0.53.2] - 2026-10-22
8
+
9
+ ### Fixed
10
+
11
+ - **A claim now dies with its session, not with the process.** Dev-flow's session state is module-level, and pi runs many sessions inside one long-lived Node process (pi-web's agent lives in the server) — so bindings and the compaction flag outlived the session that wrote them, and claim liveness asked only `pidAlive(claim.pid)`, a pid that never goes away in a shared host. The result was a lock nobody could see the owner of: one pi-web session starts a feature, that session ends, and every later session in the repo is refused the auto-bind and told the feature `is claimed by another live session` — forever, because the claim's pid is the server itself. `session_shutdown` now releases the dead session's binding, its compaction flag and every claim it held; a restored session (reload, resume, fork) reclaims what its own branch history says it holds, and a foreign live claim still wins, because restoring is not a takeover.
12
+ - **Claim keys say what kind of identity they are.** A key was either pi's session id or, when the host hands out none, the working directory — indistinguishable in `claim.json`, which made a shared-directory collision read exactly like a session that had gone missing. Keys are now `sid:<session id>` and `cwd:<directory>`, and `flow status` prints the session's own key (saying plainly when it is the directory one, because then every pi session in that directory shares a single dev-flow binding and must pass `flow use <slug>`). One migration seam, expected and harmless: a claim written by an older build carries the old bare key, so the first rival session after this upgrade needs one explicit `flow use` — the same path a takeover already takes.
13
+
7
14
  ## [0.53.1] - 2026-10-22
8
15
 
9
16
  ### Fixed
@@ -13,6 +13,17 @@ passing another session's slug to `flow approve`/`verify`/`finish` is refused un
13
13
  or two sessions quietly overwrite one state file. `flow use <slug>` switches, and ambiguous sessions must
14
14
  bind or pass explicit slugs. With nothing unfinished and no binding, the session must start a
15
15
  flow — work does not happen outside the flow.
16
+
17
+ **A claim dies with its session, not with its process.** Hosts like pi-web run many sessions inside
18
+ one long-lived Node process, so the pid on a claim says nothing about whether its holder still
19
+ exists — `session_shutdown` releases the dead session's binding and every claim it held, and a
20
+ restored session (reload, resume, fork) reclaims what its own branch history says it holds. A claim
21
+ key names the identity it came from: `sid:<session id>` when pi provides one, `cwd:<directory>`
22
+ when the host hands out none — and in that second case every session in that directory shares one
23
+ binding, so `flow status` prints the key and says so instead of leaving a shared binding to be
24
+ guessed from a path. Dev-flow keeps no per-process session state beyond that keyed map, released on
25
+ shutdown; the one other process-wide cache (`compile()` in `src/core.ts`) holds flag-less regexes,
26
+ which carry no mutable state to leak between sessions.
16
27
  Enforcement is by attribution: a write inside worktree X is judged
17
28
  by X's own feature stage no matter which session touches it; main-checkout code stays closed
18
29
  while your bound feature is in `plan`/`implement` and while any feature is unbound-and-ambiguous.
@@ -188,9 +188,17 @@ function atomicWrite(file: string, text: string): void {
188
188
  renameSync(tmp, file);
189
189
  }
190
190
 
191
- /** Session bindings: one flow per pi session; persisted per session branch via appendEntry. */
191
+ /** Session bindings: one flow per pi session; persisted per session branch via appendEntry.
192
+ * Module-level means shared by every session in the process — pi runs many sessions inside one Node
193
+ * process (pi-web's agent lives in the server) — so every key here is a session identity and every
194
+ * entry is released when that session ends (see releaseSession). */
192
195
  const bindings = new Map<string, string | null>();
193
- const sessionKey = (ctx: { cwd: string; sessionManager?: { getSessionId(): string } }) => ctx.sessionManager?.getSessionId() ?? ctx.cwd;
196
+ // Two identities are possible: pi's session id, or — when the host hands out none — the working
197
+ // directory. Both are prefixed, because a claim written under the directory identity is not a session:
198
+ // two pi sessions sharing one directory share that key, and `cwd:` in a claim file says so out loud
199
+ // instead of reading like a session that went missing.
200
+ const sessionKey = (ctx: { cwd: string; sessionManager?: { getSessionId(): string } }): string =>
201
+ ctx.sessionManager?.getSessionId() ? `sid:${ctx.sessionManager.getSessionId()}` : `cwd:${ctx.cwd}`;
194
202
  /** Sessions with a dev-flow-initiated compaction in flight (prevents stacked compactions). */
195
203
  const compacting = new Set<string>();
196
204
 
@@ -247,6 +255,20 @@ function readClaim(root: string, slug: string): SessionClaim | null {
247
255
  const writeClaim = (root: string, slug: string, key: string) =>
248
256
  atomicWrite(claimFile(root, slug), `${JSON.stringify({ key, pid: process.pid, at: now() })}\n`);
249
257
 
258
+ /** Let go of everything one session held. In a shared-process host the pid on a claim is the server's and
259
+ * never dies, so a claim left behind by a finished session keeps refusing every later session with
260
+ * "claimed by another live session" — the lock has to end with the session that wrote it. Idempotent:
261
+ * shutdown, reload and process exit can converge on this path. */
262
+ function releaseSession(root: string, key: string): void {
263
+ bindings.delete(key);
264
+ compacting.delete(key);
265
+ if (!existsSync(dfDir(root))) return;
266
+ for (const d of readdirSync(dfDir(root), { withFileTypes: true })) {
267
+ if (!d.isDirectory()) continue;
268
+ if (readClaim(root, d.name)?.key === key) rmSync(claimFile(root, d.name), { force: true });
269
+ }
270
+ }
271
+
250
272
  /** The auto-bind this session was denied by another live session's claim — lets the unbound-context message name the feature. */
251
273
  function claimBlocker(root: string, key: string): { state: FlowState; claim: SessionClaim } | null {
252
274
  const un = unfinishedStates(root);
@@ -639,6 +661,9 @@ ${mdDigest(draftRefresh)}`))
639
661
  ];
640
662
  });
641
663
  if (warn.length) lines.push(...warn);
664
+ // which identity this session's state is keyed by: a claim fight is undiagnosable when the key
665
+ // could be a session id or a shared directory and the output does not say which
666
+ lines.push(`session: ${sess.key}${sess.key.startsWith("cwd:") ? " — no session id from pi, so every pi session in this directory shares one dev-flow binding (pass flow use <slug> in each)" : ""}`);
642
667
  const refl = readReflections(root);
643
668
  if (refl.length) lines.push(`↻ ${refl.length} reflection(s) open — run flow doctor (it reviews and routes them)`);
644
669
  lines.push(...rmapLine);
@@ -1316,7 +1341,12 @@ export default function devFlow(pi: ExtensionAPI) {
1316
1341
  const ce = e as unknown as { type: string; customType?: string; data?: { slug?: string | null } };
1317
1342
  if (ce.type === "custom" && ce.customType === "dev-flow-bind") slug = ce.data?.slug ?? null;
1318
1343
  }
1319
- bindings.set(sessionKey(ctx), slug);
1344
+ const key = sessionKey(ctx);
1345
+ bindings.set(key, slug);
1346
+ // A reload ends the old session, which released its claim; the restored one must reclaim what its own
1347
+ // branch says it holds, or the repo answers "claimed by another live session" to the session that
1348
+ // holds it. A foreign live claim wins — restoring is not a takeover.
1349
+ if (slug && readState(ctx.cwd, slug) && !claimBlocks(readClaim(ctx.cwd, slug), key)) writeClaim(ctx.cwd, slug, key);
1320
1350
  };
1321
1351
 
1322
1352
  pi.registerCommand("flow", {
@@ -1355,6 +1385,12 @@ export default function devFlow(pi: ExtensionAPI) {
1355
1385
  refreshStatus(ctx);
1356
1386
  });
1357
1387
  pi.on("session_tree", async (_event, ctx) => rebuildSession(ctx));
1388
+ // The session, not the process, owns the binding and the claim: pi-web keeps serving other sessions
1389
+ // after one ends, and a stale claim there is a permanent lock (its pid is the server's, always alive).
1390
+ pi.on("session_shutdown", async (_event, ctx) => {
1391
+ if (!existsSync(dfDir(ctx.cwd))) return;
1392
+ releaseSession(ctx.cwd, sessionKey(ctx));
1393
+ });
1358
1394
  pi.on("turn_end", async (_event, ctx) => {
1359
1395
  refreshStatus(ctx);
1360
1396
  // pi checks its compaction threshold between every turn; our safe points must be as frequent.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-dev-flow",
3
- "version": "0.53.1",
3
+ "version": "0.53.2",
4
4
  "description": "Enforced feature workflow for Pi: plan → implement → pre-release → release, gated checkpoints, self-improvement reflections, one git worktree per feature, changelog + publish as part of the release",
5
5
  "type": "module",
6
6
  "keywords": [
package/src/core.ts CHANGED
@@ -894,6 +894,9 @@ const READ_ONLY_GIT = new Set(["log", "show", "diff", "status", "blame", "cat-fi
894
894
  const GIT_INVOKED = /(^|[\s/;|&(])git\s+(\S+)/;
895
895
 
896
896
  // project.md is re-parsed on every tool call; compiled rules are cached by source.
897
+ // Process-global on purpose, and safe only because compile() builds flag-less regexes: no /g and no /y
898
+ // means no lastIndex, so a shared RegExp carries nothing mutable between sessions. Add a stateful flag
899
+ // here and this cache becomes cross-session state — the one thing this package must never keep.
897
900
  const regexCache = new Map<string, RegExp | null>();
898
901
  function compile(source: string): RegExp | null {
899
902
  if (!regexCache.has(source)) {