Skip to content

feat(chat): add session-free writers for drafts, checked as the run's person - #1959

Merged
gkhngyk merged 3 commits into
ansvisor:mainfrom
cihan-gyk:feat/chat-session-free-writers
Nov 1, 2026
Merged

gkhngyk merged 3 commits into
ansvisor:mainfrom
cihan-gyk:feat/chat-session-free-writers

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

Chat can now create and change content drafts for a person without their browser session, for the triggered agent runs that have none (the minute tick and signals). Step 2 of the owner's two-step fix for session-free agent writes (2026-11-01), web side: the writers and their checks. The engine doesn't call them yet (next PR), so nothing changes in behavior. No migration, no Help change.

What changes

  • One write core (web/src/lib/content/draft-writes.ts): insertDraft, saveDraft and changeDraft, moved unchanged from content-drafts.ts. The caller passes the client. The three session server actions now do their own checks, then call the core.
  • Checks for a person, without a session (web/src/lib/chat/actor/access.ts):
    • actorOf(ctx) takes { userId, organizationId } from the engine's run context. It never reads them from the model's tool input.
    • canAccessBrandFor: the brand is in the run's organization and can_access_brand(brand, person) (00388) allows it.
    • isManagerFor: the person's current role in that organization, read live.
    • assertWritableFor: refuses the demo brand, and an inactive plan on cloud. These are the rules and error classes of subscription-guard.ts. A brand it can't find is refused too.
  • Session-free writers (web/src/lib/chat/actor/drafts.ts): createContentDraftFor and updateContentDraftFor. The order is access, role, writable, then the core with the service role.
    • updateContentDraftFor reads the draft's brand from the database, not from the input.
    • Sources (opportunity, prompt, topic) are always filtered by brand.
  • Registry: rows gain sessionFree?: true and runFor?, on create_draft and update_draft only. defineTool requires both together, on WRITE rows only.
  • Ownership: a draft written for a person's own agent is theirs: author: 'user', created_by = the person, as startDraftFromAction already does. Library's "Mine" and "Who" follow it. 'agent' would send the draft to the review queue and drop its owner.

Safety

  • The new modules are not 'use server', so the browser can't call them. Server action files only import them; they don't re-export them.
  • The service role skips the demo trigger (00381) and RLS. So the demo refusal is done in code on every install, and every source row is filtered by brand. Another brand's opportunity, prompt or topic gives not_found.
  • Access is checked before the demo and plan checks, so another brand's demo or plan state doesn't leak.
  • The session-free path is stricter than the session one: content_drafts RLS is still organization-level, and this path also checks brand access.

Known, left as is

  • A History version written with the service role has created_by null, because content_draft_keep_version writes auth.uid(). Nothing reads it today.
  • When the engine starts calling runFor, session-free runs must still go through precedence: minRole, menu and Chat access, and approval policy. That is the next PR.

A pre-push review found no blocking issue. Its small fixes are in: refuse a brand it can't find, copy the refusal objects, and tests for an archived draft, the agent's writing lock and another brand's prompt.

Related issue

None. Follow-up to the agent runs review (owner decision 2026-11-01, "İkisi de, sırayla").

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

