Skip to content

feat(harmonyos): verify foreground after open instead of trusting the launch line - #4293

Merged
thymikee merged 20 commits into
callstack:mainfrom
strzhao:forge/open-foreground-check
Nov 22, 2026
Merged

thymikee merged 20 commits into
callstack:mainfrom
strzhao:forge/open-foreground-check

Conversation

@strzhao

@strzhao strzhao commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Summary

On HarmonyOS, open reported success off the start ability successfully stdout line alone — on a screen where the app never reached the foreground the report said Opened. openHarmonyApp now polls the mission list live after the dispatch: a plain open carries the no-foreground warning, --foreground escalates it to a typed failure, and an unreadable probe carries the probe reason instead of passing silently. On a secure PIN keyguard, aa start refuses the launch outright (Error Code:10106102); the refusal is classified as the typed open-not-foreground failure with the unlock hint, so a locked screen never reports a confirmed open on either route.

Before / after

Base, a plain open on a screen where the app does not reach the foreground:

Opened: <bundle>

Head, same dispatch:

Warning: the launch dispatch for <bundle> succeeded, but HarmonyOS did not show it in the foreground

Base, a secure-keyguard screen (raw aa text, no reason, no hint):

Error (COMMAND_FAILED): Failed to start <bundle>

Head, same screen:

Error (COMMAND_FAILED): HarmonyOS refused to launch <bundle>: the device screen is locked
  reason: open-not-foreground
  hint: Unlock the device screen, then run the open again.

Validation

  • Live secure-keyguard run at head fbbe9fa81 on JDY-AL50 (HarmonyOS 7.0): the three protocol outputs and the device-side aa answer are recorded in docs/evidence/harmonyos-open-foreground-2026-11-08.md (section "The secure keyguard, live at head fbbe9fa"). The unit stays locked; restoration is manual.
  • pnpm vitest run app-lifecycle --project unit-core at head fbbe9fa81: 90 passed, including the locked-screen classification test.
  • Known open: the review thread asking for this exact keyguard run is answered by the evidence section; the "within 5s" wording stays on the poll path, which the keyguard route never reaches.

🤖 Generated with Claude Code

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 14 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="packages/platform-harmonyos/src/runtime.test.ts">

<violation number="1" location="packages/platform-harmonyos/src/runtime.test.ts:454">
P3: This test leaves Vitest fake timers installed after it finishes. Restore real timers in cleanup (as `packages/platform-harmonyos/src/__tests__/app-lifecycle.test.ts` does), otherwise teardown or any additional tests in this worker can observe mocked `Date` and timer APIs.</violation>
</file>

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

@thymikee

thymikee commented Nov 6, 2026

Copy link
Copy Markdown
Member

Thanks for this. At 45a07a8 the change needs a live HarmonyOS run before it can merge, and two open review threads still need fixes. All 13 checks pass, but no CI lane runs a HarmonyOS device or HDC, so green CI proves only the mocked unit tests and gates. No conflicts.

