From a1ef61a6f07d0a707503b54a4200e646a92e7b5b Mon Sep 17 00:00:00 2001 From: qiansc Date: Fri, 13 Nov 2026 11:27:22 +0000 Subject: [PATCH] fix(context-cli): keep later production stages and cancel partial revision delivery on rollback A rollback now always records the production stage it found (null when none), so finishing it never discards a stage prepared afterwards. Rolling back also deletes the partial revision delivery selection of the replaced revision, which otherwise stopped close. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 2 + .../manuals/guides/knowledge-updates.md | 6 ++- .../resources/procedures/knowledge-updates.md | 6 ++- .../__tests__/projectKnowledgeUpdate.test.ts | 52 +++++++++++++++++++ .../src/project/revisionDelivery.ts | 3 +- .../context-cli/src/project/taskRollback.ts | 50 +++++++++++------- .../context/docs/guides/knowledge-updates.md | 6 ++- 7 files changed, 100 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a9930662..a5a140da 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ All notable changes to Context are documented here. - The rollback preview shows what happens to the current production stage (`production.effect`): `discard-whole-task` deletes the stage, its unfinished tasks and its working directory at cleanup; `keep` means only a page revision that interrupted production is rolled back, and production resumes unchanged. Previously abandoning such a revision also deleted production. - While a rollback is active, the build no longer checks a partial-delivery selection that the rollback removed ("No reviewed articles can be delivered"); the rollback cleanup settles it. - Rollback cleanup removes production before the rollback record, so a cleanup that fails can be retried. +- A production stage prepared after a rollback was applied (for example with a `prepare-current` command obtained earlier) is no longer deleted when that rollback finishes; the rollback now records that it found no stage, and only the stage it disclosed can be discarded. +- Rolling back a page revision with a requested partial delivery no longer stops `context close` with "Partial revision delivery is no longer active": the rollback cancels that delivery selection and lists its pages in the preview (`discarded_revision_delivery`). ## 0.7.98 - 2026-11-11 diff --git a/packages/context-cli/context-workflow/resources/manuals/guides/knowledge-updates.md b/packages/context-cli/context-workflow/resources/manuals/guides/knowledge-updates.md index 2de05204..96f34bf2 100644 --- a/packages/context-cli/context-workflow/resources/manuals/guides/knowledge-updates.md +++ b/packages/context-cli/context-workflow/resources/manuals/guides/knowledge-updates.md @@ -621,8 +621,10 @@ already delivered pages. The preview shows actual before/after contents and draft loss, including the open production stage: `production.effect` is `discard-whole-task` (cleanup deletes the stage, its unfinished tasks and every unsubmitted file in its working directory) or `keep` (only a page revision that -interrupted production is rolled back; production resumes unchanged). State this -to the user. Once the user approves that exact scope, run the preview's apply +interrupted production is rolled back; production resumes unchanged). +`discarded_revision_delivery` lists pages of a requested partial delivery that +the rollback cancels. A production stage prepared after the rollback was applied +is not part of it and is kept. State this to the user. Once the user approves that exact scope, run the preview's apply command with its plan digest. Then follow the rollback Route through close, build and cleanup. A failed build retries delivery, not the already-applied reversions. Begin the independent task only after cleanup succeeds. diff --git a/packages/context-cli/context-workflow/resources/procedures/knowledge-updates.md b/packages/context-cli/context-workflow/resources/procedures/knowledge-updates.md index 8d967bf5..6d491031 100644 --- a/packages/context-cli/context-workflow/resources/procedures/knowledge-updates.md +++ b/packages/context-cli/context-workflow/resources/procedures/knowledge-updates.md @@ -621,8 +621,10 @@ already delivered pages. The preview shows actual before/after contents and draft loss, including the open production stage: `production.effect` is `discard-whole-task` (cleanup deletes the stage, its unfinished tasks and every unsubmitted file in its working directory) or `keep` (only a page revision that -interrupted production is rolled back; production resumes unchanged). State this -to the user. Once the user approves that exact scope, run the preview's apply +interrupted production is rolled back; production resumes unchanged). +`discarded_revision_delivery` lists pages of a requested partial delivery that +the rollback cancels. A production stage prepared after the rollback was applied +is not part of it and is kept. State this to the user. Once the user approves that exact scope, run the preview's apply command with its plan digest. Then follow the rollback Route through close, build and cleanup. A failed build retries delivery, not the already-applied reversions. Begin the independent task only after cleanup succeeds. diff --git a/packages/context-cli/src/__tests__/projectKnowledgeUpdate.test.ts b/packages/context-cli/src/__tests__/projectKnowledgeUpdate.test.ts index b740730a..6fdbf3dd 100644 --- a/packages/context-cli/src/__tests__/projectKnowledgeUpdate.test.ts +++ b/packages/context-cli/src/__tests__/projectKnowledgeUpdate.test.ts @@ -304,6 +304,58 @@ test("abandoning a page revision that interrupted production keeps production un expect(await readProductionStage(root)).toEqual(before); }, 60_000); +test("a production stage prepared after a rollback without one is kept when the rollback finishes", async () => { + const { rollbackProjectTask, finishTaskRollback, readTaskRollback } = await import("../project/taskRollback.js"); + const { productionPlanningRequest } = await import("../project/productionPlanning.js"); + const { prepareCurrentProductionStage } = await import("../project/productionStagePreparation.js"); + const { readProductionStage } = await import("../project/productionStageStore.js"); + const { resumeWorkspaceTask } = await import("../project/taskResumption.js"); + const root = await initialRevisionKnowledge(roots); + await resumeWorkspaceTask(root); + // A prepare-current command obtained before the rollback. + const request = (await productionPlanningRequest(root))!; + const value = { summary: "Discard the unfinished task.", discard_unfinished: true as const, files: [] }; + const plan = await rollbackProjectTask({ projectRoot: root, value }); + expect(plan.production).toBeNull(); + await rollbackProjectTask({ projectRoot: root, value, apply: true, plan_digest: plan.revision }); + await prepareCurrentProductionStage({ projectRoot: root, revision: request.revision }); + const later = (await readProductionStage(root))!; + expect(later).toBeDefined(); + await closeProjectWorkspace(root); + await buildProjectPackages(root); + expect(await finishTaskRollback(root)).toMatchObject({ action: "finished", production: "kept" }); + expect(await readTaskRollback(root)).toBeUndefined(); + expect(await readProductionStage(root)).toEqual(later); +}, 60_000); + +test("rolling back a partially delivered page revision discards its delivery selection so close and build proceed", async () => { + const { rollbackProjectTask, finishTaskRollback, readTaskRollback } = await import("../project/taskRollback.js"); + const root = await initialRevisionKnowledge(roots); + await beginKnowledgeUpdate(root, { scopes: [scope] }); + const update = (await readKnowledgeUpdate(root))!; + await complete(root, { stage: "source-update", decisions: selectedUpdatePages(update).map(({ path }) => ({ path, instruction: "Improve this explanation." })), scope_summary: "Both pages.", new_topics: [] }); + const first = (await readApprovedRevision(root))!; + const { approvedContextSectionsInMarkdown } = await import("../project/verifyContextSections.js"); + const section = approvedContextSectionsInMarkdown(first.target.markdown)[0]!; + await completeApprovedRevision({ projectRoot: root, revision: first.revision, + sections: [{ section_id: section.id!, content: [{ markdown: `${section.readerVisibleBody}\n\nClarified usage.` }] }] }); + const second = (await readApprovedRevision(root))!; + await complete(root, { stage: "approved-revision", markdown: second.target.markdown.replace("public entry point", "documented public entry point") }); + await approveOne(root, first.target.path); + const { requestRevisionDelivery, readRevisionDelivery } = await import("../project/revisionDelivery.js"); + await requestRevisionDelivery(root); + expect((await readRevisionDelivery(root))?.partial.paths).toEqual([`knowledge/${first.target.path}`]); + const value = { summary: "Abandon the page revisions.", discard_unfinished: true as const, files: [] }; + const plan = await rollbackProjectTask({ projectRoot: root, value }); + expect(plan.discarded_revision_delivery).toEqual([`knowledge/${first.target.path}`]); + await rollbackProjectTask({ projectRoot: root, value, apply: true, plan_digest: plan.revision }); + expect(await readRevisionDelivery(root)).toBeUndefined(); + await closeProjectWorkspace(root); + await buildProjectPackages(root); + await finishTaskRollback(root); + expect(await readTaskRollback(root)).toBeUndefined(); +}, 60_000); + test.each(["architecture", "standards"])("explicit page move to %s preserves identity and updates incoming navigation through Review apply", async (collection) => { const { beginDocumentRevision } = await import("../project/documentRevision.js"); const { access } = await import("node:fs/promises"); diff --git a/packages/context-cli/src/project/revisionDelivery.ts b/packages/context-cli/src/project/revisionDelivery.ts index a9045a99..6a8bc84c 100644 --- a/packages/context-cli/src/project/revisionDelivery.ts +++ b/packages/context-cli/src/project/revisionDelivery.ts @@ -11,7 +11,8 @@ import { selectPartialDelivery } from "./partialDelivery.js"; import { readApprovedRevision } from "./approvedRevision.js"; import { readCandidateRecords } from "./candidateLedger.js"; -const FILE = join(LIFECYCLE_ROOT, "revision-delivery.json"); +export const REVISION_DELIVERY_FILE = join(LIFECYCLE_ROOT, "revision-delivery.json"); +const FILE = REVISION_DELIVERY_FILE; const stateSchema = z.object({ partial: z.object({ kind: z.literal("revision"), paths: z.array(z.string().startsWith("knowledge/")).min(1), refs: z.array(z.string()).min(1) }).strict(), diff --git a/packages/context-cli/src/project/taskRollback.ts b/packages/context-cli/src/project/taskRollback.ts index 6a9c48b7..d19071d0 100644 --- a/packages/context-cli/src/project/taskRollback.ts +++ b/packages/context-cli/src/project/taskRollback.ts @@ -8,6 +8,7 @@ import { CANDIDATE_LEDGER_FILE, readCandidateRecords } from "./candidateLedger.j import { durableContentDigest } from "./durableSingleFileTransaction.js"; import { runDurableMultiFileTransaction, safeProjectTarget } from "./durableMultiFileTransaction.js"; import { withProjectWriteLock } from "./writeLock.js"; +import { REVISION_DELIVERY_FILE, readRevisionDelivery } from "./revisionDelivery.js"; const digest = z.string().regex(/^sha256:[a-f0-9]{64}$/u); const schema = z.object({ @@ -22,8 +23,9 @@ const stateSchema = z.object({ protocol: z.literal("context.task-rollback/v1"), }).strict()), // The production stage present when the rollback was applied and whether the // rollback discards it (whole current task) or only the revision that - // interrupted it (production resumes unchanged). Absent in older states. - production: z.object({ stage: z.string().min(1), discard: z.boolean() }).strict().optional(), + // interrupted it (production resumes unchanged). null records that no stage + // existed, so a stage prepared later is never discarded; absent in older states. + production: z.object({ stage: z.string().min(1), discard: z.boolean() }).strict().nullable().optional(), }).strict(); const UNFINISHED_TASK_STATUSES = new Set(["pending", "issued", "blocked"]); @@ -45,6 +47,15 @@ async function productionScope(projectRoot: string) { digest: indexerProtocolDigest(stage), }; } +function describeProduction(production: Awaited>) { + if (!production) return null; + return production.discard + ? { stage: production.stage, effect: "discard-whole-task", unfinished_tasks: production.unfinished_tasks, + deleted_working_directory: production.working_directory, + note: "The whole current task is discarded: the production stage, its unfinished tasks and every unsubmitted file in the working directory." } + : { stage: production.stage, effect: "keep", unfinished_tasks: production.unfinished_tasks, + note: "Only the page revision that interrupted production is rolled back; production resumes unchanged." }; +} export async function readTaskRollback(projectRoot: string) { const raw = await readJsonMaybe(projectRoot, await revisionStoragePath(projectRoot)); if (!raw || typeof raw !== "object" || !("protocol" in raw) || raw.protocol !== "context.task-rollback/v1") return undefined; @@ -102,28 +113,30 @@ export async function rollbackProjectTask(input: { const production = await productionScope(input.projectRoot); const current = await contents(input.projectRoot, await revisionStoragePath(input.projectRoot)); const candidateBytes = await contents(input.projectRoot, CANDIDATE_LEDGER_FILE); + // A partial delivery selected from the replaced revision cannot be closed + // against the rollback; it is discarded with that revision. + const delivery = await readRevisionDelivery(input.projectRoot); + const deliveryBytes = await contents(input.projectRoot, REVISION_DELIVERY_FILE); const revision = indexerProtocolDigest({ value, targets, candidateBytes: candidateBytes ?? null, - current: current ?? null, ledger: ledger ?? null, - ...(production ? { production: { stage: production.digest, discard: production.discard } } : {}) }); + current: current ?? null, ledger: ledger ?? null, deliveryBytes: deliveryBytes ?? null, + production: production ? { stage: production.digest, discard: production.discard } : null }); if (!input.apply) return { action: "preview", revision, summary: value.summary, changes, discarded_candidates: candidates.map((candidate) => candidate.candidate_id), discarded_tasks: ledger?.entries.length ?? 0, + discarded_revision_delivery: delivery?.partial.paths ?? [], // Disclose the production stage before approval: what is deleted with the task. - production: production === undefined ? null : production.discard - ? { stage: production.stage, effect: "discard-whole-task", unfinished_tasks: production.unfinished_tasks, - deleted_working_directory: production.working_directory, - note: "The whole current task is discarded: the production stage, its unfinished tasks and every unsubmitted file in the working directory." } - : { stage: production.stage, effect: "keep", unfinished_tasks: production.unfinished_tasks, - note: "Only the page revision that interrupted production is rolled back; production resumes unchanged." }, + production: describeProduction(production), next: `context task rollback --input --apply --plan-digest '${revision}' --format json` }; if (input.plan_digest !== revision) throw new TypeError("Rollback preview is missing or stale; preview the same explicit input again before applying."); const state = { protocol: "context.task-rollback/v1", revision, summary: value.summary, files: targets.map(({ path, target_digest }) => ({ path, target_digest })), - ...(production ? { production: { stage: production.stage, discard: production.discard } } : {}) }; + production: production ? { stage: production.stage, discard: production.discard } : null }; const body = `${JSON.stringify(state)}\n`; targets.push({ path: await revisionStoragePath(input.projectRoot), operation: "write", base_digest: current === undefined ? null : durableContentDigest(current), target_digest: durableContentDigest(body), content: body }); + if (deliveryBytes !== undefined) targets.push({ path: REVISION_DELIVERY_FILE, operation: "delete", + base_digest: durableContentDigest(deliveryBytes), target_digest: null }); if (candidateBytes !== undefined) targets.push({ path: CANDIDATE_LEDGER_FILE, operation: "delete", base_digest: durableContentDigest(candidateBytes), target_digest: null }); targets.sort((a, b) => a.path < b.path ? -1 : a.path > b.path ? 1 : 0); @@ -155,14 +168,15 @@ export async function finishTaskRollback(projectRoot: string) { const production = await readProductionStage(projectRoot); // Discard only the production stage this rollback disclosed as part of the // task. A rollback of a revision that interrupted production retires just its - // own pointer, like a finished revision; production resumes unchanged. - // Older states without the record discard the stage they were applied over. - if (production && rollback.production?.discard === false) { + // own pointer, like a finished revision; production resumes unchanged. A + // stage prepared after the rollback (none or another stage recorded) was + // never approved for discard and is kept the same way. Older states without + // the record discard the stage they were applied over. + const recorded = rollback.production; + if (production && recorded !== undefined && (recorded === null || !recorded.discard || recorded.stage !== production.id)) { await rm(join(projectRoot, await revisionStoragePath(projectRoot)), { force: true }); - return { action: "finished", production: "resumed", next: "context status --format json" }; - } - if (production && rollback.production && rollback.production.stage !== production.id) { - throw new TypeError("The production stage changed after this rollback was applied; it was not part of the approved rollback. Follow context status --format json."); + return { action: "finished", production: recorded?.stage === production.id ? "resumed" : "kept", + next: "context status --format json" }; } const { clearCompletedLifecycle } = await import("./lifecycleCleanup.js"); await clearCompletedLifecycle(projectRoot, { discardUnfinishedProduction: true }); diff --git a/packages/context/docs/guides/knowledge-updates.md b/packages/context/docs/guides/knowledge-updates.md index a5cbd1e3..f577b463 100644 --- a/packages/context/docs/guides/knowledge-updates.md +++ b/packages/context/docs/guides/knowledge-updates.md @@ -615,8 +615,10 @@ already delivered pages. The preview shows actual before/after contents and draft loss, including the open production stage: `production.effect` is `discard-whole-task` (cleanup deletes the stage, its unfinished tasks and every unsubmitted file in its working directory) or `keep` (only a page revision that -interrupted production is rolled back; production resumes unchanged). State this -to the user. Once the user approves that exact scope, run the preview's apply +interrupted production is rolled back; production resumes unchanged). +`discarded_revision_delivery` lists pages of a requested partial delivery that +the rollback cancels. A production stage prepared after the rollback was applied +is not part of it and is kept. State this to the user. Once the user approves that exact scope, run the preview's apply command with its plan digest. Then follow the rollback Route through close, build and cleanup. A failed build retries delivery, not the already-applied reversions. Begin the independent task only after cleanup succeeds.