pi-better-subagents 0.32.4 → 0.32.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.
@@ -1,5 +1,5 @@
1
1
  import { createHash } from "node:crypto";
2
- import { readFileSync, readdirSync } from "node:fs";
2
+ import { readFileSync, readdirSync, statSync } from "node:fs";
3
3
  import { homedir, tmpdir } from "node:os";
4
4
  import { join } from "node:path";
5
5
  import { readAssignmentWaits } from "./shared-operation-coordinator/assignment-observation.mjs";
@@ -65,31 +65,135 @@ function readAttempts(options) {
65
65
  const attempts = [];
66
66
  try {
67
67
  for (const id of readdirSync(join(base, "runs"))) {
68
- try {
69
- const candidate = JSON.parse(readFileSync(join(base, "runs", id, "meta.json"), "utf8"));
70
- if (candidate && typeof candidate === "object") attempts.push(candidate);
71
- }
72
- catch { /* A partial unrelated record must not hide a continuation. */ }
68
+ const candidate = readAttempt(base, id);
69
+ if (candidate) attempts.push(candidate);
73
70
  }
74
71
  } catch { /* Registry gaps cannot establish a newer attempt. */ }
75
72
  return attempts;
76
73
  }
77
74
 
75
+ function readAttempt(base, id) {
76
+ try {
77
+ const candidate = JSON.parse(readFileSync(join(base, "runs", id, "meta.json"), "utf8"));
78
+ return candidate && typeof candidate === "object" ? candidate : undefined;
79
+ } catch { /* A partial unrelated record must not hide a continuation. */ }
80
+ }
81
+
82
+ const attemptIndexes = new Map();
83
+ const UNCOMMITTED_RETRY_MS = 1_000;
84
+ const conversationKey = (sessionId, cwd) => JSON.stringify([sessionId, cwd]);
85
+
86
+ function removeIdentity(index, id) {
87
+ const key = index.identities.get(id);
88
+ if (key === undefined) return;
89
+ index.identities.delete(id);
90
+ const group = index.byConversation.get(key);
91
+ group.delete(id);
92
+ if (!group.size) index.byConversation.delete(key);
93
+ }
94
+
95
+ function setIdentity(index, id, candidate) {
96
+ const key = conversationKey(candidate.sessionId, candidate.cwd);
97
+ if (index.identities.get(id) === key) return;
98
+ removeIdentity(index, id);
99
+ index.identities.set(id, key);
100
+ let group = index.byConversation.get(key);
101
+ if (!group) index.byConversation.set(key, group = new Set());
102
+ group.add(id);
103
+ }
104
+
105
+ function runsSignature(base) {
106
+ try {
107
+ const stat = statSync(join(base, "runs"), { bigint: true });
108
+ return `${stat.dev}:${stat.ino}:${stat.size}:${stat.mtimeNs}:${stat.ctimeNs}`;
109
+ } catch { return undefined; }
110
+ }
111
+
112
+ function prepareAttemptIndex(options) {
113
+ const base = options.baseDir ?? join(tmpdir(), "pi-better-subagents");
114
+ const signature = runsSignature(base);
115
+ if (!signature) {
116
+ attemptIndexes.delete(base);
117
+ return { index: { base, byConversation: new Map() } };
118
+ }
119
+ let index = attemptIndexes.get(base);
120
+ let initialAttempts;
121
+ if (!index) {
122
+ let ids;
123
+ try { ids = new Set(readdirSync(join(base, "runs"))); }
124
+ catch { return { index: { base, byConversation: new Map() } }; }
125
+ index = { base, signature, ids, identities: new Map(), byConversation: new Map(),
126
+ uncommitted: new Set(), nextRetryAt: Date.now() + UNCOMMITTED_RETRY_MS };
127
+ initialAttempts = new Map();
128
+ for (const id of ids) {
129
+ const candidate = readAttempt(base, id);
130
+ if (candidate) {
131
+ setIdentity(index, id, candidate);
132
+ initialAttempts.set(id, candidate);
133
+ }
134
+ else index.uncommitted.add(id);
135
+ }
136
+ attemptIndexes.set(base, index);
137
+ if (attemptIndexes.size > 8) attemptIndexes.delete(attemptIndexes.keys().next().value);
138
+ } else if (index.signature !== signature) {
139
+ let ids;
140
+ try { ids = new Set(readdirSync(join(base, "runs"))); }
141
+ catch {
142
+ attemptIndexes.delete(base);
143
+ return { index: { base, byConversation: new Map() } };
144
+ }
145
+ for (const id of index.ids) {
146
+ if (ids.has(id)) continue;
147
+ removeIdentity(index, id);
148
+ index.uncommitted.delete(id);
149
+ }
150
+ for (const id of ids) {
151
+ if (index.ids.has(id)) continue;
152
+ const candidate = readAttempt(base, id);
153
+ if (candidate) setIdentity(index, id, candidate);
154
+ else index.uncommitted.add(id);
155
+ }
156
+ index.ids = ids;
157
+ index.signature = signature;
158
+ }
159
+ if (Date.now() >= index.nextRetryAt) {
160
+ for (const id of index.uncommitted) {
161
+ const candidate = readAttempt(base, id);
162
+ if (!candidate) continue;
163
+ setIdentity(index, id, candidate);
164
+ index.uncommitted.delete(id);
165
+ }
166
+ index.nextRetryAt = Date.now() + UNCOMMITTED_RETRY_MS;
167
+ }
168
+ return { index, initialAttempts };
169
+ }
170
+
171
+ function readConversationAttempts(index, meta, initialAttempts) {
172
+ const matches = [];
173
+ for (const id of index.byConversation.get(conversationKey(meta.sessionId, meta.cwd)) ?? []) {
174
+ const fresh = initialAttempts?.get(id) ?? readAttempt(index.base, id);
175
+ if (!fresh) {
176
+ removeIdentity(index, id);
177
+ index.uncommitted.add(id);
178
+ continue;
179
+ }
180
+ setIdentity(index, id, fresh);
181
+ if (fresh.sessionId === meta.sessionId && fresh.cwd === meta.cwd) matches.push(fresh);
182
+ }
183
+ return matches;
184
+ }
185
+
78
186
  /** One synchronous UI refresh shares a snapshot; the next refresh reads disk again. */
