@twinsearth/dsh-fang 3.3.1 → 3.7.6

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 (58) hide show
  1. package/README.md +3 -3
  2. package/client.js +709 -61
  3. package/cordis.patch.yml +26 -20
  4. package/docs/APPROVAL-DRILL.md +42 -0
  5. package/docs/AUTO-WATCHDOG.md +46 -0
  6. package/docs/DECISION-LOSSLESS.md +36 -0
  7. package/docs/ESSENCE-AUTO-SYNC.md +32 -0
  8. package/docs/INBOX-SCROLL.md +33 -0
  9. package/docs/OVERLAY-MASTER-SWITCH.md +35 -0
  10. package/docs/PACKAGE-RENAME-v3.3.3.md +40 -0
  11. package/docs/RESTART-CONFIRMATION.md +29 -0
  12. package/docs/TEST-STABILITY-v3.4.0.md +30 -0
  13. package/package.json +9 -5
  14. package/src/compose.mjs +4 -1
  15. package/src/contract/VENDOR.json +6 -5
  16. package/src/contract/constants.js +16 -1
  17. package/src/contract/relay.js +12 -6
  18. package/src/kernel/types.mjs +3 -1
  19. package/src/lib/accelerated-fork.mjs +813 -0
  20. package/src/lib/accelerated-install.mjs +295 -0
  21. package/src/lib/accelerated-web.mjs +103 -0
  22. package/src/lib/approval-drill.mjs +111 -0
  23. package/src/lib/boot-guard.mjs +6 -2
  24. package/src/lib/chat-import-delegation.mjs +47 -0
  25. package/src/lib/compat.mjs +5 -1
  26. package/src/lib/contract-delegation.mjs +161 -0
  27. package/src/lib/default-essence.mjs +7 -4
  28. package/src/lib/essence.mjs +113 -7
  29. package/src/lib/fork-config-lock.mjs +2 -2
  30. package/src/lib/fork-core-settings.mjs +95 -0
  31. package/src/lib/fork-export.mjs +85 -0
  32. package/src/lib/global-redline.mjs +164 -0
  33. package/src/lib/http-auth.mjs +36 -0
  34. package/src/lib/install-snapshot.mjs +77 -0
  35. package/src/lib/lsp-delegation.mjs +70 -0
  36. package/src/lib/lsp-provider.mjs +250 -0
  37. package/src/lib/main-gate.mjs +34 -28
  38. package/src/lib/mirror.mjs +115 -4
  39. package/src/lib/node-compat.mjs +24 -0
  40. package/src/lib/pro-delegation.mjs +185 -0
  41. package/src/lib/runtime-capture.mjs +11 -3
  42. package/src/lib/watch-guard.mjs +51 -0
  43. package/src/plugins/approver.mjs +5 -2
  44. package/src/plugins/auditor.mjs +125 -18
  45. package/src/plugins/decision-center.mjs +77 -34
  46. package/src/plugins/forker.mjs +258 -33
  47. package/src/plugins/orchestrator.mjs +1508 -65
  48. package/src/plugins/restart-guard.mjs +79 -2
  49. package/src/plugins/session-catalog.mjs +127 -40
  50. package/tools/approval-drill.mjs +21 -0
  51. package/tools/fangctl.mjs +21 -4
  52. package/tools/gate/main-city.mjs +1 -1
  53. package/tools/restart-supervisor.mjs +425 -0
  54. package/tools/restart-watchdog.mjs +568 -0
  55. package/vault/client.js +49 -0
  56. package/vault/cordis.patch.yml +3 -0
  57. package/vault/lib/vault.mjs +219 -0
  58. package/vault/package.json +29 -0
