dsh-any-agent 0.3.8 → 0.4.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/src/workspace.mjs CHANGED
@@ -329,22 +329,33 @@ export function scanSessions() {
329
329
  *
330
330
  * 会对 id 做形状校验(只允许 UUID / `session-` 前缀那类字符),避免 `..` 之类逃出会话目录。
331
331
  */
332
- export async function findSessionHeader(sessionId) {
333
- if (typeof sessionId !== 'string' || !/^[A-Za-z0-9._-]{4,80}$/.test(sessionId)) return null;
334
- if (!existsSync(SESSIONS_ROOT)) return null;
332
+ export async function findSessionHeaderStatus(sessionId) {
333
+ if (typeof sessionId !== 'string' || !/^[A-Za-z0-9._-]{4,80}$/.test(sessionId)) {
334
+ return { status: 'missing', header: null, reason: 'id 形状非法' };
335
+ }
336
+ if (!existsSync(SESSIONS_ROOT)) return { status: 'missing', header: null, reason: '会话目录不存在' };
335
337
  let buckets;
336
338
  try {
337
339
  buckets = readdirSync(SESSIONS_ROOT);
338
- } catch {
339
- return null;
340
+ } catch (e) {
341
+ return { status: 'error', header: null, reason: `会话目录不可读: ${e.message}` };
340
342
  }
341
343
  for (const bucket of buckets) {
342
344
  const f = join(SESSIONS_ROOT, bucket, sessionId, 'session.v4.jsonl.zstd');
343
345
  if (!existsSync(f)) continue;
344
346
  const header = await readHeaderAsync(f);
345
- if (header?.id === sessionId) return header;
347
+ if (header?.id === sessionId) return { status: 'found', header, file: f };
348
+ if (header === null) {
349
+ // 文件存在但头读不出来(超时/损坏/IO):不能当成“会话不存在”,
350
+ // 否则 forget 墓碑会被误删、按 id 查询也会误报未知会话。
351
+ return { status: 'error', header: null, reason: `无法读取会话头: ${f}`, file: f };
352
+ }
346
353
  }
347
- return null;
354
+ return { status: 'missing', header: null, reason: '会话文件不存在' };
355
+ }
356
+
357
+ export async function findSessionHeader(sessionId) {
358
+ return (await findSessionHeaderStatus(sessionId)).header;
348
359
  }
349
360
 
350
361
  /**