diff --git a/osiris_cli/living.py b/osiris_cli/living.py index 6c41d55c..b4028ffb 100644 --- a/osiris_cli/living.py +++ b/osiris_cli/living.py @@ -136,12 +136,18 @@ def repeating(text: str, span: int = REPEAT_SPAN, limit: int = REPEAT_LIMIT) -> class Held(str): """A message typed or pasted while OSIRIS was answering, sent next. `pasted` travels with it so the REPL does not classify it by the previous prompt's input (2026-10-08: a document - pasted during a reply was dispatched as typed and trained on).""" + pasted during a reply was dispatched as typed and trained on). `transport` is how it + arrived: "bracketed_paste", "typed", or "unknown" for lines that came in together with no + paste markers, which may be a paste whose markers never arrived and are never trained on.""" pasted: bool = False + transport: str = "typed" - def __new__(cls, text: str, pasted: bool = False): + def __new__(cls, text: str, pasted: bool = False, transport: Optional[str] = None): + if transport not in (None, "typed", "unknown"): + raise ValueError(f"held transport {transport!r} is not 'typed' or 'unknown'") s = super().__new__(cls, text) s.pasted = pasted + s.transport = "bracketed_paste" if pasted else (transport or "typed") return s @@ -2177,7 +2183,16 @@ def _hold(self, typed: str) -> None: return lines = typed.replace("\r", "\n").split("\n") partial = lines.pop().strip() - self.held.extend(Held(line.strip()) for line in lines if line.strip()) + finished = [line.strip() for line in lines if line.strip()] + # Several finished lines during one reply, with no paste markers, may be a paste whose + # markers the terminal never sent (2026-12-05: a PowerShell transcript pasted during a + # reply arrived unmarked, became 28 "typed" messages, and the core trained on them). + # Each is still sent, but as unknown transport: recorded, never trained on. + transport = "unknown" if len(finished) > 1 else "typed" + self.held.extend(Held(line, transport=transport) for line in finished) + if transport == "unknown": + self.out(f" · {len(finished)} lines arrived together while I answered, without paste markers; " + "each is sent but not learned (use /learn last to propose one)\n") if partial: self.out(f" · you were typing {partial!r} while I answered -- not sent; retype it to send\n") diff --git a/osiris_cli/osiris_repl.py b/osiris_cli/osiris_repl.py index dcab8b26..83925bee 100644 --- a/osiris_cli/osiris_repl.py +++ b/osiris_cli/osiris_repl.py @@ -1450,10 +1450,20 @@ def small_talk(text): return _LIVING +# Transport of the held message dispatch_held is sending, while it is sent. The command router +# strips a Held to a plain str before conversation, so its own attribute does not survive. +_HELD_TRANSPORT: Optional[str] = None + + def _resolve_input_transport(prompt: str) -> str: + own = getattr(prompt, "transport", None) + if own in ("bracketed_paste", "typed", "unknown"): + return own pasted_attr = getattr(prompt, "pasted", None) if pasted_attr is not None: return "bracketed_paste" if bool(pasted_attr) else "typed" + if _HELD_TRANSPORT is not None: + return _HELD_TRANSPORT if otc is not None and isinstance(getattr(otc, "_last_input", None), dict) and "pasted" in otc._last_input: return "bracketed_paste" if bool(otc._last_input.get("pasted")) else "typed" return "unknown" @@ -2177,7 +2187,9 @@ def safe_dispatch(state: OsirisReplState, line: str) -> None: def dispatch_held(state: OsirisReplState, line: str) -> None: """A message held while OSIRIS answered. A multi-line paste is content: it goes to the conversation whole, never to the command router, where a first word such as /apply would - run a command and drop the rest. Typed lines and one-line pastes dispatch as usual.""" + run a command and drop the rest. Typed lines and one-line pastes dispatch as usual, under + the held message's own transport (an unmarked line is "unknown": recorded, never learned).""" + global _HELD_TRANSPORT if getattr(line, "pasted", False) and "\n" in line.strip(): try: from osiris_cli.living import Held @@ -2185,7 +2197,11 @@ def dispatch_held(state: OsirisReplState, line: str) -> None: except Exception as exc: # noqa: BLE001 - as safe_dispatch: the session continues print(f"\n[!] the held paste failed: {type(exc).__name__}: {str(exc)[:200]}\n") return - safe_dispatch(state, line) + _HELD_TRANSPORT = getattr(line, "transport", None) + try: + safe_dispatch(state, line) + finally: + _HELD_TRANSPORT = None _MULTILINE_COMMAND_ALLOWLIST = {"/code", "/learn", "learn", "/swarm", "swarm", "/evolve", "evolve"} @@ -2807,7 +2823,9 @@ def boot_repl() -> None: if otc is not None and hasattr(otc, "_last_input"): otc._last_input["pasted"] = pasted shown = line if len(line) <= 200 else f"{line[:200]}… ({len(line):,} characters)" - print(f"{prompt}{shown} \033[2m({'pasted' if pasted else 'typed'} while OSIRIS was answering)\033[0m") + how = "pasted" if pasted else ( + "arrived unmarked with other lines" if getattr(line, "transport", "typed") == "unknown" else "typed") + print(f"{prompt}{shown} \033[2m({how} while OSIRIS was answering)\033[0m") dispatch_held(SESSION, line) continue try: diff --git a/tests/test_console_paste.py b/tests/test_console_paste.py index 276730d9..a4d1478e 100644 --- a/tests/test_console_paste.py +++ b/tests/test_console_paste.py @@ -244,6 +244,43 @@ def test_held_messages_carry_their_transport(): assert [(m, m.pasted) for m in o.held] == [("typed line", False), ("pasted\nblock", True)] +def test_several_unmarked_lines_during_a_reply_are_held_as_unknown_transport(): + # 2026-12-05: a PowerShell transcript pasted during a reply arrived without paste markers. + o, out = _holder() + o._hold(living.keys_typed("PS C:\\Users\\x> hostname\nworkstation\nPS C:\\Users\\x> adb version\n")) + assert o.held == ["PS C:\\Users\\x> hostname", "workstation", "PS C:\\Users\\x> adb version"] + assert [(m.transport, m.pasted) for m in o.held] == [("unknown", False)] * 3 + assert "3 lines arrived together" in "".join(out) and "not learned" in "".join(out) + o, out = _holder() + o._hold(living.keys_typed("one typed line\n")) + assert [(m, m.transport) for m in o.held] == [("one typed line", "typed")] and out == [] + + +def test_held_transport_is_one_of_the_known_kinds(): + assert Held("x").transport == "typed" and Held("x", pasted=True).transport == "bracketed_paste" + assert Held("x", transport="unknown").transport == "unknown" + with pytest.raises(ValueError): + Held("x", transport="bracketed_paste") # a paste is said with pasted=True, not claimed + + +def test_an_unmarked_held_line_is_recorded_but_never_trained_on(tmp_path, monkeypatch): + core = FakeCore() + o, out = make(tmp_path, core=core) + monkeypatch.setattr(osiris_repl, "get_living", lambda: o) + if osiris_repl.otc is not None: # the REPL loop marks every unpasted held line "typed" + monkeypatch.setattr(osiris_repl.otc, "_last_input", {"pasted": False}, raising=False) + o.converse("first question") # exchange 0 is held out either way + osiris_repl.dispatch_held(osiris_repl.OsirisReplState(), + Held("Get-CimInstance Win32_Processor printed these columns", transport="unknown")) + assert core.learned == [] + assert "the core trains on this exchange" not in "".join(out) + records = [json.loads(line) for line in open(o.log_path, encoding="utf-8").read().splitlines()] + assert (records[-1]["input_transport"], records[-1]["learnable"]) == ("unknown", False) + assert osiris_repl._HELD_TRANSPORT is None # scoped to that one dispatch + osiris_repl.dispatch_held(osiris_repl.OsirisReplState(), Held("and this one I typed myself")) + assert len(core.learned) == 1 # a typed held line still trains, as before + + def _feed(chunks): """read/ready over a queue of (arrives_after_idle_wait, bytes).""" queue = list(chunks)