dsh-context-graph 0.6.0 → 0.6.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/README.md CHANGED
@@ -193,6 +193,9 @@ so restate every key you keep.
193
193
  | `inlineMaxChars` | `1200` | A single dominant hit this short is shown verbatim |
194
194
  | `surfaceMaxChats` | `20` | Shared chats listed on the first message of each chat |
195
195
  | `builderMaxTokens` | `8192` | Output cap for the indexing call; thinking models spend part of it reasoning |
196
+ | `builderMaxInputChars` | `60000` | Most text one indexing call reads; a long chat indexed for the first time keeps only its newest messages |
197
+ | `rangeMaxChats` | `3` | When a message names a time and no topic matches, at most this many chats from that time are listed |
198
+ | `rangeMaxTopics` | `3` | ...with this many topics each. The list names what it left out; `recall("2026-11-01")` shows the full day |
196
199
  | `provider`, `model` | `''` | Empty: reuse the conversation's routed provider and model. Set both to index with a cheaper or faster model |
197
200
 
198
201
  ## Updating
@@ -209,7 +212,7 @@ Remove with `dsh plugin --profile desktop remove dsh-context-graph`.
209
212
  ## On disk
210
213
 
211
214
  ```
212
- .context-graph/chats/<sessionId>/meta.json cid, title, startedAt, lastActiveAt, turns; memoryOff / private only after off / unshare
215
+ .context-graph/chats/<sessionId>/meta.json cid, title, startedAt, lastActiveAt, turns, builtSeq (how far the chat is indexed); memoryOff / private only after off / unshare
213
216
  .context-graph/chats/<sessionId>/graph.json topics and links; every topic has its time
214
217
  .context-graph/chats/<sessionId>/src/<id>.md verbatim messages behind each topic
215
218
  ```
package/README.zh.md CHANGED
@@ -70,7 +70,7 @@ DeepSeek Harness 插件:给每个对话一张小型记忆图谱,安装后默
70
70
  ## 文件
71
71
 
72
72
  ```
73
- .context-graph/chats/<对话>/meta.json 编号、标题、开始/最近时间、轮数;执行 off / unshare 后才有 memoryOff / private
73
+ .context-graph/chats/<对话>/meta.json 编号、标题、开始/最近时间、轮数、builtSeq(已索引到哪里);执行 off / unshare 后才有 memoryOff / private
74
74
  .context-graph/chats/<对话>/graph.json 主题与链接,每个主题带时间
75
75
  .context-graph/chats/<对话>/src/<id>.md 每个主题背后的原始消息
76
76
  ```
package/cordis.patch.yml CHANGED
@@ -11,5 +11,8 @@
11
11
  inlineMaxChars: 1200 # one dominant hit this short is shown verbatim, no recall needed
12
12
  surfaceMaxChats: 20 # shared chats listed on the first message of each chat
13
13
  builderMaxTokens: 8192 # thinking models spend part of this on reasoning
14
+ builderMaxInputChars: 60000 # newest messages the indexing call reads; older ones are skipped
15
+ rangeMaxChats: 3 # a message naming a time with no topic match lists at most this many chats...
16
+ rangeMaxTopics: 3 # ...with this many topics each; recall("YYYY-MM-DD") shows the full day
14
17
  provider: '' # empty: reuse the conversation's routed provider/model
15
18
  model: ''
package/graph.js CHANGED
@@ -180,11 +180,11 @@ export const globalId = (view, id) => `c${view.cid}${id}`
180
180
  * try chats active in that range first, then topics updated in it, then everything.
181
181
  * @returns { hits: [{ id, node, score, via, view }], slice }
182
182
  */
