Skip to content

feat(hooks): a PreToolUse hook denies uno_app_restart while Hot Reload applied every edit since the last start - #203

Closed
vatsashah45 wants to merge 6 commits into
mainfrom
dev/vatsa/hook-restart-guard
Closed

vatsashah45 wants to merge 6 commits into
mainfrom
dev/vatsa/hook-restart-guard

Conversation

@vatsashah45

Copy link
Copy Markdown
Contributor

Stacked on #202 (merge that first; this diff shows only its own commit once #202 is in). The enforcement #201 could not be: a hook that denies the restart instead of a sentence that asks the agent not to.

Why a hook

Nine catalog sessions on this plugin restarted the app 6 to 13 times each right after the Hot Reload hook had said applied, a third of all their launches, for reasons like "constructor changes need a fresh model instance" or "restarting cleanly". Two A/Bs of the uno-testing text (unoplatform/private#1217 on dev/steve/uno-testing-loop, unoplatform/private#1220 on #201's branch) moved that by nothing: the restart is decided from what the agent believes about Hot Reload, and the skill is loaded in about half the sessions anyway. A PreToolUse hook is read every time.

What it does

  • hot-reload-edit.sh now records, per session ($TMPDIR/uno-studio-hooks/<session_id>.state), the last verdict it got from the DevServer and whether any edit since the last start touched a file that runs at startup: App.xaml.cs, Program.cs, .csproj, .props, .targets, solution, global.json, appsettings*.json, manifests, .plist. Those edits are recorded for every file type, not only .xaml/.cs.
  • app-restart-guard.sh pre (PreToolUse on mcp__.*__uno_execute_tool) denies uno_app_restart while the last verdict is applied, no changes or superseded and no startup file changed. The reason says why and what to do: re-enter the state under test and assert; a restart is allowed after RudeEdit, InternalError, a compile failure or a startup-file edit; uno_app_close then uno_app_start for other arguments, data or platform. A pending verdict is denied until uno_app_hot_reload_status answers. Everything else goes through: no edit since the last start, an ignored edit, no CLI on the machine.
  • app-restart-guard.sh post (PostToolUse on the same tool) clears the state on uno_app_start or uno_app_restart.
  • Only uno_app_restart is guarded. uno_app_close + uno_app_start is the legitimate way to relaunch with other arguments or data, so it stays open; if the sessions move to that pair to get around the guard, the next step is to deny uno_app_close under the same condition unless the following start changes arguments or target.

Test-Hooks.ps1 runs the pair through the sequence (applied → denied; restart → cleared; rude edit, project file, App.xaml.cs, no CLI → allowed; pending → denied with the status advice). Test-Plugin.ps1 passes. Not yet measured on a catalog run: that is the next skill-ab invocation, with this branch as -A.

🤖 Generated with Claude Code

vatsashah45 and others added 3 commits October 20, 2026 21:06
With the DevServer CLI present but no app connected yet, `hot-reload-status` answers without a verdict (exit code 0), and the hook fell through to its reminder on every scaffolding edit: 14 to 20 notes a session before the first `uno_app_start`, 4.5k to 8.4k persistent tokens of hook text, and an agent that learned to skim the hook's notes. Now a no-verdict answer prints nothing; the reminder stays for a machine without the CLI. `Test-Hooks.ps1` covers the case.

Validated on three catalog sessions (unoplatform/private#1220): zero notes before the first start, hook text in context down to 3.2k–5.3k tokens.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
…d applied every edit since the last start

Nine catalog sessions restarted 6 to 13 times each right after the hook had said `applied`, a third of their launches, and two A/Bs of the skill text (unoplatform/private#1217, #1220) moved that by nothing: the restart is decided from what the agent believes about Hot Reload. So the hook decides. `hot-reload-edit.sh` records each verdict for the session, and any edit to a file that runs at startup (App.xaml.cs, Program.cs, project, props, appsettings, manifest). `app-restart-guard.sh pre` denies `uno_app_restart` while the last verdict is applied, no changes or superseded and no such file changed, with the reason and the way out (assert; or `uno_app_close` then `uno_app_start` for other arguments, data or platform); a pending verdict is denied until the status tool answers; a RudeEdit, InternalError, compile failure, ignored edit, a startup-file edit, no CLI, or no edit since the last start lets it through. `app-restart-guard.sh post` clears the state on a start or restart. State lives in $TMPDIR/uno-studio-hooks/<session_id>.state.

Test-Hooks.ps1 covers the guard; the README's hooks row says what it does.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
record() reused the `startup` variable that the applied message appends for an App.xaml.cs edit, so every applied note
ended in "uno_app_start.0" and the startup sentence never appeared (catalog-20261020-213956, all three sessions).
The test now checks the end of the applied note and the App.xaml.cs sentence, and clears the guard state before its cases.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@vatsashah45

Copy link
Copy Markdown
Contributor Author

Validation: three catalog sessions on this branch at 5c3373f, results in unoplatform/private#1222, against the nine earlier Uno sessions on main and #201's branch.

per session A1 A2 A3 nine earlier sessions
uno_app_restart allowed / denied by the guard 0 / 1 8 / 1 2 / 1 5–20 / —
starts + restarts 16 23 13 19–48
source edits after the first start 14 36 11 57–65
hook notes (persistent tokens) 1.2k 2.5k 0.8k 3.2k–8.4k
wall time 26 min 38 min 26 min 35–65 min
cost $8.96 $15.48 $10.31 $11.98–$22.38, median $14.69
  • The guard fired once per session, each time on a restart after an applied verdict with no startup-file edit: the agent wanted a "clean state" after Hot Reload. That is the case this PR targets.
  • Each denial became uno_app_close + uno_app_start, the path the deny reason leaves open. So the guard redirects the restart it catches rather than removing it. If that matters, the next step is to deny uno_app_start under the same state when the arguments did not change.
  • The restarts that went through were the allowed cases: A2's eight followed App.xaml.cs edits in a startup-theme debugging loop, the rest a RudeEdit or a Program.cs/.csproj edit.
  • Launches, hook text, time and median cost are all down, but fewer source edits after the first start explains most of it, and three sessions cannot tell whether the guard caused that.
  • The run exposed a bug in this branch: record() reused the startup variable, so every applied note ended in uno_app_start.0 and the App.xaml.cs startup sentence never appeared. Fixed in db46fc6, with a test. The guard itself read the state correctly, as the denials show.

…a denied restart does not become close+start

In catalog-20261020-213956 every denied restart turned into uno_app_close then uno_app_start, the path the deny reason
leaves open. The guard now denies the close once, with the reason, when every edit since the last start was applied
and none touched startup code; the same call again goes through, so a real close costs one extra call and nothing dead-ends.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@vatsashah45

Copy link
Copy Markdown
Contributor Author

Follow-up to the validation above, in 1c6aacb: a uno_app_close in the same state (every edit since the last start applied, none to startup code) is now denied once with the reason, and the same call again goes through. That closes the close+start path each denied restart took, at the cost of one extra call for a real close. Not yet measured on a run.

vatsashah45 and others added 2 commits October 21, 2026 15:16
…nce, so a denied restart does not become close+start"

This reverts commit 1c6aacb.
Only an edit refreshes the recorded verdict, so a pending one went stale after the status tool had answered Ready
(catalog-20261021-103500 A2, on Android: two denials and a failed restart before the agent rewrote its code).
The guard now denies a pending restart once per edit and says so; the same call again goes through.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@vatsashah45

Copy link
Copy Markdown
Contributor Author

Second run, on 1c6aacb (the guard plus the one-time uno_app_close denial), three sessions, results in unoplatform/private#1223.

per session A1 A2 A3 previous run (guard only)
uno_app_restart denied (applied / pending) 2 / 0 0 / 2 2 / 0 1 / 0 each
uno_app_close denied once 13 2 5 —
what followed each close denial close again, start close again, start close again, start —
uno_app_start failed, app still running 3 0 1 0
starts + restarts 42 17 21 13–23
cost $21.77 $9.36 $12.07 $8.96–$15.48
  • The close nudge is out (e3564b4). Twenty denials and not one relaunch avoided: the reason said a second call goes through, and the agent made it every time. Four times it skipped the second close and uno_app_start failed on the locked build output. A one-time deny only buys a call when the agent has no reason to want the action; here it wants the relaunch for the same reason it wanted the restart.
  • The restart deny holds: four applied-state denials on "clean state after Hot Reload" restarts, and every allowed restart followed a startup-code edit or a RudeEdit.
  • Stale pending verdict, fixed (e885a73): only an edit refreshed the recorded verdict, so after the status tool reported Ready on Android, A2 was denied twice and a third attempt failed before it rewrote its code to dodge Hot Reload. A pending verdict now denies once per edit and says so.
  • A1 is the dearest session of the fifteen: a long App.xaml.cs theme and double-navigation chase the guard allows by design, with the nudge's extra calls on top. The two cheap sessions are cheaper than any of the nine on main.

Net on the branch: the restart guard, the pending-once fix and the applied-note fix. The relaunch habit that remains is close + start with the same arguments; a window-size argument on uno_app_start (unoplatform/uno.app-mcp#214) removes one of its reasons, and the rest is the agent's belief about Hot Reload, which text and hooks have not moved.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants