Aperture: Overview of Proposed Changes

A detailed overview of the proposed architectural and analytical changes to Aperture. The agreed direction is to classify collections in configurable batches, identify the dominant line of discussion, use Jev for structured narrative and sentiment decisions, and use Gemini to group unmatched content into candidate narratives.

Project: ApertureFocus: Narrative-centric analyticsStatus: Architecture agreed; implementation audit pending

01Purpose and scope

This document describes the proposed evolution of Aperture from conventional trend analytics into a narrative-centric social-media intelligence system, including the target architecture, processing methodology, persistence changes, and implementation plan.

Analytical methodology

  • Define what a narrative represents.
  • Classify content at batch level.
  • Separate narrative identity, sentiment, and relevance.
  • Handle mixed and unmatched batches without forcing a label.

Model responsibilities

  • Use Jev for structured classification and sentiment.
  • Use Gemini for grouping unmatched content and proposing narratives.
  • Bound model context and API usage.
  • Validate outputs before they enter persistent storage.

Backend architecture

  • Preserve the existing ingestion pipeline where practical.
  • Add narrative-aware processing and persistence.
  • Design global narrative identities that can span trends.
  • Make retries, duplicate prevention, and analytics auditable.
The core objective is to understand not only what is trending, but the distinct conversations taking place within a trend, how those conversations feel, and how they change over time.

02Executive summary

Aperture will retain its existing cross-platform collection and asynchronous processing foundations while adding a batch-level narrative intelligence layer. Rather than treating all posts under a trend as one homogeneous dataset, the system will identify coherent lines of discussion and analyse them as distinct entities.

The scraper's collection size and the analytical batch size will be independently configurable. The initial target is approximately 10,000 events per day, with an initial internal batch size of 20 events. Jev will be the primary engine for batch sentiment and narrative classification. Gemini will be invoked when a batch has no defensible existing narrative match or is too mixed to classify reliably; it will group related unmatched batches before proposing new narrative candidates.

Agreed

Batch-level classification, configurable batch sizing, Jev-based sentiment, Gemini fallback clustering, global narrative identities, and preservation of canonical events.

Guardrail

One batch may receive at most one primary narrative assignment, but it is not required to receive one if the evidence is mixed or insufficient.

Implementation posture

Audit existing queues, workers, database helpers, migrations, and package dependencies before making code changes. The supplied configuration and migration are reference baselines, not immutable designs.

03Core concept: what is a narrative?

A narrative is a distinct, meaningful line of discussion occurring within a trend. It is not limited to a causal explanation, a factual claim, or a single opinion. It can describe how people frame an event, debate its legitimacy, discuss an associated issue, or talk about its consequences.

Example: “Protests in India”

Protesters as agitators

Posts framing protesters as troublemakers, disruptors, or threats to public order.

Demands are legitimate

Posts defending protesters' demands, their right to protest, or the legitimacy of their cause.

Women's empowerment

Posts discussing women's rights, gender equality, safety, or broader empowerment issues associated with the protest.

Economic disruption

Posts discussing traffic, business interruption, supply disruption, or price effects associated with the protest.

These narratives do not all answer the same question. Some express a framing of the participants; some discuss the legitimacy of the protest; others focus on a connected social issue or consequence. They are separate lines of conversation within the same trend.

Working definition: A narrative is a coherent and recurring line of discussion within or across trends, evidenced by content that meaningfully participates in that discussion.

Shared keywords alone are insufficient. A post that mentions women's empowerment in passing should not automatically be assigned to the women's empowerment narrative. Classification must consider the meaning and relevance of the content.

04Product objectives and analytical outcomes

The narrative layer should make the broader trend more understandable, not replace trend-level analytics. A trend remains the broad subject or event being tracked; narratives represent distinct conversations inside or related to it.

