Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ LaunchAgent com.openkakao.auto-reply.session-monitor
-> caffeinate openkakao-cli auto-reply
```

`src/commands/auto_reply_host.rs` holds `auto-reply-host` (`--status`, `--bake`, `--disable`, `--tick`). The lock chain is `session-monitor.lock`, then `session-watchdog.owner.lock`, then `supervisor.owner.lock`. `docs/auto-reply-launchd-supervision.md` still describes an `open -g -b com.apple.Terminal` launch. The tick code uses `/bin/sh`.
`src/commands/auto_reply_host.rs` holds `auto-reply-host` (`--status`, `--bake`, `--disable`, `--tick`). The lock chain is `session-monitor.lock`, then `session-watchdog.owner.lock`, then `supervisor.owner.lock`. The tick starts the pinned `.command` with `/bin/sh`; `docs/auto-reply-launchd-supervision.md` is the runbook.

Diagrams for these flows are in `docs/architecture/`. `jarvis-system.html` is the end-to-end map.

Expand Down
157 changes: 93 additions & 64 deletions docs/auto-reply-launchd-supervision.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

| Artifact | Status |
|---|---|
| `scripts/auto-reply-session-monitor.py` | **current** host |
| `src/commands/auto_reply_host.rs` (`openkakao-cli auto-reply-host --tick`) | **current** host; the session-monitor plist runs it |
| `scripts/auto-reply-session-monitor.py` | **retired** Terminal-opening monitor; still staged into the runtime, but the baked plist does not run it |
| `scripts/prepare-auto-reply-session-runtime.py` | **current** bake |
| `scripts/auto-reply-service.py` | **current** Terminal session entry |
| `scripts/auto-reply-service.py` | **current** session entry |
| `scripts/auto-reply-supervisor.py` | **current** |
| `scripts/auto-reply-db-watch.py` | **current** |
| `scripts/auto-reply-worker.py` | **current** |
Expand All @@ -32,42 +33,66 @@
| `src/auto_reply_service.rs` LaunchAgent labels | **legacy identity** |
| `src/auto_reply_service.rs` status/log/hook helpers | **current shared** (`ax-watch`, health binary) |

This runbook covers two distinct layouts. The current layout restores the
database-authoritative foreground CLI inside the logged-in user's Aqua session
through Terminal. A per-user LaunchAgent owns only a small, Kakao-blind monitor;
it never owns the AX/database worker. The older watch/health pair remains
documented separately for observation and alerting. Do not run both layouts as
reply owners.
This runbook covers two distinct layouts. The current layout runs the
database-authoritative foreground CLI inside the logged-in user's Aqua session,
started by a per-user LaunchAgent. The LaunchAgent's own process is a small,
Kakao-blind tick; it never owns the AX/database worker. The older watch/health
pair remains documented separately for observation and alerting. Do not run
both layouts as reply owners.

<a id="persistent-auto-reply-launchagent"></a>

## Current-user persistence: monitor LaunchAgent and Terminal host
## Current-user persistence: session-monitor LaunchAgent and host tick

The current monitor label is
`com.openkakao.auto-reply.session-monitor`. It is a one-shot process limited to
the logged-in Aqua session. It validates a private manifest and the SHA-256 of
an immutable mode-`0500` `.command`, checks the session-watchdog owner lock, and
only when the lock is free requests this exact command with:
`com.openkakao.auto-reply.session-monitor`. It is limited to the logged-in Aqua
session. Each run executes the stable staged binary as
`openkakao-cli auto-reply-host --tick --manifest <manifest> --state-root <root>`
(`src/commands/auto_reply_host.rs`). The tick:

1. validates the private manifest and the SHA-256 of the immutable mode-`0500`
`start-auto-reply-session.command` it names;
2. clears `com.apple.quarantine` from the runtime directory so Gatekeeper does
not prompt on every restart;
3. takes `session-monitor.lock`, honors the `session-monitor.disabled`
sentinel, and returns without launching while `session-watchdog.owner.lock`
or `supervisor.owner.lock` is held;
4. applies a 60-second cooldown and a circuit of at most three launches per
15 minutes; and
5. starts the pinned command directly:

```text
/usr/bin/open -g -b com.apple.Terminal <pinned-command>
/bin/sh <pinned-command>
```

Do not add `-j` or `--hide`: they are the same flag, and it can hide an
already-open Terminal application. The monitor never scripts Terminal windows:
it does not learn which window it opened, so closing or minimizing by title
could reach a window the user owns.
The child gets a cleared environment (`HOME`, `PATH=/usr/bin:/bin`,
`TMPDIR=/tmp`) and `/dev/null` for stdin, stdout and stderr. The plist sets
`AbandonProcessGroup=true`, so launchd leaves the watchdog running after the
tick exits.

The monitor does not read the KakaoTalk database, call Accessibility or System
The tick does not use `open -g -b com.apple.Terminal`, Terminal, or Apple
Events. Opening the `.command` document through LaunchServices made Gatekeeper
ask to allow a freshly packaged launcher and made macOS ask for Automation
permission on every restart, so the tick starts the service as its own child
instead. `scripts/auto-reply-session-monitor.py` still holds the older
Terminal-opening monitor and is still copied into each runtime, but the baked
plist does not run it.

The tick does not read the KakaoTalk database, call Accessibility or System
Events, inspect the OpenKakao configuration or Codex credentials, generate a
reply, or send a message. It reads only its private manifest/status/lock files
and uses a cooldown plus a bounded launch-attempt circuit. Consequently,
granting Accessibility or Full Disk Access to the monitor or to a generic
launchd process is neither required nor part of this layout.

Terminal is the existing user-approved TCC trust boundary. The pinned command
starts `auto-reply-service.py --mode session` in Terminal with
isolated Python flags (`-E -B -S`) and stdin from `/dev/null`. The session
reply, or send a message. It reads only its private manifest/status/lock files.

The pinned command starts `auto-reply-service.py --mode session` with isolated
Python flags (`-E -B -S`), a cleared environment, and stdin from `/dev/null`.
No Terminal sits in the chain, so Terminal's TCC grants do not cover the
service. macOS keys Full Disk Access and Automation approval on a file path, so
the packager keeps one signed CLI at the fixed path
`<state-root>/../bin/openkakao-cli` and points both the plist and the command at
it (`_stage_stable_binary` in `scripts/prepare-auto-reply-session-runtime.py`).
The operator approves that path once, and the approval survives re-bakes. The
worker and DB watcher prefer the same path (`_stable_cli_path`). Approve every
permission the reply path needs, Accessibility for the AX send included, on that
path, and prove it with the read-only preflight rather than a loaded plist. The session
watchdog takes `session-watchdog.owner.lock`, performs a fresh read-only
identity-bound preflight before every child start, and only then runs the same
`openkakao-cli auto-reply --chat <exact-selector>` foreground safety gates
Expand All @@ -76,18 +101,15 @@ watchdog-to-guardian control pipe and guardian-to-auto-reply liveness pipe are
EOF-only ownership proofs: death of either owner stops all room worker process
groups before a bounded-backoff restart. The liveness descriptor is validated
as a read-only FIFO, marked close-on-exec, and removed from worker environments
before supervisors are spawned. Neither the monitor, watchdog, nor guardian
before supervisors are spawned. Neither the tick, watchdog, nor guardian
adopts or kills an unrelated process.

This is login recovery, not execution through power-off or logout. After a
reboot, the service can return only after the same user logs in and Aqua,
Terminal, Terminal's existing user-granted TCC authorization, logged-in
KakaoTalk, and the exact window are available. It cannot answer while the Mac
is powered off or the user is logged out, and it does not bypass KakaoTalk
logout, a locked-down TCC policy, or an unavailable window. If a managed
deployment must remove the Terminal dependency, the long-term design is a
separately signed native host with its own explicit user-granted macOS
permissions—not an attempt to disguise a bare LaunchAgent as Terminal.
reboot, the service can return only after the same user logs in and Aqua, the
TCC grants on the stable binary, logged-in KakaoTalk, and the exact window are
available. It cannot answer while the Mac is powered off or the user is logged
out, and it does not bypass KakaoTalk logout, a locked-down TCC policy, or an
unavailable window.

### Required configuration

Expand Down Expand Up @@ -214,25 +236,29 @@ openkakao-cli auto-reply \
--json
```

