From 3d62293a1e18862113554cae16bbcb22461add34 Mon Sep 17 00:00:00 2001 From: BukeLy Date: Tue, 26 May 2026 14:48:53 +0800 Subject: [PATCH] fix(filesystem): avoid duplicate pifs chat output --- pageindex/filesystem/cli.py | 8 ++++++-- tests/test_pifs_cli.py | 35 ++++++++++++++++++++++++++++++++--- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/pageindex/filesystem/cli.py b/pageindex/filesystem/cli.py index 4d6d8eb..9625fe8 100644 --- a/pageindex/filesystem/cli.py +++ b/pageindex/filesystem/cli.py @@ -140,7 +140,9 @@ def _run_ask(argv: list[str], *, workspace_default: str | None) -> int: raise ValueError("ask requires a question") filesystem = _filesystem_from_workspace(args.workspace) - print(run_pifs_agent(filesystem, question, **_agent_kwargs(args))) + answer = run_pifs_agent(filesystem, question, **_agent_kwargs(args)) + if args.stream_mode == "off": + print(answer) return 0 @@ -164,7 +166,9 @@ def _run_chat(argv: list[str], *, workspace_default: str | None) -> int: continue if question.lower() in EXIT_COMMANDS: break - print(run_pifs_agent(filesystem, question, **_agent_kwargs(args))) + answer = run_pifs_agent(filesystem, question, **_agent_kwargs(args)) + if args.stream_mode == "off": + print(answer) return 0 diff --git a/tests/test_pifs_cli.py b/tests/test_pifs_cli.py index 70f510e..74e95b4 100644 --- a/tests/test_pifs_cli.py +++ b/tests/test_pifs_cli.py @@ -58,7 +58,7 @@ def test_cli_ask_invokes_agent_with_question(monkeypatch, capsys, tmp_path): "--model", "test-model", "--stream-mode", - "tools", + "off", "--max-turns", "7", "--max-seconds", @@ -80,7 +80,7 @@ def test_cli_ask_invokes_agent_with_question(monkeypatch, capsys, tmp_path): assert question == "What is inside?" assert kwargs == { "model": "test-model", - "stream_mode": "tools", + "stream_mode": "off", "max_turns": 7, "max_seconds": 3.5, "reasoning_effort": "low", @@ -140,7 +140,7 @@ def test_cli_chat_runs_one_question_and_exits(monkeypatch, capsys, tmp_path): status = cli.main(["chat", "--workspace", str(workspace), "--model", "test-model"]) assert status == 0 - assert capsys.readouterr().out == "answer:Summarize the workspace\n" + assert capsys.readouterr().out == "" assert len(agent_calls) == 1 filesystem, question, kwargs = agent_calls[0] assert filesystem.workspace == workspace @@ -149,6 +149,35 @@ def test_cli_chat_runs_one_question_and_exits(monkeypatch, capsys, tmp_path): assert kwargs["stream_mode"] == "all" +def test_cli_ask_does_not_reprint_streamed_agent_output(monkeypatch, capsys, tmp_path): + from pageindex.filesystem import cli + + workspace = tmp_path / "workspace" + + def fake_run_pifs_agent(filesystem, question, **kwargs): + print("streamed answer") + return "returned answer" + + monkeypatch.setattr(cli, "PageIndexFileSystem", FakeFileSystem) + monkeypatch.setattr(cli, "run_pifs_agent", fake_run_pifs_agent) + + status = cli.main( + [ + "ask", + "--workspace", + str(workspace), + "--stream-mode", + "all", + "What", + "is", + "inside?", + ] + ) + + assert status == 0 + assert capsys.readouterr().out == "streamed answer\n" + + def test_cli_chat_stream_mode_can_be_overridden(monkeypatch, tmp_path): from pageindex.filesystem import cli