fix(control): detect direct-typed Claude prompts as working

Adds a claude case to _agent_direct_activity that reads the most recent
~/.claude/projects/<dashed-cwd>/*.jsonl mtime. The daemon already consumes
this signal for grok; with claude wired in, prompts typed straight into the
TUI (no Cockpit dispatch sentinel) surface as Working instead of falling
through to Agent shell open.

Gates direct-terminal evidence on readyAt so a just-finished agent stays
Ready: a Stop hook fires after the last transcript write, and without the
gate the next daemon tick would re-promote the stale mtime back to Working
and overwrite the legitimate ready badge.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
g-butandClaude Opus 4.7 committed 2026-05-28 15:58:35 +02:00
1 parent 6da8d7ea1e
commit 7d7c40a0cd
3 files changed
+45 -1

No files matched your search

+12
View File
@@ -106,6 +106,18 @@ _agent_direct_activity() {
| sort -nr | head -1 | cut -d' ' -f2-)
[ -n "$file" ] && printf '%s|Direct terminal activity\n' "$(stat -c '%Y' "$file" 2>/dev/null)"
;;
claude)
# Claude Code stores per-project transcripts at
# ~/.claude/projects/<dashed-cwd>/<session_uuid>.jsonl; the dir name is the
# absolute path with every "/" replaced by "-". mtime updates on every
# appended event, so a fresh mtime is reliable evidence the agent is
# actively processing — including prompts the user typed directly into
# the TUI (no Cockpit dispatch sentinel exists for those).
encoded_dir=$(printf '%s' "$dir" | tr '/' '-')
file=$(find "$HOME/.claude/projects/$encoded_dir" -maxdepth 1 -name '*.jsonl' -printf '%T@ %p\n' 2>/dev/null \
| sort -nr | head -1 | cut -d' ' -f2-)
[ -n "$file" ] && printf '%s|Direct terminal activity\n' "$(stat -c '%Y' "$file" 2>/dev/null)"
;;
# Add adapters here only when they expose a stable, project-scoped log.
# Process detection, handoffs, and web-dispatched prompts work without it.
esac
+10 -1
View File
@@ -864,7 +864,16 @@ _build_state_json() {
observation_label="${activity#*|}"
if [ -n "$activity" ] && [[ "$observation_mtime" =~ ^[0-9]+$ ]]; then
observation_age=$(( $(date +%s) - observation_mtime ))
if [ "$observation_age" -ge 0 ] && [ "$observation_age" -lt 45 ]; then
# A just-finished agent writes its last transcript entry milliseconds
# before the Stop hook clears cpk and stamps readyAt. Without this
# gate, the next daemon tick would re-promote the stale transcript
# back to "Working" and overwrite the legitimate "Ready" state.
local ready_overrides_observation="false"
if [ "$ready_at" != "null" ] && [ "$ready_at" -ge "$observation_mtime" ]; then
ready_overrides_observation="true"
fi
if [ "$observation_age" -ge 0 ] && [ "$observation_age" -lt 45 ] \
&& [ "$ready_overrides_observation" = "false" ]; then
cpk="direct_terminal"
cpl="$observation_label"
cpsat="$observation_mtime"
+23
View File
@@ -196,6 +196,29 @@ function runTests(): void {
assert(getProjectDisplayState(project, ["Cockpit"], nowS).stateLabel === "Working", "fresh prompt must show Working");
});
check("direct-terminal observation is surfaced as Working", () => {
// Daemon-side path for prompts the user typed directly into Claude (no
// Cockpit dispatch sentinel). cockpit-daemon.sh sets currentPrompt.key to
// "direct_terminal" with startedAt = the transcript's mtime when the tab is
// open, no other prompt is tracked, and the agent has not just signaled
// ready. The presenter must treat this exactly like any tracked prompt so
// chips/badges read "Working" instead of falling through to "Agent shell
// open" (the limitation 6da8d7e called out).
const nowS = 1_700_000_100;
const project = stubProject({
tab: "Cockpit",
agentRunning: true,
activeAgents: ["claude"],
currentPrompt: { key: "direct_terminal", label: "Direct terminal activity", startedAt: nowS - 3 },
});
const state = getProjectDisplayState(project, ["Cockpit"], nowS);
assert(state.stateLabel === "Working", "direct-terminal observation must report Working");
assert(state.isAgentWorking, "isAgentWorking is the SSOT chips read");
const snapshot = buildProjectOperationsSnapshot(project, ["Cockpit"], nowS);
assert(snapshot.phase === "working", "snapshot phase must match the badge");
assert(snapshot.evidenceLabel === "Live agent process detected", "evidence must read live, not historical");
});
check("handoff written after prompt marks it completed", () => {
const nowS = 1_700_000_100;
const project = stubProject({