opencode-work-context 0.4.8 → 0.4.9

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/README.md CHANGED
@@ -283,8 +283,11 @@ On Linux notification servers that support actions, `Open session` routes the
283
283
  originating OpenCode process to the affected native session. If that process
284
284
  runs in tmux, its validated `TMUX_PANE` is selected with fixed `tmux` arguments.
285
285
  On X11, the loader records the active window with `xprop` and raises it with
286
- `wmctrl` when those optional tools are available. Each step is independent and
287
- best-effort: navigation can still succeed if pane or window activation fails.
286
+ `wmctrl` when those optional tools are available. Pane/window activation happens
287
+ before session navigation so a hidden TUI can process the route change. Attention
288
+ is acknowledged only after the destination route is verified; failed navigation
289
+ leaves it unread and shows an in-TUI warning. Each activation step is independent
290
+ and best-effort: navigation can still succeed if pane or window activation fails.
288
291
  Wayland focus restrictions and terminal tabs without a public activation API
289
292
  may require the user to foreground the terminal manually. Session and window
290
293
  identifiers never appear in notification text or configurable commands.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "opencode-work-context",
3
3
  "type": "module",
4
- "version": "0.4.8",
4
+ "version": "0.4.9",
5
5
  "description": "Filesystem work-context core and OpenCode adapter",
6
6
  "license": "MIT",
