@tintinweb/pi-subagents 0.6.2 → 0.7.0

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/dist/index.js CHANGED
@@ -23,30 +23,36 @@ import { GroupJoinManager } from "./group-join.js";
23
23
  import { resolveAgentInvocationConfig, resolveJoinMode } from "./invocation-config.js";
24
24
  import { resolveModel } from "./model-resolver.js";
25
25
  import { createOutputFilePath, streamToOutputFile, writeInitialEntry } from "./output-file.js";
26
+ import { SubagentScheduler } from "./schedule.js";
27
+ import { resolveStorePath, ScheduleStore } from "./schedule-store.js";
26
28
  import { applyAndEmitLoaded, saveAndEmitChanged } from "./settings.js";
27
29
  import { AgentWidget, describeActivity, formatDuration, formatMs, formatTokens, formatTurns, getDisplayName, getPromptModeLabel, SPINNER, } from "./ui/agent-widget.js";
30
+ import { showSchedulesMenu } from "./ui/schedule-menu.js";
31
+ import { addUsage, getLifetimeTotal, getSessionContextPercent } from "./usage.js";
28
32
  // ---- Shared helpers ----
29
33
  /** Tool execute return value for a text response. */
30
34
  function textResult(msg, details) {
31
35
  return { content: [{ type: "text", text: msg }], details: details };
32
36
  }
