diff --git a/apps/server/src/device/DeviceService.test.ts b/apps/server/src/device/DeviceService.test.ts index f2e0bcdf0c725..dab657fce11f9 100644 --- a/apps/server/src/device/DeviceService.test.ts +++ b/apps/server/src/device/DeviceService.test.ts @@ -313,15 +313,18 @@ const fixture = Effect.fn("fixture")(function* ( start: Effect.void, ready: Effect.void, getSettings: Ref.get(settings), - updateSettings: (patch) => - Ref.updateAndGet(settings, (current) => ({ - ...current, - enableDeviceSupport: patch.enableDeviceSupport ?? current.enableDeviceSupport, - enableAgentDeviceAccess: - patch.enableAgentDeviceAccess ?? current.enableAgentDeviceAccess, - deviceOnboardingCompleted: - patch.deviceOnboardingCompleted ?? current.deviceOnboardingCompleted, - })), + updateSettings: (update) => + Ref.updateAndGet(settings, (current) => { + const patch = ServerSettings.resolveServerSettingsPatch(current, update); + return { + ...current, + enableDeviceSupport: patch.enableDeviceSupport ?? current.enableDeviceSupport, + enableAgentDeviceAccess: + patch.enableAgentDeviceAccess ?? current.enableAgentDeviceAccess, + deviceOnboardingCompleted: + patch.deviceOnboardingCompleted ?? current.deviceOnboardingCompleted, + }; + }), streamChanges: Stream.empty, subscribeChanges: Effect.succeed(Stream.empty), }), diff --git a/apps/server/src/orchestration-v2/ThreadSettlementService.test.ts b/apps/server/src/orchestration-v2/ThreadSettlementService.test.ts index cca6ee08e7116..e3946028dc4b1 100644 --- a/apps/server/src/orchestration-v2/ThreadSettlementService.test.ts +++ b/apps/server/src/orchestration-v2/ThreadSettlementService.test.ts @@ -20,7 +20,6 @@ import { type OrchestrationV2ThreadShell, type PullRequestSummary, type ServerSettings as ContractServerSettings, - type ServerSettingsPatch, } from "@t3tools/contracts"; import { applyServerSettingsPatch } from "@t3tools/shared/serverSettings"; import * as SecureRandom from "effect/crypto/SecureRandom"; @@ -598,9 +597,13 @@ const makeHarness = Effect.fn("makeThreadSettlementHarness")(function* (options: const scriptRuns = yield* Queue.unbounded(); - const updateSettings = (patch: ServerSettingsPatch) => + const updateSettings = (update: ServerSettings.ServerSettingsUpdate) => Effect.gen(function* () { - const next = applyServerSettingsPatch(yield* Ref.get(settings), patch); + const current = yield* Ref.get(settings); + const next = applyServerSettingsPatch( + current, + ServerSettings.resolveServerSettingsPatch(current, update), + ); yield* Ref.set(settings, next); yield* PubSub.publish(settingsChanges, next); return next; diff --git a/apps/server/src/project/ProjectService.test.ts b/apps/server/src/project/ProjectService.test.ts index 1517bed31ba23..1cf679771b9a2 100644 --- a/apps/server/src/project/ProjectService.test.ts +++ b/apps/server/src/project/ProjectService.test.ts @@ -35,6 +35,8 @@ import * as WorkspacePaths from "../workspace/WorkspacePaths.ts"; import * as ProjectEnrichmentService from "./ProjectEnrichmentService.ts"; import * as ProjectFaviconResolver from "./ProjectFaviconResolver.ts"; import * as ProjectService from "./ProjectService.ts"; +import { resolveProjectScripts } from "@t3tools/shared/projectScripts"; +import { resolveProjectSettings } from "@t3tools/shared/projectSettings"; import * as RepositoryIdentityResolver from "./RepositoryIdentityResolver.ts"; const layerWorkspacePaths = Layer.succeed(WorkspacePaths.WorkspacePaths, { @@ -96,13 +98,14 @@ const layerTestFor = ( | ProjectFaviconResolver.ProjectFaviconResolver | RepositoryIdentityResolver.RepositoryIdentityResolver >, + settings?: Parameters[0], ) => RuntimeLayer.layerProjectService.pipe( Layer.provideMerge(ProjectEnrichmentService.layer), Layer.provideMerge(layerWorkspacePaths), Layer.provideMerge(projectMetadataLayer), Layer.provideMerge(SqlitePersistence.layerMemory), - Layer.provideMerge(ServerSettings.layerTest()), + Layer.provideMerge(ServerSettings.layerTest(settings)), Layer.provide(ServerConfig.layerTest(process.cwd(), { prefix: "project-service-test-" })), Layer.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)), Layer.provideMerge(layerEnrichmentCompletions), @@ -615,6 +618,63 @@ it.layer(layerTest)("ProjectService", (it) => { ); }); +it.effect("an update on folded settings reaches the actions and defaults new threads use", () => + Effect.gen(function* () { + const service = yield* ProjectService.ProjectService; + const projectId = ProjectId.make("project:folded"); + const setup = { + id: "setup", + name: "Setup", + command: "vp install", + icon: "configure", + runOnWorktreeCreate: true, + } as const; + yield* service.create({ + commandId: CommandId.make("command:project:folded-create"), + projectId, + title: "Folded", + workspaceRoot: "/work/folded", + }); + + const updated = yield* service.update({ + commandId: CommandId.make("command:project:folded-update"), + projectId, + scripts: [setup], + defaultThreadEnvMode: "worktree", + }); + + const settingsService = yield* ServerSettings.ServerSettingsService; + const settings = yield* settingsService.getSettings; + assert.deepEqual(resolveProjectScripts(settings, updated), [setup]); + assert.equal( + resolveProjectSettings(settings, projectId, updated).settings.defaultThreadEnvMode, + "worktree", + ); + + yield* service.update({ + commandId: CommandId.make("command:project:folded-update-local"), + projectId, + defaultThreadEnvMode: "local", + }); + // A late retry of the first command must not reapply its superseded value. + const retried = yield* service.update({ + commandId: CommandId.make("command:project:folded-update"), + projectId, + scripts: [setup], + defaultThreadEnvMode: "worktree", + }); + assert.equal( + resolveProjectSettings(yield* settingsService.getSettings, projectId, retried).settings + .defaultThreadEnvMode, + "local", + ); + }).pipe( + Effect.provide( + layerTestFor(layerMetadata, { projectSettingsFolded: true, defaultThreadEnvMode: "local" }), + ), + ), +); + it.effect("keeps project snapshots available when optional metadata enrichment fails", () => Effect.gen(function* () { const layerFailingMetadata = Layer.merge( diff --git a/apps/server/src/project/ProjectService.ts b/apps/server/src/project/ProjectService.ts index 7080a9ff2bdfa..1d956f363efe7 100644 --- a/apps/server/src/project/ProjectService.ts +++ b/apps/server/src/project/ProjectService.ts @@ -244,6 +244,37 @@ export const make = Effect.gen(function* () { ), ); + // Launch and clients read these from settings overrides once legacy project fields are folded. + const writeProjectSettingsOverrides = ( + command: Exclude, + ) => { + const changes = { + defaultProjectScripts: command.scripts, + ...(command.type === "project.meta.update" + ? { + defaultModelSelection: command.defaultModelSelection, + defaultThreadEnvMode: command.defaultThreadEnvMode, + defaultAutoPull: command.autoPull, + } + : {}), + }; + if (Object.values(changes).every((value) => value === undefined)) return Effect.void; + return serverSettings.updateSettings((settings) => { + const entry: Record = { + ...settings.projectSettingsOverrides[command.projectId], + }; + for (const [key, value] of Object.entries(changes)) { + if (value === null) delete entry[key]; + else if (value !== undefined) entry[key] = value; + } + return { + projectSettingsOverrides: { + [command.projectId]: Object.keys(entry).length === 0 ? null : entry, + }, + }; + }); + }; + const commit = Effect.fn("ProjectService.commit")(function* (command: ProjectCommand) { const { projectId } = command; const dispatchError = (cause: unknown) => @@ -270,13 +301,17 @@ export const make = Effect.gen(function* () { now, }); if (Result.isSuccess(planned)) { - const { receipt } = yield* eventSink.commitProjectCommand({ + const { receipt, committed } = yield* eventSink.commitProjectCommand({ commandId: command.commandId, projectId, commandType: command.type, acceptedAt: now, event: planned.success, }); + // Under the project lock and only on first commit, so retries cannot undo newer commands. + if (committed && command.type !== "project.delete") { + yield* writeProjectSettingsOverrides(command); + } return receipt; } return yield* eventSink.commitRejectedProjectCommand({ diff --git a/apps/server/src/provider/ProviderRegistry.test.ts b/apps/server/src/provider/ProviderRegistry.test.ts index b5501943ca04f..02ea1696ffbcb 100644 --- a/apps/server/src/provider/ProviderRegistry.test.ts +++ b/apps/server/src/provider/ProviderRegistry.test.ts @@ -289,10 +289,13 @@ function makeMutableServerSettingsService( start: Effect.void, ready: Effect.void, getSettings: Ref.get(settingsRef), - updateSettings: (patch) => + updateSettings: (update) => Effect.gen(function* () { const current = yield* Ref.get(settingsRef); - const next = applyServerSettingsPatch(current, patch); + const next = applyServerSettingsPatch( + current, + ServerSettingsModule.resolveServerSettingsPatch(current, update), + ); encodeServerSettings(next); yield* Ref.set(settingsRef, next); yield* PubSub.publish(changes, next); diff --git a/apps/server/src/serverSettings.ts b/apps/server/src/serverSettings.ts index 251becba12f6c..cadf300ad3339 100644 --- a/apps/server/src/serverSettings.ts +++ b/apps/server/src/serverSettings.ts @@ -250,6 +250,16 @@ function ensureProviderInstanceMutationAllowed( return Effect.void; } +/** A function is computed from the current settings under the write lock. */ +export type ServerSettingsUpdate = + | ServerSettingsPatch + | ((current: ServerSettings) => ServerSettingsPatch); + +export const resolveServerSettingsPatch = ( + current: ServerSettings, + update: ServerSettingsUpdate, +): ServerSettingsPatch => (typeof update === "function" ? update(current) : update); + export class ServerSettingsService extends Context.Service< ServerSettingsService, { @@ -263,7 +273,7 @@ export class ServerSettingsService extends Context.Service< readonly getSettings: Effect.Effect; readonly updateSettings: ( - patch: ServerSettingsPatch, + patch: ServerSettingsUpdate, ) => Effect.Effect; readonly updateProviderInstance: ( @@ -326,7 +336,12 @@ const makeTest = Effect.fn("serverSettings.makeTest")(function* ( getSettings, updateSettings: (patch) => updateTestSettings((currentSettings) => - Effect.succeed(applyServerSettingsPatch(currentSettings, patch)), + Effect.succeed( + applyServerSettingsPatch( + currentSettings, + resolveServerSettingsPatch(currentSettings, patch), + ), + ), ), updateProviderInstance: (mutation, patch = {}) => updateTestSettings((currentSettings) => @@ -1184,8 +1199,11 @@ const make = Effect.gen(function* () { }; const updateAndPersistSettings = ( - patch: ServerSettingsPatch, - update: (current: ServerSettings) => Effect.Effect, + patchInput: ServerSettingsUpdate, + update: ( + current: ServerSettings, + patch: ServerSettingsPatch, + ) => Effect.Effect, ): Effect.Effect => writeSemaphore.withPermits(1)( Effect.gen(function* () { @@ -1197,9 +1215,10 @@ const make = Effect.gen(function* () { cause: parseFailure.value, }); } + const patch = resolveServerSettingsPatch(current, patchInput); // An older client echoes a reference as the redacted marker; restore it before vetting so // a removed provider's reference is cleared below rather than restored by persistence. - const updated = withEchoedSecretReferences(current, yield* update(current)); + const updated = withEchoedSecretReferences(current, yield* update(current, patch)); // Vet before secrets: a variable the policy hides from clients is not one they removed. const vetted = managed.vetWrite(current, yield* normalizeServerSettings(updated), patch); if (vetted._tag === "Locked") { @@ -1367,8 +1386,8 @@ const make = Effect.gen(function* () { start, ready: Deferred.await(startedDeferred), getSettings: getMaterializedSettings, - updateSettings: (patch) => - updateAndPersistSettings(patch, (current) => + updateSettings: (patchInput) => + updateAndPersistSettings(patchInput, (current, patch) => ensureSecretProviderWritesAllowed(patch, settingsPath).pipe( Effect.as(applyServerSettingsPatch(current, patch)), ),