QuestionAnalytical levelExample output
What is happening?TrendTrend summary, volume, velocity, platform distribution.
What are people discussing about it?NarrativeDominant narratives and concise descriptions.
How does the content feel?Batch / narrativePositive, neutral, negative, anger, sarcasm, or other defined affect labels.
Which content belongs to a narrative?Batch and event associationsBatch assignment plus evidence-aware event associations.
Does the same conversation appear elsewhere?Global narrativeAssociations to multiple trends and future cross-trend discovery.
How is it changing?Time-series analyticsVolume and sentiment snapshots, lifecycle, and eventually narrative evolution.

05Current architecture baseline

The supplied backend uses Node.js/CommonJS, BullMQ, Redis, PostgreSQL, and a canonical collection format. The existing pipeline is intended to separate ingestion from analytics processing and final persistence.

ScraperUpstash Redis StreamProducerBullMQ FlowAnalytics WorkersDatabase ParentPostgreSQL

Current worker responsibilities

Sentiment worker

Currently invokes the existing sentiment pipeline. This production path will be replaced by Jev after validation and integration.

Demographic worker

Runs demographic and Google Trends-related analysis using the current project modules.

Trend worker

Runs trend analysis, global ranking enrichment, forecasting, and trend description generation.

Network worker

Runs network analysis for the canonical collection.

Database parent

Collects child results, coordinates persistence, and writes collection, event, trend, and snapshot data.

The current producer validates canonical data from the Upstash stream and constructs a BullMQ flow. The existing database parent expects a fixed set of analytics child results, so introducing narrative outputs requires updating that contract carefully.

The supplied migrate.js creates trend_analytics, but the worker references additional relational tables. The actual database schema must be audited before designing migrations; we must not assume the provided migration is the complete schema.

06Target processing architecture

The ingestion path should remain largely unchanged. The major addition is narrative-aware classification between canonical collection ingestion and final analytical persistence. The final BullMQ dependency graph will be based on the full code audit, especially the existing database parent's child-result and transaction handling.

SCRAPER
   |
   v
UPSTASH REDIS STREAM
   |
   v
PRODUCER: read, validate, and construct BullMQ flow
   |
   +---------------------+--------------------+-------------------+
   |                     |                    |                   |
   v                     v                    v                   v
Trend analysis    Demographic analysis  Network analysis   Jev analysis
                                                              |
                                                    Split collection into
                                                    configurable batches
                                                              |
                                                              v
                                                    Retrieve candidate pool
                                                              |
                                                              v
                                                    Batch narrative + sentiment
                                                              |
                                       +----------------------+------------------+
                                       |                      |                  |
                                       v                      v                  v
                                 Existing match          Mixed batch       No suitable match
                                       |                      |                  |
                                       |                      +--------+---------+
                                       |                               |
                                       |                               v
                                       |                     Gemini fallback
                                       |                     group and propose
                                       |                               |
                                       +---------------+---------------+
                                                       |
                                                       v
                                          Validated analysis results
                                                       |
                                                       v
                                           Database persistence parent
                                                       |
                      +-------------------------------+--------------------------+
                      |                               |                          |
                      v                               v                          v
              Canonical events                 Narrative records          Snapshots / metrics
                      |                               |                          |
                      +-------------------------------+--------------------------+
                                                       |
                                                       v
                                      Searchable narrative intelligence
The Jev stage returns structured decisions; it should not independently commit partially coordinated event-to-narrative relationships. Persistence remains coordinated so event records, batch decisions, narrative identities, and associations can be written in a safe order.

07Batch-level classification strategy

The classification unit is a batch, not an individual event. This is intended to reduce redundant API requests, give Jev context across related posts, and avoid creating excessive niche narratives from small wording differences.

Configurable splitting

Collection sizeConfigured batch sizeResulting batches
30 events2020 + 10
100 events2020 + 20 + 20 + 20 + 20
7 events207
40 events1010 + 10 + 10 + 10

The current scraper may collect up to approximately 30 events per interval, but the analytical layer must not depend on that fixed value. If collection size changes, only configuration should need adjustment.

