-
Notifications
You must be signed in to change notification settings - Fork 0
Add repo wiki advisory context packets #474
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 3 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
6579672
Add repo wiki advisory context packets
ada4e1d
Gate missing repo wiki sources as stale
eb39352
Recompute repo wiki packet hashes
f3ed50e
Honor review-mode context demotion
83a8254
Tighten repo wiki freshness gates
99ca85c
Confine repo wiki packet paths
a5aaabf
Verify repo wiki packet freshness
1171740
Fix repo wiki packet file read race
929c9bd
Quote advisory context packets in prompts
e4f7b3a
merge: update OpenWiki advisory context with main
dd4ee65
fix: validate repo wiki packet metadata
db498ab
Merge remote-tracking branch 'origin/main' into issue-415-openwiki-ad…
66d7712
Merge remote-tracking branch 'origin/main' into issue-415-openwiki-ad…
16f20ae
fix: degrade unreadable repo wiki packets
30124de
Merge remote-tracking branch 'origin/main' into issue-415-openwiki-ad…
18615bd
fix: harden advisory packet context handling
281d619
Merge remote-tracking branch 'origin/main' into issue-415-openwiki-ad…
d370566
fix: cap repo wiki packet reads
e9a9310
fix: read repo wiki packet through bounded fd
1b39d00
test: cover repo wiki worker success path
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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,262 @@ | ||
| import { createHash } from "node:crypto"; | ||
| import { existsSync, readFileSync } from "node:fs"; | ||
| import { isAbsolute, resolve } from "node:path"; | ||
| import { formatRepoWikiPacketMarkdown, type RepoWikiPacket, type RepoWikiSourceStatus } from "./repo-wiki-packet.js"; | ||
| import { containsSecretLikeText, redactSecrets } from "./secrets.js"; | ||
|
|
||
| export interface RepoWikiContextConfig { | ||
| enabled: boolean; | ||
| packetPath: string; | ||
| maxPacketBytes: number; | ||
| includeStaleContext: boolean; | ||
| } | ||
|
|
||
| export interface RepoWikiContextPacket { | ||
| sha256: string; | ||
| byteEstimate: number; | ||
| tokenEstimate: number; | ||
| markdown: string; | ||
| repoWiki: { | ||
| freshness: RepoWikiSourceStatus | "unknown"; | ||
| degradedMode: boolean; | ||
| degradedReason?: string; | ||
| sourcePath?: string; | ||
| packetVersion?: string; | ||
| }; | ||
| } | ||
|
|
||
| export type RepoWikiContextOmittedReason = | ||
| "disabled" | "missing_packet" | "stale_packet" | "budget_exceeded" | "secret_detected" | "invalid_packet"; | ||
|
|
||
| export type RepoWikiContextBuildResult = | ||
| | { packet: RepoWikiContextPacket; omitted?: never } | ||
| | { | ||
| packet?: never; | ||
| omitted: { | ||
| reason: RepoWikiContextOmittedReason; | ||
| detail: string; | ||
| sourcePath?: string; | ||
| }; | ||
| }; | ||
|
|
||
| export function buildRepoWikiContextPacket(input: { | ||
| repo: string; | ||
| worktreePath: string; | ||
| config: RepoWikiContextConfig; | ||
| }): RepoWikiContextBuildResult { | ||
| if (!input.config.enabled) { | ||
| return { | ||
| omitted: { | ||
| reason: "disabled", | ||
| detail: "repoWikiContext.enabled is false" | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| const sourcePath = resolvePacketPath(input.worktreePath, input.config.packetPath); | ||
| const evidenceSourcePath = formatPacketPathForEvidence(input.config.packetPath); | ||
| if (!existsSync(sourcePath)) { | ||
| return { | ||
| omitted: { | ||
| reason: "missing_packet", | ||
| detail: "Repo wiki packet not found", | ||
| sourcePath: evidenceSourcePath | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| const raw = readFileSync(sourcePath, "utf8"); | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| if (containsSecretLikeText(raw)) { | ||
| return { | ||
| omitted: { | ||
| reason: "secret_detected", | ||
| detail: "Repo wiki packet contains secret-like text", | ||
| sourcePath: evidenceSourcePath | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| const parsed = parseRepoWikiContextRaw(raw, evidenceSourcePath); | ||
| if (!parsed.ok) { | ||
| return { | ||
| omitted: { reason: "invalid_packet", detail: parsed.error, sourcePath: evidenceSourcePath } | ||
| }; | ||
| } | ||
|
|
||
| const byteEstimate = Buffer.byteLength(parsed.packet.markdown, "utf8"); | ||
| if (byteEstimate > input.config.maxPacketBytes) { | ||
|
100yenadmin marked this conversation as resolved.
Outdated
|
||
| return { | ||
| omitted: { | ||
| reason: "budget_exceeded", | ||
| detail: `Repo wiki packet exceeded maxPacketBytes (${byteEstimate} > ${input.config.maxPacketBytes})`, | ||
| sourcePath: evidenceSourcePath | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| if (containsSecretLikeText(parsed.packet.markdown)) { | ||
| return { | ||
| omitted: { | ||
| reason: "secret_detected", | ||
| detail: "Rendered repo wiki packet contains secret-like text", | ||
| sourcePath: evidenceSourcePath | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| const freshness = parsed.packet.repoWiki.freshness; | ||
| if ((freshness === "stale" || freshness === "missing") && !input.config.includeStaleContext) { | ||
| return { | ||
| omitted: { | ||
| reason: "stale_packet", | ||
| detail: "Repo wiki packet is not fresh and includeStaleContext is false", | ||
| sourcePath: evidenceSourcePath | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| return { | ||
| packet: { | ||
| ...parsed.packet, | ||
| byteEstimate, | ||
| tokenEstimate: Math.max(1, Math.ceil(byteEstimate / 4)), | ||
| repoWiki: { | ||
| ...parsed.packet.repoWiki, | ||
| sourcePath: evidenceSourcePath | ||
| } | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| function resolvePacketPath(worktreePath: string, packetPath: string): string { | ||
| return isAbsolute(packetPath) ? packetPath : resolve(worktreePath, packetPath); | ||
|
100yenadmin marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| function formatPacketPathForEvidence(packetPath: string): string { | ||
| return isAbsolute(packetPath) ? "[absolute-packet-path]" : packetPath; | ||
| } | ||
|
|
||
| function parseRepoWikiContextRaw( | ||
| raw: string, | ||
| sourcePath: string | ||
| ): { ok: true; packet: RepoWikiContextPacket } | { ok: false; error: string } { | ||
| const trimmed = raw.trimStart(); | ||
| if (!trimmed) return { ok: false, error: "Repo wiki packet is empty" }; | ||
| if (!trimmed.startsWith("{")) return packetFromMarkdown(raw); | ||
|
|
||
| try { | ||
| const parsed = JSON.parse(raw) as unknown; | ||
| if (!isRecord(parsed)) return { ok: false, error: "Repo wiki packet JSON must be an object" }; | ||
| if (typeof parsed.markdown === "string") return packetFromGenericJson(parsed); | ||
| if (looksLikeRepoWikiPacket(parsed)) return packetFromRepoWikiPacket(parsed); | ||
| return { | ||
| ok: false, | ||
| error: `Unsupported repo wiki packet shape at ${redactSecrets(sourcePath)}` | ||
| }; | ||
| } catch (error) { | ||
| return { | ||
| ok: false, | ||
| error: `Repo wiki packet JSON did not parse: ${error instanceof Error ? error.message : String(error)}` | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| function packetFromMarkdown(markdown: string): { | ||
| ok: true; | ||
| packet: RepoWikiContextPacket; | ||
| } { | ||
| const byteEstimate = Buffer.byteLength(markdown, "utf8"); | ||
| return { | ||
| ok: true, | ||
| packet: { | ||
| sha256: sha256(markdown), | ||
| byteEstimate, | ||
| tokenEstimate: Math.max(1, Math.ceil(byteEstimate / 4)), | ||
| markdown, | ||
| repoWiki: { | ||
| freshness: "unknown", | ||
| degradedMode: false | ||
| } | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| function packetFromGenericJson(parsed: Record<string, unknown>): { | ||
| ok: true; | ||
| packet: RepoWikiContextPacket; | ||
| } { | ||
| const markdown = String(parsed.markdown); | ||
|
100yenadmin marked this conversation as resolved.
|
||
| const byteEstimate = Buffer.byteLength(markdown, "utf8"); | ||
| const freshness = | ||
| readFreshness(parsed.freshness) ?? readFreshness(readNested(parsed, "repoWiki", "freshness")) ?? "unknown"; | ||
| const degradedMode = | ||
| typeof readNested(parsed, "repoWiki", "degradedMode") === "boolean" | ||
| ? Boolean(readNested(parsed, "repoWiki", "degradedMode")) | ||
| : freshness === "stale" || freshness === "missing"; | ||
|
100yenadmin marked this conversation as resolved.
Outdated
|
||
|
|
||
| return { | ||
| ok: true, | ||
| packet: { | ||
| sha256: sha256(markdown), | ||
| byteEstimate, | ||
| tokenEstimate: Math.max(1, Math.ceil(byteEstimate / 4)), | ||
| markdown, | ||
| repoWiki: { | ||
|
100yenadmin marked this conversation as resolved.
|
||
| freshness, | ||
| degradedMode, | ||
| ...(typeof parsed.packetVersion === "string" ? { packetVersion: parsed.packetVersion } : {}) | ||
| } | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| function packetFromRepoWikiPacket(packet: RepoWikiPacket): { | ||
| ok: true; | ||
| packet: RepoWikiContextPacket; | ||
| } { | ||
| const markdown = formatRepoWikiPacketMarkdown(packet); | ||
| const byteEstimate = Buffer.byteLength(markdown, "utf8"); | ||
| return { | ||
| ok: true, | ||
| packet: { | ||
| sha256: sha256(markdown), | ||
| byteEstimate, | ||
| tokenEstimate: Math.max(1, Math.ceil(byteEstimate / 4)), | ||
| markdown, | ||
| repoWiki: { | ||
| freshness: packet.source.status, | ||
|
100yenadmin marked this conversation as resolved.
Outdated
|
||
| degradedMode: packet.degraded, | ||
| ...(packet.source.staleReason ? { degradedReason: packet.source.staleReason } : {}), | ||
| packetVersion: packet.packetVersion | ||
| } | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| function looksLikeRepoWikiPacket(input: unknown): input is RepoWikiPacket { | ||
|
100yenadmin marked this conversation as resolved.
Outdated
|
||
| if (!isRecord(input)) return false; | ||
| return ( | ||
| isRecord(input.repo) && | ||
| isRecord(input.source) && | ||
| typeof input.source.status === "string" && | ||
| Array.isArray(input.includedSections) && | ||
| typeof input.packetSha === "string" | ||
| ); | ||
| } | ||
|
|
||
| function readFreshness(value: unknown): RepoWikiSourceStatus | "unknown" | undefined { | ||
| return value === "fresh" || value === "stale" || value === "missing" || value === "unknown" ? value : undefined; | ||
| } | ||
|
|
||
| function readNested(input: Record<string, unknown>, objectKey: string, valueKey: string): unknown { | ||
|
100yenadmin marked this conversation as resolved.
Outdated
|
||
| const nested = input[objectKey]; | ||
| return isRecord(nested) ? nested[valueKey] : undefined; | ||
| } | ||
|
|
||
| function isRecord(value: unknown): value is Record<string, unknown> { | ||
| return typeof value === "object" && value !== null && !Array.isArray(value); | ||
| } | ||
|
|
||
| function sha256(text: string): string { | ||
| return createHash("sha256").update(text).digest("hex"); | ||
| } | ||
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.