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
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,86 @@ test('an explicit tool profile remains an exact ceiling over scoped Tool additio
assert.equal(composer.resolveTools?.().filter(({ name }) => name === 'Read').length, 1);
});

test('the composer caches the Host base but reassembles scoped Plugin prompts each step', async () => {
let pluginText = 'FIRST_PLUGIN_PROMPT';
let assemblies = 0;
const composer = createFixtureComposer({
resolveAdditionalSystemPrompt: async (_context, baseText) => {
assemblies += 1;
return {
text: `${baseText}\n\n${pluginText}`,
sourceRevisions: [{ id: 'plugin.system-prompt', revision: `revision-${assemblies}` }],
};
},
});
const context = { sessionId: 'session', turnId: 'turn', cwd: '/workspace' };

const first = await composer.resolveSystemPrompt(context);
pluginText = 'SECOND_PLUGIN_PROMPT';
const second = await composer.resolveSystemPrompt(context);

assert.match(first.text ?? '', /FIRST_PLUGIN_PROMPT/u);
assert.match(second.text ?? '', /SECOND_PLUGIN_PROMPT/u);
assert.equal(assemblies, 2);
assert.deepEqual(
second.sourceRevisions.find(({ id }) => id === 'plugin.system-prompt'),
{ id: 'plugin.system-prompt', revision: 'revision-2' },
);
});

test('the composer preserves scoped dynamic contexts for each model step', async () => {
const contexts = [{ name: 'plugin:context', text: 'EPHEMERAL_CONTEXT' }];
const composer = createFixtureComposer({
resolveAdditionalSystemPrompt: async (_context, baseText) => ({
text: baseText,
contexts,
sourceRevisions: [],
}),
});

const prompt = await composer.resolveSystemPrompt({
sessionId: 'session',
turnId: 'turn',
cwd: '/workspace',
});

assert.deepEqual(prompt.contexts, contexts);
});

test('scoped Plugin Skill contributions join the canonical model inventory', async () => {
const composer = createFixtureComposer({
skills: {
readCanonicalModelInventory: async ({ projectRoot }: { projectRoot: string }) => ({
revision: 'base-revision',
projectRoot,
inventory: [],
diagnostics: [],
discoveryDiagnostics: [],
}),
} as unknown as HostSkillCatalogCoordinator,
pluginSkills: {
snapshot: (sessionId: string) => ({
revision: 4,
skills: [
{
name: 'plugin-probe',
description: `Scoped skill for ${sessionId}`,
instructions: 'PLUGIN_SKILL_INSTRUCTIONS',
},
],
}),
} as never,
});

const prompt = await composer.resolveSystemPrompt({
sessionId: 'session-skill',
turnId: 'turn-skill',
cwd: '/workspace',
});
assert.match(prompt.text ?? '', /plugin-probe/u);
assert.match(prompt.text ?? '', /Scoped skill for session-skill/u);
});

function tool(name: string): MakaTool {
return {
name,
Expand Down Expand Up @@ -184,7 +264,13 @@ function createFixtureComposer(
skills: {
readCanonicalModelInventory: async () => ({ inventory: [] }),
} as unknown as HostSkillCatalogCoordinator,
memory: {} as HostMemoryCoordinator,
memory: {
readPromptProjection: async () => ({
bundleRevision: null,
memoryRevision: null,
body: undefined,
}),
} as unknown as HostMemoryCoordinator,
sessionTodo: {} as SessionTodoToolStore,
builtinTools: {},
...overrides,
Expand Down
89 changes: 89 additions & 0 deletions packages/runtime-host/src/__tests__/plugin-data-runtime.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import assert from 'node:assert/strict';
import { mkdtemp, readFile, rm } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { test } from 'node:test';
import { HostPluginDataRuntime } from '../server/plugin-data-runtime.js';

const namespace = Object.freeze({ extensionId: 'fixture.extension', scopeId: 'session:test' });

test('Plugin data persists CAS mutations and seals credentials at rest', async () => {
const root = await mkdtemp(join(tmpdir(), 'maka-plugin-data-'));
try {
const runtime = new HostPluginDataRuntime(root);
assert.deepEqual(await runtime.read(namespace, 'settings', 'mode'), {
revision: 0,
value: undefined,
});
assert.deepEqual(
await runtime.mutate(namespace, 'settings', [
{ key: 'mode', value: 'strict', expectedRevision: 0 },
]),
{
mode: { revision: 1, value: 'strict' },
},
);
await assert.rejects(
runtime.mutate(namespace, 'settings', [{ key: 'mode', value: 'loose', expectedRevision: 0 }]),
/revision conflict/u,
);
await runtime.mutate(namespace, 'storage', [
{ key: 'state/count', value: 1 },
{ key: 'state/name', value: 'fixture' },
]);
await runtime.commitCredential(namespace, 'token', 'never-plaintext', { provider: 'fixture' });

const restarted = new HostPluginDataRuntime(root);
assert.deepEqual(await restarted.read(namespace, 'settings', 'mode'), {
revision: 1,
value: 'strict',
});
assert.deepEqual(Object.keys(await restarted.list(namespace, 'storage', 'state/')), [
'state/count',
'state/name',
]);
assert.equal(
await restarted.useCredential(namespace, 'token', (secret) => secret),
'never-plaintext',
);

const files = await findJson(root);
const disk = (await Promise.all(files.map((path) => readFile(path, 'utf8')))).join('\n');
assert.equal(disk.includes('never-plaintext'), false);
} finally {
await rm(root, { recursive: true, force: true });
}
});

async function findJson(root: string): Promise<string[]> {
const { readdir } = await import('node:fs/promises');
const output: string[] = [];
const visit = async (directory: string): Promise<void> => {
for (const entry of await readdir(directory, { withFileTypes: true })) {
const path = join(directory, entry.name);
if (entry.isDirectory()) await visit(path);
else if (entry.name.endsWith('.json')) output.push(path);
}
};
await visit(root);
return output;
}
Loading