Jev's expected decision for each batch

  1. Review the batch content and the bounded candidate narrative pool.
  2. Identify the existing narrative that best represents the dominant line of discussion, if one does.
  3. Assess relevance and whether the apparent match is sufficiently strong.
  4. Determine whether the batch is mixed or lacks a defensible match.
  5. Return batch-level sentiment characteristics using the agreed sentiment taxonomy.
  6. Provide structured outputs that the backend can validate and persist.
One batch receives at most one primary narrative assignment. It does not follow that every event in the batch expresses that narrative equally, or that every batch must be assigned to a narrative.

08Mixed and unmatched batches

We agreed that the system must be able to abstain when a batch does not have a defensible dominant narrative. Forcing a winner in a mixed batch would create false precision and contaminate downstream analytics.

Illustrative outcomes

Matched

A clear dominant line of discussion corresponds to an existing narrative. Assign the batch to that narrative.

Mixed

Multiple competing lines of discussion are present and none is sufficiently dominant. Escalate the batch to the fallback workflow.

No suitable match

The batch represents a meaningful discussion not covered by the existing candidates. Send it for Gemini-assisted grouping and narrative proposal.

Insufficient evidence

The content is too ambiguous, noisy, or sparse for a reliable decision. Preserve the data and avoid inventing a narrative.

These are logical result categories; the exact persisted status names will be finalised with the schema. An uncertain model response and a technical API failure must remain distinct: uncertainty is a valid analytical result, while a timeout or service error is an operational failure that may require a retry.

09Relevance and event-assignment policy

Canonical events must be preserved even when they are not strongly relevant to a particular narrative. Their presence may still matter for understanding the trend collection and the broader conversation.

However, trend association and narrative evidence are not equivalent. A post may be collected because it relates to a trend without meaningfully participating in the batch's assigned narrative.

MetricIrrelevant or weakly related eventsReason
Trend / collection volumeIncludedThey remain part of the collected trend dataset.
Batch processing volumeIncludedThey were processed as part of the batch.
Narrative evidence or supportNot automatically counted as strong evidenceBatch assignment must not imply every event expresses the narrative.
Narrative-specific sentimentIncluded only when meaningful relevance supports the analysisPrevents incidental mentions and noise from distorting narrative sentiment.

Where appropriate, the system can store an estimated relevant-event count or relevance measure. Such model-based values must be labelled as estimates, not verified ground truth.

Sentiment is not support. A post can express anger, sarcasm, fear, or neutral reporting while discussing a narrative; the system must not infer agreement or endorsement from sentiment alone.

10Jev's responsibilities

Jev is the primary structured reasoning and classification engine. It will handle decisions that can be expressed as typed questions against the current batch and candidate narrative context.

Narrative selection

Identify the best-fitting existing narrative for the dominant line of discussion, or indicate that no candidate is a defensible fit.

Dominance and ambiguity

Evaluate whether one narrative represents the batch sufficiently well or whether the content is mixed.

Relevance

Assess whether the content meaningfully participates in the selected narrative rather than merely mentioning related terms.

Sentiment and affect

Classify the batch's overall emotional characteristics according to a defined taxonomy, including sarcasm or anger where supported by the design.

Structured output

Return machine-usable answers that can be validated rather than generating unconstrained prose for the application to parse.

Cost control

Answer compatible questions in one request where possible and operate on a bounded candidate set.

The implementation will use the TypeSafe SDK and documented response types. The exact primitives, schemas, and response validation will be verified against the installed SDK version and current official documentation during implementation.

11Sentiment architecture

The existing sentiment pipeline will be replaced by Jev as the production sentiment engine. The reason is cost efficiency and suitability for the required structured classification workflow; the user has already validated that Jev works for this purpose.

Separate analytical dimensions

DimensionQuestionExample
NarrativeWhat line of discussion is this about?“Protesters are agitators.”
Sentiment / affectWhat emotional tone is expressed?Anger, sarcasm, fear, neutral reporting.
StanceWhat position does the author take towards a specific claim or subject?Supports, opposes, questions, or does not clearly take a position.
RelevanceHow meaningfully does the content participate in this narrative?Directly discusses the claim versus mentions it incidentally.

