File size: 9,578 Bytes
cc10b69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# Schema Versioning and Rollback Documentation

## Overview

This document establishes the schema versioning strategy and rollback procedures for the activity logging system. It ensures data integrity, backward compatibility, and reliable version control for production deployments.

---

## Current Schema Version: 1.0

### Schema Definition (v1.0)

**File:** `activity_log.csv`  
**Created:** 2025-11-05T10:51:00Z  
**Commit Hash:** 3a67095  

#### Required Fields

| Field | Type | Description | Validation |
|-------|------|-------------|------------|
| `timestamp` | ISO 8601 String | UTC timestamp of activity | Must be valid ISO 8601 format (YYYY-MM-DDTHH:MM:SSZ) |
| `agent_id` | String | Identifier of the agent performing activity | Non-empty string |
| `activity_type` | Enum | Type of activity performed | Must be one of: `init`, `validate`, `ingest`, `update`, `delete`, `query`, `commit`, `rollback` |
| `entity_id` | String | Identifier of entity being acted upon | Non-empty string |
| `status` | Enum | Outcome status of activity | Must be one of: `created`, `success`, `failed`, `in_progress`, `warning`, `error` |
| `notes` | String | Human-readable description | Non-empty string |
| `context` | String | Additional context or metadata | Non-empty string |

#### CSV Format
```csv
timestamp,agent_id,activity_type,entity_id,status,notes,context
2025-11-05T10:51:00Z,comet_assistant,init,activity_log_schema,created,"Canonical CSV schema for activity tracking established","Step 1: Schema and CSV Foundation - initial setup"
```

---

## Version History

### Version 1.0 (Current)
**Date:** 2025-11-05  
**Commit:** 3a67095  
**Status:** Active  

**Changes:**
- Initial schema establishment
- 7 required fields defined
- Atomic ingestion function implemented
- Schema validation with error handling
- Request ID and commit hash tracking

**Files:**
- `activity_log.csv` - Data storage
- `activity_ingestion.py` - Ingestion function with validation
- `SCHEMA_VERSIONING.md` - This documentation

**Migration:** N/A (initial version)

---

## Rollback Procedures

### Git-Based Rollback

All schema changes and data are tracked via Git commits. To roll back to a previous version:

#### 1. Identify Target Commit

```bash
# View commit history
git log --oneline activity_log.csv activity_ingestion.py

# Example output:
# 3a67095 Implement atomic activity ingestion with schema validation
# b1a2c3d Initial activity_log.csv setup
```

#### 2. Rollback Commands

**Option A: Soft Rollback (Review changes before commit)**
```bash
# Reset to previous commit (keeps changes in working directory)
git checkout <commit-hash> -- activity_log.csv activity_ingestion.py
git commit -m "Rollback to schema version X.X (commit <commit-hash>)"
```

**Option B: Hard Rollback (Direct revert)**
```bash
# Revert to specific commit
git revert <commit-hash>
```

**Option C: File-Specific Rollback**
```bash
# Rollback only the CSV data file
git checkout <commit-hash> -- activity_log.csv
git commit -m "Rollback activity_log.csv to <commit-hash>"
```

#### 3. Verify Rollback

```bash
# Check current schema version
grep "SCHEMA_VERSION" activity_ingestion.py

# Validate CSV structure
head -n 5 activity_log.csv

# Run validation tests
python activity_ingestion.py
```

---

## Schema Change Tracking

### Change Log Format

Every schema modification MUST be logged using this structure:

```markdown
### Version X.Y (Status: Active/Deprecated)
**Date:** YYYY-MM-DD  
**Commit:** <git-commit-hash>  
**Status:** Active | Deprecated | Archived  

**Changes:**
- [ADD] New field: `field_name` (type, description)
- [MODIFY] Changed field: `field_name` (old -> new behavior)
- [REMOVE] Deprecated field: `field_name` (reason)
- [BREAKING] Breaking change description

**Migration:**
- Step 1: Migration instructions
- Step 2: Validation procedures

**Rollback Instructions:**
- Specific rollback notes if different from standard procedure
```

### Schema Version Numbering

- **Major Version (X.0):** Breaking changes, incompatible with previous versions
- **Minor Version (X.Y):** New fields added, backward compatible
- **Patch Version (X.Y.Z):** Bug fixes, validation updates, no schema changes

---

## Commit Tracking System

### Request ID Format

Every ingestion operation generates a unique request ID:

```
req_<16-char-hex>

Example: req_a1b2c3d4e5f67890
```

### Commit Hash Generation

Pseudo-commit hashes track each row insertion:

```python
commit_hash = SHA256(request_id + timestamp + agent_id + activity_type)[:16]
```

Example: `3f8d2a1bc5e97604`

### Operation Log

All operations are logged to `activity_ingestion.log`:

```json
{
  "timestamp": "2025-11-05T10:51:00Z",
  "request_id": "req_a1b2c3d4e5f67890",
  "operation": "ingest",
  "status": "success",
  "agent_id": "comet_assistant",
  "activity_type": "validate",
  "error": ""
}
```

