Skip to content

Commit 67fc011

Browse files
strzhaoclaude
andcommitted
fix(daemon): name the command and session in the generic-route sessionless refusal
A session-bound generic-route command (screenshot, scroll, ...) with no live session answered with the generic "No active session. Run open first." — an answer that names neither the command that failed nor the session address that would fix it. A caller retrying the same command (screenshot --out repeatedly) learns nothing from each failure. The refusal now answers in the session-guard shape the rest of the daemon already uses ("<command> requires active session \"<address>\". Run open first."), so the echoed address is the one the caller can pass to --session. The refusal also precedes the capture, and a new test pins that --out stays untouched by it. Co-Authored-By: Claude Code <[email protected]>
1 parent a68693c commit 67fc011

2 files changed

Lines changed: 76 additions & 2 deletions

File tree

‎packages/daemon/src/server/__tests__/request-router-screenshot.test.ts‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,3 +669,72 @@ test('screenshot --pixel-density keeps overlay refs aligned to scaled iOS simula
669669
});
670670
}
671671
});
672+
673+
test('a sessionless screenshot refusal names the command and the session, and writes no file', async () => {
674+
// No session is published: the generic route must stop the request before any device work and
675+
// answer with the session a caller can reopen, not a generic "no active session" that sends the
676+
// caller retrying the same command.
677+
const sessionStore = makeSessionStore('agent-device-router-screenshot-sessionless-');
678+
const runtime = screenshotRuntimeFixture();
679+
const handler = createRequestHandler({
680+
host: createTestHostEnvironment(),
681+
createCommandSurface: testCreateCommandSurface,
682+
logPath: path.join(mkdtempForTestSync('daemon'), 'daemon.log'),
683+
token: 'test-token',
684+
sessionStore,
685+
leaseRegistry: new LeaseRegistry(),
686+
deviceInventoryGateways: createTestDeviceInventoryGateways(),
687+
deviceRuntimeGateway: runtime.gateway,
688+
trackDownloadableArtifact: () => 'artifact-id',
689+
});
690+
691+
const outDir = mkdtempForTestSync('agent-device-screenshot-sessionless-out-');
692+
const outPath = path.join(outDir, 'shot.png');
693+
const response = await handler({
694+
token: 'test-token',
695+
session: 'default',
696+
command: 'screenshot',
697+
positionals: [],
698+
flags: { out: outPath },
699+
});
700+
701+
expect(response.ok).toBe(false);
702+
if (response.ok) return;
703+
expect(response.error.code).toBe('SESSION_NOT_FOUND');
704+
expect(response.error.message).toBe(
705+
'screenshot requires active session "default". Run open first.',
706+
);
707+
// The refusal precedes the capture, so --out stays untouched: no stray file at the requested
708+
// destination for the caller to reconcile with the rejection.
709+
expect(fs.existsSync(outPath)).toBe(false);
710+
expect(runtime.captureScreenshot.mock.calls).toEqual([]);
711+
});
712+
713+
test('every generic-route sessionless refusal names its own command', async () => {
714+
// The refusal lives on the shared generic route, so it must stay command-aware: a second
715+
// session-bound command reading "screenshot" would mislead the caller about what failed.
716+
const sessionStore = makeSessionStore('agent-device-router-scroll-sessionless-');
717+
const handler = createRequestHandler({
718+
host: createTestHostEnvironment(),
719+
createCommandSurface: testCreateCommandSurface,
720+
logPath: path.join(mkdtempForTestSync('daemon'), 'daemon.log'),
721+
token: 'test-token',
722+
sessionStore,
723+
leaseRegistry: new LeaseRegistry(),
724+
deviceInventoryGateways: createTestDeviceInventoryGateways(),
725+
trackDownloadableArtifact: () => 'artifact-id',
726+
});
727+
728+
const response = await handler({
729+
token: 'test-token',
730+
session: 'default',
731+
command: 'scroll',
732+
positionals: ['down'],
733+
flags: {},
734+
});
735+
736+
expect(response.ok).toBe(false);
737+
if (response.ok) return;
738+
expect(response.error.code).toBe('SESSION_NOT_FOUND');
739+
expect(response.error.message).toBe('scroll requires active session "default". Run open first.');
740+
});

‎packages/daemon/src/server/request-router.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import { timingSafeStringEqual } from '@agent-device/host-kit/transport';
2222
import {
2323
type ResponseCost,
2424
errorResponse,
25-
noActiveSessionError,
2625
type DaemonResponse,
2726
type DaemonResponseData,
2827
} from '@agent-device/kernel/contracts';
@@ -660,7 +659,13 @@ async function dispatchGenericForLockedScope(params: {
660659
const { lockedScope, logPath, sessionStore, androidObservation, createCommandSurface } = params;
661660
const ref = sessionStore.lookup(lockedScope.sessionName);
662661
if (!ref) {
663-
return noActiveSessionError();
662+
// The generic route is where a session-bound command (screenshot, scroll, …) stops before any
663+
// device work, so the refusal names the command and the session address a caller can reopen —
664+
// the same answer shape as the file-command session guard — instead of a generic "no session".
665+
return errorResponse(
666+
'SESSION_NOT_FOUND',
667+
`${lockedScope.req.command} requires active session "${lockedScope.sessionName}". Run open first.`,
668+
);
664669
}
665670
const session = sessionStore.requireCurrent(ref);
666671

0 commit comments

Comments
 (0)