These dimensions must not be collapsed into one label. A negative sentiment does not necessarily mean opposition to a narrative, and positive wording can be sarcastic. The sentiment taxonomy will be explicitly defined in Jev parameters.

The primary initial output is batch-level sentiment. Narrative-level sentiment should be calculated from content meaningfully associated with that narrative where the available evidence supports it. The system must not present a batch-level aggregate as if every post had that same sentiment.

Migration approach

  1. Integrate Jev into the sentiment/narrative processing path.
  2. Validate the output and compare a representative sample against the old pipeline as a sanity check.
  3. Make Jev the production path.
  4. Remove obsolete sentiment dependencies and modules once references and tests confirm they are no longer needed.

12Gemini fallback and narrative formation

Gemini is not the default classifier for every batch. Its purpose is to help discover coherent narratives when the existing narrative pool does not provide a defensible match or the batch is too mixed to classify reliably.

Jev unmatched / mixed batchesBounded fallback workloadGemini groupingCandidate narrativesValidation and deduplicationPersist

Fallback workflow

  1. Collect candidates: gather unmatched or ambiguous batch results into a bounded workload. The system should not necessarily wait for the whole day; window size and trigger thresholds can be configured.
  2. Group related content: Gemini identifies coherent groups of related unmatched batches rather than creating a narrative for every batch independently.
  3. Propose narratives: each candidate includes a concise title, description, core line of discussion, associated batch IDs, and supporting evidence or ambiguity notes.
  4. Validate output: validate schema, required fields, length limits, and referenced batch IDs before accepting the response.
  5. Check for duplicates: compare candidates with existing global narratives to avoid creating near-duplicate identities.
  6. Persist and reuse: approved narratives become available in the candidate pool for future Jev classification.
Gemini proposes candidate narratives; the application remains responsible for validation, duplicate resolution, idempotency, and database writes. A model response must not directly create unchecked production records.

If no coherent grouping emerges, the batches remain unassigned or pending review rather than receiving invented narrative labels.

13Confidence, candidate pools, and cost

The candidate pool may contain many semantically related narratives. The system therefore needs to distinguish a model's preference for one candidate from evidence that the batch genuinely has a dominant narrative.

Decision principles

  • Use a configurable decision threshold rather than hard-coding a number throughout the code.
  • Start with a provisional threshold of 0.80 only if the selected Jev primitive exposes a compatible decision-strength or confidence measure.
  • Do not interpret the threshold as a calibrated 80% probability of correctness without empirical validation.
  • Consider ambiguity between candidates and relevance to the batch, not only the top-ranked candidate.
  • Permit mixed or insufficient-evidence outcomes.
Important: a Choice distribution or confidence signal is not automatically the percentage of posts belonging to each narrative. A model's preference for a candidate does not establish that a literal majority of events express it. The implementation must use only measurements that the selected primitive actually supports.

Token and request efficiency

The initial workload target is approximately 10,000 events per day. With a batch size of 20, that corresponds to roughly 500 classification batches per day if every event is processed once and there are no retries. This is a planning estimate, not a guaranteed request count or cost.

  • Send compatible questions in one Jev request.
  • Keep candidate pools bounded and relevant.
  • Avoid repeating large context packages unnecessarily.
  • Use Gemini only for unresolved batches.
  • Measure requests, input tokens, latency, retries, and fallback rates.

Published TypeSafe pricing has been cited as $0.042 per million input tokens, with output tokens free under the referenced pricing model. Current pricing and the deployed account's terms should be rechecked during implementation. Real costs will depend on state size, candidate descriptions, prompt overhead, retries, and fallback volume.

14Conceptual data model

Narratives are global entities. They must not be children of a single trend in the relational model, because the same underlying line of discussion may occur in several trends. Cross-trend semantic clustering is a later feature, but the schema should not prevent it.

