Skip to content

Commit 34e73d2

Browse files
committed
fix: bind revision handoffs to gateway snapshots
1 parent bb80e50 commit 34e73d2

6 files changed

Lines changed: 82 additions & 19 deletions

File tree

‎ui/src/app/bootstrap.test.ts‎

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
normalizeInitialApplicationLocation,
1212
resolveInitialApplicationLocation,
1313
} from "./bootstrap-location.ts";
14-
import { bootstrapApplication } from "./bootstrap.ts";
14+
import { bootstrapApplication, createSkillWorkshopRevisionHandoff } from "./bootstrap.ts";
1515
import type { ApplicationContext } from "./context.ts";
1616
import { loadSettings, saveSettings } from "./settings.ts";
1717

@@ -28,6 +28,59 @@ function deferred<T>() {
2828
return { promise, resolve };
2929
}
3030

31+
describe("createSkillWorkshopRevisionHandoff", () => {
32+
it("does not consume a handoff through a replacement snapshot with the same client", () => {
33+
const client = { request: vi.fn() } as unknown as GatewayBrowserClient;
34+
const owner = {
35+
phase: "connected",
36+
client,
37+
hello: null,
38+
} as ApplicationContext["gateway"]["snapshot"];
39+
const replacement = { ...owner };
40+
const handoff = {
41+
sessionKey: "agent:main:revision",
42+
instructions: "Revise the skill.",
43+
owner,
44+
proposalId: "proposal-1",
45+
proposalAgentId: "main",
46+
};
47+
const revisions = createSkillWorkshopRevisionHandoff();
48+
49+
revisions.prepare(handoff);
50+
51+
expect(revisions.consume(handoff.sessionKey, replacement)).toBeNull();
52+
expect(revisions.consume(handoff.sessionKey, owner)).toEqual(handoff);
53+
});
54+
55+
it("clears only the handoff that became stale", () => {
56+
const owner = {
57+
phase: "connected",
58+
client: { request: vi.fn() } as unknown as GatewayBrowserClient,
59+
hello: null,
60+
} as ApplicationContext["gateway"]["snapshot"];
61+
const stale = {
62+
sessionKey: "agent:main:stale",
63+
instructions: "Stale revision.",
64+
owner,
65+
proposalId: "proposal-stale",
66+
proposalAgentId: "main",
67+
};
68+
const current = {
69+
...stale,
70+
sessionKey: "agent:main:current",
71+
instructions: "Current revision.",
72+
proposalId: "proposal-current",
73+
};
74+
const revisions = createSkillWorkshopRevisionHandoff();
75+
76+
revisions.prepare(stale);
77+
revisions.prepare(current);
78+
revisions.clear(stale);
79+
80+
expect(revisions.consume(current.sessionKey, owner)).toEqual(current);
81+
});
82+
});
83+
3184
describe("normalizeInitialApplicationLocation", () => {
3285
it("routes an opaque persisted key without aborting bootstrap", () => {
3386
expect(

‎ui/src/app/bootstrap.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ function createApplicationNavigationPreferences(
204204
};
205205
}
206206

207-
function createSkillWorkshopRevisionHandoff(): ApplicationSkillWorkshopRevisionHandoff {
207+
export function createSkillWorkshopRevisionHandoff(): ApplicationSkillWorkshopRevisionHandoff {
208208
let pending: Parameters<ApplicationSkillWorkshopRevisionHandoff["prepare"]>[0] | null = null;
209209
return {
210210
prepare: (handoff) => {
@@ -218,8 +218,10 @@ function createSkillWorkshopRevisionHandoff(): ApplicationSkillWorkshopRevisionH
218218
pending = null;
219219
return handoff;
220220
},
221-
clear: () => {
222-
pending = null;
221+
clear: (handoff) => {
222+
if (!handoff || pending === handoff) {
223+
pending = null;
224+
}
223225
},
224226
};
225227
}

‎ui/src/app/context.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type { SessionCapability } from "../lib/sessions/index.ts";
99
import type { WorkboardCapability } from "../lib/workboard/capability.ts";
1010
import type { AgentSelectionCapability } from "./agent-selection.ts";
1111
import type { ApplicationConfigCapability } from "./config.ts";
12-
import type { ApplicationGateway } from "./gateway.ts";
12+
import type { ApplicationGateway, ApplicationGatewaySnapshot } from "./gateway.ts";
1313
import type { NativeChatDrafts } from "./native-bridge.ts";
1414
import type { NativeNotificationsCapability } from "./native-notifications.ts";
1515
import type { ApplicationOverlays } from "./overlays.ts";
@@ -58,15 +58,18 @@ export type ApplicationNavigationOptions = Partial<
5858
type SkillWorkshopRevisionHandoff = {
5959
sessionKey: string;
6060
instructions: string;
61-
owner: object;
61+
owner: ApplicationGatewaySnapshot;
6262
proposalId: string;
6363
proposalAgentId: string;
6464
};
6565

6666
export type ApplicationSkillWorkshopRevisionHandoff = {
6767
prepare: (handoff: SkillWorkshopRevisionHandoff) => void;
68-
consume: (sessionKey: string, owner: object | null) => SkillWorkshopRevisionHandoff | null;
69-
clear: () => void;
68+
consume: (
69+
sessionKey: string,
70+
owner: ApplicationGatewaySnapshot | null,
71+
) => SkillWorkshopRevisionHandoff | null;
72+
clear: (handoff?: SkillWorkshopRevisionHandoff) => void;
7073
};
7174

7275
export type ApplicationInitialUserMessage = {

‎ui/src/pages/chat/chat-pane-lifecycle.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,10 @@ export abstract class ChatPaneLifecycle extends ChatPaneBoard {
348348
if (!this.active || !state || !state.connected || state.sessionKey !== expectedSessionKey) {
349349
return;
350350
}
351-
const revision = this.context.skillWorkshopRevision.consume(expectedSessionKey, state.client);
351+
const revision = this.context.skillWorkshopRevision.consume(
352+
expectedSessionKey,
353+
this.context.gateway.snapshot,
354+
);
352355
if (!revision) {
353356
return;
354357
}

‎ui/src/pages/skill-workshop/skill-workshop-page.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ describe("SkillWorkshopPage lifecycle", () => {
437437
expect(context.skillWorkshopRevision.prepare).toHaveBeenCalledWith({
438438
sessionKey: "agent:research:revision",
439439
instructions: "revise it",
440-
owner: context.gateway.snapshot.client,
440+
owner: context.gateway.snapshot,
441441
proposalId: "proposal-owner",
442442
proposalAgentId: "research",
443443
});

‎ui/src/pages/skill-workshop/skill-workshop-page.ts‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -450,25 +450,27 @@ class SkillWorkshopPage extends OpenClawLightDomElement {
450450
if (!sessionKey) {
451451
throw new Error(scope.sessions.state.error ?? "Could not prepare a Skill Workshop thread.");
452452
}
453-
const owner = scope.gateway.snapshot.client;
454-
if (!owner) {
453+
const owner = scope.gateway.snapshot;
454+
if (!owner.client) {
455455
return;
456456
}
457+
const handoff = {
458+
sessionKey,
459+
instructions,
460+
owner,
461+
proposalId: proposal.key,
462+
proposalAgentId: normalizeAgentId(proposal.origin?.agentId ?? proposalAgentId),
463+
};
457464
try {
458-
scope.revision.prepare({
459-
sessionKey,
460-
instructions,
461-
owner,
462-
proposalId: proposal.key,
463-
proposalAgentId: normalizeAgentId(proposal.origin?.agentId ?? proposalAgentId),
464-
});
465+
scope.revision.prepare(handoff);
465466
} catch (error) {
466467
if (!this.isCurrentSourceScope(scope)) {
467468
return;
468469
}
469470
throw error;
470471
}
471472
if (!this.isCurrentSourceScope(scope)) {
473+
scope.revision.clear(handoff);
472474
return;
473475
}
474476
scope.navigate(

0 commit comments

Comments
 (0)