Documentation
¶
Overview ¶
Package sessions finds the conversations agents saved for a directory, so conch can list and resume them. Each agent keeps its own store; this package only reads them:
- Claude Code: ~/.claude/projects/<encoded dir>/<session id>.jsonl
- Codex: ~/.codex/sessions/YYYY/MM/DD/rollout-*.jsonl (session_meta first)
- Gemini CLI: ~/.gemini/tmp/<project>/chats/session-*.json
- OpenCode: ~/.local/share/opencode/opencode.db (sqlite), or the older storage/session/*/*.json files
- Copilot CLI: ~/.copilot/session-state/<session id>/events.jsonl
- Cline: ~/.cline/data/sessions/<id>/<id>.json and <id>.messages.json
- Qwen Code: ~/.qwen/projects/<encoded dir>/chats/<session id>.jsonl
- Goose: ~/.local/share/goose/sessions/sessions.db (sqlite)
Index ¶
- func AgentName(agent string) string
- func CodexRollout(e Env, id string) string
- func ConductorDB(home string) string
- func Delete(ctx context.Context, e Env, s Session, trashDir string) error
- func Handoff(s Session, turns []Turn) string
- func Key(s Session) string
- func Match(ctx context.Context, e Env, list []Session, query string) map[string]string
- type Account
- type ChatTurn
- type ChatUpdate
- type ClaudeWriter
- func (ClaudeWriter) Agent() string
- func (w ClaudeWriter) Append(ctx context.Context, im Imported, dir, sid, path, after string) (int, error)
- func (w ClaudeWriter) Check(ctx context.Context, im Imported, dir string) error
- func (w ClaudeWriter) Filed(path, cwd string) bool
- func (w ClaudeWriter) Folder(cwd string) string
- func (w ClaudeWriter) Move(path, cwd string) (string, error)
- func (w ClaudeWriter) Original(id string) (path, cwd string)
- func (w ClaudeWriter) Write(ctx context.Context, im Imported, dir string) (string, string, error)
- type Env
- type ImportOutcome
- type ImportRecord
- type ImportState
- type ImportStatus
- type Imported
- type ImportedMessage
- type Session
- type SessionAppender
- type SessionWriter
- type Turn
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CodexRollout ¶ added in v0.2.1
CodexRollout is the file Codex keeps thread id in, in this environment's Codex home (CODEX_HOME, else ~/.codex), or "" when it has none: rollout-TIME-ID.jsonl under its sessions folder. Only that home, since `codex resume` started by conch reads only it.
func ConductorDB ¶ added in v0.2.1
ConductorDB is where Conductor keeps its database on a Mac.
func Delete ¶
Delete removes a saved session: files move to trashDir (so a mistake can be undone), OpenCode's database entries are deleted through its CLI.
func Handoff ¶
Handoff renders a conversation as a Markdown document another agent can read to continue the work. Very long messages are cut, and when the whole conversation is too long the oldest messages are left out (the first request is always kept).
Types ¶
type Account ¶ added in v0.2.1
type Account struct {
Agent, Name, Dir string
}
Account is one of an agent's other accounts: its configuration folder.
type ChatTurn ¶ added in v0.2.0
type ChatTurn struct {
Seq int // its place in the whole conversation, from 0; stable as turns are added
Role string // "user", "assistant", "tool" or "note"
Text string // for a tool, its name
Detail string // for a tool, what it was used on, on one line: a file, a command
Input string // for a tool, what it was given, as a chat shows it
InputKind string // "diff" (lines marked - and +), "code", or "" for text
Output string // for a tool, what came back
Error bool // the tool failed
Done bool // the tool's result has come
Time time.Time
// contains filtered or unexported fields
}
ChatTurn is one entry of a conversation as a chat draws it: something the user typed, something the agent said, a tool it used — with what it was given and what came back — or a note (an interruption).
type ChatUpdate ¶ added in v0.2.0
ChatUpdate is what a chat is sent: the turns that are new or changed since the version it has, or — Reset — the newest turns to start again from, when it has none, or another log's (a new conversation, a file rewritten, a server restarted).
type ClaudeWriter ¶ added in v0.2.1
type ClaudeWriter struct {
Env Env
}
ClaudeWriter writes Claude Code sessions: one JSONL file in the project folder Claude keeps for dir, under $CLAUDE_CONFIG_DIR/projects or ~/.claude/projects, which `claude --resume ID` opens with its history.
Conductor kept each of the agent's stream messages (the Claude Agent SDK's) whole, so a line is that message with what Claude Code adds around it: a uuid chained to the line before by parentUuid, the session, the time, the folder and branch. A subagent's messages (the SDK's parent_tool_use_id) are sidechains with a chain of their own, as Claude Code keeps them out of the conversation it resumes.
func (ClaudeWriter) Agent ¶ added in v0.2.1
func (ClaudeWriter) Agent() string
Agent is the agent whose sessions it writes.
func (ClaudeWriter) Append ¶ added in v0.2.1
func (w ClaudeWriter) Append(ctx context.Context, im Imported, dir, sid, path, after string) (int, error)
Append adds the messages after the one with id after to the session at path, which an earlier Write made from the same conversation. The lines already written are rebuilt from the messages up to after, so the new ones chain on from them; the file is put back as it was if the write fails part way.
func (ClaudeWriter) Check ¶ added in v0.2.1
Check says whether Write would write im, writing nothing.
func (ClaudeWriter) Filed ¶ added in v0.2.1
func (w ClaudeWriter) Filed(path, cwd string) bool
Filed says whether the session at path is in the project folder Claude Code reads for cwd: the only one `claude --resume` run there looks in.
func (ClaudeWriter) Folder ¶ added in v0.2.1
func (w ClaudeWriter) Folder(cwd string) string
Folder is cwd's project folder, made or not.
func (ClaudeWriter) Move ¶ added in v0.2.1
func (w ClaudeWriter) Move(path, cwd string) (string, error)
Move puts the session at path into cwd's project folder, unchanged — one carried on keeps everything — so that `claude --resume` run in cwd finds it: a session written for a worktree since removed is otherwise found nowhere. A file already there by that name is never written over.
func (ClaudeWriter) Original ¶ added in v0.2.1
func (w ClaudeWriter) Original(id string) (path, cwd string)
Original is Claude Code's own session id, as Claude Code wrote it while another app ran it (Conductor runs Claude Code with the usual config folder): its file under any project folder, and the folder it ran in, read from its lines. Nothing for an id it has no file of, one that is not a plain name, or a file that names no folder.
type Env ¶
type Env struct {
Home string
Getenv func(string) string
// Accounts are the agents' other accounts (internal/accounts), whose
// folders hold conversations of their own.
Accounts []Account
}
Env is where agents keep their data.
type ImportOutcome ¶ added in v0.2.1
type ImportOutcome struct {
Status ImportStatus
AgentSessionID string
Path string
Added int // messages added, for ImportAdded
Why string // for ImportCarriedOn and ImportSkipped
Err error // for ImportFailed
}
ImportOutcome is what ImportResumable did with one conversation.
func ImportPlan ¶ added in v0.2.1
func ImportPlan(im Imported, st ImportState) ImportOutcome
ImportPlan says what ImportResumable would do with im, writing nothing: ImportWritten for a conversation not imported before (which may yet fail), ImportAdded with the messages it would add, or why it would leave it alone. Cheap: no session is read, only stat'ed.
func ImportResumable ¶ added in v0.2.1
func ImportResumable(ctx context.Context, im Imported, w SessionWriter, st ImportState, now time.Time) ImportOutcome
ImportResumable makes im a session of its agent through w, or brings the one an earlier import wrote up to date, and records it in st:
- a conversation not imported before is written;
- one with new messages gets them only if the session file is as conch left it; one the person carried on with is left alone and said to be;
- nothing is ever deleted, nor written again once removed.
A conversation that cannot be written comes back ImportFailed, and st is left as it was for it.
type ImportRecord ¶ added in v0.2.1
type ImportRecord struct {
Agent string `json:"agent"`
AgentSessionID string `json:"agentSessionID"`
Path string `json:"path"`
// LastMessageID is the last of the conversation's rows the session
// holds; Size is the session file's size once conch wrote it, which
// it is still only if the agent has not written to it since.
LastMessageID string `json:"lastMessageID"`
ImportedAt time.Time `json:"importedAt"`
Size int64 `json:"size"`
// Dir is the folder it was written to resume in: the one the person
// chose (-dir) for a conversation whose own folders are gone, which
// nothing in the other app's database says. Older states have none.
Dir string `json:"dir,omitempty"`
}
ImportRecord is what an import wrote for one conversation, so that importing again adds only what is new, and never writes over what the person did with the session since.
type ImportState ¶ added in v0.2.1
type ImportState map[string]ImportRecord
ImportState maps the other app's conversation id to what was written for it: $CONCH_HOME/imports/conductor/state.json for Conductor.
func LoadImportState ¶ added in v0.2.1
func LoadImportState(path string) (ImportState, error)
LoadImportState reads the state at path; none there is an empty one. A file that cannot be read is an error, not an empty state: saved over, it would have every conversation imported a second time.
func (ImportState) Save ¶ added in v0.2.1
func (st ImportState) Save(path string) error
Save writes the state to path whole, through a file beside it.
type ImportStatus ¶ added in v0.2.1
type ImportStatus string
ImportStatus is what became of one conversation.
const ( ImportWritten ImportStatus = "written" // a new session ImportAdded ImportStatus = "added" // new messages added to the session written before ImportUnchanged ImportStatus = "unchanged" // imported before, nothing new ImportCarriedOn ImportStatus = "carried-on" // the session changed since: left alone ImportSkipped ImportStatus = "skipped" // nothing said in it ImportFailed ImportStatus = "failed" // not resumable; Err says why // ImportFound: the agent kept the conversation itself (Codex's own // rollout, written as Conductor ran it), so it resumes with nothing // written. ImportFound ImportStatus = "found" )
type Imported ¶ added in v0.2.1
type Imported struct {
Session Session
Repo string // the repository's name
Root string // the repository's folder
// Workspace is the folder the conversation ran in, which may be gone:
// Session.Dir is the nearest of it and Root that is still there.
Workspace string
// AgentSession is the agent's own id for the conversation, as
// Conductor kept it (its claude_session_id column, for every agent):
// a Codex thread id, whose rollout Codex itself wrote.
AgentSession string
Turns []Turn
// Messages are the conversation's rows as they were stored, in order:
// what a writer needs to make a session the agent resumes, which the
// handoff (Turns) leaves out — tool calls, their results, ids.
Messages []ImportedMessage
// Unread counts the agent's messages that were neither text nor a
// tool call conch knows to leave out.
Unread int
}
Imported is one conversation read from another app's store.
func ReadConductor ¶ added in v0.2.1
ReadConductor reads every conversation in a Conductor database, oldest first. The database is copied before it is read, with its write-ahead log, so a Conductor that is still open is neither locked nor written to.
type ImportedMessage ¶ added in v0.2.1
type ImportedMessage struct {
ID string
Role string // "user" or "assistant", as the row said
// Content is the row as stored: a user's prompt as typed, or one
// message of the agent's stream as JSON.
Content string
Time time.Time
}
ImportedMessage is one row of a conversation as the other app kept it.
type Session ¶
type Session struct {
Agent string
ID string
Dir string // where it ran
Branch string // when the agent recorded it
Title string
Started time.Time
Updated time.Time
// Path is the agent's session file, or its database (OpenCode).
Path string
// Cost is what the agent said the conversation cost, and Output the
// tokens it generated; both 0 when the store doesn't say. Read from
// the tail of the store, so listing stays cheap.
Cost float64
Output int
// Account is the agent's account whose folder holds it, "" for the
// agent's own.
Account string
// contains filtered or unexported fields
}
Session is one saved agent conversation.
func List ¶
List returns the sessions that ran in any of dirs (or below them), newest first, at most limit. It asks every store itself and waits for the answer: callers of this one — search, sharing, usage — want what is there now, not what a store said a moment ago.
func ListStatus ¶ added in v0.1.3
ListStatus is List, and whether every store answered. complete is false when an agent that keeps its sessions behind another program (see slow.go) was too slow: that agent's sessions are missing from the list, which is worth asking for again in a moment.
type SessionAppender ¶ added in v0.2.1
type SessionAppender interface {
Append(ctx context.Context, im Imported, dir, agentSessionID, path, after string) (added int, err error)
}
SessionAppender adds what was said since an earlier import to the session that import wrote. after is the id of the last message that import wrote; added counts the messages it adds now.
type SessionWriter ¶ added in v0.2.1
type SessionWriter interface {
Agent() string
Write(ctx context.Context, im Imported, dir string) (agentSessionID, path string, err error)
}
SessionWriter makes a conversation imported from another app into a session of its agent's own, which that agent resumes as if it had written it. dir is the folder it resumes in: the workspace it ran in if that is still there, else the repository.