Files
callstack__agent-device/scripts/depgraph/affected.test.ts
Michał Pierzchała e832325e87 refactor(substrate): split host mechanics into @agent-device/host-kit capability ports (#2088)
* refactor: split generic host mechanics into @agent-device/host-kit (#2082 W1)

The shared src/utils closure that blocked the platform-family moves lands
on declared owners: generic host mechanics form a new private
@agent-device/host-kit package between kernel and capture-kit, and
capture-kit keeps capture, snapshot, and recording behavior, depending on
host-kit for the mechanics it needs. tar-stream and yauzl move with the
archive code.

Every seam's exported subpaths are pinned in package-boundaries.test.ts,
the layering model ranks the new zone, R13's allow-list names it, and each
seam carries an exact eager-closure row. ADR-0019's substrate amendment
describes the layout.

Tests that mocked two of the moved modules separately became duplicate
same-seam vi.mock factories, where the second silently replaced the first;
those are merged, and the mocks that production code reaches past are
pinned at their injection points instead.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018VngeKZH6zBuJzNBk5YzUH

* refactor(host-kit): one narrow capability port per export

The four technical barrels (exec/fs/values/request) grouped by category
rather than by capability, so a consumer needing one mechanic evaluated
unrelated ones. Each export is now a single capability over the host
machine: command, process, diagnostics, retry, archive, file, request,
version. A port re-exports only what a consumer of that capability uses,
and every port carries its own eager-closure row.

Most of the old values barrel was never host mechanics. Pure record
readers, config-source values, result text, memoization, async scoping,
coordinate validation, and device-scope parsing touch no process, file, or
environment, so they join kernel's other primitives instead.

Closures fall accordingly: capture-kit's png-worker-client from 20 to 10,
png-resize from 28 to 18, session-teardown from 79 to 68, and the CLI from
386 to 380.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018VngeKZH6zBuJzNBk5YzUH

* chore: drop the migration inventories and trim the touched comments

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018VngeKZH6zBuJzNBk5YzUH

* docs: trim the touched host-kit and mutation-lane comments

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018VngeKZH6zBuJzNBk5YzUH

* docs: keep tool directives only in the touched files

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018VngeKZH6zBuJzNBk5YzUH

* docs: keep tool directives only across the touched tree

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018VngeKZH6zBuJzNBk5YzUH

* fix: point the Swift parity comment at the real TS twin and test

The W1 move rewrote this citation to packages/contracts/src/mobile-snapshot-semantics.ts,
which does not exist: the module went to capture-kit while isTapPointInsideViewport itself
went to packages/contracts/src/snapshot-visibility.ts. The TS test line was left pointing at
the pre-move path. Both now resolve.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018VngeKZH6zBuJzNBk5YzUH

* fix: repoint comment citations at the homes this refactor moved them to

The W1 move left ~20 comment citations pointing at src/utils/*.ts and
src/request/*.ts paths that no longer exist. Each now names the capability
port that owns the symbol, which survives further file moves:

  exec -> host-kit/command          host-process, owner-identity -> host-kit/process
  diagnostics -> host-kit/diagnostics   atomic-file, process-lock -> host-kit/file
  retry -> host-kit/retry           request progress/cancel -> host-kit/request
  version -> host-kit/version       ttl-memo, source-value, parsing, device-isolation,
                                    keyed-lock, success-text -> kernel subpaths

Comment-only; no closure, budget, or behavior change. ADR citations are left
as written, being dated records of the decision rather than live references.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018VngeKZH6zBuJzNBk5YzUH

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-08-28 07:46:48 +02:00

235 lines
8.7 KiB
TypeScript

import assert from 'node:assert/strict';
import { execFileSync } from 'node:child_process';
import { readFileSync } from 'node:fs';
import path from 'node:path';
import { test } from 'node:test';
import { selectChecks } from '../check-affected/model.ts';
import { resolveImportEdges } from '../layering/model.ts';
import {
bound,
collectDependents,
commandsReaching,
formatBounded,
guaranteeRowsForFile,
rankByFanIn,
weakDirectDependents,
zoneBreakdown,
} from './affected.ts';
import {
computeBlastRadius,
formatBlastRadius,
guaranteeRows,
parseInvocation,
parseRouteEntries,
} from './affected-run.ts';
import { collapseEdges, type GraphNode } from './model.ts';
const repoRoot = execFileSync('git', ['rev-parse', '--show-toplevel'], {
encoding: 'utf8',
}).trim();
function edgesOf(entries: Record<string, string>) {
return collapseEdges(resolveImportEdges(new Map(Object.entries(entries))));
}
function nodesOf(entries: Record<string, Partial<GraphNode>>): Map<string, GraphNode> {
return new Map(
Object.entries(entries).map(([id, node]) => [
id,
{ id, zone: 'core', loc: 1, fanIn: 0, fanOut: 0, cycle: -1, ...node },
]),
);
}
test('collectDependents separates direct importers from transitive ones', () => {
const edges = edgesOf({
'src/core/target.ts': 'export const t = 1;',
'src/core/direct.ts': "export { t } from './target.ts';",
'src/core/far.ts': "import { t } from './direct.ts';\nexport const f = t;",
'src/core/unrelated.ts': 'export const u = 1;',
});
assert.deepEqual(collectDependents('src/core/target.ts', edges), {
direct: ['src/core/direct.ts'],
all: ['src/core/direct.ts', 'src/core/far.ts'],
transitiveOnly: ['src/core/far.ts'],
});
});
test('collectDependents ignores type-only and dynamic dependents, which are counted separately', () => {
const edges = edgesOf({
'src/core/target.ts': 'export type T = string;\nexport const t = 1;',
'src/core/typed.ts': "import type { T } from './target.ts';\nexport type U = T;",
'src/core/lazy.ts': "export const load = () => import('./target.ts');",
});
assert.deepEqual(collectDependents('src/core/target.ts', edges).all, []);
assert.deepEqual(weakDirectDependents('src/core/target.ts', edges), { type: 1, dynamic: 1 });
});
test('zoneBreakdown and rankByFanIn order by weight, then name', () => {
const nodes = nodesOf({
'src/daemon/a.ts': { zone: 'daemon-server', fanIn: 3 },
'src/daemon/b.ts': { zone: 'daemon-server', fanIn: 9 },
'src/core/c.ts': { zone: 'core', fanIn: 9 },
});
const files = ['src/daemon/a.ts', 'src/daemon/b.ts', 'src/core/c.ts'];
assert.deepEqual(zoneBreakdown(files, nodes), [
{ zone: 'daemon-server', count: 2 },
{ zone: 'core', count: 1 },
]);
assert.deepEqual(
rankByFanIn(files, nodes).map((entry) => entry.file),
['src/core/c.ts', 'src/daemon/b.ts', 'src/daemon/a.ts'],
);
});
test('commandsReaching follows the dynamic import a route uses to load its handler', () => {
const edges = edgesOf({
'src/daemon/handlers/session.ts':
"import { helper } from '../../utils/helper.ts';\nexport const h = helper;",
'src/daemon/handlers/find.ts': 'export const f = 1;',
'src/utils/helper.ts': 'export const helper = 1;',
'src/daemon/chain.ts': [
"export const routes = { session: () => import('./handlers/session.ts'),",
" find: () => import('./handlers/find.ts') };",
].join('\n'),
});
const chains = [
{ command: 'open', route: 'session', entry: 'src/daemon/handlers/session.ts' },
{ command: 'find', route: 'find', entry: 'src/daemon/handlers/find.ts' },
];
assert.deepEqual(
commandsReaching('src/utils/helper.ts', chains, edges).map((chain) => chain.command),
['open'],
);
// The entry module itself counts as part of its own chain.
assert.deepEqual(
commandsReaching('src/daemon/handlers/find.ts', chains, edges).map((chain) => chain.command),
['find'],
);
});
test('guaranteeRowsForFile matches module-qualified cells only, on the whole path', () => {
const rows = [
{
path: 'runtime-selector',
guarantee: 'disambiguation',
kind: 'runtime',
via: 'packages/selectors/src/internal/resolve.ts#resolveSelectorChain',
},
{
path: 'runtime-ref',
guarantee: 'occlusion',
kind: 'runtime',
via: 'packages/selectors/src/internal/resolve-extra.ts#other',
},
{
path: 'native-ref',
guarantee: 'errorTaxonomy',
kind: 'delegated',
via: 'runner errors fall back to tree resolution',
},
];
assert.deepEqual(
guaranteeRowsForFile('packages/selectors/src/internal/resolve.ts', rows).map(
(row) => row.guarantee,
),
['disambiguation'],
);
});
test('bounded lists disclose what they hid', () => {
assert.deepEqual(bound([1, 2, 3, 4], 2), { shown: [1, 2], hidden: 2 });
assert.equal(formatBounded(bound([1, 2, 3, 4], 2), String), '1, 2 (+2 more)');
assert.equal(formatBounded(bound([1, 2], 5), String), '1, 2');
assert.equal(formatBounded(bound([], 5), String), '(none)');
});
test('every daemon route in the real route table resolves to a handler entry', () => {
const chainFile = 'src/daemon/request-handler-chain.ts';
const source = readFileSync(path.join(repoRoot, chainFile), 'utf8');
const entries = parseRouteEntries(source, chainFile);
const declared = [...source.matchAll(/(\w+): defineDaemonRoute\(/g)].map((match) => match[1]!);
assert.ok(declared.length > 0, 'route table shape changed: no defineDaemonRoute entries found');
assert.deepEqual([...entries.keys()].sort(), [...declared].sort());
assert.equal(entries.get('generic'), 'src/daemon/request-generic-dispatch.ts');
});
test('the real guarantee matrix contributes module-qualified rows', () => {
const rows = guaranteeRows();
assert.ok(rows.length > 0);
assert.ok(
rows.some((row) => row.via.startsWith('src/') && row.via.includes('#')),
'expected at least one runtime cell naming a module',
);
});
test('a flag keeps its value whichever side of the path it is on', () => {
const expected = { file: 'src/utils/exec.ts', json: true, limit: 25 };
assert.deepEqual(parseInvocation(['src/utils/exec.ts', '--json', '--limit', '25']), expected);
assert.deepEqual(parseInvocation(['--json', '--limit', '25', 'src/utils/exec.ts']), expected);
assert.deepEqual(parseInvocation(['src/utils/exec.ts', '--json', '--limit=25']), expected);
assert.deepEqual(parseInvocation(['src/utils/exec.ts']), {
file: 'src/utils/exec.ts',
json: false,
limit: 10,
});
assert.deepEqual(parseInvocation(['--help']), { help: true });
});
test('parseInvocation rejects a missing path, a second path, and a non-positive limit', () => {
assert.throws(() => parseInvocation(['--json']), /missing <path>/);
assert.throws(() => parseInvocation(['a.ts', 'b.ts']), /expected one <path>, got 2: a\.ts b\.ts/);
assert.throws(() => parseInvocation(['a.ts', '--limit', '0']), /positive integer/);
assert.throws(() => parseInvocation(['a.ts', '--limit', 'lots']), /positive integer/);
});
test('text output bounds every list and says what it hid', () => {
const text = formatBlastRadius(
{
file: 'src/utils/exec.ts',
zone: 'utils',
fanIn: 81,
fanOut: 3,
dependents: {
direct: ['src/a.ts', 'src/b.ts', 'src/c.ts'],
transitive: ['src/d.ts'],
byZone: [{ zone: 'utils', count: 4 }],
topByFanIn: [{ file: 'src/a.ts', fanIn: 7 }],
weakDirect: { type: 2, dynamic: 1 },
},
gates: { checks: ['lint', 'typecheck'], failOpen: false },
commands: [{ command: 'click', route: 'interaction', entry: 'src/daemon/handlers/x.ts' }],
liveOwners: { manifest: 'test/…/coverage-manifest.ts', present: false, owners: [] },
guaranteeRows: [],
},
2,
);
assert.match(text, /3 direct, 1 transitive .*2 type-only and 1 dynamic/);
assert.match(text, /direct: {2}src\/a\.ts, src\/b\.ts \(\+1 more\)/);
assert.match(text, /gates for this set: lint, typecheck/);
assert.match(text, /is not in this tree/);
});
test('blast radius over the real tree agrees with check:affected and stays under 10s', async () => {
const file = 'packages/host-kit/src/internal/exec.ts';
const started = Date.now();
const radius = await computeBlastRadius(repoRoot, file);
const elapsed = Date.now() - started;
assert.ok(elapsed < 10_000, `blast radius took ${elapsed}ms`);
assert.ok(radius.dependents.direct.length > 0);
// The gate plan is check:affected's own selection over the dependent set, so it can only ever
// be a superset of the plan for the file alone — a contradiction would mean an edit selected
// fewer gates the more files it touched.
const single = selectChecks({ changedFiles: [file] });
for (const check of single.checks) assert.ok(radius.gates.checks.includes(check), check);
});