# Storage

Profile data lives in `~/.optchat/profiles/<name>/` (override the root with `OPTCHAT_HOME`). The README has the table of what each path holds.

## Git checkpoints

Each profile folder is a local Git repository, committed after each turn (memory and config; not runs, HTML, or usage). It has no remote, so it is not a backup. To back up, copy the folder while Pi is closed.

## Import generations

An import builds a new memory generation (`memories/<id>/`) and switches `active-memory.json` to it only when the whole tree is ready. Older generations are kept.

## The `Store` seam

Memory reaches disk through one seam, `Store` in `src/core/store.ts` (Pi's `FileStore` is in `src/store.ts`): the log, summary nodes, saved view and images, which is what would travel between machines. The saved view (`view.json`) holds the chat's view and the compactions' view, each with its batch state; an older one without the compactions' view refolds it from the chat's, once.

- Runs, recovery journals, imports and their pointer, usage, settings and `AGENTS.md` stay local files.
- Pi uses `FileStore` (the layout in the README). `pi-optchat/durable` has `SqliteStore`, for a Durable Object's SQLite ([durable.md](durable.md)). Another, such as Postgres, would implement the same few operations.
- Its lease (take, heartbeat, release) is optional: without one, OptChat keeps its machine-local profile lock.
- Import records (`imports/*.json`, the staging generations and the `active-memory.json` pointer) are per machine, so a second machine with the same `~/.claude` or Codex history on disk may offer those sessions for import again.

## Reading profiles from another app

`pi-optchat/files` lets another app on the same machine, such as a GUI, read profiles while Pi runs them, and change them. Only the holder of a profile's lock writes it, except AGENTS.md (Pi reads it fresh each turn): `updateConfig` takes the lock itself or goes through the Pi that holds it. It loads only Node at runtime. Like `./core`, it ships compiled; its declarations name pi-ai's types, so without pi-ai installed, use `skipLibCheck`.

- `dataHome()`, `listProfiles(home?)`, `profilePath(name, home?)`: the data directory (`OPTCHAT_HOME` or `~/.optchat`), its profiles, and one's directory. `home` defaults to `dataHome()`.
- `createProfile(name, home?)`: a new profile with the default config and AGENTS.md, as `/optchat profile` creates one. It throws if the profile exists or the name isn't valid.
- `loadConfig(profileDir)`: the profile's `ProfileConfig`: every setting in `SETTINGS` (label, description, range, default and when a change applies), the `compactor` and `subagent` models, and `main`, the main model last picked in Pi. `defaults` is a new profile's config. It throws on a file it can't read, such as one a newer OptChat wrote with a value this copy refuses.
- `updateConfig(profileDir, patch)`: changes settings and the `compactor` or `subagent` model (`{ provider, model, thinking }`), as `/optchat settings` does, and returns the config as it now is. While a Pi owns the profile, that Pi checks and applies the change at once; a model must be one it is logged in to (Pi's RPC `get_available_models` lists them). With no owner it writes config.json under the profile's lock, keeping keys it doesn't know, and can check a model's shape only. It throws the reason a change can't be made: a bad value, an unknown key, `main` (set it in Pi, for example RPC `set_model`, and Pi saves it), an import in progress, or an owner that takes no changes (`pi -p`).
- `readInstructions(profileDir)`, `saveInstructions(profileDir, text)`: the profile's AGENTS.md. A save applies from the next turn and to new subagents.
- `memoryDirectory(profileDir)`: the generation `active-memory.json` points at, or the profile itself.
- `readMemory(memoryDir)`: a closed `Memory` with history, view, zoom, search and images as the files are now. It creates no directory, builds no summary and takes no lock. A directory that doesn't exist reads as an empty memory. A line Pi is still writing is skipped; read again for later messages.
- `readRuns(profileDir)`: the profile's subagents and connected windows as Pi saved them (`RunInfo[]`: task, state, timestamps, parent, transcript path and so on). A record that can't be read is left out, and a profile without runs gives `[]`. Transcripts are Pi session files: `sessionFile` when the profile has one, and for a connected window running in its own Pi, `window.session` until the owner copies it to `sessionFile` when the window ends. Both are optional. A window with `window.after` shares that session with earlier conversations; its own messages are those with `timestamp >= after`.
- `readUsage(profileDir)`: the profile's usage ledger as it is on disk, `{ entries, warnings }`, each entry a `UsageEntry` (date, role, model, tokens and cost). It writes nothing; a record Pi is still writing is skipped, and `warnings` says how many lines can't be read. `selectUsage(entries, range, now?, session?)` picks a range of `USAGE_RANGES` (`This session` is the Pi session named, and nothing without one) and `summarizeUsage(entries)` sums it into a total and a row per role and model, and `cacheShare(usage)` is the percent of a total's input that was read from the cache: the same code `/optchat usage` shows its numbers with, so an app that uses them shows the same ones.
- `readLimits(profileDir)`: how much of a subscription's limits is used, as the provider said with its latest answer to this profile, or `undefined` when none was saved (or the file is not one). `Limits` is `{ at, limits, overage, refused? }`: each `Limit` is a window of `LIMIT_WINDOWS` (`5h`, `7d`) with the share `used` (1 is all of it), when it `resets` and whether it is `reached`; `overage` says requests past a reached limit go through as extra usage; `refused` is when a request was last turned away for a rate limit (noted only on limits already saved), and is gone with the next answer. Dates are ISO. Only Anthropic says this, to a subscription's login, in the headers of each answer: the main chat's, a subagent's and the compactor's all save it. Only the Pi that holds the profile writes, so a chat in a connected window that runs in its own Pi saves nothing: the snapshot is then as old as the owner's latest answer. An API key gets no such headers, and Pi hands on none from OpenAI or Codex on its usual connection, so those save nothing. Only the latest is kept, so check `at` and `resets` before showing it as current.
- `readImage(memoryDir, name)`: one kept image, `{ mimeType, data }`, without reading the memory.
- `fingerprint(memoryDir)`: a string that changes whenever the memory does, so an app knows when to read it again.
- `profileOwner(profileDir)`: the Pi holding the profile, `{ profile, pid, host, cwd }`, or `undefined`. It asks the lock and never takes it. It rejects only when a holder is there but doesn't answer in 1.5 s, or describes itself another way: either way the profile is busy.

It can also reach a profile's subagents through the Pi that owns it, over that Pi's connected-window socket, as the user would from the Agents page:

- `attachAgent(profileDir, id, onEvent, onDisconnect?)`: opens any subagent by its exact ID (a finished one too, from `readRuns`), or throws. Its conversation so far, then live, arrives as `WindowEvent`s; a `status` event carries `LiveState` (state, model, the streaming reply, running tools, queued messages). The handle has `say(text)` (a message from the user to a live agent), `resume(text)` (the same to a finished one, which goes on in its earlier conversation as the main agent's `tell` would resume it; only the main agent's own subagents resume, and a nested one throws "Only <parent> can resume it"), `interrupt()`, `stop()` (it ends and its parent gets the usual "Stopped by the user" report) and `close()`, which detaches only. A finished agent sends a `finished` event, and another one after a resume.
- `resumeCutOff(profileDir)`: resumes the subagents the last close or crash cut off (records with `cutOff`), and tells the main agent once, as interactive Pi does when it opens the profile. An RPC host resumes none by itself, so this is its way to, once its user says so.

It can import history into a profile the way `/optchat import` does, with the app choosing instead of the terminal pickers ([import.md](import.md#from-another-app)):

- `historyRoots(source, home?)`, `scanLocal(source, roots?)`, `scanClaudeMemories(root?)`, `scanChatGPT(path)`: the conversations each source has, `Scan` (`Conversation[]`, newest first, and warnings). They read no profile and call no model. `historyRoots` gives the folders a source keeps under `home` (the user's by default), so an app's tests can scan a folder of their own.
- `previewImport(profileDir, conversations)`: what importing those conversations, from any mix of sources, would add (`added`, `skipped` as already in memory, `warnings`) and cost (`append` and `rebuild`, each with rough source `tokens` and up to how many new `summaries`). It writes nothing.
- `startImport(profileDir, { conversations, mode, chatgpt? })`: the Pi that owns the profile stages the import and runs it; this returns once it runs. `chatgpt` is the path the ChatGPT conversations were scanned from: their contents stay out of the request, and the owner reads them there again.
- `controlImport(profileDir, 'pause' | 'resume' | 'discard')`: pauses the running import, resumes a paused or failed one, or drops a pending one (with no Pi on the profile, a discard happens under its lock).
- `readImport(profileDir)`: `ImportStatus`, at any time and without Pi: `idle` (with `last`, the latest finished import), or `running`, `paused` or `failed` with the import's mode, numbers and `error`. An import whose Pi quit reads `paused`.

## Windows differences

- **Named pipes, not socket files.** The profile lock and the connected-window bridge listen on `\\.\pipe\optchat-<hash>-lock` and `optchat-<hash>-windows`, where the hash comes from the profile path: a Windows pipe has no filesystem entry inside the profile, so the hash is the only thing separating profiles. Pipes are machine-wide and carry no file permissions, so a Windows profile is not permission-protected the way a POSIX socket file (mode 0600) is.
- **File modes and fsync** (profile folder 0700, socket 0600, directory fsync after a rename) are POSIX-only and are skipped on Windows.
