Address dev utility review findings
Co-Authored-By: Claude <[email protected]>
This commit is contained in:
10 files changed
+148
-26
No files matched your search
@@ -730,12 +730,6 @@ export function runDevRunnerWithInput(input: DevRunnerCliInput) {
|
||||
// with creating it. An interrupt landing in between would otherwise
|
||||
// leave a mapping pointing at a port nothing is listening on.
|
||||
//
|
||||
// Deliberately no ownership tracking beyond that: if a second runner
|
||||
// takes this port during a fast restart, the first's exit can briefly
|
||||
// tear down the new mapping — visible (the URL stops working) and fixed
|
||||
// by re-running --share. A lease protocol closing that window existed
|
||||
// and was removed as more machinery than a dev convenience warrants.
|
||||
//
|
||||
// A tailnet that isn't up shouldn't stop the dev server from starting —
|
||||
// warn, and carry on serving locally.
|
||||
const shared = yield* Effect.gen(function* () {
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
// @effect-diagnostics nodeBuiltinImport:off - exercises the executable path guard in a child process.
|
||||
import { assert, it } from "@effect/vitest";
|
||||
import * as NodeChildProcess from "node:child_process";
|
||||
import * as NodeFS from "node:fs";
|
||||
import * as NodeOS from "node:os";
|
||||
import * as NodePath from "node:path";
|
||||
|
||||
it("refuses to seed a target inside T3CODE_HOME through a symlink", () => {
|
||||
const root = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-dev-seed-cli-"));
|
||||
try {
|
||||
const sharedHome = NodePath.join(root, "shared");
|
||||
const sharedStateDir = NodePath.join(sharedHome, "userdata");
|
||||
const targetBaseDir = NodePath.join(root, "target");
|
||||
NodeFS.mkdirSync(sharedStateDir, { recursive: true });
|
||||
NodeFS.mkdirSync(targetBaseDir, { recursive: true });
|
||||
NodeFS.writeFileSync(NodePath.join(sharedStateDir, "state.sqlite"), "must stay untouched");
|
||||
NodeFS.symlinkSync(sharedStateDir, NodePath.join(targetBaseDir, "userdata"), "dir");
|
||||
|
||||
const result = NodeChildProcess.spawnSync(
|
||||
process.execPath,
|
||||
[
|
||||
NodePath.join(import.meta.dirname, "dev-seed.ts"),
|
||||
"--from",
|
||||
NodePath.join(root, "source"),
|
||||
"--to",
|
||||
targetBaseDir,
|
||||
],
|
||||
{
|
||||
encoding: "utf8",
|
||||
env: { ...process.env, NO_COLOR: "1", T3CODE_HOME: sharedHome },
|
||||
},
|
||||
);
|
||||
|
||||
assert.notEqual(result.status, 0);
|
||||
assert.include(
|
||||
`${result.stdout}${result.stderr}`,
|
||||
"that database lives in the shared T3 Code home",
|
||||
);
|
||||
assert.equal(
|
||||
NodeFS.readFileSync(NodePath.join(sharedStateDir, "state.sqlite"), "utf8"),
|
||||
"must stay untouched",
|
||||
);
|
||||
} finally {
|
||||
NodeFS.rmSync(root, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
+17
-6
@@ -23,6 +23,21 @@ import { findRunningServerPid, findRunningServerPidSync } from "./lib/server-run
|
||||
const DEFAULT_THREAD_LIMIT = 25;
|
||||
const DEFAULT_ACTIVITY_LIMIT = 200;
|
||||
|
||||
const resolveSharedHome = (path: Path.Path): string => {
|
||||
const configured = process.env.T3CODE_HOME?.trim();
|
||||
if (!configured) {
|
||||
return path.join(NodeOS.homedir(), ".t3");
|
||||
}
|
||||
if (configured === "~") {
|
||||
return NodeOS.homedir();
|
||||
}
|
||||
return path.resolve(
|
||||
configured.startsWith("~/") || configured.startsWith("~\\")
|
||||
? path.join(NodeOS.homedir(), configured.slice(2))
|
||||
: configured,
|
||||
);
|
||||
};
|
||||
|
||||
class DevSeedTargetError extends Schema.TaggedErrorClass<DevSeedTargetError>()(
|
||||
"DevSeedTargetError",
|
||||
{
|
||||
@@ -156,10 +171,6 @@ const copyAttachments = Effect.fn("devSeed.copyAttachments")(function* (input: {
|
||||
const entries = yield* fileSystem
|
||||
.readDirectory(input.sourceDir)
|
||||
.pipe(Effect.orElseSucceed(() => [] as Array<string>));
|
||||
if (entries.length === 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const wanted = new Set(input.attachmentIds);
|
||||
const matches = entries.filter((entry) => wanted.has(path.parse(entry).name));
|
||||
if (matches.length === 0) {
|
||||
@@ -188,7 +199,7 @@ const copyAttachments = Effect.fn("devSeed.copyAttachments")(function* (input: {
|
||||
const devSeedCli = Command.make("dev-seed", {
|
||||
from: Flag.string("from").pipe(
|
||||
Flag.withDescription(
|
||||
"Base directory to copy from (default: the shared T3 Code home, ~/.t3). Read-only.",
|
||||
"Base directory to copy from (default: T3CODE_HOME, or ~/.t3). Read-only.",
|
||||
),
|
||||
Flag.optional,
|
||||
Flag.map(Option.getOrUndefined),
|
||||
@@ -229,7 +240,7 @@ const devSeedCli = Command.make("dev-seed", {
|
||||
const fileSystem = yield* FileSystem.FileSystem;
|
||||
const path = yield* Path.Path;
|
||||
|
||||
const sharedHome = path.join(NodeOS.homedir(), ".t3");
|
||||
const sharedHome = resolveSharedHome(path);
|
||||
const sourceBaseDir = input.from ? path.resolve(input.from) : sharedHome;
|
||||
|
||||
const defaultTarget = yield* resolveWorktreeT3Home(process.cwd());
|
||||
|
||||
@@ -232,7 +232,7 @@ function copyRows(input: {
|
||||
// at the limit instead of ranking every row.
|
||||
const statement = input.source.prepare(
|
||||
`SELECT ${selectList} FROM ${input.table} WHERE "${input.keyColumn}" = ?
|
||||
ORDER BY ${input.perKeyLimit.orderBy} DESC LIMIT ?`,
|
||||
ORDER BY ${input.perKeyLimit.orderBy} LIMIT ?`,
|
||||
);
|
||||
for (const key of input.keys) {
|
||||
insertFrom(statement.iterate(key, input.perKeyLimit.limit));
|
||||
@@ -590,8 +590,8 @@ export function seedDevDatabase(options: DevSeedOptions): DevSeedSummary {
|
||||
// back to the timestamp — same drift tolerance as the rest of the copy.
|
||||
perKeyLimit: {
|
||||
orderBy: hasColumn(source, "projection_thread_activities", "sequence")
|
||||
? `"sequence" DESC, "created_at" DESC, "activity_id"`
|
||||
: `"created_at" DESC, "activity_id"`,
|
||||
? `"sequence" DESC, "created_at" DESC, "activity_id" DESC`
|
||||
: `"created_at" DESC, "activity_id" DESC`,
|
||||
limit: options.activityLimit,
|
||||
},
|
||||
}),
|
||||
|
||||
@@ -192,6 +192,40 @@ describe("shareDevServer", () => {
|
||||
});
|
||||
|
||||
it.layer(NodeServices.layer)("dev share cleanup ownership", (it) => {
|
||||
it.effect("claims ownership via an atomic rename", () =>
|
||||
Effect.gen(function* () {
|
||||
const leasePath = "/leases/5788.owner";
|
||||
const tempDirectory = "/leases/5788.owner.temp";
|
||||
const writes: Array<readonly [path: string, contents: string]> = [];
|
||||
const renames: Array<readonly [from: string, to: string]> = [];
|
||||
|
||||
yield* claimDevShareLease({ leasePath, ownerId: "new-runner", webPort: 5788 }).pipe(
|
||||
Effect.provideService(
|
||||
FileSystem.FileSystem,
|
||||
FileSystem.makeNoop({
|
||||
makeDirectory: () => Effect.void,
|
||||
makeTempDirectoryScoped: (options) => {
|
||||
assert.equal(options?.directory, "/leases");
|
||||
assert.equal(options?.prefix, "5788.owner.");
|
||||
return Effect.succeed(tempDirectory);
|
||||
},
|
||||
writeFileString: (path, contents) => {
|
||||
writes.push([path, contents]);
|
||||
return Effect.void;
|
||||
},
|
||||
rename: (from, to) => {
|
||||
renames.push([from, to]);
|
||||
return Effect.void;
|
||||
},
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
assert.deepEqual(writes, [[`${tempDirectory}/owner.tmp`, "new-runner"]]);
|
||||
assert.deepEqual(renames, [[`${tempDirectory}/owner.tmp`, leasePath]]);
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("keeps the prior owner when clearing its mapping fails", () =>
|
||||
Effect.gen(function* () {
|
||||
const fileSystem = yield* FileSystem.FileSystem;
|
||||
|
||||
@@ -199,8 +199,19 @@ export const claimDevShareLease = Effect.fn("devShare.claimDevShareLease")(funct
|
||||
) {
|
||||
const fileSystem = yield* FileSystem.FileSystem;
|
||||
const path = yield* Path.Path;
|
||||
yield* fileSystem.makeDirectory(path.dirname(lease.leasePath), { recursive: true }).pipe(
|
||||
Effect.andThen(fileSystem.writeFileString(lease.leasePath, lease.ownerId)),
|
||||
yield* Effect.scoped(
|
||||
Effect.gen(function* () {
|
||||
const leaseDirectory = path.dirname(lease.leasePath);
|
||||
yield* fileSystem.makeDirectory(leaseDirectory, { recursive: true });
|
||||
const tempDirectory = yield* fileSystem.makeTempDirectoryScoped({
|
||||
directory: leaseDirectory,
|
||||
prefix: `${path.basename(lease.leasePath)}.`,
|
||||
});
|
||||
const tempPath = path.join(tempDirectory, "owner.tmp");
|
||||
yield* fileSystem.writeFileString(tempPath, lease.ownerId);
|
||||
yield* fileSystem.rename(tempPath, lease.leasePath);
|
||||
}),
|
||||
).pipe(
|
||||
Effect.mapError((cause) => new DevShareLeaseClaimError({ leasePath: lease.leasePath, cause })),
|
||||
);
|
||||
return lease;
|
||||
|
||||
Reference in new issue
Block a user