dsh-message-rewind 1.2.0 → 1.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +4 -4
- package/README.md +4 -4
- package/client.js +31 -3
- package/client.test.js +29 -4
- package/files.test.js +23 -2
- package/index.js +20 -2
- package/index.test.js +22 -9
- package/manifest.test.js +2 -2
- package/package.json +2 -2
- package/rewind.js +15 -2
- package/rewind.test.js +13 -0
package/README.en.md
CHANGED
|
@@ -25,25 +25,25 @@ Only messages you sent have these buttons. The **Copy** button is still there, i
|
|
|
25
25
|
|
|
26
26
|
Your own messages get **edit / delete / copy** buttons under the bubble:
|
|
27
27
|
|
|
28
|
-
<img src="https://raw.githubusercontent.com/xCube007/dsh-message-rewind/main/img/message-actions.png" alt="Edit, delete and copy buttons under a user message" width="760">
|
|
28
|
+
<img src="https://raw.githubusercontent.com/xCube007/dsh-message-rewind/main/img/message-actions-v1.2.1.png" alt="Edit, delete and copy buttons under a user message" width="760">
|
|
29
29
|
|
|
30
30
|
The popover opens right under the button you clicked and points at it with a small arrow; click outside or press Esc to dismiss it. The editor reads the file changes recorded after that message as soon as it opens — collapsed into one **N file changes recorded** line unless you expand it — and a two-way switch decides whether files are restored too:
|
|
31
31
|
|
|
32
32
|
| Edit popover | Delete confirmation |
|
|
33
33
|
| :---: | :---: |
|
|
34
|
-
| <img src="https://raw.githubusercontent.com/xCube007/dsh-message-rewind/main/img/edit-dialog.png" alt="Edit popover: file changes collapse to one line, rollback scope is a two-way switch" width="360"> | <img src="https://raw.githubusercontent.com/xCube007/dsh-message-rewind/main/img/delete-dialog.png" alt="Delete confirmation: a one-line confirm when no files changed" width="360"> |
|
|
34
|
+
| <img src="https://raw.githubusercontent.com/xCube007/dsh-message-rewind/main/img/edit-dialog-v1.2.1.png" alt="Edit popover: file changes collapse to one line, rollback scope is a two-way switch" width="360"> | <img src="https://raw.githubusercontent.com/xCube007/dsh-message-rewind/main/img/delete-dialog-v1.2.1.png" alt="Delete confirmation: a one-line confirm when no files changed" width="360"> |
|
|
35
35
|
|
|
36
36
|
The popover follows its button: it flips above when there is no room below, narrows to the screen width on small viewports, and keeps the same layering in the light theme:
|
|
37
37
|
|
|
38
38
|
| Flips above near the bottom | Narrow screen (390px) | Light theme |
|
|
39
39
|
| :---: | :---: | :---: |
|
|
40
|
-
| <img src="https://raw.githubusercontent.com/xCube007/dsh-message-rewind/main/img/popover-flip.png" alt="Popover flipped above the button, arrow pointing down at it" width="240"> | <img src="https://raw.githubusercontent.com/xCube007/dsh-message-rewind/main/img/popover-narrow.png" alt="Popover narrowed to a 390px viewport" width="240"> | <img src="https://raw.githubusercontent.com/xCube007/dsh-message-rewind/main/img/popover-light.png" alt="Delete popover in the light theme" width="240"> |
|
|
40
|
+
| <img src="https://raw.githubusercontent.com/xCube007/dsh-message-rewind/main/img/popover-flip-v1.2.1.png" alt="Popover flipped above the button, arrow pointing down at it" width="240"> | <img src="https://raw.githubusercontent.com/xCube007/dsh-message-rewind/main/img/popover-narrow-v1.2.1.png" alt="Popover narrowed to a 390px viewport" width="240"> | <img src="https://raw.githubusercontent.com/xCube007/dsh-message-rewind/main/img/popover-light-v1.2.1.png" alt="Delete popover in the light theme" width="240"> |
|
|
41
41
|
|
|
42
42
|
Snapshot and Git-worktree limits are shown in the popover itself, not after a failed operation.
|
|
43
43
|
|
|
44
44
|
## Install
|
|
45
45
|
|
|
46
|
-
Requires DSH 0.2.
|
|
46
|
+
Requires DSH 0.2.2-rc.1 or newer (0.2.x) and Node.js ≥ 20; verified on 0.2.2-rc.1. DSH validates `engines.dsh` at startup and refuses to load the plugin on an older host.
|
|
47
47
|
|
|
48
48
|
### From npm (recommended)
|
|
49
49
|
|
package/README.md
CHANGED
|
@@ -27,25 +27,25 @@
|
|
|
27
27
|
|
|
28
28
|
自己发出的消息,气泡下方会多出「编辑 / 删除 / 复制」三个按钮:
|
|
29
29
|
|
|
30
|
-
<img src="https://raw.githubusercontent.com/xCube007/dsh-message-rewind/main/img/message-actions.png" alt="消息气泡下方的编辑、删除、复制按钮" width="760">
|
|
30
|
+
<img src="https://raw.githubusercontent.com/xCube007/dsh-message-rewind/main/img/message-actions-v1.2.1.png" alt="消息气泡下方的编辑、删除、复制按钮" width="760">
|
|
31
31
|
|
|
32
32
|
点铅笔或垃圾桶之后,小窗直接出现在那个按钮正下方,并用一个小箭头指向它;点窗外或按 Esc 就收起。编辑窗一打开就会读出这条消息之后记录到的文件改动,默认只显示一行「已记录 N 处文件改动」,展开才看具体路径;要不要连文件一起还原,用一个二选一的开关决定:
|
|
33
33
|
|
|
34
34
|
| 编辑小窗 | 删除确认 |
|
|
35
35
|
| :---: | :---: |
|
|
36
|
-
| <img src="https://raw.githubusercontent.com/xCube007/dsh-message-rewind/main/img/edit-dialog.png" alt="编辑小窗:文件改动收起成一行,回滚范围二选一" width="360"> | <img src="https://raw.githubusercontent.com/xCube007/dsh-message-rewind/main/img/delete-dialog.png" alt="删除确认:没有文件改动时只剩一行确认" width="360"> |
|
|
36
|
+
| <img src="https://raw.githubusercontent.com/xCube007/dsh-message-rewind/main/img/edit-dialog-v1.2.1.png" alt="编辑小窗:文件改动收起成一行,回滚范围二选一" width="360"> | <img src="https://raw.githubusercontent.com/xCube007/dsh-message-rewind/main/img/delete-dialog-v1.2.1.png" alt="删除确认:没有文件改动时只剩一行确认" width="360"> |
|
|
37
37
|
|
|
38
38
|
小窗跟着按钮走:窗口底部放不下时自动翻到按钮上方,窄屏下收窄到屏幕宽度,亮色主题也保持同一套层级:
|
|
39
39
|
|
|
40
40
|
| 靠近底部时翻到上方 | 窄屏(390px) | 亮色主题 |
|
|
41
41
|
| :---: | :---: | :---: |
|
|
42
|
-
| <img src="https://raw.githubusercontent.com/xCube007/dsh-message-rewind/main/img/popover-flip.png" alt="小窗翻到按钮上方,箭头朝下指向按钮" width="240"> | <img src="https://raw.githubusercontent.com/xCube007/dsh-message-rewind/main/img/popover-narrow.png" alt="窄屏下小窗收窄到屏幕宽度" width="240"> | <img src="https://raw.githubusercontent.com/xCube007/dsh-message-rewind/main/img/popover-light.png" alt="亮色主题下的删除小窗" width="240"> |
|
|
42
|
+
| <img src="https://raw.githubusercontent.com/xCube007/dsh-message-rewind/main/img/popover-flip-v1.2.1.png" alt="小窗翻到按钮上方,箭头朝下指向按钮" width="240"> | <img src="https://raw.githubusercontent.com/xCube007/dsh-message-rewind/main/img/popover-narrow-v1.2.1.png" alt="窄屏下小窗收窄到屏幕宽度" width="240"> | <img src="https://raw.githubusercontent.com/xCube007/dsh-message-rewind/main/img/popover-light-v1.2.1.png" alt="亮色主题下的删除小窗" width="240"> |
|
|
43
43
|
|
|
44
44
|
快照失效、工作区不在 Git 仓库等限制也会直接标在小窗里,不用等到操作失败才知道。
|
|
45
45
|
|
|
46
46
|
## 安装
|
|
47
47
|
|
|
48
|
-
需要 DSH 0.2.
|
|
48
|
+
需要 DSH 0.2.2-rc.1 及以上(0.2.x),Node.js ≥ 20;已在 0.2.2-rc.1 上验证。宿主启动时会按 `engines.dsh` 校验,低于该版本会拒绝加载。
|
|
49
49
|
|
|
50
50
|
### 从 npm 安装(推荐)
|
|
51
51
|
|
package/client.js
CHANGED
|
@@ -43,6 +43,7 @@ window.__ModuleLoader__.load({
|
|
|
43
43
|
'error.incomplete-snapshot': '部分文件改动快照已不可用,只能只回滚消息。',
|
|
44
44
|
'error.no-workspace': '这个会话没有工作区,只能只回滚消息。',
|
|
45
45
|
'error.invalid-session': '会话标识无效。',
|
|
46
|
+
'error.missing-session': '找不到这个会话,它可能已被删除。',
|
|
46
47
|
'error.unknown-endpoint': '未知接口。',
|
|
47
48
|
'error.request-too-large': '请求体过大。',
|
|
48
49
|
'error.bad-request': '请求格式无效。',
|
|
@@ -85,6 +86,7 @@ window.__ModuleLoader__.load({
|
|
|
85
86
|
'error.incomplete-snapshot': 'Some file snapshots are unavailable; roll back messages only.',
|
|
86
87
|
'error.no-workspace': 'This conversation has no workspace; roll back messages only.',
|
|
87
88
|
'error.invalid-session': 'Invalid session id.',
|
|
89
|
+
'error.missing-session': 'This conversation no longer exists.',
|
|
88
90
|
'error.unknown-endpoint': 'Unknown endpoint.',
|
|
89
91
|
'error.request-too-large': 'The request body is too large.',
|
|
90
92
|
'error.bad-request': 'Invalid request.',
|
|
@@ -323,7 +325,7 @@ window.__ModuleLoader__.load({
|
|
|
323
325
|
|
|
324
326
|
return {
|
|
325
327
|
name: 'dsh-message-rewind',
|
|
326
|
-
inject: ['slots', 'uiWorkspace', 'locale', 'connection'],
|
|
328
|
+
inject: ['slots', 'uiWorkspace', 'locale', 'connection', 'sessions'],
|
|
327
329
|
apply(ctx) {
|
|
328
330
|
try {
|
|
329
331
|
install(ctx);
|
|
@@ -402,6 +404,26 @@ window.__ModuleLoader__.load({
|
|
|
402
404
|
// belongs to, so a closed or replaced dialog is never resurrected.
|
|
403
405
|
const patchIf = (token, fields) => { if (current !== null && current.token === token) publish({ ...current, ...fields }); };
|
|
404
406
|
|
|
407
|
+
// 0.2.2 only opens a Session its Client catalog knows. A child the Host
|
|
408
|
+
// creates over RPC enters that catalog through a forwarded
|
|
409
|
+
// Session-added frame that races this reply, so pull the Session list
|
|
410
|
+
// (a refresh) until the row lands before navigating; otherwise
|
|
411
|
+
// `openSession` throws and strands the user on the source conversation.
|
|
412
|
+
async function openCreated(sessionId) {
|
|
413
|
+
const catalogued = () => {
|
|
414
|
+
try {
|
|
415
|
+
return ctx.sessions.list.getSnapshot().byId[sessionId] !== undefined;
|
|
416
|
+
} catch {
|
|
417
|
+
return false;
|
|
418
|
+
}
|
|
419
|
+
};
|
|
420
|
+
for (let attempt = 0; attempt < 10 && !catalogued(); attempt += 1) {
|
|
421
|
+
if (attempt > 0) await new Promise((resolveDelay) => window.setTimeout(resolveDelay, 100));
|
|
422
|
+
await ctx.sessions.refresh().catch(() => {});
|
|
423
|
+
}
|
|
424
|
+
ctx.uiWorkspace.openSession(sessionId);
|
|
425
|
+
}
|
|
426
|
+
|
|
405
427
|
async function execute(rewindFiles) {
|
|
406
428
|
const request = current;
|
|
407
429
|
if (!request || request.busy) return;
|
|
@@ -420,7 +442,13 @@ window.__ModuleLoader__.load({
|
|
|
420
442
|
publish(null);
|
|
421
443
|
// Navigate first so the source's client reference is released, then let
|
|
422
444
|
// the host retire its log: the sidebar keeps exactly one conversation.
|
|
423
|
-
|
|
445
|
+
try {
|
|
446
|
+
await openCreated(result.sessionId);
|
|
447
|
+
} catch (error) {
|
|
448
|
+
// The rollback happened; never retire the source the UI still shows.
|
|
449
|
+
console.warn('[message-rewind] the new conversation could not be opened:', error instanceof Error ? error.message : String(error));
|
|
450
|
+
return;
|
|
451
|
+
}
|
|
424
452
|
const source = result.sourceSessionId;
|
|
425
453
|
if (typeof source === 'string' && source !== result.sessionId) {
|
|
426
454
|
Promise.resolve()
|
|
@@ -643,7 +671,7 @@ window.__ModuleLoader__.load({
|
|
|
643
671
|
() => copyText())));
|
|
644
672
|
}
|
|
645
673
|
|
|
646
|
-
ctx.slots.inject('conversation.chat.node', () => ctx.slots.register({ name: 'conversation.chat.node', key: 'user', locale: NS }, UserActions));
|
|
674
|
+
ctx.slots.inject('conversation.chat.node', () => ctx.slots.register({ name: 'conversation.chat.node', key: 'user', priority: -1, locale: NS }, UserActions));
|
|
647
675
|
ctx.slots.inject('shell.overlay', () => ctx.slots.register({ name: 'shell.overlay', id: 'message-rewind.dialog', locale: NS }, RewindDialog));
|
|
648
676
|
}
|
|
649
677
|
};
|
package/client.test.js
CHANGED
|
@@ -72,6 +72,7 @@ Object.defineProperty(globalThis, 'navigator', { value: { clipboard: { writeText
|
|
|
72
72
|
// The Host's `inspect` answer; the dialog tests below change it to exercise the
|
|
73
73
|
// recorded-file and lost-snapshot branches.
|
|
74
74
|
let inspectResult = { files: [], incomplete: false, unfinished: false };
|
|
75
|
+
const knownSessions = {};
|
|
75
76
|
const ctx = {
|
|
76
77
|
effect: (fn) => { fn(); return () => {}; },
|
|
77
78
|
locale: {
|
|
@@ -92,16 +93,36 @@ const ctx = {
|
|
|
92
93
|
},
|
|
93
94
|
},
|
|
94
95
|
uiWorkspace: {
|
|
95
|
-
|
|
96
|
+
// 0.2.2 refuses a Session the Client catalog does not know.
|
|
97
|
+
openSession: (id) => {
|
|
98
|
+
if (knownSessions[id] === undefined) throw new Error(`sessions.retain: unknown session ${id}`);
|
|
99
|
+
calls.push(['openSession', id]);
|
|
100
|
+
},
|
|
101
|
+
},
|
|
102
|
+
sessions: {
|
|
103
|
+
list: { getSnapshot: () => ({ byId: knownSessions }) },
|
|
104
|
+
refresh: () => { calls.push(['sessions.refresh']); knownSessions['child-1'] = { sessionId: 'child-1' }; return Promise.resolve(); },
|
|
96
105
|
},
|
|
97
106
|
slots: {
|
|
98
107
|
inject: (name, callback) => { callback(); return () => {}; },
|
|
99
|
-
|
|
108
|
+
// 0.2.2 rejects a second keyed entry at the same priority; a lower one shadows.
|
|
109
|
+
register: (options, component) => {
|
|
110
|
+
const clash = registrations.some((entry) => entry.options.name === options.name
|
|
111
|
+
&& entry.options.key !== undefined
|
|
112
|
+
&& entry.options.key === options.key
|
|
113
|
+
&& (entry.options.priority ?? 0) === (options.priority ?? 0));
|
|
114
|
+
if (clash) throw new Error(`keyed slot "${options.name}" already has an entry for key "${options.key}" at priority ${options.priority ?? 0}`);
|
|
115
|
+
registrations.push({ options, component });
|
|
116
|
+
return () => {};
|
|
117
|
+
},
|
|
100
118
|
},
|
|
101
119
|
};
|
|
102
120
|
|
|
103
121
|
const originalError = globalThis.console.error;
|
|
104
122
|
globalThis.console.error = (...args) => { throw args[0]; };
|
|
123
|
+
// The shipped Chat plugin owns the same keyed seat at the default priority; a
|
|
124
|
+
// same-priority second registration is what 0.2.2 refuses.
|
|
125
|
+
registrations.push({ options: { name: 'conversation.chat.node', key: 'user', locale: '@deepseek-ai/dsh-client-ui-chat' }, component: () => null });
|
|
105
126
|
new Function('window', source)(globalThis.window);
|
|
106
127
|
const definition = globalThis.__definition;
|
|
107
128
|
assert.equal(definition.id, 'dsh-message-rewind');
|
|
@@ -110,15 +131,16 @@ const plugin = definition.factory((id) => {
|
|
|
110
131
|
throw new Error(`unexpected require: ${id}`);
|
|
111
132
|
});
|
|
112
133
|
assert.equal(plugin.name, 'dsh-message-rewind');
|
|
113
|
-
assert.deepEqual(plugin.inject, ['slots', 'uiWorkspace', 'locale', 'connection']);
|
|
134
|
+
assert.deepEqual(plugin.inject, ['slots', 'uiWorkspace', 'locale', 'connection', 'sessions']);
|
|
114
135
|
plugin.apply(ctx);
|
|
115
136
|
globalThis.console.error = originalError;
|
|
116
137
|
|
|
117
138
|
assert.equal(styleTags.length, 1, 'one stylesheet is inserted');
|
|
118
139
|
assert.deepEqual(calls.filter((call) => call[0] === 'locale.register').map((call) => call[2]), ['zh', 'en']);
|
|
119
|
-
const nodeRegistration = registrations.find((entry) => entry.options.key === 'user');
|
|
140
|
+
const nodeRegistration = registrations.find((entry) => entry.options.key === 'user' && entry.options.priority === -1);
|
|
120
141
|
const overlayRegistration = registrations.find((entry) => entry.options.id === 'message-rewind.dialog');
|
|
121
142
|
assert.ok(nodeRegistration, 'the user chat node renderer is registered');
|
|
143
|
+
assert.equal(nodeRegistration.options.priority, -1, 'a lower priority shadows the shipped user row instead of colliding with it');
|
|
122
144
|
assert.ok(overlayRegistration, 'the overlay dialog is registered');
|
|
123
145
|
|
|
124
146
|
// --- render the user message row -------------------------------------------
|
|
@@ -220,6 +242,9 @@ footer().children.filter(Boolean).at(-1).props.onClick();
|
|
|
220
242
|
await tick();
|
|
221
243
|
assert.ok(!renderDialog(), 'a clean edit closes the popover after forking');
|
|
222
244
|
assert.deepEqual(calls.filter((call) => call[0] === 'openSession'), [['openSession', 'child-1']], 'the branch is opened');
|
|
245
|
+
const openedAt = calls.findIndex((call) => call[0] === 'openSession');
|
|
246
|
+
const refreshedAt = calls.findIndex((call) => call[0] === 'sessions.refresh');
|
|
247
|
+
assert.ok(refreshedAt !== -1 && refreshedAt < openedAt, 'the fresh child is pulled into the client catalog before it is opened');
|
|
223
248
|
await new Promise((resolve) => setTimeout(resolve, 400));
|
|
224
249
|
const purged = calls.filter((call) => call[0] === 'rpc.call' && call[2] === 'purge').at(-1);
|
|
225
250
|
assert.deepEqual(purged[3], { sessionId: 'session-1' }, 'the untouched source is retired');
|
package/files.test.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* File-rewind self-check against a real temporary directory. The recorded
|
|
3
3
|
* diffs are handcrafted to match what `dsh-workspace-changes` produces: it diffs
|
|
4
|
-
* newline-terminated text with jsdiff
|
|
5
|
-
*
|
|
4
|
+
* newline-terminated text with jsdiff but hands back every hunk line without its
|
|
5
|
+
* newline — an empty body is one blank line — and a deletion is
|
|
6
|
+
* `before: true, after: false`.
|
|
6
7
|
*/
|
|
7
8
|
import assert from 'node:assert/strict';
|
|
8
9
|
import { promises as fsp } from 'node:fs';
|
|
@@ -49,6 +50,26 @@ await check('created file is removed', async () => {
|
|
|
49
50
|
await assert.rejects(() => fsp.stat(abs(createdName)));
|
|
50
51
|
});
|
|
51
52
|
|
|
53
|
+
// --- the recorder's hunk lines carry no newline; a blank line is one line ---
|
|
54
|
+
const blankCreatedName = 'blank-created.txt';
|
|
55
|
+
const blankCreatedDiff = textDiff(blankCreatedName, false, true, [{ oldStart: 0, oldLines: 0, newStart: 1, newLines: 3, lines: ['+a', '+', '+b'] }]);
|
|
56
|
+
await check('created file with a blank line is removed', async () => {
|
|
57
|
+
await fsp.writeFile(abs(blankCreatedName), 'a\n\nb\n', 'utf8');
|
|
58
|
+
const targets = await planRewind(readDiffFrom({ '13:0': blankCreatedDiff }), recordsFor([{ seq: 13, files: [{ path: blankCreatedName, display: blankCreatedName }] }]));
|
|
59
|
+
assert.deepEqual([...targets.values()][0].original, { exists: false, text: null });
|
|
60
|
+
await applyRewind(targets);
|
|
61
|
+
await assert.rejects(() => fsp.stat(abs(blankCreatedName)));
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
const blankEditedName = 'blank-edited.txt';
|
|
65
|
+
const blankEditedDiff = textDiff(blankEditedName, true, true, [{ oldStart: 1, oldLines: 4, newStart: 1, newLines: 4, lines: [' a', ' ', '-b', '+B', ' c'] }]);
|
|
66
|
+
await check('blank context line still inverts a modification', async () => {
|
|
67
|
+
await fsp.writeFile(abs(blankEditedName), 'a\n\nB\nc\n', 'utf8');
|
|
68
|
+
const targets = await planRewind(readDiffFrom({ '14:0': blankEditedDiff }), recordsFor([{ seq: 14, files: [{ path: blankEditedName, display: blankEditedName }] }]));
|
|
69
|
+
await applyRewind(targets);
|
|
70
|
+
assert.equal(await fsp.readFile(abs(blankEditedName), 'utf8'), 'a\n\nb\nc\n');
|
|
71
|
+
});
|
|
72
|
+
|
|
52
73
|
// --- a turn that modified a file is reverted -------------------------------
|
|
53
74
|
const editedName = 'edited.txt';
|
|
54
75
|
const editedDiff = textDiff(editedName, true, true, [{ oldStart: 1, oldLines: 3, newStart: 1, newLines: 3, lines: [' zero\n', ' one\n', '-two\n', '+TWO\n'] }]);
|
package/index.js
CHANGED
|
@@ -13,7 +13,7 @@ const SESSIONS_ROOT = join(process.env.DSH_HOME ?? join(homedir(), '.dsh'), 'ses
|
|
|
13
13
|
/** The harness mints `session-<uuid>`; refuse anything that could address a path. */
|
|
14
14
|
const SESSION_ID = /^session-[0-9a-zA-Z]+(?:-[0-9a-zA-Z]+)*$/;
|
|
15
15
|
|
|
16
|
-
export const inject = ['sessionController', 'workspaceChanges', 'workspaceController', 'connection', 'webServer'];
|
|
16
|
+
export const inject = ['sessionController', 'sessionQuery', 'workspaceChanges', 'workspaceController', 'connection', 'webServer'];
|
|
17
17
|
|
|
18
18
|
const text = (error) => (error instanceof Error ? error.message : String(error));
|
|
19
19
|
|
|
@@ -112,8 +112,26 @@ export function apply(ctx) {
|
|
|
112
112
|
return diff;
|
|
113
113
|
}
|
|
114
114
|
|
|
115
|
+
/**
|
|
116
|
+
* Read one session's recorded events and creation metadata without activating
|
|
117
|
+
* it. DSH 0.2.2 replaced `sessionController.inspect` with the cold-safe
|
|
118
|
+
* `sessionQuery.readSession`, whose `session` field is the Session header.
|
|
119
|
+
* @param sessionId - session whose durable log is read.
|
|
120
|
+
* @returns the full event log and the metadata the cut needs.
|
|
121
|
+
*/
|
|
122
|
+
async function readInspection(sessionId) {
|
|
123
|
+
let read;
|
|
124
|
+
try {
|
|
125
|
+
read = await ctx.sessionQuery.readSession(sessionId);
|
|
126
|
+
} catch (error) {
|
|
127
|
+
if (error?.code === 'SESSION_QUERY_SESSION_NOT_FOUND') throw userError('missing-session', '找不到这个会话,它可能已被删除。');
|
|
128
|
+
throw error;
|
|
129
|
+
}
|
|
130
|
+
return { events: read.events, meta: { cwd: read.session.cwd, agentPreset: read.session.agentPreset } };
|
|
131
|
+
}
|
|
132
|
+
|
|
115
133
|
async function load(sessionId, seq) {
|
|
116
|
-
const inspection = await
|
|
134
|
+
const inspection = await readInspection(sessionId);
|
|
117
135
|
const cut = prepareCut(inspection.events, seq);
|
|
118
136
|
const records = cut.records.map((recordSeq) => ({
|
|
119
137
|
seq: recordSeq,
|
package/index.test.js
CHANGED
|
@@ -98,15 +98,17 @@ const makeContext = (overrides = {}) => ({
|
|
|
98
98
|
archiveSession: async ({ sessionId }) => { routeCalls.push(['archive', sessionId]); },
|
|
99
99
|
},
|
|
100
100
|
workspaceChanges: { summary: () => undefined, diff: async () => undefined },
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
101
|
+
sessionQuery: {
|
|
102
|
+
readSession: async () => ({
|
|
103
|
+
session: { id: 'session-source', cwd: 'D:/tmp', agentPreset: 'preset-a' },
|
|
104
104
|
events: [
|
|
105
105
|
{ seq: 0, type: 'turn/start', data: { turn: 1 } },
|
|
106
106
|
{ seq: 1, type: 'user/message', data: { source: { kind: 'user' }, content: [{ type: 'text', text: 'hi' }] } },
|
|
107
107
|
{ seq: 2, type: 'turn/end', data: { turn: 1, reason: { kind: 'completed' } } },
|
|
108
108
|
],
|
|
109
109
|
}),
|
|
110
|
+
},
|
|
111
|
+
sessionController: {
|
|
110
112
|
fork: async () => ({ sessionId: 'session-child' }),
|
|
111
113
|
create: async (request) => { routeCalls.push(['create', request]); return { sessionId: 'session-child' }; },
|
|
112
114
|
prompt: async () => ({ accepted: true }),
|
|
@@ -175,17 +177,14 @@ await check('execute binds the replacement to the source workspace', async () =>
|
|
|
175
177
|
|
|
176
178
|
await check('an open turn is refused with a localizable code', async () => {
|
|
177
179
|
const context = makeContext({
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
180
|
+
sessionQuery: {
|
|
181
|
+
readSession: async () => ({
|
|
182
|
+
session: { id: 'session-source', cwd: 'D:/tmp' },
|
|
181
183
|
events: [
|
|
182
184
|
{ seq: 0, type: 'turn/start', data: { turn: 1 } },
|
|
183
185
|
{ seq: 1, type: 'user/message', data: { source: { kind: 'user' }, content: [{ type: 'text', text: 'hi' }] } },
|
|
184
186
|
],
|
|
185
187
|
}),
|
|
186
|
-
fork: async () => ({ sessionId: 'session-child' }),
|
|
187
|
-
create: async () => ({ sessionId: 'session-child' }),
|
|
188
|
-
prompt: async () => ({ accepted: true }),
|
|
189
188
|
},
|
|
190
189
|
});
|
|
191
190
|
apply(context);
|
|
@@ -193,6 +192,20 @@ await check('an open turn is refused with a localizable code', async () => {
|
|
|
193
192
|
assert.equal(envelope.result.error.details.rewindCode, 'open-turn');
|
|
194
193
|
});
|
|
195
194
|
|
|
195
|
+
await check('a deleted session is reported with a localizable code', async () => {
|
|
196
|
+
const context = makeContext({
|
|
197
|
+
sessionQuery: {
|
|
198
|
+
readSession: async () => {
|
|
199
|
+
throw Object.assign(new Error('session "session-source" not found'), { code: 'SESSION_QUERY_SESSION_NOT_FOUND' });
|
|
200
|
+
},
|
|
201
|
+
},
|
|
202
|
+
});
|
|
203
|
+
apply(context);
|
|
204
|
+
const { envelope } = await rpc(registrations.at(-1).handler, 'inspect', { sessionId: 'session-source', seq: 1 });
|
|
205
|
+
assert.equal(envelope.result.ok, false);
|
|
206
|
+
assert.equal(envelope.result.error.details.rewindCode, 'missing-session');
|
|
207
|
+
});
|
|
208
|
+
|
|
196
209
|
await check('purge reports a failed archive instead of deleting', async () => {
|
|
197
210
|
const context = makeContext({
|
|
198
211
|
workspaceController: { archiveSession: async () => { throw new Error('workspace/session-active'); } },
|
package/manifest.test.js
CHANGED
|
@@ -11,8 +11,8 @@ const pkg = JSON.parse(await readFile(new URL('./package.json', import.meta.url)
|
|
|
11
11
|
|
|
12
12
|
assert.equal(pkg.name, 'dsh-message-rewind');
|
|
13
13
|
assert.match(pkg.version, /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/);
|
|
14
|
-
// Bounded to the tested DSH release line: rc.
|
|
15
|
-
assert.match(pkg.engines?.dsh ?? '', /^>=0\.2\.
|
|
14
|
+
// Bounded to the tested DSH release line: 0.2.2-rc.1 through 0.2.x, never 0.3.
|
|
15
|
+
assert.match(pkg.engines?.dsh ?? '', /^>=0\.2\.2-rc\.\d+ <0\.3\.0-0$/);
|
|
16
16
|
assert.equal(pkg.engines?.node, '>=20');
|
|
17
17
|
assert.equal(pkg.dsh?.manifestVersion, 1);
|
|
18
18
|
assert.equal(pkg.dsh?.bundle?.patch, './cordis.patch.yml');
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-message-rewind",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.2",
|
|
4
4
|
"description": "DeepSeek Harness plugin: edit sent messages and roll back following turns, optionally restoring recorded file changes.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "xCube007",
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
],
|
|
43
43
|
"engines": {
|
|
44
44
|
"node": ">=20",
|
|
45
|
-
"dsh": ">=0.2.
|
|
45
|
+
"dsh": ">=0.2.2-rc.1 <0.3.0-0"
|
|
46
46
|
},
|
|
47
47
|
"scripts": {
|
|
48
48
|
"test": "node manifest.test.js && node rewind.test.js && node files.test.js && node index.test.js && node client.test.js",
|
package/rewind.js
CHANGED
|
@@ -65,6 +65,19 @@ export function terminated(text) {
|
|
|
65
65
|
return text.endsWith('\n') ? text : `${text}\n`;
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
+
/**
|
|
69
|
+
* One hunk line's body with its terminating newline. The recorder's jsdiff
|
|
70
|
+
* output drops the newline from every hunk line, so an empty body means one
|
|
71
|
+
* blank line rather than no line; older fixtures keep the newline. Both shapes
|
|
72
|
+
* must read as exactly one line ending.
|
|
73
|
+
* @param body - the text after the leading marker.
|
|
74
|
+
* @returns the body with one terminating newline.
|
|
75
|
+
*/
|
|
76
|
+
function lineText(body) {
|
|
77
|
+
if (body === '') return '\n';
|
|
78
|
+
return body.endsWith('\n') ? body : `${body}\n`;
|
|
79
|
+
}
|
|
80
|
+
|
|
68
81
|
/**
|
|
69
82
|
* Undo one recorded diff against the current text.
|
|
70
83
|
* A hunk carries only changed and context lines; unrelated trailing lines are retained.
|
|
@@ -84,7 +97,7 @@ export function inversePatch(current, hunks) {
|
|
|
84
97
|
cursor = start;
|
|
85
98
|
for (const line of hunk.lines) {
|
|
86
99
|
const marker = line[0];
|
|
87
|
-
const body =
|
|
100
|
+
const body = lineText(line.slice(1));
|
|
88
101
|
if (marker === '\\') throw userError('unsupported-diff', '无结尾换行的文件差异暂不支持,未回滚任何文件。');
|
|
89
102
|
if (marker === '+') {
|
|
90
103
|
if (lines[cursor] !== body) throw userError('file-changed', '文件版本已改变,未回滚任何文件。');
|
|
@@ -115,7 +128,7 @@ export function textSide(exists, hunks, after) {
|
|
|
115
128
|
for (const hunk of hunks) {
|
|
116
129
|
for (const line of hunk.lines) {
|
|
117
130
|
const wanted = after ? line[0] !== '-' : line[0] !== '+';
|
|
118
|
-
if (wanted && line[0] !== '\\') lines.push(
|
|
131
|
+
if (wanted && line[0] !== '\\') lines.push(lineText(line.slice(1)));
|
|
119
132
|
}
|
|
120
133
|
}
|
|
121
134
|
if (!exists) {
|
package/rewind.test.js
CHANGED
|
@@ -72,5 +72,18 @@ assert.equal(terminated(''), '');
|
|
|
72
72
|
assert.equal(inversePatch('abc', [{ oldStart: 0, oldLines: 0, newStart: 1, newLines: 1, lines: ['+abc'] }]), '');
|
|
73
73
|
const unterminated = [{ oldStart: 1, oldLines: 1, newStart: 1, newLines: 2, lines: [' x\n', '+y'] }];
|
|
74
74
|
assert.equal(inversePatch('x\ny', unterminated), 'x\n');
|
|
75
|
+
// The recorder's jsdiff output strips the newline from every hunk line, and an
|
|
76
|
+
// empty body is one blank line rather than no line; both shapes must invert.
|
|
77
|
+
const stripped = [{ oldStart: 1, oldLines: 4, newStart: 1, newLines: 4, lines: [' a', ' ', '-b', '+B', ' c'] }];
|
|
78
|
+
assert.equal(textSide(true, stripped, true), 'a\n\nB\nc\n');
|
|
79
|
+
assert.equal(textSide(true, stripped, false), 'a\n\nb\nc\n');
|
|
80
|
+
assert.equal(inversePatch('a\n\nB\nc\n', stripped), 'a\n\nb\nc\n');
|
|
81
|
+
const insertedBlank = [{ oldStart: 1, oldLines: 4, newStart: 1, newLines: 5, lines: [' a', ' ', ' b', '+', ' c'] }];
|
|
82
|
+
assert.equal(textSide(true, insertedBlank, true), 'a\n\nb\n\nc\n');
|
|
83
|
+
assert.equal(inversePatch('a\n\nb\n\nc\n', insertedBlank), 'a\n\nb\nc\n');
|
|
84
|
+
const createdBlank = [{ oldStart: 0, oldLines: 0, newStart: 1, newLines: 3, lines: ['+a', '+', '+b'] }];
|
|
85
|
+
assert.equal(textSide(true, createdBlank, true), 'a\n\nb\n');
|
|
86
|
+
assert.equal(textSide(false, createdBlank, false), null);
|
|
87
|
+
|
|
75
88
|
console.log('rewind self-check passed');
|
|
76
89
|
|