Skip to content

Commit 2c0e891

Browse files
feat(settings): add per-project overrides for scopable server settings (pingdotgg#11176)
1 parent fb52d12 commit 2c0e891

25 files changed

Lines changed: 1229 additions & 154 deletions

‎apps/server/src/environment/ServerEnvironment.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ export const make = Effect.gen(function* () {
222222
threadSettlement: true,
223223
threadAutoSettlement: true,
224224
threadRestartContinuation: true,
225+
projectSettingsOverrides: true,
225226
threadSnooze: true,
226227
environmentThemes: true,
227228
usageLimitSources: true,

‎apps/server/src/git/GitManager.ts‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,16 @@ import {
2929
type VcsStatusRemoteResult,
3030
VcsStatusResult,
3131
ModelSelection,
32+
type ProjectId,
3233
SourceControlProviderError,
3334
type SourceControlWritingStyleSettings,
35+
type ThreadId,
3436
} from "@t3tools/contracts";
37+
import {
38+
hasProjectSettingsOverrides,
39+
resolveProjectSettings,
40+
} from "@t3tools/shared/projectSettings";
41+
import * as ProjectionSnapshotQuery from "../orchestration/Services/ProjectionSnapshotQuery.ts";
3542
import {
3643
detectSourceControlProviderFromGitRemoteUrl,
3744
mergeGitStatusParts,
@@ -661,6 +668,28 @@ export const make = Effect.gen(function* () {
661668

662669
const sourceControlProvider = (cwd: string) => sourceControlProviders.resolve({ cwd });
663670
const serverSettingsService = yield* ServerSettings.ServerSettingsService;
671+
// Optional: git actions also run from the CLI and tests without orchestration.
672+
const projectionQuery = yield* Effect.serviceOption(
673+
ProjectionSnapshotQuery.ProjectionSnapshotQuery,
674+
);
675+
/** Environment settings with the acting project's overrides applied. */
676+
const projectSettingsFor = Effect.fnUntraced(function* (input: {
677+
readonly cwd: string;
678+
readonly threadId?: ThreadId | undefined;
679+
}) {
680+
const settings = yield* serverSettingsService.getSettings;
681+
if (!hasProjectSettingsOverrides(settings) || Option.isNone(projectionQuery)) return settings;
682+
const projectId = yield* (
683+
input.threadId !== undefined
684+
? projectionQuery.value
685+
.getThreadShellById(input.threadId)
686+
.pipe(Effect.map(Option.map((thread) => thread.projectId)))
687+
: projectionQuery.value
688+
.getActiveProjectByWorkspaceRoot(input.cwd)
689+
.pipe(Effect.map(Option.map((project) => project.id)))
690+
).pipe(Effect.orElseSucceed(() => Option.none<ProjectId>()));
691+
return resolveProjectSettings(settings, Option.getOrNull(projectId)).settings;
692+
});
664693
const readRepositoryInstructions = (cwd: string, fileName: string) =>
665694
Effect.gen(function* () {
666695
const root = yield* fileSystem.realPath(cwd);
@@ -2600,7 +2629,7 @@ export const make = Effect.gen(function* () {
26002629
let commitMessageForStep = input.commitMessage;
26012630
let preResolvedCommitSuggestion: CommitAndBranchSuggestion | undefined = undefined;
26022631

2603-
const textGenerationSettings = yield* serverSettingsService.getSettings.pipe(
2632+
const textGenerationSettings = yield* projectSettingsFor(input).pipe(
26042633
Effect.flatMap((settings) =>
26052634
settings.sourceControlWriterModelSelection === null
26062635
? Effect.succeed({

‎apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,7 @@ projectionSnapshotLayer("ProjectionSnapshotQuery", (it) => {
682682
if (context._tag === "Some") {
683683
assert.deepEqual(context.value, {
684684
id: ThreadId.make("thread-1"),
685+
projectId: asProjectId("project-1"),
685686
title: "Thread 1",
686687
session: snapshot.threads[0]?.session,
687688
});

‎apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ const ProjectionThreadActivityIdRowSchema = Schema.Struct({
142142
const ProjectionThreadSessionDbRowSchema = ProjectionThreadSession;
143143
const ProjectionThreadRuntimeContextDbRowSchema = Schema.Struct({
144144
id: ThreadId,
145+
projectId: ProjectId,
145146
title: Schema.String,
146147
session: Schema.NullOr(ProjectionThreadSessionDbRowSchema),
147148
});
@@ -1231,6 +1232,7 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
12311232
sql`
12321233
SELECT
12331234
threads.thread_id AS id,
1235+
threads.project_id AS "projectId",
12341236
threads.title,
12351237
sessions.thread_id AS "threadId",
12361238
sessions.status,
@@ -1251,6 +1253,7 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
12511253
Effect.map((rows) =>
12521254
rows.map((row) => ({
12531255
id: row.id,
1256+
projectId: row.projectId,
12541257
title: row.title,
12551258
session: row.threadId === null ? null : row,
12561259
})),
@@ -3164,6 +3167,7 @@ pending_approval_requests AS (
31643167
);
31653168
return Option.map(context, (row) => ({
31663169
id: row.id,
3170+
projectId: row.projectId,
31673171
title: row.title,
31683172
session: row.session === null ? null : mapSessionRow(row.session),
31693173
}));

‎apps/server/src/orchestration/Layers/ProviderCommandReactor.ts‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ import {
5454
resolveSourceControlWriterModelSelection,
5555
ServerSettingsService,
5656
} from "../../serverSettings.ts";
57+
import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
5758
import { VcsStatusBroadcaster } from "../../vcs/VcsStatusBroadcaster.ts";
5859
import { GitWorkflowService } from "../../git/GitWorkflowService.ts";
5960
const isProviderAdapterRequestError = Schema.is(ProviderAdapterRequestError);
@@ -329,6 +330,16 @@ const make = Effect.gen(function* () {
329330
const vcsStatusBroadcaster = yield* VcsStatusBroadcaster;
330331
const textGeneration = yield* TextGeneration;
331332
const serverSettingsService = yield* ServerSettingsService;
333+
/** Environment settings with the thread's project overrides applied. */
334+
const projectSettingsForThread = Effect.fnUntraced(function* (threadId: ThreadId) {
335+
const settings = yield* serverSettingsService.getSettings;
336+
if (Object.keys(settings.projectSettingsOverrides).length === 0) return settings;
337+
const thread = yield* projectionSnapshotQuery
338+
.getThreadShellById(threadId)
339+
.pipe(Effect.orElseSucceed(() => Option.none()));
340+
return resolveProjectSettings(settings, Option.isSome(thread) ? thread.value.projectId : null)
341+
.settings;
342+
});
332343
const serverCommandId = (tag: string) =>
333344
crypto.randomUUIDv4.pipe(Effect.map((uuid) => CommandId.make(`server:${tag}:${uuid}`)));
334345
const serverEventId = () => crypto.randomUUIDv4.pipe(Effect.map(EventId.make));
@@ -996,7 +1007,7 @@ const make = Effect.gen(function* () {
9961007
const cwd = input.worktreePath;
9971008
const attachments = input.attachments ?? [];
9981009
yield* Effect.gen(function* () {
999-
const settings = yield* serverSettingsService.getSettings;
1010+
const settings = yield* projectSettingsForThread(input.threadId);
10001011
const modelSelection =
10011012
settings.sourceControlWriterModelSelection === null
10021013
? settings.textGenerationModelSelection
@@ -1047,8 +1058,9 @@ const make = Effect.gen(function* () {
10471058
}) {
10481059
const attachments = input.attachments ?? [];
10491060
yield* Effect.gen(function* () {
1050-
const { textGenerationModelSelection: modelSelection } =
1051-
yield* serverSettingsService.getSettings;
1061+
const { textGenerationModelSelection: modelSelection } = yield* projectSettingsForThread(
1062+
input.threadId,
1063+
);
10521064

10531065
const generated = yield* textGeneration
10541066
.generateThreadTitle({
@@ -1117,8 +1129,10 @@ const make = Effect.gen(function* () {
11171129
thread,
11181130
projects: project ? [project] : [],
11191131
}) ?? process.cwd();
1120-
const { textGenerationModelSelection: modelSelection } =
1121-
yield* serverSettingsService.getSettings;
1132+
const { textGenerationModelSelection: modelSelection } = resolveProjectSettings(
1133+
yield* serverSettingsService.getSettings,
1134+
thread.projectId,
1135+
).settings;
11221136
const generated = yield* textGeneration.generateThreadTitle({
11231137
cwd,
11241138
message,

‎apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import {
5151
import { projectActivityPayload } from "../ActivityPayloadProjection.ts";
5252
import { forkParked } from "../../serverActivation.ts";
5353
import { ServerSettingsService } from "../../serverSettings.ts";
54+
import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
5455
import { canReplaceThreadTitle } from "../threadTitles.ts";
5556

5657
const providerTurnKey = (threadId: ThreadId, turnId: TurnId) => `${threadId}:${turnId}`;
@@ -1668,7 +1669,10 @@ const make = Effect.gen(function* () {
16681669

16691670
const assistantDeliveryMode: AssistantDeliveryMode = yield* Effect.map(
16701671
serverSettingsService.getSettings,
1671-
(settings) => (settings.enableLegacyTokenStreaming ? "streaming" : "buffered"),
1672+
(settings) =>
1673+
resolveProjectSettings(settings, thread.projectId).settings.enableLegacyTokenStreaming
1674+
? "streaming"
1675+
: "buffered",
16721676
);
16731677
if (assistantDeliveryMode === "buffered") {
16741678
const spillChunk = yield* appendBufferedAssistantText(assistantMessageId, assistantDelta);
@@ -1709,7 +1713,10 @@ const make = Effect.gen(function* () {
17091713
});
17101714
const assistantDeliveryMode: AssistantDeliveryMode = yield* Effect.map(
17111715
serverSettingsService.getSettings,
1712-
(settings) => (settings.enableLegacyTokenStreaming ? "streaming" : "buffered"),
1716+
(settings) =>
1717+
resolveProjectSettings(settings, thread.projectId).settings.enableLegacyTokenStreaming
1718+
? "streaming"
1719+
: "buffered",
17131720
);
17141721
const flushedMessageIds =
17151722
assistantDeliveryMode === "buffered"

‎apps/server/src/orchestration/Services/ProjectionSnapshotQuery.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ export interface ProjectionSnapshotQueryShape {
209209
readonly getThreadRuntimeContext: (
210210
threadId: ThreadId,
211211
) => Effect.Effect<
212-
Option.Option<Pick<OrchestrationThreadShell, "id" | "title" | "session">>,
212+
Option.Option<Pick<OrchestrationThreadShell, "id" | "projectId" | "title" | "session">>,
213213
ProjectionRepositoryError
214214
>;
215215

‎apps/server/src/orchestration/ThreadSettlementReactor.test.ts‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,35 @@ const startHarness = Effect.fn("startThreadSettlementHarness")(function* (
302302
});
303303

304304
describe("ThreadSettlementReactor", () => {
305+
it("distinguishes a project that inherits the threshold from one that disables it", () => {
306+
const inherits = ThreadSettlementReactor.autoSettlementSettingsKey({
307+
...DEFAULT_SERVER_SETTINGS,
308+
projectSettingsOverrides: { [PROJECT_ID]: { sidebarAutoSettleOnMerge: true } },
309+
});
310+
const never = ThreadSettlementReactor.autoSettlementSettingsKey({
311+
...DEFAULT_SERVER_SETTINGS,
312+
projectSettingsOverrides: {
313+
[PROJECT_ID]: { sidebarAutoSettleOnMerge: true, sidebarAutoSettleAfterDays: null },
314+
},
315+
});
316+
assert.notStrictEqual(inherits, never);
317+
});
318+
319+
it("ignores project overrides that do not touch settlement", () => {
320+
const base = ThreadSettlementReactor.autoSettlementSettingsKey({
321+
...DEFAULT_SERVER_SETTINGS,
322+
projectSettingsOverrides: { [PROJECT_ID]: { sidebarAutoSettleOnMerge: false } },
323+
});
324+
const unrelated = ThreadSettlementReactor.autoSettlementSettingsKey({
325+
...DEFAULT_SERVER_SETTINGS,
326+
projectSettingsOverrides: {
327+
[LINKED_PROJECT_ID]: { defaultThreadEnvMode: "worktree" },
328+
[PROJECT_ID]: { sidebarAutoSettleOnMerge: false, defaultAutoPull: true },
329+
},
330+
});
331+
assert.strictEqual(base, unrelated);
332+
});
333+
305334
it.effect(
306335
"settles all-terminal links from snapshots and keeps open or unsynced links active",
307336
() =>
@@ -486,6 +515,59 @@ describe("ThreadSettlementReactor", () => {
486515
),
487516
);
488517

518+
it.effect("a project override settles only that project's inactive threads", () =>
519+
Effect.scoped(
520+
Effect.gen(function* () {
521+
yield* TestClock.setTime(Date.parse(NOW));
522+
const overriddenProject = ProjectId.make("overridden-project");
523+
const fixture = yield* makeHarness({
524+
snapshot: makeSnapshot(
525+
[
526+
makeThread("inherits-thread"),
527+
makeThread("overridden-thread", { projectId: overriddenProject }),
528+
],
529+
[makeProject(), makeProject(overriddenProject, "/workspace/overridden")],
530+
),
531+
settings: {
532+
...DEFAULT_SERVER_SETTINGS,
533+
sidebarAutoSettleAfterDays: null,
534+
sidebarAutoSettleOnMerge: false,
535+
projectSettingsOverrides: {
536+
[overriddenProject]: { sidebarAutoSettleAfterDays: 1 },
537+
},
538+
},
539+
});
540+
541+
yield* Effect.gen(function* () {
542+
const reactor = yield* ThreadSettlementReactor.ThreadSettlementReactor;
543+
yield* reactor.start();
544+
yield* Queue.take(fixture.settingsReads);
545+
yield* Deferred.succeed(fixture.activation, undefined);
546+
yield* Queue.take(fixture.snapshotReads);
547+
yield* reactor.drain;
548+
assert.deepStrictEqual(
549+
(yield* Ref.get(fixture.commands)).map((command) => command.threadId),
550+
[ThreadId.make("overridden-thread")],
551+
);
552+
553+
// Clearing the override is a settlement change, so the sweep re-arms.
554+
yield* fixture.updateSettings({
555+
projectSettingsOverrides: { [overriddenProject]: null },
556+
sidebarAutoSettleAfterDays: 1,
557+
});
558+
yield* Queue.take(fixture.snapshotReads);
559+
yield* reactor.drain;
560+
// The static snapshot never records the first settlement, so the
561+
// second sweep dispatches for both; the inheriting thread is new.
562+
assert.include(
563+
(yield* Ref.get(fixture.commands)).map((command) => command.threadId),
564+
ThreadId.make("inherits-thread"),
565+
);
566+
}).pipe(Effect.provide(fixture.layer));
567+
}),
568+
),
569+
);
570+
489571
it.effect("starts without clients and skips protected threads before pull request lookup", () =>
490572
Effect.scoped(
491573
Effect.gen(function* () {

0 commit comments

Comments
 (0)