Repository navigation
Dedupe algorithm listing and cap check by unique name - #12
Conversation
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.
There was a problem hiding this comment.
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_savecap enforcement to count unique algorithm names (not raw rows). - Update
finny_algorithm_listto 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.
| 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 }, | ||
| } |
| 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 }, |
| // 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) | ||
|
|
| 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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| 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.
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]>
Why
The agent recently ran into this confusing state in a session:
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
tool/algorithm-save.ts
What this does NOT change
Test plan