@jl1990/pi-scheduler 0.3.0 → 0.3.1

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.
@@ -10,6 +10,7 @@ import { Type } from "typebox";
10
10
 
11
11
  // Keep the scheduler logic testable from plain node --test.
12
12
  const core = require("./scheduler-core.cjs");
13
+ const lifecycle = require("./scheduler-lifecycle.cjs");
13
14
 
14
15
  const ACTIONS = ["notify", "prompt", "shell", "message"] as const;
15
16
  const TYPES = ["once", "interval", "cron"] as const;
@@ -123,13 +124,7 @@ export default function schedulerExtension(pi: ExtensionAPI) {
123
124
  const firing = new Set<string>();
124
125
 
125
126
  function isSessionActive(ctx: ExtensionContext, generation = sessionGeneration): boolean {
126
- if (activeCtx !== ctx || sessionGeneration !== generation) return false;
127
- try {
128
- ctx.isIdle(); // Pi throws when a captured extension context has become stale.
129
- return true;
130
- } catch {
131
- return false;
132
- }
127
+ return lifecycle.isSessionContextActive(activeCtx, ctx, sessionGeneration, generation);
133
128
  }
134
129
 
135
130
  async function loadTasks(): Promise<void> {
@@ -490,7 +485,7 @@ export default function schedulerExtension(pi: ExtensionAPI) {
490
485
  });
491
486
 
492
487
  pi.on("session_shutdown", async (_event, ctx) => {
493
- if (activeCtx !== ctx) return;
488
+ if (!isSessionActive(ctx)) return;
494
489
  ++sessionGeneration;
495
490
  clearTimers();
496
491
  if (ctx.hasUI) {
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+
3
+ function isSessionContextActive(activeCtx, candidateCtx, currentGeneration, expectedGeneration) {
4
+ if (!activeCtx || currentGeneration !== expectedGeneration) return false;
5
+ try {
6
+ // Pi creates distinct context wrappers for events, commands, and tool calls.
7
+ // Probe both wrappers for staleness instead of comparing object identity.
8
+ activeCtx.isIdle();
9
+ candidateCtx.isIdle();
10
+ return true;
11
+ } catch {
12
+ return false;
13
+ }
14
+ }
15
+
16
+ module.exports = { isSessionContextActive };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jl1990/pi-scheduler",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "description": "Give Pi coding agents a clock: schedule reminders, shell commands, and self-waking prompts for CI polling and autonomous follow-ups.",
5
5
  "author": "jl1990",
6
6
  "license": "MIT",