Skip to content
pi-c Documentation
Login

Documentation

/* Rewind (rewind.c): go back to an earlier user message of a conversation
 * and restore the conversation, the files write and edit changed after it,
 * or both, as Claude Code's checkpoints do. A bundled component over the
 * file history core records (filelog.c); see docs/manual/rewind.md. */
#ifndef PIC_REWIND_H
#define PIC_REWIND_H

#include <stdio.h>

#include "pic.h"
#include "rpc.h"

enum { PIC_REWIND_BOTH, PIC_REWIND_CONVERSATION, PIC_REWIND_FILES };

typedef struct {
    int applied;           /* 0: files were refused and nothing was done */
    char *text;            /* the message rewound to, for the editor (malloc'd) */
    int switched;          /* the conversation was rewound: now in conversation */
    char conversation[64]; /* "" a new, empty one */
    pic_restore_result restore;
    struct pic_rewind_refusal {
        char *path;
        const char *why;
    } *refused;
    size_t nrefused;
} pic_rewind_report;

/* SESSION's user messages, newest first, as JSON lines: entry, text, and
 * files (how many files write and edit changed after it). */
int pic_rewind_points(pic_store *s, const char *session, FILE *out);
/* Rewinds SESSION (an id, prefix or "latest") to before its user message
 * ENTRY in MODE. A file changed since pic last wrote it, or whose earlier
 * bytes were not kept, is refused; with any refused, nothing is done unless
 * FORCE_PARTIAL, which restores the rest. 0 (see r->applied), or -1. */
int pic_rewind(pic_store *s, const char *session, sqlite3_int64 entry, int mode, int force_partial, pic_rewind_report *r);
/* Undoes the latest file restore of SESSION (an undo's too: a redo). */
int pic_rewind_undo(pic_store *s, const char *session, int force_partial, pic_rewind_report *r);
/* What SESSION's write and edit calls changed, along its path (a fork's origin's too, and the
 * helpers it started in this journal), each file from before the first of them to what it holds
 * now, as JSON (sqlite3_malloc'd): {"files":[{"path" (relative to WORKSPACE when inside it),
 * "status" (added, modified, deleted), "added" and "removed" (lines) and "diff" (the unified
 * diff's hunks), or "why" (binary, not kept, too large, not a file)}]}, with "cut":true past 200
 * files, past STOP_MS (CLOCK_MONOTONIC ms; 0: no limit), or where more would not fit 3 MiB. A
 * file as it was before is left out; changes made by bash or outside pic are not tracked. NULL
 * with s->err when SESSION is not found. */
char *pic_rewind_changes(pic_store *s, const char *session, const char *workspace, long long stop_ms);
/* The report for a person, paths shown relative to WORKSPACE, or as JSON. */
void pic_rewind_print(const pic_rewind_report *r, const char *workspace, int json, FILE *out);
void pic_rewind_report_free(pic_rewind_report *r);

/* The RPC command rewind (rpc.h's component commands). */
extern const pic_rpc_command pic_rewind_rpc;

#endif