Your own context, next to the numbers
By the end of this lesson you will have a note beside each ticker, kept in this browser and keyed by ticker, a page that survives its own corrupted storage, and a test that keeps it that way.
Here your tool stops being a worse version of a public website and becomes something only you have.
A price is available everywhere. Why you are watching this instrument is not available anywhere except in your head, and a tool that holds it beats one that does not, permanently. "Bought at 291, thesis is the services margin" is the note that turns a number into a decision you can review.
Where notes live, and why not in the same file
Your tickers are in src/lib/watchlist.ts and are committed to git. Notes should not be. They change often, they are personal, and some of them will mention money.
Keep them client-side, keyed by ticker:
type Notes = Record<string, { note?: string; tags?: string[] }>;
const KEY = "watchlist-notes-v1";
export function loadNotes(): Notes {
try {
return JSON.parse(localStorage.getItem(KEY) ?? "{}");
} catch {
return {}; // corrupt storage must not white-screen the tool
}
}
Three things in that small function.
The v1 in the key. When your shape changes, bump to v2 and you have not silently broken every existing user, including yourself in three weeks.
The try/catch. localStorage can hold anything, including half a write from a crashed tab. JSON.parse throws on that, and an uncaught throw during render is a blank page. This is a two-line defence against a class of bug that is very hard to reproduce on purpose.
This is deliberately temporary. localStorage means this browser, this
profile, until something clears it — it does not follow you to another machine,
it can be evicted with no event you can catch, and nothing on a server can read
it. That is the right trade today, because it buys you a working feature in ten
minutes with no backend. Build a Dashboard migrates it to a server store, and the reason
it can wait until then is that nothing in Build Your First Tool through Backtest Strategies needs your notes to
exist anywhere but in front of you.
Keyed by ticker, not by index. Index-keyed notes attach to positions, so reordering your list silently moves every note to the wrong instrument. That is data corruption that looks like a UI bug.
The client-side boundary you are now crossing
Until now everything rendered on the server. Notes are typed by a human, so this feature needs a client component, and that is the exact conversion the loop lesson told you to watch for.
Do it deliberately and keep it small: the note editor is a client island; the page stays a server component. The "use client" directive goes on the little component that owns the input, not on the page. If your assistant proposes converting the page, that is the diff to reject, because a client page is a page whose data fetching can drift into the browser.
Tags earn their place only if you use them
Tags are how you ask "show me only the things I am watching for the same reason" once your list outgrows a screen. Two rules keep them from becoming clutter: a small fixed vocabulary you actually reuse, and no tag that duplicates something the data already tells you. sector:tech is derivable from the API. thesis:margins is not, and that is the kind worth typing.
Try it now
The prompt
First, in your editor, move "notes on symbols" out of the "Not yet" line in your PLAN.md: the plan now includes them, and the agent reads the plan before every change. Then mock mode on, plan mode on, and paste:
Add a note to each watchlist row, stored in localStorage under the key
"watchlist-notes-v1" and keyed by ticker, never by position. Put
loadNotes and saveNotes in src/lib/notes.ts, and the input in a small
"use client" component; the page itself stays a server component. Plan
first and name the files. Write tests/notes.test.ts before the code,
stubbing localStorage with vi.stubGlobal so the test decides exactly
what is stored: loadNotes returns {} when the stored value is not valid
JSON, and a saved note comes back under its own ticker. Show me them
failing, then the code. No API request. Run npm test,
npm run typecheck and npm run check:leak.
A good answer adds src/lib/notes.ts, the test file and one small client component, touches the island that renders the rows, and leaves src/lib/watchlist.ts alone: notes are not part of the committed list.
Check it yourself
- The first test run was red, with the corrupt-storage test among the failures.
- Green afterwards, and every earlier test still passes.
- In mock mode, add a note to one row and reload: it is still there. Change the sort order: it stays beside the same ticker.
git diff --statdoes not listsrc/lib/watchlist.ts, andgrep -n "use client" src/app/page.tsxprints nothing.
Break it on purpose
Now do the test that matters: open developer tools, corrupt the stored JSON by hand, and reload. If your tool white-screens, your try/catch is missing or in the wrong place. A tool that dies because of its own saved data is a tool you will stop trusting. Then delete the try that protects JSON.parse (your agent may have put it in a helper beside loadNotes; it is the one around the parse, not the one around localStorage access) and run npm test: the corrupt-storage test goes red, which is why nobody has to open developer tools to check it again. Put it back.
Compare with ours
Our save, and the test that guards the load
export function saveNotes(notes: Notes): void {
localStorage.setItem(KEY, JSON.stringify(notes));
}
it("returns no notes when the stored value is not JSON", () => {
vi.stubGlobal("localStorage", { getItem: () => "{broken", setItem: () => {} });
expect(loadNotes()).toEqual({});
});
loadNotes is the function at the top of this lesson, unchanged. The full state at the end of this unit is on the starter's branch ref/build-your-first-tool/u3.
Checkpoint
npm test
git status # only the files you expect, and no .env*
git add -A
git commit -m "Notes beside each ticker, kept in this browser, keyed by ticker"
Read git status first: it should list the notes module, its test, the components the agent changed and your PLAN.md, and no .env* file. If your rule from Pair asks the agent to split changes over a set size, answer that this lesson is one change. Then answer this lesson's questions.