@jjh32456/longlive 0.0.0-stage → 2.0.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/CHANGELOG.md +235 -0
- package/LICENSE +21 -0
- package/QUICKSTART.md +362 -0
- package/README.en.md +64 -0
- package/README.md +256 -2
- package/SPEC.md +211 -0
- package/build-info.json +1665 -0
- package/cordis.patch.yml +208 -0
- package/docs/A1-MIGRATION.md +75 -0
- package/docs/ANSWER-GUARD.md +92 -0
- package/docs/ARCHITECTURE.md +128 -0
- package/docs/AUTO-RECALL-WIRING.md +115 -0
- package/docs/AUTO-RECALL.md +237 -0
- package/docs/CONCEPTS.md +143 -0
- package/docs/CONFIG.md +839 -0
- package/docs/ERRORS.md +271 -0
- package/docs/FAQ.md +186 -0
- package/docs/FEATURES.md +579 -0
- package/docs/INDEX-FRESHNESS.md +21 -0
- package/docs/METRICS.md +244 -0
- package/docs/MIGRATION-1.6.0.md +220 -0
- package/docs/OPERATIONS.md +439 -0
- package/docs/PANEL-INTEGRATION.md +190 -0
- package/docs/PRIVACY.md +88 -0
- package/docs/QUICKSTART.md +155 -0
- package/docs/RAW-INDEX-POLICY.md +7 -0
- package/docs/README.en.md +48 -0
- package/docs/RESIDENT-WATCHER.md +90 -0
- package/docs/SESSION-WATCH.md +214 -0
- package/docs/TOOLS.md +184 -0
- package/docs/TROUBLESHOOTING.md +329 -0
- package/icon.svg +11 -0
- package/lib/client.js +436 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +163 -4
- package/runbook.md +343 -0
- package/scripts/archive-digest-build.mjs +668 -0
- package/scripts/auto-truncate-run.ps1 +752 -0
- package/scripts/backfill-auto-cards.mjs +98 -0
- package/scripts/edges-build.mjs +1003 -0
- package/scripts/index-maintain.mjs +39 -0
- package/scripts/index-reconcile.mjs +49 -0
- package/scripts/index-status.mjs +28 -0
- package/scripts/install-resident-watcher.ps1 +15 -0
- package/scripts/install-session-watch.ps1 +332 -0
- package/scripts/install-surgery-task.ps1 +174 -0
- package/scripts/lib/auto-truncate-run.ps1 +512 -0
- package/scripts/lib/session-watch-fixture.mjs +207 -0
- package/scripts/longlive-doctor.mjs +578 -0
- package/scripts/metrics-read.mjs +79 -0
- package/scripts/metrics-sample.mjs +215 -0
- package/scripts/migrate-180.mjs +163 -0
- package/scripts/migrate-200.mjs +8 -0
- package/scripts/migrate-a1-legacy.mjs +62 -0
- package/scripts/migrate-lifecycle.mjs +157 -0
- package/scripts/pack.mjs +392 -0
- package/scripts/recall-index-build.mjs +128 -0
- package/scripts/recall-links-build.mjs +420 -0
- package/scripts/rescore-cards.mjs +36 -0
- package/scripts/resident-watcher-status.ps1 +15 -0
- package/scripts/resident-watcher.ps1 +643 -0
- package/scripts/session-watch-drill.mjs +553 -0
- package/scripts/session-watch-status.ps1 +146 -0
- package/scripts/session-watch.mjs +1367 -0
- package/scripts/surgery-inspect.mjs +122 -0
- package/scripts/surgery-run.cmd +34 -0
- package/scripts/truncate-cli.mjs +279 -0
- package/scripts/unattended-surgery.ps1 +1019 -0
- package/scripts/uninstall-resident-watcher.ps1 +15 -0
- package/scripts/uninstall-session-watch.ps1 +192 -0
- package/scripts/verify-card-anchors.mjs +138 -0
- package/scripts/watch-loop.ps1 +916 -0
- package/src/actions/audit.js +125 -0
- package/src/actions/evidence.js +122 -0
- package/src/actions/fs.js +162 -0
- package/src/activity/index.js +873 -0
- package/src/activity/model.js +351 -0
- package/src/activity/store.js +786 -0
- package/src/activity/summary.js +180 -0
- package/src/activity/thinking.js +191 -0
- package/src/adapters/constants.js +11 -0
- package/src/adapters/engram/data/aliases.json +51 -0
- package/src/adapters/engram/data/policy.json +8 -0
- package/src/adapters/engram/data/routing-rules.json +546 -0
- package/src/adapters/engram/index.js +278 -0
- package/src/adapters/registry.js +315 -0
- package/src/answer/audit.js +238 -0
- package/src/answer/claim-guard.mjs +234 -0
- package/src/answer/evidence.js +30 -0
- package/src/answer/guard.js +1370 -0
- package/src/answer/index.js +1086 -0
- package/src/answer/query.js +111 -0
- package/src/answer/second-hop.mjs +101 -0
- package/src/auto-truncate-status.js +164 -0
- package/src/auto-truncate.js +1181 -0
- package/src/autoprocedure/candidate.js +342 -0
- package/src/autoprocedure/config.js +122 -0
- package/src/autoprocedure/evidence.js +345 -0
- package/src/autoprocedure/index.js +83 -0
- package/src/autoprocedure/ledger.js +329 -0
- package/src/autoprocedure/lint.js +232 -0
- package/src/autoprocedure/llm.js +409 -0
- package/src/autoprocedure/locks.js +102 -0
- package/src/autoprocedure/paths.js +39 -0
- package/src/autoprocedure/privacy.js +87 -0
- package/src/autoprocedure/rules.js +288 -0
- package/src/autoprocedure/runtime.js +718 -0
- package/src/autoprocedure/session-reader.js +328 -0
- package/src/backup.js +310 -0
- package/src/bus/bus.mjs +1446 -0
- package/src/codec.js +269 -0
- package/src/compaction/cut-engine.js +44 -0
- package/src/compaction/engine-core.js +1237 -0
- package/src/compaction/entry.js +228 -0
- package/src/compaction.js +230 -0
- package/src/conclusion.js +366 -0
- package/src/config.js +835 -0
- package/src/d6/runtime.js +517 -0
- package/src/d6/surface.js +181 -0
- package/src/deps.js +189 -0
- package/src/engine-status.js +141 -0
- package/src/features.js +1035 -0
- package/src/handoff/index.js +1077 -0
- package/src/handoff/model.js +1058 -0
- package/src/handoff/store.js +1233 -0
- package/src/heartbeat.js +385 -0
- package/src/home.js +16 -0
- package/src/host-version.js +280 -0
- package/src/hot-truncate-diag.js +98 -0
- package/src/identity/card-marker.js +281 -0
- package/src/identity/handover.js +845 -0
- package/src/identity/index.js +337 -0
- package/src/identity/inject-position.js +133 -0
- package/src/identity/writeback.js +344 -0
- package/src/identity.js +118 -0
- package/src/index.js +647 -0
- package/src/lifecycle/gap-clocks.js +364 -0
- package/src/lifecycle/index.js +269 -0
- package/src/lifecycle/proactive.js +1426 -0
- package/src/live-target.js +212 -0
- package/src/live.js +1603 -0
- package/src/log-rotation.js +153 -0
- package/src/memory/a1-migration.js +190 -0
- package/src/memory/activity-capture.js +229 -0
- package/src/memory/anchor-fallback.js +116 -0
- package/src/memory/anchors.js +194 -0
- package/src/memory/archive-cold-state.mjs +794 -0
- package/src/memory/archive-staging.mjs +305 -0
- package/src/memory/archive.mjs +415 -0
- package/src/memory/audit-records.js +222 -0
- package/src/memory/auto-cards.js +441 -0
- package/src/memory/capture-host.js +297 -0
- package/src/memory/capture.js +496 -0
- package/src/memory/card-anchors.js +203 -0
- package/src/memory/card-priority.js +110 -0
- package/src/memory/cards.js +425 -0
- package/src/memory/catchup.mjs +534 -0
- package/src/memory/conclusion-distill.js +381 -0
- package/src/memory/conclusion-drafts.js +243 -0
- package/src/memory/conflicts.js +1060 -0
- package/src/memory/consolidate.js +834 -0
- package/src/memory/decay.js +311 -0
- package/src/memory/distill.js +485 -0
- package/src/memory/event-index.js +1826 -0
- package/src/memory/evidence-chain.js +295 -0
- package/src/memory/evidence.js +213 -0
- package/src/memory/filter-runner.js +555 -0
- package/src/memory/filter.js +1035 -0
- package/src/memory/forget-v2.js +680 -0
- package/src/memory/forget.js +531 -0
- package/src/memory/frames-activation.js +249 -0
- package/src/memory/frontmatter.js +454 -0
- package/src/memory/fts-query.js +342 -0
- package/src/memory/governance.js +604 -0
- package/src/memory/index-freshness.js +234 -0
- package/src/memory/index.js +435 -0
- package/src/memory/l2-gate.js +169 -0
- package/src/memory/land.mjs +743 -0
- package/src/memory/lifecycle.js +235 -0
- package/src/memory/lock.js +121 -0
- package/src/memory/maintenance.js +1149 -0
- package/src/memory/manifest.js +245 -0
- package/src/memory/memory-health.mjs +607 -0
- package/src/memory/migrate-200.js +1681 -0
- package/src/memory/migrate-lifecycle.js +1808 -0
- package/src/memory/migrate.js +139 -0
- package/src/memory/paths.js +229 -0
- package/src/memory/procedure-candidate.mjs +377 -0
- package/src/memory/procedure.js +982 -0
- package/src/memory/prospective-memory.mjs +362 -0
- package/src/memory/provenance.js +119 -0
- package/src/memory/recall-feedback.mjs +406 -0
- package/src/memory/recall-v2.js +218 -0
- package/src/memory/recall.js +357 -0
- package/src/memory/reconcile.js +265 -0
- package/src/memory/records.js +433 -0
- package/src/memory/reinforce.js +281 -0
- package/src/memory/retention.js +162 -0
- package/src/memory/review.js +124 -0
- package/src/memory/state.js +279 -0
- package/src/memory/tiers.js +168 -0
- package/src/memory/topic-phrases.js +73 -0
- package/src/memory/trust.js +97 -0
- package/src/memory/turn-budget.mjs +290 -0
- package/src/memory/version-chain.js +264 -0
- package/src/memory/wake-bridge.mjs +2999 -0
- package/src/memory/wal.js +137 -0
- package/src/memory/working-frame.js +779 -0
- package/src/metrics/classify.mjs +191 -0
- package/src/metrics/estimate.mjs +108 -0
- package/src/metrics/index.mjs +204 -0
- package/src/metrics/measure.mjs +462 -0
- package/src/metrics/prestep-latency.mjs +307 -0
- package/src/metrics/store.mjs +264 -0
- package/src/original.js +294 -0
- package/src/policy/threshold-policy.js +224 -0
- package/src/policy-profile.js +463 -0
- package/src/recall/anaphora.mjs +87 -0
- package/src/recall/archive-config.mjs +130 -0
- package/src/recall/archive-fallback.mjs +390 -0
- package/src/recall/archive-search.mjs +428 -0
- package/src/recall/archive-state.mjs +588 -0
- package/src/recall/brain.mjs +501 -0
- package/src/recall/cards.mjs +202 -0
- package/src/recall/current-line.mjs +110 -0
- package/src/recall/edges.js +398 -0
- package/src/recall/embedding.mjs +698 -0
- package/src/recall/entities.mjs +112 -0
- package/src/recall/explain.mjs +443 -0
- package/src/recall/force-policy.mjs +138 -0
- package/src/recall/gate-log.mjs +280 -0
- package/src/recall/gate-metrics.mjs +191 -0
- package/src/recall/index.js +1337 -0
- package/src/recall/inject-order.mjs +279 -0
- package/src/recall/link-recall.mjs +183 -0
- package/src/recall/links.mjs +696 -0
- package/src/recall/privacy.mjs +208 -0
- package/src/recall/recall-core.mjs +810 -0
- package/src/recall/shared-visibility.js +302 -0
- package/src/recall/time-normalizer.mjs +196 -0
- package/src/recall/turn-budget-inject.mjs +154 -0
- package/src/retention/cold-gate.mjs +146 -0
- package/src/retention/config.js +163 -0
- package/src/retention/index.js +801 -0
- package/src/role/activation.js +191 -0
- package/src/role/attention.js +318 -0
- package/src/role/base.js +36 -0
- package/src/role/channels.js +423 -0
- package/src/role/index.js +425 -0
- package/src/role/interfaces.js +115 -0
- package/src/role/personality.js +370 -0
- package/src/role/readers.js +151 -0
- package/src/role/relationships.js +477 -0
- package/src/role/store.js +202 -0
- package/src/role/writeprotect.js +373 -0
- package/src/router/aliases.json +182 -0
- package/src/router/background/agent-runner.mjs +239 -0
- package/src/router/background/anchor.mjs +323 -0
- package/src/router/background/audit.mjs +337 -0
- package/src/router/background/budget-ledger.mjs +648 -0
- package/src/router/background/host-sim.mjs +451 -0
- package/src/router/background/index.mjs +81 -0
- package/src/router/background/memory.mjs +63 -0
- package/src/router/background/mock-llm.mjs +203 -0
- package/src/router/background/queue.mjs +224 -0
- package/src/router/background/retry.mjs +255 -0
- package/src/router/background/router.mjs +1459 -0
- package/src/router/background/stream.mjs +276 -0
- package/src/router/background/tool-policy.mjs +255 -0
- package/src/router/design-headings.json +221 -0
- package/src/router/face/config.mjs +155 -0
- package/src/router/face/context.mjs +284 -0
- package/src/router/face/face.mjs +466 -0
- package/src/router/face/identity.mjs +130 -0
- package/src/router/face/index.mjs +65 -0
- package/src/router/face/journal.mjs +119 -0
- package/src/router/face/llm.mjs +252 -0
- package/src/router/face/outbox.mjs +153 -0
- package/src/router/face/paths.mjs +54 -0
- package/src/router/judge.mjs +987 -0
- package/src/router/policies/generic.json +131 -0
- package/src/router/route.js +1766 -0
- package/src/router/router.mjs +535 -0
- package/src/router/routing-rules.json +816 -0
- package/src/router/run-sim.mjs +530 -0
- package/src/router/runtime.js +569 -0
- package/src/router/service/apply.mjs +1218 -0
- package/src/router/service/board.mjs +712 -0
- package/src/router/service/caller.mjs +131 -0
- package/src/router/service/config.mjs +361 -0
- package/src/router/service/context.mjs +314 -0
- package/src/router/service/hint.mjs +668 -0
- package/src/router/service/lease.mjs +494 -0
- package/src/router/service/live.mjs +251 -0
- package/src/router/service/lock.mjs +550 -0
- package/src/router/service/merge.mjs +255 -0
- package/src/router/service/model-resolve.mjs +350 -0
- package/src/router/service/paths.mjs +168 -0
- package/src/router/service/proposals.mjs +1461 -0
- package/src/router/service/queue.mjs +477 -0
- package/src/router/service/reconcile.mjs +271 -0
- package/src/router/service/service.mjs +2998 -0
- package/src/router/service/sources.mjs +352 -0
- package/src/router/service/status.mjs +520 -0
- package/src/router/service/store.mjs +476 -0
- package/src/router/service/ticklock.mjs +239 -0
- package/src/router/service/tokens.mjs +95 -0
- package/src/router/service/tools.mjs +248 -0
- package/src/router/service/wake.mjs +544 -0
- package/src/router/service/wal.mjs +685 -0
- package/src/router/shadow.mjs +821 -0
- package/src/surgery-recovery.js +1909 -0
- package/src/tool-output.js +485 -0
- package/src/tools.js +3262 -0
- package/src/truncate-visible.js +2416 -0
- package/src/truncate.js +999 -0
- package/src/ui/actions.js +1312 -0
- package/src/ui/flags.js +259 -0
- package/src/ui/panel-api.js +371 -0
- package/src/ui/panel-auth.js +251 -0
- package/src/ui/panel-integrity.js +243 -0
- package/src/ui/routes-readonly.js +759 -0
- package/src/ui/routes.js +284 -0
- package/src/util/append-store.js +368 -0
- package/src/util/local-day.js +30 -0
- package/src/util/redact.js +227 -0
- package/src/util/stale-locks.js +148 -0
- package/src/version.js +99 -0
- package/test/fixtures/pre-step-container.mjs +100 -0
- package/test/p1_probe.mjs +97 -0
package/src/deps.js
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { optionalPeerNames as adapterOptionalPeerNames } from './adapters/registry.js';
|
|
2
|
+
|
|
3
|
+
// Optional runtime dependencies for dsh-longlive (0.8.0-rc.8).
|
|
4
|
+
//
|
|
5
|
+
// longlive has no hard runtime dependencies. It cooperates with optional
|
|
6
|
+
// external memory plugins (registered via src/adapters/) when one is installed
|
|
7
|
+
// in the host profile, but it must load and work without them. This module answers one question for the startup
|
|
8
|
+
// path and for scripts/longlive-doctor.mjs:
|
|
9
|
+
//
|
|
10
|
+
// "Which DSH profiles are in scope, and is the optional peer enabled there?"
|
|
11
|
+
//
|
|
12
|
+
// Enabled means either an own key in the profile's `dependencies` (installed)
|
|
13
|
+
// or the package name in `dsh.profile.bundles` (enabled bundle). Nothing here
|
|
14
|
+
// throws: an unreadable or missing profile manifest is skipped, and a DSH home
|
|
15
|
+
// without profiles is reported as vacuously fine. Startup must never crash
|
|
16
|
+
// because an optional peer is absent.
|
|
17
|
+
|
|
18
|
+
import fs from 'node:fs';
|
|
19
|
+
import os from 'node:os';
|
|
20
|
+
import path from 'node:path';
|
|
21
|
+
import { fileURLToPath } from 'node:url';
|
|
22
|
+
|
|
23
|
+
const HERE = path.dirname(fileURLToPath(import.meta.url));
|
|
24
|
+
const PACKAGE_ROOT = path.resolve(HERE, '..');
|
|
25
|
+
|
|
26
|
+
/** The plugin itself — used to find profiles where longlive is enabled. */
|
|
27
|
+
export const LONGLIVE_PLUGIN = '@jjh32456/longlive';
|
|
28
|
+
|
|
29
|
+
/** Used only when package.json cannot be read or has no optional peers. */
|
|
30
|
+
export const KNOWN_OPTIONAL_PEERS = Object.freeze(adapterOptionalPeerNames());
|
|
31
|
+
|
|
32
|
+
let manifestCache = null;
|
|
33
|
+
|
|
34
|
+
function ownManifest() {
|
|
35
|
+
if (manifestCache) return manifestCache;
|
|
36
|
+
try {
|
|
37
|
+
manifestCache = JSON.parse(fs.readFileSync(path.join(PACKAGE_ROOT, 'package.json'), 'utf8'));
|
|
38
|
+
} catch {
|
|
39
|
+
manifestCache = { peerDependenciesMeta: Object.fromEntries(KNOWN_OPTIONAL_PEERS.map((name) => [name, { optional: true }])) };
|
|
40
|
+
}
|
|
41
|
+
return manifestCache;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Names of optional peers declared by a package manifest.
|
|
46
|
+
* Falls back to the known peer when the manifest carries no
|
|
47
|
+
* `peerDependenciesMeta.optional` entries.
|
|
48
|
+
*/
|
|
49
|
+
export function optionalPeerNames(manifest = ownManifest()) {
|
|
50
|
+
const peers = manifest?.peerDependencies;
|
|
51
|
+
const meta = manifest?.peerDependenciesMeta;
|
|
52
|
+
const names = [];
|
|
53
|
+
if (meta && typeof meta === 'object') {
|
|
54
|
+
for (const [name, settings] of Object.entries(meta)) {
|
|
55
|
+
if (settings?.optional === true && (!peers || Object.prototype.hasOwnProperty.call(peers, name))) {
|
|
56
|
+
names.push(name);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return names.length > 0 ? names : KNOWN_OPTIONAL_PEERS.slice();
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** DSH home: explicit option, then $DSH_HOME, then <homedir>/.dsh. */
|
|
64
|
+
export function resolveDshHome(env = process.env) {
|
|
65
|
+
const configured = env?.DSH_HOME;
|
|
66
|
+
if (typeof configured === 'string' && configured.trim() !== '') {
|
|
67
|
+
return path.resolve(configured.trim());
|
|
68
|
+
}
|
|
69
|
+
return path.join(os.homedir(), '.dsh');
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Read <home>/profiles/<name>/package.json.
|
|
74
|
+
* Returns [{name, manifest, error}] sorted by name. Never throws: a missing
|
|
75
|
+
* profiles directory is an empty list, an unreadable manifest keeps its row
|
|
76
|
+
* with `manifest: null` and an `error`.
|
|
77
|
+
*/
|
|
78
|
+
export function scanProfiles(home) {
|
|
79
|
+
const profilesDir = path.join(home, 'profiles');
|
|
80
|
+
let entries;
|
|
81
|
+
try {
|
|
82
|
+
entries = fs.readdirSync(profilesDir, { withFileTypes: true });
|
|
83
|
+
} catch {
|
|
84
|
+
return [];
|
|
85
|
+
}
|
|
86
|
+
const rows = [];
|
|
87
|
+
for (const entry of entries) {
|
|
88
|
+
if (!entry.isDirectory()) continue;
|
|
89
|
+
const manifestPath = path.join(profilesDir, entry.name, 'package.json');
|
|
90
|
+
try {
|
|
91
|
+
const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8'));
|
|
92
|
+
rows.push({ name: entry.name, manifest, error: null });
|
|
93
|
+
} catch (error) {
|
|
94
|
+
rows.push({ name: entry.name, manifest: null, error });
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
rows.sort((a, b) => a.name.localeCompare(b.name));
|
|
98
|
+
return rows;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function bundleNames(manifest) {
|
|
102
|
+
const bundles = manifest?.dsh?.profile?.bundles;
|
|
103
|
+
if (!Array.isArray(bundles)) return [];
|
|
104
|
+
return bundles
|
|
105
|
+
.map((bundle) => (typeof bundle === 'string' ? bundle : bundle?.name))
|
|
106
|
+
.filter((name) => typeof name === 'string' && name !== '');
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function hasPackage(manifest, name) {
|
|
110
|
+
if (!manifest || typeof manifest !== 'object') return false;
|
|
111
|
+
const deps = manifest.dependencies;
|
|
112
|
+
if (deps && typeof deps === 'object' && Object.prototype.hasOwnProperty.call(deps, name)) {
|
|
113
|
+
return true;
|
|
114
|
+
}
|
|
115
|
+
return bundleNames(manifest).includes(name);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* One-line startup guidance for missing optional peers.
|
|
120
|
+
*
|
|
121
|
+
* Returns null when nothing is missing, so a healthy profile stays quiet.
|
|
122
|
+
* Deliberately pure: the startup path must never install or prompt, only point
|
|
123
|
+
* at the explicit `--setup` wizard, which prints a plan and asks for YES.
|
|
124
|
+
*/
|
|
125
|
+
export function formatMissingDepsWarning(missing) {
|
|
126
|
+
const names = Array.isArray(missing)
|
|
127
|
+
? missing.filter((name) => typeof name === 'string' && name.trim() !== '')
|
|
128
|
+
: [];
|
|
129
|
+
if (names.length === 0) return null;
|
|
130
|
+
return `可选依赖缺失:${names.join('、')};安装运行 \`node scripts/longlive-doctor.mjs --setup\`(会先列清单再要你确认)`;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Check the optional peers against the profiles that matter.
|
|
135
|
+
*
|
|
136
|
+
* Scope:
|
|
137
|
+
* 1. explicit `options.profile` or $DSH_PROFILE -> that profile only;
|
|
138
|
+
* 2. otherwise profiles where dsh-longlive is enabled, requiring the
|
|
139
|
+
* peer in every one of them;
|
|
140
|
+
* 3. if longlive is enabled nowhere, every profile, accepting the peer if any
|
|
141
|
+
* profile has it.
|
|
142
|
+
*
|
|
143
|
+
* @returns {{ok: boolean, profile: string|null, missing: string[], present: string[]}}
|
|
144
|
+
*/
|
|
145
|
+
export function checkOptionalDeps(options = {}) {
|
|
146
|
+
const env = options.env ?? process.env;
|
|
147
|
+
const home = options.home ? path.resolve(options.home) : resolveDshHome(env);
|
|
148
|
+
const dependencies = Array.isArray(options.dependencies) && options.dependencies.length > 0
|
|
149
|
+
? options.dependencies.slice()
|
|
150
|
+
: optionalPeerNames(ownManifest());
|
|
151
|
+
|
|
152
|
+
const rows = scanProfiles(home);
|
|
153
|
+
const readable = rows.filter((row) => row.manifest && !row.error);
|
|
154
|
+
|
|
155
|
+
// No profile directory at all: nothing to check, and startup must stay quiet.
|
|
156
|
+
if (readable.length === 0) {
|
|
157
|
+
return { ok: true, profile: null, missing: [], present: [] };
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const requested = options.profile ?? env?.DSH_PROFILE;
|
|
161
|
+
let scoped;
|
|
162
|
+
let profile;
|
|
163
|
+
let requireAll;
|
|
164
|
+
if (typeof requested === 'string' && requested.trim() !== '') {
|
|
165
|
+
scoped = readable.filter((row) => row.name === requested.trim());
|
|
166
|
+
profile = requested.trim();
|
|
167
|
+
requireAll = true;
|
|
168
|
+
} else {
|
|
169
|
+
const longliveProfiles = readable.filter((row) => hasPackage(row.manifest, LONGLIVE_PLUGIN));
|
|
170
|
+
if (longliveProfiles.length > 0) {
|
|
171
|
+
scoped = longliveProfiles;
|
|
172
|
+
requireAll = true;
|
|
173
|
+
} else {
|
|
174
|
+
scoped = readable;
|
|
175
|
+
requireAll = false;
|
|
176
|
+
}
|
|
177
|
+
profile = scoped.length === 1 ? scoped[0].name : scoped.map((row) => row.name).join(',');
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const present = [];
|
|
181
|
+
const missing = [];
|
|
182
|
+
for (const dependency of dependencies) {
|
|
183
|
+
const holds = scoped.filter((row) => hasPackage(row.manifest, dependency)).length;
|
|
184
|
+
const ok = requireAll ? scoped.length > 0 && holds === scoped.length : holds > 0;
|
|
185
|
+
(ok ? present : missing).push(dependency);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
return { ok: missing.length === 0, profile: profile || null, missing, present };
|
|
189
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
// engines.dsh self-check data for the panel.
|
|
2
|
+
//
|
|
3
|
+
// The plugin never blocks on this: `apply()` warns once and keeps loading, and
|
|
4
|
+
// this module only persists the verdict to
|
|
5
|
+
// `<memoryRoot>/.longlive/status.json` so a read-only panel can show a red dot
|
|
6
|
+
// without parsing host logs. Shape (documented in docs/PANEL-INTEGRATION.md):
|
|
7
|
+
//
|
|
8
|
+
// {
|
|
9
|
+
// "engineMismatch": {
|
|
10
|
+
// "hostVersion": "0.1.9",
|
|
11
|
+
// "required": ">=0.2.0 <1.0.0",
|
|
12
|
+
// "at": "2026-11-05T13:02:01.000Z",
|
|
13
|
+
// "source": "config.hostVersion", // how the version was found
|
|
14
|
+
// "path": "..." // only for hostPackage
|
|
15
|
+
// }
|
|
16
|
+
// }
|
|
17
|
+
//
|
|
18
|
+
// Rules:
|
|
19
|
+
// - mismatch -> write/update `engineMismatch`; other keys in the file survive;
|
|
20
|
+
// - match -> delete a stale `engineMismatch`; delete the file if that was
|
|
21
|
+
// the only key, so a recovered host stops showing a red dot;
|
|
22
|
+
// - unknown -> leave the file alone (no evidence either way);
|
|
23
|
+
// - every disk error is returned, never thrown: a read-only memory root must
|
|
24
|
+
// not become a failed plugin load.
|
|
25
|
+
|
|
26
|
+
import fs from 'node:fs';
|
|
27
|
+
import path from 'node:path';
|
|
28
|
+
|
|
29
|
+
export const ENGINE_STATUS_DIR = '.longlive';
|
|
30
|
+
export const ENGINE_STATUS_FILE = 'status.json';
|
|
31
|
+
|
|
32
|
+
/** `<memoryRoot>/.longlive/status.json`, or null when no root is configured. */
|
|
33
|
+
export function engineStatusPath(memoryRoot) {
|
|
34
|
+
if (typeof memoryRoot !== 'string' || !memoryRoot.trim()) return null;
|
|
35
|
+
return path.join(path.resolve(memoryRoot.trim()), ENGINE_STATUS_DIR, ENGINE_STATUS_FILE);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function timestamp(now) {
|
|
39
|
+
try {
|
|
40
|
+
const value = typeof now === 'function' ? now() : now;
|
|
41
|
+
if (value === undefined || value === null || value === '') return new Date().toISOString();
|
|
42
|
+
const date = value instanceof Date ? value : new Date(value);
|
|
43
|
+
if (Number.isNaN(date.getTime())) return new Date().toISOString();
|
|
44
|
+
return date.toISOString();
|
|
45
|
+
} catch {
|
|
46
|
+
return new Date().toISOString();
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function readStatus(file) {
|
|
51
|
+
try {
|
|
52
|
+
const value = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
53
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
54
|
+
return { exists: true, value: null, corrupt: true };
|
|
55
|
+
}
|
|
56
|
+
return { exists: true, value, corrupt: false };
|
|
57
|
+
} catch (error) {
|
|
58
|
+
if (error?.code === 'ENOENT') return { exists: false, value: null, corrupt: false };
|
|
59
|
+
return { exists: true, value: null, corrupt: true };
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Atomic enough for a single small file: tmp in the same directory, then rename. */
|
|
64
|
+
function writeStatus(file, value) {
|
|
65
|
+
const dir = path.dirname(file);
|
|
66
|
+
const tmp = path.join(dir, `.${ENGINE_STATUS_FILE}.tmp-${process.pid}-` +
|
|
67
|
+
`${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}`);
|
|
68
|
+
try {
|
|
69
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
70
|
+
fs.writeFileSync(tmp, `${JSON.stringify(value, null, 2)}\n`, 'utf8');
|
|
71
|
+
fs.renameSync(tmp, file);
|
|
72
|
+
return { ok: true };
|
|
73
|
+
} catch (error) {
|
|
74
|
+
try { fs.unlinkSync(tmp); } catch { /* best effort */ }
|
|
75
|
+
return { ok: false, error };
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function mismatchRecord(compat, detection, now) {
|
|
80
|
+
const record = {
|
|
81
|
+
hostVersion: compat.version,
|
|
82
|
+
required: compat.range,
|
|
83
|
+
at: timestamp(now),
|
|
84
|
+
};
|
|
85
|
+
if (detection?.source) record.source = detection.source;
|
|
86
|
+
if (detection?.path) record.path = detection.path;
|
|
87
|
+
return record;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Persist one compatibility verdict.
|
|
92
|
+
*
|
|
93
|
+
* @param {string|null} memoryRoot resolved memory root (null disables writes)
|
|
94
|
+
* @param {{compat?: object, detection?: object, now?: Date|string|number|Function}} options
|
|
95
|
+
* @returns {{ok: boolean, action: string, file?: string, error?: Error}}
|
|
96
|
+
*/
|
|
97
|
+
export function recordEngineStatus(memoryRoot, options = {}) {
|
|
98
|
+
const file = engineStatusPath(memoryRoot);
|
|
99
|
+
if (!file) return { ok: false, action: 'no-memory-root' };
|
|
100
|
+
|
|
101
|
+
const compat = options.compat;
|
|
102
|
+
if (!compat || typeof compat !== 'object' || typeof compat.status !== 'string') {
|
|
103
|
+
return { ok: false, action: 'no-compat', file };
|
|
104
|
+
}
|
|
105
|
+
if (compat.status === 'unknown') {
|
|
106
|
+
return { ok: true, action: 'unknown', file };
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const state = readStatus(file);
|
|
110
|
+
|
|
111
|
+
if (compat.status === 'mismatch') {
|
|
112
|
+
// A corrupt file cannot be merged, so it is replaced; the panel only reads
|
|
113
|
+
// this file, and a broken status.json has no usable keys to preserve.
|
|
114
|
+
const status = state.corrupt || !state.value ? {} : state.value;
|
|
115
|
+
status.engineMismatch = mismatchRecord(compat, options.detection, options.now);
|
|
116
|
+
const written = writeStatus(file, status);
|
|
117
|
+
if (!written.ok) return { ok: false, action: 'write-failed', file, error: written.error };
|
|
118
|
+
return { ok: true, action: 'wrote', file };
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// status === 'ok': clear a stale record. An unreadable file is left alone —
|
|
122
|
+
// without parsing it we cannot tell whether it holds a mismatch.
|
|
123
|
+
if (!state.exists) return { ok: true, action: 'clean', file };
|
|
124
|
+
if (state.corrupt || !state.value) return { ok: true, action: 'unreadable', file };
|
|
125
|
+
if (!Object.prototype.hasOwnProperty.call(state.value, 'engineMismatch')) {
|
|
126
|
+
return { ok: true, action: 'clean', file };
|
|
127
|
+
}
|
|
128
|
+
delete state.value.engineMismatch;
|
|
129
|
+
if (Object.keys(state.value).length === 0) {
|
|
130
|
+
try {
|
|
131
|
+
fs.unlinkSync(file);
|
|
132
|
+
return { ok: true, action: 'cleared', file };
|
|
133
|
+
} catch (error) {
|
|
134
|
+
if (error?.code === 'ENOENT') return { ok: true, action: 'cleared', file };
|
|
135
|
+
return { ok: false, action: 'cleanup-failed', file, error };
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
const written = writeStatus(file, state.value);
|
|
139
|
+
if (!written.ok) return { ok: false, action: 'write-failed', file, error: written.error };
|
|
140
|
+
return { ok: true, action: 'cleared', file };
|
|
141
|
+
}
|