dsh-ws-recency 0.27.0 → 0.28.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.
Files changed (3) hide show
  1. package/README.md +5 -2
  2. package/index.js +167 -23
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -80,11 +80,14 @@ DSH 原生的置顶只把会话顶到**它所属工作区分组的第一行**,
80
80
 
81
81
  **不是物理删除**:目录只是被改名搬走。每个条目在 `.trash/ws-recency/` 下带一份 `.wsr-trash.json`(会话 id、原 cwd、时间、来源),回收站面板会列出它们:
82
82
 
83
- - 「恢复」:按记录的 cwd 算回 `sessions/<project>/<id>/`,并重新挂到对应工作区;
83
+ - 「恢复」:按记录的 cwd 算回 `sessions/<project>/<id>/`,重新挂到对应工作区,最后摘掉删除时打上的归档标记;
84
+ - 删除会**保留归档标记**(原来没有的会补上):日志已经离开 `sessions/`,宿主的列表里没有它,而归档是唯一一个其它插件都认识的“已收起来”状态——dsh-context 之类的看板就是靠它把已删会话显示成不可点。恢复时摘标记、彻底删除时先摘标记再删文件(0.28.0 起);
84
85
  - 「彻底删除」:真正删掉这份日志(二次确认);
85
86
  - 每条记录是一张**卡片**:标题、来源(已删除 / 移动前的旧副本 / 已归档)、轮次、步骤、token 总量、日志大小、时间、原目录(归档的统计按日志大小缓存,第一次读完后不再重算);
86
87
  已删除会话的标题与统计在删除时就写进 `.wsr-trash.json`,旧条目则在第一次打开面板时从日志里补算并缓存;
87
88
  - 恢复/扫描/移动之后宿主会广播 `api-session/added`;面板与「扫描并刷新」都能重载页面(不重启应用)让侧栏立刻看到改动;
89
+ - 广播里带**列表投影**(标题、最后提问时间来自投影缓存,缓存没记录时读日志第一页):侧栏是靠这些投影把会话分进工作区分组的,只有摘要没有投影时,会话会“进了列表但不进分组”,看起来像没恢复——0.27.1 修;
90
+ - 恢复 = 恢复日志 + 重新挂到工作区 + 广播;先归档再删除的会话,恢复后重新出现在原工作区分组里,不需要重启;
88
91
  - 移动操作留下的旧副本会标成「移动时的旧副本」,只允许彻底删除;
89
92
  - 所有确认(删除、恢复、彻底删除、移动、取消归档)都是**页面内弹层**,不调用系统 `confirm`/`alert`:系统对话框在 Windows 上关掉后会让渲染进程丢掉键盘焦点,表现为输入框点不进去、切换会话也没用,只能切走再切回来(0.24.2 修)。
90
93
 
@@ -197,7 +200,7 @@ dsh plugin --profile <profile> add github:chyxhtc/dsh-ws-recency
197
200
  或固定版本的预构建包:
198
201
 
199
202
  ```powershell
200
- dsh plugin --profile <profile> add https://github.com/chyxhtc/dsh-ws-recency/releases/download/v0.27.0/dsh-ws-recency-0.27.0.tgz
203
+ dsh plugin --profile <profile> add https://github.com/chyxhtc/dsh-ws-recency/releases/download/v0.28.0/dsh-ws-recency-0.28.0.tgz
201
204
  ```
202
205
 
203
206
  装完把 `dsh-ws-recency` 加进 `~/.hypers-work/profiles/<profile>/package.json` 的 `dsh.profile.bundles`,重启应用。
package/index.js CHANGED
@@ -32,7 +32,7 @@ import { dirname, join } from "node:path";
32
32
  export const name = "ws-recency";
33
33
 
34
34
  /** Version marker reported by the health route. */
35
- export const HOST_VERSION = "0.27.0";
35
+ export const HOST_VERSION = "0.28.0";
36
36
 
37
37
  /**
38
38
  * Exact routes the client half calls. Registered on the bare web server, so they
@@ -130,6 +130,10 @@ async function moveWithRetry(source, target) {
130
130
  await rename(source, target);
131
131
  return true;
132
132
  } catch (error) {
133
+ /* Only a momentary lock is worth retrying; "not there" means the move
134
+ cannot happen at all, and treating it as success would leave the caller
135
+ with an empty trash entry and no error. */
136
+ if (error?.code === "ENOENT") return false;
133
137
  if (attempt === RENAME_RETRY - 1) throw error;
134
138
  await new Promise((resolve) => setTimeout(resolve, RENAME_GAP_MS));
135
139
  }
@@ -187,9 +191,18 @@ export async function trashSession(ctx, sessionId) {
187
191
  }
