Skip to content

feat(chat): add the workspace skill store, save_skill and the skills API (W3-06) - #2004

Merged
gkhngyk merged 20 commits into
ansvisor:mainfrom
cihan-gyk:feature/chat-skill-store
Nov 3, 2026
Merged

gkhngyk merged 20 commits into
ansvisor:mainfrom
cihan-gyk:feature/chat-skill-store

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

A workspace's own skills (CHAT-13), behind CHAT_AGENTIC, on chat_skills from 00458. Stacked on #1990 (W3-DB, migrations 00457–00459): merge it first. Until then, this PR's diff also shows #1990's commit. No migration of its own. Plan: Chat waves plan, W3-06.

Stacked on #2003 (W3-01, with W2-09 and W2-03): merge those first; conflicts already resolved.

There is no new screen: the Skills page comes in W3-07 and imports in W3-09. A saved skill shows in the / menu right away.

The store (lib/chat/skills/store.ts)

  • Admins and Managers save a skill made in Chat. It is checked as a SKILL.md (checkSkillFile: the format and the scan) and may not take an Ansvisor skill's name or a shortcut's (/start, /analyze, /new-report, /create, /optimize, /opportunities, /audit, W2-09).
  • It counts against the plan's My skills limit: trial 3, Starter 10, Growth 50, Enterprise 200 (owner decision); no limit on self-hosted. Rejected skills don't count.
  • A saved skill is approved, on and private to its creator at once. Its checksum is of the file as stored.
  • What a person may use (enabledSkillsFor): the library minus what the workspace turned off (chat_settings.disabled_skills), then the approved skills that are on and are theirs or shared. The / menu reads the same list (skillMenuFor).
  • On or off is workspace-wide (Wave 3 decision 10): an Admin or the creator for a workspace skill, an Admin for a library skill. The creator or an Admin deletes one.
  • Sharing: an Admin shares a skill with the workspace; a Manager asks the Admins through chat_access_requests.

Engine (thin hook)

  • A workspace skill's body is read from its row only when a run loads it, and only while it matches its checksum. A skill changed since it was saved is switched off instead.
  • "Used skill" now says where the skill comes from: Ansvisor library, You, Your workspace, or the repo owner.
  • An imported skill taints the run (CHAT-20 §16a): a change shows its card even in Full auto, and the skill's block is marked as written outside the workspace.

Agents

  • An agent whose skill is off, rejected, gone or changed is refused on Activate, Resume and Run now (skill_off), and a triggered run pauses with skill_off.
  • Workspace skills need no connection.

Registry: save_skill (CHAT-20 §9 row 75): AskFA, Admins and Managers. Its card shows the instructions in full. Undo (30 days) deletes the skill while it is still private and unchanged. It goes through the saveChatSkill server action, which checks the workspace isn't read-only first.

API

  • GET /api/chat/skills: the library with what is off, the workspace skills the person sees, the plan's limit, and what the person may do with each. Every role reads; bodies stay on the server.
  • PATCH and DELETE /api/chat/skills/{id}: an Admin or the creator.
  • POST /api/chat/skills/{id}/share: an Admin shares (and answers the requests to share it); the creator asks the Admins.
  • PATCH /api/chat/skills/library/{name}: Admins.
  • Every write checks the plan first (writeRefusal).

Fixes on top of W2-09 and W3-01 (one commit each, with tests)

  • A scheduled job's skill is checked as a run reads it (enabledSkillsFor and its checksum), not against the library alone. A job on a workspace skill no longer turns off as skill_off, and a library skill the workspace turned off no longer runs.
  • The seven shortcut names are reserved: a workspace skill can't be saved under one, and an older row with one stays out of the / menu and the run.
  • The plan's limit, and the list's "used", count only skills that aren't rejected.
  • The dashboard layout test stubs skillMenuFor, so it no longer fails to load ("supabaseUrl is required").

Help: help/chat/skills.md gets "Save your own skill" (still approved: false).

Notes

  • No automatic sharing on approval (the CHAT-20 rule is kept). Approving the save card keeps the skill private. Approving a Manager's share request in Settings › Team › Roles & access › Requests doesn't share it either; only an Admin's share does.
  • Shortcut names (start, analyze, new-report, create, optimize, opportunities, audit) reserved for workspace skills: fixed in this PR.
  • The plan's limit counts only skills that aren't rejected: fixed in this PR.
  • A scheduled job's skill checked against the workspace's skills too, not the library alone: fixed in this PR.

Related issue

None. Chat waves plan, W3-06 (CHAT-13).

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

Unit tests:

  • skills/store.test.ts: saving (roles, read-only, the SKILL.md check and scan, library and shortcut names, the plan's limits without rejected skills), what a person may use and the / menu, bodies only while the checksum matches, on/off and delete, sharing and requests, the list, an agent's skills, a step's source.
  • api/chat/skills/route.test.ts: off with the new Chat off, no bodies in the list, the plan checked first on every write, refusals as values, who may turn on, delete and share, library skills an Admin's.
  • engine.test.ts: the person's own skill loads with "You", a teammate's shared skill with "Your workspace" and never their private one, an import taints the run, a changed skill is switched off, a library skill turned off is left out.
  • schedule/worker.test.ts: a job on the creator's own skill runs; one on a library skill the workspace turned off, or on a changed skill, turns off (skill_off).
  • app/[locale]/(dashboard)/layout.test.tsx loads again.
  • agents.test.ts, agent-driver.test.ts (skill_off), registry/tools.test.ts, write.test.ts (save_skill by role and mode), skills/library.test.ts, _chat-page-access.test.ts.
  • Chat's web tests (src/lib/chat, src/app/api/chat, src/lib/help, the coverage test and the dashboard layout test), with W2-09, W2-03 and W3-01 merged in: 111 files / 1,650 tests passed in the branch's worktree, except one (engine.test.ts "keeps the setup rows…"), which timed out on a busy machine and passes alone in 3.4 s.

Manually (CHAT_AGENTIC=true, as the demo Admin, at /dashboard/chat):

  1. Ask "Save this as a skill called /pricing-brief: open with the answer, then three short sections." Chat shows the save_skill card with the name, what it does and the instructions in full. Approve it.
  2. In a new chat, type /: Skills lists /pricing-brief. Send "/pricing-brief our Pro plan": the first step is "Used skill" with You under it.
  3. Ask Chat to save a skill called /report, then one called /analyze: both are refused, as an Ansvisor skill and a shortcut have those names.
  4. GET /api/chat/skills lists the library and /pricing-brief, with the plan's limit. PATCH /api/chat/skills/library/report with { "enabled": false }: /report leaves the / menu. Turn it back on.
  5. In the Activity log, Undo the save: the skill is deleted while it is still private and unchanged.

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 (e90a0a2), run on the fork (https://github.com/cihan-gyk/ansvisor/actions/runs/41440881743): 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 12 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]>
…-09)

Each / menu shortcut (CHAT-08) now has a hidden playbook in skills/
(metadata.hidden: "true"): its allowed reads, default scope, output,
question rules and next moves, checked by the same format, scan and CI as
the library skills. The menu gains /start, /optimize, /opportunities and
/audit; "Create a report on" moves to /new-report, as /report is the
Weekly report skill's.

The composer sends shortcutId with the message while its trigger words
stay (rewritten, it drops), and keeps it on Retry. Home chips whose job
is a shortcut's send its id; chips for the focus areas' skills ask with
the skill's /name. The server takes the id only for a message that is
still that shortcut's, writes chat_messages.shortcut_id, loads the
playbook first as one of the run's three skill bodies, offers only the
reads in its data plan, and tells the model in one line what the plan
leaves out.

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

Before work in several like parts (drafts, pages, tasks), the model calls
the engine's plan_parts with how many; the server counts how many the AI
limit left covers (fitChatUnits and POST /api/internal/chat/budget/fit,
each part one write step's worst case, an agent run's own cap included):
a count, never dollars.

All covered, the work goes on. None, the run pauses at the AI limit as
before. Some, a question card asks "Your remaining AI limit covers about
{k} of the {n} ...": Do the first {k} now (Recommended), Get more AI
credits (pauses at the limit) or Cancel (stops). With no one there (an
agent's or a scheduled run), the first {k} are done and filed and the run
stops at the AI limit as it ends. A skill's run is checked as it starts,
before anything is read. Nothing is checked on the workspace's own key.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…duled jobs

Move the RRULE subset (DAILY/WEEKLY/MONTHLY on a zone's clock) from
agent-schedule.ts to schedule/rule.ts, which agent-schedule.ts re-exports,
and add nextJobRun for a chat_schedules row. Extract openHeadlessRun
(headless-run.ts) from openAgentRun: the chat, the message, the answer,
the Chat run and its state, written in the same order; agent runs use it
unchanged. No behaviour change: the agent tests pass as they are.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Chat's scheduled jobs (CHAT-12) now run by themselves, behind
CHAT_AGENTIC. The server's minute cron calls the new
/api/internal/chat/schedules/tick (CHAT_INTERNAL_SECRET) right after the
agents' tick; it settles open runs, resumes jobs the AI limit paused and
runs the due ones (schedule/worker.ts):

- A due job is claimed by moving next_run_at (or retry_at) from what was
  read, so two workers run a slot once; chat_schedule_runs' unique slot
  and attempt is the backstop. A recurring slot over an hour late is
  skipped (no catch-up); a busy workspace waits for the next minute.
- Each run re-checks its creator: Admin or Manager and the brand still
  theirs (else owner_role), brand there (missing_source), skill on
  (skill_off), AI limit (paused_cap, the job pauses at cap). A read-only
  workspace skips the slot; with Chat off nothing is touched.
- The run opens in the job's own chat as its creator (openHeadlessRun),
  Queued then claimRunSlot, always Auto · Normal, with no session, so
  every change waits as a card.
- A failed run retries once 10 minutes on; 3 failures in a row turn the
  job off. After the reset a cap-paused job goes on at its next slot.

Engine (thin hooks): a job's own run is metered as feature 'schedule'
with its scheduleId (budget.ts meteredAs; the server passes
p_schedule_id), and a question in it takes the Recommended option and
lists it at the top of the answer (schedule/unattended.ts).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…API (W3-06)

A workspace's own skills (CHAT-13, chat_skills from 00458), behind
CHAT_AGENTIC, with no new screen:

- skills/store.ts: saving a skill made in Chat (Admins and Managers),
  checked as its SKILL.md with checkSkillFile, never with an Ansvisor
  skill's name, under the plan's My skills limit (trial 3, Starter 10,
  Growth 50, Enterprise 200; none self-hosted). Approved, on and private
  to its creator at once. Its checksum is of the file as stored.
- What a person may use (enabledSkillsFor): the library but for what the
  workspace turned off (chat_settings.disabled_skills), then the approved
  skills that are on and theirs or shared. The / menu reads the same
  (skillMenuFor), so a saved skill shows there at once.
- Engine, a thin hook: a workspace skill's body is read from its row when
  a run loads it, only while it matches its checksum (else it is switched
  off). "Used skill" says where it comes from (Ansvisor library, You, Your
  workspace, the repo owner). An imported skill taints the run (CHAT-20
  §16a): a change shows its card even in Full auto, and its block is
  marked as written outside the workspace.
- Agents: an agent whose skill is off, rejected, gone or changed is
  refused on Activate/Resume/Run now, and a triggered one pauses with
  skill_off. Workspace skills need no connection.
- Registry: save_skill (CHAT-20 §9 row 75: AskFA, Admins and Managers,
  Undo 30 days by deleting it while private and unchanged), through the
  saveChatSkill server action (assertWritable first).
- API: GET /api/chat/skills; PATCH and DELETE /api/chat/skills/{id} (an
  Admin or the creator); POST /api/chat/skills/{id}/share (an Admin
  shares and answers the requests; the creator asks the Admins through
  chat_access_requests); PATCH /api/chat/skills/library/{name} (Admins).
  Every write checks the plan first (writeRefusal).
- Help: skills.md gets "Save your own skill" (still unapproved).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The worker checked a job's /skill against the library alone, so a job
on a workspace skill was turned off as skill_off, and a library skill
the workspace turned off still ran. It now reads the creator's skills
as a run does (enabledSkillsFor) and checks the skill's checksum.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A workspace skill could be saved as /analyze or another shortcut's
command (W2-09), which would then mean two things. Saving refuses the
seven shortcut names as it refuses the library's, and a skill that
already has one stays out of the / menu and the run.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
My skills' plan limit (Trial 3 / Starter 10 / Growth 50 / Enterprise
200) counted every skill row, so an import an Admin rejected kept
taking a place. The count, and the list's "used", now leave rejected
skills out.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The layout now reads the / menu's skills from skills/store.ts, which
opens the admin client, so the test file failed to load ("supabaseUrl
is required"). The test stubs skillMenuFor as it stubs the other Chat
reads.

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

…heck hits

The fit check (fitChatUnits) took the smaller of what is left of the
month's AI limit and what an agent run's own cap leaves, but only returned
the count. The engine then paused every new cap path (a skill's run that
can't start, a job none of whose parts fit, Get more AI credits, and the
parts done with no one there) at the month's limit. For a triggered run
stopped by its own cap, the agent driver read capHit 'month' and paused
the whole agent until the month resets instead of ending the run as
"Stopped at its limit".

The server now returns which limit binds ('run' when the run's cap leaves
no more than the month, as reserveChatStep's run_cap), the route passes it
on, and the web client reads it (the month's when absent). The engine
pauses with that limit, keeping it on the parts card and on limitStop, as
modelStep already does for run_cap refusals.
# Conflicts:
#	help/chat/chat.md
#	server/src/lib/chat-budget.test.js
#	server/src/routes/internal-chat-budget.js
#	server/src/routes/internal-chat-budget.test.js
#	web/messages/en.json
#	web/src/lib/chat/engine.test.ts
#	web/src/lib/chat/engine.ts
# Conflicts:
#	server/src/lib/brand-access-policies.test.js
#	server/src/lib/chat-budget.js
#	supabase/schema.sql
#	web/src/lib/chat/agent-driver.ts
#	web/src/lib/chat/schema.test.ts
#	web/src/types/supabase.ts
# Conflicts:
#	web/messages/en.json
#	web/src/lib/chat/agent-driver.ts
#	web/src/lib/chat/agents.ts
@github-actions github-actions Bot removed 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
# Conflicts:
#	help/chat/chat.md
#	web/messages/en.json
#	web/src/lib/chat/engine.ts
#	web/src/lib/chat/schedule/worker.test.ts
#	web/src/lib/chat/schedule/worker.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 9cc1acc into ansvisor:main Nov 3, 2026
5 checks passed
@gkhngyk gkhngyk mentioned this pull request Nov 3, 2026
3 tasks done
gkhngyk added a commit that referenced this pull request Nov 3, 2026
The squash merge of #2005 (0b47e04) carried an old tree of the PR that
predated the main it was merged into, so it reverted about 17,000 lines
that #1923–#2004 had added: migrations 00470, 00472 and 00473, the
report card, brand integrations, take-down, the model menu and more.
#2008 and #2011 then merged on top.

This commit carries the tree of 9cc1acc (main before #2005, what
production runs) with #2005, #2008 and #2011 merged onto it again
against the versions they were built on. Compared with 9cc1acc it
deletes nothing, and every changed file is one of those three PRs' own
or a generated snapshot. Web tsc and the chat/help/mcp suites (2,844)
and the server suite (5,289) pass.
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