-
Notifications
You must be signed in to change notification settings - Fork 3.1k
refactor: centralize NUL-delimited composite keys #13532
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| export function zeroID(...parts: (string | number | boolean)[]) { | ||
| if (parts.length === 2) return `${parts[0]}\0${parts[1]}` | ||
| if (parts.length === 3) return `${parts[0]}\0${parts[1]}\0${parts[2]}` | ||
| return parts.join("\0") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import { describe, expect, expectTypeOf, test } from "bun:test" | ||
| import { zeroID } from "@opencode-ai/core/kilocode/zero-id" | ||
|
|
||
| describe("zeroID", () => { | ||
| test("accepts only string, number, and boolean parts", () => { | ||
| expectTypeOf<Parameters<typeof zeroID>>().toEqualTypeOf<(string | number | boolean)[]>() | ||
| expectTypeOf<ReturnType<typeof zeroID>>().toEqualTypeOf<string>() | ||
| }) | ||
|
|
||
| test("matches template literals for short composite keys", () => { | ||
| const values = ["", "a", "a\0b", "路径", "null", "undefined", 0, -0, -1, 0.5, NaN, Infinity, -Infinity, true, false] | ||
| for (const first of values) { | ||
| for (const second of values) { | ||
| expect(zeroID(first, second)).toBe(`${first}\0${second}`) | ||
| for (const third of values) { | ||
| expect(zeroID(first, second, third)).toBe(`${first}\0${second}\0${third}`) | ||
| } | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| test("matches array joins across arities and preserves empty parts", () => { | ||
| const cases: Parameters<typeof zeroID>[] = [ | ||
| [], | ||
| [""], | ||
| [false], | ||
| [0], | ||
| ["", ""], | ||
| ["", "", ""], | ||
| ["", "", "", ""], | ||
| ["prefix", "", 0, false, "suffix"], | ||
| ["/repo", "", "ancestor", "file.ts", true, "modified", 3, 0, false, ""], | ||
| ["\0", "a\0b", "", "路径", NaN, -0, -Infinity, true, false], | ||
| ] | ||
| for (const parts of cases) { | ||
| const expected = parts.join("\0") | ||
| expect(zeroID(...parts)).toBe(expected) | ||
| expect(Buffer.from(zeroID(...parts))).toEqual(Buffer.from(expected)) | ||
| } | ||
| }) | ||
|
|
||
| test("preserves namespace prefixes, suffixes, and nested keys", () => { | ||
| expect(zeroID("project", "")).toBe("project\0") | ||
| expect(zeroID("", "file.ts")).toBe("\0file.ts") | ||
| expect(zeroID("error", "message")).toBe("error\0message") | ||
| expect(zeroID(zeroID("project", "session"), "file.ts")).toBe("project\0session\0file.ts") | ||
| expect(zeroID("ab", "c")).not.toBe(zeroID("a", "bc")) | ||
| expect(zeroID("project", "session").startsWith(zeroID("project", ""))).toBe(true) | ||
| expect(zeroID("project-other", "session").startsWith(zeroID("project", ""))).toBe(false) | ||
| }) | ||
|
|
||
| test("supports explicit caller-specific nullish coercion", () => { | ||
| for (const value of [null, undefined]) { | ||
| expect(zeroID("scope", String(value))).toBe(`scope\0${value}`) | ||
| expect(zeroID("scope", value ?? "")).toBe(["scope", value].join("\0")) | ||
| } | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.