dsh-coding-enhanced 0.0.0-stage → 0.5.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +254 -0
- package/LICENSE +21 -0
- package/README.md +453 -2
- package/cordis.patch.yml +5 -0
- package/docs/withdraw.md +560 -0
- package/lib/client.js +1727 -0
- package/lib/index.js +1771 -0
- package/package.json +52 -4
package/docs/withdraw.md
ADDED
|
@@ -0,0 +1,560 @@
|
|
|
1
|
+
# Turn withdraw: how it works
|
|
2
|
+
|
|
3
|
+
> Withdraw does not delete the turn. It restores the workspace from a shadow-git
|
|
4
|
+
> tree and appends one model-only surface replacement; the append-only session
|
|
5
|
+
> log keeps the original turn.
|
|
6
|
+
|
|
7
|
+
This document explains the mechanism twice, from two angles:
|
|
8
|
+
|
|
9
|
+
- **Project view** — what the plugin does, in what order, and where the code lives.
|
|
10
|
+
- **Git repository view** — what is stored in the shadow repositories and which
|
|
11
|
+
git commands capture and restore it.
|
|
12
|
+
- Then the **session-format view** — how one appended event removes the turn from
|
|
13
|
+
the model's context without removing it from the transcript.
|
|
14
|
+
- Finally the failure modes and the exact bug that corrupted a session log in
|
|
15
|
+
version 0.5.6.
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
## 1. The three effects
|
|
20
|
+
|
|
21
|
+
Withdraw is not a single operation. One click has three independent effects:
|
|
22
|
+
|
|
23
|
+
| Effect | Mechanism | What the user sees |
|
|
24
|
+
|---|---|---|
|
|
25
|
+
| **Workspace rollback** | restore files from the shadow-git tree captured before the turn | files return to their pre-turn contents |
|
|
26
|
+
| **Model-context cancellation** | append one `surfaceOp: replace` event that shadows the turn's surface nodes | the AI no longer sees the turn; the next prompt behaves as if it never happened |
|
|
27
|
+
| **Transcript marking** | persist `sessions[sessionId].withdrawn[turn]` in `state.json` | the turn stays on screen, greyed, with a `Withdrawn` badge |
|
|
28
|
+
|
|
29
|
+
The original turn events stay in the append-only log. That is deliberate: the
|
|
30
|
+
human transcript and the model transcript are different projections of the same
|
|
31
|
+
log.
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
## 2. Project view: the plugin's function flow
|
|
36
|
+
|
|
37
|
+
### 2.1 Turn start — capture before any mutation
|
|
38
|
+
|
|
39
|
+
`ctx.on('session/event')` watches for `turn/start`
|
|
40
|
+
([`lib/index.js:1227`](../lib/index.js#L1227-L1262)). For a top-level session it:
|
|
41
|
+
|
|
42
|
+
1. remembers the turn in `activeTurns`;
|
|
43
|
+
2. starts `captureSnapshot(sessionId, cwd, turn)`;
|
|
44
|
+
3. stores the in-flight promise in `pendingSnapshots`.
|
|
45
|
+
|
|
46
|
+
The very first tool call of the turn waits for that promise in a
|
|
47
|
+
`tools/pre-execute` hook ([`lib/index.js:1264`](../lib/index.js#L1264-L1276)).
|
|
48
|
+
This is the ordering guarantee: no tool can mutate a file before the pre-turn
|
|
49
|
+
tree exists.
|
|
50
|
+
|
|
51
|
+
Subagent sessions are skipped (`header.origin === 'subagent'` or
|
|
52
|
+
`delegationDepth > 0`), so only the top-level conversation owns snapshots.
|
|
53
|
+
|
|
54
|
+
### 2.2 Turn end — seal the snapshot
|
|
55
|
+
|
|
56
|
+
On `turn/end` the plugin ([`lib/index.js:1243`](../lib/index.js#L1243-L1260)):
|
|
57
|
+
|
|
58
|
+
- removes the session from `activeTurns`;
|
|
59
|
+
- writes the `turn/end` event sequence into `snapshot.endSeq`;
|
|
60
|
+
- persists the snapshot map.
|
|
61
|
+
|
|
62
|
+
`endSeq` is what makes a snapshot “complete” for withdraw. If the plugin was
|
|
63
|
+
reloaded while a turn was running, `status` repairs a missing `endSeq` by
|
|
64
|
+
scanning for the matching `turn/end`
|
|
65
|
+
([`handleStatus`](../lib/index.js#L1476-L1492)).
|
|
66
|
+
|
|
67
|
+
### 2.3 Status — can this turn be withdrawn?
|
|
68
|
+
|
|
69
|
+
The browser calls `POST /api/coding-enhanced` with
|
|
70
|
+
`{ endpoint: 'status', payload: { sessionId } }`. The host replies with:
|
|
71
|
+
|
|
72
|
+
| Field | Meaning |
|
|
73
|
+
|---|---|
|
|
74
|
+
| `latestTurn` | last completed turn in the session |
|
|
75
|
+
| `active` | a turn is currently running |
|
|
76
|
+
| `canWithdraw` | only the latest completed turn with a complete snapshot |
|
|
77
|
+
| `withdrawable` | every retained completed turn whose surface range still exists |
|
|
78
|
+
| `turns` | turns already marked withdrawn |
|
|
79
|
+
| `anchors` | message id → turn, so the UI can put the button beside the right message |
|
|
80
|
+
| `reason` | why the action is unavailable: `active`, `no-snapshot`, `already`, `incomplete`, `disabled`, `no-turn` |
|
|
81
|
+
|
|
82
|
+
Two details matter:
|
|
83
|
+
|
|
84
|
+
- **Only completed turns are withdrawable.** An active turn's snapshot is still
|
|
85
|
+
being written and files are changing; racing a restore against it is unsafe.
|
|
86
|
+
- **Older retained turns are reversible too.** They show **Withdraw from here**
|
|
87
|
+
and cancel every turn from there through the latest one.
|
|
88
|
+
|
|
89
|
+
### 2.4 The withdraw transaction
|
|
90
|
+
|
|
91
|
+
`handleWithdraw` ([`lib/index.js:1533`](../lib/index.js#L1533-L1645)) runs these
|
|
92
|
+
steps in order:
|
|
93
|
+
|
|
94
|
+
1. **Validate the request** — live session, withdraw enabled, no active turn,
|
|
95
|
+
not already withdrawn, snapshot exists and has `endSeq`.
|
|
96
|
+
2. **Find the surface range** — `surfaceRange(session, turn)` walks from the
|
|
97
|
+
target `turn/start`, skips events already shadowed by earlier replacements,
|
|
98
|
+
and records the first and last model-visible (`surfaceOp: 'append'`) events
|
|
99
|
+
of that turn ([`lib/index.js:1311`](../lib/index.js#L1311-L1341)).
|
|
100
|
+
3. **Collect the full cancellation range** — starting at that first surface
|
|
101
|
+
event, collect every non-shadowed surface event through the end of the log.
|
|
102
|
+
This is why withdrawing an older turn also cancels later turns.
|
|
103
|
+
4. **Compute the affected turns** — every `turn/start` at or after the target
|
|
104
|
+
range start.
|
|
105
|
+
5. **Restore the workspace** — `restoreTree(snapshot)` (section 3.3).
|
|
106
|
+
6. **Append the tombstone** — one surface replacement covering the range
|
|
107
|
+
(section 4).
|
|
108
|
+
7. **Persist the withdrawal** — mark every affected turn in
|
|
109
|
+
`state.sessions[sessionId].withdrawn`, drop their snapshots and keepalive
|
|
110
|
+
refs, and optionally run `git gc`.
|
|
111
|
+
|
|
112
|
+
The restore happens **before** the context append. If the restore fails, nothing
|
|
113
|
+
is appended. If the restore succeeds but the append fails, the files are already
|
|
114
|
+
back and the RPC returns `cancel-failed` with that exact warning — the two
|
|
115
|
+
effects cannot be made atomic across the filesystem and the session log.
|
|
116
|
+
|
|
117
|
+
### 2.5 Multi-turn rewind
|
|
118
|
+
|
|
119
|
+
The replacement range is not limited to the selected turn. If turn 12 is
|
|
120
|
+
withdrawn while 13 and 14 exist, the surface range runs from 12's first message
|
|
121
|
+
to 14's last message. All three turns are marked withdrawn, and the workspace is
|
|
122
|
+
restored to the snapshot of turn 12 — i.e. before 12, 13 and 14.
|
|
123
|
+
|
|
124
|
+
Snapshots have a retention limit (`keepTurns`, default 10, range 1–50). Older
|
|
125
|
+
snapshots lose their keepalive refs and are pruned, so they are no longer
|
|
126
|
+
withdrawable.
|
|
127
|
+
|
|
128
|
+
### 2.6 Browser side
|
|
129
|
+
|
|
130
|
+
`lib/client.js` has a small per-session store
|
|
131
|
+
([`lib/client.js:309`](../lib/client.js#L309-L405)):
|
|
132
|
+
|
|
133
|
+
- `refresh(sessionId)` calls `status`;
|
|
134
|
+
- `useRecord` subscribes and polls every 1.5 s while the action is mounted;
|
|
135
|
+
- `WithdrawAction` ([`lib/client.js:458`](../lib/client.js#L458-L540)) derives
|
|
136
|
+
the button from `anchors[messageId]`, renders a `Withdrawn` chip when
|
|
137
|
+
`turns[turn]` exists, and calls `withdraw` after `window.confirm`;
|
|
138
|
+
- after a successful call it updates the local record immediately and then
|
|
139
|
+
re-fetches status.
|
|
140
|
+
|
|
141
|
+
`WithdrawMarker` ([`lib/client.js:555`](../lib/client.js#L555)) renders the
|
|
142
|
+
turn-tail banner. Transcript turns are found by the DOM attribute
|
|
143
|
+
`[data-chat-turn]`; cancelled turns get `data-withdraw-canceled`, which applies
|
|
144
|
+
the grey dashed style declared at [`lib/client.js:179`](../lib/client.js#L179).
|
|
145
|
+
|
|
146
|
+
### 2.7 State and settings
|
|
147
|
+
|
|
148
|
+
Everything lives under `$DSH_HOME/coding-enhanced`:
|
|
149
|
+
|
|
150
|
+
```text
|
|
151
|
+
coding-enhanced/
|
|
152
|
+
├─ settings.json feature flags
|
|
153
|
+
├─ state.json withdrawn turns + snapshot map
|
|
154
|
+
└─ repos/
|
|
155
|
+
└─ <sha1(workspace cwd)[:20]>.git/
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
`state.json` has two top-level maps:
|
|
159
|
+
|
|
160
|
+
```json
|
|
161
|
+
{
|
|
162
|
+
"sessions": {
|
|
163
|
+
"session-…": {
|
|
164
|
+
"withdrawn": {
|
|
165
|
+
"44": { "at": 1794376877122, "files": 1 }
|
|
166
|
+
},
|
|
167
|
+
"updatedAt": 1794376877122
|
|
168
|
+
}
|
|
169
|
+
},
|
|
170
|
+
"snapshots": {
|
|
171
|
+
"session-…": {
|
|
172
|
+
"44": {
|
|
173
|
+
"tree": "…",
|
|
174
|
+
"cwd": "/Users/xthw/work_dir",
|
|
175
|
+
"gitDir": "/Users/xthw/.dsh/coding-enhanced/repos/….git",
|
|
176
|
+
"endSeq": 5993,
|
|
177
|
+
"createdAt": 1794376870000,
|
|
178
|
+
"nested": []
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
`settings.json` holds `review`, `withdraw`, `keepTurns`, `nestedSnapshots`, and
|
|
186
|
+
`autoGc`. A legacy `$DSH_HOME/withdraw-round` directory is renamed to
|
|
187
|
+
`coding-enhanced` on load.
|
|
188
|
+
|
|
189
|
+
The state file is capped at 200 sessions; the least recently updated sessions are
|
|
190
|
+
dropped first.
|
|
191
|
+
|
|
192
|
+
### 2.8 Code map
|
|
193
|
+
|
|
194
|
+
| Concern | Location |
|
|
195
|
+
|---|---|
|
|
196
|
+
| Settings / state load and save | [`lib/index.js:276`](../lib/index.js#L276-L338) |
|
|
197
|
+
| Snapshot refs and nested refs | [`lib/index.js:341`](../lib/index.js#L341-L374) |
|
|
198
|
+
| Shadow repo setup | [`lib/index.js:554`](../lib/index.js#L554-L566) |
|
|
199
|
+
| Snapshot capture | [`lib/index.js:619`](../lib/index.js#L619-L675) |
|
|
200
|
+
| Retention / GC | [`lib/index.js:446`](../lib/index.js#L446-L539) |
|
|
201
|
+
| Workspace restore | [`lib/index.js:1369`](../lib/index.js#L1369-L1474) |
|
|
202
|
+
| Status RPC | [`lib/index.js:1476`](../lib/index.js#L1476-L1531) |
|
|
203
|
+
| Withdraw RPC | [`lib/index.js:1533`](../lib/index.js#L1533-L1645) |
|
|
204
|
+
| Browser store and action | [`lib/client.js:309`](../lib/client.js#L309-L540) |
|
|
205
|
+
| Withdrawn banner | [`lib/client.js:555`](../lib/client.js#L555) |
|
|
206
|
+
|
|
207
|
+
---
|
|
208
|
+
|
|
209
|
+
## 3. Git repository view: the shadow repository
|
|
210
|
+
|
|
211
|
+
The snapshot is **not** a copy of the project and **not** a commit in the
|
|
212
|
+
project's repository. It is a separate repository whose worktree is the project
|
|
213
|
+
directory.
|
|
214
|
+
|
|
215
|
+
### 3.1 Repository layout
|
|
216
|
+
|
|
217
|
+
`shadowRepoPath(root, cwd)` hashes the workspace path:
|
|
218
|
+
|
|
219
|
+
```js
|
|
220
|
+
sha1(cwd).slice(0, 20) + '.git'
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
So one workspace always maps to the same shadow repository, outside the
|
|
224
|
+
workspace:
|
|
225
|
+
|
|
226
|
+
```text
|
|
227
|
+
$DSH_HOME/coding-enhanced/repos/<hash>.git/
|
|
228
|
+
├─ HEAD
|
|
229
|
+
├─ config core.bare=false, core.worktree=<project cwd>
|
|
230
|
+
├─ index the shadow index
|
|
231
|
+
├─ objects/ blobs and trees
|
|
232
|
+
├─ info/exclude plugin excludes
|
|
233
|
+
└─ refs/coding-enhanced/<sessionHash>/<turn> keepalive ref → tree
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
Initialization is equivalent to:
|
|
237
|
+
|
|
238
|
+
```sh
|
|
239
|
+
git init --quiet --bare <gitDir>
|
|
240
|
+
git --git-dir <gitDir> config core.bare false
|
|
241
|
+
git --git-dir <gitDir> config core.worktree <cwd>
|
|
242
|
+
git --git-dir <gitDir> config core.autocrlf false
|
|
243
|
+
git --git-dir <gitDir> config core.quotepath false
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
A ref points **directly at a tree**, not at a commit. Git keeps tree and blob
|
|
247
|
+
objects reachable from that ref, so `git gc` cannot delete a retained snapshot.
|
|
248
|
+
The session part of the ref is `sha256(sessionId).slice(0, 16)`, so two sessions
|
|
249
|
+
on the same workspace do not collide.
|
|
250
|
+
|
|
251
|
+
### 3.2 Capture — exactly what runs
|
|
252
|
+
|
|
253
|
+
At turn start, with `--git-dir <gitDir> --work-tree <cwd>`:
|
|
254
|
+
|
|
255
|
+
```sh
|
|
256
|
+
git add -A --ignore-errors
|
|
257
|
+
git write-tree
|
|
258
|
+
git update-ref refs/coding-enhanced/<sessionHash>/<turn> <tree>
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
Properties of that capture:
|
|
262
|
+
|
|
263
|
+
- **Tracked and untracked files are included.** `add -A` stages everything not
|
|
264
|
+
ignored, so a file created outside git is still restorable.
|
|
265
|
+
- **Deleted files are represented implicitly** — the tree simply lacks them;
|
|
266
|
+
restoring the tree brings them back.
|
|
267
|
+
- **No commit is created.** `write-tree` writes only tree/blob objects.
|
|
268
|
+
- **The project index is not used.** `GIT_INDEX_FILE` points inside the shadow
|
|
269
|
+
repository.
|
|
270
|
+
- **Unreadable paths do not fail the turn.** `add -A --ignore-errors` is used; a
|
|
271
|
+
path git refuses is added to `info/exclude` and reported, then the add is
|
|
272
|
+
retried.
|
|
273
|
+
- **Normal git ignore rules apply.** The shadow repo has its own
|
|
274
|
+
`info/exclude` (`.git/`, `node_modules/`, `.DS_Store`,
|
|
275
|
+
`.dsh-coding-enhanced/`), and because its worktree is the project directory,
|
|
276
|
+
the project's `.gitignore` files also affect what `add -A` stages.
|
|
277
|
+
|
|
278
|
+
The tree hash is stored in `state.json`, and the ref keeps it alive.
|
|
279
|
+
|
|
280
|
+
### 3.3 Restore — exactly what runs
|
|
281
|
+
|
|
282
|
+
`restoreWorktree(gitDir, cwd, tree)` ([`lib/index.js:1369`](../lib/index.js#L1369-L1437))
|
|
283
|
+
is a three-way operation between the snapshot tree and the current worktree:
|
|
284
|
+
|
|
285
|
+
```sh
|
|
286
|
+
# 1. What is the workspace now?
|
|
287
|
+
git add -A --ignore-errors
|
|
288
|
+
git write-tree # currentTree
|
|
289
|
+
|
|
290
|
+
# 2. Which paths differ?
|
|
291
|
+
git diff-tree -r --name-status -z <tree> <currentTree>
|
|
292
|
+
|
|
293
|
+
# 3. Which mode does each snapshot path have?
|
|
294
|
+
git ls-tree -r -z <tree>
|
|
295
|
+
|
|
296
|
+
# 4. For each difference:
|
|
297
|
+
# A (added since snapshot) -> delete the path
|
|
298
|
+
# M / D -> git cat-file blob <tree>:<path> and rewrite it
|
|
299
|
+
# 120000 -> recreate as a symlink
|
|
300
|
+
# 100755 -> recreate with executable bit
|
|
301
|
+
|
|
302
|
+
# 5. Reset the shadow index to the restored tree
|
|
303
|
+
git read-tree <tree>
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
Important details:
|
|
307
|
+
|
|
308
|
+
- **Added files are deleted**, including untracked files created during the turn.
|
|
309
|
+
- **Deleted files come back** because the blob exists in the snapshot tree.
|
|
310
|
+
- **Modified files are rewritten from blobs**, not from a diff patch.
|
|
311
|
+
- **Symlinks and executable bits are preserved.**
|
|
312
|
+
- **Nested git repositories are skipped** when the path is a gitlink (`160000`)
|
|
313
|
+
or contains `.git`; their contents are not in the outer snapshot. They are
|
|
314
|
+
restored separately from their own shadow repos.
|
|
315
|
+
- `read-tree` leaves the shadow index matching the restored state, so the next
|
|
316
|
+
turn's `add -A` produces a clean diff.
|
|
317
|
+
|
|
318
|
+
`restoreTree` ([`lib/index.js:1439`](../lib/index.js#L1439-L1474)) then repeats
|
|
319
|
+
the restore for each nested snapshot, merges the restored file lists, and
|
|
320
|
+
returns `{ count, files, skipped }`. `skipped` is shown to the user.
|
|
321
|
+
|
|
322
|
+
### 3.4 Nested repositories
|
|
323
|
+
|
|
324
|
+
`findNestedRepos(cwd)` walks the workspace up to depth 12, skipping
|
|
325
|
+
`node_modules`, `.git`, and `.dsh-coding-enhanced`
|
|
326
|
+
([`lib/index.js:594`](../lib/index.js#L594-L617)). Every directory containing a
|
|
327
|
+
`.git` is snapshotted into its own shadow repository with its own worktree.
|
|
328
|
+
Only working files are captured — the nested `.git` directory is excluded — so
|
|
329
|
+
withdraw restores the nested working tree without rewriting its history.
|
|
330
|
+
|
|
331
|
+
A nested repository created **during** the turn has no snapshot. It is left in
|
|
332
|
+
place and reported as skipped rather than deleted.
|
|
333
|
+
|
|
334
|
+
### 3.5 Cleanup and GC
|
|
335
|
+
|
|
336
|
+
Withdraw and retention pruning remove the snapshot's refs. The affected shadow
|
|
337
|
+
repositories are marked dirty. When `autoGc` is on, or when the user clicks
|
|
338
|
+
**Clean up now**, the plugin runs:
|
|
339
|
+
|
|
340
|
+
```sh
|
|
341
|
+
git gc --prune=now --quiet
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
Trees that are still referenced by retained snapshots survive; unreferenced
|
|
345
|
+
objects are collected. GC is serialized per workspace so concurrent sessions
|
|
346
|
+
cannot race the same repository.
|
|
347
|
+
|
|
348
|
+
---
|
|
349
|
+
|
|
350
|
+
## 4. Session-format view: hiding the turn from the model
|
|
351
|
+
|
|
352
|
+
DSH's session log is append-only. Each event has a sequence number. Message
|
|
353
|
+
events additionally carry a `surfaceOp`:
|
|
354
|
+
|
|
355
|
+
```ts
|
|
356
|
+
type SurfaceOp =
|
|
357
|
+
| 'append'
|
|
358
|
+
| { op: 'replace'; startSeq: SessionSeq; endSeq: SessionSeq }
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
The **surface** is the ordered list of events that derive the provider message
|
|
362
|
+
history. An `append` event joins at its own position. A `replace` event shadows
|
|
363
|
+
an existing surface range and takes its place. The original events remain
|
|
364
|
+
readable; they are simply no longer on the current surface.
|
|
365
|
+
|
|
366
|
+
### 4.1 The tombstone event
|
|
367
|
+
|
|
368
|
+
Withdraw appends one event at the end of the log:
|
|
369
|
+
|
|
370
|
+
```json
|
|
371
|
+
{
|
|
372
|
+
"type": "user/message",
|
|
373
|
+
"seq": 5994,
|
|
374
|
+
"time": 1794376877121,
|
|
375
|
+
"data": {
|
|
376
|
+
"id": "coding-enhanced-withdraw-44-…",
|
|
377
|
+
"role": "user",
|
|
378
|
+
"source": { "kind": "coding-enhanced-withdraw" },
|
|
379
|
+
"content": []
|
|
380
|
+
},
|
|
381
|
+
"sourceEventSeqs": [5971, 5973, 5975, 5979, 5981, 5985, 5987, 5991],
|
|
382
|
+
"surfaceOp": {
|
|
383
|
+
"op": "replace",
|
|
384
|
+
"startSeq": 5971,
|
|
385
|
+
"endSeq": 5991
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
```
|
|
389
|
+
|
|
390
|
+
- `startSeq` / `endSeq` must name events already on the surface; the range is
|
|
391
|
+
inclusive and spans the withdrawn turns.
|
|
392
|
+
- `sourceEventSeqs` must cite **every** shadowed surface node. DSH rejects a
|
|
393
|
+
replacement that omits one.
|
|
394
|
+
- The replacement event itself becomes the new surface node at that position.
|
|
395
|
+
- The tombstone is an **empty user message**. DSH's derived history keeps the
|
|
396
|
+
node, but provider adapters drop empty user/assistant content before building
|
|
397
|
+
the wire request (the DeepSeek adapter does this explicitly), so nothing is
|
|
398
|
+
sent to the model.
|
|
399
|
+
- The custom `source.kind` marks it as plugin-owned, so normal user-message
|
|
400
|
+
consumers (for example session-title selection, which looks for
|
|
401
|
+
`source.kind === "user"`) do not mistake it for a real prompt.
|
|
402
|
+
|
|
403
|
+
### 4.2 Why a `user/message` and not a `developer/message`
|
|
404
|
+
|
|
405
|
+
Only `user/message` is both surface-eligible and legal outside an open
|
|
406
|
+
turn/step. `developer/message`, `system/message`, and `assistant/message` are
|
|
407
|
+
step-scoped; `tool/result` needs an open turn or step as well. Withdraw appends
|
|
408
|
+
after `turn/end`, so those types cannot be used. Section 7 explains the exact
|
|
409
|
+
validator that rejected the old implementation.
|
|
410
|
+
|
|
411
|
+
---
|
|
412
|
+
|
|
413
|
+
## 5. End-to-end example
|
|
414
|
+
|
|
415
|
+
Assume turn 44 in session `session-d2d9…`:
|
|
416
|
+
|
|
417
|
+
```text
|
|
418
|
+
seq 5970 turn/start 44
|
|
419
|
+
seq 5971 user/message ← first surface node of turn 44
|
|
420
|
+
seq 5972 session-log-deepseek/delivery-accepted
|
|
421
|
+
seq 5973 assistant/message
|
|
422
|
+
seq 5974 tool/call
|
|
423
|
+
seq 5975 tool/result
|
|
424
|
+
…
|
|
425
|
+
seq 5991 assistant/message ← last surface node
|
|
426
|
+
seq 5992 step/end
|
|
427
|
+
seq 5993 turn/end 44
|
|
428
|
+
seq 5994 user/message (replace 5971..5991) ← the withdrawn tombstone
|
|
429
|
+
```
|
|
430
|
+
|
|
431
|
+
After the append:
|
|
432
|
+
|
|
433
|
+
- the surface is `[… earlier nodes …, 5994]`;
|
|
434
|
+
- the model deriving history from the surface skips every node in
|
|
435
|
+
`5971..5991`;
|
|
436
|
+
- `state.json` records `withdrawn["44"] = { at, files }`;
|
|
437
|
+
- the transcript still contains 5971..5991 because those are append-origin
|
|
438
|
+
events, and the client greys the turn.
|
|
439
|
+
|
|
440
|
+
The live surface is updated immediately by `session.append`; the persisted log
|
|
441
|
+
contains the same replacement, so a restart replays to the same surface.
|
|
442
|
+
|
|
443
|
+
---
|
|
444
|
+
|
|
445
|
+
## 6. Failure modes and limits
|
|
446
|
+
|
|
447
|
+
| Situation | What happens |
|
|
448
|
+
|---|---|
|
|
449
|
+
| Turn is still running | `reason: active`; action disabled |
|
|
450
|
+
| Plugin loaded after the turn started, so no snapshot | `reason: no-snapshot` |
|
|
451
|
+
| Snapshot was pruned by retention | `reason: no-snapshot`; only `keepTurns` recent turns remain |
|
|
452
|
+
| Turn has no model-visible messages | `no-surface`; nothing to cancel |
|
|
453
|
+
| File restore fails | `restore-failed`; no context change is made |
|
|
454
|
+
| Restore succeeds but append fails | files are already restored, RPC returns `cancel-failed` |
|
|
455
|
+
| Nested repo created during the turn | left in place, reported as `skipped` |
|
|
456
|
+
| Path is ignored by `.gitignore` or plugin excludes | not snapshotted, so not restored |
|
|
457
|
+
| Nested repo too broken to snapshot | outer snapshot skips it, reported |
|
|
458
|
+
|
|
459
|
+
Withdraw is intentionally not a full version-control system: it restores the
|
|
460
|
+
captured working tree, not reflog history, stashes, submodule commits, or files
|
|
461
|
+
outside the workspace.
|
|
462
|
+
|
|
463
|
+
---
|
|
464
|
+
|
|
465
|
+
## 7. Why the old implementation corrupted the session
|
|
466
|
+
|
|
467
|
+
### 7.1 The old tombstone
|
|
468
|
+
|
|
469
|
+
Version 0.5.6 appended this after `turn/end`:
|
|
470
|
+
|
|
471
|
+
```json
|
|
472
|
+
{
|
|
473
|
+
"type": "developer/message",
|
|
474
|
+
"data": {
|
|
475
|
+
"turn": 44,
|
|
476
|
+
"step": 4,
|
|
477
|
+
"message": {
|
|
478
|
+
"id": "coding-enhanced-44-…",
|
|
479
|
+
"role": "developer",
|
|
480
|
+
"source": { "kind": "user" },
|
|
481
|
+
"content": []
|
|
482
|
+
}
|
|
483
|
+
},
|
|
484
|
+
"surfaceOp": { "op": "replace", "startSeq": 5971, "endSeq": 5991 }
|
|
485
|
+
}
|
|
486
|
+
```
|
|
487
|
+
|
|
488
|
+
It looked plausible: the turn number and step number were copied from the turn.
|
|
489
|
+
But the type was wrong for the position in the log.
|
|
490
|
+
|
|
491
|
+
### 7.2 Live append vs. reload replay
|
|
492
|
+
|
|
493
|
+
DSH validates an append **locally**:
|
|
494
|
+
|
|
495
|
+
- data shape (`validateSessionEventData`);
|
|
496
|
+
- surface metadata (range, source-event coverage, eligibility);
|
|
497
|
+
- JSON serializability.
|
|
498
|
+
|
|
499
|
+
It does **not** replay turn/step relationships on every append. The log is
|
|
500
|
+
written successfully, so the UI reports success and the turn is withdrawn.
|
|
501
|
+
|
|
502
|
+
On the next load, DSH reconstructs global lifecycle state from the whole log
|
|
503
|
+
(`restoreReleasedV4Artifact` → `assertReleasedV4Relationships`). That replay
|
|
504
|
+
walks the events in order and maintains:
|
|
505
|
+
|
|
506
|
+
- `this.turn` — `turn/start` opens it, `turn/end` closes it;
|
|
507
|
+
- `this.step` — `step/start` opens it, `step/end` closes it.
|
|
508
|
+
|
|
509
|
+
`developer/message` is a **step-scoped** event. The replay calls:
|
|
510
|
+
|
|
511
|
+
```js
|
|
512
|
+
requireStep(event, data) {
|
|
513
|
+
if (this.turn === null || this.step === null ||
|
|
514
|
+
data.turn !== this.turn || data.step !== this.step) {
|
|
515
|
+
throw new SessionFormatError(
|
|
516
|
+
`${event.type} does not match an open turn and step`
|
|
517
|
+
)
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
```
|
|
521
|
+
|
|
522
|
+
Timeline for the old tombstone:
|
|
523
|
+
|
|
524
|
+
```text
|
|
525
|
+
turn/start 44 this.turn = 44
|
|
526
|
+
step/start 4 this.step = 4
|
|
527
|
+
step/end this.step = null
|
|
528
|
+
turn/end 44 this.turn = null
|
|
529
|
+
developer/message this.turn = null, this.step = null
|
|
530
|
+
data.turn = 44, data.step = 4
|
|
531
|
+
→ requireStep throws
|
|
532
|
+
```
|
|
533
|
+
|
|
534
|
+
The numbers were correct, but there was no open turn or step to attach them to.
|
|
535
|
+
The log was structurally plausible and permanently invalid.
|
|
536
|
+
|
|
537
|
+
### 7.3 Why `user/message` fixes it
|
|
538
|
+
|
|
539
|
+
`user/message` is surface-eligible but **not** step-scoped. Replay validates its
|
|
540
|
+
surface replacement (range and source coverage) and its message fields, but it
|
|
541
|
+
does not require an open turn or step. That makes it the only message type that
|
|
542
|
+
can be appended as a standalone tombstone after `turn/end`. It is also the type
|
|
543
|
+
DSH itself uses for compaction checkpoints that replace a surface range.
|
|
544
|
+
|
|
545
|
+
### 7.4 Repairing an already-corrupted log
|
|
546
|
+
|
|
547
|
+
The broken event is the last event in the affected log, so it can be rewritten
|
|
548
|
+
without renumbering:
|
|
549
|
+
|
|
550
|
+
1. decompress every zstd frame of `session.v4.jsonl.zstd`;
|
|
551
|
+
2. parse the final JSONL event;
|
|
552
|
+
3. replace its `developer/message` payload with the `user/message` tombstone
|
|
553
|
+
shape, keeping `seq`, `time`, `surfaceOp`, and `sourceEventSeqs`;
|
|
554
|
+
4. recompress that frame with a checksum and replace the file atomically;
|
|
555
|
+
5. replay the whole artifact through DSH's own
|
|
556
|
+
`restoreReleasedV4Artifact` validator.
|
|
557
|
+
|
|
558
|
+
The original file is kept as a timestamped `.bak-…` next to the log. Future
|
|
559
|
+
withdrawals are correct because the plugin now emits the `user/message` shape
|
|
560
|
+
directly.
|