xonecas.io
64875
Mc

mini-coder / commit 3b67e42

fix: preserve loaded skill context

xonecas2026-03-25 3b67e42847ab06598941be8b0a7b65092c6c182f parent b93bce5

5 files changed

TODO.md+1−1
@@ -1,3 +1,3 @@
11 # mini-coder TODO
22
3-- Review AUDIT-REPORT.md findings with user
3+No open items.
src/cli/tool-render.test.ts+17−0
@@ -211,4 +211,21 @@ describe("renderToolResult", () => {
211211 expect(plain).toContain("deploy · local · Deploy safely");
212212 expect(plain).toContain("release · global · Ship releases");
213213 });
214+
215+ test("renders duplicate readSkill loads as already loaded instead of not found", () => {
216+ captureStdout();
217+
218+ renderToolResult(
219+ "readSkill",
220+ {
221+ skill: null,
222+ note: 'Skill "deploy" is already loaded in this session.',
223+ },
224+ false,
225+ );
226+
227+ const plain = stripAnsi(getCapturedStdout());
228+ expect(plain).toContain("already loaded in this session");
229+ expect(plain).not.toContain("(not found)");
230+ });
214231 });
src/cli/tool-result-renderers.ts+9−1
@@ -231,10 +231,15 @@ function renderReadSkillResult(
231231 name?: string;
232232 description?: string;
233233 source?: "local" | "global";
234- };
234+ } | null;
235+ note?: string;
235236 };
236237 if (!r || typeof r !== "object") return false;
237238 if (!r.skill) {
239+ if (typeof r.note === "string" && r.note.trim().length > 0) {
240+ writeln(`${G.info} ${c.dim("skill")} ${c.dim(r.note.trim())}`);
241+ return true;
242+ }
238243 writeln(`${G.info} ${c.dim("skill")} ${c.dim("(not found)")}`);
239244 return true;
240245 }
@@ -242,6 +247,9 @@ function renderReadSkillResult(
242247 label: "skill",
243248 verboseOutput: _opts?.verboseOutput === true,
244249 });
250+ if (typeof r.note === "string" && r.note.trim().length > 0) {
251+ writeln(c.dim(r.note.trim()));
252+ }
245253 return true;
246254 }
247255
src/llm-api/turn-context.ts+36−2
@@ -129,11 +129,42 @@ export function getMessageDiagnostics(
129129 };
130130 }
131131
132+type ToolCallPruneType =
133+ | "all"
134+ | "before-last-message"
135+ | `before-last-${number}-messages`;
136+
137+function collectPrunableToolNames(messages: CoreMessage[]): string[] {
138+ const names = new Set<string>();
139+
140+ for (const message of messages) {
141+ if (!Array.isArray(message.content)) continue;
142+ for (const part of message.content) {
143+ if (!isRecord(part)) continue;
144+ const partRecord = part as Record<string, unknown>;
145+ const toolName = partRecord.toolName;
146+ if (typeof toolName !== "string" || toolName.length === 0) continue;
147+ if (toolName === "readSkill") continue;
148+ names.add(toolName);
149+ }
150+ }
151+
152+ return [...names].sort((a, b) => a.localeCompare(b));
153+}
154+
155+function buildToolCallPruning(
156+ messages: CoreMessage[],
157+ type: ToolCallPruneType,
158+): "none" | Array<{ type: ToolCallPruneType; tools: string[] }> {
159+ const tools = collectPrunableToolNames(messages);
160+ return tools.length === 0 ? "none" : [{ type, tools }];
161+}
162+
132163 export function applyContextPruning(messages: CoreMessage[]): CoreMessage[] {
133164 return pruneMessages({
134165 messages,
135166 reasoning: "before-last-message",
136- toolCalls: "before-last-40-messages",
167+ toolCalls: buildToolCallPruning(messages, "before-last-40-messages"),
137168 emptyMessages: "remove",
138169 }) as CoreMessage[];
139170 }
@@ -164,7 +195,10 @@ export function applyStepPruning(
164195 return pruneMessages({
165196 messages,
166197 reasoning: "none",
167- toolCalls: `before-last-${40 + newMessageCount}-messages`,
198+ toolCalls: buildToolCallPruning(
199+ messages,
200+ `before-last-${40 + newMessageCount}-messages`,
201+ ),
168202 emptyMessages: "remove",
169203 }) as CoreMessage[];
170204 }
src/llm-api/turn.test.ts+77−0
@@ -725,6 +725,83 @@ describe("applyContextPruning", () => {
725725 expect(pruned.length).toBeLessThan(messages.length);
726726 expect(pruned.at(-1)?.role).toBe("user");
727727 });
728+
729+ test("keeps readSkill history while pruning other stale tool results", () => {
730+ const messages: CoreMessage[] = [];
731+
732+ for (let i = 0; i < 10; i++) {
733+ messages.push({ role: "user", content: `skill user ${i}` });
734+ messages.push({
735+ role: "assistant",
736+ content: [
737+ {
738+ type: "tool-call",
739+ toolCallId: `skill-${i}`,
740+ toolName: "readSkill",
741+ input: { name: `skill-${i}` },
742+ },
743+ ],
744+ } as unknown as CoreMessage);
745+ messages.push({
746+ role: "tool",
747+ content: [
748+ {
749+ type: "tool-result",
750+ toolCallId: `skill-${i}`,
751+ toolName: "readSkill",
752+ output: { skill: { name: `skill-${i}`, content: `body ${i}` } },
753+ },
754+ ],
755+ } as unknown as CoreMessage);
756+ }
757+
758+ for (let i = 0; i < 20; i++) {
759+ messages.push({ role: "user", content: `shell user ${i}` });
760+ messages.push({
761+ role: "assistant",
762+ content: [
763+ {
764+ type: "tool-call",
765+ toolCallId: `shell-${i}`,
766+ toolName: "shell",
767+ input: { command: `echo ${i}` },
768+ },
769+ ],
770+ } as unknown as CoreMessage);
771+ messages.push({
772+ role: "tool",
773+ content: [
774+ {
775+ type: "tool-result",
776+ toolCallId: `shell-${i}`,
777+ toolName: "shell",
778+ output: { stdout: `${i}` },
779+ },
780+ ],
781+ } as unknown as CoreMessage);
782+ }
783+
784+ messages.push({ role: "user", content: "final" });
785+
786+ const pruned = applyContextPruning(messages);
787+ const toolCallIds = new Set(
788+ pruned.flatMap((message) =>
789+ Array.isArray(message.content)
790+ ? message.content.flatMap((part) =>
791+ typeof part === "object" &&
792+ part !== null &&
793+ "toolCallId" in part &&
794+ typeof part.toolCallId === "string"
795+ ? [part.toolCallId]
796+ : [],
797+ )
798+ : [],
799+ ),
800+ );
801+
802+ expect(toolCallIds.has("skill-0")).toBe(true);
803+ expect(toolCallIds.has("shell-0")).toBe(false);
804+ });
728805 });
729806
730807 describe("applyStepPruning", () => {