-
Notifications
You must be signed in to change notification settings - Fork 438
feat: [ENG-2070] Dream undo — revert last dream from previousTexts #392
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,262 @@ | ||
| /** | ||
| * Dream Undo — reverts the last dream's file changes using previousTexts from the dream log. | ||
| * | ||
| * Runs directly from CLI (no daemon/agent needed). Pure file I/O. | ||
| * Only undoes the LAST dream — not a history stack. | ||
| */ | ||
|
|
||
| import {mkdir, unlink, writeFile} from 'node:fs/promises' | ||
| import {dirname, resolve} from 'node:path' | ||
|
|
||
| import type {DreamLogEntry, DreamOperation} from './dream-log-schema.js' | ||
| import type {DreamState} from './dream-state-schema.js' | ||
|
|
||
| export type DreamUndoDeps = { | ||
| archiveService?: {restoreEntry(stubPath: string, directory?: string): Promise<string>} | ||
| contextTreeDir: string | ||
| dreamLogStore: { | ||
| getById(id: string): Promise<DreamLogEntry | null> | ||
| save(entry: DreamLogEntry): Promise<void> | ||
| } | ||
| dreamStateService: { | ||
| read(): Promise<DreamState> | ||
| write(state: DreamState): Promise<void> | ||
| } | ||
| manifestService: {buildManifest(dir?: string): Promise<unknown>} | ||
| } | ||
|
|
||
| export interface DreamUndoResult { | ||
| deletedFiles: string[] | ||
| dreamId: string | ||
| errors: string[] | ||
| restoredArchives: string[] | ||
| restoredFiles: string[] | ||
| } | ||
|
|
||
| export async function undoLastDream(deps: DreamUndoDeps): Promise<DreamUndoResult> { | ||
| const {contextTreeDir, dreamLogStore, dreamStateService, manifestService} = deps | ||
|
|
||
| // ── Precondition checks ───────────────────────────────────────────────── | ||
| const state = await dreamStateService.read() | ||
| if (!state.lastDreamLogId) { | ||
| throw new Error('No dream to undo') | ||
| } | ||
|
|
||
| const log = await dreamLogStore.getById(state.lastDreamLogId) | ||
| if (!log) { | ||
| throw new Error(`Dream log not found: ${state.lastDreamLogId}`) | ||
| } | ||
|
|
||
| if (log.status === 'undone') { | ||
| throw new Error(`Dream already undone: ${state.lastDreamLogId}`) | ||
| } | ||
|
|
||
| if (log.status !== 'completed' && log.status !== 'partial') { | ||
| throw new Error(`Cannot undo dream with status: ${log.status}`) | ||
| } | ||
|
|
||
| // ── Reverse operations ────────────────────────────────────────────────── | ||
| const result: DreamUndoResult = { | ||
| deletedFiles: [], | ||
| dreamId: log.id, | ||
| errors: [], | ||
| restoredArchives: [], | ||
| restoredFiles: [], | ||
| } | ||
|
|
||
| // Track pending merges to remove (for PRUNE/SUGGEST_MERGE) | ||
| const mergesToRemove: Array<{mergeTarget: string; sourceFile: string}> = [] | ||
|
|
||
| const reversed = [...log.operations].reverse() | ||
| for (const op of reversed) { | ||
| try { | ||
| // eslint-disable-next-line no-await-in-loop | ||
| await undoOperation(op, {contextTreeDir, deps, mergesToRemove, result}) | ||
| } catch (error) { | ||
| result.errors.push(error instanceof Error ? error.message : String(error)) | ||
| } | ||
| } | ||
|
|
||
| // ── Post-undo: rebuild manifest ───────────────────────────────────────── | ||
| try { | ||
| await manifestService.buildManifest(contextTreeDir) | ||
|
RyanNg1403 marked this conversation as resolved.
|
||
| } catch (error) { | ||
| result.errors.push(`Manifest rebuild failed: ${error instanceof Error ? error.message : String(error)}`) | ||
| } | ||
|
RyanNg1403 marked this conversation as resolved.
|
||
|
|
||
| // ── Post-undo: mark log as undone ─────────────────────────────────────── | ||
| const undoneLog: DreamLogEntry = { | ||
| completedAt: log.completedAt, | ||
| id: log.id, | ||
| operations: log.operations, | ||
| startedAt: log.startedAt, | ||
| status: 'undone', | ||
| summary: log.summary, | ||
| trigger: log.trigger, | ||
| undoneAt: Date.now(), | ||
| } | ||
| await dreamLogStore.save(undoneLog) | ||
|
|
||
| // ── Post-undo: rewind dream state ─────────────────────────────────────── | ||
| let {pendingMerges} = state | ||
| if (mergesToRemove.length > 0) { | ||
| pendingMerges = (pendingMerges ?? []).filter( | ||
| (pm) => !mergesToRemove.some((rm) => rm.sourceFile === pm.sourceFile && rm.mergeTarget === pm.mergeTarget), | ||
| ) | ||
| } | ||
|
|
||
| await dreamStateService.write({ | ||
| ...state, | ||
| lastDreamAt: null, | ||
| pendingMerges, | ||
| totalDreams: Math.max(0, state.totalDreams - 1), | ||
| }) | ||
|
RyanNg1403 marked this conversation as resolved.
|
||
|
|
||
| return result | ||
| } | ||
|
|
||
| /** Unlink a file, ignoring ENOENT (already gone) but rethrowing other errors. */ | ||
| async function unlinkSafe(filePath: string): Promise<void> { | ||
| try { | ||
| await unlink(filePath) | ||
| } catch (error) { | ||
| if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error | ||
| } | ||
| } | ||
|
|
||
| /** Resolve a relative path within contextTreeDir, rejecting traversal outside the tree. */ | ||
| function safePath(contextTreeDir: string, relativePath: string): string { | ||
| const full = resolve(contextTreeDir, relativePath) | ||
| if (!full.startsWith(contextTreeDir + '/') && full !== contextTreeDir) { | ||
| throw new Error(`Path traversal blocked: ${relativePath}`) | ||
| } | ||
|
|
||
| return full | ||
| } | ||
|
|
||
| // ── Per-operation undo handlers ─────────────────────────────────────────────── | ||
|
|
||
| type UndoContext = { | ||
| contextTreeDir: string | ||
| deps: DreamUndoDeps | ||
| mergesToRemove: Array<{mergeTarget: string; sourceFile: string}> | ||
| result: DreamUndoResult | ||
| } | ||
|
|
||
| async function undoOperation(op: DreamOperation, ctx: UndoContext): Promise<void> { | ||
| switch (op.type) { | ||
| case 'CONSOLIDATE': { | ||
| await undoConsolidate(op, ctx.contextTreeDir, ctx.result) | ||
| break | ||
| } | ||
|
|
||
| case 'PRUNE': { | ||
| await undoPrune(op, ctx) | ||
| break | ||
| } | ||
|
|
||
| case 'SYNTHESIZE': { | ||
| await undoSynthesize(op, ctx.contextTreeDir, ctx.result) | ||
| break | ||
| } | ||
| } | ||
| } | ||
|
|
||
| async function undoConsolidate( | ||
| op: Extract<DreamOperation, {type: 'CONSOLIDATE'}>, | ||
| contextTreeDir: string, | ||
| result: DreamUndoResult, | ||
| ): Promise<void> { | ||
| switch (op.action) { | ||
| case 'CROSS_REFERENCE': { | ||
| // Non-destructive — skip | ||
| break | ||
| } | ||
|
|
||
| case 'MERGE': { | ||
| if (!op.previousTexts || Object.keys(op.previousTexts).length === 0) { | ||
| throw new Error(`Cannot undo MERGE: missing previousTexts for ${op.outputFile ?? op.inputFiles[0]}`) | ||
| } | ||
|
|
||
| // Restore all source files from previousTexts | ||
| for (const [filePath, content] of Object.entries(op.previousTexts)) { | ||
| const fullPath = safePath(contextTreeDir, filePath) | ||
| // eslint-disable-next-line no-await-in-loop | ||
| await mkdir(dirname(fullPath), {recursive: true}) | ||
| // eslint-disable-next-line no-await-in-loop | ||
| await writeFile(fullPath, content, 'utf8') | ||
|
RyanNg1403 marked this conversation as resolved.
|
||
| result.restoredFiles.push(filePath) | ||
| } | ||
|
|
||
| // Delete merged output if it wasn't an original source | ||
| if (op.outputFile && !op.previousTexts[op.outputFile]) { | ||
| await unlinkSafe(safePath(contextTreeDir, op.outputFile)) | ||
| result.deletedFiles.push(op.outputFile) | ||
|
RyanNg1403 marked this conversation as resolved.
|
||
| } | ||
|
|
||
| break | ||
| } | ||
|
|
||
| case 'TEMPORAL_UPDATE': { | ||
| if (!op.previousTexts || Object.keys(op.previousTexts).length === 0) { | ||
| throw new Error(`Cannot undo TEMPORAL_UPDATE: missing previousTexts for ${op.inputFiles[0]}`) | ||
| } | ||
|
|
||
| for (const [filePath, content] of Object.entries(op.previousTexts)) { | ||
| const fullPath = safePath(contextTreeDir, filePath) | ||
| // eslint-disable-next-line no-await-in-loop | ||
| await mkdir(dirname(fullPath), {recursive: true}) | ||
| // eslint-disable-next-line no-await-in-loop | ||
| await writeFile(fullPath, content, 'utf8') | ||
| result.restoredFiles.push(filePath) | ||
| } | ||
|
|
||
| break | ||
| } | ||
| } | ||
| } | ||
|
|
||
| async function undoSynthesize( | ||
| op: Extract<DreamOperation, {type: 'SYNTHESIZE'}>, | ||
| contextTreeDir: string, | ||
| result: DreamUndoResult, | ||
| ): Promise<void> { | ||
| // UPDATE modified a pre-existing file — can't undo without previousTexts (not captured by SYNTHESIZE) | ||
| if (op.action === 'UPDATE') { | ||
| throw new Error(`Cannot undo SYNTHESIZE/UPDATE: previousTexts not captured for ${op.outputFile}`) | ||
| } | ||
|
|
||
| // CREATE — delete the synthesized file | ||
| await unlinkSafe(safePath(contextTreeDir, op.outputFile)) | ||
| result.deletedFiles.push(op.outputFile) | ||
|
RyanNg1403 marked this conversation as resolved.
|
||
| } | ||
|
|
||
| async function undoPrune( | ||
| op: Extract<DreamOperation, {type: 'PRUNE'}>, | ||
| ctx: UndoContext, | ||
| ): Promise<void> { | ||
| switch (op.action) { | ||
| case 'ARCHIVE': { | ||
| if (!ctx.deps.archiveService) { | ||
| throw new Error(`Cannot undo PRUNE/ARCHIVE: no archive service available for ${op.file}`) | ||
| } | ||
|
|
||
| const restored = await ctx.deps.archiveService.restoreEntry(op.file, ctx.contextTreeDir) | ||
| ctx.result.restoredArchives.push(restored) | ||
| break | ||
| } | ||
|
|
||
| case 'KEEP': { | ||
| // No-op — nothing was changed | ||
| break | ||
| } | ||
|
|
||
| case 'SUGGEST_MERGE': { | ||
| if (op.mergeTarget) { | ||
| ctx.mergesToRemove.push({mergeTarget: op.mergeTarget, sourceFile: op.file}) | ||
| } | ||
|
|
||
| break | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.