Skip to content

feat(action-center): measure the position outcome of sent Improve actions - #1018

Merged
gkhngyk merged 7 commits into
ansvisor:mainfrom
cihan-gyk:feature/prompts-improve-outcome
Oct 15, 2026
Merged

gkhngyk merged 7 commits into
ansvisor:mainfrom
cihan-gyk:feature/prompts-improve-outcome

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

PROMPT-008. Sending Improve suggestions to Action Center (#1012) was half the loop. This PR closes it by showing whether the prompt's positions actually moved after the work.

Stacked PR: this builds on #1010 → #1011 → #1012, so the diff against main includes their commits until they merge. Only the last commit (feat(action-center): measure the position outcome…) belongs to this PR.

  • Baseline at send time: the prompt's last 7 days per platform the action covers (mention state, avg. mention position, citation rank).
    • It is computed with classifyPlatform, the same rule the Positions panel and Improve tab use.
    • It is stored once in actions.baseline.
    • A trigger (actions_keep_improve_baseline) rejects any later change to an Improve action's baseline.
  • Daily refresh: sweepImproveOutcomes runs in the daily job next to sweepActionValidation.
    • It re-reads the same 7-day window ending now, for every Improve action that is open or completed within the last 30 days.
    • It stores the comparison on the new actions.position_outcome.
    • Dismissed actions are not tracked.
  • Outcome card on the action's Overview: one row per platform, e.g. ChatGPT #3.4 → #2.0 ✓.
    • Summary line: "Position improved on 1 of 2 platforms".
    • A platform with no runs on either side shows "Not enough data" instead of a delta.
  • 30 days after completion the read (the 7 days ending on day 30) is final:
    • a position_outcome_recorded event is written to the action's History;
    • the History table's Result column shows "Position improved on X of Y platforms";
    • the History drawer shows the same card ("Day 30").
  • Positions panel: a prompt with a tracked Improve action shows an "Improving · AC-n" badge with the mean delta (#3.4 → #2.0); clicking it opens the action.

Verdict per platform (threshold confirmed with product: 0.5):

Before → after Verdict
not mentioned → mentioned improved
mentioned → not mentioned declined
mention position moved ≥ 0.5 improved / declined
smaller move no meaningful change
no runs on either side not enough data

Data

Code

  • Server:
    • new lib/action-center/improve-outcome.js: readPlatformStates, platformVerdict, compareOutcome, refreshImproveOutcome, sweepImproveOutcomes, with tests;
    • from-prompt-improve.js: the 7-day baseline at send and the first read;
    • server.js: the daily hook.
  • Web:
    • lib/improve-outcome.ts: parsing, labels and the panel's tracked-action pick, with tests;
    • improve-outcome-card.tsx;
    • drawer, History drawer and History table;
    • the Positions panel badge;
    • en.json strings.

Related issue

PROMPT-008 (internal product task)

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

  1. Apply migrations through 00124.
  2. Send Improve suggestions from a prompt's Positions panel (Improve tab → Send Actions). The new action's baseline holds the last 7 days per covered platform.
  3. Run the sweep once by hand (the cron is skipped in development):
    cd server && node --input-type=module -e "import 'dotenv/config'; const m = await import('./src/lib/action-center/improve-outcome.js'); console.log(await m.sweepImproveOutcomes()); process.exit(0)"
  4. Open the action → Overview. The Outcome card lists each platform as before → last 7 days, with "Not enough data" where a side had no runs.
    • The demo seed has no mention_position; set a few to see positions.
  5. Open the prompt's Positions panel: the header shows Improving · AC-n.
  6. To see the final read, set the action to completed with completed_at more than 30 days ago and run the sweep. Then:
    • History → Result shows "Position improved on X of Y platforms";
    • the History drawer shows the Day 30 card;
    • the action's History tab has the event.
  7. Check light and dark mode.

I ran steps 3–7 locally on the dev database and reverted the test data afterwards.

Checklist

Also run:

Check Result
yarn test 486/486
yarn build passed
server: npm run format && npm run lint && npm test 1081/1081
npm run version:check passed
bash supabase/build-schema.sh run

🤖 Generated with Claude Code

cihan-gyk and others added 4 commits October 13, 2026 13:39
The Positions panel was fixed to the table's range, so checking a prompt
over another period meant changing the whole table. The panel now has its
own 24h / 7d / 30d / 90d / Custom control. It opens on the table's range,
and changing it reloads only the panel. The open prompt and the panel's
range are mirrored to the URL (?positions=<id>&positionsRange=7d) so the
view can be shared. A custom range no longer spells its dates twice in
the header.

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

For one prompt and the panel's range, the Positions panel gets an Improve
tab: per AI platform where the brand is not mentioned, mentioned late, or
mentioned early but not cited, AI-generated suggestions grounded in that
platform's own evidence (fan-out sub-queries, cited domains, competitor
URLs, the answer text). The server checks every source against that
evidence and drops invented ones, so each card names something real.
Suggestions are editable drafts: check, edit, remove (undoable) or add
your own.

Generation is always the user's click and is metered: each call's real
token cost is charged against a monthly budget of 1% of the plan price
(Growth $2.49, Enterprise $4.99 or 1% of plan_overrides.contractMonthlyUsd),
so the share holds whichever model IMPROVE_SUGGESTION_MODEL points at.
Sets are cached per prompt, range and location; newer runs show a
"Regenerate" notice instead of rebuilding automatically. Growth+ only.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The Improve tab's footer gets Send Actions: the checked suggestions, exactly
as edited, become one Action Center action (kind prompt_improve, category
compete). Each suggestion is a task with its body, platform and evidence
in the new action_tasks.details column. The action's why and goal are
written from the platform states at send time, its impact from the
prompt's demand and the size of the gap, and its baseline keeps the
positions per platform for PROMPT-008. Validation measures it on its own
prompt.

After a send the button turns into Open in Actions, the footer shows the
action id, and the sent suggestions are marked. The send is recorded on
the cached set, so this survives a reload, and is dropped once the action
is dismissed. An idempotency key per send makes a double click create one
action.

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

An Improve action now keeps the prompt's last 7 days per platform at send
time as a fixed baseline (a trigger stops it being edited). A daily sweep
re-reads the same 7 days with the Positions panel's rule while the action
is open and for 30 days after completion, storing the comparison on
actions.position_outcome.

The action's Overview shows an Outcome card (baseline → last 7 days per
platform, "Not enough data" where a side had no runs), the Positions panel
shows "Improving · AC-n" with the delta, and the read at day 30 is final:
it is recorded in History and shown in its Result column.

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

