Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

fix: handle review relay redirects and explicit reruns

Tina6ea05715d5e
diff --git a/scripts/ui-review-bot/README.md b/scripts/ui-review-bot/README.md
index 8092437b2..6fcafcbaa 100644
--- a/scripts/ui-review-bot/README.md
+++ b/scripts/ui-review-bot/README.md
@@ -73,7 +73,7 @@ The code reviewer uses `deepseek/deepseek-v4-flash` through OpenRouter. It reads
Deployment requires `code-review.yml`, the updated Worker/schema, Worker secrets `OPENROUTER_API_KEY` and `REVIEW_PROXY_TOKEN`, Actions secret `PEEKAREVIEW_PROXY_TOKEN` with the same 64-character hexadecimal token, and Actions variable `PEEKAREVIEW_MODEL_URL` pointing to the Worker's `/review-model` endpoint. The provider key stays in the Worker for code reviews. The existing screenshot workflow keeps its separate provider-key configuration.
-The relay requires an initialized `review_budget` row with `id = 1`, `ceiling = 850000`, and `initial_spend` equal to the already committed trial spending in integer microdollars. It refuses inference without initialization. The $0.85 trial ceiling includes prior development spend and a 15% allowance; it never resets automatically. Requests reserve maximum cost atomically before inference, and unknown charges remain reserved. Successful identical requests are cached; unresolved requests are not automatically retried. A local review also has a $0.10 limit and a 12-minute deadline.
+The relay requires an initialized `review_budget` row with `id = 1`, `ceiling = 850000`, and `initial_spend` equal to the already committed trial spending in integer microdollars. It refuses inference without initialization. The $0.85 trial ceiling includes prior development spend and a 15% allowance; it never resets automatically. Requests reserve maximum cost atomically before inference, and unknown charges remain reserved. Successful identical requests are cached; unresolved requests are not automatically retried within a run. An explicitly requested new run can try again while retaining the earlier reservation. A local review also has a $0.10 limit and a 12-minute deadline.
For a private local review:
diff --git a/scripts/ui-review-bot/code-review/cli.mjs b/scripts/ui-review-bot/code-review/cli.mjs
index 97b2e77b7..eab6ac313 100644
--- a/scripts/ui-review-bot/code-review/cli.mjs
+++ b/scripts/ui-review-bot/code-review/cli.mjs
@@ -109,6 +109,8 @@ async function main() {
JSON.stringify(
{
report: path.join(folder, "review.md"),
+ status: report.status,
+ failure: report.failure || null,
findings: report.findings.length,
cost: report.cost,
budget: await budget.status(),
diff --git a/scripts/ui-review-bot/code-review/model.mjs b/scripts/ui-review-bot/code-review/model.mjs
index e53e0a7ea..12325917b 100644
--- a/scripts/ui-review-bot/code-review/model.mjs
+++ b/scripts/ui-review-bot/code-review/model.mjs
@@ -1,4 +1,4 @@
-import { createHash } from "node:crypto";
+import { createHash, randomUUID } from "node:crypto";
import { mkdir, readFile, writeFile } from "node:fs/promises";
import path from "node:path";
import { parseJSON } from "../core.mjs";
@@ -29,6 +29,7 @@ export class ReviewerModel {
deadline,
});
this.calls = [];
+ this.attempt = randomUUID();
}
async ask(system, input, label) {
@@ -86,6 +87,7 @@ export class ReviewerModel {
headers: {
Authorization: `Bearer ${this.key}`,
"Content-Type": "application/json",
+ "X-Peekareview-Attempt": this.attempt,
"X-OpenRouter-Title": "Peekareq code review beta",
},
body: JSON.stringify(body),
diff --git a/scripts/ui-review-bot/tests/review-budget.test.mjs b/scripts/ui-review-bot/tests/review-budget.test.mjs
index a6f2d711a..8fb94c6aa 100644
--- a/scripts/ui-review-bot/tests/review-budget.test.mjs
+++ b/scripts/ui-review-bot/tests/review-budget.test.mjs
@@ -46,10 +46,17 @@ async function setup(t, ceiling = 850000) {
};
}
-function request(text = "review", token = "a".repeat(64)) {
+function request(
+ text = "review",
+ token = "a".repeat(64),
+ attempt = "11111111-1111-4111-8111-111111111111",
+) {
return new Request("https://example.test/review-model", {
method: "POST",
- headers: { Authorization: `Bearer ${token}` },
+ headers: {
+ Authorization: `Bearer ${token}`,
+ "X-Peekareview-Attempt": attempt,
+ },
body: JSON.stringify({
model: MODEL,
response_format: responseFormat("investigate"),
@@ -77,6 +84,7 @@ test("shared SQL ledger reserves before inference and concurrent identical reque
let calls = 0;
const provider = async (_url, options) => {
calls++;
+ assert.equal(options.redirect, "manual");
assert.equal(
db.prepare("SELECT status FROM review_requests").get().status,
"reserved",
@@ -168,7 +176,10 @@ test("proxy rejects unknown stages and replaces client schemas with trusted defi
const make = (input) =>
new Request("https://example.test/review-model", {
method: "POST",
- headers: { Authorization: `Bearer ${"a".repeat(64)}` },
+ headers: {
+ Authorization: `Bearer ${"a".repeat(64)}`,
+ "X-Peekareview-Attempt": "11111111-1111-4111-8111-111111111111",
+ },
body: JSON.stringify(input),
});
body.response_format.json_schema.name = "arbitrary";
@@ -217,3 +228,41 @@ test("schema-invalid replies are charged but cannot replay as successful cached
assert.equal((await reviewModel(request(), env, provider)).status, 409);
assert.equal(calls, 1);
});
+
+test("a new review can retry an unresolved request while retaining its earlier reservation", async (t) => {
+ const { db, env } = await setup(t);
+ const first = await reviewModel(request(), env, async () => {
+ throw Error("timeout");
+ });
+ assert.equal(first.status, 502);
+ const reserved = db
+ .prepare("SELECT charged FROM review_requests")
+ .get().charged;
+ assert.equal(
+ (
+ await reviewModel(request(), env, async () => {
+ throw Error("No automatic replay");
+ })
+ ).status,
+ 409,
+ );
+ const second = await reviewModel(
+ request("review", "a".repeat(64), "22222222-2222-4222-8222-222222222222"),
+ env,
+ async () => reply(),
+ );
+ assert.equal(second.status, 200);
+ const total = db
+ .prepare("SELECT SUM(charged) AS total FROM review_requests")
+ .get().total;
+ assert.equal(total, reserved + 1150);
+ const cached = await reviewModel(
+ request("review", "a".repeat(64), "33333333-3333-4333-8333-333333333333"),
+ env,
+ async () => {
+ throw Error("Completed response should be shared");
+ },
+ );
+ assert.equal(cached.status, 200);
+ assert.equal((await cached.json()).usage.cost, 0);
+});
diff --git a/scripts/ui-review-bot/worker/review-model.mjs b/scripts/ui-review-bot/worker/review-model.mjs
index b2e947c35..d910faa0c 100644
--- a/scripts/ui-review-bot/worker/review-model.mjs
+++ b/scripts/ui-review-bot/worker/review-model.mjs
@@ -39,6 +39,9 @@ export async function reviewModel(request, env, fetcher = fetch) {
if (request.method !== "POST") return fail(405, "Method not allowed");
if (!env.OPENROUTER_API_KEY)
return fail(503, "Review provider is not configured");
+ const attempt = request.headers.get("x-peekareview-attempt");
+ if (!/^[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/.test(attempt || ""))
+ return fail(400, "Invalid review attempt");
let input;
let reserved;
let format;
@@ -71,14 +74,21 @@ export async function reviewModel(request, env, fetcher = fetch) {
},
};
const serialized = JSON.stringify(body);
- const hash = Buffer.from(
+ const contentHash = Buffer.from(
await crypto.subtle.digest("SHA-256", encoder.encode(serialized)),
).toString("hex");
- const cached = await env.DB.prepare(
- "SELECT status, response FROM review_requests WHERE hash = ?",
- )
- .bind(hash)
- .first();
+ const hash = `${contentHash}.${attempt}`;
+ const cached =
+ (await env.DB.prepare(
+ "SELECT status, response FROM review_requests WHERE status = 'complete' AND (hash = ? OR hash GLOB ?) LIMIT 1",
+ )
+ .bind(contentHash, `${contentHash}.*`)
+ .first()) ||
+ (await env.DB.prepare(
+ "SELECT status, response FROM review_requests WHERE hash = ?",
+ )
+ .bind(hash)
+ .first());
if (cached) {
if (cached.status !== "complete")
return fail(409, "Prior request unresolved; no automatic retry");
@@ -105,7 +115,7 @@ export async function reviewModel(request, env, fetcher = fetch) {
"https://openrouter.ai/api/v1/chat/completions",
{
method: "POST",
- redirect: "error",
+ redirect: "manual",
signal: AbortSignal.timeout(180_000),
headers: {
Authorization: `Bearer ${env.OPENROUTER_API_KEY}`,