Skip to content

armgit / ax

Public Imported from GitHub
0

Agent Executor (AX) is Google's open agentic orchestration runtime

Go

Introduce sequence numbers to event logs

JBDd3643fdcc582
diff --git a/README.md b/README.md
index 588d776..c7f94f6 100644
--- a/README.md
+++ b/README.md
@@ -218,12 +218,12 @@ gar trigger --session-id session123 \
### Event Log Format
-Event logs use JSON Lines format (one JSON object per line). Each entry includes the session ID and checkpoint ID (for content events) for traceability:
+Event logs use JSON Lines format (one JSON object per line). Each entry includes the session ID, a monotonic sequence number, and checkpoint ID (for content events) for traceability:
```json
-{"session_id": "session123", "timestamp": "2026-01-02T10:30:00Z", "type": "CONTENT_IN", "checkpoint_id": "550e8400-e29b-41d4-a716-446655440000", "data": {...}}
-{"session_id": "session123", "timestamp": "2026-01-02T10:30:01Z", "type": "CONTENT_OUT", "checkpoint_id": "6ba7b810-9dad-11d1-80b4-00c04fd430c8", "data": {...}}
-{"session_id": "session123", "timestamp": "2026-01-02T10:30:02Z", "type": "LIFECYCLE", "data": {...}}
+{"session_id": "session123", "timestamp": "2026-01-02T10:30:00Z", "seq": 0, "type": "CONTENT_IN", "checkpoint_id": "550e8400-e29b-41d4-a716-446655440000", "data": {...}}
+{"session_id": "session123", "timestamp": "2026-01-02T10:30:01Z", "seq": 1, "type": "CONTENT_OUT", "checkpoint_id": "6ba7b810-9dad-11d1-80b4-00c04fd430c8", "data": {...}}
+{"session_id": "session123", "timestamp": "2026-01-02T10:30:02Z", "seq": 2, "type": "LIFECYCLE", "data": {...}}
```
**Event Types:**
diff --git a/internal/eventlog/eventlog.go b/internal/eventlog/eventlog.go
index 761b554..cb82147 100644
--- a/internal/eventlog/eventlog.go
+++ b/internal/eventlog/eventlog.go
@@ -22,6 +22,7 @@ const (
type Entry struct {
SessionID string `json:"session_id"`
Timestamp time.Time `json:"timestamp"`
+ Sequence int64 `json:"seq"` // Monotonic sequence number
Type EventType `json:"type"`
CheckpointID string `json:"checkpoint_id,omitempty"` // UUID for checkpoint tracking
Data map[string]interface{} `json:"data"`
diff --git a/internal/eventlog/file.go b/internal/eventlog/file.go
index e564524..1228aa8 100644
--- a/internal/eventlog/file.go
+++ b/internal/eventlog/file.go
@@ -21,6 +21,7 @@ type FileEventLog struct {
filePath string
file *os.File
writer *bufio.Writer
+ sequence int64 // Monotonic sequence counter
}
// FileConfig configures a FileEventLog instance.
@@ -52,11 +53,32 @@ func NewFileEventLog(config FileConfig) (*FileEventLog, error) {
return nil, fmt.Errorf("failed to open event log file: %w", err)
}
+ // Read existing entries to determine the current sequence number
+ var sequence int64
+ if fileInfo, err := os.Stat(filePath); err == nil && fileInfo.Size() > 0 {
+ readFile, err := os.Open(filePath)
+ if err != nil {
+ file.Close()
+ return nil, fmt.Errorf("failed to read existing log for sequence: %w", err)
+ }
+ scanner := bufio.NewScanner(readFile)
+ for scanner.Scan() {
+ var entry Entry
+ if err := json.Unmarshal(scanner.Bytes(), &entry); err == nil {
+ if entry.Sequence > sequence {
+ sequence = entry.Sequence
+ }
+ }
+ }
+ readFile.Close()
+ }
+
return &FileEventLog{
sessionID: config.SessionID,
filePath: filePath,
file: file,
writer: bufio.NewWriter(file),
+ sequence: sequence,
}, nil
}
@@ -76,11 +98,15 @@ func (e *FileEventLog) Append(entryType EventType, checkpointID string, data map
entry := Entry{
SessionID: e.sessionID,
Timestamp: time.Now(),
+ Sequence: e.sequence,
Type: entryType,
CheckpointID: checkpointID,
Data: data,
}
+ // Increment sequence for next entry
+ e.sequence++
+
jsonData, err := json.Marshal(entry)
if err != nil {
return fmt.Errorf("failed to marshal entry: %w", err)
@@ -165,6 +191,7 @@ func (e *FileEventLog) RetrieveEntries(ctx context.Context) ([]Entry, error) {
scanner := bufio.NewScanner(readFile)
lineNum := 0
+ var expectedSequence int64 = 0
for scanner.Scan() {
lineNum++
line := scanner.Bytes()
@@ -174,6 +201,12 @@ func (e *FileEventLog) RetrieveEntries(ctx context.Context) ([]Entry, error) {
return nil, fmt.Errorf("failed to unmarshal entry at line %d: %w", lineNum, err)
}
+ // Validate sequence number
+ if entry.Sequence != expectedSequence {
+ return nil, fmt.Errorf("sequence number mismatch at line %d: expected %d, got %d", lineNum, expectedSequence, entry.Sequence)
+ }
+ expectedSequence++
+
entries = append(entries, entry)
}