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(voice): bound check_task reports and leave code alone when inlining images

LeoXingae2796a93043
diff --git a/apps/teamhub/internal/voice/voice_live.go b/apps/teamhub/internal/voice/voice_live.go
index 20e9c847..1637872d 100644
--- a/apps/teamhub/internal/voice/voice_live.go
+++ b/apps/teamhub/internal/voice/voice_live.go
@@ -1040,7 +1040,7 @@ func (s *Service) ExecuteLiveToolCall(ctx context.Context, request teamhubintern
if err != nil {
return "", fmt.Errorf("check foreground transition: %w", err)
}
- if switching && request.Name != "list_tasks" && request.Name != "recall_memory" && request.Name != "read_document" {
+ if switching && request.Name != "list_tasks" && request.Name != "check_task" && request.Name != "recall_memory" && request.Name != "read_document" {
return voicelive.Output(map[string]string{"status": "switching", "detail": "The previous foreground is stopping. This operation was not accepted; retry after the transition."}), nil
}
digest := sha256.Sum256([]byte(request.TurnId + "\x00" + request.Name + "\x00" + request.Arguments))
diff --git a/apps/teamhub/internal/voice/voice_live_tasks.go b/apps/teamhub/internal/voice/voice_live_tasks.go
index a378b95b..cd1340bb 100644
--- a/apps/teamhub/internal/voice/voice_live_tasks.go
+++ b/apps/teamhub/internal/voice/voice_live_tasks.go
@@ -429,7 +429,7 @@ func (s *Service) checkVoiceLiveTask(ctx context.Context, session voicelive.Sess
return "", err
}
reports, err := s.queries.ListVoiceLiveTaskReports(ctx, store.ListVoiceLiveTaskReportsParams{
- TaskID: task.WorkItemID, TenantID: session.TenantID, RowLimit: voicelive.MaxCheckedReports,
+ TaskID: task.WorkItemID, TenantID: session.TenantID, RowLimit: voicelive.MaxCheckedReports + 1,
})
if err != nil {
return "", err
@@ -447,8 +447,11 @@ func (s *Service) checkVoiceLiveTask(ctx context.Context, session voicelive.Sess
for _, approval := range view.PendingApprovals {
checked.Approvals = append(checked.Approvals, approval.Title)
}
- for index := len(reports) - 1; index >= 0; index-- {
- checked.Reports = append(checked.Reports, voicelive.CheckedActivity{At: reports[index].CreatedAt.Time, Text: reports[index].Body})
+ if len(reports) > voicelive.MaxCheckedReports {
+ reports, checked.OlderReports = reports[:voicelive.MaxCheckedReports], true
+ }
+ for _, report := range reports {
+ checked.Reports = append(checked.Reports, voicelive.CheckedActivity{At: report.CreatedAt.Time, Text: report.Body})
}
return voicelive.Output(voicelive.CheckTaskOutput(time.Now(), checked)), nil
}
diff --git a/apps/teamhub/internal/voicelive/document_images.go b/apps/teamhub/internal/voicelive/document_images.go
index d83df068..1e446a17 100644
--- a/apps/teamhub/internal/voicelive/document_images.go
+++ b/apps/teamhub/internal/voicelive/document_images.go
@@ -33,10 +33,67 @@ func PlaygroundImages(markdown string) []string {
}
// InlineImages points each image at the data URI given for its destination.
+// Text inside code spans and code blocks is left as written.
func InlineImages(markdown string, uris map[string]string) string {
- pairs := make([]string, 0, 2*len(uris))
+ source := []byte(markdown)
+ root := goldmark.New(goldmark.WithExtensions(extension.GFM)).Parser().Parse(goldmarktext.NewReader(source))
+ var code [][2]int
+ _ = ast.Walk(root, func(node ast.Node, entering bool) (ast.WalkStatus, error) {
+ if !entering {
+ return ast.WalkContinue, nil
+ }
+ switch node.(type) {
+ case *ast.FencedCodeBlock, *ast.CodeBlock:
+ lines := node.Lines()
+ if lines.Len() > 0 {
+ code = append(code, [2]int{lines.At(0).Start, lines.At(lines.Len() - 1).Stop})
+ }
+ return ast.WalkSkipChildren, nil
+ case *ast.CodeSpan:
+ for child := node.FirstChild(); child != nil; child = child.NextSibling() {
+ if text, ok := child.(*ast.Text); ok {
+ code = append(code, [2]int{text.Segment.Start, text.Segment.Stop})
+ }
+ }
+ return ast.WalkSkipChildren, nil
+ }
+ return ast.WalkContinue, nil
+ })
+ inCode := func(at int) bool {
+ return slices.ContainsFunc(code, func(span [2]int) bool { return at >= span[0] && at < span[1] })
+ }
+ type edit struct {
+ at, end int
+ uri string
+ }
+ var edits []edit
for destination, uri := range uris {
- pairs = append(pairs, "]("+destination, "]("+uri)
+ target := "](" + destination
+ for from := 0; ; {
+ index := strings.Index(markdown[from:], target)
+ if index < 0 {
+ break
+ }
+ at := from + index
+ end := at + len(target)
+ from = end
+ // The destination ends here, not inside a longer path.
+ if end < len(markdown) && !strings.ContainsRune(") \t\n", rune(markdown[end])) {
+ continue
+ }
+ if !inCode(at) {
+ edits = append(edits, edit{at + 2, end, uri})
+ }
+ }
+ }
+ slices.SortFunc(edits, func(a, b edit) int { return a.at - b.at })
+ var out strings.Builder
+ last := 0
+ for _, e := range edits {
+ out.WriteString(markdown[last:e.at])
+ out.WriteString(e.uri)
+ last = e.end
}
- return strings.NewReplacer(pairs...).Replace(markdown)
+ out.WriteString(markdown[last:])
+ return out.String()
}
diff --git a/apps/teamhub/internal/voicelive/playground_test.go b/apps/teamhub/internal/voicelive/playground_test.go
index da6edb5d..135da48f 100644
--- a/apps/teamhub/internal/voicelive/playground_test.go
+++ b/apps/teamhub/internal/voicelive/playground_test.go
@@ -74,9 +74,10 @@ func TestEveryToolSchemaHasARequiredList(t *testing.T) {
func TestPlaygroundImagesInlineOnlyPlaygroundFiles(t *testing.T) {
markdown := "# Title\n\n![one](./playground/workitem_1/a.png)\n\n![web](https://example.com/b.png)\n\n" +
"![again](./playground/workitem_1/a.png) ![two](playground/room_1/c.jpg \"caption\")\n\n" +
- "```\n![code](./playground/workitem_1/d.png)\n```\n\n![folder](./playground/workitem_1/) ![bare](e.png)\n"
+ "```\n![code](./playground/workitem_1/a.png)\n```\n\n`![span](./playground/workitem_1/a.png)` ![longer](./playground/workitem_1/a.png.bak)\n\n" +
+ "![folder](./playground/workitem_1/) ![bare](e.png)\n"
images := PlaygroundImages(markdown)
- if want := []string{"./playground/workitem_1/a.png", "playground/room_1/c.jpg"}; !slices.Equal(images, want) {
+ if want := []string{"./playground/workitem_1/a.png", "playground/room_1/c.jpg", "./playground/workitem_1/a.png.bak"}; !slices.Equal(images, want) {
t.Fatalf("images=%q, want %q", images, want)
}
inlined := InlineImages(markdown, map[string]string{images[0]: "data:image/png;base64,QQ==", images[1]: "data:image/jpeg;base64,Qg=="})
@@ -85,6 +86,11 @@ func TestPlaygroundImagesInlineOnlyPlaygroundFiles(t *testing.T) {
t.Fatalf("inlined lacks %q:\n%s", want, inlined)
}
}
+ for _, kept := range []string{"![code](./playground/workitem_1/a.png)", "`![span](./playground/workitem_1/a.png)`", "![longer](./playground/workitem_1/a.png.bak)"} {
+ if !strings.Contains(inlined, kept) {
+ t.Fatalf("inlined changed %q:\n%s", kept, inlined)
+ }
+ }
}
func TestImageAnchorFindsTheBlockAnImageGoesBelow(t *testing.T) {
diff --git a/apps/teamhub/internal/voicelive/tasks.go b/apps/teamhub/internal/voicelive/tasks.go
index 3a17a730..12e1b34d 100644
--- a/apps/teamhub/internal/voicelive/tasks.go
+++ b/apps/teamhub/internal/voicelive/tasks.go
@@ -5,6 +5,7 @@ import (
"strconv"
"strings"
"time"
+ "unicode/utf8"
)
// Task tools start and steer the Room Agent's work. Each task is its own
@@ -145,7 +146,7 @@ var taskTools = []Tool{
},
{
Name: ToolCheckTask,
- Description: "Read one of the user's tasks in full, as its coordinator does: its state and since when, the latest steps, the approvals it waits on, how many messages wait for it, and every report it sent back, oldest first and each up to 4000 characters. " +
+ Description: "Read one of the user's tasks in full, as its coordinator does: its state and since when, the latest steps, the approvals it waits on, how many messages wait for it, and its latest reports, newest first and about 4000 characters in all; older_reports_omitted means earlier ones were left out. " +
"list_tasks shows only the start of the latest report: use this when the user asks for details it left out (which models, what each option costs, what exactly it found or why it stopped), or before saying how a task really stands. " +
"A report may hold lines addressed to you, such as how to deliver its files; those are not for the user. Speak of the work in the first person.",
Parameters: objectSchema([]string{"task_id"}, map[string]any{
@@ -279,14 +280,15 @@ func TaskAnswer(details string) string {
return "The user said this about your task by voice; take it into account and go on: " + details
}
-// MaxCheckedReports is how many of a task's latest reports check_task shows.
+// MaxCheckedReports is how many of a task's latest reports check_task reads.
const MaxCheckedReports = 10
-// checkedReportRunes bounds each report check_task shows, as task check does.
-const checkedReportRunes = 4000
+// checkedReportsRunes bounds all the report text one check_task returns: the
+// output stays in the realtime context for every later turn of the call.
+const checkedReportsRunes = 4000
// CheckedTask is one task as check_task reads it: its Task's state and the
-// reports it sent back, oldest first.
+// reports it sent back, newest first.
type CheckedTask struct {
TaskID, Goal, Folder, Status, Reason string
StatusSince time.Time
@@ -294,6 +296,8 @@ type CheckedTask struct {
Approvals []string
QueuedMessages int
Reports []CheckedActivity
+ // OlderReports is set when the task has reports older than Reports.
+ OlderReports bool
}
// CheckedActivity is one dated step or report.
@@ -309,13 +313,19 @@ func CheckTaskOutput(now time.Time, task CheckedTask) map[string]any {
activity = append(activity, map[string]string{"ago": SpokenDuration(now.Sub(step.At)), "kind": step.Kind, "summary": step.Text})
}
reports := make([]map[string]string, 0, len(task.Reports))
+ left := checkedReportsRunes
for _, report := range task.Reports {
- reports = append(reports, map[string]string{"ago": SpokenDuration(now.Sub(report.At)), "text": truncateRunes(report.Text, checkedReportRunes)})
+ if left <= 0 {
+ break
+ }
+ text := truncateRunes(report.Text, left)
+ left -= utf8.RuneCountInString(text)
+ reports = append(reports, map[string]string{"ago": SpokenDuration(now.Sub(report.At)), "text": text})
}
output := map[string]any{
"task_id": task.TaskID, "goal": truncateRunes(task.Goal, 300), "folder": task.Folder,
"status": task.Status, "status_for": SpokenDuration(now.Sub(task.StatusSince)),
- "recent_activity": activity, "reports": reports,
+ "recent_activity": activity, "reports": reports, "older_reports_omitted": task.OlderReports || len(reports) < len(task.Reports),
"pending_approvals": task.Approvals, "queued_messages": task.QueuedMessages,
}
if task.Reason != "" {
diff --git a/apps/teamhub/internal/voicelive/tasks_test.go b/apps/teamhub/internal/voicelive/tasks_test.go
index ebcff54f..f4aed1e0 100644
--- a/apps/teamhub/internal/voicelive/tasks_test.go
+++ b/apps/teamhub/internal/voicelive/tasks_test.go
@@ -3,6 +3,8 @@ package voicelive
import (
"strings"
"testing"
+ "time"
+ "unicode/utf8"
)
func TestTaskIDRoundTrip(t *testing.T) {
@@ -60,3 +62,22 @@ func TestTaskListGivesTheLatestReport(t *testing.T) {
t.Fatalf("tasks: %v", tasks)
}
}
+
+func TestCheckTaskOutputBoundsAllReportText(t *testing.T) {
+ now := time.Now()
+ long := strings.Repeat("\u00e9", 3000)
+ output := CheckTaskOutput(now, CheckedTask{TaskID: "t1", StatusSince: now, Reports: []CheckedActivity{
+ {At: now, Text: long}, {At: now, Text: long}, {At: now, Text: "oldest"},
+ }})
+ reports := output["reports"].([]map[string]string)
+ total := 0
+ for _, report := range reports {
+ total += utf8.RuneCountInString(report["text"])
+ }
+ if len(reports) != 2 || reports[0]["text"] != long || total != checkedReportsRunes || output["older_reports_omitted"] != true {
+ t.Fatalf("reports=%d total=%d omitted=%v", len(reports), total, output["older_reports_omitted"])
+ }
+ if output := CheckTaskOutput(now, CheckedTask{Reports: []CheckedActivity{{At: now, Text: "done"}}}); output["older_reports_omitted"] != false {
+ t.Fatalf("a short history is whole: %v", output)
+ }
+}
diff --git a/apps/teamhub/tests/integration/voice_live_test.go b/apps/teamhub/tests/integration/voice_live_test.go
index 871e967a..3442b49c 100644
--- a/apps/teamhub/tests/integration/voice_live_test.go
+++ b/apps/teamhub/tests/integration/voice_live_test.go
@@ -684,10 +684,10 @@ func TestVoiceLiveTasksReportResultsAndWhatTheyWaitFor(t *testing.T) {
if _, err := env.pool.Exec(t.Context(), `UPDATE work_items SET task_execution_status='waiting_for_approval' WHERE id=$1`, waiting); err != nil {
t.Fatal(err)
}
- // check_task reads one task as its coordinator does, every report included.
+ // check_task reads one task as its coordinator does, with its latest reports.
checked := tool("lt_check", "check-1", "check_task", map[string]any{"task_id": first["task_id"]})
reports, _ := checked["reports"].([]any)
- if checked["status"] != "idle" || checked["folder"] != "./playground/"+workItem(first["task_id"])+"/" || len(reports) != 1 ||
+ if checked["status"] != "idle" || checked["folder"] != "./playground/"+workItem(first["task_id"])+"/" || len(reports) != 1 || checked["older_reports_omitted"] != false ||
reports[0].(map[string]any)["text"] != "Sony at 1899 is the best fit, added to the cart." {
t.Fatalf("check_task=%v", checked)
}
diff --git a/apps/voice-runtime/src/live-worker.ts b/apps/voice-runtime/src/live-worker.ts
index c6be53e5..4fb6af2c 100644
--- a/apps/voice-runtime/src/live-worker.ts
+++ b/apps/voice-runtime/src/live-worker.ts
@@ -70,7 +70,7 @@ const retryMs = 300;
* hears its real result instead of a timeout.
*/
const confirmedWriteTimeoutMs = 60_000;
-/** A card's files, or an image put into a document, upload from the Computer for up to two minutes. */
+/** A card's files, or images a document is written with or gets, come from the Computer for up to two minutes. */
const sendCardTimeoutMs = 130_000;
/** A local browser step may wake a sleeping Computer before it runs. */
const browserTimeoutMs = 70_000;
@@ -1575,7 +1575,9 @@ export class LiveVoiceWorker {
const result = await this.client.executeLiveToolCall(
request,
AbortSignal.timeout(
- call.name === "send_card" || call.name === "insert_document_image"
+ call.name === "send_card" ||
+ call.name === "insert_document_image" ||
+ call.name === "create_document"
? sendCardTimeoutMs
: parseArguments(call)?.confirmed === true
? confirmedWriteTimeoutMs