🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
import { describe, expect, it, vi } from 'vitest';
import {
type ILocalSystemService,
LocalSystemExecutionRuntime,
} from '../LocalSystemExecutionRuntime';
const createService = (overrides: Partial<ILocalSystemService> = {}): ILocalSystemService => ({
editLocalFile: vi.fn(),
getCommandOutput: vi.fn(),
globFiles: vi.fn(),
grepContent: vi.fn(),
killCommand: vi.fn(),
listLocalFiles: vi.fn(),
moveLocalFiles: vi.fn(),
readLocalFile: vi.fn(),
readLocalFiles: vi.fn(),
renameLocalFile: vi.fn(),
runCommand: vi.fn(),
searchLocalFiles: vi.fn(),
writeFile: vi.fn(),
...overrides,
});
describe('LocalSystemExecutionRuntime.editFile', () => {
it('reports a failed edit as a failure, with the underlying message intact', async () => {
const service = createService({
editLocalFile: vi.fn().mockResolvedValue({
error: 'The specified old_string was not found in the file',
replacements: 0,
success: false,
}),
});
const runtime = new LocalSystemExecutionRuntime(service);
const output = await runtime.editFile({
all: false,
path: 'C:/foo.ts',
replace: 'bar',
search: 'foo',
});
// `success` drives the tool_end event, `usage.tools.byTool[].errors` and
// the trace inspector's ✓/✗ — an edit that changed nothing must not land
// in any of them as a success.
expect(output.success).toBe(false);
// …while `error` reaches `pluginError`, which the EditLocalFile renderer
// branches on to draw its "Edit Failed" alert.
expect((output.error as { message: string }).message).toBe(
'The specified old_string was not found in the file',
);
// The model reads `content`; it must still carry the real reason.
expect(output.content).toBe('The specified old_string was not found in the file');
expect((output.state as { replacements: number }).replacements).toBe(0);
});
// `executeToolWithRetry` escalates on `error.kind === 'retry'`. These
// failures were never retried while they claimed success; flipping the flag
// must not quietly enrol them in the retry loop.
it('does not mark a failed edit as retryable', async () => {
const service = createService({
editLocalFile: vi.fn().mockResolvedValue({
error: { kind: 'retry', message: 'transient' },
replacements: 0,
success: false,
}),
});
const runtime = new LocalSystemExecutionRuntime(service);
const output = await runtime.editFile({
all: false,
path: 'C:/foo.ts',
replace: 'bar',
search: 'foo',
});
expect(output.success).toBe(false);
expect((output.error as { kind?: string }).kind).toBe('stop');
});
it('returns a formatted success result on a successful edit', async () => {
const service = createService({
editLocalFile: vi.fn().mockResolvedValue({
diffText: 'diff',
linesAdded: 1,
linesDeleted: 1,
replacements: 1,
success: true,
}),
});
const runtime = new LocalSystemExecutionRuntime(service);
const output = await runtime.editFile({
all: false,
path: 'C:/foo.ts',
replace: 'bar',
search: 'foo',
});
expect(output.success).toBe(true);
expect((output.state as { replacements: number }).replacements).toBe(1);
expect(output.content).not.toContain('UNKNOWN_EXEC_ERROR');
});
});
describe('LocalSystemExecutionRuntime.globFiles', () => {
it('forwards limit into the local-system service glob params', async () => {
const service = createService({
globFiles: vi.fn().mockResolvedValue({
files: ['/tmp/a.ts'],
success: true,
total_files: 1,
}),
});
const runtime = new LocalSystemExecutionRuntime(service);
await runtime.globFiles({
directory: '/tmp',
limit: 42,
pattern: '**/*.ts',
});
expect(service.globFiles).toHaveBeenCalledWith({
limit: 42,
pattern: '**/*.ts',
scope: '/tmp',
});
});
it('applies the agent-facing default glob limit', async () => {
const service = createService({
globFiles: vi.fn().mockResolvedValue({ files: [], success: true, total_files: 0 }),
});
const runtime = new LocalSystemExecutionRuntime(service);
await runtime.globFiles({ directory: '/tmp', pattern: '**/*' });
expect(service.globFiles).toHaveBeenCalledWith({
limit: 1000,
pattern: '**/*',
scope: '/tmp',
});
});
it('caps oversized agent-facing glob limits without changing the file search service', async () => {
const service = createService({
globFiles: vi.fn().mockResolvedValue({ files: [], success: true, total_files: 0 }),
});
const runtime = new LocalSystemExecutionRuntime(service);
await runtime.globFiles({ directory: '/tmp', limit: 5000, pattern: '**/*' });
expect(service.globFiles).toHaveBeenCalledWith({
limit: 1000,
pattern: '**/*',
scope: '/tmp',
});
});
});
describe('LocalSystemExecutionRuntime.grepContent', () => {
// Regression: exercise the REAL callService → denormalizeParams path (do NOT
// mock runtime.grepContent). Pre-fix, denormalizeParams collapsed grep args to
// `{cwd, filePattern, output_mode, pattern}` — dropping every filter flag and
// renaming `glob`→`filePattern` (which the desktop `buildGrepArgs` never reads),
// so `-i` / typed / glob-scoped searches silently returned 0 matches.
it('forwards the full param set (glob/type/flags/search-root) to the service', async () => {
const service = createService({
grepContent: vi.fn().mockResolvedValue({
engine: 'rg',
matches: [],
success: true,
total_matches: 0,
}),
});
const runtime = new LocalSystemExecutionRuntime(service);
await runtime.grepContent({
'-A': 3,
'-B': 2,
'-C': 1,
'-i': true,
'-n': true,
'glob': '**/*.ts',
'head_limit': 50,
'multiline': true,
'output_mode': 'content',
'path': '/repo',
'pattern': 'Foo',
'type': 'ts',
});
const forwarded = (service.grepContent as ReturnType<typeof vi.fn>).mock.calls[0][0];
// Every filter flag the LLM set MUST reach the desktop search — the
// desktop `buildGrepArgs` reads these exact keys.
expect(forwarded).toMatchObject({
'-A': 3,
'-B': 2,
'-C': 1,
'-i': true,
'-n': true,
'glob': '**/*.ts',
'head_limit': 50,
'multiline': true,
'output_mode': 'content',
'pattern': 'Foo',
'type': 'ts',
});
// Search root reaches the desktop `resolveSearchPath` via path/scope/cwd.
expect(forwarded.path ?? forwarded.scope ?? forwarded.cwd).toBe('/repo');
// `glob` must NOT be renamed to `filePattern` — the desktop never reads it.
expect(forwarded.filePattern).toBeUndefined();
});
});
describe('LocalSystemExecutionRuntime.readFile', () => {
it('routes uploaded image results onto state.images', async () => {
const service = createService({
readLocalFile: vi.fn().mockResolvedValue({
content: '[Image: cat.png]',
fileType: 'image/png',
filename: 'cat.png',
imageFileId: 'file-1',
imageUrl: 'https://files.example.com/cat.png',
isImage: true,
}),
});
const runtime = new LocalSystemExecutionRuntime(service);
const output = await runtime.readFile({ path: '/tmp/cat.png' });
expect(output.success).toBe(true);
// The uploaded reference flows onto state.images so the MessageContent
// tool-message processor can turn it into an image_url part.
expect(output.state?.images).toEqual([
{ fileId: 'file-1', mediaType: 'image/png', url: 'https://files.example.com/cat.png' },
]);
expect(output.content).toBe('[Image: cat.png]');
});
it('degrades to the text path when the image upload was declined (no url)', async () => {
const service = createService({
readLocalFile: vi.fn().mockResolvedValue({
content: '[Image: cat.png] (upload unavailable — the model cannot view this image)',
fileType: 'image/png',
filename: 'cat.png',
isImage: true,
}),
});
const runtime = new LocalSystemExecutionRuntime(service);
const output = await runtime.readFile({ path: '/tmp/cat.png' });
expect(output.success).toBe(true);
expect(output.state?.images).toBeUndefined();
expect(output.content).toContain('[Image: cat.png]');
});
it('leaves text-file results unchanged (no images on state)', async () => {
const service = createService({
readLocalFile: vi.fn().mockResolvedValue({
content: 'hello',
fileType: 'txt',
filename: 'a.txt',
totalCharCount: 5,
totalLineCount: 1,
}),
});
const runtime = new LocalSystemExecutionRuntime(service);
const output = await runtime.readFile({ path: '/tmp/a.txt' });
expect(output.state?.images).toBeUndefined();
expect(output.content).toContain('hello');
});
// Regression: a default-window read used to render a bare `File: <path>`
// header with no hint that only the first 200 lines were returned, so the
// model re-read the file over and over to discover it was truncated. The
// service always reports the actual window (`loc`) and `totalLineCount`;
// a window that stops before EOF must open with a marker carrying both.
it('marks the returned window and total line count on truncated reads', async () => {
const service = createService({
readLocalFile: vi.fn().mockResolvedValue({
content: 'first 200 lines…',
fileType: 'txt',
filename: 'big.txt',
loc: [0, 200],
totalCharCount: 148_370,
totalLineCount: 2545,
}),
});
const runtime = new LocalSystemExecutionRuntime(service);
const output = await runtime.readFile({ path: '/tmp/big.txt' });
expect(output.content).toBe(
'1 first 200 lines…\n[Showing lines 1-200 of 2545 lines, 148370 characters. To continue, call readFile again with path="/tmp/big.txt" and loc=[200, 400].]',
);
});
it('omits the window marker when the window reaches the end of the file', async () => {
const service = createService({
readLocalFile: vi.fn().mockResolvedValue({
content: 'tail',
fileType: 'txt',
filename: 'big.txt',
loc: [2500, 2545],
totalCharCount: 148_370,
totalLineCount: 2545,
}),
});
const runtime = new LocalSystemExecutionRuntime(service);
const output = await runtime.readFile({ endLine: 2545, path: '/tmp/big.txt', startLine: 2500 });
expect(output.content).toBe('2501 tail');
});
it('prefixes each content line with its 1-based line number', async () => {
const service = createService({
readLocalFile: vi.fn().mockResolvedValue({
content: 'alpha\nbeta\ngamma',
fileType: 'txt',
filename: 'a.txt',
loc: [10, 13],
totalCharCount: 15,
totalLineCount: 13,
}),
});
const runtime = new LocalSystemExecutionRuntime(service);
const output = await runtime.readFile({ loc: [10, 13], path: '/tmp/a.txt' });
expect(output.content).toContain('11 alpha');
expect(output.content).toContain('12 beta');
expect(output.content).toContain('13 gamma');
// State keeps the raw content for UI rendering; only the agent-facing
// text carries the gutter.
expect(output.state?.content).toBe('alpha\nbeta\ngamma');
});
// Regression: when the service cut the content at its char cap, the window
// marker would claim coverage that was never delivered, silently skipping
// the window's tail. The embedded truncation warning already tells the
// model to narrow the range.
it('suppresses the window marker when the service truncated the content', async () => {
const service = createService({
readLocalFile: vi.fn().mockResolvedValue({
content:
'partial\n[content truncated: response was 600000 chars, kept first 500000. Use a smaller line range or grep to narrow down.]',
fileType: 'txt',
filename: 'big.txt',
loc: [0, 1000],
totalCharCount: 3_000_000,
totalLineCount: 2545,
truncated: true,
}),
});
const runtime = new LocalSystemExecutionRuntime(service);
const output = await runtime.readFile({ path: '/tmp/big.txt' });
expect(output.content).not.toContain('(lines');
expect(output.content).toContain('content truncated');
expect(output.state?.truncated).toBe(true);
});
// The cloud sandbox reader returns no `loc` and its startLine/endLine args
// are 1-based inclusive; the fallback window must be normalized before the
// formatter's end-exclusive math, or a full 200-line read is labeled
// "(lines 1-199 of ...)" and the gutter contradicts the marker.
it('normalizes a loc-less 1-based inclusive window (cloud sandbox shape)', async () => {
const service = createService({
readLocalFile: vi.fn().mockResolvedValue({
content: 'some lines',
fileType: 'txt',
filename: 'big.txt',
totalCharCount: 148_370,
totalLineCount: 2545,
}),
});
const runtime = new LocalSystemExecutionRuntime(service);
const output = await runtime.readFile({ endLine: 200, path: '/tmp/big.txt', startLine: 1 });
expect(output.content).toContain(
'[Showing lines 1-200 of 2545 lines, 148370 characters. To continue, call readFile again with path="/tmp/big.txt", startLine=201 and endLine=400.]',
);
expect(output.content).toContain('1 some lines');
});
// The cloud manifest makes startLine/endLine independently optional: an
// endLine-only read stops mid-file and must still get the window marker,
// a startLine-only read runs to EOF and must not.
it('marks an endLine-only loc-less read (cloud sandbox shape)', async () => {
const service = createService({
readLocalFile: vi.fn().mockResolvedValue({
content: 'some lines',
fileType: 'txt',
filename: 'big.txt',
totalCharCount: 148_370,
totalLineCount: 2545,
}),
});
const runtime = new LocalSystemExecutionRuntime(service);
const output = await runtime.readFile({ endLine: 200, path: '/tmp/big.txt' });
expect(output.content).toContain(
'[Showing lines 1-200 of 2545 lines, 148370 characters. To continue, call readFile again with path="/tmp/big.txt", startLine=201 and endLine=400.]',
);
expect(output.content).toContain('1 some lines');
});
it('does not mark a startLine-only loc-less read that runs to EOF', async () => {
const service = createService({
readLocalFile: vi.fn().mockResolvedValue({
content: 'tail',
fileType: 'txt',
filename: 'big.txt',
totalCharCount: 148_370,
totalLineCount: 2545,
}),
});
const runtime = new LocalSystemExecutionRuntime(service);
const output = await runtime.readFile({ path: '/tmp/big.txt', startLine: 2001 });
expect(output.content).not.toContain('(lines');
expect(output.content).toContain('2001 tail');
});
});
describe('LocalSystemExecutionRuntime.executeToolCall — working directory anchoring', () => {
const WD = '/Users/me/project';
// Regression: the desktop executor spawned shells in the app install
// directory because no cwd ever reached the IPC layer. The runner spawns in
// `params.cwd`, so it MUST carry the agent's working directory.
it('injects the working directory as runCommand cwd and maps run_in_background', async () => {
const service = createService({
runCommand: vi.fn().mockResolvedValue({ exit_code: 0, stdout: 'ok', success: true }),
});
const runtime = new LocalSystemExecutionRuntime(service);
await runtime.executeToolCall(
'runCommand',
{ command: 'ls', run_in_background: true },
{ workingDirectory: WD },
);
expect(service.runCommand).toHaveBeenCalledWith(
expect.objectContaining({ command: 'ls', cwd: WD, run_in_background: true }),
);
});
it('keeps a server-injected absolute cwd (gateway path, trustArgsCwd)', async () => {
const service = createService({
runCommand: vi.fn().mockResolvedValue({ exit_code: 0, success: true }),
});
const runtime = new LocalSystemExecutionRuntime(service);
await runtime.executeToolCall(
'runCommand',
{ command: 'ls', cwd: '/from/server' },
{ trustArgsCwd: true },
);
expect(service.runCommand).toHaveBeenCalledWith(
expect.objectContaining({ command: 'ls', cwd: '/from/server' }),
);
});
// Security: an agent with no configured working directory leaves BOTH
// `workingDirectory` and `trustArgsCwd` unset on the renderer path, which is
// exactly the shape a gateway call has. Inferring trust from the missing
// working directory would hand this call the model's own `cwd`, so the
// contract has to be explicit.
it.each([
['runCommand', { command: 'cat passwd', cwd: '/etc' }, 'runCommand'],
['readFile', { cwd: '/etc', path: 'passwd' }, 'readLocalFile'],
] as const)(
'drops a model-supplied cwd for %s when the caller did not vouch for it',
async (api, args, serviceMethod) => {
const service = createService({
[serviceMethod]: vi.fn().mockResolvedValue({ success: true }),
});
const runtime = new LocalSystemExecutionRuntime(service);
await runtime.executeToolCall(api, { ...args });
expect(service[serviceMethod]).toHaveBeenCalledWith(
expect.objectContaining({ cwd: undefined }),
);
},
);
it('drops a model-supplied cwd from grepContent when the caller did not vouch for it', async () => {
const grepContent = vi.fn().mockResolvedValue({ matches: [], success: true, total_matches: 0 });
const runtime = new LocalSystemExecutionRuntime(createService({ grepContent }));
await runtime.executeToolCall('grepContent', { cwd: '/etc', pattern: 'root', scope: '.' });
// `cwd` outranks `path` as the search root downstream, so the model's value
// must not survive — the audited relative scope is what remains.
const forwarded = grepContent.mock.calls[0][0];
expect(forwarded.cwd).not.toBe('/etc');
expect(forwarded.path).not.toBe('/etc');
});
// Security: the out-of-scope intervention audit inspects path/scope fields
// but not `cwd`. A model-supplied cwd on the audited (renderer) path could
// smuggle a relative path outside the workspace — it must be ignored.
it('ignores a model-supplied cwd when a trusted working directory is present', async () => {
const service = createService({
runCommand: vi.fn().mockResolvedValue({ exit_code: 0, success: true }),
});
const runtime = new LocalSystemExecutionRuntime(service);
await runtime.executeToolCall(
'runCommand',
{ command: 'cat passwd', cwd: '/etc' },
{ workingDirectory: WD },
);
expect(service.runCommand).toHaveBeenCalledWith(expect.objectContaining({ cwd: WD }));
});
it('ignores a model-supplied cwd for file ops on the audited path', async () => {
const service = createService({
readLocalFile: vi.fn().mockResolvedValue({ content: '', success: true }),
});
const runtime = new LocalSystemExecutionRuntime(service);
await runtime.executeToolCall(
'readFile',
{ cwd: '/etc', path: 'passwd' },
{ workingDirectory: WD },
);
expect(service.readLocalFile).toHaveBeenCalledWith(expect.objectContaining({ cwd: WD }));
});
it('ignores a model-supplied grep/glob cwd alias on the audited path', async () => {
const service = createService({
globFiles: vi.fn().mockResolvedValue({ files: [], success: true, total_files: 0 }),
grepContent: vi.fn().mockResolvedValue({ matches: [], success: true, total_matches: 0 }),
});
const runtime = new LocalSystemExecutionRuntime(service);
await runtime.executeToolCall(
'globFiles',
{ cwd: '/etc', pattern: '*' },
{ workingDirectory: WD },
);
expect(service.globFiles).toHaveBeenCalledWith(expect.objectContaining({ scope: WD }));
await runtime.executeToolCall(
'grepContent',
{ cwd: '/etc', pattern: 'root' },
{ workingDirectory: WD },
);
const grepForwarded = (service.grepContent as ReturnType<typeof vi.fn>).mock.calls[0][0];
expect(grepForwarded.cwd).toBe(WD);
expect(grepForwarded.path).toBe(WD);
});
it.each([
[
'readFile',
{ path: 'src/a.ts' },
'readLocalFile',
{ cwd: WD, fullContent: undefined, loc: undefined, path: 'src/a.ts' },
],
[
'writeFile',
{ content: 'x', path: 'src/a.ts' },
'writeFile',
{ content: 'x', cwd: WD, path: 'src/a.ts' },
],
[
'editFile',
{ file_path: 'a.ts', new_string: 'b', old_string: 'a' },
'editLocalFile',
{
cwd: WD,
file_path: 'a.ts',
new_string: 'b',
old_string: 'a',
replace_all: undefined,
},
],
[
'moveFiles',
{ items: [{ newPath: 'b.ts', oldPath: 'a.ts' }] },
'moveLocalFiles',
{ cwd: WD, items: [{ newPath: 'b.ts', oldPath: 'a.ts' }] },
],
[
'listFiles',
{ path: '.' },
'listLocalFiles',
{ cwd: WD, limit: undefined, path: '.', sortBy: undefined, sortOrder: undefined },
],
] as const)(
'forwards the working directory as cwd for %s',
async (apiName, args, serviceMethod, expected) => {
const service = createService({
[serviceMethod]: vi.fn().mockResolvedValue({ replacements: 1, success: true }),
});
const runtime = new LocalSystemExecutionRuntime(service);
await runtime.executeToolCall(apiName, args as Record<string, any>, {
workingDirectory: WD,
});
expect(service[serviceMethod as keyof ILocalSystemService]).toHaveBeenCalledWith(expected);
},
);
it('forwards cwd to readFiles (direct service call path)', async () => {
const service = createService({
readLocalFiles: vi.fn().mockResolvedValue([]),
});
const runtime = new LocalSystemExecutionRuntime(service);
await runtime.executeToolCall(
'readFiles',
{ paths: ['a.ts', 'b.ts'] },
{ workingDirectory: WD },
);
expect(service.readLocalFiles).toHaveBeenCalledWith({ cwd: WD, paths: ['a.ts', 'b.ts'] });
});
it('anchors an omitted grep scope onto the working directory with full param forwarding', async () => {
const service = createService({
grepContent: vi.fn().mockResolvedValue({ matches: [], success: true, total_matches: 0 }),
});
const runtime = new LocalSystemExecutionRuntime(service);
await runtime.executeToolCall(
'grepContent',
{ '-i': true, 'glob': '**/*.ts', 'pattern': 'foo' },
{ workingDirectory: WD },
);
const forwarded = (service.grepContent as ReturnType<typeof vi.fn>).mock.calls[0][0];
expect(forwarded).toMatchObject({ '-i': true, 'glob': '**/*.ts', 'pattern': 'foo' });
expect(forwarded.path ?? forwarded.scope ?? forwarded.cwd).toBe(WD);
});
it('resolves glob scope "." to the working directory', async () => {
const service = createService({
globFiles: vi.fn().mockResolvedValue({ files: [], success: true, total_files: 0 }),
});
const runtime = new LocalSystemExecutionRuntime(service);
await runtime.executeToolCall(
'globFiles',
{ pattern: '**/*.ts', scope: '.' },
{ workingDirectory: WD },
);
expect(service.globFiles).toHaveBeenCalledWith({ limit: 100, pattern: '**/*.ts', scope: WD });
});
it('defaults the searchFiles directory to the working directory', async () => {
const service = createService({
searchLocalFiles: vi.fn().mockResolvedValue([]),
});
const runtime = new LocalSystemExecutionRuntime(service);
await runtime.executeToolCall('searchFiles', { keywords: 'foo' }, { workingDirectory: WD });
expect(service.searchLocalFiles).toHaveBeenCalledWith(
expect.objectContaining({ directory: WD, keywords: 'foo', limit: 100 }),
);
});
});
describe('LocalSystemExecutionRuntime.executeToolCall — dispatch', () => {
it('normalizes legacy API aliases (readLocalFile → readFile)', async () => {
const service = createService({
readLocalFile: vi.fn().mockResolvedValue({ content: 'hi', success: true }),
});
const runtime = new LocalSystemExecutionRuntime(service);
const output = await runtime.executeToolCall('readLocalFile', { path: '/tmp/a.txt' });
expect(output?.success).toBe(true);
expect(service.readLocalFile).toHaveBeenCalled();
});
it('routes legacy renameLocalFile through the typed renameFile method', async () => {
const service = createService({
renameLocalFile: vi.fn().mockResolvedValue({ newPath: '/tmp/b.txt', success: true }),
});
const runtime = new LocalSystemExecutionRuntime(service);
const output = await runtime.executeToolCall('renameLocalFile', {
newName: 'b.txt',
path: '/tmp/a.txt',
});
expect(output?.success).toBe(true);
expect(service.renameLocalFile).toHaveBeenCalledWith({ newName: 'b.txt', path: '/tmp/a.txt' });
});
it('maps shell_id to commandId and forwards filter/timeout for getCommandOutput', async () => {
const service = createService({
getCommandOutput: vi.fn().mockResolvedValue({ exit_code: 0, stdout: '', success: true }),
});
const runtime = new LocalSystemExecutionRuntime(service);
await runtime.executeToolCall('getCommandOutput', {
filter: 'ERROR',
shell_id: 'sh-1',
timeout: 5000,
});
expect(service.getCommandOutput).toHaveBeenCalledWith({
filter: 'ERROR',
shell_id: 'sh-1',
timeout: 5000,
});
});
it('returns null for non-local-system tools so callers can fall back', async () => {
const runtime = new LocalSystemExecutionRuntime(createService());
expect(await runtime.executeToolCall('runHeteroTask', {})).toBeNull();
});
});
describe('LocalSystemExecutionRuntime shell session lookups', () => {
// Regression: a shell_id that reaches a device process which never issued it
// (app restarted, or two device processes for one machine) came back as
// "[UNKNOWN_EXEC_ERROR] Tool execution failed" — the service's reason was
// dropped, so the model could not tell a lost session from a failed command.
const notFound =
'Shell ID sh-1a2b3c-4 not found in this device process. It may have been started by a different or restarted process; its output is not available here.';
it('surfaces the not-found reason for getCommandOutput', async () => {
const service = createService({
getCommandOutput: vi.fn().mockResolvedValue({
error: notFound,
output: '',
running: false,
stderr: '',
stdout: '',
success: false,
}),
});
const runtime = new LocalSystemExecutionRuntime(service);
const output = await runtime.executeToolCall('getCommandOutput', { shell_id: 'sh-1a2b3c-4' });
expect(output?.success).toBe(false);
expect(output?.content).toBe(notFound);
});
it('surfaces the not-found reason for killCommand', async () => {
const service = createService({
killCommand: vi.fn().mockResolvedValue({ error: notFound, success: false }),
});
const runtime = new LocalSystemExecutionRuntime(service);
const output = await runtime.executeToolCall('killCommand', { shell_id: 'sh-1a2b3c-4' });
expect(output?.success).toBe(false);
expect(output?.content).toBe(notFound);
});
});
describe('LocalSystemExecutionRuntime.runCommand', () => {
it('surfaces a pre-spawn failure reason instead of UNKNOWN_EXEC_ERROR', async () => {
// A command that never starts has no process, so no stderr and no exit
// code — the exact shape every Local Sandbox refusal takes. The reason used
// to be dropped, leaving the user (and the model) with a generic failure
// and nothing to act on.
const service = createService({
runCommand: vi.fn().mockResolvedValue({
error:
'Local Sandbox requires a working directory. Set one for this agent (or topic) and run the command again.',
success: false,
}),
});
const runtime = new LocalSystemExecutionRuntime(service);
const output = await runtime.runCommand({ command: 'whoami' } as never);
expect(output.content).toContain('Local Sandbox requires a working directory');
expect(output.content).not.toContain('UNKNOWN_EXEC_ERROR');
});
it('keeps stdout when a failed command reports neither an error nor stderr', async () => {
// A CLI that prints its failure on stdout and exits non-zero, reported
// with `success: false` and nothing else to explain it. The generic
// fallback used to replace the only diagnostic the model could act on.
const service = createService({
runCommand: vi.fn().mockResolvedValue({
exit_code: 1,
stderr: '',
stdout: '✗ Provider deepseek check failed\nError: InvalidProviderAPIKey\n',
success: false,
}),
});
const runtime = new LocalSystemExecutionRuntime(service);
const output = await runtime.executeToolCall('runCommand', {
command: 'lh provider test deepseek',
});
expect(output?.content).toContain('Error: InvalidProviderAPIKey');
expect(output?.content).not.toContain('UNKNOWN_EXEC_ERROR');
});
it('reports whether the command was actually sandboxed', async () => {
const service = createService({
runCommand: vi.fn().mockResolvedValue({
exit_code: 0,
sandboxed: true,
stdout: 'srt-sandbox',
success: true,
}),
});
const runtime = new LocalSystemExecutionRuntime(service);
const output = await runtime.runCommand({ command: 'whoami' } as never);
expect((output.state as { sandboxed?: boolean }).sandboxed).toBe(true);
});
});