@polderlabs/bizar-plugin 0.5.4

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 (60) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +448 -0
  3. package/bun.lock +88 -0
  4. package/index.ts +1113 -0
  5. package/package.json +42 -0
  6. package/scripts/check-forbidden-imports.sh +33 -0
  7. package/src/background-state.ts +463 -0
  8. package/src/background.ts +964 -0
  9. package/src/commands-impl.ts +369 -0
  10. package/src/commands.ts +880 -0
  11. package/src/event-stream.ts +574 -0
  12. package/src/fingerprint.ts +120 -0
  13. package/src/handoff.ts +79 -0
  14. package/src/http-client.ts +467 -0
  15. package/src/logger.ts +144 -0
  16. package/src/loop.ts +176 -0
  17. package/src/options.ts +421 -0
  18. package/src/plan-fs.ts +323 -0
  19. package/src/report.ts +178 -0
  20. package/src/research-prompt.ts +35 -0
  21. package/src/serve.ts +476 -0
  22. package/src/settings.ts +349 -0
  23. package/src/state.ts +298 -0
  24. package/src/tools/bg-collect.ts +104 -0
  25. package/src/tools/bg-get-comments.ts +239 -0
  26. package/src/tools/bg-kill.ts +87 -0
  27. package/src/tools/bg-spawn.ts +263 -0
  28. package/src/tools/bg-status.ts +99 -0
  29. package/src/tools/plan-action.ts +767 -0
  30. package/src/tools/wait-for-feedback.ts +402 -0
  31. package/tests/attach-handler-bug.test.ts +166 -0
  32. package/tests/background-state.test.ts +277 -0
  33. package/tests/background.test.ts +402 -0
  34. package/tests/block.test.ts +193 -0
  35. package/tests/canonical-key-order.test.ts +71 -0
  36. package/tests/commands-impl.test.ts +442 -0
  37. package/tests/commands.test.ts +548 -0
  38. package/tests/config.test.ts +122 -0
  39. package/tests/dispose.test.ts +336 -0
  40. package/tests/event-stream.test.ts +409 -0
  41. package/tests/event.test.ts +262 -0
  42. package/tests/fingerprint.test.ts +161 -0
  43. package/tests/http-client.test.ts +403 -0
  44. package/tests/init-helpers.test.ts +203 -0
  45. package/tests/integration/slash-command.test.ts +348 -0
  46. package/tests/integration/tool-routing.test.ts +314 -0
  47. package/tests/loop.test.ts +397 -0
  48. package/tests/options.test.ts +274 -0
  49. package/tests/serve.test.ts +335 -0
  50. package/tests/settings.test.ts +351 -0
  51. package/tests/stall-think.test.ts +749 -0
  52. package/tests/state.test.ts +275 -0
  53. package/tests/tools/bg-collect.test.ts +337 -0
  54. package/tests/tools/bg-get-comments.test.ts +485 -0
  55. package/tests/tools/bg-kill.test.ts +231 -0
  56. package/tests/tools/bg-spawn.test.ts +311 -0
  57. package/tests/tools/bg-status.test.ts +216 -0
  58. package/tests/tools/plan-action.test.ts +599 -0
  59. package/tests/tools/wait-for-feedback.test.ts +390 -0
  60. package/tsconfig.json +29 -0