Trends

Broad subjects being tracked, with existing trend identifiers and metadata.

Collections

Ingestion/analysis runs that preserve the canonical collection and its processing lifecycle.

Events

Canonical posts or other collected content, independent of whether a narrative is assigned.

Narratives

Global, searchable identities for coherent lines of discussion.

Narrative–trend links

Associations showing where a global narrative appears or is relevant.

Narrative batches

Primary batch-level classification decisions, status, method, and decision metadata.

Narrative–event links

Event-level associations where the content meaningfully participates in the narrative.

Narrative snapshots

Time-indexed metrics for volume, sentiment, relevance, and growth.

Trend 1 -----------+                         +----------- Trend 2
                   |                         |
                   +---- Narrative X --------+
                              |
                    +---------+----------+
                    |                    |
              Batch decisions       Event associations
                    |                    |
              Batch metrics        Relevant evidence
                    |                    |
                    +---------+----------+
                              |
                     Narrative snapshots

This is a conceptual model, not a final migration. Existing tables and keys must be inspected before deciding which entities are new and which existing tables should be extended.

Why keep batch and event associations separate?

The batch record captures the model's primary decision, preserving the agreed batch-first design. Event associations allow more precise evidence and sentiment calculations without asserting that every event in a batch belongs equally to its primary narrative. The distinction also makes results easier to audit.

15Persistence flow and database integrity

The existing database parent gathers analytics child results and coordinates persistence. The preferred approach is to retain that responsibility unless the full code audit identifies a strong reason to restructure it.

Expected persistence sequence

  1. Validate the canonical collection and analytics child outputs.
  2. Persist or resolve the trend and collection records.
  3. Persist canonical events and required author records.
  4. Resolve existing narrative identities and validate proposed new identities.
  5. Persist batch-level classification decisions.
  6. Persist narrative-to-trend and narrative-to-event associations where justified.
  7. Persist sentiment and narrative snapshots using stable identifiers.
  8. Mark the collection complete only when the required persistence operations succeed.

The precise transaction boundaries and ordering must match the existing schema and foreign-key relationships. We should not write narrative-event associations before their referenced event records exist.

Migration strategy

  • Inspect the live schema and current database helper modules before writing DDL.
  • Preserve existing trend, collection, event, and analytics data.
  • Add constraints and indexes that support idempotent writes and common queries.
  • Use explicit migration files or a repeatable migration process rather than relying solely on an old one-table bootstrap script.
  • Keep queryable fields relational; use JSONB for flexible evidence or model metadata where justified.
The current migrate.js is a baseline reference only. It creates trend_analytics, while the worker references several other tables. The actual database state must be verified before a migration is finalised.

16Candidate retrieval and global narrative search

Jev should not receive every narrative in the database on every request. As the global pool grows, context size and irrelevant candidates would increase cost and reduce decision quality.

Initial candidate retrieval

  • Prioritise narratives already associated with the current trend.
  • Allow relevant global narratives to be considered when they plausibly apply across trends.
  • Apply a configurable maximum candidate count, initially in the approximate range of 30–50 as a starting hypothesis.
  • Keep candidate descriptions concise and structured.
  • Measure whether the correct narrative is present in the candidate set before tuning the limit.

The first implementation can use database-backed retrieval and stable matching metadata. More advanced semantic search or embeddings can be added later if the narrative library's size and measured retrieval quality justify it.

Longer-term capability

Narratives should be searchable entities in their own right. A researcher should eventually be able to search for a subject or line of discussion, find related narratives across multiple trends, inspect their supporting content, and compare their evolution over time. This is a future product capability; the initial schema should enable it without pretending full semantic clustering is already implemented.

17Snapshots, metrics, and analytics

Narrative analytics must be time-aware. A narrative is not just a label; it is an entity whose volume, sentiment, relevance, and prominence may change across collections and over time.

