Skip to content

Let assigned agents react to comments on their tasks - #843

Merged
pikann merged 3 commits into
Paca-AI:masterfrom
sebathi:feat/agent-assigned-task-comments
Dec 1, 2026
Merged

pikann merged 3 commits into
Paca-AI:masterfrom
sebathi:feat/agent-assigned-task-comments

Conversation

@sebathi

@sebathi sebathi commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Closes #841.

Problem

An agent assigned to a task only picks up new comments when it is @-mentioned in them. handleMentions in activity_service.go is the single path from a comment to an agent and never looks at the task's assignees. The automation workaround (comment_added → trigger_ai_agent) refires on the agent's own reply comment and cannot batch comments (see the issue).

Change

Two per-agent settings, off by default, on both project and global agents:

  • assigned_comment_trigger — a comment by a person on a task the agent is assigned to starts a comment_mention conversation for it, as an @-mention would. Comments by agents, service accounts and the platform itself (an automation's add_comment) never count, so a reply cannot retrigger its author. A comment that already @-mentions the agent triggers through the mention path only, never twice. The commenter must be allowed to use the agent, as with a mention (same triggergate path).
  • assigned_comment_window_minutes (default 2, 0 = immediate) — further comments on the same task during the window are batched into the same conversation. The first comment fixes when it starts; later ones append their text to agent_pending_comment_triggers (one row per agent and task). The conversation message is the collected comments separated by blank lines; the conversation's comment_id is the last one.

The resulting conversation queues behind parallelism_limit like any other trigger, so nothing changes in dispatch.

How it runs

worker.AssignedCommentConsumer is a consumer group (api.assigned_comment_trigger) on paca.activities, a peer of the autofill, auto-assign and automation listeners, shaped after QuestionNotificationConsumer (own group starting at $, streamReclaimer for unacked entries). Nothing runs on the request that posts the comment.

  • For each task.comment.added it calls ActivitySvc.HandleAssignedComment, which appends the comment to the pending batch of every assigned agent that opted in. A zero window is just a batch that is due at once.
  • The same read loop fires the due batches: right after a read that queued something, and otherwise every 15s. There is no scheduler and no leader lock. Every replica sweeps, and each batch is claimed with DELETE ... WHERE last_comment_id = <the one read>, so it starts one conversation however many replicas read it, and a comment that lands mid-sweep leaves the batch for the next pass.
  • Before firing, a batch is re-checked: if the setting was turned off or the agent is no longer assigned, it is dropped.
  • Delivery is at-least-once, as for the other consumers. A lookup failure leaves the entry unacked for the reclaimer, and a batch ignores a comment it already ends with, so a redelivery is not recorded twice. A pod that dies between starting a conversation and the ack can still repeat it.

Files

  • migrations/000100_add_agent_assigned_comment_trigger.sql — two agents columns plus the pending table.
  • domain/agent/comment_trigger.go, repository/postgres/comment_trigger_repository.go — pending batch type and repository (append / list due / claim).
  • service/task/activity_service.go — HandleAssignedComment and FireDuePendingCommentTriggers for the consumer. AddComment and handleMentions are as on master; the mention trigger and the new one share startCommentConversation.
  • worker/assigned_comment_consumer.go, bootstrap/app.go — consumer and wiring.
  • Agent entity, repository, service (clamp 0–1440 and nil-means-unchanged patch), DTOs and handlers for the two settings.
  • apps/web: Agent type and payloads, a switch plus a window input in the agent settings form, translations in all locales.
  • docs/ai-agent/overview.md — new "Comments on assigned tasks" section under the comment-mention trigger.

Tests

  • activity_service_test.go, with in-memory fakes: zero window, setting off, comment by the agent, comment by a service account, agent already mentioned (exactly one trigger), a two-minute window that batches two comments and fires once, the same event delivered twice, a lookup failure returned for redelivery, a comment arriving mid-sweep, the same batch read by two replicas, and a batch dropped after the setting is turned off or the agent is unassigned.
  • assigned_comment_consumer_test.go, with miniredis: a comment is handed over and acked, a failed one stays pending, other entries (other topics, edited comments, comments with no author, undecodable payloads) are acked untouched, the sweep is due after queueing and on the interval, and the running loop picks up a new comment.

go vet ./... and go test ./... -race pass in services/api; tsc --noEmit, biome check and the locale parity test pass in apps/web.

Not covered

No Postgres-backed test for the new repository (those are skipped without PACA_TEST_PG_DSN). The MCP server does not expose agent settings, so it is untouched.

An agent assigned to a task only picked up new comments when it was
@-mentioned in them: handleMentions was the single path from a comment to
an agent, and it never looked at the task's assignees. The automation
workaround (comment_added -> trigger_ai_agent) retriggers on the agent's
own reply and cannot batch comments.

Add two per-agent settings, off by default:

- assigned_comment_trigger: a comment by a person on a task the agent is
  assigned to starts a comment_mention conversation for it. Comments by
  agents never count, so a reply cannot retrigger its author, and a
  comment that already mentions the agent triggers through the mention
  path only.
- assigned_comment_window_minutes (default 2, 0 = immediate): further
  comments on the same task during the window are batched into the same
  conversation. The first comment fixes when it starts; later ones append
  their text to agent_pending_comment_triggers, one row per agent and
  task, and worker.AssignedCommentScheduler fires the due rows under a
  leader lock, in the same shape as WaitScheduler.

The conversation is authorized as the last person who commented and
queues behind parallelism_limit like any other trigger. Settings flow
through the agent DTOs, the settings form (with translations) and the
agent overview docs.

Closes Paca-AI#841

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Important

Two concurrency defects in the batching path can permanently drop a person's comment or start the same conversation twice, and one smaller gap lets service-account comments through.

Approach: sound — the trigger reuses the existing mention conversation path and the triggergate authorization instead of adding a parallel dispatcher, and batching is opt-in and bounded.

Reviewed changes

  • Data model — migration 000100 adds the two agent settings and the pending-batch table; agent select/insert/update carry the new columns.
  • Trigger path — handleAssignedAgents runs after handleMentions on a person's comment, starting immediately or queuing per (agent, task); the mention refactor shares startCommentConversation.
  • Scheduler — AssignedCommentScheduler fires due batches every 15s under a Valkey leader lock, wired into start/stop.
  • API + UI — domain/DTO/handler plumbing with a 0–1440 clamp, plus the settings switch and window input with locale strings.
  • Docs + tests — new "Comments on assigned tasks" section and five in-memory activity-service cases.

ℹ️ A queued batch is not re-checked against the agent's current state

Turning the setting off or unassigning the agent during a window still fires the already-queued batch; with assigned_comment_window_minutes up to 24h that can be a long and surprising delay. Re-loading the agent (and its current assignees) in FireDuePendingCommentTriggers would close it, or the behaviour could be documented as intended.

Technical details
# Queued batches ignore later opt-out / unassignment

## Affected sites
- `services/api/internal/service/task/activity_service.go:390-408` — `FireDuePendingCommentTriggers` fires from the row only; it never reloads the agent or the task.
- `services/api/internal/service/task/activity_service.go:361-366` — opt-in is checked only when the comment is queued.

## Required outcome
- Decide whether disabling the setting (or unassigning the agent) should cancel a pending batch. If yes, re-check `agent.AssignedCommentTrigger` and the agent's current assignee membership before firing; if no, state it in the docs so the 2-minute default is not mistaken for "immediate off".

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏

Address review findings on the assigned-comment trigger:

- A comment appended between ListDue's snapshot and the unconditional
  delete was removed unseen and lost. Replace Delete with Claim, a delete
  conditional on the last comment id the scheduler read: when a comment
  arrived since, the row is left for the next tick, which then carries
  it. The same claim makes a batch fire at most once.
- The scheduler's leader lock was never renewed, so a tick that outlived
  its TTL let another replica process the same rows. Renew the token
  before every batch, as WaitScheduler does, and stop once it is lost.
- A service account commenting through an API key passed as a person.
  Require a human member instead of only excluding agents.
- A batch queued before the setting was turned off, or before the agent
  was unassigned, still fired when its window elapsed. Re-check both
  before firing and drop the batch otherwise.

Cover each with a test, plus the scheduler's lock handling.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Approach: sound — the new commit keeps the single shared conversation path and replaces the racy delete with an atomic claim, which is the right layer for the at-most-once guarantee.

Reviewed changes

  • Atomic batch claim — Delete is replaced by Claim(agentID, taskID, lastCommentID), which only removes the row while its last_comment_id still matches the ListDue snapshot; a comment that lands mid-run is kept for the next tick and a batch fires at most once even if replicas overlap.
  • Fire-time re-check — pendingCommentStillWanted reloads the agent and task assignees before each batch and drops it when the setting was turned off or the agent was unassigned; lookup failures leave the row for retry.
  • Human-only authors — handleAssignedAgents now requires MemberType == "human", so service-account commenters no longer trigger, with a new regression test.
  • Leader lock renewal — the scheduler renews its token before every batch and stops the tick when the lock is lost; a lost lock is never released or stolen back, and new scheduler tests cover acquire/skip/mid-tick loss.
  • Tests and docs — activity-service cases for the mid-run comment, opt-out, unassignment and proceed stop signal, plus the updated overview wording.

Pullfrog  | View workflow run | Using deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏

@pikann

pikann commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Hi @sebathi, thanks for this PR, nice work! 🙌

Could we use a stream consumer here, like our other consumers, instead of a separate scheduler? It would keep things consistent and make horizontal scaling easier.

QuestionNotificationConsumer is a good example to follow. What do you think?

Replace AssignedCommentScheduler with AssignedCommentConsumer, a consumer
group on the activity stream like the other task listeners. It reads
task.comment.added, queues the comment for every assigned agent that
opted in, and fires the due batches from the same read loop.

Nothing runs on the request that posts the comment any more, and there is
no scheduler or leader lock: every replica sweeps, and the atomic claim
keeps a batch to one conversation. A zero window is a batch that is due
at once, so both paths fire through the same claim.

A lookup failure leaves the event unacked for the reclaimer, and a batch
ignores a comment it already ends with, so a redelivery is not recorded
twice.
@sebathi

sebathi commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

Makes sense, done in a8573f9. AssignedCommentScheduler is gone and there is now an AssignedCommentConsumer: its own consumer group on paca.activities, shaped after QuestionNotificationConsumer (group starts at $, streamReclaimer for unacked entries). AddComment no longer does any of this, so it is back to what it is on master.

One thing worth a look: the batching window still needs something to fire the batches when they come due, and a stream can't do that by itself. I put that sweep in the consumer's own read loop (right after a read that queued a comment, otherwise every 15s) instead of a separate worker. There is no leader lock; every replica sweeps, and each batch is claimed with a conditional DELETE, so it fires once.

If you would rather not have the sweep there at all, the alternative is to drop the window and always trigger immediately. Happy to go that way if you prefer it simpler.

I updated the PR description to match.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Approach: sound — the new commit moves assigned-comment detection onto the activity stream (api.assigned_comment_trigger group) and keeps the atomic Claim as the at-most-once mechanism, replacing the leader-locked scheduler without adding a parallel dispatch path.

Reviewed changes

  • Replaced the leader-locked scheduler with a stream consumer — worker.AssignedCommentConsumer reads task.comment.added from paca.activities through its own consumer group starting at $, decodes the activity payload, and acks non-comment, edited, authorless and malformed entries untouched.
  • Took queueing off the request path — handleAssignedAgents became HandleAssignedComment(projectID, taskID, commentID, actorMemberID, content), which re-resolves the author by member id (rejecting agents and service accounts), loads the task, skips @-mentioned agents and appends to each opted-in agent's batch.
  • Swept due batches without a lock — FireDuePendingCommentTriggers lost its proceed callback and now fires right after a read that queued a batch or every 15s; the existing Claim fence keeps it at-most-once across replicas, and the re-check of opt-in/assignment still runs before the claim.
  • Made batch append idempotent — the upsert now carries WHERE agent_pending_comment_triggers.last_comment_id <> EXCLUDED.last_comment_id, so a redelivered comment that is already the row's last is a no-op.
  • Updated wiring, docs and tests — app.go starts/stops the consumer, the overview describes the consumer model, and tests cover ack/redelivery, untouched entries, sweep scheduling, the two-replica claim and a partial lookup failure.

Pullfrog  | View workflow run | Using deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏

@pikann pikann left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thank you for the contribution! 🚀

@pikann
pikann merged commit 6fa06b5 into Paca-AI:master Dec 1, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] Let an assigned agent react to new comments on its task

2 participants