Skip to main content

Protect Pipeline (fetch-new-docs)

Overview

The protect cron job selects documents that need a new proof anchored. Selection runs a MongoDB aggregation: documents newer than START_TIME, without an existing proof of the target type, joined via documentRef.

Document timestamp field

Canonical field: createdDate.

SourceField writtenPicked by protect?
Legacy backend / IP Magic Box manualcreatedDate (+ legacy created)Yes (since 1.0.0)
Neo-backend / Tower v2 agent ingestcreatedDate onlyYes (since 1.0.0)
Very old docscreated only (rare)No — migrate if needed

Before 1.0.0, the pipeline matched created (Mongoose legacy), so neo-ingested documents were invisible to protect (admin showed 0/0).

Aggregation pipeline

Stages in fetch-new-docs.ts:

  1. $matchcreatedDate: { $gt: new Date(startTime) }
  2. $projecthash, denomination, createdDate
  3. $lookup — proofs collection where documentRef = _id and type = ps
  4. $match_pa array empty (no proof yet)

Configuration

Env varRole
START_TIMEISO timestamp — only documents after this date are eligible
DOCS_PER_RUNOptional cap on documents processed per protect run

Key files

  • src/database/requests/fetch-new-docs.ts — pipeline definition
  • src/database/requests/fetch-new-docs.spec.ts — unit tests on pipeline stages
  • src/app.service.ts — protect job orchestration
  • Backend createdDate proxy for legacy API consumers
  • Neo-backend writes createdDate on agent deposits