Enhanced context handoff: compression, versioning, I2I integration#5
Enhanced context handoff: compression, versioning, I2I integration#5SuperInstance wants to merge 1 commit intomainfrom
Conversation
flux-baton v3 — T-016 integration + handoff improvements New features: - Context compression: summarize previous agent state for handoff with ratio tracking (compress_context, compress_handoff_text) - Priority task queue: heapq-based queue with I2I v2 TASK_CLAIM/COMPLETE semantics (TaskQueue, PrioritizedTask) - Handoff acknowledgment protocol: send/ack/timeout lifecycle (HandoffAck, HandoffAckTracker) - Context versioning: semver per generation with content hash chain (ContextVersion, ContextVersionTracker) - Conflict resolution: 4 strategies for duplicate task claims (ConflictResolver: FCFS, highest confidence, priority, manual escalation) - Handoff metrics: success rate, compression ratio, quality, duration (HandoffMetrics) - I2I v2 integration: TASK_CLAIM, TASK_COMPLETE, BATON_PACKED, BATON_ACK, BATON_CLAIM_CONFLICT, BATON_CONTEXT_VERSION Enhanced Baton class: - v3 snapshot persists CONTEXT_VERSION, TASK_QUEUE, COMPRESSED_CONTEXT, HANDOFF_METRICS alongside existing files - write_handoff includes task queue summary, metrics, context version - i2i_task_claim() and i2i_task_complete() methods - acknowledge_handoff() and check_handoff_timeouts() methods Tests: 75 tests across 10 test classes, all passing
| if cv_json: | ||
| try: | ||
| cv_data = json.loads(cv_json) | ||
| cv = ContextVersion(**cv_data) |
There was a problem hiding this comment.
🔴 ContextVersion deserialization always fails due to extra semver key in to_dict()
to_dict() at flux_baton.py:631 includes a "semver" key (a computed @property, not a constructor field). During restore() at flux_baton.py:1075, ContextVersion(**cv_data) is called with this dict, which raises TypeError: __init__() got an unexpected keyword argument 'semver' because the ContextVersion dataclass (flux_baton.py:569-579) has no semver field. The bare except: pass at line 1078 silently swallows this error, so context version is never successfully restored from persisted state — every restore silently loses the version tracking chain.
| cv = ContextVersion(**cv_data) | |
| cv_data = json.loads(cv_json) | |
| cv_data.pop("semver", None) | |
| cv = ContextVersion(**cv_data) |
Was this helpful? React with 👍 or 👎 to provide feedback.
Debug
| # Write 12: GENERATION (COMMIT MARKER — written LAST) | ||
| results.append(self._write(".baton/GENERATION", | ||
| str(new_gen), f"baton: GENERATION → {new_gen} (commit)")) | ||
|
|
||
| # Send I2I notification | ||
|
|
||
| # v3 Write 13: HANDOFF_METRICS.json | ||
| metrics_record = self.metrics.record_handoff( | ||
| generation=new_gen, | ||
| context_version=ctx_version.semver, | ||
| original_size=compressed.original_size_bytes, | ||
| compressed_size=compressed.compressed_size_bytes, | ||
| quality_score=quality.get("average", 0), | ||
| duration_seconds=time.time() - snapshot_start, | ||
| ) | ||
| results.append(self._write(".baton/HANDOFF_METRICS.json", | ||
| json.dumps({"handoffs": self.metrics._handoffs}, indent=2), | ||
| f"baton: metrics gen-{new_gen}")) |
There was a problem hiding this comment.
🔴 GENERATION commit marker written before HANDOFF_METRICS.json, breaking atomicity guarantee
The module docstring (flux_baton.py:16) and the comment at line 1290 state that GENERATION is the atomic commit marker and must be written last. However, HANDOFF_METRICS.json (Write 13, lines 1294-1305) is written after GENERATION (Write 12, lines 1290-1292). This breaks the atomicity invariant: a reader could see the new generation number but find missing or stale metrics data. The metrics write should be moved before the GENERATION write.
| # Write 12: GENERATION (COMMIT MARKER — written LAST) | |
| results.append(self._write(".baton/GENERATION", | |
| str(new_gen), f"baton: GENERATION → {new_gen} (commit)")) | |
| # Send I2I notification | |
| # v3 Write 13: HANDOFF_METRICS.json | |
| metrics_record = self.metrics.record_handoff( | |
| generation=new_gen, | |
| context_version=ctx_version.semver, | |
| original_size=compressed.original_size_bytes, | |
| compressed_size=compressed.compressed_size_bytes, | |
| quality_score=quality.get("average", 0), | |
| duration_seconds=time.time() - snapshot_start, | |
| ) | |
| results.append(self._write(".baton/HANDOFF_METRICS.json", | |
| json.dumps({"handoffs": self.metrics._handoffs}, indent=2), | |
| f"baton: metrics gen-{new_gen}")) | |
| # v3 Write 12: HANDOFF_METRICS.json | |
| metrics_record = self.metrics.record_handoff( | |
| generation=new_gen, | |
| context_version=ctx_version.semver, | |
| original_size=compressed.original_size_bytes, | |
| compressed_size=compressed.compressed_size_bytes, | |
| quality_score=quality.get("average", 0), | |
| duration_seconds=time.time() - snapshot_start, | |
| ) | |
| results.append(self._write(".baton/HANDOFF_METRICS.json", | |
| json.dumps({"handoffs": self.metrics._handoffs}, indent=2), | |
| f"baton: metrics gen-{new_gen}")) | |
| # Write 13: GENERATION (COMMIT MARKER — written LAST) | |
| results.append(self._write(".baton/GENERATION", | |
| str(new_gen), f"baton: GENERATION → {new_gen} (commit)")) |
Was this helpful? React with 👍 or 👎 to provide feedback.
T-016 integration + handoff improvements
flux-baton v3
New Features
compress_context()andcompress_handoff_text()with ratio trackingTaskQueuewith heapq-based ordering,TASK_CLAIM/TASK_COMPLETEI2I v2 semanticsHandoffAckTrackerwith send/ack/reject/timeout lifecycleContextVersion(semver per generation) with content hash chain viaContextVersionTrackerConflictResolverwith 4 strategies (FCFS, highest confidence, priority, manual escalation)HandoffMetrics— success rate, compression ratio, quality score, durationEnhanced Baton Class
snapshot()now persists: CONTEXT_VERSION.json, TASK_QUEUE.json, COMPRESSED_CONTEXT.json, HANDOFF_METRICS.jsonwrite_handoff()includes task queue summary, metrics, and context versioni2i_task_claim()/i2i_task_complete()for I2I v2 message processingacknowledge_handoff()/check_handoff_timeouts()for ack protocolTests