| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284 |
- import { access, mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises';
- import { tmpdir } from 'node:os';
- import { dirname, join } from 'node:path';
- import { fileURLToPath } from 'node:url';
- import { describe, expect, it, vi } from 'vitest';
- import { resolveAgentCapabilities } from '@mavis/config';
- import type { IRuntimeEvent } from '@mavis/protocol';
- import {
- buildLocalNativeRuntimeTools,
- buildLocalTurnToolSources,
- } from '../../src/api/local-native-tools.js';
- import { withoutLocalMavisCronGuidance } from '../../src/api/local-mavis-cron-guidance.js';
- import { LocalEventSink } from '../../src/events/sink.js';
- import { LocalMcpService } from '../../../local-runtime-v2/src/service/mcp/index.js';
- import { LocalMemoryFacade } from '../../src/memory/local-memory-facade.js';
- import { initSkillService } from '../../src/skills/skill-service.js';
- const packageRoot = join(dirname(fileURLToPath(import.meta.url)), '../..');
- describe('local native turn tools', () => {
- it('exposes native web_search only when the local product owner gate is enabled', () => {
- const base = {
- workspaceRoot: '/workspace',
- agentName: 'mavis',
- webSearchAdapter: { search: vi.fn() },
- };
- expect(buildLocalNativeRuntimeTools(base).map((tool) => tool.def.name)).not.toContain(
- 'web_search',
- );
- expect(
- buildLocalNativeRuntimeTools({ ...base, webSearchEnabled: true }).map(
- (tool) => tool.def.name,
- ),
- ).toContain('web_search');
- });
- it('executes native search through the authenticated gateway without a Matrix MCP server', async () => {
- vi.stubEnv('MAVIS_BUILD_ENV', 'test');
- const dataDir = await mkdtemp(join(tmpdir(), 'native-search-'));
- const mcpService = new LocalMcpService(() => dataDir);
- const fetchImpl = vi.fn<typeof fetch>().mockResolvedValue(
- new Response(
- JSON.stringify({
- code: 0,
- results: [{ title: 'Result', link: 'https://example.test', snippet: 'Evidence' }],
- total: 1,
- }),
- { status: 200 },
- ),
- );
- const input = {
- dataDir,
- workspaceRoot: dataDir,
- agentName: 'mavis',
- sessionId: 'search-session',
- toolsDisabled: false,
- nativeWebSearchEnabled: true,
- mcpService,
- emitBusEvent: vi.fn(),
- threadGoal: { runtimeToolsFor: async () => [] } as never,
- authContext: { accessToken: 'test-search-token' },
- fetchImpl,
- routingContextGetter: () => ({ bedrockLane: 'search-lane' }),
- };
- try {
- const sources = await buildLocalTurnToolSources(input);
- expect(sources.mcpEntries).toEqual([]);
- expect(await mcpService.listServers()).toEqual([]);
- const searches = sources.nativeTools.filter((tool) => tool.def.name === 'web_search');
- expect(searches).toHaveLength(1);
- const result = await searches[0]!.impl.execute(
- { sessionId: 'search-session', turnId: 'search-turn' },
- { query: 'MiniMax' },
- );
- expect(JSON.parse(result.text)).toMatchObject({ total: 1 });
- expect(result.details).toMatchObject({
- server: 'matrix',
- tool: 'web_search',
- mcp: { isError: false },
- });
- const [url, request] = fetchImpl.mock.calls[0]!;
- expect(String(url)).toMatch(/\/mavis\/api\/v1\/mcp\/web_search$/);
- expect(new Headers(request?.headers).get('authorization')).toBe('Bearer test-search-token');
- expect(new Headers(request?.headers).get('bedrock-lane')).toBe('search-lane');
- expect(JSON.parse(request!.body as string)).toEqual({ query: 'MiniMax' });
- const noTools = await buildLocalTurnToolSources({ ...input, toolsDisabled: true });
- expect(noTools.nativeTools).toEqual([]);
- } finally {
- vi.unstubAllEnvs();
- await mcpService.close();
- await rm(dataDir, { recursive: true, force: true });
- }
- });
- it('omits Memory from Custom and factless tool catalogs while retaining full Mavis Memory', async () => {
- const dataDir = await mkdtemp(join(tmpdir(), 'local-native-memory-scope-'));
- try {
- const facade = new LocalMemoryFacade({
- config: () => ({ dataDir, enabled: true }),
- nowMs: () => 1_700_000_000_000,
- });
- const customTools = buildLocalNativeRuntimeTools({
- dataDir,
- workspaceRoot: dataDir,
- agentName: 'writer',
- memoryFacade: facade,
- memoryAgentScopeEnabled: false,
- });
- const factlessTools = buildLocalNativeRuntimeTools({
- dataDir,
- workspaceRoot: dataDir,
- agentName: 'mavis',
- memoryFacade: facade,
- });
- expect(customTools.map((tool) => tool.def.name)).not.toContain('memory');
- expect(factlessTools.map((tool) => tool.def.name)).not.toContain('memory');
- await expect(access(join(dataDir, 'agents', 'writer', 'memory'))).rejects.toThrow();
- const primaryMemory = buildLocalNativeRuntimeTools({
- dataDir,
- workspaceRoot: dataDir,
- agentName: 'mavis',
- memoryFacade: facade,
- memoryAgentScopeEnabled: true,
- memoryReadAgentNames: ['mavis', 'main'],
- }).find((tool) => tool.def.name === 'memory');
- if (!primaryMemory) throw new Error('primary memory tool not found');
- const primaryResult = await primaryMemory.impl.execute(
- { sessionId: 'ses_mavis', turnId: 'turn_mavis', agentName: 'mavis' },
- { target: 'main', operation: 'append', content: 'PRIMARY_SENTINEL' },
- );
- expect(primaryResult.details).toMatchObject({ ok: true });
- const userResult = await primaryMemory.impl.execute(
- { sessionId: 'ses_mavis', turnId: 'turn_mavis', agentName: 'mavis' },
- {
- target: 'user',
- operation: 'append',
- content: 'shared preference',
- reason: 'requested',
- },
- );
- expect(userResult.details).toMatchObject({ ok: true });
- await expect(facade.getAgentMemory('mavis')).resolves.toMatchObject({
- content: expect.stringContaining('PRIMARY_SENTINEL'),
- });
- await expect(facade.getUserMemory()).resolves.toMatchObject({
- content: expect.stringContaining('shared preference'),
- });
- } finally {
- await rm(dataDir, { recursive: true, force: true });
- }
- });
- it('removes Cron from the Mavis Tool contract when the Cron adapter is absent', () => {
- const base = {
- workspaceRoot: '/workspace',
- agentName: 'mavis',
- mavisAgentAdapter: {} as never,
- };
- const withoutCron = buildLocalNativeRuntimeTools(base).find(
- (tool) => tool.def.name === 'mavis',
- );
- const withCron = buildLocalNativeRuntimeTools({
- ...base,
- mavisCronAdapter: {} as never,
- }).find((tool) => tool.def.name === 'mavis');
- expect(withoutCron?.def.description).toContain('agent — local desktop agent roster');
- expect(withoutCron?.def.description).toContain('session — local desktop conversations');
- expect(withoutCron?.def.description).not.toContain('cron — local desktop scheduled tasks');
- expect(withoutCron?.def.description).not.toContain('command: "cron');
- expect(withoutCron?.def.description).not.toMatch(/scheduled tasks/i);
- expect(JSON.stringify(withoutCron?.def.schema)).not.toMatch(/cron/i);
- expect(withCron?.def.description).toContain('cron — local desktop scheduled tasks');
- });
- it('strips Cron-only args from the model-visible Mavis schema when Cron is absent', () => {
- // Description-only stripping still leaked the capability: `cron_id`,
- // `schedule`, `every` and friends stayed in the args schema, which is
- // enough for a model to infer Cron exists and call into an error.
- const base = {
- workspaceRoot: '/workspace',
- agentName: 'mavis',
- mavisAgentAdapter: {} as never,
- };
- const argsOf = (tool: { def: { schema: unknown } } | undefined) =>
- (tool?.def.schema as { properties: { args: { properties: Record<string, unknown> } } })
- .properties.args.properties;
- const withoutCron = argsOf(
- buildLocalNativeRuntimeTools(base).find((tool) => tool.def.name === 'mavis'),
- );
- const withCron = argsOf(
- buildLocalNativeRuntimeTools({ ...base, mavisCronAdapter: {} as never }).find(
- (tool) => tool.def.name === 'mavis',
- ),
- );
- for (const field of [
- 'cron_id',
- 'cron_name',
- 'schedule',
- 'every',
- 'after',
- 'at',
- 'active_hours',
- 'quiet_on_skip',
- 'timezone',
- 'model',
- 'prompt',
- 'session',
- ]) {
- expect(withoutCron).not.toHaveProperty(field);
- expect(withCron).toHaveProperty(field);
- }
- // Session and MCP command arguments remain available.
- for (const field of ['session_id', 'mode', 'agent_name', 'limit', 'cursor', 'enabled']) {
- expect(withoutCron).toHaveProperty(field);
- }
- expect(withoutCron.enabled).toMatchObject({
- type: 'boolean',
- description: expect.stringContaining('mcp create/update'),
- });
- expect(withoutCron.agent_name).toMatchObject({
- description: expect.stringContaining('Required for agent get/update/delete'),
- });
- expect(withoutCron.transport).toMatchObject({
- anyOf: [{ const: 'stdio' }, { const: 'http' }, { const: 'streamable-http' }, { const: 'sse' }],
- description: expect.stringContaining('Do not mix stdio and remote fields.'),
- });
- // No residual Cron wording in retained field descriptions either.
- for (const field of Object.values(withoutCron)) {
- const description = (field as { description?: string }).description;
- if (typeof description === 'string') expect(description).not.toMatch(/cron/i);
- }
- });
- it('derives the Cron-less Mavis contract without mutating the frozen definition', () => {
- // `LocalMavisToolDef` is contract-frozen by a snapshot test; stripping must
- // copy rather than edit it in place, or the desktop host loses Cron too.
- const base = {
- workspaceRoot: '/workspace',
- agentName: 'mavis',
- mavisAgentAdapter: {} as never,
- };
- const withoutCron = buildLocalNativeRuntimeTools(base).find(
- (tool) => tool.def.name === 'mavis',
- );
- if (!withoutCron) throw new Error('mavis tool not found');
- expect(withoutLocalMavisCronGuidance(withoutCron).def).toEqual(withoutCron.def);
- const stillHasCron = buildLocalNativeRuntimeTools({
- ...base,
- mavisCronAdapter: {} as never,
- }).find((tool) => tool.def.name === 'mavis');
- expect(stillHasCron?.def.description).toContain('cron — local desktop scheduled tasks');
- const schema = stillHasCron?.def.schema as {
- properties: { command: { description: string }; args: { description: string; properties: Record<string, unknown> } };
- };
- expect(schema.properties.command.description).toContain('cron resolve-model');
- expect(schema.properties.args.description).toContain('cron once requires exactly one');
- expect(schema.properties.args.properties).toHaveProperty('cron_id');
- expect(schema.properties.args.properties).toHaveProperty('model');
- });
- it('keeps the Memory tool read-only when a session disables memory writes', async () => {
- const getUserMemory = vi.fn(async () => ({ content: 'saved user memory' }));
- const appendMemory = vi.fn();
- const memory = buildLocalNativeRuntimeTools({
- workspaceRoot: '/workspace',
- agentName: 'mavis',
- memoryFacade: { getUserMemory, appendMemory } as never,
- memoryEnabled: true,
- memoryReadEnabled: true,
- memoryWriteEnabled: false,
- memoryAgentScopeEnabled: true,
- }).find((tool) => tool.def.name === 'memory');
- expect(memory).toBeDefined();
- expect(memory?.def.description).toBe(
- 'Read and search local memory. Supports target=user|main|topic.',
- );
- const schema = memory?.def.schema as {
- properties: {
- target: { anyOf: Array<{ const: string }> };
- operation: { anyOf: Array<{ const: string }> };
- };
- };
- expect(schema.properties.target.anyOf.map(({ const: value }) => value)).toEqual([
- 'user',
- 'main',
- 'topic',
- ]);
- expect(schema.properties.operation.anyOf.map(({ const: value }) => value)).toEqual([
- 'read',
- 'search',
- ]);
- const context = { sessionId: 'read-only-memory', turnId: 'turn-1', agentName: 'mavis' };
- const read = await memory!.impl.execute(context, { target: 'user', operation: 'read' });
- const write = await memory!.impl.execute(context, {
- target: 'main',
- operation: 'append',
- content: 'blocked memory',
- });
- expect(read.text).toBe('saved user memory');
- expect(write.details).toMatchObject({
- kind: 'memory',
- ok: false,
- code: 'MEMORY_WRITE_DISABLED',
- });
- expect(appendMemory).not.toHaveBeenCalled();
- });
- it('filters configurable built-in tools and only the delegation entry point', async () => {
- const dataDir = await mkdtemp(join(tmpdir(), 'local-native-capability-tools-'));
- try {
- const { nativeTools: tools } = await buildLocalTurnToolSources({
- toolsDisabled: false,
- workspaceRoot: process.cwd(),
- agentName: 'mavis',
- dataDir,
- eventWriter: new LocalEventSink(),
- sessionId: 'capability-session',
- mcpService: new LocalMcpService(() => dataDir),
- threadGoal: { runtimeToolsFor: () => [] } as never,
- emitBusEvent: () => {},
- builtinCapabilities: resolveAgentCapabilities({
- tools: ['read', 'task_query', 'task_output', 'task_stop'],
- features: { mavis: false, delegation: false },
- }),
- taskAdapter: {} as never,
- taskControlAdapter: {} as never,
- mavisAgentAdapter: {} as never,
- });
- const names = tools.map((tool) => tool.def.name);
- expect(names).toContain('read');
- expect(names).toContain('skill');
- expect(names).toEqual(expect.arrayContaining(['task_query', 'task_output', 'task_stop']));
- for (const hidden of [
- 'write',
- 'edit',
- 'bash',
- 'grep',
- 'glob',
- 'todowrite',
- 'web_fetch',
- 'mavis',
- 'website_deploy',
- 'workspace_semantic_search',
- 'task',
- 'task_append',
- ]) {
- expect(names).not.toContain(hidden);
- }
- } finally {
- await rm(dataDir, { recursive: true, force: true });
- }
- });
- it('removes Mavis-only guidance from task schema when Mavis is not available', async () => {
- const dataDir = await mkdtemp(join(tmpdir(), 'local-native-capability-tools-'));
- try {
- const { nativeTools: tools } = await buildLocalTurnToolSources({
- toolsDisabled: false,
- workspaceRoot: process.cwd(),
- agentName: 'mavis',
- dataDir,
- eventWriter: new LocalEventSink(),
- sessionId: 'task-without-mavis-session',
- mcpService: new LocalMcpService(() => dataDir),
- threadGoal: { runtimeToolsFor: () => [] } as never,
- emitBusEvent: () => {},
- builtinCapabilities: resolveAgentCapabilities({
- tools: ['read'],
- features: { mavis: false, delegation: true },
- }),
- taskAdapter: {} as never,
- });
- const task = tools.find((tool) => tool.def.name === 'task');
- expect(task).toBeDefined();
- const schema = task!.def.schema as {
- properties: { agent_name: { description: string } };
- };
- expect(schema.properties.agent_name.description).toContain(
- 'Use explore, worker, or verifier',
- );
- expect(schema.properties.agent_name.description).not.toContain('general');
- expect(schema.properties.agent_name.description).not.toContain('coder');
- expect(schema.properties.agent_name.description).toContain(
- 'For a known custom Agent, use its stable `requestRef`',
- );
- expect(schema.properties.agent_name.description).toContain('agent:<stable-name>');
- expect(schema.properties.agent_name.description).toContain(
- 'ordinary custom names use their raw stable name',
- );
- expect(schema.properties.agent_name.description).not.toMatch(/\bmavis\b/i);
- } finally {
- await rm(dataDir, { recursive: true, force: true });
- }
- });
- it('exposes the complete task family when delegation is enabled', async () => {
- const dataDir = await mkdtemp(join(tmpdir(), 'local-native-delegation-tools-'));
- try {
- const { nativeTools: tools } = await buildLocalTurnToolSources({
- toolsDisabled: false,
- workspaceRoot: process.cwd(),
- agentName: 'mavis',
- dataDir,
- eventWriter: new LocalEventSink(),
- sessionId: 'delegation-enabled',
- mcpService: new LocalMcpService(() => dataDir),
- threadGoal: { runtimeToolsFor: () => [] } as never,
- emitBusEvent: () => {},
- builtinCapabilities: resolveAgentCapabilities({
- tools: ['bash', 'task_query', 'task_output', 'task_stop'],
- features: { mavis: false, delegation: true },
- }),
- taskAdapter: {} as never,
- taskControlAdapter: {} as never,
- taskAppendAdapter: {} as never,
- });
- const names = tools.map((tool) => tool.def.name);
- expect(names).toContain('bash');
- expect(names).toEqual(
- expect.arrayContaining(['task', 'task_append', 'task_query', 'task_output', 'task_stop']),
- );
- } finally {
- await rm(dataDir, { recursive: true, force: true });
- }
- });
- it('uses the turn reporter for todo event identity without reusing it as the message id', async () => {
- const dataDir = await mkdtemp(join(tmpdir(), 'local-native-sources-'));
- const events: IRuntimeEvent[] = [];
- const nextEventId = vi.fn(() => 'evt_hosted-turn_todo_updated_39');
- const nextRuntimeSeq = vi.fn(() => 39);
- const runtimeToolsFor = vi.fn(async () => []);
- try {
- const sources = await buildLocalTurnToolSources({
- toolsDisabled: false,
- workspaceRoot: process.cwd(),
- agentName: 'mavis',
- dataDir,
- sessionId: 'hosted-session',
- mcpService: new LocalMcpService(() => dataDir),
- threadGoal: { runtimeToolsFor } as never,
- emitBusEvent: () => {},
- });
- const todo = sources.nativeTools.find((tool) => tool.def.name === 'todowrite');
- if (!todo) throw new Error('todowrite tool was not assembled');
- await todo.impl.execute(
- {
- sessionId: 'hosted-session',
- turnId: 'hosted-turn',
- agentName: 'mavis',
- eventWriter: {
- pushRuntime: async () => undefined,
- appendEvents: async () => {
- throw new Error('legacy writer must not own todo event identity');
- },
- },
- reporter: {
- nextEventId,
- nextRuntimeSeq,
- appendEvents: async (next) => {
- events.push(...next);
- },
- },
- },
- { todos: [{ content: 'wire host', status: 'in_progress' }] },
- );
- expect(events).toHaveLength(1);
- expect(events[0]).toMatchObject({
- event_id: 'evt_hosted-turn_todo_updated_39',
- runtime_seq: 39,
- });
- expect(nextEventId).toHaveBeenCalledWith('todo_updated');
- expect(nextRuntimeSeq).toHaveBeenCalledOnce();
- const event = events[0];
- if (!event) throw new Error('todo_updated event was not emitted');
- const streamResp = JSON.parse(event.payload.stream_resp ?? '{}') as {
- agent_message?: { msg_id?: string };
- };
- expect(streamResp.agent_message?.msg_id).toMatch(/^todo_/u);
- expect(streamResp.agent_message?.msg_id).not.toBe(event.event_id);
- expect(sources.mcpEntries).toEqual([]);
- expect(sources.threadGoalTools).toEqual([]);
- expect(runtimeToolsFor).toHaveBeenCalledWith(false, 'hosted-session');
- } finally {
- await rm(dataDir, { recursive: true, force: true });
- }
- });
- it('degrades todo event emission when the turn reporter is unavailable', async () => {
- const dataDir = await mkdtemp(join(tmpdir(), 'local-native-sources-'));
- const emitBusEvent = vi.fn();
- try {
- const sources = await buildLocalTurnToolSources({
- toolsDisabled: false,
- workspaceRoot: process.cwd(),
- agentName: 'mavis',
- dataDir,
- sessionId: 'hosted-session',
- mcpService: new LocalMcpService(() => dataDir),
- threadGoal: { runtimeToolsFor: () => [] } as never,
- emitBusEvent,
- });
- const todo = sources.nativeTools.find((tool) => tool.def.name === 'todowrite');
- if (!todo) throw new Error('todowrite tool was not assembled');
- const result = await todo.impl.execute(
- {
- sessionId: 'hosted-session',
- turnId: 'hosted-turn',
- agentName: 'mavis',
- },
- { todos: [{ content: 'keep working', status: 'in_progress' }] },
- );
- expect(result.details).toMatchObject({ event_emitted: false });
- expect(emitBusEvent).toHaveBeenCalledWith(
- 'todo.event_emit_failed',
- expect.objectContaining({
- sessionId: 'hosted-session',
- turnId: 'hosted-turn',
- reason: 'turn_reporter_unavailable',
- }),
- );
- } finally {
- await rm(dataDir, { recursive: true, force: true });
- }
- });
- it('degrades a rejected todo event append even when failure diagnostics throw', async () => {
- const dataDir = await mkdtemp(join(tmpdir(), 'local-native-sources-'));
- const emitBusEvent = vi.fn((type: string) => {
- if (type === 'todo.event_emit_failed') {
- throw new Error('diagnostic delivery unavailable');
- }
- });
- try {
- const sources = await buildLocalTurnToolSources({
- toolsDisabled: false,
- workspaceRoot: process.cwd(),
- agentName: 'mavis',
- dataDir,
- sessionId: 'hosted-session',
- mcpService: new LocalMcpService(() => dataDir),
- threadGoal: { runtimeToolsFor: () => [] } as never,
- emitBusEvent,
- });
- const todo = sources.nativeTools.find((tool) => tool.def.name === 'todowrite');
- if (!todo) throw new Error('todowrite tool was not assembled');
- const result = await todo.impl.execute(
- {
- sessionId: 'hosted-session',
- turnId: 'hosted-turn',
- agentName: 'mavis',
- reporter: {
- nextEventId: () => 'evt_hosted-turn_todo_updated_1',
- nextRuntimeSeq: () => 1,
- appendEvents: async () => {
- throw new Error('strict delivery rejected event');
- },
- },
- },
- { todos: [{ content: 'keep working', status: 'in_progress' }] },
- );
- expect(result.details).toMatchObject({ event_emitted: false });
- expect(emitBusEvent).toHaveBeenCalledWith(
- 'todo.event_emit_failed',
- expect.objectContaining({
- sessionId: 'hosted-session',
- turnId: 'hosted-turn',
- reason: 'strict delivery rejected event',
- }),
- );
- } finally {
- await rm(dataDir, { recursive: true, force: true });
- }
- });
- it('keeps task control tools available when nested task creation is disabled', async () => {
- const dataDir = await mkdtemp(join(tmpdir(), 'local-native-tools-'));
- try {
- const { nativeTools: tools } = await buildLocalTurnToolSources({
- toolsDisabled: false,
- workspaceRoot: process.cwd(),
- agentName: 'mavis',
- dataDir,
- eventWriter: new LocalEventSink(),
- sessionId: 'task-session',
- mcpService: new LocalMcpService(() => dataDir),
- threadGoal: { runtimeToolsFor: () => [] } as never,
- emitBusEvent: () => {},
- enableTaskTool: false,
- taskAdapter: {} as never,
- taskAppendAdapter: {} as never,
- taskControlAdapter: {
- get: async () => undefined,
- list: async () => ({ items: [] }),
- readOutput: async () => ({ content: '', nextOffset: 0 }),
- stop: async () => undefined,
- },
- });
- const toolNames = tools.map((tool) => tool.def.name);
- expect(toolNames).toEqual(expect.arrayContaining(['bash']));
- expect(toolNames).not.toContain('task');
- // A task child may neither start nor continue a delegation, so the raw
- // v1 source never injects `task_append` there either. The read/control
- // handles stay and are removed later by the final catalog gate.
- expect(toolNames).not.toContain('task_append');
- expect(toolNames).toEqual(expect.arrayContaining(['task_query', 'task_output', 'task_stop']));
- } finally {
- await rm(dataDir, { recursive: true, force: true });
- }
- });
- it('includes todowrite for regular turns (default)', async () => {
- const dataDir = await mkdtemp(join(tmpdir(), 'local-native-tools-'));
- try {
- const { nativeTools: tools } = await buildLocalTurnToolSources({
- toolsDisabled: false,
- workspaceRoot: process.cwd(),
- agentName: 'mavis',
- dataDir,
- eventWriter: new LocalEventSink(),
- sessionId: 'todo-default-session',
- mcpService: new LocalMcpService(() => dataDir),
- threadGoal: { runtimeToolsFor: () => [] } as never,
- emitBusEvent: () => {},
- });
- expect(tools.map((tool) => tool.def.name)).toContain('todowrite');
- } finally {
- await rm(dataDir, { recursive: true, force: true });
- }
- });
- it('drops todowrite for subagent turns (enableTodoWriteTool: false)', async () => {
- // Subagent turns run in hidden child sessions; their todowrite calls would
- // pollute the parent-facing task list stream, so the assembly gates it off
- // the same way enableTaskTool gates nested task creation.
- const dataDir = await mkdtemp(join(tmpdir(), 'local-native-tools-'));
- try {
- const { nativeTools: tools } = await buildLocalTurnToolSources({
- toolsDisabled: false,
- workspaceRoot: process.cwd(),
- agentName: 'mavis',
- dataDir,
- eventWriter: new LocalEventSink(),
- sessionId: 'todo-gated-session',
- mcpService: new LocalMcpService(() => dataDir),
- threadGoal: { runtimeToolsFor: () => [] } as never,
- emitBusEvent: () => {},
- enableTodoWriteTool: false,
- });
- const toolNames = tools.map((tool) => tool.def.name);
- expect(toolNames).not.toContain('todowrite');
- expect(toolNames).toEqual(expect.arrayContaining(['bash', 'read'])); // siblings survive
- } finally {
- await rm(dataDir, { recursive: true, force: true });
- }
- });
- it('wires injected fetchImpl into the local web_fetch tool', async () => {
- const dataDir = await mkdtemp(join(tmpdir(), 'local-native-tools-web-fetch-'));
- const fetchImpl = vi.fn(async () => {
- return new Response('hello from injected fetch', {
- status: 200,
- headers: { 'Content-Type': 'text/plain' },
- });
- });
- try {
- const { nativeTools: tools } = await buildLocalTurnToolSources({
- toolsDisabled: false,
- workspaceRoot: process.cwd(),
- agentName: 'mavis',
- dataDir,
- eventWriter: new LocalEventSink(),
- sessionId: 'web-fetch-session',
- mcpService: new LocalMcpService(() => dataDir),
- threadGoal: { runtimeToolsFor: () => [] } as never,
- emitBusEvent: () => {},
- fetchImpl: fetchImpl as unknown as typeof fetch,
- });
- const webFetch = tools.find((tool) => tool.def.name === 'web_fetch');
- expect(webFetch).toBeDefined();
- const result = await webFetch!.impl.execute(
- { sessionId: 'web-fetch-session', turnId: 'turn-web-fetch', agentName: 'mavis' },
- { url: 'https://example.test/page' },
- );
- expect(fetchImpl).toHaveBeenCalledTimes(1);
- expect(fetchImpl.mock.calls[0]?.[0]).toBe('https://example.test/page');
- expect(result.text).toContain('hello from injected fetch');
- } finally {
- await rm(dataDir, { recursive: true, force: true });
- }
- });
- it('wires the local mavis tool to agent, cron, session, and MCP adapters', async () => {
- const dataDir = await mkdtemp(join(tmpdir(), 'local-native-tools-'));
- const cronCalls: unknown[] = [];
- const cronSessionCalls: unknown[] = [];
- const sessionCalls: unknown[] = [];
- const busEvents: unknown[] = [];
- try {
- const { nativeTools: tools } = await buildLocalTurnToolSources({
- toolsDisabled: false,
- workspaceRoot: process.cwd(),
- agentName: 'mavis',
- dataDir,
- eventWriter: new LocalEventSink(),
- sessionId: 'session-current',
- mcpService: new LocalMcpService(() => dataDir),
- threadGoal: { runtimeToolsFor: () => [] } as never,
- emitBusEvent: (type, payload) => busEvents.push({ type, payload }),
- mavisAgentAdapter: {
- listAgents: async () => ({ agents: [] }),
- createAgent: async () => ({ name: 'created' }),
- getAgent: async (req) => ({ agent: { name: req.name, displayName: req.name } }),
- updateAgent: async (req) => ({ success: true, agent: { name: req.name } }),
- deleteAgent: async () => ({ success: true }),
- },
- mavisCronAdapter: {
- listCrons: async (req) => {
- cronCalls.push(req);
- return { tasks: [{ cronId: 'cron-1', agentName: req.agentName }] };
- },
- getCron: async (req) => ({ task: { cronId: req.cronId } }),
- createCron: async (req) => ({ task: { cronId: 'created', cronName: req.cronName } }),
- updateCron: async (req) => ({ task: { cronId: req.cronId } }),
- deleteCron: async () => ({ success: true }),
- triggerCron: async () => ({ success: true }),
- listCronSessions: async (req) => {
- cronSessionCalls.push(req);
- return {
- sessions: [{ sessionId: 'sess-1', createdAt: 1700000000000 }],
- hasMore: true,
- nextCursor: 'sess-1',
- };
- },
- },
- mavisSessionAdapter: {
- listSessions: async () => ({ sessions: [] }),
- getSession: async (req) => ({
- session: { sessionId: req.sessionId, agentName: 'mavis' },
- }),
- updateSession: async (req) => ({ success: true, session: { sessionId: req.sessionId } }),
- deleteSession: async () => ({ success: true }),
- listMessages: async (req) => {
- sessionCalls.push(req);
- return { messages: [{ content: 'ok' }] };
- },
- },
- });
- const mavis = tools.find((tool) => tool.def.name === 'mavis');
- expect(mavis).toBeDefined();
- const cronResult = await mavis!.impl.execute(
- { sessionId: 'session-current', turnId: 'turn-1', agentName: 'mavis' },
- { command: 'cron list', args: { agent_name: 'me', limit: 1 } },
- );
- expect(JSON.parse(cronResult.text)).toMatchObject({
- ok: true,
- command: 'cron list',
- response: { tasks: [{ cronId: 'cron-1', agentName: 'mavis' }] },
- });
- expect(cronCalls).toEqual([{ agentName: 'mavis', limit: 1 }]);
- const cronSessionsResult = await mavis!.impl.execute(
- { sessionId: 'session-current', turnId: 'turn-1', agentName: 'mavis' },
- {
- command: 'cron sessions',
- args: { cron_id: 'cron-1', cursor: 'sess-0', limit: 1 },
- },
- );
- expect(JSON.parse(cronSessionsResult.text)).toMatchObject({
- ok: true,
- command: 'cron sessions',
- response: {
- sessions: [{ sessionId: 'sess-1', createdAt: 1700000000000 }],
- hasMore: true,
- nextCursor: 'sess-1',
- },
- });
- expect(cronSessionCalls).toEqual([{ cronId: 'cron-1', cursor: 'sess-0', limit: 1 }]);
- const sessionResult = await mavis!.impl.execute(
- { sessionId: 'session-current', turnId: 'turn-1', agentName: 'mavis' },
- { command: 'session messages', args: { session_id: 'me' } },
- );
- expect(JSON.parse(sessionResult.text)).toMatchObject({
- ok: true,
- command: 'session messages',
- response: { messages: [{ content: 'ok' }] },
- });
- expect(sessionCalls).toEqual([{ sessionId: 'session-current' }]);
- const mcpResult = await mavis!.impl.execute(
- { sessionId: 'session-current', turnId: 'turn-1', agentName: 'mavis' },
- {
- command: 'mcp create',
- args: { name: 'docs', transport: 'stdio', command: 'npx', enabled: false },
- },
- );
- expect(JSON.parse(mcpResult.text)).toMatchObject({
- ok: true,
- command: 'mcp create',
- response: { server: { name: 'docs', enabled: false } },
- });
- expect(busEvents).toContainEqual({
- type: 'mcp.settings.changed',
- payload: { operation: 'create', name: 'docs' },
- });
- } finally {
- await rm(dataDir, { recursive: true, force: true });
- }
- });
- it('blocks a disabled built-in Skill from known-name reads without hiding other Skills', async () => {
- const dataDir = await mkdtemp(join(tmpdir(), 'local-native-disabled-skill-'));
- try {
- initSkillService({
- configGetter: () => ({ dataDir, defaultModel: 'test/model' }),
- skillRegistryRoots: [
- {
- id: 'builtin-disabled-skill-test',
- kind: 'builtin',
- rootPath: join(packageRoot, 'assets/skills'),
- },
- ],
- });
- const commonInput = {
- toolsDisabled: false,
- workspaceRoot: process.cwd(),
- agentName: 'mavis',
- dataDir,
- eventWriter: new LocalEventSink(),
- sessionId: 'disabled-skill-session',
- mcpService: new LocalMcpService(() => dataDir),
- threadGoal: { runtimeToolsFor: () => [] } as never,
- emitBusEvent: () => {},
- };
- const { nativeTools: unrestrictedTools } = await buildLocalTurnToolSources(commonInput);
- const unrestrictedSkill = unrestrictedTools.find((tool) => tool.def.name === 'skill');
- const context = {
- sessionId: 'disabled-skill-session',
- turnId: 'turn-skill',
- agentName: 'mavis',
- };
- const available = await unrestrictedSkill!.impl.execute(context, { name: 'lark-tools' });
- expect(available.text).toContain('# Feishu / Lark Tools');
- const { nativeTools: restrictedTools } = await buildLocalTurnToolSources({
- ...commonInput,
- disabledBuiltinSkillNames: ['lark-tools'],
- });
- const restrictedSkill = restrictedTools.find((tool) => tool.def.name === 'skill');
- const blocked = await restrictedSkill!.impl.execute(context, { name: 'lark-tools' });
- const unrelated = await restrictedSkill!.impl.execute(context, { name: 'skill-creator' });
- expect(blocked.text).toBe('Local skill not found: lark-tools');
- expect(unrelated.text).toContain('name: skill-creator');
- } finally {
- await rm(dataDir, { recursive: true, force: true });
- }
- });
- it('does not read a recreated Agent private skill through a frozen model tool', async () => {
- const dataDir = await mkdtemp(join(tmpdir(), 'local-native-frozen-private-'));
- try {
- const root = join(dataDir, 'agents', 'deleted-agent', 'skills', 'private-skill');
- await mkdir(root, { recursive: true });
- await writeFile(
- join(root, 'SKILL.md'),
- '---\nname: private-skill\ndescription: Private skill\n---\nRECREATED AGENT',
- );
- const resolveAgentReadScope = vi.fn(async () => {
- throw new Error('Unexpected live Agent lookup');
- });
- initSkillService({
- configGetter: () => ({ dataDir, defaultModel: 'test/model' }),
- resolveAgentReadScope,
- });
- const { nativeTools } = await buildLocalTurnToolSources({
- toolsDisabled: false,
- workspaceRoot: dataDir,
- agentName: 'deleted-agent',
- dataDir,
- sessionId: 'frozen-private-session',
- skipAgentResolution: true,
- expectedAgentInstanceId: '1a28d970-1f50-488b-84c5-df039713632a',
- mcpService: new LocalMcpService(() => dataDir),
- threadGoal: { runtimeToolsFor: () => [] } as never,
- emitBusEvent: () => {},
- });
- const skill = nativeTools.find((tool) => tool.def.name === 'skill');
- const context = {
- sessionId: 'frozen-private-session',
- turnId: 'turn-2',
- agentName: 'deleted-agent',
- };
- const marker = join(dataDir, 'agents', 'deleted-agent', '.agent-instance-id');
- await writeFile(marker, '1a28d970-1f50-488b-84c5-df039713632a');
- expect((await skill!.impl.execute(context, { name: 'private-skill' })).text).toContain(
- 'RECREATED AGENT',
- );
- await writeFile(marker, '2a28d970-1f50-488b-84c5-df039713632a');
- const result = await skill!.impl.execute(context, { name: 'private-skill' });
- expect(result.text).toBe('Local skill not found: private-skill');
- expect(resolveAgentReadScope).not.toHaveBeenCalled();
- } finally {
- await rm(dataDir, { recursive: true, force: true });
- }
- });
- it('blocks a Skill known from historical messages when the frozen selector closes it', async () => {
- const dataDir = await mkdtemp(join(tmpdir(), 'local-native-frozen-skill-'));
- try {
- initSkillService({
- configGetter: () => ({ dataDir, defaultModel: 'test/model' }),
- skillRegistryRoots: [
- {
- id: 'builtin-frozen-skill-test',
- kind: 'builtin',
- rootPath: join(packageRoot, 'assets/skills'),
- },
- ],
- });
- const { nativeTools } = await buildLocalTurnToolSources({
- toolsDisabled: false,
- workspaceRoot: process.cwd(),
- agentName: 'mavis',
- dataDir,
- eventWriter: new LocalEventSink(),
- sessionId: 'frozen-skill-session',
- mcpService: new LocalMcpService(() => dataDir),
- threadGoal: { runtimeToolsFor: () => [] } as never,
- emitBusEvent: () => {},
- allowedSkillNames: [],
- allowedExtensionSkillNames: [],
- });
- const skill = nativeTools.find((tool) => tool.def.name === 'skill');
- const blocked = await skill!.impl.execute(
- { sessionId: 'frozen-skill-session', turnId: 'turn-2', agentName: 'mavis' },
- { name: 'lark-tools' },
- );
- expect(blocked.text).toBe('Local skill not found: lark-tools');
- expect(blocked.details).toMatchObject({ found: false });
- expect(blocked.text).not.toContain('SKILL.md');
- } finally {
- await rm(dataDir, { recursive: true, force: true });
- }
- });
- it('reads Plugin skill content from the capability view captured for this turn', async () => {
- const dataDir = await mkdtemp(join(tmpdir(), 'local-native-tools-plugin-skill-'));
- try {
- initSkillService({
- configGetter: () => ({ dataDir, defaultModel: 'test/model' }),
- skillRegistryRoots: [],
- });
- const commonInput = {
- toolsDisabled: false,
- workspaceRoot: process.cwd(),
- agentName: 'mavis',
- dataDir,
- eventWriter: new LocalEventSink(),
- sessionId: 'plugin-skill-session',
- mcpService: new LocalMcpService(() => dataDir),
- threadGoal: { runtimeToolsFor: () => [] } as never,
- emitBusEvent: () => {},
- desktopCapabilities: {
- revision: 'plugin-r1',
- plugins: [{ name: 'notes', appProviders: [] }],
- skills: [
- {
- pluginName: 'notes',
- name: 'notes:plugin-notes',
- description: 'Plugin notes workflow',
- content: '# Captured Plugin notes body',
- location: join(dataDir, 'plugins', 'notes', 'skills', 'plugin-notes', 'SKILL.md'),
- sourceKind: 'desktop-plugin',
- },
- ],
- runtimeTools: [],
- runtimeToolBindings: [],
- },
- };
- const { nativeTools: blockedTools } = await buildLocalTurnToolSources({
- ...commonInput,
- allowedSkillNames: [],
- allowedExtensionSkillNames: ['notes:plugin-notes'],
- });
- const blockedSkill = blockedTools.find((tool) => tool.def.name === 'skill');
- const blocked = await blockedSkill!.impl.execute(
- { sessionId: 'plugin-skill-session', turnId: 'turn-1', agentName: 'mavis' },
- { name: 'notes:plugin-notes' },
- );
- expect(blocked.text).toBe('Local skill not found: notes:plugin-notes');
- const { nativeTools: tools } = await buildLocalTurnToolSources({
- ...commonInput,
- allowedSkillNames: [' NOTES:PLUGIN-NOTES '],
- allowedExtensionSkillNames: [' NOTES:PLUGIN-NOTES '],
- });
- const skill = tools.find((tool) => tool.def.name === 'skill');
- const loaded = await skill!.impl.execute(
- { sessionId: 'plugin-skill-session', turnId: 'turn-1', agentName: 'mavis' },
- { name: 'notes:plugin-notes' },
- );
- expect(loaded.text).toContain('# Captured Plugin notes body');
- expect(loaded.details).toMatchObject({
- found: true,
- source: 'desktop-plugin',
- });
- } finally {
- await rm(dataDir, { recursive: true, force: true });
- }
- });
- it('does not load the CU skill through the model skill tool when the turn snapshot is inactive', async () => {
- const dataDir = await mkdtemp(join(tmpdir(), 'local-native-cu-skill-'));
- try {
- initSkillService({
- configGetter: () => ({
- dataDir,
- defaultModel: 'test/model',
- beta: { cuMode: true },
- }),
- skillRegistryRoots: [
- {
- id: 'builtin-cu-skill-test',
- kind: 'builtin',
- rootPath: join(packageRoot, 'assets/skills'),
- },
- ],
- });
- const commonInput = {
- toolsDisabled: false,
- workspaceRoot: process.cwd(),
- agentName: 'mavis',
- dataDir,
- eventWriter: new LocalEventSink(),
- sessionId: 'cu-skill-session',
- mcpService: new LocalMcpService(() => dataDir),
- threadGoal: { runtimeToolsFor: () => [] } as never,
- emitBusEvent: () => {},
- };
- const { nativeTools: inactiveTools } = await buildLocalTurnToolSources({
- ...commonInput,
- cuModeActive: false,
- });
- const inactiveSkill = inactiveTools.find((tool) => tool.def.name === 'skill');
- const blocked = await inactiveSkill!.impl.execute(
- { sessionId: 'cu-skill-session', turnId: 'turn-skill-off', agentName: 'mavis' },
- { name: 'cu-desktop' },
- );
- expect(blocked.text).toBe('Local skill not found: cu-desktop');
- const { nativeTools: activeTools } = await buildLocalTurnToolSources({
- ...commonInput,
- cuModeActive: true,
- });
- const activeSkill = activeTools.find((tool) => tool.def.name === 'skill');
- const loaded = await activeSkill!.impl.execute(
- { sessionId: 'cu-skill-session', turnId: 'turn-skill-on', agentName: 'mavis' },
- { name: 'cu-desktop' },
- );
- expect(loaded.text).toContain('# Computer Use — Desktop Operation Guide');
- } finally {
- await rm(dataDir, { recursive: true, force: true });
- }
- });
- it('blocks direct skill reads when the owning capability is disabled', async () => {
- const dataDir = await mkdtemp(join(tmpdir(), 'local-native-owned-skills-'));
- try {
- initSkillService({
- configGetter: () => ({
- dataDir,
- defaultModel: 'test/model',
- }),
- skillRegistryRoots: [
- {
- id: 'builtin-owned-skill-test',
- kind: 'builtin',
- rootPath: join(packageRoot, 'assets/skills'),
- },
- {
- id: 'agent-owned-skill-test',
- kind: 'builtin',
- scope: 'mavis',
- rootPath: join(packageRoot, '../local-runtime-v2/assets/agents/mavis/skills'),
- },
- ],
- });
- const { nativeTools: tools } = await buildLocalTurnToolSources({
- toolsDisabled: false,
- workspaceRoot: process.cwd(),
- agentName: 'mavis',
- dataDir,
- eventWriter: new LocalEventSink(),
- sessionId: 'owned-skill-session',
- mcpService: new LocalMcpService(() => dataDir),
- threadGoal: { runtimeToolsFor: () => [] } as never,
- emitBusEvent: () => {},
- builtinCapabilities: resolveAgentCapabilities({
- tools: ['read', 'write'],
- skills: ['visual-page'],
- features: { mavis: false, delegation: false },
- }),
- memoryEnabled: false,
- });
- const skill = tools.find((tool) => tool.def.name === 'skill');
- const context = {
- sessionId: 'owned-skill-session',
- turnId: 'owned-skill-turn',
- agentName: 'mavis',
- };
- for (const name of ['mavis', 'create-agent', 'mavis-team']) {
- const blocked = await skill!.impl.execute(context, { name });
- expect(blocked.text).toBe(`Local skill not found: ${name}`);
- }
- const allowed = await skill!.impl.execute(context, { name: 'visual-page' });
- expect(allowed.text).toContain('# Visual Page Skill');
- } finally {
- await rm(dataDir, { recursive: true, force: true });
- }
- });
- it('keeps Mavis-owned skill reads when the Mavis feature owner is selected', async () => {
- const dataDir = await mkdtemp(join(tmpdir(), 'local-native-mavis-skills-'));
- try {
- initSkillService({
- configGetter: () => ({
- dataDir,
- defaultModel: 'test/model',
- }),
- skillRegistryRoots: [
- {
- id: 'builtin-mavis-skill-test',
- kind: 'builtin',
- rootPath: await createSyntheticSkills(dataDir),
- },
- ],
- });
- const { nativeTools: tools } = await buildLocalTurnToolSources({
- toolsDisabled: false,
- workspaceRoot: process.cwd(),
- agentName: 'mavis',
- dataDir,
- eventWriter: new LocalEventSink(),
- sessionId: 'mavis-skill-session',
- mcpService: new LocalMcpService(() => dataDir),
- threadGoal: { runtimeToolsFor: () => [] } as never,
- emitBusEvent: () => {},
- builtinCapabilities: resolveAgentCapabilities({
- tools: [],
- skills: [],
- features: { mavis: true, delegation: false },
- }),
- memoryEnabled: false,
- });
- const skill = tools.find((tool) => tool.def.name === 'skill');
- const context = {
- sessionId: 'mavis-skill-session',
- turnId: 'mavis-skill-turn',
- agentName: 'mavis',
- };
- const mavis = await skill!.impl.execute(context, { name: 'mavis' });
- expect(mavis.text).toContain('# Mavis');
- const createAgent = await skill!.impl.execute(context, { name: 'create-agent' });
- expect(createAgent.text).toContain('# Create Agent');
- expect(createAgent.text).toContain(
- 'Only proceed after the user explicitly asks for or approves creating an agent.',
- );
- } finally {
- await rm(dataDir, { recursive: true, force: true });
- }
- });
- it('reads miniapp-creator through the generic Skill allowlist only when Host lifecycle is available', async () => {
- const dataDir = await mkdtemp(join(tmpdir(), 'local-native-miniapp-skill-'));
- try {
- initSkillService({
- configGetter: () => ({ dataDir, defaultModel: 'test/model' }),
- skillRegistryRoots: [
- {
- id: 'builtin-miniapp-skill-read-test',
- kind: 'builtin',
- rootPath: await createSyntheticSkills(dataDir),
- },
- ],
- });
- const buildSkill = async (miniappAvailable: boolean) => {
- const sessionId = `miniapp-skill-${miniappAvailable ? 'available' : 'unavailable'}`;
- const { nativeTools } = await buildLocalTurnToolSources({
- toolsDisabled: false,
- workspaceRoot: process.cwd(),
- agentName: 'mavis',
- dataDir,
- eventWriter: new LocalEventSink(),
- sessionId,
- mcpService: new LocalMcpService(() => dataDir),
- threadGoal: { runtimeToolsFor: () => [] } as never,
- emitBusEvent: () => {},
- builtinCapabilities: resolveAgentCapabilities({
- tools: ['read', 'write', 'edit', 'bash'],
- skills: [],
- features: { mavis: false, delegation: false, webSearch: false },
- }),
- ...(miniappAvailable ? { miniappAvailable: true } : {}),
- memoryEnabled: false,
- });
- return { sessionId, skill: nativeTools.find((tool) => tool.def.name === 'skill')! };
- };
- const available = await buildSkill(true);
- const availableResult = await available.skill.impl.execute(
- {
- sessionId: available.sessionId,
- turnId: 'miniapp-skill-available-turn',
- agentName: 'mavis',
- },
- { name: 'miniapp-creator' },
- );
- expect(availableResult.text).toContain('# Mini App Creator');
- const unavailable = await buildSkill(false);
- const unavailableResult = await unavailable.skill.impl.execute(
- {
- sessionId: unavailable.sessionId,
- turnId: 'miniapp-skill-unavailable-turn',
- agentName: 'mavis',
- },
- { name: 'miniapp-creator' },
- );
- expect(unavailableResult.text).toBe('Local skill not found: miniapp-creator');
- } finally {
- await rm(dataDir, { recursive: true, force: true });
- }
- });
- });
- // Exercise generic skill ownership with synthetic assets; desktop-only builtins
- // are deliberately absent from the standalone distribution.
- async function createSyntheticSkills(dataDir: string): Promise<string> {
- const root = join(dataDir, 'fixture-skills');
- for (const [name, title] of [['mavis', 'Mavis'], ['create-agent', 'Create Agent'], ['miniapp-creator', 'Mini App Creator']]) {
- const dir = join(root, name!);
- await mkdir(dir, { recursive: true });
- await writeFile(join(dir, 'SKILL.md'), `---\nname: ${name}\ndescription: Synthetic test skill.\n---\n# ${title}\nOnly proceed after the user explicitly asks for or approves creating an agent.\n`);
- }
- return root;
- }
|