@twinsearth/dsh-fang 3.2.1 → 3.3.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/client.js +40 -18
- package/docs/DSH-ARCHITECTURE-MAP.md +73 -0
- package/docs/MAIN-CITY-GATE.md +57 -0
- package/package.json +1 -1
- package/src/kernel/types.mjs +1 -1
- package/src/lib/decision-policy.mjs +22 -4
- package/src/lib/main-gate.mjs +526 -0
- package/src/lib/restart-contract.mjs +103 -1
- package/src/plugins/decision-center.mjs +72 -12
- package/src/plugins/forker.mjs +16 -0
- package/src/plugins/healer.mjs +38 -0
- package/src/plugins/restart-guard.mjs +75 -1
- package/src/plugins/scorer.mjs +37 -0
- package/tools/fangctl.mjs +21 -2
- package/tools/gate/main-city.mjs +204 -0
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
3
|
+
// Copyright (c) 2026 dsh-Fang contributors — see LICENSE-CORE.
|
|
4
|
+
//
|
|
5
|
+
// 主城门禁 CLI:任何进主城的动作(打包/发布/安装/部署/新建 profile)先过这里。
|
|
6
|
+
// node tools/gate/main-city.mjs --target source
|
|
7
|
+
// node tools/gate/main-city.mjs --target package --package-dir dist/npm/package --tarball dist/dsh-fang-X.tgz
|
|
8
|
+
// node tools/gate/main-city.mjs --target publish --package-dir ... --tarball ... --tag vX --check-registry
|
|
9
|
+
// node tools/gate/main-city.mjs --target install --home <DSH_HOME> --profile fang-x --package-dir <dir>
|
|
10
|
+
// node tools/gate/main-city.mjs --target deploy --home <DSH_HOME> --profile fang-x --repair
|
|
11
|
+
// node tools/gate/main-city.mjs --target profile --home <DSH_HOME> --profile demo --create
|
|
12
|
+
import fs from 'node:fs'
|
|
13
|
+
import path from 'node:path'
|
|
14
|
+
import { fileURLToPath } from 'node:url'
|
|
15
|
+
|
|
16
|
+
import {
|
|
17
|
+
DEFAULT_MIN_FREE_BYTES,
|
|
18
|
+
DEFAULT_PROFILE_BUNDLES,
|
|
19
|
+
GATE_TARGETS,
|
|
20
|
+
createGatedProfile,
|
|
21
|
+
formatGateFailure,
|
|
22
|
+
runMainGate,
|
|
23
|
+
} from '../../src/lib/main-gate.mjs'
|
|
24
|
+
|
|
25
|
+
function parseArgs(argv = []) {
|
|
26
|
+
const out = { target: 'package', json: false, repair: false, create: false, force: false, allowExisting: false, checkRegistry: false }
|
|
27
|
+
for (let index = 0; index < argv.length; index += 1) {
|
|
28
|
+
const arg = String(argv[index])
|
|
29
|
+
const next = () => {
|
|
30
|
+
if (index + 1 >= argv.length) throw new Error(`missing value for ${arg}`)
|
|
31
|
+
return String(argv[++index])
|
|
32
|
+
}
|
|
33
|
+
if (arg === '--json') { out.json = true; continue }
|
|
34
|
+
if (arg === '--repair') { out.repair = true; continue }
|
|
35
|
+
if (arg === '--create') { out.create = true; continue }
|
|
36
|
+
if (arg === '--force') { out.force = true; continue }
|
|
37
|
+
if (arg === '--allow-existing') { out.allowExisting = true; continue }
|
|
38
|
+
if (arg === '--require-backup') { out.requireBackup = true; continue }
|
|
39
|
+
if (arg === '--check-registry') { out.checkRegistry = true; continue }
|
|
40
|
+
if (arg === '--supervisor-present') { out.supervisorPresent = true; continue }
|
|
41
|
+
if (arg === '--help' || arg === '-h') { out.help = true; continue }
|
|
42
|
+
if (arg === '--target') { out.target = next(); continue }
|
|
43
|
+
if (arg.startsWith('--target=')) { out.target = arg.slice(9); continue }
|
|
44
|
+
if (arg === '--root') { out.root = next(); continue }
|
|
45
|
+
if (arg.startsWith('--root=')) { out.root = arg.slice(7); continue }
|
|
46
|
+
if (arg === '--version') { out.version = next(); continue }
|
|
47
|
+
if (arg.startsWith('--version=')) { out.version = arg.slice(10); continue }
|
|
48
|
+
if (arg === '--dist') { out.distDir = next(); continue }
|
|
49
|
+
if (arg.startsWith('--dist=')) { out.distDir = arg.slice(7); continue }
|
|
50
|
+
if (arg === '--package-dir') { out.packageDir = next(); continue }
|
|
51
|
+
if (arg.startsWith('--package-dir=')) { out.packageDir = arg.slice(14); continue }
|
|
52
|
+
if (arg === '--tarball') { out.tarball = next(); continue }
|
|
53
|
+
if (arg.startsWith('--tarball=')) { out.tarball = arg.slice(10); continue }
|
|
54
|
+
if (arg === '--tag') { out.tag = next(); continue }
|
|
55
|
+
if (arg.startsWith('--tag=')) { out.tag = arg.slice(6); continue }
|
|
56
|
+
if (arg === '--npm-latest') { out.npmLatest = next(); continue }
|
|
57
|
+
if (arg.startsWith('--npm-latest=')) { out.npmLatest = arg.slice(13); continue }
|
|
58
|
+
if (arg === '--home') { out.homeDir = next(); continue }
|
|
59
|
+
if (arg.startsWith('--home=')) { out.homeDir = arg.slice(7); continue }
|
|
60
|
+
if (arg === '--profile') { out.profile = next(); continue }
|
|
61
|
+
if (arg.startsWith('--profile=')) { out.profile = arg.slice(10); continue }
|
|
62
|
+
if (arg === '--profile-dir') { out.profileDir = next(); continue }
|
|
63
|
+
if (arg.startsWith('--profile-dir=')) { out.profileDir = arg.slice(14); continue }
|
|
64
|
+
if (arg === '--data-dir') { out.dataDir = next(); continue }
|
|
65
|
+
if (arg.startsWith('--data-dir=')) { out.dataDir = arg.slice(11); continue }
|
|
66
|
+
if (arg === '--actor') { out.actor = next(); continue }
|
|
67
|
+
if (arg.startsWith('--actor=')) { out.actor = arg.slice(8); continue }
|
|
68
|
+
if (arg === '--bundles') { out.bundles = next().split(',').map((item) => item.trim()).filter(Boolean); continue }
|
|
69
|
+
if (arg.startsWith('--bundles=')) { out.bundles = arg.slice(10).split(',').map((item) => item.trim()).filter(Boolean); continue }
|
|
70
|
+
if (arg === '--min-free-mb') { out.minFreeBytes = Math.max(0, Number(next()) || 0) * 1024 * 1024; continue }
|
|
71
|
+
if (arg.startsWith('--min-free-mb=')) { out.minFreeBytes = Math.max(0, Number(arg.slice(14)) || 0) * 1024 * 1024; continue }
|
|
72
|
+
throw new Error(`unknown argument: ${arg}`)
|
|
73
|
+
}
|
|
74
|
+
if (!out.minFreeBytes) out.minFreeBytes = DEFAULT_MIN_FREE_BYTES
|
|
75
|
+
return out
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const USAGE = `usage: node tools/gate/main-city.mjs --target <${GATE_TARGETS.join('|')}> [options]
|
|
79
|
+
|
|
80
|
+
options:
|
|
81
|
+
--root <dir> source root (default: process.cwd())
|
|
82
|
+
--version <semver> expected version (default: package.json)
|
|
83
|
+
--dist <dir> dist directory containing manifest.json/tarball (default: dist)
|
|
84
|
+
--package-dir <dir> staged package directory (dist/npm/package)
|
|
85
|
+
--tarball <file> explicit tarball path
|
|
86
|
+
--tag <vX.Y.Z> release tag to match against version
|
|
87
|
+
--npm-latest <version> current registry latest (offline check)
|
|
88
|
+
--check-registry fetch npm latest before publish check
|
|
89
|
+
--allow-existing allow an already-published version (idempotent republish)
|
|
90
|
+
--home <dir> DSH_HOME that owns the profile
|
|
91
|
+
--profile <name> profile name
|
|
92
|
+
--profile-dir <dir> explicit profile directory
|
|
93
|
+
--data-dir <dir> explicit fang data dir (default <home>/fang/<profile>)
|
|
94
|
+
--create create the profile after a green gate
|
|
95
|
+
--force allow replacing an existing profile (backup taken)
|
|
96
|
+
--repair create a .gate-backups snapshot when a profile has none
|
|
97
|
+
--require-backup block deploy when no profile backup exists
|
|
98
|
+
--bundles a,b,c bundles for a newly created profile
|
|
99
|
+
--min-free-mb <n> minimum free disk space (default ${Math.round(DEFAULT_MIN_FREE_BYTES / 1024 / 1024)})
|
|
100
|
+
--actor <id> recorded in the chained audit record
|
|
101
|
+
--json machine-readable report
|
|
102
|
+
-h, --help
|
|
103
|
+
`
|
|
104
|
+
|
|
105
|
+
async function npmLatest(version) {
|
|
106
|
+
try {
|
|
107
|
+
const res = await fetch('https://registry.npmjs.org/@twinsearth%2Fdsh-fang', { signal: AbortSignal.timeout(15000) })
|
|
108
|
+
if (!res.ok) return { ok: false, detail: `registry HTTP ${res.status}` }
|
|
109
|
+
const body = await res.json()
|
|
110
|
+
return { ok: true, latest: String(body?.['dist-tags']?.latest ?? '') }
|
|
111
|
+
} catch (error) {
|
|
112
|
+
return { ok: false, detail: String(error?.message ?? error) }
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
async function main() {
|
|
117
|
+
let args
|
|
118
|
+
try { args = parseArgs(process.argv.slice(2)) } catch (error) {
|
|
119
|
+
console.error(`[main-city] ${error?.message ?? error}`)
|
|
120
|
+
console.error(USAGE)
|
|
121
|
+
return 2
|
|
122
|
+
}
|
|
123
|
+
if (args.help) { console.log(USAGE); return 0 }
|
|
124
|
+
if (!GATE_TARGETS.includes(args.target)) {
|
|
125
|
+
console.error(`[main-city] unknown target: ${args.target}`)
|
|
126
|
+
console.error(USAGE)
|
|
127
|
+
return 2
|
|
128
|
+
}
|
|
129
|
+
if (args.checkRegistry && args.target === 'publish') {
|
|
130
|
+
const latest = await npmLatest()
|
|
131
|
+
if (latest.ok) args.npmLatest = latest.latest
|
|
132
|
+
else if (!args.json) console.warn(`[main-city] registry check skipped: ${latest.detail}`)
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
let report = runMainGate({
|
|
136
|
+
target: args.target,
|
|
137
|
+
root: args.root,
|
|
138
|
+
version: args.version,
|
|
139
|
+
distDir: args.distDir,
|
|
140
|
+
packageDir: args.packageDir,
|
|
141
|
+
tarball: args.tarball,
|
|
142
|
+
tag: args.tag,
|
|
143
|
+
npmLatest: args.npmLatest,
|
|
144
|
+
allowExisting: args.allowExisting,
|
|
145
|
+
homeDir: args.homeDir,
|
|
146
|
+
profile: args.profile,
|
|
147
|
+
profileDir: args.profileDir,
|
|
148
|
+
dataDir: args.dataDir,
|
|
149
|
+
repair: args.repair,
|
|
150
|
+
requireBackup: args.requireBackup,
|
|
151
|
+
supervisorPresent: args.supervisorPresent,
|
|
152
|
+
create: args.create,
|
|
153
|
+
force: args.force,
|
|
154
|
+
minFreeBytes: args.minFreeBytes,
|
|
155
|
+
actor: args.actor,
|
|
156
|
+
})
|
|
157
|
+
|
|
158
|
+
if (args.target === 'profile' && args.create === true && report.ok) {
|
|
159
|
+
const created = createGatedProfile({
|
|
160
|
+
homeDir: args.homeDir,
|
|
161
|
+
profile: args.profile,
|
|
162
|
+
bundles: args.bundles ?? DEFAULT_PROFILE_BUNDLES,
|
|
163
|
+
force: args.force,
|
|
164
|
+
minFreeBytes: args.minFreeBytes,
|
|
165
|
+
})
|
|
166
|
+
report = { ...report, create: created }
|
|
167
|
+
if (!created.ok) {
|
|
168
|
+
report.ok = false
|
|
169
|
+
report.blockers = [...(report.blockers ?? []), { id: 'profile-create: failed', detail: `${created.error}${created.detail ? ': ' + created.detail : ''}` }]
|
|
170
|
+
report.summary = { ...report.summary, blockers: report.summary.blockers + 1 }
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const reportFile = path.join(process.cwd(), 'dist', 'main-city-gate.json')
|
|
175
|
+
try {
|
|
176
|
+
fs.mkdirSync(path.dirname(reportFile), { recursive: true })
|
|
177
|
+
fs.writeFileSync(reportFile, JSON.stringify(report, null, 2) + '\n', 'utf8')
|
|
178
|
+
} catch { /* report is best-effort */ }
|
|
179
|
+
|
|
180
|
+
if (args.json) console.log(JSON.stringify(report, null, 2))
|
|
181
|
+
else {
|
|
182
|
+
for (const item of report.checks ?? []) {
|
|
183
|
+
if (item.ok && item.severity === 'info') continue
|
|
184
|
+
console.log(`${item.ok ? 'PASS' : 'FAIL'} ${item.id.padEnd(48)} ${item.detail}`)
|
|
185
|
+
if (item.repair) console.log(` repair: ${item.repair}`)
|
|
186
|
+
}
|
|
187
|
+
console.log(`\n${report.ok ? 'GREEN' : 'RED'} main-city gate ${report.target} ` +
|
|
188
|
+
`blockers=${report.summary?.blockers ?? 0} warnings=${report.summary?.warnings ?? 0} grade=${report.score?.grade ?? '?'}`)
|
|
189
|
+
if (!report.ok) console.log(formatGateFailure(report))
|
|
190
|
+
console.log(`report: ${reportFile}`)
|
|
191
|
+
}
|
|
192
|
+
return report.ok ? 0 : 1
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const isMain = (() => {
|
|
196
|
+
try { return Boolean(process.argv[1]) && path.resolve(process.argv[1]) === path.resolve(fileURLToPath(import.meta.url)) } catch { return false }
|
|
197
|
+
})()
|
|
198
|
+
|
|
199
|
+
if (isMain) {
|
|
200
|
+
main().then((code) => { process.exitCode = code }).catch((error) => {
|
|
201
|
+
console.error(`[main-city] ${error?.stack ?? error}`)
|
|
202
|
+
process.exitCode = 1
|
|
203
|
+
})
|
|
204
|
+
}
|