Skip to content

Dedupe algorithm listing and cap check by unique name - #12

Merged
Jaiminp007 merged 4 commits into
devfrom
fix/algorithm-list-dedupe
May 3, 2026
Merged

Jaiminp007 merged 4 commits into
devfrom
fix/algorithm-list-dedupe

Conversation

@Jaiminp007

Copy link
Copy Markdown
Collaborator

Why

The agent recently ran into this confusing state in a session:

The user wants me to save the modified version as a new algorithm, but they're at the 5-algo limit. I see they have these saved algorithms:

  1. orb-opening-range (v7)
  2. orb-opening-range (v1)
  3. capital-fortress (v4)
  4. capital-fortress (v1)
  5. rng-euler (v2)
  6. btc-per-lot-dca (v10)

That's 6 listed but the system says 5-algo limit.

The user actually had 4 unique algorithms (free tier cap = 5), but the listing exposed historical/duplicate-name rows so the agent thought the cap was already exceeded. The save itself succeeded only because it was an update of an existing name (which skips the limit check entirely).

What

Two paired changes so the listing and the cap check tell the same story:

tool/algorithm-list.ts

  • Collapse rows to one entry per unique name (latest version wins).
  • Add { count, capacity, remaining, tier } to the response body and the title — gives the agent a number to reason about instead of a list to count.
  • Free tier output now reads e.g. `4 algorithms (free tier, 1 slot left)` with the structured header echoed in the JSON.

tool/algorithm-save.ts

  • Cap check now compares `new Set(allAlgos.map(a => a.name)).size` against `Plan.SAVE_CAP[tier]` instead of `allAlgos.length`. A user with duplicate-name rows isn't blocked from saving when their unique-name count is under cap.

What this does NOT change

  • No schema migration.
  • No backfill of existing duplicate rows.
  • No change to the cap values (free: 5, lite: 15, pro: ∞).
  • Update saves (existing name) still skip the cap check, same as before.

Test plan

  • On a free-tier account with 4 unique names: `finny_algorithm_list` returns count: 4, remaining: 1.
  • Saving a 5th unique name: succeeds (4 → 5).
  • Saving a 6th unique name: blocked with the existing upgrade message.
  • Updating any existing name: still allowed regardless of count.
  • An account with duplicate-name rows: listing collapses to one entry showing latest version; cap reflects unique-name count.

Fix two related papercuts that confuse the agent when checking the
saved-algorithm cap:

1. algorithm-list now collapses to one entry per unique name (latest
   version wins) and surfaces { count, capacity, remaining, tier } in
   both the title and the JSON body. Historical/orphan rows under the
   same name no longer make the model think the user is over their cap.

2. algorithm-save's limit check counts unique names instead of total
   rows. Without this, a user with duplicate-name rows would be blocked
   even when the listing reports them as under the cap — paired so the
   two views stay consistent.

No schema change, no backfill. Existing duplicate rows are left in
place; the listing just hides them and the cap check ignores them.
Copilot AI review requested due to automatic review settings May 3, 2026 19:41

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Aligns algorithm listing and plan-cap enforcement so the agent sees (and reasons about) the same “unique algorithm names” count that the save path enforces, avoiding confusion from historical/duplicate-name rows.

Changes:

  • Update finny_algorithm_save cap enforcement to count unique algorithm names (not raw rows).
  • Update finny_algorithm_list to dedupe by algorithm name (latest version wins) and include plan capacity/remaining info in the response.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
packages/opencode/src/tool/algorithm-save.ts Changes cap check to compare unique-name count vs Plan.SAVE_CAP[tier].
packages/opencode/src/tool/algorithm-list.ts Collapses listing to one entry per unique name and adds plan capacity/remaining reporting to title/body/metadata.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 40 to 45
if (unique.length === 0) {
return {
title: "No algorithms",
output: "No algorithms saved yet. Use Build mode to generate a trading algorithm.",
metadata: { count: 0 },
output: `No algorithms saved yet. Use Build mode to generate a trading algorithm. (Plan: ${tier}, capacity: ${Number.isFinite(capacity) ? capacity : "unlimited"})`,
metadata: { count: 0, capacity, remaining, tier },
}
Comment on lines +57 to +67
const header = {
count: unique.length,
capacity: Number.isFinite(capacity) ? capacity : "unlimited",
remaining: remaining ?? "unlimited",
tier,
}

return {
title: `${algos.length} algorithm${algos.length === 1 ? "" : "s"}`,
output: JSON.stringify(summary, null, 2),
metadata: { count: algos.length },
title: `${unique.length} algorithm${unique.length === 1 ? "" : "s"} (${tier} tier${Number.isFinite(capacity) ? `, ${remaining} slot${remaining === 1 ? "" : "s"} left` : ""})`,
output: JSON.stringify({ ...header, algorithms: summary }, null, 2),
metadata: { count: unique.length, capacity, remaining, tier },
Comment on lines +26 to +35
// Collapse to one entry per unique name (latest version wins). This
// hides historical/orphan rows so the model doesn't double-count
// when checking whether the user is at their plan's save cap.
const latestByName = new Map<string, (typeof algos)[number]>()
for (const a of algos) {
const prev = latestByName.get(a.name)
if (!prev || a.version > prev.version) latestByName.set(a.name, a)
}
const unique = Array.from(latestByName.values()).sort((a, b) => b.time_updated - a.time_updated)

Comment on lines +114 to +121
const cap = Plan.SAVE_CAP[tier]
if (Number.isFinite(cap)) {
const allAlgos = await Algorithm.list()
if (allAlgos.length >= cap) {
// Count unique algorithm names, not rows. Historical /
// orphaned rows under the same name should not consume cap
// slots — this matches what algorithm-list now shows.
const uniqueCount = new Set(allAlgos.map((a) => a.name)).size
if (uniqueCount >= cap) {
#1 Always return JSON from algorithm-list. The empty-state branch
   previously returned a plain English string while the populated
   branch returned JSON; now both branches return the same shape so
   any caller can parse the output uniformly.

#2 Normalize capacity / remaining to (number | null) everywhere.
   The structured body and the metadata field both use null for
   unlimited tiers — no more Number.POSITIVE_INFINITY in metadata
   silently coercing to null when serialized for logs/telemetry.

#3 Tests for buildAlgorithmListPayload — extracted as a pure helper
   so the dedupe-by-name, latest-version-wins, capacity reporting,
   and JSON-roundtrip behavior can be exercised without spinning up
   Convex or the Plan service.

#4 Tests for countUniqueAlgorithms — the cap-check helper extracted
   from algorithm-save. Covers the orphan-row scenario (duplicate
   names not consuming extra cap slots) and the at-cap boundary
   condition that blocks new saves.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

capacity: number | null
/** remaining slots, or null when the tier is unlimited */
remaining: number | null
tier: string
const latestByName = new Map<string, Algorithm.Info>()
for (const a of algos) {
const prev = latestByName.get(a.name)
if (!prev || a.version > prev.version) latestByName.set(a.name, a)
- Type AlgorithmListPayload.tier as Plan.Tier instead of string so the
  schema reflects the closed enum and accidental tier labels can't sneak
  into tool output / metadata.
- Break ties in the dedupe loop by time_updated when versions are equal.
  Previously, two same-name rows on the same version would let
  Algorithm.list() ordering decide the winner non-deterministically;
  now the most recently updated row wins, matching the tool's stated
  'latest' semantics. Added a focused test covering this exact case.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +6 to +8
function row(over: Partial<Algorithm.Info>): Algorithm.Info {
return {
algorithmId: over.algorithmId ?? `id-${Math.random()}`,
Replace Math.random() with a monotonic counter so test runs are
reproducible and fixture-derived IDs stay stable if future assertions
ever include them.
@Jaiminp007
Jaiminp007 merged commit 45b22ab into dev May 3, 2026
spandanniroula pushed a commit to spandanniroula/finny that referenced this pull request Jun 12, 2026
Fixes all 17 Copilot comments:

Telemetry opt-out (polarline-markets#1, polarline-markets#2, polarline-markets#3, polarline-markets#9, polarline-markets#16):
- tracker.ts now reads FINNY_TELEMETRY=0 / OPENCODE_TELEMETRY=0 at module
  load, so the opt-out applies in every entrypoint (TUI worker, server,
  CLI), not just the bootstrap path.
- Email dialog auto-prompt and /subscribe slash command also gated on
  Analytics.isEnabled() — email POST goes to the same Convex deployment.

Tracker correctness (polarline-markets#5, polarline-markets#14, polarline-markets#17):
- drain() now also waits for the DeviceProfile.userId() resolution promise,
  not just the post-send promise.
- worker rpc.shutdown() and main-thread thread.ts both call
  Analytics.drain() before tearing down — neither beforeExit nor SIGTERM
  fire on worker.terminate() / process.exit(0).
- beforeExit handler runs at most once per process, preventing an infinite
  drain loop when a hung telemetry request later resolves and re-empties
  the loop.

Privacy (polarline-markets#10, polarline-markets#11, polarline-markets#12):
- Drop process.argv from tui.worker.booted (could include --prompt text
  and project paths).
- error.surfaced now sends only error.name; error messages frequently
  embed user prompt content and local file paths.
- Don't persist the submitted email address to local KV (no reader, just
  unnecessary PII retention).

Email dialog state machine (polarline-markets#4, polarline-markets#6, polarline-markets#8):
- Wait for kv.ready before evaluating the prompt gate so we don't re-show
  the dialog to users who already submitted/skipped.
- Conditional dismiss handler — only marks "skipped" when no terminal
  status has been set, so a successful submit isn't overwritten by the
  dismiss callback that fires on dialog.clear() and isn't suppressed by
  another dialog replacing this one.

Server-side validation (polarline-markets#15):
- subscribe mutation now rejects empty / oversized / regex-failing emails
  with a ConvexError, so the public Convex client can't bypass the TUI
  regex.

Command coverage (polarline-markets#13):
- Hook Analytics.track() in DialogCommand.trigger() instead of the bus
  event handler. trigger() is the central activation point for palette,
  slash, keybind, plugin, and bus paths — single hook, full coverage,
  no double-counting.

Comment wording (polarline-markets#7):
- bootstrap.ts: "opt-in by default" → "enabled by default" (correct
  description of opt-out semantics).

Co-Authored-By: Claude Opus 4.7 (1M context) <[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