Stage the release binary, service entrypoint, monitor, and all runtime assets in
a private immutable runtime directory. The Terminal command must be a static
file—not a mutable project checkout or a shell command assembled by the
monitor. A representative command is:
Stage the release binary, service entrypoint, and all runtime assets in a
private immutable runtime directory with
`scripts/prepare-auto-reply-session-runtime.py` (below). The pinned command
must be a static file, not a mutable project checkout or a shell command
assembled by the tick. The packager writes it; a representative command is:

```sh
#!/bin/sh
exec '/absolute/path/to/python3' -E -B -S \
'/absolute/private/runtime/auto-reply-service.py' \
umask 077
/usr/bin/env -i HOME='/Users/me' PATH=/opt/homebrew/bin:/usr/bin:/bin TMPDIR=/tmp \
'/absolute/path/to/python3' -E -B -S \
'/absolute/private/runtime/scripts/auto-reply-service.py' \
--mode session \
--python '/absolute/path/to/python3' \
--entry '/absolute/private/runtime/auto-reply-service.py' \
--bin '/absolute/private/runtime/openkakao-cli' \
--config '/Users/me/.config/openkakao/config.toml' \
--chat 'bind:417780809780519:부자멘토멘티' \
--entry '/absolute/private/runtime/scripts/auto-reply-service.py' \
--bin '/Users/me/Library/Application Support/openkakao/bin/openkakao-cli' \
--config '/absolute/private/runtime/config.toml' \
--state-root '/Users/me/Library/Application Support/openkakao/auto-reply' \
</dev/null \
>>'/Users/me/Library/Application Support/openkakao/auto-reply/session-service/watchdog.out.log' \
2>>'/Users/me/Library/Application Support/openkakao/auto-reply/session-service/watchdog.err.log'
status=$?
exit "$status"
```

