package codex import ( "bytes" "encoding/json" "reflect" "strings" "amadan.net/oficina/equipo/internal/runner/provider" ) // The thin runner drives the pinned app-server itself (one long-lived // coordinator process per person). These are the launcher's own checks, // exported so both paths hold the same policy. // ValidateThinPolicy re-derives a launch's policy from its manifest and // refuses anything the rendering would not produce, or a binary that is // not the pinned one (digest and code-mode host digest; tests pass their // fake's). func ValidateThinPolicy(b provider.Binding, p provider.Policy, digest, hostDigest string) error { l := &Launcher{digest: digest, hostDigest: hostDigest} _, err := l.validatePolicy(b, p) return err } // InitializeParams is the client's initialize request. func InitializeParams() json.RawMessage { return marshal(map[string]any{"clientInfo": map[string]any{"name": "equipo", "version": "2"}, "capabilities": map[string]any{"experimentalApi": true}}) } // CheckInitialize accepts only the pinned version answering with its home // at /home/runner (the runtime copy, never the source home). func CheckInitialize(raw json.RawMessage) error { var v struct{ UserAgent, CodexHome string } if json.Unmarshal(raw, &v) != nil || !strings.HasPrefix(v.UserAgent, "equipo/"+Version+" ") || v.CodexHome != "/home/runner" { return provider.ErrVersion } return nil } // CheckConfig validates config/read's effective configuration against // the policy, as the launcher does before every thread. func CheckConfig(raw json.RawMessage, p provider.Policy) error { return validateConfig(raw, p) } // ThreadParams are thread/start (id empty) or thread/resume parameters // from the policy's thread policy. func ThreadParams(p provider.Policy, id string) json.RawMessage { method := "thread/start" if id != "" { method = "thread/resume" } params := threadParams(p.ThreadPolicy, method, id) return marshal(params) } // CheckThread accepts a thread/start or thread/resume answer only when // the thread runs the policy's model, provider, cwd, approvals and // permission profile; it returns the thread id. func CheckThread(raw json.RawMessage, model, id string) (string, error) { var v struct { Thread struct{ ID string } Model, ModelProvider, Cwd, ApprovalPolicy, ApprovalsReviewer string ActivePermissionProfile *struct { ID string Extends json.RawMessage } RuntimeWorkspaceRoots []string } if json.Unmarshal(raw, &v) != nil || v.Thread.ID == "" || (id != "" && v.Thread.ID != id) || v.Model != model || v.ModelProvider != "openai" || v.Cwd != "/workspace" || v.ApprovalPolicy != "on-request" || v.ApprovalsReviewer != "user" || v.ActivePermissionProfile == nil || v.ActivePermissionProfile.ID != "equipo" || !bytes.Equal(v.ActivePermissionProfile.Extends, []byte("null")) || !reflect.DeepEqual(v.RuntimeWorkspaceRoots, []string{"/workspace"}) { return "", provider.ErrPolicy } return v.Thread.ID, nil } // TurnParams is turn/start for one user message on a thread. func TurnParams(p provider.Policy, thread, text string) json.RawMessage { var params map[string]json.RawMessage _ = json.Unmarshal(p.TurnPolicy, ¶ms) if params == nil { params = map[string]json.RawMessage{} } params["threadId"] = marshal(thread) params["input"] = marshal([]any{map[string]any{"type": "text", "text": text}}) return marshal(params) } // TurnParamsWithImages is turn/start with the text, then each image as a // localImage input by its path inside the namespace (the pinned // app-server's UserInput, from its generated JSON schema). func TurnParamsWithImages(p provider.Policy, thread, text string, images []string) json.RawMessage { if len(images) == 0 { return TurnParams(p, thread, text) } var params map[string]json.RawMessage _ = json.Unmarshal(p.TurnPolicy, ¶ms) if params == nil { params = map[string]json.RawMessage{} } input := []any{map[string]any{"type": "text", "text": text}} for _, path := range images { input = append(input, map[string]any{"type": "localImage", "path": path}) } params["threadId"] = marshal(thread) params["input"] = marshal(input) return marshal(params) } // ToolApproval reads an mcpServer/elicitation/request for an MCP tool // call: the server and the tool's arguments. ok is false for any other // kind of elicitation (which is declined). func ToolApproval(params json.RawMessage) (server string, args json.RawMessage, ok bool) { var p struct { ServerName, Mode string Meta struct { Kind string `json:"codex_approval_kind"` Arguments json.RawMessage `json:"tool_params"` } `json:"_meta"` } if json.Unmarshal(params, &p) != nil || p.Mode != "form" || p.Meta.Kind != "mcp_tool_call" { return "", nil, false } return p.ServerName, p.Meta.Arguments, true } // SameArguments compares two JSON tool argument documents. func SameArguments(a, b json.RawMessage) bool { return sameJSON(a, b) }