33
- /** Safe token formatting wraps session.getSessionStats() in try-catch. */
34
- function safeFormatTokens(session) {
35
- if (!session)
36
- return "";
37
- try {
38
- return formatTokens(session.getSessionStats().tokens.total);
39
- }
40
- catch {
41
- return "";
42
- }
37
+ /** Format an agent's lifetime token total, or "" when zero. */
38
+ function formatLifetimeTokens(o) {
39
+ const t = getLifetimeTotal(o.lifetimeUsage);
40
+ return t > 0 ? formatTokens(t) : "";
43
41
  }
44
42
  /**
45
43
  * Create an AgentActivity state and spawn callbacks for tracking tool usage.
46
44
  * Used by both foreground and background paths to avoid duplication.
47
45
  */
48
46
  function createActivityTracker(maxTurns, onStreamUpdate) {
49
- const state = { activeTools: new Map(), toolUses: 0, turnCount: 1, maxTurns, tokens: "", responseText: "", session: undefined };
47
+ const state = {
48
+ activeTools: new Map(),
49
+ toolUses: 0,
50
+ turnCount: 1,
51
+ maxTurns,
52
+ responseText: "",
53
+ session: undefined,
54
+ lifetimeUsage: { input: 0, output: 0, cacheWrite: 0 },
55
+ };
50
56
  const callbacks = {
51
57
  onToolActivity: (activity) => {
52
58
  if (activity.type === "start") {
@@ -61,7 +67,6 @@ function createActivityTracker(maxTurns, onStreamUpdate) {
61
67
  }
62
68
  state.toolUses++;
63
69
  }
64
- state.tokens = safeFormatTokens(state.session);
65
70
  onStreamUpdate?.();
66
71
  },
67
72
  onTextDelta: (_delta, fullText) => {
@@ -75,6 +80,10 @@ function createActivityTracker(maxTurns, onStreamUpdate) {
75
80
  onSessionCreated: (session) => {
76
81
  state.session = session;
77
82
  },
83
+ onAssistantUsage: (usage) => {
84
+ addUsage(state.lifetimeUsage, usage);
85
+ onStreamUpdate?.();
86
+ },
78
87
  };
79
88
  return { state, callbacks };
80
89
  }
@@ -105,14 +114,10 @@ function escapeXml(s) {
105
114
  function formatTaskNotification(record, resultMaxLen) {
106
115
  const status = getStatusLabel(record.status, record.error);
107
116
  const durationMs = record.completedAt ? record.completedAt - record.startedAt : 0;
108
- let totalTokens = 0;
109
- try {
110
- if (record.session) {
111
- const stats = record.session.getSessionStats();
112
- totalTokens = stats.tokens?.total ?? 0;
113
- }
114
- }
115
- catch { /* session stats unavailable */ }
117
+ const totalTokens = getLifetimeTotal(record.lifetimeUsage);
118
+ const contextPercent = getSessionContextPercent(record.session);
119
+ const ctxXml = contextPercent !== null ? `<context_percent>${Math.round(contextPercent)}</context_percent>` : "";
120
+ const compactXml = record.compactionCount ? `<compactions>${record.compactionCount}</compactions>` : "";
116
121
  const resultPreview = record.result
117
122
  ? record.result.length > resultMaxLen
118
123
  ? record.result.slice(0, resultMaxLen) + "\n...(truncated, use get_subagent_result for full output)"
@@ -126,7 +131,7 @@ function formatTaskNotification(record, resultMaxLen) {
126
131
  `<status>${escapeXml(status)}</status>`,
127
132
  `<summary>Agent "${escapeXml(record.description)}" ${record.status}</summary>`,
128
133
  `<result>${escapeXml(resultPreview)}</result>`,
129
- `<usage><total_tokens>${totalTokens}</total_tokens><tool_uses>${record.toolUses}</tool_uses><duration_ms>${durationMs}</duration_ms></usage>`,
134
+ `<usage><total_tokens>${totalTokens}</total_tokens><tool_uses>${record.toolUses}</tool_uses>${ctxXml}${compactXml}<duration_ms>${durationMs}</duration_ms></usage>`,
130
135
  `</task-notification>`,
131
136
  ].filter(Boolean).join('\n');
132
137
  }
@@ -135,7 +140,7 @@ function buildDetails(base, record, activity, overrides) {
135
140
  return {
136
141
  ...base,
137
142
  toolUses: record.toolUses,
138
- tokens: safeFormatTokens(record.session),
143
+ tokens: formatLifetimeTokens(record),
139
144
  turnCount: activity?.turnCount,
140
145
  maxTurns: activity?.maxTurns,
141
146
  durationMs: (record.completedAt ?? Date.now()) - record.startedAt,
@@ -147,12 +152,7 @@ function buildDetails(base, record, activity, overrides) {
147
152
  }
148
153
  /** Build notification details for the custom message renderer. */
149
154
  function buildNotificationDetails(record, resultMaxLen, activity) {
150
- let totalTokens = 0;
151
- try {
152
- if (record.session)
153
- totalTokens = record.session.getSessionStats().tokens?.total ?? 0;
154
- }
155
- catch { }
155
+ const totalTokens = getLifetimeTotal(record.lifetimeUsage);
156
156
  return {
157
157
  id: record.id,
158
158
  description: record.description,
@@ -299,18 +299,15 @@ export default function (pi) {
299
299
  /** Helper: build event data for lifecycle events from an AgentRecord. */
300
300
  function buildEventData(record) {
301
301
  const durationMs = record.completedAt ? record.completedAt - record.startedAt : Date.now() - record.startedAt;
302
- let tokens;
303
- try {
304
- if (record.session) {
305
- const stats = record.session.getSessionStats();
306
- tokens = {
307
- input: stats.tokens?.input ?? 0,
308
- output: stats.tokens?.output ?? 0,
309
- total: stats.tokens?.total ?? 0,
310
- };
311
- }
312
- }
313
- catch { /* session stats unavailable */ }
302
+ // All three fields are lifetime-accumulated (Σ over every assistant message_end),
303
+ // so they survive compaction together — input + output ≤ total always.
304
+ // tokens is omitted when nothing was ever produced (e.g. agent errored before
305
+ // any message_end fired), preserving prior payload shape.
306
+ const u = record.lifetimeUsage;
307
+ const total = getLifetimeTotal(u);
308
+ const tokens = total > 0
309
+ ? { input: u.input, output: u.output, total }
310
+ : undefined;
314
311
  return {
315
312
  id: record.id,
316
313
  type: record.type,
@@ -367,6 +364,16 @@ export default function (pi) {
367
364
  type: record.type,
368
365
  description: record.description,
369
366
  });
367
+ }, (record, info) => {
368
+ // Emit compacted event when agent's session compacts (preserves count on record).
369
+ pi.events.emit("subagents:compacted", {
370
+ id: record.id,
371
+ type: record.type,
372
+ description: record.description,
373
+ reason: info.reason,
374
+ tokensBefore: info.tokensBefore,
375
+ compactionCount: record.compactionCount,
376
+ });
370
377
  });
371
378
  // Expose manager via Symbol.for() global registry for cross-package access.
372
379
  // Standard Node.js pattern for cross-package singletons (used by OpenTelemetry, etc.).
@@ -379,12 +386,38 @@ export default function (pi) {
379
386
  };
380
387
  // --- Cross-extension RPC via pi.events ---
381
388
  let currentCtx;
382
- // Capture ctx from session_start for RPC spawn handler
389
+ // ---- Subagent scheduler ----
390
+ // Session-scoped: store is constructed inside session_start once sessionId
391
+ // is available. Mirrors pi-chonky-tasks's session-scoped task store —
392
+ // schedules reset on /new, restore on /resume.
393
+ const scheduler = new SubagentScheduler();
394
+ function startScheduler(ctx) {
395
+ try {
396
+ const sessionId = ctx.sessionManager?.getSessionId?.();
397
+ if (!sessionId)
398
+ return; // sessionId not yet available — try again on next event
399
+ const path = resolveStorePath(ctx.cwd, sessionId);
400
+ const store = new ScheduleStore(path);
401
+ scheduler.start(pi, ctx, manager, store);
402
+ pi.events.emit("subagents:scheduler_ready", { sessionId, jobCount: store.list().length });
403
+ }
404
+ catch (err) {
405
+ // Scheduling is non-essential — log and move on so the rest of the
406
+ // extension keeps working if e.g. .pi/ is unwritable.
407
+ console.warn("[pi-subagents] Failed to start scheduler:", err);
408
+ }
409
+ }
410
+ // Capture ctx from session_start for RPC spawn handler + start the scheduler.
383
411
  pi.on("session_start", async (_event, ctx) => {
384
412
  currentCtx = ctx;
385
- manager.clearCompleted(); // preserve existing behavior
413
+ manager.clearCompleted();
414
+ if (isSchedulingEnabled() && !scheduler.isActive())
415
+ startScheduler(ctx);
416
+ });
417
+ pi.on("session_before_switch", () => {
418
+ manager.clearCompleted();
419
+ scheduler.stop();
386
420
  });
387
- pi.on("session_before_switch", () => { manager.clearCompleted(); });
388
421
  const { unsubPing: unsubPingRpc, unsubSpawn: unsubSpawnRpc, unsubStop: unsubStopRpc } = registerRpcHandlers({
389
422
  events: pi.events,
390
423
  pi,
@@ -401,6 +434,7 @@ export default function (pi) {
401
434
  unsubPingRpc();
402
435
  currentCtx = undefined;
403
436
  delete globalThis[MANAGER_KEY];
437
+ scheduler.stop();
404
438
  manager.abortAll();
405
439
  for (const timer of pendingNudges.values())
406
440
  clearTimeout(timer);
@@ -413,6 +447,15 @@ export default function (pi) {
413
447
  let defaultJoinMode = 'smart';
414
448
  function getDefaultJoinMode() { return defaultJoinMode; }
415
449
  function setDefaultJoinMode(mode) { defaultJoinMode = mode; }
450
+ // Master switch for the schedule subagent feature. Defaults to enabled.
451
+ // Read once at extension init (before tool registration) so the Agent tool's
452
+ // param schema reflects the persisted setting. Runtime toggles via /agents
453
+ // → Settings short-circuit the menu entry + the execute-time addJob path
454
+ // immediately, but the schema-level removal only takes effect on next
455
+ // extension load (next pi session). Documented in CHANGELOG/README.
456
+ let schedulingEnabled = true;
457
+ function isSchedulingEnabled() { return schedulingEnabled; }
458
+ function setSchedulingEnabled(b) { schedulingEnabled = b; }
416
459
  // ---- Batch tracking for smart join mode ----
417
460
  // Collects background agent IDs spawned in the current turn for smart grouping.
418
461
  // Uses a debounced timer: each new agent resets the 100ms window so that all
@@ -497,8 +540,25 @@ export default function (pi) {
497
540
  setDefaultMaxTurns,
498
541
  setGraceTurns,
499
542
  setDefaultJoinMode,
543
+ setSchedulingEnabled,
500
544
  }, (event, payload) => pi.events.emit(event, payload));
501
545
  // ---- Agent tool ----
546
+ // Schedule param + its guideline are gated on `schedulingEnabled` (read once
547
+ // at registration; flipping the setting later requires next pi session for
548
+ // the schema to update). Defining the shape once and spreading it via Partial
549
+ // preserves Type.Object's inference when present and produces a
550
+ // `schedule`-free schema when absent — zero LLM-context cost in disabled mode.
551
+ const scheduleParamShape = {
552
+ schedule: Type.Optional(Type.String({
553
+ description: 'Opt-in only — fire later instead of now. Omit to run immediately (the default, almost always correct). ' +
554
+ 'Formats: 6-field cron ("0 0 9 * * 1" = 9am Mon), interval ("5m"/"1h"), one-shot ("+10m" or ISO). ' +
555
+ 'Forces run_in_background; incompatible with inherit_context and resume. Returns job ID.',
556
+ })),
557
+ };
558
+ const scheduleParam = isSchedulingEnabled() ? scheduleParamShape : {};
559
+ const scheduleGuideline = isSchedulingEnabled()
560
+ ? `\n- Use \`schedule\` only when the user explicitly asked for scheduled / recurring / delayed execution (e.g. "every Monday", "in an hour"). Don't auto-schedule from vague intent like "monitor X" — run once now or ask.`
561
+ : "";
502
562
  pi.registerTool(defineTool({
503
563
  name: "Agent",
504
564
  label: "Agent",
@@ -522,7 +582,7 @@ Guidelines:
522
582
  - Use model to specify a different model (as "provider/modelId", or fuzzy e.g. "haiku", "sonnet").
523
583
  - Use thinking to control extended thinking level.
524
584
  - Use inherit_context if the agent needs the parent conversation history.
525
- - Use isolation: "worktree" to run the agent in an isolated git worktree (safe parallel file modifications).`,
585
+ - Use isolation: "worktree" to run the agent in an isolated git worktree (safe parallel file modifications).${scheduleGuideline}`,
526
586
  parameters: Type.Object({
527
587
  prompt: Type.String({
528
588
  description: "The task for the agent to perform.",
@@ -558,6 +618,7 @@ Guidelines:
558
618
  isolation: Type.Optional(Type.Literal("worktree", {
559
619
  description: 'Set to "worktree" to run the agent in a temporary git worktree (isolated copy of the repo). Changes are saved to a branch on completion.',
560
620
  })),
621
+ ...scheduleParam,
561
622
  }),
562
623
  // ---- Custom rendering: Claude Code style ----
563
624
  renderCall(args, theme) {
@@ -700,6 +761,45 @@ Guidelines:
700
761
  modelName: agentModelName,
701
762
  tags: agentTags.length > 0 ? agentTags : undefined,
702
763
  };
764
+ // ---- Schedule: register a job, don't spawn now ----
765
+ if (params.schedule) {
766
+ if (!isSchedulingEnabled()) {
767
+ return textResult("Scheduling is disabled in this project. Enable via /agents → Settings → Scheduling.");
768
+ }
769
+ if (params.resume) {
770
+ return textResult("Cannot combine `schedule` with `resume` — schedules create fresh agents.");
771
+ }
772
+ if (params.inherit_context) {
773
+ return textResult("Cannot combine `schedule` with `inherit_context` — there is no parent conversation at fire time.");
774
+ }
775
+ if (params.run_in_background === false) {
776
+ return textResult("Cannot combine `schedule` with `run_in_background: false` — scheduled jobs always run in background.");
777
+ }
778
+ if (!scheduler.isActive()) {
779
+ return textResult("Scheduler is not active in this session yet. Try again after the session has fully started.");
780
+ }
781
+ try {
782
+ const job = scheduler.addJob({
783
+ name: params.description,
784
+ description: params.description,
785
+ schedule: params.schedule,
786
+ subagent_type: subagentType,
787
+ prompt: params.prompt,
788
+ model: params.model,
789
+ thinking: thinking,
790
+ max_turns: effectiveMaxTurns,
791
+ isolated: isolated,
792
+ isolation: isolation,
793
+ });
794
+ const next = scheduler.getNextRun(job.id);
795
+ return textResult(`Scheduled "${job.name}" (id: ${job.id}, type: ${job.scheduleType}). ` +
796
+ `Next run: ${next ?? "(unknown)"}. ` +
797
+ `Manage via /agents → Scheduled jobs.`);
798
+ }
799
+ catch (err) {
800
+ return textResult(err instanceof Error ? err.message : String(err));
801
+ }
802
+ }
703
803
  // Resume existing agent
704
804
  if (params.resume) {
705
805
  const existing = manager.getRecord(params.resume);
@@ -792,7 +892,7 @@ Guidelines:
792
892
  const details = {
793
893
  ...detailBase,
794
894
  toolUses: fgState.toolUses,
795
- tokens: fgState.tokens,
895
+ tokens: formatLifetimeTokens(fgState),
796
896
  turnCount: fgState.turnCount,
797
897
  maxTurns: fgState.maxTurns,
798
898
  durationMs: Date.now() - startedAt,
@@ -833,6 +933,7 @@ Guidelines:
833
933
  inheritContext,
834
934
  thinkingLevel: thinking,
835
935
  isolation,
936
+ signal,
836
937
  ...fgCallbacks,
837
938
  });
838
939
  clearInterval(spinnerInterval);
@@ -842,7 +943,7 @@ Guidelines:
842
943
  widget.markFinished(fgId);
843
944
  }
844
945
  // Get final token count
845
- const tokenText = safeFormatTokens(fgState.session);
946
+ const tokenText = formatLifetimeTokens(fgState);
846
947
  const details = buildDetails(detailBase, record, fgState, { tokens: tokenText });
847
948
  const fallbackNote = fellBack
848
949
  ? `Note: Unknown agent type "${rawType}" — using general-purpose.\n\n`
@@ -890,10 +991,18 @@ Guidelines:
890
991
  }
891
992
  const displayName = getDisplayName(record.type);
892
993
  const duration = formatDuration(record.startedAt, record.completedAt);
893
- const tokens = safeFormatTokens(record.session);
894
- const toolStats = tokens ? `Tool uses: ${record.toolUses} | ${tokens}` : `Tool uses: ${record.toolUses}`;
994
+ const tokens = formatLifetimeTokens(record);
995
+ const contextPercent = getSessionContextPercent(record.session);
996
+ const statsParts = [`Tool uses: ${record.toolUses}`];
997
+ if (tokens)
998
+ statsParts.push(tokens);
999
+ if (contextPercent !== null)
1000
+ statsParts.push(`Context: ${Math.round(contextPercent)}%`);
1001
+ if (record.compactionCount)
1002
+ statsParts.push(`Compactions: ${record.compactionCount}`);
1003
+ statsParts.push(`Duration: ${duration}`);
895
1004
  let output = `Agent: ${record.id}\n` +
896
- `Type: ${displayName} | Status: ${record.status} | ${toolStats} | Duration: ${duration}\n` +
1005
+ `Type: ${displayName} | Status: ${record.status} | ${statsParts.join(" | ")}\n` +
897
1006
  `Description: ${record.description}\n\n`;
898
1007
  if (record.status === "running") {
899
1008
  output += "Agent is still running. Use wait: true or check back later.";
@@ -952,7 +1061,18 @@ Guidelines:
952
1061
  try {
953
1062
  await steerAgent(record.session, params.message);
954
1063
  pi.events.emit("subagents:steered", { id: record.id, message: params.message });
955
- return textResult(`Steering message sent to agent ${record.id}. The agent will process it after its current tool execution.`);
1064
+ const tokens = formatLifetimeTokens(record);
1065
+ const contextPercent = getSessionContextPercent(record.session);
1066
+ const stateParts = [];
1067
+ if (tokens)
1068
+ stateParts.push(tokens);
1069
+ stateParts.push(`${record.toolUses} tool ${record.toolUses === 1 ? "use" : "uses"}`);
1070
+ if (contextPercent !== null)
1071
+ stateParts.push(`context ${Math.round(contextPercent)}% full`);
1072
+ if (record.compactionCount)
1073
+ stateParts.push(`${record.compactionCount} compaction${record.compactionCount === 1 ? "" : "s"}`);
1074
+ return textResult(`Steering message sent to agent ${record.id}. The agent will process it after its current tool execution.\n` +
1075
+ `Current state: ${stateParts.join(" · ")}`);
956
1076
  }
957
1077
  catch (err) {
958
1078
  return textResult(`Failed to steer agent: ${err instanceof Error ? err.message : String(err)}`);
@@ -1000,6 +1120,11 @@ Guidelines:
1000
1120
  if (allNames.length > 0) {
1001
1121
  options.push(`Agent types (${allNames.length})`);
1002
1122
  }
1123
+ // Scheduled jobs entry (always present when scheduler is active)
1124
+ if (scheduler.isActive()) {
1125
+ const jobCount = scheduler.list().length;
1126
+ options.push(`Scheduled jobs (${jobCount})`);
1127
+ }
1003
1128
  // Actions
1004
1129
  options.push("Create new agent");
1005
1130
  options.push("Settings");
@@ -1022,6 +1147,10 @@ Guidelines:
1022
1147
  await showAllAgentsList(ctx);
1023
1148
  await showAgentsMenu(ctx);
1024
1149
  }
1150
+ else if (choice.startsWith("Scheduled jobs (")) {
1151
+ await showSchedulesMenu(ctx, scheduler);
1152
+ await showAgentsMenu(ctx);
1153
+ }
1025
1154
  else if (choice === "Create new agent") {
1026
1155
  await showCreateWizard(ctx);
1027
1156
  }
@@ -1480,6 +1609,7 @@ ${systemPrompt}
1480
1609
  defaultMaxTurns: getDefaultMaxTurns() ?? 0,
1481
1610
  graceTurns: getGraceTurns(),
1482
1611
  defaultJoinMode: getDefaultJoinMode(),
1612
+ schedulingEnabled: isSchedulingEnabled(),
1483
1613
  };
1484
1614
  }
1485
1615
  async function showSettings(ctx) {
@@ -1488,6 +1618,7 @@ ${systemPrompt}
1488
1618
  `Default max turns (current: ${getDefaultMaxTurns() ?? "unlimited"})`,
1489
1619
  `Grace turns (current: ${getGraceTurns()})`,
1490
1620
  `Join mode (current: ${getDefaultJoinMode()})`,
1621
+ `Scheduling (current: ${isSchedulingEnabled() ? "enabled" : "disabled"})`,
1491
1622
  ]);
1492
1623
  if (!choice)
1493
1624
  return;
@@ -1546,6 +1677,24 @@ ${systemPrompt}
1546
1677
  notifyApplied(ctx, `Default join mode set to ${mode}`);
1547
1678
  }
1548
1679
  }
1680
+ else if (choice.startsWith("Scheduling")) {
1681
+ const val = await ctx.ui.select("Schedule subagent feature", [
1682
+ "enabled — Agent tool accepts a `schedule` param; /agents → Scheduled jobs visible",
1683
+ "disabled — `schedule` removed from Agent tool spec (no LLM-context cost); menu hidden",
1684
+ ]);
1685
+ if (val) {
1686
+ const enabled = val.startsWith("enabled");
1687
+ if (enabled === isSchedulingEnabled()) {
1688
+ ctx.ui.notify(`Scheduling already ${enabled ? "enabled" : "disabled"}.`, "info");
1689
+ }
1690
+ else {
1691
+ setSchedulingEnabled(enabled);
1692
+ if (!enabled)
1693
+ scheduler.stop(); // immediate kill — outstanding fires stop ticking
1694
+ notifyApplied(ctx, `Scheduling ${enabled ? "enabled" : "disabled"}. Tool spec change takes effect on next pi session.`);
1695
+ }
1696
+ }
1697
+ }
1549
1698
  }
1550
1699
  // Persist the current snapshot, emit `subagents:settings_changed`, and surface
1551
1700
  // the right toast. Successful saves show info; persistence failures downgrade
@@ -0,0 +1,36 @@
1
+ /**
2
+ * schedule-store.ts — File-backed store for scheduled subagents.
3
+ *
4
+ * Session-scoped: each pi session owns its own schedules at
5
+ * `<cwd>/.pi/subagent-schedules/<sessionId>.json`. `/new` starts a fresh
6
+ * empty store; `/resume` reloads.
7
+ *
8
+ * Concurrency model lifted from pi-chonky-tasks/src/task-store.ts: every
9
+ * mutation acquires a PID-based exclusion lock, re-reads the latest state
10
+ * from disk, applies the change, atomic-writes via temp+rename, releases.
11
+ */
12
+ import type { ScheduledSubagent } from "./types.js";
13
+ /** Resolve the storage path for a session-scoped store. */
14
+ export declare function resolveStorePath(cwd: string, sessionId: string): string;
15
+ export declare class ScheduleStore {
16
+ private filePath;
17
+ private lockPath;
18
+ private jobs;
19
+ constructor(filePath: string);
20
+ /** Load from disk into the in-memory cache. Silent on parse errors. */
21
+ private load;
22
+ /** Atomic write via temp file + rename (POSIX-atomic). */
23
+ private save;
24
+ /** Acquire lock → reload → mutate → save → release. */
25
+ private withLock;
26
+ /** Read-only — returns a snapshot of the in-memory cache. */
27
+ list(): ScheduledSubagent[];
28
+ /** Read-only check — uses the cache. */
29
+ hasName(name: string, exceptId?: string): boolean;
30
+ get(id: string): ScheduledSubagent | undefined;
31
+ add(job: ScheduledSubagent): void;
32
+ update(id: string, patch: Partial<ScheduledSubagent>): ScheduledSubagent | undefined;
33
+ remove(id: string): boolean;
34
+ /** Delete the backing file (used when no jobs remain, optional cleanup). */
35
+ deleteFileIfEmpty(): void;
36
+ }
@@ -0,0 +1,144 @@
1
+ /**
2
+ * schedule-store.ts — File-backed store for scheduled subagents.
3
+ *
4
+ * Session-scoped: each pi session owns its own schedules at
5
+ * `<cwd>/.pi/subagent-schedules/<sessionId>.json`. `/new` starts a fresh
6
+ * empty store; `/resume` reloads.
7
+ *
8
+ * Concurrency model lifted from pi-chonky-tasks/src/task-store.ts: every
9
+ * mutation acquires a PID-based exclusion lock, re-reads the latest state
10
+ * from disk, applies the change, atomic-writes via temp+rename, releases.
11
+ */
12
+ import { existsSync, mkdirSync, readFileSync, renameSync, unlinkSync, writeFileSync } from "node:fs";
13
+ import { dirname, join } from "node:path";
14
+ const LOCK_RETRY_MS = 50;
15
+ const LOCK_MAX_RETRIES = 100;
16
+ function isProcessRunning(pid) {
17
+ try {
18
+ process.kill(pid, 0);
19
+ return true;
20
+ }
21
+ catch {
22
+ return false;
23
+ }
24
+ }
25
+ function acquireLock(lockPath) {
26
+ for (let i = 0; i < LOCK_MAX_RETRIES; i++) {
27
+ try {
28
+ writeFileSync(lockPath, `${process.pid}`, { flag: "wx" });
29
+ return;
30
+ }
31
+ catch (e) {
32
+ if (e.code === "EEXIST") {
33
+ try {
34
+ const pid = parseInt(readFileSync(lockPath, "utf-8"), 10);
35
+ if (pid && !isProcessRunning(pid)) {
36
+ unlinkSync(lockPath);
37
+ continue;
38
+ }
39
+ }
40
+ catch { /* ignore — try again */ }
41
+ const start = Date.now();
42
+ while (Date.now() - start < LOCK_RETRY_MS) { /* busy wait */ }
43
+ continue;
44
+ }
45
+ throw e;
46
+ }
47
+ }
48
+ throw new Error(`Failed to acquire schedule lock: ${lockPath}`);
49
+ }
50
+ function releaseLock(lockPath) {
51
+ try {
52
+ unlinkSync(lockPath);
53
+ }
54
+ catch { /* ignore */ }
55
+ }
56
+ /** Resolve the storage path for a session-scoped store. */
57
+ export function resolveStorePath(cwd, sessionId) {
58
+ return join(cwd, ".pi", "subagent-schedules", `${sessionId}.json`);
59
+ }
60
+ export class ScheduleStore {
61
+ filePath;
62
+ lockPath;
63
+ jobs = new Map();
64
+ constructor(filePath) {
65
+ this.filePath = filePath;
66
+ this.lockPath = filePath + ".lock";
67
+ mkdirSync(dirname(filePath), { recursive: true });
68
+ this.load();
69
+ }
70
+ /** Load from disk into the in-memory cache. Silent on parse errors. */
71
+ load() {
72
+ if (!existsSync(this.filePath))
73
+ return;
74
+ try {
75
+ const data = JSON.parse(readFileSync(this.filePath, "utf-8"));
76
+ this.jobs.clear();
77
+ for (const j of data.jobs ?? [])
78
+ this.jobs.set(j.id, j);
79
+ }
80
+ catch { /* corrupt — start fresh, next save rewrites */ }
81
+ }
82
+ /** Atomic write via temp file + rename (POSIX-atomic). */
83
+ save() {
84
+ const data = { version: 1, jobs: [...this.jobs.values()] };
85
+ const tmp = this.filePath + ".tmp";
86
+ writeFileSync(tmp, JSON.stringify(data, null, 2));
87
+ renameSync(tmp, this.filePath);
88
+ }
89
+ /** Acquire lock → reload → mutate → save → release. */
90
+ withLock(fn) {
91
+ acquireLock(this.lockPath);
92
+ try {
93
+ this.load();
94
+ const result = fn();
95
+ this.save();
96
+ return result;
97
+ }
98
+ finally {
99
+ releaseLock(this.lockPath);
100
+ }
101
+ }
102
+ /** Read-only — returns a snapshot of the in-memory cache. */
103
+ list() {
104
+ return [...this.jobs.values()];
105
+ }
106
+ /** Read-only check — uses the cache. */
107
+ hasName(name, exceptId) {
108
+ for (const j of this.jobs.values()) {
109
+ if (j.id !== exceptId && j.name === name)
110
+ return true;
111
+ }
112
+ return false;
113
+ }
114
+ get(id) {
115
+ return this.jobs.get(id);
116
+ }
117
+ add(job) {
118
+ this.withLock(() => {
119
+ this.jobs.set(job.id, job);
120
+ });
121
+ }
122
+ update(id, patch) {
123
+ return this.withLock(() => {
124
+ const existing = this.jobs.get(id);
125
+ if (!existing)
126
+ return undefined;
127
+ const updated = { ...existing, ...patch };
128
+ this.jobs.set(id, updated);
129
+ return updated;
130
+ });
131
+ }
132
+ remove(id) {
133
+ return this.withLock(() => this.jobs.delete(id));
134
+ }
135
+ /** Delete the backing file (used when no jobs remain, optional cleanup). */
136
+ deleteFileIfEmpty() {
137
+ if (this.jobs.size === 0 && existsSync(this.filePath)) {
138
+ try {
139
+ unlinkSync(this.filePath);
140
+ }
141
+ catch { /* ignore */ }
142
+ }
143
+ }
144
+ }