Skip to content

Commit 50610af

Browse files
committed
fix(keystone): accept pending cancellation acknowledgements
1 parent 1bf6a86 commit 50610af

2 files changed

Lines changed: 83 additions & 16 deletions

File tree

‎frontend/src/lib/keystone/nativeTransport.test.ts‎

Lines changed: 66 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,72 @@ describe('native Keystone transport', () => {
7070
expect(proxy).toMatchObject({ ok: false, kind: 'invalid' });
7171
expect(JSON.stringify(proxy)).not.toContain('Bad Gateway');
7272
});
73-
it('preserves a completed-versus-cancel race status', async () => {
74-
const fetcher = vi
75-
.fn()
76-
.mockResolvedValue(json({ schemaVersion: 1, run: { status: 'completed' } }));
77-
const result = await createNativeKeystoneTransport(fetcher).page.cancelRun(
78-
'investigation',
79-
'run',
80-
);
81-
expect(result).toEqual({ ok: true, value: { status: 'completed' } });
82-
expect(fetcher.mock.calls[0][1].credentials).toBe('include');
73+
it.each([
74+
['queued', 'queued'],
75+
['running', 'resolving_scope'],
76+
] as const)(
77+
'accepts a cancellation acknowledgement while the native run is still %s',
78+
async (nativeStatus, browserStatus) => {
79+
const fetcher = vi
80+
.fn()
81+
.mockResolvedValue(json({ schemaVersion: 1, run: { status: nativeStatus } }));
82+
const result = await createNativeKeystoneTransport(fetcher).page.cancelRun(
83+
'investigation',
84+
'run',
85+
);
86+
// Cancellation may be acknowledged before the coordinator finishes teardown.
87+
// Preserve a nonterminal result until the terminal event arrives over SSE.
88+
expect(result).toEqual({ ok: true, value: { status: browserStatus } });
89+
expect(fetcher.mock.calls[0][0]).toBe(
90+
'/api/prism/v1/keystone/native/investigations/investigation/runs/run/cancel',
91+
);
92+
expect(fetcher.mock.calls[0][1]).toMatchObject({
93+
method: 'POST',
94+
credentials: 'include',
95+
});
96+
},
97+
);
98+
it.each(['completed', 'partial', 'failed', 'cancelled', 'interrupted'] as const)(
99+
'preserves the native %s status when cancellation races with termination',
100+
async (status) => {
101+
const fetcher = vi.fn().mockResolvedValue(json({ schemaVersion: 1, run: { status } }));
102+
const result = await createNativeKeystoneTransport(fetcher).page.cancelRun(
103+
'investigation',
104+
'run',
105+
);
106+
expect(result).toEqual({ ok: true, value: { status } });
107+
expect(fetcher.mock.calls[0][1].credentials).toBe('include');
108+
},
109+
);
110+
it.each([
111+
null,
112+
{},
113+
{ schemaVersion: 2, run: { status: 'running' } },
114+
{ schemaVersion: 1 },
115+
{ schemaVersion: 1, run: null },
116+
{ schemaVersion: 1, run: {} },
117+
...[
118+
null,
119+
1,
120+
true,
121+
{},
122+
[],
123+
'',
124+
'unknown',
125+
'draft',
126+
'resolving_scope',
127+
'awaiting_confirmation',
128+
'querying',
129+
'composing',
130+
].map((status) => ({
131+
schemaVersion: 1,
132+
run: { status },
133+
})),
134+
])('rejects an invalid native cancellation response: %j', async (body) => {
135+
const result = await createNativeKeystoneTransport(
136+
vi.fn().mockResolvedValue(json(body)),
137+
).page.cancelRun('investigation', 'run');
138+
expect(result.ok).toBe(false);
83139
});
84140
it('creates only the selected export evidence and pins server hash', async () => {
85141
const scope = {

‎frontend/src/lib/keystone/nativeTransport.ts‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -287,15 +287,26 @@ export function createNativeKeystoneTransport(
287287
),
288288
),
289289
cancelRun: (id, run) =>
290-
request(
290+
request<{ status: NodeStatus }>(
291291
`${path(id)}/runs/${encodeURIComponent(run)}/cancel`,
292292
(v) => {
293293
const o = v as { schemaVersion?: unknown; run?: { status?: unknown } };
294-
return o.schemaVersion === 1 &&
295-
typeof o.run?.status === 'string' &&
296-
['completed', 'partial', 'failed', 'cancelled', 'interrupted'].includes(o.run.status)
297-
? { ok: true, value: { status: o.run.status as NodeStatus } }
298-
: { ok: false, error: 'run' };
294+
if (o.schemaVersion !== 1) return { ok: false, error: 'run' };
295+
switch (o.run?.status) {
296+
// A claimed run acknowledges cancellation before teardown finishes.
297+
// Match the native running event projection; SSE retains its detailed phase.
298+
case 'running':
299+
return { ok: true, value: { status: 'resolving_scope' } };
300+
case 'queued':
301+
case 'completed':
302+
case 'partial':
303+
case 'failed':
304+
case 'cancelled':
305+
case 'interrupted':
306+
return { ok: true, value: { status: o.run.status } };
307+
default:
308+
return { ok: false, error: 'run' };
309+
}
299310
},
300311
json({}),
301312
),

0 commit comments

Comments
 (0)