Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions .ai/contexts/build-release.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,13 @@ stays selected, including on Windows with an 8.3 TEMP path.
The exported `STRONG_MARKERS` retains the study's POSIX/platform variants.
Relative production requires are followed transitively with a per-test visited
set: reaching a production file containing `process.platform` selects the test.
Test helpers are traversed to reach production dependencies, but their own
platform markers do not opt a caller in. Node_modules are excluded. Literal requires
Test helpers are traversed to reach production dependencies. Their
`processes`, `filesystem`, `conpty` and `links` markers also select callers
transitively, as do those markers in production dependencies. Helper-only
`platform` and `winpath` markers do not propagate: `dom-setup.js` must not
select every jsdom caller solely for its platform setup. Other marker categories
remain direct-test checks; production `process.platform` remains transitive.
Node_modules are excluded. Literal requires
resolve files, `.js`, `.cjs` and `index.js`; unresolved literals in test fixtures
are ignored. The read/dependency caches are scoped to one selection call.

Expand All @@ -85,9 +90,19 @@ marker must retain the explicit opt-in: `task-review-regressions.test.js`
retains the #650 reader/worker regression family, and
`derive-project-path.test.js` exercises the separator expression in
`resolveWorktreePath` using real tmpdir paths, without a `process.platform`
branch. No production-module exclusion list is applied. The selection was 368
of 556 files when this was written; selection above roughly 75% requires
reporting contributors before considering justified exclusions.
branch. No production-module exclusion list is applied. The selection is 387
of 570 files at #1029 (379 before the change: 13 added, 5 removed); selection
above roughly 75% requires reporting contributors before considering justified
exclusions.

The FILETIME conversion and comparison live in the pure
`src/main/cli/proc-start.js`, re-exported by `cli-session-state.js`.
`bg-worker-liveness.js` imports the pure module, so roster/UI tests no longer
reach process probes just to compare creation timestamps. With only this split,
selection falls to 341 files; the transitive marker rule brings back 33 and adds
13. `remote-attach.js` still uses `remote-ssh-binary.js` to resolve the actual SSH
executable; its platform-specific executable checks and search paths are not a
pure-helper dependency and remain selected.

To force Windows execution, add a relevant strong marker or the explicit test
source comment `// platform: windows`; extend the exported marker set when a
Expand Down
5 changes: 5 additions & 0 deletions .ai/contexts/cli-session-state.md
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,11 @@ Two rules share that snapshot and must not be confused:
descriptor of an asked session belongs to a conversation that has a PTY
(`exclude.needsProcessTable`), then applies the filter. Nothing is spawned
otherwise.
- **Pure creation-time comparison.** `src/main/cli/proc-start.js` owns
`toCreated`, `procStartMatches`, `WINDOWS_FILETIME_RE` and
`PROC_START_TOLERANCE`, with no filesystem, process or platform dependencies.
`cli-session-state.js` re-exports them; `bg-worker-liveness.js` imports the
pure module directly so roster consumers do not load process probes.
- **Identity, for the guards.** Pid numbers are not identities. A guard treats a
descriptor as own only when: the table holds its pid with a creation time within
`PROC_START_TOLERANCE` (100 ticks, 10 us) of the descriptor's `procStart` (`pidDomain: "win32:anchor"` FILETIME); every link up
Expand Down
19 changes: 4 additions & 15 deletions cli-session-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { execFile } = require('child_process');
const os = require('os');
const path = require('path');
const { bridgeSessionKey } = require('./bridge-session-id');
const { procStartMatches, toCreated, WINDOWS_FILETIME_RE, PROC_START_TOLERANCE } = require('./src/main/cli/proc-start');

const DEFAULT_DIR = path.join(os.homedir(), '.claude', 'sessions');
const STATE_FILE_RE = /^\d+\.json$/;
Expand All @@ -18,11 +19,8 @@ const MAX_LIVE_QUERY_IDS = 200;
const GET_STATUS_PROBE_THROTTLE_MS = 5000;
const MAX_PROBE_PIDS = 64;
const PROBE_TIMEOUT_MS = 5000;
const WINDOWS_FILETIME_RE = /^\d{17,19}$/;
const PROCESS_TABLE_TTL_MS = 3000;
const PROCESS_TABLE_MAX_BUFFER = 4 << 20;
const PROC_START_TOLERANCE = 100n;
const abs = (n) => (n < 0n ? -n : n);

let dir = DEFAULT_DIR;
let activeSessions = null;
Expand Down Expand Up @@ -112,12 +110,6 @@ function defaultReadParentPid(pid) {
}
}

// see .ai/contexts/cli-session-state.md ("Own descendants outside Linux")
function toCreated(value) {
if (value == null || value === '') return null;
try { return BigInt(value); } catch { return null; }
}

const TABLE_LINE_RE = /^\s*(\d+)\s+(\d+)(?:\s+(\d+))?\s*(?:\t(.*))?$/;
const TABLE_LSTART_RE = /^\s*(\d+)\s+(\d+)\s+([A-Za-z]{3}\s+[A-Za-z]{3}\s+\d+\s+[\d:]+\s+\d{4})\s*$/;

