Skip to content

Commit 95c6ead

Browse files
mkantenkoclaude
andcommitted
fix(opencode): keep the replaced service independent of T3 Code
The replacement inherited T3 Desktop's ELECTRON_RUN_AS_NODE and ran in T3 Code's systemd unit, so stopping T3 Code killed it. Run it in its own scope without that variable, make the stop and start uninterruptible, and replace once per update so a version the probe can't match never becomes a kill loop. Document the restart. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent fbee882 commit 95c6ead

4 files changed

Lines changed: 55 additions & 14 deletions

File tree

‎docs/user/providers-opencode.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ settings. T3 Code uses only that configured password for an external server; it
4343
does not forward a local `OPENCODE_SERVER_PASSWORD`. If connection or version checks
4444
fail, check the URL, credentials, and OpenCode version, then refresh provider status.
4545

46+
If **Server URL** points at this machine's OpenCode background service, updating
47+
OpenCode from T3 Code restarts that service on the new version. Running turns are
48+
interrupted, and the service runs with T3 Code's environment. Set any variables it
49+
needs with `opencode service set env <name> <value>`.
50+
4651
After a lost connection, send another prompt to reconnect to the same OpenCode
4752
session.
4853

‎packages/provider-opencode/src/server/driver.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { makeOpenCodeTextGeneration } from "./textGeneration.ts";
1717
import * as ProviderLatestVersions from "@t3tools/provider-core/server/ProviderLatestVersions";
1818
import * as ProviderHost from "@t3tools/provider-core/server/ProviderHost";
1919
import { expandHomePath } from "@t3tools/provider-core/server/pathExpansion";
20+
import { AgentScope } from "@t3tools/shared/AgentScope";
2021
import * as HostProcess from "@t3tools/shared/HostProcess";
2122
import * as OpenCodeAdapterV2 from "./adapter.ts";
2223
import * as OpenCode2AdapterV2 from "./v2/adapter.ts";
@@ -196,6 +197,7 @@ export const OpenCodeDriver: ProviderDriver<
196197
const fileSystem = yield* FileSystem.FileSystem;
197198
const pathService = yield* Path.Path;
198199
const openCodeRuntime = yield* OpenCodeRuntime.OpenCodeRuntime;
200+
const agentScope = yield* AgentScope;
199201
const httpClient = yield* HttpClient.HttpClient;
200202
const latestVersions = yield* ProviderLatestVersions.ProviderLatestVersions;
201203
const digest = yield* Digest.Digest;
@@ -457,6 +459,7 @@ export const OpenCodeDriver: ProviderDriver<
457459
cause,
458460
}),
459461
),
462+
Effect.provideService(AgentScope, agentScope),
460463
Effect.provideService(OpenCodeRuntime.OpenCodeRuntime, openCodeRuntime),
461464
Effect.provideService(FileSystem.FileSystem, fileSystem),
462465
Effect.provideService(Path.Path, pathService),

‎packages/provider-opencode/src/server/v2/OpenCode2Service.test.ts‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { expect, vi } from "vite-plus/test";
99
import * as OpenCodeRuntime from "../OpenCodeRuntime.ts";
1010
import { replaceOutdatedService } from "./OpenCode2Service.ts";
1111

12-
const service = vi.hoisted(() => ({ discover: vi.fn(), ensure: vi.fn() }));
12+
const service = vi.hoisted(() => ({ discover: vi.fn(), stop: vi.fn(), ensure: vi.fn() }));
1313
vi.mock("@opencode/client/effect/service", () => ({ Service: service }));
1414

