Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions .github/fork-seams.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
]
},
"apps/server/src/server.ts": {
"why": "The fork's service layers are wired here in one place each, from fork/forkLayers.ts (#384), so this file keeps as few fork lines as it can. The ten lines in \"added\" were its pending lines (ceiling 10) when it moved here (#361). The announced line gives the runtime's workers upstream's orchestrator with one change: an idle compaction send to a thread whose handoff mode is \"handoff\" is skipped, so the handoff service alone owns that thread (fork/handoff, #361).",
"why": "The fork's service layers are wired here in one place each, from fork/forkLayers.ts (#384), so this file keeps as few fork lines as it can. The ten lines in \"added\" were its pending lines (ceiling 10) when it moved here (#361). The last line in \"added\" gives the runtime's workers upstream's orchestrator with one change: an idle compaction send to a thread whose handoff mode is \"handoff\" is skipped, so the handoff service alone owns that thread (fork/handoff, #361).",
"added": [
"import * as ForkLayers from \"./fork/forkLayers.ts\";",
"// Fork (waits): the `pr` judge reads through the same pull request service and its caches.",
Expand All @@ -42,14 +42,9 @@
"ForkLayers.layerRoutes,",
"const layerWorkAdmission = yield* ForkLayers.makeWorkAdmissionLayer(config);",
"Layer.provide(ForkLayers.holdWorkCloudLink(CloudLink.layer, ConnectReachability.layer)),",
"Layer.provide(Layer.merge(layerActivation, layerWorkAdmission)),"
],
"announced": {
"since": "2026-11-20",
"added": [
"Layer.provide(ForkLayers.handoffThreadsSkipIdleCompaction), // Fork: one owner (fork/handoff)"
]
}
"Layer.provide(Layer.merge(layerActivation, layerWorkAdmission)),",
"Layer.provide(ForkLayers.handoffThreadsSkipIdleCompaction), // Fork: one owner (fork/handoff)"
]
},
"apps/server/src/serverSettings.ts": {
"why": "Every settings write goes through upstream's patch and write paths here. The fork swaps in its patch (fork/serverSettingsPatch.ts, under upstream's name, so a project's MCP profile a client left out is kept) and calls its save check (fork/serverSettingsWriteCheck.ts) once in each write path. The load decodes the file after fork/settingsFileRead.ts (old auto-update names renamed, an unusable interval left out so it reads as its default). The lines from \"import { renameRetiredAutoUpdateKeys }\" on predate this list and record main as it was when every upstream file began to count (#277); they are to move into apps/server/src/fork/ (follow-up), and the file cannot grow meanwhile.",
Expand Down
8 changes: 8 additions & 0 deletions apps/server/src/fork/forkLayers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import * as UsagePacer from "../usage/UsagePacer.ts";
import * as UsageReadingsHistory from "../usage/UsageReadingsHistory.ts";
import * as WorkAdmission from "../workAdmission.ts";
import * as ActivityProbe from "./activityProbe.ts";
import * as HandoffOwner from "./handoff/handoffOwner.ts";
import * as HandoffService from "./handoff/HandoffService.ts";

/** Wraps upstream's agent activity relay: a held server (`serve --hold-work`) publishes none. */
Expand Down Expand Up @@ -43,6 +44,13 @@ export const layerRuntimeServices = Layer.mergeAll(
HandoffService.layer,
);

/**
* Provided to the runtime's core dependencies (server.ts's `layerRuntimeCoreDependenciesBase`, first
* in its pipe, so only its own workers see it): upstream's orchestrator, except that idle compaction
* skips a thread whose handoff mode is `handoff`, which the handoff service alone owns.
*/
export const handoffThreadsSkipIdleCompaction = HandoffOwner.layer;

/** In the routes, after upstream's follower of tasks in linked environments. */
export const layerRoutes = Layer.mergeAll(
// /mcp reads the usage pacer and the slot gate only if they are there (the usage pacing gate,
Expand Down
76 changes: 76 additions & 0 deletions apps/server/src/fork/handoff/HandoffService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
type OrchestrationV2ThreadProjection,
RunId,
RuntimeRequestId,
type ServerSettings as ServerSettingsValue,
} from "@t3tools/contracts";
import * as DateTime from "effect/DateTime";
import * as Deferred from "effect/Deferred";
Expand Down Expand Up @@ -70,6 +71,8 @@ const threadChanged = (data: OrchestrationV2ThreadProjection) =>
const harness = (state: {
data: OrchestrationV2ThreadProjection;
settings: Partial<HandoffSettings>;
/** The thread's project override (over the defaults), or none. */
project?: Partial<HandoffSettings> | undefined;
/** Runs inside the mocked dispatch, after the command is recorded. */
onDispatch?: (command: OrchestrationV2ServerCommand) => Effect.Effect<void>;
}) =>
Expand Down Expand Up @@ -120,6 +123,14 @@ const harness = (state: {
getSettings: Effect.sync(() => ({
...DEFAULT_SERVER_SETTINGS,
handoff: { ...DEFAULT_HANDOFF_SETTINGS, ...state.settings },
projectSettingsOverrides:
state.project === undefined
? {}
: ({
[state.data.thread.projectId]: {
handoff: { ...DEFAULT_HANDOFF_SETTINGS, ...state.project },
},
} as unknown as ServerSettingsValue["projectSettingsOverrides"]),
})),
}),
);
Expand Down Expand Up @@ -286,6 +297,71 @@ describe("HandoffService", () => {
}),
);

