Skip to content

Linger is An Open-Source Personal Agent That Runs Fully on Your PC. We aim to break Big Tech’s grip on AI by bringing powerful local AI to everyday computers through efficient model–harness co-design, so everyone can run their own.

fix(teamhub): rename the email canvas before its subject line changes

muterC39342d00bbfc
diff --git a/apps/teamhub/internal/voice/voice_live_canvas.go b/apps/teamhub/internal/voice/voice_live_canvas.go
index 605632ea..3f4fcc6f 100644
--- a/apps/teamhub/internal/voice/voice_live_canvas.go
+++ b/apps/teamhub/internal/voice/voice_live_canvas.go
@@ -141,9 +141,10 @@ func (s *Service) openVoiceLiveCanvas(ctx context.Context, session voicelive.Ses
return created, "", tx.Commit(ctx)
}
-// editVoiceLiveEmailCanvas replaces the canvas with the changed email as
-// the Agent, only when it is still the revision the live model last saw:
-// the user's own edits in between are returned to it, never overwritten.
+// editVoiceLiveEmailCanvas changes the email on the canvas as the Agent. To,
+// cc and subject change on the latest canvas; the body changes only while it
+// is still the text the live model last read, so the user's own edits to it
+// are returned to the model, never overwritten.
func (s *Service) editVoiceLiveEmailCanvas(ctx context.Context, session voicelive.Session, request teamhubinternal.VoiceLiveToolCall) (string, error) {
var args voicelive.EditEmailCanvasArguments
if voicelive.DecodeArguments(request.Arguments, &args) != nil || args.DocumentID == "" {
@@ -231,7 +232,7 @@ func (s *Service) voiceLiveCanvasBodySince(ctx context.Context, revisionID strin
}
// applyVoiceLiveEmailEdit makes one email edit on the canvas as read. A
-// native edit is one operation: the header, the title and the body's text
+// native edit is one operation: the title, the header and the body's text
// change in turn, each against the revision the one before made. conflicted
// means another change landed first, after which part of it may be made.
func (s *Service) applyVoiceLiveEmailEdit(ctx context.Context, session voicelive.Session, request teamhubinternal.VoiceLiveToolCall,
@@ -241,13 +242,15 @@ func (s *Service) applyVoiceLiveEmailEdit(ctx context.Context, session voicelive
return voicelive.ErrorOutput(problem), false, nil
}
edits := []map[string]string{}
- if args.To != nil || args.Cc != nil || args.Subject != nil {
- edits = append(edits, map[string]string{"command": "block_replace", "block_id": canvas.firstBlockID, "content": header})
- }
if args.Subject != nil && voicelive.EmailCanvasTitle(*args.Subject) != voicelive.EmailCanvasTitle(canvas.fields["subject"]) {
// The canvas is named after its email, as open_email_canvas names it.
+ // The title goes before the subject line, so a try redone after the
+ // subject changed has already renamed it.
edits = append(edits, map[string]string{"command": "set_title", "title": voicelive.EmailCanvasTitle(*args.Subject)})
}
+ if args.To != nil || args.Cc != nil || args.Subject != nil {
+ edits = append(edits, map[string]string{"command": "block_replace", "block_id": canvas.firstBlockID, "content": header})
+ }
if args.Body != nil {
// The other text runs go first, last to first, and the attachments
// between them stay; the new text then takes the first text's place,
@@ -606,7 +609,6 @@ func (s *Service) voiceLiveDocument(ctx context.Context, principal agentcredenti
return voiceLiveDocumentState{}, document.ErrRevisionUnavailable
}
-// voiceLiveCanvas reads the latest Gmail canvas as the Agent.
// voiceLiveCanvasText is a canvas's Markdown without its header, and the
// text of that body without attachment lines, as the live model reads it.
func voiceLiveCanvasText(markdown string) (body, text string) {
@@ -614,6 +616,7 @@ func voiceLiveCanvasText(markdown string) (body, text string) {
return body, strings.TrimSpace(voiceLiveAttachmentLine.ReplaceAllString(body, "")) + "\n"
}
+// voiceLiveCanvas reads the latest Gmail canvas as the Agent.
func (s *Service) voiceLiveCanvas(ctx context.Context, principal agentcredential.Principal, documentID string) (voiceLiveCanvasState, error) {
state, err := s.voiceLiveDocument(ctx, principal, documentID)
if err != nil {
diff --git a/apps/teamhub/tests/integration/voice_live_canvas_test.go b/apps/teamhub/tests/integration/voice_live_canvas_test.go
index 70c8b6d9..29e4c3c4 100644
--- a/apps/teamhub/tests/integration/voice_live_canvas_test.go
+++ b/apps/teamhub/tests/integration/voice_live_canvas_test.go
@@ -19,13 +19,41 @@ import (
teamhubinternal "github.com/simplexai-labs/linger/contracts-go/teamhubinternal"
)
-// canvasRuntime is a Document Runtime whose state is the canvas Markdown
-// itself: it exports that Markdown, its header and paragraphs as blocks, and
-// applies a whole-canvas block_replace to the body in pool.
// canvasAttachmentLink is an attachment line as the Markdown export writes
// a file block: its name and its Document Asset.
var canvasAttachmentLink = regexp.MustCompile(`^\[([^\]]*)\]\(document-asset:([A-Za-z0-9_-]+)\)$`)
+// canvasConflicts makes a canvas's next changes conflict, as if another
+// change landed first: skip changes apply before conflicts of them conflict.
+var canvasConflicts sync.Map // document ID → *canvasConflictPlan
+
+type canvasConflictPlan struct {
+ mu sync.Mutex
+ skip, conflicts int
+}
+
+func canvasConflicted(documentID string) bool {
+ value, ok := canvasConflicts.Load(documentID)
+ if !ok {
+ return false
+ }
+ plan := value.(*canvasConflictPlan)
+ plan.mu.Lock()
+ defer plan.mu.Unlock()
+ if plan.skip > 0 {
+ plan.skip--
+ return false
+ }
+ if plan.conflicts > 0 {
+ plan.conflicts--
+ return true
+ }
+ return false
+}
+
+// canvasRuntime is a Document Runtime whose state is the canvas Markdown
+// itself: it exports that Markdown, its header and paragraphs as blocks, and
+// applies a whole-canvas block_replace to the body in pool.
func canvasRuntime(t *testing.T, computes *atomic.Int32, replaced chan<- string, pool func() *pgxpool.Pool) *httptest.Server {
t.Helper()
digest := func(data []byte) string {
@@ -108,7 +136,7 @@ func canvasRuntime(t *testing.T, computes *atomic.Int32, replaced chan<- string,
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
- if request.ExpectedSeq != nil && *request.ExpectedSeq != seq {
+ if request.ExpectedSeq != nil && *request.ExpectedSeq != seq || canvasConflicted(documentID) {
write(nodeservicesinternal.ConflictedDocumentChangeResult{Status: "conflicted", Reason: "stale_head"})
return
}
@@ -274,11 +302,11 @@ func TestVoiceLiveWritesAndChangesAnEmailCanvasAsTheAgent(t *testing.T) {
if changed["status"] != "changed" || next == "" || next == revisionID {
t.Fatalf("edit=%v", changed)
}
+ // The canvas title follows the subject, so the model never renames it.
+ <-replaced
if content := <-replaced; content != "```\ncanvas: gmail\nto: [email protected]\ncc: [email protected]\nsubject: Next Wed barbecue pickup\nstatus: editing\nlink:\nexternal_id:\n```\n" {
t.Fatalf("replacement=%q", content)
}
- // The canvas title follows the subject, so the model never renames it.
- <-replaced
if countRows(t, env, `SELECT count(*) FROM documents WHERE id=$1 AND title='Next Wed barbecue pickup'`, documentID) != 1 {
t.Fatal("the canvas title did not follow the subject")
}
@@ -305,6 +333,15 @@ func TestVoiceLiveWritesAndChangesAnEmailCanvasAsTheAgent(t *testing.T) {
outdated["revision_id"] != rewritten["revision_id"] || !strings.Contains(outdated["body"].(string), "Barbecue is on me!") {
t.Fatalf("body edit over a newer body=%v", outdated)
}
+ drain := func() {
+ for {
+ select {
+ case <-replaced:
+ default:
+ return
+ }
+ }
+ }
stateOf := func() string {
t.Helper()
var state []byte
@@ -313,6 +350,27 @@ func TestVoiceLiveWritesAndChangesAnEmailCanvasAsTheAgent(t *testing.T) {
}
return string(state)
}
+ // A change that lands while an edit is made, here after its title step,
+ // is redone on the latest canvas as new operations and lands once.
+ canvasConflicts.Store(documentID, &canvasConflictPlan{skip: 2, conflicts: 1})
+ before := computes.Load()
+ redone := call("edit-3c", "edit_email_canvas", map[string]string{"document_id": documentID, "revision_id": rewritten["revision_id"].(string),
+ "subject": "Saturday barbecue pickup", "body": "Hi Amy,\n\nBarbecue on Saturday!"})
+ drain()
+ if state := stateOf(); redone["status"] != "changed" || !strings.Contains(state, "subject: Saturday barbecue pickup\n") ||
+ !strings.HasSuffix(state, "```\n\nHi Amy,\n\nBarbecue on Saturday!\n") || computes.Load()-before < 3 ||
+ countRows(t, env, `SELECT count(*) FROM documents WHERE id=$1 AND title='Saturday barbecue pickup'`, documentID) != 1 {
+ t.Fatalf("edit redone after a conflict=%v state=%q", redone, state)
+ }
+ // One that keeps conflicting gives up after a few tries with the latest
+ // canvas, and nothing more changes.
+ canvasConflicts.Store(documentID, &canvasConflictPlan{conflicts: 3})
+ before, unchanged := computes.Load(), stateOf()
+ gaveUp := call("edit-3d", "edit_email_canvas", map[string]string{"document_id": documentID, "revision_id": redone["revision_id"].(string), "body": "Hi Amy,\n\nNever mind."})
+ canvasConflicts.Delete(documentID)
+ if gaveUp["error"] != "canvas_changed" || gaveUp["revision_id"] != redone["revision_id"] || computes.Load() != before || stateOf() != unchanged {
+ t.Fatalf("edit that keeps conflicting=%v state=%q", gaveUp, stateOf())
+ }
setState := func(state string) string {
t.Helper()
if _, err := bodies.Exec(t.Context(), `UPDATE document_bodies SET seq=seq+1,state=$2 WHERE document_id=$1`, documentID, []byte(state)); err != nil {