Skip to content

Commit 42ce911

Browse files
committed
fix: resolve repair tombstones through the scoped session address
1 parent ff62082 commit 42ce911

2 files changed

Lines changed: 63 additions & 12 deletions

File tree

‎src/daemon/__tests__/request-router-repair-expired.test.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import { createRequestHandler } from './test-device-runtime-gateway.ts';
1818
import type { DaemonRequest } from '../daemon-request.ts';
1919
import type { SessionState } from '../session-state.ts';
2020
import type { DeviceInfo } from '@agent-device/kernel/device';
21+
import { resolveEffectiveSessionName } from '../session-routing.ts';
22+
import { scopeRequestSession } from '../request-admission.ts';
2123
import { LeaseRegistry } from '../lease-registry.ts';
2224
import { makeSessionStore } from '../../__tests__/test-utils/store-factory.ts';
2325
import { inspectAdReplay } from '@agent-device/ad-replay';
@@ -170,3 +172,45 @@ test('a replay --from continuation on a reaped repair session gets REPAIR_SESSIO
170172

171173
fs.rmSync(root, { recursive: true, force: true });
172174
});
175+
176+
test.each(['cwd', 'tenant'] as const)(
177+
'repair markers use the resolved %s address for expiry and commit failure',
178+
async (scope) => {
179+
for (const failedCommit of [false, true]) {
180+
const { sessionStore, handler } = makeHandler(`router-repair-${scope}-`);
181+
const req = closeRequest('default');
182+
req.meta =
183+
scope === 'cwd'
184+
? { cwd: mkdtempForTestSync('repair-workspace-') }
185+
: { tenantId: 'tenant-a', sessionIsolation: 'tenant' };
186+
const address = resolveEffectiveSessionName(scopeRequestSession(req), sessionStore, {
187+
attachesToSession: false,
188+
});
189+
expect(address).not.toBe('default');
190+
const ref = sessionStore.publish(address, tombstonedSession('default'));
191+
sessionStore.writeRepairTombstone(
192+
ref,
193+
undefined,
194+
failedCommit ? { code: 'EACCES', message: 'script publication denied' } : undefined,
195+
);
196+
sessionStore.retire(ref);
197+
const response = await handler(req);
198+
expect(response).toMatchObject({
199+
ok: false,
200+
error: { code: failedCommit ? 'REPAIR_COMMIT_FAILED' : 'REPAIR_SESSION_EXPIRED' },
201+
});
202+
if (!response.ok && failedCommit)
203+
expect(response.error.message).toContain('script publication denied');
204+
}
205+
},
206+
);
207+
208+
test('a raw default repair marker cannot explain a different workspace session', async () => {
209+
const { sessionStore, handler } = makeHandler('router-repair-workspace-isolation-');
210+
const ref = sessionStore.publish('default', tombstonedSession('default'));
211+
sessionStore.writeRepairTombstone(ref);
212+
sessionStore.retire(ref);
213+
const req = closeRequest('default');
214+
req.meta = { cwd: mkdtempForTestSync('different-repair-workspace-') };
215+
expect(await handler(req)).toMatchObject({ ok: false, error: { code: 'SESSION_NOT_FOUND' } });
216+
});

‎src/daemon/request-router.ts‎

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,9 @@ function repairExpiredIfTombstoned(
616616
sessionStore: SessionStore,
617617
): DaemonError {
618618
if (error.code !== 'SESSION_NOT_FOUND') return error;
619-
const tombstone = sessionStore.readRepairTombstone(req.session);
619+
const address = resolveTombstoneSessionAddress(req, sessionStore);
620+
if (address === undefined) return error;
621+
const tombstone = sessionStore.readRepairTombstone(address);
620622
if (!tombstone) return error;
621623
const reRun = tombstone.sourcePath
622624
? `re-run: replay ${tombstone.sourcePath} --save-script`
@@ -659,22 +661,27 @@ function idleExpiredIfTombstoned(
659661
return normalizeError(sessionIdleExpiredError(tombstone.owner, tombstone));
660662
}
661663

664+
function resolveTombstoneSessionAddress(
665+
req: DaemonRequest,
666+
sessionStore: SessionStore,
667+
): string | undefined {
668+
try {
669+
return resolveEffectiveSessionName(scopeRequestSession(req), sessionStore, {
670+
attachesToSession: false,
671+
});
672+
} catch {
673+
return undefined;
674+
}
675+
}
676+
662677
function readIdleExpiryTombstoneSafely(
663678
req: DaemonRequest,
664679
sessionStore: SessionStore,
665680
): IdleSessionTombstone | undefined {
681+
const address = resolveTombstoneSessionAddress(req, sessionStore);
682+
if (address === undefined) return undefined;
666683
try {
667-
// The address is resolved exactly as the request itself resolved it, tenant scope included: a
668-
// tenant-isolated request keeps its sessions under `<tenant>:<name>`, so reading the raw name
669-
// would miss this request's own marker and could instead surface another tenant's, reporting an
670-
// unrelated device as the one this caller just lost.
671-
const scopedReq = scopeRequestSession(req);
672-
// `attachesToSession: false` is the inventory reading: it never refuses an ambiguous workspace,
673-
// which is right here because this read is a question about an absent session, not a request to
674-
// act through one.
675-
return sessionStore.readIdleExpiryTombstone(
676-
resolveEffectiveSessionName(scopedReq, sessionStore, { attachesToSession: false }),
677-
);
684+
return sessionStore.readIdleExpiryTombstone(address);
678685
} catch {
679686
return undefined;
680687
}

0 commit comments

Comments
 (0)