pi-better-goal 0.17.1 → 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 +1 -1
- package/src/index.ts +63 -1
- package/src/types.ts +5 -0
package/package.json
CHANGED
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();
|
|
@@ -1467,6 +1497,29 @@ export default function (pi: ExtensionAPI): void {
|
|
|
1467
1497
|
preferences = { ...preferences, autoStartFromOutcome: false };
|
|
1468
1498
|
notifyGoal(ctx, error instanceof Error ? error.message : String(error), "error");
|
|
1469
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
|
+
}
|
|
1470
1523
|
const restoredGoal = getGoal(ctx);
|
|
1471
1524
|
if (restoredGoal?.status === "active" && !restoredGoal.command && skillCommandName(restoredGoal.objective)) {
|
|
1472
1525
|
setGoal(goalWithStatus(restoredGoal, "paused"), ctx, "runtime");
|
|
@@ -1516,6 +1569,8 @@ export default function (pi: ExtensionAPI): void {
|
|
|
1516
1569
|
backgroundDrainTracker = null;
|
|
1517
1570
|
continuationQueuedFor = null;
|
|
1518
1571
|
providerCache.clear();
|
|
1572
|
+
latestSnapshot = null;
|
|
1573
|
+
setSessionIdle(false);
|
|
1519
1574
|
currentCtx = ctx;
|
|
1520
1575
|
await publishSnapshot(ctx);
|
|
1521
1576
|
syncPollingState();
|
|
@@ -1640,6 +1695,8 @@ export default function (pi: ExtensionAPI): void {
|
|
|
1640
1695
|
agentGeneration = executionGeneration;
|
|
1641
1696
|
currentCtx = ctx;
|
|
1642
1697
|
foregroundRunning = true;
|
|
1698
|
+
verifyIdleOnSettle = false;
|
|
1699
|
+
setSessionIdle(false);
|
|
1643
1700
|
agentLoopActive = true;
|
|
1644
1701
|
clearIdleContinuation();
|
|
1645
1702
|
continuationQueuedFor = null;
|
|
@@ -1730,7 +1787,9 @@ export default function (pi: ExtensionAPI): void {
|
|
|
1730
1787
|
escapeAbortSuppression = undefined;
|
|
1731
1788
|
currentCtx = ctx;
|
|
1732
1789
|
foregroundRunning = false;
|
|
1733
|
-
const
|
|
1790
|
+
const verify = verifyIdleOnSettle;
|
|
1791
|
+
verifyIdleOnSettle = false;
|
|
1792
|
+
const snapshot = await publishSnapshot(ctx, true, verify ? "connect" : undefined);
|
|
1734
1793
|
if (!snapshot) return;
|
|
1735
1794
|
const goal = getGoal(ctx);
|
|
1736
1795
|
if (!isPokeable(goal) || snapshot.backgroundRunning || hasUnverifiedActivity(snapshot)) {
|
|
@@ -1834,6 +1893,9 @@ export default function (pi: ExtensionAPI): void {
|
|
|
1834
1893
|
clearIdleContinuation();
|
|
1835
1894
|
currentCtx = undefined;
|
|
1836
1895
|
latestSnapshot = null;
|
|
1896
|
+
if (typeof stopIdleInspection === "function") stopIdleInspection();
|
|
1897
|
+
stopIdleInspection = undefined;
|
|
1898
|
+
idleChannel = undefined;
|
|
1837
1899
|
collectionPending = false;
|
|
1838
1900
|
try {
|
|
1839
1901
|
setNavigatorPanel(ctx, EXTENSION_NAME, undefined);
|
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"
|