@aiwayds/dsh-topics-memory 0.9.0 → 0.10.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/README.md CHANGED
@@ -6,10 +6,28 @@ A dsh plugin: maintains "working topic memory" as an [OKF (Open Knowledge Format
6
6
 
7
7
  > **Requires dsh >= 0.1.2-rc.1** — this plugin targets the dsh RC/stable line only (CI and releases resolve the newest of the `latest`/`next` dist-tags at runtime). **The alpha line is no longer supported.**
8
8
 
9
+ ## Demo
10
+
11
+ The full flow in about four minutes: topics captured, distilled, and injected in a real session.
12
+
13
+ https://github.com/user-attachments/assets/8c06cc98-b1ed-402b-9110-4f9a93eb15bc
14
+
9
15
  ## The problem it solves
10
16
 
11
17
  Long sessions forget. Cross-session, even more so. This plugin maintains **structured topic memory**: each Topic records a matter's **name, dependencies, open questions, current conclusion, impact, and recommendations**. When a conclusion changes, edit the file and commit — `git log` directly answers "when, by whom, and why did this conclusion change".
12
18
 
19
+ ## Why this plugin exists: memory is edited, not accumulated
20
+
21
+ The short version: **more memory is not better memory.**
22
+
23
+ Most memory tools assume accumulation — record everything, retrieve broadly. That may work for humans; for LLMs it backfires twice over. Model attention is a finite resource, so a giant memory bank means every turn is spent digging for signal in noise. Worse, process memories hoard intermediate judgments that were right once and wrong later — and they will confidently steer the model into bad decisions.
24
+
25
+ So this plugin takes a hard editorial line on what deserves to be remembered: **a topic records exactly four things — the question that started it, the conclusion it reached, what it impacts, and what it depends on. Everything in between — the discussion, the dead ends, the wrong turns — is deliberately not memory.** Process belongs to the session; when the session ends, it goes. Only conclusions that survive distillation make it into the bundle.
26
+
27
+ Short-term memory is the session's own job — the conversation context already is one, and a plugin that feeds it back is noise. Long-term memory belongs to topics: small, structured, git-traceable, injected in budgeted slices with zero hits meaning zero injection. Every turn hands the model the **minimum high-value context**, not the biggest warehouse.
28
+
29
+ This plugin is not trying to be the model's notebook. It is trying to be the model's editor: deciding what is worth keeping — and, more importantly, what should be forgotten.
30
+
13
31
  ## Core features
14
32
 
15
33
  - **Strict OKF v0.2 compliance**: each Topic is a `markdown + YAML frontmatter` concept document (`type: Topic`) that the whole OKF ecosystem (Obsidian, OKF validators) can consume directly; ships with the provenance (`sources`), trust (`generated`/`verified`), and lifecycle (`status`/`stale_after`) field families.
@@ -30,7 +48,7 @@ Long sessions forget. Cross-session, even more so. This plugin maintains **struc
30
48
 
31
49
  | Model tools | Purpose |
32
50
  |---|---|
33
- | `topic_save` | Distill/revise a Topic (name / dependencies / open questions / conclusion / impact / recommendations) |
51
+ | `topic_save` | Save/revise a Topic (name / dependencies / open questions / conclusion / impact / recommendations) |
34
52
  | `topic_observe` | Jot an atomic observation (decision/finding/constraint/question), pending distill |
35
53
  | `topic_search` | LLM-free keyword search over memory |
36
54
  | `topic_history` | A topic's conclusion change history (git log as a tool) |
@@ -115,7 +133,7 @@ This project's shape is directly inspired and supported by:
115
133
  ## Known boundaries
116
134
 
117
135
  - **Subagents engage memory by default — one switch to opt out**: by default (`include-subagents` on), injection and observation apply to subagent sessions too. `/topics set include-subagents off` skips delegated sessions entirely — no injection, no observation, no distill triggers; the topic tools stay on the global layer, so an explicit `topic_save` from a child still lands. Out-of-process subagents (claude-code/codex providers) never load this plugin anyway.
118
- - **Session-end distill in headless one-shot sessions**: the plugin's disposer now waits (bounded, up to 90s) for the exit-triggered distill to land before unload completes, so a headless process no longer loses that race by default; a hanging model call gives up at the cap and exit proceeds without that run. Within that 90s window the exit run may complete only part of its call budget — the remaining backlog waits for the next trigger. The exit trigger is skipped entirely while a session-end run is still in flight (the same pool head would otherwise be fed to the model twice). Manual `/topics distill` runs inside a live session bypass the race entirely; `meta/distill-state.json` records each lane's outcome, checkable via `/topics status`.
136
+ - **Exit is local-only (0.10.0)**: the plugin's disposer makes one local git commit of the meta sidecars (observations / injections / distill state) and never waits on the network — no pull, no push, no model call, so host exit no longer pays a git round-trip or a bounded distill wait (the old 90s cap is gone; only a 10s guard against a pathological git stall remains). The exit distill trigger is fired fire-and-forget and is skipped entirely while a session-end run is still in flight (the same pool head would otherwise be fed to the model twice). Nothing is lost by skipping: observations are write-through on disk, the deferred push is replayed by the next boot's pull, and the skipped distill is replayed there too (boot-replay). `meta/distill-state.json` records each lane's outcome, checkable via `/topics status`.
119
137
  - **Observation GC (three strikes)**: an observation the model actually evaluated (parseable answer, however useless) but no op consumed accrues one failed attempt; the third failed attempt physically deletes it — explicitly authorized cleanup of raw data the lane demonstrably cannot process. Runs that never evaluated the batch never count: infrastructure failures (network errors, unconfigured distill route → readable `no-model` short-circuit) and unparseable output (`invalid-output`) are exempt, and a batch still mid-shrink on output-limit retries is only counted once a verdict is reached (success, floor stop, stall, or an explicit skip). Deletions are committed immediately (data destruction stays git-traceable); pure attempt counters follow the usual flush cadence.
120
138
  - **Config read timing**: `/topics set` and `settings.yaml` edits take effect most reliably from the next session start.
121
139
  - **Picking a distill model**: `/topics onboard` splits the distill decision into two dependent questions (provider first, then that provider's model catalog), pre-validated with `resolveModelInfo` — a provider with no live route blocks and re-asks, an off-catalog model (a non-NO_ADAPTER failure: outside the advisory catalog, possibly still usable) warns but is allowed; hosts without an ask UI or a usable model route fall back to typed input. The same validation backs the `/topics set` picker panels.
package/README.zh.md CHANGED
@@ -6,10 +6,28 @@
6
6
 
7
7
  > **要求 dsh >= 0.1.2-rc.1** — 本插件只跟随 dsh RC/stable 线(CI 与发版在运行时解析 latest/next 中更新的 dist-tag)。**不再支持 alpha 线。**
8
8
 
9
+ ## 演示
10
+
11
+ 约 4 分钟的完整流程:topic 沉淀、蒸馏、注入在真实会话里跑起来。
12
+
13
+ https://github.com/user-attachments/assets/6e9b346d-2ec9-4148-ae62-7087797d3188
14
+
9
15
  ## 它解决什么问题
10
16
 
11
17
  长会话会失忆,跨会话更会。本插件维护一份**结构化的 topic 记忆**:每个 Topic 记录一件事的**名字、依赖、未决问题、目前结论、影响、建议**。结论变了就改文件、打 commit——`git log` 直接回答「这个结论什么时候、被谁、为什么改的」。
12
18
 
19
+ ## 初衷:记忆是编辑出来的,不是攒出来的
20
+
21
+ 先说结论:**记忆不是越多越好。**
22
+
23
+ 「记忆」类产品的常见思路是多多益善——能记的都记,用时全量检索。这对人也许成立,对 LLM 恰恰相反,而且反在两处:模型的注意力是有限资源,巨型记忆库意味着每一轮对话都在噪音里翻找信号;更危险的是,过程记忆里囤着大量「当时正确、事后失效」的中间判断,它们会理直气壮地把模型引向错误的决定。
24
+
25
+ 所以本插件对「记什么」做了一次苛刻的取舍:**一个 topic 只记四件事——它从什么问题开始、得出了什么结论、影响什么、依赖什么。至于得出结论的过程:讨论、试错、绕过的弯路,一概不记。**过程属于 session,session 结束就该随它去;能留下的,只有经得起蒸馏的结论。
26
+
27
+ 短时记忆交给 session,对话上下文本就是它,不需要插件再回喂一遍;长时记忆交给 topics——小、结构化、git 可追溯,注入按预算切片、零命中零注入,每轮给模型的都是**最小的高价值上下文**,而不是越大越好的仓库。
28
+
29
+ 所以这个插件想做的不是模型的记事本,而是模型的编辑:替它决定什么值得记住,以及——更要紧的——什么应该被忘掉。
30
+
13
31
  ## 核心特性
14
32
 
15
33
  - **OKF v0.2 严格合规**:每个 Topic 是 `markdown + YAML frontmatter` 的 concept 文档(`type: Topic`),可被 Obsidian、OKF 校验器等整个生态直接消费;自带 provenance(`sources`)、trust(`generated`/`verified`)、lifecycle(`status`/`stale_after`)三族字段。
@@ -115,7 +133,7 @@ dsh plugin --profile <name> remove @aiwayds/dsh-topics-memory
115
133
  ## 已知边界
116
134
 
117
135
  - **子代理默认参与记忆,可整体关掉**:默认(`include-subagents` 开)注入与观察同样作用于子代理会话。`/topics set include-subagents off` 后,delegation depth > 0 的子代理会话被整体跳过——不注入、不观察、不触发蒸馏;topic 工具始终在全局层(子代理显式 `topic_save` 不受开关影响)。跨进程子代理(claude-code/codex 等 provider)本就不加载本插件。
118
- - **headless 单发会话里的 session-end 蒸馏**:插件 disposer 现在会等待退出触发的末次蒸馏落盘(有界等待,上限 90s;挂死的模型调用在超时后放弃、退出照常进行),headless 进程默认不再输掉这场竞速。90s 窗口内退出 run 可能只完成部分调用预算,剩余积压留待下次触发。已有 session-end run 在跑时退出触发整体跳过(否则同一全局队头批次会被双份喂给模型)。会话内 `/topics distill` 手动触发则完全绕开竞速;`meta/distill-state.json` 记录每次 lane 的结局,`/topics status` 可查。
136
+ - **退出路径本地化(0.10.0)**:插件 disposer 只做一步本地 git commit(meta 侧车文件:observations / injections / distill state),不再等待任何网络——没有 pull、没有 push、没有模型调用,宿主退出不再支付 git 双程与有界蒸馏等待(旧的 90s 上限移除,仅保留 10s 兜底以防病理性 git 卡死)。退出蒸馏触发改为 fire-and-forget;已有 session-end run 在跑时整体跳过(否则同一全局队头批次会被双份喂给模型)。跳过不丢活:observations 本就 write-through 落盘,推迟的 push 由下次启动的 pull 补推,被跳过的蒸馏同样由下次启动回放(boot-replay)。`meta/distill-state.json` 记录每次 lane 的结局,`/topics status` 可查。
119
137
  - **观察 GC(三振删除)**:被模型实际评估(返回了可解析应答,无论内容有无价值)却未被任何 op 消费的观察记一次 failed attempt,连续 3 次即物理删除(用户已明确授权删除 lane 确实无法处理的原始观察)。模型从未评估过的批次永不计数:基础设施失败(网络错误、蒸馏路由未配置 → 可读 `no-model` 短路)与输出不可解析(`invalid-output`)豁免;输出上限减半重试中的批次只有到达裁决(成功 / 触底 / stalled / 明确跳过)才计一次。删除立即 commit(数据销毁 git 可追溯),纯计数沿用 flush 节奏。
120
138
  - **配置读取时机**:`/topics set` 与 settings.yaml 修改在下次会话启动后生效最稳。
121
139
  - **蒸馏选模型**:`/topics onboard` 的蒸馏一步拆成两问(先 provider 后 model,模型列表取自该 provider 的目录),选完经 `resolveModelInfo` 预校验——provider 无活路由会阻断重选,模型目录校验非 NO_ADAPTER 失败(目录外,可能仍可用)则警告但放行;无 ask UI 或无可用模型路由的环境自动退回文本输入。`/topics set` 的选择面板走同一套校验。
package/lib/distill.d.ts CHANGED
@@ -69,8 +69,10 @@ export declare class Distiller {
69
69
  * caller can hook post-run cleanup — the per-session llm capture in
70
70
  * index.ts must live exactly as long as a run that may read it. `manual`
71
71
  * is the /topics distill trigger: same lane, same in-flight guard.
72
+ * `boot-replay` is the session-start backlog drain: a previous exit's
73
+ * skipped (or killed-mid-run) distill replays here.
72
74
  */
73
- request(sessionId: string, reason: 'every-n' | 'session-end' | 'manual'): Promise<DistillResult> | undefined;
75
+ request(sessionId: string, reason: 'every-n' | 'session-end' | 'manual' | 'boot-replay'): Promise<DistillResult> | undefined;
74
76
  /** True while a run for the session is still in flight (teardown guard). */
75
77
  hasPending(sessionId: string): boolean;
76
78
  /** True while ANY session's run is still in flight (exit-dispose guard). */
package/lib/distill.js CHANGED
@@ -65,6 +65,8 @@ export class Distiller {
65
65
  * caller can hook post-run cleanup — the per-session llm capture in
66
66
  * index.ts must live exactly as long as a run that may read it. `manual`
67
67
  * is the /topics distill trigger: same lane, same in-flight guard.
68
+ * `boot-replay` is the session-start backlog drain: a previous exit's
69
+ * skipped (or killed-mid-run) distill replays here.
68
70
  */
69
71
  request(sessionId, reason) {
70
72
  if (!this.configured)
package/lib/git.d.ts CHANGED
@@ -51,7 +51,10 @@ export interface PullOutcome {
51
51
  * `pull --rebase` with conflict containment: on conflict we abort the rebase
52
52
  * (ADR 0003 — no automatic smart merging) and report the conflicted paths so
53
53
  * the caller can mark those topics. Remote and branch are explicit — the
54
- * bundle never depends on upstream tracking config.
54
+ * bundle never depends on upstream tracking config. IDENTITY rides along:
55
+ * a rebase REPLAYS local commits, which requires a committer identity, and
56
+ * a fresh machine (CI, new host) may have none — without this the pull
57
+ * fails with an empty conflicted list on every session start.
55
58
  */
56
59
  export declare function pullRebase(cwd: string, token?: string, remote?: string, branch?: string): Promise<PullOutcome>;
57
60
  export declare function push(cwd: string, token?: string, remote?: string, branch?: string): Promise<{
package/lib/git.js CHANGED
@@ -117,10 +117,13 @@ export async function fileAtRev(cwd, path, rev) {
117
117
  * `pull --rebase` with conflict containment: on conflict we abort the rebase
118
118
  * (ADR 0003 — no automatic smart merging) and report the conflicted paths so
119
119
  * the caller can mark those topics. Remote and branch are explicit — the
120
- * bundle never depends on upstream tracking config.
120
+ * bundle never depends on upstream tracking config. IDENTITY rides along:
121
+ * a rebase REPLAYS local commits, which requires a committer identity, and
122
+ * a fresh machine (CI, new host) may have none — without this the pull
123
+ * fails with an empty conflicted list on every session start.
121
124
  */
122
125
  export async function pullRebase(cwd, token, remote = 'origin', branch = 'main') {
123
- const r = await runGit(['pull', '--rebase', '--autostash', remote, branch], { cwd, token, mayFail: true, timeoutMs: 60_000 }).catch((e) => {
126
+ const r = await runGit([...IDENTITY, 'pull', '--rebase', '--autostash', remote, branch], { cwd, token, mayFail: true, timeoutMs: 60_000 }).catch((e) => {
124
127
  return { code: -1, stdout: '', stderr: String(e instanceof Error ? e.message : e) };
125
128
  });
126
129
  if (r.code === 0)
package/lib/index.d.ts CHANGED
@@ -18,13 +18,13 @@
18
18
  import type { Context } from '@deepseek-ai/cordis';
19
19
  export declare const name = "dsh-topics-memory";
20
20
  /**
21
- * Bounded wait for the process-exit distill. Session teardown is the last
22
- * distill trigger, but the disposer used to fire it fire-and-forget — process
23
- * exit always won the race and the run died unwritten. The disposer now waits
24
- * for the run, capped so a pathological (hanging) model call can never wedge
25
- * the host's exit.
21
+ * Bounded wait for the process-exit meta commit. The exit path is
22
+ * local-only: no pull/push, no model call — the deferred push rides the
23
+ * next boot's pull and the skipped exit distill is replayed there. The
24
+ * window only bounds the local `git commit` against a pathological stall
25
+ * (a wedged index.lock, a hung filesystem), which should never fire.
26
26
  */
27
- export declare const EXIT_DISTILL_TIMEOUT_MS = 90000;
27
+ export declare const EXIT_COMMIT_TIMEOUT_MS = 10000;
28
28
  /**
29
29
  * Resolve when `p` settles (rejection swallowed) or after `ms`, whichever
30
30
  * comes first. The cap timer is unref'd: an otherwise-idle event loop exits
package/lib/index.js CHANGED
@@ -11,13 +11,13 @@ import { CONFIG_KEYS, TopicsConfig } from "./config.js";
11
11
  import { isDelegated } from "./delegation.js";
12
12
  export const name = 'dsh-topics-memory';
13
13
  /**
14
- * Bounded wait for the process-exit distill. Session teardown is the last
15
- * distill trigger, but the disposer used to fire it fire-and-forget — process
16
- * exit always won the race and the run died unwritten. The disposer now waits
17
- * for the run, capped so a pathological (hanging) model call can never wedge
18
- * the host's exit.
14
+ * Bounded wait for the process-exit meta commit. The exit path is
15
+ * local-only: no pull/push, no model call — the deferred push rides the
16
+ * next boot's pull and the skipped exit distill is replayed there. The
17
+ * window only bounds the local `git commit` against a pathological stall
18
+ * (a wedged index.lock, a hung filesystem), which should never fire.
19
19
  */
20
- export const EXIT_DISTILL_TIMEOUT_MS = 90_000;
20
+ export const EXIT_COMMIT_TIMEOUT_MS = 10_000;
21
21
  /**
22
22
  * Resolve when `p` settles (rejection swallowed) or after `ms`, whichever
23
23
  * comes first. The cap timer is unref'd: an otherwise-idle event loop exits
@@ -301,10 +301,10 @@ export function apply(ctx) {
301
301
  // mid-pipeline deferred to THIS pipeline and never fires again.
302
302
  const slowLane = new SlowLane(service, caller, releaseSessionLlm);
303
303
  // ---- Observer (M2) ----
304
- // The process-exit disposer awaits this run (bounded): the callback below
305
- // records it synchronously when the fake 'dispose' session's end trigger
306
- // fires, so the disposer can wait for the last distill to land.
307
- let exitDistill;
304
+ // Trigger runs are fire-and-forget everywhere now — including the exit
305
+ // one. Observations are write-through on disk, so a run the process exits
306
+ // under is never lost work: its input is still undistilled and the next
307
+ // boot's replay distills it.
308
308
  const observer = new Observer(service, (sessionId, reason) => {
309
309
  // Trigger-time capture: the agent is still registered here, so its scoped
310
310
  // ctx can hand over the llm instance whose adapters are live.
@@ -321,8 +321,6 @@ export function apply(ctx) {
321
321
  // and its own settle hook does the release.
322
322
  const run = distiller.request(sessionId, reason);
323
323
  if (run !== undefined) {
324
- if (sessionId === 'dispose')
325
- exitDistill = run;
326
324
  void run.finally(() => releaseSessionLlm(sessionId)).catch(() => undefined);
327
325
  }
328
326
  });
@@ -445,36 +443,63 @@ export function apply(ctx) {
445
443
  releaseSessionLlm(sessionId);
446
444
  }));
447
445
  }
448
- // ---- Sync lifecycle: pull on session start, flush on dispose ----
446
+ // ---- Sync lifecycle: pull on session start, local commit on dispose ----
449
447
  ctx.on('session/event', ((session, event) => {
450
448
  if (event.type === 'agent/session-start') {
451
449
  void sync
452
450
  .pull()
453
451
  .catch(() => undefined)
454
- .finally(() => store.ensure().catch(() => undefined));
452
+ .finally(() => store.ensure().catch(() => undefined))
453
+ .then(() => {
454
+ // Boot replay: the observations JSONL is the durable distill queue.
455
+ // Whatever the previous exit skipped (local-only exit since the
456
+ // no-network exit commit) or a killed run left unmarked is still
457
+ // undistilled here. Empty pool and no-model are cheap no-ops inside
458
+ // the distiller; the per-session dedup guards double runs.
459
+ void store
460
+ .undistilledObservations(1)
461
+ .then((pending) => {
462
+ if (pending.length === 0)
463
+ return;
464
+ // Same trigger-time capture the observer callback does: the
465
+ // replay run's caller resolves candidates lazily and a fresh
466
+ // session has no prior capture to lean on.
467
+ try {
468
+ captureFromAgent(agents()?.get(session.id));
469
+ }
470
+ catch {
471
+ // contained — the run below fails with the readable
472
+ // no-adapter detail instead
473
+ }
474
+ const run = distiller.request(String(session.id), 'boot-replay');
475
+ if (run !== undefined)
476
+ void run.catch(() => undefined);
477
+ })
478
+ .catch(() => undefined);
479
+ });
455
480
  }
456
481
  }));
457
482
  ctx.effect(() => {
458
483
  void store.ensure().catch(() => undefined);
459
484
  // Async disposer: cordis awaits it during unload (Disposable may be
460
- // async), so the exit distill gets its bounded window before the
461
- // process goes away instead of always losing the exit race.
485
+ // async). The exit path is local-only — one git commit of the meta
486
+ // sidecars (observations / injection log / distill state), no pull, no
487
+ // push, no model call. The deferred push rides the next boot's pull
488
+ // (sync.pull replays it) and the skipped exit distill is replayed
489
+ // there too; both only ever needed the on-disk state, which the commit
490
+ // below makes durable before the process goes away.
462
491
  return () => {
463
492
  // A real session's session-end run (agent/disposed trigger, moments
464
493
  // earlier) may still be in flight — and the fake-'dispose' run would
465
494
  // feed the SAME global pool head to the model a second time (double
466
495
  // evaluation, double GC attempts). Skip the exit trigger while any
467
- // run is pending; the bounded wait below then simply has nothing
468
- // extra to await.
496
+ // run is pending; its capture is already write-through on disk.
469
497
  if (!distiller.hasAnyPending()) {
470
498
  observer.onSessionEvent('dispose', 'agent/disposed', undefined);
471
499
  }
472
500
  sync.dispose();
473
- // The exit flush (final meta commit + ledger flush) rides the SAME
474
- // bounded window as the exit distill — fire-and-forget here loses the
475
- // exit race that window was built to win.
476
- const exitFlush = sync.commitMeta().then(() => sync.flush()).catch(() => undefined);
477
- return settleBounded(exitDistill === undefined ? exitFlush : Promise.allSettled([exitDistill, exitFlush]), EXIT_DISTILL_TIMEOUT_MS);
501
+ const exitFlush = sync.commitMeta().catch(() => undefined);
502
+ return settleBounded(exitFlush, EXIT_COMMIT_TIMEOUT_MS);
478
503
  };
479
504
  }, 'topics: lifecycle');
480
505
  // ---- /topics command (optional peer) ----
package/lib/sync.d.ts CHANGED
@@ -32,7 +32,12 @@ export declare class Sync {
32
32
  private readonly urlFor;
33
33
  constructor(store: BundleStore, cfg: () => SyncConfig, token?: TokenResolver, urlFor?: UrlResolver);
34
34
  get active(): boolean;
35
- /** Session start (or `/topics sync pull`): rebase onto remote, mark conflicts. */
35
+ /**
36
+ * Session start (or `/topics sync pull`): rebase onto remote, mark
37
+ * conflicts, then replay any push the previous session's local-only exit
38
+ * deferred. The replay is best-effort — a failure just marks lastError and
39
+ * the debounced flush (or the next boot's pull) retries.
40
+ */
36
41
  pull(): Promise<{
37
42
  ok: boolean;
38
43
  conflicted: string[];
package/lib/sync.js CHANGED
@@ -35,7 +35,12 @@ export class Sync {
35
35
  get active() {
36
36
  return this.cfg().repo !== '';
37
37
  }
38
- /** Session start (or `/topics sync pull`): rebase onto remote, mark conflicts. */
38
+ /**
39
+ * Session start (or `/topics sync pull`): rebase onto remote, mark
40
+ * conflicts, then replay any push the previous session's local-only exit
41
+ * deferred. The replay is best-effort — a failure just marks lastError and
42
+ * the debounced flush (or the next boot's pull) retries.
43
+ */
39
44
  async pull() {
40
45
  if (!this.active)
41
46
  return { ok: true, conflicted: [], message: 'local-only 模式,无同步' };
@@ -52,6 +57,23 @@ export class Sync {
52
57
  const prior = await this.store.getConflicts();
53
58
  if (prior.size > 0)
54
59
  await this.store.setConflicts([]);
60
+ // Deferred-push replay: the previous exit committed locally only, so a
61
+ // successful rebase here is the moment the backlog lands on the remote.
62
+ try {
63
+ if ((await gitmod.unpushedCount(this.store.root)) > 0) {
64
+ const pushed = await gitmod.push(this.store.root, token);
65
+ if (pushed.ok) {
66
+ this.lastPushAt = new Date().toISOString();
67
+ this.lastError = '';
68
+ }
69
+ else {
70
+ this.lastError = pushed.output.slice(-500);
71
+ }
72
+ }
73
+ }
74
+ catch {
75
+ // pull must stay best-effort for the session-start caller
76
+ }
55
77
  return { ok: true, conflicted: [], message: '已与远端同步' };
56
78
  }
57
79
  const conflictedSlugs = outcome.conflicted.map((p) => p.replace(/^topics\//, '').replace(/\.md$/, ''));
@@ -59,7 +81,14 @@ export class Sync {
59
81
  await this.store.setConflicts(outcome.conflicted);
60
82
  }
61
83
  this.lastError = outcome.output.slice(-500);
62
- return { ok: false, conflicted: conflictedSlugs, message: `rebase 冲突:${conflictedSlugs.join('、') || '未知路径'}(已标记降权,待手工解决)` };
84
+ // A rebase can fail without producing conflicted paths (identity missing,
85
+ // fetch failure, corrupt state) — surface the git output tail instead of
86
+ // the misleading 「未知路径」 so a headless failure is diagnosable.
87
+ if (conflictedSlugs.length === 0) {
88
+ const tail = outcome.output.trim().split('\n').slice(-3).join(';').slice(-200);
89
+ return { ok: false, conflicted: [], message: `rebase 失败(无冲突文件):${tail !== '' ? tail : 'git 无输出'}` };
90
+ }
91
+ return { ok: false, conflicted: conflictedSlugs, message: `rebase 冲突:${conflictedSlugs.join('、')}(已标记降权,待手工解决)` };
63
92
  }
64
93
  /** Called after every write-through commit — schedules the debounced push. */
65
94
  schedulePush() {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@aiwayds/dsh-topics-memory",
3
- "version": "0.9.0",
4
- "description": "dsh-plugin: OKF v0.2 topic memory for dsh - local-first git-tracked bundle, hot-path LLM-free injection, two-stage observer with background distill",
3
+ "version": "0.10.0",
4
+ "description": "dsh-plugin: quality-first topic memory for dsh — a topic keeps the starting question, conclusion, impact and dependencies, not the process; local-first git-traced OKF bundle, budgeted LLM-free injection",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",
7
7
  "types": "lib/index.d.ts",
package/src/distill.ts CHANGED
@@ -161,8 +161,10 @@ export class Distiller {
161
161
  * caller can hook post-run cleanup — the per-session llm capture in
162
162
  * index.ts must live exactly as long as a run that may read it. `manual`
163
163
  * is the /topics distill trigger: same lane, same in-flight guard.
164
+ * `boot-replay` is the session-start backlog drain: a previous exit's
165
+ * skipped (or killed-mid-run) distill replays here.
164
166
  */
165
- request(sessionId: string, reason: 'every-n' | 'session-end' | 'manual'): Promise<DistillResult> | undefined {
167
+ request(sessionId: string, reason: 'every-n' | 'session-end' | 'manual' | 'boot-replay'): Promise<DistillResult> | undefined {
166
168
  if (!this.configured) return undefined
167
169
  if (this.inFlight.has(sessionId)) return undefined
168
170
  const run = this.run(sessionId).finally(() => this.inFlight.delete(sessionId))
package/src/git.ts CHANGED
@@ -152,10 +152,13 @@ export interface PullOutcome {
152
152
  * `pull --rebase` with conflict containment: on conflict we abort the rebase
153
153
  * (ADR 0003 — no automatic smart merging) and report the conflicted paths so
154
154
  * the caller can mark those topics. Remote and branch are explicit — the
155
- * bundle never depends on upstream tracking config.
155
+ * bundle never depends on upstream tracking config. IDENTITY rides along:
156
+ * a rebase REPLAYS local commits, which requires a committer identity, and
157
+ * a fresh machine (CI, new host) may have none — without this the pull
158
+ * fails with an empty conflicted list on every session start.
156
159
  */
157
160
  export async function pullRebase(cwd: string, token?: string, remote = 'origin', branch = 'main'): Promise<PullOutcome> {
158
- const r = await runGit(['pull', '--rebase', '--autostash', remote, branch], { cwd, token, mayFail: true, timeoutMs: 60_000 }).catch((e) => {
161
+ const r = await runGit([...IDENTITY, 'pull', '--rebase', '--autostash', remote, branch], { cwd, token, mayFail: true, timeoutMs: 60_000 }).catch((e) => {
159
162
  return { code: -1, stdout: '', stderr: String(e instanceof Error ? e.message : e) } satisfies GitResult
160
163
  })
161
164
  if (r.code === 0) return { ok: true, conflicted: [], output: r.stdout + r.stderr }
package/src/index.ts CHANGED
@@ -39,13 +39,13 @@ import { isDelegated } from './delegation.ts'
39
39
  export const name = 'dsh-topics-memory'
40
40
 
41
41
  /**
42
- * Bounded wait for the process-exit distill. Session teardown is the last
43
- * distill trigger, but the disposer used to fire it fire-and-forget — process
44
- * exit always won the race and the run died unwritten. The disposer now waits
45
- * for the run, capped so a pathological (hanging) model call can never wedge
46
- * the host's exit.
42
+ * Bounded wait for the process-exit meta commit. The exit path is
43
+ * local-only: no pull/push, no model call — the deferred push rides the
44
+ * next boot's pull and the skipped exit distill is replayed there. The
45
+ * window only bounds the local `git commit` against a pathological stall
46
+ * (a wedged index.lock, a hung filesystem), which should never fire.
47
47
  */
48
- export const EXIT_DISTILL_TIMEOUT_MS = 90_000
48
+ export const EXIT_COMMIT_TIMEOUT_MS = 10_000
49
49
 
50
50
  /**
51
51
  * Resolve when `p` settles (rejection swallowed) or after `ms`, whichever
@@ -368,10 +368,10 @@ export function apply(ctx: Context): void {
368
368
  const slowLane = new SlowLane(service, caller, releaseSessionLlm)
369
369
 
370
370
  // ---- Observer (M2) ----
371
- // The process-exit disposer awaits this run (bounded): the callback below
372
- // records it synchronously when the fake 'dispose' session's end trigger
373
- // fires, so the disposer can wait for the last distill to land.
374
- let exitDistill: Promise<DistillResult> | undefined
371
+ // Trigger runs are fire-and-forget everywhere now — including the exit
372
+ // one. Observations are write-through on disk, so a run the process exits
373
+ // under is never lost work: its input is still undistilled and the next
374
+ // boot's replay distills it.
375
375
  const observer = new Observer(service, (sessionId, reason) => {
376
376
  // Trigger-time capture: the agent is still registered here, so its scoped
377
377
  // ctx can hand over the llm instance whose adapters are live.
@@ -387,7 +387,6 @@ export function apply(ctx: Context): void {
387
387
  // and its own settle hook does the release.
388
388
  const run = distiller.request(sessionId, reason)
389
389
  if (run !== undefined) {
390
- if (sessionId === 'dispose') exitDistill = run
391
390
  void run.finally(() => releaseSessionLlm(sessionId)).catch(() => undefined)
392
391
  }
393
392
  })
@@ -508,13 +507,37 @@ export function apply(ctx: Context): void {
508
507
  }) as never)
509
508
  }
510
509
 
511
- // ---- Sync lifecycle: pull on session start, flush on dispose ----
510
+ // ---- Sync lifecycle: pull on session start, local commit on dispose ----
512
511
  ctx.on('session/event' as never, ((session: { id: unknown }, event: SessionEvent) => {
513
512
  if (event.type === 'agent/session-start') {
514
513
  void sync
515
514
  .pull()
516
515
  .catch(() => undefined)
517
516
  .finally(() => store.ensure().catch(() => undefined))
517
+ .then(() => {
518
+ // Boot replay: the observations JSONL is the durable distill queue.
519
+ // Whatever the previous exit skipped (local-only exit since the
520
+ // no-network exit commit) or a killed run left unmarked is still
521
+ // undistilled here. Empty pool and no-model are cheap no-ops inside
522
+ // the distiller; the per-session dedup guards double runs.
523
+ void store
524
+ .undistilledObservations(1)
525
+ .then((pending) => {
526
+ if (pending.length === 0) return
527
+ // Same trigger-time capture the observer callback does: the
528
+ // replay run's caller resolves candidates lazily and a fresh
529
+ // session has no prior capture to lean on.
530
+ try {
531
+ captureFromAgent(agents()?.get(session.id) as unknown)
532
+ } catch {
533
+ // contained — the run below fails with the readable
534
+ // no-adapter detail instead
535
+ }
536
+ const run = distiller.request(String(session.id), 'boot-replay')
537
+ if (run !== undefined) void run.catch(() => undefined)
538
+ })
539
+ .catch(() => undefined)
540
+ })
518
541
  }
519
542
  }) as never)
520
543
 
@@ -522,27 +545,24 @@ export function apply(ctx: Context): void {
522
545
  () => {
523
546
  void store.ensure().catch(() => undefined)
524
547
  // Async disposer: cordis awaits it during unload (Disposable may be
525
- // async), so the exit distill gets its bounded window before the
526
- // process goes away instead of always losing the exit race.
548
+ // async). The exit path is local-only — one git commit of the meta
549
+ // sidecars (observations / injection log / distill state), no pull, no
550
+ // push, no model call. The deferred push rides the next boot's pull
551
+ // (sync.pull replays it) and the skipped exit distill is replayed
552
+ // there too; both only ever needed the on-disk state, which the commit
553
+ // below makes durable before the process goes away.
527
554
  return () => {
528
555
  // A real session's session-end run (agent/disposed trigger, moments
529
556
  // earlier) may still be in flight — and the fake-'dispose' run would
530
557
  // feed the SAME global pool head to the model a second time (double
531
558
  // evaluation, double GC attempts). Skip the exit trigger while any
532
- // run is pending; the bounded wait below then simply has nothing
533
- // extra to await.
559
+ // run is pending; its capture is already write-through on disk.
534
560
  if (!distiller.hasAnyPending()) {
535
561
  observer.onSessionEvent('dispose', 'agent/disposed', undefined)
536
562
  }
537
563
  sync.dispose()
538
- // The exit flush (final meta commit + ledger flush) rides the SAME
539
- // bounded window as the exit distill — fire-and-forget here loses the
540
- // exit race that window was built to win.
541
- const exitFlush = sync.commitMeta().then(() => sync.flush()).catch(() => undefined)
542
- return settleBounded(
543
- exitDistill === undefined ? exitFlush : Promise.allSettled([exitDistill, exitFlush]),
544
- EXIT_DISTILL_TIMEOUT_MS,
545
- )
564
+ const exitFlush = sync.commitMeta().catch(() => undefined)
565
+ return settleBounded(exitFlush, EXIT_COMMIT_TIMEOUT_MS)
546
566
  }
547
567
  },
548
568
  'topics: lifecycle',
package/src/sync.ts CHANGED
@@ -51,7 +51,12 @@ export class Sync {
51
51
  return this.cfg().repo !== ''
52
52
  }
53
53
 
54
- /** Session start (or `/topics sync pull`): rebase onto remote, mark conflicts. */
54
+ /**
55
+ * Session start (or `/topics sync pull`): rebase onto remote, mark
56
+ * conflicts, then replay any push the previous session's local-only exit
57
+ * deferred. The replay is best-effort — a failure just marks lastError and
58
+ * the debounced flush (or the next boot's pull) retries.
59
+ */
55
60
  async pull(): Promise<{ ok: boolean; conflicted: string[]; message: string }> {
56
61
  if (!this.active) return { ok: true, conflicted: [], message: 'local-only 模式,无同步' }
57
62
  const token = await this.safeToken()
@@ -66,6 +71,21 @@ export class Sync {
66
71
  // A successful rebase clears previous conflict marks.
67
72
  const prior = await this.store.getConflicts()
68
73
  if (prior.size > 0) await this.store.setConflicts([])
74
+ // Deferred-push replay: the previous exit committed locally only, so a
75
+ // successful rebase here is the moment the backlog lands on the remote.
76
+ try {
77
+ if ((await gitmod.unpushedCount(this.store.root)) > 0) {
78
+ const pushed = await gitmod.push(this.store.root, token)
79
+ if (pushed.ok) {
80
+ this.lastPushAt = new Date().toISOString()
81
+ this.lastError = ''
82
+ } else {
83
+ this.lastError = pushed.output.slice(-500)
84
+ }
85
+ }
86
+ } catch {
87
+ // pull must stay best-effort for the session-start caller
88
+ }
69
89
  return { ok: true, conflicted: [], message: '已与远端同步' }
70
90
  }
71
91
  const conflictedSlugs = outcome.conflicted.map((p) => p.replace(/^topics\//, '').replace(/\.md$/, ''))
@@ -73,7 +93,14 @@ export class Sync {
73
93
  await this.store.setConflicts(outcome.conflicted)
74
94
  }
75
95
  this.lastError = outcome.output.slice(-500)
76
- return { ok: false, conflicted: conflictedSlugs, message: `rebase 冲突:${conflictedSlugs.join('、') || '未知路径'}(已标记降权,待手工解决)` }
96
+ // A rebase can fail without producing conflicted paths (identity missing,
97
+ // fetch failure, corrupt state) — surface the git output tail instead of
98
+ // the misleading 「未知路径」 so a headless failure is diagnosable.
99
+ if (conflictedSlugs.length === 0) {
100
+ const tail = outcome.output.trim().split('\n').slice(-3).join(';').slice(-200)
101
+ return { ok: false, conflicted: [], message: `rebase 失败(无冲突文件):${tail !== '' ? tail : 'git 无输出'}` }
102
+ }
103
+ return { ok: false, conflicted: conflictedSlugs, message: `rebase 冲突:${conflictedSlugs.join('、')}(已标记降权,待手工解决)` }
77
104
  }
78
105
 
79
106
  /** Called after every write-through commit — schedules the debounced push. */