Skip to content

feat(chat): let the person attach Chat's file to its task and mark it done, with Undo (W2-12) - #1902

Merged
gkhngyk merged 17 commits into
ansvisor:mainfrom
cihan-gyk:feature/chat-w2-12-attach-done
Oct 31, 2026
Merged

gkhngyk merged 17 commits into
ansvisor:mainfrom
cihan-gyk:feature/chat-w2-12-attach-done

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

Chat W2-12 lets the person attach a file Chat made to its Action Center task and mark the task done, in one click, as themselves (CHAT-09 items 12–13 and 16, mockup CHAT-mockup-v2.html#s-done). Stacked on #1900 (W2-11); review only the last commit. Everything stays behind CHAT_AGENTIC / CHAT_AGENTIC_ORGS. No migration: chat_file_task_links and chat_files.status (00411) and chat_task_state.file_id (00418) already exist.

Chat never closes a task itself. No model tool can. The task is closed only by the person's click, through the same write the Action Center drawer uses.

Which task a file belongs to (engine)

  • When a /tasks run files a deliverable, it goes on chat_task_state.file_id of a task the run is writing. The candidates are open tasks of the action the run last read (get_action) that have no file yet.
    • With one candidate, the file goes on that task.
    • With more than one, a page draft goes on the only create or optimize task.
    • Otherwise nothing is written, and the card shows no "Task:" line.
  • If the model attached the file to one task with W2-10's Attach only, the card uses that task.
  • After a reload, each deliverable's task comes back from chat_task_state.file_id or its single link.

Server actions (lib/actions/chat-task-done.ts), the person's click only

  • The file is found by (runId, n); no id reaches the browser. A run that isn't the person's own, live chat answers not_found.

  • The refusals:

    • anyone but an Admin or Manager gets forbidden;
    • a read-only workspace gets read_only, and the card opens the plan dialog;
    • a closed action gets closed.

    The plan check is each action's first await.

  • attachAndMarkDone:

    • writes the link (attached_by = the person) and marks the file done;
    • closes the task with the drawer's write. logEvent, touchAction and a new writeTaskStatus move to lib/action-center/task-status.ts, and updateTaskStatus now calls it, with the same behaviour.
    • The History line names the person, with source: 'chat', the conversation and the run.
    • The x/y progress follows, as in Action Center.
  • attachOnly calls W2-10's attach as the person. The file becomes attached, and the task stays open.

  • Undo (toast "Task marked done in Action Center"):

    • It puts back the task's previous status and the file's previous status, and removes the link if the click made it.
    • It writes a History event with via: 'undo'.
    • It works only while three things hold: this mark-done is still the action's latest History event, the task is still completed, and the action's status is unchanged, all within an hour. Otherwise it answers changed and changes nothing.

Card (DeliverableCard)

  • A file with a task shows "Task: {title}" and Attach to task & mark done. Attach only is in ⋯ while the file is a draft.
  • After the click: "Done · attached to task". A task already closed in Action Center shows "Done in Action Center".
  • Analysts and Agency partners see the "Task:" line without the button.
  • The task strip (W2-11) is read again, so its row turns Done.

Help: help/chat/chat.md gets "Close a task from what Chat made" (approved: false).

Note: Action Center tasks count as text from outside (CHAT-20 §16a, generated tasks). So a /tasks draft after get_action asks for a card even in Full auto, as since W2-11.

Related issue

None. Chat Wave 2 (W2-12).

Type of change

  • feat — New feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

How to test

  1. Set CHAT_AGENTIC=true. Run /tasks on a brand with an open task Chat can draft for.
  2. The deliverable card shows "Task: …" and Attach to task & mark done.
  3. Click it:
    • the card says "Done · attached to task";
    • the strip's row turns Done;
    • in Action Center the task is completed, with a History line by you from Chat.
  4. Click Undo in the toast: the task is open again and the file is a draft. Close the action in Action Center first, and Undo answers that it changed.
  5. In ⋯, Attach only attaches the file and leaves the task open.
  6. As an Analyst, the card shows the task line without the button.
  7. Unit tests:
    • chat-task-done (roles, read-only, closed action, every Undo case);
    • the engine's file_id hook (one task, the draft tie-break, two candidates, an action not read, another run);
    • thread, the card, task-status, and the Action Center drawer tests.

Checked in the browser at 1024px, light and dark: a card with a task, a done card, a card with no task, and the ⋯ menu. The user approved the screens. Screenshots are available on request.

Checklist

  • Branch follows the naming convention
  • Commits follow Conventional Commits
  • yarn lint passes (0 errors; 7 old warnings in files this PR doesn't touch)
  • yarn typecheck passes
  • yarn format:check passes
  • Changes are focused — one concern per PR
  • Help article updated under help/ (or no user-visible change)

On this branch (aeda08a):

🤖 Generated with Claude Code

cihan-gyk and others added 16 commits October 31, 2026 03:42
…ad a skill when Chat picks it (W2-04)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… skills (W2-04)

The library is now the v2 mockup's Ansvisor skills, by the ids CHAT-21's
agents use (winback, tasks, quality, faq, report, beat, refresh, publish,
shopping, wrongclaim, answerfirst, facts, compare, translate), with the
mockup's titles and card lines. Six reuse the earlier bodies; the nine
earlier skills that map to none are removed. A message calls a skill by
the /commands it starts with, so a page path like "/faq" later in the text
isn't one, and the /report skill takes the /report shortcut's place in the
/ menu.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…l name a skill writes (W2-04)

- The scan also reads the front matter's values once parsed, so a
  double-quoted escape can't hide an instruction, hidden character, email
  or price. The reader keeps metadata without a prototype, needs a space
  after each colon and refuses tabs and mixed indents.
- Every snake_case name in backticks must be one of Chat's tools.
- report, facts and publish describe what Chat does: no email, approved
  facts only, a CMS draft.
- A skill the model picks after the run read outside text isn't loaded;
  the person's /name still is. The prompt says a /name without a block
  wasn't loaded.
- skills/README.md says how a Chat skill is added, and that outside
  skills come later through import.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… (W2-04)

On Chat Home the box sits high on the page, so the / menu with the
fourteen skills ran past the header. The /, @ and + menus now pick their
side as they open, and again when the window changes size: above when
their rows fit between the box and the nearest clipping box (the page's
scroll area under the header) or the window, else below when there's more
room there. Their height is the room on that side less the gap, at most
300px, and the active row stays in view as the arrows move it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ding outside text (W2-04)

A run started from a tainted run's next move starts tainted (fromTaintedMove):
its message is that model's text, so a leading /name in it is the model's
pick, not the person's. Such a run now loads no typed skill; the person's own
/name in the same chat still loads.
…uplicates checked first (W2-10)

CHAT-09 §E and CHAT-20 §12 (V1 Final rules), behind the web's Chat tools.

- POST /api/action-center/chat-actions/check: the brand's active actions
  that already cover a finding's prompts, pages or competitors (their kept
  list, 00244, and the prompts and pages their payload names), closest
  first: all of it, or some.
- POST /api/action-center/chat-actions: the finding as an action, kind
  chat_finding with payload.source chat, the chat, the run and
  execution.mode (assisted or automated). An action that covers it answers
  409 duplicate with that action and whether Create anyway may go (only
  when the entities differ); the same idempotency key returns what it made.
- Frozen at creation: every entity, the targets, the baseline (the basket's
  metrics over the 14 days before, each named prompt per named platform)
  and the approved tasks, checked against AC-004 (a page or place per task,
  Done when, no analyze/identify/validate/measure, no "for N prompts").
  tasks_generated_at is set, so the task writer never rewrites them.
- Manual: Chat's own dedup key takes no definition slot, and the generator
  leaves chat_finding out of the day's five. The engine scores it on
  creation (priority.js sentByHand) and validate.js measures it on its
  prompts and pages, as other person-sent kinds.
- Admins and Managers of the brand only; both routes on the demo-brand list.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…tasks and set Ready for review, with Undo (W2-10)

CHAT-09 §D–E, CHAT-20 §9 rows 43–46, §10a–b, §12. Behind CHAT_AGENTIC.

Tools (lib/chat/registry/action-center-tools.ts, Admins and Managers,
policy Ask (auto allowed in Full auto)):
- create_action (Send to Action Center): its card lists the tasks; a
  duplicate check runs before the card (a row's new `precheck`), so an
  active action that covers it is said with no card, for Add to AC-{n};
  Create anyway only where the entities differ. Undo 24 h while no task
  started: the action is cancelled, in its History.
- attach_to_action (Add to AC-{n}): the finding as evidence in History;
  the action's targets never change. Undo 24 h.
- attach_file_to_task (Attach only): chat_file_task_links, file attached,
  History line. Undo 7 d. The model now gets the file id a change filed.
- set_task_ready_for_review: Chat's status in chat_task_state and a
  History line; the task's own status doesn't change. Undo 7 d.
No tool completes, dismisses or edits a task or an action.

Chat: Send to Action Center under the latest answer (Admins, Managers, own
chat); approval cards list a change's items; a new action.sent event shows
"Sent to Action Center · AC-n" / "Added to AC-n" with Open in Actions
(/dashboard/action-center/actions/[id] redirects to its drawer), kept after
a reload; the place (chat, run, approval key, execution.mode) reaches the
write rows.

Action Center: "Attached from Chat" in a task's details (name, who, when,
Open) with a count on the task row; History lines for created from Chat,
attachments, findings, Ready for review and their Undo; kind chat_finding
with its title, finding, goal and a Source: Chat block.

Help: chat, action-tasks and action-center-actions updated.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… as any person's send (W2-10)

The owner's decision of 2026-10-31: a Chat-created action counts toward the
brand's daily five new actions like Improve's, Content's or Mentions' sends.
Those are never refused or queued for it: they are made, and the nightly
generator opens fewer of its own, the rest waiting in its queue. So:

- generate.js counts chat_finding again (the exclusion is taken back), with
  a test that Chat's sends today leave the generator less room.
- POST /api/action-center/chat-actions still always makes the action, and
  says the day's count (dailyLimit: used, limit, reached) after it.
- createChatAction passes on daily_limit_reached and the one sentence Chat
  says then (chatEngine.actionCard.dailyLimit); create_action's description
  tells the model to say it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…rver (W2-10)

Review follow-ups for Send to Action Center's route (CHAT-09 §E, CHAT-20 §12).

- S1: an action whose tasks can't be written is deleted again, so no action
  is left without the tasks the person approved; the same approval tried
  again makes it whole.
- S2: the duplicate check reads the competitors a payload names
  (competitorIds, competitorId) beside the kept list.
- K3: the chat, the run and how it was approved are read again on the
  server (verifiedPlace): the caller's own chat, a run of it, and its step
  by the approval key, whose mark says automated. A caller's word alone is
  never recorded; executionMode is null when it can't be read.
- K4: an approval whose action was cancelled or deleted since answers 409
  conflict instead of "made".
- K5: the routes' 400s carry code invalid.
- K9: a bare word ("pricing", "blog/aeo") is a path on the brand's site,
  never a host.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… and the Action Center drawer (W2-10)

- B1: Send to Action Center sends the answer's run (fromRunId). The server
  reads that run's taint, as for a next move; a run that isn't this chat's,
  or an unreadable one, counts as tainted. A change written from an answer
  that read outside text shows its card even in Full auto (§16a rule 3).
- S3: create_anyway shows its card in every mode, Full auto included
  (alwaysCardWhen, read as overBatch by the precedence): that choice is the
  person's (CHAT-09 18a).
- S4: the card shows what is frozen once made: each task with its new text
  (shortened, Show all), the finding (why), the goal and the targets
  (prompts, pages, competitors, platforms). cardDetails replaces cardItems;
  ApprovalCard takes children; copy in chatRun.proposal.
- S5: Undo of a sent action only dismisses it (no deleted_at), so it stays
  in Dismissed, with "Cancelled from Chat by {user} · undo" in History; any
  event but created since (assigned, edited, a task added, a finding or a
  file attached) answers changed.
- S6: conflict is a said refusal: a closed action's or task's words reach
  the model.
- K1: a file's title on the card is read within the brand's workspace.
- K3: the server action reads the chat, run and step itself (the step the
  engine marks when it ran without a card) and records nothing a caller
  only said; Ready for review refuses a task of another brand than its
  chat's.
- K5: 403 not_in_workspace / no_brand_access and 409 conflict are said as
  such.
- K6: the paperclip says "{n} files attached from Chat" to screen readers.
- K7: unique keys; Open the chat only for a reader who may open it.
- K8: actions/[id] opens the drawer on the action's brand with no filter.
- K10: a finding's note and sources show in History; a withdrawn one says
  which finding.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
# Conflicts:
#	web/src/lib/chat/engine.test.ts
… in a task strip under the answer (W2-11)

CHAT-09 §B, CHAT-08 Complete tasks, mockup #s-work / #s-done; behind
CHAT_AGENTIC, no migration (chat_task_state, 00418).
- task-strip.ts: a /tasks run queues the week's tasks, at most 4, due
  first and by Priority Score; opening an action (get_action) marks its
  tasks Writing; Ready for review comes from W2-10; the run's end settles
  Writing back to Queued. Done is read from the Action Center only.
- The engine keeps the strip from its reads, only in a run that loaded the
  /tasks skill, and never fails the run over it; `tasks.updated` carries
  the whole strip, with no ids.
- The answer shows "Action Center tasks · N due", "X of N ready" and a row
  per task ("Priority 86 · Fix", Queued / Writing / Ready for review /
  Done), and once the run ended "Close tasks in Action Center". A reload
  rebuilds it from chat_task_state as the person may see it.
- Complete with Chat sends /tasks; the /tasks skill sets Ready for review
  and helps with open Getting started steps, never marking one done.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A task with no title is worded from its key in en.json (taskText); a key
en.json doesn't word now reads "An Action Center task" instead of the
message path next-intl falls back to.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…s card, with Undo (W2-12)

CHAT-09 items 12–16; no migration (chat_file_task_links 00411,
chat_task_state.file_id 00418).
- A /tasks run's deliverable is tied to its task only when that is
  certain: the one task Chat is writing of the action it opened last
  (get_action), a page draft breaking a tie; else no "Task:" line.
- Its card says "Task: {title}"; Admins and Managers get Attach to task &
  mark done and, in ⋯, Attach only. The card names its file by its run and
  place; the run must be of the person's own chat.
- Mark done links the file, sets it done and completes the task through
  the Action Center's own write (task-status.ts, shared with the drawer's
  updateTaskStatus): the person is the actor, Chat the source in History.
  Its toast's Undo puts the task, the file and the link back within the
  hour, and says changed once the task, its action or its History moved.