188
192
  await new Promise((resolve) => setTimeout(resolve, STOP_SETTLE_MS));
189
193
 
190
- try { await ctx.workspaceRegistry.unarchiveSession(sessionId); } catch { /* not archived */ }
191
194
  try { await ctx.workspaceRegistry.unpinSession(sessionId); } catch { /* not pinned */ }
192
195
 
196
+ /* Keep the archive flag instead of clearing it. A trashed session whose log
197
+ moved out of `sessions/` is invisible to the Host's listing, and the flag is
198
+ the only state other plugins can read; dropping it is what made a deleted
199
+ session look alive on boards that keep their own cache. The move below is
200
+ undone with the flag when it fails, so the session returns to its place. */
201
+ const wasArchived = archivedSessionIds(ctx).includes(sessionId);
202
+ if (!wasArchived) {
203
+ try { await ctx.workspaceRegistry.archiveSession(sessionId); } catch { /* an older Host may not offer it */ }
204
+ }
205
+
193
206
  /* Same filtered-view trap as the move: the workspace's own sessionIds may
194
207
  already hide the id while the stored record still lists it. */
195
208
  const rawMembers = await rawWorkspaceMembership();
@@ -215,24 +228,36 @@ export async function trashSession(ctx, sessionId) {
215
228
  let moved;
216
229
  try {
217
230
  moved = await trashSessionLog(ctx, meta, trashRoot);
218
- if (moved !== undefined) {
219
- /* Card facts are captured now, while the log is still on this machine:
220
- the list only has to read the sidecar afterwards. */
221
- const card = await trashEntryMetadata(moved);
222
- await writeTrashSidecar(moved, {
223
- sessionId: meta.id,
224
- cwd: typeof meta.cwd === "string" ? meta.cwd : null,
225
- reason: "delete",
226
- at: Date.now(),
227
- ...(card === null ? {} : { meta: card }),
228
- });
229
- }
231
+ if (moved === undefined) {
232
+ /* Nothing was parked and nothing was lost: the session could not even be
233
+ listed, so it may have been erased out of band. Roll the flag back. */
234
+ if (!wasArchived) {
235
+ try { await ctx.workspaceRegistry.unarchiveSession(sessionId); } catch { /* best effort */ }
236
+ }
237
+ return { ok: false, error: "the session's log is not on disk" };
238
+ }
239
+ /* Card facts are captured now, while the log is still on this machine:
240
+ the list only has to read the sidecar afterwards. */
241
+ const card = await trashEntryMetadata(moved);
242
+ await writeTrashSidecar(moved, {
243
+ sessionId: meta.id,
244
+ cwd: typeof meta.cwd === "string" ? meta.cwd : null,
245
+ reason: "delete",
246
+ at: Date.now(),
247
+ /* Restore has to know it owes the Host an unarchive. */
248
+ archived: true,
249
+ ...(card === null ? {} : { meta: card }),
250
+ });
230
251
  } catch (error) {
252
+ /* The flag would otherwise hide a session whose log never left the disk. */
253
+ if (!wasArchived) {
254
+ try { await ctx.workspaceRegistry.unarchiveSession(sessionId); } catch { /* best effort */ }
255
+ }
231
256
  return { ok: false, error: error instanceof Error ? error.message : String(error) };
232
257
  }
233
258
 
234
259
  ctx.emit("api-session/removed", sessionId);
235
- return { ok: true, trash: moved };
260
+ return { ok: true, trash: moved, archivedKept: true };
236
261
  }
237
262
 
238
263
  /** POST { sessionId } -> move that session into the trash folder. */
