Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.
codexsummary: one owner for magpie's Codex compaction item and the message it stands for
yetone694674cf0d99
diff --git a/internal/codexsummary/codexsummary.go b/internal/codexsummary/codexsummary.gonew file mode 100644index 000000000..c06621b19--- /dev/null+++ b/internal/codexsummary/codexsummary.go@@ -0,0 +1,55 @@+// Package codexsummary is the compaction magpie answers Codex with when it+// compacts a conversation itself (a model of magpie's, or Codex's own+// models through magpie's accounts), and the message it stands for.+//+// Codex keeps a compaction item whole, encrypted_content and all, in its+// rollout, and sends it back as it is with every later turn. magpie's is+// "magpie1:" and the summary in base64, which only magpie reads: sent to+// OpenAI itself (magpie quit, or disconnected) it is refused as+// invalid_encrypted_content, and the conversation can't go on (LukeMa on+// X). What it is read as is the message Codex's own local compaction+// keeps in its place: its summary prefix, a newline and the summary, as a+// user message, which Codex knows for a summary (is_summary_message).+// The gateway sends that in place of the item, and a rollout is made+// portable by writing it there (sessions.PortCodexRollouts).+package codexsummary++import (+ "encoding/base64"+ "strings"+)++// Mark begins the encrypted_content of a compaction item magpie made.+const Mark = "magpie1:"++// Prefix is Codex's own (Apache-2.0, openai/codex,+// prompts/templates/compact/summary_prefix.md).+const Prefix = `Another language model started to solve this problem and produced a summary of its thinking process. You also have access to the state of the tools that were used by that language model. Use this to build on the work that has already been done and avoid duplicating work. Here is the summary produced by the other language model, use the information in this summary to assist with your own analysis:`++// Encrypted is the encrypted_content of magpie's compaction item for a+// summary.+func Encrypted(summary string) string {+ return Mark + base64.StdEncoding.EncodeToString([]byte(summary))+}++// Message is what a compaction item's encrypted_content is read as: ours+// is false for one magpie didn't make (OpenAI's own, left as it is), and+// msg nil for one of magpie's whose summary can't be read, which is left+// out.+func Message(encrypted string) (msg map[string]any, ours bool) {+ b64, ok := strings.CutPrefix(encrypted, Mark)+ if !ok {+ return nil, false+ }+ b, err := base64.StdEncoding.DecodeString(b64)+ if err != nil {+ return nil, true+ }+ return UserMessage(Prefix + "\n" + string(b)), true+}++// UserMessage is a Responses input message of the user's.+func UserMessage(text string) map[string]any {+ return map[string]any{"type": "message", "role": "user",+ "content": []map[string]any{{"type": "input_text", "text": text}}}+}diff --git a/internal/codexsummary/codexsummary_test.go b/internal/codexsummary/codexsummary_test.gonew file mode 100644index 000000000..e087b8b29--- /dev/null+++ b/internal/codexsummary/codexsummary_test.go@@ -0,0 +1,20 @@+package codexsummary++import (+ "reflect"+ "testing"+)++func TestMessage(t *testing.T) {+ msg, ours := Message(Encrypted("done: a, b\nnext: c"))+ want := UserMessage(Prefix + "\ndone: a, b\nnext: c")+ if !ours || !reflect.DeepEqual(msg, want) {+ t.Fatalf("magpie's compaction read as %v, %v; want %v", msg, ours, want)+ }+ if msg, ours := Message("gAAAAABopaque=="); ours || msg != nil {+ t.Fatalf("OpenAI's own compaction read as magpie's: %v", msg)+ }+ if msg, ours := Message(Mark + "%%%"); !ours || msg != nil {+ t.Fatalf("an unreadable summary of magpie's: %v, %v; want nil, true", msg, ours)+ }+}diff --git a/internal/gateway/codex_backend.go b/internal/gateway/codex_backend.goindex 44ac174ac..3777b8295 100644--- a/internal/gateway/codex_backend.go+++ b/internal/gateway/codex_backend.go@@ -4,7 +4,6 @@ import ("bytes""cmp""context"- "encoding/base64""encoding/json""errors""fmt"@@ -21,6 +20,7 @@ import ("github.com/yetone/magpie/internal/access""github.com/yetone/magpie/internal/catalog""github.com/yetone/magpie/internal/codexcat"+ "github.com/yetone/magpie/internal/codexsummary""github.com/yetone/magpie/internal/provider""github.com/yetone/magpie/internal/settings""github.com/yetone/magpie/internal/usage"@@ -40,8 +40,8 @@ const CodexPath = "/backend-api/codex"var codexAPIBase = "https://api.openai.com/v1"// magpieCompaction marks a compaction item magpie made: its summary, which-// only magpie reads back.-const magpieCompaction = "magpie1:"+// only magpie reads back (codexsummary).+const magpieCompaction = codexsummary.Markvar nativeSealedAgentPayload = regexp.MustCompile(`^gAAAAA[A-Za-z0-9_-]+={0,2}$`)@@ -57,7 +57,7 @@ Include:Be concise, structured, and focused on helping the next LLM seamlessly continue the work.`-const codexSummaryPrefix = `Another language model started to solve this problem and produced a summary of its thinking process. You also have access to the state of the tools that were used by that language model. Use this to build on the work that has already been done and avoid duplicating work. Here is the summary produced by the other language model, use the information in this summary to assist with your own analysis:`+const codexSummaryPrefix = codexsummary.Prefixfunc (s *Server) codexBackend(w http.ResponseWriter, r *http.Request) {// Responses over a WebSocket: 426 sends Codex to plain HTTP at once@@ -1823,14 +1823,14 @@ func codexInputAs(body []byte, magpieModel, native bool) (_ []byte, compact boolswitch it["type"] {case "compaction", "compaction_summary":enc, _ := it["encrypted_content"].(string)- sum, ok := strings.CutPrefix(enc, magpieCompaction)- if !ok {+ msg, ours := codexsummary.Message(enc)+ if !ours {out = append(out, it) // OpenAI's own, for OpenAIcontinue}changed = true- if b, err := base64.StdEncoding.DecodeString(sum); err == nil {- out = append(out, userMessage(codexSummaryPrefix+"\n"+string(b)))+ if msg != nil {+ out = append(out, msg)}case "compaction_trigger":if !magpieModel || native {@@ -2499,10 +2499,7 @@ func forVendorChanges(body []byte) bool {})}-func userMessage(text string) map[string]any {- return map[string]any{"type": "message", "role": "user",- "content": []map[string]any{{"type": "input_text", "text": text}}}-}+func userMessage(text string) map[string]any { return codexsummary.UserMessage(text) }// codexCompact compacts a conversation held by a magpie model: the model// summarises it, and the summary goes back to Codex as the compaction item@@ -2627,7 +2624,7 @@ func (s *Server) codexCompact(w http.ResponseWriter, r *http.Request, body []bytid = fmt.Sprintf("resp_magpie_%d", time.Now().UnixNano())}item := map[string]any{"type": "compaction", "id": "cmp_" + strings.TrimPrefix(id, "resp_"),- "encrypted_content": magpieCompaction + base64.StdEncoding.EncodeToString([]byte(sum.String()))}+ "encrypted_content": codexsummary.Encrypted(sum.String())}done := map[string]any{"id": id, "object": "response", "status": "completed", "output": []any{item}}if len(res.Usage) > 0 {done["usage"] = res.Usage