dsh-xlock 0.0.0-stage → 0.2.9

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/client.js ADDED
@@ -0,0 +1,307 @@
1
+ /**
2
+ * dsh-xlock —— 浏览器半(会话行标记 + 原生菜单动作)
3
+ *
4
+ * 数据全部来自宿主半的只读路由:
5
+ * GET /dsh-xlock/session-state 每会话的锁 / 归档 / 活跃度
6
+ * GET /dsh-xlock/session-ids 全量会话 id(给平台列表做集合变化检测)
7
+ * POST /dsh-xlock/session-state/reset {sessionId} 只删锁标记,归档不受影响
8
+ *
9
+ * 视觉语言(用户 2026-11-19 定稿):
10
+ * 圆环 实线 + 呼吸 = 另一台机器**真在写**
11
+ * 圆环 虚线 + 灰淡 = 只剩锁标记(残锁),超时也不隐藏——那正是要解锁的情形
12
+ * 圆点 实线 = 创建归属来自创建记录;点线 = 由目录桶推断
13
+ * 不再画「锁 / 归」文字徽标:锁与圆环虚线态同源、信息更少;归档态平台自己已经用
14
+ * 行淡化 + 悬浮卡片表达了,再标一次就是重复。
15
+ *
16
+ * 加载器契约(DSH 硬规则):必须是
17
+ * window.__ModuleLoader__.load({ id, factory: (require) => exports })
18
+ * 旧 `{ id, exports: fn }` 形态会崩 web boot。factory 返回的对象必须声明
19
+ * `inject`,否则平台不发放 slots 席位(ctx.slots 取不到 → 静默不注册)。
20
+ */
21
+
22
+ window.__ModuleLoader__.load({
23
+ id: "dsh-xlock",
24
+ factory: function (require) {
25
+ "use strict";
26
+ var React = require("react");
27
+
28
+ var STATE = "/dsh-xlock/session-state";
29
+ var RESET = "/dsh-xlock/session-state/reset";
30
+ var IDS = "/dsh-xlock/session-ids";
31
+ var REFRESH_MS = 15000;
32
+ var CSS_TEXT = [
33
+ '.dxl-ring{display:inline-block;width:11px;height:11px;border-radius:50%;box-sizing:border-box;cursor:default;vertical-align:middle}',
34
+ '.dxl-ring[data-activity="writing"]{border:2px solid ***REMOVED***f0883e;animation:dxl-pulse 1.6s ease-in-out infinite}',
35
+ '.dxl-ring[data-activity="silent"]{border:2px dashed ***REMOVED***8b949e;opacity:.6}',
36
+ '.dxl-origin{display:inline-block;width:8px;height:8px;border-radius:50%;box-sizing:border-box;cursor:default;vertical-align:middle;margin-left:3px}',
37
+ '.dxl-origin[data-source="record"]{border:1.5px solid ***REMOVED***58a6ff}',
38
+ '.dxl-origin[data-source="bucket"]{border:1.5px dotted ***REMOVED***58a6ff;opacity:.75}',
39
+ '@keyframes dxl-pulse{0%,100%{opacity:1}50%{opacity:.35}}',
40
+ '@media (prefers-reduced-motion:reduce){.dxl-ring[data-activity="writing"]{animation:none}}',
41
+ ].join("");
42
+
43
+ // ── 状态容器 + 订阅(失败保留旧值标 stale,绝不因为一次拉取失败清空标记)──
44
+ var state = { byId: {}, loaded: false, stale: false };
45
+ var listeners = [];
46
+
47
+ function notify() {
48
+ for (var i = 0; i < listeners.length; i++) {
49
+ try { listeners[i](state); } catch (e) {}
50
+ }
51
+ }
52
+
53
+ function readDoc(doc) {
54
+ var byId = {};
55
+ var list = doc && Array.isArray(doc.sessions) ? doc.sessions : [];
56
+ for (var i = 0; i < list.length; i++) {
57
+ var item = list[i];
58
+ if (!item || typeof item.sessionId !== "string") continue;
59
+ byId[item.sessionId] = item;
60
+ }
61
+ return { byId: byId, loaded: true, stale: false };
62
+ }
63
+
64
+ function refresh() {
65
+ return fetch(STATE, { headers: { accept: "application/json" } })
66
+ .then(function (r) { return r.json(); })
67
+ .then(function (doc) { state = readDoc(doc); notify(); return state; })
68
+ .catch(function () {
69
+ state = { byId: state.byId || {}, loaded: state.loaded, stale: true };
70
+ notify();
71
+ return state;
72
+ });
73
+ }
74
+
75
+ function useXlock() {
76
+ var pair = React.useState(state);
77
+ var value = pair[0];
78
+ var setValue = pair[1];
79
+ React.useEffect(function () {
80
+ function listener(next) { setValue(next); }
81
+ listeners.push(listener);
82
+ listener(state);
83
+ return function () {
84
+ var at = listeners.indexOf(listener);
85
+ if (at >= 0) listeners.splice(at, 1);
86
+ };
87
+ }, []);
88
+ return value;
89
+ }
90
+
91
+ function hostOf(item) {
92
+ return item.host || (item.createdHost || "");
93
+ }
94
+
95
+ /** 活跃度三态文案(圆环 tooltip)。残锁也如实说明「多久没有新日志」。 */
96
+ function ringLabel(item) {
97
+ var host = hostOf(item) || "另一台机器";
98
+ var act = (item && item.activity) || null;
99
+ var writing = act && act.activity === "writing";
100
+ var freshMs = act && typeof act.freshMs === "number" ? act.freshMs : null;
101
+ if (writing) {
102
+ return "另一台机器(" + host + ")正在运行这个会话";
103
+ }
104
+ if (freshMs !== null) {
105
+ var sec = Math.max(0, Math.round(freshMs / 1000));
106
+ var ago = sec < 90 ? sec + " 秒" : Math.round(sec / 60) + " 分钟";
107
+ return "另一台机器(" + host + ")留下了锁标记,已停止写入 " + ago + ";可在会话菜单「解除跨机锁」";
108
+ }
109
+ return "另一台机器(" + host + ")留下了锁标记,没有可判读的会话日志;可在会话菜单「解除跨机锁」";
110
+ }
111
+
112
+ /**
113
+ * 会话行左侧的活跃度圆环(`sidebar.session.row.leading`)。
114
+ *
115
+ * 只画 **held-foreign**:本机持有 / 已释放 / 无标记都是正常状态,画出来只是噪音。
116
+ * 平台只在本行 primary status 为 idle 时才渲染这个槽(本机自己在跑的会话由平台
117
+ * 自己画状态点),所以两者永不并排。
118
+ *
119
+ * 不设新鲜度截断:残锁(日志早停)也画,只是换成虚线灰淡 —— 那恰是最需要
120
+ * 人看见并解锁的状态。
121
+ */
122
+ function RemoteRunningMark(props) {
123
+ var data = useXlock();
124
+ var sessionId = props && props.sessionId;
125
+ var item = sessionId && data.byId ? data.byId[sessionId] : null;
126
+ if (!item || item.lockState !== "held-foreign") return null;
127
+ var writing = Boolean(item.activity && item.activity.activity === "writing");
128
+ var label = ringLabel(item);
129
+ return React.createElement("span", {
130
+ className: "dxl-ring",
131
+ role: "img",
132
+ "aria-label": label,
133
+ title: label,
134
+ "data-dsh-xlock": hostOf(item) || "unknown",
135
+ "data-activity": writing ? "writing" : "silent",
136
+ });
137
+ }
138
+
139
+ /**
140
+ * 会话行左侧的创建归属圆点(同一格,order 排在圆环之后)。
141
+ *
142
+ * 只画**异机创建**的行:本机建的是默认情况,画满一屏等于噪音;归属未知不画
143
+ * (宁可没有,也不拿锁标记猜)。与圆环同格,所以对端正在跑时让路——
144
+ * 同一格永远只有一个标记。
145
+ */
146
+ function SessionOriginMark(props) {
147
+ var data = useXlock();
148
+ var sessionId = props && props.sessionId;
149
+ var item = sessionId && data.byId ? data.byId[sessionId] : null;
150
+ if (!item) return null;
151
+ if (item.lockState === "held-foreign") return null;
152
+ if (item.createdRemote !== true) return null;
153
+ var source = item.originSource;
154
+ if (source !== "record" && source !== "bucket") return null;
155
+ var host = item.createdHost || "另一台机器";
156
+ var basis = source === "record" ? "创建记录" : "目录桶推断";
157
+ var label = "这条会话是在 " + host + " 上创建的 · 依据" + basis;
158
+ return React.createElement("span", {
159
+ className: "dxl-origin",
160
+ role: "img",
161
+ "aria-label": label,
162
+ title: label,
163
+ "data-dsh-xlock": host,
164
+ "data-source": source,
165
+ });
166
+ }
167
+
168
+ /**
169
+ * 会话「...」菜单 / 右键菜单里的「解除跨机锁」。
170
+ * 走原生 `ctx.contextMenus`:字体、行高、图标位、键盘导航全部由平台渲染,
171
+ * 与「重命名 / 固定 / 归档」等内置项完全一致(自绘 slot button 做不到)。
172
+ * 只在 held-foreign 时产出动作。
173
+ */
174
+ function registerMenu(ctx) {
175
+ return ctx.contextMenus.register({
176
+ id: "xlock.unlock",
177
+ target: "session",
178
+ order: 120,
179
+ observe: function () { return []; },
180
+ resolve: function (target) {
181
+ var sessionId = target && target.sessionId;
182
+ var item = sessionId && state.byId ? state.byId[sessionId] : null;
183
+ if (!item || item.lockState !== "held-foreign") return [];
184
+ var host = hostOf(item) || "异机";
185
+ return [{
186
+ id: "xlock.unlock",
187
+ label: "解除跨机锁(" + host + ")",
188
+ danger: true,
189
+ run: function () {
190
+ return fetch(RESET, {
191
+ method: "POST",
192
+ headers: { "content-type": "application/json" },
193
+ body: JSON.stringify({ sessionId: sessionId }),
194
+ })
195
+ .then(function () { return refresh(); })
196
+ .catch(function () {});
197
+ },
198
+ }];
199
+ },
200
+ });
201
+ }
202
+
203
+ // ── 会话集合变化检测 ────────────────────────────────────────────────
204
+ // 平台只在连接建立时重拉一次会话列表,之后靠事件增量维护。异机删掉一条会话
205
+ // 时本机不会产生 session/disposed,那行会一直挂着。宿主半扫盘能看见「少了一条」,
206
+ // 所以由它给出全量集合;客户端比对集合变了就让平台重拉权威基线(基线里已消失
207
+ // 的身份会被移除)。宁可让平台重算,也不要在客户端自己拼列表。
208
+ var sessionSet = { lastIds: "", refreshCount: 0, lastError: "" };
209
+ var clientCtx = null;
210
+
211
+ function checkSessionSet(ids) {
212
+ var key = (Array.isArray(ids) ? ids.slice().sort() : []).join(",");
213
+ if (key === sessionSet.lastIds) return false;
214
+ sessionSet.lastIds = key;
215
+ refreshPlatformSessions();
216
+ return true;
217
+ }
218
+
219
+ function refreshPlatformSessions() {
220
+ var sessions = null;
221
+ try {
222
+ sessions = clientCtx && typeof clientCtx.get === "function" ? clientCtx.get("sessions") : null;
223
+ } catch (e) {
224
+ sessions = null;
225
+ }
226
+ if (!sessions || typeof sessions.refresh !== "function") {
227
+ sessionSet.lastError = "sessions service unavailable";
228
+ return Promise.resolve(false);
229
+ }
230
+ return Promise.resolve(sessions.refresh()).then(function () {
231
+ sessionSet.refreshCount = sessionSet.refreshCount + 1;
232
+ sessionSet.lastError = "";
233
+ return true;
234
+ }).catch(function (error) {
235
+ sessionSet.lastError = String((error && error.message) || error);
236
+ return false;
237
+ });
238
+ }
239
+
240
+ function pollSessionIds() {
241
+ return fetch(IDS, { headers: { accept: "application/json" } })
242
+ .then(function (r) { return r.json(); })
243
+ .then(function (doc) { checkSessionSet(doc && doc.ids); })
244
+ .catch(function () {});
245
+ }
246
+
247
+ var timer = null;
248
+ var idTimer = null;
249
+ function install() {
250
+ var el = document.createElement("style");
251
+ el.setAttribute("data-plugin-css", "dsh-xlock");
252
+ el.textContent = CSS_TEXT;
253
+ document.head.appendChild(el);
254
+ void refresh();
255
+ void pollSessionIds();
256
+ timer = setInterval(function () { void refresh(); }, REFRESH_MS);
257
+ idTimer = setInterval(function () { void pollSessionIds(); }, REFRESH_MS);
258
+ return function () {
259
+ if (timer) { clearInterval(timer); timer = null; }
260
+ if (idTimer) { clearInterval(idTimer); idTimer = null; }
261
+ if (el && el.parentNode) el.parentNode.removeChild(el);
262
+ };
263
+ }
264
+
265
+ function apply(ctx) {
266
+ clientCtx = ctx;
267
+ ctx.effect(function () { return install(); }, "dsh-xlock: polling and styles");
268
+ ctx.effect(function () { return registerMenu(ctx); }, "dsh-xlock: session menu action");
269
+ ctx.slots.inject("sidebar.session.row.leading", function () {
270
+ return ctx.slots.register({
271
+ name: "sidebar.session.row.leading",
272
+ id: "xlock-remote",
273
+ order: 20,
274
+ label: function () { return "异机运行中"; },
275
+ }, RemoteRunningMark);
276
+ });
277
+ ctx.slots.inject("sidebar.session.row.leading", function () {
278
+ return ctx.slots.register({
279
+ name: "sidebar.session.row.leading",
280
+ id: "xlock-origin",
281
+ order: 30,
282
+ label: function () { return "会话创建归属"; },
283
+ }, SessionOriginMark);
284
+ });
285
+ }
286
+
287
+ var exports = {
288
+ inject: ["slots", "contextMenus"],
289
+ STATE: STATE,
290
+ RESET: RESET,
291
+ IDS: IDS,
292
+ apply: apply,
293
+ readDoc: readDoc,
294
+ refresh: refresh,
295
+ pollSessionIds: pollSessionIds,
296
+ __internals: {
297
+ useXlock: useXlock,
298
+ ringLabel: ringLabel,
299
+ RemoteRunningMark: RemoteRunningMark,
300
+ SessionOriginMark: SessionOriginMark,
301
+ registerMenu: registerMenu,
302
+ checkSessionSet: checkSessionSet,
303
+ },
304
+ };
305
+ return exports;
306
+ },
307
+ });
package/config.json ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "$comment": "dsh-xlock 配置(随同步面共享,各机一致;单机覆盖走 cordis.patch.yml 注入配置)。",
3
+ "retentionDays": 30,
4
+ "exportRetentionDays": 180,
5
+ "maxQuarantineBytes": 536870912,
6
+ "acquireQuiesceMs": 0,
7
+ "divergentGate": "block",
8
+ "startupGc": true,
9
+ "archiveSyncRoute": true
10
+ }
@@ -0,0 +1,10 @@
1
+ ***REMOVED*** dsh-xlock — 跨机粘性会话锁 + 冲突副本治理
2
+ ***REMOVED***
3
+ ***REMOVED*** 把本包插进 profile 的条目列表:cordis 加载 index.js(宿主半),
4
+ ***REMOVED*** apply() 里 ctx.inject(['sessionPersistence']) 后对实例做 open/create
5
+ ***REMOVED*** 包装(跨机锁标记 + 尾部修复 + 冲突副本门禁)。详见包内 README.md。
6
+
7
+ - insert:
8
+ - id: dsh-xlock
9
+ name: 'dsh-xlock'
10
+ config: {}
@@ -0,0 +1,195 @@
1
+ /**
2
+ * dsh-xlock — 归档标记:把「归档」这个跨机传不过来的状态,落到会话目录里。
3
+ *
4
+ * 平台把归档集存在 `~/.dsh/storages/workspace.json` 的 `global.archivedSessionIds`,
5
+ * 而 `storages` **不在同步面上**,于是对端归档一条会话,本机永远不知道 ——
6
+ * 侧栏照旧挂着那一行。会话目录本身是同步的(锁标记就是这么过来的),所以把
7
+ * 归档状态也写进会话目录:谁写都同步得到,且不依赖任何平台私有存储。
8
+ *
9
+ * 本模块(含 core/archivesync.mjs 的双向同步器)整体从 dsh-tweaks 迁入:
10
+ * 「会话相关的跨机同步」从此全部由 dsh-xlock 负责,tweaks 不再参与这件事。
11
+ *
12
+ * 标记文件:`<session-dir>/.dsh-xlock-archive.json`
13
+ * { v:1, archived:boolean, host, machineId, at } // at = epoch ms,仅供排障
14
+ *
15
+ * 命名三点约束(决定这个名字的形状,改动前先看清楚):
16
+ * ① **必须让 core/marker.mjs 的 MARKER_CONFLICT_RE 负向先行放行 `-archive`**,
17
+ * 否则它的 Syncthing 冲突副本会被当成「粘锁争抢痕迹」抛 XlockHeldError,
18
+ * 把该会话的每一次打开都挡死(这正是迁移前 tweaks 取名要避开 `.dsh-xlock`
19
+ * 前缀的原因,迁移成自己人后必须同步改那条正则);
20
+ * ② 与 `.dsh-xlock-origin.json` 同形(单个点开头文件)—— 会话目录是 Syncthing
21
+ * 双向同步面,冲突处理只在**文件**粒度成熟(`<名字>.sync-conflict-…` 副本
22
+ * 能被 xlock 的 core/sessionfs.mjs 盘点、清理)。若写成子目录,冲突会产出
23
+ * 目录副本,既不在任何现有清理路径里,也会一直挂在会话目录里;
24
+ * ③ 后缀 `.json` 让同步冲突名落在 `…archive.json.sync-conflict-…`,同样是文件。
25
+ *
26
+ * 迁移兼容(双读单写):旧名 `.dsh-tweaks-archive.json`(dsh-tweaks 时代写下的
27
+ * 存量)仍可读,写只写新文件名。两台机器都升级到本版本后,旧名自然不再新增。
28
+ */
29
+
30
+ import { existsSync, readdirSync } from 'node:fs'
31
+ import { dirname, join } from 'node:path'
32
+ import { readJsonSafe, ensureDir, atomicWriteText } from './util.mjs'
33
+ import { loadIdentity } from './identity.mjs'
34
+
35
+ /** 归档标记的新文件名(xlock 自己的标记,与锁标记 `.dsh-xlock.json` 同级)。 */
36
+ export const ARCHIVE_MARKER_NAME = '.dsh-xlock-archive.json'
37
+
38
+ /** 旧文件名(dsh-tweaks 时代写下的存量,只读兼容,不再新增)。 */
39
+ export const LEGACY_ARCHIVE_MARKER_NAME = '.dsh-tweaks-archive.json'
40
+
41
+ /**
42
+ * 归档标记(新旧两个名字)+ 任意 Syncthing 冲突副本后缀。
43
+ * 供 core/sessionfs.mjs 分类时复用,避免别处再硬编码一次文件名(耦合病根)。
44
+ */
45
+ export const ARCHIVE_MARKER_RE =
46
+ /^\.dsh-(?:xlock|tweaks)-archive\.json(\.sync-conflict-\d{8}-\d{6}-[A-Za-z0-9]+\.\w+)?$/
47
+
48
+ /**
49
+ * 会话目录里的归档标记完整路径(新名)。
50
+ * @param {string} sessionDir
51
+ * @returns {string}
52
+ */
53
+ export function archiveMarkerPath(sessionDir) {
54
+ return join(sessionDir, ARCHIVE_MARKER_NAME)
55
+ }
56
+
57
+ /**
58
+ * 会话目录里的归档标记旧名完整路径(迁移期读兼容用)。
59
+ * @param {string} sessionDir
60
+ * @returns {string}
61
+ */
62
+ export function legacyArchiveMarkerPath(sessionDir) {
63
+ return join(sessionDir, LEGACY_ARCHIVE_MARKER_NAME)
64
+ }
65
+
66
+ /**
67
+ * 本机身份(host + machineId)—— 写标记时盖个「谁改的」,仅供排障。
68
+ * 复用 xlock 自己的 loadIdentity()(会按需生成 machineId.json),失败退化成
69
+ * 空身份,绝不因为缺身份文件就让归档标记写不下去。
70
+ */
71
+ function archiveIdentity() {
72
+ try {
73
+ const id = loadIdentity()
74
+ return { host: id.host || '', machineId: id.uuid || null }
75
+ } catch {
76
+ return { host: '', machineId: null }
77
+ }
78
+ }
79
+
80
+ /** 会话目录名 → sessionId(目录名以 `session-<uuid>` 结尾时直接用,否则取最后一段)。 */
81
+ export function sessionIdFromDirName(name) {
82
+ if (typeof name !== 'string' || !name) return null
83
+ const dash = name.lastIndexOf('-session-')
84
+ if (dash >= 0) return 'session-' + name.slice(dash + '-session-'.length)
85
+ return name.startsWith('session-') ? name : null
86
+ }
87
+
88
+ /**
89
+ * 读一个会话目录里的归档标记(双读:新文件名优先,不存在才读旧文件名)。
90
+ *
91
+ * 返回 `{ archived, by, at, source }`:
92
+ * · `archived` —— 这份标记说的是「已归档」还是「已取消归档」(显式 false 也是
93
+ * 事实,它能让「对端取消归档」同样跨机传播,而不是只能单向归档);
94
+ * · `by`/`at` —— 谁在什么时候写的(host + machineId + epoch ms),供排障;
95
+ * · `source` —— `marker`(本机读到标记)/ `none`(没有标记)。
96
+ *
97
+ * 缺失或损坏一律当 `none`:归档状态拿不准时**保持平台原状**,绝不因为读不懂
98
+ * 一个文件就把别人的会话从列表里抹掉。新文件存在但损坏时**不**回退读旧文件,
99
+ * 以免拿一份更早世代的旧数据盖掉真实状态。
100
+ */
101
+ export function readArchiveMarker(sessionDir) {
102
+ const primary = archiveMarkerPath(sessionDir)
103
+ const fallback = legacyArchiveMarkerPath(sessionDir)
104
+ const file = existsSync(primary) ? primary : existsSync(fallback) ? fallback : null
105
+ if (!file) return { archived: null, by: null, at: null, source: 'none' }
106
+ const doc = readJsonSafe(file, undefined)
107
+ if (!doc || typeof doc !== 'object' || typeof doc.archived !== 'boolean') {
108
+ return { archived: null, by: null, at: null, source: 'none' }
109
+ }
110
+ return {
111
+ archived: doc.archived,
112
+ by: {
113
+ host: typeof doc.host === 'string' ? doc.host : '',
114
+ machineId: typeof doc.machineId === 'string' ? doc.machineId : '',
115
+ },
116
+ at: Number.isFinite(doc.at) ? doc.at : null,
117
+ source: 'marker',
118
+ }
119
+ }
120
+
121
+ /**
122
+ * 写一个会话目录的归档标记(原子写:temp+rename,复用 core/util.mjs 的
123
+ * atomicWriteText——同目录临时文件 + fsync + rename,比 tweaks 原实现多一层
124
+ * fsync 保障)。
125
+ *
126
+ * **单写**:只写新文件名 `.dsh-xlock-archive.json`,不碰旧文件名(存量那份留着
127
+ * 让还没升级的对端继续读到旧值,双读顺序保证新值优先,不会被旧文件盖回去)。
128
+ *
129
+ * 返回值如实:写成功 `{ ok: true }`,目录不存在/不可写返回 `{ ok: false, error }`,
130
+ * 调用方(同步器)据此决定要不要告警 —— 不假装成功。
131
+ */
132
+ export function writeArchiveMarker(sessionDir, archived, identity = archiveIdentity(), now = Date.now()) {
133
+ const me = identity || archiveIdentity()
134
+ const file = archiveMarkerPath(sessionDir)
135
+ try {
136
+ ensureDir(dirname(file))
137
+ atomicWriteText(
138
+ file,
139
+ JSON.stringify({
140
+ v: 1,
141
+ archived: archived === true,
142
+ host: me.host || '',
143
+ machineId: me.machineId || null,
144
+ at: now,
145
+ }) + '\n',
146
+ )
147
+ return { ok: true }
148
+ } catch (error) {
149
+ return { ok: false, error: String(error?.message ?? error) }
150
+ }
151
+ }
152
+
153
+ /**
154
+ * 扫全部会话,返回**每条会话目录里归档标记说了什么**:
155
+ * `{ sessionId, dir, archived, by, at, source }`(没有标记的也在结果里,archived 为 null)。
156
+ *
157
+ * 纯读取:只读自己那个小 json,不 stat 日志、不碰任何会话文件。
158
+ * @param {{ sessionsDir?: string }} [options]
159
+ */
160
+ export function scanArchiveMarkers({ sessionsDir } = {}) {
161
+ const out = []
162
+ if (!sessionsDir) return out
163
+ let buckets
164
+ try {
165
+ buckets = readdirSync(sessionsDir, { withFileTypes: true })
166
+ } catch {
167
+ return out
168
+ }
169
+ for (const bucket of buckets) {
170
+ if (!bucket.isDirectory()) continue
171
+ const bucketDir = join(sessionsDir, bucket.name)
172
+ let dirs
173
+ try {
174
+ dirs = readdirSync(bucketDir, { withFileTypes: true })
175
+ } catch {
176
+ continue
177
+ }
178
+ for (const dir of dirs) {
179
+ if (!dir.isDirectory()) continue
180
+ const sessionId = sessionIdFromDirName(dir.name)
181
+ if (!sessionId) continue
182
+ const sessionDir = join(bucketDir, dir.name)
183
+ const marker = readArchiveMarker(sessionDir)
184
+ out.push({
185
+ sessionId,
186
+ dir: sessionDir,
187
+ archived: marker.archived,
188
+ by: marker.by,
189
+ at: marker.at,
190
+ source: marker.source,
191
+ })
192
+ }
193
+ }
194
+ return out
195
+ }