The state root and runtime directory must be user-owned mode `0700`; the
Expand All @@ -251,14 +277,15 @@ root and command digest:
}
```

Install an Aqua-only per-user LaunchAgent whose `ProgramArguments` invoke the
pinned CPython with `-E -B -S`, then the pinned
`auto-reply-session-monitor.py`, `--manifest <absolute-manifest>`, and
`--state-root <absolute-state-root>`. Use `RunAtLoad=true` plus a bounded
`StartInterval`; do not use `KeepAlive` for the one-shot monitor. Its standard
input should be `/dev/null`, and its logs should live under the private state
root. Loading the plist does not itself prove that Terminal accepted the handoff
or that the reply worker is ready.
The packager also writes the candidate Aqua-only per-user LaunchAgent. Its
`ProgramArguments` are the stable binary, `auto-reply-host`, `--tick`,
`--manifest <absolute-manifest>`, and `--state-root <absolute-state-root>`. It
sets `RunAtLoad=true`, `AbandonProcessGroup=true`, `StartInterval` and
`ThrottleInterval` to the same bounded interval, `ProcessType=Standard`,
`LimitLoadToSessionType=Aqua`, and `Umask` `077`; it does not use `KeepAlive`
for the one-shot tick. Its standard input is `/dev/null`, and its logs live
under `<state-root>/session-monitor/`. Loading the plist does not itself prove
that the tick started the watchdog or that the reply worker is ready.

Check all three layers:

Expand All @@ -275,7 +302,8 @@ jq '{readiness,mode,target_chat_id,fence_reason,readiness_reasons}' \
```

`watchdog_running` in monitor status means the watchdog owner lock was held;
`launch_requested` means only that macOS accepted the request to open Terminal.
`launch_requested` means only that the tick spawned `/bin/sh` on the pinned
command; `open_failed` means the spawn itself failed.
It is not readiness. The watchdog must report fresh `mode=current_login_session`
and `state=running`, and the room supervisor must independently report fresh
database-authoritative readiness. The watchdog status deliberately records that
Expand All @@ -285,7 +313,7 @@ recreate it only on a later logged-in Aqua session.
Changing the binary, configuration, chat selector, service entrypoint, runtime
asset, command, or interpreter requires a new immutable runtime, a new command
digest/manifest, and a fresh preflight. A private mode-`0600`
`session-monitor.disabled` sentinel stops new Terminal handoffs. Disable the
`session-monitor.disabled` sentinel stops new launches. Disable the
monitor before stopping the verified watchdog PID so the next interval cannot
immediately reopen it.

Expand Down Expand Up @@ -397,24 +425,25 @@ scripts/status-auto-reply-service.sh \
--chat-id 123456789
```

It recognizes both the Terminal monitor and the older direct compatibility
It recognizes both the session monitor and the older direct compatibility
service, rejects simultaneous control planes, and requires fresh independent
room readiness for every selected room. To remove only Terminal persistence,
room readiness for every selected room. To remove only session-monitor persistence,
run `scripts/uninstall-auto-reply-session-monitor.sh`. That helper creates the
private disable sentinel before verified `bootout`, preserves the plist and all
private disable sentinel before verified `bootout`, renames the plist to
`<plist>.disabled.<unix-time>.<pid>`, preserves all
runtime/room state, and deliberately does not kill a running watchdog or worker.

### Direct LaunchAgent compatibility path

`install-auto-reply-service.sh --mode production` installs the
older direct service label `com.openkakao.auto-reply.autoreply`; it does not
install the Terminal-mediated monitor described above. That path is valid only
install the session monitor described above. That path is valid only
if the exact launchd execution identity has independently been granted and
verified for every required protected-data and Accessibility/TCC operation. A
successful interactive-shell preflight, a loaded plist, or Terminal's TCC
authorization does not prove that direct launchd identity. On a host where
Terminal is authorized but the direct launchd identity is denied, use the
current-user Terminal layout and leave the direct service unloaded.
successful interactive-shell preflight, a loaded plist, or a grant on the
session monitor's stable binary does not prove that direct launchd identity. On
a host where that identity is denied, use the session-monitor layout and leave
the direct service unloaded.

## Legacy watch/health LaunchAgents

Expand Down
Loading