Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

fix(daemon): scan comment roots before bulk reads in agent catch-up (MUL-5372) (#6093)

Bohan Jiange45a8f6c1212
diff --git a/server/internal/daemon/execenv/execenv_test.go b/server/internal/daemon/execenv/execenv_test.go
index 5579f58dc3..095022cfb2 100644
--- a/server/internal/daemon/execenv/execenv_test.go
+++ b/server/internal/daemon/execenv/execenv_test.go
@@ -4730,7 +4730,7 @@ func TestInjectRuntimeConfigBriefKeepsStaticCatchUpRead(t *testing.T) {
}
s := string(data)
- if !strings.Contains(s, "multica issue comment list "+issueID+" --recent 10 --output json") {
+ if !strings.Contains(s, "multica issue comment list "+issueID+" --roots-only --summary --output json") {
t.Errorf("brief must keep the static catch-up read\n---\n%s", s)
}
if strings.Contains(s, "--recent 20") {
@@ -4800,11 +4800,11 @@ func TestInjectRuntimeConfigBriefOmitsResumedThreadAnchor(t *testing.T) {
}
}
-// TestInjectRuntimeConfigAssignmentTriggerMentionsRecent pins that the
+// TestInjectRuntimeConfigAssignmentTriggerScansRootsFirst pins that the
// assignment-triggered Workflow keeps comment catch-up mandatory while bounding
-// the mandatory first read to the recent active-thread window. Older context
-// stays reachable through explicit pagination.
-func TestInjectRuntimeConfigAssignmentTriggerMentionsRecent(t *testing.T) {
+// the mandatory first read to a roots scan. Older context stays reachable
+// through explicit pagination.
+func TestInjectRuntimeConfigAssignmentTriggerScansRootsFirst(t *testing.T) {
t.Parallel()
dir := t.TempDir()
@@ -4820,17 +4820,20 @@ func TestInjectRuntimeConfigAssignmentTriggerMentionsRecent(t *testing.T) {
// Mandatory comment catch-up must stay, but the required first read is
// bounded to recent active threads instead of the full flat timeline.
for _, want := range []string{
- "multica issue comment list issue-1 --recent 10 --output json",
+ "multica issue comment list issue-1 --roots-only --summary --output json",
"this is mandatory, not optional",
"Skipping this step is the most common cause",
} {
if !strings.Contains(s, want) {
- t.Errorf("assignment Workflow regressed mandatory recent-first catch-up, missing %q\n---\n%s", want, s)
+ t.Errorf("assignment Workflow regressed mandatory scan-first catch-up, missing %q\n---\n%s", want, s)
}
}
- // Older context must remain reachable through pagination.
+ // Older context must remain reachable through pagination. The cursor label
+ // and flags are documented in `## Available Commands` rather than restated
+ // inside the step (MUL-5372), so assert the label itself, not the literal
+ // `Next thread cursor: ...` stderr line the old step-3 copy quoted.
for _, want := range []string{
- "Next thread cursor:",
+ "Next thread cursor",
"--before",
"--before-id",
} {
@@ -4858,6 +4861,69 @@ func TestInjectRuntimeConfigAssignmentTriggerMentionsRecent(t *testing.T) {
}
}
+// TestInjectRuntimeConfigCatchUpScansRootsFirst locks in MUL-5372: the
+// mandatory step-3 catch-up leads with a bounded `--roots-only --summary` scan
+// and an explicit per-thread drill-down, instead of making `--recent 10` the
+// required first read. `--recent N` caps threads, not comments — it returns every
+// descendant of each thread — so as the mandatory read it handed every run the
+// issue's entire comment history, and duplicated the bounded thread read the
+// per-turn message already points at on comment-triggered turns.
+//
+// It also pins the placement rule: the workflow step names only the reads it
+// mandates, while flag semantics (including the saturation trap) live once in
+// `## Available Commands`. Restating them per step is what bloated the step.
+func TestInjectRuntimeConfigCatchUpScansRootsFirst(t *testing.T) {
+ t.Parallel()
+
+ dir := t.TempDir()
+ if _, err := InjectRuntimeConfig(dir, "claude", TaskContextForEnv{IssueID: "issue-1"}); err != nil {
+ t.Fatalf("InjectRuntimeConfig failed: %v", err)
+ }
+ data, err := os.ReadFile(filepath.Join(dir, "CLAUDE.md"))
+ if err != nil {
+ t.Fatalf("read CLAUDE.md: %v", err)
+ }
+ s := string(data)
+
+ for _, want := range []string{
+ // The cheap scan is the first thing step 3 asks for.
+ "multica issue comment list issue-1 --roots-only --summary --output json",
+ // ...followed by an explicit, bounded drill-down.
+ "multica issue comment list issue-1 --thread <thread-id> --tail 30 --output json",
+ // The saturation semantics that made --recent 10 misleading are stated
+ // once, in the flag reference rather than in the step.
+ "caps THREADS, not comments",
+ "no per-thread cap",
+ "fewer than N root threads",
+ } {
+ if !strings.Contains(s, want) {
+ t.Errorf("brief missing bounded catch-up guidance %q\n---\n%s", want, s)
+ }
+ }
+
+ // The workflow steps must not hand the agent a ready-to-paste bulk read;
+ // that is what made the mandatory step pull whole histories.
+ if strings.Contains(s, "multica issue comment list issue-1 --recent") {
+ t.Errorf("workflow steps must not present an issue-scoped --recent command\n---\n%s", s)
+ }
+ // The saturation warning belongs to the flag reference, which introduces
+ // the flag generically as `--recent N`.
+ if !strings.Contains(s, "--recent N") {
+ t.Errorf("Available Commands must still document --recent N\n---\n%s", s)
+ }
+
+ // The catch-up stays mandatory — this change is about payload shape, not
+ // about letting agents skip context and act on stale instructions.
+ for _, want := range []string{
+ "this is mandatory, not optional",
+ "Skipping this step is the most common cause",
+ } {
+ if !strings.Contains(s, want) {
+ t.Errorf("step 3 must stay mandatory, missing %q\n---\n%s", want, s)
+ }
+ }
+}
+
// TestInjectRuntimeConfigIssueMetadataSectionScope locks in MUL-2017:
// the `## Issue Metadata` section (semantic guide + recommended keys +
// pin/clear rules) and the `metadata list` workflow step are emitted only
diff --git a/server/internal/daemon/execenv/reply_instructions.go b/server/internal/daemon/execenv/reply_instructions.go
index dbdaa12af2..f86cbd4be8 100644
--- a/server/internal/daemon/execenv/reply_instructions.go
+++ b/server/internal/daemon/execenv/reply_instructions.go
@@ -81,8 +81,11 @@ func BuildResumedCommentsHint(issueID, triggerCommentID, triggerThreadID string)
// timeline (oldest-first, server cap 2000), point the agent at the triggering
// CONVERSATION: `--thread <trigger> --tail 30` returns that thread's root plus
// its 30 newest replies (root is always included, even at --tail 0) — the
-// context the triggering comment actually needs. A `--recent 10` pointer is kept
-// for cross-thread background the agent can pull on judgment.
+// context the triggering comment actually needs. Cross-thread background is a
+// cheap `--roots-only --summary` scan; the hint deliberately does NOT name
+// `--recent`, whose saturation trap and pagination live once in the brief's
+// `## Available Commands` (MUL-5372). Per-turn hints name only the reads they
+// actually want the agent to run.
//
// Both surfaces call this so the cold fallback cannot drift between them (same
// single-source rule as BuildNewCommentsHint, PR #2816). Returns "" when there
@@ -97,8 +100,9 @@ func BuildColdCommentsHint(issueID, triggerCommentID, triggerThreadID string) st
"Read the triggering conversation first: "+
"`multica issue comment list %s --thread %s --tail 30 --output json` "+
"(that thread's root + its 30 newest replies). "+
- "Need cross-thread background? `multica issue comment list %s --recent 10 --output json` "+
- "(resolved threads come back folded — `--full` to expand).\n\n",
+ "Need cross-thread background? Scan the other threads cheaply with "+
+ "`multica issue comment list %s --roots-only --summary --output json` and expand only "+
+ "what looks relevant.\n\n",
issueID, threadID, issueID,
)
}
diff --git a/server/internal/daemon/execenv/runtime_config_sections.go b/server/internal/daemon/execenv/runtime_config_sections.go
index 349bb4362a..5dfd4f8b5d 100644
--- a/server/internal/daemon/execenv/runtime_config_sections.go
+++ b/server/internal/daemon/execenv/runtime_config_sections.go
@@ -257,7 +257,7 @@ func writeAvailableCommands(b *strings.Builder) {
b.WriteString("Prefer `--output json` for structured data. The default brief lists only the core agent loop and common issue create/update tasks; for everything else run `multica --help` or `multica <command> --help`.\n\n")
b.WriteString("### Core\n")
b.WriteString("- `multica issue get <id> --output json` — full issue.\n")
- b.WriteString("- `multica issue comment list <issue-id> [--thread <comment-id> [--tail N] | --recent N] [--before <ts> --before-id <uuid>] [--since <RFC3339>] [--full] --output json` — thread-aware comment reads. Resolved threads come back folded by default on complete-thread reads (default list, `--recent`, `--thread` without `--tail`); pass `--full` to expand. Page older replies / threads with `--before`/`--before-id` (stderr labels: `Next reply cursor`, `Next thread cursor`); `--help` for full semantics.\n")
+ b.WriteString("- `multica issue comment list <issue-id> [--roots-only] [--summary] [--thread <comment-id> [--tail N] | --recent N] [--before <ts> --before-id <uuid>] [--since <RFC3339>] [--full] --output json` — thread-aware comment reads. `--recent N` caps THREADS, not comments: every returned thread carries its root plus EVERY descendant with no per-thread cap, so on an issue with fewer than N root threads it hands you the entire history apart from the resolved threads it folds. `--roots-only` (top-level comments with `reply_count` + `last_activity_at`) and `--summary` (clip each body to a preview) are how you bound a wide read; `--thread <id> --tail N` is how you bound a deep one. Resolved threads come back folded by default on complete-thread reads (default list, `--recent`, `--thread` without `--tail`); pass `--full` to expand. Page older replies / threads with `--before`/`--before-id` (stderr labels: `Next reply cursor`, `Next thread cursor`); `--help` for full semantics.\n")
b.WriteString("- `multica issue create --title \"...\" [--description-file <path>] [--priority X] [--status X] [--assignee X | --assignee-id <uuid>] [--parent <issue-id>] [--stage N] [--project <project-id>] [--due-date <RFC3339>] [--attachment <path>]` — create an issue. For agent-authored long descriptions prefer `--description-file <path>` (heredoc stdin can swallow trailing flags, #4182). Write that file inside your working directory (e.g. `./description.md`), never `/tmp` or shared paths, and treat a failed write as fatal — the CLI rejects a path outside the workdir so a stale file from another run can't leak in (MUL-4252).\n")
b.WriteString("- `multica issue update <id> [--title X] [--description-file <path>] [--priority X] [--status X] [--assignee X] [--parent <issue-id>] [--stage N] [--project <project-id>] [--due-date <RFC3339>]` — update fields; pass `--parent \"\"` to clear parent.\n")
b.WriteString("- `multica issue status <id> <status>` — flip status (todo / in_progress / in_review / done / blocked / backlog / cancelled).\n")
@@ -446,6 +446,22 @@ func writeWorkflowAutopilot(b *strings.Builder, ctx TaskContextForEnv) {
// "own the status arc" and "do not touch the status" can never be read as
// peer instructions with no arbitration.
//
+// Step 3 asks for a roots scan first, not `--recent 10` (MUL-5372). `--recent N`
+// caps THREADS, not comments: each returned thread carries its root plus every
+// descendant with no depth cap, so on an issue with fewer than N root threads it
+// returns the entire comment history. Because this step is mandatory and fires on
+// every run, making it the bulk read meant every reply turn re-read the whole
+// issue — and, on comment-triggered turns, duplicated the bounded thread read the
+// per-turn message had already pointed at (see daemon.buildCommentPrompt and
+// BuildColdCommentsHint). `--roots-only --summary` keeps the anti-stale property
+// that step exists for — the agent still sees every thread that exists — at a
+// fraction of the payload, and the drill-down stays explicit.
+//
+// The step names ONLY the two reads it mandates. Flag semantics — including the
+// `--recent N` saturation trap above — belong to `## Available Commands`, which
+// is the single discovery point for the comment-read surface; repeating them per
+// step is what made this one bloat in the first place.
+//
// Ordinary agents own the full status arc for their issue: open with
// in_progress, deliver with in_review. Squad leaders share the opening
// in_progress step so the parent leaves todo as soon as coordination
@@ -466,7 +482,7 @@ func writeWorkflowIssue(b *strings.Builder, ctx TaskContextForEnv) {
b.WriteString("**Steps 1–6 — both modes**\n\n")
fmt.Fprintf(b, "1. Run `multica issue get %s --output json` to understand the issue context\n", ctx.IssueID)
fmt.Fprintf(b, "2. Run `multica issue metadata list %s --output json` to see what prior agents pinned — best-effort, empty `{}` and CLI failures are normal. See the `## Issue Metadata` section above for what to look for.\n", ctx.IssueID)
- fmt.Fprintf(b, "3. Run `multica issue comment list %s --recent 10 --output json` to catch up on recent active comment threads — this is mandatory, not optional. Earlier comments often carry context the issue body lacks (e.g. which repo to work in, the prior agent's findings, the reason the issue was reassigned to you). Skipping this step is the most common cause of agents acting on stale or incomplete instructions. Resolved threads come back folded — `--full` to expand. If the recent window shows that older context is needed, page older threads with the stderr `Next thread cursor:` values and the matching `--before` / `--before-id` flags until you have enough history. In Reply mode the per-turn user message also tells you which thread to start from.\n", ctx.IssueID)
+ fmt.Fprintf(b, "3. Catch up on the comment history — this is mandatory, not optional, but read it in two bounded steps instead of one bulk pull. First scan every thread cheaply: `multica issue comment list %s --roots-only --summary --output json`, which tells you what discussion exists without paying for its contents. Then expand only the threads that matter: `multica issue comment list %s --thread <thread-id> --tail 30 --output json`. Earlier comments often carry context the issue body lacks (e.g. which repo to work in, the prior agent's findings, the reason the issue was reassigned to you). Skipping this step is the most common cause of agents acting on stale or incomplete instructions — so always run the scan, even when the trigger looks self-contained. In Reply mode the per-turn user message names the thread to expand first; the scan is how you decide whether any OTHER thread is also relevant. If these two reads genuinely are not enough, the rest of the read surface and its pagination cursors are documented once in `## Available Commands` above.\n", ctx.IssueID, ctx.IssueID)
b.WriteString("4. Complete the task within your Agent Identity boundaries. Do not investigate, implement, create issues, update issues, or delegate if your Agent Identity forbids that action; if your role is delegation-only, perform the allowed delegation work and stop once that outcome is delivered.\n")
if ctx.IsSquadLeader {
fmt.Fprintf(b, "5. **Post your final results as a comment** (unless your outcome is `no_action` — in that case, calling `multica squad activity %s no_action --reason \"...\"` alone is sufficient; you MUST exit without posting any comment. DO NOT post a comment announcing no_action or saying you are exiting silently): post it with `multica issue comment add %s` using the platform-correct non-inline mode from ## Comment Formatting (never inline `--content`). Your results are only visible to the user if posted via this CLI call; text in your terminal or run logs is NOT delivered.\n", ctx.IssueID, ctx.IssueID)
diff --git a/server/internal/daemon/prompt.go b/server/internal/daemon/prompt.go
index e80578c069..dd63a23976 100644
--- a/server/internal/daemon/prompt.go
+++ b/server/internal/daemon/prompt.go
@@ -104,7 +104,7 @@ func buildPromptBody(task Task, provider string) string {
fmt.Fprintf(&b, "> %s\n\n", task.HandoffNote)
}
fmt.Fprintf(&b, "Start by running `multica issue get %s --output json` to understand your task, then complete it.\n", task.IssueID)
- fmt.Fprintf(&b, "For comment history, follow the rule in your runtime workflow file (assignment-triggered tasks treat the read as mandatory). Start with `multica issue comment list %s --recent 10 --output json` to read the 10 most recently active threads, then page older threads via the stderr `Next thread cursor: ...` line and the matching `--before` / `--before-id` until you have enough history. Resolved threads come back folded — `--full` to expand. `--since <RFC3339>` is still available for incremental polling and may combine with `--recent`.\n", task.IssueID)
+ fmt.Fprintf(&b, "For comment history, follow the rule in your runtime workflow file (assignment-triggered tasks treat the read as mandatory). Scan the threads first with `multica issue comment list %s --roots-only --summary --output json`, then expand only what matters with `--thread <thread-id> --tail 30`. Your runtime workflow file documents the rest of the read surface, including pagination and `--since` for incremental polling.\n", task.IssueID)
return b.String()
}
@@ -299,7 +299,7 @@ func buildCommentPrompt(task Task, provider string) string {
} else if cold := execenv.BuildColdCommentsHint(task.IssueID, task.TriggerCommentID, task.TriggerThreadID); cold != "" {
b.WriteString(cold)
} else {
- fmt.Fprintf(&b, "Read the discussion: `multica issue comment list %s --recent 10 --output json` (resolved threads come back folded — `--full` to expand).\n\n", task.IssueID)
+ fmt.Fprintf(&b, "Read the discussion: scan with `multica issue comment list %s --roots-only --summary --output json`, then expand what matters with `--thread <thread-id> --tail 30`.\n\n", task.IssueID)
}
// Reply routing. When this run coalesced comments spanning MORE THAN ONE
// root thread, answer each thread in its own thread instead of dumping one
diff --git a/server/internal/daemon/prompt_test.go b/server/internal/daemon/prompt_test.go
index eb7f72f3d1..f2e91cfa7c 100644
--- a/server/internal/daemon/prompt_test.go
+++ b/server/internal/daemon/prompt_test.go
@@ -606,25 +606,40 @@ func TestBuildChatPromptSlashSkills(t *testing.T) {
})
}
-// TestBuildPromptDefaultMentionsRecent pins that the catch-all fallback
+// TestBuildPromptDefaultScansRootsFirst pins that the catch-all fallback
// prompt (no trigger comment, no chat, no autopilot, no quick-create)
-// starts assignment-triggered comment catch-up with a bounded recent read,
-// while still keeping older history available through pagination.
-func TestBuildPromptDefaultMentionsRecent(t *testing.T) {
+// starts assignment-triggered comment catch-up with a bounded roots scan and
+// only then offers the full-thread read, while still keeping older history
+// available through pagination.
+func TestBuildPromptDefaultScansRootsFirst(t *testing.T) {
out := BuildPrompt(Task{IssueID: "issue-default-1"}, "claude")
for _, s := range []string{
- "multica issue comment list issue-default-1 --recent 10 --output json",
- "Next thread cursor:",
+ "multica issue comment list issue-default-1 --roots-only --summary --output json",
"--since",
} {
if !strings.Contains(out, s) {
t.Errorf("default BuildPrompt missing %q\n--- output ---\n%s", s, out)
}
}
- // And the default path must NOT inject a --thread example, because there
- // is no trigger comment id to anchor on.
- if strings.Contains(out, "--thread") {
- t.Errorf("default BuildPrompt should NOT mention --thread (no trigger comment to anchor on)\n--- output ---\n%s", out)
+ // MUL-5372: the per-turn prompt names only the reads it wants run. Flag
+ // mechanics — cursors, the --recent saturation trap — live once in the
+ // runtime workflow file's `## Available Commands`, so restating them here
+ // would put the same reference text on every turn.
+ if strings.Contains(out, "--recent") {
+ t.Errorf("default BuildPrompt should not restate the --recent surface\n--- output ---\n%s", out)
+ }
+ if strings.Contains(out, "Next thread cursor:") {
+ t.Errorf("default BuildPrompt should not restate pagination mechanics\n--- output ---\n%s", out)
+ }
+ // MUL-5372: this path now leads with a cheap roots scan, and the scan is
+ // what supplies thread ids, so a generic `--thread <thread-id>` drill-down
+ // is well-founded here. What must still never appear is a CONCRETE anchor —
+ // the default path has no trigger comment to derive one from, and an
+ // interpolated id would send the agent after a thread that does not exist.
+ for _, seg := range strings.Split(out, "--thread")[1:] {
+ if !strings.HasPrefix(seg, " <thread-id>") {
+ t.Errorf("default BuildPrompt must only use the generic --thread <thread-id> placeholder, never a concrete anchor\n--- output ---\n%s", out)
+ }
}
// The legacy "If you need comment history" soft phrasing conflicts with
// the assignment-trigger runtime workflow, which treats reading comments
@@ -724,11 +739,15 @@ func TestBuildPromptColdStartThreadRead(t *testing.T) {
if !strings.Contains(out, "multica issue comment list "+issueID+" --thread thread-root-1 --tail 30 --output json") {
t.Errorf("cold start must point at the triggering thread read, got:\n%s", out)
}
- if !strings.Contains(out, "multica issue comment list "+issueID+" --recent 10 --output json") {
- t.Errorf("cold start cross-thread fallback should use recent 10, got:\n%s", out)
+ // MUL-5372: cross-thread background is a cheap roots scan. The hint names
+ // only the reads it wants run — `--recent` and its saturation trap are
+ // documented once in the brief's `## Available Commands`, so restating the
+ // flag surface here would put reference text on every cold turn.
+ if !strings.Contains(out, "multica issue comment list "+issueID+" --roots-only --summary --output json") {
+ t.Errorf("cold start should offer the cheap roots scan for cross-thread background, got:\n%s", out)
}
- if strings.Contains(out, "--recent 20") {
- t.Errorf("cold start cross-thread fallback still uses recent 20, got:\n%s", out)
+ if strings.Contains(out, "--recent") {
+ t.Errorf("cold start hint should not restate the --recent surface, got:\n%s", out)
}
}