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
7 changes: 1 addition & 6 deletions scripts/agent-benchmark/driver.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import {
benchmarkCcache,
ccacheMeasurements,
runnerToolOutput,
topLevelShellCommand,
} from './run-guards.mjs';

const launchCrashVariant = 'launch-crash';
Expand Down Expand Up @@ -1628,12 +1629,6 @@ function commandEvidence(meta, eventsPath, runDir) {
return { commands, activities, completedEvents, invalidReasons };
}

function topLevelShellCommand(command) {
const trimmed = String(command ?? '').trim();
const match = trimmed.match(/^\/bin\/(?:zsh|bash|sh) -lc (["'])([\s\S]*)\1$/);
return (match?.[2] ?? trimmed).trim();
}

function agentDeviceOpenCommand(meta, appAlive) {
const prefix = agentDeviceCommand(meta, 'open com.appandflow.trailhead --foreground');
if (!meta.deviceTargetingRequired) return prefix;
Expand Down
7 changes: 5 additions & 2 deletions scripts/agent-benchmark/run-guards.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,13 @@ export function benchmarkTarget(config, selection) {
return { key, machine: config.machine, ...value };
}

function topLevelShellCommand(command) {
export function topLevelShellCommand(command) {
const trimmed = String(command ?? '').trim();
const match = trimmed.match(/^\/bin\/(?:zsh|bash|sh) -lc (["'])([\s\S]*)\1$/);
return (match?.[2] ?? trimmed).trim();
if (!match) return trimmed;
const source =
match[1] === '"' ? match[2].replace(/\\(["\\$`\n])/g, (_, char) => (char === '\n' ? '' : char)) : match[2];
return source.trim();
}

export function shellCommandSegments(command) {
Expand Down
20 changes: 20 additions & 0 deletions scripts/agent-benchmark/run-guards.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
benchmarkTiming,
parseBenchmarkTargets,
shellCommandSegments,
topLevelShellCommand,
stimShellProvenanceInvalidReasons,
benchmarkCcache,
assertAndroidDoctorClean,
Expand Down Expand Up @@ -211,6 +212,25 @@ describe('benchmark run guards', () => {
'echo "a && b"',
'stim guide agent',
]);
const search = 'rg -n "run:android|agent-device|emulator" .';
const body = `${search} | sed -n '1,180p'`;
const wrapped = `/bin/zsh -lc ${JSON.stringify(body)}`;
expect(shellCommandSegments(wrapped)).toEqual([search, "sed -n '1,180p'"]);
expect(agentDeviceIsolationInvalidReasons([{ command: wrapped }], 'env expected agent-device ')).toEqual([]);
});

it('decodes one shell quoting layer while preserving proof command boundaries and literal backslashes', () => {
const proof =
'env AGENT_DEVICE_STATE_DIR=/tmp/bench AGENT_DEVICE_SESSION=run agent-device wait text "Offline maps"';
expect(topLevelShellCommand(`/bin/zsh -lc ${JSON.stringify(proof)}`)).toBe(proof);
expect(topLevelShellCommand(`/bin/zsh -lc ${JSON.stringify(`${proof}; agent-device close`)}`)).not.toBe(proof);
const literal = "rg '\\d+\\s' file";
expect(topLevelShellCommand(`/bin/zsh -lc ${JSON.stringify(literal)}`)).toBe(literal);
expect(topLevelShellCommand('/bin/zsh -lc "echo \\$VALUE"')).toBe('echo $VALUE');
expect(topLevelShellCommand('/bin/zsh -lc "echo \\q"')).toBe('echo \\q');
const quotedNewline = "printf '%s' 'before" + '\\'.repeat(2) + "\nafter'";
expect(topLevelShellCommand(`/bin/zsh -lc "${quotedNewline}"`)).toBe("printf '%s' 'before\\\nafter'");
expect(topLevelShellCommand('/bin/zsh -lc "echo before\\\nafter"')).toBe('echo beforeafter');
});

it('rejects setup recovery inside the timer', () => {
Expand Down
Loading