it.effect.each([
{ server: on, project: { mode: "off" }, sends: 0 },
{ server: on, project: { mode: "compact" }, sends: 0 },
{ server: {}, project: on, sends: 1 },
// The project's own thread entry wins over its mode, as for the server's.
{ server: on, project: { mode: "off", threads: { [threadId]: "handoff" } }, sends: 1 },
{ server: on, project: { mode: "handoff", threads: { [threadId]: "off" } }, sends: 0 },
] as ReadonlyArray<{
server: Partial<HandoffSettings>;
project: Partial<HandoffSettings>;
sends: number;
}>)(
"a project's own setting decides for its threads, past the size limit (sends $sends)",
({ server, project, sends }) =>
Effect.gen(function* () {
const state = { data: projection({ usedTokens: 400_000 }), settings: server, project };
const { sent, emit, reads } = yield* harness(state);
yield* emit(runUpdated(run()), "handOffWhenDue", 1);
expect(messages(sent)).toHaveLength(sends);
// Off for the thread reads nothing more than its settings.
if (sends === 0) expect(reads.count).toBe(0);
}),
);

it.effect(
"its project switched off while the handoff waits on a busy thread: sends nothing",
() =>
Effect.gen(function* () {
const quiet = projection({ usedTokens: 160_000 });
const state = {
data: {
...quiet,
runtimeRequests: [
{ id: RuntimeRequestId.make("request-1"), status: "pending" },
] as unknown as OrchestrationV2ThreadProjection["runtimeRequests"],
},
settings: on,
project: undefined as Partial<HandoffSettings> | undefined,
};
const { sent, emit } = yield* harness(state);
yield* emit(runUpdated(run()), "readThread", 1);
state.project = { mode: "off" };
state.data = quiet;
yield* emit(threadChanged(state.data), "handOffWhenDue", 1);
expect(sent).toEqual([]);
}),
);

it.effect("its project switched off while the idle timer sleeps: sends nothing", () =>
Effect.gen(function* () {
const state = {
data: projection({ usedTokens: 100_000, cacheExpiresInMs: 10 * MINUTE_MS }),
settings: on,
project: undefined as Partial<HandoffSettings> | undefined,
};
const { sent, emit } = yield* harness(state);
yield* emit(runUpdated(run()), "readThread", 1);
yield* TestClock.adjust(MINUTE_MS);
state.project = { mode: "off" };
yield* TestClock.adjust(10 * MINUTE_MS);
yield* emit(null, "handOffWhenDue", 1);
expect(sent).toEqual([]);
}),
);

it.effect.each([
{ read: true, visits: 1 },
{ read: false, visits: 0 },
Expand Down
8 changes: 2 additions & 6 deletions apps/server/src/fork/handoff/HandoffService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
CommandId,
MessageId,
type OrchestrationV2Run,
resolveHandoffSettings,
type RunId,
type ThreadId,
} from "@t3tools/contracts";
Expand All @@ -28,6 +27,7 @@ import { type HandoffTrigger, handoffDecision } from "./handoffDecision.ts";
import { dispatchHandoff, readHandoffRecords } from "./handoffDispatch.ts";
import { HANDOFF_RETRY_SUFFIX, handoffThreadFacts, parseHandoffMessageId } from "./handoffFacts.ts";
import { handoffMessageText } from "./handoffMessage.ts";
import { threadHandoffSettings } from "./handoffOwner.ts";

/** Part 3 reads the turn items of this many latest runs; the budget keeps the newest of them. */
const TRANSCRIPT_RUNS = 20;
Expand All @@ -47,11 +47,7 @@ export const make = Effect.gen(function* () {
const readThread = Effect.fn("HandoffService.readThread")(function* (threadId: ThreadId) {
const current = yield* serverSettings.getSettings;
const thread = yield* projections.getThread(threadId);
const resolved = resolveHandoffSettings(
current.handoff,
current.projectSettingsOverrides[thread.projectId]?.handoff,
threadId,
);
const resolved = threadHandoffSettings(current, thread.projectId, threadId);
// Off (the default) and `compact` read nothing more.
if (resolved.mode !== "handoff") return null;
return { ...resolved, records: yield* withStores(readHandoffRecords(threadId)) };
Expand Down
Loading
Loading