@gooftoken/dsh-rewind 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +29 -2
- package/assets/icon.svg +6 -0
- package/client.js +1549 -0
- package/cordis.patch.yml +4 -0
- package/docs/DETAIL.md +153 -0
- package/lib/command.d.ts +48 -0
- package/lib/compaction.d.ts +105 -0
- package/lib/config.d.ts +22 -0
- package/lib/git/provider.d.ts +26 -0
- package/lib/git/runner.d.ts +45 -0
- package/lib/git/snapshot.d.ts +60 -0
- package/lib/git/workspace.d.ts +54 -0
- package/lib/index.d.ts +18 -0
- package/lib/index.js +2631 -0
- package/lib/paths.d.ts +16 -0
- package/lib/recorder.d.ts +50 -0
- package/lib/replay.d.ts +72 -0
- package/lib/store.d.ts +34 -0
- package/lib/surface.d.ts +74 -0
- package/lib/types.d.ts +120 -0
- package/lib/undo.d.ts +38 -0
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +92 -4
package/cordis.patch.yml
ADDED
package/docs/DETAIL.md
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
# 回退与压缩 / Rewind & compaction
|
|
2
|
+
|
|
3
|
+
在 Web GUI 会话里输入 `/rewind`,或在会话中双击 Esc,打开面板。面板底部是横向操作条,用左右方向键或点击切换:
|
|
4
|
+
|
|
5
|
+
**文件与对话 · 对话 · 文件 · 压缩此后 · 压缩此前**
|
|
6
|
+
|
|
7
|
+
如果最近一次回退仍可撤销,页脚左侧还会显示「撤销上次回退」按钮。上下方向键选择轮次,Enter 执行。三种回退模式:
|
|
8
|
+
|
|
9
|
+
- **文件**:每个轮次开始时,插件用私有 Git 对象库对仓库工作树做一次快照(不改动仓库自己的 index、objects、refs 和提交历史)。恢复时重新读取当前工作树,与快照做 `diff-tree`,把差异路径精确写回或删除。
|
|
10
|
+
- **对话**:在当前会话内把模型可见的对话面收回到该轮之前(会话日志只追加,不删历史)。尾部由一个空的 `developer/message` 切点替换(不产生模型消息,也不写回退提示/marker),所以模型前缀就是该轮之前的内容。同一 Session、同一 Agent 继续工作。
|
|
11
|
+
- **压缩此后 / 压缩此前**:用当前路由模型把选中区间压成一条摘要,并按 DSH 的标准压缩事务写入:`compaction/start` → `compaction/summary` → 替换型 `user/message`(`surfaceOp: replace`,source 为 `compact-checkpoint`)→ `compaction/end`,`turn` 为 `null`。模型看到的该段历史被摘要替换;转录在真正的压缩落点显示 DSH 原生的可展开「上下文已压缩」标记(压缩此前落在目标轮开始处,压缩此后落在该轮开始处并同时收起其后的转录,使可见历史与模型上下文一样结束在摘要上)。`/rewind` 命令的结果文本、JSON 和内部 list 调用不再出现在转录里。
|
|
12
|
+
|
|
13
|
+
「压缩此后」把**选中轮次及之后**的可见对话压成摘要,之前的原文保留;「压缩此前」把**该轮之前**的可见对话压成摘要,该轮及之后的原文保留。轮次边界天然是工具调用/结果的完整边界。
|
|
14
|
+
|
|
15
|
+
**无文件变更时隐藏文件模式**:该轮没有可用检查点,或当前工作树已经与检查点树一致时,操作条只保留「对话 / 压缩此后 / 压缩此前」;「压缩此前」在选中轮次之前没有可见内容时也会隐藏。
|
|
16
|
+
|
|
17
|
+
**压缩之前轮次的对话回退**:同一会话无法撤销 surface 替换,因此对已被压缩覆盖的轮次,插件会重建压缩前的消息:**第一条重建的 user 消息**(新副本)用 `replace` 替换压缩后的整段尾部,其余重建消息按顺序 append 在它后面。这样 surface 直接变成 `[公共前缀, 重建消息...]`,没有额外的回退标记;第一条消息的来源标成 `rewind-replay`(仅本地标识,模型只看到原文),客户端据此隐藏旧尾部,并按 `rewind-replay-*` 消息 id 隐藏重建副本。assistant 消息和切点必须落在打开的 turn/step 内,因此重建副本写在一段带 `rewindReplay` 标记的内部 turn 里,并补齐配对的 `tool/call`,否则日志会在下次加载时报 `assistant/message does not match an open turn and step`;写入后同时把宿主的 turn 游标推进到该内部 turn,下一轮真实 turn 从其后编号,否则会复用编号、重载时报 `turn/start does not open the expected turn`。面板不会列出这段内部 turn。若第一条重建节点不是 user(例如目标轮之前没有消息,或首条缺失的是 assistant 回复),则回退为空的 `developer/message` 切点(来源 `rewind-cut`,不产生模型消息),由它 replace 旧尾部,所有重建消息再 append。模型与转录都回到压缩前,不新建会话,后台任务不变。副本会占用日志空间,并重写该段 prompt 前缀(后续请求需要重建缓存)。
|
|
18
|
+
|
|
19
|
+
**撤销回退**:`/rewind undo`(或面板底部的「撤销上次回退」)把最近一次成功回退的文件和对话恢复到回退之前,仍在同一会话内。只支持一级,且要求回退之后没有任何进展:插件保存回退完成时的完整 surface 哈希和回退后工作树字段,undo 时两者都必须仍一致;发过新消息、跑过压缩、手改过文件都会拒绝,报「回退之后会话已变化」。撤销写入一个空的 `developer/message`(来源 `rewind-undo`,不产生模型消息)替换回退产生的表面节点,并把被回退遮掉的消息副本追加在同一段带 `rewindReplay` 标记的内部 turn 里(副本按需补齐 `tool/call`,并同步推进宿主 turn 游标),所以模型前缀回到回退前,日志仍可重载。undo 记录保存在插件存储(`~/.dsh/rewind`)里,成功后清空;没有 redo,输入框草稿不变。回退后先改过工作树再 undo 会被拒绝,避免把回退后的编辑一起抹掉;文件回退若有跳过的 gitlink 路径,工作树可能与记录字段不同,undo 同样会拒绝。
|
|
20
|
+
|
|
21
|
+
**转录行为**
|
|
22
|
+
|
|
23
|
+
- 文件/对话回退成功后约 4.5 秒浮层确认,失败约 7 秒错误浮层;错误不再回显成会话行(`--dry-run` 的预览行仍保留)。
|
|
24
|
+
- 被回退隐藏的消息仍完整保留在会话日志与 API/审计数据里,只是不再出现在 Chat 转录和模型上下文中;压缩前重放产生的副本同样要隐藏:旧尾部由第一条重建消息的范围隐藏,副本由 `rewind-replay-*` 消息 id 隐藏。旧版本写入的 `rewind-checkpoint` 提示会在下一次回退重建前缀时从重建列表里剔除,新回退不再产生。压缩的摘要标记落在压缩边界,展开可看摘要;「压缩此后」还会把被收起的尾部从转录中隐藏。
|
|
25
|
+
- 面板会用一次内部的 `/rewind list --json --brief` 刷新每轮的文件变更状态,该命令行在转录里自动隐藏。
|
|
26
|
+
- 对话/文件与对话回退成功后,目标轮第一条用户提示的文本会放回输入框(替换现有草稿),方便编辑后重新发送;图片和文件附件不会重建。
|
|
27
|
+
|
|
28
|
+
**保证**
|
|
29
|
+
|
|
30
|
+
1. **不打断后台任务**:插件从不取消运行中的轮次、jobs、子代理或进程;前台轮次未结束时只拒绝操作,由你决定是否停止。后台任务继续在原会话运行,结果作为新轮次追加。
|
|
31
|
+
2. **普通回退不丢 prompt 缓存**:对话回退只追加一个空的 `developer/message` 切点(不产生模型消息),既有请求前缀逐字节不变;压缩前的重放会改写该段前缀,需要重建缓存(见上一节)。
|
|
32
|
+
3. **压缩会改写被替换的那段**:摘要替换必然改变该段前缀,后续请求需要重建这段缓存;其余历史、同一 Session/Agent 与后台任务不受影响。压缩同样以空闲维护任务运行,不会打断后台任务。
|
|
33
|
+
|
|
34
|
+
## 命令
|
|
35
|
+
|
|
36
|
+
```text
|
|
37
|
+
/rewind 用法说明
|
|
38
|
+
/rewind list [--json] 列出可回退的轮次、文件状态与是否已被回退/压缩
|
|
39
|
+
/rewind apply <turn> [--files] [--conversation] [--compact-before|--compact-after] [--dry-run] [--json]
|
|
40
|
+
/rewind undo [--dry-run] [--json] 撤销最近一次成功回退
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
- 不带模式时默认同时回退文件和对话;`--files`、`--conversation` 只做其中一项;`--compact-before` / `--compact-after` 做压缩,不能与文件/对话模式同时使用。
|
|
44
|
+
- `--dry-run` 只预览:文件/对话模式不写入、不替换;压缩模式不调用模型,只报告区间条目数与估算 tokens(有 token meter 时);undo 只校验并报告将恢复的内容。
|
|
45
|
+
- 已被更早回退隐藏的轮次不会出现在面板里,宿主也拒绝把对话/压缩再次落到这些轮次上(`--files` 仍可用)。
|
|
46
|
+
- 已被更早压缩覆盖的轮次仍显示在面板里:文件原地恢复;「对话 / 文件与对话」会在原会话重放压缩前的历史(不新建会话);压缩模式不提供。
|
|
47
|
+
- `undo` 撤销最近一次成功回退;只支持一级,回退之后有任何新进展(新轮次、压缩、surface 变化、工作树改动)都会拒绝。成功后记录清空,不能 redo。
|
|
48
|
+
- 会话正在跑(前台一轮未结束)时拒绝执行,不会替你中断。
|
|
49
|
+
- 子代理会话不可回退/压缩。
|
|
50
|
+
|
|
51
|
+
## 压缩的前提与失败
|
|
52
|
+
|
|
53
|
+
- 目标轮次必须已结束,Agent 空闲,且没有另一个压缩正在进行(共享的 `compaction/start` 锁)。
|
|
54
|
+
- 需要当前会话已经路由过一次模型(或 Agent 配置了 provider/model),并且宿主挂载了 token meter;否则命令返回「压缩不可用」。
|
|
55
|
+
- 摘要调用复用会话的 system prompt、tools 前缀与路由,输出上限 8192 tokens;期间由维护任务挡住新的唤醒输入。
|
|
56
|
+
- 摘要失败、被取消或区间在等待期间变化时,事务会写入带 `error` 的 `compaction/end`,模型可见对话面保持不变,错误通过浮层/命令结果报告。
|
|
57
|
+
|
|
58
|
+
## 文件回退的范围
|
|
59
|
+
|
|
60
|
+
- 覆盖仓库根目录下的整棵工作树:tracked 文件、未忽略的 untracked 文件,包括通过 bash、子代理或手动方式产生的改动。
|
|
61
|
+
- 不覆盖:`.gitignore` 忽略的文件、仓库外的文件、子模块(gitlink)内部。
|
|
62
|
+
- 恢复的是该轮**开始时**的整棵工作树,因此检查点之后的手动或 shell 改动也会被还原;面板中会说明这一点。正在运行的任务此后写入的文件同样会被覆盖。
|
|
63
|
+
- 只还原工作树,不移动 HEAD、不改 index、不删除提交。
|
|
64
|
+
- 需要本机可用的 `git`,且会话工作目录位于 Git 仓库内;否则文件回退不可用,对话/压缩照常。
|
|
65
|
+
- 面板没有逐文件预览;失败时命令结果会列出路径样本,对话回退不会在文件失败后继续。
|
|
66
|
+
|
|
67
|
+
## 配置
|
|
68
|
+
|
|
69
|
+
| 字段 | 默认值 | 含义 |
|
|
70
|
+
|---|---|---|
|
|
71
|
+
| `enabled` | `true` | 是否启用插件 |
|
|
72
|
+
| `storageDir` | `~/.dsh/rewind` | 检查点与会话索引的本地目录 |
|
|
73
|
+
| `timeoutMs` | `30000` | 单条 git 命令超时 |
|
|
74
|
+
| `outputMaxBytes` | `8388608` | 单条 git 命令保留的输出上限 |
|
|
75
|
+
| `maxPathsPerRestore` | `1000` | 一次文件回退允许的最大路径数,超出则拒绝 |
|
|
76
|
+
| `retentionDays` | `30` | 会话索引保留天数;过期索引与无引用的对象目录会被清理 |
|
|
77
|
+
|
|
78
|
+
## 限制
|
|
79
|
+
|
|
80
|
+
- 打开面板会对每个检查点做一次当前树比对(每个仓库一次工作树快照),仓库很大时会有短暂延迟;比对失败时文件模式仍显示,由执行阶段报告具体错误。
|
|
81
|
+
- 压缩会消耗一次模型调用,并可能耗时数十秒;关闭面板不会中止已经开始的操作,结束后仍会显示浮层。
|
|
82
|
+
- 与 `dsh-workspace-changes` 同时启用时,每轮会各做一次 Git 快照;若不需要变更卡片可关闭它以减少开销。
|
|
83
|
+
- 转录隐藏是客户端展示层的收缩(复用对话定义的能力),不是删除日志:回退隐藏旧尾部;「压缩此后」把被摘要的尾部一起收起,「压缩此前」保留原文。左侧轮次导轨由 Host 的 `turnOutline` 投影驱动,仍会列出回退或压缩前的轮次,以及内部重放 turn。
|
|
84
|
+
- undo 只支持一级且必须在会话继续前进之前使用;undo 本身不可撤销。撤销会追加消息副本、重写该段 prompt 前缀(下一次请求需重建缓存),并占用日志空间。未保存草稿、后台任务和子代理不会被改动。
|
|
85
|
+
- 与 `dsh-claude-style` 或市场上的 `dsh-rewind` 同时启用时各自记录,互不感知。
|
|
86
|
+
|
|
87
|
+
---
|
|
88
|
+
|
|
89
|
+
## English
|
|
90
|
+
|
|
91
|
+
Type `/rewind` in a Web GUI session, or double-press Escape, to open the picker. A horizontal action bar switches with the left/right arrows or a click:
|
|
92
|
+
|
|
93
|
+
**Files & conversation · Conversation · Files · Compact after · Compact before**
|
|
94
|
+
|
|
95
|
+
When the last rewind can still be undone, an **Undo last rewind** button appears on the left of the footer. Up/down select a turn; Enter applies. Three kinds of action:
|
|
96
|
+
|
|
97
|
+
- **Files**: each turn start snapshots the repository work tree into a private Git object store (the repository's index, objects, refs, and commits are never touched). A restore re-reads the current work tree, diffs it against the checkpoint tree, and writes or deletes the changed paths.
|
|
98
|
+
- **Conversation**: a surface replacement returns the model-visible conversation to before that turn inside the same session. The tail is replaced by an empty `developer/message` cut node (no model message, no rewind marker), so the model prefix is exactly the pre-turn prefix. The same Session and Agent keep working.
|
|
99
|
+
- **Compact after / Compact before**: the routed model condenses the selected span into one summary, written as a standard DSH compaction transaction: `compaction/start` → `compaction/summary` → a replacement `user/message` (`surfaceOp: replace`, source `compact-checkpoint`) → `compaction/end`, with `turn: null`. The model sees the summary instead of that span; the transcript shows DSH's native expandable "context compacted" marker at the real compaction boundary (the target turn's start for both modes; "compact after" also collapses the summarized tail so the visible history ends at the summary like the model context). `/rewind` result text, JSON, and internal list calls no longer appear in the transcript.
|
|
100
|
+
|
|
101
|
+
**Compact after** summarizes the selected turn and everything after it, keeping the earlier conversation verbatim; **Compact before** summarizes everything before it, keeping that turn and everything after verbatim. Turn boundaries are complete tool-call/result boundaries.
|
|
102
|
+
|
|
103
|
+
**File modes hide themselves when there is nothing to restore**: no usable checkpoint, or the work tree already matches the checkpoint tree. "Compact before" is also hidden when the selected turn has no earlier visible content.
|
|
104
|
+
|
|
105
|
+
**Conversation rewind before a compaction**: the surface fold cannot un-shadow a replaced range, so for a turn a compaction already covered the plugin rebuilds the pre-compaction messages. The first rebuilt user message replaces the compacted tail, and the remaining rebuilt messages are appended after it; no surface marker is written. (An ordinary rewind likewise uses an empty `developer/message` cut node with no model message.) If the first rebuilt node is not a user message (nothing before the target, or the first missing node is an assistant), an empty `developer/message` cut node (`rewind-cut`, no model message) replaces the tail and every rebuilt message is appended after it. The head carries a local `rewind-replay`/`rewind-cut` source tag (the model sees the original text only), the transcript hides the old tail from that tag's range, and the fresh copies are hidden by their `rewind-replay-*` message ids. The model and transcript return to the pre-compaction state without creating a session; background work is untouched. The copies occupy log space (assistant stream evidence is not duplicated) and rewrite that part of the prompt prefix, so the next request rebuilds it.
|
|
106
|
+
|
|
107
|
+
**Undo**: `/rewind undo`, or the picker's **Undo last rewind** button, restores the files and conversation of the last successful rewind in the same session. It is one level and requires that nothing moved since: the plugin stores the complete surface hash and the work tree the rewind left behind, and both must still match. A new turn, a compaction, or a manual file edit refuses the undo. Undo writes one empty `developer/message` (`rewind-undo`, no model message) that replaces the rewind's surface nodes, and appends copies of the messages the rewind shadowed inside one tagged internal turn (copied assistants carry matching `tool/call` records), so the model prefix returns to its pre-rewind state and the log still reloads. The record lives in the plugin store and is cleared after success; there is no redo, and the composer draft is untouched. A work-tree edit after the rewind is refused rather than overwritten; a file rewind that skipped gitlink paths may leave the tree unequal to the recorded post-rewind tree, which also blocks undo.
|
|
108
|
+
|
|
109
|
+
**Transcript behavior**
|
|
110
|
+
|
|
111
|
+
- A file/conversation rewind confirms with a ~4.5s toast; failures use a ~7s error toast instead of an echoed command row (`--dry-run` preview rows stay).
|
|
112
|
+
- Messages hidden by a rewind stay in the session log and API/audit data; they leave the Chat transcript and the model context. Replay copies are hidden by their message ids and the old tail by the first rebuilt node's range. Old `rewind-checkpoint` markers from earlier versions are dropped when a rewind next rebuilds the prefix; new rewinds no longer write them. Compaction itself is model-context-only: it does not hide transcript history, and its marker stays and expands to the summary.
|
|
113
|
+
- The picker refreshes per-turn file state with one internal `/rewind list --json --brief`; that command row is suppressed in the transcript.
|
|
114
|
+
|
|
115
|
+
**Guarantees**
|
|
116
|
+
|
|
117
|
+
1. **Background work is not interrupted**: running turns, jobs, subagents, and processes are never cancelled. An action is refused while a foreground turn runs; background work keeps running and its results arrive as new turns.
|
|
118
|
+
2. **Plain rewinds preserve the prompt cache**: a conversation rewind that does not replay a compacted prefix appends only an empty `developer/message` cut node, which produces no model message, so the request prefix is unchanged. The pre-compaction replay rewrites that prefix and rebuilds it; it adds no marker message of its own.
|
|
119
|
+
3. **Compaction rewrites the replaced span**: the summary necessarily changes that part of the prefix, so the next request rebuilds that part of the cache; the rest of the history, the same Session/Agent, and background work are unaffected. Compaction runs as an idle maintenance task too.
|
|
120
|
+
|
|
121
|
+
**Command**
|
|
122
|
+
|
|
123
|
+
```text
|
|
124
|
+
/rewind list [--json]
|
|
125
|
+
/rewind apply <turn> [--files] [--conversation] [--compact-before|--compact-after] [--dry-run] [--json]
|
|
126
|
+
/rewind undo [--dry-run] [--json]
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
- With no mode, both files and conversation are rewound. `--compact-before` / `--compact-after` cannot be combined with a file or conversation mode.
|
|
130
|
+
- `--dry-run` previews only: no file writes and no surface replacement; a compaction dry run reports the item count and estimated tokens (when a meter is mounted) without calling the model; an undo dry run validates and reports what it would restore.
|
|
131
|
+
- Turns already hidden by an earlier rewind are omitted from the picker and refused for conversation/compaction; `--files` still works on them.
|
|
132
|
+
- Turns already shadowed by an earlier compaction stay in the picker: files restore in place, while conversation / files & conversation replay the pre-compaction history into this session (compaction modes are not offered).
|
|
133
|
+
- `undo` reverses the last successful rewind, one level only. It is refused after any later progress (a new turn, a compaction, a surface change, or a work-tree edit), and the record is cleared on success; there is no redo.
|
|
134
|
+
- Rewind is refused while a foreground turn runs; subagent sessions are not supported.
|
|
135
|
+
|
|
136
|
+
**Compaction preconditions**
|
|
137
|
+
|
|
138
|
+
- The target turn ended, the Agent is idle, and no other compaction holds the shared `compaction/start` lock.
|
|
139
|
+
- A routed provider/model exists (a previous turn was routed, or AgentOptions supplies both) and the Host mounts a token meter; otherwise the command reports compaction as unavailable.
|
|
140
|
+
- The summary call replays the session's system prompt and tools prefix with the routed model (8192 output-token cap); maintenance withholds new waking input while it runs.
|
|
141
|
+
- On a summary failure, cancellation, or a changed span, the transaction writes `compaction/end` with `error` and leaves the model-visible surface untouched; the error is reported through the toast and command result.
|
|
142
|
+
|
|
143
|
+
**File scope**
|
|
144
|
+
|
|
145
|
+
- The whole work tree under the repository root, including tracked files, unignored untracked files, and changes made through bash, subagents, or by hand.
|
|
146
|
+
- Not covered: ignored files, files outside the repository, submodule contents.
|
|
147
|
+
- Restoring a turn restores the work tree at that turn start, so later shell or manual edits (and files written by running jobs) are reverted too; the picker states this. HEAD, the index, and commits are untouched.
|
|
148
|
+
- Requires a usable `git` and a session working directory inside a repository; conversation rewind and compaction still work without them.
|
|
149
|
+
- There is no per-file preview; failures list path samples and the conversation is not rewound after a file failure.
|
|
150
|
+
|
|
151
|
+
**Configuration**: `storageDir=~/.dsh/rewind`, `timeoutMs=30000`, `outputMaxBytes=8388608`, `maxPathsPerRestore=1000`, `retentionDays=30`.
|
|
152
|
+
|
|
153
|
+
**Limits**: opening the picker compares the current tree for every checkpoint (one work-tree snapshot per repository), which can take a moment in very large repositories; if the comparison fails the file modes stay visible and the apply reports the concrete error. Compaction costs one model call and may take tens of seconds; closing the picker does not abort a started action, and its toast still appears. Transcript hiding is a rewind-only client-side view contraction, not log deletion; the left turn rail is driven by the Host `turnOutline` projection and still lists pre-rewind/pre-compaction turns, including the internal replay turn. Undo is one level, must be used before the session moves on, and is not itself undoable; it appends message copies and rewrites that part of the prompt prefix. Running alongside `dsh-workspace-changes` doubles the per-turn Git snapshot work; running alongside `dsh-claude-style` or the market `dsh-rewind` records checkpoints independently.
|
package/lib/command.d.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `/rewind`: list checkpoints and apply a file, conversation, or compaction
|
|
3
|
+
* change inside one idle maintenance task.
|
|
4
|
+
*/
|
|
5
|
+
import type { CommandInvocation, CommandResult } from '@deepseek-ai/dsh-commands';
|
|
6
|
+
import type { SessionEvent } from '@deepseek-ai/dsh-session';
|
|
7
|
+
import type { LlmLike, TokenMeterLike } from './compaction.js';
|
|
8
|
+
import type { Config } from './config.js';
|
|
9
|
+
import type { Git } from './git/runner.js';
|
|
10
|
+
import type { WorkspaceResolver } from './git/workspace.js';
|
|
11
|
+
import type { CheckpointStore } from './store.js';
|
|
12
|
+
import type { EventsObservation, LoggerLike, TurnFacts } from './types.js';
|
|
13
|
+
/** One rewind intent the picker and grammar share. */
|
|
14
|
+
export type ApplyMode = 'both' | 'files' | 'conversation' | 'compact-before' | 'compact-after';
|
|
15
|
+
/** Host dependencies the command needs. */
|
|
16
|
+
export interface RewindCommandDeps {
|
|
17
|
+
config: Config;
|
|
18
|
+
store: CheckpointStore;
|
|
19
|
+
workspaces: WorkspaceResolver;
|
|
20
|
+
git: () => Promise<Git | null>;
|
|
21
|
+
llm: () => LlmLike | undefined;
|
|
22
|
+
tokenMeter: () => TokenMeterLike | undefined;
|
|
23
|
+
observe(sessionId: string): Promise<EventsObservation>;
|
|
24
|
+
logger: LoggerLike;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Fold turn boundaries, start times, and first human prompt previews.
|
|
28
|
+
* @param events - complete session log.
|
|
29
|
+
* @returns turns in ascending order.
|
|
30
|
+
*/
|
|
31
|
+
export declare function deriveTurns(events: readonly SessionEvent[]): TurnFacts[];
|
|
32
|
+
/**
|
|
33
|
+
* The user's original prompt for one turn, used to repopulate the composer
|
|
34
|
+
* after a conversation rewind. Markers, compaction summaries, and agent
|
|
35
|
+
* instructions are not prompts.
|
|
36
|
+
* @param events - complete session log.
|
|
37
|
+
* @param turn - host-assigned target turn.
|
|
38
|
+
* @returns the prompt text, or undefined when the turn has none.
|
|
39
|
+
*/
|
|
40
|
+
export declare function targetPrompt(events: readonly SessionEvent[], turn: number): string | undefined;
|
|
41
|
+
/**
|
|
42
|
+
* Execute one `/rewind` invocation. Expected failures settle as error results;
|
|
43
|
+
* an occupied agent is refused without cancelling the running turn.
|
|
44
|
+
* @param deps - host dependencies.
|
|
45
|
+
* @param invocation - command invocation from the registry.
|
|
46
|
+
* @returns settled human or machine-readable result.
|
|
47
|
+
*/
|
|
48
|
+
export declare function runRewindCommand(deps: RewindCommandDeps, invocation: CommandInvocation): Promise<CommandResult>;
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import type { CompactionId } from '@deepseek-ai/dsh-compaction';
|
|
2
|
+
import type { ContentBlock, GenerateOptions, StreamChunk } from '@deepseek-ai/dsh-llm/types';
|
|
3
|
+
import type { Session, SessionEvent, SessionSeq } from '@deepseek-ai/dsh-session';
|
|
4
|
+
import type { CommandId } from '@deepseek-ai/dsh-commands/brand';
|
|
5
|
+
declare module '@deepseek-ai/dsh-llm/types' {
|
|
6
|
+
interface LlmRequestPurposeMap {
|
|
7
|
+
/** One compaction checkpoint summary. */
|
|
8
|
+
compaction: true;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
declare module '@deepseek-ai/dsh-llm/message' {
|
|
12
|
+
interface MessageSourceMap {
|
|
13
|
+
/** The replacement user message a compaction wrote. */
|
|
14
|
+
'compact-checkpoint': {
|
|
15
|
+
kind: 'compact-checkpoint';
|
|
16
|
+
compactionId: CompactionId;
|
|
17
|
+
sourceCommandId?: CommandId;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
/** Which side of the selected turn the summary replaces. */
|
|
22
|
+
export type CompactionDirection = 'before' | 'after';
|
|
23
|
+
/** Minimal LLM seam the summarizer needs. */
|
|
24
|
+
export interface LlmLike {
|
|
25
|
+
stream(options: GenerateOptions): AsyncIterable<StreamChunk>;
|
|
26
|
+
}
|
|
27
|
+
/** Minimal token-meter seam used for the compaction shadow price. */
|
|
28
|
+
export interface TokenMeterLike {
|
|
29
|
+
measure(session: Session): {
|
|
30
|
+
readonly nodes: readonly {
|
|
31
|
+
readonly seq: SessionSeq;
|
|
32
|
+
readonly tokens: number;
|
|
33
|
+
readonly heuristicTokens: number;
|
|
34
|
+
}[];
|
|
35
|
+
};
|
|
36
|
+
estimateMessage(message: {
|
|
37
|
+
readonly content: readonly ContentBlock[];
|
|
38
|
+
}): number;
|
|
39
|
+
}
|
|
40
|
+
/** Agent fields the summarizer reads without depending on the agent package. */
|
|
41
|
+
export interface CompactionAgentLike {
|
|
42
|
+
readonly session: Session;
|
|
43
|
+
readonly options?: {
|
|
44
|
+
readonly provider?: string;
|
|
45
|
+
readonly model?: string;
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
/** One validated span of current surface positions to replace. */
|
|
49
|
+
export interface CompactionPlan {
|
|
50
|
+
/** Target turn the user selected. */
|
|
51
|
+
readonly turn: number;
|
|
52
|
+
/** Which side of the turn is shadowed. */
|
|
53
|
+
readonly direction: CompactionDirection;
|
|
54
|
+
/** First shadowed surface node, inclusive. */
|
|
55
|
+
readonly start: SessionSeq;
|
|
56
|
+
/** Last shadowed surface node, inclusive. */
|
|
57
|
+
readonly end: SessionSeq;
|
|
58
|
+
/** Every shadowed surface node, in surface order. */
|
|
59
|
+
readonly shadowed: readonly SessionSeq[];
|
|
60
|
+
}
|
|
61
|
+
/** Facts one committed compaction returns to the command. */
|
|
62
|
+
export interface CompactionOutcome {
|
|
63
|
+
readonly compactionId: string;
|
|
64
|
+
readonly summarySeq: SessionSeq;
|
|
65
|
+
readonly shadowedItemCount: number;
|
|
66
|
+
readonly shadowedTokenCount: number;
|
|
67
|
+
readonly summaryTokenCount: number;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Whether an earlier compaction already shadowed a turn's prompt.
|
|
71
|
+
* @param events - complete session log.
|
|
72
|
+
* @param turn - host-assigned turn to test.
|
|
73
|
+
* @returns true when the turn is no longer part of the visible surface.
|
|
74
|
+
*/
|
|
75
|
+
export declare function hiddenByCompaction(events: readonly SessionEvent[], turn: number): boolean;
|
|
76
|
+
/**
|
|
77
|
+
* Turn-level summary markers: newest compaction per turn boundary. Used by the
|
|
78
|
+
* list command to omit turns an earlier compaction already condensed.
|
|
79
|
+
* @param events - complete session log.
|
|
80
|
+
* @returns set of turns whose first prompt is shadowed.
|
|
81
|
+
*/
|
|
82
|
+
export declare function compactedTurns(events: readonly SessionEvent[]): Set<number>;
|
|
83
|
+
/**
|
|
84
|
+
* Plan the surface span one compaction mode replaces.
|
|
85
|
+
* @param session - live session whose current surface is read.
|
|
86
|
+
* @param events - complete session log at the same cut.
|
|
87
|
+
* @param turn - target turn.
|
|
88
|
+
* @param direction - summarize before or after the turn.
|
|
89
|
+
* @returns the span, or null when the selected side is empty.
|
|
90
|
+
*/
|
|
91
|
+
export declare function planCompaction(session: Session, events: readonly SessionEvent[], turn: number, direction: CompactionDirection): CompactionPlan | null;
|
|
92
|
+
/**
|
|
93
|
+
* Run one idle-range compaction transaction.
|
|
94
|
+
* @param deps - LLM and token-meter seams resolved from the Host composition.
|
|
95
|
+
* @param session - live session whose surface is replaced.
|
|
96
|
+
* @param events - log used to build the summarization prefix.
|
|
97
|
+
* @param plan - validated span to replace.
|
|
98
|
+
* @param agent - routing options and session owner.
|
|
99
|
+
* @param signal - cancellation for the request.
|
|
100
|
+
* @returns committed compaction facts.
|
|
101
|
+
*/
|
|
102
|
+
export declare function compactPlan(deps: {
|
|
103
|
+
readonly llm: LlmLike;
|
|
104
|
+
readonly meter: TokenMeterLike;
|
|
105
|
+
}, session: Session, events: readonly SessionEvent[], plan: CompactionPlan, agent: CompactionAgentLike, signal: AbortSignal): Promise<CompactionOutcome>;
|
package/lib/config.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import z from '@deepseek-ai/schemastery';
|
|
2
|
+
/** Default directory holding session checkpoint indexes and Git object stores. */
|
|
3
|
+
export declare const DEFAULT_STORAGE_DIR: string;
|
|
4
|
+
/** Plugin configuration; every value is read once at activation. */
|
|
5
|
+
export interface Config {
|
|
6
|
+
/** Register `/rewind` and snapshot turns at all. */
|
|
7
|
+
enabled: boolean;
|
|
8
|
+
/** Directory for session indexes and private Git object stores. */
|
|
9
|
+
storageDir: string;
|
|
10
|
+
/** Milliseconds one git command may run before it is terminated. */
|
|
11
|
+
timeoutMs: number;
|
|
12
|
+
/** Bytes of stdout retained per git command. */
|
|
13
|
+
outputMaxBytes: number;
|
|
14
|
+
/** Maximum changed paths one file rewind may apply. */
|
|
15
|
+
maxPathsPerRestore: number;
|
|
16
|
+
/** Days a session checkpoint index is retained. */
|
|
17
|
+
retentionDays: number;
|
|
18
|
+
}
|
|
19
|
+
/** Schemastery validation for {@link Config}. */
|
|
20
|
+
export declare const Config: z<Config>;
|
|
21
|
+
/** Defaults used by a direct caller or a config-less Loader row. */
|
|
22
|
+
export declare const DEFAULT_CONFIG: Config;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/** Lazily resolve the Host subprocess runner into the Git face the store consumes. */
|
|
2
|
+
import type { SubprocessRuntime } from '@deepseek-ai/dsh-subprocess';
|
|
3
|
+
import type { LoggerLike } from '../types.js';
|
|
4
|
+
import { type Git } from './runner.js';
|
|
5
|
+
/** One process-wide Git runtime, resolved on first use. */
|
|
6
|
+
export declare class GitProvider {
|
|
7
|
+
private readonly subprocess;
|
|
8
|
+
private readonly signal;
|
|
9
|
+
private readonly limits;
|
|
10
|
+
private readonly logger;
|
|
11
|
+
private runtime;
|
|
12
|
+
private warned;
|
|
13
|
+
/**
|
|
14
|
+
* @param subprocess - Host subprocess capability.
|
|
15
|
+
* @param signal - plugin lifetime; aborts executable resolution.
|
|
16
|
+
* @param limits - per-command timeout and stdout cap.
|
|
17
|
+
* @param logger - warning sink.
|
|
18
|
+
*/
|
|
19
|
+
constructor(subprocess: SubprocessRuntime, signal: AbortSignal, limits: {
|
|
20
|
+
timeoutMs: number;
|
|
21
|
+
outputMaxBytes: number;
|
|
22
|
+
}, logger: LoggerLike);
|
|
23
|
+
/** Resolve the git runtime, or null when the Host has no usable git. */
|
|
24
|
+
get(): Promise<Git | null>;
|
|
25
|
+
private resolve;
|
|
26
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/** Run git through the Host subprocess capability with a scrubbed environment. */
|
|
2
|
+
import type { SubprocessRuntime } from '@deepseek-ai/dsh-subprocess';
|
|
3
|
+
/** Settled git command facts; a nonzero exit is a result, not an exception. */
|
|
4
|
+
export interface GitResult {
|
|
5
|
+
/** Process exit code, or null when terminated by a signal. */
|
|
6
|
+
exitCode: number | null;
|
|
7
|
+
/** Collected stdout text. */
|
|
8
|
+
stdout: string;
|
|
9
|
+
/** Collected stderr tail. */
|
|
10
|
+
stderr: string;
|
|
11
|
+
/** True when stdout exceeded the per-command cap. */
|
|
12
|
+
truncated: boolean;
|
|
13
|
+
}
|
|
14
|
+
/** Facts one git invocation runs under. */
|
|
15
|
+
export interface GitOptions {
|
|
16
|
+
/** Working directory of the command. */
|
|
17
|
+
cwd: string;
|
|
18
|
+
/** Extra environment on top of the scrubbed baseline. */
|
|
19
|
+
env?: Readonly<Record<string, string>>;
|
|
20
|
+
/** Bytes written to stdin, when the command reads it. */
|
|
21
|
+
stdin?: string;
|
|
22
|
+
/** Per-command stdout cap replacing the runner default. */
|
|
23
|
+
maxBytes?: number;
|
|
24
|
+
/** Caller cancellation. */
|
|
25
|
+
signal: AbortSignal;
|
|
26
|
+
}
|
|
27
|
+
/** The narrow git face the snapshot store depends on; tests provide a Node adapter. */
|
|
28
|
+
export interface Git {
|
|
29
|
+
run(args: readonly string[], options: GitOptions): Promise<GitResult>;
|
|
30
|
+
}
|
|
31
|
+
/** Reject a failed command with its stderr. */
|
|
32
|
+
export declare function assertGit(result: GitResult, what: string): GitResult;
|
|
33
|
+
/** Resolve the git executable once; the macOS developer-tools stub counts as absent. */
|
|
34
|
+
export declare function resolveGit(subprocess: SubprocessRuntime, signal: AbortSignal): Promise<string | null>;
|
|
35
|
+
/** Git runner over the Host subprocess capability. */
|
|
36
|
+
export declare class SubprocessGit implements Git {
|
|
37
|
+
private readonly subprocess;
|
|
38
|
+
private readonly executable;
|
|
39
|
+
private readonly limits;
|
|
40
|
+
constructor(subprocess: SubprocessRuntime, executable: string, limits: {
|
|
41
|
+
timeoutMs: number;
|
|
42
|
+
outputMaxBytes: number;
|
|
43
|
+
});
|
|
44
|
+
run(args: readonly string[], options: GitOptions): Promise<GitResult>;
|
|
45
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { ChangedPath } from '../types.js';
|
|
2
|
+
import { type Git } from './runner.js';
|
|
3
|
+
import type { GitWorkspace } from './workspace.js';
|
|
4
|
+
/** A path one restore could not apply. */
|
|
5
|
+
export interface RestoreFailure {
|
|
6
|
+
/** Slash-separated repository-relative path. */
|
|
7
|
+
path: string;
|
|
8
|
+
/** Why the path could not be restored. */
|
|
9
|
+
reason: string;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Write the complete work tree, including untracked and modified files but not
|
|
13
|
+
* ignored ones, as a tree object through a private index seeded from the
|
|
14
|
+
* repository's index. New blobs and the tree land in the private object store.
|
|
15
|
+
* @param git - command runner.
|
|
16
|
+
* @param workspace - addressed repository with its private object store.
|
|
17
|
+
* @param signal - cancellation.
|
|
18
|
+
* @returns the tree object id.
|
|
19
|
+
*/
|
|
20
|
+
export declare function snapshotTree(git: Git, workspace: GitWorkspace, signal: AbortSignal): Promise<string>;
|
|
21
|
+
/**
|
|
22
|
+
* List the work-tree paths that differ between two snapshot trees without
|
|
23
|
+
* rename detection, so both ends of a rename appear as a deletion and an add.
|
|
24
|
+
* @param git - command runner.
|
|
25
|
+
* @param workspace - addressed repository.
|
|
26
|
+
* @param before - checkpoint tree id.
|
|
27
|
+
* @param after - current tree id.
|
|
28
|
+
* @param signal - cancellation.
|
|
29
|
+
* @returns changed paths relative to the repository root.
|
|
30
|
+
*/
|
|
31
|
+
export declare function changedPaths(git: Git, workspace: GitWorkspace, before: string, after: string, signal: AbortSignal): Promise<ChangedPath[]>;
|
|
32
|
+
/**
|
|
33
|
+
* Work-tree paths one snapshot tree records as gitlinks (nested repositories
|
|
34
|
+
* and submodules), whose contents snapshots never descend into.
|
|
35
|
+
* @param git - command runner.
|
|
36
|
+
* @param workspace - addressed repository.
|
|
37
|
+
* @param tree - snapshot tree id.
|
|
38
|
+
* @param maxBytes - stdout cap for the tree listing.
|
|
39
|
+
* @param signal - cancellation.
|
|
40
|
+
* @returns repo-relative gitlink paths.
|
|
41
|
+
*/
|
|
42
|
+
export declare function gitlinkPaths(git: Git, workspace: GitWorkspace, tree: string, maxBytes: number, signal: AbortSignal): Promise<Set<string>>;
|
|
43
|
+
/**
|
|
44
|
+
* Materialize selected paths from one snapshot tree into the work tree. File
|
|
45
|
+
* modes, executable bits, and symbolic links follow the tree.
|
|
46
|
+
* @param git - command runner.
|
|
47
|
+
* @param workspace - addressed repository.
|
|
48
|
+
* @param tree - tree whose entries are written.
|
|
49
|
+
* @param paths - repo-relative paths to write.
|
|
50
|
+
* @param signal - cancellation.
|
|
51
|
+
* @returns per-path failures; an empty array means every path was written.
|
|
52
|
+
*/
|
|
53
|
+
export declare function restoreTreePaths(git: Git, workspace: GitWorkspace, tree: string, paths: readonly string[], signal: AbortSignal): Promise<RestoreFailure[]>;
|
|
54
|
+
/**
|
|
55
|
+
* Remove work-tree paths that the checkpoint tree does not hold.
|
|
56
|
+
* @param root - repository top-level directory.
|
|
57
|
+
* @param paths - repo-relative paths to remove.
|
|
58
|
+
* @returns per-path failures; a missing path is not a failure.
|
|
59
|
+
*/
|
|
60
|
+
export declare function removeWorktreePaths(root: string, paths: readonly string[]): Promise<RestoreFailure[]>;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { type Git } from './runner.js';
|
|
2
|
+
/** The repository enclosing a Session working directory and its private object store. */
|
|
3
|
+
export interface GitWorkspace {
|
|
4
|
+
/** Repository top-level directory, the root every diff path is relative to. */
|
|
5
|
+
root: string;
|
|
6
|
+
/** Absolute git directory holding the repository's own index. */
|
|
7
|
+
gitDir: string;
|
|
8
|
+
/** Private directory holding this workspace's snapshot objects and scratch indexes. */
|
|
9
|
+
scratch: string;
|
|
10
|
+
/** Environment routing object writes to {@link scratch} and object reads through the repository store. */
|
|
11
|
+
env: Readonly<Record<string, string>>;
|
|
12
|
+
/** Work-tree paths a snapshot must skip: the private directory when it lies inside the work tree. */
|
|
13
|
+
excludes: readonly string[];
|
|
14
|
+
}
|
|
15
|
+
/** Produce the private object-store directory for one canonical repository root. */
|
|
16
|
+
export type ScratchFor = (root: string) => Promise<string>;
|
|
17
|
+
/**
|
|
18
|
+
* Locate the repository enclosing `cwd`. The repository's own index, objects,
|
|
19
|
+
* refs, and work tree stay untouched: writes go to the private object store,
|
|
20
|
+
* and the repository's object store is attached read-only as an alternate.
|
|
21
|
+
* @param git - command runner.
|
|
22
|
+
* @param cwd - absolute working directory.
|
|
23
|
+
* @param scratchFor - yields the private object-store directory for the located root.
|
|
24
|
+
* @param signal - cancellation.
|
|
25
|
+
* @param storageRoot - plugin storage root to exclude when it lies inside the work tree.
|
|
26
|
+
* @returns the workspace, or null when the directory is outside any repository.
|
|
27
|
+
*/
|
|
28
|
+
export declare function locateWorkspace(git: Git, cwd: string, scratchFor: ScratchFor, signal: AbortSignal, storageRoot?: string): Promise<GitWorkspace | null>;
|
|
29
|
+
/** Workspace resolution shared by the turn recorder and the restore command. */
|
|
30
|
+
export declare class WorkspaceResolver {
|
|
31
|
+
private readonly storageDir;
|
|
32
|
+
private readonly git;
|
|
33
|
+
/**
|
|
34
|
+
* @param storageDir - plugin storage root holding `workspaces/`.
|
|
35
|
+
* @param git - lazy git runtime, or null when git is unavailable.
|
|
36
|
+
*/
|
|
37
|
+
constructor(storageDir: string, git: () => Promise<Git | null>);
|
|
38
|
+
/**
|
|
39
|
+
* Resolve the workspace enclosing one working directory.
|
|
40
|
+
* @param cwd - absolute session working directory.
|
|
41
|
+
* @param signal - cancellation.
|
|
42
|
+
* @returns the workspace, or null outside a repository.
|
|
43
|
+
*/
|
|
44
|
+
forCwd(cwd: string, signal: AbortSignal): Promise<GitWorkspace | null>;
|
|
45
|
+
/**
|
|
46
|
+
* Resolve one recorded repository root again at restore time.
|
|
47
|
+
* @param root - recorded repository top-level directory.
|
|
48
|
+
* @param signal - cancellation.
|
|
49
|
+
* @returns the workspace, or null when the directory is no longer a repository.
|
|
50
|
+
*/
|
|
51
|
+
forRoot(root: string, signal: AbortSignal): Promise<GitWorkspace | null>;
|
|
52
|
+
/** Private object-store directory for one canonical repository root. */
|
|
53
|
+
private scratchFor;
|
|
54
|
+
}
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* rewind: per-turn Git working-tree checkpoints and in-place conversation
|
|
3
|
+
* rewind for one session.
|
|
4
|
+
* @module @gooftoken/dsh-rewind
|
|
5
|
+
*/
|
|
6
|
+
import type { Context } from '@deepseek-ai/cordis';
|
|
7
|
+
import { type Config as ConfigShape } from './config.js';
|
|
8
|
+
export { Config } from './config.js';
|
|
9
|
+
/** Stable Loader identity. */
|
|
10
|
+
export declare const name = "rewind";
|
|
11
|
+
/** Services the plugin composes with. */
|
|
12
|
+
export declare const inject: string[];
|
|
13
|
+
/**
|
|
14
|
+
* Register `/rewind`, the checkpoint recorder, and the Git runtime.
|
|
15
|
+
* @param ctx - plugin context with commands, session query, working directory, and subprocess.
|
|
16
|
+
* @param config - plugin configuration; defaults apply when omitted.
|
|
17
|
+
*/
|
|
18
|
+
export declare function apply(ctx: Context, config?: ConfigShape): void;
|