Skip to content

An open-source, faster Devin alternative

TypeScript

cli: memory edit recovery keeps its target and reports a lost response as unconfirmed

Igor Zalutskidb808999cdfd
diff --git a/cli/src/memory-commands.test.ts b/cli/src/memory-commands.test.ts
index 452c9892..df823b78 100644
--- a/cli/src/memory-commands.test.ts
+++ b/cli/src/memory-commands.test.ts
@@ -236,7 +236,9 @@ test("an over-limit save keeps the edited draft and says where it is", async (co
}
});
-test("a save that never reaches the API keeps the edited draft", async (context) => {
+// Review reproduction (U8): no answer from the API is not proof that
+// nothing was written.
+test("a save whose response is lost keeps the draft and is reported as unconfirmed", async (context) => {
context.mock.method(globalThis, "fetch", async () => {
throw new TypeError("fetch failed");
});
@@ -248,16 +250,54 @@ test("a save that never reaches the API keeps the edited draft", async (context)
}).catch((error: unknown) => error);
assert.ok(error instanceof CLIError);
- assert.equal(error.code, "command_failed");
- assert.match(error.message, /was not saved: fetch failed/);
+ assert.equal(error.code, "memory_save_unconfirmed");
+ assert.match(error.message, /may or may not have been saved: fetch failed/);
+ assert.doesNotMatch(error.message, /was not saved/);
+ assert.match(
+ error.hint,
+ /Run `opencomputer memory show requirements workshop --project prj_1 --environment development` to see whether the revision changed/,
+ );
assert.match(error.hint, new RegExp(`kept at ${draft.path}`));
- assert.deepEqual(error.details, { editedTextPath: draft.path });
+ assert.deepEqual(error.details, { editedTextPath: draft.path, expectedRevision: '"rev-1"' });
await access(draft.path);
} finally {
await draft.cleanup();
}
});
+// Review reproduction (U8): the recovery command targets what the edit
+// targeted, carries the summary, and quotes what the shell would split.
+test("the draft recovery command keeps project, environment, summary and shell quoting", async (context) => {
+ context.mock.method(globalThis, "fetch", async () =>
+ Response.json(
+ { error: { code: "memory_limit_exceeded", message: "Text is 9000 bytes; the limit is 8192." } },
+ { status: 413 },
+ ),
+ );
+ const directory = await mkdtemp(join(tmpdir(), "opencomputer memory test-"));
+ const path = join(directory, "requirements--workshop.md");
+ await writeFile(path, "text", "utf8");
+ try {
+ const error = await saveMemoryEdit(new OpenComputerClient(config), {
+ ...target,
+ projectId: "prj_other",
+ environment: "production",
+ summary: "Workshop's requirements",
+ draft: { path, discard: async () => undefined },
+ }).catch((error: unknown) => error);
+
+ assert.ok(error instanceof CLIError);
+ assert.equal(error.code, "memory_too_large");
+ const command = error.hint.match(/`(opencomputer memory edit [^`]+)`/)?.[1];
+ assert.equal(
+ command,
+ `opencomputer memory edit requirements workshop --project prj_other --environment production --summary 'Workshop'\\''s requirements' --text-file '${path}'`,
+ );
+ } finally {
+ await rm(directory, { recursive: true, force: true });
+ }
+});
+
test("a saved edit discards its draft", async (context) => {
context.mock.method(globalThis, "fetch", async () =>
Response.json(
diff --git a/cli/src/memory-commands.ts b/cli/src/memory-commands.ts
index 4ff35fde..07cf7e16 100644
--- a/cli/src/memory-commands.ts
+++ b/cli/src/memory-commands.ts
@@ -106,10 +106,49 @@ export interface MemoryEditDraft {
discard: () => Promise<void>;
}
+/** A word as a POSIX shell reads it back unchanged. */
+export function shellWord(value: string): string {
+ return /^[A-Za-z0-9_@%+=:,./-]+$/.test(value)
+ ? value
+ : `'${value.replaceAll("'", "'\\''")}'`;
+}
+
+/**
+ * The command that retries an edit from its kept draft: the same target
+ * (project and environment included, so it never lands on the linked
+ * project's Development by default), the same summary, and a path the
+ * shell reads back whatever it contains.
+ */
+export function memoryEditResumeCommand(input: {
+ projectId: string;
+ resource: string;
+ id: string;
+ environment: MemoryEnvironment;
+ summary?: string;
+ draftPath: string;
+}): string {
+ return [
+ "opencomputer memory edit",
+ shellWord(input.resource),
+ shellWord(input.id),
+ "--project",
+ shellWord(input.projectId),
+ "--environment",
+ input.environment,
+ ...(input.summary !== undefined
+ ? ["--summary", shellWord(input.summary)]
+ : []),
+ "--text-file",
+ shellWord(input.draftPath),
+ ].join(" ");
+}
+
/**
* Replaces a document's text at the revision that was read. On success the
* draft file goes; on any failure it stays and the error names it, so an
- * over-limit, unreachable or failing save costs no work.
+ * over-limit, unreachable or failing save costs no work. An answer from the
+ * API is a verdict; no answer at all (the request never arrived, or its
+ * response was lost) is not, and is reported as an unconfirmed write.
*/
export async function saveMemoryEdit(
client: OpenComputerClient,
@@ -135,9 +174,19 @@ export async function saveMemoryEdit(
: {};
const resume = draft
? ` Your edited text is kept at ${draft.path}; save it with ` +
- `\`opencomputer memory edit ${input.resource} ${input.id} --text-file ${draft.path}\`.`
+ `\`${memoryEditResumeCommand({ ...input, draftPath: draft.path })}\`.`
: "";
- if (error instanceof APIError && error.status === 412) {
+ if (!(error instanceof APIError)) {
+ const failure = structuredError(error);
+ throw new CLIError(
+ "memory_save_unconfirmed",
+ `${input.resource}/${input.id} may or may not have been saved: ${failure.message}`,
+ `Run \`opencomputer memory show ${shellWord(input.resource)} ${shellWord(input.id)} --project ${shellWord(input.projectId)} --environment ${input.environment}\` to see whether the revision changed before saving again.` +
+ resume,
+ { ...kept, expectedRevision: input.etag },
+ );
+ }
+ if (error.status === 412) {
const latest = await client
.memoryDocument(input)
.catch(() => null);