Getting Physara's Analyzers Ready for Real Use
There is a point in a build where the feature works, the screen looks good, the demo goes through, and it becomes very tempting to move on.
I was basically there with two of the biggest AI features in Physara.
The Physique Analyzer worked.
The Meal Analyzer worked.
I had already spent a lot of time on both of them.
But before starting the next major system, Training Intelligence, I wanted to do one more thing.
I wanted to try to break them.
Not redesign them.
Not add more features.
Just go through the real production paths and ask whether I would actually trust them if people started using Physara tomorrow.
That turned into a much bigger hardening pass than I expected.
And it reminded me again that "working" is not the same thing as "ready."
The Physique Analyzer Had Two Different Problems
The first problem was reliability.
The second problem was trust.
Those are related, but they are not the same.
On the reliability side, I had already rebuilt the analyzer around a durable job system and a server-side media pipeline.
A user can upload photos, start one analysis, leave the screen, come back, and reconnect to the same job.
The photos are normalized before inference.
The backend tracks the job state.
Temporary failures can retry.
Duplicate requests should not create duplicate analyses.
That architecture solved a lot.
But production testing still exposed things that only show up when all the pieces are connected.
One of them was a database permission issue.
The job creation RPC existed.
The service role was supposed to call it.
But the live permissions were wrong, so a request that looked completely valid from the app could still fail before the analysis job was created.
That was not an AI problem.
It was not a frontend problem.
It was a Postgres ACL problem.
The fix was small, but the only reason I found it was because I was testing the actual live path instead of assuming the architecture was enough.
Then the Analyzer Was Reliable, but the Advice Still Bothered Me
Once the feature was completing consistently, I started paying more attention to the output itself.
That exposed a different issue.
The analyzer could produce something that was technically valid but still did not feel like guidance I would want to follow.
For example, an area could be clearly developed and still somehow become a priority.
A result could say one thing was strong, then give programming that made it sound like that same area needed specialization.
Some of the recommendations were too specific.
Some sections were too long.
And the system did not have a strong enough concept of "nothing needs special attention."
That last one mattered to me.
If someone has a balanced physique, Physara should be able to say that.
It should not invent a weakness because the product expects every analysis to end with one.
So I changed the model underneath the result.
Instead of treating every assessed area like it needed a score and ranking, the analyzer now works with clearer states:
- standout strength
- balanced
- development priority
- limited evidence
- not assessable
That sounds simple, but it changed a lot.
A body part can now just be balanced.
A photo can be usable overall while still not supporting a conclusion about one specific area.
A non-neutral pose can prevent a symmetry judgment without invalidating the rest of the scan.
Clothing can block a region completely.
And if there is no real development priority, the answer can simply be:
No specialization needed.
I Removed Fake Precision
One of the things I have become more careful about with AI products is numbers that look scientific but are really just presentation.
The earlier Physique Analyzer had development scores that looked like a 0 to 10 measurement.
The problem was that the photos did not support that level of precision.
The scores were derived from internal confidence and target logic, not an actual measurable physical quantity.
So I removed them.
Same thing with progress language.
If two scans are not comparable enough, Physara should not say someone "improved."
If an area was covered in one photo and visible in another, that is not a valid before-and-after comparison.
If the pose changed enough to affect shape, the system needs to be careful.
I would rather have the app say it does not have enough comparable evidence than show a clean-looking number that does not really mean anything.
Training Advice Needed a Boundary Too
The AI is useful for visual observation.
I did not want it inventing the training program.
That responsibility now sits with Physara's own training rules.
The model can say what it sees.
Physara decides what that means for training.
That separation became stricter during this pass.
The prompt now explicitly avoids generating exercises, sets, reps, RIR, rest periods, weekly volume, or a full program.
The training side uses structured protocols instead.
Those protocols define things like:
- movement patterns
- exercise options
- rep ranges
- RIR
- rest
- weekly set ranges
- frequency
- progression
- technique guidance
That also let me fix another problem I had with the old output.
It would sometimes act like one exact exercise was mandatory.
I do not want Physara saying someone has to do a Smith machine calf raise because one model response happened to pick it.
The program should understand the training target first, then choose from compatible exercises based on equipment and preferences.
The Result Screen Needed the Same Treatment
After fixing the logic, the screen itself still felt too much like an AI report.
There was a giant paragraph at the top.
Then a lot of detail.
Then more detail.
Then training guidance.
Then limitations.
Then more explanation.
The information was correct, but the hierarchy was wrong.
So I rebuilt the presentation around the decisions a user actually cares about.
The top now answers:
What stands out?
What looks balanced?
Is there a development focus?
What views were actually assessed?
Then the user can go deeper.
The body map shows the evidence visually.
Strengths and balanced areas have their own sections.
Training guidance is compact by default and expands when someone wants the details.
Limitations are grouped instead of repeating the same sentence for every body part.
The reassessment section separates optional progress photos from a formal physique reassessment.
The end result is much calmer.
One of the final real scans returned:
- standout: mid chest and shoulders
- balanced: upper chest, arms, abdominal development, lateral delts
- development focus: none needed
That was actually one of my favorite test results because the system did not force a weakness.
It just said what it could support.
Then I Did the Same Thing to the Meal Analyzer
Once the Physique Analyzer was in a place I trusted, I did not want to immediately jump into the next feature.
The Meal Analyzer had been around longer, and it had already taught me a lot about production debugging.
I decided to give it the same kind of release pass.
The instruction was basically:
Do not redesign this.
Trace the real path.
Find the things that can corrupt data, duplicate work, lose edits, or create misleading nutrition.
That pass found several problems that were much more serious than anything visual.
Duplicate Saves Were Possible
A user can analyze a meal, edit it, upload the photo, and save it.
That sounds like one action.
Behind the scenes it crosses multiple boundaries.
If the upload succeeds but the save response gets lost, the client can retry.
If two taps happen close together, two requests can race.
If the UI does not know whether the first request committed, it is easy to accidentally create duplicate meals.
The fix was to give the draft a stable save identity.
Retries use the same meal identity.
The database serializes those retries and checks whether that meal already exists before creating another one.
One intentional save should create one meal.
That rule now lives in the persistence layer instead of depending on the button behaving perfectly.
Saved Nutrition Totals Needed a Real Source of Truth
Another problem was that the database could accept meal totals from the client even if those totals did not match the individual food items.
That is dangerous because the item list and the saved calories could disagree.
Now the database derives the totals from the actual saved items.
The client can still edit a serving.
The user can still correct a food.
But when the meal is saved, the database recalculates:
- calories
- protein
- carbs
- fat
from the item quantities and per-100-gram nutrition values.
That gives the saved meal one source of truth.
The hardening pass also added bounds around nutrition values and serving quantities so malformed or impossible values do not quietly enter the diary.
Reopening a Meal Exposed a Serving Bug
One of the easier bugs to miss was household serving quantities.
A meal could look correct when it was first analyzed and saved.
Then when it was reopened, some quantities could reset to one serving.
That means the original screen could say one thing while History later showed something different.
That is exactly the kind of bug I care about in a tracking app.
If the user fixes a portion and saves it, reopening that meal should show the exact same final state.
The persistence and editor flow now preserve those quantities correctly.
Then I Hit the Same Edge Runtime Problem Again
This part was almost funny because I had already seen a version of it with the Physique Analyzer.
The Meal Analyzer still had an image validation path that ended in:
createImageBitmap()
That is a browser-style decoder.
It works locally.
It is not something I want to depend on inside Supabase Edge for production image validation.
In the real Edge environment, that call could reject a valid photo before Gemini ever saw it.
Instead of adding another decoder workaround, I replaced that check with deterministic file validation.
The Meal Analyzer now validates the actual structure of supported image formats:
- JPEG
- PNG
- WebP
It checks the file signature.
It checks dimensions.
It rejects truncated or spoofed payloads.
It keeps the protection against extreme image dimensions.
It does not depend on browser image decoding.
This is lighter than the full Physique Media Pipeline because the two features have different needs, but the rule is the same:
Do not trust a filename or MIME type alone.
And do not let an unreliable runtime decoder become the gatekeeper for every valid photo.
Lost AI Responses Were Still Wasting Work
The other major Meal Analyzer issue was inference replay.
Imagine Gemini successfully analyzes the meal.
Then the mobile connection drops before the response reaches the phone.
From the user's point of view, the request failed.
So they tap Try Again.
Without durable request identity, the backend has no way to know the first analysis already happened.
Gemini gets called again.
That wastes provider work and creates more ways for two responses to disagree.
I fixed that with a durable request/replay layer.
Each photo analysis now gets a stable analysis ID.
The server combines:
- verified user
- analysis ID
- image SHA-256
and claims the request before calling Gemini.
The request can be:
- processing
- completed
- retryable failure
- terminal failure
If the same request comes back after the result has already been stored, the server replays the stored response.
If another request arrives while the first is still running, it does not start a second Gemini call.
If the same ID is reused with a different image, it is rejected.
Retryable failures are bounded.
And if the replay database itself is unavailable, the function fails instead of calling Gemini without tracking it.
That is the kind of behavior I want around paid AI work.
The Final Test Was Production
I did not want the last step to be another unit test.
Both analyzers went through full automated validation, but then I deployed them and used them.
The final Meal Analyzer pass had:
- 1,230 frontend tests passing
- 258 Meal frontend tests passing
- 237 Edge Function tests passing
- SQL regression tests passing
- typecheck, lint, build, and diff checks passing
Then I ran two real meal analyses in production.
Both completed.
Both Gemini calls succeeded.
Both photo uploads succeeded.
Both meals saved successfully.
There were two Gemini starts for two real analyses, which is what I wanted to see.
No duplicate inference.
No duplicate save.
I also reopened the meals and verified the saved state.
The Physique Analyzer went through the same kind of production smoke testing before I froze it.
What I Learned From This Pass
The biggest thing is that a release hardening pass should be different from normal feature work.
The goal is not:
What else can I add?
The goal is:
What can still go wrong with what I already built?
That changes the questions.
Can the same button create two records?
What happens if the response disappears after the database already committed?
What happens if the user leaves the screen?
Does a saved edit survive reopening?
Can the AI output something the database should reject?
Can a valid phone photo fail because the server runtime does not support the decoder?
Can two workers believe they both own the same request?
Does the product know when not to make a recommendation?
Those are not the things you usually see in a demo.
But they are the things that decide whether the product feels dependable after someone uses it every day.
I Finally Froze Both Analyzers
After the production tests passed, I merged the final Physique Analyzer work into main.
Then I merged the Meal Analyzer reliability work.
That gave me a clean checkpoint before starting the next major system.
I could keep polishing either analyzer forever.
There will always be another edge case, another copy change, another piece of UI I could revisit.
But there is also a point where the right move is to stop touching something that is working well, document the remaining non-blockers, and move forward.
For now:
Physique Analyzer is frozen.
Meal Analyzer is frozen.
The next build is Training Intelligence.
And I am intentionally not starting that one with code.