Skip to content

fix(harmonyos): clamp the open foreground probe's hdc read to the request deadline - #5055

Merged
thymikee merged 8 commits into
callstack:mainfrom
strzhao:forge/open-probe-deadline-clamp
Nov 24, 2026
Merged

thymikee merged 8 commits into
callstack:mainfrom
strzhao:forge/open-probe-deadline-clamp

Conversation

@strzhao

@strzhao strzhao commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Follow-up to #4860 (answered yes): the pre-dispatch already-foreground probe read the device on a fixed 15s hdc timeout, so a hung aa dump -l could hold the dispatch past the request deadline the #4293 poll already respects. readForegroundBundle now receives the request deadline beside the signal, and the probe clamps its read to the remaining budget (Math.min against 15s); a spent deadline skips the device. A deadline or cancel mid-read keeps the advisory outcome: the probe claims nothing, no new error code or CLI change. The post-open poll is untouched.

Before / after

open <bundle>, 2s of request budget left, hung foreground read (unit test, fake timers):

before: dispatch delayed ~15s — the probe held its fixed 15s timeout past the request deadline
after:  probe gives up at the 2s clamp and stops holding the dispatch at the deadline

The clamp test pins the read timeout at 2000ms under a +2s deadline (15000 far); a spent deadline reads nothing.

Validation

  • Targeted vitest: platform-harmonyos 141, contracts 807, daemon sessions 1272; integration node 153. pnpm tsc -b and pnpm format zero-diff; reason tables unchanged.
  • check:affected stops at the local layering gate; detach control at base ecb2089867 gives byte-identical violation lists at the polish head — zero PR-introduced violations.
  • Live on the JDY-AL50 via node bin/agent-device.mjs after pnpm build + pnpm clean:daemon (doc docs/evidence/harmonyos-open-probe-clamp-2026-11-24.md): the already-foreground open warned alreadyForeground in 0.84 s; at the polish head 9e23b8a13, replay --timeout 3000 against an hdc wrapper hanging on aa dump -l cut the probe's read at +3.29 s (2.23 s lived, not 15 s), dispatched without waiting, settled daemon_request_timeout/3000, no alreadyForeground claim (wall 3.68 s). An unlocked-screen re-record awaits the next device unlock.

Refs #4860


Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

View guided diff

…uest deadline

