Files
kilocode/script/upstream/progress.test.ts

405 lines
22 KiB
TypeScript

import { expect, test } from "bun:test"
import {
apiLiterals,
areaOf,
canon,
cellRecord,
classify,
compare,
countedFeatures,
featureOf,
grid,
history,
historyBase,
importBindings,
knownFeatures,
parseHunks,
parseProofs,
patchId,
placement,
proofByCell,
scanImports,
snapshotSummary,
squarify,
} from "./progress"
const split = { "session/steering": "steering", "kilo-ui": "ui", plugins: "plugin" }
test("API proofs match standalone route literals, not schema imports", () => {
expect(apiLiterals('import { Rpc } from "@opencode/schema/kilocode/marketplace"')).toEqual(new Set())
expect(apiLiterals('"/kilocode/marketplace/install"\n\'/kilocode/example\'\n`/kilocode/usage`')).toEqual(
new Set(["/kilocode/marketplace/install", "/kilocode/example", "/kilocode/usage"]),
)
})
test("history uses v2 rather than feature-branch or synthetic merge ancestry", async () => {
const current = JSON.stringify({ summary: { done: 92, total: 9829, percent: 0.9 }, coreImports: 12 }) + "\n"
const points = await history(current, async (args) => {
if (args[0] === "rev-parse") return "resolved"
if (args[0] === "log") {
expect(args).toEqual(["log", "--first-parent", "--format=%H %cs", "origin/v2", "--", "migration-tracking/progress/progress.json"])
return "current 2026-10-22\nbase 2026-10-22\nolder 2026-10-21"
}
if (args[1].startsWith("current:")) return current.trimEnd()
return JSON.stringify({
summary: { done: args[1].startsWith("base:") ? 92 : 90, total: 9829, percent: 0.9 },
coreImports: 9,
})
})
expect(points).toEqual([
{ date: "2026-10-21", done: 90, total: 9829, percent: 0.9 },
{ date: "2026-10-22", done: 92, total: 9829, percent: 0.9 },
])
})
test("history walks the base it is given and fails loudly when that ref does not resolve", async () => {
const seen: string[][] = []
const current = JSON.stringify({ summary: { done: 1, total: 2, percent: 50 }, coreImports: 1 }) + "\n"
await history(current, async (args) => {
seen.push(args)
return args[0] === "log" ? "" : "resolved"
}, "fork/v2")
expect(seen[0]).toEqual(["rev-parse", "--verify", "fork/v2^{commit}"])
expect(seen[1]).toContain("fork/v2")
await expect(
history(current, async (args) => {
if (args[0] === "rev-parse") throw new Error("unknown revision")
return ""
}, "typo/v2"),
).rejects.toThrow("Progress history base ref not found: typo/v2")
})
test("historyBase defaults to origin/v2 and takes only the explicit override, never remote names", () => {
expect(historyBase({})).toBe("origin/v2")
expect(historyBase({ KILO_PROGRESS_BASE_V2: "" })).toBe("origin/v2")
expect(historyBase({ KILO_PROGRESS_BASE_V2: "fork/v2" })).toBe("fork/v2")
// An `upstream` remote is OpenCode's frozen v2 in the standard layout, so its presence changes nothing.
expect(historyBase({ GIT_REMOTES: "origin\nupstream" })).toBe("origin/v2")
})
test("featureOf: packages, directories, split table and loose files", () => {
const known = knownFeatures(
["packages/kilo-memory/src/a.ts", "packages/opencode/src/kilocode/agent-manager/tool.ts", "packages/opencode/src/kilocode/session/steering.ts"],
split,
)
expect(featureOf("packages/kilo-memory/src/a.ts", split)).toBe("memory")
expect(featureOf("packages/kilo-gateway-core/src/device.ts", split)).toBe("gateway")
expect(featureOf("packages/kilo-ui/src/x.tsx", split)).toBe("ui")
expect(featureOf("packages/opencode/src/kilocode/agent-manager/tool.ts", split)).toBe("agent-manager")
expect(featureOf("packages/opencode/src/kilocode/session/steering.ts", split)).toBe("steering")
expect(featureOf("packages/opencode/src/kilocode/session/other.ts", split)).toBe("session")
expect(featureOf("packages/opencode/src/kilocode/plugins/x.ts", split)).toBe("plugin")
// loose files join the longest known feature that prefixes their name, else keep their own name
expect(featureOf("packages/opencode/test/kilocode/agent-manager-service.test.ts", split, known)).toBe("agent-manager")
expect(featureOf("packages/core/src/kilocode/caffeination.ts", split, known)).toBe("caffeination")
expect(featureOf("packages/core/src/x.ts", split)).toBeUndefined()
})
test("areaOf names the upstream area of a shared file", () => {
expect(areaOf("packages/opencode/src/session/prompt.ts")).toBe("session")
expect(areaOf("packages/tui/src/component/x.tsx")).toBe("tui/component")
expect(areaOf("packages/opencode/test/tool/bash.test.ts")).toBe("test")
expect(areaOf("packages/core/package.json")).toBe("core/pkg")
})
test("canon makes a brand rename compare equal and keeps real changes apart", () => {
expect(canon(['import { x } from "@opencode-ai/sdk"'])).toBe(canon(['import { x } from "@kilocode/sdk" // kilocode_change']))
expect(canon(["const name = 'OpenCode'"])).toBe(canon(["const name = 'Kilo Code'"]))
expect(canon(["const a = 1"])).not.toBe(canon(["const a = 2"]))
})
test("patchId ignores whitespace and comments and is stable", () => {
expect(patchId(["const a = 1 // kilocode_change", " const b = 2"])).toBe(patchId(["const a=1", "const b=2 // other note"]))
expect(patchId(["const a = 1"])).not.toBe(patchId(["const a = 2"]))
expect(patchId(["const a = 1"])).toMatch(/^[0-9a-f]{12}$/)
})
const diff = [
"diff --git a/packages/opencode/src/session/prompt.ts b/packages/opencode/src/session/prompt.ts",
"--- a/packages/opencode/src/session/prompt.ts",
"+++ b/packages/opencode/src/session/prompt.ts",
"@@ -10,0 +11,2 @@",
'+import { Steering } from "@/kilocode/session/steering" // kilocode_change',
"+const MAX = 8192 // kilocode_change",
"@@ -40,0 +43 @@",
"+// kilocode_change end",
"@@ -50 +53 @@",
'-const brand = "OpenCode"',
'+const brand = "Kilo Code" // kilocode_change',
"@@ -60,0 +64 @@",
"+ Steering.apply(input)",
"@@ -70,0 +75 @@",
"+ const x = 1 // kilocode_change(sandbox) start - limit",
"@@ -80,0 +86 @@",
"+ const y = untyped()",
"@@ -90,0 +97 @@",
"+++x // content that starts with ++ is not a file header",
].join("\n")
test("parseHunks and classify: tag, import, identifier, branding, marker-only, unattributed", () => {
const hunks = parseHunks(diff)
expect(hunks).toHaveLength(7)
expect(hunks[0].line).toBe(11)
expect(hunks[6].added).toEqual(["++x // content that starts with ++ is not a file header"])
const bindings = importBindings('import { Steering } from "@/kilocode/session/steering"\n// import { Old } from "@/kilocode/sandbox/old"\n', split)
expect(bindings.get("Steering")).toBe("steering")
expect(bindings.has("Old")).toBe(false)
const results = hunks.map((h) => classify(h, bindings, split))
expect(results[0]).toEqual({ kind: "cell", feature: "steering", how: "import" })
expect(results[1]).toEqual({ kind: "marker" })
expect(results[2]).toEqual({ kind: "branding" })
expect(results[3]).toEqual({ kind: "cell", feature: "steering", how: "identifier" })
expect(results[4]).toEqual({ kind: "cell", feature: "sandbox", how: "tag" })
expect(results[5]).toEqual({ kind: "cell", feature: "unattributed:session", how: "unattributed" })
})
test("squarify fills the panel with one rectangle per value, areas proportional to the values", () => {
const rects = squarify([6, 3, 1], 0, 0, 100, 50)
expect(rects).toHaveLength(3)
const areas = rects.map(([, , w, h]) => w * h)
expect(areas.reduce((a, b) => a + b, 0)).toBeCloseTo(5000, 6)
expect(areas[0] / areas[2]).toBeCloseTo(6, 6)
for (const [x, y, w, h] of rects) expect(x >= 0 && y >= 0 && x + w <= 100.000001 && y + h <= 50.000001).toBe(true)
})
test("grid returns exactly count cells inside the box", () => {
const cells = grid(7, 10, 10, 30, 20)
expect(cells).toHaveLength(7)
for (const [x, y, w, h] of cells) expect(x >= 10 && y >= 10 && x + w <= 40.000001 && y + h <= 30.000001).toBe(true)
expect(grid(0, 0, 0, 10, 10)).toEqual([])
})
test("compare reports the delta per feature and the cells that closed, dropped or reopened", () => {
const kinds = (module: number, patch: number) => ({ module: { done: module, total: 10 }, api: { done: 0, total: 0 }, patch: { done: patch, total: 5 }, test: { done: 0, total: 0 } })
const base = {
summary: { done: 1, total: 30, percent: 3.3, dropped: 0 },
features: [{ feature: "gateway", done: 1, total: 15, dropped: 0, kinds: kinds(1, 0) }, { feature: "memory", done: 0, total: 15, dropped: 0, kinds: kinds(0, 0) }],
done: ["module:a", "module:gone"],
dropped: [],
}
const head = {
summary: { done: 4, total: 30, percent: 13.3, dropped: 1 },
features: [{ feature: "gateway", done: 4, total: 15, dropped: 1, kinds: kinds(3, 1) }, { feature: "memory", done: 0, total: 15, dropped: 0, kinds: kinds(0, 0) }],
done: ["module:a", "module:b", "patch:gateway:0123456789ab"],
dropped: ["patch:gateway:ffffffffffff"],
}
const out = compare(base, head)
expect(out).toContain("3.3% → 13.3%")
expect(out).toContain("| gateway | 1/15 | 4/15 | +2 modules done; +1 patch done; +1 dropped |")
expect(out).not.toContain("| memory |")
expect(out).toContain("2 cells closed")
expect(out).toContain("1 cells reopened")
expect(out).toContain("`module:gone`")
expect(out).toContain("`patch:gateway:ffffffffffff`")
expect(compare(head, head)).toContain("No change to the progress map.")
expect(compare({}, head)).toContain("0% → 13.3%")
})
const rungs = { "packages/core/src/session/compaction.ts": { rung: 3 as const }, "packages/core/src/sse.ts": { rung: 4 as const } }
test("parseProofs: kilo_port in Kilo-owned files, kilocode_change in shared files, each with its landed rung", () => {
const grep = [
"packages/kilo-plugin/src/sandbox-guard.ts:12:kilo_port(sandbox:1a2b3c4d5e6f)",
"packages/kilo-cli/src/index.ts:40:kilo_port(cli:0123456789ab)",
"packages/core/src/kilocode/gateway.ts:7:kilo_port(gateway:aaaaaaaaaaaa)",
"packages/core/src/session/compaction.ts:88:kilocode_change(compaction-policy:bbbbbbbbbbbb)",
"packages/core/src/sse.ts:5:kilocode_change(process:cccccccccccc)",
"packages/core/src/unlisted.ts:1:kilocode_change(process:dddddddddddd)",
"packages/core/src/tool/grep.ts:3:kilo_port(tool:eeeeeeeeeeee)",
"packages/kilo-plugin/src/x.ts:9:kilocode_change(tool:ffffffffffff)",
"packages/kilo-plugin/src/y.ts:1:kilo_port(tool:not-an-id)",
"packages/kilo-plugin/src/agent/index.ts:4:kilo_port(unattributed:agent:abcdefabcdef)",
"packages/kilo-plugin/src/z.ts:2:kilo_port(unattributed:tui/component:012345012345)",
].join("\n")
expect(parseProofs(grep, rungs).map((p) => [p.id, p.at, p.rung, p.misplaced])).toEqual([
["patch:sandbox:1a2b3c4d5e6f", "packages/kilo-plugin/src/sandbox-guard.ts:12", 1, false],
["patch:cli:0123456789ab", "packages/kilo-cli/src/index.ts:40", 2, false],
["patch:gateway:aaaaaaaaaaaa", "packages/core/src/kilocode/gateway.ts:7", 1, false],
["patch:compaction-policy:bbbbbbbbbbbb", "packages/core/src/session/compaction.ts:88", 3, false],
["patch:process:cccccccccccc", "packages/core/src/sse.ts:5", 4, false],
// a shared file without a shared-patches.json entry counts as rung 4
["patch:process:dddddddddddd", "packages/core/src/unlisted.ts:1", 4, false],
["patch:tool:eeeeeeeeeeee", "packages/core/src/tool/grep.ts:3", 4, true],
["patch:tool:ffffffffffff", "packages/kilo-plugin/src/x.ts:9", 1, true],
// an unattributed patch names its box as the feature
["patch:unattributed:agent:abcdefabcdef", "packages/kilo-plugin/src/agent/index.ts:4", 1, false],
["patch:unattributed:tui/component:012345012345", "packages/kilo-plugin/src/z.ts:2", 1, false],
])
})
test("proofByCell reports a cell proved by a kilo_port and a marker once, at the highest rung", () => {
const proofs = parseProofs(
[
"packages/core/src/session/compaction.ts:88:kilocode_change(compaction-policy:bbbbbbbbbbbb)",
"packages/kilo-plugin/src/compaction.ts:3:kilo_port(compaction-policy:bbbbbbbbbbbb)",
"packages/kilo-plugin/src/a.ts:1:kilo_port(sandbox:1a2b3c4d5e6f)",
"packages/kilo-plugin/src/b.ts:1:kilo_port(sandbox:1a2b3c4d5e6f)",
].join("\n"),
rungs,
)
const best = proofByCell(proofs)
expect([...best.keys()]).toEqual(["patch:compaction-policy:bbbbbbbbbbbb", "patch:sandbox:1a2b3c4d5e6f"])
expect(best.get("patch:compaction-policy:bbbbbbbbbbbb")?.rung).toBe(3)
expect(best.get("patch:sandbox:1a2b3c4d5e6f")?.at).toBe("packages/kilo-plugin/src/a.ts:1")
})
test("scanImports counts distinct core-internal modules and enforces the logic-package rule", () => {
const out = scanImports([
["packages/core/src/kilocode/gateway.ts", 'import { Bus } from "../bus.js"\nimport { Variant } from "../variant.js"\nimport { Rpc } from "./gateway-rpc.js"\nimport { define } from "@opencode/plugin/effect/plugin"\n'],
["packages/tui/src/kilocode/gateway.tsx", 'import { Rpc } from "@opencode/core/kilocode/gateway-rpc"\nimport { useTheme } from "../context/theme"\nexport { Bus } from "@opencode/core/bus"\n'],
["packages/kilo-plugin/src/index.ts", 'const { Session } = await import("@opencode/core/session/index.ts")\n// import { Old } from "@opencode/core/old"\n'],
["packages/kilo-gateway-core/src/http.ts", 'import { Effect } from "effect"\nimport type { Model } from "@opencode/schema/model"\nimport { x } from "./x"\n'],
])
expect(out.modules).toEqual(["@opencode/core/bus", "@opencode/core/session", "@opencode/core/variant"])
expect(out.violations).toEqual(["packages/kilo-gateway-core/src/http.ts: @opencode/schema/model"])
})
test("placement counts new marked lines in shared files by rung and new kilo_port annotations", () => {
const diff = [
"diff --git a/packages/core/src/session/compaction.ts b/packages/core/src/session/compaction.ts",
"@@ -1,0 +2,3 @@",
"+ // kilocode_change start",
"+ const toolOutput = input.toolOutput",
"+ // kilocode_change end",
"@@ -2 +5 @@",
"- const max = 1250",
"+ const max = toolOutput ?? 1250 // kilocode_change(compaction-policy) seam",
"diff --git a/packages/core/src/sse.ts b/packages/core/src/sse.ts",
"@@ -2,0 +3 @@",
"+const b = 2",
"@@ -3,0 +5 @@",
"+unmarked()",
"diff --git a/packages/core/src/added.ts b/packages/core/src/added.ts",
"new file mode 100644",
"--- /dev/null",
"+++ b/packages/core/src/added.ts",
"@@ -0,0 +1,2 @@",
"+// kilocode_change - new file",
"+export const added = 1",
"diff --git a/packages/kilo-plugin/src/guard.ts b/packages/kilo-plugin/src/guard.ts",
"@@ -0,0 +1,3 @@",
"+// kilo_port(sandbox:1a2b3c4d5e6f)",
"+// kilo_port(unattributed:agent:abcdefabcdef)",
"+export const guard = 1 // kilocode_change",
"diff --git a/script/upstream/x.test.ts b/script/upstream/x.test.ts",
"@@ -0,0 +1 @@",
"+const s = 'kilo_port(sandbox:1a2b3c4d5e6f)'",
].join("\n")
const contents = new Map([
[
"packages/core/src/session/compaction.ts",
["export function configure(input) {", " // kilocode_change start", " const toolOutput = input.toolOutput", " // kilocode_change end", " const max = toolOutput ?? 1250 // kilocode_change(compaction-policy) seam", "}"].join("\n"),
],
// the added line 3 sits inside a block that was already there; line 5 has no marker
["packages/core/src/sse.ts", ["// kilocode_change start", "const a = 1", "const b = 2", "// kilocode_change end", "unmarked()"].join("\n")],
["packages/core/src/added.ts", "// kilocode_change - new file\nexport const added = 1\n"],
])
const place = placement(diff, contents, rungs)
expect(place).toEqual({ marked: { plugin: 0, host: 0, seam: 4, patch: 3 }, ports: 2, owned: 3 })
const summary = (coreImports: number) => ({ done: 0, total: 1, percent: 0, dropped: 0, coreImports, sharedPatches: { seam: 13, patch: 1 } })
const head = { summary: summary(2), coreImports: ["@opencode/core/bus", "@opencode/core/variant"] }
const out = compare({ summary: summary(1), coreImports: ["@opencode/core/variant"] }, head, place)
expect(out).toContain("**Placement:** 7 new marked lines in shared files (4 seam, 3 patch), 2 new `kilo_port` annotations, 1 new core-internal import (`@opencode/core/bus`).")
expect(out).toContain("**Ratchets:** 2 core-internal imports (base 1); shared patches by rung 13 seam, 1 patch (base 13 seam, 1 patch).")
expect(compare({}, head, { marked: { plugin: 0, host: 0, seam: 0, patch: 0 }, ports: 0, owned: 0 })).toContain(
"**Placement:** 0 new marked lines in shared files, 0 new `kilo_port` annotations, core-internal imports not compared (the base map has no import list).",
)
})
test("compare says when Kilo-owned code moved but no cell changed", () => {
const quiet = compare({}, { summary: { done: 0, total: 1, percent: 0, dropped: 0 } }, { marked: { plugin: 0, host: 0, seam: 0, patch: 0 }, ports: 0, owned: 42 })
expect(quiet).toContain("No cell closed, although the PR adds 42 lines in Kilo-owned files.")
expect(quiet).toContain("If it ports v1 behavior, add a done-proof or a disposition for each cell; v2-only work needs none.")
expect(compare({}, { summary: { done: 0, total: 1, percent: 0, dropped: 0 } }, { marked: { plugin: 0, host: 0, seam: 0, patch: 0 }, ports: 0, owned: 0 })).toContain(
"No change to the progress map.",
)
const proved = compare({ summary: { done: 0, total: 1, percent: 0, dropped: 0 }, done: [] }, { summary: { done: 1, total: 1, percent: 100, dropped: 0 }, done: ["module:a"] }, { marked: { plugin: 0, host: 0, seam: 0, patch: 0 }, ports: 1, owned: 42 })
expect(proved).toContain("1 cells closed")
expect(proved).not.toContain("No cell closed")
})
test("compare and history count only non-excluded feature boxes and ignore legacy unattributed entries", async () => {
const kinds = (moduleDone: number, moduleTotal: number, patchDone = 0, patchTotal = 0) => ({
module: { done: moduleDone, total: moduleTotal },
api: { done: 0, total: 0 },
patch: { done: patchDone, total: patchTotal },
test: { done: 0, total: 0 },
})
const legacyBase = {
summary: { done: 68, total: 1100, percent: 6.2, dropped: 5 },
features: [
{ feature: "gateway", done: 10, total: 20, dropped: 2, kinds: kinds(10, 20) },
{ feature: "console", done: 0, total: 80, dropped: 0, kinds: kinds(0, 80) },
{ feature: "docs", done: 0, total: 500, dropped: 0, kinds: kinds(0, 500), excluded: "Docs site" },
{ feature: "unattributed:session", done: 58, total: 500, dropped: 3, kinds: kinds(0, 0, 58, 500) },
],
done: ["module:gateway/a.ts", "patch:unattributed:session:0123456789ab"],
dropped: ["patch:unattributed:session:ffffffffffff"],
}
const head = {
summary: { done: 15, total: 20, percent: 75, dropped: 2 },
features: [
{ feature: "gateway", done: 15, total: 20, dropped: 2, kinds: kinds(15, 20) },
{ feature: "console", done: 0, total: 80, dropped: 0, kinds: kinds(0, 80), excluded: "Out of scope" },
{ feature: "docs", done: 0, total: 500, dropped: 0, kinds: kinds(0, 500), excluded: "Docs site" },
],
done: ["module:gateway/a.ts", "module:gateway/b.ts"],
dropped: ["patch:gateway:0123456789ab", "patch:unattributed:session:ffffffffffff", "patch:unattributed:session:0123456789ab"],
}
expect(countedFeatures(legacyBase, { console: "Out of scope" }).map((f) => f.feature)).toEqual(["gateway"])
expect(snapshotSummary(legacyBase, { console: "Out of scope" })).toEqual({ done: 10, total: 20, percent: 50, dropped: 2 })
const delta = compare(legacyBase, head)
expect(delta).toContain("10% → 75%")
expect(delta).toContain("| console | 0/80 | excluded | -80 modules total |")
expect(delta).toContain("| gateway | 10/20 | 15/20 | +5 modules done |")
expect(delta).not.toContain("unattributed:")
expect(delta).not.toContain("cells reopened")
expect(delta).toContain("1 cells dropped")
expect(delta).toContain("`patch:gateway:0123456789ab`")
const points = await history(
JSON.stringify(head) + "\n",
async (args) => {
if (args[0] === "rev-parse") return "resolved"
if (args[0] === "log") return "sha1 2026-11-01"
return JSON.stringify(legacyBase)
},
"origin/v2",
{ console: "Out of scope" },
)
expect(points).toEqual([{ date: "2026-11-01", done: 10, total: 20, percent: 50 }])
})
test("a feature excluded between two snapshots does not look like reopened cells", () => {
const kinds = () => ({ module: { done: 1, total: 2 }, api: { done: 0, total: 0 }, patch: { done: 0, total: 0 }, test: { done: 0, total: 0 } })
const cell = { kind: "module" as const, feature: "console", source: "packages/kilo-console/src/App.tsx" }
const cells = [{ id: "module:packages/kilo-console/src/App.tsx", ...cell, v2: "packages/kilo-console/src/App.tsx" }]
// The snapshot record keeps the cell even though `console` is excluded, so both snapshots list it.
expect(cellRecord(cells).done).toEqual(["module:packages/kilo-console/src/App.tsx"])
const base = { features: [{ feature: "console", done: 1, total: 2, dropped: 0, kinds: kinds() }], ...cellRecord(cells) }
const head = { features: [{ feature: "console", done: 1, total: 2, dropped: 0, kinds: kinds(), excluded: "Out of scope" }], ...cellRecord(cells) }
const delta = compare(base, head)
expect(delta).not.toContain("cells reopened")
expect(delta).toContain("| console | 1/2 | excluded | -1 module done, -2 modules total |")
})
test("the snapshot cell record drops unattributed cells but keeps excluded ones", () => {
const record = cellRecord([
// an excluded feature keeps its done cell, so it never looks reopened
{ id: "module:a", kind: "module", feature: "console", source: "a", v2: "a" },
{ id: "patch:gateway:0123456789ab", kind: "patch", feature: "gateway", source: "b:1", v2: "c.ts:1", rung: 1 },
// unattributed hunks belong to no feature, so they stay out of the record and out of `summary`
{ id: "patch:unattributed:session:ffffffffffff", kind: "patch", feature: "unattributed:session", source: "d:1", state: "dropped" },
{ id: "patch:unattributed:session:0123456789ab", kind: "patch", feature: "unattributed:session", source: "e:1", v2: "f.ts:1", rung: 1 },
])
expect(record).toEqual({
done: ["module:a", "patch:gateway:0123456789ab"],
dropped: [],
landed: { "patch:gateway:0123456789ab": "plugin" },
})
})