diff --git a/docs/superpowers/evidence/flag-system-sweep-2026-08-10.json b/docs/superpowers/evidence/flag-system-sweep-2026-08-10.json new file mode 100644 index 00000000..f0255021 --- /dev/null +++ b/docs/superpowers/evidence/flag-system-sweep-2026-08-10.json @@ -0,0 +1,55 @@ +{ + "intent": "Let the Device Owner sweep hide the preinstalled apps it exists to block -- YouTube and Chrome ship with FLAG_SYSTEM and were skipped entirely -- without exposing the rest of the platform to being hidden.", + "scope": [ + "python_pkg/focus_policy/export.py", + "python_pkg/focus_policy/tests/test_export.py", + "focus_owner/android/app/src/main/kotlin/com/kuhy/focus_owner/FocusPolicy.kt", + "focus_owner/android/app/src/main/kotlin/com/kuhy/focus_owner/EnforcementRunner.kt", + "focus_owner/assets/policy.json", + "Non-goal: Device Owner provisioning itself, which still needs the factory reset" + ], + "changes": [ + "Added BLOCKABLE_SYSTEM_PACKAGES to the exporter, rendered as a new blockable_system_packages field: chrome, vending, youtube, youtube.music, videos. com.android.vending is included deliberately -- leaving Play reachable makes every other removal a one-tap undo.", + "The exporter subtracts anything already allowed, so an asset can never declare a package both allowed and blockable.", + "EnforcementRunner.installedThirdPartyPackages became sweepablePackages(policy): third-party apps as before, plus system apps the policy names. System apps stay default-deny.", + "FocusPolicy gained blockableSystemPackages, parsed with a new optionalStringSet so an asset predating the field reads as the empty set instead of throwing -- a throw would make decide() skip the pass and silently disable enforcement." + ], + "verification": [ + { + "command": "measured blast radius before changing anything: pm list packages -s vs the policy", + "result": "pass", + "evidence": "320 system packages on the device, of which 243 match no allowlist entry and no never_disable_prefix -- including com.android.cellbroadcastreceiver (emergency alerts), com.android.credentialmanager and com.android.devicelockcontroller. This is why the filter was not simply deleted." + }, + { + "command": "profile-owner harness (user 10), tap 'Run enforcement now' with YouTube installed", + "result": "pass", + "evidence": "'setApplicationHidden(com.google.android.youtube,true)=true' then 'applied LOCATION_UNKNOWN: hid 1, restored 0'; dumpsys confirms hidden=true. Before this change the package was invisible to the sweep." + }, + { + "command": "same harness with YouTube temporarily added to the allowlist", + "result": "pass", + "evidence": "'setApplicationHidden(com.google.android.youtube,false)=true', 'restored 1', hidden=false. Both directions of the system-app path work." + }, + { + "command": "dumpsys on safety-critical system packages after the sweep", + "result": "pass", + "evidence": "com.android.cellbroadcastreceiver, com.android.credentialmanager, com.android.devicelockcontroller and com.android.settings all installed=true hidden=false. All five blockable packages are hidden=true. Default-deny holds." + }, + { + "command": "pytest python_pkg/focus_policy/ ; gradlew :app:testDebugUnitTest ; flutter build apk --debug", + "result": "pass", + "evidence": "125 Python tests pass, 100% branch coverage on focus_policy. Kotlin unit tests BUILD SUCCESSFUL. APK builds." + } + ], + "risks": [ + "Hiding com.android.vending under Device Owner removes the user's ability to install or update ANY app from Play, not just to reinstall YouTube. That is the intent, but it is a large blast radius and should be applied late, after the release path has been re-tested -- consistent with the wipe checklist's 'one restriction at a time'.", + "BLOCKABLE_SYSTEM_PACKAGES is a hardcoded list in the exporter rather than something derived from config.sh. Adding a new preinstalled distraction means editing Python, not the shell config. Chosen deliberately: this list is a safety boundary, and a typo in config.sh should not be able to widen it.", + "Verified under a profile owner, not a device owner. The DPM calls are identical but the package set differs: a work profile does not provision every system app (talkback, for example, is installed=false there), so the real user-0 sweep will see more packages than this harness did.", + "An intermediate `comm` on visible-vs-all package lists made it look like the sweep had hidden ~60 system packages. It had not -- those were absent from the profile, not hidden. Distinguishing installed= from hidden= is what settled it; the app logged exactly one setApplicationHidden call." + ], + "rollback": [ + "git revert. The Kotlin change falls back to filtering every FLAG_SYSTEM package, and the extra asset field is then ignored.", + "After reverting, regenerate the asset so blockable_system_packages disappears: python -m python_pkg.focus_policy --config phone_focus_mode/config.sh --redact-home --output focus_owner/assets/policy.json", + "Anything already hidden stays hidden until a pass unhides it; on this device the same apps are also removed via distraction_purge.sh, which is independent." + ] +} diff --git a/focus_owner/android/app/src/main/kotlin/com/kuhy/focus_owner/EnforcementRunner.kt b/focus_owner/android/app/src/main/kotlin/com/kuhy/focus_owner/EnforcementRunner.kt index 8d2fda09..05b3e659 100644 --- a/focus_owner/android/app/src/main/kotlin/com/kuhy/focus_owner/EnforcementRunner.kt +++ b/focus_owner/android/app/src/main/kotlin/com/kuhy/focus_owner/EnforcementRunner.kt @@ -49,7 +49,7 @@ class EnforcementRunner(private val context: Context) { return EnforcementDecision.evaluate( effective, EnforcementInputs( - installedPackages = installedThirdPartyPackages(), + installedPackages = sweepablePackages(effective), minutesSinceMidnight = minutes, latitude = fix?.first, longitude = fix?.second, @@ -94,7 +94,18 @@ class EnforcementRunner(private val context: Context) { } /** - * Third-party packages, the only ones the allowlist sweep considers. + * Packages the allowlist sweep considers: every third-party app, plus the + * system apps the policy names in `blockable_system_packages`. + * + * System apps are default-deny rather than filtered out entirely, because + * the apps most worth blocking are preinstalled — YouTube ships at + * `/product/app/YouTube` with `FLAG_SYSTEM`, and so does Chrome — while + * most of the rest are platform components. Measured on this device: 320 + * system packages, of which 243 match no allowlist entry and no + * `never_disable_prefix`, including `com.android.cellbroadcastreceiver` + * (emergency alerts) and `com.android.devicelockcontroller`. Sweeping + * those under Device Owner risks an unrecoverable phone, so eligibility is + * opt-in per package. * * [PackageManager.MATCH_UNINSTALLED_PACKAGES] is required, not optional: a * package hidden by `setApplicationHidden` is dropped from the default @@ -105,10 +116,14 @@ class EnforcementRunner(private val context: Context) { * on device: after a pass hid three apps, the following pass reported * `hid 0, restored 0` and left them hidden permanently. */ - private fun installedThirdPartyPackages(): Set { + private fun sweepablePackages(policy: FocusPolicy): Set { val pm = context.packageManager return pm.getInstalledApplications(PackageManager.MATCH_UNINSTALLED_PACKAGES) - .filter { (it.flags and android.content.pm.ApplicationInfo.FLAG_SYSTEM) == 0 } + .filter { info -> + val isSystem = + (info.flags and android.content.pm.ApplicationInfo.FLAG_SYSTEM) != 0 + !isSystem || info.packageName in policy.blockableSystemPackages + } .map { it.packageName } .toSet() } diff --git a/focus_owner/android/app/src/main/kotlin/com/kuhy/focus_owner/FocusPolicy.kt b/focus_owner/android/app/src/main/kotlin/com/kuhy/focus_owner/FocusPolicy.kt index 3a7b5ed1..c7f457d8 100644 --- a/focus_owner/android/app/src/main/kotlin/com/kuhy/focus_owner/FocusPolicy.kt +++ b/focus_owner/android/app/src/main/kotlin/com/kuhy/focus_owner/FocusPolicy.kt @@ -61,6 +61,17 @@ data class FocusPolicy( val workoutUnblockDomains: Set, val curfew: CurfewWindow?, val launcherPackage: String?, + /** + * System apps the sweep may hide, named individually. + * + * The sweep is default-deny for `FLAG_SYSTEM` packages, because most of + * them are platform components: on this device 243 of 320 match no + * allowlist entry and no prefix, including the emergency-alert receiver. + * Only packages named here are eligible, and an asset that predates this + * field parses to the empty set, preserving the old never-touch-system + * behaviour. + */ + val blockableSystemPackages: Set = emptySet(), ) { /** * Whether a package must never be hidden. @@ -143,6 +154,7 @@ data class FocusPolicy( nightAllowedPackages = json.stringSet("night_allowed_packages"), neverDisablePrefixes = json.stringSet("never_disable_prefixes"), workoutUnblockDomains = json.stringSet("workout_unblock_domains"), + blockableSystemPackages = json.optionalStringSet("blockable_system_packages"), curfew = curfewJson?.let { CurfewWindow( startMinutes = parseHhMm(it.getString("start"), "curfew.start"), @@ -163,6 +175,20 @@ data class FocusPolicy( return (0 until array.length()).mapTo(mutableSetOf()) { array.getString(it) } } + /** + * A list that may be absent, read as empty. + * + * Used for fields added after assets were already shipped. The strict + * [stringSet] would throw, and an unparsable policy makes the runner + * skip the pass entirely — safe, but it silently disables enforcement + * rather than degrading to the previous behaviour. A present-but-wrong + * type is still an error, since that is a real mistake. + */ + private fun JSONObject.optionalStringSet(field: String): Set { + if (!has(field)) return emptySet() + return stringSet(field) + } + private fun parseHhMm(value: String, field: String): Int { val parts = value.split(":") val hour = parts.getOrNull(0)?.toIntOrNull() diff --git a/focus_owner/assets/policy.json b/focus_owner/assets/policy.json index 4405423d..a85aece4 100644 --- a/focus_owner/assets/policy.json +++ b/focus_owner/assets/policy.json @@ -87,6 +87,13 @@ "pl.plksa.portalpasazera", "ws.xsoh.etar" ], + "blockable_system_packages": [ + "com.android.chrome", + "com.android.vending", + "com.google.android.apps.youtube.music", + "com.google.android.videos", + "com.google.android.youtube" + ], "browser_packages": [ "com.android.chrome", "org.mozilla.fenix" diff --git a/python_pkg/focus_policy/export.py b/python_pkg/focus_policy/export.py index 681f7173..376002a9 100644 --- a/python_pkg/focus_policy/export.py +++ b/python_pkg/focus_policy/export.py @@ -27,6 +27,30 @@ SCHEMA_VERSION = 1 # rather than left to a hand-edit of the rendered asset. ENFORCER_PACKAGE = "com.kuhy.focus_owner" +# System apps the sweep is allowed to hide, named one by one. +# +# The Kotlin runner skips every package with FLAG_SYSTEM, which means the +# Device Owner path cannot touch the apps most worth blocking: YouTube ships +# at /product/app/YouTube as a system app, and so does Chrome. Simply dropping +# that filter is not the fix -- measured on the Pixel 6a, it would expose 320 +# system packages of which 243 match no allowlist entry and no +# never_disable_prefix, including com.android.cellbroadcastreceiver (emergency +# alerts), com.android.credentialmanager and com.android.devicelockcontroller. +# Hiding those under Device Owner risks an unrecoverable device. +# +# So the sweep stays default-deny for system apps and this is the opt-in. +# com.android.vending is included deliberately: leaving Play reachable makes +# every other removal a one-tap undo. +BLOCKABLE_SYSTEM_PACKAGES = frozenset( + { + "com.android.chrome", + "com.android.vending", + "com.google.android.apps.youtube.music", + "com.google.android.videos", + "com.google.android.youtube", + }, +) + def policy_to_dict(policy: FocusPolicy) -> dict[str, Any]: """Return a JSON-serialisable representation of ``policy``.""" @@ -54,6 +78,12 @@ def policy_to_dict(policy: FocusPolicy) -> dict[str, Any]: "never_disable_prefixes": sorted(policy.never_disable_prefixes), "workout_unblock_domains": sorted(policy.workout_unblock_domains), "browser_packages": sorted(policy.browser_packages), + # Absent from an older asset, so the Kotlin loader must treat a + # missing key as the empty set -- i.e. keep today's behaviour of + # never touching a system app. + "blockable_system_packages": sorted( + BLOCKABLE_SYSTEM_PACKAGES - {*policy.allowed_packages, ENFORCER_PACKAGE}, + ), } diff --git a/python_pkg/focus_policy/tests/test_export.py b/python_pkg/focus_policy/tests/test_export.py index 9542ac49..3b87a320 100644 --- a/python_pkg/focus_policy/tests/test_export.py +++ b/python_pkg/focus_policy/tests/test_export.py @@ -71,6 +71,38 @@ def test_enforcer_is_not_duplicated_when_already_present() -> None: assert payload["night_allowed_packages"].count(ENFORCER_PACKAGE) == 1 +def test_blockable_system_packages_names_the_distraction_apps() -> None: + """The sweep is default-deny for system apps; this is the opt-in. + + YouTube and Chrome ship with FLAG_SYSTEM, so without this list the Device + Owner sweep skips exactly the apps it exists to block. Play is included + because leaving it reachable makes every other removal a one-tap undo. + """ + blockable = policy_to_dict(_policy())["blockable_system_packages"] + + assert "com.google.android.youtube" in blockable + assert "com.android.chrome" in blockable + assert "com.android.vending" in blockable + + +def test_blockable_system_packages_never_contradicts_the_allowlist() -> None: + """An allowed package must not also be listed as blockable. + + Otherwise the asset would state both at once and the outcome would depend + on which check the runner happened to apply first. + """ + payload = policy_to_dict( + _policy( + allowed_packages=frozenset( + {"pl.mbank", "com.launcher", "com.android.chrome"}, + ), + ), + ) + + assert "com.android.chrome" not in payload["blockable_system_packages"] + assert "com.android.chrome" in payload["allowed_packages"] + + def test_lists_are_sorted_for_stable_diffs() -> None: """Sorted output keeps a committed asset's diff meaningful.""" payload = policy_to_dict(_policy())