Skip to content

Commit 9bf0a9f

Browse files
committed
fix(codex): preserve concurrent thread renames
1 parent c72b57d commit 9bf0a9f

5 files changed

Lines changed: 58 additions & 3 deletions

File tree

‎apps/server/src/orchestration/decider.titleRegeneration.test.ts‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,48 @@ const readModel: OrchestrationReadModel = {
4646
};
4747

4848
it.layer(NodeServices.layer)("title regeneration decider", (it) => {
49+
it.effect("applies a title update while the expected title is still current", () =>
50+
Effect.gen(function* () {
51+
const result = yield* decideOrchestrationCommand({
52+
command: {
53+
type: "thread.meta.update",
54+
commandId: CommandId.make("cmd-title-update-current"),
55+
threadId: ThreadId.make("thread-1"),
56+
title: "Provider title",
57+
expectedTitle: "Manual title",
58+
},
59+
readModel,
60+
});
61+
const event = Array.isArray(result) ? result[0] : result;
62+
63+
expect(event.type).toBe("thread.meta-updated");
64+
if (event.type === "thread.meta-updated") {
65+
expect(event.payload.title).toBe("Provider title");
66+
}
67+
}),
68+
);
69+
70+
it.effect("does not replace a title that changed after the caller sampled it", () =>
71+
Effect.gen(function* () {
72+
const result = yield* decideOrchestrationCommand({
73+
command: {
74+
type: "thread.meta.update",
75+
commandId: CommandId.make("cmd-title-update-stale"),
76+
threadId: ThreadId.make("thread-1"),
77+
title: "Provider title",
78+
expectedTitle: "Previous provider title",
79+
},
80+
readModel,
81+
});
82+
const event = Array.isArray(result) ? result[0] : result;
83+
84+
expect(event.type).toBe("thread.meta-updated");
85+
if (event.type === "thread.meta-updated") {
86+
expect(event.payload.title).toBeUndefined();
87+
}
88+
}),
89+
);
90+
4991
it.effect("preserves updatedAt for a stale completion", () =>
5092
Effect.gen(function* () {
5193
const result = yield* decideOrchestrationCommand({

‎apps/server/src/orchestration/decider.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,12 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
805805
thread.branch !== command.expectedBranch
806806
? thread.branch
807807
: command.branch;
808+
const title =
809+
command.title !== undefined &&
810+
command.expectedTitle !== undefined &&
811+
thread.title !== command.expectedTitle
812+
? undefined
813+
: command.title;
808814
const occurredAt = yield* nowIso;
809815
return {
810816
...(yield* withEventBase({
@@ -816,7 +822,7 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
816822
type: "thread.meta-updated",
817823
payload: {
818824
threadId: command.threadId,
819-
...(command.title !== undefined ? { title: command.title } : {}),
825+
...(title !== undefined ? { title } : {}),
820826
...(command.regenerateTitle === true
821827
? {
822828
regenerateTitle: true as const,
@@ -827,7 +833,7 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
827833
},
828834
}
829835
: {}),
830-
...(command.title !== undefined && thread.titleRegeneration != null
836+
...(title !== undefined && thread.titleRegeneration != null
831837
? { titleRegeneration: null }
832838
: {}),
833839
...(command.modelSelection !== undefined

‎apps/server/src/provider/Layers/ProviderThreadDiscovery.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,10 @@ it.effect("refreshes imported metadata without replacing the projected session s
308308
commands[0]?.type === "thread.meta.update" && commands[0].title,
309309
"Fresh Codex title",
310310
);
311+
NodeAssert.equal(
312+
commands[0]?.type === "thread.meta.update" && commands[0].expectedTitle,
313+
originalTitle,
314+
);
311315
NodeAssert.equal(bindings[0]?.status, undefined);
312316
NodeAssert.equal(bindings[0]?.runtimeMode, undefined);
313317
NodeAssert.equal(readModel.threads[0]?.title, originalTitle);

‎apps/server/src/provider/Layers/ProviderThreadDiscovery.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,9 @@ export const synchronizeDiscoveredProviderThreads = Effect.fn(
298298
discovered.updatedAt,
299299
),
300300
threadId: linkedThread.id,
301-
...(providerOwnsTitle && title !== metadata.providerTitle ? { title } : {}),
301+
...(providerOwnsTitle && title !== metadata.providerTitle
302+
? { title, expectedTitle: metadata.providerTitle }
303+
: {}),
302304
});
303305
if (providerOwnsTitle && title !== metadata.providerTitle) {
304306
linkedThread.title = title;

‎packages/contracts/src/orchestration.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,7 @@ const ThreadMetaUpdateCommand = Schema.Struct({
753753
commandId: CommandId,
754754
threadId: ThreadId,
755755
title: Schema.optional(TrimmedNonEmptyString),
756+
expectedTitle: Schema.optional(TrimmedNonEmptyString),
756757
regenerateTitle: Schema.optional(Schema.Literal(true)),
757758
modelSelection: Schema.optional(ModelSelection),
758759
branch: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),

0 commit comments

Comments
 (0)