Documentation
¶
Overview ¶
Package opencode is the OpenCode 2.x module. OpenCode keeps every session in one SQLite database, {data}/opencode.db (WAL mode, event-sourced), which its background service, TUI and desktop app share. hopsesh reads it through the Host's SQL, from an allowlist of tables, and never writes it: changes go through OpenCode's own program (opencode api, session import and delete), never starting, stopping or restarting its service. Where the database cannot be read, OpenCode's API and export are read instead.
A session's file, for moves and packages, is made from the database (Materializer): JSON lines at an anchor hopsesh keeps, {data}/hopsesh/opencode/sessions/<id>.jsonl, beside which its lineage sits. Child sessions (subagents) belong to their root session: they are counted, not listed, and travel with it.
Index ¶
- Variables
- type Module
- func (m *Module) Account(ctx context.Context, h agent.Host, in agent.Install) (agent.Account, error)
- func (m *Module) AfterInstall(ctx context.Context, h agent.Host, in agent.Install, key agent.SessionKey, ...) error
- func (m *Module) Backup(ctx context.Context, h agent.Host, in agent.Install, sid agent.SessionID) (string, agent.SessionStamp, error)
- func (m *Module) Bundle(ctx context.Context, h agent.Host, in agent.Install, s agent.Summary) (agent.Bundle, error)
- func (m *Module) ChangeDir(ctx context.Context, h agent.Host, in agent.Install, s agent.Summary, ...) error
- func (*Module) ChangeDirHelp() (how, undo string)
- func (m *Module) CheckInstall(ctx context.Context, h agent.Host, in agent.Install, version string) error
- func (m *Module) ContextCapacity(ctx context.Context, h agent.Host, in agent.Install, s *agent.Summary) (ir.Capacity, error)
- func (*Module) DecodeNoticeHook(raw []byte) (agent.NoticeHookCall, bool)
- func (m *Module) Delete(ctx context.Context, h agent.Host, in agent.Install, sid agent.SessionID, ...) error
- func (m *Module) Detect(_ context.Context, h agent.Host) (agent.Install, error)
- func (*Module) EncodeNoticeHook(_ agent.NoticeHookCall, a agent.NoticeHookAnswer) []byte
- func (m *Module) HookTrust(ctx context.Context, h agent.Host, in agent.Install, _ []string) agent.HookTrust
- func (*Module) InSession(_ agent.Install, dir string) string
- func (*Module) InstallsViaVendor() bool
- func (m *Module) Integration(h agent.Host, in agent.Install) agent.Integration
- func (m *Module) List(ctx context.Context, h agent.Host, in agent.Install) (agent.Listing, error)
- func (m *Module) Live(ctx context.Context, h agent.Host, in agent.Install, ids []agent.SessionID) (map[agent.SessionID]agent.LiveInfo, error)
- func (m *Module) Materialize(ctx context.Context, h agent.Host, in agent.Install, s agent.Summary, ...) ([]byte, error)
- func (m *Module) MovementHooks(h agent.Host, in agent.Install, bin string, args ...string) agent.HookIntegration
- func (m *Module) NativeParentGone(ctx context.Context, h agent.Host, in agent.Install, s agent.Summary) (bool, error)
- func (*Module) NewSessionID(_ agent.Install, now time.Time) agent.SessionID
- func (m *Module) PlanChangeDir(ctx context.Context, h agent.Host, in agent.Install, s agent.Summary, ...) (agent.DirPlan, error)
- func (m *Module) PlanMove(_, _ agent.Install, s agent.Summary, b agent.Bundle, p agent.Placement) (agent.MovePlan, error)
- func (m *Module) PrepareAccount(ctx context.Context, h agent.Host, in agent.Install, o agent.AccountPrep) (agent.AccountPrepared, error)
- func (m *Module) Preview(ctx context.Context, h agent.Host, in agent.Install, s agent.Summary, n int) (agent.Preview, error)
- func (*Module) Profile(agent.Install) ir.Profile
- func (m *Module) ProjectSanitized(source, target ir.Segment) ([]ir.Projection, error)
- func (m *Module) Read(ctx context.Context, h agent.Host, in agent.Install, s agent.Summary, ...) (ir.Segment, error)
- func (m *Module) RecoverWrite(ctx context.Context, h agent.Host, in agent.Install, req ir.WriteRequest) (ir.WriteResult, error)
- func (m *Module) Rename(ctx context.Context, h agent.Host, in agent.Install, s agent.Summary, ...) error
- func (m *Module) Restore(ctx context.Context, h agent.Host, in agent.Install, sid agent.SessionID, ...) (agent.SessionStamp, error)
- func (m *Module) Resume(in agent.Install, key agent.SessionKey, p agent.Placement, ...) agent.Command
- func (m *Module) ResumeOn(ctx context.Context, h agent.Host, in agent.Install, key agent.SessionKey, ...) (agent.Command, error)
- func (*Module) Sanitize() agent.RewritePolicy
- func (m *Module) SessionStamp(ctx context.Context, h agent.Host, in agent.Install, sid agent.SessionID) (agent.SessionStamp, error)
- func (m *Module) SessionWatchPaths(in agent.Install, pa agent.Path) []string
- func (*Module) Spec() agent.Spec
- func (m *Module) StartProgram(ctx context.Context, h agent.Host, in agent.Install, restart bool) (string, error)
- func (m *Module) Stop(ctx context.Context, h agent.Host, in agent.Install, s agent.Summary, ...) error
- func (*Module) Verify(ctx context.Context, h agent.Host, mp agent.MovePlan, staged map[string]string, ...) error
- func (m *Module) VerifyNativeFork(ctx context.Context, h agent.Host, in agent.Install, ...) ([]agent.NativeInheritance, error)
- func (*Module) VirtualSessions()
- func (m *Module) Write(ctx context.Context, h agent.Host, in agent.Install, req ir.WriteRequest) (ir.WriteResult, error)
Constants ¶
This section is empty.
Variables ¶
var ( // ErrOwnServer: the turn runs in an OpenCode process with a server of its own // (--standalone), which hopsesh cannot reach. ErrOwnServer = errors.New("the session runs in an OpenCode window with its own server (opencode --standalone); stop it there (Esc)") // ErrSuspended: the background service is not running and the session's row claims a // turn: a window with its own server runs it, or it waits for the service, which // hopsesh never starts. Nothing tells the two apart. ErrSuspended = errors.New("the session's turn runs in an OpenCode window with its own server (opencode --standalone), or it stopped with OpenCode's background service and continues when the service starts again; stop it in that window (Esc), or open OpenCode and stop it there") // ErrStillRunning: the turn did not end within the grace period. ErrStillRunning = errors.New("the OpenCode session is still working; stop it in OpenCode (Esc), then try again") )
Stop errors.
Functions ¶
This section is empty.
Types ¶
type Module ¶
type Module struct{}
Module is the OpenCode module.
func (*Module) Account ¶
func (m *Module) Account(ctx context.Context, h agent.Host, in agent.Install) (agent.Account, error)
Account lists the providers OpenCode is signed in to, from `opencode auth list --format json`, which prints provider names and connection labels and never a secret (decision D7). OpenCode signs in per provider, not per person, so the account has no key: moving a session is never taken as moving it to another account. Its database's credential and account tables are never read.
func (*Module) AfterInstall ¶
func (m *Module) AfterInstall(ctx context.Context, h agent.Host, in agent.Install, key agent.SessionKey, p agent.Placement) error
AfterInstall has OpenCode import the copy the core placed for key: the session, then its children. It is idempotent: a session already there with the placed messages is left as it is, and one there with other messages (work done since a first, interrupted run, or another session at the id) is ErrDiverged.
func (*Module) Backup ¶
func (m *Module) Backup(ctx context.Context, h agent.Host, in agent.Install, sid agent.SessionID) (string, agent.SessionStamp, error)
Backup saves the session and its children as OpenCode exports them: <id>-<time>.json, and each child in <id>-<time>.children/<n>-<child>.json, parents first. The stamp is the session's before and after the export, which must agree.
func (*Module) Bundle ¶
func (m *Module) Bundle(ctx context.Context, h agent.Host, in agent.Install, s agent.Summary) (agent.Bundle, error)
Bundle is the session's file and one for each of its descendants, all made from the database (Virtual). Where nothing runs (a peer's package), the files hopsesh placed are the bundle.
func (*Module) ChangeDir ¶
func (m *Module) ChangeDir(ctx context.Context, h agent.Host, in agent.Install, s agent.Summary, p agent.DirPlan) error
ChangeDir asks OpenCode's background service to move the session and waits until it has: the session's newest location-switched message names the folder.
func (*Module) ChangeDirHelp ¶
ChangeDirHelp says how OpenCode changes a session's folder, and how undo puts it back.
func (*Module) CheckInstall ¶
func (m *Module) CheckInstall(ctx context.Context, h agent.Host, in agent.Install, version string) error
CheckInstall is checkTarget before anything changes here (agent.InstallChecker), for the version that last wrote the session moved; AfterInstall checks every session it imports.
func (*Module) ContextCapacity ¶
func (m *Module) ContextCapacity(ctx context.Context, h agent.Host, in agent.Install, s *agent.Summary) (ir.Capacity, error)
ContextCapacity takes the model from the session's newest reply (for a new session, from the newest reply of any) and its window from the models.dev catalog OpenCode caches in its database. OpenCode's config file holds provider keys, so it is never read (decision D6): a model the catalog does not know (a local provider) gets the fallback window. A session's usage is its newest reply's tokens, plus the bytes of what follows; after a compaction newer than that reply, the bytes from the compaction on. Where the database cannot be read (none yet, another machine's closed WAL, a schema hopsesh does not know), an existing session's usage is unknown.
func (*Module) DecodeNoticeHook ¶
func (*Module) DecodeNoticeHook(raw []byte) (agent.NoticeHookCall, bool)
DecodeNoticeHook reads the plugin's call. OpenCode keeps sessions in its database, so there is no transcript path: the core finds the session's anchor from its id.
func (*Module) Delete ¶
func (m *Module) Delete(ctx context.Context, h agent.Host, in agent.Install, sid agent.SessionID, expect agent.SessionStamp) error
Delete deletes the session, and with it its children, through OpenCode, only while its stamp is expect. The stamp is read last, right before the delete, so that the running service has as little time as it can to add a turn in between (it still can: OpenCode's delete names no state).
func (*Module) Detect ¶
Detect resolves the roots and, on a Mac, the desktop app. The "db" root names the database when it is not {data}/opencode.db: OPENCODE_DB's, or another channel's.
func (*Module) EncodeNoticeHook ¶
func (*Module) EncodeNoticeHook(_ agent.NoticeHookCall, a agent.NoticeHookAnswer) []byte
EncodeNoticeHook writes the verdict: a block with its reason (on any event: the plugin refuses tool calls, not prompts), or a notice for the model.
func (*Module) HookTrust ¶
func (m *Module) HookTrust(ctx context.Context, h agent.Host, in agent.Install, _ []string) agent.HookTrust
HookTrust asks OpenCode's running service whether it loaded the hopsesh plugin. OpenCode asks no one before it loads a plugin folder, but a plugin entry in an OpenCode config ("-hopsesh" or "-*"; a selector "x.*" matches only ids that start with "x.", which "hopsesh" does not) removes it, a policy (plugin.use) can deny it, and a plugin that fails to load is listed as failed. hopsesh never reads OpenCode's config (it holds provider keys): the service's list says what it loaded. With the service stopped nothing can be asked (a private server answers before it loads plugins), so trust is unknown until OpenCode runs.
func (*Module) InstallsViaVendor ¶
InstallsViaVendor reports that OpenCode's import is the install.
func (*Module) Integration ¶
Integration says OpenCode reads skills from ~/.agents/skills (also ~/.claude/skills, where hopsesh puts Claude Code's copy). Its approval rules for hopsesh's commands are a file hopsesh owns beside the plugin, {config}/.hopsesh/rules.json, which the plugin applies (permission "evaluate"); OpenCode's own config is never edited. Without the plugin (hopsesh notices install) the rules do nothing.
func (*Module) List ¶
List lists the root sessions: child sessions (subagents) are counted on their root, as OpenCode's own list shows them; archived sessions are of the kind "archived". The database is checked first for every column the module reads (as every read does).
func (*Module) Live ¶
func (m *Module) Live(ctx context.Context, h agent.Host, in agent.Install, ids []agent.SessionID) (map[agent.SessionID]agent.LiveInfo, error)
Live says which sessions run a turn now (or have one suspended). The rows answer alone unless one of the sessions has a turn in flight (or they cannot be read): only then is OpenCode asked, since each question is a process, and a service that stops between the probe and the API call would be started again by that call.
func (*Module) Materialize ¶
func (m *Module) Materialize(ctx context.Context, h agent.Host, in agent.Install, s agent.Summary, f agent.BundleFile) ([]byte, error)
Materialize is a bundle file's bytes: the session (or the child session the file is) as JSON lines, for a move or a package. They are made from OpenCode's own export where its program runs, since that is what its import reads: the session's permission rules (a column the module cannot name) and the media OpenCode keeps apart (the database holds only "media:" references, which an import refuses) are in it. Where the program does not run, they are made from the database, and a session with media is refused.
func (*Module) MovementHooks ¶
func (m *Module) MovementHooks(h agent.Host, in agent.Install, bin string, args ...string) agent.HookIntegration
MovementHooks is the plugin folder, its notice hook run with argv (no shell: the plugin spawns the program with its arguments).
func (*Module) NativeParentGone ¶
func (m *Module) NativeParentGone(ctx context.Context, h agent.Host, in agent.Install, s agent.Summary) (bool, error)
NativeParentGone reports a fork whose parent OpenCode no longer has: an OpenCode fork holds its whole inherited history, so it is a conversation of its own then.
func (*Module) NewSessionID ¶
NewSessionID mints a session id in OpenCode's form, for a copy kept beside the original or a session written from another agent.
func (*Module) PlanChangeDir ¶
func (m *Module) PlanChangeDir(ctx context.Context, h agent.Host, in agent.Install, s agent.Summary, dir string) (agent.DirPlan, error)
PlanChangeDir has OpenCode's background service move the session (DirAgent); its anchor stays where it is.
func (*Module) PlanMove ¶
func (m *Module) PlanMove(_, _ agent.Install, s agent.Summary, b agent.Bundle, p agent.Placement) (agent.MovePlan, error)
PlanMove places the session's file at the target's anchor for its id (a keep-both copy renamed), and its children's files beside it, rewritten with the module's policy.
func (*Module) PrepareAccount ¶
func (m *Module) PrepareAccount(ctx context.Context, h agent.Host, in agent.Install, o agent.AccountPrep) (agent.AccountPrepared, error)
PrepareAccount gives a new account folder its own service port: the first one after OpenCode's default that no other account folder was given and nothing listens on.
func (*Module) Preview ¶
func (m *Module) Preview(ctx context.Context, h agent.Host, in agent.Install, s agent.Summary, n int) (agent.Preview, error)
Preview reads the end of a session: its newest messages from the database (a window that grows until it holds n messages), or the whole session from OpenCode's export, and its first prompt.
func (*Module) Profile ¶
Profile is the fallback window (the core reads a write's real one from ContextCapacity), shell calls replayed as OpenCode's own, and no appends to an existing session (decision D4).
func (*Module) ProjectSanitized ¶
ProjectSanitized maps a sanitized copy's nodes to the source's: the sanitizer drops reasoning items only and keeps everything else in order, so the copy is the source without its reasoning nodes.
func (*Module) Read ¶
func (m *Module) Read(ctx context.Context, h agent.Host, in agent.Install, s agent.Summary, from ir.Cursor) (ir.Segment, error)
Read lifts a session into IR, from the database, else from OpenCode's export, or where nothing runs (a peer's package) from the session's JSON lines at s.Path; a copy the core staged (s.Path not the session's anchor) from that file only. Its cursor counts settled messages, the same in every mode, and its head is the last node read.
func (*Module) RecoverWrite ¶
func (m *Module) RecoverWrite(ctx context.Context, h agent.Host, in agent.Install, req ir.WriteRequest) (ir.WriteResult, error)
RecoverWrite finishes a Write that was interrupted: it finds the session by the id Write gave it, checks the file Write placed is the one the request makes (writing it when Write had not), imports it when OpenCode does not have it yet, and returns the result Write would have. A session there with other messages was used since: ErrDiverged.
func (*Module) Rename ¶
func (m *Module) Rename(ctx context.Context, h agent.Host, in agent.Install, s agent.Summary, title string) error
Rename gives the session a new title the way OpenCode's own rename does (PATCH /api/session/<id>), so OpenCode and its desktop app show it. OpenCode names a session only while it is untitled, so the title stays.
func (*Module) Restore ¶
func (m *Module) Restore(ctx context.Context, h agent.Host, in agent.Install, sid agent.SessionID, backup string) (agent.SessionStamp, error)
Restore imports a Backup again, the session first and each child after its parent, each in the folder it was in. A session already there with the backup's messages is left (a restore that stopped halfway is finished); with others, it is newer work, and ErrDiverged.
func (*Module) Resume ¶
func (m *Module) Resume(in agent.Install, key agent.SessionKey, p agent.Placement, o agent.ResumeOptions) agent.Command
Resume is `opencode -s <id> [--prompt …]` in the session's folder. It never forks (no CapFork yet). The program is the PATH opencode; ResumeOn picks one of the background service's version and checks the session is there first.
func (*Module) ResumeOn ¶
func (m *Module) ResumeOn(ctx context.Context, h agent.Host, in agent.Install, key agent.SessionKey, p agent.Placement, o agent.ResumeOptions) (agent.Command, error)
ResumeOn is Resume after checking the machine: the session must be there (OpenCode's -s makes an empty session for an id it does not have), and the program must be of the running background service's version, or the TUI would restart the service (D2): that is agent.ErrRestartsService, unless the person agreed (o.RestartService).
func (*Module) Sanitize ¶
func (*Module) Sanitize() agent.RewritePolicy
Sanitize is the extra policy for a move to a machine signed in to another account: reasoning items, which a provider may bind to the account that produced them, are dropped from every assistant message. The rest keeps its order.
func (*Module) SessionStamp ¶
func (m *Module) SessionStamp(ctx context.Context, h agent.Host, in agent.Install, sid agent.SessionID) (agent.SessionStamp, error)
SessionStamp is a session's settled-message count and last update, the same from the database and from OpenCode's export. A session that is not there is ErrNotFound.
func (*Module) SessionWatchPaths ¶
SessionWatchPaths are the database Detect chose and its WAL (every change lands there) and hopsesh's own files; never logs, snapshots or the config.
func (*Module) StartProgram ¶
func (m *Module) StartProgram(ctx context.Context, h agent.Host, in agent.Install, restart bool) (string, error)
StartProgram is the program a new session runs: as for a resume, one of the running background service's version (the TUI would restart it otherwise, D2), unless the person agreed (restart). The PATH opencode is in.Binary.
func (*Module) Stop ¶
func (m *Module) Stop(ctx context.Context, h agent.Host, in agent.Install, s agent.Summary, grace time.Duration) error
Stop interrupts the session's turn through the background service (POST /api/session/<id>/interrupt) and waits for it to end. It never signals a process: the service serves every session, its desktop app and other windows, and a TUI attached to it is only a client.
func (*Module) Verify ¶
func (*Module) Verify(ctx context.Context, h agent.Host, mp agent.MovePlan, staged map[string]string, p agent.Placement) error
Verify checks the staged copy reads as OpenCode sessions: the session at the placement's id, and each child file the child it is named for, hanging from the family.
func (*Module) VerifyNativeFork ¶
func (m *Module) VerifyNativeFork(ctx context.Context, h agent.Host, in agent.Install, parent, child agent.Summary) ([]agent.NativeInheritance, error)
VerifyNativeFork checks that child is a fork of parent: its row names parent, and its messages up to the boundary read as the parent's, message by message (by seq).
func (*Module) VirtualSessions ¶
func (*Module) VirtualSessions()
VirtualSessions marks OpenCode's sessions as rows of a database, each one's size a count of messages.
func (*Module) Write ¶
func (m *Module) Write(ctx context.Context, h agent.Host, in agent.Install, req ir.WriteRequest) (ir.WriteResult, error)
Write writes a new OpenCode session from the request: its transfer at the session's anchor, imported by OpenCode's program under the id the core chose (req.SessionID), or, without one in OpenCode's form, an id made from the operation's.