mirror of
https://github.com/callstack/agent-device.git
synced 2026-09-14 20:06:34 +08:00
dcd8b65d4c
* refactor(daemon): split daemon/types.ts into request and session-state modules `src/daemon/types.ts` served two audiences from one file: the dispatch request shape and the daemon's live session record. It also sat in the only daemon type cycle — it imported `RefFrame` from `ref-frame.ts`, which imported `SessionState` back — so neither file could be read in isolation. Three modules replace it, each importing only downward: - `daemon-request-wire.ts` declares `DaemonWireRequest`: a dispatched request with no `internal` key and no property path to `SessionState` or `DeviceLease`, so a consumer can read a request's command, flags and public metadata without depending on the session record. - `daemon-request.ts` adds the daemon-only half (`DaemonRequestInternal`, which stays unexported) plus the response vocabulary. - `session-state.ts` owns `SessionState` and the shapes only it holds. The cycle is cut by `ref-frame-slot.ts`, declared below both `ref-frame.ts` and `session-state.ts`: it owns the frame VALUE (the class stays unexported, so the type remains nominal and unconstructible from outside), while `ref-frame.ts` keeps every lifetime transition and every `session.refFrame` write. No behavior change: every importer moves to the module owning the symbol it uses, with no re-export shim at the old path. `client-normalizers.ts` takes `SessionRuntimeHints` from `@agent-device/kernel/contracts`, which declares it. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Ujrc8LYmvM249WY8921J1Y * test(daemon): assert the wire request shape cannot reach session state A type-level walk over `DaemonWireRequest` fails `tsc` if the shape regains an `internal` key or grows a property path back to `SessionState` or `DeviceLease`. Positive controls over `DaemonRequest` prove the walk finds both when they are there, so a walk that never matches anything cannot pass by accident. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Ujrc8LYmvM249WY8921J1Y * test(daemon): keep the three over-budget test files at their base length Splitting `daemon/types.ts` turns one combined import into two in every file that used both halves. Three of those test files are already over the 1,000-line tripwire, where the size ratchet allows no growth, so each sheds one line that was carrying nothing: - `snapshot-handler.test.ts` and `find.test.ts` each drop a `toHaveLength` assertion an adjacent `toEqual` on an explicit array literal already makes. - `session-replay-repair-transaction.test.ts` names the filtered close actions instead of wrapping the expression across three lines inside `expect`. No assertion is weakened and no test content is removed. Splitting these files along the modules they mirror is the standing remedy, but none of those modules split here, so it stays out of this change and is tracked in #2353. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Ujrc8LYmvM249WY8921J1Y * chore(gates): point the daemon modularity and wire-compat gates at the split modules R7 now locates the `SessionState` declaration by the declaration itself rather than by a recorded path: `sessionStateWritePressure` measures the merge-base tree too, and that tree still declares it in `daemon/types.ts` — a path constant would measure it as zero pressure and bank the headroom. R10's external-importer ratchet covers all three modules that replaced `daemon/types.ts`, so moving a symbol between them cannot reopen the boundary to a new outside zone. The recorded membership is unchanged: `client-normalizers.ts` and `remote/daemon-artifacts.ts` both import `daemon-request.ts` only. The daemon RPC closure gate waives `DaemonRequest`, `DaemonResponse` and `DaemonArtifact` by path, so those three keys follow the declarations to `daemon-request.ts`. `DaemonRequest`'s rationale now says what it is — the server-side narrowing of the kernel declaration that fixes the wire shape — rather than calling it a re-export alias. The `live-state-shape` and session-resource declaration sites move with `SessionState`; the depgraph lookalike fixture takes a new plausible path now that `daemon/session-state.ts` is the real root. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Ujrc8LYmvM249WY8921J1Y --------- Co-authored-by: Claude <noreply@anthropic.com>
155 lines
5.3 KiB
TypeScript
155 lines
5.3 KiB
TypeScript
import assert from 'node:assert/strict';
|
|
import fs from 'node:fs';
|
|
import http from 'node:http';
|
|
import os from 'node:os';
|
|
import path from 'node:path';
|
|
import { test } from 'vitest';
|
|
import type { ReplaySuiteResult } from '@agent-device/contracts/replay';
|
|
import {
|
|
cleanupDownloadableArtifact,
|
|
trackDownloadableArtifact,
|
|
} from '../../../src/daemon/artifact-tracking.ts';
|
|
import type { DaemonRequest } from '../../../src/daemon/daemon-request.ts';
|
|
import { createDaemonHttpServer } from '../../../src/daemon/server/http-server.ts';
|
|
import { attachRemoteReplayTestArtifacts } from '../../../src/daemon/replay/internal/test-command.ts';
|
|
import {
|
|
downloadRemoteArtifact,
|
|
materializeRemoteArtifacts,
|
|
} from '../../../src/remote/daemon-artifacts.ts';
|
|
import {
|
|
closeLoopbackServer,
|
|
listenOnLoopback,
|
|
skipWhenLoopbackUnavailable,
|
|
} from '../../../src/__tests__/test-utils/loopback.ts';
|
|
|
|
const SUITE_INVOCATION_ID = 'cd5f9c01feec8d70';
|
|
|
|
test('the composed daemon and client path owners report the directory that was materialized', async (t) => {
|
|
if (await skipWhenLoopbackUnavailable(t, 'remote test-artifacts materialization coverage'))
|
|
return;
|
|
|
|
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-device-remote-suite-response-'));
|
|
const daemonSuiteDir = path.join(tempDir, 'daemon-side', SUITE_INVOCATION_ID);
|
|
const daemonAttemptDir = path.join(daemonSuiteDir, 'flow.ad', 'attempt-1');
|
|
const clientRoot = path.join(tempDir, 'caller-side', 'artifacts');
|
|
const clientSuiteDir = path.join(clientRoot, SUITE_INVOCATION_ID);
|
|
fs.mkdirSync(daemonAttemptDir, { recursive: true });
|
|
fs.writeFileSync(path.join(daemonAttemptDir, 'replay.ad'), 'open "Demo"\n');
|
|
fs.writeFileSync(path.join(daemonAttemptDir, 'result.txt'), 'status: passed\n');
|
|
|
|
const request: DaemonRequest = {
|
|
token: 'daemon-token',
|
|
session: 'default',
|
|
command: 'test',
|
|
positionals: [],
|
|
meta: { clientArtifactPaths: { artifactsDir: clientRoot } },
|
|
};
|
|
const suite: ReplaySuiteResult = {
|
|
total: 1,
|
|
executed: 1,
|
|
passed: 1,
|
|
failed: 0,
|
|
skipped: 0,
|
|
notRun: 0,
|
|
durationMs: 1,
|
|
failures: [],
|
|
artifactsDir: daemonSuiteDir,
|
|
tests: [
|
|
{
|
|
file: 'flow.ad',
|
|
session: 'default:test:1',
|
|
status: 'passed',
|
|
durationMs: 1,
|
|
attempts: 1,
|
|
artifactsDir: daemonAttemptDir,
|
|
replayed: 1,
|
|
healed: 0,
|
|
},
|
|
],
|
|
};
|
|
const response = {
|
|
ok: true as const,
|
|
data: attachRemoteReplayTestArtifacts(suite, request),
|
|
};
|
|
const artifact = response.data.artifacts?.[0];
|
|
assert.ok(artifact?.path);
|
|
const artifactId = trackDownloadableArtifact({
|
|
artifactPath: artifact.path,
|
|
artifactType: artifact.artifactType,
|
|
fileName: artifact.fileName,
|
|
});
|
|
response.data.artifacts = [{ ...artifact, artifactId }];
|
|
|
|
const server = await createDaemonHttpServer({
|
|
token: 'daemon-token',
|
|
handleRequest: async () => {
|
|
throw new Error('not exercised: this test only hits the /artifacts route');
|
|
},
|
|
});
|
|
|
|
try {
|
|
const port = await listenOnLoopback(server);
|
|
const materialized = await materializeRemoteArtifacts(
|
|
{ baseUrl: `http://127.0.0.1:${port}`, token: 'daemon-token' },
|
|
request,
|
|
response,
|
|
);
|
|
|
|
assert.equal(materialized.ok, true);
|
|
if (!materialized.ok) return;
|
|
assert.equal(materialized.data?.artifactsDir, clientSuiteDir);
|
|
assert.equal(materialized.data?.artifacts?.[0]?.localPath, clientSuiteDir);
|
|
assert.equal(
|
|
fs.readFileSync(path.join(clientSuiteDir, 'flow.ad', 'attempt-1', 'replay.ad'), 'utf8'),
|
|
'open "Demo"\n',
|
|
);
|
|
assert.equal(
|
|
fs.readFileSync(path.join(clientSuiteDir, 'flow.ad', 'attempt-1', 'result.txt'), 'utf8'),
|
|
'status: passed\n',
|
|
);
|
|
} finally {
|
|
cleanupDownloadableArtifact(artifactId);
|
|
await closeLoopbackServer(server);
|
|
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
test('directory downloads stage beside the destination and finish timeout cleanup before rejecting', async (t) => {
|
|
if (await skipWhenLoopbackUnavailable(t, 'remote test-artifacts atomic staging coverage')) return;
|
|
|
|
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-device-remote-suite-timeout-'));
|
|
const clientRoot = path.join(tempDir, 'caller-side', 'artifacts');
|
|
const server = http.createServer((_req, res) => {
|
|
res.statusCode = 200;
|
|
res.write('partial archive');
|
|
});
|
|
|
|
try {
|
|
const port = await listenOnLoopback(server);
|
|
const download = downloadRemoteArtifact({
|
|
baseUrl: `http://127.0.0.1:${port}`,
|
|
token: 'daemon-token',
|
|
artifactId: 'stalled-directory',
|
|
destinationPath: clientRoot,
|
|
requestScope: {},
|
|
isDirectory: true,
|
|
timeoutMs: 80,
|
|
});
|
|
await new Promise((resolve) => setTimeout(resolve, 30));
|
|
const stagingWhileActive = fs.existsSync(clientRoot)
|
|
? fs.readdirSync(clientRoot).filter((entry) => entry.startsWith('.agent-device-download-'))
|
|
: [];
|
|
await assert.rejects(download, /timed out/);
|
|
assert.equal(stagingWhileActive.length, 1);
|
|
assert.deepEqual(
|
|
fs.existsSync(clientRoot)
|
|
? fs.readdirSync(clientRoot).filter((entry) => entry.startsWith('.agent-device-download-'))
|
|
: [],
|
|
[],
|
|
);
|
|
} finally {
|
|
await closeLoopbackServer(server);
|
|
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
}
|
|
});
|