Skip to content

feat(db): add Chat Wave 4's tables for models, drafts, What's new, Library, agent calls and outside e-mails (W4-DB, 00462–00467) - #2006

Merged
gkhngyk merged 4 commits into
ansvisor:mainfrom
cihan-gyk:feature/chat-wave4-db
Nov 2, 2026
Merged

gkhngyk merged 4 commits into
ansvisor:mainfrom
cihan-gyk:feature/chat-wave4-db

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

Chat Wave 4's tables in one PR, behind CHAT_AGENTIC, one file per feature: the model menu and image metering, a draft's origin, What's new reads, Library skills, agents that call agents, and e-mails to addresses outside the workspace. Nothing reads or writes them yet; each feature's code comes in its own PRs, which merge only once the feature is complete. Migrations 00462–00467. Plan: Chat Wave 4 plan (2026-11-02), W4-DB; owner decisions 2026-11-02.

Stacked on #1990 (W3-DB, 00457–00459): 00465 changes chat_skills and 00467 chat_schedules. Merged in, not rebased.

00462 · models and images

  • chat_model_prices.supported_parameters and context_length: the model menu offers only a model that can call tools. Null until the next price sync lists the model. Still server only.
  • prompt_improve_usage.units: how many images an image row paid for (1–10, image rows only). The row's cost stays the provider's real cost, with no markup (owner decision 2026-11-02).

00463 · where a draft was made

  • content_drafts.origin (editor by default, chat, action_center, agent, mcp) and conversation_id (on delete set null).
  • Existing drafts with an action_id become action_center; the updated_at trigger is off for that one statement, so the Library's order doesn't change.
  • A member's own write may say editor or chat and name only a chat of their own; neither changes afterwards. Everything else is the server's. This is its own trigger, so guard_content_draft_server_columns stays as it is.

00464 · What's new reads

  • chat_whats_new_reads (user_id, entry_slug, read_at), one row per person and entry. A person reads only their own rows; the server writes.

00465 · Library skills

  • chat_skills.source gains library, with library_ref (once per workspace) and update_checked_at for the weekly update check.
  • 00458's unnamed rule that only an import has a source URL and commit is found by its definition, dropped, and added again with a name, now allowing library too. A Library skill must have its ref and a pinned commit.

