Skip to content

armgit / mso

Public Imported from GitHub
0

Browser-based graphical shell and control plane for a Linux server you own — real terminal, file manager, live metrics and BYOK AI in one mobile-first workspace. Next.js 16 · React 19 · node-pty · Tailwind v4 · shadcn/ui. Public alpha, self-hosted.

feat(skills): wire catalog TTL cache into /api/skills list route

Abdurrahman Fakhrul2614984752c5
diff --git a/app/api/skills/route.ts b/app/api/skills/route.ts
index 55b31229..41e2f699 100644
--- a/app/api/skills/route.ts
+++ b/app/api/skills/route.ts
@@ -2,6 +2,7 @@ import { NextRequest, NextResponse } from "next/server";
import { requireSession } from "@/lib/auth/require-session";
import { IS_DEMO } from "@/lib/demo";
import { catalogSkillsDetailed, resolveSkill, readSkillFile, type SkillInfo } from "@/lib/skills/catalog";
+import { catalogTtl } from "@/lib/skills/catalog-ttl";
import { listLearnedRecipes } from "@/lib/workflow";
import { searchSkillMemory } from "@/lib/skills/search";
@@ -44,21 +45,23 @@ export async function GET(req: NextRequest) {
}));
}
- const { skills, scan } = await catalogSkillsDetailed();
- if (!name) {
- const recipes = (await listLearnedRecipes({ ownerView: true })).map((r) => ({
- id: r.id, intent: r.intent, project: r.project, summary: r.summary, attempts: r.attempts,
- successes: r.successes, failures: r.failures, fastestDurationMs: r.fastestDurationMs, updatedAt: r.updatedAt,
- }));
- return NextResponse.json({ skills, recipes, scan });
+ if (name) {
+ const { skills } = await catalogSkillsDetailed();
+ const { skill, ambiguous } = resolveSkill(skills, name);
+ // A bare name matching several projects is not a 404 and must not be a guess:
+ // returning one project's instructions under another's name is the whole bug.
+ if (ambiguous) return NextResponse.json({ error: "ambiguous", candidates: ambiguous }, { status: 409 });
+ if (!skill) return NextResponse.json({ error: "not_found" }, { status: 404 });
+ const content = await readSkillFile(skill.path);
+ if (content === null) return NextResponse.json({ error: "not_found" }, { status: 404 });
+ return NextResponse.json({ skill, content: content.slice(0, 24_000), truncated: content.length > 24_000 });
}
- const { skill, ambiguous } = resolveSkill(skills, name);
- // A bare name matching several projects is not a 404 and must not be a guess:
- // returning one project's instructions under another's name is the whole bug.
- if (ambiguous) return NextResponse.json({ error: "ambiguous", candidates: ambiguous }, { status: 409 });
- if (!skill) return NextResponse.json({ error: "not_found" }, { status: 404 });
- const content = await readSkillFile(skill.path);
- if (content === null) return NextResponse.json({ error: "not_found" }, { status: 404 });
- return NextResponse.json({ skill, content: content.slice(0, 24_000), truncated: content.length > 24_000 });
+ const refresh = req.nextUrl.searchParams.get("refresh") === "1" || req.nextUrl.searchParams.get("refresh") === "true";
+ const { skills, scan } = await catalogTtl.get({ refresh });
+ const recipes = (await listLearnedRecipes({ ownerView: true })).map((r) => ({
+ id: r.id, intent: r.intent, project: r.project, summary: r.summary, attempts: r.attempts,
+ successes: r.successes, failures: r.failures, fastestDurationMs: r.fastestDurationMs, updatedAt: r.updatedAt,
+ }));
+ return NextResponse.json({ skills, recipes, scan });
}
diff --git a/lib/skills/catalog-ttl.test.ts b/lib/skills/catalog-ttl.test.ts
new file mode 100644
index 00000000..56bd8085
--- /dev/null
+++ b/lib/skills/catalog-ttl.test.ts
@@ -0,0 +1,179 @@
+import { beforeEach, describe, expect, it, vi } from "vitest";
+import { NextRequest } from "next/server";
+import { createCatalogTtl, catalogTtl, DEFAULT_CATALOG_TTL_MS } from "./catalog-ttl";
+import type { SkillCatalog } from "./catalog";
+
+vi.mock("@/lib/auth/require-session", () => ({ requireSession: vi.fn(async () => true) }));
+vi.mock("@/lib/workflow", () => ({ listLearnedRecipes: vi.fn(async () => []) }));
+
+const mockScanReport: SkillCatalog["scan"] = {
+ truncated: true,
+ truncationReasons: ["deadline", "maxProjects"],
+ scannedRoots: 4,
+ scannedProjects: 2,
+ continuation: {
+ pendingRoots: ["/repo/project-b"],
+ cursors: [{ root: "/repo/project-a", entriesConsumed: 12 }],
+ pendingProjects: 1,
+ cursorSemantics: "readdir-position",
+ note: "Pass cursor back to resume",
+ cursor: "cursor-token-xyz",
+ },
+};
+
+const mockCatalog: SkillCatalog = {
+ skills: [{ id: "proj/task", name: "task", path: "/skills/task", description: "Sample", source: "project", trust: "untrusted" }],
+ scan: mockScanReport,
+};
+
+describe("catalog-ttl memo", () => {
+ it("one load within TTL, two loads after expiry", async () => {
+ let clock = 1_000, loadCount = 0;
+ const memo = createCatalogTtl({
+ load: async () => { loadCount++; return mockCatalog; },
+ now: () => clock,
+ ttlMs: 15_000,
+ });
+
+ const first = await memo.get();
+ expect(loadCount).toBe(1);
+ expect(first).toBe(mockCatalog);
+
+ clock = 5_000;
+ const withinTtl = await memo.get();
+ expect(loadCount).toBe(1);
+ expect(withinTtl).toBe(mockCatalog);
+
+ clock = 16_001;
+ const firstAfterExpiry = await memo.get();
+ expect(loadCount).toBe(2);
+ expect(firstAfterExpiry).toBe(mockCatalog);
+
+ clock = 20_000;
+ await memo.get();
+ expect(loadCount).toBe(2);
+
+ clock = 32_000;
+ const secondAfterExpiry = await memo.get();
+ expect(loadCount).toBe(3);
+ expect(secondAfterExpiry).toBe(mockCatalog);
+ });
+
+ it("scan and continuation fields pass through byte-identical", async () => {
+ const memo = createCatalogTtl({ load: async () => mockCatalog, now: () => 1_000 });
+ const result = await memo.get();
+ expect(result).toBe(mockCatalog);
+ expect(result.scan.truncated).toBe(true);
+ expect(result.scan.truncationReasons).toEqual(["deadline", "maxProjects"]);
+ expect(result.scan.continuation?.cursor).toBe("cursor-token-xyz");
+ expect(result.scan.continuation?.cursorSemantics).toBe("readdir-position");
+ expect(result.scan.continuation?.cursors).toEqual([{ root: "/repo/project-a", entriesConsumed: 12 }]);
+ expect(result.scan.continuation?.pendingRoots).toEqual(["/repo/project-b"]);
+ });
+
+ it("refresh:true forces a reload", async () => {
+ let clock = 1_000, loadCount = 0;
+ const memo = createCatalogTtl({
+ load: async () => { loadCount++; return mockCatalog; },
+ now: () => clock,
+ ttlMs: 15_000,
+ });
+ await memo.get();
+ expect(loadCount).toBe(1);
+
+ clock = 2_000;
+ await memo.get({ refresh: true });
+ expect(loadCount).toBe(2);
+ });
+
+ it("a rejected load is not cached", async () => {
+ let fail = true, loadCount = 0;
+ const memo = createCatalogTtl({
+ load: async () => {
+ loadCount++;
+ if (fail) throw new Error("scan failed");
+ return mockCatalog;
+ },
+ now: () => 1_000,
+ ttlMs: 15_000,
+ });
+
+ await expect(memo.get()).rejects.toThrow("scan failed");
+ expect(loadCount).toBe(1);
+
+ fail = false;
+ const recovered = await memo.get();
+ expect(recovered).toBe(mockCatalog);
+ expect(loadCount).toBe(2);
+ });
+
+ it("invalidate() clears cached state and in-flight", async () => {
+ let loadCount = 0;
+ const memo = createCatalogTtl({
+ load: async () => { loadCount++; return mockCatalog; },
+ now: () => 1_000,
+ ttlMs: 15_000,
+ });
+ await memo.get();
+ expect(loadCount).toBe(1);
+
+ memo.invalidate();
+ await memo.get();
+ expect(loadCount).toBe(2);
+ });
+
+ it("coalesces concurrent in-flight requests", async () => {
+ let loadCount = 0;
+ const memo = createCatalogTtl({
+ load: async () => {
+ loadCount++;
+ await new Promise((r) => setTimeout(r, 10));
+ return mockCatalog;
+ },
+ now: () => 1_000,
+ });
+
+ const [a, b] = await Promise.all([memo.get(), memo.get()]);
+ expect(loadCount).toBe(1);
+ expect(a).toBe(mockCatalog);
+ expect(b).toBe(mockCatalog);
+ });
+
+ it("default ttlMs is between 10000 and 30000", () => {
+ expect(DEFAULT_CATALOG_TTL_MS).toBeGreaterThanOrEqual(10_000);
+ expect(DEFAULT_CATALOG_TTL_MS).toBeLessThanOrEqual(30_000);
+ });
+});
+
+describe("/api/skills route integration", () => {
+ beforeEach(() => {
+ catalogTtl.invalidate();
+ });
+
+ it("uses catalogTtl for list and forwards refresh flag", async () => {
+ const { GET } = await import("@/app/api/skills/route");
+ const getSpy = vi.spyOn(catalogTtl, "get").mockResolvedValue(mockCatalog);
+
+ const res = await GET(new NextRequest("http://localhost/api/skills"));
+ expect(res.status).toBe(200);
+ expect(getSpy).toHaveBeenCalledWith({ refresh: false });
+
+ await GET(new NextRequest("http://localhost/api/skills?refresh=1"));
+ expect(getSpy).toHaveBeenCalledWith({ refresh: true });
+
+ await GET(new NextRequest("http://localhost/api/skills?refresh=true"));
+ expect(getSpy).toHaveBeenCalledWith({ refresh: true });
+
+ getSpy.mockRestore();
+ });
+
+ it("bypasses catalogTtl when querying by name", async () => {
+ const { GET } = await import("@/app/api/skills/route");
+ const getSpy = vi.spyOn(catalogTtl, "get");
+
+ await GET(new NextRequest("http://localhost/api/skills?name=nonexistent-skill"));
+ expect(getSpy).not.toHaveBeenCalled();
+
+ getSpy.mockRestore();
+ });
+});