Metric familyInitial purposeImportant qualification
VolumeCount of batches/events associated with a narrative over a period.Distinguish batch volume from relevant event volume.
SentimentAggregate emotional characteristics for relevant narrative content.Do not treat sentiment as endorsement or stance.
RelevanceEstimate how strongly associated events participate in the narrative.Model estimates should be identified as such.
Trend associationShow the trends in which a narrative appears.One narrative may span several trends.
Temporal changeTrack growth, decline, and changing emotional patterns.Requires consistent snapshots and comparable measurement definitions.

The existing trend snapshot and analytics tables should be reused where their semantics fit. Narrative-specific snapshots should be added only after reviewing existing snapshot conventions and uniqueness keys.

18Queue and worker integration

The current flow has sentiment, demographic, trend, and network children coordinated by a database parent. The narrative feature will change the sentiment processing path and extend the child-result contract.

Integration principles

  • Keep canonical validation at the ingestion boundary.
  • Keep analytics workers focused on producing structured results.
  • Update the database parent's expected child-result set deliberately; a new child or combined Jev worker must not be silently omitted.
  • Ensure a failed Jev request is not confused with a valid mixed classification.
  • Decide whether Gemini fallback runs inside the classification workflow or in a separate queue based on latency, retry isolation, and throughput needs.
  • Keep queue names, payload schemas, and worker responsibilities centralised and versioned where practical.
No additional queue will be added merely for architectural neatness. A separate Gemini fallback queue is justified if it gives clear benefits in latency isolation, bounded concurrency, retries, or operational control.

The final topology will be decided after inspecting queues.js, worker.js, producer.js, and the database modules together.

19Reliability, idempotency, and failure handling

The pipeline must remain correct when jobs retry, workers restart, API calls time out, or database transactions fail. Model calls are not guaranteed to execute exactly once, so persistence must be designed for idempotent reprocessing.

Idempotency

Stable collection/batch identifiers, unique constraints, and upsert patterns should prevent duplicate narrative identities, event associations, or snapshots.

Retries

Use bounded retries and backoff for transient API and infrastructure failures. Avoid infinite retries or repeated expensive calls without limits.

Timeouts

Set explicit request timeouts and handle stalled jobs without losing the source collection or its processing state.

Partial failures

Represent pending or failed fallback processing explicitly. Do not mark a collection fully complete if required persistence has failed.

Transactions

Coordinate relational writes so that events, narrative records, associations, and snapshots remain consistent.

Concurrency

Bound Jev/Gemini concurrency and guard against simultaneous creation of equivalent narratives.

We should also inspect the current producer's polling and pending-message handling during the audit, including whether polling cycles can overlap and whether reclaimed stream messages are actually reprocessed. Any change there should be justified by the existing implementation rather than bundled into the feature without review.

20Validation, provenance, and analytical integrity

Narratives describe what content discusses; they are not automatically verified statements of fact. The system must avoid turning allegations, rumours, or a model-generated summary into asserted truth.

  • Preserve links or stable identifiers back to the original events wherever available.
  • Store the narrative's descriptive framing separately from factual verification.
  • Validate all model output against an explicit schema and field limits.
  • Validate that referenced batch IDs and event IDs belong to the expected collection.
  • Keep model confidence/decision metadata separate from observed counts.
  • Use cautious language in narrative summaries where the underlying content contains allegations or uncertain claims.
  • Do not label an account a bot solely because its posts are similar or its engagement is unusual; such signals are not proof and bot detection is outside the initial core scope unless separately prioritised.
The core implementation is narrative classification, batch sentiment, fallback grouping, and persistence. Broader features such as coordinated-activity detection, advanced search disambiguation, and cross-trend semantic clustering should remain separate workstreams unless explicitly pulled into the first release.

21Configuration and environment

The supplied config.js establishes required infrastructure environment variables for Upstash Redis, local Redis, and PostgreSQL. It exposes shared and worker-specific Redis connections, PostgreSQL connection helpers, health checks, and shutdown handling.

The narrative implementation should extend configuration in a controlled way, validating required settings at startup without exposing secrets in logs or reports.

