pi-better-goal 0.17.0 → 0.18.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-better-goal",
3
- "version": "0.17.0",
3
+ "version": "0.18.0",
4
4
  "description": "Pi extension for goal tracking with background-aware continuation.",
5
5
  "license": "MIT",
6
6
  "type": "module",
package/src/index.ts CHANGED
@@ -49,6 +49,9 @@ import { workflowRehydrateMessage, WORKFLOW_REHYDRATE_MESSAGE_TYPE } from "./reh
49
49
  import { canStartGoalFromOutcome, GOAL_PANEL_START_EVENT, type GoalPanelStartRequest, OUTCOME_ACCEPTED_EVENT, requestTaskContext, type OutcomeAcceptedEvent } from "./task-context.js";
50
50
  import {
51
51
  EVENT_ACTIVITY,
52
+ sessionIdleEvent,
53
+ type SessionIdleInspection,
54
+ type SessionIdleState,
52
55
  EVENT_READY,
53
56
  EVENT_REGISTER_PROVIDER,
54
57
  EVENT_TERMINAL_ATTENTION,
@@ -348,6 +351,27 @@ export default function (pi: ExtensionAPI): void {
348
351
  let collecting = false;
349
352
  let collectionPending = false;
350
353
  let latestSnapshot: ActivitySnapshot | null = null;
354
+ let idleChannel: string | undefined;
355
+ let stopIdleInspection: (() => void) | undefined;
356
+ let sessionIdle: SessionIdleState = false;
357
+ let verifyIdleOnSettle = false;
358
+
359
+ function setSessionIdle(idle: SessionIdleState) {
360
+ if (!idleChannel || idle === sessionIdle) return;
361
+ sessionIdle = idle;
362
+ pi.events.emit(idleChannel, idle);
363
+ }
364
+
365
+ function updateSessionIdle(goal: GoalSnapshot | null) {
366
+ if (foregroundRunning || (goal && goal.status !== "paused" && goal.status !== "complete")) {
367
+ setSessionIdle(false);
368
+ } else if (dirtyProviders.size || lastAppliedSnapshotSequence !== snapshotSequence) {
369
+ setSessionIdle("checking");
370
+ } else {
371
+ setSessionIdle(latestSnapshot?.coverage === "verified" && !latestSnapshot.foregroundRunning && !latestSnapshot.backgroundRunning);
372
+ }
373
+ }
374
+
351
375
  let backgroundDrainTracker: BackgroundDrainTracker | null = null;
352
376
  let lastAttentionSignature = "";
353
377
  let continuationQueuedFor: string | null = null;
@@ -435,6 +459,7 @@ export default function (pi: ExtensionAPI): void {
435
459
  clearIdleContinuation();
436
460
  syncPollingState();
437
461
  syncResumeTool(goal);
462
+ updateSessionIdle(goal);
438
463
  // Force a full redraw when the dock height changes (absent ↔ visible clock).
439
464
  refreshGoalWidget?.(!wasVisible || !isGoalClockVisible(goal));
440
465
  };
@@ -453,6 +478,7 @@ export default function (pi: ExtensionAPI): void {
453
478
  clearIdleContinuation();
454
479
  syncPollingState();
455
480
  syncResumeTool(null);
481
+ updateSessionIdle(null);
456
482
  refreshGoalWidget?.(wasVisible);
457
483
  };
458
484
 
@@ -699,6 +725,7 @@ export default function (pi: ExtensionAPI): void {
699
725
  if (cached && !provider.onActivityChanged && !explicit && Date.now() < (compatibilityReadAt.get(provider.id) ?? 0)) return cached;
700
726
  if (cached && provider.onActivityChanged && !dirtyProviders.has(provider.id) &&
701
727
  !(typeof cached.nextDeadline === "number" && cached.nextDeadline <= Date.now())) return cached;
728
+ if (sessionIdle === true) setSessionIdle("checking");
702
729
  dirtyProviders.delete(provider.id);
703
730
  let value: BackgroundProviderSnapshot;
704
731
  try {
@@ -748,6 +775,7 @@ export default function (pi: ExtensionAPI): void {
748
775
  clearIdleContinuation();
749
776
  }
750
777
 
778
+ updateSessionIdle(goal);
751
779
  pi.events.emit(EVENT_ACTIVITY, snapshot);
752
780
  if (execution !== executionGeneration) return null;
753
781
  const attention = terminalAttentionSignature(snapshot);
@@ -855,6 +883,7 @@ export default function (pi: ExtensionAPI): void {
855
883
  providerUnsubscribers.set(provider.id, provider.onActivityChanged(() => {
856
884
  if (!currentCtx || execution !== executionGeneration || providers.get(provider.id) !== provider) return;
857
885
  dirtyProviders.add(provider.id);
886
+ if (sessionIdle === true) setSessionIdle("checking");
858
887
  collectIfPossible();
859
888
  }, currentCtx));
860
889
  }
@@ -997,6 +1026,7 @@ export default function (pi: ExtensionAPI): void {
997
1026
  providerCache.delete(accepted.id);
998
1027
  providerCacheSequences.set(accepted.id, ++snapshotSequence);
999
1028
  dirtyProviders.add(accepted.id);
1029
+ if (sessionIdle === true) setSessionIdle("checking");
1000
1030
  providerBindings.delete(accepted.id);
1001
1031
  bindProviderSubscriptions();
1002
1032
  if (latestSnapshot) collectIfPossible();
@@ -1296,7 +1326,13 @@ export default function (pi: ExtensionAPI): void {
1296
1326
  if (latest?.goalId !== current.goalId || latest.status !== current.status) {
1297
1327
  return { content: [{ type: "text", text: "Goal changed during the completion check. Read the current Goal." }], details: { ok: false, goal: latest } };
1298
1328
  }
1299
- const audit = requestTaskContext(pi, "audit", current);
1329
+ if (gated) {
1330
+ const owner = getWorkflow(ctx);
1331
+ if (owner?.name !== gated.name || owner.path !== gated.path) {
1332
+ return { content: [{ type: "text", text: "Workflow changed during the completion check. Read the current Goal." }], details: { ok: false, goal: latest } };
1333
+ }
1334
+ }
1335
+ const audit = requestTaskContext(pi, { action: "audit", workflowOwnsPlan: !!gated }, current);
1300
1336
  const criteria = audit.doneWhen?.length ? audit.doneWhen : audit.objective ? [audit.objective] : [];
1301
1337
  const evidence = [...input.evidence ?? [], ...audit.evidence ?? []];
1302
1338
  const missing = criteria.filter(criterion => !evidence.some(item => item.criterion === criterion && item.result.trim()));
@@ -1311,7 +1347,7 @@ export default function (pi: ExtensionAPI): void {
1311
1347
  }
1312
1348
  const goal = goalWithStatus(latest, "complete");
1313
1349
  setGoal(goal, ctx, "tool");
1314
- if (getWorkflow(ctx)) recordWorkflow(null);
1350
+ if (gated) recordWorkflow(null);
1315
1351
  const warning = verdict?.allow ? verdict.warning : undefined;
1316
1352
  return {
1317
1353
  content: [{ type: "text", text: warning ? `Goal marked complete. ${warning}` : "Goal marked complete." }],
@@ -1461,6 +1497,29 @@ export default function (pi: ExtensionAPI): void {
1461
1497
  preferences = { ...preferences, autoStartFromOutcome: false };
1462
1498
  notifyGoal(ctx, error instanceof Error ? error.message : String(error), "error");
1463
1499
  }
1500
+ if (typeof stopIdleInspection === "function") stopIdleInspection();
1501
+ idleChannel = undefined;
1502
+ sessionIdle = false;
1503
+ verifyIdleOnSettle = false;
1504
+ latestSnapshot = null;
1505
+ const sessionId = ctx.sessionManager.getSessionId?.();
1506
+ if (ctx.hasUI && ctx.mode === "tui" && sessionId) {
1507
+ idleChannel = sessionIdleEvent(sessionId);
1508
+ stopIdleInspection = pi.events.on(idleChannel + ":inspect", (value) => {
1509
+ const request = value as SessionIdleInspection;
1510
+ request.idle = sessionIdle;
1511
+ // A plain conversation may only have a passive, unverified provider cache.
1512
+ if (!request.verify) return;
1513
+ if (foregroundRunning) {
1514
+ // The Recap settlement handler can run before Goal's handler.
1515
+ verifyIdleOnSettle = true;
1516
+ return;
1517
+ }
1518
+ if (!latestSnapshot || !hasUnverifiedActivity(latestSnapshot)) return;
1519
+ const goal = getGoal(ctx);
1520
+ if (!goal || goal.status === "paused" || goal.status === "complete") void publishSnapshot(ctx, true, "connect");
1521
+ });
1522
+ }
1464
1523
  const restoredGoal = getGoal(ctx);
1465
1524
  if (restoredGoal?.status === "active" && !restoredGoal.command && skillCommandName(restoredGoal.objective)) {
1466
1525
  setGoal(goalWithStatus(restoredGoal, "paused"), ctx, "runtime");
@@ -1510,6 +1569,8 @@ export default function (pi: ExtensionAPI): void {
1510
1569
  backgroundDrainTracker = null;
1511
1570
  continuationQueuedFor = null;
1512
1571
  providerCache.clear();
1572
+ latestSnapshot = null;
1573
+ setSessionIdle(false);
1513
1574
  currentCtx = ctx;
1514
1575
  await publishSnapshot(ctx);
1515
1576
  syncPollingState();
@@ -1634,6 +1695,8 @@ export default function (pi: ExtensionAPI): void {
1634
1695
  agentGeneration = executionGeneration;
1635
1696
  currentCtx = ctx;
1636
1697
  foregroundRunning = true;
1698
+ verifyIdleOnSettle = false;
1699
+ setSessionIdle(false);
1637
1700
  agentLoopActive = true;
1638
1701
  clearIdleContinuation();
1639
1702
  continuationQueuedFor = null;
@@ -1724,7 +1787,9 @@ export default function (pi: ExtensionAPI): void {
1724
1787
  escapeAbortSuppression = undefined;
1725
1788
  currentCtx = ctx;
1726
1789
  foregroundRunning = false;
1727
- const snapshot = await publishSnapshot(ctx);
1790
+ const verify = verifyIdleOnSettle;
1791
+ verifyIdleOnSettle = false;
1792
+ const snapshot = await publishSnapshot(ctx, true, verify ? "connect" : undefined);
1728
1793
  if (!snapshot) return;
1729
1794
  const goal = getGoal(ctx);
1730
1795
  if (!isPokeable(goal) || snapshot.backgroundRunning || hasUnverifiedActivity(snapshot)) {
@@ -1828,6 +1893,9 @@ export default function (pi: ExtensionAPI): void {
1828
1893
  clearIdleContinuation();
1829
1894
  currentCtx = undefined;
1830
1895
  latestSnapshot = null;
1896
+ if (typeof stopIdleInspection === "function") stopIdleInspection();
1897
+ stopIdleInspection = undefined;
1898
+ idleChannel = undefined;
1831
1899
  collectionPending = false;
1832
1900
  try {
1833
1901
  setNavigatorPanel(ctx, EXTENSION_NAME, undefined);
@@ -50,6 +50,8 @@ export interface TaskContextRequest {
50
50
  status?: GoalSnapshot["status"];
51
51
  doneWhen?: readonly string[];
52
52
  openSteps?: readonly string[];
53
+ /** The Goal delegated its checklist to the workflow checked by the completion gate. */
54
+ workflowOwnsPlan?: boolean;
53
55
  /** Evidence retained by a finished workflow bound to this Goal. */
54
56
  evidence?: readonly { criterion: string; result: string }[];
55
57
  ledgerBlocker?: string;
@@ -75,12 +77,12 @@ export interface TaskContextRequest {
75
77
  */
76
78
  export function requestTaskContext(
77
79
  pi: Pick<ExtensionAPI, "events">,
78
- action: TaskContextAction,
80
+ action: TaskContextAction | { action: "audit"; workflowOwnsPlan: boolean },
79
81
  goal?: GoalSnapshot | null,
80
82
  width?: number,
81
83
  manual = false,
82
84
  ): TaskContextRequest {
83
- const request: TaskContextRequest = { action };
85
+ const request: TaskContextRequest = typeof action === "string" ? { action } : { ...action };
84
86
  if (manual) request.manual = true;
85
87
  if (width !== undefined) request.width = width;
86
88
  if (goal) {
package/src/types.ts CHANGED
@@ -9,6 +9,11 @@ export const EVENT_REGISTER_PROVIDER = "pi-better-goal:register-provider";
9
9
  export const EVENT_ACTIVITY = "pi-better-goal:activity";
10
10
  export const EVENT_TERMINAL_ATTENTION = "pi-better-goal:terminal-attention";
11
11
 
12
+ /** Session-local transition and synchronous inspection of verified foreground/worker idleness. */
13
+ export const sessionIdleEvent = (sessionId: string) => `pi-better-goal:idle:${sessionId}`;
14
+ export type SessionIdleState = boolean | "checking";
15
+ export interface SessionIdleInspection { idle?: SessionIdleState; verify?: boolean }
16
+
12
17
  export type ActivityCategory =
13
18
  | "foreground-running"
14
19
  | "background-running"