79
187
  export function createAssignmentProjector(options = {}) {
80
- let snapshot;
188
+ let prepared;
189
+ const snapshots = new Map();
81
190
  const readSnapshot = (meta) => {
82
- if (!snapshot) {
83
- snapshot = new Map();
84
- for (const candidate of readAttempts(options)) {
85
- let workspaces = snapshot.get(candidate.sessionId);
86
- if (!workspaces) snapshot.set(candidate.sessionId, workspaces = new Map());
87
- let attempts = workspaces.get(candidate.cwd);
88
- if (!attempts) workspaces.set(candidate.cwd, attempts = []);
89
- attempts.push(candidate);
90
- }
191
+ const key = conversationKey(meta.sessionId, meta.cwd);
192
+ if (!snapshots.has(key)) {
193
+ prepared ??= prepareAttemptIndex(options);
194
+ snapshots.set(key, readConversationAttempts(prepared.index, meta, prepared.initialAttempts));
91
195
  }
92
- return snapshot.get(meta.sessionId)?.get(meta.cwd) ?? [];
196
+ return snapshots.get(key);
93
197
  };
94
198
  return (meta, processStatus = meta.status) => assignmentFromSnapshot(meta, processStatus, options,
95
199
  () => readSnapshot(meta));
package/index.ts CHANGED
@@ -2355,11 +2355,15 @@ export default function (pi: ExtensionAPI) {
2355
2355
  const cfg = loadConfig();
2356
2356
  const savedSkillValue = cfg.defaultSkills;
2357
2357
  const hasSavedSkillChoice = savedSkillValue !== undefined && savedSkillValue !== null;
2358
- // Like skills, only save a timing key this session overrides or the
2359
- // config already carries; built-in defaults need no stored copy.
2358
+ // An environment timeout shown on the page becomes the saved
2359
+ // default; an untouched built-in default still needs no key.
2360
2360
  const savedTiming: { maxMinutes?: number; toolWaitMinutes?: number; stuckMinutes?: number } = {};
2361
+ const timeout = timingState();
2361
2362
  if (timingOverride.maxMinutes !== undefined) {
2362
2363
  savedTiming.maxMinutes = timingOverride.maxMinutes;
2364
+ } else if (timeout.timeoutSource === "environment") {
2365
+ if (timeout.timingError) return { ok: false, message: `Could not save subagent defaults: ${timeout.timingError}` };
2366
+ savedTiming.maxMinutes = timeout.maxMinutes;
2363
2367
  } else if (isTimingMinutes(cfg.maxMinutes)) {
2364
2368
  savedTiming.maxMinutes = cfg.maxMinutes;
2365
2369
  }
@@ -2382,7 +2386,10 @@ export default function (pi: ExtensionAPI) {
2382
2386
  defaultSkills: skills.mode === "inherit" ? null : [...skills.paths],
2383
2387
  }),
2384
2388
  });
2389
+ const sessionTimeout = timingOverride.maxMinutes;
2385
2390
  resetSettings();
2391
+ // The session timeout beats the environment, even after saving defaults.
2392
+ if (sessionTimeout !== undefined) changeTiming("maxMinutes", sessionTimeout);
2386
2393
  return { ok: true, message: "Subagent defaults saved." };
2387
2394
  } catch (error) {
2388
2395
  return { ok: false, message: `Could not save subagent defaults: ${error instanceof Error ? error.message : String(error)}` };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-better-subagents",
3
- "version": "0.32.4",
3
+ "version": "0.32.6",
4
4
  "description": "Pi extension for detached, sandboxed subagent runs that keep the foreground session free.",
5
5
  "license": "MIT",
6
6
  "type": "module",