Skip to content

Commit f1d0f2a

Browse files
fix(contracts): accept legacy writing styles when reading settings (pingdotgg#21665)
1 parent 1e951e9 commit f1d0f2a

2 files changed

Lines changed: 80 additions & 12 deletions

File tree

‎packages/contracts/src/settings.test.ts‎

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,30 @@ describe("ServerSettings worktree defaults", () => {
915915
});
916916

917917
describe("ServerSettings.sourceControlWritingStyle", () => {
918+
it.each([
919+
{ legacy: "", mode: "repo_conventions", customInstructions: "" },
920+
{ legacy: " \t\n ", mode: "repo_conventions", customInstructions: "" },
921+
{
922+
legacy: " Prefer concise wording.\nKeep issue references. ",
923+
mode: "custom",
924+
customInstructions: "Prefer concise wording.\nKeep issue references.",
925+
},
926+
])("normalizes legacy writing style $legacy in snapshots only", ({ legacy, ...expected }) => {
927+
const style = { ...expected, followChangeRequestTemplates: true };
928+
for (const [input, normalized] of [
929+
[{ sourceControlWritingStyle: legacy }, { sourceControlWritingStyle: style }],
930+
[
931+
{ projectSettingsOverrides: { project: { sourceControlWritingStyle: legacy } } },
932+
{ projectSettingsOverrides: { project: { sourceControlWritingStyle: style } } },
933+
],
934+
] as const) {
935+
const settings = decodeServerSettings(input);
936+
expect(settings).toMatchObject(normalized);
937+
expect(encodeServerSettings(settings)).toMatchObject(normalized);
938+
expect(() => decodeServerSettingsPatch(input)).toThrow();
939+
}
940+
});
941+
918942
it("defaults all style settings for legacy configs", () => {
919943
const settings = decodeServerSettings({});
920944

@@ -926,17 +950,33 @@ describe("ServerSettings.sourceControlWritingStyle", () => {
926950
expect(settings.sourceControlWriterModelSelection).toBeNull();
927951
});
928952

929-
it("trims partial style updates", () => {
930-
const patch = decodeServerSettingsPatch({
931-
sourceControlWritingStyle: {
932-
mode: "custom",
933-
customInstructions: " Prefer concise wording. ",
934-
},
935-
});
936-
937-
expect(patch.sourceControlWritingStyle).toEqual({
953+
it("preserves structured encoding and trims partial style updates", () => {
954+
const style = {
938955
mode: "custom",
939956
customInstructions: "Prefer concise wording.",
957+
followChangeRequestTemplates: false,
958+
};
959+
const input = {
960+
sourceControlWritingStyle: style,
961+
projectSettingsOverrides: { project: { sourceControlWritingStyle: style } },
962+
};
963+
expect(encodeServerSettings(decodeServerSettings(input))).toMatchObject(input);
964+
expect(decodeServerSettingsPatch(input)).toEqual(input);
965+
966+
const partial = { mode: "custom", customInstructions: " Prefer concise wording. " };
967+
const normalized = { mode: "custom", customInstructions: "Prefer concise wording." };
968+
expect(
969+
decodeServerSettingsPatch({
970+
sourceControlWritingStyle: partial,
971+
projectSettingsOverrides: { project: { sourceControlWritingStyle: partial } },
972+
}),
973+
).toEqual({
974+
sourceControlWritingStyle: normalized,
975+
projectSettingsOverrides: {
976+
project: {
977+
sourceControlWritingStyle: { ...normalized, followChangeRequestTemplates: true },
978+
},
979+
},
940980
});
941981
});
942982
});

‎packages/contracts/src/settings.ts‎

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,26 @@ export const SourceControlWritingStyleSettings = Schema.Struct({
816816
});
817817
export type SourceControlWritingStyleSettings = typeof SourceControlWritingStyleSettings.Type;
818818

819+
const SourceControlWritingStyleSetting = Schema.Union([
820+
TrimmedString,
821+
SourceControlWritingStyleSettings,
822+
]).pipe(
823+
Schema.decodeTo(
824+
Schema.toType(SourceControlWritingStyleSettings),
825+
SchemaTransformation.transform({
826+
decode: (value) =>
827+
typeof value === "string"
828+
? {
829+
mode: value.length > 0 ? ("custom" as const) : ("repo_conventions" as const),
830+
customInstructions: value,
831+
followChangeRequestTemplates: true,
832+
}
833+
: value,
834+
encode: (value) => value,
835+
}),
836+
),
837+
);
838+
819839
export const BranchNamingMode = Schema.Literals(["static", "semantic", "custom"]);
820840
export type BranchNamingMode = typeof BranchNamingMode.Type;
821841

@@ -964,7 +984,7 @@ export const ProjectSettingsOverrides = Schema.Struct({
964984
sourceControlWriterModelSelection: Schema.optionalKey(Schema.NullOr(ModelSelection)),
965985
reviewGuideModelSelection: Schema.optionalKey(Schema.NullOr(ModelSelection)),
966986
reviewGuideInstructions: Schema.optionalKey(TrimmedString),
967-
sourceControlWritingStyle: Schema.optionalKey(SourceControlWritingStyleSettings),
987+
sourceControlWritingStyle: Schema.optionalKey(SourceControlWritingStyleSetting),
968988
removeAgentCreditsOnMerge: Schema.optionalKey(Schema.Boolean),
969989
branchNamingMode: Schema.optionalKey(BranchNamingMode),
970990
branchNamePrefix: Schema.optionalKey(TrimmedString),
@@ -1204,7 +1224,7 @@ export const ServerSettings = Schema.Struct({
12041224
branchNamePrefix: TrimmedString.pipe(Schema.withDecodingDefault(Effect.succeed("t3"))),
12051225
branchNameInstructions: TrimmedString.pipe(Schema.withDecodingDefault(Effect.succeed(""))),
12061226
removeAgentCreditsOnMerge: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(false))),
1207-
sourceControlWritingStyle: SourceControlWritingStyleSettings.pipe(
1227+
sourceControlWritingStyle: SourceControlWritingStyleSetting.pipe(
12081228
Schema.withDecodingDefault(Effect.succeed({})),
12091229
),
12101230
sourceControlWriterModelSelection: Schema.NullOr(ModelSelection).pipe(
@@ -1432,7 +1452,15 @@ export const ServerSettingsPatch = Schema.Struct({
14321452
* field existed cannot wipe it by editing another key.
14331453
*/
14341454
projectSettingsOverrides: Schema.optionalKey(
1435-
Schema.Record(ProjectId, Schema.NullOr(ProjectSettingsOverrides)),
1455+
Schema.Record(
1456+
ProjectId,
1457+
Schema.NullOr(
1458+
Schema.Struct({
1459+
...ProjectSettingsOverrides.fields,
1460+
sourceControlWritingStyle: Schema.optionalKey(SourceControlWritingStyleSettings),
1461+
}),
1462+
),
1463+
),
14361464
),
14371465
enableAgentDeviceAccess: Schema.optionalKey(Schema.Boolean),
14381466
enableDeviceSupport: Schema.optionalKey(Schema.Boolean),

0 commit comments

Comments
 (0)