@twinsearth/dsh-fang 3.1.20 → 3.1.21

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twinsearth/dsh-fang",
3
- "version": "3.1.20",
3
+ "version": "3.1.21",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "dsh-Fang (瓮城) — mother/fork plugin development environment for DeepSeek Harness: one readonly mother, a disposable child dsh-Web per task.",
@@ -50,7 +50,7 @@
50
50
  "node": ">=22"
51
51
  },
52
52
  "optionalDependencies": {
53
- "@twinsearth/dsh-fang-queue-journal": "0.1.4",
53
+ "@twinsearth/dsh-fang-queue-journal": "^0.1.5",
54
54
  "@twinsearth/dsh-fang-web-entry": "0.1.1"
55
55
  },
56
56
  "devDependencies": {
@@ -129,7 +129,7 @@ export const AUDIT = {
129
129
  RUNTIME_ERROR: 'runtime-error',
130
130
  }
131
131
 
132
- export const FANG_VERSION = '3.1.20'
132
+ export const FANG_VERSION = '3.1.21'
133
133
 
134
134
  /**
135
135
  * @typedef {object} ForkInfo
@@ -7,6 +7,10 @@ import path from 'node:path'
7
7
  const MAX_DRAFT = 100_000
8
8
  const MAX_QUEUE = 100
9
9
  const MAX_ITEM = 4_000
10
+ // v3.1.21:队列「必丢」根因之一是所有会话挤在同一个 key(后者覆盖前者)。
11
+ // 新增 append-only 历史文件:任何一次含队列的保存都会追加一条快照,
12
+ // 即使主 journal 被覆盖,历史里仍能找回来。
13
+ const MAX_HISTORY = 500
10
14
 
11
15
  function cleanText(value, limit) {
12
16
  return String(value ?? '').replace(/\0/g, '').slice(0, limit)
@@ -15,6 +19,7 @@ function cleanText(value, limit) {
15
19
  export class ComposerJournal {
16
20
  constructor(dataDir) {
17
21
  this.file = path.join(dataDir, 'composer-journal.json')
22
+ this.historyFile = path.join(dataDir, 'composer-journal-history.jsonl')
18
23
  }
19
24
 
20
25
  load() {
@@ -58,6 +63,7 @@ export class ComposerJournal {
58
63
  draft: cleanText(input.draft, MAX_DRAFT),
59
64
  queue,
60
65
  path: cleanText(input.path, 500),
66
+ sessionKey: cleanText(input.sessionKey, 200),
61
67
  updatedAt: new Date().toISOString(),
62
68
  }
63
69
  const all = this.load()
@@ -69,9 +75,48 @@ export class ComposerJournal {
69
75
  for (const stale of keys.slice(0, keys.length - 100)) delete all[stale]
70
76
  }
71
77
  this.saveFile(all)
78
+ this.appendHistory({ ...entry, sessionKey: cleanText(input.sessionKey, 200) || entry.key })
72
79
  return entry
73
80
  }
74
81
 
82
+ /** 追加一条历史快照(只在有队列内容时写,避免草稿噪音)。 */
83
+ appendHistory(entry) {
84
+ try {
85
+ const queue = Array.isArray(entry?.queue) ? entry.queue : []
86
+ if (queue.length === 0) return
87
+ fs.mkdirSync(path.dirname(this.historyFile), { recursive: true })
88
+ fs.appendFileSync(this.historyFile, JSON.stringify({
89
+ key: entry.key,
90
+ queue,
91
+ draft: entry.draft || "",
92
+ path: entry.path || "",
93
+ sessionKey: entry.sessionKey || "",
94
+ updatedAt: entry.updatedAt,
95
+ }) + "\n", { encoding: "utf8", mode: 0o600 })
96
+ // 裁剪:超过上限时重写最后 MAX_HISTORY 行
97
+ const raw = fs.readFileSync(this.historyFile, "utf8").trim().split("\n").filter(Boolean)
98
+ if (raw.length > MAX_HISTORY) {
99
+ fs.writeFileSync(this.historyFile, raw.slice(-MAX_HISTORY).join("\n") + "\n", { encoding: "utf8", mode: 0o600 })
100
+ }
101
+ } catch { /* 历史写入失败不影响主 journal */ }
102
+ }
103
+
104
+ /** 读取历史快照(newest first),可按 sessionKey 过滤。 */
105
+ history({ limit = 50, sessionKey = "" } = {}) {
106
+ try {
107
+ const raw = fs.readFileSync(this.historyFile, "utf8").trim().split("\n").filter(Boolean)
108
+ const items = []
109
+ for (const line of raw) {
110
+ try {
111
+ const entry = JSON.parse(line)
112
+ if (sessionKey && String(entry?.sessionKey || "") !== String(sessionKey)) continue
113
+ items.push(entry)
114
+ } catch { /* 跳过坏行 */ }
115
+ }
116
+ return items.slice(-Math.max(1, Number(limit) || 50)).reverse()
117
+ } catch { return [] }
118
+ }
119
+
75
120
  remove(key) {
76
121
  const all = this.load()
77
122
  const id = String(key)
@@ -3581,13 +3581,23 @@ export function apply(ctx, config) {
3581
3581
  const key = String(body?.key ?? '')
3582
3582
  if (!key) return json(res, 400, { ok: false, error: 'journal-key-required' })
3583
3583
  try {
3584
- return json(res, 200, { ok: true, entry: composerJournal.save(key, body) })
3584
+ return json(res, 200, { ok: true, entry: composerJournal.save(key, { ...body, sessionKey: body?.sessionKey || body?.sessionId || '' }) })
3585
3585
  } catch (error) {
3586
3586
  return json(res, 400, { ok: false, error: String(error?.message ?? error) })
3587
3587
  }
3588
3588
  },
3589
3589
  })
3590
3590
 
3591
+ // v3.1.21:append-only 历史快照(主 journal 被覆盖时的兜底恢复源)。
3592
+ routeMethods(`${prefix}/api/composer/journal/history`, {
3593
+ GET: (req, res) => {
3594
+ const query = new URL(String(req.url ?? '/'), 'http://x').searchParams
3595
+ const limit = Math.max(1, Math.min(200, Number(query.get('limit') || 20)))
3596
+ const sessionKey = String(query.get('sessionKey') || '')
3597
+ return json(res, 200, { ok: true, items: composerJournal.history({ limit, sessionKey }) })
3598
+ },
3599
+ })
3600
+
3591
3601
  // Restore saved queue rows into the official Session inbox WITHOUT waking the
3592
3602
  // agent. The queue journal is a recovery surface; it must not send messages.
3593
3603
  route('POST', `${prefix}/api/composer/queue`, async (req, res) => {