Skip to content

Commit 34f942e

Browse files
authored
fix(agents): tolerate auth mutation before first prepared-model-runtime owner (#111627) (#111701)
1 parent e2e394e commit 34f942e

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

‎src/agents/prepared-model-runtime.lifecycle.test.ts‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ describe("prepared model runtime snapshots", () => {
107107
getTesting().resetPreparedModelRuntimeSnapshotsForTest();
108108
mocks.discoverAuthStorage.mockClear();
109109
mocks.discoverModels.mockClear();
110-
mocks.ensureOpenClawModelsJson.mockClear();
110+
mocks.ensureOpenClawModelsJson.mockReset();
111+
mocks.ensureOpenClawModelsJson.mockResolvedValue({ agentDir: "/tmp/agent", wrote: false });
111112
mocks.buildPreparedModelCatalogSnapshot.mockClear();
112113
mocks.ensureRuntimePluginsLoaded.mockClear();
113114
mocks.loadStaticCatalog.mockClear();
@@ -700,6 +701,22 @@ describe("prepared model runtime snapshots", () => {
700701
}
701702
});
702703

704+
it("does not replay an auth mutation that occurs before the first owner is registered", async () => {
705+
getTesting().setModelRuntimeBuildTimeoutMsForTest(100);
706+
mocks.configuredAgentIds = ["default"];
707+
mocks.ensureRuntimePluginsLoaded.mockImplementationOnce(() => {
708+
mocks.mutationListener?.({ affectsInheritedStores: true });
709+
});
710+
mocks.ensureOpenClawModelsJson
711+
.mockResolvedValueOnce({ agentDir: "/tmp/unused-agent", wrote: false })
712+
.mockImplementationOnce(async () => await new Promise<never>(() => {}));
713+
714+
await expect(
715+
refreshPreparedModelRuntimeSnapshots({}, { gatewayLifecycle: true }),
716+
).resolves.toBeUndefined();
717+
expect(mocks.ensureOpenClawModelsJson).toHaveBeenCalledOnce();
718+
});
719+
703720
it("awaits auth invalidation queued during lifecycle publication", async () => {
704721
mocks.configuredAgentIds = ["default"];
705722
await refreshPreparedModelRuntimeSnapshots({});

‎src/agents/prepared-model-runtime.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,7 @@ function invalidateForAuthMutation(event: AuthMutationEvent): void {
698698
agentDir: normalizeOptionalDir(event.agentDir),
699699
};
700700
const staleError = new Error("prepared model runtime owner is stale after auth mutation");
701+
let invalidatedOwner = false;
701702
for (const owner of owners.values()) {
702703
if (
703704
!normalizedEvent.affectsInheritedStores &&
@@ -706,10 +707,16 @@ function invalidateForAuthMutation(event: AuthMutationEvent): void {
706707
) {
707708
continue;
708709
}
710+
invalidatedOwner = true;
709711
owner.generation += 1;
710712
owner.needsRefresh = true;
711713
owner.refreshError = staleError;
712714
}
715+
if (!invalidatedOwner) {
716+
// A first owner reads the already-published auth snapshot while it builds. Replaying an earlier
717+
// mutation would immediately stale that initial generation even though no prior owner existed.
718+
return;
719+
}
713720
pendingAuthMutations.push(normalizedEvent);
714721
void enqueuePreparedModelRuntimePublication(drainPendingAuthMutations).catch((error: unknown) => {
715722
if (error instanceof PreparedModelRuntimePublicationSupersededError) {

0 commit comments

Comments
 (0)