Skip to content

Commit fef678f

Browse files
committed
feat: add detailed documentation for fixing workspace upsert bug blocking backfill
1 parent b6da5b6 commit fef678f

File tree

1 file changed

+143
-0
lines changed
  • specs/014-workspace-upsert-backfill-bug

1 file changed

+143
-0
lines changed
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
---
2+
status: planned
3+
created: '2025-11-11'
4+
tags:
5+
- bug
6+
- backfill
7+
- api
8+
- database
9+
priority: high
10+
created_at: '2025-11-11T15:06:38.494Z'
11+
---
12+
13+
# Fix Workspace Upsert Bug Blocking Backfill
14+
15+
> **Status**: 📅 Planned · **Priority**: High · **Created**: 2025-11-11
16+
17+
## Overview
18+
19+
The backfill feature successfully parses Copilot chat sessions and sends events to the API, but no data reaches PostgreSQL due to a workspace upsert bug. When the API tries to upsert a workspace that already exists, it fails with "Unique constraint failed on the fields: (`workspace_id`)", causing all batch inserts to fail.
20+
21+
## Problem
22+
23+
The collector's backfill process:
24+
25+
1. ✅ Parses chat session files correctly (798 events parsed)
26+
2. ✅ Sends batches to the API endpoint
27+
3. ❌ API fails to upsert workspace with unique constraint error
28+
4. ❌ All batches retry 4 times and fail
29+
5. ❌ Zero data inserted into `agent_sessions` or `agent_events` tables
30+
31+
## Root Cause
32+
33+
The API's workspace upsert logic doesn't properly handle the case when a workspace already exists in the database. The Prisma upsert is failing on the unique constraint for `workspace_id`.
34+
35+
## Impact
36+
37+
- Backfill feature is completely blocked
38+
- Historical data cannot be imported
39+
- Manual workspace creation doesn't help (creates the conflict)
40+
41+
## Related Issues
42+
43+
- Spec #006: Go Collector Next Phase (backfill feature)
44+
- Database has proper constraints and foreign keys
45+
- Collector batch retry logic works correctly (retries 4 times as expected)
46+
47+
## Design
48+
49+
### Error Analysis
50+
51+
The API returns:
52+
53+
```
54+
Status 500: "Unique constraint failed on the fields: (`workspace_id`)"
55+
```
56+
57+
This occurs when the API tries to upsert a workspace that already exists. The Prisma upsert operation should use:
58+
59+
- `where: { workspace_id }` to find existing workspace
60+
- `update: { ...fields }` to update if found
61+
- `create: { ...fields }` to insert if not found
62+
63+
### Likely Causes
64+
65+
1. **Incorrect upsert key**: Using wrong unique field in `where` clause
66+
2. **Missing update data**: Empty `update` object causes constraint violation
67+
3. **Conflicting unique fields**: Trying to update other unique fields that conflict
68+
4. **Transaction issues**: Workspace upsert happening inside a transaction that conflicts
69+
70+
### Solution Approach
71+
72+
1. **Investigate API endpoint**: Find the workspace upsert code in the API
73+
2. **Fix Prisma upsert**: Ensure proper `where`, `update`, `create` clauses
74+
3. **Add logging**: Log workspace upsert operations for debugging
75+
4. **Test with existing workspaces**: Verify upsert works when workspace already exists
76+
77+
## Plan
78+
79+
- [ ] Locate the API endpoint handling batch inserts (`/api/observability/batch` or similar)
80+
- [ ] Find the workspace upsert code in the API handler
81+
- [ ] Review Prisma upsert query structure
82+
- [ ] Fix upsert to properly handle existing workspaces
83+
- [ ] Add error handling and logging for workspace operations
84+
- [ ] Test with manually created workspace
85+
- [ ] Test with backfill on fresh database
86+
- [ ] Verify data reaches `agent_sessions` and `agent_events` tables
87+
88+
## Test
89+
90+
### Prerequisites
91+
92+
- Project id=1 exists in database
93+
- Machine record exists
94+
- Workspace `aebecdd872cc19008a36d00765d84755` exists
95+
96+
### Test Cases
97+
98+
- [ ] **Fresh backfill**: Run backfill with no existing workspace → should succeed
99+
- [ ] **Existing workspace**: Run backfill with pre-existing workspace → should upsert and succeed
100+
- [ ] **Concurrent requests**: Multiple batches trying to upsert same workspace → should handle gracefully
101+
- [ ] **Data verification**: Verify `agent_sessions` count > 0 and `agent_events` count > 0 after backfill
102+
- [ ] **Event integrity**: Verify events have correct workspace_id, project_id, session_id references
103+
104+
### Success Criteria
105+
106+
```bash
107+
# After backfill:
108+
SELECT COUNT(*) FROM agent_sessions; -- Should be > 0
109+
SELECT COUNT(*) FROM agent_events; -- Should be > 0
110+
SELECT COUNT(*) FROM workspaces WHERE workspace_id = 'aebecdd872cc19008a36d00765d84755'; -- Should be 1
111+
```
112+
113+
## Notes
114+
115+
### Investigation Log (2025-11-11)
116+
117+
**Setup:**
118+
119+
- Created project id=1 manually
120+
- Created machine and workspace manually
121+
- Prisma client was missing (regenerated)
122+
- Web API had TypeScript compilation errors (fixed by regenerating Prisma)
123+
124+
**Backfill Results:**
125+
126+
- 798 events parsed from chat sessions
127+
- All batch inserts failed with workspace upsert error
128+
- Collector retry logic worked (4 attempts per batch)
129+
- Final result: 0 rows in agent_sessions, 0 rows in agent_events
130+
131+
**Error Pattern:**
132+
133+
```
134+
Failed to send batch (attempt 1/4): unexpected status 500:
135+
"Unique constraint failed on the fields: (`workspace_id`)"
136+
```
137+
138+
### Next Steps
139+
140+
1. Find the batch insert API endpoint code
141+
2. Examine the Prisma upsert query
142+
3. Fix the upsert logic
143+
4. Re-test backfill

0 commit comments

Comments
 (0)