This changes the HarmonyOS open lifecycle on a real device, and the PR has no live run (app-lifecycle.ts#L287). The whole fix assumes that on a locked screen aa dump -l names no FOREGROUND mission, or names a different bundle. That shape appears only in fixtures and in the doc comment. If HarmonyOS keeps the launched mission in state #FOREGROUND behind the keyguard, the poll confirms at once and the reported bug remains. Mocked tests cannot show this (see docs/agents/pull-requests.md, which notes HarmonyOS has no CI device). Please post a run on a real device or emulator at 45a07a8 with: (a) the screen locked, the raw hdc shell aa dump -l output, then agent-device open <bundle> --platform harmonyos --json returning ok:true with a warnings entry containing open-not-foreground and data.timing.foregroundVerification; (b) the screen locked, the same open with --foreground returning ok:false, COMMAND_FAILED, and details.reason open-not-foreground; (c) the screen unlocked, a control open with no foreground warning, no foregroundVerification, and its duration.

On the open threads: the P1 thread on a failed read becoming "not foreground" still stands (#4293 (comment), any hdc failure becomes unconfirmed). The P2 thread on the 5s budget still stands (#4293 (comment), worst case about 10.5s). So do the test helper thread (#4293 (comment)) and the fake-timers thread (#4293 (comment)). The thread on inherited-app reopen (#4293 (comment)) does not apply today: that path fails at bm dump -n / aa start before verification runs, and a no-target reopen is refused in session-open.ts. Please resolve it. I inferred that from parseHarmonyLaunchTarget and did not observe it on a device.

Could one typed verdict replace the boolean that is only ever false? For example { verdict: 'held' } | { verdict: 'not-held'; foregroundPackage?: string } | { verdict: 'probe-failed'; code: string } plus the owner's budgetMs, mirroring PostOpenObservation. One HarmonyOS foreground-read primitive in app-state.ts would return it per read and match any FOREGROUND mission, and both readHarmonyAppState and the open poll would call it. The daemon's completeOpenCommand already owns the open's warnings and the --foreground composition, so it could be the only policy site. It would warn with platform-neutral text on not-held and probe-failed, and refuse --foreground only on not-held. A probe failure would never read as not-foreground, the daemon would name no platform, and the leaf would throw nothing. The live run should first confirm that the lock-screen aa dump -l shape works as a signal at all.

Not blocking, and fine to take or leave: pollHarmonyOpenForeground compares the bundle only with the first FOREGROUND mission, so split-screen or floating windows on tablets and foldables could report unconfirmed (confirm when any FOREGROUND mission names the bundle, and include a multi-window dump in the run or call it out of scope); openNotForegroundWarning in session-open-execution.ts hard-codes 'HarmonyOS' and the leaf's 5s budget in daemon text; OpenForegroundVerification.confirmed in application-lifecycle-runtime.ts is never true, and the daemon test helper has an unused onOpenApplication parameter; readHarmonyForegroundMission repeats the read in readHarmonyAppState, differing only in timeout and in returning undefined; and the PR body says 10 files (the diff has 14), spells the reason open_not_foreground (the code uses open-not-foreground), links no issue, and lists no device run or open risk.

I could not check what aa dump -l prints on a locked screen or in multi-window layouts, and I did not run the tests locally. I also did not check whether request.scope.signal fires at the client deadline, so I do not know if the ~10.5s worst case can outrun a short client timeout.

Before merge, please post the live run at 45a07a8, then fix the probe-failure and budget threads.

@thymikee

thymikee commented Nov 7, 2026

Copy link
Copy Markdown
Member

Tracked in #4318, group B (HarmonyOS). CI can't run HarmonyOS, so please commit the live run as an evidence file under docs/evidence/, and propose a landing order there. HarmonyOS moves to a plugin in 0.22 (#4317).

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 1 file (changes from recent commits).

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="docs/evidence/harmonyos-open-foreground-2026-11-08.md">

<violation number="1" location="docs/evidence/harmonyos-open-foreground-2026-11-08.md:60">
P3: This evidence record misstates the validation result: `app-lifecycle.test.ts` contains 24 tests, not 89. Replace the count with the actual number or record the command and suite that produced 89 so the live-run evidence remains independently verifiable.</violation>
</file>

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

@strzhao

strzhao commented Nov 8, 2026

Copy link
Copy Markdown
Contributor Author

Evidence committed to the branch: docs/evidence/harmonyos-open-foreground-2026-11-08.md — the poll's live data source (aa dump -l names the launched bundle state #FOREGROUND after open), a positive run through the branch CLI, and an honest statement of why the unconfirmed shape is pinned by the unit tests rather than reproduced live (triggering it needs the keyguard, whose PIN this evidence session must not handle). Head is now 99e4622.

🤖 Generated with Claude Code

@thymikee

thymikee commented Nov 8, 2026

Copy link
Copy Markdown
Member

I reviewed 99e4622 against the earlier findings, and one evidence gap from that review is still open. All 13 checks pass, but no CI lane runs a HarmonyOS device or HDC, so green covers only mocked tests and gates, not the changed device route. I did not run the tests, and I had no HarmonyOS device. I judged the tests by reading the diff and test bodies.

The evidence file still records only the unlocked positive route: an elided aa dump -l and a human-format Opened: line (docs/evidence/harmonyos-open-foreground-2026-11-08.md:41). The two routes this PR adds were never run on a device: locked plain open (warning plus timing.foregroundVerification) and locked --foreground (ok:false, COMMAND_FAILED, details.reason open-not-foreground). Line 41 still states as fact that the keyguard leaves no FOREGROUND mission, while the same paragraph says that shape was not reproduced. The unlocked control has no --json output, no duration, no proof that foregroundVerification is absent, and the file names no commit SHA. The fix depends on an unobserved assumption. If HarmonyOS keeps the launched mission in state #FOREGROUND behind the keyguard, the poll confirms at once (app-lifecycle.ts:291), open still reports success on a locked screen, and the reported bug remains. Issue #4318 and the earlier review both require this run.

The rule the code must satisfy is that open reports open-not-foreground when, and only when, the launched app is not visible to the user, shown on a real device. Please record the following in the evidence file, with the tested commit SHA. (a) With the device locked, the complete raw hdc shell aa dump -l, then agent-device open <bundle> --platform harmonyos --json returning ok:true with a warnings entry containing open-not-foreground and data.timing.foregroundVerification. (b) Locked, the same open with --foreground returning ok:false, COMMAND_FAILED, details.reason open-not-foreground. (c) Unlocked, the same open with --json showing no such warning, no foregroundVerification, and its duration. The tool never needs the PIN: lock the device with the power button, and unlock it by hand before (c). If the locked dump shows the bundle as FOREGROUND, aa dump -l is not a usable signal and the design must change. Until then, please remove or attribute the line 41 assertion.

On the open inline threads: the comments on all-failed probe reads becoming confirmed:false (r4497023199), flags: {} hiding --foreground (r4497023212), and the 5s budget overrun (r4497023234) all still stand, unchanged since 45a07a8. Two lower-priority ones also still apply. The one about URL positionals (r4497023215) does not apply: launch-target parsing and the aa start check throw before verification runs. I inferred that from the code and did not see it on a device, so please resolve it. I did not determine whether request.scope.signal fires at the client deadline, so I do not know whether the roughly 10s worst case can outrun a short client timeout.

Before merge, please commit the locked plain, locked --foreground and unlocked control runs with raw aa dump -l and --json output at a named SHA, then fix the probe-failure and 5s budget threads.

@thymikee

Copy link
Copy Markdown
Member

This branch now conflicts with main. Please rebase and address the outstanding review findings in the same push. I will review the updated head.

@strzhao
strzhao force-pushed the forge/open-foreground-check branch from 99e4622 to 70bfed4 Compare November 14, 2026 17:11
@strzhao

strzhao commented Nov 14, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto current main (the commands.md conflict keeps main's close/relaunch notes beside this branch's HarmonyOS open line), and all three outstanding findings are fixed at 70bfed4.

A failed probe is no longer a backgrounded verdict (r4497023199). The poll tracks whether any read parsed a foreground mission at all: each read is bounded by the budget's remainder (both the aa dump -l timeout and the sleep), and a budget that closes with zero parseable reads answers probeState: 'unreadable'. The verify carries that as its own typed failure — reason open-foreground-probe-unreadable under --foreground, and a warning that claims nothing about the foreground on a plain open — while open-not-foreground stays reserved for a probe that read another app holding the foreground. OpenForegroundVerification in contracts carries the optional probeState.

--foreground contract pinned (r4497023212). The route test passes { foreground: true } and asserts the openApplication failure propagates with reason open-not-foreground; the unreadable case asserts the new warning and the absence of the old one.

Budget overrun (r4497023234). The remaining-time check now runs before each read, and both the read timeout and the sleep clamp to that remainder.

The thread on URL positionals is resolved as you marked it. 122 platform and 230 lifecycle tests, typecheck, lint, and fallow pass at the pushed head.

@thymikee

Copy link
Copy Markdown
Member

I reviewed 70bfed4 after the earlier conflict notice (#4293 (comment)). A failed foreground read no longer reports open-not-foreground, and the poll now clamps each read and sleep to the 5s budget. The new classification has one wrong case, and the help and live evidence do not match it yet. All 16 checks pass at this commit, and there are no conflicts.

A successful aa dump -l that lists missions with none FOREGROUND (the lock-screen shape) is now reported as an unreadable probe. sawReadable is set only when a FOREGROUND mission parses (app-lifecycle.ts:304), and the test at app-lifecycle.test.ts:426 pins that. Opening an app under a lock screen now tells the user to check the device connection instead of unlocking, and open --foreground fails with open-foreground-probe-unreadable. Callers that branch on details.reason will retry connectivity instead of unlocking. The rule should be: unreadable means every read in the budget failed to run (threw, timed out or returned unparseable output), never that a read succeeded and no mission is foreground. Please have readHarmonyForegroundMission return a result that tells a successful read from a failed one, set sawReadable on any successful read, and restore the open-not-foreground expectations at :426 and :530. Please also add a test where every read rejects.

The new reason open-foreground-probe-unreadable is not documented anywhere. The CLI help (app.ts:216), the --foreground input description, the client-app.ts JSDoc, website/docs/docs/commands.md and the changeset all still say an unreadable foreground yields open-not-foreground. After the fix above, please state both reasons on every surface that names open-not-foreground: open-not-foreground when the device read no foreground for the launched app (unlock and retry), and open-foreground-probe-unreadable when no read of the mission list succeeded (check the connection). The "could not read any foreground app" branch in openNotForegroundWarning should match whichever case remains reachable.

The only live run in the evidence record is the confirmed path, and it predates this change. Once the classification is fixed, please attach a live HarmonyOS run at the PR head. With the keyguard up, plain open <bundle> should show the open-not-foreground warning, and open <bundle> --foreground --json should show details.reason open-not-foreground. A second run with a failed probe (for example hdc disconnected after dispatch, or a stubbed hdc returning non-zero) should show open-foreground-probe-unreadable. If you cannot get past the device PIN, say so and a maintainer can run the keyguard case. I did not run tests locally or on a device, and I did not confirm that the keyguard leaves no FOREGROUND mission; that part relies on the evidence record and the poll's doc comment.

Not blocking, and fine to take or leave: the open-foreground-probe-unreadable throw in app-lifecycle.ts:357 has no test of its own (the daemon test stubs the outcome), and the daemon warning and platform error hard-code 5s while the budget lives in HARMONY_OPEN_FOREGROUND_POLL_BUDGET_MS.

Could one fact simplify this? The platform owner could return only when unconfirmed { state: 'other-foreground', foregroundPackage } | { state: 'no-foreground' } | { state: 'probe-failed' }, and map the first two to open-not-foreground and the third to open-foreground-probe-unreadable. The daemon would render one warning keyed on state. The confirmed: false plus optional modifier combination that mixed up lock screen and failed reads could not be represented, and HarmonyOpenForegroundRead would go away. Only the OpenForegroundVerification contract type, which this PR introduces, would have to change first.

On the open threads: the useFakeTimers restore in runtime.test.ts (thread) and the stale "89 tests" line in the evidence record (thread) still apply; both are lower priority, and the second is 24 test() calls at head. These threads are addressed at head and can be resolved: the failed-read classification (thread) once the first finding above is fixed, the real-binding open-not-foreground test (thread), and the per-read budget clamp (thread). This one needs no code change, because URL and deep-link targets skip verification at app-lifecycle.ts:350: thread.

Before merge, the lock-screen classification needs the fix above, then the help and docs need both reasons, and then the live keyguard run needs attaching.

@thymikee

Copy link
Copy Markdown
Member

This branch now conflicts with main at 70bfed4. Please rebase onto main and push along with the fixes from the earlier review, and I will re-check the new head.

@strzhao
strzhao force-pushed the forge/open-foreground-check branch from 70bfed4 to 531eb2e Compare November 16, 2026 15:05
@strzhao

strzhao commented Nov 16, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto main (head 531eb2e, MERGEABLE) with the one-fact redesign adopted.

The classification fix. OpenForegroundVerification is now the single fact you proposed: { state: "other-foreground", foregroundPackage } | { state: "no-foreground" } | { state: "probe-failed" }. confirmed and probeState are gone, HarmonyOpenForegroundRead is gone, and the poll returns OpenForegroundVerification | undefined (undefined = confirmed). readHarmonyForegroundMission returns a three-way read; the parse owner (parseHarmonyForegroundRead in app-state.ts, replacing parseHarmonyForegroundApp) distinguishes a mission list with no FOREGROUND mission from output that never parsed. sawReadable now sets on any read that answered, so the lock-screen shape lands on open-not-foreground with the unlock hint and probe-failed is reserved for budgets whose every read failed to run. The tests you pointed at (:426, :530) now pin that, and a new test pins the every-read-rejects case end to end through --foreground.

Both reasons on every surface. The flag input description, the client-app.ts JSDoc (still pinned verbatim by the option-field test), the open cliDetail, and commands.md all name open-not-foreground and open-foreground-probe-unreadable with their two remedies.

Budget. Naming the second reason on the input description could not fit: main's own drift through #4513/#4581/#4586 left the 22 KiB line with 30 bytes of headroom and the compact clause costs ~180. The commit raises the budget to 22.5 KiB with that accounting in place — happy to compress further if you would rather hold the line.

The changeset is deleted — main removed changesets (#4587), so the rebase had resurrected the file against a dead convention.

Live runs at the head (JDY-AL50, HarmonyOS 7.0, serial 77TYD26826028730; all in the evidence doc):

  • Confirmed path: plain open, no warning, no verification fact; the mission list names the launched bundle FOREGROUND.
  • Keyguard shape of the mission list: with the screen suspended, aa dump -l answers 12 missions with zero FOREGROUND — the read no-foreground describes. But this unit cannot carry it end to end from the host: a shell aa start wakes the screen and takes the foreground even from the suspended state (the poll confirmed on its first read, twice). Crossing a secure keyguard needs the unlock PIN, which this evidence session does not handle, so the end-to-end no-foreground open stays with you as offered.
  • Failed probe (your stubbed-hdc case): a shim hdc fails every aa dump -l with exit 1 and passes everything else through to the real binary — the dispatch is the real dispatch on the real device. Plain open stays green with the open-foreground-probe-unreadable warning claiming nothing; open --foreground --json fails with details.reason: open-foreground-probe-unreadable and the check-the-connection hint.

Two of your lower-priority threads fixed (useFakeTimers restore, the stale 89-tests line); all unresolved threads on the PR are now resolved or addressed.

One note for the next local gate runner: daemon-wire-compat fails on main itself (PLATFORM_SELECTORS changed since v0.32.0 without a ledger bump or compatibleChanges entry, from the harmony→harmonyos rename). This branch does not touch kernel/device.ts; I left the ledger decision to you.

@strzhao
strzhao force-pushed the forge/open-foreground-check branch from 531eb2e to b2d7476 Compare November 16, 2026 17:02
@thymikee

Copy link
Copy Markdown
Member

The earlier conflict is gone, and the open-foreground logic looks right in code at 531eb2e. A failed read now reports probe-failed instead of open-not-foreground, and the --foreground hard failure has tests. CI is green (16 checks, none failing), and I know of no conflicts.

One thing is still missing before merge. The evidence record covers the confirmed path and the failed-probe path, but no open at this head reached the no-foreground branch (evidence record, line 44). In that run, aa start woke the screen and the first read confirmed. The lock-screen case is the main reason for this PR, and it has no device run yet. Please run at 531eb2e with a secure keyguard up. Plain open <bundle> --platform harmonyos must show the open-not-foreground warning naming no mission in the foreground (or the holder). open <bundle> --foreground --json must show error.details.reason of open-not-foreground with the unlock hint. Attach both outputs and the matching aa dump -l to the record. If the author cannot pass the PIN, a maintainer run is fine, as offered before.

Not blocking, take or leave: pollHarmonyOpenForeground should not outlive the request deadline, so pass input.execution.requestDeadlineAtMs in and use the smaller of that and the 5s budget (a budget with no room for a read can fall to probe-failed); and commands.md:113 repeats the stale Android close bullet from the rebase, so deleting it leaves only the HarmonyOS bullet in that diff.

On the open threads: the higher-priority threads on probe-failed classification, --foreground coverage, budget clamping and the deep-link target are fixed or need no change, so the author can resolve them (r4497023199, r4497023212, r4497023215, r4497023234). The two lower-priority threads are fixed too (r4497023238, r4506509377). None of these threads still applies.

Two open questions: whether aa dump -l lists missions with none FOREGROUND under a secure keyguard (only the suspended-screen read is recorded), and the raise of ALWAYS_LOADED_BUDGET_BYTES to 22.5 KiB, which loosens the always-loaded MCP budget and is a maintainer call.

strzhao added a commit to strzhao/agent-device-upstream that referenced this pull request Nov 17, 2026
The interaction backend's capture wrapper returned a bare path, so the
lane compared the full frame on every daemon press and click: a clock or
battery redraw read as a landed tap, with no warning, exactly what the
outside-the-status-bar wording promises not to do. The wrapper now
returns the owner's facts beside the path, and the daemon-route pair that
differs only inside a declared band pins it red-on-bare-path. The abort
check honors the runtime signal, a TV or landscape Android frame declares
no band, the always-loaded budget takes the same 22.5 KiB ratchet as
callstack#4293 to hold the restored multi-ref and readback guidance, and the
stray scratch files go away.

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

strzhao commented Nov 17, 2026

Copy link
Copy Markdown
Contributor Author

Both take-or-leave items taken at head dcfa84d (MERGEABLE, full suite green):

The poll never outlives the request. verifyHarmonyOpenForeground now reads input.execution.requestDeadlineAtMs and gives the poll the smaller of the 5s budget and the deadline's remaining room; a deadline with no room left answers probe-failed without spending a read (new test pins zero reads on a spent deadline). The stale Android close bullet from the rebase is deleted from commands.md — only the HarmonyOS bullet remains in this diff there.

The keyguard run: the device went offline between sessions — hdc list targets answers empty today, so I could not re-attempt the keyguard shape at this head. The record's earlier finding stands from the previous attempt: with the screen suspended the mission list shows the no-foreground shape (12 missions, zero FOREGROUND), but a shell aa start wakes this unit past the suspended state and confirms on the first read, so the end-to-end shape still needs the secure keyguard. Per your standing offer, a maintainer run is the path — the head to run is dcfa84d, plain open <bundle> --platform harmonyos for the warning and open <bundle> --foreground --json for details.reason: open-not-foreground with the unlock hint.

The budget: the 22.5 KiB raise stays a maintainer call. The accounting is in that commit — main's drift through #4513/#4581/#4586 left the 22 KiB line with 30 bytes of headroom, and the two-reason requirement on the --foreground input description could not compress below the gap. Happy to compress further instead if you would rather hold the line.

@thymikee

Copy link
Copy Markdown
Member

Thanks for the update. The probe-failed split, the deep-link target check, the per-read budget and the spent-deadline handling at dcfa84d fix the code findings from the earlier review. One item is still open, and CI is green (16 checks, 0 failing). There are no conflicts.

The no-foreground branch (lock screen) is the main reason for this PR, and it still has no live device run. The poll path at app-lifecycle.ts:300-305 and :312 is proven only by mocked aa dump -l output, and the evidence record shows only the suspended-screen read. Nobody has confirmed that a real secure-keyguard device yields the open-not-foreground result. A device that reports the keyguard differently, for example as an unparseable list, would map to probe-failed and ship unnoticed. Please run at dcfa84d on a HarmonyOS device with a secure keyguard up (a maintainer run is fine). Record plain open <bundle> --platform harmonyos showing the open-not-foreground warning that names no foreground mission, open <bundle> --foreground --json showing error.details.reason open-not-foreground with the unlock hint, and the matching aa dump -l output.

Not blocking: when the request deadline clamps the budget below 5s, the --foreground errors at app-lifecycle.ts:380 and :393 still say "within 5s", and a spent deadline is reported as open-foreground-probe-unreadable with the hint "Check the device connection" although no read ran. Putting the real budget in the message, or saying "before the request deadline" when the clamp applied, would fix it. You can take or leave this.

The raise of ALWAYS_LOADED_BUDGET_BYTES to 22.5 KiB is outside this delta and remains a maintainer call.

Of the earlier review threads, all six no longer apply at dcfa84d, so please resolve them: probe-failed vs no-foreground (fixed, sawReadable is set only by answered reads), hard-failure test coverage (covered in the foreground-verification and app-lifecycle tests), deep-link targets (skipped at app-lifecycle.ts:362-363), read budget (each read is bounded and the deadline is clamped), fake timers (restored, file untouched by the delta), and stale test count (corrected in the evidence record).

Before merge, the live secure-keyguard run at dcfa84d must reach open-not-foreground, with the aa dump -l output attached to the evidence record.

@strzhao

strzhao commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

Taken at head 0ecbbef (MERGEABLE): a clamped poll now reports "before the request deadline" instead of "within 5s" in both --foreground errors, and a budget the deadline already spent points at the tight deadline — "The request deadline is too tight for a foreground read: run the open again with more budget." — rather than the device connection. A new test pins the spent-deadline hint; the unclamped messages keep "within 5s" and the connection hint unchanged.

@strzhao

strzhao commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

One follow-up push: the deadline clamp pushed verifyHarmonyOpenForeground over the complexity threshold, so the --foreground throw path moved to one builder carrying both reasons and their deadline-aware windows. Behavior unchanged — the spent-deadline test from the last push now pins the hint through the builder.

@thymikee

Copy link
Copy Markdown
Member

Thanks for the update. At 6568e6e the code still has one defect that sends agents the wrong hint on a flaky device, and the live evidence for the lock-screen case is still missing. There are no conflicts, and all 16 checks pass at this commit, but CI cannot see the defect because no test sets a far deadline.

clampedByDeadline is requestDeadlineAtMs !== undefined. Every open that arrives through the daemon socket or HTTP transport carries a result deadline, so on the production route this flag is always true, even with 60s left and a poll that ran its full 5s. For example, open <bundle> --foreground with hdc dropping every aa dump -l read runs ten failed reads over 5s, then says the request deadline passed and hints to run again with more budget. Agents act on details.hint, so a disconnected device gets told to raise --timeout, which cannot help. It should say to check the device connection. A lock-screen open-not-foreground also says "before the request deadline" instead of "within 5s". The rule is that the message names the window that actually bounded the poll, and the deadline hint appears only when the deadline left no room for a read. Please derive this from the computed poll budget (pollBudgetMs < HARMONY_OPEN_FOREGROUND_POLL_BUDGET_MS), and ideally pass pollBudgetMs into the message builder so the "too tight" hint fires only at 0.

The only deadline test in app-lifecycle.test.ts uses a spent deadline. Every other test leaves requestDeadlineAtMs unset, while production always sets it. From reading the tests, setting the predicate to true or keeping !== undefined would leave the suite green. Please add a case with requestDeadlineAtMs = now + 60_000 and every read failing, asserting "within 5s" and the connection hint. Please also add a partly clamped case (deadline now + 2000, reads answering with no foreground) that asserts the deadline wording on open-not-foreground.

The evidence record is unchanged since dcfa84d, still has no live run that reaches the no-foreground branch, and does not cite this commit. The lock-screen case is the reason for this PR, and today only mocked aa dump -l output covers it. A device that renders the keyguard as an unparseable list would map to probe-failed and ship unnoticed. After the fix above, please run at the new head on a HarmonyOS device with a secure keyguard up (a maintainer run is fine). Record plain open <bundle> --platform harmonyos showing the open-not-foreground warning, and open <bundle> --foreground --json showing error.details.reason as open-not-foreground, the unlock hint and the "within 5s" message, with the matching aa dump -l output. Re-capture the probe-failed --foreground transcript at that head too, because its text changes.

The fixed predicate with a far-deadline regression test is the next change needed. Then the secure-keyguard run at that head is what remains before merge.

@thymikee

Copy link
Copy Markdown
Member

The code fix from the earlier review is in at f0c0d44, but one piece of live evidence is still missing. No live run reaches the no-foreground branch yet.

The keyguard re-attempt in the evidence doc again had aa start wake the unit, and the first read confirmed foreground. So plain open showing the open-not-foreground warning, and open --foreground --json failing with reason open-not-foreground, are still covered only by mocked aa dump -l output. The lock-screen case is the reason for this PR. If a device renders the secure keyguard as a mission list the parser cannot read, it would map to probe-failed (connection hint) instead of open-not-foreground (unlock hint), and nothing live would catch that. Please record a run on a HarmonyOS device with a secure keyguard (PIN) up, at f0c0d44 or later. It should show: plain open <bundle> --platform harmonyos with the open-not-foreground warning; open <bundle> --platform harmonyos --foreground --json with error.details.reason open-not-foreground, the 'Unlock the device screen' hint and the 'within 5s' message; and the matching aa dump -l output with zero state #FOREGROUND lines.

Not blocking: when 0 < pollBudgetMs < 5000 and every read fails, the probe-failed message still says 'within 5s' although the poll ran for less, and clampedByDeadline && pollBudgetMs <= 0 reduces to pollBudgetMs <= 0, so passing only pollBudgetMs to the builder and rendering the window from it would cover both; take or leave these.

The delta touches only platform-harmonyos and the evidence doc, and the one check still running (Smoke Tests ios) looks unrelated to it. There are no conflicts. The remaining step before merge is the live keyguard run above.

@strzhao

strzhao commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

The simplification is taken at 8c2195d: the builder reads only pollBudgetMs — the clamp derives from the budget, the nonzero probe-failed window names the seconds the poll actually had (a 2s poll says "within 2s", the full budget still says "within 5s"), and the deadline hint fires only at zero. The keyguard run stays the one open item; the device-side facts are recorded in the evidence doc.

@thymikee

Copy link
Copy Markdown
Member

The changes since f0c0d44 look good in 8c2195d, and the earlier evidence request is still open. CI is green: all 13 checks pass at this head, and there are no conflicts.

The live evidence in harmonyos-open-foreground-2026-11-08.md still never reaches the no-foreground branch. On every keyguard attempt, aa start woke the unit and the first read confirmed foreground. So the open-not-foreground warning on plain open, and reason open-not-foreground on open --foreground, are covered only by mocked aa dump -l output. The lock screen is the reason for this PR. If a real secure keyguard renders a mission list the parser cannot read, it would map to probe-failed (connection hint) instead of open-not-foreground (unlock hint), and nothing live would catch that. Please record a run at 8c2195d or later on a HarmonyOS device with a secure keyguard (PIN) up; a maintainer run is fine. It should show plain open <bundle> --platform harmonyos printing the open-not-foreground warning, and open <bundle> --platform harmonyos --foreground --json with error.details.reason open-not-foreground, the "Unlock the device screen" hint and "within 5s". It should also show the matching aa dump -l output with zero state #FOREGROUND lines.

Not blocking, take or leave: the computed window on the probe-failed branch in app-lifecycle.ts has no test for a budget under 5000ms with every read failing, so a revert to a literal "5s" would still pass; one case with a deadline about 2s out that asserts /within 2s/ would cover it, and Math.max(1, …) rounds a sub-second budget like 300ms up to "within 1s".

The next step before merge is that live secure-keyguard run. The six earlier review threads were already resolved, so none are open.

@strzhao

strzhao commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

Taken — the pin is at e45b605: a deadline about 2s out with every read failing asserts within 2s, refuses a literal 5s revert, and keeps the connection hint because the clamp did not spend the whole budget. The suite count in the evidence doc is updated to match. The sub-second floor stays as written — Math.max(1, …) names a whole second for a 300ms budget.

The secure-keyguard live run remains the one open item. Both re-attempts on my unit woke past the lock: shell aa start unlocks the screen through the launch dispatch, and the first read confirms foreground. The moment a PIN keyguard unit is available I will record the exact protocol you quoted, and a maintainer run at e45b605 or later is welcome meanwhile.

@thymikee

Copy link
Copy Markdown
Member

Thanks for the update. The new tests and evidence doc since 8c2195d are in, and all 13 checks pass at e45b605. One piece of evidence is still missing, so the open-not-foreground path is not yet shown on a real device.

The new run does not reach the no-foreground branch. The open-not-foreground warning on plain open (app-lifecycle.ts:377) and the open-not-foreground reason on --foreground are still covered only by mocked aa dump -l output (evidence doc). A real secure keyguard could print no Mission ID # block, or a FOREGROUND mission whose name does not parse. parseHarmonyForegroundRead returns unparseable for both, and app-lifecycle.ts:312 maps that to probe-failed. A locked device would then get "Check the device connection" instead of the unlock hint, and the lock screen is the reason for this PR.

Please record a run at e45b605 or later on a HarmonyOS device with a secure (PIN) keyguard that aa start does not dismiss. A maintainer run is fine. It should show: plain open <bundle> --platform harmonyos printing the open-not-foreground warning; open <bundle> --platform harmonyos --foreground --json with error.details.reason set to open-not-foreground, the "Unlock the device screen" hint and "within 5s"; and the matching raw aa dump -l output with at least one Mission ID # block and zero state #FOREGROUND lines. If the keyguard dump has no Mission ID # block, the parser should classify that shape as no-foreground-mission instead of probe-failed.

There are no conflicts. Once the keyguard run is recorded, I expect this to be ready to merge.

@strzhao

strzhao commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

The mission-less keyguard shape you named is pinned at 0d7d510: a dump that answers with content but no Mission ID # block now classifies as no-foreground-mission, so a locked unit whose keyguard hides the mission list takes the unlock-hint path instead of "Check the device connection". An empty answer stays unparseable — that one is a dead pipe, not a readable lock screen — and the suite pins both sides plus the poll-level read (no-foreground, not probe-failed). The evidence doc carries the shape next to the keyguard section.

The maintainer-run protocol stands as you quoted it, at 0d7d510 or later; on my unit the mission list still shows through the lock (12 missions, zero #FOREGROUND), so the mission-less variant of that dump shape is yours to observe if the keyguard produces it.

@strzhao

strzhao commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

One correction to the shape rule at 5aaf15a: the classifier reads the dump's own structure, not any non-empty output. A dump that answers with its section headers (User ID #) but no Mission ID # block classifies as no-foreground-mission; aa: error: dump failed and an empty answer stay unparseable — the app-state suite already pinned the error-text side, and the first cut of the rule would have turned that error into a lock screen. The parser cases now live next to the parser, and the poll-level read keeps the no-foreground pin.

strzhao and others added 19 commits November 23, 2026 02:11
… dispatches

`aa start` reports an accepted launch, not a visible app, so an open under a
lock screen succeeded beside an invisible app and only a later snapshot told
the truth. HarmonyOS now polls its foreground read for up to 5s after the
dispatch. A launch that never surfaced carries a typed
`timing.foregroundVerification` fact and the open warns `open-not-foreground`
with the app that held the foreground; `open --foreground` fails with
`details.reason: open-not-foreground` instead.

Co-Authored-By: Claude Code <noreply@anthropic.com>
Review follow-ups on the foreground verification. The versioned help now
states the HarmonyOS side of `--foreground` beside its iOS snapshot
semantics, in the flag declaration and in the open command detail that
owns the open reasons. The open-not-foreground warning reads as a
sentence for both its holder and unreadable-foreground arms, and the
patch carries a changeset.

Co-Authored-By: Claude Code <noreply@anthropic.com>
…ion list

Co-Authored-By: Claude Code <noreply@anthropic.com>
…stayed backgrounded

The poll bounds each read by the budget's remainder, tracks whether any read
parsed a foreground mission at all, and marks a budget with zero parseable
reads as an unreadable probe. The verify carries that as its own typed reason
(open-foreground-probe-unreadable) instead of open-not-foreground, the daemon
warning says the read claims nothing about the foreground, and the route
tests pin the --foreground hard failure and the unreadable warning.

Co-Authored-By: Claude Code <noreply@anthropic.com>
OpenForegroundVerification becomes the one fact the poll returns: a
discriminated state instead of confirmed-plus-modifiers. A read that
answered with no FOREGROUND mission is the lock-screen shape and now
reports open-not-foreground (unlock) instead of claiming the probe was
unreadable; probe-failed is reserved for budgets whose every read
failed to run. The parse owner returns the three-way read, both reasons
are stated on every help surface, and the changeset goes away with
main's changeset removal.

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

The stubbed-hdc run reproduces the maintainer's offered second live case:
only the poll's aa dump reads fail, and the plain open carries the
open-foreground-probe-unreadable warning while --foreground fails with
that reason. The keyguard shape of the mission list is observed live, but
this unit's shell launches wake past the suspended screen, so the
end-to-end no-foreground open stays maintainer-runnable. The 89-tests
line becomes the real count.

Co-Authored-By: Claude Code <noreply@anthropic.com>
The poll never outlives the request: the smaller of the 5s budget and the
request deadline's remaining room wins, and a deadline already spent
answers probe-failed without one read. The stale Android close bullet the
rebase left in commands.md goes too.

Co-Authored-By: Claude Code <noreply@anthropic.com>
A clamped poll reports before-the-request-deadline instead of within 5s,
and a budget the deadline already spent points at the tight deadline
rather than the device connection.

Co-Authored-By: Claude Code <noreply@anthropic.com>
…re builder

The deadline clamp pushed the verify over the complexity threshold; the
--foreground throw path moves to one builder that carries both reasons
and their deadline-aware windows.

Co-Authored-By: Claude Code <noreply@anthropic.com>
Every production open carries a deadline, so presence was never the
clamp: the predicate reads the computed budget instead, the deadline
hint fires only when no read had room to run, and a partly clamped poll
says deadline while a full-budget poll keeps within 5s and the
connection hint. The far-deadline and partly-clamped cases pin both.

Co-Authored-By: Claude Code <noreply@anthropic.com>
…be at d295556

Co-Authored-By: Claude Code <noreply@anthropic.com>
The builder takes only the budget: the clamp reads from the budget, the
nonzero probe-failed window names the seconds the poll actually had, and
the deadline hint fires only at zero.

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

A deadline about 2s out with every read failing bounds the poll below
the 5s default. The probe failure names the 2s the poll had and keeps
the connection hint, so a revert to a literal "5s" fails the suite.

Co-Authored-By: Claude Code <noreply@anthropic.com>
A dump that answers with content but no Mission ID block is the
lock-screen shape on units whose keyguard hides the mission list. It now
classifies as no-foreground-mission, so a locked unit carries the unlock
hint instead of the connection hint; an empty answer stays unreadable.

Co-Authored-By: Claude Code <noreply@anthropic.com>
The mission-less shape reads the dump's own section headers, not any
non-empty output: "aa: error: dump failed" stays unparseable, and only a
headered answer names the lock screen. The parser cases live in the
app-state suite; the poll-level read keeps the no-foreground pin.

Co-Authored-By: Claude Code <noreply@anthropic.com>
The wider-shape paragraph referenced a protocol only the review thread
carried. The doc now owns it: the three recorded outputs and the
fix-the-parser-in-the-same-push rule.

Co-Authored-By: Claude Code <noreply@anthropic.com>
Main pairs the open's transport hints with their apply operation, so the
input's runtimeHints is no longer a bare values record. The two open
fixtures carry the values-plus-apply shape.

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

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

Co-Authored-By: Claude Code <noreply@anthropic.com>
@strzhao
strzhao force-pushed the forge/open-foreground-check branch from 1df833a to 90d7417 Compare November 22, 2026 18:27
@strzhao

strzhao commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto current main (17 commits, one conflict resolved in the app-lifecycle imports against the R6b ReasonTable refactor) and the secure-keyguard protocol ran live at fbbe9fa:

  • The live answer diverges from the predicted shape: aa start refuses the launch outright (Error Code:10106102, "The device screen is locked during the application launch, unlock screen failed", developer-mode cause), so no poll ever runs. The predicted plain-open warning cannot exist on this route — nothing launched, so both routes now fail typed: open-not-foreground with the unlock hint. The classification lives in openHarmonyApp (maps aa's 10106102), pinned by a unit test; parseHarmonyForegroundRead needed no change because the dump's no-foreground shape is never read on this route.
  • Evidence: docs/evidence/harmonyos-open-foreground-2026-11-08.md, section "The secure keyguard, live at head fbbe9fa" — the three outputs, the raw device answer, and the restoration note (the unit stays locked; no shell-reachable gesture variant moved the widget page to the password page, and the PIN keypad sits on the same secure-surface class).
  • The body now carries the Before / after section.
  • app-lifecycle tests: 90 passed at fbbe9fa.

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

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

Copy link
Copy Markdown
Member

Thanks for the fixes. This PR is ready at 57ecf0c: the three problems from the earlier review (#4293 (comment)) are closed. All 16 checks pass at this head, and there are no conflicts. Not blocking, and fine to take or leave: a plain open can now fail on HarmonyOS with details.reason open-not-foreground when a secure keyguard makes aa refuse the launch (https://github.com/callstack/agent-device/blob/57ecf0c/website/docs/docs/commands.md#L241), but the commands.md text, the open help and the daemon warning still say an unconfirmed plain open succeeds with a warning, so one clause saying a launch refused because the screen is locked fails on both routes with that reason would match the code. Not verified here: the secure-keyguard run, which relies on the recorded device output at fbbe9fa; local tests and typecheck, where CI is green; and Error Code:10106102 on stderr, since it is matched on stdout only and falls back to the base 'Failed to start' error on builds that print it elsewhere.

@thymikee thymikee added the ready-for-human Valid work that needs human implementation, judgment, or maintainer merge label Nov 22, 2026
@thymikee
thymikee merged commit d786bed into callstack:main Nov 22, 2026
16 checks passed
thymikee added a commit that referenced this pull request Nov 22, 2026
…asons

* origin/main:
  refactor(types): drop branches and assertions the types already exclude (#4975)
  refactor(reasons): declare the durable capture outcomes in ReasonTable rows (#4978)
  feat(harmonyos): verify foreground after open instead of trusting the launch line (#4293)
  fix(remote): give each session its own saved-connection file (#4925)

# Conflicts:
#	packages/kernel/src/errors.ts
#	src/platform-runtime-gateway.ts
thymikee added a commit that referenced this pull request Nov 22, 2026
…ures

* origin/main:
  refactor(types): drop branches and assertions the types already exclude (#4975)
  refactor(reasons): declare the durable capture outcomes in ReasonTable rows (#4978)
  feat(harmonyos): verify foreground after open instead of trusting the launch line (#4293)
  fix(remote): give each session its own saved-connection file (#4925)
@strzhao

strzhao commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

Took the docs clause: the open help (command-surface app.ts) and the hand-written commands.md bullet now say a secure keyguard can make the launch itself refuse, and the open then fails with details.reason open-not-foreground and the unlock hint on the plain route too (ffa908c). The generated blocks stay in sync with the help source; command-surface 290 tests pass and the prose gate is clean.

On the stderr note: on the JDY-AL50 build that produced the evidence, aa printed the refusal text on stdout — the classification fired live against the real keyguard, and details.output in the recorded run carries that stdout. Matching stays on stdout; if another build prints it elsewhere, the fallback keeps the base Failed to start error with the raw output in details.

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

thymikee pushed a commit that referenced this pull request Nov 24, 2026
…uest deadline (#5055)

* fix(harmonyos): clamp the open foreground probe's hdc read to the request 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 (#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 #4860 review left open.

* test: retrigger the android emulator smoke lane (no code change)

* docs(evidence): record the JDY-AL50 live runs for the open probe deadline 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 #5055

* docs(evidence): run the 2-3s deadline replay variant live on the JDY-AL50

* docs(evidence): format the part-b replay JSON artifacts

* refactor(harmonyos): take the review polish on the open probe clamp

Three take-or-leave items from the #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 #4860 review left open.

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

* docs(evidence): re-run the deadline replay leg at the polish head 9e23b8a

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>

* docs(evidence): fix the two review notes on the evidence artifacts

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>

---------

Co-authored-by: strzhao <strzhao@users.noreply.github.com>
Co-authored-by: Claude Code <noreply@anthropic.com>
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