Skip to content

fix/issue-59: Stream the Neo4j push in two passes - #64

Open
rahlk wants to merge 16 commits into
mainfrom
fix/issue-59-oom-on-large-apps
Open

rahlk wants to merge 16 commits into
mainfrom
fix/issue-59-oom-on-large-apps

Conversation

@rahlk

@rahlk rahlk commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #59. --emit neo4j projects one module at a time, in two passes, so the push no longer holds the full output tree or the full GraphRows.

Motivation and Context

On large apps, --emit neo4j ran out of memory. It kept the full output tree and the full GraphRows in memory at the same time. For nats-server, this was approximately 25 GB.

How Has This Been Tested?

  • A parity test compares the streamed rows with the Project rows, on the existing fixtures and through the emitter stream.
  • WriteCypher writes the same bytes on each run (determinism test).
  • A live push of nats-server writes the same graph as the old code: 1,858,128 nodes and 10,419,341 relationships.
  • A new memory-limit test (internal/memtest, build tag memtest) runs cango and Neo4j in containers with no swap. At 16 GB, nats-server peaks at 7.8 GiB and gitea at 15.8 GiB. Both pass.

Breaking Changes

None. The rows in graph.cypher are now in module order. The graph that the file makes does not change.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the Codellm-Devkit Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

  • The emitter returns a re-iterable stream of modules. Pass 1 writes the nodes of each module, then the ghost nodes and the application node. Pass 2 writes the edges.
  • The Bolt writer and the graph.cypher writer use the same two passes. graph.cypher goes to a temporary file, and then the writer renames it.
  • The lazy push, the eager purges and the stored-level gate do not change.
  • The writer sets a module's content_hash only after all node rows are in the database. If a push stops before this, the next lazy push writes the module again.
  • The analysis memory (Reduce analysis memory at -a 4 #62) is not part of this PR.
  • Spec: docs/design/specs/2026-11-24-neo4j-streaming-push.md.

lamwassi and others added 16 commits November 19, 2026 21:35
Spec and CLAUDE.md schema decisions from the design loop: application.artifacts
(all non-.go files, metadata-first, --artifact-text opt-in), can://<app>/artifact ids,
version-less pkg:golang package ids, go.mod requires as dependencies with go.sum locks,
L1 at every level. Config keys deferred.

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
Adds application.artifacts (every non-.go project file: format, roles, size, sha256;
source only with --artifact-text) and application.dependencies (go.mod requires with
direct, locked_version from go.sum, provides_imports by longest prefix). The Neo4j
projector writes :Artifact (app-scoped), :Package (pkg:golang/<module>), HAS_ARTIFACT,
DECLARES_DEPENDENCY and LOCKS. Config keys are deferred.

Refs #45

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
upsertNodes and upsertEdges sent each node or edge group as one UNWIND statement in one write transaction. On a large app at L4 one group holds millions of rows. On nats-server the push exceeded the Neo4j limit dbms.memory.transaction.total.max and failed with MemoryPoolOutOfMemoryError.

Split each group into chunks of boltBatchSize (5000) rows. Each chunk runs in its own write transaction. Build the driver parameters for each chunk only. The snapshot writer already chunks its UNWIND batches.

On nats-server the push now completes with 1858128 nodes and 10419341 relationships, exit 0. Client peak memory stays at about 25 GB. That is the second part of #59 and needs separate work.
Design for part 2 of #59. Two passes over the emitter bound client memory. A testcontainers harness checks a 16 GB limit. Includes #62.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…pen (#59)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…59)

Each app runs as --app-name <sanitised base name>, passed through
MEMTEST_APP_NAME, so every push owns its own can://<name> keyspace in the
fresh Neo4j container; duplicate names fail before any container starts.
The pure helpers and the exit/OOMKilled/peak decision move to an untagged
file with table tests, so plain go test covers them without Docker.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
WithWaitStrategy wraps the strategy in wait.ForAll with a 60 s deadline,
which capped every cango run at about 60 s whatever ForExit's exit timeout.
Use WithWaitStrategyAndDeadline with the same 5 h run bound.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Gitea passes at 16 GB with the streaming push alone (peak 15.8 GiB,
the same under a 24 GB limit), so #59 closes on the push. §8 records
the split and the results; §7.5 drops the expectation that gitea
needs the analysis-memory work.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Node groups flush in group-key order and in 5000-row chunks, so a
:GoModule row could land with its content_hash before the rest of its
module's nodes. A push that died in between left a matching hash; the
next lazy push skipped the module and MATCH dropped the edges into its
missing nodes.

The Bolt writer now upserts :GoModule rows with content_hash = null and
keeps only the (id, hash) pairs; late stamps them in boltBatchSize
UNWIND batches after the final node flush. The final graph is unchanged
and graph.cypher is untouched.

Also: align the package doc's stage numbers with the stager (0-5), mark
upsertNodes/upsertEdges/partitionNodes as test seams, run pushPaths'
replay in its own subtest, and cover the lazy push in StreamPushOrder.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- spec 7.3: the harness mounts the app at /src-app and copies it to /app;
- WriteCypher: a killed process leaves its hidden temp file behind;
- cypher.go header: WriteCypher is deterministic by module key order plus
  each builder's sort; add a byte-for-byte determinism test;
- streamGraph doc: the late and tail builders are app-wide;
- v2emit.Stream is not safe for concurrent use;
- the streamed-script test compares row lines as a multiset keyed by
  statement, and checks its ReadFile error;
- the parity tests name the failing fixture.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Since the renderer writes through a scriptWriter interface instead of
*strings.Builder, golangci-lint's errcheck flags every unchecked
WriteString/WriteByte. Route the writes through put, which drops the
error on purpose: a strings.Builder never fails, and a bufio.Writer
keeps its first error and WriteCypher checks it at Flush.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OOM on v. large applications when emitting to a neo4j database.

2 participants