/** * Reconcile service for `.peaks/2026-MM-DD-session-<6hex>/` directories. * * Reconcile scans the project root's `.peaks/` directory, identifies a * canonical session via a 4-tier heuristic, re-points * `.peaks/_runtime/session.json` (the canonical new home of the * binding; legacy `.peaks/.session.json` is read-only back-compat), * and (optionally, with apply === true) deletes empty / abandoned * session dirs older than olderThanMs. * * As of slice 2026-06-05-peaks-runtime-layer the top-level orchestrator * also runs `migrateOldRuntimeState` at the start so pre-migration * trees have their `.peaks/.session.json` / `.peaks/.active-skill.json` * / `.peaks/sop-state/` files moved into `.peaks/_runtime/`. * * Pure hand-rolled; uses only node:fs, node:path, and the existing * session-manager helper for writing the binding. No new dependencies. */ import type { ReconcileOptions, ReconcileResult, SessionEntry } from './reconcile-types.js'; /** * Walk the project root's `.peaks/` directory and return an entry per * session dir matching the standard naming pattern, sorted by name * ascending (the most recent is last by sort order, since the date * prefix dominates the lexicographic order). * * Each entry's `lastActivity` is the mtime of the inner `session.json` * file, or null if that file is missing. `artifactCount` is the count * of files under the dir excluding `session.json` itself. */ export declare function discoverSessions(projectRoot: string): SessionEntry[]; /** * 4-tier canonical selection. Tiers evaluated in order; first one that * yields a session id wins. * * 1. active-skill sessionId, if it matches a real entry * 2. entry with the most recent `session.json` mtime * 3. entry with the most recent mtime of any file inside it * 4. entry whose dir name sorts last lexicographically */ export declare function pickCanonicalSession(entries: SessionEntry[], activeSkillSessionId: string | null): { sessionId: string; source: ReconcileResult['canonicalSource']; } | null; /** * Write `.peaks/.session.json` to bind the project to `canonicalSessionId`. * Preserves the projectRoot. The previous binding is returned so the CLI * can surface the re-point delta. */ export declare function repointSessionJson(projectRoot: string, canonicalSessionId: string, repointedFrom: string | null): { repointedFrom: string | null; repointedTo: string; }; /** * Identify deletion candidates. A session is a candidate when: * - the resolved `lastActivity` is older than `ageThresholdMs`, AND * - the dir is "empty or auto-only" (artifactCount === 0, OR the * only file is `session.json` which is auto-generated). * * If `lastActivity` is null (no `session.json` inside), the session's * own dir mtime is used as a fallback so empty dirs without inner * metadata are still fair-game. */ export declare function findDeletionCandidates(entries: SessionEntry[], ageThresholdMs: number): SessionEntry[]; /** * Apply or report deletion of the given candidates. When `apply` is * false, just return `wouldDelete` and do not touch disk. When `apply` * is true, actually `rm -rf` each dir and accumulate any per-dir * errors in the result. */ export declare function applyDeletions(candidates: SessionEntry[], apply: boolean): { deleted: string[]; wouldDelete: string[]; errors: Array<{ sessionId: string; message: string; }>; }; /** * Sync the single `change//` live marker under * `.peaks/_runtime/change/`. The marker is an EMPTY directory (no * symlinks, no manifest, no content). Slice 006 collapses the F3 * per-change-id symlink layer to a single live marker so the * `change/` layer is a single sentinel — easy for the LLM to * navigate, easy for tests to assert on. * * Steps: * 1. Ensure `.peaks/_runtime/change/` exists. * 2. List its entries. * 3. Remove every entry whose name is NOT `/` * (no-op for those whose name matches). * 4. If `/` is missing, create it (empty). * 5. Return the diff for telemetry. * * Path-traversal guards: the canonical session id is validated by * the caller (`SESSION_ID_PATTERN` in the session-manager helper). * The function only ever operates under the project-root-resolved * `.peaks/_runtime/change/` dir, so the resolved paths cannot * escape the project tree. * * @returns `{ removed, created, error }`: * - `removed`: list of entry names that were deleted (e.g. `['/']`). * - `created`: name of the new marker (e.g. `'/'`) or null * when the canonical marker already existed (no-op). * - `error`: error message string or null. */ export declare function syncChangeMarker(projectRoot: string, canonicalSessionId: string): { removed: string[]; created: string | null; error: string | null; }; /** * One-time migration step (added in slice 2026-06-05-peaks-runtime-layer). * * Move the legacy runtime files at: * - `.peaks/.session.json` * - `.peaks/.active-skill.json` * - `.peaks/sop-state/` * into their new canonical home at: * - `.peaks/_runtime/session.json` * - `.peaks/_runtime/active-skill.json` * - `.peaks/_runtime/sop-state/` * * Behavior: * - Idempotent: re-running on a tree that is already on the new * layout produces `migratedFiles: []`. * - Best-effort: uses `fs.renameSync` (atomic on POSIX, best-effort * on Windows) and falls back to `copyFileSync` + `unlinkSync` if * rename throws (e.g. cross-device move on Windows). Errors are * collected per file and returned in the `errors` array so the * reconcile envelope can surface them without blocking the rest of * the migration. * - Creates `.peaks/_runtime/` on demand if any of the old paths * are present. * * @returns `{ migratedFiles, errors }`. `migratedFiles` lists the * *old* relative paths (e.g. `.peaks/.session.json`) that were * successfully moved, in move order. `errors` lists per-file * failures with the old path and a human-readable message. */ export declare function migrateOldRuntimeState(projectRoot: string): { migratedFiles: string[]; errors: Array<{ path: string; message: string; }>; }; /** * One-time sub-agent state migration (slice 2026-06-06-sub-agent-spawn-bug-and-decouple). * * Move the legacy per-session sub-agent state files at: * - `.peaks/_runtime//system/subagent-progress.json` * - `.peaks/_runtime//system/progress-spawn.json` * into the new canonical home at: * - `.peaks/_sub_agents//subagent-progress.json` * - `.peaks/_sub_agents//progress-spawn.json` * * Behavior: * - Idempotent: re-running on a tree that is already on the new layout * produces `migratedFiles: []`. * - Best-effort: uses `fs.renameSync` and falls back to `copyFileSync + * unlinkSync` if rename throws (e.g. cross-device move on Windows). * - Empty `/system/` dir removal (R-2 guard): the legacy `system/` * subdir is only removed when it has zero other files, so a tree where * the user had unrelated content in `system/` is left untouched. * - New-path-wins: when both old and new files exist, the old file is * removed (the new path is authoritative). * * Walks every discovered session — not just the canonical one — so a user * with 6 pre-migration sessions gets all of them migrated in one reconcile * pass. * * @returns `{ migratedFiles, errors }`. `migratedFiles` lists the *old* * relative paths (e.g. `.peaks/_runtime//system/subagent-progress.json`) that * were successfully moved. `errors` lists per-file failures. */ export declare function migrateSubAgentState(projectRoot: string): { migratedFiles: string[]; errors: Array<{ path: string; message: string; }>; }; /** * Top-level orchestrator. Wires migration (added in slice * 2026-06-05-peaks-runtime-layer), discovery, canonical pick, re-point, * deletion-candidate selection, and deletion into a single result. */ export declare function reconcileWorkspace(options: ReconcileOptions): ReconcileResult;