Files
multica/packages/core/issues/mutations.ts
Naiyuan Qing 4ab335b8a5 MUL-3416: Issue pre-trigger preview + Handoff Note (#4383)
* feat(issues): unify run-enqueue decision behind WillEnqueueRun + preview endpoint

Collapse the issue update/batch enqueue copies into one service predicate
service.IssueService.WillEnqueueRun, shared verbatim with a new dry-run
endpoint POST /api/issues/preview-trigger so the four entry points stop
drifting (squad/self-loop/batch omissions, MUL-3375). The private-agent gate
stays at the HTTP boundary: write paths inject allow-all, preview injects the
real gate so it never leaks a private agent's readiness.

Add suppress_run to issue update/batch: the change applies but no run starts.
Remove the now-dead handler mirrors shouldEnqueueSquadLeaderOnAssign /
isSquadLeaderReady. service.Create and the comment trigger chain are untouched.

Tests: preview behavior, preview<->write-path match, batch aggregation,
member no-trigger, suppress_run skip, malformed-body 400.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-authored-by: multica-agent <github@multica.ai>

* feat(issues): inject handoff note into assigned runs via first-class task field

Add an optional handoff_note carried by issue assign/promote into the run's
opening prompt and issue_context.md, via a dedicated agent_task_queue column
(migration 122) and a daemon assignment-handoff render branch — never a
fabricated comment, never trigger_comment_id (MUL-3375 §6.1).

Thread the note through enqueueIssueTask/enqueueMentionTask + WithHandoff
public variants and dispatchIssueRun; suppress_run or a parked write drops it
(no run = nothing to inject). Soft version gate: MinHandoffCLIVersion +
HandoffSupported, surfaced per-trigger as handoff_supported in the preview so
the UI can gray the note box on old daemons; the assignment never hard-fails.

Tests: daemon prompt + issue_context render via the assignment branch (not
quick-create/comment), version helper matrix, note persists on the task,
suppressed assign enqueues nothing.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-authored-by: multica-agent <github@multica.ai>

* feat(issues): leave a display-only handoff record on the timeline

When an assign/promote with a handoff note starts a run, write one
type='handoff' timeline record via TaskService.RecordHandoff — a direct
Queries.CreateComment + timeline event that bypasses Handler.CreateComment, so
it never reaches triggerTasksForComment and cannot start a second run
(MUL-3375 §6.2, the must-not-retrigger invariant). Author is the actor who
handed off; body is the note. Migration 123 admits the 'handoff' comment type.
Recorded only on a real run start: suppress_run or a parked write writes
nothing. enqueueSquadLeaderTask now reports whether it enqueued so the trace
is gated on an actual dispatch.

Test: exactly one handoff record on assign-with-note, exactly one task (no
re-trigger), and no record when suppressed.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-authored-by: multica-agent <github@multica.ai>

* feat(issues): frontend plumbing for issue-trigger preview + handoff (core)

Add api.previewIssueTrigger + IssueTriggerPreviewSchema (zod parseWithFallback),
the use-issue-trigger-preview hook, issueKeys.issueTriggerPreview(+All) with WS
queue-state invalidation, suppress_run/handoff_note on UpdateIssueRequest, the
'handoff' CommentType, and stripping of the control fields from optimistic
update/batch cache patches (MUL-3375 §9).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-authored-by: multica-agent <github@multica.ai>

* fix(issues): exclude handoff records from new-comment counting

type='handoff' is a display-only timeline record, not conversation. Exclude it
from CountNewCommentsSince so a handoff note never inflates the count of
"new comments to catch up on" fed to a claiming agent (MUL-3375 §12). Analytics
already excludes it (RecordHandoff is a direct write that emits no analytics
event), and the comment-trigger path is already bypassed.

Test: a handoff record does not bump the new-comment count; a real comment does.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-authored-by: multica-agent <github@multica.ai>

* feat(issues): pre-trigger preview UI, handoff note, timeline card (web/desktop)

Wire the §9 frontend onto the preview endpoint + handoff fields:
- Delete the backlog blocking dialog (backlog-agent-hint*) and its modal type;
  the over-eager nag is gone. Backlog awareness is now a passive label.
- RunConfirmModal: single assign + batch assign/status route here. Shows the
  backend predicate's verdict ("将启动 @X" / "将启动 N 个" / parked), an optional
  handoff note (assign only, soft-gated by handoff_supported), and 暂不启动 —
  then applies via update/batch. No frontend guessing.
- create modal: passive CreateRunHint ("将启动 @X" / backlog parked).
- single status change stays a direct apply (unchanged).
- timeline: render type='handoff' as a distinct, non-interactive handoff card.
- i18n run_confirm + handoff_card across en/ja/ko/zh-Hans; drop backlog action
  keys; locale parity green.

Tests: use-issue-actions (assign → run-confirm modal, member → direct),
create-issue + comment-card suites updated/green; views typecheck + lint clean.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-authored-by: multica-agent <github@multica.ai>

* test(issues): use a valid anchor in the handoff count-exclusion test

CountNewCommentsSince filters id <> @anchor_id; SQL id <> NULL is NULL and
excludes every row, so an empty anchor made the control assertion read 0. The
production caller always passes a real anchor — mirror that with a non-matching
sentinel uuid.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-authored-by: multica-agent <github@multica.ai>

* test(issues): RunConfirmModal apply logic (start/suppress/note-gate/batch)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-authored-by: multica-agent <github@multica.ai>

* test(core): preview schema malformed/missing/null fallback coverage

Cover IssueTriggerPreviewSchema via parseWithFallback (MUL-3375): well-formed
parse, top-level + item default fills (empty/older backend), and fallback to
{ triggers: [], total_count: 0 } for malformed shapes, a dropped required
issue_id, a wrong-typed total_count, and null/non-object bodies — so the four
entry points degrade to "nothing will start" instead of throwing.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-authored-by: multica-agent <github@multica.ai>

* refactor(issues): remove display-only handoff timeline record (留痕)

The handoff "留痕" timeline record (type='handoff' comment written on run
start) was judged superfluous and dropped per product call. This removes
only the display-only trace; the handoff NOTE injection into the run's
opening prompt + issue_context.md is untouched.

- backend: drop RecordHandoff + its call in dispatchIssueRun
- db: drop the `type <> 'handoff'` exclusion in CountNewCommentsSince and
  migration 123 (comment_type_check reverts to the 4-type set from 001);
  no production data exists for this unreleased feature
- frontend: drop the "handoff" CommentType, HandoffCard, and handoff_card
  i18n (all locales)
- tests: drop handoff_count_test.go and the record-write assertions in
  issue_trigger_preview_test.go (note-injection tests retained)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-authored-by: multica-agent <github@multica.ai>

* feat(issues): dismissable run-confirm modal + team-handoff copy

Two fixes to the pre-trigger confirm modal (MUL-3375).

1. Dismissable: switch RunConfirmModal from AlertDialog to the standard
   shadcn Dialog so it has the close (X) button + Esc + click-outside.
   Previously the only choices were "start" / "don't start now" with no
   way to abort the action entirely; dismissing now cancels with no write.

2. Copy: rework the action-surface wording away from the backend term
   "run" toward team-handoff voice — 指派 / 开始 / 交接 (run stays only on
   record surfaces). Unifies the note's three names to "交接说明", and
   parallels the rewrite across en/ja/ko.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-authored-by: multica-agent <github@multica.ai>

* chore(agent): bump handoff note min CLI version to 0.3.28

The daemon release that renders handoff notes ships in 0.3.28 (0.3.27
was the prior tag), so move the soft-gate threshold up. Below this the
note is silently dropped and the frontend grays the note box — assignment
is never blocked.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(issues): skip run-confirm when batch-moving issues to backlog

A move into backlog never starts a run (service/issue_trigger.go), so the
pre-trigger confirm modal degenerated to an empty "won't start" box with a
single Apply button — pure friction. Apply directly instead, matching the
single-issue status path. Other target statuses still route through the
modal.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* feat(issues): refine pre-trigger preview hint and copy

- Move the create-issue run hint to a reveal band (grid 0fr→1fr) above the
  property toolbar. It was sharing the footer button row and, lacking a
  width constraint, reflowed the submit buttons whenever it appeared.
  Restyle to a borderless, comment-style avatar+caption that is purely a
  caption (non-interactive avatar).
- Distinguish squad from agent in the pre-trigger copy: a squad's leader
  evaluates and delegates rather than "starting work" itself. Add
  will_start_named_squad / will_start_squad / create_will_start_squad across
  en/zh/ja/ko (reusing the squad_leader_* evaluate→arrange vocabulary) and
  branch run-confirm + the create hint on squad assignees.
- Bold the assignee name in the run-confirm headline via a language-safe
  sentinel split (no per-language prefix/suffix keys).
- Align zh "开始处理" → "开始工作" on the single-assign copy.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* test(issues): stub ActorAvatar in create-issue suite

CreateRunHint now renders an ActorAvatar for agent/squad assignees, which
pulls in getActorInitials/getActorAvatarUrl + the workspace/presence/navigation
hook tree. This form-focused suite only stubbed getActorName, so the
squad-forwarding test crashed with "getActorInitials is not a function". Stub
the avatar inert — its own behavior is covered elsewhere.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Walt <walt@multica.ai>
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
Co-authored-by: multica-agent <github@multica.ai>
2026-06-23 13:17:13 +08:00

991 lines
37 KiB
TypeScript

import { useState, useCallback } from "react";
import { useMutation, useQueryClient, type QueryKey } from "@tanstack/react-query";
import { api } from "../api";
import {
issueKeys,
ISSUE_PAGE_SIZE,
type AssigneeGroupedIssuesFilter,
type IssueSortParam,
type MyIssuesFilter,
} from "./queries";
import { projectKeys } from "../projects/queries";
import {
addIssueToBuckets,
findIssueLocation,
getBucket,
patchIssueInBuckets,
setBucket,
} from "./cache-helpers";
import {
cleanupDeletedIssueCaches,
collectDeletedIssueCacheMetadata,
invalidateDeletedIssueDependentCaches,
invalidateDeletedIssueParentCaches,
invalidateIssueScopedCaches,
pruneDeletedIssueFromListCaches,
pruneDeletedIssueFromParentChildrenCaches,
} from "./delete-cache";
import { useWorkspaceId } from "../hooks";
import { useRecentContextStore } from "../chat/recent-context-store";
import { useRecentIssuesStore } from "./stores";
import type { GroupedIssuesResponse, Issue, IssueAssigneeGroup, IssueReaction, IssueStatus } from "../types";
import type {
CreateIssueRequest,
UpdateIssueRequest,
ListIssuesCache,
} from "../types";
import type { TimelineEntry, IssueSubscriber, Reaction } from "../types";
import { sortTimelineEntriesAsc } from "./timeline-sort";
// ---------------------------------------------------------------------------
// Shared mutation variable types — used by both mutation hooks and
// useMutationState consumers to keep the type assertion in sync.
// ---------------------------------------------------------------------------
export type ToggleCommentReactionVars = {
commentId: string;
emoji: string;
existing: Reaction | undefined;
};
export type ToggleIssueReactionVars = {
emoji: string;
existing: IssueReaction | undefined;
};
// ---------------------------------------------------------------------------
// Per-status pagination
// ---------------------------------------------------------------------------
/**
* Paginate one status column into the cache. Works for both the workspace
* issue list and per-scope My Issues lists (pass `myIssues` to target the
* latter).
*
* `sort` must match the sort the consuming `useQuery` was called with —
* the query key embeds it (see `listSorted` / `myListSorted`), so a load-more
* with the wrong sort would patch a stale cache entry that nobody is
* subscribed to. It is also threaded into the API request so the appended
* page lines up with the server-side ordering of the existing items.
*/
export function useLoadMoreByStatus(
status: IssueStatus,
myIssues?: { scope: string; filter: MyIssuesFilter },
sort?: IssueSortParam,
) {
const qc = useQueryClient();
const wsId = useWorkspaceId();
const [isLoading, setIsLoading] = useState(false);
const activeKey = myIssues
? issueKeys.myListSorted(wsId, myIssues.scope, myIssues.filter, sort)
: issueKeys.listSorted(wsId, sort);
const cache = qc.getQueryData<ListIssuesCache>(activeKey);
const bucket = cache?.byStatus[status];
const loaded = bucket?.issues.length ?? 0;
const total = bucket?.total ?? 0;
const hasMore = loaded < total;
const loadMore = useCallback(async () => {
if (isLoading || !hasMore) return;
setIsLoading(true);
try {
const res = await api.listIssues({
status,
limit: ISSUE_PAGE_SIZE,
offset: loaded,
...sort,
...myIssues?.filter,
});
qc.setQueryData<ListIssuesCache>(activeKey, (old) => {
if (!old) return old;
const prev = getBucket(old, status);
const existingIds = new Set(prev.issues.map((i) => i.id));
const appended = res.issues.filter((i) => !existingIds.has(i.id));
return setBucket(old, status, {
issues: [...prev.issues, ...appended],
total: res.total,
});
});
} finally {
setIsLoading(false);
}
}, [qc, activeKey, status, loaded, hasMore, isLoading, myIssues?.filter, sort]);
return { loadMore, hasMore, isLoading, total };
}
/**
* Paginate one assignee-grouped board column into the cache. `queryKey`
* already pins the active cache entry (it's the same object the consuming
* `useQuery` registered), so the cache lookup and `setQueryData` target the
* right row. `sort` is threaded into the API request so the appended page
* lines up with the server-side ordering of the existing items.
*/
export function useLoadMoreByAssigneeGroup(
group: Pick<IssueAssigneeGroup, "id" | "assignee_type" | "assignee_id">,
queryKey: QueryKey,
filter: AssigneeGroupedIssuesFilter,
sort?: IssueSortParam,
) {
const qc = useQueryClient();
const [isLoading, setIsLoading] = useState(false);
const cache = qc.getQueryData<GroupedIssuesResponse>(queryKey);
const cachedGroup = cache?.groups.find((g) => g.id === group.id);
const loaded = cachedGroup?.issues.length ?? 0;
const total = cachedGroup?.total ?? 0;
const hasMore = loaded < total;
const loadMore = useCallback(async () => {
if (isLoading || !hasMore) return;
setIsLoading(true);
try {
const res = await api.listGroupedIssues({
group_by: "assignee",
limit: ISSUE_PAGE_SIZE,
offset: loaded,
...sort,
...filter,
group_assignee_type: group.assignee_type ?? "none",
group_assignee_id: group.assignee_id ?? undefined,
});
const nextGroup = res.groups[0];
if (!nextGroup) return;
qc.setQueryData<GroupedIssuesResponse>(queryKey, (old) => {
if (!old) return old;
return {
groups: old.groups.map((existing) => {
if (existing.id !== nextGroup.id) return existing;
const existingIds = new Set(existing.issues.map((issue) => issue.id));
const appended = nextGroup.issues.filter((issue) => !existingIds.has(issue.id));
return {
...existing,
issues: [...existing.issues, ...appended],
total: nextGroup.total,
};
}),
};
});
} finally {
setIsLoading(false);
}
}, [filter, group.assignee_id, group.assignee_type, hasMore, isLoading, loaded, qc, queryKey, sort]);
return { loadMore, hasMore, isLoading, total };
}
// ---------------------------------------------------------------------------
// Issue CRUD
// ---------------------------------------------------------------------------
export function useCreateIssue() {
const qc = useQueryClient();
const wsId = useWorkspaceId();
return useMutation({
mutationFn: (data: CreateIssueRequest) => api.createIssue(data),
onSuccess: (newIssue) => {
for (const [key, data] of qc.getQueriesData<ListIssuesCache>({ queryKey: issueKeys.list(wsId) })) {
if (data) qc.setQueryData<ListIssuesCache>(key, addIssueToBuckets(data, newIssue));
}
// Surface the just-created issue in cmd+k's Recent list without
// requiring the user to open it first.
useRecentIssuesStore.getState().recordVisit(wsId, newIssue.id);
// Invalidate parent's children query so sub-issues list updates immediately
if (newIssue.parent_issue_id) {
qc.invalidateQueries({ queryKey: issueKeys.children(wsId, newIssue.parent_issue_id) });
qc.invalidateQueries({ queryKey: issueKeys.childProgress(wsId) });
}
},
onSettled: () => {
qc.invalidateQueries({ queryKey: issueKeys.list(wsId) });
qc.invalidateQueries({ queryKey: issueKeys.assigneeGroupsAll(wsId) });
qc.invalidateQueries({ queryKey: issueKeys.myAssigneeGroupsAll(wsId) });
qc.invalidateQueries({ queryKey: issueKeys.projectGanttAll(wsId) });
qc.invalidateQueries({ queryKey: projectKeys.all(wsId) });
},
});
}
export function useUpdateIssue() {
const qc = useQueryClient();
const wsId = useWorkspaceId();
// Every bucketed board cache an optimistic move must keep in sync: the
// workspace board (issueKeys.list*) AND the My-Issues / Project board
// (issueKeys.myList* under `my`), which share the ListIssuesCache shape.
// Filtering by `byStatus` skips the grouped (assignee) and flat
// (gantt/detail/children) caches that also live under those prefixes. The
// board reconciles local columns from its own feeding cache on settle, so a
// move that only patched the workspace cache would snap back on My-Issues /
// Project boards.
const readBucketedLists = () =>
[
...qc.getQueriesData<ListIssuesCache>({ queryKey: issueKeys.list(wsId) }),
...qc.getQueriesData<ListIssuesCache>({ queryKey: issueKeys.myAll(wsId) }),
].filter(
(entry): entry is [QueryKey, ListIssuesCache] => !!entry[1]?.byStatus,
);
return useMutation({
mutationFn: ({ id, ...data }: { id: string } & UpdateIssueRequest) =>
api.updateIssue(id, data),
onMutate: ({ id, ...data }) => {
// suppress_run / handoff_note are write-time control fields, not Issue
// columns — they steer enqueue/injection on the server and must never be
// written into the query cache (MUL-3375). Strip them from the patch; the
// mutationFn above still sends the full payload to the API.
const { suppress_run: _suppressRun, handoff_note: _handoffNote, ...patch } = data;
// Fire-and-forget cancelQueries — keeps onMutate synchronous so the
// cache update happens in the same tick as mutate(). Awaiting would
// yield to the event loop, letting @dnd-kit reset its visual state
// before the optimistic update lands.
qc.cancelQueries({ queryKey: issueKeys.list(wsId) });
qc.cancelQueries({ queryKey: issueKeys.myAll(wsId) });
const prevLists = readBucketedLists();
const firstListData = prevLists[0]?.[1];
const prevDetail = qc.getQueryData<Issue>(issueKeys.detail(wsId, id));
// Resolve parent_issue_id from the freshest source so we can keep the
// parent's children cache in sync (used by the parent issue's
// sub-issues list). Falls back to scanning loaded children caches —
// when the user navigates straight to a parent's detail page, the
// child may live only there, not in detail/list.
let parentId: string | null =
prevDetail?.parent_issue_id ??
(firstListData ? findIssueLocation(firstListData, id)?.issue.parent_issue_id : null) ??
null;
if (!parentId) {
const childrenCaches = qc.getQueriesData<Issue[]>({
queryKey: [...issueKeys.all(wsId), "children"],
});
for (const [key, data] of childrenCaches) {
if (!data?.some((c) => c.id === id)) continue;
const candidate = key[key.length - 1];
if (typeof candidate === "string") {
parentId = candidate;
break;
}
}
}
const prevChildren = parentId
? qc.getQueryData<Issue[]>(issueKeys.children(wsId, parentId))
: undefined;
for (const [key, cached] of prevLists) {
if (cached) qc.setQueryData<ListIssuesCache>(key, patchIssueInBuckets(cached, id, patch));
}
qc.setQueryData<Issue>(issueKeys.detail(wsId, id), (old) =>
old ? { ...old, ...patch } : old,
);
if (parentId) {
qc.setQueryData<Issue[]>(
issueKeys.children(wsId, parentId),
(old) =>
old?.map((c) => (c.id === id ? { ...c, ...patch } : c)),
);
}
return { prevLists, prevDetail, prevChildren, parentId, id };
},
onError: (_err, _vars, ctx) => {
if (ctx?.prevLists) {
for (const [key, snapshot] of ctx.prevLists) {
qc.setQueryData(key, snapshot);
}
}
if (ctx?.prevDetail)
qc.setQueryData(issueKeys.detail(wsId, ctx.id), ctx.prevDetail);
if (ctx?.parentId && ctx.prevChildren !== undefined) {
qc.setQueryData(
issueKeys.children(wsId, ctx.parentId),
ctx.prevChildren,
);
}
},
onSuccess: (serverIssue) => {
// Reconcile with the authoritative server entity by patching the one card
// in place — NOT by invalidating + refetching the list. The list refetch
// is what made a successful move flicker: the optimistic card was already
// in the right place, then the refetch replaced the whole column and the
// card re-landed. updateIssue returns the full issue and a position update
// touches only that row, so a surgical patch is the authoritative
// reconcile and is a visual no-op when the optimistic value matched.
for (const [key, cached] of readBucketedLists()) {
qc.setQueryData<ListIssuesCache>(
key,
patchIssueInBuckets(cached, serverIssue.id, serverIssue),
);
}
qc.setQueryData<Issue>(issueKeys.detail(wsId, serverIssue.id), (old) =>
old ? { ...old, ...serverIssue } : old,
);
},
onSettled: (_data, _err, vars, ctx) => {
// The issue's own list + detail caches are reconciled surgically in
// onSuccess / onError, so they are deliberately NOT invalidated here — a
// full-list refetch on settle is what made drags flicker. Only aggregate
// caches that cannot be patched from a single issue are refreshed below.
qc.invalidateQueries({ queryKey: issueKeys.assigneeGroupsAll(wsId) });
qc.invalidateQueries({ queryKey: issueKeys.myAssigneeGroupsAll(wsId) });
qc.invalidateQueries({ queryKey: issueKeys.projectGanttAll(wsId) });
if (
vars.status !== undefined ||
Object.prototype.hasOwnProperty.call(vars, "project_id")
) {
qc.invalidateQueries({ queryKey: projectKeys.all(wsId) });
}
// Refresh the issue's attachments cache when the description editor
// bound new uploads — the description editor reads `issueAttachments`
// to resolve text-preview Eye gates, and unlike other mutations this
// payload mutates the attachment join table.
if (vars.attachment_ids?.length) {
qc.invalidateQueries({ queryKey: issueKeys.attachments(vars.id) });
}
// Invalidate old parent's children cache
if (ctx?.parentId) {
qc.invalidateQueries({
queryKey: issueKeys.children(wsId, ctx.parentId),
});
qc.invalidateQueries({ queryKey: issueKeys.childProgress(wsId) });
}
// Invalidate new parent's children cache when parent_issue_id changed
const newParentId = vars.parent_issue_id;
if (newParentId && newParentId !== ctx?.parentId) {
qc.invalidateQueries({
queryKey: issueKeys.children(wsId, newParentId),
});
qc.invalidateQueries({ queryKey: issueKeys.childProgress(wsId) });
}
// Invalidate the batched-children cache only when the parent link
// actually changed. The WS path (ws-updaters.ts) invalidates
// unconditionally because it doesn't know what the server change
// touched; here onMutate already patched issueKeys.children(parent)
// optimistically, so we only need to flush when the parent relation
// itself moved.
if (ctx?.parentId || newParentId) {
qc.invalidateQueries({ queryKey: issueKeys.childrenByParentsAll(wsId) });
}
},
});
}
export function useDeleteIssue() {
const qc = useQueryClient();
const wsId = useWorkspaceId();
return useMutation({
mutationFn: (id: string) => api.deleteIssue(id),
onMutate: async (id) => {
await Promise.all([
qc.cancelQueries({ queryKey: issueKeys.list(wsId) }),
qc.cancelQueries({ queryKey: issueKeys.myAll(wsId) }),
]);
const metadata = collectDeletedIssueCacheMetadata(qc, wsId, id);
await Promise.all(
metadata.parentIssueIds.map((parentId) =>
qc.cancelQueries({ queryKey: issueKeys.children(wsId, parentId) }),
),
);
const prevLists = qc.getQueriesData<ListIssuesCache>({ queryKey: issueKeys.list(wsId) });
const prevMyLists = qc.getQueriesData<ListIssuesCache>({
queryKey: issueKeys.myAll(wsId),
});
const prevDetail = qc.getQueryData<Issue>(issueKeys.detail(wsId, id));
const prevChildren = new Map<string, Issue[] | undefined>();
for (const parentId of metadata.parentIssueIds) {
prevChildren.set(
parentId,
qc.getQueryData<Issue[]>(issueKeys.children(wsId, parentId)),
);
}
pruneDeletedIssueFromListCaches(qc, wsId, id);
pruneDeletedIssueFromParentChildrenCaches(qc, wsId, id, metadata);
qc.removeQueries({ queryKey: issueKeys.detail(wsId, id) });
return { id, metadata, prevLists, prevMyLists, prevDetail, prevChildren };
},
onError: (_err, _id, ctx) => {
if (ctx?.prevLists) {
for (const [key, snapshot] of ctx.prevLists) {
qc.setQueryData(key, snapshot);
}
}
if (ctx?.prevMyLists) {
for (const [key, snapshot] of ctx.prevMyLists) {
qc.setQueryData(key, snapshot);
}
}
if (ctx?.prevDetail) {
qc.setQueryData(issueKeys.detail(wsId, ctx.id), ctx.prevDetail);
}
if (ctx?.prevChildren) {
for (const [parentId, snapshot] of ctx.prevChildren) {
qc.setQueryData(issueKeys.children(wsId, parentId), snapshot);
}
}
},
onSuccess: (_data, id, ctx) => {
useRecentContextStore.getState().forgetContext(wsId, { type: "issue", id });
cleanupDeletedIssueCaches(qc, wsId, id, ctx?.metadata);
},
onSettled: (_data, _err, _id, ctx) => {
qc.invalidateQueries({ queryKey: issueKeys.list(wsId) });
qc.invalidateQueries({ queryKey: issueKeys.assigneeGroupsAll(wsId) });
qc.invalidateQueries({ queryKey: issueKeys.myAssigneeGroupsAll(wsId) });
qc.invalidateQueries({ queryKey: issueKeys.projectGanttAll(wsId) });
qc.invalidateQueries({ queryKey: projectKeys.all(wsId) });
if (ctx?.metadata) invalidateDeletedIssueParentCaches(qc, wsId, ctx.metadata);
},
});
}
export function useBatchUpdateIssues() {
const qc = useQueryClient();
const wsId = useWorkspaceId();
return useMutation({
mutationFn: ({
ids,
updates,
}: {
ids: string[];
updates: UpdateIssueRequest;
}) => api.batchUpdateIssues(ids, updates),
onMutate: async ({ ids, updates }) => {
// Patch BOTH the workspace board (issueKeys.list) and the filtered
// My-Issues / Project / actor lists (issueKeys.myAll). The single-issue
// update already patches both; batch only touched issueKeys.list, so a
// batch edit on a My-Issues board had no optimistic effect and relied
// entirely on the settle refetch. Filter to bucketed (byStatus) caches so
// grouped/flat caches under the same prefix are skipped.
//
// Control fields steer the server; they are not Issue columns and must
// not enter the cache (MUL-3375). mutationFn still sends them.
const { suppress_run: _suppressRun, handoff_note: _handoffNote, ...patch } = updates;
await qc.cancelQueries({ queryKey: issueKeys.list(wsId) });
await qc.cancelQueries({ queryKey: issueKeys.myAll(wsId) });
const prevLists = [
...qc.getQueriesData<ListIssuesCache>({ queryKey: issueKeys.list(wsId) }),
...qc.getQueriesData<ListIssuesCache>({ queryKey: issueKeys.myAll(wsId) }),
].filter(
(entry): entry is [QueryKey, ListIssuesCache] => !!entry[1]?.byStatus,
);
for (const [key, cached] of prevLists) {
let next = cached;
for (const id of ids) next = patchIssueInBuckets(next, id, patch);
qc.setQueryData<ListIssuesCache>(key, next);
}
// Mirror the optimistic patch into any loaded children cache so
// sub-issue rows on a parent's detail page reflect the change too.
const idSet = new Set(ids);
const childrenCaches = qc.getQueriesData<Issue[]>({
queryKey: [...issueKeys.all(wsId), "children"],
});
const prevChildren = new Map<string, Issue[] | undefined>();
const affectedParentIds = new Set<string>();
for (const [key, data] of childrenCaches) {
if (!data?.some((c) => idSet.has(c.id))) continue;
const parentId = key[key.length - 1];
if (typeof parentId !== "string") continue;
affectedParentIds.add(parentId);
prevChildren.set(parentId, data);
qc.setQueryData<Issue[]>(issueKeys.children(wsId, parentId), (old) =>
old?.map((c) => (idSet.has(c.id) ? { ...c, ...patch } : c)),
);
}
return { prevLists, prevChildren, affectedParentIds };
},
onError: (_err, _vars, ctx) => {
if (ctx?.prevLists) {
for (const [key, snapshot] of ctx.prevLists) {
qc.setQueryData(key, snapshot);
}
}
if (ctx?.prevChildren) {
for (const [parentId, snapshot] of ctx.prevChildren) {
qc.setQueryData(issueKeys.children(wsId, parentId), snapshot);
}
}
},
onSettled: (_data, _err, _vars, ctx) => {
// Deliberately NOT invalidating issueKeys.list / myAll here: the onMutate
// patch above is a complete surgical reconcile for these bucketed boards
// (batch changes status / priority / project — never a server-computed
// value), so a full-board refetch on settle would only re-introduce the
// flicker the single-issue update already removed. Aggregate / grouped
// caches that cannot be recomputed from a single-issue patch are still
// refreshed below.
qc.invalidateQueries({ queryKey: issueKeys.assigneeGroupsAll(wsId) });
qc.invalidateQueries({ queryKey: issueKeys.myAssigneeGroupsAll(wsId) });
qc.invalidateQueries({ queryKey: issueKeys.projectGanttAll(wsId) });
if (
_vars.updates.status !== undefined ||
Object.prototype.hasOwnProperty.call(_vars.updates, "project_id")
) {
qc.invalidateQueries({ queryKey: projectKeys.all(wsId) });
}
if (ctx?.affectedParentIds && ctx.affectedParentIds.size > 0) {
for (const parentId of ctx.affectedParentIds) {
qc.invalidateQueries({
queryKey: issueKeys.children(wsId, parentId),
});
}
qc.invalidateQueries({ queryKey: issueKeys.childProgress(wsId) });
}
},
});
}
export function useBatchDeleteIssues() {
const qc = useQueryClient();
const wsId = useWorkspaceId();
return useMutation({
mutationFn: (ids: string[]) => api.batchDeleteIssues(ids),
onMutate: async (ids) => {
await Promise.all([
qc.cancelQueries({ queryKey: issueKeys.list(wsId) }),
qc.cancelQueries({ queryKey: issueKeys.myAll(wsId) }),
]);
const metadataById = new Map(
ids.map((id) => [
id,
collectDeletedIssueCacheMetadata(qc, wsId, id),
]),
);
const parentIssueIds = new Set<string>();
for (const metadata of metadataById.values()) {
for (const parentId of metadata.parentIssueIds) {
parentIssueIds.add(parentId);
}
}
await Promise.all(
Array.from(parentIssueIds).map((parentId) =>
qc.cancelQueries({ queryKey: issueKeys.children(wsId, parentId) }),
),
);
const prevLists = qc.getQueriesData<ListIssuesCache>({ queryKey: issueKeys.list(wsId) });
const prevMyLists = qc.getQueriesData<ListIssuesCache>({
queryKey: issueKeys.myAll(wsId),
});
const prevChildren = new Map<string, Issue[] | undefined>();
for (const parentId of parentIssueIds) {
prevChildren.set(
parentId,
qc.getQueryData<Issue[]>(issueKeys.children(wsId, parentId)),
);
}
for (const id of ids) {
const metadata = metadataById.get(id);
pruneDeletedIssueFromListCaches(qc, wsId, id);
if (metadata) {
pruneDeletedIssueFromParentChildrenCaches(qc, wsId, id, metadata);
}
}
return { prevLists, prevMyLists, prevChildren, parentIssueIds, metadataById };
},
onError: (_err, _ids, ctx) => {
if (ctx?.prevLists) {
for (const [key, snapshot] of ctx.prevLists) {
qc.setQueryData(key, snapshot);
}
}
if (ctx?.prevMyLists) {
for (const [key, snapshot] of ctx.prevMyLists) {
qc.setQueryData(key, snapshot);
}
}
if (ctx?.prevChildren) {
for (const [parentId, snapshot] of ctx.prevChildren) {
qc.setQueryData(issueKeys.children(wsId, parentId), snapshot);
}
}
},
onSuccess: (data, ids, ctx) => {
if (data.deleted === ids.length) {
const { forgetContext } = useRecentContextStore.getState();
for (const id of ids) {
forgetContext(wsId, { type: "issue", id });
cleanupDeletedIssueCaches(qc, wsId, id, ctx?.metadataById.get(id));
}
return;
}
if (ctx?.prevLists) {
for (const [key, snapshot] of ctx.prevLists) {
qc.setQueryData(key, snapshot);
}
}
if (ctx?.prevMyLists) {
for (const [key, snapshot] of ctx.prevMyLists) {
qc.setQueryData(key, snapshot);
}
}
if (ctx?.prevChildren) {
for (const [parentId, snapshot] of ctx.prevChildren) {
qc.setQueryData(issueKeys.children(wsId, parentId), snapshot);
}
}
for (const id of ids) {
invalidateIssueScopedCaches(qc, wsId, id);
}
qc.invalidateQueries({ queryKey: issueKeys.all(wsId) });
invalidateDeletedIssueDependentCaches(qc, wsId);
},
onSettled: (_data, _err, _ids, ctx) => {
qc.invalidateQueries({ queryKey: issueKeys.list(wsId) });
qc.invalidateQueries({ queryKey: issueKeys.assigneeGroupsAll(wsId) });
qc.invalidateQueries({ queryKey: issueKeys.myAssigneeGroupsAll(wsId) });
qc.invalidateQueries({ queryKey: issueKeys.projectGanttAll(wsId) });
qc.invalidateQueries({ queryKey: projectKeys.all(wsId) });
if (ctx?.parentIssueIds && ctx.parentIssueIds.size > 0) {
invalidateDeletedIssueParentCaches(qc, wsId, {
parentIssueIds: Array.from(ctx.parentIssueIds),
});
}
},
});
}
// ---------------------------------------------------------------------------
// Comments / Timeline
// ---------------------------------------------------------------------------
type TimelineCache = TimelineEntry[];
export function useCreateComment(issueId: string) {
const qc = useQueryClient();
return useMutation({
mutationFn: ({
content,
type,
parentId,
attachmentIds,
suppressAgentIds,
}: {
content: string;
type?: string;
parentId?: string;
attachmentIds?: string[];
suppressAgentIds?: string[];
}) => api.createComment(issueId, content, type, parentId, attachmentIds, suppressAgentIds),
onSuccess: (comment) => {
const entry: TimelineEntry = {
type: "comment",
id: comment.id,
actor_type: comment.author_type,
actor_id: comment.author_id,
content: comment.content,
parent_id: comment.parent_id,
comment_type: comment.type,
reactions: comment.reactions ?? [],
attachments: comment.attachments ?? [],
created_at: comment.created_at,
updated_at: comment.updated_at,
};
// Dedupe by id: the `comment:created` WS event may have already added
// this entry from the broadcast path before this onSuccess fires. Skip
// the append if the entry is already in the cache.
qc.setQueryData<TimelineCache>(issueKeys.timeline(issueId), (old) => {
if (!old) return [entry];
if (old.some((e) => e.id === entry.id)) return old;
return sortTimelineEntriesAsc([...old, entry]);
});
// Posting a comment changes the trigger answer itself (the enqueued
// task now dedupes follow-up triggers), so cached previews for this
// issue are stale the moment the create lands.
qc.invalidateQueries({ queryKey: issueKeys.commentTriggerPreview(issueId) });
},
// No onSettled invalidate. The `comment:created` WS broadcast keeps
// the timeline cache fresh after a successful create, and reconnect
// recovery in useIssueTimeline already invalidates if the connection
// dropped. Re-fetching on every submit replaces every entry's
// reference, which forces every memoized CommentCard subtree to
// re-render (visible as a flash across sibling threads during AI
// streaming).
});
}
export function useUpdateComment(issueId: string) {
const qc = useQueryClient();
return useMutation({
mutationFn: ({
commentId,
content,
attachmentIds,
suppressAgentIds,
}: {
commentId: string;
content: string;
attachmentIds: string[];
suppressAgentIds?: string[];
}) => api.updateComment(commentId, content, attachmentIds, suppressAgentIds),
onMutate: async ({ commentId, content, attachmentIds }) => {
await qc.cancelQueries({ queryKey: issueKeys.timeline(issueId) });
const prev = qc.getQueryData<TimelineCache>(issueKeys.timeline(issueId));
const kept = new Set(attachmentIds);
qc.setQueryData<TimelineCache>(issueKeys.timeline(issueId), (old) =>
old?.map((e) =>
e.id === commentId
? { ...e, content, attachments: e.attachments?.filter((a) => kept.has(a.id)) }
: e,
),
);
return { prev };
},
onError: (_err, _vars, ctx) => {
if (ctx?.prev !== undefined) {
qc.setQueryData(issueKeys.timeline(issueId), ctx.prev);
}
},
onSettled: () => {
qc.invalidateQueries({ queryKey: issueKeys.timeline(issueId) });
},
});
}
export function useDeleteComment(issueId: string) {
const qc = useQueryClient();
return useMutation({
mutationFn: (commentId: string) => api.deleteComment(commentId),
onMutate: async (commentId) => {
await qc.cancelQueries({ queryKey: issueKeys.timeline(issueId) });
const prev = qc.getQueryData<TimelineCache>(issueKeys.timeline(issueId));
// Cascade: collect all descendants of the deleted comment.
const toRemove = new Set<string>([commentId]);
if (prev) {
let changed = true;
while (changed) {
changed = false;
for (const e of prev) {
if (
e.parent_id &&
toRemove.has(e.parent_id) &&
!toRemove.has(e.id)
) {
toRemove.add(e.id);
changed = true;
}
}
}
}
qc.setQueryData<TimelineCache>(issueKeys.timeline(issueId), (old) =>
old?.filter((e) => !toRemove.has(e.id)),
);
return { prev };
},
onError: (_err, _id, ctx) => {
if (ctx?.prev !== undefined) {
qc.setQueryData(issueKeys.timeline(issueId), ctx.prev);
}
},
onSettled: () => {
qc.invalidateQueries({ queryKey: issueKeys.timeline(issueId) });
},
});
}
// Every comment id in the same thread as `commentId` — the thread root plus
// every descendant. Mirrors the server's thread walk in
// ClearOtherThreadResolutions so the resolve optimistic update can clear sibling
// resolutions exactly as the backend will, instead of briefly showing two
// resolutions until the refetch settles.
function collectThreadCommentIds(
entries: TimelineCache,
commentId: string,
): Set<string> {
const byId = new Map<string, TimelineEntry>();
for (const e of entries) {
if (e.type === "comment") byId.set(e.id, e);
}
// Walk up to the thread root (cycle-guarded against malformed parent chains).
let rootId = commentId;
const guard = new Set<string>();
let cur = byId.get(commentId);
while (cur?.parent_id && byId.has(cur.parent_id) && !guard.has(cur.id)) {
guard.add(cur.id);
rootId = cur.parent_id;
cur = byId.get(cur.parent_id);
}
// Expand back down over the whole subtree.
const childrenByParent = new Map<string, string[]>();
for (const e of byId.values()) {
if (e.parent_id) {
const list = childrenByParent.get(e.parent_id) ?? [];
list.push(e.id);
childrenByParent.set(e.parent_id, list);
}
}
const ids = new Set<string>([rootId]);
const stack = [rootId];
while (stack.length > 0) {
const id = stack.pop()!;
for (const child of childrenByParent.get(id) ?? []) {
if (!ids.has(child)) {
ids.add(child);
stack.push(child);
}
}
}
return ids;
}
export function useResolveComment(issueId: string) {
const qc = useQueryClient();
return useMutation({
mutationFn: ({ commentId, resolved }: { commentId: string; resolved: boolean }) =>
resolved ? api.resolveComment(commentId) : api.unresolveComment(commentId),
onMutate: async ({ commentId, resolved }) => {
await qc.cancelQueries({ queryKey: issueKeys.timeline(issueId) });
const prev = qc.getQueryData<TimelineCache>(issueKeys.timeline(issueId));
qc.setQueryData<TimelineCache>(issueKeys.timeline(issueId), (old) => {
if (!old) return old;
// Resolving makes this comment the sole resolution in its thread, so
// mirror the server (ClearOtherThreadResolutions) and clear every other
// resolution in the same thread. Without this the cache shows two
// resolutions until the settle refetch, which is exactly the flash the
// single-resolution fix removes. Unresolve only clears its own row.
const threadIds = resolved ? collectThreadCommentIds(old, commentId) : null;
return old.map((e) => {
if (e.id === commentId) {
return {
...e,
resolved_at: resolved ? new Date().toISOString() : null,
resolved_by_type: resolved ? e.resolved_by_type ?? null : null,
resolved_by_id: resolved ? e.resolved_by_id ?? null : null,
};
}
if (resolved && e.resolved_at && threadIds?.has(e.id)) {
return { ...e, resolved_at: null, resolved_by_type: null, resolved_by_id: null };
}
return e;
});
});
return { prev };
},
onError: (_err, _vars, ctx) => {
if (ctx?.prev !== undefined) {
qc.setQueryData(issueKeys.timeline(issueId), ctx.prev);
}
},
onSettled: () => {
qc.invalidateQueries({ queryKey: issueKeys.timeline(issueId) });
},
});
}
export function useToggleCommentReaction(issueId: string) {
const qc = useQueryClient();
return useMutation({
mutationKey: ["toggleCommentReaction", issueId] as const,
mutationFn: async ({
commentId,
emoji,
existing,
}: ToggleCommentReactionVars) => {
if (existing) {
await api.removeReaction(commentId, emoji);
return null;
}
return api.addReaction(commentId, emoji);
},
onSettled: () => {
qc.invalidateQueries({ queryKey: issueKeys.timeline(issueId) });
},
});
}
// ---------------------------------------------------------------------------
// Issue-level Reactions
// ---------------------------------------------------------------------------
export function useToggleIssueReaction(issueId: string) {
const qc = useQueryClient();
return useMutation({
mutationKey: ["toggleIssueReaction", issueId] as const,
mutationFn: async ({
emoji,
existing,
}: ToggleIssueReactionVars) => {
if (existing) {
await api.removeIssueReaction(issueId, emoji);
return null;
}
return api.addIssueReaction(issueId, emoji);
},
onSettled: () => {
qc.invalidateQueries({ queryKey: issueKeys.reactions(issueId) });
},
});
}
// ---------------------------------------------------------------------------
// Issue Subscribers
// ---------------------------------------------------------------------------
export function useToggleIssueSubscriber(issueId: string) {
const qc = useQueryClient();
return useMutation({
mutationFn: async ({
userId,
userType,
subscribed,
}: {
userId: string;
userType: "member" | "agent";
subscribed: boolean;
}) => {
if (subscribed) {
await api.unsubscribeFromIssue(issueId, userId, userType);
} else {
await api.subscribeToIssue(issueId, userId, userType);
}
},
onMutate: async ({ userId, userType, subscribed }) => {
await qc.cancelQueries({ queryKey: issueKeys.subscribers(issueId) });
const prev = qc.getQueryData<IssueSubscriber[]>(
issueKeys.subscribers(issueId),
);
if (subscribed) {
qc.setQueryData<IssueSubscriber[]>(
issueKeys.subscribers(issueId),
(old) =>
old?.filter(
(s) => !(s.user_id === userId && s.user_type === userType),
),
);
} else {
const temp: IssueSubscriber = {
issue_id: issueId,
user_type: userType,
user_id: userId,
reason: "manual",
created_at: new Date().toISOString(),
};
qc.setQueryData<IssueSubscriber[]>(
issueKeys.subscribers(issueId),
(old) => {
if (
old?.some(
(s) => s.user_id === userId && s.user_type === userType,
)
)
return old;
return [...(old ?? []), temp];
},
);
}
return { prev };
},
onError: (_err, _vars, ctx) => {
if (ctx?.prev)
qc.setQueryData(issueKeys.subscribers(issueId), ctx.prev);
},
onSettled: () => {
qc.invalidateQueries({ queryKey: issueKeys.subscribers(issueId) });
},
});
}