Skip to content

Commit c7e3f45

Browse files
fix(plugins): structure host errors and scope route styles
Keep plugin-owned failure diagnostics while storing host boundary context separately. Move route visibility styling to SidebarInset and remove the static home-region test.
1 parent e8ddf8c commit c7e3f45

6 files changed

Lines changed: 54 additions & 41 deletions

File tree

‎apps/server/src/plugins/PluginHost.test.ts‎

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -169,25 +169,36 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("PluginHost", (it
169169
}).pipe(Effect.provide(layerConfig())),
170170
);
171171

172-
it.effect("marks a plugin that throws in activate as failed and keeps the others running", () =>
173-
Effect.gen(function* () {
174-
yield* writePluginsConfig({
175-
enabled: { [hello]: true, [broken]: true },
176-
devPaths: [`${fixturesDir}broken-plugin`, `${fixturesDir}hello-plugin`],
177-
});
178-
yield* Effect.gen(function* () {
179-
const host = yield* PluginHost.PluginHost;
180-
const states = yield* waitForStatuses(host, { [broken]: "failed", [hello]: "active" });
181-
assert.deepStrictEqual(stateOf(states, broken)?.failure, {
182-
stage: "activate",
183-
message: "broken-plugin refuses to start",
172+
it.effect(
173+
"marks a plugin that throws while loading or in activate as failed and keeps the others running",
174+
() =>
175+
Effect.gen(function* () {
176+
yield* writeInstalledPlugin("unloadable", `throw new Error("unloadable cannot load");`);
177+
yield* writePluginsConfig({
178+
enabled: { [hello]: true, [broken]: true, unloadable: true },
179+
devPaths: [`${fixturesDir}broken-plugin`, `${fixturesDir}hello-plugin`],
184180
});
185-
const missing = yield* host
186-
.invoke({ pluginId: broken, method: "ping", input: undefined, scopes: operate })
187-
.pipe(Effect.flip);
188-
assert.strictEqual(missing._tag, "PluginNotActiveError");
189-
}).pipe(Effect.provide(layerHost()));
190-
}).pipe(Effect.provide(layerConfig())),
181+
yield* Effect.gen(function* () {
182+
const host = yield* PluginHost.PluginHost;
183+
const states = yield* waitForStatuses(host, {
184+
[broken]: "failed",
185+
[hello]: "active",
186+
unloadable: "failed",
187+
});
188+
assert.deepStrictEqual(stateOf(states, broken)?.failure, {
189+
stage: "activate",
190+
message: "broken-plugin refuses to start",
191+
});
192+
assert.deepStrictEqual(stateOf(states, "unloadable")?.failure, {
193+
stage: "load",
194+
message: "unloadable cannot load",
195+
});
196+
const missing = yield* host
197+
.invoke({ pluginId: broken, method: "ping", input: undefined, scopes: operate })
198+
.pipe(Effect.flip);
199+
assert.strictEqual(missing._tag, "PluginNotActiveError");
200+
}).pipe(Effect.provide(layerHost()));
201+
}).pipe(Effect.provide(layerConfig())),
191202
);
192203

193204
it.effect("fails a plugin with server code below Node 24 and runs one without", () =>

‎apps/server/src/plugins/PluginHost.ts‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ import {
5050
type PluginActivation,
5151
type PluginApiServices,
5252
} from "./PluginApi.ts";
53-
import { errorText, PluginCodeError, pluginCodeScope } from "./api/facade.ts";
53+
import { errorText, isPluginCodeError, PluginCodeError, pluginCodeScope } from "./api/facade.ts";
5454
import { makePluginFailureGuard } from "./pluginFailureGuard.ts";
5555
import { makePluginFailures } from "./pluginFailures.ts";
5656
import { discoverPlugins, type DiscoveredPlugin } from "./pluginDiscovery.ts";
@@ -236,6 +236,12 @@ interface Found {
236236
// absolute path loads files from disk in both, and plugin code resolves its own imports.
237237
const requireFromDisk = NodeModule.createRequire(process.execPath);
238238

239+
// A failed plugin's row shows the plugin's own error text, a deliberate exception to fixed messages.
240+
const pluginFailureText = (cause: Cause.Cause<PluginCodeError>) => {
241+
const error = Cause.squash(cause);
242+
return errorText(isPluginCodeError(error) ? error.cause : error);
243+
};
244+
239245
const isPluginDefinition = (value: unknown): value is PluginDefinition =>
240246
typeof value === "object" &&
241247
value !== null &&
@@ -332,11 +338,14 @@ const make = Effect.gen(function* () {
332338
const runCleanup = (pluginId: PluginId, cleanup: () => void | Promise<void>) =>
333339
Effect.tryPromise({
334340
try: () => pluginCodeScope.run(pluginId, () => Promise.resolve().then(cleanup)),
335-
catch: (cause) => new PluginCodeError({ cause }),
341+
catch: (cause) => new PluginCodeError({ pluginId, stage: "cleanup", cause }),
336342
}).pipe(
337343
Effect.catchTags({
338344
PluginCodeError: (error) =>
339-
Effect.logWarning("plugin cleanup failed", { pluginId, reason: error.message }),
345+
Effect.logWarning("plugin cleanup failed", {
346+
pluginId,
347+
reason: errorText(error.cause),
348+
}),
340349
}),
341350
);
342351

@@ -382,7 +391,7 @@ const make = Effect.gen(function* () {
382391
}
383392
return definition;
384393
},
385-
catch: (cause) => new PluginCodeError({ cause }),
394+
catch: (cause) => new PluginCodeError({ pluginId: plugin.id, stage: "load", cause }),
386395
});
387396

388397
const activatePlugin = Effect.fn("PluginHost.activate")(function* (plugin: DiscoveredPlugin) {
@@ -433,7 +442,7 @@ const make = Effect.gen(function* () {
433442
if (definition._tag === "Failure") {
434443
return yield* setRecord(plugin.id, {
435444
status: "failed",
436-
failure: { stage: "load", message: errorText(Cause.squash(definition.cause)) },
445+
failure: { stage: "load", message: pluginFailureText(definition.cause) },
437446
});
438447
}
439448
const activation = makePluginActivation();
@@ -456,7 +465,8 @@ const make = Effect.gen(function* () {
456465
pluginCodeScope.run(plugin.id, () =>
457466
Promise.resolve().then(() => definition.value.activate(api)),
458467
),
459-
catch: (cause) => new PluginCodeError({ cause }),
468+
catch: (cause) =>
469+
new PluginCodeError({ pluginId: plugin.id, stage: "activate", cause }),
460470
}).pipe(Effect.timeoutOption(ACTIVATE_DEADLINE)),
461471
).pipe(Effect.exit);
462472
if (activated._tag === "Success" && Option.isSome(activated.value)) {
@@ -472,7 +482,7 @@ const make = Effect.gen(function* () {
472482
stage: "activate",
473483
message:
474484
activated._tag === "Failure"
475-
? errorText(Cause.squash(activated.cause))
485+
? pluginFailureText(activated.cause)
476486
: `activate did not finish within ${Duration.toSeconds(ACTIVATE_DEADLINE)} seconds.`,
477487
},
478488
});

‎apps/server/src/plugins/api/facade.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// must follow plugin code through Node's own callbacks.
33
import * as NodeAsyncHooks from "node:async_hooks";
44

5-
import type { PluginId } from "@t3tools/contracts";
5+
import { PluginId } from "@t3tools/contracts";
66
import * as Cause from "effect/Cause";
77
import type * as Context from "effect/Context";
88
import * as Effect from "effect/Effect";
@@ -26,12 +26,15 @@ export const errorText = (cause: unknown) => {
2626
};
2727

2828
export class PluginCodeError extends Schema.TaggedError<PluginCodeError>()("PluginCodeError", {
29+
pluginId: PluginId,
30+
stage: Schema.Literals(["load", "activate", "cleanup"]),
2931
cause: Schema.Defect(),
3032
}) {
3133
override get message(): string {
32-
return errorText(this.cause);
34+
return `Plugin ${this.pluginId} code failed during ${this.stage}.`;
3335
}
3436
}
37+
export const isPluginCodeError = Schema.is(PluginCodeError);
3538

3639
/**
3740
* The plugin whose code is running, carried through every timer, promise and Node callback that

‎apps/web/src/components/ChatView.browser.test.tsx‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { page, userEvent } from "vite-plus/test/browser";
99
import "~/index.css";
1010

1111
import { AppRoot } from "~/AppRoot";
12-
import { NoProjectsHero } from "~/components/NoProjectsHero";
1312
// The route loads ChatView lazily, and its first transform (React Compiler over ~4k lines)
1413
// takes 6-11s; importing it here keeps that out of the test's timeout.
1514
import "~/components/ChatView";
@@ -218,10 +217,3 @@ it("marks the window's main areas with the region names plugin styles target", a
218217
.element(page.getByRole("region", { name: "Pull requests" }))
219218
.toHaveAttribute("data-t3-region", "pull-requests");
220219
});
221-
222-
it("marks the home page with the region name plugin styles target", async () => {
223-
await using _screen = await render(<NoProjectsHero />);
224-
const homeTitle = page.getByText("What should we work on?");
225-
await expect.element(homeTitle).toBeInTheDocument();
226-
expect(homeTitle.element().closest("[data-t3-region]")).toHaveAttribute("data-t3-region", "home");
227-
});

‎apps/web/src/components/ui/sidebar.tsx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,9 @@ function SidebarInset({ className, ...props }: React.ComponentProps<"main">) {
634634
<main
635635
className={cn(
636636
"relative flex min-w-0 w-full flex-1 flex-col bg-background surface-grain",
637+
// Settings or Usage shown as a page (a plugin's `app.pageFrame`) hides the thread kept
638+
// mounted under it.
639+
"has-[~_[data-app-route-page]]:hidden",
637640
"md:peer-data-[variant=inset]:peer-data-[state=collapsed]:ms-2 md:peer-data-[variant=inset]:m-2 md:peer-data-[variant=inset]:ms-0 md:peer-data-[variant=inset]:rounded-xl md:peer-data-[variant=inset]:shadow-sm/5",
638641
className,
639642
)}

‎apps/web/src/index.css‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,12 +1101,6 @@ html[data-mobile-composer-route-transition="true"]::view-transition-old(t3-mobil
11011101
background-size: var(--surface-grain-size);
11021102
}
11031103

1104-
/* Settings or Usage shown as a page (a plugin's `app.pageFrame`) hides the thread kept mounted
1105-
under it. */
1106-
[data-slot="sidebar-inset"]:has(~ [data-app-route-page]) {
1107-
display: none;
1108-
}
1109-
11101104
/* Suppress all transitions during theme changes */
11111105
.no-transitions,
11121106
.no-transitions *,

0 commit comments

Comments
 (0)