Nothing is visible yet: the engine wires runFor in the next PR. Unit tests:

  • chat/actor/access.test.ts, each rule allowed and refused:
    • another organization;
    • a person who left it;
    • the demo brand, on cloud and off;
    • read-only;
    • a read error;
    • a brand it can't find.
  • chat/actor/drafts.test.ts:
    • the two paths write the same draft row and the same History version;
    • nothing is written for another organization, an unshared brand, another brand's opportunity or prompt, an analyst, the demo brand, read-only, an archived draft, or the agent's writing lock.
  • chat/registry/tools.test.ts: only these two rows are sessionFree, and runFor takes the person from the context.
  • The session actions' tests (content-drafts*, chat-content, write, undo, engine) pass unchanged.

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 (0f8b1a1), run on the fork (https://github.com/cihan-gyk/ansvisor/actions/runs/41334838132): 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 1, 2026 20:29
… person

A run with no session behind it (a schedule's or a signal's run) can't
call the server actions Chat's WRITE rows use. This adds the first two
writers that run as the run's person instead, with the service role,
after the checks the person's session would get (the MCP pattern):

- lib/content/draft-writes.ts: the core of createContentDraft,
  saveContentDraft and updateContentDraft (insertDraft, saveDraft,
  changeDraft, promptsFor, keepVersion), taking the client it writes
  with. Moved out of the 'use server' module unchanged, so the session
  actions and the run's writers share one write and none of it is
  callable from a browser.
- lib/chat/actor/access.ts: canAccessBrandFor (can_access_brand, 00388,
  asked about the person, in the run's workspace), isManagerFor (live
  role in that organization), assertWritableFor (demo brand on any
  install, read-only plan on the cloud, as assertWritable).
- lib/chat/actor/drafts.ts: createContentDraftFor and
  updateContentDraftFor. The draft is the person's own: created_by is
  them and author stays 'user', not the Content Agent's 'agent' draft.
- Registry rows get sessionFree + runFor (create_draft, update_draft);
  defineTool keeps them on writes only and together. Nothing calls
  runFor yet, so behavior doesn't change; wiring the engine is the next
  PR, after the sessionless-writes-ask change merges.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…opy draft refusals

Review follow-ups for the session-free draft writers:

- assertWritableFor throws 'not_found' for a brand that isn't the run's
  workspace's, before its demo flag: another workspace's demo brand says
  nothing about itself.
- draftChangeRefused returns a new refusal each time instead of handing
  out shared objects a caller could change.
- Tests on the run's path: an archived draft is not found, a page the
  Content Agent is writing comes back as a conflict, and a prompt or
  topic of another brand is refused as a draft's source.

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

@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 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 3f05873 into ansvisor:main Nov 1, 2026
6 checks passed
gkhngyk pushed a commit that referenced this pull request Nov 2, 2026
…ts person (#1972)

* feat(chat): let a run with no session make a session-free change as its person

An agent run that the minute tick or a signal started (or that another
person's Run now picked up) has no session of its person behind it, so
#1946 turned every change it would make by itself into a card. The draft
rows already have their own writers for such a run (#1959: create_draft
and update_draft runFor, checked as the run's person with the service
role); this wires the engine to them.

- engine.ts: askWithoutSession leaves a sessionFree row's decision as the
  precedence made it, read for the run's person like any call's (role,
  excludeRoles, plan, policy, mode, demo brand, brand scope): Full auto,
  an Admin's Always or the person's tick run it; Ask me first still shows
  the card. Every other row still asks first, with the no-session note.
- write.ts: runLoggedWrite takes withoutSession and runs the row's runFor
  instead of run, with the same idempotency key, Activity log row and
  Undo window. A row without runFor refuses before anything runs.
- The person is always the run's (the tool context the engine builds from
  the run's row), never the model's input. A card's Approve still runs in
  its person's own request, with their session.

Tests: Full auto makes the draft with no card through createContentDraftFor,
logged as the run's person's with Undo (and undone); Ask me first still
asks and the owner's Approve uses their session; a row without runFor
still asks; a Manager made an Analyst mid-run is refused update_draft
(forbidden_role) and an Analyst's run never gets it; the owner's own Run
now still uses their session; runLoggedWrite runs runFor for both draft
rows, once per approval, and refuses a row without it.

Help article updated (help/chat/agents.md: "When a run asks before a change").

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

* fix(chat): decide an agent run's change in the mode it has now

Review follow-ups (D-3, D-6) for the session-free engine:

- An agent run's change is decided in the mode the run has now, read
  fresh at each change (agentModeNow): the mode it started in, or the
  agent's own mode when it was set lower since, and Ask me first once an
  Admin turned Full auto off for agents (chat_settings.agents_full_auto)
  under a run that started in it. What can't be read asks first. So a
  session-free row no longer runs by itself after the owner set the agent
  to Ask me first, or the Admin turned Full auto off, mid-run. A person's
  own chat keeps its mode; reads are not affected.
- Engine tests for the session-free path with an Admin's Never (not
  offered, nothing made) and Ask (its card in Full auto), "Full auto for
  members" off (an Analyst's Full auto asks, an Admin's runs), and the
  agent run's mode now: still Full auto runs, Full auto off for agents or
  the agent set to Ask me first mid-run asks, an unreadable agent asks.

Help article updated (help/chat/agents.md: "When a run asks before a change").

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

---------

Co-authored-by: Cihan <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
gkhngyk added a commit that referenced this pull request Nov 2, 2026
…session is behind it (#1973)

* feat(chat): let a run with no session make a session-free change as its person

An agent run that the minute tick or a signal started (or that another
person's Run now picked up) has no session of its person behind it, so
#1946 turned every change it would make by itself into a card. The draft
rows already have their own writers for such a run (#1959: create_draft
and update_draft runFor, checked as the run's person with the service
role); this wires the engine to them.

- engine.ts: askWithoutSession leaves a sessionFree row's decision as the
  precedence made it, read for the run's person like any call's (role,
  excludeRoles, plan, policy, mode, demo brand, brand scope): Full auto,
  an Admin's Always or the person's tick run it; Ask me first still shows
  the card. Every other row still asks first, with the no-session note.
- write.ts: runLoggedWrite takes withoutSession and runs the row's runFor
  instead of run, with the same idempotency key, Activity log row and
  Undo window. A row without runFor refuses before anything runs.
- The person is always the run's (the tool context the engine builds from
  the run's row), never the model's input. A card's Approve still runs in
  its person's own request, with their session.

Tests: Full auto makes the draft with no card through createContentDraftFor,
logged as the run's person's with Undo (and undone); Ask me first still
asks and the owner's Approve uses their session; a row without runFor
still asks; a Manager made an Analyst mid-run is refused update_draft
(forbidden_role) and an Analyst's run never gets it; the owner's own Run
now still uses their session; runLoggedWrite runs runFor for both draft
rows, once per approval, and refuses a row without it.

Help article updated (help/chat/agents.md: "When a run asks before a change").

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

* feat(chat): write through the API server as the run's person when no session is behind it

A run an agent's trigger starts has no session of its person, so its
writes through the API server (an opportunity's status, a dismissed prompt
suggestion, Send to Action Center and its duplicate check, Site Audit)
asked first on a card (#1946). The server takes such writes as a member
since #1949 (CHAT_ACTING_SECRET + X-Acting-User, on seven routes); this is
the web's side.

- lib/chat/actor/api.ts: apiAs sends a request as the run's person: their
  own session's token in their own request, else the acting secret with
  X-Acting-User naming them, never another person's session. The person
  is the run's (the tool context the engine builds from the run's row),
  never the model's input. With CHAT_ACTING_SECRET unset or shorter than
  32 characters the way is closed (actingOpen) and nothing is sent. A 401
  or 403 chat_off on the acting way comes back as a value in the person's
  words (acting_refused, chat_off: chatEngine.acting in en.json), which
  the engine says on the step and to the model.
- lib/chat/actor/api-writes.ts and action-center.ts:
  updateOpportunityStatusFor, dismissSuggestionFor, startSiteAuditsFor,
  createChatActionFor and checkChatActionCoverFor, with what the
  session's action checks first done as the person (isManagerFor,
  assertWritableFor, the chat being theirs for History's place).
- Their requests and the reading of the answers move out of the 'use
  server' modules, unchanged, so both ways share them:
  lib/action-center/chat-send.ts (Send to Action Center) and
  lib/audit/start-answer.ts (Site Audit's start).
- Registry: update_opportunity, dismiss_prompt_suggestion, create_action
  and run_site_audit are sessionFree with runFor and runForOpen: actingOpen;
  create_action has precheckFor, so its duplicate is checked as the run's
  person before any card. defineTool keeps runForOpen and precheckFor to
  session-free rows; runsWithoutSession says whether a row runs now.
- Engine: askWithoutSession and runLoggedWrite use runsWithoutSession, so
  with the way closed these rows ask first as before; precheckOf uses
  precheckFor in a run with no session. Every change still goes through
  the precedence as the run's person (role, excludeRoles, plan, policy,
  mode); run_site_audit keeps its card in every mode, so its runFor isn't
  reached yet.
- build_report stays as it is: its report reads fourteen sections with
  the person's session before the summary, so it still asks first.
- web/.env.example and the self-host env docs say what the web sends and
  that, unset, those changes ask first.

Help article updated (help/chat/agents.md: "When a run asks before a change").

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

* fix(chat): decide an agent run's change in the mode it has now

Review follow-ups (D-3, D-6) for the session-free engine:

- An agent run's change is decided in the mode the run has now, read
  fresh at each change (agentModeNow): the mode it started in, or the
  agent's own mode when it was set lower since, and Ask me first once an
  Admin turned Full auto off for agents (chat_settings.agents_full_auto)
  under a run that started in it. What can't be read asks first. So a
  session-free row no longer runs by itself after the owner set the agent
  to Ask me first, or the Admin turned Full auto off, mid-run. A person's
  own chat keeps its mode; reads are not affected.
- Engine tests for the session-free path with an Admin's Never (not
  offered, nothing made) and Ask (its card in Full auto), "Full auto for
  members" off (an Analyst's Full auto asks, an Admin's runs), and the
  agent run's mode now: still Full auto runs, Full auto off for agents or
  the agent set to Ask me first mid-run asks, an unreadable agent asks.

Help article updated (help/chat/agents.md: "When a run asks before a change").

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

* fix(chat): act as the run's person with the secret only, and ask when the server takes no one

Review follow-ups (O-1, D-2, D-4, D-5, D-6) for the writes through the
API server for a run with no session:

- apiAs always sends CHAT_ACTING_SECRET with X-Acting-User naming the
  run's person, and never reads a session or a cookie: the engine calls
  runFor only when no session of the person is behind the run. A 401 is
  said once as a warning (a setup to fix), never with the secret.
- precheckOf: in a run with no session only the row's precheckFor
  checks, and only while its way is open; with the way closed nothing is
  checked before the card (a request's session there isn't the person's),
  and the change checks again on approval. An acting refusal in Send to
  Action Center's duplicate check is no longer logged as an error.
- When the server takes no one to act as (acting_refused: the web's and
  the server's secrets differ), the change is no longer a failed step: it
  waits on its card with the no-session note, its step no longer marked
  as made by itself, and the owner's Approve makes it with their session
  on the same approval (runLoggedWrite gives the idempotency key back for
  such a refusal). chat_off still ends the step with its words.
- run_site_audit leaves the session-free rows: its card in every mode
  means its runFor is never reached. startSiteAuditsFor, the shared
  lib/audit/start-answer.ts and the audits.ts change are gone; audits.ts
  and audit-tools.ts are as on main.
- web/.env.example and the self-host env docs say which writes use the
  secret and what a value that differs from the server's does.

Tests: apiAs never reads a session and always names the run's person; the
401 warning; acting_refused turns into the card and the owner's Approve
makes it once, logged as approved; Send to Action Center in the owner's
own request uses precheck and createChatAction, never the run's-person
ones; with the way closed nothing is checked before the card.

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

---------

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