The pre-dispatch already-foreground probe ran one `aa dump -l` read on a
fixed 15s timeout, so a hung read could hold the dispatch past a request
deadline the post-open foreground poll (callstack#4293) would have respected. The
probe now receives the request's deadline beside the signal, clamps its
read to the remaining budget (Math.min against the 15s default), skips
the device entirely when a spent deadline leaves no room for one read,
and still claims nothing when the deadline or a cancel cuts the read —
the advisory outcome machinery is unchanged and no reason set grows.

Follow-up to the optional question the callstack#4860 review left open.
@strzhao

strzhao commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

CI status at head 1d6fc063d: every lane this diff owns is green — Repo Guards & Compatibility, Integration Tests, Coverage, Lint/Typecheck/Package, Smoke (web/ios/macos) — so the layering ratchet and the binder/runtime/open-completion lanes confirm the local detach-control result.

The one red lane is the Android emulator smoke job (run 45258852813): live Android emulator fixture E2E asserts helperTransport === 'persistent-session' but the emulator answered 'instrumentation'. Nothing in this diff touches Android, the daemon's Android open path, or the android-helper transport — the five changed files are the HarmonyOS probe (app-state.ts, runtime.ts), the contracts readForegroundBundle wiring, and their tests; Android builds its lifecycle operations directly and never wires readForegroundBundle. The same lane is green on main at the pinned base ecb2089867. I have no rerun rights on this repo — could you rerun that job?

@strzhao

strzhao commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Retriggered the android emulator smoke lane with an empty commit (decd95e8d, no code change): the failing assertion is helper-install metadata (expected persistent-session, got instrumentation, installReason: current) on a surface this PR's five files cannot reach. If the lane fails again identically, I'll dig deeper.

@thymikee

Copy link
Copy Markdown
Member

Thanks for the fix.

The code change looks reasonable at decd95e, but live evidence for the changed HarmonyOS route is still missing. This PR changes a device-facing path: the pre-dispatch aa dump -l read and its timeout under a request deadline. docs/agents/device-verification.md says HarmonyOS has no provisioned CI device and that mocked tests do not replace local hardware evidence. docs/agents/pull-requests.md makes live evidence a merge-ready condition for device-facing changes. The PR body lists only unit and integration runs with a mocked runHarmonyShell, so the wiring from the daemon request deadline to the real runCmd timeout is unproven (app-state.ts:72). The clamp could silently not apply on the production route, for example if the deadline does not reach execution.requestDeadlineAtMs for a plain open. Unit tests that pass the deadline straight into probeHarmonyForegroundBundle cannot show that. Before merge, please attach a live run on a physical HarmonyOS device through node bin/agent-device.mjs, after pnpm build and pnpm clean:daemon, at the last code SHA (decd95e is an empty retrigger commit). First, a plain open <bundle> on an already-foreground target with a normal deadline should still return the alreadyForeground warning with no added probe latency. Second, the same open under a 2-3s client or transport deadline, with the foreground read made slow (an hdc wrapper on PATH that sleeps on aa dump -l, or an unplugged or locked device), should return with no alreadyForeground claim and with the read ending near the deadline instead of at 15s. Please record the commands, device, SHA and elapsed times.

Not blocking, and fine to take or leave: type the readForegroundBundle param in harmonyOpenVerifiedLifecycle (runtime.ts:143) as DirectApplicationLifecycleParams['readForegroundBundle'], as the binder does, so the contract owns the shape; keep one small helper next to hdc.ts for the remaining read budget, since the clamp arithmetic now appears in both app-state.ts:75 and verifyHarmonyOpenForeground; and reword the comment at app-state.ts:62 to say the read never outlives the remaining request budget, because the clamp bounds the read and not the dispatch time.

CI is still running on decd95e, and no check has failed at this head. The earlier Android emulator smoke failure at 1d6fc06 does not overlap this diff: readForegroundBundle is wired only by the HarmonyOS runtime, so other platforms return before the new argument is read. Not verified here: the unit tests were not re-run, the real runCmd timeout was read from exec.ts and not exercised, and the local layering and fallow parity claims were not reproduced. No HarmonyOS device or hdc was available for this review.

The next step is the live HarmonyOS evidence for the tight-deadline open. After that, the change is ready once CI is green.

…line clamp

Scenario a: the already-foreground open answers with the alreadyForeground
warning at 0.7 s with no added probe latency. Scenario b: an hdc wrapper that
sleeps on the probe read cannot hold the dispatch past the admitted request
budget — the probe read dies at its 15 s clamp, the poll read at its 5 s
budget, and the open returns with the advisory unreadable warning and no
alreadyForeground claim. The 2-3 s remaining-deadline variant is not
expressible on the shipped CLI (open's result budget is a fixed 60 s floor and
the pre-probe route spends at most ~10 s), so the clamp below 15 s stays
pinned by the unit suite; the live run is the wiring half of that proof.

Refs callstack#5055
@thymikee

Copy link
Copy Markdown
Member

Thanks for the update and the new evidence doc.

The Part (a) run is now recorded, and that part of the earlier findings is fixed. One required live run is still missing at f42463d.

The evidence doc runs part (b) only at the 15s default, so it cannot show whether the request deadline reaches the probe on a real daemon request (evidence doc, line 50). It also says a budget below 15s cannot be expressed on the shipped CLI. That looks wrong: replay <script.ad> --timeout 3000 gives the request a 3s execution deadline (timeout-policy.ts:159-167), and each replay step re-enters the daemon with the same request id (session-replay-action-runtime.ts:160). An open step should then reach readForegroundBundle with about 3s left. Today only mocked runHarmonyShell tests cover that route. The repo's PR and device-verification docs require live hardware evidence for device-facing HarmonyOS changes, and mocked tests do not replace it.

Please attach one more run at the PR's code SHA (1d6fc06, equal to f42463d) from node bin/agent-device.mjs after pnpm build and pnpm clean:daemon. Use the JDY-AL50 with the app already in the foreground and the same hdc wrapper (timestamped log, aa dump -l sleeping 25s). Run agent-device replay on a one-line script open <bundle> --platform harmonyos with --timeout 3000. The output must show the probe's hdc ... aa dump -l read ending about 3s after the request started (not ~15s) and no alreadyForeground warning, with elapsed times recorded. Then please change the 'not expressible on the shipped CLI' sentence to name the replay route.

CI is green at f42463d: 14 checks, none failing. The earlier Android emulator smoke failure at 1d6fc06 did not touch this code, and the new commit only adds a docs file. Not verified here: the replay route was traced statically, not run, since no HarmonyOS device or hdc was available. The evidence doc's timings were not reproduced either. Before merge, the live replay run above is needed.

@strzhao
strzhao force-pushed the forge/open-probe-deadline-clamp branch from 66e129b to 2b9d875 Compare November 24, 2026 04:00
@thymikee

Copy link
Copy Markdown
Member

Thanks for the update. At 6df63a6 the live replay --timeout 3000 run that was missing at f42463d is now committed, so that earlier gap is closed and nothing remains open from it. All 14 checks pass at this head, and the new commits only touch docs and evidence. The earlier Android emulator smoke failure at 1d6fc06 was on a surface this diff does not reach, and that lane is now green. No conflicts. Not verified here: the JDY-AL50 runs were checked against the committed artifacts and the quoted wrapper timeline, not reproduced on a device. Two small notes that do not hold the PR: the evidence doc cites commit 66e129b, which is not in this PR's history, and replay-partb.json contains a local absolute path.

@thymikee thymikee added the ready-for-human Valid work that needs human implementation, judgment, or maintainer merge label Nov 24, 2026
strzhao and others added 3 commits November 24, 2026 13:01
Three take-or-leave items from the callstack#5055 review. The open lifecycle
binds readForegroundBundle as DirectApplicationLifecycleParams'
own shape, so the contract owns the type. The remaining-read-budget
clamp arithmetic (Math.min against a read's default, floored at 0,
spent deadline answers 0) moves to one helper beside hdc.ts that
both the open-time probe and the post-open poll budget call. The
probe's doc comment now says the clamp bounds the read, not the
dispatch it precedes.

Follow-up to the optional question the callstack#4860 review left open.

Co-Authored-By: Claude Code <noreply@anthropic.com>
…3b8a

Same replay route on the JDY-AL50 at the review-polish code SHA, screen
PIN-locked: the probe's aa dump -l read lived 2.23s under a 3s replay
deadline (cut at +3.29s, not 25s wrapper, not 15s default), the aa start
dispatch followed without waiting on the hung read, and the request
settled daemon_request_timeout with no alreadyForeground claim. Names
the observed hdc resolution mechanism: the daemon prefers its toolchain
roots over PATH, so the wrapper rides HDC_SDK_PATH.

Co-Authored-By: Claude Code <noreply@anthropic.com>
Cite the evidence commits that exist in this PR's history instead of the
unreferenced 66e129b, and scrub the local absolute paths from the
committed replay JSON artifacts (repo-relative script paths, placeholder
state dir).

Co-Authored-By: Claude Code <noreply@anthropic.com>
@strzhao

strzhao commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Polish applied as 9e23b8a13 (one commit on top): harmonyOpenVerifiedLifecycle binds readForegroundBundle as DirectApplicationLifecycleParams['readForegroundBundle']; the remaining-read-budget clamp (Math.min against a read's default, floored at 0) moved to one helper beside hdc.ts (remainingReadBudgetMs in packages/platform-harmonyos/src/read-budget.ts) that both the open-time probe and the post-open poll budget call; and the app-state.ts comment now says the clamp bounds the read, not the dispatch it precedes. Targeted vitest (platform-harmonyos 56 across app-state/runtime/app-lifecycle, contracts interaction 21), pnpm tsc -b and pnpm format pass; the layering detach control at base ecb2089867 is byte-identical at the new head — zero PR-introduced violations.

Your two notes are fixed too: the doc cites the evidence commits that exist in this history (2b9d875, 6df63a6) instead of 66e129b90a, and the three committed replay JSON artifacts carry no local absolute paths (repo-relative script paths, placeholder state dir).

Live re-run of the replay leg at the final code SHA 9e23b8a13, JDY-AL50 (华为畅享 90 Plus), HarmonyOS 7.0.0.111, serial 77TYD26826028730, screen PIN-locked at run time (the read-timing claims do not depend on the screen state), through node bin/agent-device.mjs after pnpm build + pnpm clean:daemon:

$ printf 'open com.sankuai.dianping --platform harmonyos\n' > /tmp/ad5055-lega/one-line.ad
$ PATH=/tmp/ad5055-lega:$PATH HDC_SDK_PATH=/tmp/ad5055-lega \
  AGENT_DEVICE_STATE_DIR=/tmp/ad5055-lega/state \
  node bin/agent-device.mjs replay /tmp/ad5055-lega/one-line.ad --timeout 3000 --json
… "success": false, "code": "REPLAY_DIVERGENCE",
  "details": { "reason": "daemon_request_timeout", "timeoutMs": 3000,
               "action": "open \"com.sankuai.dianping\"" }

Timeline, client request start against the wrapper's epochs (the wrapper sleeps 25 s only on aa dump -l):

+1.062s  wrapper: hdc -t 77TYD26826028730 shell aa dump -l   (probe read; sleeps 25 s)
+3.292s  wrapper: next read begins — bm dump -n com.sankuai.dianping
         (the probe's read lived 2.23 s — cut at the deadline, not 25 s, not 15 s)
+3.454s  wrapper: aa start … (the dispatch never waited on the hung read)

CLI wall 3.68 s; the request settled daemon_request_timeout / timeoutMs: 3000 with no alreadyForeground claim — the read ended near the 3 s deadline and claimed nothing, as the clamp requires. One mechanism note: a plain PATH prefix does not reach the daemon's hdc — resolveHdc (packages/platform-harmonyos/src/inventory.ts) prefers the toolchain roots and resolves an absolute binary — so the wrapper rides HDC_SDK_PATH, the daemon's own first root check, the same mechanism the recorded runs used. Evidence committed under docs/evidence/ad-5055-lega/ (wrapper log, replay JSON, request ndjson) with a doc section.

The already-foreground leg was recorded at 01:58 UTC on the unlocked screen (scenario a in the evidence doc: the alreadyForeground warning, warm open 0.84 s wall). The screen has since re-locked and automation cannot pass the PIN page, so a fresh unlocked-screen re-record awaits the next device unlock; the recorded run stands as that leg's evidence.

New head ab2366e16 — docs-only commits after the polish commit, so the code SHA stays 9e23b8a13. CI reruns on the push.

@thymikee thymikee removed the ready-for-human Valid work that needs human implementation, judgment, or maintainer merge label Nov 24, 2026
@thymikee

Copy link
Copy Markdown
Member

Thanks for the polish. At ab2366e both notes from the earlier review (#5055 (comment)) are closed, and the new changes add no regression. All 15 checks pass at this head, and the earlier Android emulator smoke failure at 1d6fc06 did not touch this diff and is green now. No conflicts were reported, and nothing else is needed before merge. Not verified here: the JDY-AL50 device timings were not reproduced, only checked for consistency against the committed artifacts and the quoted wrapper timeline, and tests were not run locally because the change is a behavior-preserving refactor with green CI.

@thymikee thymikee added the ready-for-human Valid work that needs human implementation, judgment, or maintainer merge label Nov 24, 2026
@thymikee
thymikee merged commit 4dffcd3 into callstack:main Nov 24, 2026
15 checks passed
@strzhao

strzhao commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Leg B re-recorded on the unlocked device, closing the note from the review thread.

  • Command: open com.huawei.hmos.settings --platform harmonyos --serial 77TYD26826028730 --json — three runs at the merged head (worktree built at ab2366e16; current main carries it).
  • Device: Huawei Enjoy 90 Plus (JDY-AL50), HarmonyOS 7.0.0.111.
  • Run 1 (cold): opened, wall 4.73 s. Run 2: wall 0.843 s. Run 3, full JSON: startup durationMs: 750, wall 0.849 s, and the warning is present: "App was already foreground; open re-activated it without restarting. Use --relaunch to restart."
  • Reading: the plain open on an already-foreground target returns the alreadyForeground warning with no added probe latency (0.849 s ≈ the 0.84 s warm-open baseline recorded during review).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-human Valid work that needs human implementation, judgment, or maintainer merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants