Skip to content

feat(teams): auto-notify leader LLM on task completion (closes #4, compat with #6)#7

Open
RensTillmann wants to merge 2 commits intotmustier:mainfrom
RensTillmann:feat/leader-auto-notify-on-task-completion
Open

feat(teams): auto-notify leader LLM on task completion (closes #4, compat with #6)#7
RensTillmann wants to merge 2 commits intotmustier:mainfrom
RensTillmann:feat/leader-auto-notify-on-task-completion

Conversation

@RensTillmann
Copy link

Problem

When teammates complete tasks, leader-inbox.ts calls ctx.ui.notify() which only shows a UI toast. The leader LLM never receives a message in its conversation, so it goes stale after delegating — the human has to manually poke it to check progress.

This is exactly the problem described in #4.

Solution

Three notification layers, each serving a different purpose:

1. Per-task completion (onTaskCompleted callback)

When a worker completes a task, inject the completion info into the leader's conversation via pi.sendMessage() with triggerTurn: false. This adds context without interrupting an active turn — the leader sees it on its next response.

2. Batch-complete auto-wake (DelegationTracker)

When ALL tasks from a single delegate() call reach "completed", fire pi.sendUserMessage() which triggers a new LLM turn. This wakes the idle leader to review results and continue orchestrating.

  • DelegationTracker class tracks task ID batches from each delegate() call
  • checkCompleted() polls task statuses (called every 700ms in the inbox loop)
  • Handles both idle (sendUserMessage direct) and streaming (deliverAs: "followUp") states
  • Falls back to ctx.ui.notify() if sendUserMessage throws
  • Cleared on session_switch, pruned after notification

3. DM delivery (PR #6 compat)

Includes the onDm callback pattern from #6 so both PRs can coexist. DMs are batched with a 50ms debounce and flushed via pi.sendMessage() with triggerTurn: true.

Files changed

File Change
leader-inbox.ts DelegationTracker class, pi/onDm/onTaskCompleted/delegationTracker params, batch-complete check at end
leader.ts Instantiate tracker, DM/task-completion batching + flush logic, wire callbacks into pollLeaderInbox
leader-teams-tool.ts Register delegation batch in delegate action via delegationTracker.addBatch()

Compatibility with PR #6

This PR includes the same onDm callback approach from #6 (DM routing to leader LLM context with debounce batching). If #6 merges first, this PR's DM handling can be dropped — the rest (per-task + batch-complete) is additive.

Before / After

Before: Leader delegates → workers complete → UI toast only → leader goes stale → human pokes

After: Leader delegates → workers complete → per-task info injected → all done → sendUserMessage auto-wakes leader → leader reviews and continues

Rens Tillmann added 2 commits March 6, 2026 05:38
Closes tmustier#4. Compatible with PR tmustier#6 (onDm callback).

Three notification layers:
1. Per-task: pi.sendMessage() injects completion info into leader
   context (triggerTurn: false) so it accumulates without interruption
2. Batch-complete: pi.sendUserMessage() wakes the idle leader when
   ALL tasks from a delegate() call finish
3. DMs (PR tmustier#6 compat): onDm callback with 50ms debounce batching,
   delivered via pi.sendMessage() with triggerTurn: true

DelegationTracker class tracks task ID batches from delegate calls.
Cleared on session_switch. Batches pruned after notification.
The original DelegationTracker.checkCompleted() polled listTasks() on
every inbox cycle, which raced — it could see stale task statuses and
fire false-positive batch-complete notifications immediately after
delegation.

Replaced with event-driven markCompleted(taskId): only marks a task
done when an idle_notification with completedTaskId is actually
received. No more filesystem polling for task status.

Also: batch completions are collected across all messages in one poll
cycle and deduplicated before firing notifications.
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.

1 participant