Repository navigation
feat(chat): copy each person's Agent Chat history into Chat on first open (W1-02) - #1837
Conversation
…iles, Help and add-on packs Chat's Wave 1 tables in one PR (one DB PR per wave), behind CHAT_AGENTIC: nothing reads or writes them while the flag is off. Tables, types and the demo's coverage only; the code that uses them comes in the Wave 1 PRs. - 00407: chat_conversations gets shared_at (Share with team: every member of the workspace reads it), last_read_at (unread results), deleted_at (Delete with Undo; deleted chats are hidden from every member) and a title search column; chat_messages gets context_json (what a person's message was scoped to) and a text search column. 'simple' text search, so a chat in any language is found. The author policy now leaves deleted chats out, and a shared policy reads shared ones. - 00408: chat_user_prefs gets the rest of Settings › General and the one-time notices: rail_open, panel_size, default_mode, answer_language, notify_via, show_thought, panel_on_pages, focus_prompted_at, usage_notice_period, legacy_imported_at. - 00409: chat_uploads and the private bucket chat-uploads: 10 MB a file, the composer's 11 types, 5 a message (a trigger backs the server's check), kept 90 days. - 00410: chat_tool_cache (a chat's read results by tool and scope, server only) and each step's and model call's measure: latency, cache hit, truncation, result tokens on chat_run_steps (never granted to members), prompt cache read/write tokens and latency on the ledger. - 00411: chat_files (filed deliverables and saved answers, the workspace's, kept a year, searchable by title, summary and tags), chat_file_task_links, chat_storage_usage kept by a trigger, and the private bucket chat-files. - 00412: help_answer_cache and help_gaps (server only, no workspace or person ids). - 00413: add_on_products seeded with the owner's packs (AI credits $10, $50, $250, $1,000 at half their price in budget, 12 months; storage +25/+100/+250 GB at $5/$15/$29 a month; $50 and +25 GB Most picked), add_on_purchases (Admins read), ai_credit_grants and storage_grants (server only), and prompt_improve_usage.grant_id. Members only ever read; every write is the server's. Tool names, tool results and dollars stay server only. web/src/types/supabase.ts carries the new tables and columns; schema.test.ts checks the policies, the server-only tables, the limits and the seeded packs from the SQL; steps.test.ts keeps the step measure out of the member columns. The demo coverage skips chat_files and chat_tool_cache (a person's own Chat work). Co-Authored-By: Claude Opus 5.5 <[email protected]>
Behind CHAT_AGENTIC; with it off, the Agent page and its menu link stay as
they are today.
- /dashboard/agent and every path under it answer a 301 to the matching
/dashboard/chat path, query kept, where the new Chat is on for the
person's workspace (CHAT-01 item 2). Sent with no-store, so turning the
flag off again takes effect at once. The sidebar and mobile menu link
Chat straight to /dashboard/chat.
- The chats API (CHAT-01 §1-3, 7-8, 26): GET /api/chat/conversations lists
the person's chats and the team's shared ones (or their archived ones),
newest first, with each row's brand name, dot and live step in words, and
the Chats / Archived counts; POST starts an empty chat; GET .../{id} opens
one with its messages and steps; DELETE stops a running agent, then hides
the chat for everyone; .../restore is the Undo (one minute); .../read
marks its result read for the author. Refusals come back as values
(ansvisor#1777), never thrown.
- A chat that isn't there, was deleted or isn't shared says "This chat isn't
available." with New chat (item 26); opening one marks it read.
- The engine and access requests read chats with the service role, so they
now leave deleted chats out too (ansvisor#1812 note).
- Help: a "chat" area and its first article (approved: false).
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Behind CHAT_AGENTIC; with it off the Chat pages have no frame, as today. - Every Chat route now sits in a shell (CHAT-01 items 5, 9a-9f; CHAT-23 G1): the 256px chat menu beside the page and a 52px header with the open chat's title and status pill, and the runs tray. - The menu, top to bottom: the toggle and New chat (Ctrl+Shift+O / ⌘⇧O); Menu with Home and the count of what needs the person; Governance with Activity, What Chat can do and Tool policy (the governance mockup's menu); Agents at work (needs you, running, unread results: three rows, See all / Show less, the dots' legend); Chats with its count, Pinned, Today, Yesterday, Previous 7 days, Previous 30 days, then by month, in the person's own time zone. Rows show the dot, the title (bold while a result is unread), the live step or the result's time, or the brand, and a teammate's initials on a shared chat. Items whose pages aren't built yet stay out (Inbox, Settings, Skills, Integrations, Scheduled, Files, Search, All chats, Archived). - The toggle and Ctrl+\ (⌘\) hide or show the menu and keep the choice on the person for every device (chat_user_prefs.menu_open, a server action that answers as a value, ansvisor#1777). Hidden, the header starts with a strip: toggle, New chat, runs pill. Below 1100px the menu is a drawer that leaves the saved choice alone. Ctrl+B stays Bold. - The list is read from the chats API, again on each page change, every 5 s while a chat works or waits and every 30 s otherwise. - Thin, quiet scrollbars for Chat's lists (chat-scroll). - Help: the chat menu and how to hide it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ete with Undo
Behind CHAT_AGENTIC, like the rest of the Chat shell.
- PATCH /api/chat/conversations/{id} `{ pinned?, title?, shared?,
archived? }`: the author's own chat only (else 404), a trimmed title of
1 to 200 characters, answered as a value (ansvisor#1777).
- Each chat row's ⋯ (CHAT-23 chatMenu.rowMore, G9), on hover and focus:
Open, Open in new window, then for the author Pin / Unpin, Rename,
Share with team / Stop sharing, Archive, and Delete for the author or an
Admin on a shared chat. A teammate's shared chat only opens.
- The open chat's ⋯ in the header (G10): Chat settings, Open in new
window, Pin, Rename (in the title), Share with team, Export chat, Activity
log, Archive, Delete. Export chat is shown disabled ("Coming soon"):
wiring it to the answer exports is left to W1-25.
- Rename edits the name in place, in the row or the header: Enter or
clicking away saves, Esc cancels; double-clicking a row's title starts it.
- Every change shows at once and is then sent; a refused one is put back
with "Couldn't save that". Archive's toast and Delete's (10 s) carry
Undo; Delete asks first ("Delete this chat?", focus on Cancel, a red
Delete), stops a running agent, and leaves the open chat for Chat Home.
- A pinned chat shows the pin where its dot is.
- Help: managing and sharing a chat.
Split view, Pause / Stop run, Schedule this and Save as agent come with
their pages (Wave 2 and 4).
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…heir initials Other members' profiles are hidden from the person by their policy, so a teammate's shared chat showed no initials. Read with the service role, kept to the person's workspace, for the name only. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Behind CHAT_AGENTIC, like the rest of the Chat shell.
- Chat Home (CHAT-01 items 10-12, mockup #s-home): "Good morning /
afternoon / evening, {first name}" by the person's own clock (switching
at 12:00 and 18:00; no name, no comma), then one line: "Your agents
worked while you were away. N things need you." linking to the Inbox, or
"All caught up.". The composer stays below it.
- The Action Center line (item 12, CHAT-09 item 3): "N Action Center tasks
are due this week" (singular at 1, hidden at 0): the active brand's open
actions due by Sunday, overdue ones included, read as the person by a
server action that answers as a value (ansvisor#1777). Complete with Chat asks
Chat to complete this week's tasks in a new chat and opens it while the
run goes on.
- The Inbox at /dashboard/chat/inbox (mockup #s-inbox): what needs the
person (Review), what runs (its progress, Watch) and unread results
(Open), with "1 needs you · 1 running · 1 done today"; a row opens its
chat; one line when empty.
- The chat menu gets Inbox, with its count and an amber dot while
something needs the person (also beside Menu when folded), and the
collapsed strip gets an Inbox button, as in the mockup.
- Help: Chat Home and the Inbox.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
On a person's first visit to Chat (CHAT_AGENTIC on), their Agent Chat
conversations in their workspace are copied into chat_conversations and
chat_messages with the same ids, so old chats sit in Recents by their
last message and open at /dashboard/chat/{id}. Questions and answers in
words are kept; tool rows, tool-only answers and empty conversations are
not. The default title "New conversation" becomes "New chat".
Once per person: chat_user_prefs.legacy_imported_at (00408) is set only
after a complete copy, so a copy that failed is tried again next visit.
Already-copied ids are left alone (upsert ignoring duplicates), so two
visits at once or a second run change nothing. It never throws and never
keeps Chat from opening; the old tables are only read.
The test fake learns upsert, range and ordering by several columns.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…workspace Creating an empty chat, deleting, restoring and marking your own chat read start no cost; a chat's first message is a run, which the engine refuses with 402. The read-only coverage test lists them with that reason. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…read-only workspace Co-Authored-By: Claude Opus 5.5 <[email protected]>
…workspace Co-Authored-By: Claude Opus 5.5 <[email protected]>
…e clock with each list read A teammate's shared chat that waits on its author's OK no longer shows under Agents at work or in Home's count. A chat that waits on an OK or is paused no longer keeps the list on the 5 s pace. A menu left open past midnight now moves Today's chats to Yesterday on the next list read.
A person reads only their own profile, so the author's name came back empty on every teammate's shared chat. Read it on the server, within the workspace; only the initials leave.
…count A shared chat that waits for approval waits on its author, who alone can answer it. The Inbox, its counts in the menu and the strip, and Home's "things need you" now count the person's own chats only.
Agent Chat wrote a conversation's whole thread again on every turn in one insert, so its messages share one created_at and their ids don't follow the order. The copy read them by created_at then id, which shuffled them, and Chat reads a chat by created_at alone. Read each conversation the way Agent Chat's own page did (by created_at, no tiebreak) and give the copies a strictly rising created_at, a millisecond apart where they tied. Also cut a long title or message without splitting an emoji in half.
gkhngyk
left a comment
There was a problem hiding this comment.
Looks good with f0ec1bd: one fix commit on this branch (636f89a, described below), then a clean merge of #1832's new head (with the #1829–#1831 fixes).
Fixed: copied threads lost their order
Agent Chat's onFinish (api/agent/chat/route.ts) deletes a conversation's messages and inserts the whole thread again in one statement on every turn. So all the messages in a conversation share one created_at, and their ids are random. In practice almost every copied message shares its timestamp with others. The old page reads them order('created_at') and gets them in insert order from the index.
The copy reads them order('created_at').order('id'), which sorts each thread by random id, and inserts them in that order. Chat's thread (thread.ts) and the engine's history (readHistory) then order by created_at alone, so the questions and answers of a copied chat show in a shuffled order.
The fix is on take-1837:
- Each conversation is read the way the old page reads it, by
created_atwith no tiebreak. inOrder()gives the copies a strictly risingcreated_at. A time that ties with the one before it, or comes earlier, becomes a millisecond later. A time already in order is kept exactly.generated_atfollows the answer's new time.
This also cuts a title or message without splitting an emoji in half. A lone surrogate in the JSON body is refused by Postgres, and that would fail the whole copy on every visit. New tests cover both, and they fail on the PR head.
What I checked
- Once per person, safe twice at once. Rows are written with
upsert(..., { onConflict: 'id', ignoreDuplicates: true }), which isON CONFLICT DO NOTHINGon the primary key. That isn't check-then-insert, so two tabs at once write each row once. Both tabs write in the same order, so they can't deadlock.legacy_imported_atis set only after a full copy. A chat renamed in Chat is left alone by a rerun. - Only the person's own chats. The read filters on
user_idandorganization_idfrom the server-side profile, with the service role. - Old data intact.
agent_*tables are only selected (the test checks the log), and nothing is deleted. - Flag off.
chatShellPrefsreturns before the copy whenCHAT_AGENTIC/CHAT_AGENTIC_ORGSis off for the workspace (tested). - Size and time. Today's histories are far below the 200,000-character
content_mdlimit and the 200-character title limit. The copy is capped at the newest 1,000 conversations, written 500 rows at a time. Prompts and history are bounded by the engine's history budget, so the 2 MBchat_run_state.messagescap isn't at risk. - Timestamps.
created_atis kept (apart from the millisecond bumps above), andlast_message_atis the last copied message.updated_atends up at the copy time, because the message trigger touches the chat andhandle_updated_atruns. Nothing in Chat readsupdated_at, so that's fine. - Demo brand and read-only workspace. The copy is server-side through the service role during the page load, not a route or action, so the demo trigger and
subscription-guard-coveragedon't apply. It moves the person's own existing data and starts no AI cost, so it's reasonable to run on a read-only workspace too. A one-line comment saying so would help the next reader. - No new embeds and no migration.
Note for when #1822 lands
A copied answer has no chat_runs row, which every Chat answer has, so that's the marker for copied text. #1822's fact check counts the chat history's answers as places a figure can come from. Copied Agent Chat answers were never checked, so whichever of #1822 and this PR merges second should leave answers without a run out of those sources.
Optional, not blocking
legacy_imported_atis per person while the copy is per workspace. Someone who moves to another workspace after their first visit won't get that workspace's old chats.
Merge status: the PR's head contains #1812's current head. #1829's, #1830's and #1831's current heads conflict with it on their own (conversations.ts and its test). #1832's current head, which already resolves those, merges cleanly into this PR and into take-1837. The result merges cleanly on current main.
Tests: on take-1837 merged with #1832's head and main: src/lib/chat plus the Chat page tests (32 files, 456 tests), src/lib/guards/plan-row.test.ts, src/lib/subscription-guard-coverage.test.ts, and web tsc --noEmit all pass. Prettier and eslint pass on the touched files.
…nsvisor#1837 fixes) into take-1841
…nsvisor#1837 fixes) into take-1843 # Conflicts: # web/src/components/chat/chat-shell.tsx
gkhngyk
left a comment
There was a problem hiding this comment.
Merging in order with the other approved Chat PRs. This head merges current main; where they overlapped, both sides are kept. Its tree is what main will be after this squash, checked with the full server and web suites, tsc, lint, format and next build. Chat stays behind CHAT_AGENTIC.
Summary
Chat Wave 1, behind
CHAT_AGENTIC. Stacked on #1832 (W1-05). The diff against main includes the PRs below it until they merge. The product owner saw this in a local preview and approved it.W1-02: old Agent Chat chats come over.
legacy_imported_at(00408) is written only once the copy finishes, so a partial copy retries on the next visit.No migration, no new dependency.
Related issue
Part of Chat (Wave 1, CHAT-01).
Type of change
feat— New featureHow to test
Open Chat as someone with Agent Chat history: their chats are listed with the same ids. Tests:
legacy-import.test.ts(idempotency, skipped rows, partial retry).Checklist
help/chat/*,approved: false)🤖 Generated with Claude Code