oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package runner
2
3import (
4 "bytes"
5 "context"
6 "encoding/json"
7 "io"
8
9 "amadan.net/oficina/equipo/internal/runner/provider"
10 "amadan.net/oficina/equipo/internal/runnerproto"
11)
12
13// WriteAnswer is a separate, durable path for replies in an admitted turn.
14func (c *captureInput) WriteAnswer(ctx context.Context, s provider.Session, turn string, raw []byte) (int, error) {
15 c.mu.Lock()
16 defer c.mu.Unlock()
17 if c.closed || c.ctx.Err() != nil || ctx.Err() != nil || c.launch.Selection.Provider != "codex" || s.Validate(c.launch) != nil || len(raw) > MaxFrameBytes+1 || !bytes.HasSuffix(raw, []byte{'\n'}) || !uniqueFrame(bytes.TrimSpace(raw)) {
18 return 0, runnerproto.ErrInvalid
19 }
20 var v struct {
21 ID json.RawMessage `json:"id"`
22 Method string `json:"method,omitempty"`
23 Params json.RawMessage `json:"params,omitempty"`
24 Result json.RawMessage `json:"result,omitempty"`
25 Error json.RawMessage `json:"error,omitempty"`
26 }
27 if runnerproto.DecodeLimit(bytes.NewReader(raw), &v, MaxFrameBytes) != nil || len(v.ID) == 0 || string(v.ID) == "null" {
28 return 0, runnerproto.ErrInvalid
29 }
30 var idString string
31 var idNumber int64
32 if json.Unmarshal(v.ID, &idString) != nil && json.Unmarshal(v.ID, &idNumber) != nil {
33 return 0, runnerproto.ErrInvalid
34 }
35 if len(v.Error) > 0 {
36 var denial struct {
37 Code int `json:"code"`
38 Message string `json:"message"`
39 }
40 if v.Method != "" || len(v.Result) > 0 || len(v.Params) > 0 || runnerproto.DecodeLimit(bytes.NewReader(v.Error), &denial, MaxFrameBytes) != nil || denial.Code != -32000 || denial.Message != provider.ErrPolicy.Error() {
41 return 0, runnerproto.ErrInvalid
42 }
43 } else if v.Method == "turn/steer" {
44 var p struct {
45 Thread string `json:"threadId"`
46 Expected string `json:"expectedTurnId"`
47 Input []struct {
48 Type string `json:"type"`
49 Text string `json:"text"`
50 } `json:"input"`
51 }
52 if len(v.Result) != 0 || runnerproto.DecodeLimit(bytes.NewReader(v.Params), &p, MaxFrameBytes) != nil || p.Thread != s.ID || p.Expected == "" || len(p.Input) != 1 || p.Input[0].Type != "text" || p.Input[0].Text == "" {
53 return 0, runnerproto.ErrInvalid
54 }
55 } else if v.Method == "" && len(v.Params) == 0 {
56 var r struct {
57 Action string `json:"action,omitempty"`
58 Content json.RawMessage `json:"content,omitempty"`
59 Meta json.RawMessage `json:"_meta,omitempty"`
60 Decision string `json:"decision,omitempty"`
61 Answers map[string]struct {
62 Answers []string `json:"answers"`
63 } `json:"answers,omitempty"`
64 }
65 if runnerproto.DecodeLimit(bytes.NewReader(v.Result), &r, MaxFrameBytes) != nil {
66 return 0, runnerproto.ErrInvalid
67 }
68 if r.Action != "" {
69 if (r.Action != "accept" && r.Action != "decline") || r.Decision != "" || len(r.Answers) != 0 || string(r.Meta) != "null" || (r.Action == "accept" && string(r.Content) != "{}") || (r.Action == "decline" && string(r.Content) != "null") {
70 return 0, runnerproto.ErrInvalid
71 }
72 } else if r.Decision != "" {
73 if (r.Decision != "accept" && r.Decision != "decline") || len(r.Answers) != 0 {
74 return 0, runnerproto.ErrInvalid
75 }
76 } else if len(r.Answers) == 0 {
77 return 0, runnerproto.ErrInvalid
78 }
79 } else {
80 return 0, runnerproto.ErrInvalid
81 }
82 if e := c.journal.AdmissionAllowed(ctx); e != nil {
83 return 0, e
84 }
85 key := runnerproto.EventHash([]any{"direct-wire", s.AttemptKey, turn, v.Method, v.ID})
86 hash := runnerproto.EventHash(json.RawMessage(bytes.TrimSpace(raw)))
87 tx, e := c.journal.db.BeginTx(ctx, nil)
88 if e != nil {
89 return 0, e
90 }
91 defer tx.Rollback()
92 var count int
93 if e = tx.QueryRowContext(ctx, `SELECT count(*) FROM direct_turns t JOIN direct_attempts a ON a.attempt=t.attempt WHERE t.attempt=? AND t.session=? AND t.turn=? AND t.state='dispatched' AND a.recovered=0`, s.AttemptKey, s.ID, turn).Scan(&count); e != nil || count != 1 {
94 return 0, runnerproto.ErrInvalid
95 }
96 if _, e = tx.ExecContext(ctx, "INSERT INTO journal_answers VALUES(?,?,'pending')", key, hash); e != nil {
97 return 0, runnerproto.ErrInvalid
98 }
99 if _, e = tx.ExecContext(ctx, "INSERT INTO direct_answer_scopes VALUES(?,?,?)", key, s.AttemptKey, turn); e != nil {
100 return 0, e
101 }
102 if e = tx.Commit(); e != nil {
103 return 0, c.journal.storageFailure(ctx, e)
104 }
105 if e = c.journal.currentDirectTurn(ctx, s, turn); e != nil {
106 return 0, e
107 }
108 n, e := c.input.Write(raw)
109 if e != nil || n != len(raw) {
110 c.closed = true
111 if e == nil {
112 e = io.ErrShortWrite
113 }
114 c.fail(e)
115 return n, e
116 }
117 if _, e = c.journal.db.ExecContext(ctx, "UPDATE journal_answers SET state='delivered' WHERE answer_key=? AND state='pending'", key); e != nil {
118 return n, c.journal.storageFailure(ctx, e)
119 }
120 return n, nil
121}
122