SettingInitial proposalPurpose
NARRATIVE_BATCH_SIZE20Internal classification batch size.
NARRATIVE_MATCH_THRESHOLD0.80 provisionalDecision threshold, only if compatible with the selected Jev output.
NARRATIVE_MAX_CANDIDATES30–50 initial rangeBound candidate narratives in each request.
NARRATIVE_FALLBACK_ENABLEDtrueEnable Gemini-assisted narrative formation.
Fallback grouping window/limitTo be tunedBound the unmatched batches grouped in a fallback run.
Model concurrency and retriesTo be tunedProtect latency, availability, and cost.

Exact environment variable names and defaults will be finalised after inspecting package conventions and existing configuration patterns. API keys belong in environment configuration and must never be committed or copied into the report.

22Observability and operational metrics

Cost reduction and classification quality must be measured, not assumed. The implementation should make it possible to understand how the system behaves under real ingestion conditions.

Throughput

  • Collections and events processed.
  • Internal batches produced.
  • Queue wait and processing latency.

Classification quality signals

  • Existing narrative match rate.
  • Mixed/uncertain batch rate.
  • Fallback rate.
  • Relevance estimates and duplicate candidates.

Cost and API health

  • Jev request count and token usage.
  • Gemini request count and input size.
  • Retries, timeouts, and failures.
  • Estimated cost per 1,000 events.

Persistence health

  • Duplicate prevention events.
  • Transaction failures and rollbacks.
  • Pending fallback work.
  • Idempotent retry outcomes.

These metrics will let the team tune batch size, candidate count, thresholds, fallback windows, and concurrency based on observed results rather than guesswork.

23Implementation plan and deliverables

Phase 1 — Code and schema audit

Inspect the complete worker/producer integration, queue definitions, database helper modules, package dependencies, current sentiment interface, and live schema/migrations.

Deliverable: exact file-level change list and confirmation of existing data/persistence contracts.

Phase 2 — Finalise the data model

Define global narrative identity, trend associations, batch decisions, event associations, snapshots, status values, unique constraints, and migration order.

Deliverable: reviewed schema and migration plan.

Phase 3 — Implement Jev classification

Build a standalone service that splits collections, retrieves candidates, asks compatible structured questions, validates results, and returns batch sentiment and narrative decisions without writing directly to the database.

Deliverable: Jev service, output contract, and unit tests.

Phase 4 — Implement Gemini fallback

Group related unmatched batches, generate structured narrative candidates, validate them, check for duplicates, and expose approved results for persistence.

Deliverable: fallback service and bounded retry/concurrency policy.

Phase 5 — Integrate queues and persistence

Update queue payloads, child-result validation, database parent logic, relational writes, snapshots, and processing status transitions.

Deliverable: end-to-end ingestion-to-persistence workflow.

Phase 6 — Retire the old sentiment path

Validate Jev output on a representative sample, move production processing to Jev, and remove obsolete sentiment code/dependencies after confirming there are no remaining consumers.

Deliverable: Jev-based production sentiment with obsolete path removed.

Phase 7 — Reliability and performance testing

Test edge cases, retries, idempotency, fallback behaviour, database consistency, and realistic load. Tune configuration using observed usage.

Deliverable: tested implementation, configuration documentation, and operational metrics.

24Testing and acceptance criteria

AreaScenariosExpected result
Batch splittingEmpty, smaller-than-size, exact-size, and larger-than-size collections.Stable, deterministic batches; no event lost or duplicated.
Existing matchClear match to a known narrative.One primary batch assignment with validated output.
Mixed contentSeveral plausible narratives without a defensible dominant line.Batch remains unforced and enters fallback processing.
New narrativeRelated unmatched batches describe a new line of discussion.Gemini proposes a coherent grouping; validated candidate is persisted once.
Irrelevant eventsBatch includes noise or incidental mentions.Events remain preserved; narrative metrics do not blindly treat all as equal evidence.
SentimentSarcasm, anger, neutral reporting, mixed emotional tone.Structured output follows the defined taxonomy without equating sentiment with stance.
RetriesSame job reprocessed or API response times out.No duplicate narratives, links, or snapshots; transient failures are handled safely.
PersistenceFailure during a multi-table write.Transaction handling preserves consistency and accurate completion status.
Scale and costRepresentative daily workload and fallback rates.Throughput, latency, token usage, and estimated cost are measurable.

