@gotgenes/pi-subagents 1.0.2 → 2.0.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/CHANGELOG.md CHANGED
@@ -5,6 +5,27 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [2.0.0](https://github.com/gotgenes/pi-packages/compare/pi-subagents-v1.0.2...pi-subagents-v2.0.0) (2026-05-17)
9
+
10
+
11
+ ### ⚠ BREAKING CHANGES
12
+
13
+ * the `schedule` parameter is removed from the Agent tool. The `subagents:scheduled` and `subagents:scheduler_ready` events are no longer emitted. The `/agents → Settings → Scheduling` toggle is removed.
14
+ * the `schedule` parameter is removed from the Agent tool and the `subagents:scheduled` / `subagents:scheduler_ready` events are no longer emitted. System cron (or launchd) invoking `pi` directly is the recommended replacement for recurring/delayed agent tasks.
15
+
16
+ ### Features
17
+
18
+ * remove scheduled subagents source and tests ([860a03f](https://github.com/gotgenes/pi-packages/commit/860a03f08a6dbd418b437a72c2fd05ea416abacb))
19
+ * remove scheduler wiring, types, and settings ([d5184e8](https://github.com/gotgenes/pi-packages/commit/d5184e88b181e60809b7fecc6a0971a18723bb9d))
20
+
21
+
22
+ ### Documentation
23
+
24
+ * correct Module-Level Changes table in plan [#52](https://github.com/gotgenes/pi-packages/issues/52) (AGENTS.md → SKILL.md) ([93337e2](https://github.com/gotgenes/pi-packages/commit/93337e2a6b0424dc64e0adaf7a5c6ae6913c5991))
25
+ * plan remove in-process scheduled subagents ([#52](https://github.com/gotgenes/pi-packages/issues/52)) ([bc548a4](https://github.com/gotgenes/pi-packages/commit/bc548a4dc6be4c5f810e9e33abcc76bc0d84f0da))
26
+ * remove scheduling from README, architecture doc, and skill ([b2f16f2](https://github.com/gotgenes/pi-packages/commit/b2f16f2a53674a6a2675024dfc038c4243edd299))
27
+ * **retro:** add retro notes for issue [#51](https://github.com/gotgenes/pi-packages/issues/51) ([0f741de](https://github.com/gotgenes/pi-packages/commit/0f741dedbe4a8e26fd1e39098fb153e4511082b9))
28
+
8
29
  ## [1.0.2](https://github.com/gotgenes/pi-packages/compare/pi-subagents-v1.0.1...pi-subagents-v1.0.2) (2026-05-17)
9
30
 
10
31
 
package/README.md CHANGED
@@ -34,7 +34,6 @@ https://github.com/user-attachments/assets/8685261b-9338-4fea-8dfe-1c590d5df543
34
34
  - **Styled completion notifications** — background agent results render as themed, compact notification boxes (icon, stats, result preview) instead of raw XML. Expandable to show full output. Group completions render each agent individually
35
35
  - **Event bus** — lifecycle events (`subagents:created`, `started`, `completed`, `failed`, `steered`, `compacted`) emitted via `pi.events`, enabling other extensions to react to sub-agent activity
36
36
  - **Cross-extension RPC** — other pi extensions can spawn and stop subagents via the `pi.events` event bus (`subagents:rpc:ping`, `subagents:rpc:spawn`, `subagents:rpc:stop`). Standardized reply envelopes with protocol versioning. Emits `subagents:ready` on load
37
- - **Schedule subagents** — pass `schedule` to the `Agent` tool to fire on cron / interval / one-shot. Session-scoped jobs with PID-locked persistence; results land via the same `subagent-notification` followUp path as manual background completions; manage via `/agents → Scheduled jobs`
38
37
 
39
38
  ## Install
40
39
 
@@ -63,38 +62,6 @@ Agent({
63
62
 
64
63
  Foreground agents block until complete and return results inline. Background agents return an ID immediately and notify you on completion.
65
64
 
66
- ### Scheduling
67
-
68
- Add a `schedule` field to register the agent to fire later instead of running now:
69
-
70
- ```
71
- Agent({
72
- subagent_type: "Explore",
73
- prompt: "Look at recent commits and summarize what changed since last week",
74
- description: "Weekly commit review",
75
- schedule: "0 0 9 * * 1", // 9am every Monday (6-field cron)
76
- })
77
- ```
78
-
79
- Schedule formats:
80
-
81
- - **Cron** — 6-field (`second minute hour day-of-month month day-of-week`), e.g. `"0 0 9 * * 1"` for 9am every Monday, `"0 */15 * * * *"` for every 15 minutes.
82
- - **Interval** — `"5m"`, `"1h"`, `"30s"`, `"2d"`. Fires repeatedly at that interval.
83
- - **One-shot relative** — `"+10m"`, `"+2h"`, `"+1d"`. Fires once at that future time.
84
- - **One-shot absolute** — full ISO timestamp, e.g. `"2026-12-25T09:00:00.000Z"`.
85
-
86
- When a schedule fires, the spawn runs in background and its completion notification arrives in the conversation through the same `subagent-notification` followUp path as a manually-spawned background agent — your parent agent reasons about the result the same way.
87
-
88
- Schedules are **session-scoped**: they reset on `/new` and restore on `/resume`. List and cancel via `/agents → Scheduled jobs` (creation is the `Agent` tool's job — there is no parallel manual-create wizard). Storage at `<cwd>/.pi/subagent-schedules/<sessionId>.json` with PID-based file locking for cross-instance safety.
89
-
90
- **Disable the feature entirely**: `/agents → Settings → Scheduling → disabled` removes `schedule` from the `Agent` tool spec (no LLM-context cost), hides the menu entry, and stops any active scheduler. The schema-level removal takes effect on the next pi session; the runtime kill is immediate. Re-enable from the same menu.
91
-
92
- Restrictions:
93
- - `schedule` cannot be combined with `inherit_context` (no parent conversation exists at fire time) or `resume` (schedules create fresh agents).
94
- - `run_in_background` is forced to `true`.
95
- - Scheduled fires bypass the `maxConcurrent` queue so a 5-minute interval cannot be deferred behind long-running manual agents.
96
- - **Headless `pi -p` doesn't wait for scheduled subagents.**
97
-
98
65
  ## UI
99
66
 
100
67
  The extension renders a persistent widget above the editor showing all active agents:
@@ -351,8 +318,6 @@ Agent lifecycle events are emitted via `pi.events.emit()` so other extensions ca
351
318
  | `subagents:failed` | Agent errored, stopped, or aborted | same as completed + `error`, `status` |
352
319
  | `subagents:steered` | Steering message sent | `id`, `message` |
353
320
  | `subagents:compacted` | Agent's session successfully compacted | `id`, `type`, `description`, `reason` (`"manual"` / `"threshold"` / `"overflow"`), `tokensBefore`, `compactionCount` |
354
- | `subagents:scheduled` | Schedule lifecycle change | `{ type: "added" \| "removed" \| "updated" \| "fired" \| "error", … }` (job/agentId/error fields per type) |
355
- | `subagents:scheduler_ready` | Scheduler bound to session, enabled jobs armed | `sessionId`, `jobCount` |
356
321
  | `subagents:ready` | Extension loaded and RPC handlers registered | — |
357
322
  | `subagents:settings_loaded` | Persisted settings applied at extension init | `settings` (merged global + project) |
358
323
  | `subagents:settings_changed` | `/agents` → Settings mutation was applied | `settings`, `persisted` (`boolean` — `false` on write failure) |
@@ -48,14 +48,11 @@ invocation-config.ts — merge tool params with agent config
48
48
  output-file.ts — JSONL transcript streaming
49
49
  settings.ts — persistent operational settings
50
50
 
51
- schedule.ts — cron/interval/one-shot job dispatch ← removing
52
- schedule-store.ts — file-backed schedule persistence ← removing
53
51
  cross-extension-rpc.ts — RPC over pi.events ← replacing
54
52
  group-join.ts — batch completion notifications
55
53
 
56
54
  ui/agent-widget.ts — above-editor live status widget
57
55
  ui/conversation-viewer.ts — scrollable session overlay
58
- ui/schedule-menu.ts — /agents schedule submenu ← removing
59
56
  ```
60
57
 
61
58
  ### Coupling today
@@ -63,8 +60,7 @@ ui/schedule-menu.ts — /agents schedule submenu ← removing
63
60
  The widget reads agent state by holding a direct reference to
64
61
  `AgentManager` and polling a shared mutable `Map<string, AgentActivity>`
65
62
  every 80 ms. The conversation viewer subscribes directly to `AgentSession`
66
- objects. The scheduler holds a direct `AgentManager` reference and calls
67
- `manager.spawn()`.
63
+ objects.
68
64
 
69
65
  Cross-extension consumers use an ad-hoc RPC layer over `pi.events`
70
66
  (`subagents:rpc:spawn`, `subagents:rpc:stop`, `subagents:rpc:ping`) with
@@ -342,10 +338,10 @@ Add the `SubagentsAPI` interface, serializable types, and `Symbol.for()`
342
338
  accessor functions as public exports of this package. No behavioral
343
339
  changes to the core yet.
344
340
 
345
- ### Phase 2: Remove scheduling
341
+ ### Phase 2: Remove scheduling ✓ (done — issue #52)
346
342
 
347
- Delete `schedule.ts`, `schedule-store.ts`, `ui/schedule-menu.ts`. Remove
348
- the `schedule` parameter from the `Agent` tool schema. Remove scheduler
343
+ Deleted `schedule.ts`, `schedule-store.ts`, `ui/schedule-menu.ts`. Removed
344
+ the `schedule` parameter from the `Agent` tool schema. Removed scheduler
349
345
  setup and lifecycle hooks from `index.ts`.
350
346
 
351
347
  ### Phase 3: Remove group-join, output-file, ad-hoc RPC
@@ -0,0 +1,131 @@
1
+ ---
2
+ issue: 52
3
+ issue_title: "feat: remove in-process scheduled subagents"
4
+ ---
5
+
6
+ # Remove in-process scheduled subagents
7
+
8
+ ## Problem Statement
9
+
10
+ The scheduling subsystem reimplements OS-level cron inside a process that is not designed to be a long-lived daemon.
11
+ System cron (or launchd) invoking `pi` directly is strictly superior: it survives crashes and reboots, is inspectable via `crontab -l`, and adds zero lines of code to this extension.
12
+ The current implementation weighs ~610 source lines plus ~820 test lines, a `croner` dependency, PID-locked file persistence, and scheduler lifecycle wiring scattered across `index.ts`.
13
+
14
+ ## Goals
15
+
16
+ - Delete the three scheduling source files (`schedule.ts`, `schedule-store.ts`, `ui/schedule-menu.ts`).
17
+ - Delete the three scheduling test files (`schedule.test.ts`, `schedule-store.test.ts`, `schedule-e2e.test.ts`).
18
+ - Remove all scheduler wiring from `index.ts` (~200 lines of imports, lifecycle hooks, tool-schema gates, and menu routing).
19
+ - Remove `ScheduledSubagent` and `ScheduleStoreData` interfaces from `types.ts`.
20
+ - Remove `schedulingEnabled` from `SubagentsSettings`, `SettingsAppliers`, and related sanitize/apply logic in `settings.ts`.
21
+ - Remove the `croner` dependency from `package.json`.
22
+ - Update `README.md` (remove "Scheduling" section and events-table rows) and package `AGENTS.md` (remove scheduling modules from architecture diagram and tables).
23
+ - This is a **breaking change** (`feat!:`) — the `schedule` parameter is removed from the `Agent` tool, and the `subagents:scheduled` / `subagents:scheduler_ready` events are no longer emitted.
24
+
25
+ ## Non-Goals
26
+
27
+ - Removing `bypassQueue` from `SpawnOptions` in `agent-manager.ts` — it remains useful for cross-extension RPC callers.
28
+ - Removing other subsystems slated for removal in the architecture doc (output-file, cross-extension-rpc, group-join) — those are separate issues.
29
+ - Providing a migration path or compatibility shim — no known consumers depend on the scheduling events.
30
+
31
+ ## Background
32
+
33
+ The architecture doc (`docs/architecture/architecture.md`) already marks `schedule.ts`, `schedule-store.ts`, and `ui/schedule-menu.ts` as `← removing`.
34
+ This issue executes that plan.
35
+
36
+ The scheduling code touches `index.ts` in several distinct regions:
37
+
38
+ 1. Imports (lines 27–28, 46)
39
+ 2. Scheduler instance creation and `startScheduler()` helper (lines 445–462)
40
+ 3. Lifecycle hooks: `session_start`, `session_before_switch`, `session_shutdown` (lines 470–496)
41
+ 4. The `schedule` tool-schema param shape, conditional inclusion, and guideline string (lines 621–640, 666, 719)
42
+ 5. Schedule execution path in the Agent tool handler (lines 880–918)
43
+ 6. `/agents` menu: "Scheduled jobs" entry and routing (lines 1297–1327)
44
+ 7. `/agents → Settings → Scheduling` toggle (lines 1855–1867)
45
+
46
+ The `settings.ts` module has `schedulingEnabled` woven into `SubagentsSettings`, `SettingsAppliers`, `sanitize()`, and `applySettings()`.
47
+
48
+ `types.ts` carries `ScheduledSubagent` (30 lines) and `ScheduleStoreData` (5 lines) — both are only consumed by the scheduling subsystem.
49
+
50
+ ## Design Overview
51
+
52
+ This is a pure deletion change with no new abstractions.
53
+ The approach is inside-out: delete leaf modules first (no dependents), then remove references from the wiring layer (`index.ts`, `settings.ts`, `types.ts`), then clean up docs.
54
+
55
+ The `bypassQueue` option on `SpawnOptions` stays — its JSDoc comment mentioning "scheduler" should be updated to a generic description since the scheduler is the only current user but the option is architecturally useful for any caller that needs to skip the concurrency queue.
56
+
57
+ ## Module-Level Changes
58
+
59
+ ### Delete
60
+
61
+ | File | Lines |
62
+ | ----------------------------- | ----- |
63
+ | `src/schedule.ts` | 365 |
64
+ | `src/schedule-store.ts` | 143 |
65
+ | `src/ui/schedule-menu.ts` | 104 |
66
+ | `test/schedule.test.ts` | 429 |
67
+ | `test/schedule-store.test.ts` | 154 |
68
+ | `test/schedule-e2e.test.ts` | 237 |
69
+
70
+ ### Modify
71
+
72
+ | File | Change |
73
+ | ------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
74
+ | `src/index.ts` | Remove scheduler imports, instance creation, `startScheduler()`, lifecycle hooks, `scheduleParamShape`/`scheduleParam`/`scheduleGuideline`, schedule execution path in Agent handler, "Scheduled jobs" menu entry + routing, "Scheduling" settings toggle. |
75
+ | `src/types.ts` | Remove `ScheduledSubagent` interface and `ScheduleStoreData` interface. |
76
+ | `src/settings.ts` | Remove `schedulingEnabled` from `SubagentsSettings` and `SettingsAppliers`. Remove sanitize clause and `applySettings` clause for `schedulingEnabled`. |
77
+ | `src/agent-manager.ts` | Update `bypassQueue` JSDoc to remove scheduler-specific language. |
78
+ | `package.json` | Remove `"croner": "^10.0.1"` from dependencies. |
79
+ | `README.md` | Remove "Scheduling" feature bullet, "Scheduling" subsection (lines 66–96), and `subagents:scheduled` / `subagents:scheduler_ready` rows from the events table. |
80
+ | `.pi/skills/package-pi-subagents/SKILL.md` | Remove `schedule.ts`, `schedule-store.ts`, `ui/schedule-menu.ts` from the architecture diagram and module tables. Update `index.ts` description to drop scheduler mention. (`packages/pi-subagents/AGENTS.md` is a stub — the architecture content lives in the skill file.) |
81
+
82
+ ## Test Impact Analysis
83
+
84
+ 1. No new unit tests are needed — this is pure deletion.
85
+ 2. All three scheduling test files (`schedule.test.ts`, `schedule-store.test.ts`, `schedule-e2e.test.ts`) become entirely redundant and are deleted.
86
+ 3. Existing tests for `agent-manager`, `agent-runner`, `settings`, and other modules stay as-is. The `settings.test.ts` file (if it exists) may need minor updates to remove `schedulingEnabled` from fixture data.
87
+
88
+ ## TDD Order
89
+
90
+ Since this is a removal (not a feature), the order is deletion-first with a single validation pass.
91
+
92
+ 1. **Delete scheduling source files.**
93
+ Delete `src/schedule.ts`, `src/schedule-store.ts`, `src/ui/schedule-menu.ts`.
94
+ Delete `test/schedule.test.ts`, `test/schedule-store.test.ts`, `test/schedule-e2e.test.ts`.
95
+ Commit: `feat!: remove scheduled subagents source and tests`
96
+
97
+ 2. **Remove scheduler wiring from `index.ts`.**
98
+ Remove imports, scheduler instance, `startScheduler()`, lifecycle hooks, schedule-related tool schema params and guideline, schedule execution path in Agent handler, "Scheduled jobs" menu entry/routing, and "Scheduling" settings toggle.
99
+ Commit: `feat!: remove scheduler wiring from index.ts`
100
+
101
+ 3. **Clean up types and settings.**
102
+ Remove `ScheduledSubagent` and `ScheduleStoreData` from `types.ts`.
103
+ Remove `schedulingEnabled` from `SubagentsSettings`, `SettingsAppliers`, `sanitize()`, and `applySettings()` in `settings.ts`.
104
+ Update `bypassQueue` JSDoc in `agent-manager.ts`.
105
+ Commit: `feat!: remove scheduling types and settings`
106
+
107
+ 4. **Remove `croner` dependency.**
108
+ Remove from `package.json`, run `pnpm install` to update lockfile.
109
+ Commit: `build: remove croner dependency`
110
+
111
+ 5. **Verify all tests pass.**
112
+ Run `pnpm vitest run` in the package. Fix any test fixtures that reference `schedulingEnabled` or scheduling types.
113
+ Commit (if fixes needed): `test: remove scheduling references from test fixtures`
114
+
115
+ 6. **Update documentation.**
116
+ Update `README.md`: remove scheduling feature bullet, "Scheduling" subsection, and event-table rows.
117
+ Update package `AGENTS.md`: remove scheduling modules from architecture diagram and module tables, update `index.ts` description.
118
+ Commit: `docs: remove scheduling from README and AGENTS`
119
+
120
+ ## Risks and Mitigations
121
+
122
+ | Risk | Mitigation |
123
+ | ------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------- |
124
+ | Missed scheduling reference causes compile error | `grep -rn 'schedule\|Schedule\|croner' src/` after each step to catch stragglers. TypeScript `noEmit` check catches import errors. |
125
+ | Test fixtures reference `schedulingEnabled` | Step 5 explicitly scans for and fixes these. |
126
+ | `bypassQueue` removal mistakenly included | Explicitly excluded in Non-Goals; plan preserves the option and only updates its JSDoc. |
127
+ | Breaking change not communicated | `feat!:` commit prefix triggers a major version bump via release-please. |
128
+
129
+ ## Open Questions
130
+
131
+ None — the issue scope is fully specified and the architecture doc already ratified this removal.
@@ -0,0 +1,33 @@
1
+ ---
2
+ issue: 51
3
+ issue_title: "docs: update ADR 0001 to reflect hard-fork decision"
4
+ ---
5
+
6
+ # Retro: #51 — docs: update ADR 0001 to reflect hard-fork decision
7
+
8
+ ## Final Retrospective (2026-05-16)
9
+
10
+ ### Session summary
11
+
12
+ Updated ADR 0001 (`docs/decisions/0001-deferred-patches.md`) to reflect the hard-fork decision documented in `docs/architecture/architecture.md`.
13
+ The change was planned, implemented, shipped, and released as `pi-subagents-v1.0.2` in a single clean pass with no rework.
14
+
15
+ ### Observations
16
+
17
+ #### What went well
18
+
19
+ - The entire plan→build→ship pipeline completed with zero corrections, zero CI failures, and zero user interventions.
20
+ - Parallel context gathering (issue body, `AGENTS.md`, ADR file, architecture doc, two skill files) in one tool call made the planning phase efficient.
21
+ - The 4-edit approach (`Edit` with a single call containing four `edits[]` entries) was well-matched to the task — each edit was small, unique, and non-overlapping.
22
+
23
+ #### What caused friction (agent side)
24
+
25
+ - No friction observed. The task was unambiguous and the tooling well-suited.
26
+
27
+ #### What caused friction (user side)
28
+
29
+ - No friction observed. The session required no user input beyond invoking the three slash commands.
30
+
31
+ ### Follow-ups identified
32
+
33
+ - The `package-pi-subagents` skill (`.pi/skills/package-pi-subagents/SKILL.md`) still frames the fork as "a friendly fork… carrying a small number of patches" with priorities like "stays as close to upstream as possible." This framing is now stale given the hard-fork commitment. A separate issue should update the skill to reflect the architecture document's posture.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gotgenes/pi-subagents",
3
- "version": "1.0.2",
3
+ "version": "2.0.0",
4
4
  "description": "A pi extension that brings Claude Code-style autonomous sub-agents to pi. Friendly fork of @tintinweb/pi-subagents.",
5
5
  "author": {
6
6
  "name": "Chris Lasher"
@@ -33,7 +33,6 @@
33
33
  },
34
34
  "dependencies": {
35
35
  "@sinclair/typebox": "^0.34.49",
36
- "croner": "^10.0.1",
37
36
  "nanoid": "^5.0.0"
38
37
  },
39
38
  "engines": {
@@ -40,8 +40,8 @@ interface SpawnOptions {
40
40
  isBackground?: boolean;
41
41
  /**
42
42
  * Skip the maxConcurrent queue check for this spawn — start immediately even
43
- * if the configured concurrency limit would otherwise queue it. Used by the
44
- * scheduler so a fired job can't be deferred past its trigger window.
43
+ * if the configured concurrency limit would otherwise queue it. Useful for
44
+ * callers (e.g. cross-extension RPC) that must not be deferred by the queue.
45
45
  */
46
46
  bypassQueue?: boolean;
47
47
  /** Isolation mode — "worktree" creates a temp git worktree for the agent. */
package/src/index.ts CHANGED
@@ -24,8 +24,6 @@ import { GroupJoinManager } from "./group-join.js";
24
24
  import { resolveAgentInvocationConfig, resolveJoinMode } from "./invocation-config.js";
25
25
  import { type ModelRegistry, resolveModel } from "./model-resolver.js";
26
26
  import { createOutputFilePath, streamToOutputFile, writeInitialEntry } from "./output-file.js";
27
- import { SubagentScheduler } from "./schedule.js";
28
- import { resolveStorePath, ScheduleStore } from "./schedule-store.js";
29
27
  import { applyAndEmitLoaded, type SubagentsSettings, saveAndEmitChanged } from "./settings.js";
30
28
  import { type AgentConfig, type AgentInvocation, type AgentRecord, type JoinMode, type NotificationDetails, type SubagentType } from "./types.js";
31
29
  import {
@@ -43,7 +41,6 @@ import {
43
41
  SPINNER,
44
42
  type UICtx,
45
43
  } from "./ui/agent-widget.js";
46
- import { showSchedulesMenu } from "./ui/schedule-menu.js";
47
44
  import { addUsage, getLifetimeTotal, getSessionContextPercent, type LifetimeUsage } from "./usage.js";
48
45
 
49
46
  // ---- Shared helpers ----
@@ -442,37 +439,14 @@ export default function (pi: ExtensionAPI) {
442
439
  // --- Cross-extension RPC via pi.events ---
443
440
  let currentCtx: ExtensionContext | undefined;
444
441
 
445
- // ---- Subagent scheduler ----
446
- // Session-scoped: store is constructed inside session_start once sessionId
447
- // is available. Mirrors pi-chonky-tasks's session-scoped task store —
448
- // schedules reset on /new, restore on /resume.
449
- const scheduler = new SubagentScheduler();
450
-
451
- function startScheduler(ctx: ExtensionContext) {
452
- try {
453
- const sessionId = ctx.sessionManager?.getSessionId?.();
454
- if (!sessionId) return; // sessionId not yet available — try again on next event
455
- const path = resolveStorePath(ctx.cwd, sessionId);
456
- const store = new ScheduleStore(path);
457
- scheduler.start(pi, ctx, manager, store);
458
- pi.events.emit("subagents:scheduler_ready", { sessionId, jobCount: store.list().length });
459
- } catch (err) {
460
- // Scheduling is non-essential — log and move on so the rest of the
461
- // extension keeps working if e.g. .pi/ is unwritable.
462
- console.warn("[pi-subagents] Failed to start scheduler:", err);
463
- }
464
- }
465
-
466
- // Capture ctx from session_start for RPC spawn handler + start the scheduler.
442
+ // Capture ctx from session_start for RPC spawn handler.
467
443
  pi.on("session_start", async (_event, ctx) => {
468
444
  currentCtx = ctx;
469
445
  manager.clearCompleted();
470
- if (isSchedulingEnabled() && !scheduler.isActive()) startScheduler(ctx);
471
446
  });
472
447
 
473
448
  pi.on("session_before_switch", () => {
474
449
  manager.clearCompleted();
475
- scheduler.stop();
476
450
  });
477
451
 
478
452
  const { unsubPing: unsubPingRpc, unsubSpawn: unsubSpawnRpc, unsubStop: unsubStopRpc } = registerRpcHandlers({
@@ -493,7 +467,6 @@ export default function (pi: ExtensionAPI) {
493
467
  unsubPingRpc();
494
468
  currentCtx = undefined;
495
469
  delete (globalThis as any)[MANAGER_KEY];
496
- scheduler.stop();
497
470
  manager.abortAll();
498
471
  for (const timer of pendingNudges.values()) clearTimeout(timer);
499
472
  pendingNudges.clear();
@@ -508,15 +481,6 @@ export default function (pi: ExtensionAPI) {
508
481
  function getDefaultJoinMode(): JoinMode { return defaultJoinMode; }
509
482
  function setDefaultJoinMode(mode: JoinMode) { defaultJoinMode = mode; }
510
483
 
511
- // Master switch for the schedule subagent feature. Defaults to enabled.
512
- // Read once at extension init (before tool registration) so the Agent tool's
513
- // param schema reflects the persisted setting. Runtime toggles via /agents
514
- // → Settings short-circuit the menu entry + the execute-time addJob path
515
- // immediately, but the schema-level removal only takes effect on next
516
- // extension load (next pi session). Documented in CHANGELOG/README.
517
- let schedulingEnabled = true;
518
- function isSchedulingEnabled(): boolean { return schedulingEnabled; }
519
- function setSchedulingEnabled(b: boolean) { schedulingEnabled = b; }
520
484
 
521
485
  // ---- Batch tracking for smart join mode ----
522
486
  // Collects background agent IDs spawned in the current turn for smart grouping.
@@ -611,35 +575,12 @@ export default function (pi: ExtensionAPI) {
611
575
  setDefaultMaxTurns,
612
576
  setGraceTurns,
613
577
  setDefaultJoinMode,
614
- setSchedulingEnabled,
615
578
  },
616
579
  (event, payload) => pi.events.emit(event, payload),
617
580
  );
618
581
 
619
582
  // ---- Agent tool ----
620
583
 
621
- // Schedule param + its guideline are gated on `schedulingEnabled` (read once
622
- // at registration; flipping the setting later requires next pi session for
623
- // the schema to update). Defining the shape once and spreading it via Partial
624
- // preserves Type.Object's inference when present and produces a
625
- // `schedule`-free schema when absent — zero LLM-context cost in disabled mode.
626
- const scheduleParamShape = {
627
- schedule: Type.Optional(
628
- Type.String({
629
- description:
630
- 'Opt-in only — fire later instead of now. Omit to run immediately (the default, almost always correct). ' +
631
- 'Formats: 6-field cron ("0 0 9 * * 1" = 9am Mon), interval ("5m"/"1h"), one-shot ("+10m" or ISO). ' +
632
- 'Forces run_in_background; incompatible with inherit_context and resume. Returns job ID.',
633
- }),
634
- ),
635
- };
636
- const scheduleParam: Partial<typeof scheduleParamShape> =
637
- isSchedulingEnabled() ? scheduleParamShape : {};
638
-
639
- const scheduleGuideline = isSchedulingEnabled()
640
- ? `\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.`
641
- : "";
642
-
643
584
  pi.registerTool(defineTool({
644
585
  name: "Agent",
645
586
  label: "Agent",
@@ -663,7 +604,7 @@ Guidelines:
663
604
  - Use model to specify a different model (as "provider/modelId", or fuzzy e.g. "haiku", "sonnet").
664
605
  - Use thinking to control extended thinking level.
665
606
  - Use inherit_context if the agent needs the parent conversation history.
666
- - Use isolation: "worktree" to run the agent in an isolated git worktree (safe parallel file modifications).${scheduleGuideline}`,
607
+ - Use isolation: "worktree" to run the agent in an isolated git worktree (safe parallel file modifications).`,
667
608
  parameters: Type.Object({
668
609
  prompt: Type.String({
669
610
  description: "The task for the agent to perform.",
@@ -716,7 +657,6 @@ Guidelines:
716
657
  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.',
717
658
  }),
718
659
  ),
719
- ...scheduleParam,
720
660
  }),
721
661
 
722
662
  // ---- Custom rendering: Claude Code style ----
@@ -877,47 +817,6 @@ Guidelines:
877
817
  tags: agentTags.length > 0 ? agentTags : undefined,
878
818
  };
879
819
 
880
- // ---- Schedule: register a job, don't spawn now ----
881
- if (params.schedule) {
882
- if (!isSchedulingEnabled()) {
883
- return textResult("Scheduling is disabled in this project. Enable via /agents → Settings → Scheduling.");
884
- }
885
- if (params.resume) {
886
- return textResult("Cannot combine `schedule` with `resume` — schedules create fresh agents.");
887
- }
888
- if (params.inherit_context) {
889
- return textResult("Cannot combine `schedule` with `inherit_context` — there is no parent conversation at fire time.");
890
- }
891
- if (params.run_in_background === false) {
892
- return textResult("Cannot combine `schedule` with `run_in_background: false` — scheduled jobs always run in background.");
893
- }
894
- if (!scheduler.isActive()) {
895
- return textResult("Scheduler is not active in this session yet. Try again after the session has fully started.");
896
- }
897
- try {
898
- const job = scheduler.addJob({
899
- name: params.description as string,
900
- description: params.description as string,
901
- schedule: params.schedule as string,
902
- subagent_type: subagentType,
903
- prompt: params.prompt as string,
904
- model: params.model as string | undefined,
905
- thinking: thinking,
906
- max_turns: effectiveMaxTurns,
907
- isolated: isolated,
908
- isolation: isolation,
909
- });
910
- const next = scheduler.getNextRun(job.id);
911
- return textResult(
912
- `Scheduled "${job.name}" (id: ${job.id}, type: ${job.scheduleType}). ` +
913
- `Next run: ${next ?? "(unknown)"}. ` +
914
- `Manage via /agents → Scheduled jobs.`,
915
- );
916
- } catch (err) {
917
- return textResult(err instanceof Error ? err.message : String(err));
918
- }
919
- }
920
-
921
820
  // Resume existing agent
922
821
  if (params.resume) {
923
822
  const existing = manager.getRecord(params.resume);
@@ -1294,12 +1193,6 @@ Guidelines:
1294
1193
  options.push(`Agent types (${allNames.length})`);
1295
1194
  }
1296
1195
 
1297
- // Scheduled jobs entry (always present when scheduler is active)
1298
- if (scheduler.isActive()) {
1299
- const jobCount = scheduler.list().length;
1300
- options.push(`Scheduled jobs (${jobCount})`);
1301
- }
1302
-
1303
1196
  // Actions
1304
1197
  options.push("Create new agent");
1305
1198
  options.push("Settings");
@@ -1323,9 +1216,6 @@ Guidelines:
1323
1216
  } else if (choice.startsWith("Agent types (")) {
1324
1217
  await showAllAgentsList(ctx);
1325
1218
  await showAgentsMenu(ctx);
1326
- } else if (choice.startsWith("Scheduled jobs (")) {
1327
- await showSchedulesMenu(ctx, scheduler);
1328
- await showAgentsMenu(ctx);
1329
1219
  } else if (choice === "Create new agent") {
1330
1220
  await showCreateWizard(ctx);
1331
1221
  } else if (choice === "Settings") {
@@ -1789,7 +1679,6 @@ ${systemPrompt}
1789
1679
  defaultMaxTurns: getDefaultMaxTurns() ?? 0,
1790
1680
  graceTurns: getGraceTurns(),
1791
1681
  defaultJoinMode: getDefaultJoinMode(),
1792
- schedulingEnabled: isSchedulingEnabled(),
1793
1682
  };
1794
1683
  }
1795
1684
 
@@ -1799,7 +1688,6 @@ ${systemPrompt}
1799
1688
  `Default max turns (current: ${getDefaultMaxTurns() ?? "unlimited"})`,
1800
1689
  `Grace turns (current: ${getGraceTurns()})`,
1801
1690
  `Join mode (current: ${getDefaultJoinMode()})`,
1802
- `Scheduling (current: ${isSchedulingEnabled() ? "enabled" : "disabled"})`,
1803
1691
  ]);
1804
1692
  if (!choice) return;
1805
1693
 
@@ -1850,27 +1738,6 @@ ${systemPrompt}
1850
1738
  setDefaultJoinMode(mode);
1851
1739
  notifyApplied(ctx, `Default join mode set to ${mode}`);
1852
1740
  }
1853
- } else if (choice.startsWith("Scheduling")) {
1854
- const val = await ctx.ui.select(
1855
- "Schedule subagent feature",
1856
- [
1857
- "enabled — Agent tool accepts a `schedule` param; /agents → Scheduled jobs visible",
1858
- "disabled — `schedule` removed from Agent tool spec (no LLM-context cost); menu hidden",
1859
- ],
1860
- );
1861
- if (val) {
1862
- const enabled = val.startsWith("enabled");
1863
- if (enabled === isSchedulingEnabled()) {
1864
- ctx.ui.notify(`Scheduling already ${enabled ? "enabled" : "disabled"}.`, "info");
1865
- } else {
1866
- setSchedulingEnabled(enabled);
1867
- if (!enabled) scheduler.stop(); // immediate kill — outstanding fires stop ticking
1868
- notifyApplied(
1869
- ctx,
1870
- `Scheduling ${enabled ? "enabled" : "disabled"}. Tool spec change takes effect on next pi session.`,
1871
- );
1872
- }
1873
- }
1874
1741
  }
1875
1742
  }
1876
1743
 
package/src/settings.ts CHANGED
@@ -17,15 +17,6 @@ export interface SubagentsSettings {
17
17
  defaultMaxTurns?: number;
18
18
  graceTurns?: number;
19
19
  defaultJoinMode?: JoinMode;
20
- /**
21
- * Master switch for the schedule subagent feature. Defaults to `true`.
22
- * When `false`: the `Agent` tool's `schedule` param + its guideline are
23
- * stripped from the tool spec at registration (zero LLM-context cost), the
24
- * scheduler doesn't bind to the session, and the `/agents → Scheduled jobs`
25
- * menu entry is hidden. Schema-level removal applies at extension load
26
- * (next pi session); runtime menu/runtime-fire short-circuit is immediate.
27
- */
28
- schedulingEnabled?: boolean;
29
20
  }
30
21
 
31
22
  /** Setter hooks used by applySettings to wire persisted values into in-memory state. */
@@ -34,7 +25,6 @@ export interface SettingsAppliers {
34
25
  setDefaultMaxTurns: (n: number) => void;
35
26
  setGraceTurns: (n: number) => void;
36
27
  setDefaultJoinMode: (mode: JoinMode) => void;
37
- setSchedulingEnabled: (b: boolean) => void;
38
28
  }
39
29
 
40
30
  /** Emit callback — a subset of `pi.events.emit` to keep helpers testable. */
@@ -78,9 +68,6 @@ function sanitize(raw: unknown): SubagentsSettings {
78
68
  if (typeof r.defaultJoinMode === "string" && VALID_JOIN_MODES.has(r.defaultJoinMode)) {
79
69
  out.defaultJoinMode = r.defaultJoinMode as JoinMode;
80
70
  }
81
- if (typeof r.schedulingEnabled === "boolean") {
82
- out.schedulingEnabled = r.schedulingEnabled;
83
- }
84
71
  return out;
85
72
  }
86
73
 
@@ -135,7 +122,6 @@ export function applySettings(s: SubagentsSettings, appliers: SettingsAppliers):
135
122
  if (typeof s.defaultMaxTurns === "number") appliers.setDefaultMaxTurns(s.defaultMaxTurns);
136
123
  if (typeof s.graceTurns === "number") appliers.setGraceTurns(s.graceTurns);
137
124
  if (s.defaultJoinMode) appliers.setDefaultJoinMode(s.defaultJoinMode);
138
- if (typeof s.schedulingEnabled === "boolean") appliers.setSchedulingEnabled(s.schedulingEnabled);
139
125
  }
140
126
 
141
127
  /**
package/src/types.ts CHANGED
@@ -131,46 +131,3 @@ export interface EnvInfo {
131
131
  branch: string;
132
132
  platform: string;
133
133
  }
134
-
135
- /**
136
- * A subagent spawn registered to fire on a schedule.
137
- *
138
- * Stored at `<cwd>/.pi/subagent-schedules/<sessionId>.json`. Session-scoped:
139
- * survives `/resume` but resets on `/new`, mirroring pi-chonky-tasks.
140
- */
141
- export interface ScheduledSubagent {
142
- id: string;
143
- /** Unique within store. Defaults to `description`. */
144
- name: string;
145
- description: string;
146
- /** Raw user input — cron expr | "+10m" | ISO | "5m". */
147
- schedule: string;
148
- scheduleType: "cron" | "once" | "interval";
149
- /** Computed at create time for interval/once. */
150
- intervalMs?: number;
151
-
152
- // spawn params (subset of Agent tool params; no inherit_context, no resume)
153
- subagent_type: SubagentType;
154
- prompt: string;
155
- model?: string;
156
- thinking?: ThinkingLevel;
157
- max_turns?: number;
158
- isolated?: boolean;
159
- isolation?: IsolationMode;
160
-
161
- // state
162
- enabled: boolean;
163
- /** ISO timestamp. */
164
- createdAt: string;
165
- lastRun?: string;
166
- lastStatus?: "success" | "error" | "running";
167
- /** Refreshed on every fire and on store load. */
168
- nextRun?: string;
169
- runCount: number;
170
- }
171
-
172
- export interface ScheduleStoreData {
173
- /** For future migrations. */
174
- version: 1;
175
- jobs: ScheduledSubagent[];
176
- }
@@ -1,143 +0,0 @@
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
-
13
- import { existsSync, mkdirSync, readFileSync, renameSync, unlinkSync, writeFileSync } from "node:fs";
14
- import { dirname, join } from "node:path";
15
- import type { ScheduledSubagent, ScheduleStoreData } from "./types.js";
16
-
17
- const LOCK_RETRY_MS = 50;
18
- const LOCK_MAX_RETRIES = 100;
19
-
20
- function isProcessRunning(pid: number): boolean {
21
- try { process.kill(pid, 0); return true; } catch { return false; }
22
- }
23
-
24
- function acquireLock(lockPath: string): void {
25
- for (let i = 0; i < LOCK_MAX_RETRIES; i++) {
26
- try {
27
- writeFileSync(lockPath, `${process.pid}`, { flag: "wx" });
28
- return;
29
- } catch (e: any) {
30
- if (e.code === "EEXIST") {
31
- try {
32
- const pid = parseInt(readFileSync(lockPath, "utf-8"), 10);
33
- if (pid && !isProcessRunning(pid)) {
34
- unlinkSync(lockPath);
35
- continue;
36
- }
37
- } catch { /* ignore — try again */ }
38
- const start = Date.now();
39
- while (Date.now() - start < LOCK_RETRY_MS) { /* busy wait */ }
40
- continue;
41
- }
42
- throw e;
43
- }
44
- }
45
- throw new Error(`Failed to acquire schedule lock: ${lockPath}`);
46
- }
47
-
48
- function releaseLock(lockPath: string): void {
49
- try { unlinkSync(lockPath); } catch { /* ignore */ }
50
- }
51
-
52
- /** Resolve the storage path for a session-scoped store. */
53
- export function resolveStorePath(cwd: string, sessionId: string): string {
54
- return join(cwd, ".pi", "subagent-schedules", `${sessionId}.json`);
55
- }
56
-
57
- export class ScheduleStore {
58
- private filePath: string;
59
- private lockPath: string;
60
- private jobs = new Map<string, ScheduledSubagent>();
61
-
62
- constructor(filePath: string) {
63
- this.filePath = filePath;
64
- this.lockPath = filePath + ".lock";
65
- mkdirSync(dirname(filePath), { recursive: true });
66
- this.load();
67
- }
68
-
69
- /** Load from disk into the in-memory cache. Silent on parse errors. */
70
- private load(): void {
71
- if (!existsSync(this.filePath)) return;
72
- try {
73
- const data: ScheduleStoreData = JSON.parse(readFileSync(this.filePath, "utf-8"));
74
- this.jobs.clear();
75
- for (const j of data.jobs ?? []) this.jobs.set(j.id, j);
76
- } catch { /* corrupt — start fresh, next save rewrites */ }
77
- }
78
-
79
- /** Atomic write via temp file + rename (POSIX-atomic). */
80
- private save(): void {
81
- const data: ScheduleStoreData = { version: 1, jobs: [...this.jobs.values()] };
82
- const tmp = this.filePath + ".tmp";
83
- writeFileSync(tmp, JSON.stringify(data, null, 2));
84
- renameSync(tmp, this.filePath);
85
- }
86
-
87
- /** Acquire lock → reload → mutate → save → release. */
88
- private withLock<T>(fn: () => T): T {
89
- acquireLock(this.lockPath);
90
- try {
91
- this.load();
92
- const result = fn();
93
- this.save();
94
- return result;
95
- } finally {
96
- releaseLock(this.lockPath);
97
- }
98
- }
99
-
100
- /** Read-only — returns a snapshot of the in-memory cache. */
101
- list(): ScheduledSubagent[] {
102
- return [...this.jobs.values()];
103
- }
104
-
105
- /** Read-only check — uses the cache. */
106
- hasName(name: string, exceptId?: string): boolean {
107
- for (const j of this.jobs.values()) {
108
- if (j.id !== exceptId && j.name === name) return true;
109
- }
110
- return false;
111
- }
112
-
113
- get(id: string): ScheduledSubagent | undefined {
114
- return this.jobs.get(id);
115
- }
116
-
117
- add(job: ScheduledSubagent): void {
118
- this.withLock(() => {
119
- this.jobs.set(job.id, job);
120
- });
121
- }
122
-
123
- update(id: string, patch: Partial<ScheduledSubagent>): ScheduledSubagent | undefined {
124
- return this.withLock(() => {
125
- const existing = this.jobs.get(id);
126
- if (!existing) return undefined;
127
- const updated = { ...existing, ...patch };
128
- this.jobs.set(id, updated);
129
- return updated;
130
- });
131
- }
132
-
133
- remove(id: string): boolean {
134
- return this.withLock(() => this.jobs.delete(id));
135
- }
136
-
137
- /** Delete the backing file (used when no jobs remain, optional cleanup). */
138
- deleteFileIfEmpty(): void {
139
- if (this.jobs.size === 0 && existsSync(this.filePath)) {
140
- try { unlinkSync(this.filePath); } catch { /* ignore */ }
141
- }
142
- }
143
- }
package/src/schedule.ts DELETED
@@ -1,365 +0,0 @@
1
- /**
2
- * schedule.ts — `SubagentScheduler`: timer-driven dispatcher of scheduled subagents.
3
- *
4
- * Mirrors the engine shape of pi-cron-schedule/src/scheduler.ts:
5
- * - two-Map split (jobs = croner Cron, intervals = setInterval/setTimeout)
6
- * - addJob/removeJob/updateJob/scheduleJob/unscheduleJob/executeJob
7
- * - static parsers for cron / "+10m" / "5m" / ISO formats
8
- *
9
- * Differences vs pi-cron-schedule:
10
- * - Persistence is via ScheduleStore (PID-locked, session-scoped, atomic).
11
- * - `executeJob` calls `manager.spawn(..., { bypassQueue: true })` instead
12
- * of dispatching a user message — schedule fires bypass maxConcurrent so
13
- * a 5-minute interval can't be deferred behind 4 long-running agents.
14
- * - Result delivery is implicit: spawn → background completion → existing
15
- * `subagent-notification` followUp path. No new delivery code.
16
- */
17
-
18
- import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
19
- import { Cron } from "croner";
20
- import { nanoid } from "nanoid";
21
- import type { AgentManager } from "./agent-manager.js";
22
- import { resolveModel } from "./model-resolver.js";
23
- import type { ScheduleStore } from "./schedule-store.js";
24
- import type { IsolationMode, ScheduledSubagent, SubagentType, ThinkingLevel } from "./types.js";
25
-
26
- /** Event emitted on `pi.events` for cross-extension consumers. */
27
- export type ScheduleChangeEvent =
28
- | { type: "added"; job: ScheduledSubagent }
29
- | { type: "removed"; jobId: string }
30
- | { type: "updated"; job: ScheduledSubagent }
31
- | { type: "fired"; jobId: string; agentId: string; name: string }
32
- | { type: "error"; jobId: string; error: string };
33
-
34
- /** Params accepted at job creation — ID, timestamps, and state are derived. */
35
- export interface NewJobInput {
36
- name: string;
37
- description: string;
38
- schedule: string;
39
- subagent_type: SubagentType;
40
- prompt: string;
41
- model?: string;
42
- thinking?: ThinkingLevel;
43
- max_turns?: number;
44
- isolated?: boolean;
45
- isolation?: IsolationMode;
46
- }
47
-
48
- export class SubagentScheduler {
49
- private jobs = new Map<string, Cron>();
50
- private intervals = new Map<string, NodeJS.Timeout>();
51
- private store: ScheduleStore | undefined;
52
- private pi: ExtensionAPI | undefined;
53
- private ctx: ExtensionContext | undefined;
54
- private manager: AgentManager | undefined;
55
-
56
- /** Start the scheduler: bind to a session's store and arm enabled jobs. */
57
- start(pi: ExtensionAPI, ctx: ExtensionContext, manager: AgentManager, store: ScheduleStore): void {
58
- this.pi = pi;
59
- this.ctx = ctx;
60
- this.manager = manager;
61
- this.store = store;
62
-
63
- for (const job of store.list()) {
64
- if (job.enabled) this.scheduleJob(job);
65
- }
66
- }
67
-
68
- /** Stop all timers; drop refs. Safe to call repeatedly. */
69
- stop(): void {
70
- for (const cron of this.jobs.values()) cron.stop();
71
- this.jobs.clear();
72
- for (const t of this.intervals.values()) clearTimeout(t);
73
- this.intervals.clear();
74
- this.store = undefined;
75
- this.pi = undefined;
76
- this.ctx = undefined;
77
- this.manager = undefined;
78
- }
79
-
80
- /** True if start() has bound a store and the scheduler is active. */
81
- isActive(): boolean {
82
- return this.store !== undefined;
83
- }
84
-
85
- list(): ScheduledSubagent[] {
86
- return this.store?.list() ?? [];
87
- }
88
-
89
- /**
90
- * Build a `ScheduledSubagent` from user input. Validates the schedule
91
- * format and tags `scheduleType`. Throws on invalid input.
92
- */
93
- buildJob(input: NewJobInput): ScheduledSubagent {
94
- const detected = SubagentScheduler.detectSchedule(input.schedule);
95
- return {
96
- id: nanoid(10),
97
- name: input.name,
98
- description: input.description,
99
- schedule: detected.normalized,
100
- scheduleType: detected.type,
101
- intervalMs: detected.intervalMs,
102
- subagent_type: input.subagent_type,
103
- prompt: input.prompt,
104
- model: input.model,
105
- thinking: input.thinking,
106
- max_turns: input.max_turns,
107
- isolated: input.isolated,
108
- isolation: input.isolation,
109
- enabled: true,
110
- createdAt: new Date().toISOString(),
111
- runCount: 0,
112
- };
113
- }
114
-
115
- /** Add a job, persist, and arm if enabled. Returns the stored job. */
116
- addJob(input: NewJobInput): ScheduledSubagent {
117
- const store = this.requireStore();
118
- if (store.hasName(input.name)) {
119
- throw new Error(`A scheduled job named "${input.name}" already exists.`);
120
- }
121
- const job = this.buildJob(input);
122
- store.add(job);
123
- if (job.enabled) this.scheduleJob(job);
124
- this.emit({ type: "added", job });
125
- return job;
126
- }
127
-
128
- removeJob(id: string): boolean {
129
- const store = this.requireStore();
130
- if (!store.get(id)) return false;
131
- this.unscheduleJob(id);
132
- const ok = store.remove(id);
133
- if (ok) this.emit({ type: "removed", jobId: id });
134
- return ok;
135
- }
136
-
137
- /** Toggle / mutate a job. Re-arms based on the new `enabled` state. */
138
- updateJob(id: string, patch: Partial<ScheduledSubagent>): ScheduledSubagent | undefined {
139
- const store = this.requireStore();
140
- const updated = store.update(id, patch);
141
- if (!updated) return undefined;
142
- this.unscheduleJob(id);
143
- if (updated.enabled) this.scheduleJob(updated);
144
- this.emit({ type: "updated", job: updated });
145
- return updated;
146
- }
147
-
148
- /** Next-run time as ISO, or undefined if not currently armed. */
149
- getNextRun(jobId: string): string | undefined {
150
- const cron = this.jobs.get(jobId);
151
- if (cron) return cron.nextRun()?.toISOString();
152
- const job = this.store?.get(jobId);
153
- if (!job?.enabled) return undefined;
154
- if (job.scheduleType === "once") return job.schedule;
155
- if (job.scheduleType === "interval" && job.intervalMs) {
156
- // Before the first fire there's no `lastRun`, so fall back to "now" —
157
- // accurate at create time (setInterval was just armed) and within
158
- // intervalMs of correct in any pre-first-fire view.
159
- const base = job.lastRun ? new Date(job.lastRun).getTime() : Date.now();
160
- return new Date(base + job.intervalMs).toISOString();
161
- }
162
- return undefined;
163
- }
164
-
165
- // ── Scheduling primitives ────────────────────────────────────────────
166
-
167
- private scheduleJob(job: ScheduledSubagent): void {
168
- const store = this.store;
169
- if (!store) return;
170
- try {
171
- if (job.scheduleType === "interval" && job.intervalMs) {
172
- const t = setInterval(() => this.executeJob(job.id), job.intervalMs);
173
- this.intervals.set(job.id, t);
174
- } else if (job.scheduleType === "once") {
175
- const target = new Date(job.schedule).getTime();
176
- const delay = target - Date.now();
177
- if (delay > 0) {
178
- const t = setTimeout(() => {
179
- this.executeJob(job.id);
180
- // Auto-disable one-shots after they fire (mirrors pi-cron-schedule)
181
- store.update(job.id, { enabled: false });
182
- const updated = store.get(job.id);
183
- if (updated) this.emit({ type: "updated", job: updated });
184
- }, delay);
185
- this.intervals.set(job.id, t);
186
- } else {
187
- // Past timestamp — disable, mark error, never fire
188
- store.update(job.id, { enabled: false, lastStatus: "error" });
189
- this.emit({ type: "error", jobId: job.id, error: `Scheduled time ${job.schedule} is in the past` });
190
- }
191
- } else {
192
- const cron = new Cron(job.schedule, () => this.executeJob(job.id));
193
- this.jobs.set(job.id, cron);
194
- }
195
- } catch (err) {
196
- this.emit({ type: "error", jobId: job.id, error: err instanceof Error ? err.message : String(err) });
197
- }
198
- }
199
-
200
- private unscheduleJob(id: string): void {
201
- const cron = this.jobs.get(id);
202
- if (cron) {
203
- cron.stop();
204
- this.jobs.delete(id);
205
- }
206
- const t = this.intervals.get(id);
207
- if (t) {
208
- clearTimeout(t);
209
- clearInterval(t);
210
- this.intervals.delete(id);
211
- }
212
- }
213
-
214
- /**
215
- * Fire a job: persist running state, spawn (bypassing the concurrency
216
- * queue), persist completion. Fire-and-forget: the timer tick returns
217
- * immediately so other jobs keep firing.
218
- */
219
- private executeJob(id: string): void {
220
- const store = this.store;
221
- const pi = this.pi;
222
- const ctx = this.ctx;
223
- const manager = this.manager;
224
- if (!store || !pi || !ctx || !manager) return;
225
- const job = store.get(id);
226
- if (!job?.enabled) return;
227
-
228
- store.update(id, { lastStatus: "running" });
229
-
230
- // Resolve model at fire time — registry contents may have changed since the
231
- // job was created (auth added/removed). Fall back silently to spawn-default
232
- // if resolution fails; the spawn path handles undefined model gracefully.
233
- let resolvedModel: any | undefined;
234
- if (job.model) {
235
- const r = resolveModel(job.model, ctx.modelRegistry);
236
- if (typeof r !== "string") resolvedModel = r;
237
- }
238
-
239
- let agentId: string;
240
- try {
241
- agentId = manager.spawn(pi, ctx, job.subagent_type, job.prompt, {
242
- description: job.description,
243
- isBackground: true,
244
- bypassQueue: true,
245
- model: resolvedModel,
246
- maxTurns: job.max_turns,
247
- isolated: job.isolated,
248
- thinkingLevel: job.thinking,
249
- isolation: job.isolation,
250
- });
251
- } catch (err) {
252
- const error = err instanceof Error ? err.message : String(err);
253
- store.update(id, { lastRun: new Date().toISOString(), lastStatus: "error" });
254
- this.emit({ type: "error", jobId: id, error });
255
- return;
256
- }
257
-
258
- this.emit({ type: "fired", jobId: id, agentId, name: job.name });
259
-
260
- const record = manager.getRecord(agentId);
261
- const finalize = (status: "success" | "error") => {
262
- const next = this.getNextRun(id);
263
- const current = store.get(id);
264
- store.update(id, {
265
- lastRun: new Date().toISOString(),
266
- lastStatus: status,
267
- runCount: (current?.runCount ?? 0) + 1,
268
- nextRun: next,
269
- });
270
- };
271
-
272
- // AgentManager's promise resolves either way (its .catch returns ""), so we
273
- // can't infer success/failure from the promise — read record.status instead.
274
- // Terminal states: completed/steered = success; error/aborted/stopped = error.
275
- if (record?.promise) {
276
- record.promise
277
- .then(() => {
278
- const r = manager.getRecord(agentId);
279
- const failed = r?.status === "error" || r?.status === "aborted" || r?.status === "stopped";
280
- finalize(failed ? "error" : "success");
281
- })
282
- .catch(() => finalize("error"));
283
- } else {
284
- // Spawn returned without a promise (defensive — bypassQueue path always sets one).
285
- finalize("success");
286
- }
287
- }
288
-
289
- private emit(event: ScheduleChangeEvent): void {
290
- if (this.pi) this.pi.events.emit("subagents:scheduled", event);
291
- }
292
-
293
- private requireStore(): ScheduleStore {
294
- if (!this.store) throw new Error("Scheduler not started — no active session.");
295
- return this.store;
296
- }
297
-
298
- // ── Format detection / parsers (statics — pure) ──────────────────────
299
-
300
- /**
301
- * Sniff a schedule string and tag its type. Throws on invalid input.
302
- * Order matters: relative ("+10m") and interval ("5m") both match digit+unit;
303
- * relative requires the leading "+" to disambiguate.
304
- */
305
- static detectSchedule(s: string): { type: "cron" | "once" | "interval"; intervalMs?: number; normalized: string } {
306
- const trimmed = s.trim();
307
- // "+10m" — relative one-shot
308
- const rel = SubagentScheduler.parseRelativeTime(trimmed);
309
- if (rel !== null) return { type: "once", normalized: rel };
310
- // "5m" — interval
311
- const ivl = SubagentScheduler.parseInterval(trimmed);
312
- if (ivl !== null) return { type: "interval", intervalMs: ivl, normalized: trimmed };
313
- // ISO timestamp — one-shot. Reject past timestamps upfront so we never
314
- // create a dead-on-arrival record (scheduleJob's safety net still catches
315
- // micro-races from `+0s`-style relatives).
316
- if (/^\d{4}-\d{2}-\d{2}T/.test(trimmed)) {
317
- const d = new Date(trimmed);
318
- if (!Number.isNaN(d.getTime())) {
319
- if (d.getTime() <= Date.now()) {
320
- throw new Error(`Scheduled time ${d.toISOString()} is in the past.`);
321
- }
322
- return { type: "once", normalized: d.toISOString() };
323
- }
324
- }
325
- // Cron — 6-field
326
- const cronCheck = SubagentScheduler.validateCronExpression(trimmed);
327
- if (cronCheck.valid) return { type: "cron", normalized: trimmed };
328
- throw new Error(
329
- `Invalid schedule "${s}". Use 6-field cron (e.g. "0 0 9 * * 1" — 9am every Monday), interval ("5m"/"1h"), or one-shot ("+10m" / ISO).`
330
- );
331
- }
332
-
333
- /** 6-field cron — 'second minute hour dom month dow'. */
334
- static validateCronExpression(expr: string): { valid: boolean; error?: string } {
335
- const fields = expr.trim().split(/\s+/);
336
- if (fields.length !== 6) {
337
- return {
338
- valid: false,
339
- error: `Cron must have 6 fields (second minute hour dom month dow), got ${fields.length}. Example: "0 0 9 * * 1" for 9am every Monday.`,
340
- };
341
- }
342
- try {
343
- // Croner validates by construction.
344
- new Cron(expr, () => {});
345
- return { valid: true };
346
- } catch (e) {
347
- return { valid: false, error: e instanceof Error ? e.message : "Invalid cron expression" };
348
- }
349
- }
350
-
351
- /** "+10s"/"+5m"/"+1h"/"+2d" → ISO timestamp. */
352
- static parseRelativeTime(s: string): string | null {
353
- const m = s.match(/^\+(\d+)(s|m|h|d)$/);
354
- if (!m) return null;
355
- const ms = parseInt(m[1], 10) * { s: 1000, m: 60_000, h: 3_600_000, d: 86_400_000 }[m[2] as "s" | "m" | "h" | "d"];
356
- return new Date(Date.now() + ms).toISOString();
357
- }
358
-
359
- /** "10s"/"5m"/"1h"/"2d" → milliseconds. */
360
- static parseInterval(s: string): number | null {
361
- const m = s.match(/^(\d+)(s|m|h|d)$/);
362
- if (!m) return null;
363
- return parseInt(m[1], 10) * { s: 1000, m: 60_000, h: 3_600_000, d: 86_400_000 }[m[2] as "s" | "m" | "h" | "d"];
364
- }
365
- }
@@ -1,104 +0,0 @@
1
- /**
2
- * schedule-menu.ts — `/agents → Scheduled jobs` submenu.
3
- *
4
- * Minimal v1 surface: list scheduled jobs, select one to inspect details +
5
- * confirm cancellation. No create wizard (the `Agent` tool's `schedule` param
6
- * is the canonical creation path), no toggle/cleanup (cancel is enough for
7
- * "I scheduled something dumb, get rid of it"). Add management surfaces here
8
- * if real demand emerges.
9
- */
10
-
11
- import type { ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
12
- import type { SubagentScheduler } from "../schedule.js";
13
- import type { ScheduledSubagent } from "../types.js";
14
-
15
- /** Format an ISO timestamp as relative time ("in 4h", "2d ago", "—"). */
16
- function relTime(iso: string | undefined, now = Date.now()): string {
17
- if (!iso) return "—";
18
- const t = new Date(iso).getTime();
19
- if (Number.isNaN(t)) return "—";
20
- const diff = t - now;
21
- const abs = Math.abs(diff);
22
- const future = diff > 0;
23
- if (abs < 60_000) return future ? "in <1m" : "<1m ago";
24
- const m = Math.round(abs / 60_000);
25
- if (m < 60) return future ? `in ${m}m` : `${m}m ago`;
26
- const h = Math.round(abs / 3_600_000);
27
- if (h < 24) return future ? `in ${h}h` : `${h}h ago`;
28
- const d = Math.round(abs / 86_400_000);
29
- return future ? `in ${d}d` : `${d}d ago`;
30
- }
31
-
32
- /** One-line status icon. */
33
- function statusIcon(j: ScheduledSubagent): string {
34
- if (!j.enabled) return "✗";
35
- if (j.lastStatus === "error") return "!";
36
- if (j.lastStatus === "running") return "⋯";
37
- return "✓";
38
- }
39
-
40
- /** Compact selectable row — name, schedule, agent type, next/last run, count. */
41
- function formatJob(j: ScheduledSubagent, scheduler: SubagentScheduler): string {
42
- const next = scheduler.getNextRun(j.id);
43
- return [
44
- statusIcon(j),
45
- j.name.padEnd(18).slice(0, 18),
46
- j.schedule.padEnd(14).slice(0, 14),
47
- `[${j.subagent_type}]`,
48
- `next ${relTime(next)}`,
49
- `last ${relTime(j.lastRun)}`,
50
- `runs ${j.runCount}`,
51
- ].join(" ");
52
- }
53
-
54
- /** Multi-line details block for the cancel confirm. */
55
- function formatDetails(j: ScheduledSubagent, scheduler: SubagentScheduler): string {
56
- const next = scheduler.getNextRun(j.id) ?? "—";
57
- return [
58
- `name: ${j.name}`,
59
- `schedule: ${j.schedule} (${j.scheduleType})`,
60
- `agent: ${j.subagent_type}`,
61
- `prompt: ${j.prompt.slice(0, 200)}${j.prompt.length > 200 ? "…" : ""}`,
62
- `created: ${j.createdAt}`,
63
- `last run: ${j.lastRun ?? "—"} (${j.lastStatus ?? "—"})`,
64
- `next run: ${next}`,
65
- `runs: ${j.runCount}`,
66
- ].join("\n");
67
- }
68
-
69
- /**
70
- * List scheduled jobs; selecting one opens a cancel-confirm with details.
71
- * Returns when the user backs out or after a cancellation.
72
- */
73
- export async function showSchedulesMenu(
74
- ctx: ExtensionCommandContext,
75
- scheduler: SubagentScheduler,
76
- ): Promise<void> {
77
- if (!scheduler.isActive()) {
78
- ctx.ui.notify("Scheduler is not active in this session.", "warning");
79
- return;
80
- }
81
-
82
- const jobs = scheduler.list();
83
- if (jobs.length === 0) {
84
- ctx.ui.notify("No scheduled jobs.", "info");
85
- return;
86
- }
87
-
88
- const labels = jobs.map(j => formatJob(j, scheduler));
89
- const choice = await ctx.ui.select(
90
- `Scheduled jobs (${jobs.length}) — select to cancel`,
91
- labels,
92
- );
93
- if (!choice) return;
94
-
95
- const idx = labels.indexOf(choice);
96
- if (idx < 0) return;
97
- const job = jobs[idx];
98
-
99
- const ok = await ctx.ui.confirm(`Cancel "${job.name}"?`, formatDetails(job, scheduler));
100
- if (!ok) return;
101
-
102
- scheduler.removeJob(job.id);
103
- ctx.ui.notify(`Cancelled "${job.name}".`, "info");
104
- }