name: Verify memex node
on:
pull_request:
branches: [main]
types: [opened, reopened, edited]
jobs:
memex-node-check:
name: Verify memex node created
runs-on: ubuntu-latest
if: "!contains(github.event.pull_request.title, '[skip memex]')"
steps:
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd with:
fetch-depth: 0
- name: Verify memex node and parent references
run: |
BASE=origin/${{ github.base_ref }}
# 1. New node files added
NEW_NODES=$(git diff --name-only --diff-filter=A "$BASE"...HEAD -- '.memex/nodes/*.json')
if [ -z "$NEW_NODES" ]; then
echo "::error::No new memex node found in .memex/nodes/. Run: memex node create --parent <id> --goal \"<goal>\""
exit 1
fi
echo "New node(s) found: $NEW_NODES"
# 2. Each new node's parent_ids reference existing node files at HEAD,
# OR the new node has empty parent_ids and is the only such node
# (i.e. a legitimate root, which only happens once per project).
FAILED=0
for NODE_PATH in $NEW_NODES; do
UUID=$(basename "$NODE_PATH" .json)
PARENTS=$(jq -r '.parent_ids[]?' "$NODE_PATH")
if [ -z "$PARENTS" ]; then
# Count parent-less nodes across the post-merge tree
ROOTS=0
for f in .memex/nodes/*.json; do
COUNT=$(jq -r '.parent_ids | length' "$f")
if [ "$COUNT" = "0" ]; then
ROOTS=$((ROOTS + 1))
fi
done
if [ "$ROOTS" -ne 1 ]; then
echo "::error::Node $UUID has empty parent_ids but $ROOTS parent-less nodes exist. The graph must have exactly one root."
FAILED=1
else
echo "Node $UUID is the unique root."
fi
continue
fi
for P in $PARENTS; do
if [ -f ".memex/nodes/$P.json" ]; then
echo "Node $UUID parent $P: ok."
else
echo "::error::Node $UUID references parent $P which has no .memex/nodes/$P.json."
FAILED=1
fi
done
done
exit $FAILED