Five Macroscope findings on the media push, all confirmed against the code: - adapter.py _start_turn: a failure in the awaits after registering the turn left a phantom entry that rejected every later turn.start on the thread and advertised a stale activeTurnId on reconnect. The entry is now removed on any exception, guarded on identity so a replacement installed by an error path keeps ownership. - home.py queue: a transient read error made append() see an empty queue and rewrite the file, destroying every unacknowledged delivery. Read errors now propagate; append falls back to a single O_APPEND write (cap briefly unenforced — recoverable; a destroyed outbox is not), purge declines to rewrite (the entry replays once and dedupes server-side). - adapter.py deliveries (text and media): the queue append's return was discarded, so an unpersisted delivery reported success — and media could complete its turn — even when the live send also failed. Success now requires either leg: queued-and-unsent rides the next connect, sent-but-unqueued is already at T3. - home.py permissions: the outbox (which now carries base64 media payloads, not just text) was 0644 in 0755 under a default umask. Queue file, temp file, append path, and lock sidecar are 0600 in 0700. - hermesGatewayHttp dedupe: alreadyDelivered read through getThreadDetailById, which filters archived threads — an archived Home with a failed best-effort unarchive made every retry of a deliveryId append a fresh duplicate. Dedupe now uses a purpose-built hasThreadNotificationDelivery on projection_thread_messages with no archive filter and json_extract on the deliveryId, so a match in a message body cannot false-positive. Skipped (documented on the PR): Windows lock no-op (unsupported host, pre-existing), whole-file read in build_media_delivery (the read is the 25MB check), MAX_FLUSH_PER_CONNECT bound (deliberate), bytes-based queue cap (design change nothing warrants yet). Plugin tests 150 across four files; server suites green. Co-Authored-By: Claude Fable 5 <[email protected]>
291 lines
12 KiB
TypeScript
291 lines
12 KiB
TypeScript
import * as NodeServices from "@effect/platform-node/NodeServices";
|
|
import { DEFAULT_MODEL, ProjectId, ProviderInstanceId, ThreadId } from "@t3tools/contracts";
|
|
import { assert, it } from "@effect/vitest";
|
|
import * as Crypto from "effect/Crypto";
|
|
import * as Deferred from "effect/Deferred";
|
|
import * as Effect from "effect/Effect";
|
|
import * as Fiber from "effect/Fiber";
|
|
import * as Option from "effect/Option";
|
|
import * as PlatformError from "effect/PlatformError";
|
|
import * as Ref from "effect/Ref";
|
|
import * as Stream from "effect/Stream";
|
|
|
|
import * as ServerConfig from "./config.ts";
|
|
import * as OrchestrationEngine from "./orchestration/Services/OrchestrationEngine.ts";
|
|
import * as ProjectionSnapshotQuery from "./orchestration/Services/ProjectionSnapshotQuery.ts";
|
|
import * as AnalyticsService from "./telemetry/AnalyticsService.ts";
|
|
import * as ServerRuntimeStartup from "./serverRuntimeStartup.ts";
|
|
|
|
it("uses the canonical Codex default for auto-bootstrapped model selection", () => {
|
|
assert.deepStrictEqual(ServerRuntimeStartup.getAutoBootstrapDefaultModelSelection(), {
|
|
instanceId: ProviderInstanceId.make("codex"),
|
|
model: DEFAULT_MODEL,
|
|
});
|
|
});
|
|
|
|
it.effect("enqueueCommand waits for readiness and then drains queued work", () =>
|
|
Effect.scoped(
|
|
Effect.gen(function* () {
|
|
const executionCount = yield* Ref.make(0);
|
|
const commandGate = yield* ServerRuntimeStartup.makeCommandGate;
|
|
|
|
const queuedCommandFiber = yield* commandGate
|
|
.enqueueCommand(Ref.updateAndGet(executionCount, (count) => count + 1))
|
|
.pipe(Effect.forkScoped);
|
|
|
|
yield* Effect.yieldNow;
|
|
assert.equal(yield* Ref.get(executionCount), 0);
|
|
|
|
yield* commandGate.signalCommandReady;
|
|
|
|
const result = yield* Fiber.join(queuedCommandFiber);
|
|
assert.equal(result, 1);
|
|
assert.equal(yield* Ref.get(executionCount), 1);
|
|
}),
|
|
),
|
|
);
|
|
|
|
it.effect("enqueueCommand fails queued work when readiness fails", () =>
|
|
Effect.scoped(
|
|
Effect.gen(function* () {
|
|
const commandGate = yield* ServerRuntimeStartup.makeCommandGate;
|
|
const failure = yield* Deferred.make<void, never>();
|
|
|
|
const queuedCommandFiber = yield* commandGate
|
|
.enqueueCommand(Deferred.await(failure).pipe(Effect.as("should-not-run")))
|
|
.pipe(Effect.forkScoped);
|
|
|
|
yield* commandGate.failCommandReady(
|
|
new ServerRuntimeStartup.ServerRuntimeStartupError({
|
|
mode: "web",
|
|
host: "127.0.0.1",
|
|
port: 3773,
|
|
cause: new Error("test startup failure"),
|
|
}),
|
|
);
|
|
|
|
const error = yield* Effect.flip(Fiber.join(queuedCommandFiber));
|
|
assert.equal(error.message, "Server runtime startup failed before command readiness.");
|
|
}),
|
|
),
|
|
);
|
|
|
|
it.effect("launchStartupHeartbeat does not block the caller while counts are loading", () =>
|
|
Effect.scoped(
|
|
Effect.gen(function* () {
|
|
const releaseCounts = yield* Deferred.make<void, never>();
|
|
|
|
yield* ServerRuntimeStartup.launchStartupHeartbeat.pipe(
|
|
Effect.provideService(ProjectionSnapshotQuery.ProjectionSnapshotQuery, {
|
|
getCommandReadModel: () => Effect.die("unused"),
|
|
getSnapshot: () => Effect.die("unused"),
|
|
getShellSnapshot: () => Effect.die("unused"),
|
|
getArchivedShellSnapshot: () => Effect.die("unused"),
|
|
getSnapshotSequence: () => Effect.die("unused"),
|
|
getCounts: () =>
|
|
Deferred.await(releaseCounts).pipe(
|
|
Effect.as({
|
|
projectCount: 2,
|
|
threadCount: 3,
|
|
}),
|
|
),
|
|
getActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),
|
|
getActiveAgentProject: () => Effect.succeed(Option.none()),
|
|
getProjectShellById: () => Effect.succeed(Option.none()),
|
|
getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
|
|
getThreadCheckpointContext: () => Effect.succeed(Option.none()),
|
|
getFullThreadDiffContext: () => Effect.succeed(Option.none()),
|
|
getThreadShellById: () => Effect.succeed(Option.none()),
|
|
getThreadArchiveStateById: () => Effect.succeed(Option.none()),
|
|
hasThreadNotificationDelivery: () => Effect.succeed(false),
|
|
getThreadDetailById: () => Effect.succeed(Option.none()),
|
|
getThreadDetailSnapshot: () => Effect.succeed(Option.none()),
|
|
}),
|
|
Effect.provideService(AnalyticsService.AnalyticsService, {
|
|
record: () => Effect.void,
|
|
flush: Effect.void,
|
|
}),
|
|
);
|
|
}),
|
|
),
|
|
);
|
|
|
|
it.effect("resolveWelcomeBase derives cwd and project name from server config", () =>
|
|
Effect.gen(function* () {
|
|
const welcome = yield* ServerRuntimeStartup.resolveWelcomeBase.pipe(
|
|
Effect.provideService(ServerConfig.ServerConfig, {
|
|
cwd: "/tmp/startup-project",
|
|
} as never),
|
|
);
|
|
|
|
assert.deepStrictEqual(welcome, {
|
|
cwd: "/tmp/startup-project",
|
|
projectName: "startup-project",
|
|
});
|
|
}),
|
|
);
|
|
|
|
it.effect("resolveAutoBootstrapWelcomeTargets returns existing project and thread ids", () => {
|
|
const bootstrapProjectId = ProjectId.make("project-startup-bootstrap");
|
|
const bootstrapThreadId = ThreadId.make("thread-startup-bootstrap");
|
|
|
|
return Effect.gen(function* () {
|
|
const dispatchCalls = yield* Ref.make<ReadonlyArray<string>>([]);
|
|
const targets = yield* ServerRuntimeStartup.resolveAutoBootstrapWelcomeTargets.pipe(
|
|
Effect.provideService(ServerConfig.ServerConfig, {
|
|
cwd: "/tmp/startup-project",
|
|
autoBootstrapProjectFromCwd: true,
|
|
} as never),
|
|
Effect.provideService(ProjectionSnapshotQuery.ProjectionSnapshotQuery, {
|
|
getCommandReadModel: () => Effect.die("unused"),
|
|
getSnapshot: () => Effect.die("unused"),
|
|
getShellSnapshot: () => Effect.die("unused"),
|
|
getArchivedShellSnapshot: () => Effect.die("unused"),
|
|
getSnapshotSequence: () => Effect.die("unused"),
|
|
getCounts: () => Effect.die("unused"),
|
|
getActiveProjectByWorkspaceRoot: () =>
|
|
Effect.succeed(
|
|
Option.some({
|
|
id: bootstrapProjectId,
|
|
title: "Startup Project",
|
|
workspaceRoot: "/tmp/startup-project",
|
|
defaultModelSelection: ServerRuntimeStartup.getAutoBootstrapDefaultModelSelection(),
|
|
scripts: [],
|
|
agentInstanceId: null,
|
|
createdAt: "2026-01-01T00:00:00.000Z",
|
|
updatedAt: "2026-01-01T00:00:00.000Z",
|
|
deletedAt: null,
|
|
}),
|
|
),
|
|
getActiveAgentProject: () => Effect.succeed(Option.none()),
|
|
getProjectShellById: () => Effect.die("unused"),
|
|
getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.some(bootstrapThreadId)),
|
|
getThreadCheckpointContext: () => Effect.succeed(Option.none()),
|
|
getFullThreadDiffContext: () => Effect.succeed(Option.none()),
|
|
getThreadShellById: () => Effect.die("unused"),
|
|
getThreadArchiveStateById: () => Effect.die("unused"),
|
|
hasThreadNotificationDelivery: () => Effect.die("unused"),
|
|
getThreadDetailById: () => Effect.die("unused"),
|
|
getThreadDetailSnapshot: () => Effect.die("unused"),
|
|
}),
|
|
Effect.provideService(OrchestrationEngine.OrchestrationEngineService, {
|
|
readEvents: () => Stream.empty,
|
|
dispatch: (command) =>
|
|
Ref.update(dispatchCalls, (calls) => [...calls, command.type]).pipe(
|
|
Effect.as({ sequence: 1 }),
|
|
),
|
|
streamDomainEvents: Stream.empty,
|
|
latestSequence: Effect.succeed(0),
|
|
} satisfies OrchestrationEngine.OrchestrationEngineService["Service"]),
|
|
Effect.provide(NodeServices.layer),
|
|
);
|
|
|
|
assert.deepStrictEqual(targets, {
|
|
bootstrapProjectId,
|
|
bootstrapThreadId,
|
|
});
|
|
assert.deepStrictEqual(yield* Ref.get(dispatchCalls), []);
|
|
});
|
|
});
|
|
|
|
it.effect("resolveAutoBootstrapWelcomeTargets creates a project and thread when missing", () =>
|
|
Effect.gen(function* () {
|
|
const dispatchCalls = yield* Ref.make<ReadonlyArray<string>>([]);
|
|
const targets = yield* ServerRuntimeStartup.resolveAutoBootstrapWelcomeTargets.pipe(
|
|
Effect.provideService(ServerConfig.ServerConfig, {
|
|
cwd: "/tmp/startup-project",
|
|
autoBootstrapProjectFromCwd: true,
|
|
} as never),
|
|
Effect.provideService(ProjectionSnapshotQuery.ProjectionSnapshotQuery, {
|
|
getCommandReadModel: () => Effect.die("unused"),
|
|
getSnapshot: () => Effect.die("unused"),
|
|
getShellSnapshot: () => Effect.die("unused"),
|
|
getArchivedShellSnapshot: () => Effect.die("unused"),
|
|
getSnapshotSequence: () => Effect.die("unused"),
|
|
getCounts: () => Effect.die("unused"),
|
|
getActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),
|
|
getActiveAgentProject: () => Effect.succeed(Option.none()),
|
|
getProjectShellById: () => Effect.die("unused"),
|
|
getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
|
|
getThreadCheckpointContext: () => Effect.succeed(Option.none()),
|
|
getFullThreadDiffContext: () => Effect.succeed(Option.none()),
|
|
getThreadShellById: () => Effect.die("unused"),
|
|
getThreadArchiveStateById: () => Effect.die("unused"),
|
|
hasThreadNotificationDelivery: () => Effect.die("unused"),
|
|
getThreadDetailById: () => Effect.die("unused"),
|
|
getThreadDetailSnapshot: () => Effect.die("unused"),
|
|
}),
|
|
Effect.provideService(OrchestrationEngine.OrchestrationEngineService, {
|
|
readEvents: () => Stream.empty,
|
|
dispatch: (command) =>
|
|
Ref.update(dispatchCalls, (calls) => [...calls, command.type]).pipe(
|
|
Effect.as({ sequence: 1 }),
|
|
),
|
|
streamDomainEvents: Stream.empty,
|
|
latestSequence: Effect.succeed(0),
|
|
} satisfies OrchestrationEngine.OrchestrationEngineService["Service"]),
|
|
Effect.provide(NodeServices.layer),
|
|
);
|
|
|
|
assert.equal(typeof targets.bootstrapProjectId, "string");
|
|
assert.equal(typeof targets.bootstrapThreadId, "string");
|
|
assert.deepStrictEqual(yield* Ref.get(dispatchCalls), ["project.create", "thread.create"]);
|
|
}),
|
|
);
|
|
|
|
it.effect("resolveAutoBootstrapWelcomeTargets preserves typed UUID generation failures", () =>
|
|
Effect.gen(function* () {
|
|
const crypto = yield* Crypto.Crypto;
|
|
const uuidError = PlatformError.systemError({
|
|
_tag: "Unknown",
|
|
module: "Crypto",
|
|
method: "randomUUIDv4",
|
|
description: "UUID generation unavailable",
|
|
});
|
|
const dispatchCalls = yield* Ref.make<ReadonlyArray<string>>([]);
|
|
|
|
const error = yield* ServerRuntimeStartup.resolveAutoBootstrapWelcomeTargets.pipe(
|
|
Effect.provideService(ServerConfig.ServerConfig, {
|
|
cwd: "/tmp/startup-project",
|
|
autoBootstrapProjectFromCwd: true,
|
|
} as never),
|
|
Effect.provideService(ProjectionSnapshotQuery.ProjectionSnapshotQuery, {
|
|
getCommandReadModel: () => Effect.die("unused"),
|
|
getSnapshot: () => Effect.die("unused"),
|
|
getShellSnapshot: () => Effect.die("unused"),
|
|
getArchivedShellSnapshot: () => Effect.die("unused"),
|
|
getSnapshotSequence: () => Effect.die("unused"),
|
|
getCounts: () => Effect.die("unused"),
|
|
getActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),
|
|
getActiveAgentProject: () => Effect.succeed(Option.none()),
|
|
getProjectShellById: () => Effect.die("unused"),
|
|
getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
|
|
getThreadCheckpointContext: () => Effect.succeed(Option.none()),
|
|
getFullThreadDiffContext: () => Effect.succeed(Option.none()),
|
|
getThreadShellById: () => Effect.die("unused"),
|
|
getThreadArchiveStateById: () => Effect.die("unused"),
|
|
hasThreadNotificationDelivery: () => Effect.die("unused"),
|
|
getThreadDetailById: () => Effect.die("unused"),
|
|
getThreadDetailSnapshot: () => Effect.die("unused"),
|
|
}),
|
|
Effect.provideService(OrchestrationEngine.OrchestrationEngineService, {
|
|
readEvents: () => Stream.empty,
|
|
dispatch: (command) =>
|
|
Ref.update(dispatchCalls, (calls) => [...calls, command.type]).pipe(
|
|
Effect.as({ sequence: 1 }),
|
|
),
|
|
streamDomainEvents: Stream.empty,
|
|
latestSequence: Effect.succeed(0),
|
|
} satisfies OrchestrationEngine.OrchestrationEngineService["Service"]),
|
|
Effect.provideService(Crypto.Crypto, {
|
|
...crypto,
|
|
randomUUIDv4: Effect.fail(uuidError),
|
|
}),
|
|
Effect.flip,
|
|
);
|
|
|
|
assert.strictEqual(error, uuidError);
|
|
assert.deepStrictEqual(yield* Ref.get(dispatchCalls), []);
|
|
}).pipe(Effect.provide(NodeServices.layer)),
|
|
);
|