Skip to content

feat(content): re-open finished opportunities when their signals move - #960

Merged
gkhngyk merged 1 commit into
mainfrom
feature/opportunity-reopen
Oct 7, 2026
Merged

gkhngyk merged 1 commit into
mainfrom
feature/opportunity-reopen

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

Summary

Last PR of Phase 4 in #857. The spec (§27) says: "Underlying signals keep changing. A completed opportunity that strengthens later can be re-evaluated as New Signal Detected; no duplicate opportunity should be created." A finished opportunity now goes back to New when its signals move, and keeps the same row.

When an opportunity re-opens (lib/opportunity-reopen.js)

The check runs in the nightly generation, before new opportunities are written. It only looks at opportunities that are done and whose actions are all completed and measured. Nothing re-opens before its result is known. One of these conditions must hold:

Reason Condition
score_rose The cluster's current score is at least SCORE_RISE (10) points above the opportunity's recorded score: more demand, or competitors further ahead. This is the spec's "strengthens".
gain_lost The basket's current visibility is below GAIN_LOST_RATIO (half) of the "after" value its action measured (#956), meaning the gain did not hold.

The current figures are computed the same way generation scores clusters. The measured value is the highest basket ai_visibility after-value across the opportunity's actions.

What re-opening does

  • The same row goes back to new with its score and metrics refreshed.
  • source_data.reopened records when, why, the previous and current score, and the measured and current visibility.
  • The earlier actions move to source_data.pastActions and stay visible on the Results card. The opportunity can be sent to the Action Center again.
  • Its clusters stay covered, so no near-copy is generated beside it.

Status trigger (migration 00114)

  • The status trigger from feat(content): follow an opportunity through its Action Center work #952 counted every action sent from an opportunity. After a re-open, the earlier completed actions would have made new work show as In Progress as soon as it was sent.
  • The trigger now ignores actions created before reopened.at.
  • If only earlier work changes, it leaves the status alone.

UI

  • The list marks re-opened opportunities with a New signal detected badge.
  • The detail page explains what changed. Examples: "the score rose from 72 to 84", or "visibility on these prompts was 20% after the work and is 8% now".

Tried on production data

  • Migration 00114 is applied.
  • In a rolled-back transaction, a test action went through a full cycle: complete → done, re-open → new, earlier action changed → still new, new action sent → sent, started → in_progress, completed → done.
  • The re-open query's JSON-path filter was checked read-only against production.
  • No opportunity on production is done and measured yet, so the re-open pass has nothing to act on today. Its decisions are covered by unit tests.

Related issue

Part of #857. This completes Phase 4.

Type of change

  • feat — New feature
  • test — Adding or updating tests

How to test

  1. Take a done opportunity whose actions are completed and measured. Raise its cluster's score by 10 or more, for example by lowering opportunity_score in a test database, then run generation. It returns to New with the badge and the explanation.
  2. Send it again and start the new action. The opportunity reads Sent, then In Progress, and is unaffected by the earlier, completed action.

opportunity-reopen.test.js covers both thresholds and their edges, waiting for every action to be completed and measured, and measurements that are not basket-scoped.

Checklist

  • Branch follows the naming convention
  • Commits follow Conventional Commits
  • yarn lint, yarn typecheck, yarn format:check, yarn test (418) and yarn build pass (run from web/)
  • yarn lint, yarn format:check and yarn test (1008) pass (run from server/)
  • supabase/schema.sql rebuilt with build-schema.sh

🤖 Generated with Claude Code

The spec lets a completed opportunity come back as "New signal detected"
when it later strengthens, without a duplicate being generated (#857,
Phase 4).

- Before writing new opportunities, generation re-checks the brand's done
  ones whose actions are all completed and measured. One goes back to New
  when its cluster's score rose 10 points since it was scored, or when the
  basket visibility fell below half of what its action measured
  afterwards.
- The same row re-opens with refreshed metrics and a record of why, with
  the earlier actions kept as past actions. It can be sent again.
- Migration 00114: the status trigger ignores actions created before the
  opportunity re-opened, so the new work is not shown as in progress
  because the old work is complete.
- The list marks re-opened opportunities "New signal detected", and the
  detail page says what changed. Results lists past actions too.
@github-actions github-actions Bot added the needs-migration Adds or edits a migration — apply it to the hosted database label Oct 7, 2026
@gkhngyk
gkhngyk merged commit 460242f into main Oct 7, 2026
6 checks passed
@gkhngyk
gkhngyk deleted the feature/opportunity-reopen branch October 7, 2026 13:10
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.

1 participant