TypeScript
fix(contracts): accept legacy writing styles when reading settings (#21665)
Julius Marmingef1d0f2ad8ac7
diff --git a/packages/contracts/src/settings.test.ts b/packages/contracts/src/settings.test.tsindex 805ce643f1d..562bc7a54a1 100644--- a/packages/contracts/src/settings.test.ts+++ b/packages/contracts/src/settings.test.ts@@ -915,6 +915,30 @@ describe("ServerSettings worktree defaults", () => {});describe("ServerSettings.sourceControlWritingStyle", () => {+ it.each([+ { legacy: "", mode: "repo_conventions", customInstructions: "" },+ { legacy: " \t\n ", mode: "repo_conventions", customInstructions: "" },+ {+ legacy: " Prefer concise wording.\nKeep issue references. ",+ mode: "custom",+ customInstructions: "Prefer concise wording.\nKeep issue references.",+ },+ ])("normalizes legacy writing style $legacy in snapshots only", ({ legacy, ...expected }) => {+ const style = { ...expected, followChangeRequestTemplates: true };+ for (const [input, normalized] of [+ [{ sourceControlWritingStyle: legacy }, { sourceControlWritingStyle: style }],+ [+ { projectSettingsOverrides: { project: { sourceControlWritingStyle: legacy } } },+ { projectSettingsOverrides: { project: { sourceControlWritingStyle: style } } },+ ],+ ] as const) {+ const settings = decodeServerSettings(input);+ expect(settings).toMatchObject(normalized);+ expect(encodeServerSettings(settings)).toMatchObject(normalized);+ expect(() => decodeServerSettingsPatch(input)).toThrow();+ }+ });+it("defaults all style settings for legacy configs", () => {const settings = decodeServerSettings({});@@ -926,17 +950,33 @@ describe("ServerSettings.sourceControlWritingStyle", () => {expect(settings.sourceControlWriterModelSelection).toBeNull();});- it("trims partial style updates", () => {- const patch = decodeServerSettingsPatch({- sourceControlWritingStyle: {- mode: "custom",- customInstructions: " Prefer concise wording. ",- },- });-- expect(patch.sourceControlWritingStyle).toEqual({+ it("preserves structured encoding and trims partial style updates", () => {+ const style = {mode: "custom",customInstructions: "Prefer concise wording.",+ followChangeRequestTemplates: false,+ };+ const input = {+ sourceControlWritingStyle: style,+ projectSettingsOverrides: { project: { sourceControlWritingStyle: style } },+ };+ expect(encodeServerSettings(decodeServerSettings(input))).toMatchObject(input);+ expect(decodeServerSettingsPatch(input)).toEqual(input);++ const partial = { mode: "custom", customInstructions: " Prefer concise wording. " };+ const normalized = { mode: "custom", customInstructions: "Prefer concise wording." };+ expect(+ decodeServerSettingsPatch({+ sourceControlWritingStyle: partial,+ projectSettingsOverrides: { project: { sourceControlWritingStyle: partial } },+ }),+ ).toEqual({+ sourceControlWritingStyle: normalized,+ projectSettingsOverrides: {+ project: {+ sourceControlWritingStyle: { ...normalized, followChangeRequestTemplates: true },+ },+ },});});});diff --git a/packages/contracts/src/settings.ts b/packages/contracts/src/settings.tsindex dc93afc8090..61ca1140504 100644--- a/packages/contracts/src/settings.ts+++ b/packages/contracts/src/settings.ts@@ -816,6 +816,26 @@ export const SourceControlWritingStyleSettings = Schema.Struct({});export type SourceControlWritingStyleSettings = typeof SourceControlWritingStyleSettings.Type;+const SourceControlWritingStyleSetting = Schema.Union([+ TrimmedString,+ SourceControlWritingStyleSettings,+]).pipe(+ Schema.decodeTo(+ Schema.toType(SourceControlWritingStyleSettings),+ SchemaTransformation.transform({+ decode: (value) =>+ typeof value === "string"+ ? {+ mode: value.length > 0 ? ("custom" as const) : ("repo_conventions" as const),+ customInstructions: value,+ followChangeRequestTemplates: true,+ }+ : value,+ encode: (value) => value,+ }),+ ),+);+export const BranchNamingMode = Schema.Literals(["static", "semantic", "custom"]);export type BranchNamingMode = typeof BranchNamingMode.Type;@@ -964,7 +984,7 @@ export const ProjectSettingsOverrides = Schema.Struct({sourceControlWriterModelSelection: Schema.optionalKey(Schema.NullOr(ModelSelection)),reviewGuideModelSelection: Schema.optionalKey(Schema.NullOr(ModelSelection)),reviewGuideInstructions: Schema.optionalKey(TrimmedString),- sourceControlWritingStyle: Schema.optionalKey(SourceControlWritingStyleSettings),+ sourceControlWritingStyle: Schema.optionalKey(SourceControlWritingStyleSetting),removeAgentCreditsOnMerge: Schema.optionalKey(Schema.Boolean),branchNamingMode: Schema.optionalKey(BranchNamingMode),branchNamePrefix: Schema.optionalKey(TrimmedString),@@ -1204,7 +1224,7 @@ export const ServerSettings = Schema.Struct({branchNamePrefix: TrimmedString.pipe(Schema.withDecodingDefault(Effect.succeed("t3"))),branchNameInstructions: TrimmedString.pipe(Schema.withDecodingDefault(Effect.succeed(""))),removeAgentCreditsOnMerge: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(false))),- sourceControlWritingStyle: SourceControlWritingStyleSettings.pipe(+ sourceControlWritingStyle: SourceControlWritingStyleSetting.pipe(Schema.withDecodingDefault(Effect.succeed({})),),sourceControlWriterModelSelection: Schema.NullOr(ModelSelection).pipe(@@ -1432,7 +1452,15 @@ export const ServerSettingsPatch = Schema.Struct({* field existed cannot wipe it by editing another key.*/projectSettingsOverrides: Schema.optionalKey(- Schema.Record(ProjectId, Schema.NullOr(ProjectSettingsOverrides)),+ Schema.Record(+ ProjectId,+ Schema.NullOr(+ Schema.Struct({+ ...ProjectSettingsOverrides.fields,+ sourceControlWritingStyle: Schema.optionalKey(SourceControlWritingStyleSettings),+ }),+ ),+ ),),enableAgentDeviceAccess: Schema.optionalKey(Schema.Boolean),enableDeviceSupport: Schema.optionalKey(Schema.Boolean),