diff --git a/.github/fork-seams.json b/.github/fork-seams.json index 4a577ac4b153e..b14dee515a06d 100644 --- a/.github/fork-seams.json +++ b/.github/fork-seams.json @@ -105,7 +105,7 @@ "apps/server/src/scheduling/DueHint.ts": 5, "apps/server/src/scheduling/Scheduler.test.ts": 24, "apps/server/src/scheduling/Scheduler.ts": 13, - "apps/server/src/server.ts": 39, + "apps/server/src/server.ts": 10, "apps/server/src/serverRuntimeStartup.ts": 54, "apps/server/src/serverRuntimeState.test.ts": 21, "apps/server/src/serverRuntimeState.ts": 4, @@ -173,6 +173,8 @@ "apps/web/src/components/settings/WaitsAndSlotsSettings*", "apps/web/src/components/settings/waitsAndSlotsSettings*", "packages/contracts/src/editor.test.ts", + "packages/contracts/src/handoff.test.ts", + "packages/contracts/src/handoff.ts", "packages/contracts/src/mcpProfile*", "packages/contracts/src/playtest*", "packages/contracts/src/usagePacing.ts", diff --git a/FORK_CHANGES.md b/FORK_CHANGES.md index ba9c7a418cb02..16d672976657c 100644 --- a/FORK_CHANGES.md +++ b/FORK_CHANGES.md @@ -25,6 +25,7 @@ is missing here or a setting named here does not exist. | Waits: agents can wait for a pull request, a file, a system unit, a named check command or a free agent slot (`wait_for`, `wait_list`, `wait_cancel`), and are woken when it happens. | An agent no longer polls or sleeps while it waits. | `server.waitFor`, `server.waitChecks`, `projectOverride.waitChecks` | off | [#84](https://github.com/scuttletoby/t3code/pull/84), [#90](https://github.com/scuttletoby/t3code/pull/90), [#91](https://github.com/scuttletoby/t3code/pull/91), [#103](https://github.com/scuttletoby/t3code/pull/103), [#106](https://github.com/scuttletoby/t3code/pull/106), [#109](https://github.com/scuttletoby/t3code/pull/109), [#110](https://github.com/scuttletoby/t3code/pull/110), [#116](https://github.com/scuttletoby/t3code/pull/116), [#119](https://github.com/scuttletoby/t3code/pull/119) | | Settings pages for waits, wait checks and agent slots. | Switch these on without editing a file. | `server.waitFor`, `server.waitChecks`, `server.agentSlots` | off | [#104](https://github.com/scuttletoby/t3code/pull/104) | | The fork's checks on `delegate_task` (pacing, slots) sit behind one fork module. | Fewer fork lines in upstream's code, so syncs conflict less. No change in behaviour. | none | always | [#174](https://github.com/scuttletoby/t3code/pull/174) | +| The fork's service layers in the server are wired from one fork file (`apps/server/src/fork/forkLayers.ts`). | Fewer fork lines in upstream's code, so syncs conflict less. No change in behaviour. | none | always | [#384](https://github.com/scuttletoby/t3code/pull/384) | | A pure capacity rule for "dynamic slots" (one budget per machine), with shared test cases. Not wired into anything yet. | A later step replaces fixed agent counts with what the machine can take. | none | not used | [#203](https://github.com/scuttletoby/t3code/pull/203) | | File links in chat open in the files viewer, full size, or in an editor on a remote machine, and can be downloaded, from their menu. | File links were only copyable on remote machines and had no menu on touch screens. | none | always | [#17](https://github.com/scuttletoby/t3code/pull/17) | | A hidden read-only command, `t3 __fork-report --base-dir `, prints what a T3 home holds and which fork features are on. | The go-live kit checks a build and a home before switching, without writing anything. | none | only when run | [#172](https://github.com/scuttletoby/t3code/pull/172), [#184](https://github.com/scuttletoby/t3code/pull/184) | diff --git a/apps/server/src/fork/forkLayers.ts b/apps/server/src/fork/forkLayers.ts new file mode 100644 index 0000000000000..fe39cb4239bf8 --- /dev/null +++ b/apps/server/src/fork/forkLayers.ts @@ -0,0 +1,81 @@ +/** + * Fork: the fork's own service layers, wired into upstream's server.ts in one place each, so that + * file keeps as few fork lines as it can (.github/fork-seams.json). Each export says where it goes; + * moving it keeps the same layers, built in the same places, as when they were inline there. + */ +import { Effect, Layer } from "effect"; + +import * as ServerAutoUpdate from "../cloud/autoUpdate.ts"; +import type * as CloudLink from "../cloud/CloudLink.ts"; +import * as HoldWorkConnect from "../cloud/holdWorkConnect.ts"; +import type * as ServerConfig from "../config.ts"; +import * as RemoteDelegation from "../peer/RemoteDelegation.ts"; +import * as SlotGate from "../slots/SlotGate.ts"; +import * as HeadlessUsageRefresher from "../usage/HeadlessUsageRefresher.ts"; +import * as UsagePacer from "../usage/UsagePacer.ts"; +import * as UsageReadingsHistory from "../usage/UsageReadingsHistory.ts"; +import * as WorkAdmission from "../workAdmission.ts"; + +/** Wraps upstream's agent activity relay: a held server (`serve --hold-work`) publishes none. */ +export const holdWorkAgentActivity = HoldWorkConnect.agentActivityLayer; + +/** + * Upstream's Connect link with its reachability merged in, as server.ts had it, except that a held + * server starts, registers and releases no tunnel. + */ +export const holdWorkCloudLink = ( + cloudLinkLayer: Layer.Layer, + reachabilityLayer: Layer.Layer, +) => HoldWorkConnect.layer(cloudLinkLayer).pipe(Layer.provideMerge(reachabilityLayer)); + +/** In the runtime's core dependencies: usage pacer (and its history), slot gate, usage refresher. */ +export const layerRuntimeServices = Layer.mergeAll( + UsagePacer.layer.pipe(Layer.provideMerge(UsageReadingsHistory.layer)), + SlotGate.layer, + HeadlessUsageRefresher.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, + // agentSlots). Asking for them beside /mcp makes a server wired without one a type error + // instead of a silent gate. + Layer.effectDiscard(UsagePacer.UsagePacer.pipe(Effect.asVoid)), + Layer.effectDiscard(SlotGate.SlotGate.pipe(Effect.asVoid)), + // Off unless automaticUpdates is on; shares the Update button's ServerSelfUpdate in server.ts. + ServerAutoUpdate.layer, +); + +/** + * Upstream's start of the follower of delegated tasks in linked environments, except that a held + * server (`serve --hold-work`) neither follows nor stops them. What it owes them stays owed for + * the next start without the flag. + */ +export const startRemoteDelegation = Effect.gen(function* () { + const remote = yield* RemoteDelegation.RemoteDelegation; + if ((yield* WorkAdmission.WorkAdmission).heldFromBoot) { + return yield* Effect.logInfo("Remote delegated task follower held (--hold-work)"); + } + yield* remote.start(); +}); + +/** + * One gate for the whole server: automatic updates hold new work until the restart. Made where + * server.ts makes its activation, before any layer is built; a held boot says so in the log. + * Untraced, as it was inline: no span of its own. + */ +export const makeWorkAdmissionLayer = Effect.fnUntraced(function* ( + config: Pick, +) { + const layer = Layer.succeed( + WorkAdmission.WorkAdmission, + yield* WorkAdmission.makeWorkAdmissionGate({ + marker: yield* WorkAdmission.makeFileUpdateHoldStore(config.baseDir), + heldFromBoot: config.holdWork === true, + }), + ); + if (config.holdWork === true) { + yield* Effect.logWarning(WorkAdmission.HOLD_WORK_BOOT_MESSAGE); + } + return layer; +}); diff --git a/apps/server/src/server.ts b/apps/server/src/server.ts index 6508b4d98b715..1c5eed7f4fc80 100644 --- a/apps/server/src/server.ts +++ b/apps/server/src/server.ts @@ -121,10 +121,7 @@ import * as ProviderInstanceRegistry from "./provider/ProviderInstanceRegistry.t import * as ProviderAdapterRegistry from "./orchestration-v2/ProviderAdapterRegistry.ts"; import * as ProviderRegistry from "./provider/ProviderRegistry.ts"; import * as ProviderUsageLimitsIngestion from "./provider/ProviderUsageLimitsIngestion.ts"; -import * as UsageReadingsHistory from "./usage/UsageReadingsHistory.ts"; -import * as HeadlessUsageRefresher from "./usage/HeadlessUsageRefresher.ts"; -import * as UsagePacer from "./usage/UsagePacer.ts"; -import * as SlotGate from "./slots/SlotGate.ts"; +import * as ForkLayers from "./fork/forkLayers.ts"; import * as UsageLimitSources from "./usage/UsageLimitSources.ts"; import * as ProjectFaviconResolver from "./project/ProjectFaviconResolver.ts"; import * as T3ProjectFileLoader from "./project/T3ProjectFileLoader.ts"; @@ -176,7 +173,6 @@ import * as EnvironmentAuthPolicy from "./auth/EnvironmentAuthPolicy.ts"; import * as CloudHttp from "./cloud/http.ts"; import * as GuestHttp from "./guest/http.ts"; import * as CloudLink from "./cloud/CloudLink.ts"; -import * as HoldWorkConnect from "./cloud/holdWorkConnect.ts"; import * as ConnectReachability from "./cloud/ConnectReachability.ts"; import { pendingServiceUpdateExists } from "./cloud/updateHandoff.ts"; import * as RelayTracing from "./cloud/relayTracing.ts"; @@ -192,8 +188,6 @@ import { } from "./cloud/managedTunnelStartup.ts"; import * as CloudCliTokenManager from "./cloud/CliTokenManager.ts"; import * as CloudCliState from "./cloud/CliState.ts"; -import * as ServerAutoUpdate from "./cloud/autoUpdate.ts"; -import * as WorkAdmission from "./workAdmission.ts"; import * as ServerSelfUpdate from "./cloud/selfUpdate.ts"; import * as DesktopAppUpdate from "./desktopUpdate/DesktopAppUpdate.ts"; import * as ServiceLauncherClient from "./cloud/serviceLauncherClient.ts"; @@ -677,8 +671,7 @@ const layerServerRestart = Layer.effect( ); const layerRuntimeCoreDependenciesBase = Layer.mergeAll( - // Fork: a held server (`serve --hold-work`) publishes no agent activity. - HoldWorkConnect.agentActivityLayer(AgentAwarenessRelay.layer), + ForkLayers.holdWorkAgentActivity(AgentAwarenessRelay.layer), HeldHooksWaker.layer, layerThreadSettlementWorker, layerIdleCompactionWorker, @@ -717,9 +710,7 @@ const layerRuntimeCoreDependenciesBase = Layer.mergeAll( }), ).pipe(Layer.provide(DelegatedTaskQuestionReactor.layer), Layer.provide(ProjectionStoreV2.layer)), ProviderUsageLimitsIngestion.layer, - UsagePacer.layer.pipe(Layer.provideMerge(UsageReadingsHistory.layer)), - SlotGate.layer, - HeadlessUsageRefresher.layer, + ForkLayers.layerRuntimeServices, layerProviderInstallationRefresh, ProviderInstallation.layer, ReplayMarkers.layer, @@ -854,25 +845,9 @@ const layerMakeRoutes = Layer.mergeAll( Layer.provide(ProviderAdapterRegistry.layerFromProviderInstanceRegistry), Layer.provide(McpOAuth.layerMcpClientAuthenticator), ), - // Fork: /mcp reads the usage pacer and the slot gate only if they are there (the usage pacing - // gate, agentSlots). Asking for them here, beside /mcp, makes a server wired without one a type - // error instead of a silent gate. - Layer.effectDiscard(UsagePacer.UsagePacer.pipe(Effect.asVoid)), - Layer.effectDiscard(SlotGate.SlotGate.pipe(Effect.asVoid)), // Follows delegated tasks running in linked environments, including after a restart. - Layer.effectDiscard( - Effect.gen(function* () { - const remote = yield* RemoteDelegation.RemoteDelegation; - // Fork: a held server (`serve --hold-work`) neither follows nor stops tasks in linked - // environments. What it owes them stays owed for the next start without the flag. - if ((yield* WorkAdmission.WorkAdmission).heldFromBoot) { - return yield* Effect.logInfo("Remote delegated task follower held (--hold-work)"); - } - yield* remote.start(); - }), - ), - // Off unless automaticUpdates is on; shares the Update button's ServerSelfUpdate below. - ServerAutoUpdate.layer, + Layer.effectDiscard(ForkLayers.startRemoteDelegation), + ForkLayers.layerRoutes, ).pipe( Layer.provide( PullRequestReviewGuideService.layer.pipe(Layer.provide(PullRequestReviewGuides.layer)), @@ -920,17 +895,7 @@ const layerMakeServer = Layer.unwrap( const activation = yield* Deferred.make(); const awaitActivation = Deferred.await(activation); const layerActivation = Layer.succeed(ServerActivation.ServerActivation, awaitActivation); - // One gate for the whole server: automatic updates hold new work until the restart. - const layerWorkAdmission = Layer.succeed( - WorkAdmission.WorkAdmission, - yield* WorkAdmission.makeWorkAdmissionGate({ - marker: yield* WorkAdmission.makeFileUpdateHoldStore(config.baseDir), - heldFromBoot: config.holdWork === true, - }), - ); - if (config.holdWork === true) { - yield* Effect.logWarning(WorkAdmission.HOLD_WORK_BOOT_MESSAGE); - } + const layerWorkAdmission = yield* ForkLayers.makeWorkAdmissionLayer(config); const runtimeStateParked = yield* Deferred.make(); const tailscaleParked = yield* Deferred.make(); const cloudLinkParked = yield* Deferred.make(); @@ -1369,10 +1334,7 @@ const layerMakeServer = Layer.unwrap( ); return layerServerApplication.pipe( - // Fork: a held server (`serve --hold-work`) starts, registers and releases no tunnel. - Layer.provide( - HoldWorkConnect.layer(CloudLink.layer).pipe(Layer.provideMerge(ConnectReachability.layer)), - ), + Layer.provide(ForkLayers.holdWorkCloudLink(CloudLink.layer, ConnectReachability.layer)), Layer.provideMerge(layerRuntimeServices), Layer.provideMerge( McpSessionRegistry.layer.pipe( diff --git a/docs/fork/updates.md b/docs/fork/updates.md index 6d9c587b2fcc2..0b5e36b81cd73 100644 --- a/docs/fork/updates.md +++ b/docs/fork/updates.md @@ -112,7 +112,8 @@ change, or a fork line beyond the seams, it stops and opens one issue. Then: default: keep the fork's value (and the `Fork:` comment of the inline `automaticUpdates`). 2. Check that the fork's lines in upstream files are only the seams: `node .github/scripts/fork-seam-check.cjs HEAD`. A refused line moves into the - fork's own file (a setting into `forkSettings.ts`); a changed seam line is fixed in + fork's own file (a setting into `forkSettings.ts`, a server layer into + `apps/server/src/fork/forkLayers.ts`); a changed seam line is fixed in `.github/fork-seams.json`. 3. Run the pin tests before pushing: `vp test run packages/contracts/src/forkDefaults.test.ts packages/contracts/src/forkSettings.test.ts packages/shared/src/forkSettingsPatch.test.ts apps/server/src/serverSettings.forkDefaults.test.ts apps/server/src/serverSettings.forkSettings.test.ts apps/server/src/serverSettings.forkIntervals.test.ts apps/server/src/cloud/autoUpdateSettings.test.ts apps/server/src/cloud/autoUpdate.test.ts`.