Files
hermes-agent/gateway/session_mutation_policy.py
Teknium 8729dd9e74 fix(acp): restore Accept Edits / Don't Ask as the session's owner-held edit policy
The gateway ACP projection advertised only Default and refused main's other modes with
acp_edit_policy_mutation_unavailable: the owner had no verb for an editor's edit policy.

The policy is now plain data on the session's frozen LocalSessionPolicy
(edit_approval_policy: ask / workspace_session / session, default ask), written by a
revision-fenced `session.mutate edit_policy` ({policy}), so it lives in the session's stored
receipt and survives an owner restart. Only an ACP session takes it. The owner's edit guard
(gateway/session_local_editor.py) applies it before the editor's consent with main's rule
(should_auto_approve_edit): Accept Edits lets workspace/temp edits run, Don't Ask any edit,
and sensitive paths (.env, .git, .ssh, keys) always ask. session.info reports it to every
attached surface; the ACP projection advertises all three modes, maps set_session_mode and
set_config_option(edit_approval_policy) onto the mutation, shows a new editor the session's
mode, and projects another surface's change as current_mode_update.

A receipt main minted has no field: it restores as ask, and policy_identity skips the field
at its default, so stored credential references still match. The commit keeps the cached
agent (the policy is read per edit, not by the agent).

Refs #134889 (ACP-7)
Refs #141321
2026-10-18 18:58:27 -07:00

72 lines
3.7 KiB
Python

"""Prepare the policy-only session edits: session-scope reasoning and the workspace (Move to project).
Both rewrite the frozen LocalSessionPolicy the next turn runs under, re-bound like a model switch
(policy identity covers config/cwd, so its config-secret reference is re-fingerprinted).
"""
import asyncio
import json
from dataclasses import asdict, replace
from hermes_state_runtime import RuntimeStoreError
def _rebind(authority, prepared, old, policy, **extra):
from gateway.session_policy import bind_launch_key, launch_key
from gateway.session_policy_credentials import recover_config_secrets
secrets = recover_config_secrets(authority, old) if old.config_secret_ref else {}
policy = bind_launch_key(authority, prepared['snapshot']['receipt']['session_id'], policy,
launch_key(authority, old), config_secrets=secrets)
return dict(prepared, policy=asdict(policy), **extra)
async def prepare_reasoning(authority, live, payload, prepared):
"""``/reasoning <level>`` for this session: the level ``build_policy`` freezes at creation. ``None``
(``/reasoning reset``) gives the level back to the profile."""
from gateway.session_policy import pin_reasoning_effort, profile_config, restore_policy, unpin_reasoning
old = restore_policy(prepared['snapshot']['receipt']['policy'])
frozen = old.config()
if payload['effort'] is not None:
pin_reasoning_effort(frozen, payload['effort'], session=True)
else:
# A bypass launch never reads the profile: its reset is the built-in default.
profile = {} if old.ignore_user_config else await asyncio.to_thread(profile_config, authority)
unpin_reasoning(frozen, profile)
return _rebind(authority, prepared, old, replace(old, config_json=json.dumps(frozen)))
async def prepare_workspace(authority, live, payload, prepared):
"""Move to project: execution cwd and TERMINAL_CWD move together (restore_policy's invariant)."""
import os
from gateway.session_policy import restore_policy
from hermes_constants import translate_cwd_for_wsl_backend
from tui_gateway import git_probe
old = restore_policy(prepared['snapshot']['receipt']['policy'])
def probe():
# Off-loop: a stat and up to three `git` spawns.
cwd = os.path.abspath(os.path.expanduser(translate_cwd_for_wsl_backend(payload['cwd'])))
if not os.path.isdir(cwd):
raise RuntimeStoreError('invalid_params')
return cwd, git_probe.branch(cwd), git_probe.common_repo_root(cwd)
cwd, branch, root = await asyncio.to_thread(probe)
terminal = json.loads(old.terminal_json)
terminal['TERMINAL_CWD'] = cwd
policy = replace(old, cwd=cwd, terminal_json=json.dumps(terminal))
return _rebind(authority, prepared, old, policy, git={'branch': branch, 'root': root})
async def prepare_edit_policy(authority, live, payload, prepared):
"""Main's ACP modes (Default / Accept Edits / Don't Ask) as the session's frozen edit approval
policy. Only an ACP session's edits ask an editor; selecting the current policy writes nothing."""
from gateway.session_policy import restore_policy
old = restore_policy(prepared['snapshot']['receipt']['policy'])
if old.source != 'acp':
raise RuntimeStoreError('invalid_params', field='payload.policy',
detail='only an ACP session has an editor edit approval policy')
if old.edit_approval_policy == payload['policy']:
return {'status': 'noop', 'edit_approval_policy': old.edit_approval_policy}
return _rebind(authority, prepared, old, replace(old, edit_approval_policy=payload['policy']))
PREPARE = {'reasoning': prepare_reasoning, 'workspace': prepare_workspace, 'edit_policy': prepare_edit_policy}