Repository navigation
Expand file tree
/
Copy pathsession_thread.go
More file actions
87 lines (82 loc) · 2.62 KB
/
Copy pathsession_thread.go
File metadata and controls
87 lines (82 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package codex
import (
"encoding/json"
"fmt"
)
// Harnesses run unattended with the launching user's permissions: Codex never
// raises a native approval request and applies no inner sandbox.
const (
approvalPolicyNever = "never"
sandboxDangerFullAccess = "danger-full-access"
)
func (s *Session) startThread(plan SessionPlan) error {
params := ThreadStartParams{
Cwd: plan.Cwd,
Model: plan.Model,
ModelProvider: plan.ModelProvider,
ApprovalPolicy: approvalPolicyNever,
Sandbox: sandboxDangerFullAccess,
DeveloperInstructions: plan.SystemPrompt,
}
if s.observeSubagentIdentities {
params.HistoryMode = "paginated"
}
if s.functions != nil {
params.DynamicTools = s.functions.definitions
}
s.cfg.logger.Info("codex: thread/start request",
"run_id", s.runID,
"cwd", params.Cwd,
"model", params.Model,
"model_provider", params.ModelProvider,
"sandbox", params.Sandbox,
"developer_instructions_len", len(params.DeveloperInstructions))
_, err := s.rpc.requestWithResult(s.cancelCtx, "thread/start", params, func(raw json.RawMessage) error {
return s.bindThreadResult(raw, "")
})
return err
}
func (s *Session) resumeThread(threadID string, plan SessionPlan) error {
params := ThreadResumeParams{
ThreadID: threadID, Cwd: plan.Cwd, ApprovalPolicy: approvalPolicyNever, Sandbox: sandboxDangerFullAccess,
DeveloperInstructions: plan.SystemPrompt,
}
s.usageMu.Lock()
s.resumeUsageThreadID = threadID
s.usageMu.Unlock()
defer func() {
s.usageMu.Lock()
s.resumeUsageThreadID, s.resumeUsageTotal = "", nil
s.usageMu.Unlock()
}()
_, err := s.rpc.requestWithResult(s.cancelCtx, "thread/resume", params, func(raw json.RawMessage) error {
if err := s.bindThreadResult(raw, threadID); err != nil {
return err
}
s.usageMu.Lock()
if s.resumeUsageTotal != nil {
s.usageTotal = *s.resumeUsageTotal
}
s.resumeUsageThreadID, s.resumeUsageTotal = "", nil
s.usageMu.Unlock()
return nil
})
return err
}
func (s *Session) bindThreadResult(raw json.RawMessage, expectedID string) error {
var res ThreadStartResult
if err := json.Unmarshal(raw, &res); err != nil {
return fmt.Errorf("codex: decode thread response: %w", err)
}
if res.Thread.ID == "" || (expectedID != "" && res.Thread.ID != expectedID) {
return fmt.Errorf("codex: thread response has missing or mismatched identity")
}
if threadID := s.currentThreadID(); threadID != "" && threadID != res.Thread.ID {
return fmt.Errorf("codex: thread response would replace the root identity")
}
s.setThreadID(res.Thread.ID)
if res.Model != "" {
s.resolvedModel = res.Model
}
return nil
}