- A reload brings each deliverable's task back; the strip shows Done.
- No model tool closes a task.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Resolves help/chat/chat.md keywords, thread.ts imports and thread.test.ts against the squashed ansvisor#1897/ansvisor#1900 on main.
…rk done or its Undo (W2-12)

- markTaskDoneFromCard and undoTaskDoneFromCard call can_access_brand on the task's brand, as W2-10's writes do, and refuse a file of another brand (not_found).
- assertWritable({ brandId }) runs before the first write, so a demo brand leaves no link or done file behind.
- A skipped task's why goes in the mark done's event; Undo puts it back.
- The test's person client answers can_access_brand (main's attachFileToTask now calls it).

@gkhngyk gkhngyk 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.

Approving, with 2 commits on top: commit f06a4b7 merges main in, and commit 7fc1204 fixes the card's server actions.

Merge (f06a4b7). Main now has #1897 and #1900 squashed, so the stack's history conflicts. The merge keeps main's versions of everything from W2-10 and W2-11, and puts only this PR's last commit on top. Three files needed hand fixes:

  • help/chat/chat.md: main's keywords plus the three new ones.
  • thread.ts: the why/* imports kept next to readDeliverableTasks.
  • thread.test.ts: both new tests kept.

The card test's person client also needed rpc. Without it, the Attach only tests fail on main, because attachFileToTask now checks can_access_brand.

Fix (7fc1204), chat-task-card.ts

  • markTaskDoneFromCard and undoTaskDoneFromCard now call can_access_brand on the task's brand, as W2-10's Chat writes do. They also refuse a file whose brand isn't the task's, as attachFileToTask does. Both cases answer not_found and write nothing.
  • assertWritable({ brandId }) now runs after the card is read and before the first write. Before, only the plan was checked, so on a demo brand the link and the file's done were written before the database refused the task update.
  • When the task was Skipped, its reason now goes in the mark-done event, and Undo puts it back. Before, Undo returned the task to Skipped with no reason.
  • Each fix has a test that fails without it.

Checked, fine as is

  • Admin/Manager is checked on the server on every click, and the plan check is still each action's first await (subscription-guard-coverage passes).
  • manager_execute_off covers only go-live groups, so a mark done doesn't count.
  • The action_tasks → actions embed names action_tasks_action_id_fkey, the only FK.
  • Completing a task doesn't change the action's status and has no task-level triggers on prod, so Undo leaves no completion, measurement or ledger state behind.
  • On reload the strip and the card read action_tasks.status, so they show Done or Open correctly.
  • en.json keys all exist.
  • The help section is within the limit, and the article stays approved: false.

Follow-ups (not blocking)

  • Attach only calls attachFileToTask without a place, so its History event has no conversation or run. Mark done records both.
  • Two clicks that land together can both read the task as open and write two events. The button is disabled while busy, so this is a narrow window. A conditional update (.neq('status', 'completed')) would close it.
  • The PR body names lib/actions/chat-task-done.ts. The file is chat-task-card.ts.

Tests: web vitest on src/lib/chat, src/components/chat, action-center, actions, guards, subscription-guard-coverage, demo-gate-routes, help and plan-row. 3,085 tests: all pass, except the two known timing tests, which pass when run alone. tsc --noEmit, eslint and prettier are clean on the touched files.

@gkhngyk gkhngyk 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.

Merging in order with the other approved PRs. This head merges current main; its tree is what main will be after this squash, checked with the full server and web suites, tsc, lint, format and next build.

@gkhngyk
gkhngyk merged commit e316fbb into ansvisor:main Oct 31, 2026
4 of 5 checks passed
gkhngyk pushed a commit that referenced this pull request Nov 1, 2026
…click repeats (#1935)

Follow-ups from the approval notes on #1897, #1902 and #1898.

- Add to AC-{n} (attachToAction, W2-10): the evidence_attached event
  carries its approval's key (`chat:{step}`, as Send to Action Center's
  payload does), and a retried approval finds that event and returns it
  instead of adding the finding again. Two calls at the very same moment
  are already kept apart by the engine's idempotency claim; a database-level
  guarantee would need a unique index (a migration), not added here.
- Card clicks (W2-12): Attach to task & mark done and its Undo write the
  task's status only when it really changes (`.neq('status', …)` through
  writeTaskStatus's new onlyIfChanged), so two clicks at once leave one
  History event. The drawer's own writes are unchanged.
- Attach only from a card now records the card's chat and run on its
  file_attached event, as a mark done's event does. attachFileToTask takes
  the card's place and reads it on the server (the person's own chat and a
  run of it) before recording it; no execution mode, as it is the
  person's own click.
- Daily suggestions (W2-26): recordUse skips a removed suggestion (no use,
  no feed stamp), and the page's last Site Audit matches the action's pages
  by pageKey (no scheme, www or trailing slash), by the audit's url or
  final_url, among the brand's latest 500 audits (as the server's recheck
  reads them).
- The test fake reads JSON-path filters (`data->>key`) as PostgREST does.

Co-authored-by: Cihan <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
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.

2 participants