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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Unreleased

### Fixed
- `prodex tasks complete --artifact path=text` wrote the file before it looked at the task, so a completion that was then refused had already replaced it. Found 2026-11-09: completing a finished task again, or naming a task id that does not exist, overwrote that path's saved answer, and `prodex results artifact` for the task that owned it failed from then on with "Result artifact changed after finalization". Completing an open task with another task's artifact path replaced that task's answer and was accepted, and a path refused as not a result artifact was written anyway. The task and the paths are now checked first, a finished task's files are left alone, and a path that already holds other text is refused.
- The "Model hints" a consult saves with its answer (and the `model_hints:` line of `prodex pro browser status`) could carry the prompt instead of the model. They were read from the first model selector in the page and from every visible form, and a page can hold more than the composer: with a message open for editing ("Edit message"), each line of that message naming a word such as "Pro", "latest", "high" or "auto" was saved as a hint, and after a new chat the home page's hidden composer, still mounted ahead of the conversation's, supplied its own model. Text left typed in the composer was read the same way. Hints now come only from the visible composer outside the messages, without the text typed into it.
- Editing a file through the MCP repo tools (`repo_write_file_dry_run`, then `repo_write_file_apply`) reset its file mode. The new content is written to a new file that replaces the old one, and that file got the default mode: found 2026-11-09, an edited `build.sh` lost its executable bit (755 became 644), and `repo_stage_reviewed_paths` then staged `mode change 100755 => 100644`, so the commit broke the script. A file kept at 600 became readable by everyone. The replacement now takes the mode of the file it replaces.
- `repo_stage_reviewed_paths` passed the reviewed paths to git as patterns. Found 2026-11-09: with only `pages/[id].tsx` reviewed and applied, an unreviewed local edit of `pages/i.tsx` was staged too, while the receipt listed one path. A reviewed `x[0-9].txt` next to a modified `x1.txt` failed as "Staged content changed", and undoing the stage wrote x1.txt's content into the index under `x[0-9].txt`. A file named `:!draft.md` left every other modified and untracked file staged. Paths now reach git literally.
Expand Down
14 changes: 6 additions & 8 deletions src/cli-ledger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export async function runTasksCommand(rest: string[], io: CliIO): Promise<number
status: "done",
summary,
commands: readRepeatedFlag(taskArgs, "--command"),
artifacts: await writeTaskCompleteArtifacts(targetStore, readRepeatedFlag(taskArgs, "--artifact"))
artifacts: await writeTaskCompleteArtifacts(targetStore, taskId, readRepeatedFlag(taskArgs, "--artifact"))
});
io.stdout(`${result.task_id}\t${result.status}\t${result.summary}`);
return 0;
Expand Down Expand Up @@ -367,7 +367,7 @@ export function receiptInspectionListSuffix(receipt: Receipt): string {
return "";
}

