diff --git a/agent_cost_estimator/cli.py b/agent_cost_estimator/cli.py index d00582f..694fa2d 100644 --- a/agent_cost_estimator/cli.py +++ b/agent_cost_estimator/cli.py @@ -498,7 +498,7 @@ class TurnCompletionHandler(FileSystemEventHandler): self._check_journal(event) def _check_journal(self, event): - if not event.is_directory and "journal" in str(event.src_path) and event.src_path.endswith(".jsonl"): + if not event.is_directory and ("journal" in str(event.src_path) or "messages" in str(event.src_path)) and event.src_path.endswith(".jsonl"): session_dir = Path(event.src_path).parent.parent self._run_hooks(session_dir) diff --git a/agent_cost_estimator/gui.py b/agent_cost_estimator/gui.py index a2c2fb2..f4f4b14 100644 --- a/agent_cost_estimator/gui.py +++ b/agent_cost_estimator/gui.py @@ -317,6 +317,10 @@ class AgentCostEstimatorGUI: def _run_tkinter(self, tk, ttk): """Run the GUI using Tkinter.""" + # Store for use in other methods + self.tk = tk + self.ttk = ttk + root = tk.Tk() root.title("Agent Cost Estimator") root.geometry("900x600") @@ -396,7 +400,6 @@ class AgentCostEstimatorGUI: # Store root for refresh self.root = root - self.tk = tk # Initial load self._refresh_sessions() @@ -424,7 +427,7 @@ class AgentCostEstimatorGUI: # Still add harness node even if no sessions self.tree.insert( "", - tk.END, + self.tk.END, text=harness_name, values=("", "", ""), tags=("harness",) @@ -433,7 +436,7 @@ class AgentCostEstimatorGUI: # Add harness as parent harness_node = self.tree.insert( "", - tk.END, + self.tk.END, text=harness_name, values=("", "", ""), tags=("harness",) @@ -443,7 +446,7 @@ class AgentCostEstimatorGUI: for session in harness_info.sessions: self.tree.insert( harness_node, - tk.END, + self.tk.END, text=f"{session.session_id[:8]}..." if len(session.session_id) > 8 else session.session_id, values=( @@ -470,20 +473,18 @@ class AgentCostEstimatorGUI: def _show_harness_dialog(self): """Show dialog to select default harness.""" - import tkinter as tk - available = list(SessionDiscovery.HARNESS_ADAPTERS.keys()) current = self.settings.default_harness - harness_var = tk.StringVar(value=current) + harness_var = self.tk.StringVar(value=current) - dialog = tk.Toplevel(self.root) + dialog = self.tk.Toplevel(self.root) dialog.title("Select Default Harness") dialog.transient(self.root) dialog.grab_set() - tk.Label(dialog, text="Default Harness:").pack(pady=5) + self.tk.Label(dialog, text="Default Harness:").pack(pady=5) - combo = ttk.Combobox( + combo = self.ttk.Combobox( dialog, textvariable=harness_var, values=available, @@ -502,8 +503,8 @@ class AgentCostEstimatorGUI: def cancel(): dialog.destroy() - tk.Button(dialog, text="Save", command=save).pack(side=tk.LEFT, padx=5, pady=5) - tk.Button(dialog, text="Cancel", command=cancel).pack(side=tk.RIGHT, padx=5, pady=5) + self.tk.Button(dialog, text="Save", command=save).pack(side=self.tk.LEFT, padx=5, pady=5) + self.tk.Button(dialog, text="Cancel", command=cancel).pack(side=self.tk.RIGHT, padx=5, pady=5) dialog.wait_window() @@ -530,16 +531,13 @@ class AgentCostEstimatorGUI: def _show_session_details(self, harness_name: str, session_id: str): """Show detailed information for a session.""" - import tkinter as tk - from tkinter import messagebox - # Find the session if self.session_discovery: harnesses = self.session_discovery.discover_sessions() if harness_name in harnesses: for session in harnesses[harness_name].sessions: if session.session_id == session_id: - messagebox.showinfo( + self.tk.messagebox.showinfo( f"Session: {session.session_id}", f"Model: {session.model}\n" f"Session Price: ${session.session_price:.2f}\n" @@ -548,7 +546,7 @@ class AgentCostEstimatorGUI: ) return - messagebox.showwarning("Not Found", f"Session {session_id} not found") + self.tk.messagebox.showwarning("Not Found", f"Session {session_id} not found") def _auto_refresh(self): """Auto-refresh sessions.""" diff --git a/agent_cost_estimator/integration/hermes/plugin/__init__.py b/agent_cost_estimator/integration/hermes/plugin/__init__.py index a1f6930..570d332 100644 --- a/agent_cost_estimator/integration/hermes/plugin/__init__.py +++ b/agent_cost_estimator/integration/hermes/plugin/__init__.py @@ -52,10 +52,6 @@ logger = logging.getLogger("agent_cost_estimator.hermes_plugin") DB_NAME = "state.db" LEDGER_DIR = Path("plugin-data") / "agent-cost-estimator" / "usage" LINE_PREFIX = "-- " -# How long the ordering wait gives the turn's message.complete to -# be published before the cost line publishes anyway (late beats -# absent; matches the bg-review hold pattern's spirit). -COMPLETION_WAIT_S = 60.0 # Interactive terminal surfaces that get the cost line. In current # Hermes builds the CLI chat runs through the gateway's local # adapter (Platform.LOCAL), so its platform value is "local"; @@ -464,16 +460,14 @@ def _in_daemon() -> bool: def _publish_notification(session_id: str, line: str) -> bool: """Publish a notification.show event after the turn's response. - Ordering: post_llm_call fires in the agent's worker thread - BEFORE the gateway settles the turn and publishes - message.complete, so an immediate publish lands ABOVE the - response. Wait (bounded) until the session's execution stamp - is cleared - the settlement clears it strictly after - message.complete is published - then publish, making the cost - line the last line of the turn. This is the same hold pattern - the gateway's background-review delivery uses (run - detached; post_llm_call is timeout-bounded and must return - fast). + Publish immediately: post_llm_call fires inside the turn, before + settlement, so the frame reaches the viewer while its pump is + still dispatching. The classic CLI queues it as an AgentNotice + and flushes it after run_conversation (still below the response); + waiting for the turn's execution stamp to clear would land the + frame AFTER message.complete, which the classic CLI's pump has + already returned from and _drain_stale discards - silently + dropping the line client-side (verified 2026-11-03). Returns True when the event was published (or the session is gone and the daemon owns display either way), False when this @@ -505,15 +499,14 @@ def _publish_notification(session_id: str, line: str) -> bool: "[cost] session %s has no event stream; printing", session_id) return False - execution = getattr(stream, "execution", None) - if isinstance(execution, dict): - generation = execution.get("execution_generation") - if generation is not None: - deadline = time.monotonic() + COMPLETION_WAIT_S - while (stream.execution.get( - "execution_generation") == generation - and time.monotonic() < deadline): - time.sleep(0.05) + # Publish immediately. The classic CLI's pump returns at + # message.complete and discards later frames (_drain_stale), + # so waiting for the turn's execution stamp to clear (the + # old ordering wait) made the line land after the frame the + # CLI stops at and get dropped. post_llm_call fires inside + # the turn, so the frame is dispatched before message.complete + # and the classic CLI queues it as a notice, flushed after + # run_conversation — still printed below the response. stream.publish( session_id, {"text": line, "level": "info", "kind": "sticky", diff --git a/agent_cost_estimator/integration/hermes/plugin/plugin.yaml b/agent_cost_estimator/integration/hermes/plugin/plugin.yaml index f3026cd..b244b95 100644 --- a/agent_cost_estimator/integration/hermes/plugin/plugin.yaml +++ b/agent_cost_estimator/integration/hermes/plugin/plugin.yaml @@ -1,5 +1,5 @@ name: agent-cost-estimator -version: 0.4.4 +version: 0.4.5 description: "Print an estimated cost line (provider:model | session:$X | turn:$Y) after every assistant turn, recorded from Hermes session usage." author: "nvaldez" provides_hooks: diff --git a/pyproject.toml b/pyproject.toml index e88ce04..83c7208 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "agent-cost-estimator" -version = "0.4.4" +version = "0.4.5" description = "Report token usage and estimated USD cost of AI agent sessions from local harness logs" readme = "README.md" requires-python = ">=3.9" diff --git a/tests/test_agent_cost_estimator.py b/tests/test_agent_cost_estimator.py index a420886..ee60fab 100755 --- a/tests/test_agent_cost_estimator.py +++ b/tests/test_agent_cost_estimator.py @@ -951,15 +951,16 @@ def test_plugin_publishes_notification_in_daemon(tmp_path, monkeypatch, assert capsys.readouterr().out == "" -def test_plugin_waits_for_completion_before_publish(monkeypatch): - """While the turn's execution stamp is set, the publish waits; - once cleared it publishes (ordering: line lands AFTER the - response).""" - import threading +def test_plugin_publishes_immediately_without_waiting(monkeypatch): + """The publish must NOT wait for the turn's execution stamp to + clear: the classic CLI's pump returns at message.complete and + drops later frames, so a settlement-ordered publish is lost + client-side. The frame must go out while the stamp is still set + (post_llm_call fires inside the turn).""" import types class _Stream: - execution = {"execution_generation": 7} + execution = {"execution_generation": 7} # still running published_at = None def publish(self, session_id, payload, *, event_type=None): @@ -980,20 +981,12 @@ def test_plugin_waits_for_completion_before_publish(monkeypatch): fake_gate.session_cron = fake_cron # type: ignore[attr-defined] monkeypatch.setitem(sys.modules, "gateway", fake_gate) monkeypatch.setitem(sys.modules, "gateway.session_cron", fake_cron) - monkeypatch.setattr(hp, "COMPLETION_WAIT_S", 5.0) - # Clear the stamp shortly after the publish attempt starts. - def clear_later(): - time.sleep(0.3) - stream.execution = {} - - clearer = threading.Thread(target=clear_later, daemon=True) - clearer.start() started = time.monotonic() ok = hp._publish_notification("s1", "line") assert ok is True assert stream.published_at is not None - assert stream.published_at >= started + 0.3 # waited for the clear + assert stream.published_at < started + 0.3 # did NOT wait for the stamp def test_plugin_label_uses_billing_route(tmp_path, monkeypatch, capsys):