---

## Build-Specific Rollback for Different Environments

### Development Build

```bash
# Tag development schema
git tag -a dev-schema-v1.0 -m "Development schema version 1.0"

# Rollback to dev version
git checkout dev-schema-v1.0 -- activity_log.csv activity_ingestion.py
```

### Staging Build

```bash
# Tag staging schema
git tag -a staging-schema-v1.0 -m "Staging schema version 1.0"

# Rollback to staging version
git checkout staging-schema-v1.0 -- activity_log.csv activity_ingestion.py
```

### Production Build

```bash
# Tag production schema (immutable)
git tag -a prod-schema-v1.0 -m "Production schema version 1.0" -s

# Rollback to production version
git checkout prod-schema-v1.0 -- activity_log.csv activity_ingestion.py

# Verify integrity
git verify-tag prod-schema-v1.0
```

---

## Validation Procedures

### Pre-Commit Validation

Before committing any schema changes:

1. **Run Schema Validation Tests**
   ```bash
   python activity_ingestion.py
   ```

2. **Check CSV Integrity**
   ```bash
   # Verify header
   head -n 1 activity_log.csv
   
   # Count rows
   wc -l activity_log.csv
   
   # Check for malformed rows
   awk -F',' 'NF!=7 {print NR, $0}' activity_log.csv
   ```

3. **Validate Timestamp Format**
   ```bash
   # Extract and validate timestamps
   cut -d',' -f1 activity_log.csv | tail -n +2 | while read ts; do
     date -d "$ts" > /dev/null 2>&1 || echo "Invalid: $ts"
   done
   ```

### Post-Rollback Validation

After performing a rollback:

1. Verify schema version matches expected version
2. Run full test suite: `python activity_ingestion.py`
3. Check operation logs for errors
4. Validate data integrity with sample queries
5. Document rollback in change log

---

## Error Recovery

### Corrupted CSV File

```bash
# Restore from last good commit
git checkout HEAD~1 -- activity_log.csv

# Or restore from specific commit
git checkout <commit-hash> -- activity_log.csv

# Verify restoration
python activity_ingestion.py
```

### Failed Schema Migration

```bash
# Immediate rollback
git reset --hard HEAD~1

# Or targeted rollback
git checkout HEAD~1 -- activity_ingestion.py SCHEMA_VERSIONING.md

# Document failure
echo "Migration failed: <reason>" >> SCHEMA_VERSIONING.md
git commit -m "Rollback failed migration - <reason>"
```

### Atomic Operation Failure

The `atomic_append_activity()` function handles failures automatically:

- **Schema Validation Error:** Returns error, no file modification
- **Write Lock Error:** Returns error, temp file cleaned up
- **Partial Write:** Temp file discarded, original file unchanged

All errors logged to `activity_ingestion.log` with request_id for tracing.

---

## Best Practices

### Schema Changes

1. **Always increment version number** when modifying schema
2. **Document all changes** in this file before committing
3. **Create migration scripts** for breaking changes
4. **Test thoroughly** in development before staging/production
5. **Tag releases** with semantic versioning

### Commit Messages

```
[Schema v1.1] Add new field: priority_level

- Added 'priority_level' field (enum: low, medium, high)
- Updated validation function
- Backward compatible with v1.0
- Migration: existing records default to 'medium'
```

### Data Backup

```bash
# Before major changes, create backup
cp activity_log.csv activity_log_backup_$(date +%Y%m%d).csv

# Commit backup to repo
git add activity_log_backup_*.csv
git commit -m "Backup before schema v1.1 migration"
```

---

## Testing Checklist

Before deploying any schema changes:

- [ ] Schema version updated in code
- [ ] Validation rules updated
- [ ] Test suite passes all cases
- [ ] Documentation updated
- [ ] Migration script created (if needed)
- [ ] Rollback procedure documented
- [ ] Staging deployment successful
- [ ] Production backup created
- [ ] Git tag created

---

## Contact & Support

For schema change requests or rollback assistance:

- **Repository:** huggingface.co/spaces/StrategyStack/README
- **Schema Owner:** comet_assistant
- **Last Updated:** 2025-11-05

---

## Appendix: Quick Reference

### Current Version Info
```bash
# Check schema version
grep SCHEMA_VERSION activity_ingestion.py

# View recent commits
git log -5 --oneline

# List all schema tags
git tag -l '*schema*'
```

### Emergency Rollback (One-Liner)
```bash
# Rollback everything to previous commit
git reset --hard HEAD~1 && python activity_ingestion.py
```

### Validate Current State
```bash
# Full validation
python activity_ingestion.py && \
awk -F',' 'NF!=7 {print "ERROR: Line", NR}' activity_log.csv && \
echo "Validation complete"
```

---

**Document Version:** 1.0  
**Last Modified:** 2025-11-05  
**Schema Version:** 1.0  
**Status:** Active