25Agreed decisions and design principles

DecisionStatusMeaning
Narrative definitionAgreedA coherent line of discussion within a trend; not limited to a claim or cause.
Classification unitAgreedClassify internal batches as a unit, not every event independently.
Initial batch sizeAgreed20 events, configurable independently of scraper collection size.
Mixed batch behaviourAgreedDo not force an arbitrary narrative assignment.
Irrelevant contentAgreed in principlePreserve canonical events; distinguish trend association from narrative evidence.
Sentiment engineAgreedJev replaces the existing sentiment engine after integration and sanity validation.
Gemini fallbackAgreedGroup related unmatched batches before proposing new narratives.
Narrative scopeAgreedGlobal narrative identities can be associated with multiple trends.
Confidence handlingAgreed in principleUse configurable decision logic and treat model signals according to their documented semantics.
Initial throughput targetAgreedApproximately 10,000 events/day, with room to scale.
Persistence architectureProposedRetain the database parent as coordinator unless code audit justifies a change.
Advanced cross-trend clusteringFutureEnable through global narrative identities; implement advanced semantic clustering later.

26Items to verify during implementation

The conceptual direction is settled. The remaining items are implementation details that require the actual code and schema, rather than further high-level debate.

Codebase and schema

  • Actual contents of dataIngestion/db/.
  • Current tables, foreign keys, and unique constraints.
  • Queue definitions and exact child-result contracts.
  • Installed Node.js and SDK versions.

Jev output contract

  • Which primitives best represent narrative choice, relevance, and sentiment.
  • Which response fields are available and how they should be interpreted.
  • How to represent mixed or insufficient-evidence decisions.

Fallback policy

  • Fallback grouping window and maximum input size.
  • Duplicate narrative matching policy.
  • Retry and concurrency limits.
  • Whether fallback should use a separate queue.

Operational tuning

  • Initial candidate pool limit.
  • Threshold validation sample.
  • Metrics and logging format.
  • Actual token usage and cost per event.

These are not blockers to the agreed architecture. They are details to finalise after reviewing the project files and validating the selected SDK interfaces.

27Proposed direction and next steps

Aperture will evolve from trend-level social media analytics into a narrative-centric intelligence system. The current ingestion pipeline remains the foundation; the key addition is a batch-oriented narrative and sentiment layer with a hybrid classification and discovery strategy.

Cross-platform sources
        ↓
Canonical events and collections
        ↓
Configurable internal batches
        ↓
Jev: dominant narrative + sentiment + relevance + ambiguity
        ↓
Existing narrative match ───────────────┐
        │                                │
        └─ Mixed / no suitable match     │
                    ↓                    │
             Gemini grouping             │
                    ↓                    │
          Validated narrative candidates │
                    └──────────┬─────────┘
                               ↓
                 Coordinated PostgreSQL persistence
                               ↓
           Global narratives + trend/event associations
                               ↓
          Narrative analytics, snapshots, and discovery
Final direction: Aperture should answer not only “What is trending?” but “What distinct lines of discussion are forming around this trend, which content meaningfully belongs to each, what emotional tone surrounds them, how are they changing, and where else do similar narratives appear?”

Immediate next step: audit queues.js, package.json, the database helper modules, existing schema/migrations, and the current sentiment interface. Then finalise the schema and implement the complete integrated change set, with tests and migration instructions.

Document status: proposed architecture and change overview based on the requirements and constraints discussed. Proposed schema objects, configuration names, and operational thresholds are explicitly provisional until verified against the current codebase.