@@ -0,0 +1,219 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-or-later
2
+ // Vault: curated per-fork export/import. Pure filesystem library, no Cordis/DSH imports.
3
+ import fs from 'node:fs'
4
+ import path from 'node:path'
5
+ import { createHash } from 'node:crypto'
6
+
7
+ export const VAULT_FORMAT = 'dsh-fang-vault/v1'
8
+ export const DEFAULT_OPTIONS = Object.freeze({
9
+ sessions: true,
10
+ metadata: true,
11
+ derived: true,
12
+ collaboration: true,
13
+ profile: true,
14
+ lineage: true,
15
+ logs: true,
16
+ secrets: true,
17
+ workspace: false,
18
+ workspaceSnapshot: false,
19
+ })
20
+
21
+ function sha256File(file) {
22
+ return createHash('sha256').update(fs.readFileSync(file)).digest('hex')
23
+ }
24
+
25
+ function copyTreeFiltered(from, to, { skip = new Set() } = {}) {
26
+ const st = fs.lstatSync(from)
27
+ if (st.isSymbolicLink()) {
28
+ const target = fs.readlinkSync(from)
29
+ try { fs.symlinkSync(target, to, 'junction') } catch { /* ignore */ }
30
+ return { files: 1, bytes: 0 }
31
+ }
32
+ if (st.isDirectory()) {
33
+ if (skip.has(path.basename(from))) return { files: 0, bytes: 0 }
34
+ fs.mkdirSync(to, { recursive: true })
35
+ let files = 0
36
+ let bytes = 0
37
+ for (const name of fs.readdirSync(from)) {
38
+ const r = copyTreeFiltered(path.join(from, name), path.join(to, name), { skip })
39
+ files += r.files
40
+ bytes += r.bytes
41
+ }
42
+ return { files, bytes }
43
+ }
44
+ fs.mkdirSync(path.dirname(to), { recursive: true })
45
+ fs.copyFileSync(from, to)
46
+ return { files: 1, bytes: st.size }
47
+ }
48
+
49
+ function copyIfExists(from, to) {
50
+ if (!fs.existsSync(from)) return { files: 0, bytes: 0 }
51
+ return copyTreeFiltered(from, to)
52
+ }
53
+
54
+ function readJson(file, fallback = null) {
55
+ try { return JSON.parse(fs.readFileSync(file, 'utf8')) } catch { return fallback }
56
+ }
57
+
58
+ function writeJson(file, value) {
59
+ fs.mkdirSync(path.dirname(file), { recursive: true })
60
+ fs.writeFileSync(file, `${JSON.stringify(value, null, 2)}\n`)
61
+ }
62
+
63
+ function sessionRows(homeDir) {
64
+ const root = path.join(homeDir, 'sessions')
65
+ const rows = []
66
+ if (!fs.existsSync(root)) return rows
67
+ for (const workspaceKey of fs.readdirSync(root)) {
68
+ const dir = path.join(root, workspaceKey)
69
+ if (!fs.statSync(dir).isDirectory()) continue
70
+ for (const sessionId of fs.readdirSync(dir)) {
71
+ const log = path.join(dir, sessionId, 'session.v4.jsonl.zstd')
72
+ if (!fs.existsSync(log)) continue
73
+ rows.push({ workspaceKey, sessionId, log, bytes: fs.statSync(log).size })
74
+ }
75
+ }
76
+ return rows
77
+ }
78
+
79
+ export function exportVault({ homeDir, destDir, task, options = {} }) {
80
+ const opts = { ...DEFAULT_OPTIONS, ...(options ?? {}) }
81
+ const home = path.resolve(String(homeDir ?? ''))
82
+ if (!fs.existsSync(path.join(home, 'profiles'))) return { ok: false, error: 'fork-home-invalid', detail: home }
83
+ const dest = path.resolve(String(destDir ?? ''))
84
+ fs.mkdirSync(dest, { recursive: true })
85
+ const stamp = new Date().toISOString().replace(/[:.]/g, '-')
86
+ const root = path.join(dest, `FangVault-${task?.taskId ?? 'fork'}-${stamp}`)
87
+ fs.mkdirSync(path.join(root, '00_MANIFEST'), { recursive: true })
88
+ const files = []
89
+ const add = (name, result) => { if (result?.files) files.push({ name, ...result }) }
90
+ const sessionIndex = []
91
+
92
+ if (opts.sessions) {
93
+ for (const row of sessionRows(home)) {
94
+ const targetDir = path.join(root, '10_SESSIONS', row.sessionId)
95
+ fs.mkdirSync(targetDir, { recursive: true })
96
+ const target = path.join(targetDir, 'session.v4.jsonl.zstd')
97
+ fs.copyFileSync(row.log, target)
98
+ const meta = {
99
+ sessionId: row.sessionId,
100
+ workspaceKey: row.workspaceKey,
101
+ title: '',
102
+ bytes: row.bytes,
103
+ sha256: sha256File(row.log),
104
+ }
105
+ const ws = readJson(path.join(home, 'storages', 'workspace.json'), {})
106
+ const table = ws?.tables?.workspaces ?? {}
107
+ for (const item of Object.values(table)) {
108
+ if (Array.isArray(item?.sessionIds) && item.sessionIds.includes(row.sessionId)) meta.title = item.title ?? ''
109
+ }
110
+ if (opts.metadata) writeJson(path.join(targetDir, 'session.meta.json'), meta)
111
+ add(`sessions/${row.sessionId}`, { files: 1, bytes: row.bytes })
112
+ sessionIndex.push(meta)
113
+ }
114
+ }
115
+
116
+ if (opts.metadata) {
117
+ add('workspace-index', copyIfExists(path.join(home, 'storages', 'workspace.json'), path.join(root, '50_RUNTIME_DATA', 'storages', 'workspace.json')))
118
+ }
119
+ if (opts.derived) {
120
+ add('derived', copyIfExists(path.join(home, 'storages'), path.join(root, '50_RUNTIME_DATA', 'storages')))
121
+ }
122
+ if (opts.profile) {
123
+ add('profile', copyIfExists(path.join(home, 'profiles'), path.join(root, '40_FORK_PROFILE')))
124
+ add('headless-patch', copyIfExists(path.join(home, 'profiles', String(task?.profile ?? ''), 'fang-headless.patch.yml'), path.join(root, '40_FORK_PROFILE', String(task?.profile ?? ''), 'fang-headless.patch.yml')))
125
+ }
126
+ if (opts.collaboration) {
127
+ add('collaboration', copyIfExists(path.join(home, 'xclient'), path.join(root, '50_RUNTIME_DATA', 'xclient')))
128
+ add('fang', copyIfExists(path.join(home, 'fang'), path.join(root, '50_RUNTIME_DATA', 'fang')))
129
+ }
130
+ if (opts.lineage) {
131
+ add('attachments', copyIfExists(path.join(home, 'attachments'), path.join(root, '20_ATTACHMENTS')))
132
+ add('fang-seeds', copyIfExists(path.join(home, 'fang-seeds'), path.join(root, '50_RUNTIME_DATA', 'fang-seeds')))
133
+ add('fang-meta', copyIfExists(path.join(home, 'fang-meta.json'), path.join(root, '50_RUNTIME_DATA', 'fang-meta.json')))
134
+ }
135
+ if (opts.derived) add('llm-deepseek', copyIfExists(path.join(home, 'llm-deepseek'), path.join(root, '50_RUNTIME_DATA', 'llm-deepseek')))
136
+ if (opts.logs) add('logs', copyIfExists(path.join(home, 'logs'), path.join(root, '60_LOGS')))
137
+ if (opts.secrets) {
138
+ add('credentials', copyIfExists(path.join(home, '.credentials.yaml'), path.join(root, '70_SECRETS', '.credentials.yaml')))
139
+ add('device-key', copyIfExists(path.join(home, 'xclient', 'device.key'), path.join(root, '70_SECRETS', 'device.key')))
140
+ }
141
+ if (opts.workspace && opts.workspaceSnapshot && task?.workspacePath) {
142
+ add('workspace-snapshot', copyIfExists(String(task.workspacePath), path.join(root, '30_WORKSPACES', 'snapshot')))
143
+ }
144
+
145
+ const manifest = {
146
+ format: VAULT_FORMAT,
147
+ version: 1,
148
+ exportedAt: new Date().toISOString(),
149
+ taskId: task?.taskId ?? '',
150
+ title: task?.title ?? '',
151
+ profile: task?.profile ?? '',
152
+ coreMode: task?.coreMode ?? 'stock',
153
+ sourceEssenceId: task?.syncSourceId ?? task?.essenceId ?? 'essence-official-dev',
154
+ options: opts,
155
+ sessions: sessionIndex,
156
+ files,
157
+ }
158
+ writeJson(path.join(root, '00_MANIFEST', 'manifest.json'), manifest)
159
+ const manifestBytes = fs.readFileSync(path.join(root, '00_MANIFEST', 'manifest.json'))
160
+ fs.writeFileSync(path.join(root, '00_MANIFEST', 'checksums.sha256'), `${createHash('sha256').update(manifestBytes).digest('hex')} manifest.json\n`)
161
+ fs.writeFileSync(path.join(root, '00_MANIFEST', 'IMPORT-GUIDE.md'), [
162
+ '# 瓮城 Vault 导出包',
163
+ '',
164
+ `- 分身:${manifest.title}(${manifest.taskId})`,
165
+ `- 导出时间:${manifest.exportedAt}`,
166
+ `- 会话:${sessionIndex.length}`,
167
+ '',
168
+ '在母体面板分身工作区点「导入」,选择本文件夹(含 00_MANIFEST/manifest.json)。',
169
+ '工作区项目文件默认不导出;需要时在导出对话框勾选。',
170
+ ].join('\n'))
171
+ return { ok: true, dir: root, files: files.reduce((n, x) => n + x.files, 0), bytes: files.reduce((n, x) => n + x.bytes, 0), manifest }
172
+ }
173
+
174
+ export function readVault(sourceDir) {
175
+ const dir = path.resolve(String(sourceDir ?? ''))
176
+ const manifestFile = path.join(dir, '00_MANIFEST', 'manifest.json')
177
+ if (!fs.existsSync(manifestFile)) return { ok: false, error: 'manifest-missing', detail: manifestFile }
178
+ const manifest = readJson(manifestFile, null)
179
+ if (manifest?.format !== VAULT_FORMAT) return { ok: false, error: 'format-unsupported', detail: manifest?.format ?? '' }
180
+ return { ok: true, dir, manifest }
181
+ }
182
+
183
+ export function importVault({ sourceDir, homeDir, profile }) {
184
+ const parsed = readVault(sourceDir)
185
+ if (!parsed.ok) return parsed
186
+ const home = path.resolve(String(homeDir ?? ''))
187
+ fs.mkdirSync(home, { recursive: true })
188
+ const copy = (relative, target) => copyIfExists(path.join(parsed.dir, relative), path.join(home, target))
189
+ const result = { ok: true, source: parsed.dir, files: 0, bytes: 0, warnings: [] }
190
+ const merge = (r) => { result.files += r.files; result.bytes += r.bytes }
191
+ merge(copy('10_SESSIONS', 'sessions'))
192
+ // 导出按 sessionId 平铺;DSH 需要 sessions/<workspaceKey>/<sessionId>。
193
+ for (const row of Array.isArray(parsed.manifest?.sessions) ? parsed.manifest.sessions : []) {
194
+ const from = path.join(home, 'sessions', String(row.sessionId ?? ''))
195
+ const to = path.join(home, 'sessions', String(row.workspaceKey ?? ''), String(row.sessionId ?? ''))
196
+ if (row.sessionId && row.workspaceKey && fs.existsSync(from) && !fs.existsSync(to)) {
197
+ fs.mkdirSync(path.dirname(to), { recursive: true })
198
+ try { fs.renameSync(from, to) } catch { result.warnings.push(`session-move-failed:${row.sessionId}`) }
199
+ }
200
+ }
201
+ merge(copy('20_ATTACHMENTS', 'attachments'))
202
+ merge(copy('40_FORK_PROFILE', 'profiles'))
203
+ if (profile) {
204
+ const old = fs.readdirSync(path.join(home, 'profiles'))[0]
205
+ const target = path.join(home, 'profiles', profile)
206
+ if (old && old !== profile) {
207
+ try { fs.renameSync(path.join(home, 'profiles', old), target) } catch { result.warnings.push('profile-rename-failed') }
208
+ }
209
+ }
210
+ merge(copy('50_RUNTIME_DATA/storages', 'storages'))
211
+ merge(copy('50_RUNTIME_DATA/xclient', 'xclient'))
212
+ merge(copy('50_RUNTIME_DATA/fang', 'fang'))
213
+ merge(copy('50_RUNTIME_DATA/fang-seeds', 'fang-seeds'))
214
+ merge(copy('50_RUNTIME_DATA/fang-meta.json', 'fang-meta.json'))
215
+ merge(copy('50_RUNTIME_DATA/llm-deepseek', 'llm-deepseek'))
216
+ merge(copy('60_LOGS', 'logs'))
217
+ merge(copy('70_SECRETS', '.'))
218
+ return result
219
+ }
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@twinsearth/dsh-fang-vault",
3
+ "version": "0.1.0",
4
+ "private": false,
5
+ "type": "module",
6
+ "description": "Independent Vault subplugin for dsh-Fang: curated per-fork export/import (sessions, metadata, derived data, collaboration, profile, lineage, logs, secrets; optional workspace snapshot).",
7
+ "license": "AGPL-3.0-or-later",
8
+ "exports": {
9
+ ".": "./index.js",
10
+ "./lib": "./lib/vault.mjs",
11
+ "./client": "./client.js"
12
+ },
13
+ "files": [
14
+ "index.js",
15
+ "lib/vault.mjs",
16
+ "client.js",
17
+ "cordis.patch.yml"
18
+ ],
19
+ "dsh": {
20
+ "bundle": {
21
+ "patch": "./cordis.patch.yml"
22
+ },
23
+ "client": {
24
+ "platform": "web",
25
+ "immediately": true,
26
+ "inject": []
27
+ }
28
+ }
29
+ }