@@ -0,0 +1,349 @@
1
+ /**
2
+ * settings.ts
3
+ *
4
+ * v0.4.0 — Plan settings store.
5
+ *
6
+ * Persists user-controlled settings that drive the visual-plan flow
7
+ * (`/visual-plan on|off`, last-used plan slug, default template).
8
+ *
9
+ * Spec contract:
10
+ * - §4.7 — corrupt-state fallback. A corrupt or missing file returns
11
+ * `DEFAULT_PLAN_SETTINGS`. Never throws.
12
+ * - §7.6 — no raw message content is logged. Only metadata strings.
13
+ * - §8.2 — mkdirSync on init with EACCES/EROFS handling; if creation
14
+ * fails the store returns defaults rather than throwing.
15
+ *
16
+ * The file is stored at `~/.cache/bizar/plan-settings.json`. We
17
+ * `expandHome` in the constructor, just like `StateStore` does.
18
+ *
19
+ * Atomic writes use the same `writeFileSync(tmp) + renameSync(tmp, final)`
20
+ * pattern as `state.ts`. We use a single in-memory mutex for write
21
+ * serialization (concurrent `update()` calls must not lose data).
22
+ *
23
+ * Validation:
24
+ * - `visualPlanEnabled` — must be a boolean.
25
+ * - `defaultTemplate` — must be one of the four known literals.
26
+ * - `lastUsedSlug` — must be `string | null`.
27
+ *
28
+ * Invalid values are clamped to defaults (and a warning is logged).
29
+ * The store never throws on bad input.
30
+ */
31
+
32
+ import {
33
+ readFileSync,
34
+ writeFileSync,
35
+ renameSync,
36
+ unlinkSync,
37
+ existsSync,
38
+ mkdirSync,
39
+ } from "node:fs";
40
+ import path from "node:path";
41
+ import os from "node:os";
42
+
43
+ import type { Logger } from "./logger.js";
44
+
45
+ // --- Public types --------------------------------------------------------
46
+
47
+ export type DefaultTemplate =
48
+ | "blank"
49
+ | "feature-design"
50
+ | "bug-investigation"
51
+ | "decision-record";
52
+
53
+ export const KNOWN_TEMPLATES: readonly DefaultTemplate[] = [
54
+ "blank",
55
+ "feature-design",
56
+ "bug-investigation",
57
+ "decision-record",
58
+ ] as const;
59
+
60
+ export interface PlanSettings {
61
+ /** When true, the agent's first action on a complex task is to
62
+ * call `bizar_plan_action` (e.g. with `action: "get_canvas"` or `add_element`)
63
+ * and `bizar_wait_for_feedback` to coordinate with the visual plan. */
64
+ visualPlanEnabled: boolean;
65
+ /** Default template to use when `/plan new` is invoked without one. */
66
+ defaultTemplate: DefaultTemplate;
67
+ /** Last plan slug the user opened/created. Surfaces as a default suggestion. */
68
+ lastUsedSlug: string | null;
69
+ }
70
+
71
+ export const DEFAULT_PLAN_SETTINGS: PlanSettings = {
72
+ visualPlanEnabled: false,
73
+ defaultTemplate: "blank",
74
+ lastUsedSlug: null,
75
+ };
76
+
77
+ // --- Implementation -------------------------------------------------------
78
+
79
+ /** Expand a leading `~` to the user's home directory. */
80
+ export function expandHome(p: string): string {
81
+ if (p === "~") return os.homedir();
82
+ if (p.startsWith("~/") || p.startsWith("~\\")) {
83
+ return path.join(os.homedir(), p.slice(2));
84
+ }
85
+ return p;
86
+ }
87
+
88
+ /**
89
+ * Validates an unknown value as a `PlanSettings` shape. Returns either a
90
+ * fully-validated `PlanSettings` (with invalid fields clamped to the
91
+ * defaults) or `null` if the input is not an object at all.
92
+ *
93
+ * Validation rules:
94
+ * - `visualPlanEnabled`: must be a boolean, else `false`.
95
+ * - `defaultTemplate`: must be one of `KNOWN_TEMPLATES`, else `"blank"`.
96
+ * - `lastUsedSlug`: must be `string` or `null`, else `null`.
97
+ */
98
+ function validateSettings(raw: unknown): PlanSettings | null {
99
+ if (raw === null || typeof raw !== "object" || Array.isArray(raw)) {
100
+ return null;
101
+ }
102
+ const obj = raw as Record<string, unknown>;
103
+
104
+ // visualPlanEnabled
105
+ const visualPlanEnabled =
106
+ typeof obj.visualPlanEnabled === "boolean" ? obj.visualPlanEnabled : false;
107
+
108
+ // defaultTemplate
109
+ let defaultTemplate: DefaultTemplate = "blank";
110
+ if (
111
+ typeof obj.defaultTemplate === "string" &&
112
+ (KNOWN_TEMPLATES as readonly string[]).includes(obj.defaultTemplate)
113
+ ) {
114
+ defaultTemplate = obj.defaultTemplate as DefaultTemplate;
115
+ }
116
+
117
+ // lastUsedSlug
118
+ let lastUsedSlug: string | null = null;
119
+ if (typeof obj.lastUsedSlug === "string" && obj.lastUsedSlug !== "") {
120
+ lastUsedSlug = obj.lastUsedSlug;
121
+ } else if (obj.lastUsedSlug === null) {
122
+ lastUsedSlug = null;
123
+ }
124
+
125
+ return { visualPlanEnabled, defaultTemplate, lastUsedSlug };
126
+ }
127
+
128
+ /**
129
+ * Recursive mkdirSync with EACCES/EROFS handling. Returns true if the
130
+ * directory is usable, false otherwise. Mirrors `state.ts`.
131
+ */
132
+ function ensureDir(dir: string, logger: Logger): boolean {
133
+ try {
134
+ mkdirSync(dir, { recursive: true });
135
+ return true;
136
+ } catch (err: unknown) {
137
+ const code = (err as NodeJS.ErrnoException).code;
138
+ if (code === "EACCES" || code === "EROFS") {
139
+ logger.log({
140
+ level: "error",
141
+ message: `bizar: cannot create settings directory ${dir}: ${String(err)}`,
142
+ });
143
+ return false;
144
+ }
145
+ throw err;
146
+ }
147
+ }
148
+
149
+ /**
150
+ * Persist settings atomically: write to .tmp file then rename. Mirrors
151
+ * `state.ts` `writeStateAtomic`. Best-effort: never throws, logs on failure.
152
+ */
153
+ function writeSettingsAtomic(
154
+ filePath: string,
155
+ settings: PlanSettings,
156
+ logger: Logger,
157
+ ): void {
158
+ const tmp = `${filePath}.tmp`;
159
+ try {
160
+ writeFileSync(tmp, JSON.stringify(settings, null, 2), "utf8");
161
+ renameSync(tmp, filePath);
162
+ } catch (err: unknown) {
163
+ logger.log({
164
+ level: "warn",
165
+ message: `bizar: failed to write plan settings ${filePath}: ${String(err)}`,
166
+ });
167
+ try {
168
+ if (existsSync(tmp)) unlinkSync(tmp);
169
+ } catch {
170
+ // non-fatal
171
+ }
172
+ }
173
+ }
174
+
175
+ /**
176
+ * Read settings from disk. Returns DEFAULT_PLAN_SETTINGS on missing or
177
+ * corrupt file (with a warning logged). Never throws.
178
+ */
179
+ function readSettings(filePath: string, logger: Logger): PlanSettings {
180
+ try {
181
+ const raw = readFileSync(filePath, "utf8");
182
+ let parsed: unknown;
183
+ try {
184
+ parsed = JSON.parse(raw) as unknown;
185
+ } catch {
186
+ logger.log({
187
+ level: "warn",
188
+ message: `bizar: corrupt plan settings file at ${filePath}, using defaults`,
189
+ });
190
+ return { ...DEFAULT_PLAN_SETTINGS };
191
+ }
192
+ const validated = validateSettings(parsed);
193
+ if (validated === null) {
194
+ logger.log({
195
+ level: "warn",
196
+ message: `bizar: corrupt plan settings file at ${filePath}, using defaults`,
197
+ });
198
+ return { ...DEFAULT_PLAN_SETTINGS };
199
+ }
200
+ return validated;
201
+ } catch (err: unknown) {
202
+ // Missing file → ENOENT. Other read errors are surfaced.
203
+ const code = (err as NodeJS.ErrnoException).code;
204
+ if (code !== "ENOENT") {
205
+ logger.log({
206
+ level: "warn",
207
+ message: `bizar: failed to read plan settings ${filePath}: ${String(err)}`,
208
+ });
209
+ }
210
+ return { ...DEFAULT_PLAN_SETTINGS };
211
+ }
212
+ }
213
+
214
+ /**
215
+ * Per-store async mutex. Writes through this mutex so two concurrent
216
+ * `update()` calls don't lose data. Reads bypass the mutex (the file
217
+ * write is atomic via rename, and we always re-read after write).
218
+ */
219
+ async function withLock<T>(
220
+ lock: { chain: Promise<unknown> },
221
+ fn: () => Promise<T>,
222
+ ): Promise<T> {
223
+ const prev = lock.chain ?? Promise.resolve();
224
+ const next = prev.then(fn, fn);
225
+ lock.chain = next.catch(() => {});
226
+ return next;
227
+ }
228
+
229
+ export class SettingsStore {
230
+ private filePath: string;
231
+ private settingsDir: string;
232
+ private logger: Logger;
233
+ private initialized = false;
234
+ /** Single-instance mutex (settings are global, not per-session). */
235
+ private lock = { chain: Promise.resolve() as Promise<unknown> };
236
+
237
+ /**
238
+ * @param settingsDir Directory containing the settings file. Typically
239
+ * `"~/.cache/bizar"`. The constructor
240
+ * expands `~` to the home directory.
241
+ */
242
+ constructor(settingsDir: string, logger: Logger) {
243
+ this.settingsDir = expandHome(settingsDir);
244
+ this.filePath = path.join(this.settingsDir, "plan-settings.json");
245
+ this.logger = logger;
246
+ }
247
+
248
+ /** Lazily ensure the settings directory exists. */
249
+ private ensureSettingsDir(): boolean {
250
+ if (this.initialized) return true;
251
+ this.initialized = true;
252
+ return ensureDir(this.settingsDir, this.logger);
253
+ }
254
+
255
+ /**
256
+ * Read the current settings. Returns DEFAULT_PLAN_SETTINGS if the
257
+ * file is missing or corrupt. Never throws.
258
+ */
259
+ async get(): Promise<PlanSettings> {
260
+ if (!this.ensureSettingsDir()) return { ...DEFAULT_PLAN_SETTINGS };
261
+ if (!existsSync(this.filePath)) return { ...DEFAULT_PLAN_SETTINGS };
262
+ return readSettings(this.filePath, this.logger);
263
+ }
264
+
265
+ /**
266
+ * Merge a partial patch into the current settings and persist. The
267
+ * returned value is the post-merge state.
268
+ *
269
+ * Invalid values in the patch are silently ignored — the existing
270
+ * field is preserved (we don't clamp it to a default). Use `set()`
271
+ * for type-checked setters that log a warning on rejection.
272
+ *
273
+ * Concurrent `update()` calls are serialized through the per-store
274
+ * mutex; the on-disk file is always consistent.
275
+ */
276
+ async update(patch: Partial<PlanSettings>): Promise<PlanSettings> {
277
+ if (!this.ensureSettingsDir()) return { ...DEFAULT_PLAN_SETTINGS };
278
+ return withLock(this.lock, async () => {
279
+ const current = await this.get();
280
+ const next: PlanSettings = { ...current };
281
+
282
+ // Validate each patch field independently. Invalid fields are
283
+ // skipped (the existing value is preserved).
284
+ if ("visualPlanEnabled" in patch) {
285
+ const v = patch.visualPlanEnabled;
286
+ if (typeof v === "boolean") {
287
+ next.visualPlanEnabled = v;
288
+ }
289
+ }
290
+ if ("defaultTemplate" in patch) {
291
+ const v = patch.defaultTemplate;
292
+ if (typeof v === "string" && (KNOWN_TEMPLATES as readonly string[]).includes(v)) {
293
+ next.defaultTemplate = v as DefaultTemplate;
294
+ }
295
+ }
296
+ if ("lastUsedSlug" in patch) {
297
+ const v = patch.lastUsedSlug;
298
+ if (v === null) {
299
+ next.lastUsedSlug = null;
300
+ } else if (typeof v === "string" && v !== "") {
301
+ next.lastUsedSlug = v;
302
+ }
303
+ }
304
+
305
+ writeSettingsAtomic(this.filePath, next, this.logger);
306
+ return next;
307
+ });
308
+ }
309
+
310
+ /**
311
+ * Typed setter for a single setting. Returns the post-set state.
312
+ * Unknown keys are rejected with a warning.
313
+ *
314
+ * If the value fails validation, the existing setting is preserved
315
+ * and a warning is logged. The store never throws.
316
+ */
317
+ async set<K extends keyof PlanSettings>(
318
+ setting: K,
319
+ value: PlanSettings[K],
320
+ ): Promise<PlanSettings> {
321
+ if (!this.ensureSettingsDir()) return { ...DEFAULT_PLAN_SETTINGS };
322
+ return withLock(this.lock, async () => {
323
+ const current = await this.get();
324
+ const candidate = validateSettings({ ...current, [setting]: value });
325
+ if (candidate === null) return current;
326
+
327
+ // validateSettings may clamp — only commit if the field actually changed
328
+ // (or if the user-supplied value was valid). This avoids spurious writes
329
+ // when someone calls `set("defaultTemplate", "nonsense")`.
330
+ const desired = candidate[setting];
331
+ if (desired !== value) {
332
+ this.logger.log({
333
+ level: "warn",
334
+ message: `bizar: rejected invalid value for ${String(setting)}: ${JSON.stringify(value)}, keeping ${JSON.stringify(desired)}`,
335
+ });
336
+ return current;
337
+ }
338
+
339
+ const next: PlanSettings = { ...current, [setting]: value };
340
+ writeSettingsAtomic(this.filePath, next, this.logger);
341
+ return next;
342
+ });
343
+ }
344
+
345
+ /** Absolute path of the settings file (for diagnostics/tests). */
346
+ getFilePath(): string {
347
+ return this.filePath;
348
+ }
349
+ }
package/src/state.ts ADDED
@@ -0,0 +1,298 @@
1
+ /**
2
+ * state.ts
3
+ *
4
+ * Per-session state management with atomic writes, per-session mutex,
5
+ * corrupt-state fallback, and stale-session cleanup. Per §4.
6
+ */
7
+
8
+ import { readFileSync, writeFileSync, renameSync, unlinkSync, existsSync, mkdirSync } from "node:fs";
9
+ import path from "node:path";
10
+ import os from "node:os";
11
+
12
+ /**
13
+ * Minimal Logger interface compatible with opencode's client.app.log shape.
14
+ * Matches what Tyr defines in logger.ts.
15
+ */
16
+ export interface Logger {
17
+ log(opts: { level: "debug" | "info" | "warn" | "error"; message: string }): void;
18
+ }
19
+
20
+ /** Per §4.1 schema + §4.7 empty-state shape */
21
+ export interface SessionState {
22
+ sessionId: string;
23
+ parentAgent: string | null;
24
+ startedAt: number;
25
+ lastActivityAt: number;
26
+ turnCount: number;
27
+ toolCalls: Array<{
28
+ tool: string;
29
+ fingerprint: string;
30
+ at: number;
31
+ outcome?: "ok" | "error";
32
+ }>;
33
+ warningsIssued: number;
34
+ blocksTriggered: number;
35
+ }
36
+
37
+ /** Canonical empty-state per §4.7 */
38
+ export const EMPTY_STATE: SessionState = {
39
+ sessionId: "",
40
+ parentAgent: null,
41
+ startedAt: 0,
42
+ lastActivityAt: 0,
43
+ turnCount: 0,
44
+ toolCalls: [],
45
+ warningsIssued: 0,
46
+ blocksTriggered: 0,
47
+ };
48
+
49
+ /** Maximum age in days before a state file is considered stale */
50
+ const STALE_AGE_DAYS = 7;
51
+
52
+ /** Maximum number of tool calls to retain in the rolling window */
53
+ const MAX_TOOL_CALLS = 50;
54
+
55
+ /**
56
+ * Per-session async mutex implemented as a Promise chain keyed by sessionId.
57
+ * Different sessions do not block each other. See §4.3.
58
+ * The lock map is an instance field so each StateStore has its own mutex domain.
59
+ */
60
+ async function withLock<T>(
61
+ locks: Map<string, Promise<unknown>>,
62
+ sessionId: string,
63
+ fn: () => Promise<T>
64
+ ): Promise<T> {
65
+ const prev = locks.get(sessionId) ?? Promise.resolve();
66
+ const next = prev.then(fn, fn);
67
+ locks.set(sessionId, next.catch(() => {}));
68
+ return next;
69
+ }
70
+
71
+ function stateFilePath(stateDir: string, sessionId: string): string {
72
+ return path.join(stateDir, `${sessionId}.json`);
73
+ }
74
+
75
+ function expandHome(p: string): string {
76
+ if (p.startsWith("~/") || p === "~") {
77
+ return path.join(os.homedir(), p.slice(1));
78
+ }
79
+ return p;
80
+ }
81
+
82
+ /**
83
+ * Recursive mkdirSync with EACCES/EROFS handling per §8.2.
84
+ * Returns true if the directory is usable, false if we hit a permission/readonly error.
85
+ */
86
+ function ensureDir(dir: string, logger: Logger): boolean {
87
+ try {
88
+ mkdirSync(dir, { recursive: true });
89
+ return true;
90
+ } catch (err: unknown) {
91
+ const code = (err as NodeJS.ErrnoException).code;
92
+ if (code === "EACCES" || code === "EROFS") {
93
+ logger.log({
94
+ level: "error",
95
+ message: `bizar: cannot create directory ${dir}: ${String(err)}`,
96
+ });
97
+ return false;
98
+ }
99
+ throw err;
100
+ }
101
+ }
102
+
103
+ /**
104
+ * Persist session state atomically: write to .tmp file then rename.
105
+ * Prunes toolCalls to last MAX_TOOL_CALLS entries before writing.
106
+ */
107
+ function writeStateAtomic(
108
+ filePath: string,
109
+ state: SessionState,
110
+ logger: Logger
111
+ ): void {
112
+ // prune rolling window
113
+ const pruned: SessionState = {
114
+ ...state,
115
+ toolCalls: state.toolCalls.slice(-MAX_TOOL_CALLS),
116
+ };
117
+
118
+ const tmp = `${filePath}.tmp`;
119
+ try {
120
+ writeFileSync(tmp, JSON.stringify(pruned), "utf8");
121
+ renameSync(tmp, filePath);
122
+ } catch (err: unknown) {
123
+ logger.log({
124
+ level: "warn",
125
+ message: `bizar: failed to write state file ${filePath}: ${String(err)}`,
126
+ });
127
+ // best-effort: try to clean up the tmp file if it exists
128
+ try {
129
+ if (existsSync(tmp)) unlinkSync(tmp);
130
+ } catch {
131
+ // non-fatal
132
+ }
133
+ }
134
+ }
135
+
136
+ /**
137
+ * Load state from a session file. Returns EMPTY_STATE on missing or corrupt file.
138
+ * Does not throw. See §4.7 corrupt-state fallback.
139
+ */
140
+ function readState(filePath: string, sessionId: string, logger: Logger): SessionState {
141
+ try {
142
+ const raw = readFileSync(filePath, "utf8");
143
+ const parsed = JSON.parse(raw) as SessionState;
144
+
145
+ // Basic schema validation — ensure required fields exist
146
+ if (
147
+ typeof parsed.sessionId !== "string" ||
148
+ !Array.isArray(parsed.toolCalls)
149
+ ) {
150
+ throw new Error("schema mismatch");
151
+ }
152
+
153
+ return parsed;
154
+ } catch (err: unknown) {
155
+ logger.log({
156
+ level: "warn",
157
+ message: `bizar: corrupt state file for session ${sessionId}, starting empty`,
158
+ });
159
+ return { ...EMPTY_STATE, sessionId };
160
+ }
161
+ }
162
+
163
+ export class StateStore {
164
+ private stateDir: string;
165
+ private logger: Logger;
166
+ private initialized = false;
167
+ /** Per-session async mutex — instance-level so each store has independent locks */
168
+ private locks = new Map<string, Promise<unknown>>();
169
+
170
+ constructor(stateDir: string, logger: Logger) {
171
+ // Expand ~ to home directory
172
+ this.stateDir = expandHome(stateDir);
173
+ this.logger = logger;
174
+ }
175
+
176
+ /**
177
+ * Lazily ensure the state directory exists.
178
+ * Returns false if directory creation fails (EACCES/EROFS) — caller should
179
+ * disable the plugin for this session.
180
+ */
181
+ private ensureStateDir(): boolean {
182
+ if (this.initialized) return true;
183
+ this.initialized = true;
184
+ return ensureDir(this.stateDir, this.logger);
185
+ }
186
+
187
+ /**
188
+ * Load state for a session. Returns EMPTY_STATE if the file is missing or corrupt.
189
+ * @see §4.7 corrupt-state fallback
190
+ */
191
+ async load(sessionId: string): Promise<SessionState> {
192
+ if (!this.ensureStateDir()) return { ...EMPTY_STATE, sessionId };
193
+
194
+ const filePath = stateFilePath(this.stateDir, sessionId);
195
+
196
+ if (!existsSync(filePath)) {
197
+ return { ...EMPTY_STATE, sessionId };
198
+ }
199
+
200
+ return Promise.resolve(readState(filePath, sessionId, this.logger));
201
+ }
202
+
203
+ /**
204
+ * Persist session state. Atomic write via rename from .tmp file.
205
+ */
206
+ async save(state: SessionState): Promise<void> {
207
+ if (!this.ensureStateDir()) return;
208
+
209
+ const filePath = stateFilePath(this.stateDir, state.sessionId);
210
+ writeStateAtomic(filePath, state, this.logger);
211
+ return Promise.resolve();
212
+ }
213
+
214
+ /**
215
+ * Delete the state file for a session.
216
+ */
217
+ async delete(sessionId: string): Promise<void> {
218
+ const filePath = stateFilePath(this.stateDir, sessionId);
219
+ try {
220
+ if (existsSync(filePath)) {
221
+ unlinkSync(filePath);
222
+ }
223
+ } catch (err: unknown) {
224
+ this.logger.log({
225
+ level: "warn",
226
+ message: `bizar: failed to delete state file ${filePath}: ${String(err)}`,
227
+ });
228
+ }
229
+ }
230
+
231
+ /**
232
+ * Cleanup stale session files:
233
+ * 1. Files older than STALE_AGE_DAYS (by lastActivityAt)
234
+ * 2. Files whose sessionId is not in the validSessionIds set
235
+ *
236
+ * Returns the number of files deleted.
237
+ */
238
+ async cleanup(
239
+ maxAgeDays: number = STALE_AGE_DAYS,
240
+ validSessionIds: Set<string> = new Set()
241
+ ): Promise<number> {
242
+ if (!this.ensureStateDir()) return 0;
243
+
244
+ const { readdirSync, statSync } = await import("node:fs");
245
+ let deleted = 0;
246
+ const maxAgeMs = maxAgeDays * 24 * 60 * 60 * 1000;
247
+ const now = Date.now();
248
+
249
+ let files: string[];
250
+ try {
251
+ files = readdirSync(this.stateDir).filter((f) => f.endsWith(".json"));
252
+ } catch (err: unknown) {
253
+ this.logger.log({
254
+ level: "warn",
255
+ message: `bizar: failed to read state dir for cleanup: ${String(err)}`,
256
+ });
257
+ return 0;
258
+ }
259
+
260
+ for (const file of files) {
261
+ const filePath = path.join(this.stateDir, file);
262
+ let mtime: number;
263
+ let sessionId: string;
264
+
265
+ try {
266
+ const stat = statSync(filePath);
267
+ mtime = stat.mtimeMs;
268
+ // sessionId is the filename without .json
269
+ sessionId = file.replace(/\.json$/, "");
270
+ } catch {
271
+ // skip files we can't stat
272
+ continue;
273
+ }
274
+
275
+ const tooOld = now - mtime > maxAgeMs;
276
+ const orphaned = validSessionIds.size > 0 && !validSessionIds.has(sessionId);
277
+
278
+ if (tooOld || orphaned) {
279
+ try {
280
+ unlinkSync(filePath);
281
+ deleted++;
282
+ } catch {
283
+ // best-effort: leave files we can't delete
284
+ }
285
+ }
286
+ }
287
+
288
+ return deleted;
289
+ }
290
+
291
+ /**
292
+ * Acquire the per-session mutex for the given sessionId.
293
+ * The callback is serialized with all other state operations for the same session.
294
+ */
295
+ withLock<T>(sessionId: string, fn: () => Promise<T>): Promise<T> {
296
+ return withLock(this.locks, sessionId, fn);
297
+ }
298
+ }