aetherlight
Version:
Voice-to-intelligence platform for developers. Voice capture, sprint planning with AI, bug/feature forms, pattern matching to prevent AI hallucinations.
160 lines • 5.44 kB
JavaScript
;
/**
* Workflow State Persistence - Save/load workflow state
*
* DESIGN DECISION: File-based persistence with atomic writes
* WHY: Survives VS Code restarts, enables workflow recovery
*
* REASONING CHAIN:
* 1. Workflow state serialized to JSON
* 2. Write to temp file (.tmp)
* 3. Atomic rename (temp → final)
* 4. On VS Code restart: Read state file
* 5. Reconstruct state machine from JSON
* 6. Result: Workflow resumes exactly where it left off
*
* PATTERN: Pattern-PERSISTENCE-001 (Atomic File Persistence)
* RELATED: AS-014 (File-Based IPC), AS-015 (Workflow State Machine)
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.WorkflowPersistence = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const state_machine_1 = require("./state_machine");
/**
* Workflow state persistence manager
*/
class WorkflowPersistence {
/**
* Create persistence manager
*
* @param workflowDir - Directory for workflow files (default: .lumina/workflow)
*/
constructor(workflowDir) {
this.stateFile = path.join(workflowDir, 'workflow-state.json');
// Ensure directory exists
if (!fs.existsSync(workflowDir)) {
fs.mkdirSync(workflowDir, { recursive: true });
}
}
/**
* Save workflow state
*
* DESIGN DECISION: Atomic write (temp + rename)
* WHY: Prevents corruption if VS Code crashes during write
*/
async save(machine) {
const tempFile = `${this.stateFile}.tmp`;
// Serialize to JSON (pretty for debugging)
const json = JSON.stringify(machine.toJSON(), null, 2);
// Write to temp file
fs.writeFileSync(tempFile, json, 'utf8');
// Atomic rename
fs.renameSync(tempFile, this.stateFile);
}
/**
* Load workflow state
*
* @returns Workflow state machine or null if no saved state
*/
async load() {
if (!fs.existsSync(this.stateFile)) {
return null;
}
try {
const json = fs.readFileSync(this.stateFile, 'utf8');
const data = JSON.parse(json);
return state_machine_1.WorkflowStateMachine.fromJSON(data);
}
catch (error) {
console.error('Failed to load workflow state:', error);
return null;
}
}
/**
* Check if saved state exists
*/
exists() {
return fs.existsSync(this.stateFile);
}
/**
* Delete saved state
*
* DESIGN DECISION: Manual cleanup after workflow complete
* WHY: Prevents stale state from interfering with new sprints
*/
async delete() {
if (fs.existsSync(this.stateFile)) {
fs.unlinkSync(this.stateFile);
}
}
/**
* Save workflow snapshot (lightweight alternative)
*
* DESIGN DECISION: Separate snapshot file for quick reads
* WHY: UI can read snapshot without deserializing full state machine
*
* @param snapshot - Workflow snapshot
*/
async saveSnapshot(snapshot) {
const snapshotFile = path.join(path.dirname(this.stateFile), 'workflow-snapshot.json');
const tempFile = `${snapshotFile}.tmp`;
const json = JSON.stringify(snapshot, null, 2);
fs.writeFileSync(tempFile, json, 'utf8');
fs.renameSync(tempFile, snapshotFile);
}
/**
* Load workflow snapshot
*
* @returns Workflow snapshot or null if not found
*/
async loadSnapshot() {
const snapshotFile = path.join(path.dirname(this.stateFile), 'workflow-snapshot.json');
if (!fs.existsSync(snapshotFile)) {
return null;
}
try {
const json = fs.readFileSync(snapshotFile, 'utf8');
return JSON.parse(json);
}
catch (error) {
console.error('Failed to load workflow snapshot:', error);
return null;
}
}
}
exports.WorkflowPersistence = WorkflowPersistence;
//# sourceMappingURL=persistence.js.map