dx-driven 0.1.0

Professional AI-assisted development orchestrator with binary-first architecture
Documentation
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
403
404
405
406
407
408
409
410
411
//! Binary decoder for .drv format

use super::{
    SectionType,
    schema::{
        ContextSection, DrvHeader, PersonaSection, RuleCategory, RuleEntry, StandardsSection,
        WorkflowSection, WorkflowStep,
    },
};
use crate::{DrivenError, Result, parser::UnifiedRule};
use bytes::Buf;
use std::io::Cursor;

/// Decoder for reading .drv binary files
#[derive(Debug)]
pub struct DrvDecoder<'a> {
    /// Raw data
    data: &'a [u8],
    /// Decoded string table
    string_table: Vec<&'a str>,
    /// Header
    header: DrvHeader,
}

impl<'a> DrvDecoder<'a> {
    /// Create a new decoder from binary data
    pub fn new(data: &'a [u8]) -> Result<Self> {
        if data.len() < 16 {
            return Err(DrivenError::InvalidBinary(
                "Data too short for header".to_string(),
            ));
        }

        // Parse header
        let header: DrvHeader = *bytemuck::from_bytes(&data[..16]);
        header.validate()?;

        // Verify checksum
        let stored_checksum = header.checksum;
        let computed = {
            let hash = blake3::hash(&data[16..]);
            let bytes = hash.as_bytes();
            u32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]])
        };

        if stored_checksum != computed {
            return Err(DrivenError::InvalidBinary(format!(
                "Checksum mismatch: stored={:08x}, computed={:08x}",
                stored_checksum, computed
            )));
        }

        let mut decoder = Self {
            data,
            string_table: Vec::new(),
            header,
        };

        // Parse string table (always first section after header)
        decoder.parse_string_table()?;

        Ok(decoder)
    }

    /// Parse the string table section
    fn parse_string_table(&mut self) -> Result<()> {
        let mut cursor = Cursor::new(&self.data[16..]);

        // Read section type
        if cursor.remaining() < 1 {
            return Err(DrivenError::InvalidBinary(
                "Missing string table section".to_string(),
            ));
        }

        let section_type = cursor.get_u8();
        if section_type != SectionType::StringTable as u8 {
            return Err(DrivenError::InvalidBinary(format!(
                "Expected string table (0x01), got 0x{:02x}",
                section_type
            )));
        }

        // Read count
        if cursor.remaining() < 4 {
            return Err(DrivenError::InvalidBinary(
                "Missing string table count".to_string(),
            ));
        }
        let count = cursor.get_u32_le() as usize;

        // Read strings
        self.string_table.reserve(count);
        let base_offset = 16 + 5; // header + section_type + count
        let mut offset = base_offset;

        for _ in 0..count {
            if offset + 2 > self.data.len() {
                return Err(DrivenError::InvalidBinary(
                    "Unexpected end of string table".to_string(),
                ));
            }

            let len = u16::from_le_bytes([self.data[offset], self.data[offset + 1]]) as usize;
            offset += 2;

            if offset + len > self.data.len() {
                return Err(DrivenError::InvalidBinary(
                    "String extends past end of data".to_string(),
                ));
            }

            let s = std::str::from_utf8(&self.data[offset..offset + len])
                .map_err(|e| DrivenError::InvalidBinary(format!("Invalid UTF-8: {}", e)))?;
            self.string_table.push(s);
            offset += len;
        }

        Ok(())
    }

    /// Get a string by index
    pub fn get_string(&self, idx: u32) -> Result<&'a str> {
        self.string_table
            .get(idx as usize)
            .copied()
            .ok_or_else(|| DrivenError::InvalidBinary(format!("Invalid string index: {}", idx)))
    }

    /// Decode all rules from the binary
    pub fn decode_all(&self) -> Result<Vec<UnifiedRule>> {
        let mut rules = Vec::new();
        let mut offset = 16 + 5; // Skip header and string table header

        // Skip past string table data
        for s in &self.string_table {
            offset += 2 + s.len();
        }

        // Read remaining sections
        while offset < self.data.len() {
            let section_type = SectionType::try_from(self.data[offset])?;
            offset += 1;

            match section_type {
                SectionType::StringTable => {
                    // Already parsed
                    unreachable!("String table should be first");
                }
                SectionType::Persona => {
                    let (persona, new_offset) = self.decode_persona(offset)?;
                    offset = new_offset;
                    rules.push(UnifiedRule::Persona {
                        name: self.get_string(persona.name_idx)?.to_string(),
                        role: self.get_string(persona.role_idx)?.to_string(),
                        identity: if persona.identity_idx > 0 {
                            Some(self.get_string(persona.identity_idx)?.to_string())
                        } else {
                            None
                        },
                        style: if persona.style_idx > 0 {
                            Some(self.get_string(persona.style_idx)?.to_string())
                        } else {
                            None
                        },
                        traits: persona
                            .traits
                            .iter()
                            .map(|&idx| self.get_string(idx).map(String::from))
                            .collect::<Result<Vec<_>>>()?,
                        principles: persona
                            .principles
                            .iter()
                            .map(|&idx| self.get_string(idx).map(String::from))
                            .collect::<Result<Vec<_>>>()?,
                    });
                }
                SectionType::Standards => {
                    let (standards, new_offset) = self.decode_standards(offset)?;
                    offset = new_offset;
                    for rule in standards.rules {
                        rules.push(UnifiedRule::Standard {
                            category: rule.category,
                            priority: rule.priority,
                            description: self.get_string(rule.description_idx)?.to_string(),
                            pattern: if rule.pattern_idx > 0 {
                                Some(self.get_string(rule.pattern_idx)?.to_string())
                            } else {
                                None
                            },
                        });
                    }
                }
                SectionType::Context => {
                    let (context, new_offset) = self.decode_context(offset)?;
                    offset = new_offset;
                    rules.push(UnifiedRule::Context {
                        includes: context
                            .include_patterns
                            .iter()
                            .map(|&idx| self.get_string(idx).map(String::from))
                            .collect::<Result<Vec<_>>>()?,
                        excludes: context
                            .exclude_patterns
                            .iter()
                            .map(|&idx| self.get_string(idx).map(String::from))
                            .collect::<Result<Vec<_>>>()?,
                        focus: context
                            .focus_areas
                            .iter()
                            .map(|&idx| self.get_string(idx).map(String::from))
                            .collect::<Result<Vec<_>>>()?,
                    });
                }
                SectionType::Workflow => {
                    let (workflow, new_offset) = self.decode_workflow(offset)?;
                    offset = new_offset;
                    let steps = workflow
                        .steps
                        .iter()
                        .map(|step| {
                            Ok(crate::parser::WorkflowStepData {
                                name: self.get_string(step.name_idx)?.to_string(),
                                description: self.get_string(step.description_idx)?.to_string(),
                                condition: if step.condition_idx > 0 {
                                    Some(self.get_string(step.condition_idx)?.to_string())
                                } else {
                                    None
                                },
                                actions: step
                                    .actions
                                    .iter()
                                    .map(|&idx| self.get_string(idx).map(String::from))
                                    .collect::<Result<Vec<_>>>()?,
                            })
                        })
                        .collect::<Result<Vec<_>>>()?;
                    rules.push(UnifiedRule::Workflow {
                        name: self.get_string(workflow.name_idx)?.to_string(),
                        steps,
                    });
                }
            }
        }

        Ok(rules)
    }

    fn decode_persona(&self, offset: usize) -> Result<(PersonaSection, usize)> {
        let mut cursor = Cursor::new(&self.data[offset..]);

        let name_idx = cursor.get_u32_le();
        let role_idx = cursor.get_u32_le();
        let identity_idx = cursor.get_u32_le();
        let style_idx = cursor.get_u32_le();

        let traits_count = cursor.get_u16_le() as usize;
        let mut traits = Vec::with_capacity(traits_count);
        for _ in 0..traits_count {
            traits.push(cursor.get_u32_le());
        }

        let principles_count = cursor.get_u16_le() as usize;
        let mut principles = Vec::with_capacity(principles_count);
        for _ in 0..principles_count {
            principles.push(cursor.get_u32_le());
        }

        let persona = PersonaSection {
            name_idx,
            role_idx,
            identity_idx,
            style_idx,
            traits,
            principles,
        };

        Ok((persona, offset + cursor.position() as usize))
    }

    fn decode_standards(&self, offset: usize) -> Result<(StandardsSection, usize)> {
        let mut cursor = Cursor::new(&self.data[offset..]);

        let count = cursor.get_u32_le() as usize;
        let mut rules = Vec::with_capacity(count);

        for _ in 0..count {
            let category = RuleCategory::from(cursor.get_u8());
            let priority = cursor.get_u8();
            let description_idx = cursor.get_u32_le();
            let pattern_idx = cursor.get_u32_le();

            rules.push(RuleEntry {
                category,
                priority,
                description_idx,
                pattern_idx,
            });
        }

        Ok((
            StandardsSection { rules },
            offset + cursor.position() as usize,
        ))
    }

    fn decode_context(&self, offset: usize) -> Result<(ContextSection, usize)> {
        let mut cursor = Cursor::new(&self.data[offset..]);

        let include_count = cursor.get_u16_le() as usize;
        let mut include_patterns = Vec::with_capacity(include_count);
        for _ in 0..include_count {
            include_patterns.push(cursor.get_u32_le());
        }

        let exclude_count = cursor.get_u16_le() as usize;
        let mut exclude_patterns = Vec::with_capacity(exclude_count);
        for _ in 0..exclude_count {
            exclude_patterns.push(cursor.get_u32_le());
        }

        let focus_count = cursor.get_u16_le() as usize;
        let mut focus_areas = Vec::with_capacity(focus_count);
        for _ in 0..focus_count {
            focus_areas.push(cursor.get_u32_le());
        }

        let dep_count = cursor.get_u16_le() as usize;
        let mut dependencies = Vec::with_capacity(dep_count);
        for _ in 0..dep_count {
            let name = cursor.get_u32_le();
            let version = cursor.get_u32_le();
            dependencies.push((name, version));
        }

        Ok((
            ContextSection {
                include_patterns,
                exclude_patterns,
                focus_areas,
                dependencies,
            },
            offset + cursor.position() as usize,
        ))
    }

    fn decode_workflow(&self, offset: usize) -> Result<(WorkflowSection, usize)> {
        let mut cursor = Cursor::new(&self.data[offset..]);

        let name_idx = cursor.get_u32_le();
        let step_count = cursor.get_u16_le() as usize;

        let mut steps = Vec::with_capacity(step_count);
        for _ in 0..step_count {
            let step_name_idx = cursor.get_u32_le();
            let description_idx = cursor.get_u32_le();
            let condition_idx = cursor.get_u32_le();

            let action_count = cursor.get_u16_le() as usize;
            let mut actions = Vec::with_capacity(action_count);
            for _ in 0..action_count {
                actions.push(cursor.get_u32_le());
            }

            steps.push(WorkflowStep {
                name_idx: step_name_idx,
                description_idx,
                condition_idx,
                actions,
            });
        }

        Ok((
            WorkflowSection { name_idx, steps },
            offset + cursor.position() as usize,
        ))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::format::DrvEncoder;

    #[test]
    fn test_roundtrip_empty() {
        let encoder = DrvEncoder::new();
        let data = encoder.encode(&[]).unwrap();

        let decoder = DrvDecoder::new(&data).unwrap();
        let rules = decoder.decode_all().unwrap();

        assert!(rules.is_empty());
    }

    #[test]
    fn test_invalid_magic() {
        let mut data = vec![0u8; 20];
        data[0..4].copy_from_slice(b"BAD\0");

        let result = DrvDecoder::new(&data);
        assert!(result.is_err());
    }

    #[test]
    fn test_too_short() {
        let data = vec![0u8; 10];
        let result = DrvDecoder::new(&data);
        assert!(result.is_err());
    }
}