Skip to content

A complete AI agency at your fingertips - From frontend wizards to Reddit community ninjas, from whimsy injectors to reality checkers. Each agent is a specialized expert with personality, processes, and proven deliverables.

Shell

Validate YAML reorder pointers before mutating a sequence

Rudy Celekli95ccaa639f5d
diff --git a/engineering/engineering-universal-document-compiler.md b/engineering/engineering-universal-document-compiler.md
index c1ee32d9..01f2bb6c 100644
--- a/engineering/engineering-universal-document-compiler.md
+++ b/engineering/engineering-universal-document-compiler.md
@@ -254,14 +254,32 @@ export function executeReorderTransaction(
): { updatedYaml: string; changedRange: [number, number] } {
const doc = parseDocument(yamlSource, { keepSourceTokens: true });
- const seqPath = intent.targetSequencePointer.split('/').filter(Boolean);
- const targetSeq = doc.getIn(seqPath);
+ if (doc.errors.length) throw new Error('Cannot reorder invalid YAML.');
+ const decodePointer = (pointer: string): string[] => {
+ if (pointer === '') return [];
+ if (!pointer.startsWith('/') || /~(?![01])/.test(pointer)) {
+ throw new Error('Invalid JSON pointer.');
+ }
+ return pointer.slice(1).split('/').map(part => part.replace(/~1/g, '/').replace(/~0/g, '~'));
+ };
+ const seqPath = decodePointer(intent.targetSequencePointer);
+ const sourcePath = decodePointer(intent.sourcePointer);
+ const indexToken = sourcePath.pop();
+ if (JSON.stringify(sourcePath) !== JSON.stringify(seqPath) || !/^(0|[1-9]\d*)$/.test(indexToken ?? '')) {
+ throw new Error('Source must be an item in the target sequence.');
+ }
+ const targetSeq = seqPath.length ? doc.getIn(seqPath) : doc.contents;
if (!isSeq(targetSeq)) {
throw new Error(`Target at pointer ${intent.targetSequencePointer} is not a valid sequence.`);
}
- const sourceIndex = parseInt(intent.sourcePointer.split('/').pop() || '0', 10);
+ const sourceIndex = Number(indexToken);
+ if (!Number.isSafeInteger(sourceIndex) || sourceIndex >= targetSeq.items.length ||
+ !Number.isInteger(intent.targetIndex) || intent.targetIndex < 0 ||
+ intent.targetIndex >= targetSeq.items.length) {
+ throw new Error('Reorder indices must identify valid final sequence positions.');
+ }
const [movedNode] = targetSeq.items.splice(sourceIndex, 1);
targetSeq.items.splice(intent.targetIndex, 0, movedNode);