Compare commits
7
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bc8573d736 | ||
|
|
335d6ac076 | ||
|
|
67f95ee25a | ||
|
|
7232f23400 | ||
|
|
16d3376410 | ||
|
|
008fbdd587 | ||
|
|
923837537b |
No files matched your search
@@ -68,6 +68,9 @@ npx t3
|
||||
- `bun run dev` — Starts contracts, server, and web in `turbo watch` mode.
|
||||
- `bun run dev:server` — Starts just the WebSocket server (uses Bun TypeScript execution).
|
||||
- `bun run dev:web` — Starts just the Vite dev server for the web app.
|
||||
- Dev commands default `T3CODE_STATE_DIR` to `~/.t3/dev` to keep dev state isolated from desktop/prod state.
|
||||
- Override server CLI-equivalent flags from root dev commands with `--`, for example:
|
||||
`bun run dev -- --state-dir ~/.t3/another-dev-state`
|
||||
- `bun run start` — Runs the production server (serves built web app as static files).
|
||||
- `bun run build` — Builds contracts, web app, and server through Turbo.
|
||||
- `bun run typecheck` — Strict TypeScript checks for all packages.
|
||||
|
||||
@@ -5,16 +5,8 @@ import net from "node:net";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
|
||||
import {
|
||||
app,
|
||||
BrowserWindow,
|
||||
dialog,
|
||||
ipcMain,
|
||||
Menu,
|
||||
type MenuItemConstructorOptions,
|
||||
protocol,
|
||||
shell,
|
||||
} from "electron";
|
||||
import { app, BrowserWindow, dialog, ipcMain, Menu, protocol, shell } from "electron";
|
||||
import type { MenuItemConstructorOptions } from "electron";
|
||||
|
||||
import { showDesktopConfirmDialog } from "./confirmDialog";
|
||||
import { fixPath } from "./fixPath";
|
||||
@@ -26,7 +18,8 @@ const CONFIRM_CHANNEL = "desktop:confirm";
|
||||
const CONTEXT_MENU_CHANNEL = "desktop:context-menu";
|
||||
const OPEN_EXTERNAL_CHANNEL = "desktop:open-external";
|
||||
const MENU_ACTION_CHANNEL = "desktop:menu-action";
|
||||
const STATE_DIR = path.join(os.homedir(), ".t3", "userdata");
|
||||
const STATE_DIR =
|
||||
process.env.T3CODE_STATE_DIR?.trim() || path.join(os.homedir(), ".t3", "userdata");
|
||||
const DESKTOP_SCHEME = "t3";
|
||||
const ROOT_DIR = path.resolve(__dirname, "../../..");
|
||||
const isDevelopment = Boolean(process.env.VITE_DEV_SERVER_URL);
|
||||
|
||||
@@ -317,12 +317,35 @@ export const makeOrchestrationIntegrationHarness = Effect.gen(function* () {
|
||||
}
|
||||
disposed = true;
|
||||
|
||||
yield* Effect.promise(() => runtime.runPromise(providerService.stopAll()));
|
||||
yield* Effect.promise(() => Effect.runPromise(Scope.close(scope, Exit.void)));
|
||||
yield* Effect.promise(() => runtime.dispose());
|
||||
yield* Effect.sync(() => {
|
||||
fs.rmSync(rootDir, { recursive: true, force: true });
|
||||
const shutdown = Effect.gen(function* () {
|
||||
const stopAllExit = yield* Effect.exit(
|
||||
Effect.promise(() => runtime.runPromise(providerService.stopAll())),
|
||||
);
|
||||
const closeScopeExit = yield* Effect.exit(
|
||||
Effect.promise(() => Effect.runPromise(Scope.close(scope, Exit.void))),
|
||||
);
|
||||
const disposeRuntimeExit = yield* Effect.exit(Effect.promise(() => runtime.dispose()));
|
||||
|
||||
const failureCause = Exit.isFailure(stopAllExit)
|
||||
? stopAllExit.cause
|
||||
: Exit.isFailure(closeScopeExit)
|
||||
? closeScopeExit.cause
|
||||
: Exit.isFailure(disposeRuntimeExit)
|
||||
? disposeRuntimeExit.cause
|
||||
: null;
|
||||
|
||||
if (failureCause) {
|
||||
return yield* Effect.failCause(failureCause);
|
||||
}
|
||||
});
|
||||
|
||||
yield* shutdown.pipe(
|
||||
Effect.ensuring(
|
||||
Effect.sync(() => {
|
||||
fs.rmSync(rootDir, { recursive: true, force: true });
|
||||
}),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
return {
|
||||
|
||||
@@ -13,7 +13,7 @@ import {
|
||||
ThreadId,
|
||||
} from "@t3tools/contracts";
|
||||
import { assert, it } from "@effect/vitest";
|
||||
import { Effect, Option } from "effect";
|
||||
import { Effect, Exit, Option } from "effect";
|
||||
|
||||
import type { TestTurnResponse } from "./TestProviderAdapter.integration.ts";
|
||||
import {
|
||||
@@ -133,6 +133,24 @@ const startTurn = (input: {
|
||||
createdAt: nowIso(),
|
||||
});
|
||||
|
||||
it.live("always removes integration temp dirs when shutdown fails", () =>
|
||||
Effect.gen(function* () {
|
||||
const harness = yield* makeOrchestrationIntegrationHarness;
|
||||
const rootDir = harness.rootDir;
|
||||
const originalStopAll = harness.providerService.stopAll.bind(harness.providerService);
|
||||
|
||||
(
|
||||
harness.providerService as unknown as {
|
||||
stopAll: typeof harness.providerService.stopAll;
|
||||
}
|
||||
).stopAll = () => originalStopAll().pipe(Effect.andThen(Effect.fail(new Error("boom"))));
|
||||
|
||||
const disposeExit = yield* Effect.exit(harness.dispose);
|
||||
assert.equal(Exit.isFailure(disposeExit), true);
|
||||
assert.equal(fs.existsSync(rootDir), false);
|
||||
}),
|
||||
);
|
||||
|
||||
it.live("runs a single turn end-to-end and persists checkpoint state in sqlite + git", () =>
|
||||
withHarness((harness) =>
|
||||
Effect.gen(function* () {
|
||||
|
||||
+174
-34
@@ -1,8 +1,12 @@
|
||||
import { spawn } from "node:child_process";
|
||||
import { homedir } from "node:os";
|
||||
import path from "node:path";
|
||||
import { pathToFileURL } from "node:url";
|
||||
|
||||
const BASE_SERVER_PORT = 3773;
|
||||
const BASE_WEB_PORT = 5173;
|
||||
const MAX_HASH_OFFSET = 3000;
|
||||
export const DEFAULT_DEV_STATE_DIR = path.join(homedir(), ".t3", "dev");
|
||||
const MODE_ARGS = {
|
||||
dev: [
|
||||
"run",
|
||||
@@ -17,6 +21,21 @@ const MODE_ARGS = {
|
||||
"dev:web": ["run", "dev", "--filter=@t3tools/web"],
|
||||
"dev:desktop": ["run", "dev", "--filter=@t3tools/desktop", "--filter=@t3tools/web", "--parallel"],
|
||||
};
|
||||
const FORWARDED_ENV_FLAGS = {
|
||||
"state-dir": { envName: "T3CODE_STATE_DIR", expectsValue: true },
|
||||
"auth-token": { envName: "T3CODE_AUTH_TOKEN", expectsValue: true },
|
||||
token: { envName: "T3CODE_AUTH_TOKEN", expectsValue: true },
|
||||
"no-browser": { envName: "T3CODE_NO_BROWSER", expectsValue: false },
|
||||
"auto-bootstrap-project-from-cwd": {
|
||||
envName: "T3CODE_AUTO_BOOTSTRAP_PROJECT_FROM_CWD",
|
||||
expectsValue: false,
|
||||
},
|
||||
"log-websocket-events": { envName: "T3CODE_LOG_WS_EVENTS", expectsValue: false },
|
||||
"log-ws-events": { envName: "T3CODE_LOG_WS_EVENTS", expectsValue: false },
|
||||
host: { envName: "T3CODE_HOST", expectsValue: true },
|
||||
port: { envName: "T3CODE_PORT", expectsValue: true },
|
||||
"dev-url": { envName: "VITE_DEV_SERVER_URL", expectsValue: true },
|
||||
};
|
||||
|
||||
function parseInteger(value, envName) {
|
||||
const parsed = Number(value);
|
||||
@@ -35,8 +54,8 @@ function hashSeed(seed) {
|
||||
return hash >>> 0;
|
||||
}
|
||||
|
||||
function resolveOffset() {
|
||||
const explicitOffset = process.env.T3CODE_PORT_OFFSET?.trim();
|
||||
export function resolveOffset(env = process.env) {
|
||||
const explicitOffset = env.T3CODE_PORT_OFFSET?.trim();
|
||||
if (explicitOffset) {
|
||||
const parsed = parseInteger(explicitOffset, "T3CODE_PORT_OFFSET");
|
||||
if (parsed < 0) {
|
||||
@@ -45,7 +64,7 @@ function resolveOffset() {
|
||||
return { offset: parsed, source: `T3CODE_PORT_OFFSET=${explicitOffset}` };
|
||||
}
|
||||
|
||||
const seed = process.env.T3CODE_DEV_INSTANCE?.trim();
|
||||
const seed = env.T3CODE_DEV_INSTANCE?.trim();
|
||||
if (!seed) {
|
||||
return { offset: 0, source: "default ports" };
|
||||
}
|
||||
@@ -58,9 +77,140 @@ function resolveOffset() {
|
||||
return { offset, source: `hashed T3CODE_DEV_INSTANCE=${seed}` };
|
||||
}
|
||||
|
||||
function main() {
|
||||
const mode = process.argv[2];
|
||||
const isDryRun = process.argv.includes("--dry-run");
|
||||
function parseLongFlag(raw) {
|
||||
if (!raw.startsWith("--")) return null;
|
||||
const normalized = raw.slice(2);
|
||||
const equalsIndex = normalized.indexOf("=");
|
||||
if (equalsIndex === -1) {
|
||||
return { name: normalized, value: undefined };
|
||||
}
|
||||
return {
|
||||
name: normalized.slice(0, equalsIndex),
|
||||
value: normalized.slice(equalsIndex + 1),
|
||||
};
|
||||
}
|
||||
|
||||
function toBooleanEnvValue(value) {
|
||||
if (value === undefined) return "1";
|
||||
const normalized = value.trim().toLowerCase();
|
||||
if (normalized === "false" || normalized === "0" || normalized === "no") return "0";
|
||||
return "1";
|
||||
}
|
||||
|
||||
export function parseDevRunnerArgs(rawArgs) {
|
||||
const args = rawArgs[0] === "--" ? rawArgs.slice(1) : rawArgs.slice();
|
||||
const envOverrides = {};
|
||||
const turboArgs = [];
|
||||
let isDryRun = false;
|
||||
|
||||
for (let index = 0; index < args.length; index += 1) {
|
||||
const arg = args[index];
|
||||
if (arg === "--dry-run") {
|
||||
isDryRun = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (arg === "--") {
|
||||
turboArgs.push(...args.slice(index + 1));
|
||||
break;
|
||||
}
|
||||
|
||||
const parsedFlag = parseLongFlag(arg);
|
||||
if (!parsedFlag) {
|
||||
turboArgs.push(arg);
|
||||
continue;
|
||||
}
|
||||
|
||||
const definition = FORWARDED_ENV_FLAGS[parsedFlag.name];
|
||||
if (!definition) {
|
||||
turboArgs.push(arg);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (definition.expectsValue) {
|
||||
let value = parsedFlag.value;
|
||||
if (value === undefined) {
|
||||
const next = args[index + 1];
|
||||
if (next === undefined || next === "--" || next.startsWith("-")) {
|
||||
throw new Error(`Missing value for --${parsedFlag.name}`);
|
||||
}
|
||||
value = next;
|
||||
index += 1;
|
||||
}
|
||||
envOverrides[definition.envName] = value;
|
||||
continue;
|
||||
}
|
||||
|
||||
envOverrides[definition.envName] = toBooleanEnvValue(parsedFlag.value);
|
||||
}
|
||||
|
||||
return {
|
||||
isDryRun,
|
||||
envOverrides,
|
||||
turboArgs,
|
||||
};
|
||||
}
|
||||
|
||||
function resolveStateDir(env, envOverrides) {
|
||||
const overrideValue = envOverrides?.T3CODE_STATE_DIR?.trim();
|
||||
if (overrideValue) {
|
||||
return overrideValue;
|
||||
}
|
||||
|
||||
const envValue = env?.T3CODE_STATE_DIR?.trim();
|
||||
if (envValue) {
|
||||
return envValue;
|
||||
}
|
||||
|
||||
return DEFAULT_DEV_STATE_DIR;
|
||||
}
|
||||
|
||||
export function createDevRunnerEnv({ mode, env, offset, envOverrides }) {
|
||||
const serverPort = BASE_SERVER_PORT + offset;
|
||||
const webPort = BASE_WEB_PORT + offset;
|
||||
|
||||
const output = {
|
||||
...env,
|
||||
T3CODE_PORT: String(serverPort),
|
||||
PORT: String(webPort),
|
||||
ELECTRON_RENDERER_PORT: String(webPort),
|
||||
VITE_WS_URL: `ws://localhost:${serverPort}`,
|
||||
VITE_DEV_SERVER_URL: `http://localhost:${webPort}`,
|
||||
...envOverrides,
|
||||
};
|
||||
|
||||
output.T3CODE_STATE_DIR = resolveStateDir(env, envOverrides);
|
||||
|
||||
const parsedServerPort = Number(output.T3CODE_PORT);
|
||||
if (!Number.isInteger(parsedServerPort) || parsedServerPort < 1 || parsedServerPort > 65535) {
|
||||
throw new Error(
|
||||
`Invalid T3CODE_PORT override: '${String(output.T3CODE_PORT)}'. Expected an integer between 1 and 65535.`,
|
||||
);
|
||||
}
|
||||
output.VITE_WS_URL = `ws://localhost:${parsedServerPort}`;
|
||||
|
||||
if (mode === "dev" || mode === "dev:server" || mode === "dev:web") {
|
||||
// Running server/web in browser mode should not inherit desktop launcher state.
|
||||
output.T3CODE_MODE = "web";
|
||||
if (!("T3CODE_NO_BROWSER" in envOverrides)) {
|
||||
delete output.T3CODE_NO_BROWSER;
|
||||
}
|
||||
if (!("T3CODE_AUTH_TOKEN" in envOverrides)) {
|
||||
delete output.T3CODE_AUTH_TOKEN;
|
||||
}
|
||||
delete output.T3CODE_DESKTOP_WS_URL;
|
||||
}
|
||||
|
||||
if (mode === "dev" && !output.T3CODE_LOG_WS_EVENTS) {
|
||||
output.T3CODE_LOG_WS_EVENTS = "1";
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
export function runDevRunner(argv = process.argv) {
|
||||
const mode = argv[2];
|
||||
const parsedArgs = parseDevRunnerArgs(argv.slice(3));
|
||||
if (!mode || !(mode in MODE_ARGS)) {
|
||||
const supportedModes = Object.keys(MODE_ARGS).join(", ");
|
||||
throw new Error(`Usage: bun scripts/dev-runner.mjs <mode>. Supported modes: ${supportedModes}`);
|
||||
@@ -76,37 +226,23 @@ function main() {
|
||||
);
|
||||
}
|
||||
|
||||
const env = {
|
||||
...process.env,
|
||||
T3CODE_PORT: String(serverPort),
|
||||
PORT: String(webPort),
|
||||
ELECTRON_RENDERER_PORT: String(webPort),
|
||||
VITE_WS_URL: `ws://localhost:${serverPort}`,
|
||||
VITE_DEV_SERVER_URL: `http://localhost:${webPort}`,
|
||||
};
|
||||
|
||||
if (mode === "dev" || mode === "dev:server" || mode === "dev:web") {
|
||||
// Running server/web in browser mode should not inherit desktop launcher state.
|
||||
env.T3CODE_MODE = "web";
|
||||
delete env.T3CODE_NO_BROWSER;
|
||||
delete env.T3CODE_AUTH_TOKEN;
|
||||
delete env.T3CODE_DESKTOP_WS_URL;
|
||||
}
|
||||
|
||||
if (mode === "dev" && !env.T3CODE_LOG_WS_EVENTS) {
|
||||
env.T3CODE_LOG_WS_EVENTS = "1";
|
||||
}
|
||||
const env = createDevRunnerEnv({
|
||||
mode,
|
||||
env: process.env,
|
||||
offset,
|
||||
envOverrides: parsedArgs.envOverrides,
|
||||
});
|
||||
|
||||
console.info(
|
||||
`[dev-runner] mode=${mode} source=${source} serverPort=${serverPort} webPort=${webPort}`,
|
||||
`[dev-runner] mode=${mode} source=${source} serverPort=${serverPort} webPort=${webPort} stateDir=${env.T3CODE_STATE_DIR}`,
|
||||
);
|
||||
|
||||
if (isDryRun) {
|
||||
if (parsedArgs.isDryRun) {
|
||||
return;
|
||||
}
|
||||
|
||||
const command = process.platform === "win32" ? "turbo.cmd" : "turbo";
|
||||
const child = spawn(command, MODE_ARGS[mode], {
|
||||
const child = spawn(command, [...MODE_ARGS[mode], ...parsedArgs.turboArgs], {
|
||||
stdio: "inherit",
|
||||
env,
|
||||
});
|
||||
@@ -125,9 +261,13 @@ function main() {
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
main();
|
||||
} catch (error) {
|
||||
console.error("[dev-runner]", error);
|
||||
process.exit(1);
|
||||
const entrypointPath = process.argv[1] ? pathToFileURL(process.argv[1]).href : "";
|
||||
|
||||
if (import.meta.url === entrypointPath) {
|
||||
try {
|
||||
runDevRunner();
|
||||
} catch (error) {
|
||||
console.error("[dev-runner]", error);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
DEFAULT_DEV_STATE_DIR,
|
||||
createDevRunnerEnv,
|
||||
parseDevRunnerArgs,
|
||||
} from "./dev-runner.mjs";
|
||||
|
||||
describe("parseDevRunnerArgs", () => {
|
||||
it("maps supported server flags to env overrides and keeps other flags for turbo", () => {
|
||||
const parsed = parseDevRunnerArgs([
|
||||
"--",
|
||||
"--state-dir",
|
||||
"~/alt-state",
|
||||
"--token=abc123",
|
||||
"--no-browser=false",
|
||||
"--dry-run",
|
||||
"--continue",
|
||||
]);
|
||||
|
||||
expect(parsed.isDryRun).toBe(true);
|
||||
expect(parsed.envOverrides).toEqual({
|
||||
T3CODE_STATE_DIR: "~/alt-state",
|
||||
T3CODE_AUTH_TOKEN: "abc123",
|
||||
T3CODE_NO_BROWSER: "0",
|
||||
});
|
||||
expect(parsed.turboArgs).toEqual(["--continue"]);
|
||||
});
|
||||
|
||||
it("throws when a value flag is missing its value", () => {
|
||||
expect(() => parseDevRunnerArgs(["--state-dir"])).toThrow("Missing value for --state-dir");
|
||||
});
|
||||
|
||||
it("throws when a value flag is followed by another flag token", () => {
|
||||
expect(() => parseDevRunnerArgs(["--state-dir", "--continue"])).toThrow(
|
||||
"Missing value for --state-dir",
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("createDevRunnerEnv", () => {
|
||||
it("defaults state dir to ~/.t3/dev when not provided", () => {
|
||||
const env = createDevRunnerEnv({
|
||||
mode: "dev",
|
||||
env: {},
|
||||
offset: 0,
|
||||
envOverrides: {},
|
||||
});
|
||||
|
||||
expect(env.T3CODE_STATE_DIR).toBe(DEFAULT_DEV_STATE_DIR);
|
||||
});
|
||||
|
||||
it("uses existing env state dir when --state-dir is not provided", () => {
|
||||
const env = createDevRunnerEnv({
|
||||
mode: "dev:server",
|
||||
env: { T3CODE_STATE_DIR: "/tmp/existing-state" },
|
||||
offset: 0,
|
||||
envOverrides: {},
|
||||
});
|
||||
|
||||
expect(env.T3CODE_STATE_DIR).toBe("/tmp/existing-state");
|
||||
});
|
||||
|
||||
it("lets --state-dir override existing env state dir", () => {
|
||||
const env = createDevRunnerEnv({
|
||||
mode: "dev:server",
|
||||
env: { T3CODE_STATE_DIR: "/tmp/existing-state" },
|
||||
offset: 0,
|
||||
envOverrides: { T3CODE_STATE_DIR: "/tmp/override-state" },
|
||||
});
|
||||
|
||||
expect(env.T3CODE_STATE_DIR).toBe("/tmp/override-state");
|
||||
});
|
||||
|
||||
it("treats whitespace-only --state-dir as missing and falls back to env/default", () => {
|
||||
const env = createDevRunnerEnv({
|
||||
mode: "dev:server",
|
||||
env: { T3CODE_STATE_DIR: "/tmp/existing-state" },
|
||||
offset: 0,
|
||||
envOverrides: { T3CODE_STATE_DIR: " " },
|
||||
});
|
||||
|
||||
expect(env.T3CODE_STATE_DIR).toBe("/tmp/existing-state");
|
||||
});
|
||||
|
||||
it("recomputes websocket url when port is overridden", () => {
|
||||
const env = createDevRunnerEnv({
|
||||
mode: "dev",
|
||||
env: {},
|
||||
offset: 0,
|
||||
envOverrides: { T3CODE_PORT: "4222" },
|
||||
});
|
||||
|
||||
expect(env.T3CODE_PORT).toBe("4222");
|
||||
expect(env.VITE_WS_URL).toBe("ws://localhost:4222");
|
||||
});
|
||||
|
||||
it("keeps explicitly forwarded web-mode flags", () => {
|
||||
const env = createDevRunnerEnv({
|
||||
mode: "dev",
|
||||
env: {
|
||||
T3CODE_AUTH_TOKEN: "desktop-token",
|
||||
},
|
||||
offset: 0,
|
||||
envOverrides: {
|
||||
T3CODE_NO_BROWSER: "1",
|
||||
T3CODE_AUTH_TOKEN: "cli-token",
|
||||
},
|
||||
});
|
||||
|
||||
expect(env.T3CODE_NO_BROWSER).toBe("1");
|
||||
expect(env.T3CODE_AUTH_TOKEN).toBe("cli-token");
|
||||
});
|
||||
|
||||
it("fails fast for invalid port overrides", () => {
|
||||
expect(() =>
|
||||
createDevRunnerEnv({
|
||||
mode: "dev",
|
||||
env: {},
|
||||
offset: 0,
|
||||
envOverrides: { T3CODE_PORT: "not-a-port" },
|
||||
}),
|
||||
).toThrow("Invalid T3CODE_PORT override");
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user