@loucompanion/forge-bridge 0.2.1-dev.573a8a5fa582 → 0.2.1-dev.7171bf414613
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +38 -2
- package/dist/bridge/bridge-state-io.d.ts +6 -2
- package/dist/bridge/bridge-state-io.js +22 -4
- package/dist/bridge/bridge.service.d.ts +10 -0
- package/dist/bridge/bridge.service.js +62 -92
- package/dist/bridge/bridge.types.d.ts +6 -0
- package/dist/bridge/default-session-service.d.ts +13 -0
- package/dist/bridge/default-session-service.js +22 -0
- package/dist/bridge/dispatch-offer-policy.d.ts +22 -0
- package/dist/bridge/dispatch-offer-policy.js +58 -0
- package/dist/cleanup/cleanup-wip.js +3 -3
- package/dist/cleanup/forge-owned-paths.d.ts +2 -0
- package/dist/cleanup/forge-owned-paths.js +9 -0
- package/dist/cli/cli.help.js +1 -1
- package/dist/cli/cli.service.js +3 -3
- package/dist/cli/local-config.d.ts +2 -0
- package/dist/cli/local-config.js +3 -0
- package/dist/runtime/host-load-capacity.d.ts +198 -0
- package/dist/runtime/host-load-capacity.js +353 -0
- package/dist/runtime/host-load-child-environment.d.ts +13 -0
- package/dist/runtime/host-load-child-environment.js +35 -1
- package/dist/runtime/host-load-governor-config.d.ts +23 -2
- package/dist/runtime/host-load-governor-config.js +20 -2
- package/dist/runtime/host-load-governor.d.ts +81 -1
- package/dist/runtime/host-load-governor.js +184 -15
- package/dist/runtime/host-load-live-test-slots.d.ts +61 -0
- package/dist/runtime/host-load-live-test-slots.js +193 -0
- package/dist/runtime/host-load-sampler.d.ts +17 -0
- package/dist/runtime/host-load-sampler.js +24 -0
- package/dist/runtime/host-load-step-run.d.ts +37 -3
- package/dist/runtime/host-load-step-run.js +63 -7
- package/dist/runtime/host-load-test-slot.d.ts +3 -0
- package/dist/runtime/host-load-test-slot.js +19 -7
- package/dist/sandbox/runtime.d.ts +1 -2
- package/dist/sandbox/runtime.js +7 -13
- package/dist/sandbox/sandbox-input.d.ts +22 -0
- package/dist/sandbox/sandbox-input.js +49 -0
- package/dist/sandbox/workspace.service.js +15 -2
- package/dist/session/managed-attempt-tools.js +6 -29
- package/dist/session/managed-build-receipt.d.ts +3 -1
- package/dist/session/managed-build-receipt.js +5 -2
- package/dist/session/managed-command-environment.d.ts +8 -0
- package/dist/session/managed-command-environment.js +28 -0
- package/dist/session/managed-command-host-load.d.ts +10 -5
- package/dist/session/managed-command-host-load.js +18 -14
- package/dist/session/phase-completion.artifact-bundle.d.ts +20 -0
- package/dist/session/phase-completion.artifact-bundle.js +167 -0
- package/dist/session/phase-completion.artifacts.d.ts +2 -17
- package/dist/session/phase-completion.artifacts.js +63 -165
- package/dist/session/phase-completion.d.ts +25 -2
- package/dist/session/phase-completion.helpers.js +5 -53
- package/dist/session/phase-completion.js +15 -32
- package/dist/session/phase-completion.mcp-args.js +69 -2
- package/dist/session/phase-completion.phase-aliases.d.ts +5 -0
- package/dist/session/phase-completion.phase-aliases.js +43 -0
- package/dist/session/phase-completion.pr-review-schema.d.ts +1 -0
- package/dist/session/phase-completion.pr-review-schema.js +75 -0
- package/dist/session/phase-completion.pr-review-threads.d.ts +34 -0
- package/dist/session/phase-completion.pr-review-threads.js +90 -0
- package/dist/session/phase-completion.pr-review-validation.d.ts +11 -0
- package/dist/session/phase-completion.pr-review-validation.js +215 -0
- package/dist/session/phase-completion.pr-review.d.ts +36 -0
- package/dist/session/phase-completion.pr-review.js +294 -0
- package/dist/session/phase-completion.prompts.d.ts +1 -1
- package/dist/session/phase-completion.prompts.js +14 -0
- package/dist/session/phase-completion.runtime.js +9 -3
- package/dist/session/phase-completion.schemas.js +12 -2
- package/dist/session/phase-completion.validation.d.ts +1 -0
- package/dist/session/phase-completion.validation.js +9 -1
- package/dist/session/pr-review-diff-base.d.ts +18 -0
- package/dist/session/pr-review-diff-base.js +45 -0
- package/dist/session/pr-review-placement.d.ts +50 -0
- package/dist/session/pr-review-placement.js +91 -0
- package/dist/session/pre-publication-change-set.d.ts +62 -0
- package/dist/session/pre-publication-change-set.js +166 -0
- package/dist/session/pre-publication-host-load.js +6 -5
- package/dist/session/pre-publication-process.js +3 -2
- package/dist/session/pre-publication-validation.d.ts +2 -1
- package/dist/session/pre-publication-validation.js +31 -36
- package/dist/session/provider-cli/dispatch-target-branch.d.ts +7 -0
- package/dist/session/provider-cli/dispatch-target-branch.js +8 -0
- package/dist/session/provider-cli/pr-review-credential-isolation.d.ts +83 -0
- package/dist/session/provider-cli/pr-review-credential-isolation.js +144 -0
- package/dist/session/provider-cli/pr-review-guardrails.d.ts +70 -0
- package/dist/session/provider-cli/pr-review-guardrails.js +360 -0
- package/dist/session/provider-cli/pr-review-prompt.d.ts +11 -0
- package/dist/session/provider-cli/pr-review-prompt.js +132 -0
- package/dist/session/provider-cli/pre-publication-repair.js +7 -3
- package/dist/session/provider-cli/provider-cli.base.d.ts +1 -0
- package/dist/session/provider-cli/provider-cli.deterministic.d.ts +1 -0
- package/dist/session/provider-cli/provider-cli.deterministic.js +5 -0
- package/dist/session/provider-cli/provider-cli.pr-review.d.ts +23 -0
- package/dist/session/provider-cli/provider-cli.pr-review.js +213 -0
- package/dist/session/provider-cli/provider-cli.prompts.d.ts +10 -13
- package/dist/session/provider-cli/provider-cli.prompts.js +123 -107
- package/dist/session/provider-cli/provider-cli.reapply-conflicts.d.ts +10 -0
- package/dist/session/provider-cli/provider-cli.reapply-conflicts.js +37 -0
- package/dist/session/provider-cli/provider-cli.runtime.d.ts +7 -0
- package/dist/session/provider-cli/provider-cli.runtime.js +14 -0
- package/dist/session/provider-cli/provider-cli.service-support.d.ts +37 -0
- package/dist/session/provider-cli/provider-cli.service-support.js +146 -0
- package/dist/session/provider-cli/provider-cli.service.d.ts +11 -17
- package/dist/session/provider-cli/provider-cli.service.js +24 -139
- package/dist/session/sandbox-host-capacity-gate.js +28 -6
- package/dist/session/session.parse-helpers.d.ts +10 -0
- package/dist/session/session.parse-helpers.js +84 -0
- package/dist/session/session.runtime-refresh.d.ts +46 -0
- package/dist/session/session.runtime-refresh.js +116 -0
- package/dist/session/session.service.d.ts +2 -1
- package/dist/session/session.service.js +40 -95
- package/dist/session/session.types.d.ts +4 -0
- package/dist/session/session.types.js +7 -77
- package/dist/session/session.types.pr-review.d.ts +59 -0
- package/dist/session/session.types.pr-review.js +53 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist/worktree/artifact-injector.js +24 -0
- package/dist/worktree/worktree.service.d.ts +22 -0
- package/dist/worktree/worktree.service.js +116 -12
- package/dist/worktree/worktree.types.d.ts +7 -0
- package/package.json +1 -1
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { evidenceObjectSchema, objectToolSchema } from "./phase-completion.schema-parts.js";
|
|
2
|
+
export function prReviewToolInputSchema() {
|
|
3
|
+
const stringArray = { type: "array", items: { type: "string" } };
|
|
4
|
+
return objectToolSchema({
|
|
5
|
+
status: {
|
|
6
|
+
type: "string",
|
|
7
|
+
enum: ["reviewed", "stale_head", "blocked"],
|
|
8
|
+
description: "reviewed for a completed review. stale_head when `git rev-parse HEAD` differs from the dispatched head_sha (set head_sha to what you found). blocked, with a reason, when the diff cannot be read."
|
|
9
|
+
},
|
|
10
|
+
head_sha: { type: "string", description: "Full sha of the head you reviewed: the output of `git rev-parse HEAD`. Must be the dispatched head for status reviewed." },
|
|
11
|
+
base_sha: { type: "string", description: "The dispatched base sha you diffed against." },
|
|
12
|
+
review_effort: { type: "string", enum: ["lite", "standard", "deep"] },
|
|
13
|
+
verdict: { type: "string", enum: ["changes_recommended", "needs_closer_look", "approval_recommended"], description: "changes_recommended exactly when there is an open issue: a finding in this payload or a prior thread that is still_open or reintroduced." },
|
|
14
|
+
verdict_reason: { type: "string", description: "One sentence naming the open issues, or why approval is or is not recommended." },
|
|
15
|
+
summary: { type: "string", description: "A paragraph on what the pull request does." },
|
|
16
|
+
reason: { type: "string", description: "Required for status blocked: why the review could not be done." },
|
|
17
|
+
changes: { ...stringArray, description: "The main changes, as short bullets." },
|
|
18
|
+
files: {
|
|
19
|
+
type: "array",
|
|
20
|
+
description: "The reviewed files with a short description each.",
|
|
21
|
+
items: evidenceObjectSchema(["path", "description"], { path: { type: "string" }, description: { type: "string" } })
|
|
22
|
+
},
|
|
23
|
+
findings: {
|
|
24
|
+
type: "array",
|
|
25
|
+
description: "New inline comments only; may be empty. Every line must be on the new side of the pull request diff.",
|
|
26
|
+
items: evidenceObjectSchema(["path", "line", "side", "severity", "category", "title", "body"], {
|
|
27
|
+
path: { type: "string" },
|
|
28
|
+
line: { type: "integer", minimum: 1, description: "A line on the new side of the PR diff (a changed or context line inside a hunk)." },
|
|
29
|
+
start_line: { type: ["integer", "null"], minimum: 1, description: "First line of a multi-line range inside the same hunk, or null." },
|
|
30
|
+
side: { type: "string", enum: ["RIGHT"] },
|
|
31
|
+
severity: { type: "string", enum: ["high", "medium", "low"] },
|
|
32
|
+
category: { type: "string", enum: ["correctness", "security", "contract", "tests", "data", "docs"] },
|
|
33
|
+
title: { type: "string", description: "At most about 70 characters naming the defect." },
|
|
34
|
+
body: { type: "string", description: "The inline comment: defect, concrete scenario, evidence as path:line, and what to change." },
|
|
35
|
+
also_at: {
|
|
36
|
+
type: "array",
|
|
37
|
+
items: evidenceObjectSchema(["path", "line"], { path: { type: "string" }, line: { type: "integer", minimum: 1 } })
|
|
38
|
+
},
|
|
39
|
+
suggestion: { type: ["string", "null"] }
|
|
40
|
+
})
|
|
41
|
+
},
|
|
42
|
+
prior_threads: {
|
|
43
|
+
type: "array",
|
|
44
|
+
description: "Prior review threads from the dispatch, by thread_id. Still-open threads belong here, not in findings.",
|
|
45
|
+
items: evidenceObjectSchema(["thread_id", "status"], {
|
|
46
|
+
thread_id: { type: "string" },
|
|
47
|
+
status: { type: "string", enum: ["resolved_since_last_review", "still_open", "reintroduced"] }
|
|
48
|
+
})
|
|
49
|
+
},
|
|
50
|
+
tests_checked: {
|
|
51
|
+
type: "array",
|
|
52
|
+
description: "New or changed test assertions you traced to production code. produced_at \"not produced\" needs a matching tests finding.",
|
|
53
|
+
items: evidenceObjectSchema(["test", "assertion", "produced_at"], { test: { type: "string" }, assertion: { type: "string" }, produced_at: { type: "string" } })
|
|
54
|
+
},
|
|
55
|
+
security_review: {
|
|
56
|
+
type: "object",
|
|
57
|
+
additionalProperties: true,
|
|
58
|
+
description: "Required on every reviewed payload, including reviews with no findings.",
|
|
59
|
+
properties: {
|
|
60
|
+
status: { type: "string", enum: ["findings", "no_findings"], description: "findings exactly when some finding has category security." },
|
|
61
|
+
checked: stringArray,
|
|
62
|
+
keys_checked: {
|
|
63
|
+
type: "array",
|
|
64
|
+
items: evidenceObjectSchema(["key", "per_user_data", "includes_user"], { key: { type: "string" }, per_user_data: { type: "boolean" }, includes_user: { type: "boolean" } })
|
|
65
|
+
},
|
|
66
|
+
notes: { type: "string" }
|
|
67
|
+
},
|
|
68
|
+
required: ["status", "checked", "keys_checked", "notes"]
|
|
69
|
+
},
|
|
70
|
+
skipped_files: {
|
|
71
|
+
type: "array",
|
|
72
|
+
items: evidenceObjectSchema(["path", "reason"], { path: { type: "string" }, reason: { type: "string" } })
|
|
73
|
+
}
|
|
74
|
+
}, ["status"], "For status reviewed, include head_sha, base_sha, review_effort, verdict, verdict_reason, summary, changes, files, findings, security_review. For stale_head include head_sha. For blocked include reason. Forge rejects a head_sha that is not the checked-out head, non-RIGHT sides, lines off the diff, a verdict that contradicts the open issues, and unknown prior thread ids; fix the fields it names and call again.");
|
|
75
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { PrReviewCompletionContext } from "./phase-completion.js";
|
|
2
|
+
import type { RunOnceDispatchPacket } from "./session.types.js";
|
|
3
|
+
import type { DispatchPrReviewThread } from "./session.types.pr-review.js";
|
|
4
|
+
/**
|
|
5
|
+
* The pull-request facts the pr_review completion tool holds a review to: the admitted head
|
|
6
|
+
* (prReview.headSha, never the live pull request head) and the prior review threads the
|
|
7
|
+
* dispatch lists, and the dispatched base commit the payload's base_sha must resolve to. The
|
|
8
|
+
* unresolved threads Forge raised give the anchors a new finding must not repeat, and every
|
|
9
|
+
* unresolved thread the prompt showed must be reported.
|
|
10
|
+
*/
|
|
11
|
+
export declare function prReviewContextFromDispatch(dispatch: RunOnceDispatchPacket): PrReviewCompletionContext | undefined;
|
|
12
|
+
/** An unresolved prior thread Forge raised that still sits on the new side of the diff, as the lines it is anchored to. */
|
|
13
|
+
export interface PrReviewOpenThreadAnchor {
|
|
14
|
+
readonly threadId: string;
|
|
15
|
+
readonly path: string;
|
|
16
|
+
readonly startLine: number;
|
|
17
|
+
readonly line: number;
|
|
18
|
+
}
|
|
19
|
+
/** Repo-relative path in one spelling: forward slashes, no leading "./". */
|
|
20
|
+
export declare function normalizePrReviewPath(value: string): string;
|
|
21
|
+
/**
|
|
22
|
+
* The unresolved, current threads on the new side of the diff that an earlier Forge review raised: a finding inside
|
|
23
|
+
* one's lines repeats it. A finding inside a thread anybody else raised is the reviewer's own and is posted.
|
|
24
|
+
*/
|
|
25
|
+
export declare function openPrReviewThreadAnchors(threads: readonly DispatchPrReviewThread[]): PrReviewOpenThreadAnchor[];
|
|
26
|
+
/** The unresolved threads the reviewer was shown: each must be reported in prior_threads. */
|
|
27
|
+
export declare function requiredPrReviewThreadIds(shown: readonly DispatchPrReviewThread[]): string[];
|
|
28
|
+
/** One `--pr-review-open-thread` value: the id, start line, end line and path, each URI-encoded, comma-separated. */
|
|
29
|
+
export declare function encodePrReviewOpenThread(anchor: PrReviewOpenThreadAnchor): string;
|
|
30
|
+
export declare function decodePrReviewOpenThread(value: string | undefined): PrReviewOpenThreadAnchor | null;
|
|
31
|
+
/** Rejects a finding anchored inside the lines of an open thread an earlier Forge review raised: Forge already said it. */
|
|
32
|
+
export declare function validatePrReviewFindingAnchors(findings: readonly unknown[], anchors: readonly PrReviewOpenThreadAnchor[] | undefined): void;
|
|
33
|
+
/** Requires every unresolved thread the reviewer was shown to be reported in prior_threads. */
|
|
34
|
+
export declare function validatePrReviewThreadCompleteness(reported: ReadonlySet<string>, required: readonly string[] | undefined): void;
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { renderedPrReviewThreads } from "./provider-cli/pr-review-prompt.js";
|
|
2
|
+
// The completion tool's half of "re-review without repeating": a review never posts a new comment where an
|
|
3
|
+
// unresolved thread an earlier Forge review raised already is, and it reports every unresolved thread the reviewer
|
|
4
|
+
// was shown. Which threads Forge raised comes from the dispatch (raisedByForge, computed by Forge's API), never from
|
|
5
|
+
// the reviewer. Forge's API runs the same duplicate check again when it posts the review (PrReviewRepeatGuard).
|
|
6
|
+
/**
|
|
7
|
+
* The pull-request facts the pr_review completion tool holds a review to: the admitted head
|
|
8
|
+
* (prReview.headSha, never the live pull request head) and the prior review threads the
|
|
9
|
+
* dispatch lists, and the dispatched base commit the payload's base_sha must resolve to. The
|
|
10
|
+
* unresolved threads Forge raised give the anchors a new finding must not repeat, and every
|
|
11
|
+
* unresolved thread the prompt showed must be reported.
|
|
12
|
+
*/
|
|
13
|
+
export function prReviewContextFromDispatch(dispatch) {
|
|
14
|
+
const prReview = dispatch.prReview;
|
|
15
|
+
if (!prReview) {
|
|
16
|
+
return undefined;
|
|
17
|
+
}
|
|
18
|
+
const threadIds = prReview.priorThreads.map((thread) => thread.threadId);
|
|
19
|
+
const openThreads = openPrReviewThreadAnchors(prReview.priorThreads);
|
|
20
|
+
const requiredThreadIds = requiredPrReviewThreadIds(renderedPrReviewThreads(prReview).map((entry) => entry.thread));
|
|
21
|
+
return {
|
|
22
|
+
headSha: prReview.headSha.trim() || undefined,
|
|
23
|
+
baseSha: prReview.baseSha.trim() || undefined,
|
|
24
|
+
threadIds: threadIds.length > 0 ? threadIds : undefined,
|
|
25
|
+
...(openThreads.length > 0 ? { openThreads } : {}),
|
|
26
|
+
...(requiredThreadIds.length > 0 ? { requiredThreadIds } : {})
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
/** Repo-relative path in one spelling: forward slashes, no leading "./". */
|
|
30
|
+
export function normalizePrReviewPath(value) {
|
|
31
|
+
const path = value.trim().replace(/\\/gu, "/");
|
|
32
|
+
return path.startsWith("./") ? path.slice(2) : path;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* The unresolved, current threads on the new side of the diff that an earlier Forge review raised: a finding inside
|
|
36
|
+
* one's lines repeats it. A finding inside a thread anybody else raised is the reviewer's own and is posted.
|
|
37
|
+
*/
|
|
38
|
+
export function openPrReviewThreadAnchors(threads) {
|
|
39
|
+
const anchors = [];
|
|
40
|
+
for (const thread of threads) {
|
|
41
|
+
const line = thread.line ?? 0;
|
|
42
|
+
if (!thread.raisedByForge || thread.isResolved || thread.isOutdated || !thread.path || line < 1 || (thread.side ?? "").toUpperCase() === "LEFT") {
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
const startLine = thread.startLine && thread.startLine > 0 && thread.startLine <= line ? thread.startLine : line;
|
|
46
|
+
anchors.push({ threadId: thread.threadId, path: normalizePrReviewPath(thread.path), startLine, line });
|
|
47
|
+
}
|
|
48
|
+
return anchors;
|
|
49
|
+
}
|
|
50
|
+
/** The unresolved threads the reviewer was shown: each must be reported in prior_threads. */
|
|
51
|
+
export function requiredPrReviewThreadIds(shown) {
|
|
52
|
+
return shown.filter((thread) => !thread.isResolved).map((thread) => thread.threadId);
|
|
53
|
+
}
|
|
54
|
+
/** One `--pr-review-open-thread` value: the id, start line, end line and path, each URI-encoded, comma-separated. */
|
|
55
|
+
export function encodePrReviewOpenThread(anchor) {
|
|
56
|
+
return [anchor.threadId, String(anchor.startLine), String(anchor.line), anchor.path].map(encodeURIComponent).join(",");
|
|
57
|
+
}
|
|
58
|
+
export function decodePrReviewOpenThread(value) {
|
|
59
|
+
const parts = (value ?? "").split(",").map((part) => decodeURIComponent(part));
|
|
60
|
+
if (parts.length !== 4)
|
|
61
|
+
return null;
|
|
62
|
+
const [threadId, start, end, path] = parts;
|
|
63
|
+
const startLine = Number(start);
|
|
64
|
+
const line = Number(end);
|
|
65
|
+
return threadId && path && Number.isInteger(startLine) && Number.isInteger(line) && startLine >= 1 && startLine <= line
|
|
66
|
+
? { threadId, path, startLine, line }
|
|
67
|
+
: null;
|
|
68
|
+
}
|
|
69
|
+
/** Rejects a finding anchored inside the lines of an open thread an earlier Forge review raised: Forge already said it. */
|
|
70
|
+
export function validatePrReviewFindingAnchors(findings, anchors) {
|
|
71
|
+
if (!anchors || anchors.length === 0)
|
|
72
|
+
return;
|
|
73
|
+
for (const [index, finding] of findings.entries()) {
|
|
74
|
+
const record = finding;
|
|
75
|
+
const path = normalizePrReviewPath(typeof record.path === "string" ? record.path : "");
|
|
76
|
+
const line = record.line;
|
|
77
|
+
const thread = anchors.find((anchor) => anchor.path === path && line >= anchor.startLine && line <= anchor.line);
|
|
78
|
+
if (thread) {
|
|
79
|
+
const range = thread.startLine === thread.line ? `${thread.line}` : `${thread.startLine}-${thread.line}`;
|
|
80
|
+
throw new Error(`findings[${index}] at ${path}:${line} repeats the open review thread ${thread.threadId} (${thread.path}:${range}), which an earlier Forge review raised. Don't post a duplicate comment: drop the finding and list thread ${thread.threadId} in prior_threads as still_open.`);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
/** Requires every unresolved thread the reviewer was shown to be reported in prior_threads. */
|
|
85
|
+
export function validatePrReviewThreadCompleteness(reported, required) {
|
|
86
|
+
const missing = (required ?? []).filter((threadId) => !reported.has(threadId));
|
|
87
|
+
if (missing.length === 0)
|
|
88
|
+
return;
|
|
89
|
+
throw new Error(`prior_threads must report every unresolved thread in the dispatch; missing ${missing.slice(0, 10).join(", ")}${missing.length > 10 ? ", ..." : ""}. Report each as still_open if it still applies on this head, or resolved_since_last_review if this head fixes it.`);
|
|
90
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { PrReviewCompletionContext, PrReviewStatus } from "./phase-completion.js";
|
|
2
|
+
/** Per-finding limits; Forge's API applies the same ones (PrReviewArtifactValidator.MaximumFindingTextCharacters). */
|
|
3
|
+
export declare const prReviewMaximumFindingTextCharacters: number;
|
|
4
|
+
/**
|
|
5
|
+
* Shape and consistency rules for `forge.complete_pr_review`. Anything that needs the
|
|
6
|
+
* repository (the worktree head, the diff lines) is checked separately, after this passes.
|
|
7
|
+
*
|
|
8
|
+
* `stale_head` and `blocked` are terminal outcomes, not reviews: they need only enough
|
|
9
|
+
* evidence to explain themselves. A `reviewed` payload is held to the skill's full contract.
|
|
10
|
+
*/
|
|
11
|
+
export declare function validatePrReviewPayloadShape(payload: Record<string, unknown>, status: PrReviewStatus, context?: PrReviewCompletionContext | undefined): void;
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import { normalizedString, optionalString, requireArray, requireArrayRecord, requireEvidenceRecord, requireKnownStringRecord, requireNonEmptyStringArrayRecord, requireNonEmptyStringRecord, requireObject, requiredRecord } from "./phase-completion.helpers.js";
|
|
2
|
+
import { validatePrReviewFindingAnchors, validatePrReviewThreadCompleteness } from "./phase-completion.pr-review-threads.js";
|
|
3
|
+
const prReviewVerdicts = new Set(["changes_recommended", "needs_closer_look", "approval_recommended"]);
|
|
4
|
+
const prReviewEfforts = new Set(["lite", "standard", "deep"]);
|
|
5
|
+
const prReviewSeverities = new Set(["high", "medium", "low"]);
|
|
6
|
+
const prReviewCategories = new Set(["correctness", "security", "contract", "tests", "data", "docs"]);
|
|
7
|
+
const prReviewThreadStatuses = new Set(["resolved_since_last_review", "still_open", "reintroduced"]);
|
|
8
|
+
const prReviewShaPattern = /^[0-9a-f]{7,64}$/iu;
|
|
9
|
+
/** Per-finding limits; Forge's API applies the same ones (PrReviewArtifactValidator.MaximumFindingTextCharacters). */
|
|
10
|
+
export const prReviewMaximumFindingTextCharacters = 8 * 1024;
|
|
11
|
+
/**
|
|
12
|
+
* Shape and consistency rules for `forge.complete_pr_review`. Anything that needs the
|
|
13
|
+
* repository (the worktree head, the diff lines) is checked separately, after this passes.
|
|
14
|
+
*
|
|
15
|
+
* `stale_head` and `blocked` are terminal outcomes, not reviews: they need only enough
|
|
16
|
+
* evidence to explain themselves. A `reviewed` payload is held to the skill's full contract.
|
|
17
|
+
*/
|
|
18
|
+
export function validatePrReviewPayloadShape(payload, status, context = undefined) {
|
|
19
|
+
if (status === "blocked") {
|
|
20
|
+
validateBlockedPrReview(payload);
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
const headSha = requirePrReviewHeadSha(payload);
|
|
24
|
+
const expectedHead = context?.headSha?.trim().toLowerCase();
|
|
25
|
+
if (status === "stale_head") {
|
|
26
|
+
if (expectedHead && headSha.toLowerCase() === expectedHead) {
|
|
27
|
+
throw new Error(`head_sha ${headSha} is the dispatched head, so the worktree is not stale. Review that commit and complete with status "reviewed", or set head_sha to the different commit you found.`);
|
|
28
|
+
}
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
if (expectedHead && headSha.toLowerCase() !== expectedHead) {
|
|
32
|
+
throw new Error(`head_sha ${headSha} is not the head Forge dispatched (${context.headSha}). A review is bound to the dispatched head: review that commit, or complete with status "stale_head" and head_sha set to the commit you found if the worktree is on a different one.`);
|
|
33
|
+
}
|
|
34
|
+
validateReviewedPrReviewHeader(payload);
|
|
35
|
+
validatePrReviewChanges(payload);
|
|
36
|
+
validatePrReviewRecords(payload, "files", ["path", "description"]);
|
|
37
|
+
validatePrReviewRecords(payload, "skipped_files", ["path", "reason"], false);
|
|
38
|
+
validatePrReviewRecords(payload, "tests_checked", ["test", "assertion", "produced_at"], false);
|
|
39
|
+
requireArray(payload, "findings");
|
|
40
|
+
const findings = payload.findings;
|
|
41
|
+
for (const [index, finding] of findings.entries()) {
|
|
42
|
+
validatePrReviewFinding(finding, `findings[${index}]`);
|
|
43
|
+
}
|
|
44
|
+
validatePrReviewFindingAnchors(findings, context?.openThreads);
|
|
45
|
+
const openThreads = validatePrReviewPriorThreads(payload, context);
|
|
46
|
+
validatePrReviewVerdictConsistency(payload, findings.length, openThreads);
|
|
47
|
+
validatePrReviewSecurity(payload, findings);
|
|
48
|
+
validatePrReviewTestsChecked(payload, findings);
|
|
49
|
+
}
|
|
50
|
+
function validateBlockedPrReview(payload) {
|
|
51
|
+
const hasReason = ["reason", "summary", "verdict_reason"].some((key) => optionalString(payload, key))
|
|
52
|
+
|| (Array.isArray(payload.blockers) && payload.blockers.length > 0);
|
|
53
|
+
if (!hasReason) {
|
|
54
|
+
throw new Error("A blocked pr_review needs a reason: set reason (or summary) to say why the diff could not be read.");
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
function requirePrReviewHeadSha(payload) {
|
|
58
|
+
requireNonEmptyStringRecord(payload, "head_sha", "");
|
|
59
|
+
const headSha = optionalString(payload, "head_sha").trim();
|
|
60
|
+
if (!prReviewShaPattern.test(headSha)) {
|
|
61
|
+
throw new Error("head_sha must be a commit sha (7-64 hex characters); copy it from `git rev-parse HEAD`.");
|
|
62
|
+
}
|
|
63
|
+
return headSha;
|
|
64
|
+
}
|
|
65
|
+
function validateReviewedPrReviewHeader(payload) {
|
|
66
|
+
requireNonEmptyStringRecord(payload, "base_sha", "");
|
|
67
|
+
if (!prReviewShaPattern.test(optionalString(payload, "base_sha").trim())) {
|
|
68
|
+
throw new Error("base_sha must be a commit sha (7-64 hex characters).");
|
|
69
|
+
}
|
|
70
|
+
requireKnownStringRecord(payload, "review_effort", prReviewEfforts);
|
|
71
|
+
requireKnownStringRecord(payload, "verdict", prReviewVerdicts);
|
|
72
|
+
requireNonEmptyStringRecord(payload, "verdict_reason", "");
|
|
73
|
+
requireNonEmptyStringRecord(payload, "summary", "");
|
|
74
|
+
}
|
|
75
|
+
function validatePrReviewChanges(payload) {
|
|
76
|
+
requireArray(payload, "changes");
|
|
77
|
+
for (const [index, change] of payload.changes.entries()) {
|
|
78
|
+
if (typeof change !== "string" || change.trim().length === 0) {
|
|
79
|
+
throw new Error(`changes[${index}] must be a non-empty string.`);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/** Validates prior_threads and returns how many are still_open or reintroduced. */
|
|
84
|
+
function validatePrReviewPriorThreads(payload, context) {
|
|
85
|
+
const priorThreads = payload.prior_threads === undefined || payload.prior_threads === null ? [] : payload.prior_threads;
|
|
86
|
+
if (!Array.isArray(priorThreads)) {
|
|
87
|
+
throw new Error("prior_threads must be an array.");
|
|
88
|
+
}
|
|
89
|
+
const knownThreadIds = context?.threadIds && context.threadIds.length > 0 ? new Set(context.threadIds) : null;
|
|
90
|
+
const seenThreads = new Set();
|
|
91
|
+
let openThreads = 0;
|
|
92
|
+
for (const [index, thread] of priorThreads.entries()) {
|
|
93
|
+
requireEvidenceRecord(thread, `prior_threads[${index}]`);
|
|
94
|
+
const record = thread;
|
|
95
|
+
requireNonEmptyStringRecord(record, "thread_id", `prior_threads[${index}].`);
|
|
96
|
+
requireKnownStringRecord(record, "status", prReviewThreadStatuses, `prior_threads[${index}].`);
|
|
97
|
+
const threadId = optionalString(record, "thread_id");
|
|
98
|
+
if (knownThreadIds && !knownThreadIds.has(threadId)) {
|
|
99
|
+
throw new Error(`prior_threads[${index}].thread_id ${threadId} is not a thread in the dispatch. Use only thread ids from the dispatch's prior_threads (${[...knownThreadIds].slice(0, 10).join(", ")}${knownThreadIds.size > 10 ? ", ..." : ""}) and drop the entry if the thread is not one of them.`);
|
|
100
|
+
}
|
|
101
|
+
if (seenThreads.has(threadId)) {
|
|
102
|
+
throw new Error(`prior_threads[${index}] repeats thread ${threadId}; report each thread once.`);
|
|
103
|
+
}
|
|
104
|
+
seenThreads.add(threadId);
|
|
105
|
+
const threadStatus = normalizedString(optionalString(record, "status"));
|
|
106
|
+
if (threadStatus === "still_open" || threadStatus === "reintroduced")
|
|
107
|
+
openThreads += 1;
|
|
108
|
+
}
|
|
109
|
+
validatePrReviewThreadCompleteness(seenThreads, context?.requiredThreadIds);
|
|
110
|
+
return openThreads;
|
|
111
|
+
}
|
|
112
|
+
function validatePrReviewVerdictConsistency(payload, findingCount, openThreads) {
|
|
113
|
+
const openIssues = findingCount + openThreads;
|
|
114
|
+
const verdict = normalizedString(optionalString(payload, "verdict"));
|
|
115
|
+
if (openIssues > 0 && verdict !== "changes_recommended") {
|
|
116
|
+
throw new Error(`verdict ${verdict} contradicts ${openIssues} open issue(s) (${findingCount} new finding(s), ${openThreads} still_open or reintroduced prior thread(s)). Set verdict to changes_recommended, or remove findings you cannot prove.`);
|
|
117
|
+
}
|
|
118
|
+
if (openIssues === 0 && verdict === "changes_recommended") {
|
|
119
|
+
throw new Error("verdict changes_recommended needs at least one open issue: a finding, or a prior thread that is still_open or reintroduced. Use approval_recommended, or needs_closer_look when you could not verify a risky surface.");
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
function hasFindingCategory(findings, category) {
|
|
123
|
+
return findings.some((finding) => normalizedString(finding.category) === category);
|
|
124
|
+
}
|
|
125
|
+
function validatePrReviewSecurity(payload, findings) {
|
|
126
|
+
requireObject(payload, "security_review");
|
|
127
|
+
const security = requiredRecord(payload, "security_review");
|
|
128
|
+
requireKnownStringRecord(security, "status", new Set(["findings", "no_findings"]), "security_review.");
|
|
129
|
+
requireNonEmptyStringArrayRecord(security, "checked", "security_review.");
|
|
130
|
+
requireNonEmptyStringRecord(security, "notes", "security_review.");
|
|
131
|
+
requireArrayRecord(security, "keys_checked", "security_review.");
|
|
132
|
+
const hasSecurityFinding = hasFindingCategory(findings, "security");
|
|
133
|
+
const securityStatus = normalizedString(optionalString(security, "status"));
|
|
134
|
+
if (hasSecurityFinding && securityStatus !== "findings") {
|
|
135
|
+
throw new Error('security_review.status must be "findings" when any finding has category "security".');
|
|
136
|
+
}
|
|
137
|
+
if (!hasSecurityFinding && securityStatus === "findings") {
|
|
138
|
+
throw new Error('security_review.status is "findings" but no finding has category "security". Add the finding or set status to "no_findings".');
|
|
139
|
+
}
|
|
140
|
+
for (const [index, key] of security.keys_checked.entries()) {
|
|
141
|
+
requireEvidenceRecord(key, `security_review.keys_checked[${index}]`);
|
|
142
|
+
const record = key;
|
|
143
|
+
requireNonEmptyStringRecord(record, "key", `security_review.keys_checked[${index}].`);
|
|
144
|
+
if (typeof record.per_user_data !== "boolean" || typeof record.includes_user !== "boolean") {
|
|
145
|
+
throw new Error(`security_review.keys_checked[${index}] needs boolean per_user_data and includes_user.`);
|
|
146
|
+
}
|
|
147
|
+
if (record.per_user_data === true && record.includes_user === false && !hasSecurityFinding) {
|
|
148
|
+
throw new Error(`security_review.keys_checked[${index}] holds per-user data under a key without the user, which must have a matching security finding. Add the finding.`);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
function validatePrReviewTestsChecked(payload, findings) {
|
|
153
|
+
const hasTestsFinding = hasFindingCategory(findings, "tests");
|
|
154
|
+
for (const [index, entry] of (payload.tests_checked ?? []).entries()) {
|
|
155
|
+
const produced = normalizedString(optionalString(entry, "produced_at")).replace(/_/gu, " ");
|
|
156
|
+
if (produced === "not produced" && !hasTestsFinding) {
|
|
157
|
+
throw new Error(`tests_checked[${index}] was not produced by production code, so the test cannot pass; it must have a matching finding with category "tests". Add the finding.`);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
function validatePrReviewRecords(payload, key, fields, required = true) {
|
|
162
|
+
if ((payload[key] === undefined || payload[key] === null) && !required)
|
|
163
|
+
return;
|
|
164
|
+
requireArray(payload, key);
|
|
165
|
+
for (const [index, entry] of payload[key].entries()) {
|
|
166
|
+
requireEvidenceRecord(entry, `${key}[${index}]`);
|
|
167
|
+
for (const field of fields) {
|
|
168
|
+
requireNonEmptyStringRecord(entry, field, `${key}[${index}].`);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
function validatePrReviewFinding(finding, label) {
|
|
173
|
+
requireEvidenceRecord(finding, label);
|
|
174
|
+
const record = finding;
|
|
175
|
+
for (const key of ["path", "title", "body"]) {
|
|
176
|
+
requireNonEmptyStringRecord(record, key, `${label}.`);
|
|
177
|
+
}
|
|
178
|
+
if (!Number.isInteger(record.line) || record.line < 1) {
|
|
179
|
+
throw new Error(`${label}.line must be a positive integer line number on the new side of the diff.`);
|
|
180
|
+
}
|
|
181
|
+
if (record.start_line !== undefined && record.start_line !== null) {
|
|
182
|
+
if (!Number.isInteger(record.start_line) || record.start_line < 1 || record.start_line >= record.line) {
|
|
183
|
+
throw new Error(`${label}.start_line must be null (single-line comment) or a positive integer strictly less than line.`);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
if (record.side !== "RIGHT") {
|
|
187
|
+
throw new Error(`${label}.side must be "RIGHT": inline comments anchor on the new side of the diff. Re-anchor the comment at the changed line on the new side, and name any unchanged location in the body as path:line.`);
|
|
188
|
+
}
|
|
189
|
+
requireKnownStringRecord(record, "severity", prReviewSeverities, `${label}.`);
|
|
190
|
+
requireKnownStringRecord(record, "category", prReviewCategories, `${label}.`);
|
|
191
|
+
if (record.also_at !== undefined && record.also_at !== null) {
|
|
192
|
+
if (!Array.isArray(record.also_at))
|
|
193
|
+
throw new Error(`${label}.also_at must be an array.`);
|
|
194
|
+
for (const [index, place] of record.also_at.entries()) {
|
|
195
|
+
requireEvidenceRecord(place, `${label}.also_at[${index}]`);
|
|
196
|
+
requireNonEmptyStringRecord(place, "path", `${label}.also_at[${index}].`);
|
|
197
|
+
if (!Number.isInteger(place.line) || place.line < 1) {
|
|
198
|
+
throw new Error(`${label}.also_at[${index}].line must be a positive integer.`);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
if (record.suggestion !== undefined && record.suggestion !== null && typeof record.suggestion !== "string") {
|
|
203
|
+
throw new Error(`${label}.suggestion must be null or a string.`);
|
|
204
|
+
}
|
|
205
|
+
requireFindingTextWithinCap(record, label);
|
|
206
|
+
}
|
|
207
|
+
/** Each finding's body and suggestion becomes one inline comment, which the review body's cap does not cover. */
|
|
208
|
+
function requireFindingTextWithinCap(record, label) {
|
|
209
|
+
for (const key of ["body", "suggestion"]) {
|
|
210
|
+
const value = record[key];
|
|
211
|
+
if (typeof value === "string" && value.length > prReviewMaximumFindingTextCharacters) {
|
|
212
|
+
throw new Error(`${label}.${key} is ${value.length} characters; an inline comment's ${key} may be at most ${prReviewMaximumFindingTextCharacters}. Shorten it to the input, the path and the wrong result.`);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { type ProcessRunner } from "../shared/process/process.js";
|
|
2
|
+
import type { PrReviewCompletionContext } from "./phase-completion.js";
|
|
3
|
+
/** Inclusive new-side line range that sits inside one hunk of the pull request diff. */
|
|
4
|
+
export interface DiffLineRange {
|
|
5
|
+
readonly start: number;
|
|
6
|
+
readonly end: number;
|
|
7
|
+
}
|
|
8
|
+
/** New-side (RIGHT) lines GitHub accepts inline comments on, per changed path. */
|
|
9
|
+
export type NewSideDiffLines = ReadonlyMap<string, readonly DiffLineRange[]>;
|
|
10
|
+
/**
|
|
11
|
+
* Parses `git diff --unified=3` output into the new-side line ranges of every hunk.
|
|
12
|
+
* Added and context lines both count: GitHub lets a review comment land on either.
|
|
13
|
+
* Hunk header counts drive the walk, so a content line that starts with `+++` is not mistaken for a file header.
|
|
14
|
+
*/
|
|
15
|
+
export declare function parseNewSideDiffLines(diff: string): NewSideDiffLines;
|
|
16
|
+
export declare function isLineInRanges(ranges: readonly DiffLineRange[] | undefined, line: number): boolean;
|
|
17
|
+
/** Messages for every finding whose anchor GitHub would reject, each with how to re-anchor. */
|
|
18
|
+
export declare function offDiffAnchorProblems(findings: readonly Record<string, unknown>[], diffLines: NewSideDiffLines, range: string): string[];
|
|
19
|
+
export declare function normalizeFindingPath(value: string): string;
|
|
20
|
+
/**
|
|
21
|
+
* Checks what a `reviewed` payload claims against the worktree: the review must be bound to the
|
|
22
|
+
* commit that is checked out (and to the dispatched head when Forge supplied one), `base_sha`
|
|
23
|
+
* must be a real commit, and every inline comment must land on a line of the PR diff.
|
|
24
|
+
* Throws an error that tells the agent how to repair the payload.
|
|
25
|
+
*/
|
|
26
|
+
export declare function assertPrReviewMatchesRepository(worktreePath: string, payload: Record<string, unknown>, context: PrReviewCompletionContext | undefined, processRunner?: ProcessRunner): Promise<void>;
|
|
27
|
+
/**
|
|
28
|
+
* Re-checks the recorded pr-review.result.json as if it had just been submitted. The completion tool
|
|
29
|
+
* validates what it writes, but the session can also write that file itself (the post-run worktree
|
|
30
|
+
* check skips Forge's artifact directory), so the file Forge hands on must pass the same shape,
|
|
31
|
+
* head, base and diff-anchor rules, name this work item and phase, and carry the content hash the tool
|
|
32
|
+
* computes. Returns the reason it is rejected, or null.
|
|
33
|
+
*/
|
|
34
|
+
export declare function recordedPrReviewArtifactProblem(worktreePath: string, workItemId: string, verdictJson: string, context: PrReviewCompletionContext | undefined, processRunner?: ProcessRunner): Promise<string | null>;
|
|
35
|
+
/** Renders the pr-review.report.md that sits next to pr-review.result.json. */
|
|
36
|
+
export declare function prReviewReportMarkdown(artifact: Record<string, unknown>, contentHash: string): string;
|