GeezerVison is showing github.com — feat(server,web): see and stop the dev servers a thread started (#22542) · pingdotgg/t3code@a27ec88
File treeExpand file treeCollapse file tree Open diff view settings orchestration-v2/Adapters
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/server/src/auth/RpcAuthorization.test.ts +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -59,6 +59,7 @@ describe("RPC authorization scopes", () => { | 59 | 59 | WS_METHODS.terminalClear, | 60 | 60 | WS_METHODS.terminalRestart, | 61 | 61 | WS_METHODS.terminalClose, | | 62 | + WS_METHODS.previewStopLocalServer, | 62 | 63 | ]) { | 63 | 64 | expect(requiredScopeForRpcMethod(method)).toBe(AuthTerminalOperateScope); | 64 | 65 | } | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/auth/RpcAuthorization.ts +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -196,6 +196,7 @@ export const RPC_REQUIRED_SCOPES = { | 196 | 196 | [WS_METHODS.previewClearProfile]: AuthPreviewOperateScope, | 197 | 197 | [WS_METHODS.previewReportProfiles]: AuthPreviewOperateScope, | 198 | 198 | [WS_METHODS.previewReportStatus]: AuthPreviewOperateScope, | | 199 | + [WS_METHODS.previewStopLocalServer]: AuthTerminalOperateScope, | 199 | 200 | [WS_METHODS.subscribePreviewEvents]: AuthOrchestrationReadScope, | 200 | 201 | [WS_METHODS.subscribeDiscoveredLocalServers]: AuthOrchestrationReadScope, | 201 | 202 | [WS_METHODS.deviceConfigure]: AuthSettingsWriteScope, | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/environment/ServerEnvironment.ts +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -255,6 +255,7 @@ export const make = Effect.gen(function* () { | 255 | 255 | threadPullRequestLinking: true, | 256 | 256 | serverResolvedCommandContext: true, | 257 | 257 | threadStop: true, | | 258 | + ...(hostPlatform === "win32" ? {} : { localServerStop: true }), | 258 | 259 | environmentIcon: true, | 259 | 260 | projectCloneTracking: true, | 260 | 261 | ignoredFileListing: true, | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/observability/RpcInstrumentation.ts +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -204,6 +204,7 @@ const RPC_AGGREGATES = { | 204 | 204 | [WS_METHODS.previewReportStatus]: "preview", | 205 | 205 | [WS_METHODS.subscribePreviewEvents]: "preview", | 206 | 206 | [WS_METHODS.subscribeDiscoveredLocalServers]: "preview", | | 207 | + [WS_METHODS.previewStopLocalServer]: "preview", | 207 | 208 | [WS_METHODS.deviceConfigure]: "device", | 208 | 209 | [WS_METHODS.deviceTestHost]: "device", | 209 | 210 | [WS_METHODS.deviceEasAccount]: "device", | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -74,6 +74,7 @@ import * as HostProcess from "@t3tools/shared/HostProcess"; | 74 | 74 | import { getModelSelectionStringOptionValue } from "@t3tools/shared/model"; | 75 | 75 | import { SpawnExecutableResolution } from "@t3tools/shared/shell"; | 76 | 76 |
| | 77 | +import * as PortScanner from "../../preview/PortScanner.ts"; | 77 | 78 | import { attachmentRelativePath } from "../../attachmentStore.ts"; | 78 | 79 | import { WorkflowAgentTranscriptReadObserver } from "../workflowAgentTranscript.ts"; | 79 | 80 | import * as ServerConfig from "../../config.ts"; | @@ -2325,7 +2326,7 @@ describe("ClaudeAdapterV2 MCP credential channel", () => { | 2325 | 2326 | }, | 2326 | 2327 | }); | 2327 | 2328 | }).pipe( | 2328 | | - Effect.provide(ClaudeAdapterV2.layerQueryRunner), | | 2329 | + Effect.provide(ClaudeAdapterV2.layerQueryRunner.pipe(Layer.provide(PortScanner.layerTest))), | 2329 | 2330 | Effect.provideService(ProviderEventLoggers.ProviderEventLoggers, { | 2330 | 2331 | ...ProviderEventLoggers.NoOpProviderEventLoggers, | 2331 | 2332 | native, | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +17-1Lines changed: 17 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -138,6 +138,7 @@ import { | 138 | 138 | type CompiledClaudeModelSelection, | 139 | 139 | } from "../../claudeModelOptions.ts"; | 140 | 140 | import * as ServerConfig from "../../config.ts"; | | 141 | +import * as PortScanner from "../../preview/PortScanner.ts"; | 141 | 142 | import * as ServerSettings from "../../serverSettings.ts"; | 142 | 143 | import { expandHomePath } from "@t3tools/provider-core/server/pathExpansion"; | 143 | 144 | import { spawnAndCollect } from "@t3tools/provider-core/server/snapshotProbe"; | @@ -917,6 +918,7 @@ export const layerQueryRunner: Layer.Layer< | 917 | 918 | | ChildProcessSpawner.ChildProcessSpawner | 918 | 919 | | Crypto.Crypto | 919 | 920 | | Path.Path | | 921 | + | PortScanner.PortDiscovery | 920 | 922 | | ProviderEventLoggers.ProviderEventLoggers | 921 | 923 | > = Layer.effect( | 922 | 924 | ClaudeAgentSdkQueryRunner, | @@ -928,6 +930,8 @@ export const layerQueryRunner: Layer.Layer< | 928 | 930 | const agentScope = yield* AgentScope; | 929 | 931 | const platform = yield* HostProcess.Platform; | 930 | 932 | const runFork = Effect.runForkWith(yield* Effect.context<never>()); | | 933 | + const hostEnvironment = yield* HostProcess.Environment; | | 934 | + const portDiscovery = yield* PortScanner.PortDiscovery; | 931 | 935 |
| 932 | 936 | return ClaudeAgentSdkQueryRunner.of({ | 933 | 937 | allocateSessionId: crypto | @@ -951,7 +955,15 @@ export const layerQueryRunner: Layer.Layer< | 951 | 955 | Stream.toAsyncIterable, | 952 | 956 | ); | 953 | 957 | // Send MCP credentials over stdin to keep them out of SDK argv and inherited command environments. | 954 | | - const { mcpServers, ...queryOptions } = input.options; | | 958 | + const { mcpServers, ...sdkOptions } = input.options; | | 959 | + // Jobs the agent starts inherit the thread, so port discovery can attribute their servers. | | 960 | + const queryOptions = { | | 961 | + ...sdkOptions, | | 962 | + env: { | | 963 | + ...(sdkOptions.env ?? hostEnvironment), | | 964 | + [PortScanner.AGENT_THREAD_ENV]: input.threadId, | | 965 | + }, | | 966 | + }; | 955 | 967 | // The SDK can prepend a scope wrapper to native binaries; script entries run through node. | 956 | 968 | const binaryPath = queryOptions.pathToClaudeCodeExecutable; | 957 | 969 | const launch = | @@ -998,6 +1010,10 @@ export const layerQueryRunner: Layer.Layer< | 998 | 1010 | const spawnClaudeCodeProcess = (spawnOptions: ClaudeSpawnOptions) => { | 999 | 1011 | const child = spawnProcess(spawnOptions); | 1000 | 1012 | spawnedProcess = child; | | 1013 | + // A spawn that fails emits only "error". | | 1014 | + const releaseAgent = portDiscovery.trackAgent(); | | 1015 | + child.once("exit", releaseAgent); | | 1016 | + child.on("error", releaseAgent); | 1001 | 1017 | processExited = child.exitCode !== null || child.signalCode != null; | 1002 | 1018 | processExit = new Promise<void>((resolve) => { | 1003 | 1019 | if (processExited) resolve(); | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/Adapters/ClaudeAgentSdkQueryRunner.test.ts +53Lines changed: 53 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -24,6 +24,7 @@ import { AgentScope } from "@t3tools/shared/AgentScope"; | 24 | 24 | import { ProviderEventLoggers } from "@t3tools/provider-core/server/ProviderEventLoggers"; | 25 | 25 | import { makeProviderFailure } from "@t3tools/provider-core/server/failure"; | 26 | 26 | import { signalProcessGroup } from "@t3tools/provider-core/server/processGroup"; | | 27 | +import * as PortScanner from "../../preview/PortScanner.ts"; | 27 | 28 | import { | 28 | 29 | ClaudeAgentSdkQueryRunner, | 29 | 30 | type ClaudeAgentSdkQueryOpenInput, | @@ -66,6 +67,7 @@ const writeClaudeSession = Effect.fnUntraced(function* (configDir: string) { | 66 | 67 | }); | 67 | 68 |
| 68 | 69 | const forkRunnerLayer = layerQueryRunner.pipe( | | 70 | + Layer.provide(PortScanner.layerTest), | 69 | 71 | Layer.provide(Layer.succeed(ProviderEventLoggers, { native: undefined, canonical: undefined })), | 70 | 72 | ); | 71 | 73 |
| @@ -110,6 +112,7 @@ it.effect("forks a session stored under the instance's own CLAUDE_CONFIG_DIR", ( | 110 | 112 | ).pipe(Effect.provide(NodeServices.layer)), | 111 | 113 | ); | 112 | 114 | const realQueryRunner = layerQueryRunner.pipe( | | 115 | + Layer.provide(PortScanner.layerTest), | 113 | 116 | Layer.provide( | 114 | 117 | Layer.succeed(ProviderEventLoggers, { | 115 | 118 | native: undefined, | @@ -487,4 +490,54 @@ describe.skipIf(HostProcess.Platform.defaultValue() === "win32")("Claude CLI pro | 487 | 490 | yield* Effect.promise(() => turnStopped); | 488 | 491 | }), | 489 | 492 | ); | | 493 | + | | 494 | + it.effect("marks the CLI's jobs with its thread and counts it as an agent until it exits", () => { | | 495 | + let runningAgents = 0; | | 496 | + let releaseAgent!: () => void; | | 497 | + const agentReleased = new Promise<void>((resolve) => (releaseAgent = resolve)); | | 498 | + const runner = layerQueryRunner.pipe( | | 499 | + Layer.provide( | | 500 | + Layer.mock(PortScanner.PortDiscovery)({ | | 501 | + trackAgent: () => { | | 502 | + runningAgents += 1; | | 503 | + return () => { | | 504 | + runningAgents -= 1; | | 505 | + releaseAgent(); | | 506 | + }; | | 507 | + }, | | 508 | + }), | | 509 | + ), | | 510 | + Layer.provide( | | 511 | + Layer.succeed(ProviderEventLoggers, { native: undefined, canonical: undefined }), | | 512 | + ), | | 513 | + Layer.provide(NodeServices.layer), | | 514 | + ); | | 515 | + return Effect.gen(function* () { | | 516 | + const cli = fakeClaudeCli( | | 517 | + [ | | 518 | + 'dir=$(dirname "$0")', | | 519 | + `printf '%s' "$${PortScanner.AGENT_THREAD_ENV}" > "$dir/thread"`, | | 520 | + "cat > /dev/null", | | 521 | + ].join("\n"), | | 522 | + ); | | 523 | + const dir = NodePath.dirname(cli); | | 524 | + NodeChildProcess.execFileSync("mkfifo", [NodePath.join(dir, "thread")]); | | 525 | + yield* Effect.addFinalizer(() => | | 526 | + Effect.sync(() => NodeFS.rmSync(dir, { recursive: true, force: true })), | | 527 | + ); | | 528 | + const threadRead = new Promise<string>((resolve) => { | | 529 | + let thread = ""; | | 530 | + NodeFS.createReadStream(NodePath.join(dir, "thread"), "utf8") | | 531 | + .on("data", (chunk) => (thread += chunk)) | | 532 | + .once("end", () => resolve(thread)); | | 533 | + }); | | 534 | + | | 535 | + const session = yield* openFakeCli(cli); | | 536 | + assert.equal(yield* Effect.promise(() => threadRead), "thread-fake-cli"); | | 537 | + assert.equal(runningAgents, 1); | | 538 | + yield* session.close; | | 539 | + yield* Effect.promise(() => agentReleased); | | 540 | + assert.equal(runningAgents, 0); | | 541 | + }).pipe(Effect.provide(runner)); | | 542 | + }); | 490 | 543 | }); |
|