00466 · an agent that runs another agent (CHAT-27 phase 2)

  • chat_agent_versions.can_call uuid[]: at most 5 agents, never itself, all of its own workspace (trigger).
  • chat_agent_runs.trigger_kind gains agent, with called_by_run_id (on delete cascade, like 00419's subtasks). Only such a run has a caller.
  • A trigger backs run_agent's own checks: a called run can't call further (no chains, so no cycles), an agent doesn't call itself, and a run calls at most 3 times, under an advisory lock.

00467 · e-mails outside the workspace, each sent with a click

  • chat_schedules.external_recipients: up to 5 lowercase addresses, once each.
  • chat_outbound_emails: one prepared e-mail per run and address: pending, then sent, cancelled or expired after 7 days. A failed send stays pending with its reason, so Send can be tried again. It is read as its job is.

RLS is on for both new tables: anon gets nothing, members only read, and the server writes. chat_outbound_emails reaches the brand through its job; chat_whats_new_reads is a person's own rows.

schema.sql, types/supabase.ts, schema.test.ts and the brand-access policy test are updated.

Related issue

None. Chat Wave 4 plan, W4-DB.

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

On a dev database that has 00457–00459, run the verify script inside one transaction that rolls back. It checks:

  • RLS and grants on both new tables, and that chat_model_prices still has no member grant;
  • the backfill (action_center for drafts with an action) and that no draft's updated_at moved;
  • units only on image rows;
  • origin values, and that a member can say editor or chat, can't name another person's chat, and can't change either afterwards;
  • the Library skill rules, and that a user skill still can't carry a URL;
  • can_call (same workspace, at most 5, not itself);
  • agent calls (needs a caller, no chains, not the same agent, at most 3);
  • outside addresses and prepared e-mails;
  • one What's new read per person and entry, and that anon reads nothing.

Then:

  1. bash supabase/build-schema.sh: schema.sql doesn't change.
  2. cd web && yarn vitest run src/lib/chat/schema.test.ts src/lib/supabase/embed-hints.test.ts plus the coverage tests.
  3. cd server && npx vitest run src/lib/brand-access-policies.test.js src/lib/brand-access-account-tables.test.js src/lib/demo/coverage.test.js.

Needs migrations on prod: 00462, 00463, 00464, 00465, 00466, 00467, in order, after 00457–00461 (gkhngyk). Each runs in one transaction (psql -1 -f); 00463 turns the content_drafts updated_at trigger off and on inside its own transaction.

No new environment variables.

Checklist

  • Branch follows the naming convention
  • Commits follow Conventional Commits
  • yarn lint passes
  • yarn typecheck passes
  • yarn format:check passes
  • Changes are focused — one concern per PR
  • Help article updated under help/ (or no user-visible change)

CI passed on this branch (d0a910d), run on the fork (https://github.com/cihan-gyk/ansvisor/actions/runs/41443249288): schema is up to date; web format:check, lint, typecheck, all tests and yarn build pass; server lint, format:check and all tests pass.

🤖 Generated with Claude Code

cihan-gyk and others added 3 commits November 2, 2026 09:12
…3-DB, 00457–00459)

Chat's Wave 3 tables in one PR (one DB PR per wave), behind CHAT_AGENTIC:
nothing reads or writes them while the flag is off. Tables, types, tests
and the demo's coverage only; the code that uses them comes in W3-01,
W3-06 and W3-11. Every new table has row level security on; members only
ever read, anon gets nothing, and every write is the server's.

- 00457: chat_schedules (CHAT-12): once, recurring or a follow-up check;
  run at / ready by, due and next run, the RRULE and time zone; title,
  prompt or skill, the brand fixed at creation, the creator it acts as and
  its own chat; delivery to chat and/or email, recipients only people of
  the workspace (a trigger checks), change-only with the last fingerprint;
  mode, model, effort; on/off with the reason, failures in a row, the one
  retry; a follow-up check's target, metric and who set it (card or
  full_auto, the latter only for follow-up checks). A job whose brand or
  creator is deleted turns off (missing_source, owner_role) instead of
  running on without them. chat_schedule_runs: slot and attempt (unique,
  so a slot never runs twice), Run now, the Chat run and answer, status,
  parts done, fingerprint, usage as a share, email outcome.
  chat_conversations.schedule_id with origin 'schedule' (one live chat per
  job; it stays the creator's), prompt_improve_usage.schedule_id, and
  reserve_chat_ai_usage taking p_schedule_id (default null, old calls still
  match): the row is named by the chat's job, a job that isn't the chat's
  is refused. Creator and Admins read a job, within their brands.
- 00458: chat_skills (CHAT-13): name unique in the workspace in the
  format's rules, body up to 50 KB, licenses MIT, Apache-2.0,
  BSD-2-Clause, BSD-3-Clause, ISC (an import needs one), source user or
  import with the pinned commit and checksum, scan and removed parts,
  review status (only an approved skill can be on), private or workspace.
  Private or unapproved: creator and Admins; approved workspace skills:
  every member. chat_settings.disabled_skills for library skills.
- 00459: chat_memory_items (CHAT-14): Me, brand and workspace lines, 1–300
  characters, at most 4,000 a scope (a trigger under a lock per scope,
  deleted lines left out), one profile line a person, deleted_at for Undo;
  brand lines go with the brand, Me lines with the account and when the
  person leaves the workspace (trigger on profiles). Me: only its person;
  brand lines: brand access; workspace lines: every member.
  chat_settings.memory_enabled and chat_user_prefs.memory_enabled, both on.

web/src/types/supabase.ts carries the new tables and columns; schema.test.ts
checks the Wave 3 policies, limits, vocabularies, triggers and the hold's
signature from the SQL; brand-access-policies.test.js lists the new tables;
the demo coverage skips chat_schedules and chat_memory_items.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
# Conflicts:
#	server/src/lib/brand-access-policies.test.js
#	web/src/types/supabase.ts
…brary, agent calls and outside e-mails (W4-DB, 00462–00467)

- 00462: the price catalog's supported_parameters and context_length, and
  prompt_improve_usage.units for image rows.
- 00463: content_drafts.origin and conversation_id; drafts with an action
  become action_center without touching updated_at; a member may say editor
  or chat and name only a chat of their own.
- 00464: chat_whats_new_reads, a person's own rows.
- 00465: chat_skills source library, library_ref and update_checked_at;
  00458's unnamed pin rule named and widened.
- 00466: chat_agent_versions.can_call and agent-called runs, one level deep,
  at most 3 a run.
- 00467: chat_schedules.external_recipients and chat_outbound_emails, each
  sent only with a click.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added the needs-migration Adds or edits a migration — apply it to the hosted database label Nov 2, 2026
gkhngyk
gkhngyk previously approved these changes Nov 2, 2026

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

LGTM

# Conflicts:
#	server/src/lib/brand-access-policies.test.js
#	supabase/schema.sql
#	web/src/lib/chat/schema.test.ts
#	web/src/types/supabase.ts

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

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-migration Adds or edits a migration — apply it to the hosted database

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants