@kabxx/dsh-session-move 0.1.1 → 0.1.2

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/src/sidebar.js CHANGED
@@ -1,41 +1,116 @@
1
+ import { inspectMoveHistory } from './history.js'
2
+
1
3
  const original = Symbol.for('cordis.original')
2
4
 
3
- /** Publish ordinary DSH list rows without starting an Agent or attaching a Session. */
4
- export async function refreshSessionSummaries(ctx, members, verified = new Map()) {
5
+ function rowsFor({ lastSeq, summary }, cwd, moved) {
6
+ const row = (val) => ({ ver: 1, seq: lastSeq + Number(moved), val })
7
+ return {
8
+ title: row(summary.title),
9
+ workingDirectory: row(moved ? cwd : summary.workingDirectory),
10
+ agentPreset: row(summary.agentPreset ?? null),
11
+ sessionListMetadata: row(summary.sessionListMetadata),
12
+ }
13
+ }
14
+
15
+ /** Snapshot live resident summaries before teardown so failed moves restore them without rescanning. */
16
+ export function captureLiveSummaries(ctx, writers) {
17
+ const captured = new Map()
18
+ const service = ctx.get('sessionController')
19
+ const list = (service?.[original] ?? service)?.listState
20
+ if (typeof list?.summaryFor !== 'function') return captured
21
+ for (const { session } of writers) {
22
+ try {
23
+ const summary = list.summaryFor(session)
24
+ captured.set(session.id, {
25
+ ...summary,
26
+ agentAvailable: false,
27
+ running: false,
28
+ ...(summary.projections === undefined ? {} : {
29
+ projections: { kind: 'cached', asOfSeq: summary.projections.asOfSeq, values: summary.projections.values },
30
+ }),
31
+ })
32
+ } catch (error) {
33
+ ctx.logger?.warn?.('Session move: could not capture live summary for "%s": %s', session.id, error)
34
+ }
35
+ }
36
+ return captured
37
+ }
38
+
39
+ /** Update cold list rows from the write-locked cut without constructing a full Session. */
40
+ export async function refreshSessionSummaries(ctx, members, cuts, { restored, signal, allowScan = true } = {}) {
5
41
  const service = ctx.get('sessionController')
6
42
  if (!service) return true
7
- // The pinned DSH controller has no public single-row summary operation.
43
+ // These methods are internal to the pinned DSH version. Partial checkpoint
44
+ // rows are valid: other projections still fold from the authoritative log.
8
45
  const list = (service[original] ?? service).listState
9
- if (typeof list?.summaryFor !== 'function') return false
10
- const cache = ctx.get('sessionProjectionCache')
46
+ const injectedCache = ctx.get('sessionProjectionCache')
47
+ const cache = injectedCache?.[original] ?? injectedCache
11
48
  let synced = true
12
49
 
13
- for (const { id, cwd } of members) {
50
+ for (const { id, cwd, moved = false } of members) {
14
51
  try {
15
- let session = ctx.sessions.get(id)
16
- if (session === undefined) {
17
- let cut = verified.get(id)
18
- if (!cut) {
19
- const handle = await ctx.sessionPersistence.open(id, 'read')
20
- try {
21
- cut = { header: handle.header, inheritedEventCount: handle.inheritedEventCount, ...await handle.read() }
22
- } finally { await handle.close() }
52
+ const live = ctx.sessions.get(id)
53
+ if (live !== undefined) {
54
+ if (live.header.cwd !== cwd) throw new Error('Session header changed')
55
+ ctx.emit('api-session/added', list.summaryFor(live))
56
+ continue
57
+ }
58
+ const snapshot = await ctx.sessionPersistence.stat(id)
59
+ const header = snapshot?.header
60
+ if (header?.id !== id || header.cwd !== cwd || snapshot.formatStatus !== 'current'
61
+ || snapshot.location?.kind !== 'jsonl') throw new Error('Session header changed')
62
+ const hasCut = Boolean(cuts?.has(id))
63
+ if (!hasCut && !moved && restored?.has(id)) {
64
+ const appearing = ctx.sessions.get(id)
65
+ if (appearing !== undefined) {
66
+ if (appearing.header.cwd !== cwd) throw new Error('Session header changed')
67
+ ctx.emit('api-session/added', list.summaryFor(appearing))
68
+ continue
23
69
  }
24
- session = ctx.sessions.get(id) ?? ctx.sessions.prepare(id, {
25
- meta: cut.header, inheritedEventCount: cut.inheritedEventCount,
26
- seed: cut.events, eventState: cut.eventState,
27
- })
70
+ ctx.emit('api-session/added', restored.get(id))
71
+ continue
72
+ }
73
+ if (!hasCut && !allowScan) {
74
+ if (typeof list?.summarizeCold !== 'function') throw new Error('Unsupported session summary interface')
75
+ const appearing = ctx.sessions.get(id)
76
+ if (appearing !== undefined) {
77
+ if (appearing.header.cwd !== cwd) throw new Error('Session header changed')
78
+ ctx.emit('api-session/added', list.summaryFor(appearing))
79
+ continue
80
+ }
81
+ ctx.emit('api-session/added', list.summarizeCold(snapshot))
82
+ continue
83
+ }
84
+ const cut = hasCut ? cuts.get(id) : await inspectMoveHistory({ path: snapshot.location.path, id, cwd }, signal ? { signal } : undefined)
85
+ const rows = rowsFor(cut, cwd, moved && hasCut)
86
+ const keys = ctx.sessionProjections.summaryKeys()
87
+ const values = ctx.sessionProjections.viewCheckpoint(rows, keys)
88
+ const appearing = ctx.sessions.get(id)
89
+ if (appearing !== undefined) {
90
+ if (appearing.header.cwd !== cwd) throw new Error('Session header changed')
91
+ ctx.emit('api-session/added', list.summaryFor(appearing))
92
+ continue
28
93
  }
29
- if (session.id !== id || session.header.cwd !== cwd) throw new Error('Session header changed')
30
- if (cache && ctx.sessions.get(id) === undefined) {
31
- // Bind the checkpoint to the new header for freshly connected lists.
32
- try { await cache.write(session) } catch (error) {
94
+ if (cache && typeof cache.put === 'function') {
95
+ try {
96
+ await cache.put(id, { formatVersion: header.version, createdAt: header.createdAt, cwd,
97
+ isSeeded: header.isSeeded, inheritedEventCount: cut.inheritedEventCount }, rows)
98
+ } catch (error) {
33
99
  ctx.logger?.warn?.('Session move: could not checkpoint "%s": %s', id, error)
34
100
  }
35
101
  }
36
- const current = ctx.sessions.get(id) ?? session
37
- if (current.header.cwd !== cwd) throw new Error('Session header changed')
38
- ctx.emit('api-session/added', list.summaryFor(current))
102
+ if (typeof list?.summarizeCold !== 'function') throw new Error('Unsupported session summary interface')
103
+ const summary = list.summarizeCold(snapshot)
104
+ // The stock per-record backend may refuse some child IDs. Their live
105
+ // list event can still carry the same valid projection hints.
106
+ ctx.emit('api-session/added', {
107
+ ...summary,
108
+ updatedAt: Math.max(header.createdAt, cut.summary.sessionListMetadata.lastPromptAt ?? 0),
109
+ blank: cut.summary.sessionListMetadata.blank,
110
+ ...(Object.keys(values).length === 0 ? {} : {
111
+ projections: { kind: 'cached', asOfSeq: rows.title.seq, values },
112
+ }),
113
+ })
39
114
  } catch (error) {
40
115
  synced = false
41
116
  ctx.logger?.warn?.('Session move: could not publish summary for "%s": %s', id, error)