diff --git a/src/dsh-channel.ts b/src/dsh-channel.ts index 633b147b2a..3dbb7c2017 100644 --- a/src/dsh-channel.ts +++ b/src/dsh-channel.ts @@ -184,6 +184,31 @@ export function dshProfileDependsOnBili(profileDir: string): boolean { return dshProfileDepSpec(profileDir) !== undefined; } +/** #7155: the profile a `dsh` argv boots, mirroring dsh's own parser — a bare + * first argument abbreviates `--profile ` (except the `plugin` + * subcommand, which takes its own required flag), otherwise the explicit + * `--profile ` / `--profile=` wins. undefined when the argv names + * no profile; dsh itself then errors out, so callers fall back to the + * machine-wide answer. The launcher needs this to ask "is THIS profile + * installed" instead of "does any profile on this machine have a copy". */ +export function dshProfileFromArgs(args: readonly string[]): string | undefined { + const first = args[0]; + if (first !== undefined && !first.startsWith("-") && first !== "plugin") return first; + for (let i = 0; i < args.length; i++) { + const arg = args[i]!; + if (arg === "--") break; + if (arg === "--profile") { + const value = args[i + 1]; + return value !== undefined && value !== "" && !value.startsWith("-") ? value : undefined; + } + if (arg.startsWith("--profile=")) { + const value = arg.slice("--profile=".length); + return value !== "" ? value : undefined; + } + } + return undefined; +} + /** #2894: converge this profile's declared billion-context spec to the version * bili just installed IN PLACE (the dsh desktop lane — #1575). The desktop * copy's node_modules entry is swapped by bili's own tarball installer while diff --git a/src/launcher.ts b/src/launcher.ts index b09d4a80ad..3363e22847 100644 --- a/src/launcher.ts +++ b/src/launcher.ts @@ -61,6 +61,7 @@ import { stateDir } from "./paths.js"; import { TWIN_ADOPT_EXIT_CODE } from "./listen-policy.js"; import { tcpConnectable } from "./liveness.js"; import { selfPackageRoot, isBiliPiEntry, ompPluginLoadedFrom, dshNativeInstalled, claudeNativeInstalled, piPackageStrings } from "./plugin-install.js"; +import { dshProfileFromArgs } from "./dsh-channel.js"; import { applyOmpFirstEventTimeout, applyOmpMessageThreadsOff, envMillis } from "./agent/native-bootstrap.js"; import { logHostEvent } from "./agent/host-log.js"; import { compactionDisablePatchRows } from "./agent/dsh-compaction-disable.js"; @@ -7280,8 +7281,20 @@ export async function runLaunch(params: RunLaunchParams, deps: LauncherDeps = {} const prevNodePath = env.NODE_PATH; env.NODE_PATH = prevNodePath !== undefined && prevNodePath.length > 0 ? `${dshNm}${path.delimiter}${prevNodePath}` : dshNm; let dshAcpPatch: string | undefined; - if (!dshNativeInstalled()) { + // #7155: ask about the profile this run is about to boot, not the + // machine. The machine-wide form made a bundle-less profile look + // installed whenever a sibling profile had the bundle: the overlay was + // skipped and the session ran with ZERO bili entries (no compression + // tools, no takeover, native compaction armed) while the note below + // described the other profile. + const targetProfile = dshProfileFromArgs(params.clientArgs); + if (!dshNativeInstalled(process.env, targetProfile)) { dshAcpPatch = writeDshAcpPatch(dshHomeDir, dshPluginEntry(dshOverlayHome ?? dshHomeDir)); + if (targetProfile !== undefined && dshNativeInstalled()) { + console.error( + `bili: profile "${targetProfile}" does not install billion-context (another profile on this machine does) — this run mounts the launcher overlay, so compression works for this session only. Install it permanently with: dsh plugin --profile ${targetProfile} add billion-context`, + ); + } } else { // Unconditional (#3666): the #2028 allowDshCompaction opt-out was // removed, so a native install always leaves dsh compaction armed diff --git a/src/plugin-install.ts b/src/plugin-install.ts index 3e46c60eed..a5d00241e5 100644 --- a/src/plugin-install.ts +++ b/src/plugin-install.ts @@ -1696,20 +1696,29 @@ function dshStatus(): string { return "not installed"; } -/** True when any profile carries the persistent native install — either the - * bundle-channel dep or a pre-unification managed block. The `bili dsh` - * launcher consults this before adding its own --patch overlay: cordis - * rejects duplicate loader entry ids across layers, so a second - * `id: bili-native` insert would hard-fail dsh boot whenever the persistent - * install is present. */ -export function dshNativeInstalled(env: NodeJS.ProcessEnv = process.env): boolean { +/** True when the given profile (or, with no profile, ANY profile) carries the + * persistent native install — either the bundle-channel dep or a + * pre-unification managed block. The `bili dsh` launcher consults this + * before adding its own --patch overlay. + * + * #7155: the launcher must ask about the profile it is ABOUT TO BOOT, not + * the machine. The old any-profile form made a bundle-less profile look + * installed whenever a sibling profile had the bundle: the overlay was + * skipped, the session ran with zero bili entries (no compression tools, no + * takeover, native compaction armed) and the launch note described the OTHER + * profile's state. Omitting `profile` keeps the machine-wide answer for the + * callers that genuinely need it (status/tests). */ +export function dshNativeInstalled(env: NodeJS.ProcessEnv = process.env, profile?: string): boolean { let dirs: string[]; try { dirs = dshProfileDirs(env); } catch { return false; } - return dirs.some((dir) => dshBundleInstalled(dir) || dshHasLegacyManagedBlock(dir)); + const scoped = profile === undefined || profile === "" + ? dirs + : dirs.filter((dir) => path.basename(dir) === profile); + return scoped.some((dir) => dshBundleInstalled(dir) || dshHasLegacyManagedBlock(dir)); } // — kimi ———————————————————————————————————————————————————————————————— diff --git a/tests/issue7155-dsh-native-installed-profile-scope.test.ts b/tests/issue7155-dsh-native-installed-profile-scope.test.ts new file mode 100644 index 0000000000..1120169db3 --- /dev/null +++ b/tests/issue7155-dsh-native-installed-profile-scope.test.ts @@ -0,0 +1,89 @@ +// #7155: `bili dsh --profile X` used the MACHINE-wide install answer +// (`dshNativeInstalled()` = "any profile has the bundle") to decide whether to +// mount its own --patch overlay. On a machine where a sibling profile has the +// bundle but X does not (a scratch profile created after `bili plugin install +// dsh`, a profile whose dep was removed by hand), the overlay was skipped and +// the launch note described the OTHER profile — so the session ran with zero +// bili entries: no ACP compression tools, no takeover, and native compaction +// armed (which the wire guard then refuses turn after turn). +// Fix: ask about the profile being booted, parsing `--profile` the way dsh +// itself does, and say plainly when the profile lacks the bundle. +import test from "node:test"; +import assert from "node:assert/strict"; +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import { dshNativeInstalled } from "../src/plugin-install.ts"; +import { dshProfileFromArgs } from "../src/dsh-channel.ts"; + +function withHome(home: string, fn: () => T): T { + const prev = process.env.DSH_HOME; + process.env.DSH_HOME = home; + try { + return fn(); + } finally { + if (prev === undefined) delete process.env.DSH_HOME; + else process.env.DSH_HOME = prev; + } +} + +function profile(dir: string, bundles?: string[]): void { + fs.mkdirSync(dir, { recursive: true }); + if (bundles !== undefined) { + fs.writeFileSync(path.join(dir, "package.json"), JSON.stringify({ dsh: { profile: { bundles } } })); + } +} + +test("#7155 dshNativeInstalled(profile) answers for THAT profile; omitting it stays machine-wide", () => { + const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-7155-")); + try { + withHome(home, () => { + profile(path.join(home, "profiles", "installed"), ["@deepseek-ai/dsh-base", "billion-context"]); + profile(path.join(home, "profiles", "scratch"), ["@deepseek-ai/dsh-base"]); + profile(path.join(home, "profiles", "empty")); + + assert.equal(dshNativeInstalled(), true, "machine-wide answer unchanged (backward compatible)"); + assert.equal(dshNativeInstalled(process.env, "installed"), true); + assert.equal(dshNativeInstalled(process.env, "scratch"), false, "the booted profile has no bundle"); + assert.equal(dshNativeInstalled(process.env, "empty"), false); + assert.equal(dshNativeInstalled(process.env, "never-created"), false, "an unknown profile cannot be installed"); + assert.equal(dshNativeInstalled(process.env, ""), true, "empty profile id falls back to the machine-wide answer"); + }); + } finally { + fs.rmSync(home, { recursive: true, force: true }); + } +}); + +test("#7155 dshNativeInstalled(profile) honours a legacy managed block and a missing profiles root", () => { + const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-7155-legacy-")); + try { + withHome(home, () => { + assert.equal(dshNativeInstalled(process.env, "anything"), false, "no profiles root at all"); + const legacy = path.join(home, "profiles", "old"); + profile(legacy); + fs.writeFileSync(path.join(legacy, "cordis.patch.yml"), "# bili begin (managed by billion-context — `bili plugin install dsh`)\n- insert:\n - id: bili-native\n name: /opt/bili\n# bili end\n"); + assert.equal(dshNativeInstalled(process.env, "old"), true, "a pre-unification managed block counts for that profile"); + assert.equal(dshNativeInstalled(process.env, "other"), false); + }); + } finally { + fs.rmSync(home, { recursive: true, force: true }); + } +}); + +test("#7155 dshProfileFromArgs mirrors dsh's own --profile parsing", () => { + // A bare first argument abbreviates --profile (dsh's expansion rule). + assert.equal(dshProfileFromArgs(["web"]), "web"); + assert.equal(dshProfileFromArgs(["headless", "run the tests"]), "headless"); + assert.equal(dshProfileFromArgs(["--profile", "tui"]), "tui"); + assert.equal(dshProfileFromArgs(["--profile=tui"]), "tui"); + assert.equal(dshProfileFromArgs(["--no-open", "--profile", "tui", "--port", "0"]), "tui"); + // `plugin` takes its own required flag; a positional after it is not a profile. + assert.equal(dshProfileFromArgs(["plugin", "--profile", "x", "add", "billion-context"]), "x"); + assert.equal(dshProfileFromArgs(["plugin", "add", "billion-context"]), undefined); + // No profile named at all (dsh errors out itself; the caller falls back). + assert.equal(dshProfileFromArgs([]), undefined); + assert.equal(dshProfileFromArgs(["--dump-config"]), undefined); + assert.equal(dshProfileFromArgs(["--profile"]), undefined); + // `--` terminates launcher flags: what follows belongs to the app. + assert.equal(dshProfileFromArgs(["--", "--profile", "nope"]), undefined); +});