183
- export function matchAcross(views, query, { max = 5, range } = {}) {
183
+ export function matchAcross(views, query, { max = 5, range, skip } = {}) {
184
184
  const others = views.filter((v) => !v.own)
185
185
  const run = (vs, keep = () => true) => vs
186
- .flatMap((view) => matchNodes(view.graph, query, { max }).map((h) => ({ ...h, id: globalId(view, h.id), via: h.via && globalId(view, h.via), view })))
187
- .filter(keep)
186
+ .flatMap((view) => matchNodes(view.graph, query, { max: max + (skip?.size ?? 0) }).map((h) => ({ ...h, id: globalId(view, h.id), via: h.via && globalId(view, h.via), view })))
187
+ .filter((h) => keep(h) && !skip?.has(h.id))
188
188
  .sort((a, b) => b.score - a.score)
189
189
  .slice(0, max)
190
190
  if (range) {
@@ -238,7 +238,10 @@ export function renderChatIndex(view, { max = 40 } = {}) {
238
238
  return lines.join('\n')
239
239
  }
240
240
 
241
- /** Everything that happened in a time range: each chat active then, with its topics from that range first. */
241
+ /**
242
+ * Everything that happened in a time range: each chat active then, with its topics from that range
243
+ * first. Whatever the caps cut is named, with the recall id that shows it, so nothing is hidden.
244
+ */
242
245
  export function renderRange(views, range, { maxChats = 10, maxTopics = 10 } = {}) {
243
246
  const chats = newestFirst(views.filter((v) => chatInRange(v.meta, range)))
244
247
  if (chats.length === 0) return `[${SOURCE_KIND}] No chats from ${range.label}.`
@@ -247,6 +250,11 @@ export function renderRange(views, range, { maxChats = 10, maxTopics = 10 } = {}
247
250
  lines.push(chatLine(view))
248
251
  const ids = Object.keys(view.graph.nodes).sort((a, b) => Number(inRange(view.graph.nodes[b].updated, range)) - Number(inRange(view.graph.nodes[a].updated, range)))
249
252
  for (const id of ids.slice(0, maxTopics)) lines.push(' ' + topicLine(globalId(view, id), view.graph.nodes[id]))
253
+ if (ids.length > maxTopics) lines.push(` - ... ${ids.length - maxTopics} more topics: recall("c${view.cid}")`)
254
+ }
255
+ if (chats.length > maxChats) {
256
+ const rest = chats.slice(maxChats).map((v) => `c${v.cid}`)
257
+ lines.push(`- ... ${rest.length} more chats (${rest.slice(0, 10).join(', ')}${rest.length > 10 ? ', ...' : ''}): recall a chat id or a day (YYYY-MM-DD)`)
250
258
  }
251
259
  return lines.join('\n')
252
260
  }
@@ -417,6 +425,37 @@ export function collectSpan(nodes, eventAt, afterSeq = -1) {
417
425
  return { messages, lastSeq, userTurns }
418
426
  }
419
427
 
428
+ /**
429
+ * What this chat has already been sent by the plugin and still holds in context: topic ids
430
+ * (c3n12) and whole block texts. Read from the surface, so after compaction drops a block its
431
+ * topics can be sent again.
432
+ */
433
+ export function shownContext(nodes, eventAt) {
434
+ const ids = new Set()
435
+ const texts = new Set()
436
+ for (const seq of nodes) {
437
+ const event = eventAt(seq)
438
+ if (event?.type !== 'user/message' || event.data?.source?.kind !== SOURCE_KIND) continue
439
+ const text = textOf(event.data.content)
440
+ texts.add(text)
441
+ for (const m of text.matchAll(/\bc\d+n\d+\b/g)) ids.add(m[0])
442
+ }
443
+ return { ids, texts }
444
+ }
445
+
446
+ /** The newest messages whose builder transcript fits in `maxChars` (each message counts at most `perMessage`). */
447
+ export function tailWithin(messages, maxChars, perMessage = 2000) {
448
+ let used = 0
449
+ let start = messages.length
450
+ while (start > 0) {
451
+ const cost = Math.min(messages[start - 1].text.length, perMessage) + 20
452
+ if (start < messages.length && used + cost > maxChars) break
453
+ used += cost
454
+ start--
455
+ }
456
+ return messages.slice(start)
457
+ }
458
+
420
459
  /** First human prompt of the log, shortened: the chat's title until dsh gives us a better one. */
421
460
  export function titleOf(eventAt, width = 60) {
422
461
  for (let seq = 0; ; seq++) {
package/index.js CHANGED
@@ -23,7 +23,7 @@ import { defineTool } from '@deepseek-ai/dsh-tools'
23
23
 
24
24
  import {
25
25
  builderInstruction, collectSpan, estimateTokens, matchAcross, memoryOn, mergeDelta, sharedOn, neighbors, parseBuilderOutput, parseTimeRange,
26
- renderChatIndex, renderChats, renderHits, renderMap, renderRange, SOURCE_KIND, titleOf, transcriptForBuilder, turnTranscript
26
+ renderChatIndex, renderChats, renderHits, renderMap, renderRange, shownContext, SOURCE_KIND, tailWithin, titleOf, transcriptForBuilder, turnTranscript
27
27
  } from './graph.js'
28
28
  import { appendSource, chatDir, ensureMeta, listChats, loadGraph, loadMeta, readSource, saveGraph, saveMeta } from './store.js'
29
29
  import { startUpdateCheck } from './update.js'
@@ -43,6 +43,9 @@ const DEFAULTS = {
43
43
  inlineMaxChars: 1200,
44
44
  surfaceMaxChats: 20,
45
45
  builderMaxTokens: 8192,
46
+ builderMaxInputChars: 60000,
47
+ rangeMaxChats: 3,
48
+ rangeMaxTopics: 3,
46
49
  provider: '',
47
50
  model: ''
48
51
  }
@@ -67,7 +70,8 @@ export function apply(ctx, config = {}) {
67
70
 
68
71
  const state = (sid) => {
69
72
  let st = sessions.get(sid)
70
- if (!st) { st = { lastBuiltSeq: -1, building: false, announce: true }; sessions.set(sid, st) }
73
+ // retryAt: after a failed build, wait for this many new human turns before trying again (#3).
74
+ if (!st) { st = { building: false, announce: true, retryAt: 0 }; sessions.set(sid, st) }
71
75
  return st
72
76
  }
73
77
 
@@ -121,24 +125,33 @@ export function apply(ctx, config = {}) {
121
125
  // ponytail: subagent chatter stays out of the graph; revisit if delegated work holds decisions worth keeping.
122
126
  if (isSubagent(agent)) return { status: 'skipped', reason: 'subagent session' }
123
127
  st.building = true
128
+ let span
129
+ const failed = (reason) => {
130
+ if (span) st.retryAt = span.userTurns + cfg.buildEveryTurns
131
+ return { status: 'failed', reason }
132
+ }
124
133
  try {
125
- const meta = await metaFor(agent)
134
+ let meta = await metaFor(agent)
126
135
  if (!memoryOn(meta)) return { status: 'off', reason: 'memory is off for this chat; /context-graph on resumes it' }
127
- // A chat that never ran a command has no folder yet; give it one so it gets a chat number.
128
- if (!meta) await setFlags(agent, {})
129
- const span = collectSpan(session.surface.nodes, (seq) => session.eventAt(seq), st.lastBuiltSeq)
136
+ // A chat that never ran a command has no folder yet; give it one so it gets a chat number (#1).
137
+ if (!meta) meta = await setFlags(agent, {})
138
+ // The index position is kept in meta.json so an app restart does not index the chat again (#2).
139
+ span = collectSpan(session.surface.nodes, (seq) => session.eventAt(seq), meta.builtSeq ?? -1)
130
140
  const chars = span.messages.reduce((n, m) => n + m.text.length, 0)
131
141
  if (span.messages.length === 0) return { status: 'nothing', reason: 'no new messages since the last build' }
132
- if (!force && (span.userTurns < cfg.buildEveryTurns || chars < cfg.buildMinChars)) {
133
- return { status: 'skipped', reason: `${span.userTurns}/${cfg.buildEveryTurns} human turns, ${chars}/${cfg.buildMinChars} chars` }
142
+ const needTurns = Math.max(cfg.buildEveryTurns, st.retryAt)
143
+ if (!force && (span.userTurns < needTurns || chars < cfg.buildMinChars)) {
144
+ return { status: 'skipped', reason: `${span.userTurns}/${needTurns} human turns, ${chars}/${cfg.buildMinChars} chars` }
134
145
  }
146
+ // A long chat indexed for the first time keeps the call small: only its newest messages are read (#4).
147
+ const messages = tailWithin(span.messages, cfg.builderMaxInputChars)
135
148
  const llm = ctx.get('llm')
136
149
  if (!llm) return { status: 'unavailable', reason: 'no llm service in this composition' }
137
150
  const target = resolveTarget(agent)
138
151
  if (!target) return { status: 'unavailable', reason: 'no provider/model routed yet' }
139
152
  const chat = chatDir(dirFor(agent), sid)
140
153
  const graph = await loadGraph(chat)
141
- const prompt = `${transcriptForBuilder(span.messages)}\n\n${builderInstruction(graph)}`
154
+ const prompt = `${transcriptForBuilder(messages)}\n\n${builderInstruction(graph)}`
142
155
  const assembler = new BlockAssembler()
143
156
  for await (const chunk of llm.stream({
144
157
  provider: target.provider,
@@ -162,24 +175,26 @@ export function apply(ctx, config = {}) {
162
175
  const why = finish?.kind === 'max-tokens'
163
176
  ? `the model ran out of output tokens (builderMaxTokens ${cfg.builderMaxTokens}) before finishing`
164
177
  : 'builder output was not the expected JSON'
165
- return { status: 'failed', reason: `${why}; raw output saved to ${saved}` }
178
+ return failed(`${why}; raw output saved to ${saved}`)
166
179
  }
167
180
  // Reversibility rule: a node is written only when it cites text we can store verbatim.
168
- delta.nodes = delta.nodes.filter((n) => n.msgs.some((i) => span.messages[i]))
181
+ delta.nodes = delta.nodes.filter((n) => n.msgs.some((i) => messages[i]))
169
182
  const now = new Date().toISOString()
170
183
  const touched = mergeDelta(graph, delta, { session: sid, turn, now })
171
184
  for (const { id, msgs } of touched) {
172
- const cited = msgs.map((i) => span.messages[i]).filter(Boolean)
185
+ const cited = msgs.map((i) => messages[i]).filter(Boolean)
173
186
  if (cited.length > 0) await appendSource(chat, id, { session: sid, turn, at: now, messages: cited })
174
187
  }
175
188
  await saveGraph(chat, graph)
176
- await saveMeta(dirFor(agent), sid, { ...meta, title: meta.title ?? titleOf((seq) => session.eventAt(seq)), lastActiveAt: now, turns: (meta.turns ?? 0) + span.userTurns })
177
- st.lastBuiltSeq = span.lastSeq
189
+ // Re-read: a share/off command may have changed the flags while the model was answering.
190
+ const latest = (await metaFor(agent)) ?? meta
191
+ await saveMeta(dirFor(agent), sid, { ...latest, title: latest.title ?? titleOf((seq) => session.eventAt(seq)), lastActiveAt: now, turns: (latest.turns ?? 0) + span.userTurns, builtSeq: span.lastSeq })
192
+ st.retryAt = 0
178
193
  meter.builds++
179
194
  return { status: 'built', nodes: touched.length, newNodes: touched.filter((t) => t.isNew).length, totalNodes: Object.keys(graph.nodes).length, edges: graph.edges.length, dir: chat }
180
195
  } catch (error) {
181
196
  console.error('context-graph: build failed (non-fatal)', error)
182
- return { status: 'failed', reason: String(error?.message ?? error) }
197
+ return failed(String(error?.message ?? error))
183
198
  } finally {
184
199
  st.building = false
185
200
  }
@@ -226,9 +241,15 @@ export function apply(ctx, config = {}) {
226
241
  if (list) { parts.push(list); layer = 'surface' }
227
242
  }
228
243
  const range = parseTimeRange(prompt)
229
- const { hits, slice } = matchAcross(views, prompt, { max: cfg.maxHits, range })
244
+ // Topics already sent in this chat are still in its context; do not send them again (#5).
245
+ const shown = shownContext(agent.session.surface.nodes, (seq) => agent.session.eventAt(seq))
246
+ const { hits, slice } = matchAcross(views, prompt, { max: cfg.maxHits, range, skip: shown.ids })
230
247
  if (hits.length === 0 && range) {
231
- parts.push(renderRange(views.filter((v) => !v.own), range))
248
+ // Sent with every message that names a time, so kept small; recall("YYYY-MM-DD") gives the full day.
249
+ const block = renderRange(views.filter((v) => !v.own), range, { maxChats: cfg.rangeMaxChats, maxTopics: cfg.rangeMaxTopics })
250
+ const ids = block.match(/\bc\d+n\d+\b/g) ?? []
251
+ const seen = shown.texts.has(block) || (ids.length > 0 && ids.every((id) => shown.ids.has(id)))
252
+ if (!seen) parts.push(block)
232
253
  } else {
233
254
  let inline
234
255
  if (hits.length === 1 || (hits.length > 1 && hits[0].score >= 2 * hits[1].score)) {
@@ -311,7 +332,7 @@ export function apply(ctx, config = {}) {
311
332
  const m = /^(?:c(\d+))?(n\d+)?$/.exec(id)
312
333
  if (!m || (!m[1] && !m[2])) return { status: 'unknown', text: `"${id}" is not an id. Use c3n12, c3, 2026-11-01 or t7, and only ids you were shown.` }
313
334
  const view = m[1] ? views.find((v) => String(v.cid) === m[1]) : views.find((v) => v.own)
314
- if (!view) return { status: 'unknown', text: `No chat c${m[1]} shared in this workspace.` }
335
+ if (!view) return { status: 'unknown', text: m[1] ? `No chat c${m[1]} shared in this workspace.` : 'This chat has no indexed topics yet. Use a full id such as c3n12 from a [context-graph] block.' }
315
336
  if (!m[2]) return ok(renderChatIndex(view))
316
337
  const node = view.graph.nodes[m[2]]
317
338
  if (!node) return { status: 'unknown', text: `No topic ${id}. Only use ids shown in a [context-graph] block.` }
@@ -356,7 +377,7 @@ export function apply(ctx, config = {}) {
356
377
  const result = await agent.runMaintenance(() => build(agent, { force: true }))
357
378
  const shared = (await loadViews(agent)).filter((v) => !v.own).length
358
379
  const indexed = result.status === 'built' ? `${result.totalNodes} topics in this chat` : `nothing indexed yet (${result.reason ?? result.status})`
359
- return { kind: 'success', text: `Memory on · ${indexed} · ${shared} shared ${shared === 1 ? 'chat' : 'chats'} to draw on\nThis chat indexes itself every ${cfg.buildEveryTurns} turns. /context-graph share lets other chats use it; /context-graph off stops it.` }
380
+ return { kind: 'success', text: `Memory on · ${indexed} · ${shared} shared ${shared === 1 ? 'chat' : 'chats'} to draw on\nThis chat indexes itself every ${cfg.buildEveryTurns} turns and other chats in this workspace can use it. /context-graph unshare keeps it private; /context-graph off stops it.` }
360
381
  }
361
382
  if (sub === 'off') {
362
383
  await setFlags(agent, { memoryOff: true })
@@ -391,7 +412,7 @@ export function apply(ctx, config = {}) {
391
412
  const cached = m.cacheRead + m.cacheWrite
392
413
  const hitRate = m.input + cached > 0 ? Math.round((m.cacheRead / (m.input + cached)) * 100) : 0
393
414
  const session = agent.session
394
- const pending = collectSpan(session.surface.nodes, (seq) => session.eventAt(seq), state(sid).lastBuiltSeq).userTurns
415
+ const pending = collectSpan(session.surface.nodes, (seq) => session.eventAt(seq), own?.meta.builtSeq ?? -1).userTurns
395
416
  const shared = views.filter((v) => !v.own).length
396
417
  return {
397
418
  kind: 'success',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-context-graph",
3
- "version": "0.6.0",
3
+ "version": "0.6.2",
4
4
  "description": "DeepSeek Harness plugin: memory graphs for every chat, on by default (switch the plugin off on the Plugins page to stop); other chats in the workspace recall exact past text by topic, chat or day, so fewer input tokens are re-sent",
5
5
  "icon": "./icon.svg",
6
6
  "type": "module",
@@ -26,7 +26,7 @@
26
26
  "LICENSE"
27
27
  ],
28
28
  "scripts": {
29
- "test": "node --import ./test/resolve-dsh.mjs --test test/graph.test.js test/update.test.js test/client.test.js test/plugin.test.js"
29
+ "test": "node --import ./test/resolve-dsh.mjs --test test/graph.test.js test/update.test.js test/client.test.js test/plugin.test.js test/review.test.js"
30
30
  },
31
31
  "keywords": [
32
32
  "dsh-plugin",