Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 72 additions & 2 deletions src/app/api/autoskills/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ describe("/api/autoskills", () => {
expect(JSON.parse(init.body)).toEqual({
scope: "global",
expected_updated_at: null,
expected_content_digest: null,
});
expect(init.headers["X-EvoScientist-Local-Secret"]).toBe(SECRET);
});
Expand All @@ -91,6 +92,7 @@ describe("/api/autoskills", () => {
expect(JSON.parse(fetchMock.mock.calls[0][1].body)).toEqual({
scope,
expected_updated_at: null,
expected_content_digest: null,
});
});

Expand Down Expand Up @@ -119,12 +121,43 @@ describe("/api/autoskills", () => {
expect(JSON.parse(fetchMock.mock.calls[0][1].body)).toEqual({
scope: "global",
expected_updated_at: expected,
expected_content_digest: null,
});
}
);

// The digest of the text the page showed: the core refuses a decision
// that vouches for content it did not hand out. Null checks nothing.
it.each([
[{}, null],
[{ expected_content_digest: null }, null],
[{ expected_content_digest: "a".repeat(64) }, "a".repeat(64)],
[{ expected_content_digest: "0123456789abcdef" }, "0123456789abcdef"],
])(
"approves with expected_content_digest %j forwarded as %j",
async (extra, digest) => {
fetchMock.mockResolvedValue(answer(200, {}));

await POST(
request({
action: "approve",
proposal_id: "a-b",
scope: "workspace",
...extra,
})
);

expect(JSON.parse(fetchMock.mock.calls[0][1].body)).toEqual({
scope: "workspace",
expected_updated_at: null,
expected_content_digest: digest,
});
}
);

// Reject carries the version too (the core answers 409 draft_changed for
// a draft rewritten since), never a scope.
// a draft rewritten since), never a scope or a digest: it installs
// nothing, and the core checks a digest only where it installs.
it.each([
[{}, null],
[{ expected_updated_at: null }, null],
Expand All @@ -137,8 +170,20 @@ describe("/api/autoskills", () => {
{ expected_updated_at: "2026-11-06T09:00:00Z", scope: "global" },
"2026-11-06T09:00:00Z",
],
[
{
expected_updated_at: "2026-11-06T09:00:00Z",
expected_content_digest: "f".repeat(64),
},
"2026-11-06T09:00:00Z",
],
// A reject reads no digest, so a malformed one is dropped rather than
// refused.
[{ expected_content_digest: 1 }, null],
[{ expected_content_digest: "x".repeat(65) }, null],
[{ expected_content_digest: "abc\u0007" }, null],
])(
"rejects with expected_updated_at %j forwarded as %j, and no scope",
"rejects with expected_updated_at %j forwarded as %j, and nothing else",
async (extra, expected) => {
fetchMock.mockResolvedValue(answer(200, {}));

Expand Down Expand Up @@ -166,6 +211,7 @@ describe("/api/autoskills", () => {
expected_updated_at: "2026-11-06T09:00:00Z",
scope: "global",
status: "pending",
expected_content_digest: "a".repeat(64),
},
"2026-11-06T09:00:00Z",
],
Expand Down Expand Up @@ -292,6 +338,30 @@ describe("/api/autoskills", () => {
expected_updated_at: "2026-11-06\n",
},
],
[
"a numeric expected_content_digest",
{ action: "approve", proposal_id: "a-b", expected_content_digest: 1 },
],
[
"an expected_content_digest that is an object",
{ action: "approve", proposal_id: "a-b", expected_content_digest: {} },
],
[
"an expected_content_digest over 64 characters",
{
action: "approve",
proposal_id: "a-b",
expected_content_digest: "x".repeat(65),
},
],
[
"an expected_content_digest with a control character",
{
action: "approve",
proposal_id: "a-b",
expected_content_digest: "abc\u0007",
},
],
["a run without a folder", { action: "run" }],
["a run with a blank folder", { action: "run", workspace_dir: " " }],
[
Expand Down
37 changes: 33 additions & 4 deletions src/app/api/autoskills/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ export const dynamic = "force-dynamic";

/** The longest folder path taken. */
const MAX_PATH = 4096;
/** The longest `expected_updated_at` taken (a draft's ISO timestamp). */
/** The longest `expected_updated_at` taken (a draft's ISO timestamp) and
* the longest `expected_content_digest` (the core's digest of the text it
* handed out). The digest's shape is the core's business, not this
* relay's: it checks only that the value is a short plain string, with
* no control characters in it. */
const MAX_STAMP = 64;
Comment thread
NkAntony777 marked this conversation as resolved.
Resolved

function hasControl(text: string): boolean {
Expand Down Expand Up @@ -64,15 +68,40 @@ function planOf(raw: unknown): Plan | null {
) {
return null;
}
// A reject and a revert install nothing, so they never carry a scope.
if (action === "reject" || action === "revert") {
// A revert installs nothing, so it keeps the version alone.
if (action === "revert") {
return { path, body: { expected_updated_at: expected } };
}
// A reject installs nothing either: the version it read is all it checks,
// and the core takes no digest for one.
if (action === "reject") {
return { path, body: { expected_updated_at: expected } };
}
// The digest of the draft's files as the page read the list: the core
// refuses an approve that vouches for other files. Null is not nothing —
// with no digest the core checks the approve against the digest recorded
// at submission.
const digest = body.expected_content_digest ?? null;
if (
digest !== null &&
(typeof digest !== "string" ||
digest.length > MAX_STAMP ||
hasControl(digest))
) {
return null;
}
Comment thread
NkAntony777 marked this conversation as resolved.
Resolved
const scope = body.scope ?? null;
if (scope !== null && scope !== "workspace" && scope !== "global") {
return null;
}
return { path, body: { scope, expected_updated_at: expected } };
return {
path,
body: {
scope,
expected_updated_at: expected,
expected_content_digest: digest,
},
};
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/app/components/SkillsMarketplace.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ function approved(over: Partial<Draft> & { proposal_id: string }): Draft {
skill_md_truncated: false,
rationale_md: "",
rationale_md_truncated: false,
content_digest: null,
files: [],
install_target: null,
install_base: null,
Expand Down
57 changes: 57 additions & 0 deletions src/app/components/autoskills/DraftDetail.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ function draft(over: Partial<Draft> & { proposal_id: string }): Draft {
skill_md_truncated: false,
rationale_md: "",
rationale_md_truncated: false,
content_digest: null,
files: [],
install_target: null,
install_base: null,
Expand Down Expand Up @@ -1526,6 +1527,7 @@ describe("DraftDetail", () => {
tab("Why this skill").id
);
});

it("files compare with the installed copy only for the recorded place", () => {
const note =
"Changes against the installed copy are shown for the recorded place only.";
Expand Down Expand Up @@ -1613,6 +1615,61 @@ describe("DraftDetail", () => {
expect(scopeTrigger().textContent).toBe("All projects (global)");
});

it("a digest change of the same version takes the question away and keeps the place picked", () => {
const a = draft({
proposal_id: "a",
scope: "workspace",
operation: "update",
target_skill_name: "a",
content_digest: "d-1",
});
const view = render(detail(a));
pickScope("All projects (global)");
fireEvent.click(screen.getByRole("button", { name: "Approve" }));
expect(screen.getByRole("dialog")).toBeTruthy();

// An agent wrote a file in place: the same updated_at, another digest,
// so the question asks about text the draft no longer has.
const rewritten = { ...a, content_digest: "d-2" };
view.rerender(detail(rewritten));
expect(screen.queryByRole("dialog")).toBeNull();
// The place picked stays: the version did not change, only its text.
expect(scopeTrigger().textContent).toBe("All projects (global)");
// The next click is the new version's, and it sends that digest.
fireEvent.click(screen.getByRole("button", { name: "Approve" }));
fireEvent.click(
within(screen.getByRole("dialog")).getByRole("button", {
name: "Install",
})
);
expect(onApprove).toHaveBeenCalledWith(rewritten, "global");
});

it("a click whose digest changed while its files were checked installs nothing", async () => {
const errors = vi.spyOn(toast, "error");
try {
const answers: Array<(files: UnshowableFile[] | null) => void> = [];
checkFiles.mockImplementation(
() => new Promise<UnshowableFile[] | null>((r) => answers.push(r))
);
const d = draft({ proposal_id: "x", content_digest: "d-1" });
const view = render(detail(d));
fireEvent.click(screen.getByRole("button", { name: "Approve" }));
// Rewritten in place while the check read its files: the text the
// click was made on is not the draft's any more.
view.rerender(detail({ ...d, content_digest: "d-2" }));
await act(async () => answers[0]([]));
expect(onApprove).not.toHaveBeenCalled();
expect(errors.mock.calls).toEqual([
[
"Nothing was installed: the drafts changed while their files were checked. Approve again.",
],
]);
} finally {
errors.mockRestore();
}
});

it("the question's Replace and install waits while it can't decide", () => {
const d = draft({
proposal_id: "u",
Expand Down
38 changes: 24 additions & 14 deletions src/app/components/autoskills/DraftDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,8 @@ function approveQuestion(

const NO_FILES: readonly UnshowableFile[] = [];

/** An approval as clicked: the version of the draft shown, and the place,
* in the pane's visit of that draft. */
/** An approval as clicked: the version of the draft shown (its updated_at
* and content_digest), and the place, in the pane's visit of that draft. */
interface Decision {
version: string;
visit: number;
Expand All @@ -328,27 +328,37 @@ export function DraftDetail({
// Whether the page and the backend share this machine.
const here = useProjects().files === "here";

// This version of the draft: a run that rewrites a pending draft in place
// keeps its id and gives it a new updated_at (and maybe another scope).
const version = JSON.stringify([draft.proposal_id, draft.updated_at]);
// The version under the pane: a run that rewrites a pending draft in place
// keeps its id and gives it a new updated_at (and maybe another scope),
// and an agent's plain file write keeps even its updated_at while its
// content_digest changes. The place picked follows the updated_at; the
// question and the decision follow the digest too, since that is the
// content the pane reads and shows.
const stamp = JSON.stringify([draft.proposal_id, draft.updated_at]);
const version = JSON.stringify([
draft.proposal_id,
draft.updated_at,
draft.content_digest,
]);
// Where to install: each draft opened, and each rewrite, starts at the
// scope it records.
// scope it records. A new digest of the same stamp keeps the place picked:
// only the draft's text changed.
const [pick, setPick] = useState<{ version: string; scope: DraftScope }>({
version,
version: stamp,
scope: draft.scope,
});
// The question before an approval (an update, or files the page can't
// show), for this version only, with the decision fixed at the click (the
// draft and the place shown then) and the files its check found: it
// doesn't come back when the draft is opened again, and a rewrite takes it
// away.
// doesn't come back when the draft is opened again, and a rewrite of any
// kind takes it away.
const [asked, setAsked] = useState<
(Decision & { files: readonly UnshowableFile[] }) | null
>(null);
if (pick.version !== version) {
setPick({ version, scope: draft.scope });
setAsked(null);
if (pick.version !== stamp) {
setPick({ version: stamp, scope: draft.scope });
}
if (asked && asked.version !== version) setAsked(null);
// Each draft shown here in turn: another one shown gives up a click made
// for the one before for good, even when that draft comes back (at its
// own place, not the one clicked).
Expand All @@ -361,7 +371,7 @@ export function DraftDetail({
const ownCopy = updatesOwnCopy(draft);
const scope: DraftScope = ownCopy
? "workspace"
: pick.version === version
: pick.version === stamp
? pick.scope
: draft.scope;

Expand Down Expand Up @@ -571,7 +581,7 @@ export function DraftDetail({
disabled={checking}
onValueChange={(next) => {
if (next === "workspace" || next === "global") {
setPick({ version, scope: next });
setPick({ version: stamp, scope: next });
}
}}
>
Expand Down
1 change: 1 addition & 0 deletions src/app/components/autoskills/DraftFiles.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ function draft(over: Partial<Draft> & { proposal_id: string }): Draft {
skill_md_truncated: false,
rationale_md: "",
rationale_md_truncated: false,
content_digest: null,
files: [],
install_target: null,
install_base: null,
Expand Down
1 change: 1 addition & 0 deletions src/app/components/autoskills/DraftList.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ function draft(over: Partial<Draft> & { proposal_id: string }): Draft {
skill_md_truncated: false,
rationale_md: "",
rationale_md_truncated: false,
content_digest: null,
files: [],
install_target: null,
install_base: null,
Expand Down
1 change: 1 addition & 0 deletions src/app/components/autoskills/DraftsBanner.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ function draft(over: Partial<Draft> & { proposal_id: string }): Draft {
skill_md_truncated: false,
rationale_md: "",
rationale_md_truncated: false,
content_digest: null,
files: [],
install_target: null,
install_base: null,
Expand Down
Loading
Loading