-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Track terminal listening ports in activity events and thread state #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
juliusmarminge
wants to merge
16
commits into
main
Choose a base branch
from
codething/50e6e9da
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
f2ec777
Track running terminal ports across terminal activity events
juliusmarminge 94a83ab
Require all lsof filters when checking listening ports
juliusmarminge 3d082fc
Detect real web ports and add one-click localhost shortcut
juliusmarminge b3e967e
adapters
juliusmarminge c11027b
Show terminal runtime status and web port actions in thread UI
juliusmarminge 38644b0
Detect root-process ports and harden web port probing
juliusmarminge d377e56
Merge origin/main into codething/50e6e9da
juliusmarminge 05a7018
Merge origin/main into codething/50e6e9da
juliusmarminge 75ae12f
Extract terminal inspectors into Effect services
juliusmarminge bc30ac5
Consolidate terminal inspectors under process
juliusmarminge 6aac79c
Use Effect HTTP client for web port probing
juliusmarminge 0236cc8
Move process inspector types beside their services
juliusmarminge da81a16
Normalize terminal state and harden process probing
juliusmarminge 07252e5
Refine terminal subprocess inspection and status tooltips
juliusmarminge 037a771
Merge origin/main into codething/50e6e9da
juliusmarminge b0bc101
Refactor process inspector control flow
juliusmarminge File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
162 changes: 162 additions & 0 deletions
162
apps/server/src/process/Layers/TerminalProcessInspector.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| import { spawn, type ChildProcess, type ChildProcessByStdio } from "node:child_process"; | ||
| import type { Readable } from "node:stream"; | ||
|
|
||
| import * as NodeChildProcessSpawner from "@effect/platform-node/NodeChildProcessSpawner"; | ||
| import * as NodeServices from "@effect/platform-node/NodeServices"; | ||
| import { assert, it } from "@effect/vitest"; | ||
| import { Effect, Layer } from "effect"; | ||
|
|
||
| import { TerminalProcessInspector } from "../Services/TerminalProcessInspector.ts"; | ||
| import { TerminalProcessInspectorLive } from "./TerminalProcessInspector.ts"; | ||
|
|
||
| type ListenerProcess = ChildProcessByStdio<null, Readable, Readable>; | ||
|
|
||
| interface StartedProcess { | ||
| readonly process: ListenerProcess; | ||
| readonly port: number; | ||
| } | ||
|
|
||
| const stopProcess = (child: ChildProcess) => | ||
| Effect.callback<void>((resume) => { | ||
| if (child.exitCode !== null || child.signalCode !== null) { | ||
| resume(Effect.void); | ||
| return; | ||
| } | ||
|
|
||
| child.kill("SIGTERM"); | ||
|
|
||
| const timeout = setTimeout(() => { | ||
| if (child.exitCode === null && child.signalCode === null) { | ||
| child.kill("SIGKILL"); | ||
| } | ||
| }, 1_000); | ||
|
|
||
| child.once("exit", () => { | ||
| clearTimeout(timeout); | ||
| resume(Effect.void); | ||
| }); | ||
| }); | ||
|
|
||
| const waitForPort = (child: ListenerProcess) => | ||
| Effect.callback<number, Error>((resume) => { | ||
| const timeout = setTimeout(() => { | ||
| cleanup(); | ||
| resume(Effect.fail(new Error("Timed out waiting for listener port"))); | ||
| }, 3_000); | ||
|
|
||
| let stdout = ""; | ||
| let stderr = ""; | ||
|
|
||
| const cleanup = () => { | ||
| clearTimeout(timeout); | ||
| child.stdout.off("data", onStdout); | ||
| child.stderr.off("data", onStderr); | ||
| child.off("exit", onExit); | ||
| }; | ||
|
|
||
| const onStdout = (chunk: Buffer) => { | ||
| stdout += chunk.toString("utf8"); | ||
| const match = stdout.match(/PORT:(\d+)/); | ||
| if (!match?.[1]) return; | ||
| const port = Number(match[1]); | ||
| if (!Number.isInteger(port) || port <= 0) return; | ||
| cleanup(); | ||
| resume(Effect.succeed(port)); | ||
| }; | ||
|
|
||
| const onStderr = (chunk: Buffer) => { | ||
| stderr += chunk.toString("utf8"); | ||
| }; | ||
|
|
||
| const onExit = (code: number | null) => { | ||
| cleanup(); | ||
| resume( | ||
| Effect.fail( | ||
| new Error( | ||
| `Listener process exited before reporting port (code=${String(code)}): ${stderr.trim()}`, | ||
| ), | ||
| ), | ||
| ); | ||
| }; | ||
|
|
||
| child.stdout.on("data", onStdout); | ||
| child.stderr.on("data", onStderr); | ||
| child.on("exit", onExit); | ||
|
|
||
| return Effect.sync(cleanup); | ||
| }); | ||
|
|
||
| const startListenerProcess = Effect.gen(function* () { | ||
| const script = [ | ||
| "const { createServer } = require('node:http');", | ||
| "const server = createServer((_req, res) => {", | ||
| " res.statusCode = 200;", | ||
| " res.end('ok');", | ||
| "});", | ||
| "server.listen(0, '127.0.0.1', () => {", | ||
| " const address = server.address();", | ||
| " if (typeof address !== 'object' || !address) process.exit(1);", | ||
| " console.log(`PORT:${address.port}`);", | ||
| "});", | ||
| "const shutdown = () => server.close(() => process.exit(0));", | ||
| "process.on('SIGTERM', shutdown);", | ||
| "process.on('SIGINT', shutdown);", | ||
| "setInterval(() => {}, 10_000);", | ||
| ].join(""); | ||
|
|
||
| const process = spawn("node", ["-e", script], { | ||
| stdio: ["ignore", "pipe", "pipe"], | ||
| }); | ||
| const port = yield* waitForPort(process); | ||
| return { process, port } satisfies StartedProcess; | ||
| }); | ||
|
|
||
| const nodeChildProcessLayer = NodeChildProcessSpawner.layer.pipe(Layer.provide(NodeServices.layer)); | ||
|
|
||
| const testLayer = TerminalProcessInspectorLive.pipe(Layer.provide(nodeChildProcessLayer)); | ||
|
|
||
| it.layer(testLayer)("TerminalProcessInspectorLive", (it) => { | ||
| it.effect("detects listening ports when the terminal root pid is the listener", () => | ||
| Effect.acquireUseRelease( | ||
| startListenerProcess, | ||
| ({ process, port }) => | ||
| Effect.gen(function* () { | ||
| const listenerPid = process.pid; | ||
| if (!listenerPid) { | ||
| return yield* Effect.fail(new Error("Listener process pid missing")); | ||
| } | ||
|
|
||
| const inspector = yield* TerminalProcessInspector; | ||
| const activity = yield* inspector.inspect(listenerPid); | ||
|
|
||
| assert.equal(activity.hasRunningSubprocess, true); | ||
| assert.deepStrictEqual(activity.runningPorts.includes(port), true); | ||
| }), | ||
| ({ process }) => stopProcess(process), | ||
| ), | ||
| ); | ||
|
|
||
| it.effect("returns idle activity when root process has no children and no listening ports", () => | ||
| Effect.acquireUseRelease( | ||
| Effect.sync(() => | ||
| spawn("node", ["-e", "setInterval(() => {}, 10_000)"], { | ||
| stdio: ["ignore", "ignore", "ignore"], | ||
| }), | ||
| ), | ||
| (process) => | ||
| Effect.gen(function* () { | ||
| const idlePid = process.pid; | ||
| if (!idlePid) { | ||
| return yield* Effect.fail(new Error("Idle process pid missing")); | ||
| } | ||
|
|
||
| const inspector = yield* TerminalProcessInspector; | ||
| const activity = yield* inspector.inspect(idlePid); | ||
|
|
||
| assert.equal(activity.hasRunningSubprocess, false); | ||
| assert.deepStrictEqual(activity.runningPorts, []); | ||
| }), | ||
| stopProcess, | ||
| ), | ||
| ); | ||
| }); | ||
232 changes: 232 additions & 0 deletions
232
apps/server/src/process/Layers/TerminalProcessInspector.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,232 @@ | ||
| import { Buffer } from "node:buffer"; | ||
|
|
||
| import { Effect, Layer, Option, PlatformError, Stream } from "effect"; | ||
| import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process"; | ||
|
|
||
| import { collectPosixProcessFamilyPids, checkPosixListeningPorts } from "../posix.ts"; | ||
| import type { | ||
| TerminalProcessInspectorShape, | ||
| TerminalSubprocessActivity, | ||
| } from "../Services/TerminalProcessInspector.ts"; | ||
| import { | ||
| TerminalProcessInspector, | ||
| TerminalProcessInspectionError, | ||
| } from "../Services/TerminalProcessInspector.ts"; | ||
| import { checkWindowsListeningPorts, collectWindowsChildPids } from "../win32.ts"; | ||
|
|
||
| const DEFAULT_COMMAND_KILL_GRACE_MS = 1_000; | ||
|
|
||
| interface InspectorCommandResult { | ||
| readonly stdout: string; | ||
| readonly stderr: string; | ||
| readonly exitCode: number; | ||
| } | ||
|
|
||
| interface CollectOutputResult { | ||
| readonly text: string; | ||
| readonly truncated: boolean; | ||
| } | ||
|
|
||
| interface RunInspectorCommandInput { | ||
| readonly operation: string; | ||
| readonly terminalPid: number; | ||
| readonly command: string; | ||
| readonly args: ReadonlyArray<string>; | ||
| readonly timeoutMs: number; | ||
| readonly maxOutputBytes: number; | ||
| } | ||
|
|
||
| function commandLabel(command: string, args: ReadonlyArray<string>): string { | ||
| return [command, ...args].join(" "); | ||
| } | ||
|
|
||
| const collectOutput = Effect.fn("process.collectOutput")(function* ( | ||
| stream: Stream.Stream<Uint8Array, PlatformError.PlatformError>, | ||
| maxOutputBytes: number, | ||
| ): Effect.fn.Return<CollectOutputResult, PlatformError.PlatformError> { | ||
| return yield* stream.pipe( | ||
| Stream.decodeText(), | ||
| Stream.runFold( | ||
| () => ({ | ||
| text: "", | ||
| bytes: 0, | ||
| truncated: false, | ||
| }), | ||
| (state, chunk) => { | ||
| if (state.bytes >= maxOutputBytes) { | ||
| return { | ||
| ...state, | ||
| truncated: true, | ||
| }; | ||
| } | ||
|
|
||
| const chunkBytes = Buffer.byteLength(chunk); | ||
| const remainingBytes = maxOutputBytes - state.bytes; | ||
| if (chunkBytes <= remainingBytes) { | ||
| return { | ||
| text: `${state.text}${chunk}`, | ||
| bytes: state.bytes + chunkBytes, | ||
| truncated: state.truncated, | ||
| }; | ||
| } | ||
|
|
||
| const truncatedChunk = Buffer.from(chunk).subarray(0, remainingBytes).toString("utf8"); | ||
| return { | ||
| text: `${state.text}${truncatedChunk}`, | ||
| bytes: state.bytes + remainingBytes, | ||
| truncated: true, | ||
| }; | ||
| }, | ||
| ), | ||
| Effect.map(({ text, truncated }) => ({ text, truncated })), | ||
| ); | ||
| }); | ||
|
|
||
| const makeTerminalProcessInspector = Effect.gen(function* () { | ||
| const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; | ||
|
|
||
| const runInspectorCommand = Effect.fn("process.runInspectorCommand")(function* ( | ||
| input: RunInspectorCommandInput, | ||
| ) { | ||
| const command = ChildProcess.make(input.command, [...input.args], { | ||
| killSignal: "SIGTERM", | ||
| forceKillAfter: DEFAULT_COMMAND_KILL_GRACE_MS, | ||
| }); | ||
|
|
||
| return yield* Effect.gen(function* () { | ||
| const child = yield* spawner.spawn(command).pipe( | ||
| Effect.mapError( | ||
| (cause) => | ||
| new TerminalProcessInspectionError({ | ||
| operation: input.operation, | ||
| terminalPid: input.terminalPid, | ||
| command: commandLabel(input.command, input.args), | ||
| detail: "Failed to spawn inspector command.", | ||
| cause, | ||
| }), | ||
| ), | ||
| ); | ||
|
|
||
| const [stdout, stderr, exitCode] = yield* Effect.all( | ||
| [ | ||
| collectOutput(child.stdout, input.maxOutputBytes).pipe( | ||
| Effect.mapError( | ||
| (cause) => | ||
| new TerminalProcessInspectionError({ | ||
| operation: input.operation, | ||
| terminalPid: input.terminalPid, | ||
| command: commandLabel(input.command, input.args), | ||
| detail: "Failed to read stdout from inspector command.", | ||
| cause, | ||
| }), | ||
| ), | ||
| ), | ||
| collectOutput(child.stderr, input.maxOutputBytes).pipe( | ||
| Effect.mapError( | ||
| (cause) => | ||
| new TerminalProcessInspectionError({ | ||
| operation: input.operation, | ||
| terminalPid: input.terminalPid, | ||
| command: commandLabel(input.command, input.args), | ||
| detail: "Failed to read stderr from inspector command.", | ||
| cause, | ||
| }), | ||
| ), | ||
| ), | ||
| child.exitCode.pipe( | ||
| Effect.map(Number), | ||
| Effect.mapError( | ||
| (cause) => | ||
| new TerminalProcessInspectionError({ | ||
| operation: input.operation, | ||
| terminalPid: input.terminalPid, | ||
| command: commandLabel(input.command, input.args), | ||
| detail: "Failed to read inspector command exit code.", | ||
| cause, | ||
| }), | ||
| ), | ||
| ), | ||
| ], | ||
| { concurrency: "unbounded" }, | ||
| ); | ||
|
|
||
| return { | ||
| stdout: stdout.text, | ||
| stderr: stderr.text, | ||
| exitCode, | ||
| } satisfies InspectorCommandResult; | ||
| }).pipe( | ||
| Effect.scoped, | ||
| Effect.timeoutOption(input.timeoutMs), | ||
| Effect.flatMap( | ||
| Option.match({ | ||
| onNone: () => | ||
| Effect.fail( | ||
| new TerminalProcessInspectionError({ | ||
| operation: input.operation, | ||
| terminalPid: input.terminalPid, | ||
| command: commandLabel(input.command, input.args), | ||
| detail: "Inspector command timed out.", | ||
| }), | ||
| ), | ||
| onSome: Effect.succeed, | ||
| }), | ||
| ), | ||
| ); | ||
| }); | ||
|
|
||
| const inspect: TerminalProcessInspectorShape["inspect"] = Effect.fn("process.inspect")(function* ( | ||
| terminalPid: number, | ||
| ) { | ||
| if (!Number.isInteger(terminalPid) || terminalPid <= 0) { | ||
| return { | ||
| hasRunningSubprocess: false, | ||
| runningPorts: [], | ||
| } satisfies TerminalSubprocessActivity; | ||
| } | ||
|
|
||
| if (process.platform === "win32") { | ||
| const childPids = yield* collectWindowsChildPids(terminalPid, runInspectorCommand); | ||
| const processPidsForPortScan = [terminalPid, ...childPids]; | ||
| const runningPorts = yield* checkWindowsListeningPorts(processPidsForPortScan, { | ||
| terminalPid, | ||
| runCommand: runInspectorCommand, | ||
| }); | ||
| return { | ||
| hasRunningSubprocess: childPids.length > 0 || runningPorts.length > 0, | ||
| runningPorts, | ||
| } satisfies TerminalSubprocessActivity; | ||
| } | ||
|
|
||
| const processFamilyPids = yield* collectPosixProcessFamilyPids( | ||
| terminalPid, | ||
| runInspectorCommand, | ||
| ); | ||
| if (processFamilyPids.length === 0) { | ||
| return { | ||
| hasRunningSubprocess: false, | ||
| runningPorts: [], | ||
| } satisfies TerminalSubprocessActivity; | ||
| } | ||
|
|
||
| const subprocessPids = processFamilyPids.filter((pid: number) => pid !== terminalPid); | ||
| const runningPorts = yield* checkPosixListeningPorts(processFamilyPids, { | ||
| terminalPid, | ||
| runCommand: runInspectorCommand, | ||
| platform: process.platform, | ||
| }); | ||
| return { | ||
| hasRunningSubprocess: subprocessPids.length > 0 || runningPorts.length > 0, | ||
| runningPorts, | ||
| } satisfies TerminalSubprocessActivity; | ||
| }); | ||
|
|
||
| return { | ||
| inspect, | ||
| } satisfies TerminalProcessInspectorShape; | ||
| }); | ||
|
|
||
| export const TerminalProcessInspectorLive = Layer.effect( | ||
| TerminalProcessInspector, | ||
| makeTerminalProcessInspector, | ||
| ); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.