diff --git a/lib/core.js b/lib/core.js index cb6011b..0fed91a 100644 --- a/lib/core.js +++ b/lib/core.js @@ -1451,6 +1451,13 @@ export function isDshHomeDir(dir) { return base === ".dsh" || base === "dsh-home"; } +function homeHasHarnessData(dir) { + if (!dir || !isDir(dir)) return false; + return isFile(path.join(dir, "prompt-inject.md")) + || isDir(path.join(dir, "profiles")) + || isFile(path.join(dir, "cordis.patch.yml")); +} + function desktopHomeCandidates() { const out = []; const push = (dir) => { @@ -1517,9 +1524,15 @@ export function findDshHome(options = {}) { if (fromEnv && String(fromEnv).trim()) return path.normalize(fromEnv.trim()); const desktopHost = adapterFor(detectSurface(options)) === "desktop"; if (desktopHost) { + const found = []; for (const cand of desktopHomeCandidates()) { - if (isDshHomeDir(cand)) return path.normalize(cand); + if (isDshHomeDir(cand)) found.push(path.normalize(cand)); } + const unix = path.normalize(path.join(os.homedir(), ".dsh")); + const usable = found.find((cand) => homeHasHarnessData(cand)); + if (usable) return usable; + if (isDir(unix) && homeHasHarnessData(unix)) return unix; + if (found.length) return found[0]; } const shimDir = options.shimDir !== undefined ? options.shimDir : findShimDir(); const prefix = options.prefix !== undefined ? options.prefix : findNpmPrefix(); diff --git a/lib/index.js b/lib/index.js index 14db281..7c60903 100644 --- a/lib/index.js +++ b/lib/index.js @@ -223,8 +223,31 @@ async function ensureRestartReady() { return clean; } +/** 提示词服务自己的 ctx 多半没有 scope,段会进全局层。插件若挂在某个 scope 上,预设代理的父链看不到那一层。 */ +function addPromptSection(ctx, section) { + const hosts = []; + const push = (host) => { + if (host && !hosts.includes(host)) hosts.push(host); + }; + try { push(ctx?.systemPrompt?.ctx); } catch { /* 代理未注入时点属性会抛 */ } + push(ctx); + let registered = false; + let lastError = null; + for (const host of hosts) { + try { + host.systemPrompt.section(section); + registered = true; + } catch (error) { + const msg = String(error && error.message || error); + if (msg.includes("already registered")) registered = true; + else lastError = error; + } + } + if (!registered && lastError) throw lastError; +} + export function registerPromptSections(ctx, config, _injectFile) { - ctx.systemPrompt.section({ + addPromptSection(ctx, { name: "dsh-purge", order: GLOBAL_PROMPT_ORDER + 1, interpolate: false, @@ -235,7 +258,7 @@ export function registerPromptSections(ctx, config, _injectFile) { }); if (config.capabilityProtocol !== false) { - ctx.systemPrompt.section({ + addPromptSection(ctx, { name: CAPABILITY_SECTION, order: GLOBAL_PROMPT_ORDER, interpolate: false, @@ -264,9 +287,13 @@ export function registerPromptSections(ctx, config, _injectFile) { } function readInjectFallback(context) { - const raw = core.hungOperatorPrompt(); - if (!String(raw || "").trim()) return ""; - return modePromptText(raw, isRedteamSession(context)); + let home = ""; + try { home = core.findDshHome(); } catch { home = ""; } + const raw = rules.ensureOperatorBody(home); + if (!raw) return ""; + let redteam = false; + try { redteam = isRedteamSession(context); } catch { redteam = false; } + return modePromptText(raw, redteam); } function installMnemonMessageStrip(ctx, config = {}) { @@ -418,6 +445,12 @@ function installIdentityOverride(ctx, config = {}) { if (typeof timer.unref === "function") timer.unref(); try { ctx.effect(() => () => clearInterval(timer)); } catch { /* 无 effect 时靠 unref */ } try { ctx.on("session/created", wrapAssemble); } catch { /* ignore */ } + try { + ctx.on("agent-preset/selected", () => { + try { rules.ensureOperatorBody(core.findDshHome()); } catch { /* 主目录还没有提示词 */ } + wrapAssemble(); + }, { global: true }); + } catch { /* ignore */ } try { ctx.on("agent/pre-step", (payload, next) => { wrapAssemble(); diff --git a/lib/rules.js b/lib/rules.js index e4a7721..763fa63 100644 --- a/lib/rules.js +++ b/lib/rules.js @@ -1,7 +1,14 @@ import fs from "node:fs"; import { promises as fsp } from "node:fs"; import path from "node:path"; -import { findDshHome, findOverrideFile, normalizeOverride, peekCommittedOverride } from "./core.js"; +import { + findDshHome, + findOverrideFile, + hangOperatorPrompt, + hungOperatorPrompt, + normalizeOverride, + peekCommittedOverride, +} from "./core.js"; import { modePromptText } from "./identity.js"; const RULE_ID_RE = /^[a-zA-Z0-9][a-zA-Z0-9._-]{0,63}$/; @@ -184,7 +191,7 @@ export function overrideFileText(dshHome) { function diskPrompt(dshHome) { const pinned = peekCommittedOverride(dshHome); - if (typeof pinned === "string") return normalizeOverride(pinned); + if (typeof pinned === "string" && normalizeOverride(pinned)) return normalizeOverride(pinned); return normalizeOverride(overrideFileText(dshHome)); } @@ -198,6 +205,19 @@ export function resolveInjectText(dshHome) { return { text: "", source: "none", ruleId }; } +/** + * 组装时用。内存里已经挂过就用那一份;还没挂(没点保存、也没在本次启动里挂上) + * 就从磁盘提示词或当前规则集补上。空内存不能挡住磁盘上已有的正文。 + * 磁盘和规则集都没有时保持空,不回落内置默认。 + */ +export function ensureOperatorBody(dshHome) { + const hung = normalizeOverride(hungOperatorPrompt()); + if (hung) return hung; + const resolved = resolveInjectText(dshHome); + if (resolved.text) hangOperatorPrompt(resolved.text); + return resolved.text || ""; +} + /** 官方和红队同一顺序。红队再去掉 CTF。没有正文时不注入操作者段。 */ export function resolveModeInject(dshHome, redteam) { const resolved = resolveInjectText(dshHome);