Skip to content

[P1] Command Payload Not Marshaled to JSON Before Database Insert #137

@JoshuaAFerguson

Description

@JoshuaAFerguson

Severity: P1 - HIGH
Component: API - Agent Command Creation
Report: BUG_REPORT_P1_COMMAND_PAYLOAD_JSON_MARSHALING.md

Issue

Command payload passed as Go map directly to database instead of marshaling to JSON first.

Impact

  • Session termination broken
  • Command payloads not stored correctly
  • Agent receives invalid/corrupted payload
  • Type assertion failures

Root Cause

CreateCommand function passes payload as map[string]interface{} directly to database query:

// WRONG - Go map not JSON
payload := map[string]interface{}{
    "sessionID": sessionID,
    "reason": "user_requested",
}

_, err := db.Exec(`
    INSERT INTO agent_commands (payload, ...)
    VALUES ($1, ...)
`, payload, ...)  // ERROR: Cannot insert Go map

Fix Required

Marshal payload to JSON before insert:

payload := map[string]interface{}{
    "sessionID": sessionID,
    "reason": "user_requested",
}

// Marshal to JSON
payloadJSON, err := json.Marshal(payload)
if err != nil {
    return fmt.Errorf("failed to marshal payload: %w", err)
}

_, err = db.Exec(`
    INSERT INTO agent_commands (payload, ...)
    VALUES ($1, ...)
`, payloadJSON, ...)  // Correct: JSON bytes

Alternative Fix

Use json.RawMessage:

var payloadJSON json.RawMessage
payloadJSON, err := json.Marshal(payload)

Files to Fix

  • Find all CreateCommand calls
  • api/internal/handlers/sessions.go (DeleteSession)
  • api/internal/websocket/hub.go (if creating commands)
  • Any other command creation code

Testing

  • Commands inserted successfully
  • Payload stored as valid JSON in DB
  • Agent receives correct payload
  • Session termination works end-to-end

Effort: 1-2 hours
Source: .claude/reports/BUG_REPORT_P1_COMMAND_PAYLOAD_JSON_MARSHALING.md

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions