pi-better-background-tasks 0.14.3 → 0.15.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-background-tasks",
3
- "version": "0.14.3",
3
+ "version": "0.15.0",
4
4
  "description": "Pi extension for durable background shell tasks, watchers, logs, and status inspection.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -44,6 +44,7 @@
44
44
  },
45
45
  "files": [
46
46
  "src/**/*.ts",
47
+ "src/shared-operation-coordinator/assignment-observation.mjs",
47
48
  "src/supervisor/**/*.mjs",
48
49
  "src/supervisor/**/*.js",
49
50
  "!src/**/*.test.ts",
@@ -22,6 +22,12 @@ import { formatBackgroundTaskStall, observeBackgroundTaskStall } from "./stall.j
22
22
  import type { BackgroundTaskCallbackOrigin, BackgroundTaskMeta, BackgroundTaskStatus } from "./types.js";
23
23
 
24
24
  const TERMINAL_NAVIGATOR_RETENTION_MS = 30_000;
25
+ const DURABLE_ROW_CACHE_MS = 1_000;
26
+ let durableRowCache: {
27
+ tasks: readonly DurableTaskProjection[];
28
+ at: number;
29
+ rows: BackgroundWorkRow[];
30
+ } | undefined;
25
31
 
26
32
  export function createBackgroundTasksNavigator(runtime: Pick<BackgroundTaskRuntime, "stopTask"> = { stopTask }) {
27
33
  const owner = Symbol("background-tasks navigator factory");
@@ -88,7 +94,7 @@ const provider: BackgroundWorkProvider = {
88
94
  },
89
95
  listRows: (now) => [
90
96
  ...visibleMetas(now).map((meta) => rowFromMeta(meta, now)),
91
- ...visibleDurableTasks(now).map((task) => rowFromDurable(task, now)),
97
+ ...durableRows(visibleDurableTasks(now), now),
92
98
  ],
93
99
  detail: (id, now, options) => {
94
100
  const durable = durableTask(id);
@@ -123,12 +129,12 @@ const provider: BackgroundWorkProvider = {
123
129
  },
124
130
  onVisibleChanged: (notify) => {
125
131
  const offMeta = onMetaChanged(notify);
126
- const offDurable = getObservedWaitHost(piRef)?.onDurableTaskChange(() => {
127
- invalidateDurableProjection();
132
+ const offDurable = getObservedWaitHost(piRef)?.onDurableTaskChange((change) => {
133
+ const active = activeNavigatorOrigin;
134
+ if (!active || change.workspace !== active.cwd || change.sessionId !== active.sessionId) return;
128
135
  notify();
129
136
  });
130
137
  return () => {
131
- invalidateDurableProjection();
132
138
  offMeta();
133
139
  offDurable?.();
134
140
  };
@@ -139,30 +145,24 @@ function navigatorHost() {
139
145
  return piRef ? getObservedWaitHost(piRef) : undefined;
140
146
  }
141
147
 
142
- /**
143
- * One refresh asks for {@link provider.visibleCount} and then {@link provider.listRows},
144
- * and a detail view asks again. Rebuilding the projection re-reads every durable
145
- * record, so a single paint would pay for the same list up to three times. Hold
146
- * one result per paint window; a durable-change notification clears it.
147
- */
148
- const PROJECTION_MEMO_MS = 250;
149
- let projectionMemo: { at: number; tasks: DurableTaskProjection[] } | undefined;
150
-
151
- function invalidateDurableProjection(): void {
152
- projectionMemo = undefined;
153
- }
154
-
155
148
  function visibleDurableTasks(now = Date.now()): DurableTaskProjection[] {
156
149
  const active = activeNavigatorOrigin;
157
150
  const host = navigatorHost();
158
151
  if (!active || !host) return [];
159
- if (projectionMemo && now - projectionMemo.at < PROJECTION_MEMO_MS) {
160
- return projectionMemo.tasks.filter((task) => !isExpiredDurableRow(task, now));
161
- }
152
+ // ObservedWaitHost caches unchanged projections and invalidates them on every
153
+ // durable notification, so a host redraw no longer re-queries per panel.
162
154
  const listed = host.listDurableTasks(active, { retentionMs: TERMINAL_NAVIGATOR_RETENTION_MS });
163
- const tasks = listed.filter((task) => task.dismissedAt === undefined);
164
- projectionMemo = { at: now, tasks };
165
- return tasks.filter((task) => !isExpiredDurableRow(task, now));
155
+ return listed;
156
+ }
157
+
158
+ function durableRows(tasks: readonly DurableTaskProjection[], now: number): BackgroundWorkRow[] {
159
+ if (durableRowCache && durableRowCache.tasks === tasks
160
+ && now >= durableRowCache.at && now - durableRowCache.at < DURABLE_ROW_CACHE_MS) {
161
+ return durableRowCache.rows;
162
+ }
163
+ const rows = tasks.map((task) => rowFromDurable(task, now));
164
+ durableRowCache = { tasks, at: now, rows };
165
+ return rows;
166
166
  }
167
167
 
168
168
  function durableTask(id: string): DurableTaskProjection | undefined {
@@ -172,11 +172,6 @@ function durableTask(id: string): DurableTaskProjection | undefined {
172
172
  return host.readDurableTask(active, id);
173
173
  }
174
174
 
175
- function isExpiredDurableRow(task: DurableTaskProjection, now: number): boolean {
176
- if (!task.terminal || task.endedAt === undefined) return false;
177
- return now - task.endedAt >= TERMINAL_NAVIGATOR_RETENTION_MS;
178
- }
179
-
180
175
  function rowFromDurable(task: DurableTaskProjection, now: number): BackgroundWorkRow {
181
176
  const command = durableCommandLabel(task);
182
177
  const elapsed = formatDuration((task.endedAt ?? now) - task.startedAt);