Fix the loop stalling after compaction, add an external registry

The compaction bug, which is the important one:

beta.2 taught the pruner to drop any assistant part whose reasoning item it had
removed. That was right about the 400 and wrong about everything else. On a
reasoning model every tool call carries a provider itemId, so past the threshold
the model could no longer see what it had already run, and re-ran the same tools
until maxSteps ended the turn. Reproduced at 12 model calls for a job needing 4,
with nothing but the user message reaching the wire.

The dependency is not the part, it is the itemId. A part carrying one is
serialised as `{ type: 'item_reference', id }`, a pointer to an item stored
provider-side that depends on its reasoning item. Without the itemId the same
content goes out inline and carries no dependency at all. Verified against the
provider's own serialiser: `text` with an itemId becomes item_reference, the
identical part without one becomes output_text.

So `dropOrphanedItems` becomes `detachOrphanedItems`: strip the itemId, keep the
content. Compaction may shorten the history; it must not blank it. The new test
asserts behaviour rather than shape — the loop must end because the model chose
to, and every call after the first must still carry the earlier exchange. A shape
assertion passed the whole time the model was losing its memory.

Registry, via `/registry [list|search|add|remove|installed]`:

Skills and plugins are treated differently on purpose. A skill is prompt text, so
installing one puts a stranger's words into the system prompt of every future
session in this project; the install shows the body first and the origin is
recorded, so /skills always says where an instruction came from. A plugin is a
JSON manifest of deny rules, evaluated by compiled code identical for every
install. Loading TypeScript from a URL is declined outright: a plugin that can
block tool calls could otherwise lie about blocking them.

Validated before anything is written: https only (file: and data: rejected), name
matched against ^[a-z0-9][a-z0-9-]*$ so it cannot escape its directory, size
caps on index and body, every regex compiled, pattern length capped since it runs
on every tool call, and the body's own name checked against the index. Installed
skills rank below your own, so an install can never shadow a skill you wrote.

Interface:
- Context is a percentage of the compaction threshold, amber from two thirds and
  red at 90. A turn about to lose history now says so beforehand.
- Aligned command menu and registry tables; /skills and /plugins name origins.

