diff --git a/src/grimoire/services/write_scope.py b/src/grimoire/services/write_scope.py index c04d4de1..384fe096 100644 --- a/src/grimoire/services/write_scope.py +++ b/src/grimoire/services/write_scope.py @@ -303,8 +303,21 @@ async def _titles(ids: list[int]) -> dict[int, str]: def _named(project_id: int, titles: dict[int, str]) -> str: - title = titles.get(project_id) - return f"{title} (project {project_id})" if title else f"project {project_id}" + return titles.get(project_id) or f"project {project_id}" + + +def _doing(tool: str, act: str) -> str: + """The act in words a person reads at a glance: "Retire family idea", + "git push", "Editing a file". A tool name is spelled out, not quoted.""" + act = act.strip() + if act.endswith(" in"): + act = act[:-3] + if act: + return act[:1].upper() + act[1:] + if tool: + words = tool.replace("_", " ").strip() + return words[:1].upper() + words[1:] + return "This change" async def check_act( @@ -343,22 +356,24 @@ async def check_act( targets = [{"id": p, "title": titles.get(p, "")} for p in others] if tool and is_issue_filing(tool, args): return {"allowed": True, "targets": targets, "issue": True, "reason": ""} + # The reason is the permission prompt's text: the operator reads it + # first, so it leads with what changes and where, then when to approve. + # The session reads it after a "no", so the last line is for the agent. there = ", ".join(_named(p, titles) for p in others) + home = _named(session_project, titles) if tool in RULE_CURATION_TOOLS: rules = sorted({r for name in _RULE_ARGS for r in _positive_ints(args.get(name))}) which = ", ".join(f"rule {r}" for r in rules) or "a rule" reason = ( - f"This session belongs to {_named(session_project, titles)}. {tool} changes " - f"{which} of {there}, a different project. Rules are the operator's to " - f"curate from any session: approve if they asked for this change." + f"Changing {which} in {there}, but this session is working in {home}.\n" + "Rules are yours to curate from any session. Approve if you asked for this change." ) return {"allowed": False, "targets": targets, "issue": False, "reason": reason} - what = act.strip() or (f"{tool} changes" if tool else "This changes") first = others[0] reason = ( - f"This session belongs to {_named(session_project, titles)}. {what} {there}, " - f"a different project. From here, the one change to make there is filing an " - f"issue: create_task(project_id={first}, kind=\"issue\") with how it was found " - f"and the evidence. Approve only if this session should change {there} itself." + f"{_doing(tool, act)} in {there}, but this session is working in {home}.\n" + f"Approve only if you asked for this change to {there}.\n" + f"(If not: the agent can report it there instead, with " + f"create_task(project_id={first}, kind=\"issue\").)" ) return {"allowed": False, "targets": targets, "issue": False, "reason": reason} diff --git a/tests/test_write_scope.py b/tests/test_write_scope.py index 98677a62..87b544e5 100644 --- a/tests/test_write_scope.py +++ b/tests/test_write_scope.py @@ -147,9 +147,10 @@ async def test_a_write_in_another_project_asks_and_says_how_to_file_an_issue_ins out = await check_act(1, 2, tool="update_task", args={"task_id": 5}) assert out["allowed"] is False assert out["targets"] == [{"id": 9, "title": "Elsewhere"}] - assert "Home (project 2)" in out["reason"] - assert "Elsewhere (project 9)" in out["reason"] - assert 'create_task(project_id=9, kind="issue")' in out["reason"] + first, ask, agent = out["reason"].split("\n") + assert first == "Update task in Elsewhere, but this session is working in Home." + assert ask == "Approve only if you asked for this change to Elsewhere." + assert 'create_task(project_id=9, kind="issue")' in agent async def test_a_rule_edit_in_another_project_names_the_rule_and_suggests_no_issue(): @@ -157,12 +158,12 @@ async def test_a_rule_edit_in_another_project_names_the_rule_and_suggests_no_iss with a, b, c: out = await check_act(1, 2, tool="update_rule", args={"rule_id": 130, "salience": "low"}) assert out["allowed"] is False - assert "rule 130 of Elsewhere (project 9)" in out["reason"] + assert out["reason"].startswith("Changing rule 130 in Elsewhere, but this session is working in Home.") assert "create_task" not in out["reason"] with a, b, c: related = await check_act(1, 2, tool="relate_rules", args={"from_rule_id": 4, "to_rule_id": 130, "kind": "overrides"}) - assert "rule 4, rule 130 of" in related["reason"] + assert related["reason"].startswith("Changing rule 4, rule 130 in Elsewhere") async def test_filing_an_issue_in_another_project_goes_ahead(): @@ -192,7 +193,7 @@ async def test_a_directory_in_another_project_asks_in_the_words_of_the_act(): out = await check_act(1, 2, target_project=9, act="git push in") same = await check_act(1, 2, target_project=2, act="git push in") assert out["allowed"] is False - assert "git push in Elsewhere (project 9)" in out["reason"] + assert out["reason"].startswith("Git push in Elsewhere, but this session is working in Home.") assert same["allowed"] is True