Skip to content

feat(impact-ledger): credit a page a person approved in an agent run to that run (00480) - #2137

Merged
gkhngyk merged 6 commits into
ansvisor:mainfrom
cihan-gyk:feature/agent-run-ship-credit
Nov 5, 2026
Merged

gkhngyk merged 6 commits into
ansvisor:mainfrom
cihan-gyk:feature/agent-run-ship-credit

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

AGENTS-plan §2 rule 1 and §7 step 2 say every agent closes its loop with a measured result, and its page shows "what it did and what it moved". This PR lays the groundwork for the per-agent "What this agent did" card. The owner has approved the card's mockup, and the card is being built in a separate PR on top of this one.

The gap on main

  • The impact ledger (W2-20) credits a shipped change to an agent only when the agent published it on its own. That path needs AUTONOMOUS-GO-LIVE (content_draft_deliveries.agent_run_id), CHAT_AUTO_GO_LIVE, the Admins' switch and Full auto.
  • In every other mode, a page from an agent run goes live when a person clicks its card. The CMS confirmation then wrote a ship event with no agent, no agent run and no Chat run.
  • So neither the ledger nor the run's Outcome in Run history counted the page, and the run kept showing "—".
  • The spec says otherwise:
    • CHAT-22 §2 item 2: a shipped change links to its run and agent, and to who shipped it.
    • CHAT-21 §I item 44: "a ship event exists for the run's output".
    • The #f-impact mockup has a row that reads "agent run, you approved".
  • The owner chose option A1: a page a person sends or publishes from an agent run's own card counts for that agent, at the cost of a small migration.

What changes

  • Migration 00480 adds content_draft_deliveries.via_agent_run_id:
    • a foreign key to chat_agent_runs, on delete set null, with a partial index;
    • a check that a send names at most one run.
    • agent_run_id keeps its meaning, "sent on its own", so the Library's "Sent by {agent} · on its own" is unchanged.
    • Members still only read sends (00262, 00390). Only the server writes the column.
  • Chat
    • push_cms_draft, publish_cms and trigger_content_webhook pass the chat's agent run (ctx.chat.agentRunId) to sendDraftToCms, publishDraftToCms and sendDraftToWebhook. Those send it to the API as viaAgentRunId.
    • A tool's own run here is always a person's click, because an agent that goes live on its own goes through the engine's internal route.
    • A person's own chat, the Content Editor and the Library send no run. A later publish from any of them stays the person's.
  • API
    • POST /api/content-drafts/send and POST /api/content-drafts/publish-cms take an optional viaAgentRunId, which must be a guid.
    • sendContentPages stores it on the deliveries before they go, but only after checking that it is a run of the same workspace and brand. Any member's session can call the server action, so the id is never trusted as given.
    • It is never stored next to an agent's own run. A run that doesn't check out is logged, and the page still goes.
  • Ledger (ship.js, agentOrigin)
    • It now reads via_agent_run_id too. The ship event then names the agent, the agent run, and the run's Chat run (the newest one in its chat, the same rule as the web's chatRunOf).
    • shipped_by stays the person and execution_mode stays assisted.
    • A run from another workspace names none.
    • An agent's own send works as before.
  • Run outcome
    • A ship event written after its agent run ended now marks that run Pending at once. Two cases hit this: a CMS draft published in the CMS later, and a confirmation filled in by the nightly job. Before, such a run showed "—" until day 7. An outcome already read is never overwritten.
    • A run still going says Pending when it ends, as before (markShipped).
    • The nightly syncAgentRuns then gives the verdict through the same agent_run_id. A job test covers this end to end.
  • Side fix (Reports › What Ansvisor did for you): a change closed with reason taken_down read "It can't be measured." It now reads "The page was taken down from your site."
  • Help: help/chat/agents.md gets a new section, "What a run's outcome means". It says which pages count for a run, and that a page published later from the Content Editor or the Library counts as yours. approved-articles.json is regenerated.

Open questions

  1. Should the run check be stricter? The API accepts any run of the same workspace and brand. CHAT-21 §H item 40 says only the run's acting person or an Admin may approve its cards, and the API could require that too, at the cost of a role read. The risk today is limited to which agent a page is credited to.
  2. Follow-up checks: a page that goes live after its run ended now gets Pending at once, but markShipped's follow-up checks (openAgentFollowUps) still don't open for it. That was already true on main for an agent's own late publishes, and this PR leaves it alone.

Related issue

None. AGENTS-plan §7 step 2 (W2-20 per-agent impact); the owner chose option A1 on 2026-11-05.

