GoogleCloudPlatform / GoogleCloudPlatform/generative-ai

[Bug]: Hardening required for Always-On Memory Agent (file ingestion crash + duplicate memory connections)

Open Beginner friendly
#2,945 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Jupyter Notebook
Stars
17.7k
Forks
4.5k
Avg merge
12h 38m
Merged PRs (30d)
42

Description

### File Name

`gemini/agents/always-on-memory-agent/agent.py`

### What happened?

### Description

The Always-On Memory Agent has two issues that may affect stability and data integrity.

#### 1. Memory crash risk during file ingestion

The `ingest_file` function reads file content using `file_path.read_bytes()` before validating the file size.

As a result, large files (e.g. >20MB) may be loaded entirely into memory before the size limit check is performed, potentially causing excessive memory usage, performance degradation, or process crashes.

#### 2. Duplicate memory connections

The `store_consolidation` function appends memory connections without checking whether the relationship already exists.

When consolidation is executed multiple times for the same memory pair, duplicate entries may be stored in the `connections` field, resulting in an inflated memory graph and unnecessary storage growth.

### Observed Behavior

#### File ingestion

* Large files are read into memory before size validation.
* Excessive memory consumption may occur for oversized files.
* Agent stability may be affected when processing very large files.

#### Memory connections

* Repeated consolidation operations can create duplicate relationships.
* The `connections` field may contain redundant entries.
* Memory graph data becomes unnecessarily bloated over time.

### Expected Behavior

#### File ingestion

Prompt ingestion should:

* Validate file size before reading file content into memory.
* Skip files larger than 20MB safely.
* Log a warning instead of processing oversized files.

#### Memory connections

Memory consolidation should:

* Prevent duplicate `(from_id, to_id)` relationships.
* Store each connection only once.
* Maintain a clean and deduplicated memory graph.

### How to reproduce

#### Memory ingestion issue

1. Create a file larger than 20MB (for example, 50MB).
2. Place the file into the monitored `./inbox` directory.
3. Run the Always-On Memory Agent.
4. Observe memory usage while the file is processed.

#### Duplicate connection issue

1. Execute `store_consolidation` multiple times using the same source and target memory IDs.
2. Inspect the `connections` field in the `memories` table.
3. Observe duplicate relationship entries.

### Relevant log output

```shell
Large file detected during ingestion.

Current behavior:
- File content is read into memory before validation.
- Memory usage increases unnecessarily.
- Oversized files may affect process stability.

Repeated consolidation operations:

Current behavior:
- Duplicate relationships are appended to connections.
- Memory graph grows with redundant entries.
- Storage usage increases over time.
```

### Code of Conduct

* [x] I agree to follow this project's Code of Conduct

Contributor guide

Open the contributing guide

Research direction

Start in gemini/agents/always-on-memory-agent/agent.py and inspect ingest_file and store_consolidation. Reproduce the oversized-file and repeated-consolidation cases described in the issue, then verify that files over 20MB are skipped with a warning before content is read and that each memory pair appears only once in connections.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
ai, backend
Issue type
Bug
Difficulty
2/5
Estimated time
Half a day
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
72/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.