7
7
  "author": {
@@ -2,6 +2,10 @@ import { spawn as nodeSpawn } from "node:child_process";
2
2
 
3
3
  const WINDOW_ID = /^0x[0-9a-f]+$/i;
4
4
  const MAX_OUTPUT = 128;
5
+ const routeSessionId = (api) => api?.route?.current?.name === "session"
6
+ ? api.route.current.params?.sessionID || api.route.current.params?.sessionId || api.route.current.params?.session_id || null
7
+ : null;
8
+ const wait = (delayMs) => new Promise((resolve) => setTimeout(resolve, delayMs));
5
9
 
6
10
  const run = ({ executable, args, spawn, timeoutMs, capture = false, children }) => new Promise((resolve) => {
7
11
  let child;
@@ -69,6 +73,8 @@ export const createAttentionActivator = ({
69
73
  timeoutMs = 1000,
70
74
  captureThrottleMs = 1000,
71
75
  navigationTimeoutMs = 2000,
76
+ routeVerificationMs = 500,
77
+ activationDelayMs = 50,
72
78
  now = () => Date.now(),
73
79
  } = {}) => {
74
80
  const paneId = env.TMUX && /^%\d+$/.test(env.TMUX_PANE || "") ? env.TMUX_PANE : null;
@@ -122,22 +128,40 @@ export const createAttentionActivator = ({
122
128
  }
123
129
  };
124
130
 
131
+ const verifyRoute = async (sessionId) => {
132
+ if (!("current" in (api?.route || {})) || disposed) return false;
133
+ const deadline = Date.now() + routeVerificationMs;
134
+ do {
135
+ if (!disposed && routeSessionId(api) === sessionId) return true;
136
+ if (routeVerificationMs <= 0) break;
137
+ await wait(Math.min(25, Math.max(1, deadline - Date.now())));
138
+ } while (!disposed && Date.now() < deadline);
139
+ return !disposed && routeSessionId(api) === sessionId;
140
+ };
141
+
125
142
  const activate = async (sessionId) => {
126
- if (disposed || typeof sessionId !== "string" || !sessionId || sessionId.length > 256) return false;
127
- let activated = await navigate(sessionId);
128
- if (disposed) return activated;
143
+ if (disposed || typeof sessionId !== "string" || !sessionId || sessionId.length > 256) {
144
+ return { ok: false, activated: false, navigated: false, verified: false };
145
+ }
146
+ let activated = false;
129
147
  if (paneId) {
130
148
  const selectedWindow = await run({ executable: "tmux", args: ["select-window", "-t", paneId], spawn, timeoutMs, children });
131
- if (disposed) return activated;
149
+ if (disposed) return { ok: false, activated, navigated: false, verified: false };
132
150
  const selectedPane = await run({ executable: "tmux", args: ["select-pane", "-t", paneId], spawn, timeoutMs, children });
133
- activated = selectedWindow.ok && selectedPane.ok || activated;
151
+ activated = selectedWindow.ok && selectedPane.ok;
134
152
  }
135
- if (disposed) return activated;
153
+ if (disposed) return { ok: false, activated, navigated: false, verified: false };
136
154
  if (windowId && canCaptureWindow) {
137
155
  const raised = await run({ executable: "wmctrl", args: ["-ia", windowId], spawn, timeoutMs, children });
138
156
  activated = raised.ok || activated;
139
157
  }
140
- return activated;
158
+ if (disposed) return { ok: false, activated, navigated: false, verified: false };
159
+ if (activated && activationDelayMs > 0) await wait(activationDelayMs);
160
+ if (disposed) return { ok: false, activated, navigated: false, verified: false };
161
+ const navigated = await navigate(sessionId);
162
+ if (disposed) return { ok: false, activated, navigated, verified: false };
163
+ const verified = navigated && await verifyRoute(sessionId);
164
+ return { ok: verified, activated, navigated, verified };
141
165
  };
142
166
 
143
167
  return {
@@ -86,6 +86,49 @@ export const acknowledgeNavigation = (api, sessionId, observedEventKey) => {
86
86
  if (!sessionId || !observedEventKey) return false;
87
87
  return api?.workContextAttention?.acknowledge?.(sessionId, observedEventKey) === true;
88
88
  };
89
+ export const createPendingNavigationTracker = () => {
90
+ const counts = new Map();
91
+ let total = 0;
92
+ return {
93
+ set(sessionId, pending) {
94
+ if (!sessionId) return;
95
+ const current = counts.get(sessionId) || 0;
96
+ if (pending) {
97
+ counts.set(sessionId, current + 1);
98
+ total += 1;
99
+ } else if (current > 0) {
100
+ if (current === 1) counts.delete(sessionId);
101
+ else counts.set(sessionId, current - 1);
102
+ total -= 1;
103
+ }
104
+ },
105
+ hasAny: () => total > 0,
106
+ has: (sessionId) => (counts.get(sessionId) || 0) > 0,
107
+ };
108
+ };
109
+ export const openAttentionDestination = async ({ api, activator, controller, sessionId, eventKey, setNavigationPending } = {}) => {
110
+ try { setNavigationPending?.(sessionId, true); } catch {}
111
+ try {
112
+ let result;
113
+ try { result = await activator?.activate?.(sessionId); } catch { result = null; }
114
+ const opened = result?.ok === true && result?.verified === true;
115
+ if (opened) {
116
+ controller?.acknowledge?.(sessionId, eventKey);
117
+ return true;
118
+ }
119
+ try {
120
+ api?.ui?.toast?.({
121
+ title: "Work Context",
122
+ message: "The target session could not be opened. Attention remains unread.",
123
+ variant: "warning",
124
+ duration: 5000,
125
+ });
126
+ } catch {}
127
+ return false;
128
+ } finally {
129
+ try { setNavigationPending?.(sessionId, false); } catch {}
130
+ }
131
+ };
89
132
 
90
133
  const validAttention = (value) => value
91
134
  && typeof value === "object"
@@ -260,7 +303,7 @@ export const desktopNotificationsEnabled = (projectRoot) => {
260
303
  try { return loadConfig(projectRoot).notifications?.desktop !== false; } catch { return false; }
261
304
  };
262
305
 
263
- export const createAttentionTuiController = ({ api, projectRoot, read = readWorkContextSnapshot, onAttention, isFocused = () => true, now } = {}) => {
306
+ export const createAttentionTuiController = ({ api, projectRoot, read = readWorkContextSnapshot, onAttention, isFocused = () => true, shouldAcknowledge = () => true, now } = {}) => {
264
307
  const persisted = readTuiState(projectRoot).attention;
265
308
  const engine = createAttentionEngine({
266
309
  persisted,
@@ -271,6 +314,10 @@ export const createAttentionTuiController = ({ api, projectRoot, read = readWork
271
314
  let snapshot = null;
272
315
  let disposed = false;
273
316
  let pending = Promise.resolve();
317
+ const acknowledgeObserved = (sessionId) => {
318
+ const observedEventKey = engine.attentionFor(sessionId)?.eventKey;
319
+ return observedEventKey ? engine.acknowledge(sessionId, observedEventKey) : false;
320
+ };
274
321
  const refresh = async () => {
275
322
  try {
276
323
  const result = await read({ projectRoot, sessionId: currentSessionId(api) });
@@ -278,7 +325,7 @@ export const createAttentionTuiController = ({ api, projectRoot, read = readWork
278
325
  linked = linkedSessionIds(result);
279
326
  snapshot = result.data;
280
327
  const current = currentSessionId(api);
281
- if (isFocused() !== false && current && linked.has(current)) engine.acknowledge(current);
328
+ if (isFocused() !== false && current && linked.has(current) && shouldAcknowledge(current) !== false) acknowledgeObserved(current);
282
329
  }
283
330
  } catch {}
284
331
  return linked;
@@ -290,7 +337,7 @@ export const createAttentionTuiController = ({ api, projectRoot, read = readWork
290
337
  await refresh();
291
338
  if (!linked.has(sessionId)) return { changed: false, unlinked: true };
292
339
  const foreground = isFocused() !== false ? currentSessionId(api) : undefined;
293
- if (foreground === sessionId) engine.acknowledge(sessionId);
340
+ if (foreground === sessionId && shouldAcknowledge(sessionId) !== false) acknowledgeObserved(sessionId);
294
341
  const result = engine.apply(event, { foreground });
295
342
  if (result.changed) onAttention?.({
296
343
  sessionId,
@@ -306,7 +353,7 @@ export const createAttentionTuiController = ({ api, projectRoot, read = readWork
306
353
  pending = result.catch(() => {});
307
354
  return result;
308
355
  };
309
- const acknowledge = (sessionId, observedEventKey) => engine.acknowledge(sessionId, observedEventKey);
356
+ const acknowledge = (sessionId, observedEventKey) => disposed ? false : engine.acknowledge(sessionId, observedEventKey);
310
357
  const aggregate = (matches) => {
311
358
  const bySession = new Map();
312
359
  const current = currentSessionId(api);
@@ -346,6 +393,8 @@ export const tui = async (api, options = {}) => {
346
393
  const activator = options.attentionActivator
347
394
  || (typeof api.route?.navigate === "function" ? createAttentionActivator({ api, ...options.activatorOptions }) : null);
348
395
  let controller;
396
+ const notificationNavigations = createPendingNavigationTracker();
397
+ const setNavigationPending = (sessionId, pending) => notificationNavigations.set(sessionId, pending);
349
398
  const globalChannel = options.globalAttentionChannel === false
350
399
  ? null
351
400
  : options.globalAttentionChannel || (typeof api.ui?.toast === "function" ? createGlobalAttentionChannel(options.globalAttentionOptions) : null);
@@ -361,7 +410,8 @@ export const tui = async (api, options = {}) => {
361
410
  globalChannel?.activity?.();
362
411
  void activator?.capture?.();
363
412
  const sessionId = currentSessionId(api);
364
- if (sessionId) controller?.acknowledge(sessionId);
413
+ const observedEventKey = sessionId && !notificationNavigations.hasAny() ? controller?.attentionFor(sessionId)?.eventKey : null;
414
+ if (observedEventKey) controller?.acknowledge(sessionId, observedEventKey);
365
415
  },
366
416
  onBlur: () => {
367
417
  globalChannel?.deactivate?.();
@@ -375,6 +425,7 @@ export const tui = async (api, options = {}) => {
375
425
  isFocused: options.isTuiFocused || (globalDelivery
376
426
  ? () => globalChannel.hasActiveInstance() ? globalChannel.isActive() && focus.isFocused() : focus.isFocused()
377
427
  : () => focus.isFocused()),
428
+ shouldAcknowledge: () => !notificationNavigations.hasAny(),
378
429
  onAttention: (change) => {
379
430
  try { options.onAttention?.(change); } catch {}
380
431
  const context = change.contexts?.[0];
@@ -386,7 +437,14 @@ export const tui = async (api, options = {}) => {
386
437
  workspaceTitle: context.workspaceTitle,
387
438
  stageTitle: context.stageTitle,
388
439
  });
389
- const onOpen = activator?.available ? () => activator.activate(change.sessionId) : undefined;
440
+ const onOpen = activator?.available ? () => openAttentionDestination({
441
+ api,
442
+ activator,
443
+ controller,
444
+ sessionId: change.sessionId,
445
+ eventKey: change.attention.eventKey,
446
+ setNavigationPending,
447
+ }) : undefined;
390
448
  void Promise.resolve(desktopNotifier.notify(context, { onOpen })).catch(() => {});
391
449
  },
392
450
  now: options.now,
@@ -396,7 +454,8 @@ export const tui = async (api, options = {}) => {
396
454
  registrations.push(api.event.on("tui.session.select", (event) => {
397
455
  queueMicrotask(() => {
398
456
  const sessionId = currentSessionId(api);
399
- if (sessionId) controller.acknowledge(sessionId);
457
+ const observedEventKey = sessionId && !notificationNavigations.hasAny() ? controller.attentionFor(sessionId)?.eventKey : null;
458
+ if (observedEventKey) controller.acknowledge(sessionId, observedEventKey);
400
459
  });
401
460
  }));
402
461
  let cleaned = false;