-
Notifications
You must be signed in to change notification settings - Fork 11
Refactor workspace content architecture #664
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
Show all changes
16 commits
Select commit
Hold shift + click to select a range
075f09c
refactor(workspaces): make kernel the canonical item boundary
urjitc eca0329
fix(workspaces): bound projection page reads
urjitc 70a2e49
feat(workspaces): unify AI content reads
urjitc f25f4af
fix(workspaces): bound content projection reads
urjitc d27e494
fix(workspaces): harden realtime projection state
urjitc 7546ad3
fix(workspaces): retire unsupported item rows
urjitc ce9ed12
refactor(workspaces): preserve future item surfaces
urjitc cddb113
refactor(workspaces): deepen read boundaries
urjitc e4a9178
fix(workspaces): tighten content boundaries
urjitc 837a7d9
refactor(core): consolidate binary encoding
urjitc e48391c
refactor(workspaces): centralize item policy
urjitc e9547f6
refactor(workspaces): separate mutation receipts
urjitc a6368c4
fix(workspaces): align kernel create contract
urjitc 1b72b86
refactor(workspaces): remove mutation replay receipts
urjitc 1edf3e3
test(workspaces): remove brittle receipt copy tests
urjitc 50fbf55
refactor(workspaces): simplify content read internals
urjitc 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
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
89 changes: 89 additions & 0 deletions
89
src/features/workspaces/content/workspace-content-contract.test.ts
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,89 @@ | ||
| import { asSchema } from "ai"; | ||
| import { describe, expect, it } from "vitest"; | ||
|
|
||
| import { | ||
| workspaceReadItemsInputSchema, | ||
| workspaceReadItemsOutputSchema, | ||
| } from "#/features/workspaces/content/workspace-content-contract"; | ||
|
|
||
| describe("workspace read tool schemas", () => { | ||
| it("uses one explicit read mode per request", () => { | ||
| expect( | ||
| workspaceReadItemsInputSchema.safeParse({ | ||
| requests: [ | ||
| { mode: "start", path: "/Notes" }, | ||
| { mode: "pages", path: "/Book.pdf", range: "1-3" }, | ||
| { cursor: "opaque", mode: "continue", path: "/Notes" }, | ||
| ], | ||
| }).success, | ||
| ).toBe(true); | ||
| expect( | ||
| workspaceReadItemsInputSchema.safeParse({ | ||
| requests: [{ path: "/Book.pdf", pages: "1-3" }], | ||
| }).success, | ||
| ).toBe(false); | ||
| expect( | ||
| workspaceReadItemsInputSchema.safeParse({ requests: [{ path: "/Notes" }] }).success, | ||
| ).toBe(false); | ||
| }); | ||
|
|
||
| it("emits a strict-provider-compatible JSON Schema", () => { | ||
| const { jsonSchema } = asSchema(workspaceReadItemsInputSchema); | ||
|
|
||
| expect(jsonSchema).toMatchObject({ | ||
| additionalProperties: false, | ||
| properties: { | ||
| requests: { | ||
| items: { | ||
| anyOf: [ | ||
| { additionalProperties: false, required: ["path", "mode"] }, | ||
| { additionalProperties: false, required: ["path", "mode", "range"] }, | ||
| { additionalProperties: false, required: ["path", "cursor", "mode"] }, | ||
| ], | ||
| }, | ||
| }, | ||
| }, | ||
| required: ["requests"], | ||
| }); | ||
| expect(JSON.stringify(jsonSchema)).not.toContain('"oneOf"'); | ||
| }); | ||
|
|
||
| it("keeps document and file result shapes disjoint", () => { | ||
| expect( | ||
| workspaceReadItemsOutputSchema.safeParse({ | ||
| results: [ | ||
| { | ||
| content: "# Notes", | ||
| format: "markdown", | ||
| location: { endLine: 1, kind: "lines", startLine: 1, totalLines: 1 }, | ||
| path: "/Notes", | ||
| status: "ready", | ||
| type: "document", | ||
| }, | ||
| { | ||
| content: "Page one", | ||
| format: "markdown", | ||
| location: { kind: "pages", requested: "1", returned: [1], total: 1 }, | ||
| path: "/Book.pdf", | ||
| status: "ready", | ||
| type: "file", | ||
| }, | ||
| ], | ||
| }).success, | ||
| ).toBe(true); | ||
| expect( | ||
| workspaceReadItemsOutputSchema.safeParse({ | ||
| results: [ | ||
| { | ||
| content: "Page one", | ||
| format: "markdown", | ||
| location: { kind: "pages", requested: "1", returned: [1], total: 1 }, | ||
| path: "/Notes", | ||
| status: "ready", | ||
| type: "document", | ||
| }, | ||
| ], | ||
| }).success, | ||
| ).toBe(false); | ||
| }); | ||
| }); |
118 changes: 118 additions & 0 deletions
118
src/features/workspaces/content/workspace-content-contract.ts
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,118 @@ | ||
| import { z } from "zod"; | ||
|
|
||
| import { workspaceRelationKindSchema } from "#/features/workspaces/contracts"; | ||
|
|
||
| const workspacePathSchema = z.string().min(1); | ||
|
|
||
| const readWorkspaceItemsFailureCodes = [ | ||
| "content_changed", | ||
| "invalid_cursor", | ||
| "invalid_selection", | ||
| "page_range_out_of_range", | ||
| "page_selection_too_large", | ||
| "read_budget_exceeded", | ||
| "path_is_folder", | ||
| "path_not_absolute", | ||
| "path_not_found", | ||
| "projection_failed", | ||
| "unsupported_item_type", | ||
| ] as const; | ||
|
|
||
| const workspacePageRangeSchema = z | ||
| .string() | ||
| .trim() | ||
| .min(1) | ||
| .regex(/^\d+(?:\s*-\s*\d+)?(?:\s*,\s*\d+(?:\s*-\s*\d+)?)*$/) | ||
| .describe( | ||
| "Up to 20 physical pages from an extracted file, like 1, 3, 5-7, or 1,4-6. Defaults to page 1.", | ||
| ); | ||
|
|
||
| const workspaceContentReadRequestBase = { | ||
| path: workspacePathSchema.describe("Absolute path of the workspace item to read."), | ||
| }; | ||
|
|
||
| const workspaceContentReadRequestSchema = z.union([ | ||
| z.strictObject({ | ||
| ...workspaceContentReadRequestBase, | ||
| mode: z.literal("start"), | ||
| }), | ||
| z.strictObject({ | ||
| ...workspaceContentReadRequestBase, | ||
| mode: z.literal("pages"), | ||
| range: workspacePageRangeSchema, | ||
| }), | ||
| z.strictObject({ | ||
| ...workspaceContentReadRequestBase, | ||
| cursor: z.string().min(1).max(4_096).describe("Opaque cursor returned by a previous read."), | ||
| mode: z.literal("continue"), | ||
| }), | ||
| ]); | ||
|
|
||
| export const workspaceReadItemsInputSchema = z.object({ | ||
| requests: z | ||
| .array(workspaceContentReadRequestSchema) | ||
| .min(1) | ||
| .max(20) | ||
| .describe("Ordered workspace content reads."), | ||
| }); | ||
|
|
||
| const workspaceReadPagesSchema = z.object({ | ||
| requested: z.string().describe("Requested page range."), | ||
| returned: z.array(z.number().int().min(1)).describe("Page numbers included in content."), | ||
| total: z.number().int().min(1).describe("Total pages available."), | ||
| }); | ||
|
|
||
| const workspaceReadRelationsSchema = z.array( | ||
| z.object({ | ||
| direction: z.enum(["incoming", "outgoing"]), | ||
| kind: workspaceRelationKindSchema, | ||
| note: z.string().optional(), | ||
| path: workspacePathSchema, | ||
| }), | ||
| ); | ||
|
|
||
| const workspaceContentReadResultSchema = z.union([ | ||
| z.object({ | ||
| content: z.string(), | ||
| format: z.literal("markdown"), | ||
| location: z.object({ | ||
| endLine: z.number().int().nonnegative(), | ||
| kind: z.literal("lines"), | ||
| startLine: z.number().int().nonnegative(), | ||
| totalLines: z.number().int().nonnegative(), | ||
| }), | ||
| nextCursor: z.string().optional(), | ||
| path: workspacePathSchema, | ||
| relations: workspaceReadRelationsSchema.optional(), | ||
| status: z.literal("ready"), | ||
| type: z.literal("document"), | ||
| }), | ||
| z.object({ | ||
| content: z.string(), | ||
| format: z.literal("markdown"), | ||
| location: workspaceReadPagesSchema.extend({ kind: z.literal("pages") }), | ||
| nextCursor: z.string().optional(), | ||
| path: workspacePathSchema, | ||
| relations: workspaceReadRelationsSchema.optional(), | ||
| status: z.literal("ready"), | ||
| type: z.literal("file"), | ||
| }), | ||
| z.object({ | ||
| path: workspacePathSchema, | ||
| status: z.literal("pending"), | ||
| type: z.literal("file"), | ||
| }), | ||
| z.object({ | ||
| code: z.enum(readWorkspaceItemsFailureCodes), | ||
| path: workspacePathSchema, | ||
| status: z.literal("failed"), | ||
| type: z.literal("file").optional(), | ||
| }), | ||
| ]); | ||
|
|
||
| export const workspaceReadItemsOutputSchema = z.object({ | ||
| results: z.array(workspaceContentReadResultSchema), | ||
| }); | ||
|
|
||
| export type WorkspaceContentReadRequest = z.output<typeof workspaceContentReadRequestSchema>; | ||
| export type WorkspaceContentReadResult = z.output<typeof workspaceContentReadResultSchema>; |
37 changes: 37 additions & 0 deletions
37
src/features/workspaces/content/workspace-content-cursor.ts
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,37 @@ | ||
| import { z } from "zod"; | ||
|
|
||
| import { decodeBase64UrlText, encodeBase64UrlText } from "#/lib/binary"; | ||
|
|
||
| const workspaceContentCursorSchema = z.discriminatedUnion("kind", [ | ||
| z.object({ | ||
| itemId: z.string().min(1), | ||
| kind: z.literal("document"), | ||
| offset: z.number().int().nonnegative(), | ||
| revision: z.string().min(1), | ||
| version: z.literal(1), | ||
| }), | ||
| z.object({ | ||
| itemId: z.string().min(1), | ||
| kind: z.literal("file"), | ||
| nextPage: z.number().int().positive(), | ||
| sourceHash: z.string().min(1), | ||
| version: z.literal(1), | ||
| }), | ||
| ]); | ||
|
|
||
| type WorkspaceContentCursor = z.infer<typeof workspaceContentCursorSchema>; | ||
|
|
||
| export function encodeWorkspaceContentCursor(cursor: WorkspaceContentCursor) { | ||
| return encodeBase64UrlText(JSON.stringify(cursor)); | ||
| } | ||
|
|
||
| export function decodeWorkspaceContentCursor(value: string): WorkspaceContentCursor | null { | ||
| if (value.length > 4_096) { | ||
| return null; | ||
| } | ||
| try { | ||
| return workspaceContentCursorSchema.parse(JSON.parse(decodeBase64UrlText(value))); | ||
| } catch { | ||
| return null; | ||
| } | ||
| } |
Oops, something went wrong.
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.