testsAndMisc/docs/superpowers/evidence/unrooted-focus-mode-policy-and-owner-app-2026-08-05.json
Krzysztof kuhy Rudnicki f636d2b21b Make focus policy and blocklist usable without root; fix workout CDN unblock
phone_focus_mode is built for a rooted phone: the hosts layer is a read-only
bind mount over /system/etc/hosts, chosen so that even root-in-a-terminal
cannot overwrite it. None of that ports to a stock locked device. This splits
out the parts that are device-independent so one policy can drive both.

python_pkg/focus_policy parses config.sh rather than copying it, so the shell
config stays the source of truth and the two cannot drift. It rejects the
shipped REDACTED_* coordinate placeholder instead of defaulting to (0,0),
which would move "home" to the Atlantic and quietly disable the location gate.

blocklist.py converts the hosts file to domains for a DoT resolver or a local
VpnService. Three properties of the real 194k-line file make the obvious parser
wrong, and all three are handled: generate_hosts_file.sh *comments out*
deliberate unblocks rather than deleting them, so stripping '#' re-blocks
facebook/messenger/linkedin/4chan; the StevenBlack header carries loopback and
IPv6 housekeeping that would blackhole localhost; and ~1060 upstream annotation
tokens ('#[adware.zeno]', '"spam') render as address=/"spam/, which makes
dnsmasq refuse to start -- taking DNS down rather than blocking anything.
Verified with dnsmasq --test over all 185681 generated domains.

strip_workout_hosts.py had the same class of bug on the live rooted phone:
exact set membership meant the allowlisted googlevideo.com never matched the
dynamic r*---sn-*.googlevideo.com hosts YouTube actually streams from, so the
workout unblock was only partly working. Now suffix-matched (17 lines stripped,
was 14). The three new tests were checked against the original code in a
throwaway worktree; two of them fail there, so they pin the fix rather than
passing vacuously.

focus_owner is a device-owner app that deliberately does nothing yet. It ships
clearDeviceOwnerApp() implemented and tested first, because dpm
remove-active-admin does not work on a device owner: an app that locks the
device down before proving it can let go leaves a soft-brick recoverable only
through fastboot on a locked bootloader. Built, installed and launched on the
Pixel 6a; dumpsys afterwards confirms Device Owner Type -1 and no user
restrictions, i.e. the phone is unchanged.

Measured while scoping this, and not yet reflected in any enforcement code:
on Android 16 both pm suspend and pm disable-user are cleared by a reboot, so
shell-level app blocking is per-session only. setApplicationHidden is assumed
to persist under Device Owner but is untested.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AwepyL5Lxs5QKbKkMpxxxL
2026-08-05 09:15:02 +02:00

61 lines
5.8 KiB
JSON

