Skip to content

fix: replace fake random agent states with real gateway session data#61

Open
openclawuser4158 wants to merge 1 commit into
iamlukethedev:mainfrom
openclawuser4158:fix/real-gateway-presence
Open

fix: replace fake random agent states with real gateway session data#61
openclawuser4158 wants to merge 1 commit into
iamlukethedev:mainfrom
openclawuser4158:fix/real-gateway-presence

Conversation

@openclawuser4158

Copy link
Copy Markdown

Problem

The office presence system in src/lib/office/presence.ts generates fake agent states using a seeded random hash that changes every 2 seconds. Agents randomly flip between "working", "idle", "meeting", and "error" with no connection to actual gateway runtime state. This makes the 3D office visualization misleading — agents appear busy or errored when they're actually idle, and idle when they're actually working.

Solution

Rewrites loadOfficePresenceSnapshot() to query the real OpenClaw gateway for active session data via the /tools/invoke endpoint (sessions_list tool).

Changes

  • src/lib/office/presence.ts: Replaced random state generation with actual gateway session queries
    • Reads gateway auth token dynamically from openclaw.json (no hardcoded credentials)
    • Maps gateway session status to office agent states: runningworking, error/failederror, else → idle
    • Agents with active child sessions (subagents) are mapped to meeting state (collaboration)
    • Graceful fallback: if gateway is unreachable or token is missing, all agents default to idle
    • Function is now async to support HTTP calls
  • src/app/api/office/presence/route.ts: Added await for the now-async function

Architecture note

The existing codebase queries sessions via the WebSocket RPC client (client.call("sessions.list", ...)). This change uses the HTTP /tools/invoke endpoint instead, since the presence snapshot is loaded server-side in a Next.js API route without an active WebSocket connection. Both surfaces return the same data.

Testing

  • npm run typecheck
  • npm run lint
  • npm run test — 893 passed, 2 failed (pre-existing on main, not introduced by this PR)
  • Manual verification against a live gateway with 10 agents confirmed correct state mapping

Security

  • No hardcoded credentials or tokens
  • Gateway URL is localhost-only (127.0.0.1)
  • Token read dynamically from config at runtime
  • Reviewed for sensitive data leaks — clean

- Replace random hash-based fake agent states in loadOfficePresenceSnapshot()
- Query OpenClaw gateway /tools/invoke sessions_list to get real session states
- Map gateway session status to office agent states:
  - running → working
  - error/failed → error
  - done/idle/other → idle
- Read gateway auth token dynamically from openclaw.json (no hardcoded creds)
- Handle missing tokens gracefully by falling back to idle state
- Make loadOfficePresenceSnapshot() async to support HTTP calls
- Update /api/office/presence route to handle async function
- Maintain existing function signature and types for compatibility

@oabdelmaksoud oabdelmaksoud left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed in Claw3D.

@iamlukethedev iamlukethedev left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two correctness issues block this for me:

  1. The new meeting detection appears to attribute subagent sessions to the wrong agent.
  2. The gateway config lookup is narrower than the rest of the app and will miss env/settings-backed gateway setups.

I’d also like to see coverage added for the new gateway-backed presence path, since there are currently no tests around this behavior.

// Count child sessions per agent (subagent activity implies "meeting"/collaboration)
const childCountByAgent = new Map<string, number>();
sessions.forEach(session => {
if (session.agentId && session.kind === "subagent") {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this misattributes subagent activity. extractAgentIdFromSessionKey() only parses the owner encoded in the session key (agent:{agentId}:{sessionType}), so for a subagent session it will give you the subagent's own id, not the parent/main agent you want to mark as meeting. As written, childCountByAgent is likely counting children against themselves, which means the collaboration signal won't land on the parent agent as intended.

const agentList = readConfigAgentList(config);

// Get gateway configuration for API access
const gateway = config?.gateway as Record<string, unknown> | undefined;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only reads gateway connection details from openclaw.json, but the rest of the app also supports env/settings-backed gateway defaults. In those setups, the office UI can connect successfully while this endpoint still forces everyone to idle, which is a behavior regression from the broader gateway configuration model already in the repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants