dsh-session-lineage 0.0.0-stage → 1.0.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/CHANGELOG.md +57 -0
- package/LICENSE +21 -0
- package/README.md +76 -2
- package/cordis.patch.yml +3 -0
- package/docs/ROADMAP.md +190 -0
- package/docs/images/sidebar-tree.png +0 -0
- package/icon.svg +8 -0
- package/lib/client.js +920 -0
- package/lib/index.js +85 -0
- package/lib/lineage.js +139 -0
- package/lib/state-store.js +165 -0
- package/lib/trail.js +139 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +55 -3
package/lib/index.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `dsh-session-lineage` host half.
|
|
3
|
+
*
|
|
4
|
+
* Phase 1 only: expose the inferred lineage of one workspace (and the small
|
|
5
|
+
* display-state document used by the phase-2 sidebar client) over the Host
|
|
6
|
+
* Connection's authenticated `/api` Fetch routes. No browser code is shipped
|
|
7
|
+
* yet, so this package declares no client half.
|
|
8
|
+
*
|
|
9
|
+
* @module dsh-session-lineage
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { LineageIndex } from './trail.js'
|
|
13
|
+
import { readState, writeState } from './state-store.js'
|
|
14
|
+
|
|
15
|
+
/** Stable Cordis plugin name. */
|
|
16
|
+
export const name = 'session-lineage'
|
|
17
|
+
|
|
18
|
+
/** Host services this plugin cannot work without. */
|
|
19
|
+
export const inject = ['connection', 'workspaceRegistry', 'sessionQuery', 'sessions']
|
|
20
|
+
|
|
21
|
+
const TREE_CHANNEL = '/api/session-lineage'
|
|
22
|
+
const STATE_CHANNEL = '/api/session-lineage.state'
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Write one JSON response without leaking request details.
|
|
26
|
+
*
|
|
27
|
+
* @param value - JSON-serializable payload.
|
|
28
|
+
* @param status - HTTP status.
|
|
29
|
+
* @returns the Fetch `Response`.
|
|
30
|
+
*/
|
|
31
|
+
function json(value, status = 200) {
|
|
32
|
+
return new Response(JSON.stringify(value), {
|
|
33
|
+
status,
|
|
34
|
+
headers: {
|
|
35
|
+
'content-type': 'application/json; charset=utf-8',
|
|
36
|
+
'cache-control': 'no-store',
|
|
37
|
+
},
|
|
38
|
+
})
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Mount the host half.
|
|
43
|
+
*
|
|
44
|
+
* @param ctx - Cordis plugin context with the services listed in {@link inject}.
|
|
45
|
+
*/
|
|
46
|
+
export function apply(ctx) {
|
|
47
|
+
const log = ctx.logger('session-lineage')
|
|
48
|
+
const index = new LineageIndex(ctx, log)
|
|
49
|
+
ctx.effect(() => () => index.dispose(), 'session-lineage: index')
|
|
50
|
+
|
|
51
|
+
// Workspace lineage (R1.7): owner-only, authenticated by the Connection service.
|
|
52
|
+
ctx.connection.fetch.register({
|
|
53
|
+
path: TREE_CHANNEL,
|
|
54
|
+
methods: ['GET'],
|
|
55
|
+
requestBody: 'buffered',
|
|
56
|
+
fetch: async (request) => {
|
|
57
|
+
try {
|
|
58
|
+
const url = new URL(request.url)
|
|
59
|
+
const workspaceId = url.searchParams.get('workspaceId') ?? ''
|
|
60
|
+
const debug = url.searchParams.get('debug') === '1'
|
|
61
|
+
return json(await index.tree(workspaceId, { debug }))
|
|
62
|
+
} catch (error) {
|
|
63
|
+
log.warn(error)
|
|
64
|
+
return json({ workspaceId: null, sessions: [] })
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
// Display state (R1.8): manual sibling order and collapsed subtrees.
|
|
70
|
+
ctx.connection.fetch.register({
|
|
71
|
+
path: STATE_CHANNEL,
|
|
72
|
+
methods: ['GET', 'POST'],
|
|
73
|
+
requestBody: 'buffered',
|
|
74
|
+
fetch: async (request) => {
|
|
75
|
+
if (request.method === 'GET') return json(await readState())
|
|
76
|
+
try {
|
|
77
|
+
const body = await request.json()
|
|
78
|
+
return json(await writeState(body?.state ?? body))
|
|
79
|
+
} catch (error) {
|
|
80
|
+
log.warn(error)
|
|
81
|
+
return json({ ok: false, error: 'could not save lineage state' }, 400)
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
})
|
|
85
|
+
}
|
package/lib/lineage.js
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure lineage inference over inherited event prefixes.
|
|
3
|
+
*
|
|
4
|
+
* DSH forks copy the parent's events verbatim (marked by `inheritedEventCount`
|
|
5
|
+
* and a trailing `session/end-seed` with `data.inherited === true`), so a child
|
|
6
|
+
* can be recognized by comparing its first N events with every candidate's.
|
|
7
|
+
* This module owns the comparison only; reading sessions and caching belongs to
|
|
8
|
+
* `trail.js`.
|
|
9
|
+
*
|
|
10
|
+
* @module dsh-session-lineage/lineage
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/** Upper bound of events kept in a prefix fingerprint. */
|
|
14
|
+
export const HASH_CAP = 8192
|
|
15
|
+
|
|
16
|
+
/** FNV-1a prime, applied per event over the canonical JSON text. */
|
|
17
|
+
const FNV_PRIME = 16777619
|
|
18
|
+
const FNV_OFFSET = 2166136261
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Canonical text of one raw event. Fork seeds keep `seq/time/data` identical,
|
|
22
|
+
* so this projection is stable across the parent and every copy of its prefix.
|
|
23
|
+
*
|
|
24
|
+
* @param event - one session event.
|
|
25
|
+
* @returns the comparable JSON text.
|
|
26
|
+
*/
|
|
27
|
+
export function canonicalEvent(event) {
|
|
28
|
+
return JSON.stringify({
|
|
29
|
+
type: event.type,
|
|
30
|
+
seq: event.seq,
|
|
31
|
+
time: event.time,
|
|
32
|
+
data: event.data,
|
|
33
|
+
})
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Extend one FNV-1a hash with UTF-16 code units.
|
|
38
|
+
*
|
|
39
|
+
* @param hash - previous 32-bit hash.
|
|
40
|
+
* @param text - canonical event text.
|
|
41
|
+
* @returns the next 32-bit hash.
|
|
42
|
+
*/
|
|
43
|
+
function fnv1a(hash, text) {
|
|
44
|
+
let value = hash >>> 0
|
|
45
|
+
for (let index = 0; index < text.length; index += 1) {
|
|
46
|
+
value = Math.imul(value ^ text.charCodeAt(index), FNV_PRIME)
|
|
47
|
+
}
|
|
48
|
+
return value >>> 0
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Cumulative prefix fingerprints for one event log: `hashes[i]` identifies the
|
|
53
|
+
* first `i + 1` events, so two logs share a prefix of length N exactly when
|
|
54
|
+
* their `hashes[N - 1]` values match.
|
|
55
|
+
*
|
|
56
|
+
* @param events - raw event log (any iterable is not needed; it is a readonly array).
|
|
57
|
+
* @param limit - maximum number of events to fingerprint.
|
|
58
|
+
* @returns 32-bit hashes, one per fingerprinted event.
|
|
59
|
+
*/
|
|
60
|
+
export function prefixHashes(events, limit = HASH_CAP) {
|
|
61
|
+
const size = Math.min(events.length, limit)
|
|
62
|
+
const hashes = new Uint32Array(size)
|
|
63
|
+
let hash = FNV_OFFSET
|
|
64
|
+
for (let index = 0; index < size; index += 1) {
|
|
65
|
+
hash = fnv1a(hash, canonicalEvent(events[index]))
|
|
66
|
+
hashes[index] = hash
|
|
67
|
+
}
|
|
68
|
+
return hashes
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Whether one workspace member can be a child in the inference at all: only
|
|
73
|
+
* user forks with an inherited prefix take part, and native subagent lineage is
|
|
74
|
+
* left to DSH's own client.
|
|
75
|
+
*
|
|
76
|
+
* @param entry - cached entry for one session.
|
|
77
|
+
* @returns true when the session's parent is inferred here.
|
|
78
|
+
*/
|
|
79
|
+
function isInferableChild(entry) {
|
|
80
|
+
if (entry.header.isSeeded !== true) return false
|
|
81
|
+
if (entry.header.parentSession !== undefined) return false
|
|
82
|
+
if (entry.header.origin === 'subagent') return false
|
|
83
|
+
const count = entry.n
|
|
84
|
+
return Number.isSafeInteger(count) && count > 0 && count <= entry.hashes.length
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Whether `candidate` could be the parent of `child`: its own history must be
|
|
89
|
+
* earlier (createdAt), shorter (`n < child.n`, which excludes sibling branches
|
|
90
|
+
* that share the same inherited length) and long enough to cover the child's
|
|
91
|
+
* prefix.
|
|
92
|
+
*
|
|
93
|
+
* @param candidate - cached entry for one workspace peer.
|
|
94
|
+
* @param child - cached entry for the session being explained.
|
|
95
|
+
* @returns true when the candidate's prefix can be compared.
|
|
96
|
+
*/
|
|
97
|
+
function isCandidate(candidate, child) {
|
|
98
|
+
if (candidate.header.origin === 'subagent') return false
|
|
99
|
+
if (candidate.header.parentSession !== undefined) return false
|
|
100
|
+
if (!(candidate.header.createdAt < child.header.createdAt)) return false
|
|
101
|
+
if (candidate.n >= child.n) return false
|
|
102
|
+
if (candidate.hashes.length < child.n) return false
|
|
103
|
+
return true
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Infer the parent of every inferable workspace member.
|
|
108
|
+
*
|
|
109
|
+
* Candidates are ranked by their own inherited length (the nearest ancestor
|
|
110
|
+
* that still explains the prefix); an exact tie stays unresolved, so the branch
|
|
111
|
+
* renders as a root instead of guessing.
|
|
112
|
+
*
|
|
113
|
+
* @param ids - workspace session ids in display order.
|
|
114
|
+
* @param entries - cached entries keyed by session id.
|
|
115
|
+
* @returns parent id per session; absent ids render as roots.
|
|
116
|
+
*/
|
|
117
|
+
export function inferParents(ids, entries) {
|
|
118
|
+
const parents = new Map()
|
|
119
|
+
for (const id of ids) {
|
|
120
|
+
const child = entries.get(id)
|
|
121
|
+
if (child === undefined || !isInferableChild(child)) continue
|
|
122
|
+
let best
|
|
123
|
+
let ambiguous = false
|
|
124
|
+
for (const otherId of ids) {
|
|
125
|
+
if (otherId === id) continue
|
|
126
|
+
const candidate = entries.get(otherId)
|
|
127
|
+
if (candidate === undefined || !isCandidate(candidate, child)) continue
|
|
128
|
+
if (candidate.hashes[child.n - 1] !== child.hashes[child.n - 1]) continue
|
|
129
|
+
if (best === undefined || candidate.n > best.n) {
|
|
130
|
+
best = candidate
|
|
131
|
+
ambiguous = false
|
|
132
|
+
} else if (candidate.n === best.n) {
|
|
133
|
+
ambiguous = true
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
if (best !== undefined && !ambiguous) parents.set(id, best.header.id)
|
|
137
|
+
}
|
|
138
|
+
return parents
|
|
139
|
+
}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Durable display state for the sidebar lineage: manual sibling order and
|
|
3
|
+
* collapsed subtrees. One small JSON document under the DSH home; writes are
|
|
4
|
+
* serialized, rotated to `.bak`, and replaced atomically.
|
|
5
|
+
*
|
|
6
|
+
* @module dsh-session-lineage/state-store
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { copyFile, mkdir, readFile, rename, writeFile } from 'node:fs/promises'
|
|
10
|
+
import { homedir } from 'node:os'
|
|
11
|
+
import { dirname, join } from 'node:path'
|
|
12
|
+
|
|
13
|
+
const FORMAT = 'dsh-session-lineage'
|
|
14
|
+
const STATE_VERSION = 1
|
|
15
|
+
const MAX_STATE_BYTES = 256 * 1024
|
|
16
|
+
const MAX_ORDER_ENTRIES = 20000
|
|
17
|
+
|
|
18
|
+
/** Serialized file operations: two tabs must never interleave writes. */
|
|
19
|
+
let writeChain = Promise.resolve()
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @param task - async file operation.
|
|
23
|
+
* @returns the task's own result.
|
|
24
|
+
*/
|
|
25
|
+
function serialize(task) {
|
|
26
|
+
const run = writeChain.then(task, task)
|
|
27
|
+
writeChain = run.catch(() => {})
|
|
28
|
+
return run
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Resolve the DSH home the rest of the harness uses.
|
|
33
|
+
*
|
|
34
|
+
* @returns absolute DSH home directory.
|
|
35
|
+
*/
|
|
36
|
+
function resolveDshHome() {
|
|
37
|
+
const configured = String(process.env.DSH_HOME || '').trim()
|
|
38
|
+
if (configured !== '') {
|
|
39
|
+
if (configured === '~') return homedir()
|
|
40
|
+
if (configured.startsWith('~/') || configured.startsWith('~\\')) {
|
|
41
|
+
return join(homedir(), configured.slice(2))
|
|
42
|
+
}
|
|
43
|
+
return configured
|
|
44
|
+
}
|
|
45
|
+
return join(homedir(), '.dsh')
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** @returns state document path. */
|
|
49
|
+
function stateFile() {
|
|
50
|
+
return join(resolveDshHome(), 'storages', 'session-lineage', 'state.json')
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** @returns rotated previous state document path. */
|
|
54
|
+
function backupFile() {
|
|
55
|
+
return stateFile() + '.bak'
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** @returns a fresh empty state document. */
|
|
59
|
+
export function emptyState() {
|
|
60
|
+
return { version: STATE_VERSION, order: {}, collapsed: [] }
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Validate and normalize a state document. Unknown or malformed parts are
|
|
65
|
+
* dropped instead of failing the whole save: this is display-only data.
|
|
66
|
+
*
|
|
67
|
+
* @param input - candidate document.
|
|
68
|
+
* @returns normalized `{ version, order, collapsed }`.
|
|
69
|
+
*/
|
|
70
|
+
export function normalizeState(input) {
|
|
71
|
+
const source = input !== null && typeof input === 'object' && !Array.isArray(input) ? input : {}
|
|
72
|
+
const order = {}
|
|
73
|
+
let entries = 0
|
|
74
|
+
const rawOrder = source.order
|
|
75
|
+
if (rawOrder !== null && typeof rawOrder === 'object' && !Array.isArray(rawOrder)) {
|
|
76
|
+
for (const [parentId, childIds] of Object.entries(rawOrder)) {
|
|
77
|
+
if (parentId === '' || !Array.isArray(childIds)) continue
|
|
78
|
+
const ids = childIds.filter((id) => typeof id === 'string' && id !== '')
|
|
79
|
+
if (ids.length === 0) continue
|
|
80
|
+
entries += ids.length
|
|
81
|
+
if (entries > MAX_ORDER_ENTRIES) break
|
|
82
|
+
order[parentId] = ids
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
const collapsed = []
|
|
86
|
+
const seen = new Set()
|
|
87
|
+
if (Array.isArray(source.collapsed)) {
|
|
88
|
+
for (const id of source.collapsed) {
|
|
89
|
+
if (typeof id !== 'string' || id === '' || seen.has(id)) continue
|
|
90
|
+
seen.add(id)
|
|
91
|
+
collapsed.push(id)
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return { version: STATE_VERSION, order, collapsed }
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Read one stored document.
|
|
99
|
+
*
|
|
100
|
+
* @param file - document path.
|
|
101
|
+
* @returns normalized state, or `undefined` when absent or unusable.
|
|
102
|
+
*/
|
|
103
|
+
async function readDocument(file) {
|
|
104
|
+
try {
|
|
105
|
+
const raw = await readFile(file, 'utf8')
|
|
106
|
+
if (Buffer.byteLength(raw, 'utf8') > MAX_STATE_BYTES) return undefined
|
|
107
|
+
const parsed = JSON.parse(raw)
|
|
108
|
+
if (parsed === null || typeof parsed !== 'object') return undefined
|
|
109
|
+
if (parsed.format !== FORMAT || parsed.version !== STATE_VERSION) return undefined
|
|
110
|
+
return normalizeState(parsed.state)
|
|
111
|
+
} catch {
|
|
112
|
+
return undefined
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Read the current display state, falling back to the rotated backup and then
|
|
118
|
+
* to an empty document.
|
|
119
|
+
*
|
|
120
|
+
* @returns normalized state document.
|
|
121
|
+
*/
|
|
122
|
+
export function readState() {
|
|
123
|
+
return serialize(async () => {
|
|
124
|
+
const primary = await readDocument(stateFile())
|
|
125
|
+
if (primary !== undefined) return primary
|
|
126
|
+
const backup = await readDocument(backupFile())
|
|
127
|
+
if (backup !== undefined) return backup
|
|
128
|
+
return emptyState()
|
|
129
|
+
})
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Persist a state document (serialized, atomic, `.bak`-rotated).
|
|
134
|
+
*
|
|
135
|
+
* @param input - candidate document.
|
|
136
|
+
* @returns the normalized state that was written.
|
|
137
|
+
*/
|
|
138
|
+
export function writeState(input) {
|
|
139
|
+
return serialize(async () => {
|
|
140
|
+
const state = normalizeState(input)
|
|
141
|
+
const file = stateFile()
|
|
142
|
+
const body = JSON.stringify({
|
|
143
|
+
format: FORMAT,
|
|
144
|
+
version: STATE_VERSION,
|
|
145
|
+
updatedAt: new Date().toISOString(),
|
|
146
|
+
state,
|
|
147
|
+
}, null, 2)
|
|
148
|
+
if (Buffer.byteLength(body, 'utf8') > MAX_STATE_BYTES) {
|
|
149
|
+
throw new Error('session-lineage state is too large to save')
|
|
150
|
+
}
|
|
151
|
+
await mkdir(dirname(file), { recursive: true })
|
|
152
|
+
try {
|
|
153
|
+
const previous = await readFile(file, 'utf8')
|
|
154
|
+
if (Buffer.byteLength(previous, 'utf8') <= MAX_STATE_BYTES) {
|
|
155
|
+
await copyFile(file, backupFile())
|
|
156
|
+
}
|
|
157
|
+
} catch {
|
|
158
|
+
// No readable previous document; the backup is simply not rotated.
|
|
159
|
+
}
|
|
160
|
+
const temporary = `${file}.${process.pid}.${Date.now().toString(36)}.tmp`
|
|
161
|
+
await writeFile(temporary, body, 'utf8')
|
|
162
|
+
await rename(temporary, file)
|
|
163
|
+
return state
|
|
164
|
+
})
|
|
165
|
+
}
|
package/lib/trail.js
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Workspace member reading and prefix-fingerprint cache for the host half.
|
|
3
|
+
*
|
|
4
|
+
* Every session is read once: live sessions through `ctx.sessions`, cold ones
|
|
5
|
+
* through `ctx.sessionQuery.observeSession` (the only supported cold read; it
|
|
6
|
+
* decodes the whole stored log, so the fingerprint cache is what keeps later
|
|
7
|
+
* requests cheap). Prefix content and inheritance length never change after a
|
|
8
|
+
* session is created, so the cache key is the session id alone.
|
|
9
|
+
*
|
|
10
|
+
* @module dsh-session-lineage/trail
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { HASH_CAP, inferParents, prefixHashes } from './lineage.js'
|
|
14
|
+
|
|
15
|
+
/** Cold-session reads per request; keeps the decoder and memory bounded. */
|
|
16
|
+
const READ_CONCURRENCY = 4
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Fingerprint cache and tree source for one workspace request.
|
|
20
|
+
*/
|
|
21
|
+
export class LineageIndex {
|
|
22
|
+
#ctx
|
|
23
|
+
#log
|
|
24
|
+
#entries = new Map()
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @param ctx - plugin context carrying `workspaceRegistry`, `sessions`, `sessionQuery`.
|
|
28
|
+
* @param log - logger used for degradations (never throws at callers).
|
|
29
|
+
*/
|
|
30
|
+
constructor(ctx, log) {
|
|
31
|
+
this.#ctx = ctx
|
|
32
|
+
this.#log = log
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Drop every cached fingerprint. */
|
|
36
|
+
dispose() {
|
|
37
|
+
this.#entries.clear()
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Build the lineage of one workspace.
|
|
42
|
+
*
|
|
43
|
+
* @param workspaceId - DSH workspace id.
|
|
44
|
+
* @param options - `debug` adds per-session inference inputs to the response.
|
|
45
|
+
* @returns `{ workspaceId, generatedAt, sessions }`; unknown workspaces are empty.
|
|
46
|
+
*/
|
|
47
|
+
async tree(workspaceId, { debug = false } = {}) {
|
|
48
|
+
const workspace = this.#ctx.workspaceRegistry.get(workspaceId)
|
|
49
|
+
if (workspace === undefined) {
|
|
50
|
+
return { workspaceId, generatedAt: Date.now(), sessions: [] }
|
|
51
|
+
}
|
|
52
|
+
const ids = [...new Set(workspace.sessionIds)]
|
|
53
|
+
for (let offset = 0; offset < ids.length; offset += READ_CONCURRENCY) {
|
|
54
|
+
await Promise.all(ids.slice(offset, offset + READ_CONCURRENCY).map(async (id) => {
|
|
55
|
+
try {
|
|
56
|
+
await this.#ensure(id)
|
|
57
|
+
} catch (error) {
|
|
58
|
+
this.#log.warn(`session ${id}: lineage read failed: ${String(error)}`)
|
|
59
|
+
}
|
|
60
|
+
}))
|
|
61
|
+
}
|
|
62
|
+
const entries = new Map()
|
|
63
|
+
for (const id of ids) {
|
|
64
|
+
const entry = this.#entries.get(id)
|
|
65
|
+
if (entry !== undefined) entries.set(id, entry)
|
|
66
|
+
}
|
|
67
|
+
const parents = inferParents(ids, entries)
|
|
68
|
+
const sessions = []
|
|
69
|
+
for (const id of ids) {
|
|
70
|
+
const entry = entries.get(id)
|
|
71
|
+
if (entry === undefined) continue
|
|
72
|
+
const item = { id, parentId: parents.get(id) ?? null, createdAt: entry.header.createdAt }
|
|
73
|
+
if (debug) {
|
|
74
|
+
item.n = entry.n
|
|
75
|
+
item.eventCount = entry.eventCount
|
|
76
|
+
item.isSeeded = entry.header.isSeeded === true
|
|
77
|
+
item.origin = entry.header.origin ?? null
|
|
78
|
+
}
|
|
79
|
+
sessions.push(item)
|
|
80
|
+
}
|
|
81
|
+
return { workspaceId, generatedAt: Date.now(), sessions }
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Ensure one session entry exists and is current.
|
|
86
|
+
*
|
|
87
|
+
* @param id - session id.
|
|
88
|
+
* @returns the cached entry (also stored on the index).
|
|
89
|
+
*/
|
|
90
|
+
async #ensure(id) {
|
|
91
|
+
const live = this.#ctx.sessions.get(id)
|
|
92
|
+
const cached = this.#entries.get(id)
|
|
93
|
+
if (live !== undefined) {
|
|
94
|
+
const count = Number(live.seq)
|
|
95
|
+
if (cached !== undefined && cached.eventCount === count) return cached
|
|
96
|
+
// Prefix content is immutable after a session is created: when only the
|
|
97
|
+
// event count moved on, keep the cached fingerprint instead of hashing
|
|
98
|
+
// the (possibly long) log again on every read.
|
|
99
|
+
if (
|
|
100
|
+
cached !== undefined
|
|
101
|
+
&& cached.header.isSeeded === live.header.isSeeded
|
|
102
|
+
&& cached.header.createdAt === live.header.createdAt
|
|
103
|
+
&& cached.n === Number(live.inheritedEventCount)
|
|
104
|
+
) {
|
|
105
|
+
cached.eventCount = count
|
|
106
|
+
return cached
|
|
107
|
+
}
|
|
108
|
+
const entry = this.#build(live.header, live.inheritedEventCount, live.snapshotEvents())
|
|
109
|
+
this.#entries.set(id, entry)
|
|
110
|
+
return entry
|
|
111
|
+
}
|
|
112
|
+
if (cached !== undefined) return cached
|
|
113
|
+
const observation = await this.#ctx.sessionQuery.observeSession(id, { projectionMode: 'none' })
|
|
114
|
+
try {
|
|
115
|
+
const entry = this.#build(observation.header, observation.inheritedEventCount, observation.events)
|
|
116
|
+
this.#entries.set(id, entry)
|
|
117
|
+
return entry
|
|
118
|
+
} finally {
|
|
119
|
+
observation[Symbol.dispose]?.()
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Fingerprint one loaded log and keep only what inference needs.
|
|
125
|
+
*
|
|
126
|
+
* @param header - session header from `sessions` or `sessionQuery`.
|
|
127
|
+
* @param inheritedEventCount - inherited prefix length N.
|
|
128
|
+
* @param events - raw event log.
|
|
129
|
+
* @returns cache entry.
|
|
130
|
+
*/
|
|
131
|
+
#build(header, inheritedEventCount, events) {
|
|
132
|
+
const n = Number(inheritedEventCount)
|
|
133
|
+
const hashes = prefixHashes(events, HASH_CAP)
|
|
134
|
+
if (header.isSeeded === true && n > 0 && events[n]?.type !== 'session/end-seed') {
|
|
135
|
+
this.#log.warn(`session ${header.id}: inherited marker missing at ${n}`)
|
|
136
|
+
}
|
|
137
|
+
return { header, n, eventCount: events.length, hashes }
|
|
138
|
+
}
|
|
139
|
+
}
|
package/locale/en.json
ADDED
package/locale/zh.json
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,58 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-session-lineage",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "1.0.2",
|
|
4
|
+
"description": "DSH sidebar session lineage: indent forked sessions under their parent, with collapsible branches and sibling ordering.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "lib/index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./lib/index.js",
|
|
9
|
+
"./client": "./lib/client.js",
|
|
10
|
+
"./locale/*.json": "./locale/*.json",
|
|
11
|
+
"./icon": "./icon.svg",
|
|
12
|
+
"./package.json": "./package.json"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"lib",
|
|
16
|
+
"locale",
|
|
17
|
+
"cordis.patch.yml",
|
|
18
|
+
"package.json",
|
|
19
|
+
"README.md",
|
|
20
|
+
"CHANGELOG.md",
|
|
21
|
+
"LICENSE",
|
|
22
|
+
"icon.svg",
|
|
23
|
+
"docs"
|
|
24
|
+
],
|
|
25
|
+
"keywords": [
|
|
26
|
+
"dsh-plugin",
|
|
27
|
+
"dsh-bundle",
|
|
28
|
+
"deepseek-harness",
|
|
29
|
+
"dsh",
|
|
30
|
+
"session-lineage",
|
|
31
|
+
"session-tree",
|
|
32
|
+
"fork",
|
|
33
|
+
"sidebar",
|
|
34
|
+
"lineage"
|
|
35
|
+
],
|
|
36
|
+
"author": "yangt16",
|
|
37
|
+
"license": "MIT",
|
|
38
|
+
"repository": {
|
|
39
|
+
"type": "git",
|
|
40
|
+
"url": "git+https://github.com/yangt16/dsh-session-lineage.git"
|
|
41
|
+
},
|
|
42
|
+
"homepage": "https://github.com/yangt16/dsh-session-lineage#readme",
|
|
43
|
+
"bugs": {
|
|
44
|
+
"url": "https://github.com/yangt16/dsh-session-lineage/issues"
|
|
45
|
+
},
|
|
46
|
+
"dsh": {
|
|
47
|
+
"engines": {
|
|
48
|
+
"dsh": ">=0.2.2-rc.1 <0.3.0"
|
|
49
|
+
},
|
|
50
|
+
"bundle": {
|
|
51
|
+
"patch": "./cordis.patch.yml"
|
|
52
|
+
},
|
|
53
|
+
"client": {
|
|
54
|
+
"platform": "web",
|
|
55
|
+
"immediately": true
|
|
56
|
+
}
|
|
57
|
+
}
|
|
6
58
|
}
|