Type of change

  • feat — New feature
  • fix — Bug fix (the taken_down wording only)
  • 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

  1. Apply migration 00480 (needs-migration) before deploying the server. The server reads the new column, so until 00480 is applied, ship events for CMS confirmations fail and are logged. Once it is applied, the nightly job fills in any missed ones from the last 30 days.

  2. In an agent's run that waits on a Send to CMS or Publish card, approve the card. The send's row has via_agent_run_id set to the run, and agent_run_id stays null.

  3. Once the CMS confirms the page is live, check the ship event:

    • agent_id, agent_run_id and run_id are the agent's;
    • execution_mode = 'assisted';
    • shipped_by is you.

    The run's Outcome in Agent Center › the agent › Run history shows Pending, then the day-14 verdict.

  4. Send the same kind of page from the Content Editor, the Library or your own chat. No run is named.

  5. Tests:

    • server: cd server && npx vitest run src/lib/impact-ledger src/lib/content-delivery.test.js src/lib/cms-publish.test.js src/routes/content-drafts-send.test.js src/routes/content-drafts-publish-cms.test.js
    • web: cd web && npx vitest run src/lib/chat/schema.test.ts src/lib/chat/registry/tools.test.ts src/lib/chat/write.test.ts src/lib/chat/engine.test.ts src/lib/actions/chat-content.test.ts src/components/reports/impact-report.test.tsx src/lib/help

    engine.test.ts covers the click end to end: when the owner approves an agent run's held go-live card, the send names that run; when an Admin clicks in their own chat, it names none.

There are no screenshots, because the only on-screen changes are the Help article and the one Reports reason line.

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/)
  • yarn typecheck passes (run from web/)
  • yarn format:check passes (run from web/)
  • Changes are focused — one concern per PR (plus the two-line taken_down wording fix found on the way)
  • Help article updated under help/ (or no user-visible change) — see help/README.md

Checks run locally on 4389fc9. The branch still merges cleanly into current main.

  • schema: build-schema.sh leaves schema.sql unchanged.
  • web: format:check, lint and typecheck pass. Lint has 0 errors; its 7 warnings were already on main, in files this PR doesn't touch. All tests pass (987 files, 10,590 passed, 1 skipped), and yarn build passes with CI's placeholder env.
  • server: lint and format:check pass. All tests pass (419 files, 5,781 passed, 4 skipped).

Merge note: open PR #1894 also edits assertSender and the /send route in server/src/routes/content-drafts.js, next to this PR's lines. Whichever merges second gets a small conflict there.


Generated by Claude Code

…0480)

A page a person sends or publishes from a card in an agent run's chat
("agent run, you approved", CHAT-22 §2 item 2, mockup #f-impact) named no
run, so the impact ledger never counted it for the agent.

Migration 00480 adds content_draft_deliveries.via_agent_run_id (FK to
chat_agent_runs, on delete set null, partial index), with a check that a
send names at most one run: agent_run_id keeps meaning "sent on its own"
(the Library's wording is unchanged). POST /api/content-drafts/send and
/publish-cms take an optional viaAgentRunId (a guid); sendContentPages
keeps it on the deliveries before they go, only once it is the
workspace's run on that brand (the click comes through a server action
any member's session can call), never beside an agent's own run. A run
that doesn't check out is logged and the page still goes.

schema.sql and the web's Supabase types follow; the schema test reads
the migration.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
…to that run

agentOrigin (ship.js) now reads via_agent_run_id (00480) as well as an
agent's own send: the ship event names the agent, the agent run and the
run's Chat run (its chat's newest, as the web's chatRunOf reads it),
while shipped_by stays the person and execution_mode stays assisted. A
run of another workspace names none. An agent's own send is unchanged.

A ship event written after its agent run ended (a CMS draft published
there later, a confirmation filled in by the nightly job) now marks that
run Pending at once, never over an outcome already read, so it doesn't
show "—" until day 7; a run still going says Pending as it ends
(markShipped). syncAgentRuns then gives it its verdict through the same
agent_run_id, which a job test now covers end to end.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
…its chat

push_cms_draft, publish_cms and trigger_content_webhook pass the run the
chat is (ctx.chat.agentRunId) to sendDraftToCms, publishDraftToCms and
sendDraftToWebhook, which send it to the API as viaAgentRunId. A row's
run is always the person's click here (an agent going live on its own
goes through the engine's own route), so this is "agent run, you
approved"; a person's own chat, the Content Editor and the Library send
none.

Help: Agent Center's article says what a run's outcome counts, and that
a page published later from the Content Editor or the Library is the
person's; approved-articles.json follows.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
closeTakenDown stores the reason taken_down, which Reports › What
Ansvisor did for you read as "It can't be measured.". It now says "The
page was taken down from your site.".

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
…in a person's chat

The engine test's Admin click in a person's own chat now expects no run
(null), and the owner's click on an agent run's held go-live card expects
that run: "agent run, you approved" (00480), end to end through the
engine.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
@github-actions github-actions Bot added the needs-migration Adds or edits a migration — apply it to the hosted database label Nov 5, 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.

@gkhngyk
gkhngyk merged commit 5a8de56 into ansvisor:main Nov 5, 2026
13 checks passed
cihan-gyk pushed a commit to cihan-gyk/ansvisor that referenced this pull request Nov 5, 2026
Brings in Onboarding & Help (ansvisor#2136, /setup as the 22nd skill), ansvisor#2134,
ansvisor#2135 and ansvisor#2137. Conflicts kept both: /setup then /fanout in SKILL_ORDER
and everywhere the library is listed (23 skills), Fan-out Gap Filler and
Onboarding & Help both ready (20 ready presets, 19 Soon). The tests that
used Fan-out Gap Filler as their Soon example now use Localization. Help:
skills.md gives Fan-out gaps a section of its own, so The Ansvisor skills
stays within 150 words.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
cihan-gyk pushed a commit to cihan-gyk/ansvisor that referenced this pull request Nov 5, 2026
…t-card

Brings in ansvisor#2134, ansvisor#2135, ansvisor#2136 (the Onboarding & Help agent) and ansvisor#2137
(impact-ledger ship credit, 00480).

Conflict in help/chat/agents.md: both sides added a section after "An
agent's page". Kept both, "What this agent did" (this branch) then "What
a run's outcome means" (ansvisor#2137), in the page's order; each stays under 150
words. impact-report.tsx merged cleanly: ansvisor#2137's taken_down reason in
REASONS beside this branch's move of the pills, outcome words and signed
numbers to impact-parts.ts. approved-articles.json regenerated.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
cihan-gyk pushed a commit to cihan-gyk/ansvisor that referenced this pull request Nov 5, 2026
…ites

A person's click on a card in an agent run's chat now ships with the
agent and its run (00480, ship.js agentOrigin), execution_mode assisted
and shipped_by the person; the agent's own send stays automated. The card
already reads automated as "on its own" and the rest by who sent it, so
nothing changes in the read: the tests now use that data. Each change in
the credit test names its run, a page sent later from the Content Editor
or the Library (no agent) isn't listed, and the agent's own change reads
"on its own" to every viewer. The card's fixtures ship "you approved" and
"by {name}" rows as assisted, and a page taken down shows ansvisor#2137's reason.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
cihan-gyk pushed a commit to cihan-gyk/ansvisor that referenced this pull request Nov 5, 2026
Main brought ansvisor#2137 (00480) and ansvisor#2143. schema.sql is rebuilt with
build-schema.sh (00480 then 00481). help/chat/agents.md's front matter
keeps both keyword and synonym lists (Agent work's and Onboarding's).
approved-articles.json regenerated.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
gkhngyk pushed a commit that referenced this pull request Nov 5, 2026
…n its page (#2148)

* refactor(reports): share the impact ledger's pills, outcome words and ship-row read

Reports' outcome pill colors, "Pending · early signal" label and signed
numbers move to components/reports/impact-parts.ts, and the ship_events
embed (action number, measurement) and measurementOf to lib/impact/report.ts,
so the agent page's "What this agent did" shows a change exactly as
Reports does. Reports reads and renders as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9

* feat(chat): read what an agent's runs took live in the last 30 days

GET /api/chat/agents/{id}/impact answers the agent's ship events of the
last 30 days (agent_id) with their measurements, read by Reports' own
impactRow and impactSummary (Improved counts Medium and High verdicts
only), on the brands the person may see as the ledger's policy (00424)
has it. Each change says how it went live (on its own, the reader
approved it, or a teammate) and its run's number among the agent's runs
the person sees, with the run's chat when they may open it. It also
counts the drafts the agent's runs of the last 30 days made that no ship
event names, found through the draft's chat: content_drafts.conversation_id
-> chat_conversations.agent_run_id -> chat_agent_runs.agent_id.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9

* feat(chat): show "What this agent did" on the agent page

Between Triggers and Run history, as the owner approved it: the last 30
days, with no period to pick. Changes shipped, Improved X of Y (Medium
and High verdicts only, "—" before any) and Pending; each change with
how it went live ("run #31, on its own", "you approved", "by Sam Lee"),
its outcome in Reports' words and its next checkpoint; how many drafts
aren't live yet; the method line and Open in Reports, as Reports has no
link to one change. Three states: with results, the first week, and
nothing live yet.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9

* docs(help): what an agent's page says it did in the last 30 days

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9

* test(chat): read "you approved" from the ship events #2137 writes

A person's click on a card in an agent run's chat now ships with the
agent and its run (00480, ship.js agentOrigin), execution_mode assisted
and shipped_by the person; the agent's own send stays automated. The card
already reads automated as "on its own" and the rest by who sent it, so
nothing changes in the read: the tests now use that data. Each change in
the credit test names its run, a page sent later from the Content Editor
or the Library (no agent) isn't listed, and the agent's own change reads
"on its own" to every viewer. The card's fixtures ship "you approved" and
"by {name}" rows as assisted, and a page taken down shows #2137's reason.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9

* docs(help): find "What this agent did" by its words, and say how a change went live

Agent Center's article gets keywords and synonyms for the agent page's
card (what this agent did, agent impact, what did my agent do), now that
#2136's Onboarding & Help keywords are on main, and its section says the
three ways a change went live: on its own, you approved on a card in the
run's chat (00480), or by a teammate. Sources name agent-impact.ts.
approved-articles.json follows.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9

---------

Co-authored-by: Claude <[email protected]>
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.

3 participants