docs: AI handoff for FCP-side phase 2 (clip repush button)

This commit is contained in:
jetson committed 2026-10-20 11:06:34 +07:00
1 parent 540e1b5dcd
commit ca0583b734
1 file changed
+170
+170
View File
@@ -0,0 +1,170 @@
# AI Handoff — FCP → Recounter Clip Repush (Phase 2)
**Audience:** the next AI agent (or dev) implementing the FCP side.
**Status:** Phase 1 (recounter receiver) is DONE and committed. Phase 2 (FCP
button + push) is NOT started.
**Last updated:** 2026-10-20.
---
## 1. What exists today (verified, tested, live-smoked)
### feedmill_recounter (port 9050) — receiver, DONE
`POST /api/analyze` (app.py, commit `540e1b5`):
```bash
curl -X POST http://localhost:9050/api/analyze \
-H "X-API-Key: $RECOUNTER_API_KEY" \
-F "video=@clip.mp4" \
-F "fcp_mode=C" \
-F "zone_mode=dynamic" # optional: dynamic|freeze|fixed
-F "zone_preset=..." # optional, only with zone_mode=fixed
-F "output_height=720" # optional
```
| Outcome | Code | Body |
|---|---|---|
| accepted | **202** | `{"job_id":"job-<hex8>","status_url":"/api/jobs/<id>"}` |
| GPU busy (any PENDING/RUNNING job) | **409** | `{"error":"busy","active":[{"job_id","status"}]}` |
| bad/missing key when env set | **401** | `{"error":"unauthorized"}` |
| no video / bad ext / unknown fcp_mode / no models | **400** | `{"error":"<msg>"}` (always JSON) |
- **Auth:** header `X-API-Key` is checked **iff** env `RECOUNTER_API_KEY` is
set on the recounter process. Unset = open (current default in
`start_dashboard.sh` — set it before exposing beyond LAN).
- **`fcp_mode`** = FCP model-mode letter `A|B|C|D`, mapped server-side via
recounter `config.yaml → fcp.mode_models` (mirrors FCP `models.modes`;
`C` = FCP's current `active_mode`). Explicit `model_stems` (multipart,
repeatable) + `format_<stem>`/`filter_<stem>` still work as override.
- **Busy policy:** 409 not a queue. Caller retries. One job at a time =
one GPU.
- **Results:** poll `status_url` → `{"status":"COMPLETED","results":[
{"loading_count","unloading_count","net_count","box_*",
"output_path",...}]}`. Download: `GET /download/<job_id>/<filename>`.
- Save path is shared with the UI `/upload` (collision-safe filename,
auto 720p transcode, fixed-zone resolution) via `_save_and_queue()`.
Tests: `tests/test_api_analyze.py` — 14 cases; full suite **191 passed**.
### FCP (karung_counter_semarang) — sender, NOT STARTED
Everything needed is already in FCP (researched, file:line verified):
1. **Clip creation** — `POST /api/batch-clip/<date>/<batch_number>` (no
body; office-gated). Async `_CLIP_JOBS` registry
(`counter_dashboard.py:1629-1630`, lock at 1630). Status endpoint
`.../status` → `{"status":"running"|"ready"|"error", ...}`.
Finished clip: `path` field = **abs tmp mp4**, ≤1 h retention
(`/opt/jetson-counter/tmp/clip_<uuid>.mp4`).
Download endpoint: `GET .../file`.
2. **Active model mode letter** — `GET /api/model-modes` →
`{"active":"C", ...}` (live read of `config.yaml models.active_mode`);
**better source:** the batch's own row `batches.model_mode` (the mode
that actually counted that batch) for history-page buttons.
3. **Clip file availability gate:** only push while clip job
`status == "ready"`; clip tmp file may be gone after 1 h → re-POST
`/api/batch-clip/...` (it re-renders).
4. **HTTP client:** FCP has **no `requests`** — use stdlib
`urllib.request` (repo idiom; `src/clip.py` uses it).
5. **Ports:** operator 5000, office 5721, external proxy 80/443 →
`api.` host or `/api/` → 5721. New endpoints must be gated with
`is_office_request()` like the four existing clip routes.
6. **Templates gotcha:** `templates/history.html` `renderClipButtons()`
(~L548-549) rewrites `innerHTML` of **any**
`#batchesTableBody button[data-batch]`. A new button MUST use a
different data attribute (e.g. `data-recount`) and its own
`apply*State` fn — do not reuse `data-batch`.
## 2. Environment / URLs (given by operator)
```
MOTIONEYE_URL=http://192.168.192.253:8765
MOTIONEYE_CAMERA_ID=2
```
Suggested new FCP env (`.env`, read like the other motioneye vars):
```
RECOUNTER_URL=http://localhost:9050 # http://192.168.192.96:9050 from other hosts
RECOUNTER_API_KEY= # must equal recounter's RECOUNTER_API_KEY
```
Jetson IP in `external_proxy.py` is `192.168.192.96`; recounter web UI is
`http://<host>:9050`.
## 3. Design for Phase 2 (recommended, ~sizable but follows existing patterns)
**Endpoint:** `POST /api/batch-recount/<date>/<batch_number>` (office-only)
Flow inside handler (mirror the clip worker pattern):
1. Look up batch in DB for its window + `model_mode` (fallback:
`GET /api/model-modes` active letter).
2. Ensure clip ready: check `_CLIP_JOBS` status; if none/error, chain the
existing clip POST first (reuse `_render_clip_job` logic or re-POST
internally) — **or** simplest v1: require clip `ready`, else 409 with
`"clip dulu"` message.
3. Read clip tmp file → multipart POST to `RECOUNTER_URL/api/analyze`
via `urllib` (encode multipart manually or read
`src/clip.py`/`predict.py` for an existing encoder — if none, a small
`MultipartEncoder`-style helper with stdlib `uuid` boundary is ~30
lines).
4. Send `fcp_mode=<letter>`, header `X-API-Key` if `RECOUNTER_API_KEY`
env set.
5. Store in a new `_RECOUNT_JOBS` registry (copy `_CLIP_JOBS` structure):
`{status: running|ready|error, job_id, recounter_url, error, elapsed}`.
6. `GET /api/batch-recount/<date>/<n>/status` polls recounter
`/api/jobs/<job_id>` server-side (avoids CORS) and returns combined
`{recounter_status, counts...}`.
**UI:** `templates/history.html` — sibling button in the same `<td>` as
Klip, `data-recount` attr, own dispatcher `onRecountClick()`, own state
map, label flow: `Hitung ulang` → `Mengirim…` / `Memproses… N%` →
`Selesai — lihat` (link to `RECOUNTER_URL/status/job-...`) / `Gagal`.
**409 handling:** show a toast/`alert("Sedang ada proses — coba lagi")`
and retry poll after a few seconds; do not auto-loop >3 retries.
**Safety rules:**
- Never read/write recounter paths from FCP (or vice versa) — HTTP only.
- Office-only gating on both new endpoints.
- No secrets in code; only `.env`.
## 4. Verification checklist for Phase 2
- [ ] FCP: POST recount returns 202/202-analogue without touching operator port
- [ ] Recounter job created and reaches COMPLETED with counts for a real clip
- [ ] 401 surfaces as friendly "API key salah" error, not traceback
- [ ] 409 surfaces as retry message, button not stuck busy
- [ ] Clip missing/expired → clear error, button returns to idle
- [ ] `renderClipButtons()` does NOT clobber the new button (grep innerHTML renders)
- [ ] Existing clip button still works (regression)
- [ ] FCP: `pytest` green; recounter: `python -m pytest tests/ -q` → 191 pass
## 5. Repo facts
| Fact | Value |
|---|---|
| FCP root | `/home/jetson/feedmill_semarang_project/karung_counter_semarang` |
| Recounter root | `/home/jetson/feedmill_semarang_project/feedmill_recounter` |
| Recounter branch | `main`; phase 1 commits: `540e1b5` (endpoint), `97507f8` (ERD/CHANGELOG) |
| Recounter suite | `python -m pytest tests/ -q` → 191 passed |
| Recounter mode map | `config.yaml → fcp.mode_models` (A/B/C/D) |
| FCP clip endpoints | `counter_dashboard.py:1736/1760/1854/1881` |
| FCP mode endpoint | `GET /api/model-modes` (fresh-read active_mode) |
| Mode → engines detail | FCP `config.yaml models.modes` (C = combined v4-best + box model) |
**Model stem parity confirmed:** recounter `models/` has the same weight
stems FCP modes reference (`v4-best`, `truck-detector`, `best`,
`yolo11n-bbox-100ep-sack+box-20260909-best`).
## 6. Known limitations / deferred
- Recounter has **no queue** — 409 only. If FCP needs reliable retry,
Phase 2 should implement backoff, not ask for a recounter queue.
- `RECOUNTER_API_KEY` unset by default — recommend operator sets
identical value in FCP `.env` and recounter `.env` before LAN exposure.
- Recounter config `fcp.mode_models` must be kept in sync manually if FCP
adds mode E/F with new engines (no cross-repo sync by design).
- `tests/test_api_analyze.py` stubs `_save_and_queue`; real save path
smoke-tested manually with a 2 s generated mp4 (202 → COMPLETED).