Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pkgs/mcpx/internal/e2e/effect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ var implementedEffectProbes = map[string]string{
"plugin.tools": "TestPluginToolsRegistersTheToolSetFromTheEnvironment",
"plugin.toolTiming": "TestPluginToolTimingRecordsToolCallsFromTheEnvironment",
"plugin.remember": "TestPluginRememberScopePersistsTheDaemonChoiceFromTheEnvironment",
"plugin.headless": "TestPluginHeadlessSuppressesToastsFromTheEnvironment",
"paths.state": "TestStateDirectoryIsReachableAsASetting",
"paths.configFile": "TestPathsConfigFileChangesWhichConfigLoads",
"paths.scripts": "TestPathsScriptsFindsRecipesThatLiveOnlyThere",
Expand Down Expand Up @@ -305,6 +306,7 @@ var effectProbeFuncs = map[string]func(*testing.T){
"TestPluginToolsRegistersTheToolSetFromTheEnvironment": TestPluginToolsRegistersTheToolSetFromTheEnvironment,
"TestPluginToolTimingRecordsToolCallsFromTheEnvironment": TestPluginToolTimingRecordsToolCallsFromTheEnvironment,
"TestPluginRememberScopePersistsTheDaemonChoiceFromTheEnvironment": TestPluginRememberScopePersistsTheDaemonChoiceFromTheEnvironment,
"TestPluginHeadlessSuppressesToastsFromTheEnvironment": TestPluginHeadlessSuppressesToastsFromTheEnvironment,
"TestPoolDefaultsAreReachableFromTheEnvironment": TestPoolDefaultsAreReachableFromTheEnvironment,
"TestRegistryLimitReachesTheCLI": TestRegistryLimitReachesTheCLI,
"TestRegistryPageSizeChangesWhatTheCLIAsksFor": TestRegistryPageSizeChangesWhatTheCLIAsksFor,
Expand Down
1 change: 0 additions & 1 deletion pkgs/mcpx/internal/e2e/feature_coverage_exemptions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,5 @@ setting daemon.tsnetUpTimeout no integration-test exercise yet; burn-down #8468
setting http.streamBufferInit allocation-capacity hint only; output and line ceiling are unchanged, so allocation size has no stable e2e behavior oracle
setting protoTasks.cancelGrace no integration-test exercise yet; burn-down #8473 (settings proto family); needs an EffectProbes entry + e2e test
setting plugin.disabledTools no integration-test exercise yet; burn-down #8472 (settings plugin family); needs an EffectProbes entry + e2e test
setting plugin.headless no integration-test exercise yet; burn-down #8472 (settings plugin family); needs an EffectProbes entry + e2e test
setting plugin.instructions no integration-test exercise yet; burn-down #8472 (settings plugin family); needs an EffectProbes entry + e2e test
setting embeddings.wasmPath embeddings backend: needs a compiled wasm model fixture the harness does not provide; no embed-exporting model ships with the repo
34 changes: 34 additions & 0 deletions pkgs/mcpx/internal/e2e/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,3 +395,37 @@ func TestPluginRememberScopePersistsTheDaemonChoiceFromTheEnvironment(t *testing
t.Fatalf("the plugin.remember probe did not report a clean run:\n%s", out)
}
}

// TestPluginHeadlessSuppressesToastsFromTheEnvironment is the effect
// probe for plugin.headless (#8472; the effect guard is #1971).
//
// The setting's whole effect lives in the opencode plugin host: when
// discovery cannot settle on one daemon, the session plugin warns
// the user with a toast — unless headless is set (MCPX_PLUGIN_HEADLESS
// or the plugin option), because showToast succeeds whether or not
// anyone is watching and a toast nobody can see is a leak, not a
// notification. No Go caller produces the toast, so the probe drives
// the host the way the plugin suite does — bun, the real session
// plugin, two fake daemons on real sockets so boot discovery is
// ambiguous — and the bun cases in
// plugin/opencode/mcpx/headless.test.ts assert the observable
// with/without difference: the warning toast arrives when headless
// is cleared and never when it is set. A run that skipped the cases
// is a run that did not check what this probe exists to check, so
// skips fail it, as in TestThePluginSuiteRunsAgainstARealDaemon.
func TestPluginHeadlessSuppressesToastsFromTheEnvironment(t *testing.T) {
bun := testsupport.RequireBinary(t, "bun")
recordSettingProbes(t)
cmd := exec.Command(bun, "test", "plugin/opencode/mcpx/headless.test.ts")
cmd.Dir = repoRoot(t)
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("bun test plugin/opencode/mcpx/headless.test.ts: %v\n%s", err, out)
}
if strings.Contains(string(out), " skip") && !strings.Contains(string(out), " 0 skip") {
t.Fatalf("the plugin.headless probe cases were skipped:\n%s", out)
}
if !strings.Contains(string(out), " 0 fail") {
t.Fatalf("the plugin.headless probe did not report a clean run:\n%s", out)
}
}
4 changes: 2 additions & 2 deletions pkgs/mcpx/internal/settings/effect.go

