Files
ucvl-home-vision/docs/superpowers/plans/2026-10-25-p15-power.md
T

43 lines
6.6 KiB
Markdown

# P15 managed asset implementation plan
Goal: existing household/device catalog gains a managed laptop with RUN/OP and phone tabs 位置 / 家庭 / 设备 / 沟通 / 我的. No generic remote shell.
Architecture: existing `devices` JSON objects hold the asset and metadata. New `device_power.py` holds runtime state, commands and explicit phone grants. A least-privilege Windows service polls HTTPS, accepts only signed shutdown commands, persists consumed IDs before execution and never listens on a network port. Server calls a fixed, forced-command SSH bridge on MT2500 for WOL. No credentials in catalog/UI.
User specification: delegated task from 日常操作, 2026-10-25. User approved existing smart-home integration and autonomous implementation. Retain existing pairing and consent; no automatic grants from a location-sharing token. P15 is the active development host, so real shutdown is deferred until work has ended.
## Interfaces (binding for all components)
### Phone API
POST `/api/phone/home` with existing phone pairing Bearer token. Actions:
- `catalog`: response `{households:[{id,name,kind}],devices:[{id,source,name,deviceType,familyId,familyName,siteName,power:null|PowerState}],activeId:pairedFamilyId}`. Only explicitly granted devices; paired household name remains visible. Empty device list directs user to administrator; no implied control permission.
- `command`: `{action,deviceId,op:1|2,requestId:32 lowercase hex,confirm:true}`; response `{power:PowerState}`. Shutdown requires explicit UI confirmation. UUID without hyphens creates requestId.
- `status`: `{action,deviceId}` returns `{power:PowerState}`.
PowerState = `{run:0|1|null,op:0|1|2,state:'online'|'offline'|'unknown',checkedAt:unixSeconds,canStart:boolean,canShutdown:boolean,managed:boolean,last:{id,op,status,result,at}|null}`.
`run=0` means confirmed unreachable through an available relay, not proof of physical shutdown. Unknown disables both actions. Command buttons follow `canStart/canShutdown`; status also validates server-side. OP always resets on terminal outcome/restart. RUN is sampled, never inferred from an OP request.
### Web API
GET `/api/device-power?id=...` authenticated in current household returns `{power:PowerState,grants:[{personId,view,control}],candidates:[{personId,name}],canManage:boolean}`. Only admins/managers get grants/candidates; ordinary authorized site readers see state only.
POST `/api/device-power` current household, `{action:'command',deviceId,op,requestId,confirm:true}` or `{action:'grants',deviceId,grants:[{personId,view:true,control:boolean}]}`. Commands only manager or explicit per-person grant. Grant save only current household manager, admin may select active paired people across households; family managers only own-family members. Existing paired token alone grants no device access. Grants follow stable person identity but require valid current member pairing and expiry on every request.
### Windows agent protocol
POST `/api/device-power/agent` HTTPS `https://home.ucvl.cn`, <=16KiB.
Envelope `{payload:base64(UTF8 JSON),signature:hex HMAC-SHA256(secretBytes,payload ASCII)}`. Config secret is base64 random32 bytes. Payload `{deviceId,bootId:32hex,nonce:32hex,at:unixSeconds,ready:boolean,ack:null|{id,status:'scheduled'|'failed'|'blocked',result:string}}`. At within60sec. Server nonce cache rejects duplicates. Poll every10sec. BootId fresh each process start.
Response same envelope, payload `{nonce:<request nonce>,at:unixSeconds,command:null|{id:32hex,op:2,bootId,expires:unixSeconds}}`. Verify HMAC in constant time, matching nonce/bootId, timestamp window60sec and command expiry before action. No arbitrary command/path/args accepted. Server delivers only once per command; no reissue after timeout/restart. Agent persists consumed ID before shutdown. Acknowledge 'scheduled' before calling graceful shutdown (no forced application termination). Failed ACK prevents shutdown. Agent retains seen IDs bounded, never replays on restart. Agent service can run `--check`/`--self-test` without shutdown; installation must be explicit. `ready=false` while maintenance/installation safety lock exists. LocalService or dedicated service identity granted only shutdown privilege; no admin shell. Do not change RDP/Tailscale.
## Tasks and ownership
- [x] Backend (root): failing tests for RUN unknown/stale/online/offline, OP success/failure/timeouts/restart, duplicate requests, boot changes, replay/HMAC, household and phone grants. Implement module, routes, worker, metadata preservation. Existing tables reused. Bridge script only fixed status/wake operations.
- [x] Android: update native layout/controller to five pages, old behavior keyed constants; new PhoneHomePanel calls API above. Bootstrap native look, household selection -> matching devices, explicit shutdown confirmation, no stale callbacks across pair/tab changes. Add isolated native fixture tests/preview modes; no real device calls in tests.
- [x] Web: `DevicePowerPanel.vue` + pure model .mjs tests, integrate existing DeviceCollection and device details. Show only name/location/state/buttons/time/result for managed computer card. API authorization errors explicit, no cached permission fallback. Grant editor uses candidates from API. No credential fields.
- [x] Windows: dedicated agent under `windows-power/`, .NET Framework compilation supported, self-tests for signatures/nonces/boot/expiry/duplicate journal/only OP2, install/uninstall docs/scripts; no machine mutations until root reviews.
- [x] Integrate/review: all tests, signed APKs, preview 320dp and135% font, existing location/voice regression. Resolve test fixture old three-tab assumptions.
- [ ] Production: inspect existing records first, online DB backup, idempotently create 优创青岛办 and P15, relay topology is parallel under172.16.1.1, not under MT2500 LAN. P15 LAN172.16.1.44/24 gateway172.16.1.1, TS100.82.249.65, MACs8C:3B:4A:17:BC:C4/9C:2D:CD:03:D9:24. MT2500 WAN172.16.1.163 TS100.66.203.38. Grant only verified requested owner identity, otherwise leave explicit admin grant UI. Restricted SSH key and pinned host key server-side. Agent installation on P15 with maintenance lock; dry-run validates chain without shutdown during work.
- [ ] Release: V0.1.56/code12, existing signing/backup/deploy workflow, metadata/hash/download checks. State exactly what was physically tested. Real shutdown deferred because active host.
Review focus: stale state must be unknown; revoked or moved-device grants fail closed; timeout before delivery cannot execute later; service restart cannot replay; new permissions must not attach to everyone holding a location token. No formal production objects hardcoded into fixture data.