1515
const runtimeWithBinaryVersion = (stdout: string) =>
@@ -29,7 +29,12 @@ it.layer(NodeServices.layer)("replaceOutdatedService", (it) => {
2929
yield* fileSystem.makeDirectory(binDirectory);
3030
yield* fileSystem.writeFileString(path.join(binDirectory, "opencode"), "");
3131
yield* fileSystem.chmod(path.join(binDirectory, "opencode"), 0o755);
32-
const environment = { HOME: home, PATH: binDirectory, OPENCODE_CONFIG_DIR: home };
32+
const environment = {
33+
HOME: home,
34+
PATH: binDirectory,
35+
OPENCODE_CONFIG_DIR: home,
36+
ELECTRON_RUN_AS_NODE: "1",
37+
};
3338
yield* fileSystem.writeFileString(
3439
path.join(home, "service.json"),
3540
JSON.stringify({ env: { PATH: "/nowhere", XDG_STATE_HOME: "/elsewhere" } }),
@@ -46,9 +51,14 @@ it.layer(NodeServices.layer)("replaceOutdatedService", (it) => {
4651
runtimeWithBinaryVersion(binary),
4752
),
4853
);
54+
service.stop.mockReturnValue(Effect.void);
4955
service.ensure.mockReturnValue(Effect.void);
56+
const outdated = (options: { readonly version?: string }) =>
57+
Effect.succeed(
58+
options.version === undefined ? { url: "http://0.0.0.0:49374" } : undefined,
59+
);
5060

51-
service.discover.mockReturnValue(Effect.succeed({ url: "http://0.0.0.0:49374" }));
61+
service.discover.mockImplementation(outdated);
5262
yield* run("opencode v1.18.32");
5363
expect(service.discover).not.toHaveBeenCalled();
5464

@@ -71,11 +81,15 @@ it.layer(NodeServices.layer)("replaceOutdatedService", (it) => {
7181

7282
service.discover.mockReturnValue(Effect.succeed({ url: "http://0.0.0.0:49374" }));
7383
yield* run("opencode v2.2.5");
84+
expect(service.stop).not.toHaveBeenCalled();
85+
86+
service.discover.mockImplementation(outdated);
87+
yield* run("opencode v2.2.5");
88+
expect(service.stop).toHaveBeenCalledWith({ file, pty: "handoff" });
7489
expect(service.ensure).toHaveBeenCalledWith({
7590
file,
76-
version: "2.2.5",
7791
command: [path.join(binDirectory, "opencode"), "serve", "--service"],
78-
env: { ...environment, PATH: "/nowhere" },
92+
env: { ...environment, PATH: "/nowhere", ELECTRON_RUN_AS_NODE: undefined },
7993
});
8094
}).pipe(Effect.scoped),
8195
);

‎packages/provider-opencode/src/server/v2/OpenCode2Service.ts‎

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Service } from "@opencode/client/effect/service";
2+
import { AgentScope } from "@t3tools/shared/AgentScope";
23
import * as HostProcess from "@t3tools/shared/HostProcess";
34
import { resolveCommandPath, resolveSpawnCommand } from "@t3tools/shared/shell";
45
import * as Effect from "effect/Effect";
@@ -35,8 +36,8 @@ function isRegisteredService(registeredUrl: string, serverUrl: string): boolean
3536
}
3637

3738
/**
38-
* Replaces the way the TUI does on a version mismatch, which hands persistent terminals to the new
39-
* process. `opencode service restart` shuts them down instead.
39+
* Replaces the way the TUI's restart does, which hands persistent terminals to the new process.
40+
* `opencode service restart` shuts them down instead.
4041
*/
4142
export const replaceOutdatedService = Effect.fn("OpenCode2Service.replaceOutdatedService")(
4243
function* (input: {
@@ -63,7 +64,16 @@ export const replaceOutdatedService = Effect.fn("OpenCode2Service.replaceOutdate
6364
// Configured variables below can change PATH, which a bare name would then resolve against.
6465
const executable = yield* resolveCommandPath(input.binaryPath, { env: input.environment });
6566
if (executable === null) return;
66-
const command = yield* resolveSpawnCommand(executable, ["serve", "--service"], {
67+
// A scope of its own keeps the service alive when T3 Code's systemd unit stops.
68+
const launch = yield* (yield* AgentScope).wrap({
69+
command: executable,
70+
args: ["serve", "--service"],
71+
name: "opencode-service",
72+
env: input.environment,
73+
shared: true,
74+
survivesRestart: true,
75+
});
76+
const command = yield* resolveSpawnCommand(launch.command, launch.args, {
6777
env: input.environment,
6878
extendEnv: true,
6979
bypassNpmShim: true,
@@ -89,11 +99,20 @@ export const replaceOutdatedService = Effect.fn("OpenCode2Service.replaceOutdate
8999
if (value !== undefined) env[name] = value;
90100
}
91101

92-
yield* Service.ensure({
93-
file,
94-
version,
95-
command: [command.command, ...command.args],
96-
env,
97-
});
102+
if ((yield* Service.discover({ file, version })) !== undefined) return;
103+
104+
// `ensure` with a version would kill every new service whose version never matches the probe,
105+
// and being interrupted between the stop and the start would leave no service running.
106+
yield* Service.stop({ file, pty: "handoff" }).pipe(
107+
Effect.andThen(
108+
Service.ensure({
109+
file,
110+
command: [command.command, ...command.args],
111+
// T3 Desktop's backend sets this; undefined overrides the inherited value at spawn.
112+
env: { ...env, ELECTRON_RUN_AS_NODE: undefined } as unknown as Record<string, string>,
113+
}),
114+
),
115+
Effect.uninterruptible,
116+
);
98117
},
99118
);

0 commit comments

Comments
 (0)