source: docs/P1-ConceptualModel/ERModelAIUsage.md

main
Last change on this file was 1549dae, checked in by Stefan <trsunovstefan@…>, 55 minutes ago

Correct P1/P2 consistency (Holdings, WatchlistItems), redo P5 normalization

  • Property mode set to 100644
File size: 19.1 KB
Line 
1# Entity-Relationship Model AI Usage
2
3## Name of AI service/solution that was used
4
5**Claude Code** (Anthropic)
6
7- **URL:** https://claude.com/claude-code
8- **Type of service/subscription:** Claude subscription. Session 1 used model
9 Claude Opus 4.7 (1M context); session 2 used Claude Opus 5 (1M context).
10
11## Final result
12
13### Diagram
14
15Current version: `ERModel_v05.xml` / `ERModel_v05.png` (see session 5 below). First
16version: `ERModel_v01.xml` / `ERModel_v01.png`.
17
18**Declaration of how the diagram was produced.** The initial model is the
19student's own: the entity and attribute list in [`ep-diagram.md`](ep-diagram.md),
20written in Macedonian before any AI was involved. In session 2 the AI turned that
21list into the TerraER diagram file, and while doing so proposed three changes to
22the initial model, all three listed in the model history on
23[ERModel](ERModel.md#entity-relationship-model-history):
24promoting `Markets` to its own entity set, re-expressing `holdings` and
25`watchlist_items` as M:N relationships with attributes rather than entity sets
26with foreign keys, and marking `avg_price` as derived.
27
28The diagram was not drawn by hand in the TerraER GUI. It was generated
29programmatically by constructing TerraER's own figure objects
30(`EntidadeFigure`, `RelacionamentoFigure`, `AtributoFigure`,
31`AtributoChaveFigure`, `AtributoDerivadoFigure` and the labelled line-connection
32figures) and serialising them with TerraER's own
33`DOMStorableInputOutputFormat` — the same writer the application uses when you
34choose *Save*. The file is therefore a normal TerraER document: it was verified
35by reading it back through TerraER's own reader and comparing the figure count
36(144), and it opens and can be edited in TerraER 3.11 like any hand-drawn
37diagram. `ERModel_v02.xml` is the student's own review pass over v01, done by
38hand in the GUI.
39
40`ERModel_v03.xml` / `ERModel_v03.png` (session 3, 2026-09-16) were produced the
41same way, this time as a genuine load–modify–save round trip through TerraER's
42own classes rather than a from-scratch build: `ERModel_v02.xml` was read with
43the application's real `DrawFigureFactory` and `DOMStorableInputOutputFormat`
44into a live `QuadTreeDrawing`, one `AtributoFigure` was cloned from the
45existing `quantity` attribute of `Holds` (to inherit its exact styling) and
46relabelled `reserved_quantity`, a matching `LabeledLineConnectionFigure` was
47added between it and the `Holds` diamond (`ChopDiamondConnector` /
48`ChopEllipseConnector`, the same connector pair every other attribute of
49`Holds` uses), and every entity and relationship — together with its own
50attributes, moved by the same offset — was translated proportionally toward
51the diagram's centroid to close up excess canvas space, after which every
52connection figure had `updateConnection()` called so its drawn path follows
53the moved figures. The result was written with the real writer and rendered to
54PNG with TerraER's own `ImageOutputFormat`, and re-verified by reading
55`ERModel_v03.xml` back and confirming the figure count (146 = 144 + the new
56attribute + its line) and that all five attributes of `Holds` resolve with the
57expected connector classes. No figure was hand-edited in XML.
58
59### Model description
60
61See [ERModel](ERModel.md).
62
63## Summary of AI involvement
64
65Work on this project happened in three working sessions.
66
67| | Session 1 | Session 2 | Session 3 |
68|---|---|---|---|
69| **When** | 2026-04-21 | 2026-08-06 / 2026-08-07 | 2026-09-16 |
70| **Model** | Claude Opus 4.7 (1M context) | Claude Opus 5 (1M context) | Claude Sonnet 5 |
71| **Phases advanced** | P1, P2, P3 and the first working prototype | The ER diagram file, P4 documentation, bug fixes | `Holds.reserved_quantity` added across P1–P4 |
72| **My starting material** | `ep-diagram.md`, `opis.md`, my existing Go backend and draft SQL | Everything from session 1, plus the phase rubric | Everything from sessions 1–2, plus a design review of the sell-order flow |
73
74In **session 1** I brought my own data model (`ep-diagram.md`, written in
75Macedonian before any AI was involved) and my own draft schema and Go backend. I
76used the AI to review them, and it found real errors in my SQL that I had missed
77— most seriously that I had declared `crypto_id` as a foreign key to two
78different tables at once, in three separate tables. I decided the corrections to
79adopt, chose to go solo, chose a CLI prototype over an HTTP one, and chose
80English for the documentation. The output of that session was the corrected
81schema, the sample-data script, the use-case model and the working prototype.
82
83In **session 2** I came back to produce the deliverable I was still missing — the
84TerraER diagram file itself — and to finish the P4 documentation. I also asked
85for a review pass over the database and Go code, which turned up three further
86bugs (see [PrototypeImplementationAIUsage](../P4-Prototype/PrototypeImplementationAIUsage.md)).
87
88In **session 3** I described a concrete edge case I'd spotted in the sell flow
89— nothing stopped a user from placing a second sell order against crypto
90already promised to a first, unsettled one — and asked the AI to fix it
91starting from P1 and carry the fix through every phase. This added
92`reserved_quantity` to `Holds` and changed the sell path in `trade.go` to use
93it; see the model history on
94[ERModel](ERModel.md#entity-relationship-model-history) (v03) and the log
95below.
96
97Below is the log, one entry per exchange. My prompts are quoted as **excerpts**:
98the wording is my own, unaltered, but I have left out scheduling remarks and
99other working chatter that says nothing about the model or the code. Typos are
100kept as they were. The *Intent* line above each prompt is my own note on what I
101was trying to achieve. AI responses are summarised rather than quoted in full,
102because several ran to many pages of code and file edits. The complete
103unabridged transcript is in my Claude Code session history and can be produced
104on request.
105
106## Entire AI usage log
107
108---
109
110### Session 1 — 2026-04-21
111
112**Intent:** hand the AI the whole project and the course rubric, and have it
113audit my existing model and code against the phase requirements before I commit
114to anything.
115
116**Prompt (student, excerpt):**
117> 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
118
119**Response (AI, summarised):**
120- Read `docs/opis.md`, `docs/ep-diagram.md`, `docs/dbdiagram.md`, `server/db/db.sql`, `server/db/schema.sql`, all Go sources.
121- Flagged bugs in the initial schema:
122 - `holdings.crypto_id` declared as a foreign key to both `users(id)` and `crypto(id)`; should be a separate `user_id` column.
123 - Same mistake in `orders` and `transactions`.
124 - `market_candles` referenced a non-existent `markets` table.
125 - `main.go` dropped and recreated all tables on every startup, wiping data.
126- Proposed a single authoritative schema, to be placed in `server/db/schema_creation.sql`, based on `docs/ep-diagram.md` and `docs/dbdiagram.md` with the above issues fixed.
127- Asked the student for decisions: team composition, prototype shape (CLI vs HTTP), whether to keep `available_balance`, `invested_balance`, `secret_question`, `secret_answer`, which use cases to implement, language of documentation.
128
129**Intent:** I had just pasted the course rubric into `README.md` and removed the
130half-finished frontend, so that the AI would plan against the actual phase
131requirements and against a backend-only prototype.
132
133**Prompt (student, excerpt):**
134> Read it now, just filled it in, also deleted the frontend we don' need that
135
136**Response (AI, summarised):**
137- Re-read `docs/README.md`, which now contained the course phase rubric (P0–P9).
138- Explained the constraints each phase places on AI usage (P0 forbids AI; P1–P4 allow AI only to improve the student's own initial model and require an AIUsage log).
139- Listed the deliverables per phase and asked the remaining questions.
140
141---
142
143### Session 2 — 2026-08-06 / 2026-08-07
144
145**Intent:** re-read the rubric from scratch after the gap, reduce it to a
146checklist of what each phase actually has to produce, confirm TerraER runs on
147this machine.
148
149**Prompt (student, excerpt):**
150> We will start from the begining in some ways, please read first docs/insturctions.md
151> The goal is to finish with milestone 0 so I can publish it […]
152> Read the Instuctions.md like I said and delete everything that you think we don't need
153> and for each phrase tell me what do I need to submit and how to do it. and can you also
154> do the thing with TerraER3.11.jar?
155
156**Response (AI, summarised):**
157- Read the phase rubric and condensed `Instructions.md` to a per-phase checklist
158 of deliverables, keeping every requirement and every load-bearing filename.
159- Confirmed TerraER 3.11 runs on the installed OpenJDK 21 and launched it.
160- Listed, per phase, what has to be submitted and how; flagged that P0's
161 deliverables are wiki pages, that the EPRMS proposal is a prerequisite.
162- Flagged `image.png` (a TradingView screenshot) as material the project has no
163 licence to publish.
164
165**Intent:** produce the artefacts still missing for P1–P4 — above all the TerraER
166diagram — while keeping P0 for myself, since AI use is forbidden there.
167
168**Prompt (student, excerpt):**
169> […] do all of the other Phases till m0.
170> opis.md It's p0 so I will take care of that. Delete anything that we don't need,
171> make all of the phases and terra diagram if you can, and delete anything
172> that we don't need and make a documentation about how to start it.
173
174**Intent:** ask for a review pass over the schema and the Go code rather than
175only documentation, on the grounds that a prototype I have to defend in person
176should not have known defects in it.
177
178**Prompt (student, excerpt, follow-up):**
179> Also fix some database things or golang things if you think we can do it better,
180
181**Response (AI, summarised) — the part relevant to P1:**
182- Read `ep-diagram.md` (the student's own initial model) and the existing
183 `schema_creation.sql`.
184- Reverse-engineered TerraER's file format from the distributed jar to learn the
185 element names it stores figures under (`ent`, `rel`, `atr`, `atrchave`,
186 `atrderivado`, `llabelUm`, `llabelMuitos`, `llabelDoubleUm`,
187 `llabelDoubleMuitos`, …).
188- Generated `ERModel_v01.xml` and `ERModel_v01.png` as described above, in Chen
189 notation: 8 entity sets, 10 relationships, 57 attributes, cardinality labels
190 on every relationship line and double lines for total participation.
191- Proposed the three changes to the initial model recorded in the model history.
192- Rewrote [ERModel](ERModel.md) with the per-entity documentation, candidate-key
193 justifications and attribute types the phase template requires.
194
195---
196
197> **Student action required.** Two things, in this order:
198>
199> 1. Open `ERModel_v01.xml` in TerraER, read the whole diagram, and change what
200> you disagree with. Save the result as `ERModel_v02.xml` with a matching PNG
201> and add a history line. The phase rules require that the model be yours;
202> the generated v01 is a starting point to review and take over, not an
203> answer to submit unread.
204> 2. Verify that this log matches your recollection and append the full text of
205> any further prompts. The complete transcript is in your Claude Code session
206> history.
207
208---
209
210### Session 3 — 2026-09-16
211
212**Intent:** describe a concrete gap I'd found in the sell-order design — the
213model had no way to represent crypto committed to an order that had not yet
214settled — and have the AI trace the fix through every phase rather than patch
215`trade.go` alone.
216
217**Prompt (student, excerpt):**
218> Suppose the user owns 2 BTC and wants to sell 0.5 BTC at market price. […] You
219> currently have Holds: quantity, avg_price but you don't have a concept of
220> reserved/committed crypto. For cash, you already explicitly distinguish
221> available_balance, invested_balance. For crypto, you probably want the
222> equivalent: quantity, reserved_quantity, avg_price. […] Can you please start
223> from Phase 1 and modify everything to make it work like it should, modify
224> the AIUsage.md in every Phase, and can you make the ConceptualModel a little
225> bit more put together not that much spread out.
226
227**Response (AI, summarised):**
228- Read every phase document and the live `server/trade.go`, `portfolio.go` and
229 `schema_creation.sql` to confirm the actual gap: the sell path checked
230 `holdings.quantity` directly, with nothing recording that part of a position
231 was already promised to another order.
232- Added `reserved_quantity` to `Holds` (P1), `holdings` (P2), the sell scenario
233 SQL (P3), and `trade.go`/`portfolio.go`/`schema_creation.sql` (P4) — one
234 attribute threaded consistently through all four phases rather than a
235 patch confined to the Go code.
236- Changed the sell path to lock the holding row, compute
237 `available = quantity − reserved_quantity`, reserve (`reserved_quantity +=
238 qty`) and then settle (`quantity -= qty; reserved_quantity -= qty`) inside
239 the same transaction — keeping the existing all-or-nothing guarantee rather
240 than splitting into two commits, which would leave an unresolvable `open`
241 order if the process died in between and there is no cancel use case to
242 recover it.
243- Made both buy and sell orders genuinely pass through `status='open'` before
244 `status='executed'`, instead of being inserted as `'executed'` directly, so
245 the lifecycle the ER model already claimed for `Orders` is real.
246- Added a `CHECK (reserved_quantity >= 0 AND reserved_quantity <= quantity)`
247 constraint and exposed `reserved_quantity`/`available_quantity` on
248 `v_portfolio` and in the CLI's portfolio table, so the new field is
249 observable rather than a column nobody can see.
250- Restructured this phase's [ERModel](ERModel.md) into short rationale + a
251 compact attribute table per entity/relationship, per the "more put together,
252 not that much spread out" request — no content was removed, only
253 reformatted.
254- Verified the fix against the live database (`bp_database` on `localhost:5433`):
255 ran two concurrent `sell 1.0 BTC` attempts against a holding with only 1.5
256 BTC available — exactly one succeeded, the other correctly reported
257 insufficient holding — and ran the reserve/settle sequence by hand in `psql`
258 to show `reserved_quantity` at 0.5 mid-transaction. Both are recorded in
259 [UseCase0005Implementation](../P4-Prototype/UseCase0005Implementation.md).
260
261**What I decided:** to keep reserve and settle inside one transaction rather
262than two (see the AI's reasoning above — I agreed with it, since a stuck
263`open` order with no cancel command would be a worse bug than the one being
264fixed).
265
266**Follow-up, same day:** I asked for `ERModel_v03.xml`/`.png` after all,
267having noticed the PNG still showed v02 with no `reserved_quantity` on it, and
268asked at the same time for the diagram to be a little more compact — it had a
269lot of empty canvas in the middle. The AI drove TerraER's own classes directly
270(load → clone the `quantity` attribute → relabel it → add its connecting line
271→ pull every cluster toward the centroid → save → render), described in full
272under [Diagram](#diagram) above, rather than hand-editing the XML or asking me
273to do it in the GUI. I reviewed the rendered PNG before accepting it.
274
275**Second follow-up, same day:** the first `ERModel_v03.png` rendered with a
276black background instead of white, unlike v01/v02. Cause: TerraER's
277`ImageOutputFormat` defaults to an ARGB image and paints its background with
278zero alpha (transparent), not opaque white; whatever displayed the PNG then
279flattened that transparency onto black instead of white. Fixed by exporting
280through the same `ImageOutputFormat.toImage(...)` call for figure geometry,
281but compositing its result onto an explicitly white-filled opaque `RGB` image
282before saving, rather than trusting the library's own (transparent) output.
283Verified the fix by reading back the corner pixel of the written PNG as pure
284white `(255,255,255)`, matching `ERModel_v02.png`.
285
286
287### Session 4 — 2026-09-24 (Claude Opus 5.5): v04 after P7
288
289**Prompts (student, verbatim):**
290> But this order_events is added after Phase 7 right? can we add that too?
291
292> can you make the ERmodel again with TerraER file to update it? and tell that after P7 we added this
293
294> Make it with a white background like earier versions
295
296**Response (AI, summarised):**
297
298- Explained that the P7 changes must also appear in P1 and P2, since both must describe the
299 current data structure.
300- Built `ERModel_v04.xml` in TerraER's own file format by taking `ERModel_v03.xml` unchanged
301 and appending the new elements with the same XML structure TerraER uses:
302 - the attribute `reserved_balance` on `Users`;
303 - the attribute `filled_quantity` on `Orders`;
304 - the relationships `FillsBuy` and `FillsSell` (Orders 1 : N MarketTrades, partial);
305 - the entity set `OrderEvents` (key `id`, `event_type`, `quantity`, `price`,
306 `status_after`, `created_at`) with `Logs` (Orders 1 : N OrderEvents, total on
307 OrderEvents).
308- Rendered `ERModel_v04.png` with TerraER 3.14's own drawing classes (loading the `.xml`
309 exactly as TerraER does and using its image export), on a white background and trimmed like
310 the earlier versions.
311- Updated [ERModel](ERModel.md) (title v.04, new attribute rows, the `OrderEvents` section, the
312 three relationships, and a v04 history entry stating these were added after P7).
313
314**What I decided:** to add the P7 structure to the ER model. The new elements are placed
315automatically, so the layout can be tidied by hand in TerraER.
316
317
318### Session 5 — 2026-09-29 (Claude Opus 5.5): v05, correction after review
319
320**Prompt (student, excerpt):** I pasted the reviewer's comments that `Holds` and `Contains`
321are implemented differently in the database than in the model, with the suggestion to
322"correct only the Phase 1 model so that it matches Phase 2", and asked:
323
324> Can you take a look at this stuff? and fix them, but don't push anything just make the
325> changes requested and try to explain it to me to see if you understand the problem with
326> the solution
327
328**Response (AI, summarised):**
329
330- Explained that an M:N relationship has no identifier of its own, while `holdings` and
331 `watchlist_items` each have an `id` primary key, so the database treats them as entity
332 sets.
333- Changed the model to v05. `Holdings` (`id`, `quantity`, `reserved_quantity`, derived
334 `avg_price`, `created_at`, `updated_at`) and `WatchlistItems` (`id`, `added_at`) became
335 entity sets, with the 1:N relationships `Holds`, `PositionIn`, `Contains` and `Lists`,
336 each total on the new entity's side. The old relationship keys are now stated as
337 uniqueness rules. The key descriptions no longer name foreign-key columns.
338- Generated `ERModel_v05.xml` / `ERModel_v05.png` from scratch with TerraER 3.11's own figure
339 classes and writer (adapted from the v01 generator), on a grid, with no overlapping
340 attributes. It was verified by reading the file back with TerraER's reader (184 figures)
341 and by inspecting the rendered PNG.
342- Updated [ERModel](ERModel.md) (v05 sections and history entry).
343
344**What I decided:** to follow the reviewer's advice and change the model rather than the
345database, since every later phase already uses the database as it is.
Note: See TracBrowser for help on using the repository browser.