export async function writeTaskCompleteArtifacts(store: BridgeStore, values: string[]): Promise<BridgeFile[]> {
export async function writeTaskCompleteArtifacts(store: BridgeStore, taskId: string, values: string[]): Promise<BridgeFile[]> {
const parsed: { artifactPath: string; content: string }[] = [];
const seen = new Set<string>();
for (const value of values) {
Expand All @@ -389,12 +389,10 @@ export async function writeTaskCompleteArtifacts(store: BridgeStore, values: str
seen.add(key);
parsed.push({ artifactPath, content });
}
const artifacts: BridgeFile[] = [];
for (const { artifactPath, content } of parsed) {
const storedPath = await store.writeArtifactText(artifactPath, content);
artifacts.push({ path: storedPath, role: "result" });
}
return artifacts;
return store.writeResultArtifactTexts(
taskId,
parsed.map(({ artifactPath, content }) => ({ path: artifactPath, content }))
);
}

export async function listTasksForInspection(
Expand Down
33 changes: 31 additions & 2 deletions src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,7 @@ export class BridgeStore {
);
}

async writeArtifactText(relativePath: string, content: string): Promise<string> {
async writeArtifactText(relativePath: string, content: string, options: { exclusive?: boolean } = {}): Promise<string> {
await this.ensure();
await this.assertBridgeDirIsRealDirectory();
await this.assertArtifactsDirIsRealDirectory();
Expand All @@ -838,11 +838,40 @@ export class BridgeStore {
await this.assertArtifactParentDirectory(path.dirname(artifactPath));
await this.assertArtifactTargetInsideIfExists(artifactPath);
},
{ create: true, mode: BRIDGE_FILE_MODE }
{ create: true, mode: BRIDGE_FILE_MODE, ...(options.exclusive ? { exclusive: true } : {}) }
);
return this.relativeToRoot(artifactPath);
}

/**
* The files of `tasks complete --artifact path=text`, written only once the
* completion is known to be possible. They used to be written first, so a
* completion refused afterwards had already replaced the file: found
* 2026-11-09, completing a finished task again, or a mistyped task id,
* overwrote a saved answer, and an open task completed with another task's
* artifact path replaced that task's answer. A finished task's files are not
* touched (completeTask then refuses, or accepts the same result again), and
* a path that already holds other text is refused.
*/
async writeResultArtifactTexts(taskId: string, texts: { path: string; content: string }[]): Promise<BridgeFile[]> {
const task = await this.getTask(taskId);
const artifacts: BridgeFile[] = texts.map((text) => ({ path: this.relativeToRoot(this.resolveArtifactPath(text.path)), role: "result" }));
assertFetchableResultArtifacts(artifacts);
assertDistinctArtifactPaths(artifacts);
if (task.status === "done" || task.status === "blocked") return artifacts;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Validate artifact text before accepting a terminal-task retry

When a done task has a missing or untrusted completion receipt, completeTask permits a matching retry to repair it. This early return discards the supplied artifact text, while assertResultMatchesRetry compares artifacts only by path and role. Consequently, retrying with the original summary and artifact path but different --artifact path=text content succeeds and signs the old answer, silently ignoring the requested text. Keep terminal-task files untouched, but compare their contents with the supplied text and reject a mismatch before allowing receipt repair.

Useful? React with 👍 / 👎.

for (const [index, text] of texts.entries()) {
const stored = artifacts[index].path;
try {
await this.writeArtifactText(stored, text.content, { exclusive: true });
} catch (error) {
if (!isErrorCode(error, "EEXIST")) throw error;
if ((await this.readArtifactText(stored)) === text.content) continue;
throw new Error(`${stored} already exists with other content; give this result an artifact path of its own.`);
}
}
return artifacts;
}

async hasArtifactText(relativePath: string): Promise<boolean> {
const artifactPath = this.resolveArtifactPath(relativePath);
try {
Expand Down
4 changes: 2 additions & 2 deletions tests/duplicate-result-artifacts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ describe("one result artifact path per result", () => {
});

it("refuses repeated --artifact paths before writing either one", async () => {
const { root, store } = await claimedTask();
const { root, store, taskId } = await claimedTask();
await expect(
writeTaskCompleteArtifacts(store, [".bridge/artifacts/results/b.md=one", ".bridge/artifacts/results/./b.md=two"])
writeTaskCompleteArtifacts(store, taskId, [".bridge/artifacts/results/b.md=one", ".bridge/artifacts/results/./b.md=two"])
).rejects.toThrow(/--artifact .*b\.md.*more than once/);
await expect(access(path.join(root, ".bridge", "artifacts", "results", "b.md"))).rejects.toThrow();
});
Expand Down
81 changes: 81 additions & 0 deletions tests/task-complete-artifact-order.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { access, mkdtemp, readFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import path from "node:path";
import { describe, expect, it } from "vitest";
import { runCli } from "../src/cli.js";

// Found 2026-11-09: `tasks complete --artifact path=text` wrote the file
// before it read the task, so a completion that was then refused still
// replaced the file. A second completion of a finished task, or a mistyped
// task id, overwrote a saved answer, and `results artifact` failed from then
// on with "Result artifact changed after finalization".
async function cli(cwd: string, args: string[]): Promise<{ code: number; out: string[]; err: string }> {
const out: string[] = [];
let err = "";
let code = 0;
try {
code = await runCli(args, { cwd, stdout: (line) => out.push(line), stderr: (line) => (err += `${line}\n`) });
} catch (error) {
code = 1;
err += error instanceof Error ? error.message : String(error);
}
return { code, out, err };
}

async function finishedTask(): Promise<{ cwd: string; taskId: string; artifact: string }> {
const cwd = await mkdtemp(path.join(tmpdir(), "prodex-complete-artifact-"));
const created = await cli(cwd, ["tasks", "create", "--title", "Report", "--prompt", "Write the report"]);
const taskId = created.out[0].split("\t")[0];
const artifact = ".bridge/artifacts/results/report.md";
const done = await cli(cwd, ["tasks", "complete", taskId, "--summary", "first", "--artifact", `${artifact}=original answer`]);
expect(done.code).toBe(0);
return { cwd, taskId, artifact };
}

describe("tasks complete --artifact", () => {
it("leaves a finished task's artifact alone when completing it again is refused", async () => {
const { cwd, taskId, artifact } = await finishedTask();
const again = await cli(cwd, ["tasks", "complete", taskId, "--summary", "first", "--artifact", `${artifact}=REPLACED`]);
expect(again.code).not.toBe(0);
expect(await readFile(path.join(cwd, artifact), "utf8")).toBe("original answer");
const fetched = await cli(cwd, ["results", "artifact", taskId]);
expect(fetched.out.join("\n")).toContain("original answer");
});

it("writes nothing for a task id that does not exist", async () => {
const { cwd, artifact } = await finishedTask();
const typo = await cli(cwd, ["tasks", "complete", "task_20261109_000000_typo", "--summary", "x", "--artifact", `${artifact}=oops`]);
expect(typo.code).not.toBe(0);
expect(typo.err).toMatch(/Task not found/);
expect(await readFile(path.join(cwd, artifact), "utf8")).toBe("original answer");
});

it("does not overwrite another task's artifact", async () => {
const { cwd, taskId, artifact } = await finishedTask();
const created = await cli(cwd, ["tasks", "create", "--title", "Second", "--prompt", "p"]);
const second = created.out[0].split("\t")[0];
const clash = await cli(cwd, ["tasks", "complete", second, "--summary", "s", "--artifact", `${artifact}=second answer`]);
expect(clash.code).not.toBe(0);
expect(clash.err).toMatch(/already exists/);
expect(await readFile(path.join(cwd, artifact), "utf8")).toBe("original answer");
expect((await cli(cwd, ["results", "artifact", taskId])).out.join("\n")).toContain("original answer");
});

it("writes nothing for a path that is not a result artifact", async () => {
const cwd = await mkdtemp(path.join(tmpdir(), "prodex-complete-artifact-"));
const created = await cli(cwd, ["tasks", "create", "--title", "Notes", "--prompt", "p"]);
const taskId = created.out[0].split("\t")[0];
const refused = await cli(cwd, ["tasks", "complete", taskId, "--summary", "s", "--artifact", ".bridge/artifacts/notes/scratch.md=x"]);
expect(refused.code).not.toBe(0);
await expect(access(path.join(cwd, ".bridge/artifacts/notes/scratch.md"))).rejects.toThrow();
});

it("writes the artifact of a completion it accepts", async () => {
const cwd = await mkdtemp(path.join(tmpdir(), "prodex-complete-artifact-"));
const created = await cli(cwd, ["tasks", "create", "--title", "Retry", "--prompt", "p"]);
const taskId = created.out[0].split("\t")[0];
const artifact = ".bridge/artifacts/results/retry.md";
expect((await cli(cwd, ["tasks", "complete", taskId, "--summary", "s", "--artifact", `${artifact}=same`])).code).toBe(0);
expect(await readFile(path.join(cwd, artifact), "utf8")).toBe("same");
});
});
Loading