Skip to content

feat(db): add Chat's schedules, workspace skills and memory tables (W3-DB, 00457–00459) - #1990

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

gkhngyk merged 2 commits into
ansvisor:mainfrom
cihan-gyk:feature/chat-wave3-db

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

Chat Wave 3's tables in one PR, behind CHAT_AGENTIC: scheduled jobs, workspace skills and memory. Nothing reads or writes them yet; the engine and the screens come in the following PRs. Migrations 00457–00459. Plan: Chat waves plan, Wave 3; owner decisions 2026-11-02.

00457 · scheduled jobs

  • chat_schedules and chat_schedule_runs.
  • Recipients are limited to workspace members (trigger).
  • A slot never runs twice: (schedule_id, slot_at, attempt) is unique.
  • A job whose brand or creator is deleted turns off (missing_source / owner_role).
  • full_auto creation is allowed only for follow-up checks.
  • chat_conversations.schedule_id (origin schedule) and prompt_improve_usage.schedule_id.
  • reserve_chat_ai_usage(..., p_schedule_id default null), copied from 00419. Old calls still match, and a p_schedule_id that doesn't match the run's conversation is refused, like p_agent_run_id.

00458 · workspace skills

  • chat_skills: unique name per workspace, body ≤ 50 KB, only approved skills can be on.
  • Imports need MIT, Apache-2.0, BSD-2/3-Clause or ISC (owner decision).
  • chat_settings.disabled_skills.

00459 · memory

  • chat_memory_items: user, brand and workspace lines of up to 300 characters, at most 4,000 per scope. A trigger keeps the cap and locks against concurrent inserts; deleted lines don't count.
  • Lines are deleted with their brand, and a person's own lines when they leave the workspace.
  • One "Call me …" profile line per person.
  • memory_enabled on chat_settings and chat_user_prefs, both on by default (owner decision).

RLS is on for every table: anon gets nothing, members only read, and the server writes. Brand rows use user_brand_ids(). A person's own memory lines are theirs alone, Admins included.

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

Related issue

None. Chat waves plan, Wave 3 (W3-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

Checked on the dev database, first in a rolled-back transaction with the verify script, then applied. The script checked:

  • RLS is on for all four tables, and authenticated has only SELECT;
  • one reserve_chat_ai_usage remains, with the new parameter;
  • a brand deleted turns its job off; an outside recipient is refused, a member is fine;
  • one run per slot and attempt;
  • skill name, enabled and license rules;
  • 300 / 4,000 memory caps, with deleted lines left out; one profile line per person;
  • what the demo admin sees under RLS; anon has no access.

Then:

  1. bash supabase/build-schema.sh: schema.sql doesn't change.
  2. cd server && npm test: full suite passes (363 files / 5,096 tests).
  3. cd web && yarn vitest run src/lib/chat/schema.test.ts src/lib/supabase/embed-hints.test.ts plus the coverage tests (289 tests).

Needs migrations on prod: 00457, 00458, 00459 (gkhngyk).

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)

CI passed on this branch (5324323), run on the fork (https://github.com/cihan-gyk/ansvisor/actions/runs/41398629866): 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

…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]>
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
#	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

@gkhngyk
gkhngyk merged commit 3190cb6 into ansvisor:main Nov 2, 2026
5 checks passed
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