source: docs/P4-Prototype/wiki/PrototypeImplementationAIUsage.md

main
Last change on this file was ef1c1c7, checked in by Stefan <trsunovstefan@…>, 6 days ago

Wiki docs, phase 6 and phase 7 added

  • Property mode set to 100644
File size: 19.9 KB
Line 
1= Prototype Implementation AI Usage =
2
3== Name of AI service/solution that was used ==
4
5'''Claude Code''' (Anthropic), an AI coding assistant that runs in the terminal and reads and
6edits the project files.
7
8 * '''URL:''' `https://claude.com/claude-code`
9 * '''Type of service/subscription:''' Claude subscription (Claude Code CLI). A different model was
10 used in each session:
11
12||= Session =||= Date =||= Model =||
13|| 1 || 2026-04-21 || Claude Opus 4.7 (1M context) ||
14|| 2 || 2026-08-06 / 2026-08-07 || Claude Opus 5 (1M context) ||
15|| 3 || 2026-09-16 || Claude Sonnet 5 ||
16|| 4 || 2026-09-24 || Claude Opus 5.5 (1M context) ||
17
18The same sessions also worked on other phases. This page covers only what concerns the P4
19prototype and its documentation.
20
21== Final result ==
22
23=== Results in details / description ===
24
25'''My own starting code (before any AI was used).''' Before session 1 I had written:
26
27 * a Go backend with HTTP handlers (Chi router);
28 * a draft database schema (`server/db/db.sql`, `server/db/schema.sql`) and a diagram description
29 (`docs/dbdiagram.md`), based on my data model in `ep-diagram.md`;
30 * a `main.go` / `db.go` that connected to PostgreSQL and dropped and recreated every table on
31 each start;
32 * a half-finished frontend, which I deleted myself at the start of session 1 because the
33 prototype does not need it.
34
35This code is older than the git repository. The first commit (2026-08-07) was made after
36sessions 1 and 2, so the history in git starts from the AI-improved version. My original files
37are not in the repository. What they contained, and which errors the AI found in them, is
38recorded in the session 1 log below and in [wiki:ERModelAIUsage].
39
40'''Session 1 (2026-04-21).''' Starting from that code, the AI:
41
42 * replaced my HTTP backend and the frontend scaffolding with a single-binary CLI prototype in Go,
43 split across `main.go`, `cli.go`, `auth.go`, `account.go`, `market.go`, `trade.go`,
44 `portfolio.go` and `watchlist.go`. This followed my decision to make a CLI and not a web app;
45 * corrected my schema. `crypto_id` had been declared as a foreign key to two tables at once in
46 `holdings`, `orders` and `transactions`, and `market_candles` referenced a `markets` table that
47 did not exist. The corrected schema became `schema_creation.sql`, with the sample data in
48 `data_load.sql`;
49 * replaced my `db.go`, which dropped every table on every start, with one `Connect()` plus
50 explicit `-init` and `-load-data` flags;
51 * moved `go.mod` to the project root, removed an unused MySQL driver, and made
52 `github.com/lib/pq` a direct dependency;
53 * wrote the trade flows as database transactions with `FOR UPDATE` row locks, cost-basis
54 bookkeeping and one ledger row per operation;
55 * wrote the market-simulation bot (`bots/main.go`), after I decided to keep a market simulator
56 in the project.
57
58'''Session 2 (2026-08-06/07).''' I asked for a code review. The AI found and fixed three bugs
59(`.env`/SQL path resolution, an endless loop at end of input, and a wrong error check on the
60sell path). It also replaced the read-modify-write holding update with one
61`INSERT … ON CONFLICT DO UPDATE`, removed the committed `TerraER3.11.jar` and a !TradingView
62screenshot we had no licence for, and wrote the first version of the P4 pages.
63
64'''Session 3 (2026-09-16).''' From an edge case I described, the AI added
65`holdings.reserved_quantity`, made the sell path reserve and then settle, and gave
66`orders.status` a real `open` → `executed` lifecycle.
67
68'''Session 4 (2026-09-24).''' I asked whether P4 fulfils the course rules. The AI found that the
69prototype still asked the user to type a market symbol and crypto symbols, which breaks the rule
70that the user must never have to remember identifiers or codes. It changed every choice to a
71numbered list (`market.go`, `trade.go`, `watchlist.go`), retook every screenshot, one per
72scenario step, and rewrote the P4 pages.
73
74=== Test evidence ===
75
76This is the current prototype (after session 4) on fresh sample data, logged in as `alice`. It
77is a real run, and the same run is on the screenshots of
78[wiki:UseCase0004Implementation]:
79
80{{{
81-- Place market buy order --
82
83 # Symbol Quote Last price
84 -----------------------------------------
85 1 ADA USD 0.453750
86 2 BTC USD 67140.000000
87 3 DOGE USD 0.122000
88 4 ETH USD 3520.000000
89 5 SOL USD 166.100000
90Market #: 2
91Latest price for BTC/USD = 67140.000000
92Quantity: 0.01
93Order executed: buy 0.0100 BTC @ 67140.000000 (notional 671.4000 USD)
94
95 Symbol Quantity Reserved Available Avg buy Current Value Unrealised P/L
96 ------------------------------------------------------------------------------------------------------------------
97 BTC 0.0100 0.0000 0.0100 67140.000000 67140.000000 671.4000 +0.0000
98 ETH 0.5000 0.0000 0.5000 3500.000000 3520.000000 1760.0000 +10.0000
99 ------------------------------------------------------------------------------------------------------------------
100 TOTAL 2431.4000 +10.0000
101
102 Cash available : 7578.6000 USD
103 Portfolio value: 2431.4000 USD
104 Net worth : 10010.0000 USD
105}}}
106
107== Summary of AI involvement ==
108
109||= =||= Session 1 — 2026-04-21 =||= Session 2 — 2026-08-06/07 =||= Session 3 — 2026-09-16 =||= Session 4 — 2026-09-24 =||
110|| '''What I brought''' || My Go backend (Chi HTTP handlers), draft schema, a half-finished frontend || The CLI prototype as it stood after session 1 || A design review: the sell path had no way to reserve crypto committed to an order || The official P4 instructions and the question whether the prototype and pages meet them ||
111|| '''What the AI did''' || Rewrote the backend as a CLI covering UC0001–UC0007, corrected the schema, wrote the market bot || Reviewed the code, found and fixed three bugs, improved the holding upsert || Added `holdings.reserved_quantity`, reserve-then-settle sell path, `orders.status` lifecycle, tested it including real concurrency || Audited P4, made every choice a numbered list, retook all screenshots, rewrote the P4 pages ||
112|| '''What I decided''' || To delete the frontend, to build a CLI and not a web app, to keep the market simulator || To ask for a code review and not only documentation || To keep reserve and settle in one transaction, because there is no cancel-order use case to free a stuck reservation || To fix the problems found and to have the screenshots taken from real runs ||
113
114== Entire AI usage log ==
115
116'''About this log.''' Sessions 3 and 4 are still in my local Claude Code history, so their
117prompts are quoted '''verbatim''' below, typos included. The local history of sessions 1 and 2
118(April and August 2026) is no longer available. For them, the prompts are the excerpts I wrote
119down at the time, the same ones as on [wiki:ERModelAIUsage],
120where they were logged because the same conversation produced P1–P4. The AI's responses are
121summarised, because several ran to many pages of code and file edits.
122
123----
124
125=== Session 1 — 2026-04-21 (Claude Opus 4.7, 1M context) ===
126
127'''Prompt (student, excerpt):'''
128> This is my project for my exam Database, go to the docs folder and README.md there you will find all of the documentation, afterwards take a look at the whole project. Delete everything you want add everything you want just make it work and make it fill in or to follow all of the needed instructions. Ask me anything, also tell me what do I need to do
129
130'''Response (AI, summarised):'''
131 * Read `docs/opis.md`, `docs/ep-diagram.md`, `docs/dbdiagram.md`, `server/db/db.sql`,
132 `server/db/schema.sql` and all Go sources.
133 * Found errors in my schema: `holdings.crypto_id` declared as a foreign key to both
134 `users(id)` and `crypto(id)` (and the same mistake in `orders` and `transactions`), and
135 `market_candles` referencing a non-existent `markets` table. Also found that `main.go` dropped
136 and recreated all tables on every start, wiping the data.
137 * Asked me to decide on team composition, prototype shape (CLI or HTTP), which columns to keep,
138 which use cases to implement, and the documentation language.
139
140'''Prompt (student, excerpt):'''
141> Read it now, just filled it in, also deleted the frontend we don' need that
142
143'''Response (AI, summarised):'''
144 * Re-read `docs/README.md`, which now contained the course rubric, and explained the AI rules
145 per phase and the deliverables of each phase.
146
147'''Prompt (student, excerpt):'''
148> do everything that you need to do, and tell me after what do I need to do
149
150'''Response (AI, summarised):'''
151 * Rewrote the backend as the CLI prototype (UC0001–UC0007), with the corrected
152 `schema_creation.sql`, `data_load.sql`, the new `db.go` with `-init` / `-load-data`, the
153 transactional trade flows and the market bot. The details are under ''Results in details''
154 above.
155 * Ran the prototype end to end against PostgreSQL on port 5433 and checked that a sample buy
156 and the portfolio view gave the expected numbers.
157
158----
159
160=== Session 2 — 2026-08-06 / 2026-08-07 (Claude Opus 5, 1M context) ===
161
162'''Prompt (student, excerpt):'''
163> […] do all of the other Phases till m0.
164> opis.md It's p0 so I will take care of that. Delete anything that we don't need,
165> make all of the phases and terra diagram if you can, and delete anything
166> that we don't need and make a documentation about how to start it.
167
168'''Prompt (student, excerpt, follow-up):'''
169> Also fix some database things or golang things if you think we can do it better,
170
171'''Response (AI, summarised): what changed in the prototype, and why'''
172
173'''Bugs found and fixed'''
174
175 1. `server/db/db.go` resolved `../.env` and `db/schema_creation.sql` relative to the working
176 directory, so they only worked when the program was started from inside `server/`.
177 Following the documented instructions (build from the repository root and run
178 `./eduberza -init`) failed with `password authentication failed for user "postgres"`,
179 because `.env` was never found and the defaults were used. The two SQL scripts are now
180 compiled into the binary with `go:embed`, and `.env` is searched for in the working directory
181 and every parent. Real environment variables now take precedence over the file.
182 2. `prompt()` in `server/cli.go` ignored the error from `ReadString`. At end of input (Ctrl-D,
183 or a scripted run) it returned an empty string forever, and the menu loop kept printing
184 "Unknown option." without end. It now exits cleanly.
185 3. On the sell path, `trade.go` checked `err == sql.ErrNoRows || held < qty` before checking
186 for other errors, so any scan failure was reported as "Insufficient holding". The error
187 check now comes first.
188
189'''Improvements'''
190
191 4. The holding upsert was a read-modify-write in Go. It is now a single
192 `INSERT … ON CONFLICT (user_id, crypto_id) DO UPDATE`, so PostgreSQL recomputes the average
193 in `numeric` arithmetic, relying on the unique constraint of the relational design.
194 5. `TerraER3.11.jar` was removed from the repository and `.gitignore` now excludes `*.jar`,
195 because P4 requires third-party executables to be downloaded, not committed. `.env` is
196 excluded too, and `.env.example` was added in its place.
197 6. `image.png`, a !TradingView screenshot, was deleted, because the project has no licence to
198 publish it.
199
200'''Verification.''' All seven use cases were run against PostgreSQL 16, and the four failure
201paths were tested. After a rejected purchase, the affected user had zero rows in `orders`,
202`transactions` and `holdings`.
203
204----
205
206=== Session 3 — 2026-09-16 (Claude Sonnet 5) ===
207
208'''Prompt (student, verbatim):'''
209> Soo we have a problem here In this scenario we have an edge case where our functionallity doesn't work:
210> Suppose the user owns:
211>
212> 2 BTC
213>
214> and wants to sell:
215>
216> 0.5 BTC at market price
217>
218> A sensible procedure is:
219>
220> 1. User creates an Order
221>
222> Orders gets something like:
223>
224> id user market side type quantity status
225> O1 Alice BTC/USD sell market 0.5 open
226>
227> At this point, no trade has necessarily happened yet.
228>
229> 2. Reserve the crypto
230>
231> This is where your current model has a gap.
232>
233> You currently have:
234>
235> Holds
236> quantity
237> avg_price
238>
239> but you don't have a concept of reserved/committed crypto.
240>
241> For cash, you already explicitly distinguish:
242>
243> available_balance
244> invested_balance
245>
246> For crypto, you probably want the equivalent:
247>
248> quantity
249> reserved_quantity
250> avg_price
251>
252> So Alice might go from:
253>
254> quantity = 2.0 BTC
255> reserved_quantity = 0
256>
257> to:
258>
259> quantity = 2.0 BTC
260> reserved_quantity = 0.5 BTC
261>
262> with:
263>
264> available BTC = quantity - reserved_quantity = 1.5 BTC
265>
266> The important point is that you generally don't actually remove the 0.5 BTC from her position yet. You reserve it so she cannot submit another sell order for the same BTC.
267>
268> Can you please start from Phase 1 and modify everything to make it work like it should, modify the AIUsage.md in every Phase, and can you make the ConceptualMOdel a little bit more put together not that much spread out.
269
270The other prompts of that day were about the P1 diagram (logged on
271[wiki:ERModelAIUsage]) and about P5 and P6. None of them
272changed the P4 prototype.
273
274'''Response (AI, summarised)'''
275
276The gap: `trade.go`'s sell path checked `held < qty` directly against `holdings.quantity`.
277Nothing distinguished "owned" from "owned, but already promised to an order". This was safe only
278because the whole sell runs in one transaction with a `SELECT … FOR UPDATE` lock. It would stop
279being safe the moment an order could stay `open` across transactions.
280
281What changed:
282
283 1. `holdings.reserved_quantity numeric(20,4) NOT NULL DEFAULT 0 CHECK (reserved_quantity >= 0 AND reserved_quantity <= quantity)`
284 was added to `schema_creation.sql`.
285 2. `v_portfolio` gained `reserved_quantity` and the derived `available_quantity`.
286 3. The sell path in `trade.go` now locks the holding, computes
287 `available := quantity - reserved_quantity`, rejects the order if `available < qty`,
288 reserves (`reserved_quantity += qty`), then settles (`quantity -= qty; reserved_quantity -= qty`).
289 All of this stays inside the same transaction.
290 4. Both branches of `PlaceOrder` insert the order as `status='open'` and set
291 `status='executed', executed_at=now()` at the end.
292 5. `portfolio.go` shows new `Reserved` and `Available` columns.
293 6. The sell error message became
294 `"Insufficient holding: trying to sell X, available Y (of Z held, W reserved)"`.
295
296'''Test evidence''' (PostgreSQL 16, `bp_database`, `localhost:5433`):
297
298{{{
299$ eduberza sell 0.5 BTC (Alice: 2.0000 BTC held, 0.0000 reserved)
300Order executed: sell 0.5000 BTC @ 67140.000000 (notional 33570.0000 USD)
301# holdings.quantity: 2.0000 -> 1.5000, reserved_quantity: 0.0000 (unchanged net of reserve+release)
302
303$ eduberza sell 1 ETH (Bob: no holdings row at all)
304Insufficient holding: trying to sell 1.0000, available 0.0000 (of 0.0000 held, 0.0000 reserved)
305
306# Two concurrent processes, Alice at 1.5 BTC / 0 reserved, each selling 1.0 BTC:
307=== process A === Insufficient holding: trying to sell 1.0000, available 0.5000 (of 0.5000 held, 0.0000 reserved)
308=== process B === Order executed: sell 1.0000 BTC @ 67140.000000 (notional 67140.0000 USD)
309# final holding: quantity 0.5000, reserved_quantity 0.0000 — exactly one sell went through
310
311# Reserve visible mid-transaction, in one psql session (BEGIN; ...; COMMIT;):
312before: quantity 2.0000, reserved_quantity 0.0000, available 2.0000
313after reserve: quantity 2.0000, reserved_quantity 0.5000, available 1.5000
314after settle: quantity 1.5000, reserved_quantity 0.0000, available 1.5000
315
316# The CHECK constraint holds even without going through trade.go:
317UPDATE holdings SET reserved_quantity = quantity + 1 WHERE ...;
318ERROR: new row for relation "holdings" violates check constraint "holdings_check"
319}}}
320
321(At that time the sell path still asked for a symbol, so a user with no holding could reach the
322check. Since session 4, such a user is never offered anything to sell.)
323
324'''What I decided:''' to keep reserve and settle inside a single transaction. Splitting them into
325two commits, so that an order really sits `open` and reserved in between, is what a real
326limit-order matcher will need. Building it now would add a way for an order to get stuck without
327a way to cancel it.
328
329----
330
331=== Session 4 — 2026-09-24 (Claude Opus 5.5, 1M context) ===
332
333'''Prompt (student, verbatim):'''
334> Does the P4 documentation, fullfill this:
335>
336> ''[pasted: the complete official text "Instructions on Phase P4: First Application Prototype"''
337> ''from the course site — the prototype requirements, the rule that the user must never have to''
338> ''remember identifiers or codes, the !PrototypeImplementation / UseCaseXXXXImplementation /''
339> ''!BuildInstructions templates, the source-code rules and the AI Use rules with the''
340> ''PrototypeImplementationAIUsage template]''
341>
342> is this correct?
343> Or do we need to change anything?
344
345'''Response (AI, summarised):''' an audit of the prototype and of the P4 pages against each
346requirement. The main findings:
347 * The prototype asked the user to type a market symbol (`Market symbol (e.g. BTC):`) and, in the
348 watchlist, a crypto symbol. That breaks the rule that every choice must be picked from a list.
349 * The use-case implementation pages had one screenshot per use case, not one per step, and did
350 not show the SQL at every step.
351 * `PrototypeImplementation.md` was written in Trac markup inside a Markdown file. It did not
352 follow the template, and it said identifiers were never typed, which was not accurate.
353 * `BuildInstructions` had wrong details: the `-init` message, the order of the history, an
354 outdated ER diagram path, an unpinned PostgreSQL version described as 16, and a concurrency
355 test with no steps to prepare it.
356 * This page listed only one model, still had "Student action required" notes, and had no
357 line-by-line log.
358
359'''Prompt (student, verbatim):'''
360> YEs you can start fixing it, and can you make the screenshots yourself?
361
362'''Response (AI, summarised):'''
363 * '''Code''' (`market.go`, `trade.go`, `watchlist.go`):
364 * `ListMarkets` now returns the numbered list it prints. `ChooseMarket` asks for `Market #:`
365 and takes the market by its position in that list.
366 * New `ChooseHolding` for the sell path. It lists only the user's holdings that still have a
367 free quantity, with `Held` and `Free to sell`, and asks for `Holding #:`.
368 * Watchlist ''Add'' lists, numbered, the cryptos not yet on the watchlist. ''Remove'' lists the ones
369 on it.
370 * A shared `pickNumber` refuses anything outside `1…n` with
371 `Invalid choice, enter a number from 1 to N.`
372 * The SQL that looked a crypto up by symbol (`WHERE upper(c.symbol) = upper($1)`) is gone. The
373 chosen row's id is used directly.
374 * '''Screenshots:''' all of them were retaken from real CLI sessions, driven in a pseudo-terminal
375 against freshly initialised sample data. There is one screenshot per scenario step, including
376 the alternate flows (invalid e-mail, duplicate user, wrong password, invalid amount,
377 insufficient funds, insufficient holding, invalid list choice). This gave 30 screenshots,
378 which replace the old 8.
379 * '''Documentation:'''
380 * Rewrote the seven UseCase000XImplementation pages, with the SQL of each step quoted literally
381 from the code.
382 * Rewrote [wiki:PrototypeImplementation] (real Markdown, template
383 order, accurate description of the choices), [wiki:BuildInstructions] (the
384 corrections above, a mini-guide of every menu item, and a smoke test re-run on 2026-09-24)
385 and this page.
386 * Made small matching edits to the P3 use cases UC0004, UC0005 and UC0007, so that the "system
387 lists …, user picks …" steps match the prototype.
388 * Wrote the wiki versions of the P4 pages for the faculty site.
389
390'''What I decided:''' to accept the numbered-list change, because it is what the P4 rule asks for.
391I also decided to have the screenshots made from real runs instead of editing the old ones.
Note: See TracBrowser for help on using the repository browser.