Skip to content
Open
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
4 changes: 2 additions & 2 deletions .github/workflows/windows-recovery.yml
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,8 @@ jobs:
$exitCode = $LASTEXITCODE
if ($exitCode -ne 0) { exit $exitCode }
$output = Get-Content "$env:RUNNER_TEMP/skill-catalog.tap"
if ($output -notcontains '# tests 91' -or $output -notcontains '# pass 91' -or $output -notcontains '# skipped 0') {
Write-Error 'Skill catalog gate did not run exactly 91 passing Windows tests'
if ($output -notcontains '# tests 93' -or $output -notcontains '# pass 93' -or $output -notcontains '# skipped 0') {
Write-Error 'Skill catalog gate did not run exactly 93 passing Windows tests'
exit 1
}

Expand Down
76 changes: 76 additions & 0 deletions packages/runtime-host/src/__tests__/artifact-coordinator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@
* under the License.
*/

import { encodeArtifactProjection } from '../protocol/artifact.js';
import { assertMaximalJsonPages } from './fixtures/json-pages.js';
import {
ARTIFACT_PAGE_MAX_ITEMS,
ARTIFACT_RESULT_MAX_BYTES,
type ArtifactQueryInput,
type ArtifactQueryResult,
} from '../protocol/index.js';

import assert from 'node:assert/strict';
import { createHash } from 'node:crypto';
import { mkdtemp, rm } from 'node:fs/promises';
Expand Down Expand Up @@ -514,3 +523,70 @@ test('Session Guests can read only shared attachment Artifacts from their grante
function digest(bytes: Uint8Array): `sha256:${string}` {
return `sha256:${createHash('sha256').update(bytes).digest('hex')}`;
}

test('Artifact listing preserves the maximal byte-limited prefix across continuations', async () => {
const root = await mkdtemp(join(tmpdir(), 'maka-artifact-list-pages-'));
const capability = await resolveStorageRoot({ path: root, kind: 'interactive' });
const owner = await tryAcquireInteractiveRootOwner(capability);
assert.ok(owner);
const store = await openInteractiveArtifactStoreForWrite(owner.lease);
try {
for (let index = 0; index < 20; index += 1) {
await store.create({
id: `artifact-${index}`,
sessionId: 'session-1',
turnId: 'turn-1',
name: `附件-${index}.txt`,
kind: 'file',
content: Buffer.from('content'),
summary: '文"\\🙂'.repeat(600),
source: 'tool_result',
now: index,
});
}
const expected = (await store.listPage('session-1', { offset: 0, limit: 128 })).records.map(
encodeArtifactProjection,
);
const coordinator = new HostArtifactCoordinator(
store,
() => assert.fail('query must not drain'),
new SessionAdmissionGate(),
{ probeSessionRemoval: async () => ({ kind: 'present' }) },
);
const pages: Extract<ArtifactQueryResult, { kind: 'page' }>[] = [];
let input: ArtifactQueryInput = { kind: 'list_start', sessionId: 'session-1' };
let end = 0;
do {
const outcome = await coordinator.handlers['artifact.query'](input, connectionContext);
assert.ok(outcome.ok && outcome.result.kind === 'page');
const page = outcome.result;
assert.ok(page.artifacts.length > 0);
pages.push(page);
end += page.artifacts.length;
assert.equal(page.nextCursor, end < expected.length ? String(end) : null);
if (page.nextCursor === null) break;
input = {
kind: 'list_continue',
sessionId: 'session-1',
revision: page.revision,
cursor: page.nextCursor,
};
} while (end < expected.length);
assert.ok(pages.length > 1);
assert.ok(pages[0]!.artifacts.length < ARTIFACT_PAGE_MAX_ITEMS);
assertMaximalJsonPages(pages, expected, {
maxBytes: ARTIFACT_RESULT_MAX_BYTES,
maxItems: ARTIFACT_PAGE_MAX_ITEMS,
items: (page) => page.artifacts,
candidate: (page, artifacts, end) => ({
...page,
artifacts,
nextCursor: end < expected.length ? String(end) : null,
}),
});
} finally {
store.close();
await owner.close();
await rm(root, { recursive: true, force: true });
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
* under the License.
*/

import { assertMaximalJsonPages } from './fixtures/json-pages.js';
import { EXTERNAL_SESSION_PAGE_MAX_ITEMS } from '../protocol/index.js';

import assert from 'node:assert/strict';
import { test } from 'node:test';
import {
Expand Down Expand Up @@ -240,6 +243,33 @@ test('stops catalog pages before the encoded result limit', async () => {
);
assert.equal(fixture.lookupCalls.length, 1);
assert.equal(fixture.lookupCalls[0]?.sourceSessionIds.length, 16);
const pages = [outcome.result];
let cursor: string | null = outcome.result.nextCursor;
while (cursor !== null) {
const next = await fixture.coordinator.handlers['external-session.catalog.query'](
{ adapterId: 'codex', cursor },
context,
);
assert.ok(next.ok && next.result.sessions.length > 0);
pages.push(next.result);
assert.ok(pages.length <= 20);
cursor = next.result.nextCursor;
}
const items = pages.flatMap((page) => page.sessions);
assert.deepEqual(
items.map((item) => item.id),
Array.from({ length: 20 }, (_, index) => `source-${index}`),
);
assertMaximalJsonPages(pages, items, {
maxBytes: EXTERNAL_SESSION_RESULT_MAX_BYTES,
maxItems: EXTERNAL_SESSION_PAGE_MAX_ITEMS,
items: (page) => page.sessions,
candidate: (page, sessions, end) => ({
...page,
sessions,
nextCursor: end < items.length ? String(end) : null,
}),
});
});

test('imports through the generic importer and treats repeats as independent copies', async () => {
Expand Down
48 changes: 48 additions & 0 deletions packages/runtime-host/src/__tests__/fixtures/json-pages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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';

/** Independently checks page selection using complete JSON, not the budget helper. */
export function assertMaximalJsonPages<Page extends object, Item>(
pages: readonly Page[],
expectedItems: readonly Item[],
options: {
maxBytes: number;
maxItems: number;
items: (page: Page) => readonly Item[];
candidate: (page: Page, items: readonly Item[], end: number) => object;
},
): void {
let offset = 0;
for (const page of pages) {
const limit = Math.min(expectedItems.length, offset + options.maxItems);
let end = offset;
while (end < limit) {
const candidate = options.candidate(page, expectedItems.slice(offset, end + 1), end + 1);
if (Buffer.byteLength(JSON.stringify(candidate), 'utf8') > options.maxBytes) break;
end += 1;
}
assert.ok(end > offset, 'each page must make progress');
assert.deepEqual(options.items(page), expectedItems.slice(offset, end));
assert.ok(Buffer.byteLength(JSON.stringify(page), 'utf8') <= options.maxBytes);
offset = end;
}
assert.equal(offset, expectedItems.length, 'continuations must return every item exactly once');
}
70 changes: 70 additions & 0 deletions packages/runtime-host/src/__tests__/json-array-page-budget.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* 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 { test } from 'node:test';
import { JsonArrayPageBudget } from '../server/json-array-page-budget.js';

const bytes = (value: unknown) => Buffer.byteLength(JSON.stringify(value), 'utf8');

for (const cursorKey of ['nextCursor', 'nextOffset']) {
test(`incremental ${cursorKey} budgets match whole-page JSON at exact boundaries`, () => {
const items = [
{ text: '中文🙂 \"quoted\" \\path\n', optional: undefined },
{ values: [null, true, 3.5], nested: { text: '\ud800' } },
undefined,
];
const cursors = [null, 9, 10, 99, 100, '目录\"\\🙂', { part: 'model', index: 100 }];
const empty = { kind: 'page', revision: '版本', items: [], [cursorKey]: null };
for (const cursor of cursors) {
for (const count of [1, 2, 3]) {
const limit = bytes({ ...empty, items: items.slice(0, count), [cursorKey]: cursor });
for (const delta of [-1, 0, 1]) {
const budget = new JsonArrayPageBudget(limit + delta, empty);
const accepted: unknown[] = [];
for (const item of items) {
const fits =
bytes({ ...empty, items: [...accepted, item], [cursorKey]: cursor }) <= limit + delta;
assert.equal(budget.tryAppend(item, cursor), fits);
if (fits) accepted.push(item);
}
}
}
}
});
}

test('a rejected candidate does not consume item bytes or a comma', () => {
const empty = { items: [], nextCursor: null };
const budget = new JsonArrayPageBudget(bytes({ items: ['a', 'b'], nextCursor: null }), empty);
assert.equal(budget.tryAppend('too large'.repeat(20), 99), false);
assert.equal(budget.tryAppend('a', 9), true);
assert.equal(budget.tryAppend('too large'.repeat(20), 100), false);
assert.equal(budget.tryAppend('b', null), true);
assert.equal(budget.tryAppend('c', null), false);
});

test('cursor growth and final null are charged to the candidate being tested', () => {
const empty = { items: [], nextCursor: null };
for (const cursor of [9, 10, 99, 100, null]) {
const budget = new JsonArrayPageBudget(bytes({ items: ['a', 'b'], nextCursor: cursor }), empty);
assert.equal(budget.tryAppend('a', 9), true);
assert.equal(budget.tryAppend('b', cursor), true);
}
});
73 changes: 73 additions & 0 deletions packages/runtime-host/src/__tests__/memory-coordinator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,85 @@ import {
MemoryMutateResult,
MemoryQueryInput,
MemoryQueryResult,
MEMORY_ENTRY_PAGE_MAX_ITEMS,
MEMORY_RESULT_MAX_BYTES,
type MemoryEntriesPage,
type MemoryEntryProjection,
} from '../protocol/index.js';
import { assertMaximalJsonPages } from './fixtures/json-pages.js';
import { HostMemoryCoordinator } from '../server/memory-coordinator.js';
import type { ConnectionContext } from '../server/operation-dispatcher.js';
import { RuntimePolicyActivationGate } from '../server/runtime-policy-activation-gate.js';

describe('Host Memory coordinator', () => {
test('entry queries preserve the maximal byte-limited prefix across continuations', async () => {
await withCoordinator(async ({ coordinator, memoryStore, context }) => {
await coordinator.recover();
const initial = await memoryStore.read();
const entries: MemoryEntryProjection[] = Array.from({ length: 70 }, (_, index) => ({
id: `entry-${index}`,
source: 'user_authored',
status: 'active',
title: '标题🙂 "quoted" \\path',
content: '文"\\\t🙂'.repeat(80),
scope: 'workspace',
tags: [],
}));
await memoryStore.commit({
expectedRevision: initial.revision,
memory: Buffer.from(
'# Maka Memory\n\n' +
entries
.map(
(entry) =>
`## ${entry.title}\n<!-- maka-memory: id=${entry.id} source=user_authored status=active scope=workspace -->\n${entry.content}\n`,
)
.join('\n'),
),
pending: null,
});
const pages: MemoryEntriesPage[] = [];
let input: MemoryQueryInput = { kind: 'entries_start', view: 'active' };
do {
const page = await query(coordinator, input, context);
assert.ok(page.kind === 'entries_page');
assert.ok(page.items.length > 0);
pages.push(page);
assert.ok(pages.length <= entries.length);
assert.deepEqual(
decodeHostFrame({
requestId: 'memory-page',
operation: 'memory.query',
ok: true,
result: page,
}),
{ requestId: 'memory-page', operation: 'memory.query', ok: true, result: page },
);
const end = pages.reduce((count, current) => count + current.items.length, 0);
assert.equal(page.nextCursor, end < entries.length ? end : null);
if (page.nextCursor === null) break;
input = {
kind: 'entries_continue',
view: 'active',
revision: page.revision,
cursor: page.nextCursor,
};
} while (true);
assert.ok(pages.length > 1);
assert.ok(pages[0]!.items.length < MEMORY_ENTRY_PAGE_MAX_ITEMS);
assertMaximalJsonPages(pages, entries, {
maxBytes: MEMORY_RESULT_MAX_BYTES,
maxItems: MEMORY_ENTRY_PAGE_MAX_ITEMS,
items: (page) => page.items,
candidate: (page, items, end) => ({
...page,
items,
nextCursor: end < entries.length ? end : null,
}),
});
});
});

test('initializes only when current policy permits Memory access', async () => {
await withCoordinator(async ({ coordinator, memoryStore, policyStores, context }) => {
await setIncognito(policyStores, true);
Expand Down
Loading