package repo import ( "context" "strings" "testing" "github.com/agentmail/gateway/internal/db" "github.com/agentmail/gateway/internal/models" "github.com/google/uuid" ) /* 会话维度:一个 Agent 同时在多条会话里干活时,**读一条不能动另一条**。 # 用户报的缺陷 「不同 session 的 agent 都可以看到全部邮件」—— 原先 `read_inbox` 按 **Agent** 列, 并且把列出来的都标成已读 ⇒ A 会话的 worker 会把 B 会话的未读标掉。 平时看不出来(SSE 事件在途时队列兜着),但桥重启/漏事件之后的补投判据是 `?status=unread` —— 被别人标掉的那封信**再也不会补投**。这不是"少看一封", 是静默丢信。现场实例:另一条会话的来信被我在读自己收件箱时标成已读 (`mail_reads` 里那行的 reader=pi、时间就是那次读取的时刻)。 # 判据要两侧都验 只验"收窄后看不到别人的"是不够的:把列表直接返回空也能过。所以同时验 "不带会话时两条都看得到"(反向对照),以及"全标已读"两个变体。 */ // seedMailInSession 在指定会话里造一封给 recipient 的未读邮件。 func seedMailInSession(t *testing.T, sessionID uuid.UUID, recipient string) uuid.UUID { t.Helper() var id uuid.UUID if err := db.DB.QueryRowContext(context.Background(), `INSERT INTO mails (session_id, from_name, to_name, subject, body) VALUES ($1, 'sender', $2, 's', 'b') RETURNING mail_id`, sessionID, recipient).Scan(&id); err != nil { t.Fatal(err) } return id } func twoSessionsWithUnread(t *testing.T, agent string) (uuid.UUID, uuid.UUID, uuid.UUID, uuid.UUID) { t.Helper() ctx := context.Background() s1, err := CreateSession(ctx, nil, "human", "会话一", "") if err != nil { t.Fatal(err) } s2, err := CreateSession(ctx, nil, "human", "会话二", "") if err != nil { t.Fatal(err) } return s1, s2, seedMailInSession(t, s1, agent), seedMailInSession(t, s2, agent) } func TestInboxListIsScopedBySession(t *testing.T) { setupTestDB(t) ctx := context.Background() s1, s2, m1, m2 := twoSessionsWithUnread(t, "bot") // 反向对照:不带会话 = 整个 Agent 的收件箱,两条都看得到 all, err := ListInbox(ctx, "bot", "unread", 50) if err != nil { t.Fatal(err) } if !hasMail(all, m1) || !hasMail(all, m2) { t.Fatalf("不带会话时应当两条都列出来(%d 条)", len(all)) } // ★ 收窄到会话一:只有它那条 scoped, err := ListInboxScoped(ctx, "bot", "unread", 50, s1) if err != nil { t.Fatal(err) } if !hasMail(scoped, m1) { t.Fatal("会话一的邮件应当在会话一的列表里") } if hasMail(scoped, m2) { t.Fatal("★ 会话二的邮件不该出现在会话一的列表里(用户报的缺陷)") } // 会话二同理(别只验一侧 —— 方向反了也能"过") scoped2, err := ListInboxScoped(ctx, "bot", "unread", 50, s2) if err != nil { t.Fatal(err) } if hasMail(scoped2, m1) || !hasMail(scoped2, m2) { t.Fatalf("会话二的列表应只含会话二的邮件") } } func TestMarkAllReadCanBeScopedToSession(t *testing.T) { setupTestDB(t) ctx := context.Background() s1, s2, _, _ := twoSessionsWithUnread(t, "bot") // 只标会话一 n, err := MarkAllInboxReadForSession(ctx, "bot", s1) if err != nil { t.Fatal(err) } if n != 1 { t.Fatalf("应当只标掉 1 封,实际 %d", n) } left, err := ListInboxScoped(ctx, "bot", "unread", 50, s2) if err != nil { t.Fatal(err) } if len(left) != 1 { t.Fatalf("★ 会话二的未读必须还在(实际剩 %d 条)—— 被跨会话标掉就是静默丢信", len(left)) } // 反向对照:不带会话时两条都会被标掉 if _, err := MarkAllInboxReadFor(ctx, "bot"); err != nil { t.Fatal(err) } if again, _ := ListInbox(ctx, "bot", "unread", 50); len(again) != 0 { t.Fatalf("不带会话的全标应当清空整个收件箱,实际剩 %d", len(again)) } } func TestScopedCountsMatchScopedList(t *testing.T) { setupTestDB(t) ctx := context.Background() s1, s2, _, _ := twoSessionsWithUnread(t, "bot") // 会话内的未读数(会话列表徽标用)与"只列这条会话"的口径必须一致, // 否则界面上会出现"徽标 2、列表 1"这种自相矛盾。 n1, err := CountUnreadInSession(ctx, "bot", s1) if err != nil { t.Fatal(err) } list1, err := ListInboxScoped(ctx, "bot", "unread", 50, s1) if err != nil { t.Fatal(err) } if n1 != len(list1) || n1 != 1 { t.Fatalf("会话一:计数 %d、列表 %d,期望都是 1", n1, len(list1)) } _ = s2 } func hasMail(list []models.Mail, id uuid.UUID) bool { for _, m := range list { if m.ID == id { return true } } return false } // ─── 补拉路径的档位契约(2026-09-14) ─── // // 桥的离线补投走 `GET /mail/inbox`(repo 层就是 `ListInboxScoped`)。 // 事故:插件以为这条路径**拿不到**会话档位,于是 `mailToEvent` 只搬了 8 个字段, // 补投的邮件让 worker 回落默认 workspace 档 —— 一条 full 档会话被误拦一整轮。 // 实际上服务端的行**一直带着**档位(SQL 里有 `JOIN sessions s` + // `COALESCE(NULLIF(s.permission_mode,”),'workspace')`,`models.Mail.PermissionMode` // 的注释也写明补拉路径必须有它)。这条判据把"服务端确实给了"钉死: // 谁哪天把这个 JOIN 去掉,插件侧就会静默降级,而单看插件测试是发现不了的。 func TestInboxRowsCarrySessionPermissionMode(t *testing.T) { setupTestDB(t) ctx := context.Background() seedAgent(t, "bot", 50) sid := createTestSession(t, ctx, "bot", "/tmp/ws") if _, err := SetSessionPermissionMode(ctx, sid, "full"); err != nil { t.Fatal(err) } seedMailInSession(t, sid, "bot") rows, err := ListInboxScoped(ctx, "bot", "unread", 50, sid) if err != nil { t.Fatal(err) } if len(rows) != 1 { t.Fatalf("应当恰好一条未读,实际 %d 条", len(rows)) } if rows[0].PermissionMode != "full" { t.Fatalf("补拉路径必须带上会话档位:want full, got %q", rows[0].PermissionMode) } if rows[0].PermissionEnforcement == "" { t.Fatal("permission_enforcement 也要带上(空值会让插件无从判断强制力)") } // 反向对照:档位没设过的会话落到 workspace(**不是** full)—— // 缺字段时"猜宽"就是提权,所以默认值这一侧也要验。 other := createTestSession(t, ctx, "bot", "/tmp/ws2") seedMailInSession(t, other, "bot") rows2, err := ListInboxScoped(ctx, "bot", "unread", 50, other) if err != nil { t.Fatal(err) } for _, m := range rows2 { if m.PermissionMode != "workspace" { t.Fatalf("没有档位记录的会话应当回落 workspace,got %q", m.PermissionMode) } } } // 回信地址:补拉路径也要有(与 PermissionMode 同一个理由 —— 少了它, // 补投进来的邮件拿不到回信地址,插件只能自己拼,拼错就是静默开新会话)。 func TestInboxRowsCarryReplyAddress(t *testing.T) { setupTestDB(t) ctx := context.Background() seedAgent(t, "bot", 50) // 会话带别名(回信地址里应当出现别名;没有别名的会话只写发件人) alias := "harmony-ui-alignment" sid, err := CreateSession(ctx, nil, "sender-agent", "test subject", "/tmp/ws") if err != nil { t.Fatal(err) } if _, err := db.DB.ExecContext(ctx, `UPDATE sessions SET session_alias = $1 WHERE session_id = $2`, alias, sid); err != nil { t.Fatal(err) } seedMailInSession(t, sid, "bot") rows, err := ListInboxScoped(ctx, "bot", "unread", 50, sid) if err != nil { t.Fatal(err) } if len(rows) != 1 { t.Fatalf("应当恰好一条未读,实际 %d 条", len(rows)) } got := rows[0].ReplyAddress want := models.FormatAddress(rows[0].FromName, "", alias) if got != want { t.Fatalf("回信地址要与 SSE 载荷同形:want %q, got %q", want, got) } // 反面:地址里**不该**出现 path 位(与 notify 的注释同一取舍:人没有工作目录) if strings.Count(got, "@") != 1 { t.Fatalf("回信地址形状不对:%q", got) } }