Large diffs are not rendered by default.

211 changes: 211 additions & 0 deletions pkgs/mcpx/plugin/opencode/mcpx/headless.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
/**
* plugin.headless, observed through the real boot toast (#8472).
*
* The headless setting — MCPX_PLUGIN_HEADLESS, or the plugin's
* headless option — decides whether the session plugin may put a
* toast on the user's screen. mcpx-session.ts reads it once at load
* (option, then environment, then a process-shape default: a server
* running on the main thread has no TUI attached) and the toast
* helper returns before touching the client when it is set, because
* `showToast` succeeds whether or not anyone is watching — an
* unbounded toast in CI is a silent leak, not a visible one.
*
* The cases make the boot toast fire on purpose: two fake daemons
* on real unix sockets both match the directory and neither has a
* resolve route, so discovery ends at rung 6 — a guess, announced
* with a warning toast (the daemon.test.ts arrangement). The setting
* is then the only thing that differs, and the stub client's
* showToast calls are the observable: with headless set, none; with
* it cleared, the warning arrives. The Go effect probe for the
* setting (TestPluginHeadlessSuppressesToastsFromTheEnvironment in
* internal/e2e) runs this file.
*
* bun test plugin/opencode/mcpx/headless.test.ts
*/
import { afterEach, describe, expect, mock, test } from "bun:test";
import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";

import { OPERATION_FINGERPRINT } from "../../core/ops.gen.ts";
import type { DaemonInfo } from "../../core/daemon.ts";

// The plugin package has no installed node_modules in the repo
// checkout; only the `tool` helper is needed to build the hooks.
const schemaStub: any = new Proxy(function () {}, {
get: () => schemaStub,
apply: () => schemaStub,
});
mock.module("@opencode-ai/plugin", () => ({
tool: Object.assign((x: unknown) => x, { schema: schemaStub }),
}));
// @ts-ignore: the session plugin exports only its default (loader glob).
const plugin = (await import("../mcpx-session.ts")).default;

// Captured before any test runs: tmpdir() reads TMPDIR, and the
// environment restore below must not strand it.
const REAL_TMP = tmpdir();
const scratch: string[] = [];
const stopAll: Array<() => void> = [];
const savedEnv = { ...process.env };
afterEach(() => {
while (stopAll.length) stopAll.pop()!();
// Restore in place, on the original process.env object (the
// env.test.ts pattern): replacing process.env wholesale does not
// restore the native environment other code reads.
for (const key of Object.keys(process.env)) {
if (!(key in savedEnv)) delete process.env[key];
}
Object.assign(process.env, savedEnv);
for (const dir of scratch.splice(0)) rmSync(dir, { recursive: true, force: true });
});

const tempRoot = (): string => {
const dir = mkdtempSync(join(REAL_TMP, "mcpxt-headless-"));
scratch.push(dir);
return dir;
};

/** A fake daemon on a real unix socket: health and status, no resolve route. */
const serveFake = (socket: string, config: string, servers: number): void => {
const server = Bun.serve({
unix: socket,
fetch(req) {
const url = new URL(req.url);
if (url.pathname === "/v1/health") {
return Response.json({
status: "ok",
version: "test",
pid: 1,
operationFingerprint: OPERATION_FINGERPRINT,
});
}
if (url.pathname === "/v1/status") {
return Response.json({
version: "test",
operationFingerprint: OPERATION_FINGERPRINT,
pid: 1,
socket,
config,
servers: Array.from({ length: servers }, (_, i) => ({
name: `s${i}`,
live: 1,
instances: [{ key: `ses-${i}` }],
})),
});
}
return new Response("not found", { status: 404 });
},
});
stopAll.push(() => server.stop(true));
};

const writeInfo = (state: string, key: string, info: DaemonInfo): void => {
writeFileSync(join(state, `daemon-${key}.json`), JSON.stringify(info, null, 2));
};

type ToastCall = { body?: { title?: string; message?: string; variant?: string } };

