Documentation
¶
Overview ¶
Package sessionlink joins a session Caprock started to the record the agent's own files keep of it.
Claude Code and Gemini CLI take an id on the command line, so the session Caprock starts and the session their transcript or telemetry describes are one id from the first byte. Codex and OpenCode cannot be told one. Codex names its thread itself; OpenCode creates its session when the first message is sent. Their importers find those sessions under the agent's own id, and without a link a session started from the dashboard would appear twice — once as a terminal with no cost, once as a cost with no terminal.
A link is made one of two ways, and the difference is the whole point of keeping them apart:
- Claim is exact. OpenCode's TUI runs a server on a port Caprock chose, and that server announces `session.created` for the session the TUI made. Nothing else can be on that port.
- Match is a heuristic, for Codex, which writes no id anywhere Caprock can see before the rollout appears. A rollout is matched to a spawn when it was written by the Codex TUI (not the desktop app, not a subagent, not an import), in the same directory, by a thread that started within the window after the spawn. Codex stamps a thread with the moment the TUI started, not the moment its file was first written (measured on 14 CLI rollouts: up to 4m40s apart), so the window can be tight even though the file appears whenever the user first types.
Once made, a link is stored on the session (sessions.native_id) and the importers write the thread's events under Caprock's id.
Index ¶
- Constants
- type Candidate
- type Linker
- func (l *Linker) Claim(ctx context.Context, agent, sessionID, nativeID string) bool
- func (l *Linker) Ended(sessionID string)
- func (l *Linker) Expect(agent, sessionID, cwd string, at time.Time, match bool)
- func (l *Linker) Resolve(ctx context.Context, agent string, c Candidate) string
- func (l *Linker) Waiting(agent string) bool
Constants ¶
const Grace = time.Minute
Grace is how long an expectation outlives the process that set it. A session that is typed into and closed within one import tick still has its rollout read after it has gone.
const Skew = 5 * time.Second
Skew is how far before the spawn a thread's start may be stamped. Clocks agree on one machine; this absorbs rounding, not drift.
const Window = 2 * time.Minute
Window is how long after a spawn a Codex thread may say it started and still be taken as the one Caprock launched. The TUI stamps its thread within seconds of starting; two minutes covers a loaded machine without reaching a thread the user opened later by hand.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Candidate ¶
type Candidate struct {
NativeID string
Cwd string
Started time.Time
// Eligible says the session is one a person started in the agent's own
// TUI — not a subagent, a thread imported from another agent, or one the
// desktop app owns. Only an eligible candidate is ever matched.
Eligible bool
}
Candidate is a session an importer found under the agent's own id.
type Linker ¶
type Linker struct {
Store *store.Store
Log *slog.Logger
// Now is overridable in tests.
Now func() time.Time
// contains filtered or unexported fields
}
Linker holds the spawns still waiting for their agent's id.
func (*Linker) Claim ¶
Claim links sessionID to the agent's own id, exactly. It is for a source that names the session it made — OpenCode's own server on a port Caprock chose. Reports whether the link was made.
func (*Linker) Ended ¶
Ended tells the linker a spawned process has exited. Its expectation is kept for Grace, then dropped.
func (*Linker) Expect ¶
Expect records a spawn whose agent will name the session later. match says whether Match may take it; OpenCode's spawns are claimed exactly instead.