@@ -1168,6 +1193,60 @@ export async function announceSession(ctx, sessionId) {
1168
1193
  const stat = await ctx.sessionPersistence.stat(sessionId);
1169
1194
  const header = stat !== undefined && stat !== null ? stat.header : undefined;
1170
1195
  if (header !== undefined && header !== null) {
1196
+ /* The sidebar groups by workspace through list-surface projections; a
1197
+ summary without them lands in the list but never in a workspace group.
1198
+ Read them from the projection cache (cheap) and fall back to the first
1199
+ prompt of the log (one page) when the cache has no record. */
1200
+ let values = null;
1201
+ const checkpoint = cachedCheckpoint(ctx, sessionId);
1202
+ if (checkpoint !== undefined) {
1203
+ const projections = {};
1204
+ const title = checkpointValue(checkpoint, "title");
1205
+ if (typeof title === "string" && title.trim() !== "") projections.title = title.trim();
1206
+ const listMeta = checkpointValue(checkpoint, "sessionListMetadata");
1207
+ if (listMeta !== null && typeof listMeta === "object") {
1208
+ if (typeof listMeta.lastPromptAt === "number") projections.lastPromptAt = listMeta.lastPromptAt;
1209
+ if (listMeta.blank === true) projections.blank = true;
1210
+ }
1211
+ if (Object.keys(projections).length > 0) values = projections;
1212
+ }
1213
+ if (values !== null) {
1214
+ ctx.emit("api-session/added", {
1215
+ sessionId: header.id,
1216
+ updatedAt: typeof header.updatedAt === "number" ? header.updatedAt : Date.now(),
1217
+ agentAvailable: false,
1218
+ formatStatus: "current",
1219
+ running: false,
1220
+ blank: values.blank === true,
1221
+ title: values.title ?? header.title,
1222
+ cwd: header.cwd,
1223
+ createdAt: header.createdAt,
1224
+ projections: { kind: "cached", values },
1225
+ });
1226
+ return true;
1227
+ }
1228
+ const first = await firstPromptOf(ctx, sessionId);
1229
+ if (first.title !== "" || first.lastPromptAt > 0) {
1230
+ ctx.emit("api-session/added", {
1231
+ sessionId: header.id,
1232
+ updatedAt: typeof header.updatedAt === "number" ? header.updatedAt : Date.now(),
1233
+ agentAvailable: false,
1234
+ formatStatus: "current",
1235
+ running: false,
1236
+ blank: first.title === "" && first.lastPromptAt === 0,
1237
+ title: first.title !== "" ? first.title : header.title,
1238
+ cwd: header.cwd,
1239
+ createdAt: header.createdAt,
1240
+ projections: {
1241
+ kind: "cached",
1242
+ values: {
1243
+ ...(first.title !== "" ? { title: first.title } : {}),
1244
+ ...(first.lastPromptAt > 0 ? { lastPromptAt: first.lastPromptAt } : {}),
1245
+ },
1246
+ },
1247
+ });
1248
+ return true;
1249
+ }
1171
1250
  ctx.emit("api-session/added", {
1172
1251
  sessionId: header.id,
1173
1252
  updatedAt: typeof header.updatedAt === "number" ? header.updatedAt : Date.now(),
@@ -1187,6 +1266,45 @@ export async function announceSession(ctx, sessionId) {
1187
1266
  return false;
1188
1267
  }
1189
1268
 
1269
+ /** Title and first prompt time from the log's first page; empty when unreadable. */
1270
+ async function firstPromptOf(ctx, sessionId) {
1271
+ let handle;
1272
+ try {
1273
+ handle = await ctx.sessionPersistence.open(sessionId, "read");
1274
+ } catch {
1275
+ return { title: "", lastPromptAt: 0 };
1276
+ }
1277
+ try {
1278
+ const page = await handle.read(0, 500);
1279
+ const events = page !== null && page !== undefined && Array.isArray(page.events) ? page.events : [];
1280
+ let title = "";
1281
+ let lastPromptAt = 0;
1282
+ for (const event of events) {
1283
+ if (event === null || typeof event !== "object") continue;
1284
+ if (event.type === "session/title") {
1285
+ const value = event.data?.title ?? event.data;
1286
+ if (typeof value === "string" && value.trim() !== "") title = value.trim();
1287
+ } else if (event.type === "user/message" && lastPromptAt === 0 && typeof event.time === "number") {
1288
+ lastPromptAt = event.time;
1289
+ if (title === "") {
1290
+ const text = conversationText(event.data?.message?.content ?? event.data?.content);
1291
+ const line = text.trim().split("\n")[0];
1292
+ if (line !== "") title = line.slice(0, 120);
1293
+ }
1294
+ }
1295
+ }
1296
+ return { title, lastPromptAt };
1297
+ } catch {
1298
+ return { title: "", lastPromptAt: 0 };
1299
+ } finally {
1300
+ try {
1301
+ if (typeof handle.close === "function") await handle.close();
1302
+ } catch {
1303
+ /* closing is best effort */
1304
+ }
1305
+ }
1306
+ }
1307
+
1190
1308
  /** The session root's project-directory key: separators collapse to `-`. */
1191
1309
  export function projectKey(cwd) {
1192
1310
  if (typeof cwd !== "string" || cwd === "") return undefined;
@@ -1393,6 +1511,14 @@ export async function restoreTrashEntry(ctx, name) {
1393
1511
  } catch (error) {
1394
1512
  return { ok: false, error: `could not move the files back: ${error instanceof Error ? error.message : String(error)}` };
1395
1513
  }
1514
+ /* The archive flag the delete kept is dropped only now: with the log back in
1515
+ place but the flag still set, the archive panel would show a record-only
1516
+ row for a session that has its log again. */
1517
+ try {
1518
+ await ctx.workspaceRegistry.unarchiveSession(sidecar.sessionId);
1519
+ } catch {
1520
+ /* not archived (older entries) or the Host refused; the attach below still runs */
1521
+ }
1396
1522
  const workspaces = ctx.workspaceRegistry.list();
1397
1523
  const owner = workspaces.find((workspace) => workspace.path === sidecar.cwd);
1398
1524
  let attached = false;
@@ -1428,13 +1554,31 @@ export async function restoreTrashEntry(ctx, name) {
1428
1554
  return { ok: true, sessionId: sidecar.sessionId, cwd: sidecar.cwd, attached };
1429
1555
  }
1430
1556
 
1431
- /** Delete one trash entry for good. */
1432
- export async function purgeTrashEntry(name) {
1557
+ /**
1558
+ * Erase one trash entry for good.
1559
+ *
1560
+ * A trashed session keeps the Host's archive flag (that flag is how the rest of
1561
+ * the app knows it is put away), so purging has to drop it first - otherwise the
1562
+ * archive set would keep an id whose log no longer exists.
1563
+ */
1564
+ export async function purgeTrashEntry(name, ctx = null) {
1433
1565
  const root = await trashRoot();
1434
1566
  if (name === "" || name.indexOf("..") !== -1 || name.indexOf("/") !== -1 || name.indexOf("\\") !== -1) {
1435
1567
  return { ok: false, error: "invalid entry name" };
1436
1568
  }
1437
- await rm(join(root, name), { recursive: true, force: true });
1569
+ const directory = join(root, name);
1570
+ if (ctx !== null && ctx !== undefined) {
1571
+ const sidecar = await readTrashSidecar(directory);
1572
+ const sessionId = sidecar !== null && typeof sidecar.sessionId === "string" ? sidecar.sessionId : "";
1573
+ if (sessionId !== "") {
1574
+ try {
1575
+ await ctx.workspaceRegistry.unarchiveSession(sessionId);
1576
+ } catch {
1577
+ /* not archived, or the Host refused; erasing the files still proceeds */
1578
+ }
1579
+ }
1580
+ }
1581
+ await rm(directory, { recursive: true, force: true });
1438
1582
  return { ok: true };
1439
1583
  }
1440
1584
 
@@ -1733,9 +1877,9 @@ export async function detachMissingArchive(ctx, sessionId) {
1733
1877
  /**
1734
1878
  * Delete an archived session for good.
1735
1879
  *
1736
- * The normal delete already unarchives, detaches and moves the log into the
1737
- * plugin's trash - erasing it is then the trash's own purge, so the two paths
1738
- * cannot drift apart.
1880
+ * The normal delete detaches, keeps the archive flag and moves the log into the
1881
+ * plugin's trash - erasing it is then the trash's own purge (which drops the
1882
+ * flag before the files), so the two paths cannot drift apart.
1739
1883
  */
1740
1884
  export async function forgetArchivedSession(ctx, sessionId) {
1741
1885
  const result = await trashSession(ctx, sessionId);
@@ -1745,7 +1889,7 @@ export async function forgetArchivedSession(ctx, sessionId) {
1745
1889
  const name = result.trash.split(/[/\\]/).filter((part) => part !== "").pop();
1746
1890
  if (typeof name === "string" && name !== "") {
1747
1891
  try {
1748
- purged = (await purgeTrashEntry(name)).ok === true;
1892
+ purged = (await purgeTrashEntry(name, ctx)).ok === true;
1749
1893
  } catch {
1750
1894
  purged = false;
1751
1895
  }
@@ -1906,7 +2050,7 @@ export async function handleTrashListRequest(ctx, req, res) {
1906
2050
  return;
1907
2051
  }
1908
2052
  if (action === "purge") {
1909
- sendJson(res, 200, await purgeTrashEntry(name));
2053
+ sendJson(res, 200, await purgeTrashEntry(name, ctx));
1910
2054
  return;
1911
2055
  }
1912
2056
  sendJson(res, 400, { ok: false, error: "unknown action" });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-ws-recency",
3
- "version": "0.27.0",
3
+ "version": "0.28.0",
4
4
  "description": "Sidebar session management for DeepSeek Harness: workspace groups sorted by latest activity, stale history folded, a global pinned section, follow-up marks, session delete into a recoverable trash, archived sessions managed from that same trash panel, per-session token statistics with an argument/result split and an option to count tool arguments as the agent's writing, moving a session to another workspace, and a literal content search that jumps to the turn a word was said in.",
5
5
  "license": "MIT",
6
6
  "author": "chyxhtc",