@twinsearth/dsh-fang 3.1.21 → 3.1.22
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/LICENSES/LICENSES/cordis-MIT.txt +39 -0
- package/client.js +197 -3
- package/defaults/defaults/umbrella-rules.yaml +113 -0
- package/package.json +2 -2
- package/sdk/sdk/README.md +65 -0
- package/sdk/sdk/examples/status-badge/README.md +16 -0
- package/sdk/sdk/examples/status-badge/client.js +60 -0
- package/sdk/sdk/examples/status-badge/cordis.patch.yml +7 -0
- package/sdk/sdk/examples/status-badge/index.js +12 -0
- package/sdk/sdk/examples/status-badge/package.json +24 -0
- package/sdk/sdk/templates/plugin/README.md +36 -0
- package/sdk/sdk/templates/plugin/client.js +35 -0
- package/sdk/sdk/templates/plugin/cordis.patch.yml +8 -0
- package/sdk/sdk/templates/plugin/icon.svg +5 -0
- package/sdk/sdk/templates/plugin/index.js +23 -0
- package/sdk/sdk/templates/plugin/locale/en.json +6 -0
- package/sdk/sdk/templates/plugin/package.json +36 -0
- package/src/kernel/types.mjs +1 -1
- package/src/src/compose.mjs +67 -0
- package/src/src/contract/client.js +86 -0
- package/src/src/contract/constants.js +79 -0
- package/src/src/contract/envelope.js +83 -0
- package/src/src/contract/index.js +19 -0
- package/src/src/contract/negotiate.js +106 -0
- package/src/src/contract/server.js +77 -0
- package/src/src/contract/transports.js +301 -0
- package/src/src/index.mjs +11 -0
- package/src/src/kernel/core.mjs +100 -0
- package/src/src/kernel/extensions.mjs +61 -0
- package/src/src/kernel/fsx.mjs +349 -0
- package/src/src/kernel/index.mjs +21 -0
- package/src/src/kernel/ledger.mjs +94 -0
- package/src/src/kernel/paths.mjs +182 -0
- package/src/src/kernel/role.mjs +68 -0
- package/src/src/kernel/rounds.mjs +172 -0
- package/src/src/kernel/schema.mjs +5 -0
- package/src/src/kernel/types.mjs +264 -0
- package/src/src/kernel/vendor/cosmokit.mjs +421 -0
- package/src/src/kernel/vendor/schemastery.mjs +638 -0
- package/src/src/lib/adapter-client.mjs +383 -0
- package/src/src/lib/adapter-protocol.mjs +243 -0
- package/src/src/lib/ai-secret-store.mjs +202 -0
- package/src/src/lib/boot-guard.mjs +182 -0
- package/src/src/lib/branch-store.mjs +84 -0
- package/src/src/lib/branch.mjs +74 -0
- package/src/src/lib/bridge-client.mjs +133 -0
- package/src/src/lib/broker-adapter.mjs +67 -0
- package/src/src/lib/broker.mjs +45 -0
- package/src/src/lib/business-producers.mjs +109 -0
- package/src/src/lib/command-cache.mjs +209 -0
- package/src/src/lib/compat.mjs +170 -0
- package/src/src/lib/composer-journal.mjs +128 -0
- package/src/src/lib/container/index.mjs +806 -0
- package/src/src/lib/decision-events.mjs +129 -0
- package/src/src/lib/decision-ingest-client.mjs +41 -0
- package/src/src/lib/decision-outbox.mjs +129 -0
- package/src/src/lib/default-essence.mjs +61 -0
- package/src/src/lib/desktop-launch.mjs +138 -0
- package/src/src/lib/essence.mjs +1211 -0
- package/src/src/lib/fork-client.mjs +410 -0
- package/src/src/lib/fork-config-lock.mjs +153 -0
- package/src/src/lib/fork-seed.mjs +304 -0
- package/src/src/lib/fork-supervision-settings.mjs +58 -0
- package/src/src/lib/fork-supervisor.mjs +122 -0
- package/src/src/lib/fs-guard.mjs +28 -0
- package/src/src/lib/http-auth.mjs +162 -0
- package/src/src/lib/idle-audit.mjs +178 -0
- package/src/src/lib/interaction-poke.mjs +61 -0
- package/src/src/lib/mirror-etag.mjs +92 -0
- package/src/src/lib/mirror-official-source.mjs +93 -0
- package/src/src/lib/mirror.mjs +478 -0
- package/src/src/lib/mother-link.mjs +156 -0
- package/src/src/lib/mutex.mjs +31 -0
- package/src/src/lib/permissions.mjs +115 -0
- package/src/src/lib/pnpm-store.mjs +189 -0
- package/src/src/lib/preferences.mjs +212 -0
- package/src/src/lib/producer-contract.mjs +162 -0
- package/src/src/lib/reflow.mjs +41 -0
- package/src/src/lib/remote.mjs +408 -0
- package/src/src/lib/restart-contract.mjs +650 -0
- package/src/src/lib/roots.mjs +30 -0
- package/src/src/lib/rule-registry.mjs +1101 -0
- package/src/src/lib/runtime-capture.mjs +307 -0
- package/src/src/lib/semaphore.mjs +34 -0
- package/src/src/lib/session-bundle.mjs +138 -0
- package/src/src/lib/session-format.mjs +658 -0
- package/src/src/lib/sse-parser.mjs +102 -0
- package/src/src/lib/ssrf-guard.mjs +170 -0
- package/src/src/lib/status-store.mjs +257 -0
- package/src/src/lib/task-handle.mjs +622 -0
- package/src/src/lib/ui-actions.mjs +55 -0
- package/src/src/lib/umbrella.mjs +1661 -0
- package/src/src/lib/upgrade-coordinator.mjs +107 -0
- package/src/src/lib/upgrade-status.mjs +43 -0
- package/src/src/plugins/adapter.mjs +1468 -0
- package/src/src/plugins/approver.mjs +1210 -0
- package/src/src/plugins/auditor.mjs +330 -0
- package/src/src/plugins/buffer-keeper.mjs +522 -0
- package/src/src/plugins/decision-center.mjs +2265 -0
- package/src/src/plugins/forker.mjs +1594 -0
- package/src/src/plugins/healer.mjs +664 -0
- package/src/src/plugins/hot-reloader.mjs +274 -0
- package/src/src/plugins/imports.mjs +247 -0
- package/src/src/plugins/orchestrator.mjs +3806 -0
- package/src/src/plugins/restart-guard.mjs +948 -0
- package/src/src/plugins/router.mjs +714 -0
- package/src/src/plugins/scorer.mjs +624 -0
- package/src/src/plugins/session-bridge.mjs +166 -0
- package/src/src/plugins/session-catalog.mjs +771 -0
- package/src/src/plugins/session-router.mjs +125 -0
- package/src/src/plugins/session-sync.mjs +171 -0
- package/src/src/plugins/umbrella.mjs +419 -0
- package/tools/recover-queue.mjs +133 -0
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
|
+
// Copyright (c) 2026 dsh-Fang contributors — see LICENSE-CORE.
|
|
3
|
+
// dsh-Fang kernel — filesystem helpers.
|
|
4
|
+
// All disk mutation used by Fang plugins goes through here so that:
|
|
5
|
+
// * writes are atomic (tmp + rename),
|
|
6
|
+
// * tree removal is junction-safe (never follows a reparse point into the mother),
|
|
7
|
+
// * digests are stable across platforms.
|
|
8
|
+
import fs from 'node:fs'
|
|
9
|
+
import path from 'node:path'
|
|
10
|
+
import { createHash } from 'node:crypto'
|
|
11
|
+
|
|
12
|
+
export function ensureDir(p) {
|
|
13
|
+
fs.mkdirSync(p, { recursive: true })
|
|
14
|
+
return p
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function exists(p) {
|
|
18
|
+
try {
|
|
19
|
+
fs.lstatSync(p)
|
|
20
|
+
return true
|
|
21
|
+
} catch {
|
|
22
|
+
return false
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function readJson(file, fallback = null) {
|
|
27
|
+
try {
|
|
28
|
+
return JSON.parse(fs.readFileSync(file, 'utf8'))
|
|
29
|
+
} catch {
|
|
30
|
+
return fallback
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function readTextSafe(file, fallback = '') {
|
|
35
|
+
try {
|
|
36
|
+
return fs.readFileSync(file, 'utf8')
|
|
37
|
+
} catch {
|
|
38
|
+
return fallback
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
let _tmpSeq = 0
|
|
43
|
+
export function tmpName(dir, stem = 'tmp') {
|
|
44
|
+
_tmpSeq = (_tmpSeq + 1) % 1e9
|
|
45
|
+
return path.join(dir, `.${stem}.${process.pid}.${Date.now().toString(36)}.${_tmpSeq}.tmp`)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function writeFileAtomic(file, data) {
|
|
49
|
+
ensureDir(path.dirname(file))
|
|
50
|
+
const tmp = tmpName(path.dirname(file), path.basename(file))
|
|
51
|
+
fs.writeFileSync(tmp, data)
|
|
52
|
+
try {
|
|
53
|
+
fs.renameSync(tmp, file)
|
|
54
|
+
} catch {
|
|
55
|
+
// Windows can refuse a rename while a reader holds the file open; retry once, then fall back.
|
|
56
|
+
try {
|
|
57
|
+
fs.copyFileSync(tmp, file)
|
|
58
|
+
} finally {
|
|
59
|
+
try { fs.unlinkSync(tmp) } catch { /* ignore */ }
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return file
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function writeJsonAtomic(file, value, { indent = 2 } = {}) {
|
|
66
|
+
return writeFileAtomic(file, JSON.stringify(value, null, indent) + '\n')
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function appendJsonl(file, record) {
|
|
70
|
+
ensureDir(path.dirname(file))
|
|
71
|
+
fs.appendFileSync(file, JSON.stringify(record) + '\n', 'utf8')
|
|
72
|
+
return record
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Read a JSONL file into records; unparsable lines are skipped (verifyChain reports them). */
|
|
76
|
+
export function readJsonl(file) {
|
|
77
|
+
const out = []
|
|
78
|
+
let raw = ''
|
|
79
|
+
try { raw = fs.readFileSync(file, 'utf8') } catch { return out }
|
|
80
|
+
for (const line of raw.split('\n')) {
|
|
81
|
+
const t = line.trim()
|
|
82
|
+
if (!t) continue
|
|
83
|
+
try { out.push(JSON.parse(t)) } catch { /* skip */ }
|
|
84
|
+
}
|
|
85
|
+
return out
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function sha256(input) {
|
|
89
|
+
return createHash('sha256').update(input).digest('hex')
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function sha256File(file) {
|
|
93
|
+
try {
|
|
94
|
+
return createHash('sha256').update(fs.readFileSync(file)).digest('hex')
|
|
95
|
+
} catch {
|
|
96
|
+
return null
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** Stable recursive digest of a tree: sorted rel paths + content hash; links carry their target. */
|
|
101
|
+
export function digestTree(dir, { maxFiles = 20000 } = {}) {
|
|
102
|
+
const out = {}
|
|
103
|
+
let files = 0
|
|
104
|
+
const walk = (d, rel = '') => {
|
|
105
|
+
let names
|
|
106
|
+
try { names = fs.readdirSync(d).sort() } catch { return }
|
|
107
|
+
for (const n of names) {
|
|
108
|
+
if (files >= maxFiles) return
|
|
109
|
+
const abs = path.join(d, n)
|
|
110
|
+
const r = rel ? rel + '/' + n : n
|
|
111
|
+
let st
|
|
112
|
+
try { st = fs.lstatSync(abs) } catch { continue }
|
|
113
|
+
if (st.isSymbolicLink()) {
|
|
114
|
+
try { out[r] = 'link:' + fs.readlinkSync(abs) } catch { out[r] = 'link:?' }
|
|
115
|
+
files++
|
|
116
|
+
continue
|
|
117
|
+
}
|
|
118
|
+
if (st.isDirectory()) { walk(abs, r); continue }
|
|
119
|
+
out[r] = sha256File(abs)
|
|
120
|
+
files++
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
walk(dir)
|
|
124
|
+
return out
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function diffDigests(before, after) {
|
|
128
|
+
const added = []
|
|
129
|
+
const removed = []
|
|
130
|
+
const changed = []
|
|
131
|
+
for (const [k, v] of Object.entries(after ?? {})) {
|
|
132
|
+
if (!(k in (before ?? {}))) added.push(k)
|
|
133
|
+
else if (before[k] !== v) changed.push(k)
|
|
134
|
+
}
|
|
135
|
+
for (const k of Object.keys(before ?? {})) if (!(k in (after ?? {}))) removed.push(k)
|
|
136
|
+
return { added, removed, changed, same: Object.keys(after ?? {}).length - added.length - changed.length }
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Copy a tree. Symlinks/junctions are recreated as junctions, never traversed.
|
|
140
|
+
* Safe when `to` lives inside `from`: the destination is created only after the
|
|
141
|
+
* source listing is taken, and the destination itself is never re-entered. */
|
|
142
|
+
export function copyTree(from, to, { filter = null } = {}) {
|
|
143
|
+
let names
|
|
144
|
+
try { names = fs.readdirSync(from) } catch {
|
|
145
|
+
ensureDir(to)
|
|
146
|
+
return to
|
|
147
|
+
}
|
|
148
|
+
ensureDir(to)
|
|
149
|
+
const toAbs = path.resolve(to)
|
|
150
|
+
for (const n of names) {
|
|
151
|
+
const src = path.join(from, n)
|
|
152
|
+
const dst = path.join(to, n)
|
|
153
|
+
if (filter && !filter(n, src)) continue
|
|
154
|
+
if (path.resolve(src) === toAbs) continue
|
|
155
|
+
let st
|
|
156
|
+
try { st = fs.lstatSync(src) } catch { continue }
|
|
157
|
+
if (st.isSymbolicLink()) {
|
|
158
|
+
try {
|
|
159
|
+
const target = fs.readlinkSync(src)
|
|
160
|
+
fs.symlinkSync(target, dst, 'junction')
|
|
161
|
+
} catch { /* ignore */ }
|
|
162
|
+
continue
|
|
163
|
+
}
|
|
164
|
+
if (st.isDirectory()) { copyTree(src, dst, { filter }); continue }
|
|
165
|
+
ensureDir(path.dirname(dst))
|
|
166
|
+
try { fs.copyFileSync(src, dst) } catch { /* ignore */ }
|
|
167
|
+
}
|
|
168
|
+
return to
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export function isLink(p) {
|
|
172
|
+
try {
|
|
173
|
+
return fs.lstatSync(p).isSymbolicLink()
|
|
174
|
+
} catch {
|
|
175
|
+
return false
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Copy a tree with hardlinked files: directories are recreated, ordinary files are
|
|
181
|
+
* hardlinked (same volume only; falls back to a copy), and links are recreated as links
|
|
182
|
+
* rather than traversed. Used when the essence runtime's touched packages must be
|
|
183
|
+
* materialized into the essence module tree without duplicating file bytes and without
|
|
184
|
+
* junction cycles (the runtime mirror would otherwise point back at the essence tree).
|
|
185
|
+
* @param {string} from Source directory.
|
|
186
|
+
* @param {string} to Destination directory (created; expected to be empty/new).
|
|
187
|
+
* @returns {string} The destination.
|
|
188
|
+
*/
|
|
189
|
+
export function hardlinkTree(from, to) {
|
|
190
|
+
let names
|
|
191
|
+
try { names = fs.readdirSync(from) } catch {
|
|
192
|
+
ensureDir(to)
|
|
193
|
+
return to
|
|
194
|
+
}
|
|
195
|
+
ensureDir(to)
|
|
196
|
+
for (const name of names) {
|
|
197
|
+
const src = path.join(from, name)
|
|
198
|
+
const dst = path.join(to, name)
|
|
199
|
+
let st
|
|
200
|
+
try { st = fs.lstatSync(src) } catch { continue }
|
|
201
|
+
if (st.isSymbolicLink()) {
|
|
202
|
+
let target = ''
|
|
203
|
+
try { target = fs.readlinkSync(src) } catch { continue }
|
|
204
|
+
try {
|
|
205
|
+
const absolute = path.isAbsolute(target) ? target : path.resolve(from, target)
|
|
206
|
+
const stTarget = fs.lstatSync(absolute)
|
|
207
|
+
if (stTarget.isDirectory()) fs.symlinkSync(absolute, dst, 'junction')
|
|
208
|
+
else fs.copyFileSync(absolute, dst)
|
|
209
|
+
} catch { /* an unmirrorable link is skipped */ }
|
|
210
|
+
continue
|
|
211
|
+
}
|
|
212
|
+
if (st.isDirectory()) { hardlinkTree(src, dst); continue }
|
|
213
|
+
try { fs.linkSync(src, dst) } catch {
|
|
214
|
+
try { fs.copyFileSync(src, dst) } catch { /* skip */ }
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
return to
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** Create (or replace) a Windows junction / POSIX dir symlink. */
|
|
221
|
+
export function makeJunction(target, linkPath) {
|
|
222
|
+
ensureDir(path.dirname(linkPath))
|
|
223
|
+
if (exists(linkPath)) return linkPath
|
|
224
|
+
fs.symlinkSync(path.resolve(target), linkPath, 'junction')
|
|
225
|
+
return linkPath
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Replace `dest` with a real directory that mirrors `source`'s top-level entries:
|
|
230
|
+
* directories become junctions to their real targets, plain root files are copied.
|
|
231
|
+
* Package contents are never copied, so the module tree stays shared — but every
|
|
232
|
+
* entry is its own link. That is what DSH's profile-scope resolver needs: an
|
|
233
|
+
* importer below a linked root gets the installation's `@deepseek-ai/*` packages
|
|
234
|
+
* supplied (subject to the peer declaration), while a single whole-node_modules
|
|
235
|
+
* junction leaves the importer outside every known scope. Measured consequence of
|
|
236
|
+
* the whole-tree link: plugins that lazily `import('@deepseek-ai/dsh-tools')` in a
|
|
237
|
+
* fork fall back to their offline shim — w2m's shim stamps `required: false` into a
|
|
238
|
+
* property schema, and the provider rejects every request with
|
|
239
|
+
* `Invalid schema for function 'w2m_report'` (v3.1.7 report).
|
|
240
|
+
* Junction-safe: a reparse point in `dest` is unlinked, never entered; link targets
|
|
241
|
+
* are resolved with realpath first so a link-to-link (pnpm) also lands on a dir.
|
|
242
|
+
* @param {string} source Directory whose entries are mirrored (the module tree).
|
|
243
|
+
* @param {string} dest Directory to replace with the mirror.
|
|
244
|
+
* @returns {{ok:boolean, links:string[], files:string[], error?:string}}
|
|
245
|
+
*/
|
|
246
|
+
export function mirrorModuleTree(source, dest) {
|
|
247
|
+
const src = path.resolve(String(source ?? ''))
|
|
248
|
+
if (!exists(src)) return { ok: false, links: [], files: [], error: 'source-missing' }
|
|
249
|
+
let srcStat
|
|
250
|
+
try { srcStat = fs.lstatSync(src) } catch { return { ok: false, links: [], files: [], error: 'source-unreadable' } }
|
|
251
|
+
if (!srcStat.isDirectory() && !srcStat.isSymbolicLink()) return { ok: false, links: [], files: [], error: 'source-not-directory' }
|
|
252
|
+
removeTree(dest)
|
|
253
|
+
ensureDir(dest)
|
|
254
|
+
const links = []
|
|
255
|
+
const files = []
|
|
256
|
+
let names = []
|
|
257
|
+
try { names = fs.readdirSync(src) } catch { names = [] }
|
|
258
|
+
for (const name of names) {
|
|
259
|
+
const from = path.join(src, name)
|
|
260
|
+
const to = path.join(dest, name)
|
|
261
|
+
let real = from
|
|
262
|
+
try {
|
|
263
|
+
if (fs.lstatSync(from).isSymbolicLink()) real = fs.realpathSync(from)
|
|
264
|
+
} catch { continue }
|
|
265
|
+
let targetStat
|
|
266
|
+
try { targetStat = fs.statSync(real) } catch { continue }
|
|
267
|
+
if (targetStat.isDirectory()) {
|
|
268
|
+
try {
|
|
269
|
+
fs.symlinkSync(path.resolve(real), to, 'junction')
|
|
270
|
+
links.push(name)
|
|
271
|
+
} catch { /* an unmirrorable entry is skipped; the remaining tree still boots */ }
|
|
272
|
+
continue
|
|
273
|
+
}
|
|
274
|
+
try {
|
|
275
|
+
fs.copyFileSync(from, to)
|
|
276
|
+
files.push(name)
|
|
277
|
+
} catch { /* ignore */ }
|
|
278
|
+
}
|
|
279
|
+
return { ok: true, links, files }
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Repair a profile `node_modules` that is still a whole-tree junction (v3.1.6 and
|
|
284
|
+
* older) into the per-entry mirror form. A real directory is left untouched.
|
|
285
|
+
* @param {string} modulesPath The profile's node_modules path.
|
|
286
|
+
* @returns {{ok:boolean, changed:boolean, links?:string[], files?:string[], source?:string, error?:string}}
|
|
287
|
+
*/
|
|
288
|
+
export function ensureModuleMirror(modulesPath) {
|
|
289
|
+
const p = String(modulesPath ?? '')
|
|
290
|
+
if (!p || !exists(p)) return { ok: false, changed: false, error: 'node-modules-missing' }
|
|
291
|
+
let st
|
|
292
|
+
try { st = fs.lstatSync(p) } catch { return { ok: false, changed: false, error: 'node-modules-unreadable' } }
|
|
293
|
+
if (!st.isSymbolicLink()) return { ok: true, changed: false, links: [], files: [] }
|
|
294
|
+
let source
|
|
295
|
+
try { source = fs.realpathSync(p) } catch { return { ok: false, changed: false, error: 'node-modules-target-missing' } }
|
|
296
|
+
const mirrored = mirrorModuleTree(source, p)
|
|
297
|
+
return { ...mirrored, changed: true, source }
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* Remove a file or directory tree. Junction-safe: a reparse point is unlinked,
|
|
302
|
+
* its target is never entered. This is the ONLY removal path plugins may use on
|
|
303
|
+
* fork homes, because a fork home contains a node_modules junction to the mother.
|
|
304
|
+
*/
|
|
305
|
+
export function removeTree(target) {
|
|
306
|
+
let st
|
|
307
|
+
try { st = fs.lstatSync(target) } catch { return false }
|
|
308
|
+
if (st.isSymbolicLink()) {
|
|
309
|
+
try { fs.unlinkSync(target); return true } catch { return false }
|
|
310
|
+
}
|
|
311
|
+
if (!st.isDirectory()) {
|
|
312
|
+
try { fs.unlinkSync(target); return true } catch { return false }
|
|
313
|
+
}
|
|
314
|
+
let names = []
|
|
315
|
+
try { names = fs.readdirSync(target) } catch { names = [] }
|
|
316
|
+
for (const n of names) removeTree(path.join(target, n))
|
|
317
|
+
try { fs.rmdirSync(target) } catch { /* ignore */ }
|
|
318
|
+
return true
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
export function listFiles(dir, { max = 5000 } = {}) {
|
|
322
|
+
const out = []
|
|
323
|
+
const walk = (d, rel = '') => {
|
|
324
|
+
if (out.length >= max) return
|
|
325
|
+
let names
|
|
326
|
+
try { names = fs.readdirSync(d).sort() } catch { return }
|
|
327
|
+
for (const n of names) {
|
|
328
|
+
if (out.length >= max) return
|
|
329
|
+
const abs = path.join(d, n)
|
|
330
|
+
const r = rel ? rel + '/' + n : n
|
|
331
|
+
let st
|
|
332
|
+
try { st = fs.lstatSync(abs) } catch { continue }
|
|
333
|
+
if (st.isDirectory()) walk(abs, r)
|
|
334
|
+
else out.push(r)
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
walk(dir)
|
|
338
|
+
return out
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
export function nowMs() {
|
|
342
|
+
return Date.now()
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
export function iso(ms = Date.now()) {
|
|
346
|
+
return new Date(ms).toISOString()
|
|
347
|
+
}
|
|
348
|
+
// NOTE: `sleep` lives in core.mjs only — two `export *` sources exporting the same
|
|
349
|
+
// name make the whole kernel barrel un-importable (`conflicting star exports`).
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
|
+
// Copyright (c) 2026 dsh-Fang contributors — see LICENSE-CORE.
|
|
3
|
+
// dsh-Fang kernel barrel. Plugin modules import from here:
|
|
4
|
+
// import { createRounds, resolveFangLayout, SERVICE } from '../kernel/index.mjs'
|
|
5
|
+
export * from './core.mjs'
|
|
6
|
+
export * from './fsx.mjs'
|
|
7
|
+
export * from './paths.mjs'
|
|
8
|
+
export * from './rounds.mjs'
|
|
9
|
+
export * from './types.mjs'
|
|
10
|
+
export * from './role.mjs'
|
|
11
|
+
export * from './extensions.mjs'
|
|
12
|
+
export { default as z } from './schema.mjs'
|
|
13
|
+
export {
|
|
14
|
+
GENESIS,
|
|
15
|
+
canonical,
|
|
16
|
+
hashOf,
|
|
17
|
+
lastHash,
|
|
18
|
+
appendChained,
|
|
19
|
+
verifyChain,
|
|
20
|
+
verifyAndAppendBoot,
|
|
21
|
+
} from './ledger.mjs'
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
|
+
// Copyright (c) 2026 dsh-Fang contributors — see LICENSE-CORE.
|
|
3
|
+
// 链式审计账本(共享模块)
|
|
4
|
+
// 每条记录携带 prevHash 与 hash = sha256(prevHash + canonical(record))。
|
|
5
|
+
// 能发现:改字段、改 hash、删中间行、换行序、追加伪造。
|
|
6
|
+
import fs from 'node:fs'
|
|
7
|
+
import { createHash } from 'node:crypto'
|
|
8
|
+
|
|
9
|
+
export const GENESIS = 'GENESIS'
|
|
10
|
+
|
|
11
|
+
/** 规范化:**递归**按键排序 + 去掉 hash/prevHash 自身
|
|
12
|
+
* (TS 规范 audit.canonicalJson 是递归排序;只排顶层会让嵌套键序影响 hash) */
|
|
13
|
+
export function canonical(record) {
|
|
14
|
+
const walk = (x) => {
|
|
15
|
+
if (x === null || typeof x !== 'object') return x
|
|
16
|
+
if (Array.isArray(x)) return x.map(walk)
|
|
17
|
+
const o = {}
|
|
18
|
+
for (const k of Object.keys(x).sort()) o[k] = walk(x[k])
|
|
19
|
+
return o
|
|
20
|
+
}
|
|
21
|
+
const { hash, prevHash, ...rest } = record ?? {}
|
|
22
|
+
return JSON.stringify(walk(rest))
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** 与 TS 规范 chainHash 完全一致:sha256(canonicalJson(entry) + prev) */
|
|
26
|
+
export function hashOf(record, prevHash) {
|
|
27
|
+
return createHash('sha256').update(canonical(record) + String(prevHash), 'utf8').digest('hex')
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function readLines(file) {
|
|
31
|
+
let raw = ''
|
|
32
|
+
try { raw = fs.readFileSync(file, 'utf8') } catch { return [] }
|
|
33
|
+
return raw.split('\n').filter(l => l.trim().length > 0)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function lastHash(file) {
|
|
37
|
+
const lines = readLines(file)
|
|
38
|
+
if (!lines.length) return GENESIS
|
|
39
|
+
try { return JSON.parse(lines[lines.length - 1]).hash ?? GENESIS } catch { return null }
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** 追加一条链式记录,返回 { hash, prevHash, seq } */
|
|
43
|
+
export function appendChained(file, record, opts = {}) {
|
|
44
|
+
const prevHash = lastHash(file)
|
|
45
|
+
if (prevHash === null) {
|
|
46
|
+
// 上一行不可解析 → 不静默续写(否则等于帮着掩盖篡改)
|
|
47
|
+
throw new Error(`ledger tail unparsable: ${file}`)
|
|
48
|
+
}
|
|
49
|
+
const seq = opts.seq ?? readLines(file).length
|
|
50
|
+
const body = { ...record, seq, prevHash }
|
|
51
|
+
const hash = hashOf(body, prevHash)
|
|
52
|
+
const line = JSON.stringify({ ...body, hash }) + '\n'
|
|
53
|
+
fs.appendFileSync(file, line, 'utf8')
|
|
54
|
+
return { hash, prevHash, seq }
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* 全链校验:不只看最后一条 head,而是逐条重算 + 检查 prevHash 链接。
|
|
59
|
+
* 返回 { ok, count, reason, badIndex, expected, actual }
|
|
60
|
+
*/
|
|
61
|
+
export function verifyChain(file) {
|
|
62
|
+
const lines = readLines(file)
|
|
63
|
+
if (!lines.length) return { ok: true, count: 0, reason: 'empty' }
|
|
64
|
+
let prev = GENESIS
|
|
65
|
+
for (let i = 0; i < lines.length; i++) {
|
|
66
|
+
let rec
|
|
67
|
+
try { rec = JSON.parse(lines[i]) } catch (e) {
|
|
68
|
+
return { ok: false, count: lines.length, badIndex: i, reason: 'unparsable', error: String(e?.message ?? e) }
|
|
69
|
+
}
|
|
70
|
+
if (rec.prevHash !== prev) {
|
|
71
|
+
return { ok: false, count: lines.length, badIndex: i, reason: 'broken-link', expected: prev, actual: rec.prevHash }
|
|
72
|
+
}
|
|
73
|
+
const recomputed = hashOf(rec, rec.prevHash)
|
|
74
|
+
if (recomputed !== rec.hash) {
|
|
75
|
+
return { ok: false, count: lines.length, badIndex: i, reason: 'hash-mismatch', expected: recomputed, actual: rec.hash }
|
|
76
|
+
}
|
|
77
|
+
prev = rec.hash
|
|
78
|
+
}
|
|
79
|
+
return { ok: true, count: lines.length, head: prev }
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** 启动自检 + 追加 boot 记录(corrupt 时不续写)
|
|
83
|
+
* 注意:与 TS 规范 audit.bootAudit **同名不同义** —— 那个是 sink+marker 的中断检测;
|
|
84
|
+
* 这里只是"校验全链后追加一条 boot 记录",故命名为 verifyAndAppendBoot。 */
|
|
85
|
+
export function verifyAndAppendBoot(file, extra = {}) {
|
|
86
|
+
const v = verifyChain(file)
|
|
87
|
+
if (!v.ok) return { ...v, action: 'refused-write' } // 账本可疑:不往上写
|
|
88
|
+
try {
|
|
89
|
+
const r = appendChained(file, { kind: 'boot-verify', ok: true, count: v.count, ...extra })
|
|
90
|
+
return { ...v, action: 'appended', bootHash: r.hash }
|
|
91
|
+
} catch (e) {
|
|
92
|
+
return { ...v, ok: false, reason: 'append-failed', error: String(e?.message ?? e) }
|
|
93
|
+
}
|
|
94
|
+
}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
|
+
// Copyright (c) 2026 dsh-Fang contributors — see LICENSE-CORE.
|
|
3
|
+
// dsh-Fang kernel — path resolution and the fork web-URL contract.
|
|
4
|
+
//
|
|
5
|
+
// Layout (mother):
|
|
6
|
+
// $DSH_HOME/fang/<profileTag>/ data dir (audit.jsonl, forks.json, buffer/, snapshots/, run/)
|
|
7
|
+
// Forks: every fork owns its own $DSH_HOME, so the same rule isolates it from the mother.
|
|
8
|
+
import os from 'node:os'
|
|
9
|
+
import path from 'node:path'
|
|
10
|
+
import fs from 'node:fs'
|
|
11
|
+
|
|
12
|
+
/** `--profile name` > `--profile=name` > $DSH_PROFILE > .../profiles/<name> > 'default' */
|
|
13
|
+
export function detectProfileTag(argv = process.argv, env = process.env) {
|
|
14
|
+
const argvList = Array.isArray(argv) ? argv : []
|
|
15
|
+
// The launcher sets DSH_PROFILE for the booted profile; trust it before argv, because a
|
|
16
|
+
// profile boot may re-exec with the template name still on the command line.
|
|
17
|
+
if (env && typeof env.DSH_PROFILE === 'string' && env.DSH_PROFILE.trim()) return env.DSH_PROFILE.trim()
|
|
18
|
+
const i = argvList.indexOf('--profile')
|
|
19
|
+
if (i >= 0 && argvList[i + 1]) return String(argvList[i + 1])
|
|
20
|
+
const eq = argvList.find(a => typeof a === 'string' && a.startsWith('--profile='))
|
|
21
|
+
if (eq) return eq.slice('--profile='.length)
|
|
22
|
+
const joined = argvList.join(' ')
|
|
23
|
+
const m = /[\\/]profiles[\\/]([^\\/\s"']+)/.exec(joined)
|
|
24
|
+
if (m) return m[1]
|
|
25
|
+
const cwd = process.cwd()
|
|
26
|
+
const cm = /[\\/]profiles[\\/]([^\\/\s"']+)/.exec(cwd)
|
|
27
|
+
if (cm) return cm[1]
|
|
28
|
+
return 'default'
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function resolveDshHome(env = process.env) {
|
|
32
|
+
const raw = env && env.DSH_HOME
|
|
33
|
+
if (typeof raw === 'string' && raw.trim()) return path.resolve(raw.trim())
|
|
34
|
+
return path.join(os.homedir(), '.dsh')
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function resolveProfileDir(profileTag, env = process.env) {
|
|
38
|
+
return path.join(resolveDshHome(env), 'profiles', profileTag)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** 用户级数据根:$FANG_HOME 优先;未设置时新装默认 D:\ToFuture(旧布局有数据则继续用旧布局)。 */
|
|
42
|
+
export function resolveFangHome(env = process.env) {
|
|
43
|
+
const raw = env && env.FANG_HOME
|
|
44
|
+
if (typeof raw === 'string' && raw.trim()) return path.resolve(raw.trim())
|
|
45
|
+
// 迁移指针:<DSH_HOME>/fang/fang-home.txt 第一行写明数据根(无需改环境变量/配置即可切换)。
|
|
46
|
+
try {
|
|
47
|
+
const pointer = path.join(resolveDshHome(env), 'fang', 'fang-home.txt')
|
|
48
|
+
if (fs.existsSync(pointer)) {
|
|
49
|
+
const first = fs.readFileSync(pointer, 'utf8').replace(/^\uFEFF/, '').split(/\r?\n/)[0].trim()
|
|
50
|
+
if (first && path.isAbsolute(first)) {
|
|
51
|
+
// 迁移指针可能是旧盘/旧目录:根不存在或指向普通文件时忽略,回退到本机布局,
|
|
52
|
+
// 避免 ensureDir(dataDir) 在插件 apply 阶段抛错导致“必须删 .dsh”才能启动。
|
|
53
|
+
const root = path.parse(first).root
|
|
54
|
+
const rootUsable = Boolean(root) && fs.existsSync(root)
|
|
55
|
+
let targetUsable = true
|
|
56
|
+
try { if (fs.existsSync(first) && !fs.statSync(first).isDirectory()) targetUsable = false } catch { targetUsable = false }
|
|
57
|
+
if (rootUsable && targetUsable) return path.resolve(first)
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
} catch { /* ignore */ }
|
|
61
|
+
// 兼容:已有 $DSH_HOME/fang/<tag> 数据的安装不搬家,继续旧布局。
|
|
62
|
+
try {
|
|
63
|
+
if (fs.existsSync(path.join(resolveDshHome(env), 'fang'))) return ''
|
|
64
|
+
} catch { /* ignore */ }
|
|
65
|
+
// 新装默认主文件夹(Windows:D:\ToFuture)——只对“真正的默认安装”生效:
|
|
66
|
+
// 显式设置 DSH_HOME(e2e/测试/便携安装)时不得跳盘,否则会写进真实 D: 数据根。
|
|
67
|
+
const explicitDshHome = Boolean(env && String(env.DSH_HOME ?? '').trim())
|
|
68
|
+
if (!explicitDshHome && process.platform === 'win32') {
|
|
69
|
+
try { if (fs.existsSync('D:\\')) return 'D:\\ToFuture' } catch { /* ignore */ }
|
|
70
|
+
}
|
|
71
|
+
return ''
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Every path a Fang plugin needs. Pure: no directory is created here.
|
|
76
|
+
* @returns {{dataDir:string, profileTag:string, dshHome:string, ledgerFile:string, stateFile:string,
|
|
77
|
+
* forksFile:string, pidFile:string, bufferDir:string, snapshotsDir:string, roundsDir:string,
|
|
78
|
+
* runDir:string, forkHomesDir:string}}
|
|
79
|
+
*/
|
|
80
|
+
export function resolveFangLayout(options = {}) {
|
|
81
|
+
const {
|
|
82
|
+
dataRoot = '',
|
|
83
|
+
bufferRoot = '',
|
|
84
|
+
forksRoot = '',
|
|
85
|
+
fangHome = '',
|
|
86
|
+
env = process.env,
|
|
87
|
+
argv = process.argv,
|
|
88
|
+
} = options
|
|
89
|
+
const profileTag = detectProfileTag(argv, env)
|
|
90
|
+
const dshHome = resolveDshHome(env)
|
|
91
|
+
// 显式传入 dataRoot/forksRoot(测试、独立部署)时不要自动跳到 FANG_HOME 默认值,
|
|
92
|
+
// 否则临时目录测试会写进真实的 D:\ToFuture。
|
|
93
|
+
const explicitRoots = Boolean(String(dataRoot ?? '').trim() || String(forksRoot ?? '').trim() || String(bufferRoot ?? '').trim())
|
|
94
|
+
const hasEnvFangHome = Boolean(String(env?.FANG_HOME ?? '').trim())
|
|
95
|
+
const resolvedFangHome = fangHome
|
|
96
|
+
? path.resolve(fangHome)
|
|
97
|
+
: (explicitRoots && !hasEnvFangHome ? '' : resolveFangHome(env))
|
|
98
|
+
const dataDir = dataRoot
|
|
99
|
+
? path.resolve(dataRoot)
|
|
100
|
+
: (resolvedFangHome ? path.join(resolvedFangHome, 'mirror', profileTag) : path.join(dshHome, 'fang', profileTag))
|
|
101
|
+
// 三区分离:母体=mirror/<tag>,原身=essence/,分身=forks/,会话/运行态=sessions/。
|
|
102
|
+
const essenceRoot = resolvedFangHome ? path.join(resolvedFangHome, 'essence') : path.join(dataDir, 'essences')
|
|
103
|
+
const forkHomesDir = forksRoot
|
|
104
|
+
? path.resolve(forksRoot)
|
|
105
|
+
: (resolvedFangHome ? path.join(resolvedFangHome, 'forks') : path.join(dataDir, 'forks'))
|
|
106
|
+
const sessionsRoot = resolvedFangHome ? path.join(resolvedFangHome, 'sessions') : ''
|
|
107
|
+
const runtimeRoot = resolvedFangHome ? path.join(resolvedFangHome, 'sessions') : path.join(dataDir, 'runtime')
|
|
108
|
+
return {
|
|
109
|
+
dataDir,
|
|
110
|
+
profileTag,
|
|
111
|
+
dshHome,
|
|
112
|
+
fangHome: resolvedFangHome,
|
|
113
|
+
ledgerFile: path.join(dataDir, 'audit.jsonl'),
|
|
114
|
+
stateFile: path.join(dataDir, 'state.json'),
|
|
115
|
+
forksFile: path.join(dataDir, 'forks.json'),
|
|
116
|
+
tasksFile: path.join(dataDir, 'tasks.json'),
|
|
117
|
+
pidFile: path.join(dataDir, 'fang.pid'),
|
|
118
|
+
bufferDir: bufferRoot ? path.resolve(bufferRoot) : path.join(dataDir, 'buffer'),
|
|
119
|
+
snapshotsDir: path.join(dataDir, 'snapshots'),
|
|
120
|
+
roundsDir: path.join(dataDir, 'rounds'),
|
|
121
|
+
runDir: path.join(dataDir, 'run'),
|
|
122
|
+
forkHomesDir,
|
|
123
|
+
essenceRoot,
|
|
124
|
+
runtimeRoot,
|
|
125
|
+
sessionsRoot,
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** Matches the line a `dsh web` boot prints: `dsh web: http://127.0.0.1:<port>/...token...` */
|
|
130
|
+
export const WEB_URL_RE = /dsh web:\s+(http:\/\/127\.0\.0\.1:(\d+)\/\S*)/
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Parse a fork's stdout tail into its tokenized web URL.
|
|
134
|
+
* The bare URL is 401, so a line without `?token=` is NOT a usable fork URL and
|
|
135
|
+
* parses to null — failing loud beats handing the UI an address that always 401s.
|
|
136
|
+
* @returns {{url:string, port:number, token:string}|null}
|
|
137
|
+
*/
|
|
138
|
+
export function parseWebUrl(text) {
|
|
139
|
+
if (!text) return null
|
|
140
|
+
const m = WEB_URL_RE.exec(String(text))
|
|
141
|
+
if (!m) return null
|
|
142
|
+
const url = m[1]
|
|
143
|
+
const t = /[?&]token=([^&\s]+)/.exec(url)
|
|
144
|
+
if (!t) return null
|
|
145
|
+
let token = t[1]
|
|
146
|
+
try { token = decodeURIComponent(token) } catch { /* keep raw */ }
|
|
147
|
+
return { url, port: Number(m[2]), token }
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** A fork is healthy only when its loopback URL answers; token is part of the URL. */
|
|
151
|
+
export async function probeWebUrl(url, { timeoutMs = 3000, fetchImpl = globalThis.fetch } = {}) {
|
|
152
|
+
if (typeof fetchImpl !== 'function') return { ok: false, reason: 'no-fetch' }
|
|
153
|
+
const ac = new AbortController()
|
|
154
|
+
const timer = setTimeout(() => ac.abort(), timeoutMs)
|
|
155
|
+
try {
|
|
156
|
+
const res = await fetchImpl(url, { signal: ac.signal })
|
|
157
|
+
return { ok: res.status < 500, status: res.status }
|
|
158
|
+
} catch (e) {
|
|
159
|
+
return { ok: false, reason: String(e && e.message ? e.message : e) }
|
|
160
|
+
} finally {
|
|
161
|
+
clearTimeout(timer)
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Read-only layout of the mother's session/workspace storage. Session catalog uses
|
|
168
|
+
* this; it must never write these paths ($DSH_HOME is the mother's own data).
|
|
169
|
+
*/
|
|
170
|
+
export function resolveSessionsLayout(options = {}) {
|
|
171
|
+
const { env = process.env } = options
|
|
172
|
+
const dshHome = resolveDshHome(env)
|
|
173
|
+
const sessionsDir = path.join(dshHome, 'sessions')
|
|
174
|
+
const storagesDir = path.join(dshHome, 'storages')
|
|
175
|
+
return {
|
|
176
|
+
dshHome,
|
|
177
|
+
sessionsDir,
|
|
178
|
+
storagesDir,
|
|
179
|
+
workspaceFile: path.join(storagesDir, 'workspace.json'),
|
|
180
|
+
projcacheDir: path.join(storagesDir, 'session_projcache'),
|
|
181
|
+
}
|
|
182
|
+
}
|