@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/index.js
ADDED
|
@@ -0,0 +1,647 @@
|
|
|
1
|
+
// @local/dsh-longlive — plugin entry
|
|
2
|
+
//
|
|
3
|
+
// Session longevity for DeepSeek Harness. Registers twenty-two tools:
|
|
4
|
+
// session_health / session_backup / session_truncate / session_truncate_visible /
|
|
5
|
+
// session_compact — dry-run by default, write nothing until asked with a confirm word;
|
|
6
|
+
// session_wake / lease_acquire / lease_release —
|
|
7
|
+
// the wake-up kit over the memory store (same resolver as memory_* below:
|
|
8
|
+
// memoryRoot / memRoot → $DSH_LONGLIVE_MEMORY_ROOT → legacy $DSH_MEM →
|
|
9
|
+
// <DSH home>/longlive-memory);
|
|
10
|
+
// memory_index / memory_consolidate / memory_reinforce / memory_forget /
|
|
11
|
+
// memory_procedure / memory_recall —
|
|
12
|
+
// the v0.4 memory layer, which only touches the configured memory root
|
|
13
|
+
// (since 0.7.3 an unset root derives to <DSH home>/longlive-memory;
|
|
14
|
+
// `strictMemoryRoot: true` restores the old "unset root is an error" mode);
|
|
15
|
+
// memory_recall (0.8.0) is read-only hybrid search over topics/ and journal/,
|
|
16
|
+
// and locked cards are immune to forget moves and consolidate merges;
|
|
17
|
+
// session_live_update / session_live_list / session_live_get /
|
|
18
|
+
// session_live_prune — the v0.5 "正在" live session registry under
|
|
19
|
+
// <memoryRoot>/.longlive/live, one atomic JSON row per session.
|
|
20
|
+
//
|
|
21
|
+
// 0.7.3 makes the package portable: no drive-letter default, one memory-root
|
|
22
|
+
// resolver for both families, a runtime engines.dsh check that warns and
|
|
23
|
+
// degrades instead of crashing, and platform notes in the docs.
|
|
24
|
+
//
|
|
25
|
+
// 0.7.1 fixes who counts as the user: carryFirstUserMessage and the archive
|
|
26
|
+
// card only accept a host-confirmed user-authored message (`source.kind` is
|
|
27
|
+
// "user" AND the client stamped `clientTimeZone`), never the surgical
|
|
28
|
+
// checkpoint or a runtime-context/skill-catalog block. It also adds automatic
|
|
29
|
+
// visible truncation at `turn/end` (`autoTruncate`: log / dryrun / request,
|
|
30
|
+
// default log, 300k tokens, 12 turns, 60-minute per-session cooldown). Request
|
|
31
|
+
// mode only writes `auto-truncate.request.json`; the session log is cut outside
|
|
32
|
+
// the app, after it exits (0.7.0's in-app apply corrupted a running session).
|
|
33
|
+
//
|
|
34
|
+
// 0.6.0 adds whole-turn visible truncation. On startup it also finishes or
|
|
35
|
+
// rolls back any `.truncate-visible-inflight.json` left by a crash.
|
|
36
|
+
//
|
|
37
|
+
// 0.5.1 adds auto-binding and auto-heartbeat:
|
|
38
|
+
// - identity comes from the tool execution's explicit `exec.agent` (route 1
|
|
39
|
+
// in identity.js), so `session_live_update` needs no sessionId argument;
|
|
40
|
+
// - a bound row is refreshed on `session/event` turn boundaries and by an
|
|
41
|
+
// unref'd `heartbeatMs` timer (default 15 s; 0 disables automatic writes).
|
|
42
|
+
// Startup writes no session log and no live row. It may create the derived
|
|
43
|
+
// default memory directory (<DSH home>/longlive-memory) so the memory tools
|
|
44
|
+
// work out of the box; an explicitly configured root is never created here.
|
|
45
|
+
|
|
46
|
+
import fs from 'node:fs';
|
|
47
|
+
import os from 'node:os';
|
|
48
|
+
import path from 'node:path';
|
|
49
|
+
import { registerTools, dshHome, TOOLS } from './tools.js';
|
|
50
|
+
import { resolveCurrentSessionId } from './identity.js';
|
|
51
|
+
import { createAutoHeartbeat, DEFAULT_HEARTBEAT_MS } from './heartbeat.js';
|
|
52
|
+
import { sweepInflight } from './truncate-visible.js';
|
|
53
|
+
import { checkSurgeryOnStart, receiptText } from './surgery-recovery.js';
|
|
54
|
+
import { createAutoTruncator } from './auto-truncate.js';
|
|
55
|
+
import { VERSION } from './version.js';
|
|
56
|
+
import { memoryRootSetting, defaultMemoryRoot } from './memory/paths.js';
|
|
57
|
+
import { configureAppendStore, flushAppendHealth } from './util/append-store.js';
|
|
58
|
+
import { cleanupStaleLocks } from './util/stale-locks.js';
|
|
59
|
+
import { checkHostCompatibility, declaredHostRange, detectHostVersion, warnOnce } from './host-version.js';
|
|
60
|
+
import { recordEngineStatus } from './engine-status.js';
|
|
61
|
+
import { configurePolicyProfile } from './policy-profile.js';
|
|
62
|
+
import { createRouterRuntime, createRouterService, createRouterTools, normalizeRouterConfig } from './router/runtime.js';
|
|
63
|
+
import { createRouterFace, normalizeFaceConfig } from './router/face/index.mjs';
|
|
64
|
+
import { registerFeatures, resolveActivationSwitches } from './features.js';
|
|
65
|
+
import { registerAnswerGuard } from './answer/index.js';
|
|
66
|
+
import { createAnswerTools } from './answer/query.js';
|
|
67
|
+
import { createConclusionBridge } from './conclusion.js';
|
|
68
|
+
import { checkOptionalDeps, formatMissingDepsWarning } from './deps.js';
|
|
69
|
+
import { scheduleFilterRun } from './memory/filter-runner.js';
|
|
70
|
+
import { registerMetrics } from './metrics/index.mjs';
|
|
71
|
+
import { registerToolOutputTrim } from './tool-output.js';
|
|
72
|
+
import { registerD6 } from './d6/runtime.js';
|
|
73
|
+
import { registerHostLifecycle } from './lifecycle/index.js';
|
|
74
|
+
import { registerTurnCapture } from './memory/capture-host.js';
|
|
75
|
+
import { registerAutoProcedure } from './autoprocedure/index.js';
|
|
76
|
+
import { registerPanelApiRoutes } from './ui/panel-api.js';
|
|
77
|
+
import { registerUiRoutes } from './ui/routes.js';
|
|
78
|
+
|
|
79
|
+
export const name = '@jjh32456/longlive';
|
|
80
|
+
export const inject = ['tools'];
|
|
81
|
+
/** The engines.dsh range this build was tested against (also in package.json). */
|
|
82
|
+
export const HOST_RANGE = declaredHostRange();
|
|
83
|
+
// Host-visible schema (optional: undefined when the schemastery peer is absent).
|
|
84
|
+
// Declaring it lets the Cordis loader validate a row's config at activation;
|
|
85
|
+
// see src/config.js for why the advanced router keys stay passthrough.
|
|
86
|
+
export { Config } from './config.js';
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Resolve the memory root once, at load time, for the pieces that need it
|
|
90
|
+
* (heartbeat, auto-truncation, the live registry). Never throws: a bad or
|
|
91
|
+
* missing explicitly configured root is a warning here and a coded error from
|
|
92
|
+
* the tool that actually needs it — loading the plugin must not crash.
|
|
93
|
+
*/
|
|
94
|
+
function startupMemoryRoot(ctx, config, env = process.env) {
|
|
95
|
+
const setting = memoryRootSetting(config, env);
|
|
96
|
+
if (!setting.explicit) {
|
|
97
|
+
if (setting.strict) {
|
|
98
|
+
warnOnce(ctx, 'memoryRoot 未配置且 strictMemoryRoot 已开启 — memory_* 工具会返回 ' +
|
|
99
|
+
'E_MEMORY_ROOT_UNSET;设置 memoryRoot 或 DSH_LONGLIVE_MEMORY_ROOT 后重启宿主。');
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
const derived = defaultMemoryRoot(config, env);
|
|
103
|
+
try {
|
|
104
|
+
fs.mkdirSync(derived, { recursive: true });
|
|
105
|
+
return derived;
|
|
106
|
+
} catch (error) {
|
|
107
|
+
warnOnce(ctx, `无法创建默认记忆根目录 ${derived}(${error?.code ?? error?.message})— ` +
|
|
108
|
+
'memory_* / live_* / autoTruncate 会降级;请检查写权限或显式设置 memoryRoot。');
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
const root = path.resolve(setting.explicit);
|
|
113
|
+
let ok = false;
|
|
114
|
+
try {
|
|
115
|
+
ok = fs.statSync(root).isDirectory();
|
|
116
|
+
} catch {
|
|
117
|
+
ok = false;
|
|
118
|
+
}
|
|
119
|
+
if (!ok) {
|
|
120
|
+
warnOnce(ctx, `记忆根目录 ${root}(来源 ${setting.source})不存在或不是目录 — ` +
|
|
121
|
+
'需要它的工具会返回 E_MEMORY_ROOT_MISSING;先创建该目录,或修正 memoryRoot。');
|
|
122
|
+
}
|
|
123
|
+
return root;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
let lastFeatureActivation = [];
|
|
127
|
+
|
|
128
|
+
/** 任务 activation evidence: last registerFeatures enabled list (host/probe/read-only). */
|
|
129
|
+
export function featureActivation() {
|
|
130
|
+
return [...lastFeatureActivation];
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export function apply(ctx, config = {}) {
|
|
134
|
+
const home = config.home ?? dshHome();
|
|
135
|
+
|
|
136
|
+
// 0.8.0-rc.10: optional peers are advisory. A profile without an external
|
|
137
|
+
// memory plugin loses only its optional tools; the twenty-two longlive tools and truncation
|
|
138
|
+
// must keep working. This check is read-only, never throws, and never blocks
|
|
139
|
+
// activation. The one line points at the --setup wizard and says it asks
|
|
140
|
+
// first; startup itself never installs anything.
|
|
141
|
+
try {
|
|
142
|
+
const optional = checkOptionalDeps({
|
|
143
|
+
home,
|
|
144
|
+
env: config.env ?? process.env,
|
|
145
|
+
profile: config.profile,
|
|
146
|
+
});
|
|
147
|
+
const warning = formatMissingDepsWarning(optional.missing);
|
|
148
|
+
if (warning) warnOnce(ctx, warning);
|
|
149
|
+
} catch (error) {
|
|
150
|
+
// An optional-dependency check must never be the reason a plugin fails to load.
|
|
151
|
+
warnOnce(ctx, `可选依赖自检失败(${error?.message ?? error})— 跳过自检,插件继续加载。`);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// engines.dsh is a declaration, not an enforcement: check it here, warn once,
|
|
155
|
+
// keep loading. An undetectable host version is not a mismatch. Detection is
|
|
156
|
+
// advisory, so even an unexpected proxy error must not fail activation: a
|
|
157
|
+
// host that does not expose a version is a normal embedding, not a broken
|
|
158
|
+
// installation (0.7.3 lost all 19 tools to one unguarded ctx.version read).
|
|
159
|
+
let detection = { version: null, source: null };
|
|
160
|
+
try {
|
|
161
|
+
detection = detectHostVersion(ctx, config, config?.env ?? process.env);
|
|
162
|
+
} catch (error) {
|
|
163
|
+
warnOnce(ctx, `宿主版本探测失败(${error?.message ?? error})— 跳过兼容性检查,插件继续加载。`);
|
|
164
|
+
}
|
|
165
|
+
const compat = checkHostCompatibility({
|
|
166
|
+
version: detection.version,
|
|
167
|
+
range: HOST_RANGE,
|
|
168
|
+
pluginVersion: VERSION,
|
|
169
|
+
});
|
|
170
|
+
const detectionOrigin = detection.path
|
|
171
|
+
? `${detection.source ?? 'unknown'} (${detection.path})`
|
|
172
|
+
: (detection.source ?? 'unknown');
|
|
173
|
+
if (compat.warning) warnOnce(ctx, `${compat.warning}(检测来源:${detectionOrigin})`);
|
|
174
|
+
|
|
175
|
+
const memoryRoot = startupMemoryRoot(ctx, { ...config, home });
|
|
176
|
+
|
|
177
|
+
// S1(rc.7 整合线):统一 append 层 + 陈旧锁清理。两件都是非致命:写不进去
|
|
178
|
+
// 只降级并告警,绝不让插件加载失败。
|
|
179
|
+
try {
|
|
180
|
+
configureAppendStore({ logger: { warn: (message) => warnOnce(ctx, message) }, now: config.now });
|
|
181
|
+
} catch (error) {
|
|
182
|
+
warnOnce(ctx, `append-store 初始化失败(${error?.message ?? error})— 追加失败将只走 console.warn。`);
|
|
183
|
+
}
|
|
184
|
+
if (memoryRoot) {
|
|
185
|
+
try {
|
|
186
|
+
const lockReport = cleanupStaleLocks({
|
|
187
|
+
memoryRoot,
|
|
188
|
+
now: config.now,
|
|
189
|
+
logger: { warn: (message) => warnOnce(ctx, message) },
|
|
190
|
+
audit: config.audit?.cleanStaleLocks !== false,
|
|
191
|
+
});
|
|
192
|
+
if (lockReport.removed.length > 0) {
|
|
193
|
+
warnOnce(ctx, `清理陈旧锁 ${lockReport.removed.length} 个(${lockReport.removed.map((item) => item.file).slice(0, 3).join(', ')}…)`);
|
|
194
|
+
}
|
|
195
|
+
try { flushAppendHealth(memoryRoot, { now: config.now }); } catch { /* health snapshot is best effort */ }
|
|
196
|
+
} catch (error) {
|
|
197
|
+
warnOnce(ctx, `陈旧锁清理失败(${error?.message ?? error})— 不影响加载。`);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// C1 (1.4.0): persist the verdict for the panel. Data only — the panel may
|
|
202
|
+
// render a red dot from `engineMismatch` and does not need host logs. A
|
|
203
|
+
// matching host clears a stale record. A missing/read-only memoryRoot, a
|
|
204
|
+
// corrupt status file or a race is not a load failure: recordEngineStatus
|
|
205
|
+
// never throws, and this extra guard keeps even an unforeseen bug advisory.
|
|
206
|
+
try {
|
|
207
|
+
recordEngineStatus(memoryRoot, { compat, detection, now: config.now });
|
|
208
|
+
} catch (error) {
|
|
209
|
+
warnOnce(ctx, `engines.dsh 自检状态写入失败(${error?.message ?? error})— 不影响加载。`);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// R1 路由策略:默认 generic(通用原则);宿主可配 routerPolicyProfile 指向
|
|
213
|
+
// src/router/policies/<name>.json 叠加家规(只许收窄,不许放宽 fail-closed)。
|
|
214
|
+
// 未知/损坏 profile 不抛:这里只警告,route() 自己会 fail-closed。
|
|
215
|
+
const policyProfile = configurePolicyProfile(config.routerPolicyProfile);
|
|
216
|
+
if (!policyProfile.ok) {
|
|
217
|
+
warnOnce(ctx, `routerPolicyProfile=${config.routerPolicyProfile} 不可用(${policyProfile.code}):` +
|
|
218
|
+
`${policyProfile.message} — 路由将 fail-closed。`);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// 0.7.1: automatic visible truncation. The truncator owns no listener; it is
|
|
222
|
+
// invoked by the heartbeat's `session/event` feed at `turn/end` (one
|
|
223
|
+
// evaluation per session per cooldown).
|
|
224
|
+
const autoTruncator = createAutoTruncator({
|
|
225
|
+
memoryRoot,
|
|
226
|
+
home,
|
|
227
|
+
autoTruncate: config.autoTruncate,
|
|
228
|
+
fold: config.fold,
|
|
229
|
+
truncate: config.truncate,
|
|
230
|
+
now: config.now,
|
|
231
|
+
logger: ctx?.logger,
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
// R1 运行时适配器:1.6.0 安全默认改为 off(abuse assessment §4.1 / §4.5)。
|
|
235
|
+
// 旧版默认 shadow 会把每次工具调用的参数摘要写进
|
|
236
|
+
// <memoryRoot>/.longlive/router-runtime.jsonl;现在只有显式
|
|
237
|
+
// routerRuntime='shadow'|'on' 才启用,shadowEnabled 仅作为旧配置的兼容位。
|
|
238
|
+
// shadow 只判定+记录;on 仅对高风险写操作拒绝,只读调用永不拒绝(DESIGN.md §5)。
|
|
239
|
+
// 构造失败不阻断插件加载:没有适配器就等于 off。
|
|
240
|
+
const runtimeMode = config.routerRuntime ?? 'off';
|
|
241
|
+
let routerRuntime = null;
|
|
242
|
+
try {
|
|
243
|
+
routerRuntime = createRouterRuntime({
|
|
244
|
+
mode: runtimeMode,
|
|
245
|
+
statePath: config.routerRuntimeStatePath,
|
|
246
|
+
memoryRoot,
|
|
247
|
+
maxRecords: config.routerRuntimeMaxRecords,
|
|
248
|
+
// 1.2.0 影子页签要看全部工具调用,而不只是 memory_* 四个。
|
|
249
|
+
recordAllTools: true,
|
|
250
|
+
// Guard only takes the names this plugin does not register (external memory tools).
|
|
251
|
+
ownTools: TOOLS.map((tool) => tool.name),
|
|
252
|
+
onError: () => warnOnce(ctx, `R1 记录写入失败(${config.routerRuntimeStatePath ?? '.longlive/router-runtime.jsonl'})— ` +
|
|
253
|
+
'判定照常,工具行为不受影响。'),
|
|
254
|
+
});
|
|
255
|
+
if (routerRuntime.modeInvalid) {
|
|
256
|
+
warnOnce(ctx, `routerRuntime=${JSON.stringify(config.routerRuntime)} 不是 off/shadow/on — 已按 off 处理。`);
|
|
257
|
+
}
|
|
258
|
+
if (routerRuntime.enabled && !routerRuntime.statePath) {
|
|
259
|
+
warnOnce(ctx, `routerRuntime=${routerRuntime.mode} 但没有可写的记忆根目录 — 判定会跑,记录会被丢弃。`);
|
|
260
|
+
}
|
|
261
|
+
} catch (error) {
|
|
262
|
+
warnOnce(ctx, `R1 运行时适配器创建失败(${error?.message ?? error})— 影子模式不可用,插件继续加载。`);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
const heartbeat = createAutoHeartbeat({
|
|
266
|
+
ctx,
|
|
267
|
+
memoryRoot,
|
|
268
|
+
// undefined -> 15 s; 0 (or invalid) -> no automatic writes at all.
|
|
269
|
+
heartbeatMs: config.heartbeatMs ?? DEFAULT_HEARTBEAT_MS,
|
|
270
|
+
now: config.now,
|
|
271
|
+
touch: config.touchLiveSession,
|
|
272
|
+
onTurnEnd: autoTruncator.onTurnEnd,
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
// 1.1.0 RouterService(DESIGN §2.1)。默认关闭:routerEnabled=false 时既没有
|
|
276
|
+
// service 对象,也没有定时器/监听器/目录/日志,注册表保持 22 个工具。
|
|
277
|
+
// 构造只保存配置;start() 才碰文件系统。启动失败不阻断插件加载。
|
|
278
|
+
let routerService = null;
|
|
279
|
+
let routerTools = [];
|
|
280
|
+
const routerConfig = normalizeRouterConfig({ ...config, memoryRoot });
|
|
281
|
+
for (const warning of routerConfig.warnings) {
|
|
282
|
+
warnOnce(ctx, `router 配置无效(${warning})— 已回退默认值。`);
|
|
283
|
+
}
|
|
284
|
+
// Test hooks are a build seam, not a configuration switch. A production
|
|
285
|
+
// build refuses them outright so they can never be enabled from config.
|
|
286
|
+
if (config?.routerTestHooks !== undefined && process.env.LONGLIVE_ROUTER_TEST !== '1') {
|
|
287
|
+
throw new Error('longlive router: routerTestHooks 只在测试构建可用(LONGLIVE_ROUTER_TEST=1)');
|
|
288
|
+
}
|
|
289
|
+
const testHooks = process.env.LONGLIVE_ROUTER_TEST === '1' ? config?.routerTestHooks : undefined;
|
|
290
|
+
if (routerConfig.enabled) {
|
|
291
|
+
try {
|
|
292
|
+
routerService = createRouterService(ctx, routerConfig,
|
|
293
|
+
testHooks ? { memoryRoot, testHooks } : { memoryRoot });
|
|
294
|
+
routerTools = createRouterTools(routerService);
|
|
295
|
+
if (typeof ctx?.effect === 'function') {
|
|
296
|
+
try {
|
|
297
|
+
ctx.effect(() => () => routerService.dispose(), 'longlive.router');
|
|
298
|
+
} catch {
|
|
299
|
+
/* no effect scope in a fake context */
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
Promise.resolve(routerService.start()).catch((error) => {
|
|
303
|
+
Promise.resolve(routerService.block(error?.code ?? 'E_START_FAILED', error)).catch(() => {});
|
|
304
|
+
});
|
|
305
|
+
} catch (error) {
|
|
306
|
+
routerService = null;
|
|
307
|
+
warnOnce(ctx, `router 启动失败(${error?.message ?? error})— 已按关闭处理,插件继续加载。`);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
// 1.3.1+ routerFace(v1 后台面)。默认关闭;关闭时零定时器、零目录、零 IO。
|
|
312
|
+
// 开启后它只读共享记忆、只写 journal 与 bridge outbox,不注册 turn 监听、
|
|
313
|
+
// 不执行任何工具;任何失败只落 status,绝不影响前台 turn。
|
|
314
|
+
let routerFace = null;
|
|
315
|
+
const faceConfig = normalizeFaceConfig(config.routerFace, { memoryRoot });
|
|
316
|
+
for (const warning of faceConfig.warnings) {
|
|
317
|
+
warnOnce(ctx, `routerFace 配置无效(${warning})— 已回退默认值。`);
|
|
318
|
+
}
|
|
319
|
+
if (faceConfig.enabled && memoryRoot) {
|
|
320
|
+
try {
|
|
321
|
+
routerFace = createRouterFace({ ctx, memoryRoot, config: faceConfig });
|
|
322
|
+
if (typeof ctx?.effect === 'function') {
|
|
323
|
+
try {
|
|
324
|
+
ctx.effect(() => () => routerFace.dispose(), 'longlive.routerFace');
|
|
325
|
+
} catch {
|
|
326
|
+
/* no effect scope in a fake context */
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
Promise.resolve(routerFace.start()).catch((error) => {
|
|
330
|
+
warnOnce(ctx, `routerFace 启动失败(${error?.message ?? error})— 已跳过,插件继续加载。`);
|
|
331
|
+
});
|
|
332
|
+
} catch (error) {
|
|
333
|
+
routerFace = null;
|
|
334
|
+
warnOnce(ctx, `routerFace 初始化失败(${error?.message ?? error})— 已跳过,插件继续加载。`);
|
|
335
|
+
}
|
|
336
|
+
} else if (faceConfig.enabled && !memoryRoot) {
|
|
337
|
+
warnOnce(ctx, 'routerFace 已启用但没有 memoryRoot — 已跳过。');
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
// D6 (opt-in, default off): trim the model-facing tool surface on the first
|
|
341
|
+
// claimed user message, before preStep calls tools.resolve. Registering this
|
|
342
|
+
// before registerTools matters: tool_search must be part of the registry the
|
|
343
|
+
// host sees on that very first resolve, and must not be added later.
|
|
344
|
+
let d6 = { enabled: false, tools: [] };
|
|
345
|
+
if (config?.d6?.enabled === true) {
|
|
346
|
+
try {
|
|
347
|
+
d6 = registerD6(ctx, config);
|
|
348
|
+
} catch (error) {
|
|
349
|
+
warnOnce(ctx, `D6 注册失败(${error?.message ?? error})— 已跳过,工具面保持全量。`);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
// 1.8.0 activation switches (任务 A1): default ON via Config schema; explicit
|
|
354
|
+
// false or DSH_LONGLIVE_DISABLE_* are independent kill switches.
|
|
355
|
+
const activationSwitches = resolveActivationSwitches(config, {}, process.env);
|
|
356
|
+
const answerTools = activationSwitches.answerGuardEnabled && memoryRoot
|
|
357
|
+
? createAnswerTools(memoryRoot)
|
|
358
|
+
: [];
|
|
359
|
+
|
|
360
|
+
const registered = registerTools(ctx, {
|
|
361
|
+
keepEvents: config.keepEvents ?? 20000,
|
|
362
|
+
recentWriteWindowMs: config.recentWriteWindowMs ?? 60000,
|
|
363
|
+
// Optional overrides (tests pass them explicitly; the host normally omits them).
|
|
364
|
+
home,
|
|
365
|
+
// Test seam: a suite can supply a fake defineTool and drive the real
|
|
366
|
+
// registration path without the @deepseek-ai/dsh-tools peer installed.
|
|
367
|
+
defineTool: config.defineTool,
|
|
368
|
+
// One memory-store root for both families. `memRoot` remains an accepted
|
|
369
|
+
// alias so an existing configuration keeps pointing at the same store.
|
|
370
|
+
memRoot: memoryRoot,
|
|
371
|
+
memoryRoot,
|
|
372
|
+
// Resolve "this session" from the explicit caller Agent the host puts on
|
|
373
|
+
// every tool execution (`exec.agent`), falling back to the agents service
|
|
374
|
+
// and finally to legacy ctx guesses / DSH_SESSION_ID. `null` keeps the
|
|
375
|
+
// existing explicit errors (`无法确定当前会话 id ...` / SESSION_NOT_FOUND).
|
|
376
|
+
currentSessionId: (exec) => resolveCurrentSessionId({ ctx, exec }).id,
|
|
377
|
+
// Every resolver path that had to fall back to "current" binds the id, so
|
|
378
|
+
// turn events and the fallback timer can refresh exactly that row and no
|
|
379
|
+
// other. Best effort by contract (see resolveSessionId in live.js).
|
|
380
|
+
onAutoSessionId: (id) => heartbeat.bind(id),
|
|
381
|
+
// Consumed by the tool wrapper: refresh on every longlive tool call.
|
|
382
|
+
autoHeartbeat: heartbeat,
|
|
383
|
+
// Consumed by the tool wrapper (intercept) and, when the host exposes
|
|
384
|
+
// tools.guard, by the pre-execute hook for tools registered elsewhere.
|
|
385
|
+
routerRuntime,
|
|
386
|
+
// Only present when routerEnabled=true; registerTools appends the four
|
|
387
|
+
// router_* tools from it and otherwise registers exactly the 0.9.0 set.
|
|
388
|
+
routerTools,
|
|
389
|
+
// Only present when config.d6.enabled=true; appended after the normal set
|
|
390
|
+
// so the frozen allowlist can name it without changing the baseline order.
|
|
391
|
+
d6Tools: d6.tools,
|
|
392
|
+
// 1.8.0 answer-guard: read-only answer_audit tool (appended after the baseline 22).
|
|
393
|
+
answerTools,
|
|
394
|
+
// 1.6.0 safety default: worker_spawn/worker_status/worker_kill are visible
|
|
395
|
+
// in the baseline tool table but return "unavailable" until this is true.
|
|
396
|
+
workers: config.workers,
|
|
397
|
+
// 2026-11-15 fix: session_grep 的 filter 模式读 config.tiering;这里不把手
|
|
398
|
+
// 工对象补上 raw row 的 tiering,normalizeTiering 就永远拿默认 shadow,
|
|
399
|
+
// tiering.mode:'filter' 静默失效。1.8.5 修。
|
|
400
|
+
tiering: config.tiering,
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
heartbeat.start();
|
|
404
|
+
|
|
405
|
+
// 1.2.0 记忆准入过滤器:启动后异步跑一次(unref 定时器,不阻塞加载),
|
|
406
|
+
// 只读 journal/*.md 与卡片目录,只写 <memoryRoot>/.longlive/filter/。
|
|
407
|
+
// 任何异常都只是 warning;filterEnabled=false 时连定时器都不创建。
|
|
408
|
+
if (config.filterEnabled !== false && memoryRoot) {
|
|
409
|
+
try {
|
|
410
|
+
scheduleFilterRun(memoryRoot, {
|
|
411
|
+
logger: (message) => warnOnce(ctx, message),
|
|
412
|
+
now: config.now,
|
|
413
|
+
});
|
|
414
|
+
} catch (error) {
|
|
415
|
+
warnOnce(ctx, `memory filter 启动失败(${error?.message ?? error})— 已跳过,不影响工具。`);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
// A crash between tmp-write and rename leaves a record. Startup is the one
|
|
420
|
+
// moment nothing is writing, so finish or roll it back before any tool runs.
|
|
421
|
+
if (config.recoverOnStart !== false) {
|
|
422
|
+
try {
|
|
423
|
+
sweepInflight(home);
|
|
424
|
+
} catch {
|
|
425
|
+
/* recovery is best effort; the tool still reports any record it finds */
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
// 0.9.0-rc.2: unattended-surgery post-mortem. When no stage file exists this
|
|
430
|
+
// is one `existsSync` and nothing else — no read, no log, no hook. A finished
|
|
431
|
+
// (`phase: "done"`) receipt is skipped too. The check itself is read-only for
|
|
432
|
+
// the session; at most it writes one report plus one audit line. `config.
|
|
433
|
+
// surgeryRecovery: false` turns it off for a frozen deployment.
|
|
434
|
+
//
|
|
435
|
+
// When the launcher could not write its visible receipt (failed or crashed
|
|
436
|
+
// run, or an older state file), carry the same text into the first turn so
|
|
437
|
+
// the operator still sees the verdict in the conversation body.
|
|
438
|
+
let surgeryReceipt = null;
|
|
439
|
+
if (config.surgeryRecovery !== false && memoryRoot) {
|
|
440
|
+
try {
|
|
441
|
+
const surgery = checkSurgeryOnStart({
|
|
442
|
+
memoryRoot,
|
|
443
|
+
loadedVersion: VERSION,
|
|
444
|
+
loadedHostVersion: detection.version,
|
|
445
|
+
});
|
|
446
|
+
if (surgery.checked && surgery.report) {
|
|
447
|
+
const written = surgery.report.checks?.receiptWritten === true;
|
|
448
|
+
if (!written) {
|
|
449
|
+
surgeryReceipt = receiptText(surgery.report, { appendingReceipt: false });
|
|
450
|
+
}
|
|
451
|
+
if (surgery.report.classification === 'needs-attention') {
|
|
452
|
+
warnOnce(ctx, `上次无人值守手术需要人工核对(${surgery.report.reasonCode}):` +
|
|
453
|
+
`${surgery.report.recommendation}`);
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
} catch (error) {
|
|
457
|
+
// A post-mortem is evidence, never a reason the plugin fails to load.
|
|
458
|
+
warnOnce(ctx, `手术恢复自检失败(${error?.message ?? error})— 已跳过,不影响插件加载。`);
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
if (typeof ctx?.effect === 'function') {
|
|
462
|
+
try {
|
|
463
|
+
// Plugin lifetime owns the timer and the session/event listener; Cordis
|
|
464
|
+
// runs this disposer when the plugin is unloaded.
|
|
465
|
+
ctx.effect(() => () => heartbeat.stop(), 'longlive.auto-heartbeat');
|
|
466
|
+
} catch {
|
|
467
|
+
/* no effect scope in a test/fake context: process lifetime is fine */
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
// A1 (opt-in, default off): conclusion write-back. At every turn/end with
|
|
471
|
+
// reason=completed the bridge checks the turn's last real user message plus
|
|
472
|
+
// the assistant's final text blocks with local trigger rules only — no model
|
|
473
|
+
// call — and writes one card under topics/ plus a session marker under
|
|
474
|
+
// .longlive/bridge/ (also the source A2's board reads). Failures are silent
|
|
475
|
+
// by construction and never block the turn.
|
|
476
|
+
const conclusionBridge = createConclusionBridge({
|
|
477
|
+
enabled: config.autoConclusion !== false,
|
|
478
|
+
memoryRoot,
|
|
479
|
+
now: config.now,
|
|
480
|
+
});
|
|
481
|
+
if (conclusionBridge.enabled) {
|
|
482
|
+
let disposeConclusion = null;
|
|
483
|
+
try {
|
|
484
|
+
disposeConclusion = ctx.on('session/event', (session, event) => {
|
|
485
|
+
conclusionBridge.onSessionEvent(session, event);
|
|
486
|
+
});
|
|
487
|
+
} catch (error) {
|
|
488
|
+
warnOnce(ctx, `结论回流监听注册失败(${error?.message ?? error})— 已跳过。`);
|
|
489
|
+
}
|
|
490
|
+
if (typeof disposeConclusion === 'function' && typeof ctx?.effect === 'function') {
|
|
491
|
+
try {
|
|
492
|
+
ctx.effect(() => () => disposeConclusion(), 'longlive.conclusion');
|
|
493
|
+
} catch {
|
|
494
|
+
/* fake context without an effect scope */
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
// 0.9.0-rc.1 opt-in features. Every switch defaults to off, in which case
|
|
499
|
+
// this call registers nothing, reads nothing and logs nothing.
|
|
500
|
+
const featuresResult = registerFeatures(ctx, config, {
|
|
501
|
+
memoryRoot,
|
|
502
|
+
surgeryReceipt,
|
|
503
|
+
sessionsRoot: path.join(home, 'sessions'),
|
|
504
|
+
});
|
|
505
|
+
|
|
506
|
+
// 1.8.6 autoProcedure(默认关闭):任务结束后异步做本地规则蒸馏,候选只写
|
|
507
|
+
// staging,系统永不自动转正。enabled:false 时这里不注册 hook、不建目录、不
|
|
508
|
+
// 读盘;开启但 memoryRoot 未配置时 warn + 不注册(F11)。失败只告警,绝不让
|
|
509
|
+
// 插件加载失败。
|
|
510
|
+
try {
|
|
511
|
+
const autoProcedure = registerAutoProcedure(ctx, config, {
|
|
512
|
+
memoryRoot,
|
|
513
|
+
home,
|
|
514
|
+
now: config.now ?? (() => new Date()),
|
|
515
|
+
logger: { warn: (message) => warnOnce(ctx, message) },
|
|
516
|
+
});
|
|
517
|
+
featuresResult.autoProcedure = {
|
|
518
|
+
enabled: autoProcedure.enabled,
|
|
519
|
+
registered: autoProcedure.registered,
|
|
520
|
+
hooks: autoProcedure.hooks ?? [],
|
|
521
|
+
...(autoProcedure.reason ? { reason: autoProcedure.reason } : {}),
|
|
522
|
+
};
|
|
523
|
+
} catch (error) {
|
|
524
|
+
warnOnce(ctx, '[autoprocedure] 注册失败(' + (error?.message ?? error) + ')— 已跳过,插件继续加载。');
|
|
525
|
+
featuresResult.autoProcedure = { enabled: false, registered: false, reason: 'E_AP_REGISTER' };
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
// 1.8.0 answer guard (ON by default): registers the outermost pre-step evidence
|
|
529
|
+
// injection, the system-prompt rule section, the session/event audit listener and
|
|
530
|
+
// the read-only panel route. Disabled by config.answerGuard.enabled === false or
|
|
531
|
+
// DSH_LONGLIVE_DISABLE_ANSWER_GUARD=1 — then zero hooks and zero IO.
|
|
532
|
+
try {
|
|
533
|
+
if (activationSwitches.answerGuardEnabled) {
|
|
534
|
+
const answerGuard = registerAnswerGuard(ctx, {
|
|
535
|
+
...config,
|
|
536
|
+
answerGuard: { ...(config?.answerGuard ?? {}), enabled: true },
|
|
537
|
+
}, {
|
|
538
|
+
memoryRoot,
|
|
539
|
+
home,
|
|
540
|
+
now: config.now,
|
|
541
|
+
knownTools: TOOLS.map((tool) => tool.name),
|
|
542
|
+
});
|
|
543
|
+
featuresResult.answerGuard = {
|
|
544
|
+
enabled: answerGuard.enabled,
|
|
545
|
+
systemPrompt: answerGuard.systemPrompt,
|
|
546
|
+
auditPath: answerGuard.auditPath,
|
|
547
|
+
hooks: answerGuard.hooks,
|
|
548
|
+
};
|
|
549
|
+
} else {
|
|
550
|
+
featuresResult.answerGuard = { enabled: false, reason: 'kill-switch' };
|
|
551
|
+
}
|
|
552
|
+
} catch (error) {
|
|
553
|
+
warnOnce(ctx, '[answer-guard] 注册失败(' + (error?.message ?? error) + ')— 已跳过,插件继续加载。');
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
// D1/D4: measure-only working-set ledger. Never trims, never throws; a
|
|
557
|
+
// missing memoryRoot simply disables the recorder.
|
|
558
|
+
registerMetrics(ctx, config, { memoryRoot });
|
|
559
|
+
|
|
560
|
+
// D2 (default on; literal `d2.enabled:false` is the kill switch): trim oversized tool results at the host's
|
|
561
|
+
// `tools/post-execute` waterfall and archive the full text under
|
|
562
|
+
// `<memoryRoot>/.longlive/tool-output/<turn>/<id>.txt` — the same path D3's
|
|
563
|
+
// session_original plans. Registration failure or a missing root is a
|
|
564
|
+
// warning, never a load failure; an enabled-but-broken trim stays fail-open
|
|
565
|
+
// inside the listener.
|
|
566
|
+
try {
|
|
567
|
+
registerToolOutputTrim(ctx, config, { memoryRoot });
|
|
568
|
+
} catch (error) {
|
|
569
|
+
warnOnce(ctx, `工具输出裁剪注册失败(${error?.message ?? error})— 已跳过,工具结果保持原文。`);
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
// Settings panel: the browser companion registers `plugins.row.config` and
|
|
573
|
+
// calls this same-origin route. The route writes through the Host's
|
|
574
|
+
// `configEditor`, so a switch lands in the profile patch and the ordinary
|
|
575
|
+
// Loader reconciliation reloads this entry with the new value. Profiles
|
|
576
|
+
// without a Web server simply get no route; activation never depends on it.
|
|
577
|
+
try {
|
|
578
|
+
registerUiRoutes(ctx, {
|
|
579
|
+
config,
|
|
580
|
+
memoryRoot,
|
|
581
|
+
rowId: 'session-longlive',
|
|
582
|
+
// EA-08: memoryV2 registers cap-gated #1 on the same path; never double-register.
|
|
583
|
+
skipFlags: config?.memoryV2?.enabled === true,
|
|
584
|
+
});
|
|
585
|
+
} catch (error) {
|
|
586
|
+
warnOnce(ctx, `设置面板接口注册失败(${error?.message ?? error})— 面板将显示不可用,插件继续加载。`);
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
// 1.6.9 memory/recall/forget v2 panel API (M2-F 16 read-only routes + M2-G
|
|
590
|
+
// 16 actions). Default off: when `memoryV2.enabled` is false nothing is
|
|
591
|
+
// registered and no v2 file is created, so the plugin behaves like 1.6.8.
|
|
592
|
+
if (config?.memoryV2?.enabled === true) {
|
|
593
|
+
try {
|
|
594
|
+
registerPanelApiRoutes(ctx, {
|
|
595
|
+
config,
|
|
596
|
+
memoryRoot,
|
|
597
|
+
rowId: 'session-longlive',
|
|
598
|
+
// P0-1: host-injected capability secret (profile-private env); absent => panel API fails closed.
|
|
599
|
+
hostSecret: process.env.DSH_LONGLIVE_PANEL_SECRET,
|
|
600
|
+
});
|
|
601
|
+
} catch (error) {
|
|
602
|
+
warnOnce(ctx, `面板 v2 API 注册失败(${error?.message ?? error})— 插件继续加载。`);
|
|
603
|
+
}
|
|
604
|
+
// M2-B: host lifecycle + dual gap clocks (host-gap / memory-gap recorded as
|
|
605
|
+
// two separate first-class memories; journal backfill is idempotent).
|
|
606
|
+
try {
|
|
607
|
+
const dshHome = typeof process.env.DSH_HOME === 'string' && process.env.DSH_HOME.trim() !== ''
|
|
608
|
+
? path.resolve(process.env.DSH_HOME.trim())
|
|
609
|
+
: path.join(os.homedir(), '.dsh');
|
|
610
|
+
const lifecycle = registerHostLifecycle(ctx, {
|
|
611
|
+
memoryRoot,
|
|
612
|
+
sessionRoots: [path.join(dshHome, 'sessions')],
|
|
613
|
+
registerExitHook: true,
|
|
614
|
+
hostVersion: ctx?.hostVersion,
|
|
615
|
+
pluginVersion: VERSION,
|
|
616
|
+
});
|
|
617
|
+
if (!lifecycle.ok) warnOnce(ctx, `宿主生命周期入账失败(${lifecycle.code})— 插件继续加载。`);
|
|
618
|
+
} catch (error) {
|
|
619
|
+
warnOnce(ctx, `宿主生命周期入账失败(${error?.message ?? error})— 插件继续加载。`);
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
// 1.8.1-fix(2026-11-09,复核复核后修订):逐轮采集接线。1.8.1 final 的
|
|
624
|
+
// captureTurn 没有任何 turn/end 触发点,事件索引只靠脚本回填——真机实测
|
|
625
|
+
// 11-08 17:57 后无新增。这里补上,且**独立于 memoryV2 开关**(复核 P0-1:
|
|
626
|
+
// memoryV2 是面板 API 的 kill switch,不应控制采集);异步、不阻塞宿主,
|
|
627
|
+
// 空记忆根会后台自举索引。失败只告警,不影响宿主。
|
|
628
|
+
try {
|
|
629
|
+
const captureHome = typeof process.env.DSH_HOME === 'string' && process.env.DSH_HOME.trim() !== ''
|
|
630
|
+
? path.resolve(process.env.DSH_HOME.trim())
|
|
631
|
+
: path.join(os.homedir(), '.dsh');
|
|
632
|
+
const captureWiring = registerTurnCapture(ctx, {
|
|
633
|
+
memoryRoot,
|
|
634
|
+
sessionRoots: [path.join(captureHome, 'sessions')],
|
|
635
|
+
warn: (message) => warnOnce(ctx, message),
|
|
636
|
+
now: config.now,
|
|
637
|
+
});
|
|
638
|
+
if (!captureWiring.ok) {
|
|
639
|
+
warnOnce(ctx, `逐轮采集接线不可用(${captureWiring.code})— 事件索引将依赖回填脚本。`);
|
|
640
|
+
}
|
|
641
|
+
} catch (error) {
|
|
642
|
+
warnOnce(ctx, `逐轮采集接线失败(${error?.message ?? error})— 插件继续加载。`);
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
lastFeatureActivation = featuresResult && Array.isArray(featuresResult.enabled) ? featuresResult.enabled : [];
|
|
646
|
+
return registered;
|
|
647
|
+
}
|