dsh-session-branch 0.0.0-stage → 1.3.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/CHANGELOG.md +24 -0
- package/LICENSE +22 -0
- package/README.en.md +46 -0
- package/README.md +60 -2
- package/cordis.patch.yml +3 -0
- package/docs/DETAIL.md +1 -0
- package/docs/DETAIL.zh.md +1 -0
- package/docs/preview.en.svg +50 -0
- package/docs/preview.zh.svg +50 -0
- package/icon.svg +8 -0
- package/lib/client.js +2082 -0
- package/lib/fingerprint-store.js +162 -0
- package/lib/index.js +86 -0
- package/lib/lineage.js +246 -0
- package/lib/state-store.js +194 -0
- package/lib/trail.js +296 -0
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +56 -4
package/lib/trail.js
ADDED
|
@@ -0,0 +1,296 @@
|
|
|
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. Fingerprints are
|
|
9
|
+
* also persisted to `$DSH_HOME/storages/session-lineage/fingerprints.json` so a
|
|
10
|
+
* DSH restart does not have to decode every cold log again.
|
|
11
|
+
*
|
|
12
|
+
* @module dsh-session-lineage/trail
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { readFingerprints, writeFingerprints } from './fingerprint-store.js'
|
|
16
|
+
import { HASH_CAP, extendPrefixHashes, inferParents, prefixHashes } from './lineage.js'
|
|
17
|
+
|
|
18
|
+
/** Cold-session reads per request; keeps the decoder and memory bounded. */
|
|
19
|
+
const READ_CONCURRENCY = 4
|
|
20
|
+
|
|
21
|
+
/** Sessions re-read in one request when a cached fingerprint turns out stale. */
|
|
22
|
+
const STALE_REFRESH_LIMIT = 8
|
|
23
|
+
|
|
24
|
+
/** Virtual workspace key for DSH's ungrouped (loose) session bucket. */
|
|
25
|
+
export const UNGROUPED_WORKSPACE_ID = 'ungrouped'
|
|
26
|
+
|
|
27
|
+
/** Loose session ids one ungrouped request may carry. */
|
|
28
|
+
const MAX_UNGROUPED_MEMBERS = 200
|
|
29
|
+
|
|
30
|
+
/** Debounce before the persisted fingerprint cache is rewritten. */
|
|
31
|
+
const PERSIST_DEBOUNCE_MS = 2000
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Fingerprint cache and tree source for one workspace request.
|
|
35
|
+
*/
|
|
36
|
+
export class LineageIndex {
|
|
37
|
+
#ctx
|
|
38
|
+
#log
|
|
39
|
+
#entries = new Map()
|
|
40
|
+
#fromDisk = new Set()
|
|
41
|
+
#loadedFromDisk = false
|
|
42
|
+
#dirty = false
|
|
43
|
+
#persistTimer = 0
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* @param ctx - plugin context carrying `workspaceRegistry`, `sessions`, `sessionQuery`.
|
|
47
|
+
* @param log - logger used for degradations (never throws at callers).
|
|
48
|
+
*/
|
|
49
|
+
constructor(ctx, log) {
|
|
50
|
+
this.#ctx = ctx
|
|
51
|
+
this.#log = log
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Drop every cached fingerprint and flush a pending cache write. */
|
|
55
|
+
dispose() {
|
|
56
|
+
if (this.#persistTimer !== 0) {
|
|
57
|
+
clearTimeout(this.#persistTimer)
|
|
58
|
+
this.#persistTimer = 0
|
|
59
|
+
}
|
|
60
|
+
if (this.#dirty) {
|
|
61
|
+
this.#dirty = false
|
|
62
|
+
void writeFingerprints(this.#entries)
|
|
63
|
+
}
|
|
64
|
+
this.#entries.clear()
|
|
65
|
+
this.#fromDisk.clear()
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Session ids of one tree source: a workspace's members, or the loose
|
|
70
|
+
* sessions the sidebar's ungrouped bucket currently renders.
|
|
71
|
+
*
|
|
72
|
+
* @param workspaceId - DSH workspace id or {@link UNGROUPED_WORKSPACE_ID}.
|
|
73
|
+
* @param requested - loose session ids supplied with the request.
|
|
74
|
+
* @returns member ids, or null for an unknown workspace.
|
|
75
|
+
*/
|
|
76
|
+
#members(workspaceId, requested) {
|
|
77
|
+
if (workspaceId === UNGROUPED_WORKSPACE_ID) {
|
|
78
|
+
const unique = [...new Set(Array.isArray(requested) ? requested : [])]
|
|
79
|
+
return unique
|
|
80
|
+
.filter((id) => typeof id === 'string' && id !== '')
|
|
81
|
+
.slice(0, MAX_UNGROUPED_MEMBERS)
|
|
82
|
+
}
|
|
83
|
+
const workspace = this.#ctx.workspaceRegistry.get(workspaceId)
|
|
84
|
+
return workspace === undefined ? null : [...new Set(workspace.sessionIds)]
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Build the lineage of one workspace, or of the ungrouped bucket when
|
|
89
|
+
* `workspaceId` is {@link UNGROUPED_WORKSPACE_ID} (its members are the loose
|
|
90
|
+
* session ids the caller renders).
|
|
91
|
+
*
|
|
92
|
+
* @param workspaceId - DSH workspace id or {@link UNGROUPED_WORKSPACE_ID}.
|
|
93
|
+
* @param options - `debug` adds per-session inference inputs to the response;
|
|
94
|
+
* `ids` selects the loose sessions of the ungrouped bucket.
|
|
95
|
+
* @returns `{ workspaceId, generatedAt, sessions }`; unknown workspaces are empty.
|
|
96
|
+
*/
|
|
97
|
+
async tree(workspaceId, { debug = false, ids: requested } = {}) {
|
|
98
|
+
const ids = this.#members(workspaceId, requested)
|
|
99
|
+
if (ids === null) {
|
|
100
|
+
return { workspaceId, generatedAt: Date.now(), sessions: [] }
|
|
101
|
+
}
|
|
102
|
+
await this.#loadPersisted()
|
|
103
|
+
for (let offset = 0; offset < ids.length; offset += READ_CONCURRENCY) {
|
|
104
|
+
await Promise.all(ids.slice(offset, offset + READ_CONCURRENCY).map(async (id) => {
|
|
105
|
+
try {
|
|
106
|
+
await this.#ensure(id)
|
|
107
|
+
} catch (error) {
|
|
108
|
+
this.#log.warn(`session ${id}: lineage read failed: ${String(error)}`)
|
|
109
|
+
}
|
|
110
|
+
}))
|
|
111
|
+
}
|
|
112
|
+
const entries = new Map()
|
|
113
|
+
for (const id of ids) {
|
|
114
|
+
const entry = this.#entries.get(id)
|
|
115
|
+
if (entry !== undefined) entries.set(id, entry)
|
|
116
|
+
}
|
|
117
|
+
await this.#refreshStale(entries)
|
|
118
|
+
// Native subagent sessions are rendered (and hidden) by DSH itself; they are
|
|
119
|
+
// not sidebar rows, so they must not take a slot or a display number.
|
|
120
|
+
const visible = ids.filter((id) => {
|
|
121
|
+
const entry = entries.get(id)
|
|
122
|
+
return entry !== undefined && entry.header.origin !== 'subagent'
|
|
123
|
+
})
|
|
124
|
+
const parents = inferParents(visible, entries)
|
|
125
|
+
const sessions = []
|
|
126
|
+
for (const id of visible) {
|
|
127
|
+
const entry = entries.get(id)
|
|
128
|
+
if (entry === undefined) continue
|
|
129
|
+
const item = { id, parentId: parents.get(id) ?? null, createdAt: entry.header.createdAt }
|
|
130
|
+
if (debug) {
|
|
131
|
+
item.n = entry.n
|
|
132
|
+
item.eventCount = entry.eventCount
|
|
133
|
+
item.isSeeded = entry.header.isSeeded === true
|
|
134
|
+
item.origin = entry.header.origin ?? null
|
|
135
|
+
}
|
|
136
|
+
sessions.push(item)
|
|
137
|
+
}
|
|
138
|
+
this.#schedulePersist()
|
|
139
|
+
return { workspaceId, generatedAt: Date.now(), sessions }
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Read the persisted fingerprint cache once, keeping whatever is not already
|
|
144
|
+
* in memory from this process.
|
|
145
|
+
*/
|
|
146
|
+
async #loadPersisted() {
|
|
147
|
+
if (this.#loadedFromDisk) return
|
|
148
|
+
this.#loadedFromDisk = true
|
|
149
|
+
const cached = await readFingerprints()
|
|
150
|
+
for (const [id, entry] of cached) {
|
|
151
|
+
if (this.#entries.has(id)) continue
|
|
152
|
+
this.#entries.set(id, entry)
|
|
153
|
+
this.#fromDisk.add(id)
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* A cached fingerprint only records the session as it was when it was cached.
|
|
159
|
+
* A fork created later copies more events than that, so any cached candidate
|
|
160
|
+
* that could cover a child's longer prefix is re-read once.
|
|
161
|
+
*
|
|
162
|
+
* @param entries - entries collected for this request (updated in place).
|
|
163
|
+
*/
|
|
164
|
+
async #refreshStale(entries) {
|
|
165
|
+
let maxChildN = 0
|
|
166
|
+
for (const entry of entries.values()) {
|
|
167
|
+
if (entry.header.origin === 'subagent') continue
|
|
168
|
+
if (entry.header.isSeeded !== true) continue
|
|
169
|
+
if (!Number.isSafeInteger(entry.n) || entry.n <= 0) continue
|
|
170
|
+
if (entry.n > maxChildN) maxChildN = entry.n
|
|
171
|
+
}
|
|
172
|
+
if (maxChildN <= 0) return
|
|
173
|
+
const stale = []
|
|
174
|
+
for (const [id, entry] of entries) {
|
|
175
|
+
if (!this.#fromDisk.has(id)) continue
|
|
176
|
+
if (entry.header.origin === 'subagent') continue
|
|
177
|
+
// What a fork needs is coverage of its cut point, not a large event count
|
|
178
|
+
// on disk: a truncated fingerprint (older live read) must be rebuilt even
|
|
179
|
+
// when the cached event count already reaches the child.
|
|
180
|
+
if (entry.hashes.length >= maxChildN) continue
|
|
181
|
+
stale.push(id)
|
|
182
|
+
if (stale.length >= STALE_REFRESH_LIMIT) break
|
|
183
|
+
}
|
|
184
|
+
for (const id of stale) {
|
|
185
|
+
try {
|
|
186
|
+
const entry = await this.#reload(id)
|
|
187
|
+
if (entry !== undefined) entries.set(id, entry)
|
|
188
|
+
} catch (error) {
|
|
189
|
+
this.#log.warn(`session ${id}: fingerprint refresh failed: ${String(error)}`)
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Re-read one session from its live state or stored log.
|
|
196
|
+
*
|
|
197
|
+
* @param id - session id.
|
|
198
|
+
* @returns the fresh entry.
|
|
199
|
+
*/
|
|
200
|
+
async #reload(id) {
|
|
201
|
+
this.#fromDisk.delete(id)
|
|
202
|
+
this.#entries.delete(id)
|
|
203
|
+
const entry = await this.#ensure(id)
|
|
204
|
+
return entry
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Ensure one session entry exists and is current.
|
|
209
|
+
*
|
|
210
|
+
* @param id - session id.
|
|
211
|
+
* @returns the cached entry (also stored on the index).
|
|
212
|
+
*/
|
|
213
|
+
async #ensure(id) {
|
|
214
|
+
const live = this.#ctx.sessions.get(id)
|
|
215
|
+
const cached = this.#entries.get(id)
|
|
216
|
+
if (live !== undefined) {
|
|
217
|
+
const count = Number(live.seq)
|
|
218
|
+
if (
|
|
219
|
+
cached !== undefined
|
|
220
|
+
&& cached.header.isSeeded === live.header.isSeeded
|
|
221
|
+
&& cached.header.createdAt === live.header.createdAt
|
|
222
|
+
&& cached.n === Number(live.inheritedEventCount)
|
|
223
|
+
) {
|
|
224
|
+
// The log keeps growing and a fork may be cut anywhere in it, so the
|
|
225
|
+
// fingerprint must cover every event (up to the cap), not only the
|
|
226
|
+
// prefix it was first built with.
|
|
227
|
+
const target = Math.min(count, HASH_CAP)
|
|
228
|
+
if (cached.hashes.length < target) {
|
|
229
|
+
cached.hashes = extendPrefixHashes(cached.hashes, live.snapshotEvents())
|
|
230
|
+
this.#dirty = true
|
|
231
|
+
}
|
|
232
|
+
cached.eventCount = count
|
|
233
|
+
return cached
|
|
234
|
+
}
|
|
235
|
+
const entry = this.#build(live.header, live.inheritedEventCount, live.snapshotEvents())
|
|
236
|
+
this.#store(id, entry)
|
|
237
|
+
return entry
|
|
238
|
+
}
|
|
239
|
+
if (cached !== undefined) {
|
|
240
|
+
const target = Math.min(cached.eventCount, HASH_CAP)
|
|
241
|
+
if (cached.hashes.length >= target) return cached
|
|
242
|
+
// Older live reads could persist a fingerprint shorter than the log;
|
|
243
|
+
// rebuild it instead of trusting the truncated prefix.
|
|
244
|
+
}
|
|
245
|
+
const observation = await this.#ctx.sessionQuery.observeSession(id, { projectionMode: 'none' })
|
|
246
|
+
try {
|
|
247
|
+
const entry = this.#build(observation.header, observation.inheritedEventCount, observation.events)
|
|
248
|
+
this.#store(id, entry)
|
|
249
|
+
return entry
|
|
250
|
+
} finally {
|
|
251
|
+
observation[Symbol.dispose]?.()
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Keep one freshly built entry and mark the persisted cache dirty.
|
|
257
|
+
*
|
|
258
|
+
* @param id - session id.
|
|
259
|
+
* @param entry - fresh entry.
|
|
260
|
+
*/
|
|
261
|
+
#store(id, entry) {
|
|
262
|
+
this.#entries.set(id, entry)
|
|
263
|
+
this.#fromDisk.delete(id)
|
|
264
|
+
this.#dirty = true
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Schedule one debounced cache write.
|
|
269
|
+
*/
|
|
270
|
+
#schedulePersist() {
|
|
271
|
+
if (this.#persistTimer !== 0 || !this.#dirty) return
|
|
272
|
+
this.#persistTimer = setTimeout(() => {
|
|
273
|
+
this.#persistTimer = 0
|
|
274
|
+
if (!this.#dirty) return
|
|
275
|
+
this.#dirty = false
|
|
276
|
+
void writeFingerprints(this.#entries)
|
|
277
|
+
}, PERSIST_DEBOUNCE_MS)
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Fingerprint one loaded log and keep only what inference needs.
|
|
282
|
+
*
|
|
283
|
+
* @param header - session header from `sessions` or `sessionQuery`.
|
|
284
|
+
* @param inheritedEventCount - inherited prefix length N.
|
|
285
|
+
* @param events - raw event log.
|
|
286
|
+
* @returns cache entry.
|
|
287
|
+
*/
|
|
288
|
+
#build(header, inheritedEventCount, events) {
|
|
289
|
+
const n = Number(inheritedEventCount)
|
|
290
|
+
const hashes = prefixHashes(events, HASH_CAP)
|
|
291
|
+
if (header.isSeeded === true && n > 0 && events[n]?.type !== 'session/end-seed') {
|
|
292
|
+
this.#log.warn(`session ${header.id}: inherited marker missing at ${n}`)
|
|
293
|
+
}
|
|
294
|
+
return { header, n, eventCount: events.length, hashes }
|
|
295
|
+
}
|
|
296
|
+
}
|
package/locale/en.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"meta": {
|
|
3
|
+
"title": "Branch chat · session tree",
|
|
4
|
+
"description": "Fork one session into many parallel child conversations (5 → 5.1, 5.2, 5.3 …) shown as a collapsible tree in the sidebar, and start a branch from any assistant answer. Adds a “Branch chat” sidebar item, a per-answer branch button, exact parent links, launch-time folding, and arrows only where children exist.",
|
|
5
|
+
"detail": "docs/DETAIL.md"
|
|
6
|
+
}
|
|
7
|
+
}
|
package/locale/zh.json
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,58 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-session-branch",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.3.0",
|
|
4
|
+
"description": "Branch Chat for DeepSeek Harness: fork a session into as many parallel child conversations as you want, keep every branch in a collapsible session tree, and start a branch from any answer.",
|
|
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
|
+
"docs",
|
|
18
|
+
"cordis.patch.yml",
|
|
19
|
+
"package.json",
|
|
20
|
+
"README.md",
|
|
21
|
+
"README.en.md",
|
|
22
|
+
"CHANGELOG.md",
|
|
23
|
+
"LICENSE",
|
|
24
|
+
"icon.svg"
|
|
25
|
+
],
|
|
26
|
+
"keywords": [
|
|
27
|
+
"dsh-plugin",
|
|
28
|
+
"dsh-bundle",
|
|
29
|
+
"deepseek-harness",
|
|
30
|
+
"dsh",
|
|
31
|
+
"branch",
|
|
32
|
+
"fork",
|
|
33
|
+
"session-tree",
|
|
34
|
+
"sidebar"
|
|
35
|
+
],
|
|
36
|
+
"author": "adorable520665",
|
|
37
|
+
"license": "MIT",
|
|
38
|
+
"repository": {
|
|
39
|
+
"type": "git",
|
|
40
|
+
"url": "git+https://github.com/adorable520665/dsh-session-branch.git"
|
|
41
|
+
},
|
|
42
|
+
"homepage": "https://github.com/adorable520665/dsh-session-branch#readme",
|
|
43
|
+
"bugs": {
|
|
44
|
+
"url": "https://github.com/adorable520665/dsh-session-branch/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
|
+
}
|
|
58
|
+
}
|