538 tests, up from 488. The registry is tested against a real local HTTP server,
and the guard is proven to refuse a .env write end to end rather than assumed to.
This commit is contained in:
Muhammad Zakir Ramadhan
2026-09-03 03:07:56 +07:00
parent 8b1895de98
commit 84c60f2022
26 changed files with 1929 additions and 149 deletions
+100 -26
View File
@@ -1,10 +1,24 @@
import { expect, test } from 'bun:test';
import type { ModelMessage } from 'ai';
import { dropOrphanedItems, dropOrphanedResults, prunePreservingItems } from '../src/prune';
import { detachOrphanedItems, dropOrphanedResults, prunePreservingItems } from '../src/prune';
const kinds = (messages: ModelMessage[]) =>
messages.map((m) => (Array.isArray(m.content) ? `${m.role}:${m.content.map((p) => p.type).join('+')}` : m.role));
/** Every provider itemId in a message tree, which is what the repair strips. */
const itemIds = (messages: ModelMessage[]): string[] => {
const found: string[] = [];
for (const m of messages) {
if (!Array.isArray(m.content)) continue;
for (const p of m.content as { providerOptions?: Record<string, Record<string, unknown>> }[]) {
for (const options of Object.values(p.providerOptions ?? {})) {
if (typeof options['itemId'] === 'string') found.push(options['itemId']);
}
}
}
return found;
};
/** An assistant turn as the OpenAI responses API returns it. */
const reasoningTurn = (rs: string, msg: string, text = 'answer'): ModelMessage => ({
role: 'assistant',
@@ -28,24 +42,33 @@ const toolTurn = (rs: string, call: string): ModelMessage => ({
],
});
test('a message left without its reasoning item is dropped', () => {
/**
* A part carrying an itemId is serialised as `{ type: 'item_reference', id }`,
* which depends on the stored reasoning item. Stripping the id sends the same
* content inline instead, so the turn survives without the dependency.
*/
test('a message left without its reasoning item keeps its text and loses its item id', () => {
const before = [{ role: 'user' as const, content: 'q' }, reasoningTurn('rs_1', 'msg_1')];
const after = [{ role: 'user' as const, content: 'q' }, { role: 'assistant' as const, content: [{ type: 'text' as const, text: 'answer', providerOptions: { openai: { itemId: 'msg_1' } } }] }];
const after: ModelMessage[] = [
{ role: 'user', content: 'q' },
{ role: 'assistant', content: [{ type: 'text', text: 'answer', providerOptions: { openai: { itemId: 'msg_1' } } }] },
];
const cleaned = dropOrphanedItems(before, after);
expect(JSON.stringify(cleaned)).not.toContain('msg_1');
expect(kinds(cleaned)).toEqual(['user']);
const cleaned = detachOrphanedItems(before, after);
expect(itemIds(cleaned)).toEqual([]);
expect(kinds(cleaned)).toEqual(['user', 'assistant:text']);
expect(JSON.stringify(cleaned)).toContain('answer');
});
test('a tool call left without its reasoning item is dropped too', () => {
test('a tool call left without its reasoning item survives, detached', () => {
const before = [{ role: 'user' as const, content: 'q' }, toolTurn('rs_1', 'fc_1')];
const after = [
{ role: 'user' as const, content: 'q' },
const after: ModelMessage[] = [
{ role: 'user', content: 'q' },
{
role: 'assistant' as const,
role: 'assistant',
content: [
{
type: 'tool-call' as const,
type: 'tool-call',
toolCallId: 'tc1',
toolName: 'grep',
input: { pattern: 'x' },
@@ -55,12 +78,60 @@ test('a tool call left without its reasoning item is dropped too', () => {
},
];
expect(JSON.stringify(dropOrphanedItems(before, after))).not.toContain('fc_1');
const cleaned = detachOrphanedItems(before, after);
expect(itemIds(cleaned)).toEqual([]);
// The call itself has to stay, or its result is orphaned and the model loses
// any record of what it already ran.
expect(JSON.stringify(cleaned)).toContain('tc1');
expect(kinds(cleaned)).toEqual(['user', 'assistant:tool-call']);
});
test('an empty providerOptions object is removed rather than left behind', () => {
const before = [toolTurn('rs_1', 'fc_1')];
const after: ModelMessage[] = [
{
role: 'assistant',
content: [
{
type: 'tool-call',
toolCallId: 'tc1',
toolName: 'grep',
input: { pattern: 'x' },
providerOptions: { openai: { itemId: 'fc_1' } },
},
],
},
];
const part = (detachOrphanedItems(before, after)[0]!.content as Record<string, unknown>[])[0]!;
expect('providerOptions' in part).toBe(false);
});
test('other provider options are kept when the item id is stripped', () => {
const before: ModelMessage[] = [
{
role: 'assistant',
content: [
{ type: 'reasoning', text: 't', providerOptions: { openai: { itemId: 'rs_1' } } },
{ type: 'text', text: 'a', providerOptions: { openai: { itemId: 'msg_1', phase: 'final' } } },
],
},
];
const after: ModelMessage[] = [
{
role: 'assistant',
content: [{ type: 'text', text: 'a', providerOptions: { openai: { itemId: 'msg_1', phase: 'final' } } }],
},
];
const json = JSON.stringify(detachOrphanedItems(before, after));
expect(json).not.toContain('msg_1');
expect(json).toContain('final');
});
test('a turn whose reasoning survived is left alone', () => {
const messages = [{ role: 'user' as const, content: 'q' }, reasoningTurn('rs_1', 'msg_1')];
expect(dropOrphanedItems(messages, messages)).toEqual(messages);
expect(detachOrphanedItems(messages, messages)).toEqual(messages);
});
test('nothing is touched when no reasoning was removed', () => {
@@ -68,13 +139,13 @@ test('nothing is touched when no reasoning was removed', () => {
{ role: 'user', content: 'q' },
{ role: 'assistant', content: 'plain answer' },
];
expect(dropOrphanedItems(before, before)).toEqual(before);
expect(detachOrphanedItems(before, before)).toEqual(before);
});
test('parts with no provider itemId are always kept', () => {
test('parts with no provider itemId are returned unchanged', () => {
const before = [reasoningTurn('rs_1', 'msg_1')];
const after: ModelMessage[] = [{ role: 'assistant', content: [{ type: 'text', text: 'no item id here' }] }];
expect(dropOrphanedItems(before, after)).toEqual(after);
expect(detachOrphanedItems(before, after)).toEqual(after);
});
test('user and tool messages are never affected', () => {
@@ -83,10 +154,10 @@ test('user and tool messages are never affected', () => {
{ role: 'user', content: 'q' },
{ role: 'tool', content: [{ type: 'tool-result', toolCallId: 't1', toolName: 'grep', output: { type: 'text', value: 'hit' } }] },
];
expect(dropOrphanedItems(before, after)).toEqual(after);
expect(detachOrphanedItems(before, after)).toEqual(after);
});
test('one orphaned turn does not take a healthy one with it', () => {
test('one orphaned turn does not detach a healthy one with it', () => {
const before = [
{ role: 'user' as const, content: 'q1' },
reasoningTurn('rs_1', 'msg_1', 'old answer'),
@@ -100,14 +171,15 @@ test('one orphaned turn does not take a healthy one with it', () => {
reasoningTurn('rs_2', 'msg_2', 'new answer'),
];
const cleaned = dropOrphanedItems(before, after);
const cleaned = detachOrphanedItems(before, after);
const json = JSON.stringify(cleaned);
expect(json).not.toContain('msg_1');
expect(json).toContain('old answer');
expect(json).toContain('msg_2');
expect(json).toContain('rs_2');
});
test('prunePreservingItems leaves no orphan behind on a real prune', () => {
test('prunePreservingItems leaves no item reference behind on a real prune', () => {
const messages: ModelMessage[] = [];
for (let i = 0; i < 6; i++) {
messages.push({ role: 'user', content: `question ${i} ${'x'.repeat(3000)}` });
@@ -121,12 +193,11 @@ test('prunePreservingItems leaves no orphan behind on a real prune', () => {
emptyMessages: 'remove',
});
// Every surviving text part must either have no item id or belong to a turn
// whose reasoning also survived. Since reasoning: 'all' removes them all, no
// itemId-bearing assistant part may remain.
const survivingIds = JSON.stringify(pruned);
for (let i = 0; i < 6; i++) expect(survivingIds).not.toContain(`msg_${i}`);
// reasoning: 'all' removes every reasoning item, so no surviving part may still
// reference one. The text itself stays: that is the model's memory of the turn.
expect(itemIds(pruned)).toEqual([]);
expect(pruned.filter((m) => m.role === 'user')).toHaveLength(6);
expect(JSON.stringify(pruned)).toContain('answer 5');
});
test('prunePreservingItems is a no-op when nothing needs pruning', () => {
@@ -150,7 +221,10 @@ test('a provider other than openai is handled the same way', () => {
const after: ModelMessage[] = [
{ role: 'assistant', content: [{ type: 'text', text: 'a', providerOptions: { someProvider: { itemId: 'm1' } } }] },
];
expect(dropOrphanedItems(before, after)).toEqual([]);
const cleaned = detachOrphanedItems(before, after);
expect(itemIds(cleaned)).toEqual([]);
expect(JSON.stringify(cleaned)).toContain('"text":"a"');
});
/** The assistant tool-call plus the tool message answering it, as one exchange. */