Expand Down Expand Up @@ -216,12 +208,6 @@ function tableEntry(pid) {
return processTable && processTable.map ? processTable.map.get(pid) : undefined;
}

function procStartMatches(created, procStart) {
const value = toCreated(created);
return value !== null && typeof procStart === 'string' && WINDOWS_FILETIME_RE.test(procStart)
&& abs(value - BigInt(procStart)) <= PROC_START_TOLERANCE;
}

function readWorkerProcessEvidence() {
const table = processTable?.map;
return { processTableFresh: processTableFresh() && !!table, observedAt: processTable?.observedAt,
Expand Down Expand Up @@ -1142,6 +1128,9 @@ async function liveElsewhereMany(sessionIds, hasPty, ptyPids = () => []) {

module.exports = {
procStartMatches,
toCreated,
WINDOWS_FILETIME_RE,
PROC_START_TOLERANCE,
readWorkerProcessEvidence,
init,
trackScheduleRun,
Expand Down
4 changes: 3 additions & 1 deletion scripts/platform-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const STRONG_MARKERS = {
filesystem: /\bfs\.(?:promises\.)?rename(?:Sync)?\s*\(|\brealpath(?:Sync)?\b|\bbirthtime(?:Ms|Ns)?\b|\bbigint\s*:\s*true/,
};
const MIN_PLATFORM_TESTS = 150;
const TRANSITIVE_MARKERS = ['processes', 'filesystem', 'conpty', 'links'].map(name => STRONG_MARKERS[name]);

function selectPlatformTests(files, { root = path.resolve(__dirname, '..'), minimum = MIN_PLATFORM_TESTS } = {}) {
const sources = new Map();
Expand Down Expand Up @@ -44,7 +45,8 @@ function selectPlatformTests(files, { root = path.resolve(__dirname, '..'), mini
if (visited.has(file)) return false;
visited.add(file);
return relativeDependencies(file).some(dependency =>
(!dependency.startsWith(testDir) && /process\.platform/.test(read(dependency))) || reachesPlatform(dependency, visited));
(!dependency.startsWith(testDir) && /process\.platform/.test(read(dependency)))
|| TRANSITIVE_MARKERS.some(marker => marker.test(read(dependency))) || reachesPlatform(dependency, visited));
};
const selected = files.filter(file => {
const absolute = path.resolve(root, file);
Expand Down
2 changes: 1 addition & 1 deletion src/main/bg-agents/bg-worker-liveness.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { procStartMatches } = require('../../../cli-session-state');
const { procStartMatches } = require('../cli/proc-start');

const WINDOWS_FILETIME_RE = /^\d{17,19}$/;
const timestamp = value => typeof value === 'number' && Number.isFinite(value) ? value
Expand Down
18 changes: 18 additions & 0 deletions src/main/cli/proc-start.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

const WINDOWS_FILETIME_RE = /^\d{17,19}$/;
const PROC_START_TOLERANCE = 100n;
const abs = (n) => (n < 0n ? -n : n);

function toCreated(value) {
if (value == null || value === '') return null;
try { return BigInt(value); } catch { return null; }
}

function procStartMatches(created, procStart) {
const value = toCreated(created);
return value !== null && typeof procStart === 'string' && WINDOWS_FILETIME_RE.test(procStart)
&& abs(value - BigInt(procStart)) <= PROC_START_TOLERANCE;
}

module.exports = { procStartMatches, toCreated, WINDOWS_FILETIME_RE, PROC_START_TOLERANCE };
30 changes: 30 additions & 0 deletions test/platform-test-selection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,36 @@ test('generic sources and physical CRLF line endings stay out', t => {
assert.deepEqual(selector().selectPlatformTests(['test/plain.js', 'test/crlf.js'], options), []);
});

for (const [marker, source] of Object.entries({
processes: 'spawn(command, args)',
filesystem: 'fs.realpathSync(dir)',
conpty: "require('node-pty')",
links: 'fs.symlinkSync(target, link)',
})) {
test(`a transitive test helper with ${marker} selects its caller`, t => {
const options = fixture(t, {
'test/caller.js': "require('./bridge.cjs')",
'test/bridge.cjs': "require('./helper'); require('./caller')",
'test/helper/index.js': source,
});
assert.deepEqual(selector().selectPlatformTests(['test/caller.js'], options), ['test/caller.js']);
});
}

test('helper-only platform and Windows path markers do not select callers', t => {
const options = fixture(t, {
'test/platform.js': "require('./platform.cjs')",
'test/platform.cjs': "process.platform; path.win32; 'win32'",
'test/winpath.js': "require('./winpath.cjs')",
'test/winpath.cjs': String.raw`'C:\repo'; 'RUNNER~1'`,
});
assert.deepEqual(selector().selectPlatformTests(['test/platform.js', 'test/winpath.js'], options), []);
});

test('pure worker liveness does not import platform process probes', () => {
assert.deepEqual(selector().selectPlatformTests(['test/bg-worker-liveness.test.js'], { minimum: 0 }), []);
});

test('carriage returns are stripped before strong-marker matching', t => {
const options = fixture(t, { 'test/normalized.js': 'process.\rplatform' });
assert.deepEqual(selector().selectPlatformTests(['test/normalized.js'], options), ['test/normalized.js']);
Expand Down
Loading