{
"intent": "Make focus mode's policy and blocklist reusable on an unrooted phone, and ship a device-owner app whose removal path is proven before any restriction exists. Also fix a live bug that left YouTube's CDN blocked during workouts on the rooted phone.",
"scope": [
"python_pkg/focus_policy/ - new: typed policy model, config.sh loader, hosts->domain blocklist, JSON export",
"focus_owner/ - new: com.kuhy.focus_owner Flutter+Kotlin app, INERT (no restrictions applied, not provisioned)",
"phone_focus_mode/strip_workout_hosts.py - fix exact-match domain filter",
"linux_configuration/tests/test_strip_workout_hosts.py - regression tests for the above",
"Non-goal: no Device Owner provisioning, no factory reset, no enforcement of any kind",
"Non-goal: shell-side hosts/iptables enforcement is unchanged"
],
"changes": [
"focus_policy parses phone_focus_mode/config.sh rather than duplicating its lists, so the shell config stays the single source of truth. HOME_LAT/LON load from the gitignored config_secrets.sh; the shipped REDACTED_* placeholder is rejected loudly instead of defaulting to (0,0), which would silently relocate 'home' to the Atlantic and disable the location gate.",
"blocklist.py converts the hosts file to domains. It honours commented-out entries as deliberate unblocks (generate_hosts_file.sh comments rather than deletes, so a naive parser re-blocks facebook/linkedin/4chan), skips loopback and IPv6 housekeeping lines, and rejects ~1060 upstream annotation tokens such as '#[adware.zeno]' and '\"spam' that would otherwise render as address=/\"spam/ and make dnsmasq refuse to start.",
"strip_workout_hosts.py now matches domain suffixes instead of exact set membership. YouTube streams from dynamic r*---sn-*.googlevideo.com hosts, which never equalled the allowlisted 'googlevideo.com', so the workout unblock was only partially effective on the live rooted phone.",
"focus_owner ships DevicePolicyBridge.releaseDeviceOwner() (clearDeviceOwnerApp) implemented and tested BEFORE any restriction. dpm remove-active-admin does not work on a device owner, so an app that locks down before proving it can let go leaves a soft-brick recoverable only via fastboot on a locked bootloader."
],
"verification": [
{
"command": "pre-commit run --files python_pkg/focus_policy/*.py python_pkg/focus_policy/tests/*.py",
"result": "pass",
"evidence": "All hooks pass including ruff, ruff format, mypy (strict), pylint, bandit, no-noqa. Zero lint suppressions: the three initially-added ones (S104, two type: ignore) were removed by fixing the underlying code, not by suppressing."
},
{
"command": "coverage run --branch --source=python_pkg/focus_policy -m pytest python_pkg/focus_policy/tests/ && coverage report",
"result": "pass",
"evidence": "109 passed. 219 statements, 56 branches, 0 missed = 100% branch coverage across all 5 modules."
},
{
"command": "dnsmasq --test --conf-file=<generated blocklist.conf>",
"result": "pass",
"evidence": "'dnsmasq: syntax check OK.' over all 185681 domains generated from the real 194463-line hosts file. An earlier run FAILED with 'missing \" at line 1' until invalid-domain rejection was added - this check is what caught it."
},
{
"command": "pytest linux_configuration/tests/test_strip_workout_hosts.py",
"result": "pass",
"evidence": "8 passed (5 pre-existing + 3 new). Non-vacuity confirmed: the 3 new tests were run against the ORIGINAL code in a throwaway git worktree and 2 of 3 FAILED there, so they genuinely pin the fix."
},
{
"command": "cd focus_owner && flutter analyze && flutter test",
"result": "pass",
"evidence": "'No issues found!' and '+10: All tests passed!' covering status parsing, the release path, null-reply handling, and the confirm-before-release dialog."
},
{
"command": "flutter build apk --debug && adb -s 23181JEGR08034 install -r && am start",
"result": "pass",
"evidence": "Built, installed, and launched on the Pixel 6a. Screenshot shows live platform-channel data ('Android SDK 36' read from the device), 'Device owner: no', 'Restrictions: none (inert build)'. Post-run dumpsys confirms Device Owner Type: -1, user restrictions 'none', mAppsSuspended=false - the phone is unchanged."
}
],
"risks": [
"focus_owner declares a DeviceAdminReceiver. Declaring it is inert - it applies nothing until someone runs dpm set-device-owner, which cannot succeed while the device has accounts. Still, the component now exists and should not be provisioned until the release path is exercised on a real device.",
"The suffix matching in strip_workout_hosts.py releases a broader set than before (17 lines stripped vs 14). That is the intended fix, but it means any future entry in WORKOUT_UNBLOCK_DOMAINS now releases its entire subtree - a short domain there would release a lot.",
"focus_policy duplicates the Haversine and curfew semantics of focus_daemon.sh in Python. The two must stay in agreement; the loader reads the same config.sh, but the geometry is a reimplementation, not a shared one.",
"Measured this session and NOT yet reflected in any enforcement code: both pm suspend and pm disable-user are cleared by a reboot on Android 16. setApplicationHidden is assumed to persist but is UNTESTED."
],
"rollback": [
"git revert the commit. focus_policy and focus_owner are new directories with no other module importing them, so removing them affects nothing else.",
"The only change to live behaviour is strip_workout_hosts.py, used by deploy.sh when generating the workout hosts variant on the PC. Reverting restores exact-match filtering (i.e. restores the bug).",
"After rollback validate: pytest linux_configuration/tests/test_strip_workout_hosts.py still passes, and phone_focus_mode/deploy.sh still produces a workout variant."
]
}