Copy link
Copy Markdown
Collaborator Author

@gkhngyk PROMPT-008 is ready for review: Improve actions now keep a fixed 7-day baseline, a daily sweep compares the latest 7 days per platform (Outcome card on the action's Overview, "Improving · AC-n" in the Positions panel), and the read 30 days after completion is recorded in History. It's stacked on #1012, so only the last commit is new here. Migration 00124 adds actions.position_outcome and a trigger that keeps the baseline fixed.

@github-actions github-actions Bot added the needs-migration Adds or edits a migration — apply it to the hosted database label Oct 13, 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.

Thanks, this closes the loop nicely, and the baseline trigger is safe: nothing updates actions.baseline after insert. Three things before merge:

  1. Use the Improve tab's mention rule in the verdict. #1011 now marks a platform not_mentioned when the brand appears in fewer than half its runs (MIN_MENTION_RATE). platformVerdict instead treats any mention as mentioned, so going from 0 to 1 mention in 10 runs counts as "improved" while the panel still says not mentioned. Decide mentioned or not from the snapshot's state, which classifyPlatform already sets.
  2. Filter finished actions in the sweep's query. sweepImproveOutcomes loads up to 1,000 Improve actions in open or completed states, then skips the final ones in code. Final reads accumulate, so they will eventually fill the 1,000 and newer actions stop being refreshed. Exclude them in the query, e.g. open statuses, or completed_at within the last 31 days.
  3. Merge main in. The branch still carries the pre-review #1010–#1012 commits. Your own commit conflicts only in from-prompt-improve.js; keep both sides: improvePages and improveKpiKeys from #1012, plus your 7-day baseline and first outcome read.

cihan-gyk and others added 2 commits October 15, 2026 20:27
…rove-outcome

ansvisor#1010, ansvisor#1011 and ansvisor#1012, which this branch was stacked on, are now in main
in their final, reviewed form. The merge keeps main's versions of their
files and this branch's own change (measuring the position outcome of sent
Improve actions) on top: sendImproveToActionCenter keeps main's KPI keys
from the action's pages and adds the 7-day baseline and position_outcome.
…and skip final reads in the sweep

Decide mentioned or not from the snapshot's state, so a platform under
MIN_MENTION_RATE (e.g. 1 mention in 10 runs) stays not mentioned in the
verdict, the panel's position delta and the Outcome card's label. Leave
final reads out of the sweep's query so they can't fill its limit.

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

Copy link
Copy Markdown
Collaborator Author

@gkhngyk Thanks, all three are addressed:

  1. platformVerdict now decides mentioned from the snapshot's state (from classifyPlatform), so a platform under MIN_MENTION_RATE stays not mentioned. 0 → 1 of 10 runs is "unchanged". The same rule applies to the Positions panel's average delta and to the Outcome card's "Not mentioned" label. Tests cover the 1-in-10 case, both directly and through a refresh.
  2. The sweep's query now excludes final reads with position_outcome->>final (null outcomes are still included), and the in-code skip is gone. I matched the code's own definition of final rather than a completed_at window, so a missed sweep around day 30 can't lose the final read. A new test checks the query.
  3. main is merged. from-prompt-improve.js keeps improvePages/improveKpiKeys together with the 7-day baseline and the first outcome read. 00124 doesn't clash, and schema.sql is unchanged.

…iew-b8de67

# Conflicts:
#	supabase/schema.sql

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

Thanks, all three addressed. Migration 00124 is applied to production.

@gkhngyk
gkhngyk merged commit 3f56a6f into ansvisor:main Oct 15, 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