| 1 | // Package provider defines the contract shared by direct CLI adapters. |
| 2 | package provider |
| 3 | |
| 4 | import ( |
| 5 | "context" |
| 6 | "encoding/json" |
| 7 | "io" |
| 8 | "time" |
| 9 | ) |
| 10 | |
| 11 | const Version = 2 |
| 12 | |
| 13 | // Provider pins are shared by launchers and staged-home validation. |
| 14 | const ClaudeVersion = "2.1.312" |
| 15 | const CodexVersion = "0.165.0" |
| 16 | |
| 17 | type Selection struct { |
| 18 | ConnectionID, Provider, Family, Model, AccountID string |
| 19 | } |
| 20 | |
| 21 | type Binding struct { |
| 22 | ID, Home, AccountID, Provider string |
| 23 | DedicatedHome bool `json:"-"` |
| 24 | // Token: the home's credential is a pasted setup token (TokenFile), |
| 25 | // passed to the CLI in its environment and never copied. |
| 26 | Token bool `json:"-"` |
| 27 | Expected Identity |
| 28 | CredentialHome, RuntimeRoot string |
| 29 | provenance *AccountLease |
| 30 | } |
| 31 | |
| 32 | // NetworkContract is installed by the runner, never supplied by a permission answer. |
| 33 | // V1 requires AuthHost to be empty; expired logins need a new sign-in. |
| 34 | type NetworkContract struct { |
| 35 | Conflict string |
| 36 | Provider string |
| 37 | AuthHost string |
| 38 | // Harness runs the provider's connection; "" is the provider's own |
| 39 | // CLI (Claude, Codex). A metered connection names its harness (Pi |
| 40 | // for TensorX), and the egress table is keyed by both. |
| 41 | Harness string |
| 42 | } |
| 43 | |
| 44 | type Identity struct { |
| 45 | AccountID string `json:"account_id"` |
| 46 | HomeID string `json:"home_id"` |
| 47 | ProviderVersion string `json:"provider_version"` |
| 48 | } |
| 49 | |
| 50 | type Mount struct { |
| 51 | Source, Target string |
| 52 | ReadOnly bool |
| 53 | } |
| 54 | |
| 55 | type Policy struct { |
| 56 | goLease *GoLease |
| 57 | Hash string |
| 58 | ManifestHash, InstructionHash string |
| 59 | ThreadPolicy, TurnPolicy json.RawMessage |
| 60 | Manifest json.RawMessage |
| 61 | Mounts []Mount |
| 62 | Env map[string]string |
| 63 | Settings, MCP json.RawMessage |
| 64 | } |
| 65 | |
| 66 | type Launch struct { |
| 67 | Mode string `json:",omitempty"` |
| 68 | // ToolSocket is the runner-created per-attempt endpoint, pinned by containment. |
| 69 | ToolSocket string |
| 70 | AttemptKey, Workspace, ResumeSessionID string |
| 71 | // ResearchDir is a runner-made directory of a task's kept research |
| 72 | // images, bound read-only at /research through a pinned descriptor. |
| 73 | // Containment allows it on Engineering and Review launches only. |
| 74 | ResearchDir string `json:",omitempty"` |
| 75 | // RenderDir is a runner-made directory of the step's render_page |
| 76 | // captures, bound read-only at /render through a pinned descriptor. |
| 77 | // Containment allows it on the Design step's launch only. |
| 78 | RenderDir string `json:",omitempty"` |
| 79 | Selection Selection |
| 80 | Binding Binding |
| 81 | Policy Policy |
| 82 | } |
| 83 | |
| 84 | // ID is the provider's session identity. AttemptKey remains local to Equipo, |
| 85 | // including when another attempt resumes the same provider session. |
| 86 | type Session struct { |
| 87 | ID, AttemptKey, Provider, AccountID, Model, Family string |
| 88 | } |
| 89 | |
| 90 | type Turn struct { |
| 91 | ID, AdmissionID, Prompt string |
| 92 | } |
| 93 | |
| 94 | type Answer struct { |
| 95 | // Authorize is supplied by the runner for an Equipo MCP approval. The adapter |
| 96 | // passes the captured original request; model payloads cannot supply callbacks. |
| 97 | Authorize func(context.Context, Event) error `json:"-"` |
| 98 | |
| 99 | RequestID, TurnID, Behavior string |
| 100 | Input json.RawMessage |
| 101 | } |
| 102 | |
| 103 | type Event struct { |
| 104 | ProviderTurnID string `json:"provider_turn_id,omitempty"` |
| 105 | // ToolCall is the captured item/started frame correlated by the adapter. |
| 106 | ToolCall json.RawMessage `json:",omitempty"` |
| 107 | Sequence int64 `json:"capture_sequence,omitempty"` |
| 108 | SessionID, TurnID, RequestID, Kind string |
| 109 | Payload json.RawMessage |
| 110 | } |
| 111 | |
| 112 | type Exit struct { |
| 113 | Code int |
| 114 | Signal int |
| 115 | ObservedAt time.Time |
| 116 | } |
| 117 | |
| 118 | type Process interface { |
| 119 | Stdin() io.WriteCloser |
| 120 | Stdout() io.ReadCloser |
| 121 | Stderr() io.ReadCloser |
| 122 | ID() string |
| 123 | Wait(context.Context) (Exit, error) |
| 124 | } |
| 125 | |
| 126 | // ProcessHost owns the namespace and the lifetime of its processes. |
| 127 | // Start receives the attempt lifetime context. Cancelling it kills the attempt; |
| 128 | // callers must not use a short preflight or startup-only context. |
| 129 | type ProcessHost interface { |
| 130 | Start(context.Context, Launch, []string) (Process, error) |
| 131 | } |
| 132 | |
| 133 | // Constructors for adapters must require a ProcessHost and CaptureFactory. |
| 134 | // Open sends no prompt. Attach Events before calling StartTurn. |
| 135 | type Launcher interface { |
| 136 | Preflight(context.Context, Binding, Policy) (Identity, error) |
| 137 | Open(context.Context, Launch) (Session, error) |
| 138 | Events(context.Context, Session) (<-chan Event, error) |
| 139 | StartTurn(context.Context, Session, Turn) error |
| 140 | Respond(context.Context, Session, Answer) error |
| 141 | Interrupt(context.Context, Session) error |
| 142 | Close(context.Context, Session) error |
| 143 | } |
| 144 | |
| 145 | type Frame struct { |
| 146 | Sequence int64 |
| 147 | Bytes json.RawMessage |
| 148 | } |
| 149 | |
| 150 | // Capture is the durable I/O boundary. Adapters consume Frames through their |
| 151 | // own Decode([]byte) ([]Event, error), never directly from process stdout. |
| 152 | type Capture struct { |
| 153 | Diagnostics func() string |
| 154 | Input io.WriteCloser |
| 155 | Frames <-chan Frame |
| 156 | Done <-chan error |
| 157 | } |
| 158 | |
| 159 | type CaptureFactory interface { |
| 160 | Open(context.Context, Process, Launch) (*Capture, error) |
| 161 | } |
| 162 | |
| 163 | func ValidateContract(version int, name string) error { |
| 164 | if version != Version { |
| 165 | return ErrVersion |
| 166 | } |
| 167 | if name != "claude" && name != "codex" { |
| 168 | return ErrProvider |
| 169 | } |
| 170 | return nil |
| 171 | } |
| 172 | |
| 173 | // Validate binds an observed session to its launch, including a requested resume. |
| 174 | // Identity equality does not confer permission to dispatch a turn. |
| 175 | func (s Session) Validate(l Launch) error { |
| 176 | if s.ID == "" || (l.ResumeSessionID != "" && s.ID != l.ResumeSessionID) { |
| 177 | return ErrIdentity |
| 178 | } |
| 179 | for _, pair := range [][2]string{ |
| 180 | {s.AttemptKey, l.AttemptKey}, |
| 181 | {s.Provider, l.Selection.Provider}, |
| 182 | {s.AccountID, l.Selection.AccountID}, |
| 183 | {s.Model, l.Selection.Model}, |
| 184 | {s.Family, l.Selection.Family}, |
| 185 | {s.Provider, l.Binding.Provider}, |
| 186 | {s.AccountID, l.Binding.AccountID}, |
| 187 | } { |
| 188 | if pair[0] == "" || pair[0] != pair[1] { |
| 189 | return ErrIdentity |
| 190 | } |
| 191 | } |
| 192 | return nil |
| 193 | } |
| 194 | |
| 195 | // ValidateIdentity checks correlation for an event that belongs to a turn. |
| 196 | // Session-level events without a turn use the adapter's session checks instead. |
| 197 | func (e Event) ValidateIdentity(s Session, t Turn) error { |
| 198 | if e.SessionID == "" || e.SessionID != s.ID || e.TurnID == "" || e.TurnID != t.ID { |
| 199 | return ErrIdentity |
| 200 | } |
| 201 | return nil |
| 202 | } |
| 203 | |
| 204 | // TurnBarrier is implemented by durable capture stdin. Adapters call it before |
| 205 | // the prompt write, after admission and their final account/policy checks. |
| 206 | type TurnBarrier interface { |
| 207 | BeginTurn(context.Context, Session, Turn) error |
| 208 | } |
| 209 | |
| 210 | // SessionBinding persists the provider identity as soon as Open discovers it. |
| 211 | type SessionBinding interface { |
| 212 | BindSession(context.Context, Session) error |
| 213 | } |
| 214 | |
| 215 | // AnswerWriter journals a correlated answer before sending it. It does not |
| 216 | // consume a new turn admission and refuses writes outside the active turn. |
| 217 | type AnswerWriter interface { |
| 218 | WriteAnswer(context.Context, Session, string, []byte) (int, error) |
| 219 | } |
| 220 | |
| 221 | const LauncherVersion = "direct-v3" |
| 222 | |