/**
* Load the session plugin over a state directory holding two live
* daemons that both match the session directory, so boot discovery
* is ambiguous and the boot warning toast is due. `setEnv` runs
* before the load (the setting is read from the environment at load
* time); `options` are the plugin options. Returns every showToast
* call the stub client received.
*/
const loadWithAmbiguousBoot = async (
setEnv: () => void,
options: Record<string, unknown>,
): Promise<ToastCall[]> => {
const root = tempRoot();
const state = join(root, "state");
mkdirSync(state);
const proj = join(root, "proj");
const dir = join(proj, "sub");
mkdirSync(dir, { recursive: true });

const mine = join(state, "d-mine.sock");
const other = join(state, "d-other.sock");
serveFake(mine, join(proj, ".mcpx.json"), 1);
serveFake(other, join(root, "elsewhere", ".mcpx.json"), 9);
writeInfo(state, "mine", {
socket: mine,
configPath: join(proj, ".mcpx.json"),
startedAt: new Date(Date.now() - 86_400_000).toISOString(),
});
writeInfo(state, "other", {
socket: other,
configPath: join(root, "elsewhere", ".mcpx.json"),
startedAt: new Date().toISOString(),
});

// Isolate discovery: this state directory and temp root only, no
// named endpoint, and no binary for the ladder's CLI rung to ask.
process.env.MCPX_STATE_DIR = state;
process.env.TMPDIR = root;
Comment on lines +145 to +146

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Isolate the runtime directory before daemon discovery

When running on a host with a live daemon under $XDG_RUNTIME_DIR/mcpx, this setup still discovers the host's sockets: socketDirs() in plugin/core/daemon.ts scans that directory in addition to MCPX_STATE_DIR and TMPDIR. The extra daemon can change the warning to 3 daemons, breaking the positive assertions, or answer /v1/resolve and eliminate the ambiguous warning entirely. Clear XDG_RUNTIME_DIR or point it inside the test's scratch root so both the standalone effect probe and direct Bun runs depend only on the two fake daemons.

Useful? React with 👍 / 👎.

delete process.env.MCPX_DAEMON_ENDPOINT;
delete process.env.MCPX_ENDPOINT;
delete process.env.MCPX_SOCKET;
process.env.MCPX_PLUGIN_BIN = "";
setEnv();

const toasts: ToastCall[] = [];
const client = {
session: {
get: async () => ({ data: { id: "ses_1", directory: dir, time: { created: Date.now() } } }),
},
tui: {
showToast: async (arg: ToastCall) => {
toasts.push(arg);
},
},
};
await (plugin as any)({ directory: dir, worktree: proj, project: { id: "p1" }, client }, options);
// The boot toast is fire-and-forget; let it land before counting.
await new Promise((resolve) => setTimeout(resolve, 25));
return toasts;
};

describe("plugin.headless and the boot toast", () => {
test("MCPX_PLUGIN_HEADLESS=1 suppresses the boot warning toast", async () => {
const toasts = await loadWithAmbiguousBoot(() => {
process.env.MCPX_PLUGIN_HEADLESS = "1";
}, {});
expect(toasts).toHaveLength(0);
});

test("the headless option suppresses the boot warning toast", async () => {
const toasts = await loadWithAmbiguousBoot(() => {
delete process.env.MCPX_PLUGIN_HEADLESS;
}, { headless: true });
expect(toasts).toHaveLength(0);
});

test("headless cleared by option lets the boot warning toast through", async () => {
const toasts = await loadWithAmbiguousBoot(() => {
delete process.env.MCPX_PLUGIN_HEADLESS;
}, { headless: false });
expect(toasts.length).toBeGreaterThanOrEqual(1);
expect(toasts[0].body?.message).toContain("2 daemons");
});

test("MCPX_PLUGIN_HEADLESS=0 lets the boot warning toast through", async () => {
const toasts = await loadWithAmbiguousBoot(() => {
process.env.MCPX_PLUGIN_HEADLESS = "0";
}, {});
expect(toasts.length).toBeGreaterThanOrEqual(1);
expect(toasts[0].body?.message).toContain("2 daemons");
});

test("unset everywhere defaults to headless in this process shape", async () => {
// No option and no environment: the default is the process
// shape, and a test runner's server thread has no TUI attached,
// so the default reads headless and the toast stays down. This
// is the CI case the setting exists for.
const toasts = await loadWithAmbiguousBoot(() => {
delete process.env.MCPX_PLUGIN_HEADLESS;
}, {});
expect(toasts).toHaveLength(0);
});
});