cc-viewer 1.9.33 → 1.9.35

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/server/server.js CHANGED
@@ -677,6 +677,17 @@ function startLogWatch() {
677
677
  } catch (err) { reportSwallowed('server.sid-pin', err); }
678
678
  })
679
679
  .catch((err) => { reportSwallowed('server.sid-pin-import', err); });
680
+ // SDK forceNew (2026-11): this main-turn gate is the exact moment the instance owns its
681
+ // first session dir. Lift the freshLaunch empty-view suppression (PTY parity with
682
+ // setPtySessionIdForInstance clearing the PTY record's mark). SDK sessions are not in
683
+ // pty-manager, so their clear path is separate; a no-op for PTY / non-fresh instances.
684
+ // Gated on isSdkMode: importing sdk-manager eagerly loads the Agent SDK (hard dep), which
685
+ // must NOT happen in a PTY-only install (sdk-session-index.js's stated rule).
686
+ if (isSdkMode && instanceKey) {
687
+ import('./lib/sdk-manager.js')
688
+ .then((sdk) => { try { sdk.clearSdkFreshLaunch?.(instanceKey); } catch (err) { reportSwallowed('server.sdk-fresh-clear', err); } })
689
+ .catch((err) => { reportSwallowed('server.sdk-fresh-clear-import', err); });
690
+ }
680
691
  });
681
692
  }
682
693
 
@@ -2026,7 +2037,10 @@ async function setupTerminalWebSocket(httpServer) {
2026
2037
  // unstamped (receiving all workspaces' frames).
2027
2038
  wss.handleUpgrade(req, socket, head, (ws) => {
2028
2039
  try { ws._ccvViewProject = viewProject || null; } catch {}
2029
- try { ws._ccvViewInstance = viewInstance || null; } catch {}
2040
+ // Twin-window coherence applies at the initial upgrade too, not just on re-attach:
2041
+ // connect precedes the first attach, so without it the null stamp (and the cross-twin
2042
+ // frame delivery) persists for the whole pre-poll window.
2043
+ try { ws._ccvViewInstance = _cohereAttachInstance(viewProject || null, viewInstance); } catch {}
2030
2044
  wss.emit('connection', ws, req);
2031
2045
  });
2032
2046
  });
@@ -2628,7 +2642,7 @@ async function setupTerminalWebSocket(httpServer) {
2628
2642
  // 新视图的 sdk-session-state/审批/队列帧会被静默丢弃。
2629
2643
  if (msg.type === 'attach') {
2630
2644
  try { ws._ccvViewProject = (typeof msg.project === 'string' && msg.project) || null; } catch {}
2631
- try { ws._ccvViewInstance = (typeof msg.instanceKey === 'string' && msg.instanceKey) || null; } catch {}
2645
+ try { ws._ccvViewInstance = _cohereAttachInstance(ws._ccvViewProject, msg.instanceKey); } catch {}
2632
2646
  if (isSdkMode) {
2633
2647
  // Re-attach switched the viewed workspace — re-send that view's sdk-skills
2634
2648
  // surface so the `/` menu isn't stuck on the /api/skills fallback until the next
@@ -2800,7 +2814,7 @@ async function setupTerminalWebSocket(httpServer) {
2800
2814
  try { res = (target || targetInstance) ? attachPtyFor({ project: target || undefined, instanceKey: targetInstance || undefined }) : null; } catch { res = null; }
2801
2815
  if (res && res.ok) {
2802
2816
  ws._ccvViewProject = target || null;
2803
- ws._ccvViewInstance = targetInstance || null;
2817
+ ws._ccvViewInstance = _cohereAttachInstance(ws._ccvViewProject, targetInstance);
2804
2818
  replayQueueSnapshot();
2805
2819
  try { ws.send(JSON.stringify({ type: 'attached', project: target, running: res.running, ptyKind: res.ptyKind, exitCode: res.exitCode })); } catch { }
2806
2820
  const snap = getOutputBuffer();
@@ -2811,7 +2825,7 @@ async function setupTerminalWebSocket(httpServer) {
2811
2825
  // The queue belongs to the view, not to a live PTY. A history view or
2812
2826
  // a dead instance still needs its own queue snapshot after re-stamping.
2813
2827
  ws._ccvViewProject = target || null;
2814
- ws._ccvViewInstance = targetInstance || null;
2828
+ ws._ccvViewInstance = _cohereAttachInstance(ws._ccvViewProject, targetInstance);
2815
2829
  replayQueueSnapshot();
2816
2830
  // Multi-instance: surface an ambiguous project-name attach (with the live
2817
2831
  // candidates) so the client re-issues with an instanceKey instead of silently
@@ -4235,6 +4249,39 @@ export function setSdkGetSessionStateSnapshot(fn) { _sdkGetSessionStateSnapshot
4235
4249
  let _sdkGetSdkTaskSnapshot = null;
4236
4250
  export function setSdkGetSdkTaskSnapshot(fn) { _sdkGetSdkTaskSnapshot = fn; }
4237
4251
 
4252
+ /** Reference to sdk-manager's resolveBoundPrimaryInstanceKey (set by cli.js after import). */
4253
+ let _sdkResolveBoundPrimaryInstanceKey = null;
4254
+ export function setSdkResolveBoundPrimaryInstanceKey(fn) { _sdkResolveBoundPrimaryInstanceKey = fn; }
4255
+
4256
+ /**
4257
+ * Cohere a WS attach's `_ccvViewInstance` stamp for the same-cwd twin window (2026-11).
4258
+ * A bound-project client whose `boundInstance` hasn't hydrated yet attaches with NO
4259
+ * instanceKey, so the stamp would be null and broadcastWsMessage's instance narrowing
4260
+ * ("exclude only the differently-stamped") would deliver EVERY same-cwd twin's frames to
4261
+ * it. The bound identity is a deterministic function of the cwd, so resolve it HERE at
4262
+ * attach time instead of waiting for the client's first /api/live-processes poll.
4263
+ * Only ever NARROWS (null → the bound primary): an explicit instanceKey is returned
4264
+ * untouched, a non-bound project is left alone (parallel views keep their own anchor), and
4265
+ * a single-instance cwd resolves to its only session (narrowing to it is behaviorally
4266
+ * identical to project-level for that cwd — no twin frames exist to drop). PTY mode is
4267
+ * untouched (isSdkMode gate). Resolution failure / no live session → null (unchanged).
4268
+ */
4269
+ function _cohereAttachInstance(viewProject, sentInstanceKey) {
4270
+ const sent = (typeof sentInstanceKey === 'string' && sentInstanceKey) ? sentInstanceKey : null;
4271
+ if (sent) return sent;
4272
+ if (!isSdkMode || typeof _sdkResolveBoundPrimaryInstanceKey !== 'function') return null;
4273
+ if (!viewProject || viewProject !== (_projectName || '')) return null;
4274
+ try {
4275
+ const boundCwd = process.env.CCV_PROJECT_DIR || process.cwd();
4276
+ return _sdkResolveBoundPrimaryInstanceKey(boundCwd) || null;
4277
+ } catch (err) {
4278
+ reportSwallowed('server.attach-cohere-instance', err);
4279
+ return null;
4280
+ }
4281
+ }
4282
+ // Test seam: exercise _cohereAttachInstance's resolution logic without a live socket.
4283
+ export { _cohereAttachInstance };
4284
+
4238
4285
  /** Reference to sdk-manager's sendQueuedNow (set by cli.js after import). */
4239
4286
  let _sdkSendQueuedNow = null;
4240
4287
  export function setSdkSendQueuedNow(fn) { _sdkSendQueuedNow = fn; }