Compare commits
82
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
302fd30af1 | ||
|
|
f5ea0af75d | ||
|
|
1c2f48579f | ||
|
|
d29615abeb | ||
|
|
0965519e16 | ||
|
|
7e9f0d98d8 | ||
|
|
0ff4538f36 | ||
|
|
a9709788d8 | ||
|
|
c1e5916b51 | ||
|
|
97de5480bd | ||
|
|
97d9309ca8 | ||
|
|
b1b3e0a291 | ||
|
|
53507c7abe | ||
|
|
5a1d252787 | ||
|
|
8895fea775 | ||
|
|
78882f3300 | ||
|
|
988667cd87 | ||
|
|
72968f831f | ||
|
|
af25f51f9e | ||
|
|
b3f13e130f | ||
|
|
a621262fac | ||
|
|
e027635d3f | ||
|
|
4cb33b7108 | ||
|
|
b50c9f33eb | ||
|
|
545b7d38bf | ||
|
|
5f78d67363 | ||
|
|
33d7713d4e | ||
|
|
68a4b546f9 | ||
|
|
cbcc50f35e | ||
|
|
528d2c9fd7 | ||
|
|
0eb9a2844e | ||
|
|
8e56bf7a37 | ||
|
|
352fffb3da | ||
|
|
5103492762 | ||
|
|
082aeee1f2 | ||
|
|
201901d35e | ||
|
|
42c0ca1d14 | ||
|
|
0ca2e56ccc | ||
|
|
f328cdbf8e | ||
|
|
3f57a3c978 | ||
|
|
19a7af0e71 | ||
|
|
5791c0d1e7 | ||
|
|
bf92105210 | ||
|
|
3387247842 | ||
|
|
791285f4c9 | ||
|
|
dc762df78b | ||
|
|
8277a72aea | ||
|
|
5eed0f81fb | ||
|
|
59ca5afca6 | ||
|
|
2648012ad5 | ||
|
|
63a80092c2 | ||
|
|
6ba49bf112 | ||
|
|
cf410aa9cf | ||
|
|
81ed1d816f | ||
|
|
30c0b1d5ff | ||
|
|
1086f4d582 | ||
|
|
32539e7a82 | ||
|
|
cfae020a31 | ||
|
|
5d51513ec8 | ||
|
|
9db5b49ccb | ||
|
|
cb7df2a022 | ||
|
|
812d1c3846 | ||
|
|
c8b660651e | ||
|
|
bdd2d9cf84 | ||
|
|
fc2e51a8cc | ||
|
|
d4fca0639e | ||
|
|
a17230dadb | ||
|
|
f7961f340a | ||
|
|
cfebae118d | ||
|
|
d76bfa6474 | ||
|
|
b169906fcb | ||
|
|
f3b417ced4 | ||
|
|
e9698a293c | ||
|
|
aaebe64244 | ||
|
|
f3d13c4351 | ||
|
|
3e8a006f65 | ||
|
|
a70830970d | ||
|
|
556d95df84 | ||
|
|
ad05303d23 | ||
|
|
423cb00dbf | ||
|
|
e09fbd16f9 | ||
|
|
6718ba8bdf |
No files matched your search
@@ -40,6 +40,7 @@ MEMORY_EMBEDDING_DIMS=1024
|
||||
# MEMORY_EMBEDDER_TIMEOUT=15
|
||||
# MEMORY_EMBEDDER_MAX_RETRIES=2
|
||||
# MEMORY_EMBEDDER_RETRY_DELAY=0.3
|
||||
# MEMORY_EMBEDDER_BATCH_SIZE=64
|
||||
# MEMORY_EMBEDDER_EXTRA_HEADERS={}
|
||||
# MEMORY_EMBEDDER_EXTRA_BODY={}
|
||||
|
||||
|
||||
+127
@@ -4,6 +4,72 @@ Hy Memory and MemHub are versioned and released independently.
|
||||
|
||||
## Unreleased
|
||||
|
||||
- Hy Memory: new `hy_memory.reembed` module for switching the embedding
|
||||
model. Writers must be paused for the whole run. Nothing is derived from
|
||||
the provider or endpoint: source and target are named explicitly, and the
|
||||
identity of a space is the model name plus the width (`space_identity`).
|
||||
Only Chroma and Kuzu are supported.
|
||||
- `vector_space_names(config, base_name=None)` returns every physical
|
||||
collection a client opens for the six vector roles: memory, entities,
|
||||
coding keys and working facts, each in the working and coding variants.
|
||||
- `reembed_vector_space(source, target, embedding=...)` pages every raw
|
||||
source collection, covering all users and all layers including L1_RAW. It
|
||||
embeds the text the write path embedded (`reembed_text`: the canonical
|
||||
state's `state_retrieval_text`, a schema's `retrieval_text`, otherwise the
|
||||
document). It then writes each record into the target with the same id,
|
||||
document and metadata. Each batch's widths are checked before anything is
|
||||
written. A cancel stops at a batch boundary (`ReembedCancelled`). A rerun
|
||||
skips the ids the target already holds, so it resumes, and a merge never
|
||||
overwrites the target. The source is never written.
|
||||
- `plan_reembed`, `verify_reembed` and `drop_vector_space` complete the
|
||||
vector side. `verify_reembed` checks ids, metadata, per-user counts and
|
||||
the width, and runs a self-recall sample. `drop_vector_space` refuses
|
||||
collections that a live client in this process holds open.
|
||||
- `scan_vector_spaces(persist_directory)` is read-only. It lists every
|
||||
space on disk with its roles, counts, stored width, per-user counts and
|
||||
the models recorded in metadata. `diff_spaces(source, target)` returns the
|
||||
ids that the source holds and the target lacks, whatever their status.
|
||||
- `reembed_graph(source_path, target_path, embedding=..., target_dims=...)`
|
||||
builds a new Kuzu graph at the new width. It copies every node and edge
|
||||
and recomputes `Memory.embedding`, `Memory.beh_embedding`, and the Topic
|
||||
and HierarchyTopic embeddings, then rebuilds the HNSW index. The source
|
||||
graph is opened read-only and kept for rollback. A run that fails or is
|
||||
cancelled leaves no target behind, and a rerun starts over.
|
||||
`verify_graph_reembed` and `drop_graph` complete the graph side.
|
||||
- Hy Memory: `EmbedService.embed_batch` now splits large batches into
|
||||
requests of `embedder.batch_size` texts each (`MEMORY_EMBEDDER_BATCH_SIZE`,
|
||||
default 64); before, it sent them all in one request. On a 429 it waits for
|
||||
the `Retry-After` time plus jitter, capped at 60 s. It takes a per-call
|
||||
`max_retries`. A returned vector whose width differs from the configured
|
||||
`embedding_dims` now raises `EmbeddingDimensionMismatchError` and is not
|
||||
retried.
|
||||
|
||||
## MemHub 1.0.5 — 2026-10-28
|
||||
|
||||
Build 36, on `hy-memory==2.0.8`. Linux x64/arm64 Runtime and macOS arm64
|
||||
App and Runtime. Upgrading from 1.0.4 migrates the existing library to SDK
|
||||
authority on first launch.
|
||||
|
||||
- MemHub turns off the HY pipeline log and trace at every diagnostic level
|
||||
(hy-memory 2.0.8 `MEMORY_PIPELINE_LOG_ENABLED=false`,
|
||||
`MEMORY_PIPELINE_TRACE_ENABLED=false`), so the per-step JSONL files under
|
||||
`hy-memory/governed-v2/logs/` and the `pipeline_logs` rows stop growing.
|
||||
The Debug panel no longer shows the HY pipeline section. Existing files are
|
||||
left in place.
|
||||
- HY SDK text logs are written once, to `hy_memory.log`. They are no longer
|
||||
copied to `service.log` and `service-error.log` through the SDK's
|
||||
stdout/stderr streams. SDK warnings and errors also go to `core.log`, so the
|
||||
Debug panel still shows them. A live switch of the diagnostic level now also
|
||||
changes the level of `hy_memory.log`.
|
||||
|
||||
- MemHub shows when working and coding experiences were talked about. After
|
||||
the SDK upgrade every one of them read "时间未知" (time unknown) because the
|
||||
SDK never stored their conversation time; hy-memory 2.0.7 stores it and
|
||||
Core now passes it through the memory list, detail, search and home
|
||||
summary. An experience stored before 2.0.7 shows its write time, labelled
|
||||
"写入于" / "写入时间", instead of "时间未知". Metadata repair also writes
|
||||
the conversation time it recovers from the migration bundle into the SDK's
|
||||
coding/working stores when they already have the column.
|
||||
- The SDK migration no longer fails its first run on a home with legacy
|
||||
vectors: reattaching them left a Chroma instance open with its own settings,
|
||||
and the readback that followed could not open the same store
|
||||
@@ -68,6 +134,67 @@ Hy Memory and MemHub are versioned and released independently.
|
||||
`hy_node_permissions` proof table. Evidence, governed memories, model
|
||||
profiles, SDK stores, and source/host grants are preserved.
|
||||
|
||||
## Hy Memory 2.0.9 — 2026-11-01
|
||||
|
||||
- Published
|
||||
[`hy-memory==2.0.9`](https://pypi.org/project/hy-memory/2.0.9/).
|
||||
- Placeholder values no longer overwrite the user's basic profile (L0). A
|
||||
small extraction model filled `basic_info` with `{"name": "用户"}`,
|
||||
`The user` or `N/A` when the user had said nothing about themselves, and
|
||||
each placeholder superseded the real value, so the L0 chain read
|
||||
费天翔 → Fei → 用户 → The user. Two defences:
|
||||
- `upsert_basic_profile` drops placeholder values: empty, `用户`,
|
||||
`the user`, `user`, `n/a`, `na`, `none`, `null`, `unknown`, `未提供`,
|
||||
`未知`, `不详`, `-`, `?` and the field's own name, compared
|
||||
case-insensitively with whitespace and punctuation stripped. When nothing
|
||||
is left it writes no new version and no new L0 head. Placeholders already
|
||||
stored in the chain no longer override earlier real values when the
|
||||
profile is assembled, so `get_user_profile` of an affected user shows the
|
||||
last real value again without any migration. This also stops an
|
||||
assistant quoting an old memory ("The user's name is 用户.") from being
|
||||
written back.
|
||||
- The extraction prompts (zh and en) state the rule: only what the user
|
||||
said in a `user` turn, never the field name or a placeholder, `{}`
|
||||
otherwise, with a small-talk counter-example.
|
||||
|
||||
## Hy Memory 2.0.8 — 2026-10-28
|
||||
|
||||
- Published
|
||||
[`hy-memory==2.0.8`](https://pypi.org/project/hy-memory/2.0.8/).
|
||||
- New `MEMORY_PIPELINE_LOG_ENABLED` (default `true`) turns the pipeline JSONL
|
||||
log (`{MEMORY_LOG_DIR}/<subdir>/<date>.log`) off. With `false` no JSONL
|
||||
file or directory is written. When `MEMORY_PIPELINE_TRACE_ENABLED` is also
|
||||
`false`, `store_pipeline_log` becomes a no-op that returns `True` at once:
|
||||
no timing, redaction, background task or database write. The init log line
|
||||
now states whether the JSONL log is on or off.
|
||||
- Conversation and memory text no longer appears in INFO or WARNING logs. The
|
||||
`[add]` input, search queries and rewrites, coding experience tasks, fact
|
||||
statements and subjects, reconcile op details, extracted info, embedding
|
||||
inputs and unparsable model output moved to DEBUG. INFO lines keep ids,
|
||||
counts and character lengths; parse-failure warnings remain but report only
|
||||
the length of the output, e.g. `failed to parse output (812 chars)`. The
|
||||
`[embed]` INFO line now carries `request.input_chars` instead of
|
||||
`request.input`. The `JSON_PARSE_FAILED` error message from extraction no
|
||||
longer embeds the model output either.
|
||||
|
||||
## Hy Memory 2.0.7 — 2026-10-28
|
||||
|
||||
- Published
|
||||
[`hy-memory==2.0.7`](https://pypi.org/project/hy-memory/2.0.7/).
|
||||
- Coding and working experiences and facts now keep their conversation time.
|
||||
`add(..., memory_at=...)` reached only the chat path before; the
|
||||
productivity path dropped it, so every experience had only its write time
|
||||
and MemHub showed them as "时间未知" (time unknown). `coding_memory_meta`
|
||||
and `working_fact` gain a nullable `memory_at` column (epoch seconds) in
|
||||
both the working and coding stores; existing databases get it on open, and
|
||||
their old rows stay `None`. Without `memory_at` a write uses the time the
|
||||
request arrived, as chat does. When an experience is updated by a later
|
||||
conversation it keeps the later of the two times.
|
||||
- `list_memories` (`scene="coding"`, `"working"` and `"all"`), `get` and
|
||||
`search` return `memory_at` for experiences and facts in the same field and
|
||||
format as chat (epoch seconds, `None` when unknown). Experience search
|
||||
results now also carry `created_at`.
|
||||
|
||||
## Hy Memory 2.0.6 — 2026-10-27
|
||||
|
||||
- Published
|
||||
|
||||
@@ -33,6 +33,7 @@ English | [中文](README.zh.md)
|
||||
## Latest News
|
||||
|
||||
* *<mark>[new]</mark>* 🔥🔥🔥 **[2026-10]**: 🚀 **Open-sourced** quantized **FP8** (**[Hy-Memory-30B-A3B-FP8](https://huggingface.co/tencent/Hy-Memory-30B-A3B-FP8)**) and **GGUF** (**[Hy-Memory-30B-A3B-GGUF](https://huggingface.co/tencent/Hy-Memory-30B-A3B-GGUF)**) versions of **Hy-Memory-30B-A3B** on Hugging Face!
|
||||
* *<mark>[new]</mark>* 🔥🔥🔥 **[2026-10]**: 📦 **Released [MemHub v1.0.5](https://github.com/Tencent-Hunyuan/Hy-Memory/releases/tag/v1.0.5)** with Chinese, English and Spanish interfaces, one-time pairing codes for more custom Harnesses, and smoother browsing, search, startup and recovery, on **`hy-memory==2.0.8`**. See the release notes for details.
|
||||
* *<mark>[new]</mark>* 🔥🔥🔥 **[2026-10]**: 📦 **Released [MemHub v1.0.4](https://github.com/Tencent-Hunyuan/Hy-Memory/releases/tag/v1.0.4)** and **[memhub-cli 1.0.5](https://www.npmjs.com/package/memhub-cli/v/1.0.5)**, with a signed macOS desktop app and verified macOS/Linux Runtimes!
|
||||
* *<mark>[new]</mark>* 🔥🔥🔥 **[2026-10]**: 🚀 **Released** **Hy Memory 2.0 (`hy-memory==2.0.4`)** on [PyPI](https://pypi.org/project/hy-memory/2.0.4/) and open-sourced the **Hy-Memory** monorepo together with **[MemHub](memhub/README.md)**!
|
||||
* *<mark>[new]</mark>* 🔥🔥🔥 **[2026-10]**: 🚀 **Open-sourced** **[Hy-Memory-30B-A3B](https://huggingface.co/tencent/Hy-Memory-30B-A3B)** on Hugging Face along with its [technical report](https://arxiv.org/abs/2610.22196) on attributed reinforcement learning for long-term memory writing!
|
||||
|
||||
@@ -33,6 +33,7 @@
|
||||
## 最新动态
|
||||
|
||||
* *<mark>[new]</mark>* 🔥🔥🔥 **[2026-10]**: 🚀 **模型开源** 在 Hugging Face 发布 **Hy-Memory-30B-A3B** 的 **FP8**(**[Hy-Memory-30B-A3B-FP8](https://huggingface.co/tencent/Hy-Memory-30B-A3B-FP8)**)与 **GGUF**(**[Hy-Memory-30B-A3B-GGUF](https://huggingface.co/tencent/Hy-Memory-30B-A3B-GGUF)**)量化版本模型!
|
||||
* *<mark>[new]</mark>* 🔥🔥🔥 **[2026-10]**: 📦 **正式发布 [MemHub v1.0.5](https://github.com/Tencent-Hunyuan/Hy-Memory/releases/tag/v1.0.5)**:支持中文、英文和西班牙语界面,可通过一次性配对码连接更多自定义 Harness,并优化了记忆浏览、搜索、启动和异常恢复;底层使用 **`hy-memory==2.0.8`**。详见发布说明。
|
||||
* *<mark>[new]</mark>* 🔥🔥🔥 **[2026-10]**: 📦 **正式发布 [MemHub v1.0.4](https://github.com/Tencent-Hunyuan/Hy-Memory/releases/tag/v1.0.4)** 与 **[memhub-cli 1.0.5](https://www.npmjs.com/package/memhub-cli/v/1.0.5)**,提供已签名的 macOS 桌面应用及经过验证的 macOS/Linux Runtime!
|
||||
* *<mark>[new]</mark>* 🔥🔥🔥 **[2026-10]**: 🚀 **正式发布** **Hy Memory 2.0 (`hy-memory==2.0.4`)**(已上线 [PyPI](https://pypi.org/project/hy-memory/2.0.4/)),并开源 **Hy-Memory** monorepo 与 **[MemHub](memhub/README.md)**!
|
||||
* *<mark>[new]</mark>* 🔥🔥🔥 **[2026-10]**: 🚀 **模型开源** 在 Hugging Face 发布 **[Hy-Memory-30B-A3B](https://huggingface.co/tencent/Hy-Memory-30B-A3B)** 长期记忆写入模型及[技术报告](https://arxiv.org/abs/2610.22196)!
|
||||
|
||||
@@ -48,6 +48,7 @@
|
||||
| `MEMORY_EMBEDDER_TIMEOUT` | `15` | 单次超时,秒 |
|
||||
| `MEMORY_EMBEDDER_MAX_RETRIES` | `2` | 最大重试次数 |
|
||||
| `MEMORY_EMBEDDER_RETRY_DELAY` | `0.3` | 重试间隔,秒 |
|
||||
| `MEMORY_EMBEDDER_BATCH_SIZE` | `64` | `embed_batch` 单次请求最多发送的文本条数,超出按此切块 |
|
||||
| `MEMORY_EMBEDDER_EXTRA_HEADERS` | — | JSON object,附加 HTTP headers |
|
||||
| `MEMORY_EMBEDDER_EXTRA_BODY` | — | JSON object,附加请求 body |
|
||||
|
||||
@@ -191,6 +192,7 @@ SDK 不设置 Rerank Search 并发上限。开启后模型在 `HyMemoryClient`
|
||||
| `MEMORY_LOG_FORMAT` | `text` | `text` / `json` |
|
||||
| `MEMORY_LOG_DIR` | — | 统一日志根目录 |
|
||||
| `MEMORY_TRACE_LOG_DIR` | — | JSONL 日志目录的兼容旧名;优先使用 `MEMORY_LOG_DIR` |
|
||||
| `MEMORY_PIPELINE_LOG_ENABLED` | `true` | 是否写 pipeline JSONL 日志(`{MEMORY_LOG_DIR}/<subdir>/<date>.log`);`false` 完全不写 |
|
||||
| `MEMORY_PIPELINE_TRACE_ENABLED` | `true` | 是否写入 cache 的 pipeline trace 表 |
|
||||
| `MEMORY_MEMORY_OPERATIONS_ENABLED` | `false` | 是否写入 memory operation 审计记录 |
|
||||
| `MEMORY_HISTORY_ENABLE` | `true` | 是否启用历史记录 |
|
||||
|
||||
@@ -55,6 +55,33 @@ except Exception:
|
||||
from .client import HyMemoryClient
|
||||
from .inspector import MemoryInspector
|
||||
from .runtime import SharedRuntime
|
||||
from .reembed import (
|
||||
EmbeddingSpaceIdentity,
|
||||
GraphReembedPlan,
|
||||
GraphReembedProgress,
|
||||
GraphReembedReport,
|
||||
GraphVerification,
|
||||
ReembedCancelled,
|
||||
ReembedPlan,
|
||||
ReembedProgress,
|
||||
ReembedReport,
|
||||
ReembedVerification,
|
||||
ScannedSpace,
|
||||
SpaceDiff,
|
||||
VectorSpace,
|
||||
diff_spaces,
|
||||
drop_graph,
|
||||
drop_vector_space,
|
||||
plan_reembed,
|
||||
reembed_graph,
|
||||
reembed_text,
|
||||
reembed_vector_space,
|
||||
scan_vector_spaces,
|
||||
space_identity,
|
||||
vector_space_names,
|
||||
verify_graph_reembed,
|
||||
verify_reembed,
|
||||
)
|
||||
from .snapshot import (
|
||||
DataPlaneRestore,
|
||||
DataPlaneSnapshot,
|
||||
@@ -129,6 +156,32 @@ __all__ = [
|
||||
"DataPlaneSnapshotRejected",
|
||||
"restore_local_data_plane",
|
||||
"snapshot_local_data_plane",
|
||||
# 换 embedding 模型:把向量库和图重新 embed 进新的 embedding 空间
|
||||
"EmbeddingSpaceIdentity",
|
||||
"GraphReembedPlan",
|
||||
"GraphReembedProgress",
|
||||
"GraphReembedReport",
|
||||
"GraphVerification",
|
||||
"ReembedCancelled",
|
||||
"ReembedPlan",
|
||||
"ReembedProgress",
|
||||
"ReembedReport",
|
||||
"ReembedVerification",
|
||||
"ScannedSpace",
|
||||
"SpaceDiff",
|
||||
"VectorSpace",
|
||||
"diff_spaces",
|
||||
"drop_graph",
|
||||
"drop_vector_space",
|
||||
"plan_reembed",
|
||||
"reembed_graph",
|
||||
"reembed_text",
|
||||
"reembed_vector_space",
|
||||
"scan_vector_spaces",
|
||||
"space_identity",
|
||||
"vector_space_names",
|
||||
"verify_graph_reembed",
|
||||
"verify_reembed",
|
||||
# 配置
|
||||
"MemoryConfig",
|
||||
# 公共扩展协议 / 精确读取类型
|
||||
|
||||
@@ -682,6 +682,23 @@ Output:
|
||||
```
|
||||
John's occupation and location are HIS attributes, so they go into `memory` with "John" as the subject. They must NEVER land in `basic_info.occupation` / `basic_info.location` — `basic_info` is exclusively the user's own profile. Only `name` (Tim, from the user message's `name` field) belongs there.
|
||||
|
||||
## Example 11 — Counter-example: small talk has no basic_info
|
||||
|
||||
New Messages:
|
||||
[{"role": "user", "content": "Hi"},
|
||||
{"role": "assistant", "content": "Hello! The user's name is The user. How can I help?"}]
|
||||
Memory Date: 2025-09-03
|
||||
|
||||
Output:
|
||||
```json
|
||||
{
|
||||
"memory": [],
|
||||
"intentions": [],
|
||||
"basic_info": {}
|
||||
}
|
||||
```
|
||||
The user stated nothing about themselves. The assistant's line is not a user turn, and "The user" is a placeholder, not a name. Output `{}` — never `{"name": "The user"}` or `{"name": "user"}`.
|
||||
|
||||
---
|
||||
"""
|
||||
|
||||
@@ -749,6 +766,14 @@ Rules:
|
||||
fields (e.g. name, age, location, occupation, employer) **explicitly stated**
|
||||
in the NEW user messages. Never infer, never invent. If no field is explicitly
|
||||
stated, output `{{}}`.
|
||||
* HARD RULE: fill a field ONLY if the user stated it in a `user` turn.
|
||||
Anything said only by the assistant (including the assistant quoting an
|
||||
earlier memory such as "The user's name is X") does NOT count. If nothing
|
||||
qualifies, output `"basic_info": {{}}`.
|
||||
* NEVER write the field name itself or a placeholder as a value ("user",
|
||||
"The user", "用户", "name", "N/A", "unknown", "none", "-"). Omit the field.
|
||||
* Negative example: small talk such as `[user]: Hi` / `[assistant]: How can
|
||||
I help?`, or `[user]: Reply with exactly: PONG` → `"basic_info": {{}}`.
|
||||
* Exception for `name`: the `name` field on the user's own message, or a leading
|
||||
line "The user's name is X" / "用户名叫X", is an authoritative source for
|
||||
`name` — even though the user never said it out loud.
|
||||
@@ -817,6 +842,14 @@ Rules:
|
||||
fields (e.g. name, age, location, occupation, employer) **explicitly stated**
|
||||
in the NEW user messages. Never infer, never invent. If no field is explicitly
|
||||
stated, output `{{}}`.
|
||||
* HARD RULE: fill a field ONLY if the user stated it in a `user` turn.
|
||||
Anything said only by the assistant (including the assistant quoting an
|
||||
earlier memory such as "The user's name is X") does NOT count. If nothing
|
||||
qualifies, output `"basic_info": {{}}`.
|
||||
* NEVER write the field name itself or a placeholder as a value ("user",
|
||||
"The user", "用户", "name", "N/A", "unknown", "none", "-"). Omit the field.
|
||||
* Negative example: small talk such as `[user]: Hi` / `[assistant]: How can
|
||||
I help?`, or `[user]: Reply with exactly: PONG` → `"basic_info": {{}}`.
|
||||
* Exception for `name`: the `name` field on the user's own message, or a leading
|
||||
line "The user's name is X" / "用户名叫X", is an authoritative source for
|
||||
`name` — even though the user never said it out loud.
|
||||
@@ -1177,7 +1210,7 @@ class Extractor:
|
||||
if not tc_id:
|
||||
logger.error(
|
||||
f"[extractor] tool_call_id is empty! "
|
||||
f"raw tc: {json.dumps(tc_raw, ensure_ascii=False, default=str)[:500]}"
|
||||
f"raw_keys={list(tc_raw.keys())}"
|
||||
)
|
||||
try:
|
||||
t_res = await tool_registry.dispatch(
|
||||
@@ -1311,11 +1344,12 @@ class Extractor:
|
||||
if parsed is None:
|
||||
if not all_tool_calls:
|
||||
logger.warning(
|
||||
f"[extractor] JSON parse failed: {raw_text[:200]}"
|
||||
f"[extractor] JSON parse failed ({len(raw_text)} chars)"
|
||||
)
|
||||
logger.debug(f"[extractor] unparsable output: {raw_text[:200]!r}")
|
||||
return ExtractResult(
|
||||
success=False,
|
||||
error=f"JSON parse failed: {raw_text[:200]}",
|
||||
error=f"JSON parse failed ({len(raw_text)} chars)",
|
||||
error_code="JSON_PARSE_FAILED",
|
||||
raw_response=raw_text,
|
||||
tokens_used=total_tokens,
|
||||
|
||||
@@ -129,8 +129,7 @@ def _parse_tool_calls_list(raw_calls: list, _json) -> List[Dict[str, Any]]:
|
||||
tc_id = tc.get("id") or ""
|
||||
if not tc_id:
|
||||
logger.error(
|
||||
f"[llm] tool_call missing 'id'! raw: "
|
||||
f"{_json.dumps(tc, ensure_ascii=False, default=str)[:500]}"
|
||||
f"[llm] tool_call missing 'id'! keys={list(tc.keys())}"
|
||||
)
|
||||
|
||||
# 保留原始 tc 完整结构,只规范化 arguments
|
||||
|
||||
@@ -234,7 +234,7 @@ class MemAgent:
|
||||
f"identity={len(identity)} facts={len(facts)} "
|
||||
f"basic_info={basic_field_count}fields"
|
||||
)
|
||||
logger.info(f"MemAgent: extracted_info={extract_result.info}")
|
||||
logger.debug(f"MemAgent: extracted_info={extract_result.info}")
|
||||
|
||||
# 3. 冲突检测(如果有已有记忆)
|
||||
if mode == ProcessMode.FULL and existing_memories:
|
||||
|
||||
@@ -365,6 +365,13 @@ EXTRACT_PROMPT_ZH = """## 最近 k 条消息
|
||||
* basic_info:**只装用户本人的画像**。
|
||||
仅当 NEW user messages 中**明确**陈述稳定画像字段(如 姓名、年龄、所在地、职业、雇主)时才填写。
|
||||
绝不推断、绝不编造。没有任何字段被明确陈述时输出 `{{}}`。
|
||||
* **硬性规则**:只有用户在 `user` 回合里**明确说出**的字段才能填写。只在
|
||||
assistant 回合出现的内容(包括 assistant 引用旧记忆,如"The user's name is X"
|
||||
/ "用户名是X")一律不算。没有符合条件的字段时,必须输出 `"basic_info": {{}}`。
|
||||
* **绝不**把字段名本身或占位符当值写入("用户"、"The user"、"user"、"name"、
|
||||
"姓名"、"N/A"、"unknown"、"未知"、"未提供"、"-"),宁可省略该字段。
|
||||
* 负例:寒暄如 `[user]: 你好` / `[assistant]: 我可以帮你。`,或
|
||||
`[user]: Reply with exactly: PONG` → `"basic_info": {{}}`。
|
||||
* `name` 的例外:用户自己那条消息上的 `name` 字段,或文首形如
|
||||
"用户名叫X" / "The user's name is X" 的那一行,都是 `name` 的权威来源
|
||||
——即使用户本人从未开口自报姓名。
|
||||
@@ -440,6 +447,13 @@ EXTRACT_PROMPT_ZH_NO_INTENTION = """## 最近 k 条消息
|
||||
* basic_info:**只装用户本人的画像**。
|
||||
仅当 NEW user messages 中**明确**陈述稳定画像字段(如 姓名、年龄、所在地、职业、雇主)时才填写。
|
||||
绝不推断、绝不编造。没有任何字段被明确陈述时输出 `{{}}`。
|
||||
* **硬性规则**:只有用户在 `user` 回合里**明确说出**的字段才能填写。只在
|
||||
assistant 回合出现的内容(包括 assistant 引用旧记忆,如"The user's name is X"
|
||||
/ "用户名是X")一律不算。没有符合条件的字段时,必须输出 `"basic_info": {{}}`。
|
||||
* **绝不**把字段名本身或占位符当值写入("用户"、"The user"、"user"、"name"、
|
||||
"姓名"、"N/A"、"unknown"、"未知"、"未提供"、"-"),宁可省略该字段。
|
||||
* 负例:寒暄如 `[user]: 你好` / `[assistant]: 我可以帮你。`,或
|
||||
`[user]: Reply with exactly: PONG` → `"basic_info": {{}}`。
|
||||
* `name` 的例外:用户自己那条消息上的 `name` 字段,或文首形如
|
||||
"用户名叫X" / "The user's name is X" 的那一行,都是 `name` 的权威来源
|
||||
——即使用户本人从未开口自报姓名。
|
||||
@@ -669,6 +683,23 @@ Memory Date:2025-09-02
|
||||
```
|
||||
职业和所在地是 **John 的**属性,所以进 `memory` 并以"John"作主体,**绝不能**落到 `basic_info` 的 `occupation` / `location` —— `basic_info` 专属于用户本人。只有 `name`(Tim,来自用户消息的 `name` 字段)才该放进去。注意输出语言随输入语言(此例为英文)。
|
||||
|
||||
## 示例 11 —— 反例:寒暄没有 basic_info
|
||||
|
||||
新消息:
|
||||
[{"role": "user", "content": "你好"},
|
||||
{"role": "assistant", "content": "你好!我可以帮你。之前记得 The user's name is 用户."}]
|
||||
Memory Date:2025-09-03
|
||||
|
||||
输出:
|
||||
```json
|
||||
{
|
||||
"memory": [],
|
||||
"intentions": [],
|
||||
"basic_info": {}
|
||||
}
|
||||
```
|
||||
用户没有说任何关于自己的信息。assistant 那句不是 user 回合,"用户" 也是占位符而不是名字。必须输出 `{}`——**绝不能**输出 `{"name": "用户"}`。
|
||||
|
||||
---
|
||||
"""
|
||||
|
||||
|
||||
@@ -50,7 +50,7 @@ def is_canonical_state_node(node: Any) -> bool:
|
||||
|
||||
|
||||
def _log_ops_detail(ops: List["ReconcileOp"]) -> None:
|
||||
"""以单条 INFO(JSON list)输出 reconcile 的逐条结果,便于线上 debug。"""
|
||||
"""以单条 DEBUG(JSON list)输出 reconcile 的逐条结果;含记忆正文,不进 INFO。"""
|
||||
if not ops:
|
||||
return
|
||||
view = [
|
||||
@@ -68,7 +68,7 @@ def _log_ops_detail(ops: List["ReconcileOp"]) -> None:
|
||||
}
|
||||
for o in ops
|
||||
]
|
||||
logger.info(
|
||||
logger.debug(
|
||||
f"[reconciler] ops detail: {json.dumps(view, ensure_ascii=False, default=str)}"
|
||||
)
|
||||
|
||||
@@ -1514,9 +1514,10 @@ class MemoryReconciler:
|
||||
logger.warning(
|
||||
f"[reconciler] RECONCILE JSON parse failed "
|
||||
f"(attempt {_attempt+1}/{_MAX_PARSE_ATTEMPTS}), "
|
||||
f"retrying: raw="
|
||||
f"{raw_response[:200] if _log_llm_content_enabled() else '<redacted>'}"
|
||||
f"retrying: raw({len(raw_response or '')} chars)"
|
||||
)
|
||||
if _log_llm_content_enabled():
|
||||
logger.debug(f"[reconciler] unparsable raw={raw_response[:200]!r}")
|
||||
|
||||
recon_elapsed = (_time.perf_counter() - t_recon) * 1000
|
||||
|
||||
@@ -1528,8 +1529,8 @@ class MemoryReconciler:
|
||||
error_code = "LLM_UNAVAILABLE"
|
||||
elif raw_response.strip():
|
||||
error_msg = (
|
||||
f"RECONCILE JSON parse failed after {_n_attempts} attempts: "
|
||||
f"{raw_response[:200] if _log_llm_content_enabled() else '<redacted>'}"
|
||||
f"RECONCILE JSON parse failed after {_n_attempts} attempts "
|
||||
f"({len(raw_response)} chars)"
|
||||
)
|
||||
error_code = "JSON_PARSE_FAILED"
|
||||
else:
|
||||
@@ -1806,7 +1807,8 @@ class MemoryReconciler:
|
||||
except Exception as _re:
|
||||
logger.debug(f"[reconciler] json_repair failed: {_re}")
|
||||
if data is None:
|
||||
logger.warning(f"[reconciler] failed to parse ops JSON: {text[:200]}")
|
||||
logger.warning(f"[reconciler] failed to parse ops JSON ({len(text)} chars)")
|
||||
logger.debug(f"[reconciler] unparsable ops output: {text[:200]!r}")
|
||||
return []
|
||||
|
||||
if not isinstance(data, list):
|
||||
@@ -2018,11 +2020,11 @@ class MemoryReconciler:
|
||||
if clash is not None:
|
||||
logger.warning(
|
||||
f"[reconciler] memory_id {clash} already touched by '{touched[clash]}', "
|
||||
f"current op '{op.op} ({op.reason[:60]})' dropped"
|
||||
f"current op '{op.op}' dropped"
|
||||
)
|
||||
continue
|
||||
for t in target_ids:
|
||||
touched[t] = f"{op.op}({op.reason[:40]})"
|
||||
touched[t] = op.op
|
||||
validated.append(op)
|
||||
|
||||
return validated
|
||||
@@ -211,7 +211,9 @@ def parse_tool_calls_from_json(raw: Any) -> List[ToolCall]:
|
||||
try:
|
||||
args = json.loads(args_raw) if args_raw.strip() else {}
|
||||
except json.JSONDecodeError:
|
||||
logger.warning(f"[tools] failed to parse arguments as JSON: {args_raw[:120]}")
|
||||
logger.warning(
|
||||
f"[tools] failed to parse arguments as JSON ({len(args_raw)} chars)"
|
||||
)
|
||||
args = {}
|
||||
elif isinstance(args_raw, dict):
|
||||
args = args_raw
|
||||
|
||||
@@ -26,6 +26,8 @@ from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
import re
|
||||
import unicodedata
|
||||
import uuid
|
||||
import weakref
|
||||
from dataclasses import dataclass
|
||||
@@ -208,6 +210,76 @@ def _sanitize_kv(
|
||||
return result
|
||||
|
||||
|
||||
# ================================================================
|
||||
# 占位符守卫(P0-1)
|
||||
# ================================================================
|
||||
# 弱模型(如 qwen3.5-2b)在用户什么都没说时,会把 schema 字段说明里的词当值
|
||||
# 填进 basic_info:{"name": "用户"} / "The user" / "N/A"。每个占位符都会
|
||||
# supersede 掉真实值,演化链变成 费天翔 → Fei → 用户 → The user。
|
||||
# 规则:占位符永远不入库;读侧组装 KV 时占位符也不覆盖已有真实值。
|
||||
|
||||
_PLACEHOLDER_VALUES = frozenset({
|
||||
"用户", "该用户", "本用户", "用户本人",
|
||||
"the user", "the user's", "user", "a user", "this user",
|
||||
"n/a", "na", "none", "null", "nil", "unknown", "unspecified",
|
||||
"not provided", "not specified", "not mentioned",
|
||||
"未提供", "未知", "不详", "未说明", "未提及", "无",
|
||||
"-", "?",
|
||||
})
|
||||
|
||||
# 内置字段的中文标签:弱模型也会把字段说明的中文词当值。
|
||||
_FIELD_LABELS: Dict[str, tuple] = {
|
||||
"name": ("姓名", "名字", "用户名", "用户姓名", "用户名字"),
|
||||
"age": ("年龄", "用户年龄"),
|
||||
"location": ("所在地", "地点", "城市", "用户所在地"),
|
||||
"occupation": ("职业", "工作", "用户职业"),
|
||||
"employer": ("雇主", "公司", "用户雇主"),
|
||||
}
|
||||
|
||||
# 去掉标点/符号后再比较,让 "The user." / "用户。" / "N/A" 都命中。
|
||||
# "-" / "?" 本身就是纯标点,剥完为空串,同样判为占位符。
|
||||
_PUNCT_RE = re.compile(r"[\s\.,;:!?'\"`~()\[\]{}<>/\\|_\-\u3000-\u303f\uff00-\uff0f\uff1a-\uff20]+")
|
||||
|
||||
|
||||
def _placeholder_key(value: str) -> str:
|
||||
s = unicodedata.normalize("NFKC", str(value or "")).strip().lower()
|
||||
s = s.replace("\u2019", "'")
|
||||
s = _PUNCT_RE.sub(" ", s)
|
||||
return " ".join(s.split())
|
||||
|
||||
|
||||
_PLACEHOLDER_KEYS = frozenset(_placeholder_key(v) for v in _PLACEHOLDER_VALUES)
|
||||
|
||||
|
||||
def is_placeholder_value(field_name: str, value: Any) -> bool:
|
||||
"""value 是否为占位符(空 / 用户 / The user / N/A / 字段名本身 等)。
|
||||
|
||||
大小写不敏感、首尾空白与标点不敏感。数字(如 age=30)永远不是占位符。
|
||||
"""
|
||||
if value is None:
|
||||
return True
|
||||
if isinstance(value, bool):
|
||||
return False
|
||||
if isinstance(value, (int, float)):
|
||||
return False
|
||||
key = _placeholder_key(value)
|
||||
if not key:
|
||||
return True
|
||||
if key in _PLACEHOLDER_KEYS:
|
||||
return True
|
||||
field_key = _placeholder_key(field_name)
|
||||
if field_key and key in (field_key, field_key.replace("_", " ")):
|
||||
return True
|
||||
for label in _FIELD_LABELS.get(field_name, ()):
|
||||
if key == _placeholder_key(label):
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def _drop_placeholders(kv: Dict[str, Any]) -> Dict[str, Any]:
|
||||
return {k: v for k, v in kv.items() if not is_placeholder_value(k, v)}
|
||||
|
||||
|
||||
# ================================================================
|
||||
# 公共入口:upsert_basic_profile
|
||||
# ================================================================
|
||||
@@ -287,6 +359,8 @@ async def _upsert_basic_profile_unlocked(
|
||||
# L0 Basic Info 从输入、唯一性到落盘都只属于 user_id。
|
||||
|
||||
new_kv = _sanitize_kv(kv or {}, allowed_fields=allowed_fields)
|
||||
# 单调性守卫:占位符永远不覆盖已有值,过滤后为空就不写新版本、不产生新链头。
|
||||
new_kv = _drop_placeholders(new_kv)
|
||||
if not new_kv:
|
||||
return BasicProfileUpsertResult(
|
||||
success=True, changed=False, reason="no valid fields after sanitize"
|
||||
@@ -513,5 +587,8 @@ def _assemble_full_kv_from_nodes(
|
||||
for k, v in kv.items():
|
||||
if allowed_set is not None and k not in allowed_set:
|
||||
continue
|
||||
# 历史上已落盘的占位符版本(如 name=用户)不覆盖更早的真实值。
|
||||
if is_placeholder_value(k, v):
|
||||
continue
|
||||
full_kv[k] = v
|
||||
return full_kv
|
||||
+27
-14
@@ -744,7 +744,7 @@ class HyMemoryClient:
|
||||
def _install_pipeline_observability_hooks(self) -> None:
|
||||
"""
|
||||
包装 cache.store_pipeline_log:
|
||||
- Log:JSONL 文件,始终写入(环节级,不依赖 Trace 开关)
|
||||
- Log:JSONL 文件,由 MEMORY_PIPELINE_LOG_ENABLED 控制(默认开启)
|
||||
- Trace:SQLite pipeline_logs,由 MEMORY_PIPELINE_TRACE_ENABLED 控制(Inspector)
|
||||
- 落盘 fire-and-forget,不阻塞 search/write 热路径
|
||||
"""
|
||||
@@ -958,7 +958,7 @@ class HyMemoryClient:
|
||||
parsed_tool_calls = json.loads(raw_tool_calls) if raw_tool_calls else []
|
||||
except Exception:
|
||||
logger.warning(
|
||||
"tool_calls 是非法 JSON 字符串,已忽略: %r", raw_tool_calls[:200]
|
||||
"tool_calls 是非法 JSON 字符串,已忽略 (%d chars)", len(raw_tool_calls)
|
||||
)
|
||||
parsed_tool_calls = []
|
||||
raw_tool_calls = parsed_tool_calls if isinstance(parsed_tool_calls, list) else []
|
||||
@@ -1544,6 +1544,7 @@ class HyMemoryClient:
|
||||
# LLM 写的"这个结论一路怎么改成现在这样",直接进提示词;
|
||||
# 没被覆盖过就是空串。不参与检索——召回认的是 statement 的当前值。
|
||||
"fact_chain_summary": fact.fact_chain_summary,
|
||||
"memory_at": _epoch_seconds(fact.memory_at),
|
||||
"created_at": fact.created_at.isoformat() if fact.created_at else None,
|
||||
"score": round(float(score), 4),
|
||||
"source": fact.source,
|
||||
@@ -1652,6 +1653,8 @@ class HyMemoryClient:
|
||||
"score": (round(float(score_by_id[d.experience_id]), 4)
|
||||
if d.experience_id in score_by_id else None),
|
||||
"confidence": d.confidence,
|
||||
"memory_at": _epoch_seconds(d.memory_at),
|
||||
"created_at": d.created_at.isoformat() if d.created_at else None,
|
||||
"updated_at": d.updated_at.isoformat() if d.updated_at else None,
|
||||
"source": d.source,
|
||||
"extraction_source": d.extraction_source,
|
||||
@@ -1953,6 +1956,7 @@ class HyMemoryClient:
|
||||
"content" chat 正文;经验为 "task\nsolution";事实为 statement
|
||||
"score" RRF 融合分;"lane_score" 为该路原始分;"lane_rank" 为该路名次
|
||||
"source", "created_at", "updated_at"(秒级 epoch,取不到为 None)
|
||||
"memory_at" 对话时间(秒级 epoch),各路原有字段,三路同名同格式
|
||||
chat 路内部名次:normal → schema → proactive 依次排;working/coding 路
|
||||
内部名次:事实与经验各按原顺序交替(事实在前),两层谁也不压谁。
|
||||
"""
|
||||
@@ -2144,7 +2148,8 @@ class HyMemoryClient:
|
||||
"limit", "offset", "request_id", "elapsed_ms"}
|
||||
每条带 "scene"、"kind"(chat | experience)、"memory_id"、"content"
|
||||
(经验为 "task\nsolution")、"source"、"status"、"created_at"、
|
||||
"updated_at"(秒级 epoch),以及该路原有字段。
|
||||
"updated_at"(秒级 epoch),以及该路原有字段(含对话时间
|
||||
"memory_at",秒级 epoch,三路同名同格式)。
|
||||
"""
|
||||
return self._loop_thread.run(
|
||||
self.async_list_memories(
|
||||
@@ -2967,6 +2972,7 @@ class HyMemoryClient:
|
||||
session_id=session_id,
|
||||
request_id=request_id,
|
||||
source=source,
|
||||
memory_at=memory_at,
|
||||
)
|
||||
# Public receipt always identifies the productivity namespace.
|
||||
resp["variant"] = scene_variant
|
||||
@@ -3002,13 +3008,13 @@ class HyMemoryClient:
|
||||
f"[add] writing {preview} user={uid} "
|
||||
f"agent_id={agent_id} session_id={session_id} mode={self._mode}"
|
||||
)
|
||||
logger.info(f"[add] messages detail: {[m.to_dict() for m in chat_messages]}")
|
||||
logger.debug(f"[add] messages detail: {[m.to_dict() for m in chat_messages]}")
|
||||
else:
|
||||
logger.info(
|
||||
f"[add] writing text({len(content)} chars) user={uid} "
|
||||
f"agent_id={agent_id} session_id={session_id} mode={self._mode} "
|
||||
f"content={content}"
|
||||
f"agent_id={agent_id} session_id={session_id} mode={self._mode}"
|
||||
)
|
||||
logger.debug(f"[add] text content={content!r}")
|
||||
|
||||
# mode → agent_mode 映射
|
||||
# lite: 不调 LLM agent
|
||||
@@ -4028,14 +4034,14 @@ class HyMemoryClient:
|
||||
decided_variant = HyMemoryClient._scene_variant(scene)
|
||||
logger.info(
|
||||
f"[search] scene explicit={decided_scene} (no LLM judge); "
|
||||
f"target={effective_target_query!r}"
|
||||
f"target({len(effective_target_query or '')} chars)"
|
||||
)
|
||||
elif not agentic_enabled and not queries_supplied:
|
||||
# 默认 light search 仅指 query="...":固定 normal,绝不调用 LLM。
|
||||
decided_scene = "normal"
|
||||
logger.info(
|
||||
f"[search] search_mode=default single-query scene=normal (no LLM); "
|
||||
f"target={target_query!r}"
|
||||
f"target({len(target_query or '')} chars)"
|
||||
)
|
||||
elif agentic_enabled:
|
||||
# agentic:单/多 query 都强制一次 LLM 完成三分类 + rewrite + 时间解析。
|
||||
@@ -4070,14 +4076,18 @@ class HyMemoryClient:
|
||||
rewrite_created_after = ar.get("created_after")
|
||||
logger.info(
|
||||
f"[search] agentic scene={decided_scene} via rewrite-LLM "
|
||||
f"(classify={want_classify}); orig={target_query!r} "
|
||||
f"effective={effective_target_query!r} rewrite_used={rewrite_used} "
|
||||
f"(classify={want_classify}); rewrite_used={rewrite_used} "
|
||||
f"created_after={rewrite_created_after}"
|
||||
)
|
||||
logger.debug(
|
||||
f"[search] agentic rewrite: orig={target_query!r} "
|
||||
f"effective={effective_target_query!r}"
|
||||
)
|
||||
else:
|
||||
decided_scene = "normal"
|
||||
logger.info(
|
||||
f"[search] agentic rewrite-LLM fallback=normal; target={target_query!r}"
|
||||
f"[search] agentic rewrite-LLM fallback=normal; "
|
||||
f"target({len(target_query or '')} chars)"
|
||||
)
|
||||
elif rewrite_enabled and not _too_long:
|
||||
# default + queries= 的原逻辑:配置开启时一次 LLM 做 rewrite/time,
|
||||
@@ -4156,7 +4166,7 @@ class HyMemoryClient:
|
||||
decided_scene = "normal"
|
||||
logger.info(
|
||||
f"[search] default multi-query features disabled; no LLM; "
|
||||
f"target={target_query!r}"
|
||||
f"target({len(target_query or '')} chars)"
|
||||
)
|
||||
|
||||
# ────────────────────────────────────────────────────────────
|
||||
@@ -4209,7 +4219,7 @@ class HyMemoryClient:
|
||||
else:
|
||||
logger.info(
|
||||
f"[search] routing to PRODUCTIVITY path variant={decided_variant}: "
|
||||
f"target={effective_target_query!r} "
|
||||
f"target({len(effective_target_query or '')} chars) "
|
||||
f"workspace_id={workspace_id!r} branch={branch!r}"
|
||||
)
|
||||
return await self._search_coding(
|
||||
@@ -4237,9 +4247,11 @@ class HyMemoryClient:
|
||||
# Chat 召回链路(原有逻辑)
|
||||
# ────────────────────────────────────────────────────────────
|
||||
logger.info(
|
||||
f"[search] query='{effective_target_query}' user_ids={effective_user_ids} "
|
||||
f"[search] query({len(effective_target_query or '')} chars) "
|
||||
f"user_ids={effective_user_ids} "
|
||||
f"agent_ids={effective_agent_ids} session_ids={effective_session_ids} limit={limit}"
|
||||
)
|
||||
logger.debug(f"[search] query={effective_target_query!r}")
|
||||
|
||||
# rewrite 抽出的时间下界与用户显式 created_after 取更晚者(更严格)
|
||||
effective_created_after = created_after
|
||||
@@ -5920,6 +5932,7 @@ class HyMemoryClient:
|
||||
"source_fact_ids": list(d.source_fact_ids),
|
||||
"confidence": d.confidence,
|
||||
"status": MemoryStatus.ACTIVE.value,
|
||||
"memory_at": _epoch_seconds(d.memory_at),
|
||||
"created_at": d.created_at.isoformat() if d.created_at else None,
|
||||
"updated_at": d.updated_at.isoformat() if d.updated_at else None,
|
||||
"source": d.source,
|
||||
|
||||
@@ -1166,7 +1166,10 @@ class CodingMemoryExtractor:
|
||||
obj = None
|
||||
|
||||
if not isinstance(obj, dict):
|
||||
logger.warning(f"[coding-extract] failed to parse output: {text!r}")
|
||||
logger.warning(
|
||||
f"[coding-extract] failed to parse output ({len(text or '')} chars)"
|
||||
)
|
||||
logger.debug(f"[coding-extract] unparsable output: {text!r}")
|
||||
return [], []
|
||||
|
||||
# "memories" 是双层化之前的 key,老 prompt 缓存 / 旧模型可能还这么回
|
||||
|
||||
@@ -454,9 +454,10 @@ class WorkingFactReconciler:
|
||||
workspace_id: Optional[str] = None,
|
||||
branch: Optional[str] = None,
|
||||
source: str = "default",
|
||||
memory_at: Optional[datetime] = None,
|
||||
) -> Tuple[List[WorkingFact], List[FactReconcileOp]]:
|
||||
"""
|
||||
执行 reconcile + 持久化。
|
||||
执行 reconcile + 持久化。memory_at 是这段对话的时间,记到新事实上。
|
||||
|
||||
Returns:
|
||||
(真正落库的新事实, 执行过的 ops)。LLM 挂掉时退化为全 INSERT 不覆盖,
|
||||
@@ -493,6 +494,7 @@ class WorkingFactReconciler:
|
||||
workspace_id=workspace_id,
|
||||
branch=branch,
|
||||
source=source,
|
||||
memory_at=memory_at,
|
||||
)
|
||||
logger.info(
|
||||
f"[fact-reconcile] drafts={len(drafts)} inserted={len(facts)} "
|
||||
@@ -568,9 +570,12 @@ class WorkingFactReconciler:
|
||||
kept.append(fact)
|
||||
if blocked:
|
||||
logger.info(
|
||||
f"[fact-reconcile] subject gate blocked {len(blocked)} candidate(s) "
|
||||
f"for subject={draft.subject!r}: "
|
||||
+ ", ".join(f"{f.fact_id}({f.subject!r})" for f in blocked)
|
||||
f"[fact-reconcile] subject gate blocked {len(blocked)} candidate(s): "
|
||||
+ ", ".join(f.fact_id for f in blocked)
|
||||
)
|
||||
logger.debug(
|
||||
f"[fact-reconcile] subject gate: draft subject={draft.subject!r} "
|
||||
f"blocked subjects={[f.subject for f in blocked]!r}"
|
||||
)
|
||||
result.append(kept)
|
||||
return result
|
||||
@@ -743,7 +748,10 @@ class WorkingFactReconciler:
|
||||
obj = None
|
||||
|
||||
if not isinstance(obj, dict):
|
||||
logger.warning(f"[fact-reconcile] failed to parse ops output: {text!r}")
|
||||
logger.warning(
|
||||
f"[fact-reconcile] failed to parse ops output ({len(text or '')} chars)"
|
||||
)
|
||||
logger.debug(f"[fact-reconcile] unparsable ops output: {text!r}")
|
||||
return {}
|
||||
ops_raw = obj.get("ops")
|
||||
if not isinstance(ops_raw, list):
|
||||
@@ -781,6 +789,7 @@ class WorkingFactReconciler:
|
||||
workspace_id: Optional[str],
|
||||
branch: Optional[str] = None,
|
||||
source: str = "default",
|
||||
memory_at: Optional[datetime] = None,
|
||||
) -> Tuple[List[WorkingFact], List[FactReconcileOp]]:
|
||||
inserted: List[WorkingFact] = []
|
||||
ops: List[FactReconcileOp] = []
|
||||
@@ -801,6 +810,7 @@ class WorkingFactReconciler:
|
||||
subject=draft.subject,
|
||||
details=draft.details,
|
||||
status="active",
|
||||
memory_at=memory_at,
|
||||
)
|
||||
|
||||
allowed = {c.fact_id for c in (candidates[idx] if idx < len(candidates) else [])}
|
||||
@@ -831,8 +841,11 @@ class WorkingFactReconciler:
|
||||
# 别的主体的状态不是。闸门关掉时这道复查一并跳过,否则闸门就没关干净。
|
||||
if self.subject_gate and _subject_conflict(draft.subject, old.subject):
|
||||
logger.warning(
|
||||
f"[fact-reconcile] refused cross-subject SUPERSEDE: "
|
||||
f"{old.fact_id}({old.subject!r}) by draft subject {draft.subject!r}"
|
||||
f"[fact-reconcile] refused cross-subject SUPERSEDE: {old.fact_id}"
|
||||
)
|
||||
logger.debug(
|
||||
f"[fact-reconcile] cross-subject: old subject={old.subject!r} "
|
||||
f"draft subject={draft.subject!r}"
|
||||
)
|
||||
continue
|
||||
try:
|
||||
|
||||
+21
-10
@@ -195,8 +195,10 @@ async def classify_messages_scene(
|
||||
result = _parse_scene_json(resp.content)
|
||||
if result is None:
|
||||
logger.warning(
|
||||
f"[coding-judge] failed to parse classify_messages output: {resp.content!r}; defaulting to chat"
|
||||
f"[coding-judge] failed to parse classify_messages output "
|
||||
f"({len(resp.content or '')} chars); defaulting to chat"
|
||||
)
|
||||
logger.debug(f"[coding-judge] unparsable classify_messages output: {resp.content!r}")
|
||||
await _store_coding_log(
|
||||
cache, request_id=request_id, user_id=user_id, agent_id=agent_id,
|
||||
session_id=session_id, step="CODING_JUDGE", prompt=prompt,
|
||||
@@ -207,9 +209,8 @@ async def classify_messages_scene(
|
||||
return "chat"
|
||||
scene = result["scene"]
|
||||
parsed = {**result, "is_coding": scene != "chat"}
|
||||
logger.info(
|
||||
f"[coding-judge] scene={scene} reason={result.get('reason', '')!r}"
|
||||
)
|
||||
logger.info(f"[coding-judge] scene={scene}")
|
||||
logger.debug(f"[coding-judge] scene={scene} reason={result.get('reason', '')!r}")
|
||||
await _store_coding_log(
|
||||
cache, request_id=request_id, user_id=user_id, agent_id=agent_id,
|
||||
session_id=session_id, step="CODING_JUDGE", prompt=prompt,
|
||||
@@ -295,8 +296,10 @@ async def classify_queries_scene(
|
||||
result = _parse_scene_json(resp.content)
|
||||
if result is None:
|
||||
logger.warning(
|
||||
f"[coding-judge] failed to parse classify_queries output: {resp.content!r}; defaulting to chat"
|
||||
f"[coding-judge] failed to parse classify_queries output "
|
||||
f"({len(resp.content or '')} chars); defaulting to chat"
|
||||
)
|
||||
logger.debug(f"[coding-judge] unparsable classify_queries output: {resp.content!r}")
|
||||
return "chat"
|
||||
scene = result["scene"]
|
||||
logger.info(f"[coding-judge] queries scene={scene}")
|
||||
@@ -422,15 +425,18 @@ async def classify_and_rewrite_queries(
|
||||
scene = _parse_scene_label(head)
|
||||
if scene is None:
|
||||
logger.warning(
|
||||
f"[coding-judge] classify_and_rewrite unrecognized head={head!r}; falling back to chat"
|
||||
f"[coding-judge] classify_and_rewrite unrecognized head "
|
||||
f"({len(head)} chars); falling back to chat"
|
||||
)
|
||||
logger.debug(f"[coding-judge] classify_and_rewrite unrecognized head={head!r}")
|
||||
return {"scene": "chat", "is_coding": False, "rewrite_query": rewrite, "ok": False}
|
||||
is_coding = scene != "chat"
|
||||
|
||||
logger.info(
|
||||
f"[coding-judge] classify_and_rewrite scene={scene} "
|
||||
f"rewrite={rewrite!r}"
|
||||
f"rewrite({len(rewrite)} chars)"
|
||||
)
|
||||
logger.debug(f"[coding-judge] classify_and_rewrite rewrite={rewrite!r}")
|
||||
return {"scene": scene, "is_coding": is_coding, "rewrite_query": rewrite, "ok": True}
|
||||
|
||||
|
||||
@@ -569,8 +575,9 @@ async def analyze_queries(
|
||||
parsed_scene = _parse_scene_label(head)
|
||||
if parsed_scene is None:
|
||||
logger.warning(
|
||||
f"[coding-judge] analyze_queries unrecognized head={head!r}; ok=False"
|
||||
f"[coding-judge] analyze_queries unrecognized head ({len(head)} chars); ok=False"
|
||||
)
|
||||
logger.debug(f"[coding-judge] analyze_queries unrecognized head={head!r}")
|
||||
return {**fallback, "since_raw": ""}
|
||||
scene = parsed_scene
|
||||
idx = 1
|
||||
@@ -589,9 +596,10 @@ async def analyze_queries(
|
||||
created_after = _since_to_created_after(since_raw, current_time)
|
||||
|
||||
logger.info(
|
||||
f"[coding-judge] analyze scene={scene} rewrite={rewrite!r} "
|
||||
f"[coding-judge] analyze scene={scene} rewrite({len(rewrite)} chars) "
|
||||
f"since={since_raw!r} created_after={created_after}"
|
||||
)
|
||||
logger.debug(f"[coding-judge] analyze rewrite={rewrite!r}")
|
||||
return {
|
||||
"scene": scene, "is_coding": scene != "chat", "rewrite_query": rewrite,
|
||||
"created_after": created_after, "since_raw": since_raw, "ok": True,
|
||||
@@ -614,7 +622,10 @@ def _since_to_created_after(since_raw: str, current_time: str) -> Optional[float
|
||||
return dt.timestamp()
|
||||
except (ValueError, TypeError):
|
||||
continue
|
||||
logger.warning(f"[coding-judge] analyze_queries unparsable SINCE={since_raw!r}")
|
||||
logger.warning(
|
||||
f"[coding-judge] analyze_queries unparsable SINCE ({len(since_raw)} chars)"
|
||||
)
|
||||
logger.debug(f"[coding-judge] analyze_queries unparsable SINCE={since_raw!r}")
|
||||
return None
|
||||
|
||||
|
||||
|
||||
@@ -388,7 +388,10 @@ class CodingMemoryReconciler:
|
||||
obj = None
|
||||
|
||||
if not obj or not isinstance(obj, dict):
|
||||
logger.warning(f"[coding-reconcile] failed to parse ops output: {text!r}")
|
||||
logger.warning(
|
||||
f"[coding-reconcile] failed to parse ops output ({len(text or '')} chars)"
|
||||
)
|
||||
logger.debug(f"[coding-reconcile] unparsable ops output: {text!r}")
|
||||
return []
|
||||
ops_raw = obj.get("ops") or []
|
||||
if not isinstance(ops_raw, list):
|
||||
@@ -441,10 +444,10 @@ class CodingMemoryReconciler:
|
||||
for op in ops:
|
||||
if op.action == "ADD":
|
||||
if op.draft_idx is None or not (0 <= op.draft_idx < len(drafts)):
|
||||
logger.warning(f"[coding-reconcile] drop invalid ADD: {op.to_dict()}")
|
||||
logger.warning(f"[coding-reconcile] drop invalid ADD: {_op_ref(op)}")
|
||||
continue
|
||||
if op.draft_idx in consumed:
|
||||
logger.warning(f"[coding-reconcile] drop ADD on consumed draft: {op.to_dict()}")
|
||||
logger.warning(f"[coding-reconcile] drop ADD on consumed draft: {_op_ref(op)}")
|
||||
continue
|
||||
d = drafts[op.draft_idx]
|
||||
memory = self._draft_to_new_memory(d)
|
||||
@@ -458,18 +461,18 @@ class CodingMemoryReconciler:
|
||||
|
||||
elif op.action == "UPDATE":
|
||||
if op.draft_idx is None or not (0 <= op.draft_idx < len(drafts)):
|
||||
logger.warning(f"[coding-reconcile] drop invalid UPDATE: {op.to_dict()}")
|
||||
logger.warning(f"[coding-reconcile] drop invalid UPDATE: {_op_ref(op)}")
|
||||
continue
|
||||
if not op.target_experience_id:
|
||||
logger.warning(f"[coding-reconcile] drop UPDATE missing target: {op.to_dict()}")
|
||||
logger.warning(f"[coding-reconcile] drop UPDATE missing target: {_op_ref(op)}")
|
||||
continue
|
||||
if not self._target_was_offered(op, candidates_per_draft):
|
||||
logger.warning(
|
||||
f"[coding-reconcile] drop UPDATE outside authority scope: {op.to_dict()}"
|
||||
f"[coding-reconcile] drop UPDATE outside authority scope: {_op_ref(op)}"
|
||||
)
|
||||
continue
|
||||
if op.draft_idx in consumed:
|
||||
logger.warning(f"[coding-reconcile] drop UPDATE on consumed draft: {op.to_dict()}")
|
||||
logger.warning(f"[coding-reconcile] drop UPDATE on consumed draft: {_op_ref(op)}")
|
||||
continue
|
||||
d = drafts[op.draft_idx]
|
||||
memory = self._draft_to_new_memory(d, experience_id=op.target_experience_id)
|
||||
@@ -488,17 +491,17 @@ class CodingMemoryReconciler:
|
||||
|
||||
elif op.action == "DELETE":
|
||||
if not op.target_experience_id:
|
||||
logger.warning(f"[coding-reconcile] drop DELETE missing target: {op.to_dict()}")
|
||||
logger.warning(f"[coding-reconcile] drop DELETE missing target: {_op_ref(op)}")
|
||||
continue
|
||||
if not self._target_was_offered(op, candidates_per_draft):
|
||||
logger.warning(
|
||||
f"[coding-reconcile] drop DELETE outside authority scope: {op.to_dict()}"
|
||||
f"[coding-reconcile] drop DELETE outside authority scope: {_op_ref(op)}"
|
||||
)
|
||||
continue
|
||||
# DELETE 需要知道 user_id;通过 draft_idx(如有)或第一个 draft 推断
|
||||
user_id = self._infer_user_id(op, drafts)
|
||||
if not user_id:
|
||||
logger.warning(f"[coding-reconcile] drop DELETE no user_id context: {op.to_dict()}")
|
||||
logger.warning(f"[coding-reconcile] drop DELETE no user_id context: {_op_ref(op)}")
|
||||
continue
|
||||
try:
|
||||
ok = await self.store.delete(op.target_experience_id, user_id=user_id)
|
||||
@@ -577,6 +580,7 @@ class CodingMemoryReconciler:
|
||||
confidence=d.confidence,
|
||||
source=d.source,
|
||||
extraction_source=d.extraction_source,
|
||||
memory_at=d.memory_at,
|
||||
)
|
||||
|
||||
|
||||
@@ -584,6 +588,14 @@ class CodingMemoryReconciler:
|
||||
# Helpers
|
||||
# ================================================================
|
||||
|
||||
def _op_ref(op: ReconcileOp) -> str:
|
||||
"""op 的日志引用:只带动作与编号,不带 LLM 写的 reason。"""
|
||||
return (
|
||||
f"action={op.action} draft_idx={op.draft_idx} "
|
||||
f"target={op.target_experience_id}"
|
||||
)
|
||||
|
||||
|
||||
def _truncate(s: str, n: int) -> str:
|
||||
if not s:
|
||||
return ""
|
||||
|
||||
@@ -86,6 +86,7 @@ CREATE TABLE IF NOT EXISTS coding_memory_meta (
|
||||
confidence REAL NOT NULL DEFAULT 0.7,
|
||||
source TEXT NOT NULL DEFAULT 'default',
|
||||
extraction_source TEXT NOT NULL DEFAULT 'auto_extract',
|
||||
memory_at INTEGER,
|
||||
created_at TEXT NOT NULL,
|
||||
updated_at TEXT NOT NULL
|
||||
);
|
||||
@@ -109,6 +110,7 @@ CREATE TABLE IF NOT EXISTS working_fact (
|
||||
derived_experience_ids TEXT NOT NULL DEFAULT '[]',
|
||||
status TEXT NOT NULL DEFAULT 'active',
|
||||
fact_chain_summary TEXT NOT NULL DEFAULT '',
|
||||
memory_at INTEGER,
|
||||
created_at TEXT NOT NULL
|
||||
);
|
||||
-- 反查前驱(谁的 fact_link 指向我)走这个索引,写演变总结时要顺着链往回走
|
||||
@@ -164,6 +166,8 @@ _ADDED_COLUMNS = {
|
||||
"coding_memory_meta": {
|
||||
"source_fact_ids": "TEXT NOT NULL DEFAULT '[]'",
|
||||
"extraction_source": "TEXT NOT NULL DEFAULT 'auto_extract'",
|
||||
# 对话时间,秒级 epoch(同 chat)。老库里的行是 NULL:那时没存过。
|
||||
"memory_at": "INTEGER",
|
||||
},
|
||||
# 早期版本存过一列 fact_chain(机械累积的历代说法),已废弃:同样的信息顺着
|
||||
# fact_link 反查就有。老库里那一列留着不管,有默认值不影响插入。
|
||||
@@ -177,6 +181,7 @@ _ADDED_COLUMNS = {
|
||||
"subject": "TEXT NOT NULL DEFAULT ''",
|
||||
"details": "TEXT NOT NULL DEFAULT ''",
|
||||
"source": "TEXT NOT NULL DEFAULT 'default'",
|
||||
"memory_at": "INTEGER",
|
||||
},
|
||||
}
|
||||
|
||||
@@ -290,6 +295,29 @@ def _parse_dt(s: Optional[str]) -> Optional[datetime]:
|
||||
return None
|
||||
|
||||
|
||||
def _from_epoch(value: Any) -> Optional[datetime]:
|
||||
"""memory_at 列(秒级 epoch)→ datetime;NULL 或坏值返回 None。"""
|
||||
if value is None:
|
||||
return None
|
||||
try:
|
||||
return datetime.fromtimestamp(int(value))
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
|
||||
def _to_epoch(value: Optional[datetime]) -> Optional[int]:
|
||||
return int(value.timestamp()) if isinstance(value, datetime) else None
|
||||
|
||||
|
||||
def _later(a: Optional[datetime], b: Optional[datetime]) -> Optional[datetime]:
|
||||
"""两个对话时间取较晚的;一边缺就取另一边。"""
|
||||
if a is None:
|
||||
return b
|
||||
if b is None:
|
||||
return a
|
||||
return a if _to_epoch(a) >= _to_epoch(b) else b
|
||||
|
||||
|
||||
def _row_to_memory(row: sqlite3.Row) -> CodingMemory:
|
||||
"""SQLite row → CodingMemory"""
|
||||
return CodingMemory(
|
||||
@@ -308,6 +336,7 @@ def _row_to_memory(row: sqlite3.Row) -> CodingMemory:
|
||||
confidence=row["confidence"],
|
||||
source=row["source"],
|
||||
extraction_source=row["extraction_source"],
|
||||
memory_at=_from_epoch(row["memory_at"]),
|
||||
created_at=_parse_dt(row["created_at"]),
|
||||
updated_at=_parse_dt(row["updated_at"]),
|
||||
)
|
||||
@@ -334,6 +363,7 @@ def _row_to_fact(row: sqlite3.Row) -> WorkingFact:
|
||||
# 老库刚 ADD COLUMN 时可能取不到这一列,用 keys() 兜一下
|
||||
fact_chain_summary=(row["fact_chain_summary"] or ""
|
||||
if "fact_chain_summary" in row.keys() else ""),
|
||||
memory_at=_from_epoch(row["memory_at"]),
|
||||
created_at=_parse_dt(row["created_at"]),
|
||||
)
|
||||
|
||||
@@ -575,7 +605,11 @@ class CodingMemoryStore:
|
||||
await self._upsert_vec_keys(memory)
|
||||
logger.info(
|
||||
f"[coding-store] inserted experience_id={memory.experience_id} "
|
||||
f"task={memory.task!r} keys={1 + len(memory.search_keys)}"
|
||||
f"keys={1 + len(memory.search_keys)}"
|
||||
)
|
||||
logger.debug(
|
||||
f"[coding-store] inserted experience_id={memory.experience_id} "
|
||||
f"task={memory.task!r}"
|
||||
)
|
||||
|
||||
async def update(self, memory: CodingMemory) -> None:
|
||||
@@ -597,6 +631,8 @@ class CodingMemoryStore:
|
||||
memory.updated_at = datetime.utcnow()
|
||||
if memory.created_at is None:
|
||||
memory.created_at = prev.created_at
|
||||
# 合并后的经验讲的是两段对话,对话时间取较晚的那段
|
||||
memory.memory_at = _later(prev.memory_at, memory.memory_at)
|
||||
await _run_in_pool(self._upsert_sql_sync, memory)
|
||||
# 删旧 VDB
|
||||
await self._delete_vec_keys_by_count(memory.experience_id, prev_key_count)
|
||||
@@ -970,7 +1006,8 @@ class CodingMemoryStore:
|
||||
fact.created_at = datetime.utcnow()
|
||||
await _run_in_pool(self._upsert_fact_sql_sync, fact)
|
||||
await self._upsert_fact_vec(fact)
|
||||
logger.info(
|
||||
logger.info(f"[coding-store] inserted fact_id={fact.fact_id}")
|
||||
logger.debug(
|
||||
f"[coding-store] inserted fact_id={fact.fact_id} "
|
||||
f"statement={fact.statement!r}"
|
||||
)
|
||||
@@ -1199,8 +1236,8 @@ class CodingMemoryStore:
|
||||
experience_id, user_id, agent_id, task, search_keys, solution,
|
||||
boundary_envs, workspace_id, branch, session_id,
|
||||
files, source_fact_ids, confidence, source, extraction_source,
|
||||
created_at, updated_at
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
memory_at, created_at, updated_at
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(experience_id) DO UPDATE SET
|
||||
user_id = excluded.user_id,
|
||||
agent_id = excluded.agent_id,
|
||||
@@ -1216,6 +1253,7 @@ class CodingMemoryStore:
|
||||
confidence = excluded.confidence,
|
||||
source = excluded.source,
|
||||
extraction_source = excluded.extraction_source,
|
||||
memory_at = excluded.memory_at,
|
||||
updated_at = excluded.updated_at
|
||||
""",
|
||||
(
|
||||
@@ -1226,6 +1264,7 @@ class CodingMemoryStore:
|
||||
json.dumps(memory.files, ensure_ascii=False),
|
||||
json.dumps(memory.source_fact_ids, ensure_ascii=False),
|
||||
memory.confidence, memory.source, memory.extraction_source,
|
||||
_to_epoch(memory.memory_at),
|
||||
(memory.created_at or datetime.utcnow()).isoformat(),
|
||||
(memory.updated_at or datetime.utcnow()).isoformat(),
|
||||
),
|
||||
@@ -1366,8 +1405,8 @@ class CodingMemoryStore:
|
||||
fact_id, user_id, agent_id, session_id, workspace_id, branch,
|
||||
source, statement, files, subject, details,
|
||||
fact_link, derived_experience_ids,
|
||||
status, fact_chain_summary, created_at
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
status, fact_chain_summary, memory_at, created_at
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(fact_id) DO UPDATE SET
|
||||
user_id = excluded.user_id,
|
||||
agent_id = excluded.agent_id,
|
||||
@@ -1382,7 +1421,8 @@ class CodingMemoryStore:
|
||||
fact_link = excluded.fact_link,
|
||||
derived_experience_ids = excluded.derived_experience_ids,
|
||||
status = excluded.status,
|
||||
fact_chain_summary = excluded.fact_chain_summary
|
||||
fact_chain_summary = excluded.fact_chain_summary,
|
||||
memory_at = excluded.memory_at
|
||||
""",
|
||||
(
|
||||
fact.fact_id, fact.user_id, fact.agent_id,
|
||||
@@ -1395,6 +1435,7 @@ class CodingMemoryStore:
|
||||
json.dumps(fact.derived_experience_ids, ensure_ascii=False),
|
||||
fact.status,
|
||||
fact.fact_chain_summary or "",
|
||||
_to_epoch(fact.memory_at),
|
||||
(fact.created_at or datetime.utcnow()).isoformat(),
|
||||
),
|
||||
)
|
||||
|
||||
@@ -25,6 +25,11 @@ FactStatus = Literal["active", "superseded"]
|
||||
MAX_FACT_CHAIN_ENTRIES = 8
|
||||
|
||||
|
||||
def _epoch(value: Optional[datetime]) -> Optional[int]:
|
||||
"""memory_at 对外是秒级 epoch,与 chat 一致。"""
|
||||
return int(value.timestamp()) if isinstance(value, datetime) else None
|
||||
|
||||
|
||||
# ================================================================
|
||||
# 草稿(LLM extract 阶段输出)
|
||||
# ================================================================
|
||||
@@ -53,6 +58,8 @@ class CodingMemoryDraft:
|
||||
files: List[str] = field(default_factory=list)
|
||||
source: str = "default"
|
||||
extraction_source: str = "auto_extract"
|
||||
# 对话时间(SDK 注入,同 chat 的 memory_at)
|
||||
memory_at: Optional[datetime] = None
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
from ..utils.source_filter import normalize_source
|
||||
@@ -73,6 +80,7 @@ class CodingMemoryDraft:
|
||||
"files": list(self.files),
|
||||
"source": self.source,
|
||||
"extraction_source": self.extraction_source,
|
||||
"memory_at": _epoch(self.memory_at),
|
||||
}
|
||||
|
||||
|
||||
@@ -111,6 +119,9 @@ class CodingMemory:
|
||||
extraction_source: str = "auto_extract"
|
||||
|
||||
# 时间
|
||||
# memory_at 是对话时间(同 chat),created_at / updated_at 是写库时间。
|
||||
# UPDATE 合并时取两者中较晚的那个对话时间,见 store.update。
|
||||
memory_at: Optional[datetime] = None
|
||||
created_at: Optional[datetime] = None
|
||||
updated_at: Optional[datetime] = None
|
||||
|
||||
@@ -135,6 +146,7 @@ class CodingMemory:
|
||||
"confidence": self.confidence,
|
||||
"source": self.source,
|
||||
"extraction_source": self.extraction_source,
|
||||
"memory_at": _epoch(self.memory_at),
|
||||
"created_at": self.created_at.isoformat() if self.created_at else None,
|
||||
"updated_at": self.updated_at.isoformat() if self.updated_at else None,
|
||||
}
|
||||
@@ -249,6 +261,7 @@ class WorkingFact:
|
||||
fact_chain_summary: str = ""
|
||||
|
||||
# 时间
|
||||
memory_at: Optional[datetime] = None # 对话时间(同 chat),没传时是收到请求的时间
|
||||
created_at: Optional[datetime] = None # 产生时间,新的优先
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
@@ -272,6 +285,7 @@ class WorkingFact:
|
||||
"derived_experience_ids": list(self.derived_experience_ids),
|
||||
"status": self.status,
|
||||
"fact_chain_summary": self.fact_chain_summary,
|
||||
"memory_at": _epoch(self.memory_at),
|
||||
"created_at": self.created_at.isoformat() if self.created_at else None,
|
||||
}
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ CodingWriter - 写入端协调器
|
||||
import logging
|
||||
import time
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
from ..agent.llm_provider import LLMProvider
|
||||
@@ -79,9 +80,11 @@ class CodingWriter:
|
||||
session_id: Optional[str] = None,
|
||||
request_id: Optional[str] = None,
|
||||
source: str = "default",
|
||||
memory_at: Optional[datetime] = None,
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
working/coding 段写入端到端。返回响应 dict(兼容 client.add 返回结构):
|
||||
working/coding 段写入端到端。memory_at 是对话时间,记到这次写出的经验和
|
||||
事实上;不传则用收到请求的时间(与 chat 链路一致)。返回响应 dict(兼容 client.add 返回结构):
|
||||
{
|
||||
"success": True,
|
||||
"scene": "productivity",
|
||||
@@ -101,6 +104,8 @@ class CodingWriter:
|
||||
"""
|
||||
request_id = request_id or str(uuid.uuid4())
|
||||
t0 = time.perf_counter()
|
||||
if memory_at is None:
|
||||
memory_at = datetime.now()
|
||||
|
||||
if not messages:
|
||||
return self._empty_response(request_id, t0)
|
||||
@@ -172,6 +177,7 @@ class CodingWriter:
|
||||
# extractor 只负责内容;调用来源由 SDK 入口注入,不能交给 LLM 生成。
|
||||
for draft in drafts:
|
||||
draft.source = source
|
||||
draft.memory_at = memory_at
|
||||
|
||||
# 3. 经验层 reconcile + 持久化
|
||||
ops: List[ReconcileOp] = (
|
||||
@@ -197,6 +203,7 @@ class CodingWriter:
|
||||
workspace_id=workspace_id,
|
||||
branch=branch,
|
||||
source=source,
|
||||
memory_at=memory_at,
|
||||
)
|
||||
|
||||
experience_ids = [op.target_experience_id for op in ops
|
||||
|
||||
@@ -260,6 +260,7 @@ class EmbedderConfig:
|
||||
max_retries: int = None # 最大重试次数
|
||||
retry_delay: float = None # 重试间隔(秒)
|
||||
timeout: int = None # 请求超时(秒)
|
||||
batch_size: int = None # 单次 HTTP 请求最多带多少条 input(embed_batch 按此切块)
|
||||
|
||||
# 内部平台扩展(extra_headers / extra_body 会透传给 OpenAI client)
|
||||
extra_headers: Optional[Dict[str, str]] = None
|
||||
@@ -294,6 +295,10 @@ class EmbedderConfig:
|
||||
# 请求切断并制造重试风暴。SDK 内层重试已关闭,15s 是每个显式 attempt
|
||||
# 的真实上限;部署可通过 MEMORY_EMBEDDER_TIMEOUT 覆盖。
|
||||
self.timeout = _get_env_int("MEMORY_EMBEDDER_TIMEOUT", 15)
|
||||
if self.batch_size is None:
|
||||
# 大批量(如重新 embed 整个库)不能一次 HTTP 发几千条:多数服务对
|
||||
# input 数组有上限,超了直接 400。按 batch_size 切块逐块发送。
|
||||
self.batch_size = _get_env_int("MEMORY_EMBEDDER_BATCH_SIZE", 64)
|
||||
if self.extra_headers is None:
|
||||
val = os.getenv("MEMORY_EMBEDDER_EXTRA_HEADERS")
|
||||
if val:
|
||||
|
||||
+120
-23
@@ -38,10 +38,12 @@ import importlib.util
|
||||
import json
|
||||
from json import JSONDecodeError
|
||||
import logging
|
||||
import random
|
||||
import threading
|
||||
import time
|
||||
|
||||
from ..config import MemoryConfig
|
||||
from ..embedding import EmbeddingDimensionMismatchError
|
||||
from .workload import embedding_slot
|
||||
from ..utils.log_setup import get_request_id
|
||||
|
||||
@@ -80,7 +82,7 @@ def _count_tokens(text: str) -> int:
|
||||
try:
|
||||
import tiktoken
|
||||
|
||||
return len(tiktoken.get_encoding("cl100k_base").encode(text))
|
||||
return len(tiktoken.get_encoding("cl100k_base").encode(text, disallowed_special=()))
|
||||
except Exception:
|
||||
return len(text) // _CHARS_PER_TOKEN_FALLBACK + 1
|
||||
|
||||
@@ -98,7 +100,8 @@ def _truncate_for_embedding(text: str, limit: int = _EMBED_MAX_TOKENS) -> str:
|
||||
import tiktoken
|
||||
|
||||
enc = tiktoken.get_encoding("cl100k_base")
|
||||
ids = enc.encode(text)
|
||||
# Input is ordinary user text, not a tokenizer control-token stream.
|
||||
ids = enc.encode(text, disallowed_special=())
|
||||
if len(ids) <= limit:
|
||||
return text
|
||||
dropped = len(ids) - limit
|
||||
@@ -186,6 +189,41 @@ def _redact_headers(headers: Optional[Dict[str, Any]]) -> Optional[Dict[str, Any
|
||||
return out
|
||||
|
||||
|
||||
# Retry-After 的上限:服务端给出超长等待(或恶意值)时不让单次调用挂几十分钟。
|
||||
_RETRY_AFTER_MAX_S = 60.0
|
||||
|
||||
|
||||
def _retry_after_seconds(exc: Exception) -> Optional[float]:
|
||||
"""从 429 异常的响应头里取 Retry-After(秒数或 HTTP 日期),取不到返回 None。"""
|
||||
response = getattr(exc, "response", None)
|
||||
headers = getattr(response, "headers", None)
|
||||
if not headers:
|
||||
return None
|
||||
try:
|
||||
raw = headers.get("retry-after") or headers.get("Retry-After")
|
||||
except Exception:
|
||||
return None
|
||||
if raw is None:
|
||||
return None
|
||||
raw = str(raw).strip()
|
||||
try:
|
||||
seconds = float(raw)
|
||||
except ValueError:
|
||||
try:
|
||||
from email.utils import parsedate_to_datetime
|
||||
from datetime import datetime, timezone
|
||||
|
||||
when = parsedate_to_datetime(raw)
|
||||
if when.tzinfo is None:
|
||||
when = when.replace(tzinfo=timezone.utc)
|
||||
seconds = (when - datetime.now(timezone.utc)).total_seconds()
|
||||
except Exception:
|
||||
return None
|
||||
if seconds != seconds: # NaN
|
||||
return None
|
||||
return min(max(0.0, seconds), _RETRY_AFTER_MAX_S)
|
||||
|
||||
|
||||
def _embeddings_api_url(base_url: str) -> str:
|
||||
"""OpenAI SDK posts to ``{base_url}/embeddings`` (base_url usually ends with /v1)."""
|
||||
base = (base_url or "").rstrip("/")
|
||||
@@ -204,7 +242,8 @@ class EmbedServiceConfig:
|
||||
api_key: str = ""
|
||||
base_url: str = ""
|
||||
embedding_dims: int = 1536
|
||||
batch_size: int = 100
|
||||
# embed_batch 单次 HTTP 最多带多少条 input;超出按块顺序发送
|
||||
batch_size: int = 64
|
||||
enable_cache: bool = True
|
||||
cache_size: int = 10000
|
||||
max_retries: int = 5
|
||||
@@ -286,6 +325,7 @@ class EmbedService:
|
||||
max_retries=embedder.max_retries if embedder.max_retries is not None else 5,
|
||||
retry_delay=embedder.retry_delay if embedder.retry_delay is not None else 3.0,
|
||||
timeout=embedder.timeout if embedder.timeout is not None else 60,
|
||||
batch_size=max(1, int(getattr(embedder, "batch_size", None) or 64)),
|
||||
queue_batch_size=int(os.getenv("MEMORY_EMBED_BATCH_SIZE", "8")),
|
||||
queue_batch_window_ms=float(os.getenv("MEMORY_EMBED_BATCH_WINDOW_MS", "100")),
|
||||
extra_headers=embedder.extra_headers,
|
||||
@@ -639,6 +679,8 @@ class EmbedService:
|
||||
self,
|
||||
texts: List[str],
|
||||
use_cache: bool = True,
|
||||
*,
|
||||
max_retries: Optional[int] = None,
|
||||
) -> List[List[float]]:
|
||||
"""
|
||||
批量向量化
|
||||
@@ -646,17 +688,23 @@ class EmbedService:
|
||||
Args:
|
||||
texts: 文本列表
|
||||
use_cache: 是否使用缓存
|
||||
max_retries: 本次调用的重试次数;None 用配置值。长批量任务(重新
|
||||
embed 整个库)可以给更宽的重试预算,不影响在线路径。
|
||||
|
||||
Returns:
|
||||
向量列表
|
||||
"""
|
||||
vectors, _metas = await self.embed_batch_with_meta(texts, use_cache=use_cache)
|
||||
vectors, _metas = await self.embed_batch_with_meta(
|
||||
texts, use_cache=use_cache, max_retries=max_retries,
|
||||
)
|
||||
return vectors
|
||||
|
||||
async def embed_batch_with_meta(
|
||||
self,
|
||||
texts: List[str],
|
||||
use_cache: bool = True,
|
||||
*,
|
||||
max_retries: Optional[int] = None,
|
||||
) -> Tuple[List[List[float]], List[Dict[str, Any]]]:
|
||||
"""批量向量化,同时返回每条的 provider metadata(非 vector)。"""
|
||||
if not texts:
|
||||
@@ -681,7 +729,12 @@ class EmbedService:
|
||||
indices_to_embed.append(i)
|
||||
|
||||
if texts_to_embed:
|
||||
vectors, batch_metas = await self._embed_texts_with_meta(texts_to_embed)
|
||||
# max_retries 只在显式给出时透传:保持 `_embed_texts_with_meta(texts)`
|
||||
# 这个既有调用形状不变(测试与下游替身按它打桩)。
|
||||
retry_kwargs = {} if max_retries is None else {"max_retries": max_retries}
|
||||
vectors, batch_metas = await self._embed_texts_with_meta(
|
||||
texts_to_embed, **retry_kwargs,
|
||||
)
|
||||
|
||||
for j, vector in enumerate(vectors):
|
||||
idx = indices_to_embed[j]
|
||||
@@ -699,9 +752,9 @@ class EmbedService:
|
||||
return results, metas
|
||||
|
||||
async def _embed_texts_with_meta(
|
||||
self, texts: List[str]
|
||||
self, texts: List[str], *, max_retries: Optional[int] = None,
|
||||
) -> Tuple[List[List[float]], List[Dict[str, Any]]]:
|
||||
"""实际的向量化调用:**一次 HTTP 带走整批**;返回 (vectors, metas)。
|
||||
"""实际的向量化调用:**一次 HTTP 带走一块**;返回 (vectors, metas)。
|
||||
|
||||
早期实现是 `for text in texts: await _embed_openai([text])` 逐条串行,
|
||||
原意是"避免 batch size 限制",但代价极大:线上一批 8 条 × ~5.6s 串行
|
||||
@@ -718,21 +771,34 @@ class EmbedService:
|
||||
截断在这里做(发送前逐条过 `_truncate_for_embedding`):所有 embedding
|
||||
路径(embed / embed_batch / embed_queued / *_with_meta)最终都落到这个
|
||||
方法 —— 放在各入口会漏。理由与阈值见模块头部 `_EMBED_MAX_TOKENS`。
|
||||
|
||||
切块:一次带走整批只适用于小批。几千条的 input 数组会被多数服务直接
|
||||
400(input 上限),所以按 `batch_size`(默认 64)切块,块之间顺序发送。
|
||||
不超过一块时行为与切块前逐字相同(`batch_mode="single_request"`)。
|
||||
"""
|
||||
if not texts:
|
||||
return [], []
|
||||
|
||||
safe_texts = [_truncate_for_embedding(t) for t in texts]
|
||||
vectors, meta = await self._embed_openai(safe_texts)
|
||||
base_meta = dict(meta or {})
|
||||
base_meta["batch_mode"] = "single_request"
|
||||
base_meta["batch_input_count"] = len(safe_texts)
|
||||
|
||||
chunk = max(1, int(self._embed_config.batch_size or 64))
|
||||
vectors: List[List[float]] = []
|
||||
all_metas: List[Dict[str, Any]] = []
|
||||
for idx in range(len(safe_texts)):
|
||||
m = dict(base_meta)
|
||||
m["batch_index"] = idx
|
||||
all_metas.append(m)
|
||||
chunk_count = (len(safe_texts) + chunk - 1) // chunk
|
||||
for start in range(0, len(safe_texts), chunk):
|
||||
part = safe_texts[start:start + chunk]
|
||||
retry_kwargs = {} if max_retries is None else {"max_retries": max_retries}
|
||||
part_vectors, meta = await self._embed_openai(part, **retry_kwargs)
|
||||
base_meta = dict(meta or {})
|
||||
base_meta["batch_mode"] = "single_request" if chunk_count == 1 else "chunked"
|
||||
base_meta["batch_input_count"] = len(part)
|
||||
if chunk_count > 1:
|
||||
base_meta["batch_chunk_index"] = start // chunk
|
||||
base_meta["batch_chunk_count"] = chunk_count
|
||||
for idx in range(len(part)):
|
||||
m = dict(base_meta)
|
||||
m["batch_index"] = start + idx
|
||||
all_metas.append(m)
|
||||
vectors.extend(part_vectors)
|
||||
|
||||
self._last_embed_meta = all_metas[-1] if all_metas else {}
|
||||
return vectors, all_metas
|
||||
@@ -753,16 +819,22 @@ class EmbedService:
|
||||
"""
|
||||
打一条 INFO 级 embedding 请求日志(含 request_id / provider / model / api / 入参 / 出参)。
|
||||
|
||||
出参只含 vector 维度,不含向量数值。request_id 同时写入 message,
|
||||
出参只含 vector 维度,不含向量数值。入参的 input 是待向量化的原文
|
||||
(对话 / 记忆 / 查询),INFO 只记每条的字符数(input_chars),原文仅在
|
||||
DEBUG 单独输出。request_id 同时写入 message,
|
||||
并依赖 log formatter 的 %(request_id)s(setup_logging 后生效)。
|
||||
"""
|
||||
texts = request_payload.get("input")
|
||||
info_request = {k: v for k, v in request_payload.items() if k != "input"}
|
||||
if isinstance(texts, list):
|
||||
info_request["input_chars"] = [len(str(t)) for t in texts]
|
||||
payload = {
|
||||
"request_id": get_request_id(),
|
||||
"status": status,
|
||||
"provider": self._embed_config.provider,
|
||||
"model": self._embed_config.model,
|
||||
"api": api_url,
|
||||
"request": request_payload,
|
||||
"request": info_request,
|
||||
"response": response_payload,
|
||||
}
|
||||
try:
|
||||
@@ -770,9 +842,13 @@ class EmbedService:
|
||||
except Exception:
|
||||
body = str(payload)
|
||||
logger.info(f"[embed] {body}")
|
||||
if texts is not None and logger.isEnabledFor(logging.DEBUG):
|
||||
logger.debug(
|
||||
f"[embed] input: {json.dumps(texts, ensure_ascii=False, default=str)}"
|
||||
)
|
||||
|
||||
async def _embed_openai(
|
||||
self, texts: List[str]
|
||||
self, texts: List[str], *, max_retries: Optional[int] = None,
|
||||
) -> Tuple[List[List[float]], Dict[str, Any]]:
|
||||
"""
|
||||
使用 OpenAI 兼容接口向量化
|
||||
@@ -862,7 +938,9 @@ class EmbedService:
|
||||
"endpoint_overridden": bool(_ov),
|
||||
}
|
||||
|
||||
_max_retries = max(0, int(self._embed_config.max_retries or 0))
|
||||
_max_retries = max(0, int(
|
||||
(self._embed_config.max_retries or 0) if max_retries is None else max_retries
|
||||
))
|
||||
_retry_delay = max(0.0, float(self._embed_config.retry_delay or 0.0))
|
||||
last_exc: Optional[Exception] = None
|
||||
|
||||
@@ -908,6 +986,17 @@ class EmbedService:
|
||||
f"got={len(vectors)}"
|
||||
)
|
||||
dims = [len(v) for v in vectors]
|
||||
expected_dims = self._embed_config.embedding_dims
|
||||
if expected_dims:
|
||||
bad = [d for d in dims if d != int(expected_dims)]
|
||||
if bad:
|
||||
# 宽度不对的向量写进集合会把集合的维度钉死在错误值上(Chroma
|
||||
# 首次 add 定维),或在已有集合上整批失败。确定性错误,不重试。
|
||||
raise EmbeddingDimensionMismatchError(
|
||||
"embedding dimension mismatch: "
|
||||
f"expected={int(expected_dims)} got={bad[0]} "
|
||||
f"model={self._embed_config.model}"
|
||||
)
|
||||
meta = {
|
||||
"http_status": raw.status_code,
|
||||
"response_headers": resp_headers,
|
||||
@@ -943,6 +1032,8 @@ class EmbedService:
|
||||
},
|
||||
)
|
||||
return vectors, meta
|
||||
except EmbeddingDimensionMismatchError:
|
||||
raise
|
||||
except Exception as e:
|
||||
last_exc = e
|
||||
_err_name = type(e).__name__
|
||||
@@ -976,12 +1067,18 @@ class EmbedService:
|
||||
if _retryable and attempt < _max_retries:
|
||||
# 逐出可能卡死的 client,下个 attempt 重建干净连接(恢复原防御语义)
|
||||
self._evict_client(_eff_base_url, _eff_api_key, _eff_timeout)
|
||||
_delay = _retry_delay
|
||||
_retry_after = _retry_after_seconds(e) if _status == 429 else None
|
||||
if _retry_after is not None:
|
||||
# 限流时服务端明确告诉了要等多久:按它等,再加一点抖动,
|
||||
# 避免多个 worker 在同一时刻一起醒来再次撞限流。
|
||||
_delay = max(_delay, _retry_after) + random.uniform(0, 0.25 * _retry_after)
|
||||
logger.warning(
|
||||
f"[embed] attempt {attempt + 1}/{_max_retries + 1} failed "
|
||||
f"({_err_name}): {_err_msg[:200]}; retry in {_retry_delay}s"
|
||||
f"({_err_name}): {_err_msg[:200]}; retry in {_delay:.2f}s"
|
||||
)
|
||||
if _retry_delay > 0:
|
||||
await asyncio.sleep(_retry_delay)
|
||||
if _delay > 0:
|
||||
await asyncio.sleep(_delay)
|
||||
continue
|
||||
self._log_embed_call(
|
||||
status="error",
|
||||
|
||||
@@ -22,6 +22,7 @@ import logging
|
||||
import json
|
||||
import threading
|
||||
import time
|
||||
import weakref
|
||||
|
||||
from ..models.memory import MemoryNode, MemoryLayer, MemoryStatus
|
||||
from ..config import MemoryConfig
|
||||
@@ -77,6 +78,21 @@ async def _run_in_kuzu_pool(func, *args, **kwargs):
|
||||
return await run_in_executor_guarded(_kuzu_executor, _measured)
|
||||
|
||||
|
||||
# 本进程里已打开、尚未 close 的 store。drop_graph 据此拒绝删掉一个正被
|
||||
# 活着的 client 使用的图库。WeakSet:store 被回收后自动消失。
|
||||
_LIVE_STORES: "weakref.WeakSet[KuzuGraphStore]" = weakref.WeakSet()
|
||||
|
||||
|
||||
def open_graph_paths() -> List[str]:
|
||||
"""Real paths of the graph databases live stores in this process hold open."""
|
||||
import os
|
||||
return [
|
||||
os.path.realpath(str(store._db_path))
|
||||
for store in list(_LIVE_STORES)
|
||||
if store._db is not None
|
||||
]
|
||||
|
||||
|
||||
def kuzu_executor_snapshot() -> Dict[str, Any]:
|
||||
with _kuzu_stats_lock:
|
||||
return {
|
||||
@@ -350,6 +366,7 @@ class KuzuGraphStore(GraphStoreBase):
|
||||
)
|
||||
self._db = db
|
||||
self._conn = conn
|
||||
_LIVE_STORES.add(self)
|
||||
except asyncio.TimeoutError:
|
||||
logger.warning(
|
||||
f"GraphStore init timed out after 5s (Kuzu file lock contention). "
|
||||
@@ -2441,6 +2458,7 @@ class KuzuGraphStore(GraphStoreBase):
|
||||
async def close(self) -> None:
|
||||
"""关闭连接"""
|
||||
conn, db = self._conn, self._db
|
||||
_LIVE_STORES.discard(self)
|
||||
self._conn = None
|
||||
self._db = None
|
||||
self._available = False
|
||||
|
||||
@@ -89,6 +89,17 @@ def passes_created_after(
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def physical_collection_name(base_name: Optional[str], dims: Optional[int]) -> str:
|
||||
"""The collection name a store opens for ``base_name`` at ``dims``.
|
||||
|
||||
The dims suffix keeps two embedding widths from sharing one collection.
|
||||
`hy_memory.reembed.vector_space_names` uses this too, so callers never
|
||||
re-derive the rule.
|
||||
"""
|
||||
base = base_name or "agent_memories"
|
||||
return f"{base}_{dims}" if dims else base
|
||||
|
||||
|
||||
class VectorStoreBase(ABC):
|
||||
"""
|
||||
向量存储抽象基类
|
||||
@@ -98,13 +109,10 @@ class VectorStoreBase(ABC):
|
||||
|
||||
def __init__(self, config: MemoryConfig):
|
||||
self.config = config
|
||||
base_name = config.vector_store.collection_name or "agent_memories"
|
||||
dims = config.vector_store.embedding_dims
|
||||
# 自动拼接维度后缀,避免不同 embedding 模型写入同一 collection 导致维度冲突
|
||||
if dims:
|
||||
self._collection_name = f"{base_name}_{dims}"
|
||||
else:
|
||||
self._collection_name = base_name
|
||||
self._collection_name = physical_collection_name(
|
||||
config.vector_store.collection_name, config.vector_store.embedding_dims,
|
||||
)
|
||||
|
||||
_supports_retrieval_clauses: bool = False
|
||||
_supports_narrow_episode_queries: bool = False
|
||||
|
||||
@@ -9,10 +9,12 @@ Agent Memory V2 - VectorStore (Chroma)
|
||||
- 同步 API → _run_in_vdb_pool() 包装为异步
|
||||
"""
|
||||
|
||||
from typing import Optional, List, Dict, Any
|
||||
from typing import Optional, List, Dict, Any, Set, Tuple
|
||||
import concurrent.futures
|
||||
import functools
|
||||
import logging
|
||||
import os
|
||||
import weakref
|
||||
|
||||
from ..models.memory import (
|
||||
MemoryNode, MemoryLayer, MemoryStatus,
|
||||
@@ -33,6 +35,30 @@ _vdb_executor = concurrent.futures.ThreadPoolExecutor(
|
||||
)
|
||||
|
||||
|
||||
# 本进程里已 initialize、尚未 close 的 store。drop_vector_space 据此拒绝删掉
|
||||
# 一个正被活着的 client 使用的集合。WeakSet:store 被回收后自动消失。
|
||||
_LIVE_STORES: "weakref.WeakSet[ChromaVectorStore]" = weakref.WeakSet()
|
||||
|
||||
|
||||
def _location_key(vs_config) -> Tuple[str, str]:
|
||||
if getattr(vs_config, "host", None):
|
||||
return ("http", f"{vs_config.host}:{vs_config.port or 8000}")
|
||||
return ("local", os.path.realpath(str(vs_config.persist_directory or "")))
|
||||
|
||||
|
||||
def open_collection_names(location: Tuple[str, str]) -> Set[str]:
|
||||
"""Collections that live stores in this process hold open at ``location``."""
|
||||
names: Set[str] = set()
|
||||
for store in list(_LIVE_STORES):
|
||||
if store._client is None or _location_key(store.config.vector_store) != location:
|
||||
continue
|
||||
names.add(store._collection_name)
|
||||
if store._entity_collection is not None:
|
||||
from ..pipelines._retrieval.config import entity_collection_name
|
||||
names.add(entity_collection_name(store._collection_name))
|
||||
return names
|
||||
|
||||
|
||||
async def _run_in_vdb_pool(func, *args, **kwargs):
|
||||
"""Run a synchronous Chroma call in the dedicated VDB executor."""
|
||||
call = functools.partial(func, *args, **kwargs) if args or kwargs else func
|
||||
@@ -88,6 +114,7 @@ class ChromaVectorStore(VectorStoreBase):
|
||||
return client, collection
|
||||
|
||||
self._client, self._collection = await _run_in_vdb_pool(_init)
|
||||
_LIVE_STORES.add(self)
|
||||
logger.debug(f"VectorStore initialized (Chroma), collection={self._collection_name}")
|
||||
|
||||
# ================================================================
|
||||
@@ -561,6 +588,7 @@ class ChromaVectorStore(VectorStoreBase):
|
||||
async def close(self) -> None:
|
||||
"""关闭客户端"""
|
||||
# Chroma PersistentClient 无需显式关闭
|
||||
_LIVE_STORES.discard(self)
|
||||
self._client = None
|
||||
self._collection = None
|
||||
self._entity_collection = None
|
||||
|
||||
@@ -115,11 +115,16 @@ class ReconcileHybridRetriever:
|
||||
fused_max = max(fused_all) if fused_all else 0.0
|
||||
|
||||
base = (
|
||||
f"[reconcile-hybrid][{req}] pool={pool_size} q_terms={q_terms!r} "
|
||||
f"[reconcile-hybrid][{req}] pool={pool_size} q_terms={len(q_terms)} "
|
||||
f"raw_bm25 max={raw_max:.4f} nonzero={raw_nonzero}/{len(raw_bm25)} "
|
||||
f"bm25_norm max={norm_max:.4f} nonzero={norm_nonzero}/{len(bm25_norms)} "
|
||||
f"sem_max={sem_max:.4f} fused_max={fused_max:.4f} "
|
||||
f"kept={kept}/{len(fused_all)} min_score={self._cfg.min_score} "
|
||||
f"query({len(query_text or '')} chars)"
|
||||
)
|
||||
# query 是待对账的新记忆原文,词项与预览只进 DEBUG
|
||||
logger.debug(
|
||||
f"[reconcile-hybrid][{req}] q_terms={q_terms!r} "
|
||||
f"query={q_preview!r} lemmatized={lem_preview!r}"
|
||||
)
|
||||
|
||||
|
||||
@@ -692,9 +692,10 @@ class CrossDomainSweeper:
|
||||
)
|
||||
|
||||
logger.info(
|
||||
f"[sweeper] Created L6 core {core_id[:12]}: '{core_content[:60]}' "
|
||||
f"[sweeper] Created L6 core {core_id[:12]} "
|
||||
f"from {len(cluster_ids)} basics, evidence_inherited={len(inherited_evidence_ids)}"
|
||||
)
|
||||
logger.debug(f"[sweeper] L6 core {core_id[:12]} content={core_content[:60]!r}")
|
||||
return 1, 0, [core_id]
|
||||
|
||||
except Exception as e:
|
||||
|
||||
@@ -3587,8 +3587,9 @@ class BaseReadPipeline(ReadPipeline):
|
||||
f"hidden_l0={len(hidden_l0_hits)} fwd_l6={len(forward_l6)} rev_l6={len(reverse_l6)} "
|
||||
f"fused_l6={len(fused_l6)} graph_intent={len(graph_intention_hits)} "
|
||||
f"returned={len(final_results)} "
|
||||
f"query='{request.query[:80]}'"
|
||||
f"query({len(request.query or '')} chars)"
|
||||
)
|
||||
logger.debug(f"[read/{self.VERSION}] query={request.query[:80]!r}")
|
||||
|
||||
response.success = True
|
||||
|
||||
|
||||
@@ -2516,7 +2516,8 @@ def _parse_operations_json(text: str) -> Optional[List[Dict[str, Any]]]:
|
||||
return result
|
||||
return None
|
||||
except json.JSONDecodeError:
|
||||
logger.warning(f"[S2-agent] JSON parse failed: {json_str[:200]}")
|
||||
logger.warning(f"[S2-agent] JSON parse failed ({len(json_str)} chars)")
|
||||
logger.debug(f"[S2-agent] unparsable output: {json_str[:200]!r}")
|
||||
return None
|
||||
|
||||
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -4,6 +4,7 @@ from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import concurrent.futures
|
||||
import contextvars
|
||||
import functools
|
||||
from concurrent.futures import Executor
|
||||
from typing import Any, Callable, Optional
|
||||
@@ -39,7 +40,9 @@ async def run_in_executor_guarded(
|
||||
|
||||
async def to_thread_guarded(func: Callable[..., Any], *args: Any, **kwargs: Any) -> Any:
|
||||
"""Guarded equivalent of ``asyncio.to_thread`` using the default pool."""
|
||||
return await run_in_executor_guarded(None, func, *args, **kwargs)
|
||||
context = contextvars.copy_context()
|
||||
call = functools.partial(func, *args, **kwargs)
|
||||
return await run_in_executor_guarded(None, context.run, call)
|
||||
|
||||
|
||||
def concurrent_result_guarded(
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Pipeline Log Writer — 环节级 JSONL 日志(始终开启,与 SQLite Trace 分离)。
|
||||
Pipeline Log Writer — 环节级 JSONL 日志(默认开启,MEMORY_PIPELINE_LOG_ENABLED=false 关闭;
|
||||
与 SQLite Trace 分离)。
|
||||
|
||||
目录: {log_dir}/{subdir}/{date}.log
|
||||
每行: request_id, namespace, user_id, agent_id, step, prompt, content, parsed,
|
||||
|
||||
@@ -1,16 +1,20 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Pipeline 可观测性:Log(文件 JSONL,始终开启)与 Trace(SQLite pipeline_logs,可关闭)。
|
||||
Pipeline 可观测性:Log(文件 JSONL,默认开启,可关闭)与 Trace(SQLite pipeline_logs,可关闭)。
|
||||
|
||||
- Log:运维 tail/grep,每个环节即时一行,不依赖后续步骤成功与否。
|
||||
- Trace:Inspector / 调试,落库 pipeline_logs(需 MEMORY_CACHE_BACKEND=sqlite 等持久化后端)。
|
||||
|
||||
环境变量:
|
||||
MEMORY_PIPELINE_LOG_ENABLED — Log 写 JSONL 文件,默认 true;false 时完全不写
|
||||
MEMORY_PIPELINE_TRACE_ENABLED — Trace 落库,默认 true;OpenClaw C 端可设 false
|
||||
MEMORY_MEMORY_OPERATIONS_ENABLED — memory_operations 审计表落库,默认 false
|
||||
MEMORY_LOG_DIR — Log 目录(优先)
|
||||
MEMORY_TRACE_LOG_DIR — Log 目录(兼容旧名)
|
||||
|
||||
Log 与 Trace 都关闭时不包装 store_pipeline_log,而是换成立即返回 True 的
|
||||
空操作:热路径不做任何计时、脱敏或后台任务,也不写库。
|
||||
|
||||
子目录({log_dir}/{subdir}/{date}.log)由 PipelineLogWriter 从每条记录的
|
||||
user_id 前缀(第一个 "__" 左侧)切分;无前缀回落 "default"。该前缀是
|
||||
通用 namespace,可用于项目、部署或数据分桶。
|
||||
@@ -94,6 +98,16 @@ def is_pipeline_trace_enabled() -> bool:
|
||||
)
|
||||
|
||||
|
||||
def is_pipeline_log_enabled() -> bool:
|
||||
"""是否把 pipeline step 写成 JSONL 文件({log_dir}/<subdir>/<date>.log)。默认开启。"""
|
||||
return os.getenv("MEMORY_PIPELINE_LOG_ENABLED", "true").strip().lower() in (
|
||||
"true",
|
||||
"1",
|
||||
"yes",
|
||||
"on",
|
||||
)
|
||||
|
||||
|
||||
def is_memory_operations_enabled() -> bool:
|
||||
"""是否将知识库变动写入 memory_operations 审计表。默认关闭(false)。"""
|
||||
return os.getenv("MEMORY_MEMORY_OPERATIONS_ENABLED", "false").lower() in (
|
||||
@@ -125,7 +139,7 @@ def redact_content(value: str) -> str:
|
||||
|
||||
def resolve_pipeline_log_dir() -> str:
|
||||
"""
|
||||
JSONL pipeline log 根目录(始终使用,有默认值)。
|
||||
JSONL pipeline log 根目录(MEMORY_PIPELINE_LOG_ENABLED 开启时使用,有默认值)。
|
||||
|
||||
优先级: MEMORY_LOG_DIR > MEMORY_TRACE_LOG_DIR > {MEMORY_DATA_DIR}/logs/pipeline
|
||||
"""
|
||||
@@ -228,6 +242,7 @@ def wrap_store_pipeline_log(
|
||||
) -> Callable[..., Any]:
|
||||
"""包装 cache.store_pipeline_log:计时同步、JSONL/DB 异步落盘,不堵主请求。
|
||||
|
||||
- log_writer 为 None 时不写 JSONL(MEMORY_PIPELINE_LOG_ENABLED=false)
|
||||
- step-to-step elapsed_ms / event_at_ms 在调用瞬间打点(与业务墙钟对齐)
|
||||
- event_at_ms:调用方传入非 0 值则直接复用(App 已在自己被调用的那一刻
|
||||
同步打点,比这里更接近真实事件时刻);传 0 / 不传才用本地 time.time()
|
||||
@@ -410,7 +425,8 @@ def wrap_store_pipeline_log(
|
||||
try:
|
||||
loop = asyncio.get_running_loop()
|
||||
# JSONL 用线程池,与 loop 无亲和,留在当前 loop。
|
||||
_track_task(loop.create_task(_persist_jsonl()))
|
||||
if log_writer is not None:
|
||||
_track_task(loop.create_task(_persist_jsonl()))
|
||||
# DB trace 用 cache pool(可能 aiomysql,绑定 owner_loop)。若当前不在
|
||||
# owner_loop(如 search 走独立 loop),派发回 owner_loop 执行,避免跨 loop。
|
||||
if trace_enabled:
|
||||
@@ -446,25 +462,26 @@ def wrap_store_pipeline_log(
|
||||
)
|
||||
except RuntimeError:
|
||||
# 无 running loop(极少见):退化为同步写,保证不丢日志
|
||||
try:
|
||||
log_writer.write_step(
|
||||
request_id=request_id,
|
||||
user_id=user_id,
|
||||
agent_id=agent_id,
|
||||
session_id=session_id,
|
||||
step=step,
|
||||
prompt=prompt,
|
||||
response=response,
|
||||
parsed=parsed,
|
||||
memory_ids=mem_ids,
|
||||
elapsed_ms=step_elapsed_ms,
|
||||
prompt_tokens=prompt_tokens,
|
||||
completion_tokens=completion_tokens,
|
||||
total_tokens=total_tokens,
|
||||
event_at_ms=event_at_ms,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
if log_writer is not None:
|
||||
try:
|
||||
log_writer.write_step(
|
||||
request_id=request_id,
|
||||
user_id=user_id,
|
||||
agent_id=agent_id,
|
||||
session_id=session_id,
|
||||
step=step,
|
||||
prompt=prompt,
|
||||
response=response,
|
||||
parsed=parsed,
|
||||
memory_ids=mem_ids,
|
||||
elapsed_ms=step_elapsed_ms,
|
||||
prompt_tokens=prompt_tokens,
|
||||
completion_tokens=completion_tokens,
|
||||
total_tokens=total_tokens,
|
||||
event_at_ms=event_at_ms,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
if trace_enabled:
|
||||
try:
|
||||
result = original_store(
|
||||
@@ -496,6 +513,11 @@ def wrap_store_pipeline_log(
|
||||
return _store_pipeline_observability
|
||||
|
||||
|
||||
async def _noop_store_pipeline_log(*args: Any, **kwargs: Any) -> bool:
|
||||
"""Log 与 Trace 都关闭时的 store_pipeline_log:不计时、不写文件、不写库。"""
|
||||
return True
|
||||
|
||||
|
||||
def install_pipeline_observability(
|
||||
cache: Any,
|
||||
*,
|
||||
@@ -503,6 +525,9 @@ def install_pipeline_observability(
|
||||
) -> None:
|
||||
"""给 cache 安装 JSONL + Trace 包装(fire-and-forget)。
|
||||
|
||||
MEMORY_PIPELINE_LOG_ENABLED=false 时不写 JSONL;若 Trace 也关闭,
|
||||
store_pipeline_log 被换成空操作(不包装、不写库)。
|
||||
|
||||
在 cache 所属 loop(write loop / _LoopThread)上调用,本函数会捕获该 loop
|
||||
作为 owner_loop 传给包装器:search 走独立 loop 后,从 search loop 触发的 DB
|
||||
落库会被派发回这个 owner_loop,避免跨 loop 用 aiomysql 污染连接池。
|
||||
@@ -514,15 +539,31 @@ def install_pipeline_observability(
|
||||
except RuntimeError:
|
||||
owner_loop = None
|
||||
|
||||
log_dir = resolve_pipeline_log_dir()
|
||||
log_writer = PipelineLogWriter(log_dir)
|
||||
log_enabled = is_pipeline_log_enabled()
|
||||
trace_enabled = is_pipeline_trace_enabled()
|
||||
logger.info(
|
||||
f"[{log_label}] Pipeline log (JSONL) enabled: {log_dir}/<subdir>/<date>.log "
|
||||
f"(subdir from user_id prefix before '__', else default) ; "
|
||||
f"pipeline trace (DB)={'on' if trace_enabled else 'off'} ; "
|
||||
f"persist=async(fire-and-forget)"
|
||||
)
|
||||
if not log_enabled and not trace_enabled:
|
||||
logger.info(
|
||||
f"[{log_label}] Pipeline log (JSONL) disabled ; pipeline trace (DB)=off ; "
|
||||
f"store_pipeline_log=no-op"
|
||||
)
|
||||
cache.store_pipeline_log = _noop_store_pipeline_log
|
||||
return
|
||||
|
||||
log_writer = None
|
||||
if log_enabled:
|
||||
log_dir = resolve_pipeline_log_dir()
|
||||
log_writer = PipelineLogWriter(log_dir)
|
||||
logger.info(
|
||||
f"[{log_label}] Pipeline log (JSONL) enabled: {log_dir}/<subdir>/<date>.log "
|
||||
f"(subdir from user_id prefix before '__', else default) ; "
|
||||
f"pipeline trace (DB)={'on' if trace_enabled else 'off'} ; "
|
||||
f"persist=async(fire-and-forget)"
|
||||
)
|
||||
else:
|
||||
logger.info(
|
||||
f"[{log_label}] Pipeline log (JSONL) disabled ; "
|
||||
f"pipeline trace (DB)=on ; persist=async(fire-and-forget)"
|
||||
)
|
||||
cache.store_pipeline_log = wrap_store_pipeline_log(
|
||||
cache.store_pipeline_log,
|
||||
log_writer=log_writer,
|
||||
|
||||
@@ -36,7 +36,8 @@ def count_tokens(text: str) -> int:
|
||||
enc = _get_encoder()
|
||||
if enc:
|
||||
try:
|
||||
return len(enc.encode(text))
|
||||
# Quoted control-token spellings are ordinary input text.
|
||||
return len(enc.encode(text, disallowed_special=()))
|
||||
except Exception as e:
|
||||
logger.debug(f"[token_count] encode failed, char fallback: {e}")
|
||||
return max(1, len(text) // 4)
|
||||
|
||||
+6
-4
@@ -29,10 +29,12 @@ prompt/response、密钥、访问令牌或完整本地路径。结构化日志
|
||||
文件。详细合同见
|
||||
[`docs/contracts/diagnostic-logging.md`](docs/contracts/diagnostic-logging.md)。
|
||||
|
||||
用户显式把诊断级别切换为 `debug` 时,Core 会启用 HY pipeline trace,并将
|
||||
`MEMORY_LOG_LLM_CONTENT` 设为 `true`。该模式会把完整模型 prompt/response 写入本地调试
|
||||
日志,其中可能包含获授权的对话正文和模型输出;它只应用于有意识的故障排查,不应长期
|
||||
开启,也不应在未经检查和脱敏时分享相关日志。切回 `info` 会停止后续正文记录,但不会
|
||||
HY pipeline 日志与 trace 在任何级别下都关闭。HY SDK 的文本日志只写入
|
||||
`hy-memory/governed-v2/logs/hy_memory.log`,其中的 WARNING 及以上同时写入 `core.log`;
|
||||
SDK 不在 INFO 及以上级别记录对话或记忆正文。用户显式把诊断级别切换为 `debug` 时,Core 将
|
||||
`MEMORY_LOG_LLM_CONTENT` 设为 `true`。该模式会把完整模型 prompt/response 写入
|
||||
`hy_memory.log` 的 DEBUG 行,其中可能包含获授权的对话正文和模型输出;它只应用于有意识的
|
||||
故障排查,不应长期开启,也不应在未经检查和脱敏时分享相关日志。切回 `info` 会停止后续正文记录,但不会
|
||||
自动删除此前已写入的调试文件。
|
||||
|
||||
具体行为以当前源码、网络请求实现和发布说明为准。发现实现与本说明不一致时,请按
|
||||
|
||||
+4
-4
@@ -18,13 +18,13 @@ If you run into a problem, please open an Issue with the version, platform,repro
|
||||
|
||||
## 发行与平台合同
|
||||
|
||||
源码当前固定使用公开 PyPI 正式版 `hy-memory==2.0.6`。MemHub 源码版本为 `1.0.5`
|
||||
(build 14),GitHub 仓库为
|
||||
源码当前固定使用公开 PyPI 正式版 `hy-memory==2.0.9`。MemHub 源码版本为 `1.0.5`
|
||||
(build 36),GitHub 仓库为
|
||||
[`Tencent-Hunyuan/Hy-Memory`](https://github.com/Tencent-Hunyuan/Hy-Memory),npm 包名为
|
||||
`memhub-cli`。当前代码架构见
|
||||
[`docs/ARCHITECTURE.md`](docs/ARCHITECTURE.md)。
|
||||
|
||||
`v1.0.5` 的发行目标是 Linux x64/arm64 Runtime 与 macOS arm64/x64 App 和 Runtime。
|
||||
`v1.0.5` 的发行目标是 Linux x64/arm64 Runtime 与 macOS arm64 App 和 Runtime。
|
||||
Linux 制品由 GitHub Actions 在原生架构 runner 上构建并完成生命周期验收;macOS
|
||||
制品由发行流水线完成 Developer ID 签名、公证和验收后手动上传。Windows 不支持。
|
||||
只有同时出现在 GitHub Release 与 `release-manifest.json` 中、且通过公开校验的制品才属于
|
||||
@@ -118,7 +118,7 @@ MemHub 的 SQLite 数据默认位于 `~/.one-memory`,不在 npm 包或 Runtime
|
||||
|
||||
## Hy Memory
|
||||
|
||||
SDK 依赖固定为公开 PyPI 正式版 `hy-memory==2.0.6`。SDK 配置、权限边界及 search
|
||||
SDK 依赖固定为公开 PyPI 正式版 `hy-memory==2.0.9`。SDK 配置、权限边界及 search
|
||||
返回格式见 [`MEMORY.md`](MEMORY.md)。HY search 返回的 `content` 已包含 SDK 统一生成的
|
||||
逻辑时间和演化上下文;调用方不应再次转换时间戳或重复拼接 `When`。
|
||||
|
||||
|
||||
@@ -2352,7 +2352,7 @@
|
||||
{
|
||||
"SPDXID": "SPDXRef-Package-hy-memory",
|
||||
"name": "hy-memory",
|
||||
"versionInfo": "2.0.6",
|
||||
"versionInfo": "2.0.9",
|
||||
"downloadLocation": "NOASSERTION",
|
||||
"filesAnalyzed": false,
|
||||
"licenseConcluded": "Apache-2.0",
|
||||
|
||||
@@ -9,7 +9,7 @@ Per-package license texts are not reproduced here yet; the license
|
||||
column is filled in as part of release review, and the SBOM records
|
||||
`NOASSERTION` until then rather than guessing.
|
||||
|
||||
## hy-memory 2.0.6
|
||||
## hy-memory 2.0.9
|
||||
|
||||
License: Apache-2.0
|
||||
|
||||
|
||||
@@ -162,10 +162,54 @@ it("never emits a secret from a malformed manually written event", async () => {
|
||||
command: "logs",
|
||||
state: "empty",
|
||||
events: 0,
|
||||
invalid_lines: 1,
|
||||
invalid_lines: 0,
|
||||
unrecognized_lines: 1,
|
||||
});
|
||||
});
|
||||
|
||||
// After an upgrade the files still hold an older build's records. They are
|
||||
// whole JSON lines the reader leaves out, not damage, so the text output must
|
||||
// not warn about them; only a line that is not JSON is called damaged.
|
||||
it("warns only about lines that are not JSON, not about older records", async () => {
|
||||
const dataDir = await fixture();
|
||||
const older = {
|
||||
schema_version: 0,
|
||||
service: "desktop",
|
||||
component: "desktop.lifecycle",
|
||||
event: "desktop.started",
|
||||
};
|
||||
await writeFile(
|
||||
path.join(dataDir, "logs", "diagnostic-desktop.jsonl"),
|
||||
`${JSON.stringify(older)}\n${JSON.stringify({ ...older, schema_version: 1 })}\n`,
|
||||
{ mode: 0o600 },
|
||||
);
|
||||
let stdout = "";
|
||||
let stderr = "";
|
||||
vi.spyOn(process.stdout, "write").mockImplementation((value) => {
|
||||
stdout += String(value);
|
||||
return true;
|
||||
});
|
||||
vi.spyOn(process.stderr, "write").mockImplementation((value) => {
|
||||
stderr += String(value);
|
||||
return true;
|
||||
});
|
||||
|
||||
expect(await run(["logs"], { ONE_MEMORY_DATA_DIR: dataDir })).toBe(0);
|
||||
expect(stdout).toContain("core.start_failed");
|
||||
expect(stdout).not.toContain("损坏");
|
||||
expect(stderr).toBe("");
|
||||
|
||||
await writeFile(
|
||||
path.join(dataDir, "logs", "diagnostic-cli.jsonl"),
|
||||
'{"schema_version": 1, "event": \n',
|
||||
{ mode: 0o600 },
|
||||
);
|
||||
stdout = "";
|
||||
expect(await run(["logs"], { ONE_MEMORY_DATA_DIR: dataDir })).toBe(0);
|
||||
expect(stdout).toContain("跳过 1 行损坏的诊断记录。");
|
||||
expect(stderr).toBe("memhub: 跳过 1 行损坏的诊断记录。\n");
|
||||
});
|
||||
|
||||
it("rejects invalid filters without touching the log directory", async () => {
|
||||
const dataDir = path.join(
|
||||
await mkdtemp(path.join(os.tmpdir(), "om-cli-no-log-")),
|
||||
|
||||
+143
-11
@@ -190,8 +190,10 @@ import {
|
||||
} from "./managed-cli-link.js";
|
||||
import {
|
||||
addMemory,
|
||||
DEFAULT_USER_ID,
|
||||
isMemoryId,
|
||||
isRequestId,
|
||||
isUserId,
|
||||
MEMORY_KINDS,
|
||||
MEMORY_SCENES,
|
||||
MEMORY_VARIANTS,
|
||||
@@ -209,6 +211,7 @@ import {
|
||||
renderMemoryAddText,
|
||||
renderMemoryDetailText,
|
||||
renderMemoryPageText,
|
||||
userIdParameters,
|
||||
validMemoryText,
|
||||
} from "./memory-report.js";
|
||||
import {
|
||||
@@ -538,6 +541,23 @@ async function runCommand(
|
||||
environment = { ...environment, ONE_MEMORY_DATA_DIR: lifted.dataDir };
|
||||
}
|
||||
}
|
||||
// `--user-id <id>` is global to the memory commands: it names whose
|
||||
// memories are read or written. It is lifted before dispatch so each
|
||||
// command's own parser keeps seeing exactly the arguments it always did.
|
||||
// `sources connect` gives the flag its own meaning and parses it itself.
|
||||
let userId: string | undefined;
|
||||
if (USER_ID_COMMANDS.has(arguments_[0] ?? "")) {
|
||||
const lifted = liftUserIdOption(arguments_);
|
||||
if (lifted === null) {
|
||||
return reject(
|
||||
findCommand(arguments_[0] ?? ""),
|
||||
asJson,
|
||||
"--user-id 无效:只能是 1–64 位字母、数字、点、下划线或连字符。",
|
||||
);
|
||||
}
|
||||
arguments_ = lifted.arguments_;
|
||||
userId = lifted.userId;
|
||||
}
|
||||
environment = await environmentWithManagedDataDir(environment);
|
||||
const command = arguments_[0];
|
||||
if (command === undefined) return reject(undefined, asJson, "缺少命令。");
|
||||
@@ -776,9 +796,11 @@ async function runCommand(
|
||||
const attachment = await attachToRuntimeOrNull(environment);
|
||||
if (attachment === null) return runtimeUnavailable(descriptor, asJson);
|
||||
if (parsed.kind === "status") {
|
||||
const receipt = await readMemoryWrite(attachment, parsed.id).catch(
|
||||
() => undefined,
|
||||
);
|
||||
const receipt = await readMemoryWrite(
|
||||
attachment,
|
||||
parsed.id,
|
||||
userId,
|
||||
).catch(() => undefined);
|
||||
if (receipt === undefined) {
|
||||
return emitFailure(asJson, "write status", "memory_write_read_failed");
|
||||
}
|
||||
@@ -812,6 +834,7 @@ async function runCommand(
|
||||
? {}
|
||||
: { memory_at: parsed.memory_at }),
|
||||
...(parsed.priority === undefined ? {} : { priority: parsed.priority }),
|
||||
...(userId === undefined ? {} : { user_id: userId }),
|
||||
});
|
||||
if (outcome.state !== "accepted") {
|
||||
emit(
|
||||
@@ -957,9 +980,12 @@ async function runCommand(
|
||||
state: "empty",
|
||||
events: 0,
|
||||
invalid_lines: report.invalid_lines,
|
||||
unrecognized_lines: report.unrecognized_lines,
|
||||
})}\n`
|
||||
: renderLogText(report.events, report.invalid_lines),
|
||||
);
|
||||
// Only lines that are not JSON are damage. Records an older build wrote
|
||||
// are left out without a warning: after an upgrade they are expected.
|
||||
if (report.invalid_lines) {
|
||||
process.stderr.write(
|
||||
`${PRODUCT}: 跳过 ${report.invalid_lines} 行损坏的诊断记录。\n`,
|
||||
@@ -1582,6 +1608,7 @@ async function runCommand(
|
||||
limit: parsed.limit,
|
||||
character_budget: parsed.characterBudget,
|
||||
timeout_ms: parsed.timeoutMs,
|
||||
...userIdParameters(userId),
|
||||
})
|
||||
: ({
|
||||
schema_version: 2,
|
||||
@@ -1780,6 +1807,16 @@ async function runCommand(
|
||||
}
|
||||
if (named === "sources" && arguments_[1] !== "list") {
|
||||
const parsed = parseSourcesCommand(arguments_.slice(1));
|
||||
if (
|
||||
!parsed &&
|
||||
arguments_.some((item) => item === "--user-id" || item === "--root")
|
||||
) {
|
||||
return reject(
|
||||
descriptor,
|
||||
asJson,
|
||||
"--user-id 和 --root 只能用于接入单个 hermes 来源(Hermes profile),用户 ID 只能是 1–64 位字母、数字、点、下划线或连字符。",
|
||||
);
|
||||
}
|
||||
if (!parsed) return usage();
|
||||
if (parsed.kind === "receipt") {
|
||||
const connected = await connectRuntime(environment);
|
||||
@@ -2004,7 +2041,11 @@ async function runCommand(
|
||||
);
|
||||
return connected.exitCode;
|
||||
}
|
||||
const outcome = await connected.reader<unknown>("/api/v2/user-profile");
|
||||
const profileUser = userIdParameters(userId);
|
||||
const outcome =
|
||||
profileUser.user_id === undefined
|
||||
? await connected.reader<unknown>("/api/v2/user-profile")
|
||||
: await connected.reader<unknown>("/api/v2/user-profile", profileUser);
|
||||
const value =
|
||||
outcome.state === "ok"
|
||||
? (outcome.data as {
|
||||
@@ -2109,7 +2150,7 @@ async function runCommand(
|
||||
}
|
||||
const { reader, operations } = connected;
|
||||
if (parsed.kind === "list") {
|
||||
const document = await readMemoryPage(reader, parsed.query);
|
||||
const document = await readMemoryPage(reader, parsed.query, userId);
|
||||
emit(
|
||||
asJson
|
||||
? `${JSON.stringify(document)}\n`
|
||||
@@ -2122,6 +2163,7 @@ async function runCommand(
|
||||
reader,
|
||||
parsed.memoryId,
|
||||
parsed.namespace,
|
||||
userId,
|
||||
);
|
||||
emit(
|
||||
asJson
|
||||
@@ -2133,8 +2175,8 @@ async function runCommand(
|
||||
if (parsed.kind === "add" || parsed.kind === "add-status") {
|
||||
const document =
|
||||
parsed.kind === "add"
|
||||
? await addMemory(operations, text ?? "", parsed.requestId)
|
||||
: await readMemoryAddStatus(reader, parsed.requestId);
|
||||
? await addMemory(operations, text ?? "", parsed.requestId, userId)
|
||||
: await readMemoryAddStatus(reader, parsed.requestId, userId);
|
||||
emit(
|
||||
asJson
|
||||
? `${JSON.stringify(document)}\n`
|
||||
@@ -2149,9 +2191,13 @@ async function runCommand(
|
||||
content: text,
|
||||
...namespaceParameters(parsed.namespace),
|
||||
...(parsed.task === undefined ? {} : { task: parsed.task }),
|
||||
...userIdParameters(userId),
|
||||
})
|
||||
: await operations.delete<unknown>(
|
||||
withQuery(memoryPath, namespaceParameters(parsed.namespace)),
|
||||
withQuery(memoryPath, {
|
||||
...namespaceParameters(parsed.namespace),
|
||||
...userIdParameters(userId),
|
||||
}),
|
||||
);
|
||||
const document = {
|
||||
schema_version: 2,
|
||||
@@ -3675,13 +3721,21 @@ async function connectSources(
|
||||
asJson,
|
||||
);
|
||||
}
|
||||
if (parsed.importHistory) {
|
||||
// Another user's Hermes profile is granted and imported in one connect
|
||||
// request: only that request can carry a user and a root.
|
||||
if (
|
||||
parsed.importHistory ||
|
||||
parsed.userId !== undefined ||
|
||||
parsed.root !== undefined
|
||||
) {
|
||||
const document = await connectSourcesWithImport(reader, operations, {
|
||||
sourceIds: parsed.sourceIds,
|
||||
confirmOverrides: parsed.confirmOverrides,
|
||||
...(parsed.requestId === undefined
|
||||
? {}
|
||||
: { requestId: parsed.requestId }),
|
||||
...(parsed.userId === undefined ? {} : { userId: parsed.userId }),
|
||||
...(parsed.root === undefined ? {} : { root: parsed.root }),
|
||||
});
|
||||
emitFlowDocument(document, asJson);
|
||||
return flowExitCode(document);
|
||||
@@ -5131,14 +5185,23 @@ type SourcesCommand =
|
||||
importHistory: boolean;
|
||||
confirmOverrides: boolean;
|
||||
requestId?: string;
|
||||
/** `--user-id`: whose grant (Hermes only). Absent for `local`. */
|
||||
userId?: string;
|
||||
/** `--root`: an absolute root other than the candidate (Hermes only). */
|
||||
root?: string;
|
||||
}
|
||||
| { kind: "all"; importHistory: boolean; confirmOverrides: boolean }
|
||||
| { kind: "receipt"; requestId: string };
|
||||
|
||||
const SOURCE_ID_SHAPE = /^[a-z0-9][a-z0-9-]{0,127}$/;
|
||||
|
||||
/** Only Hermes keeps one history per profile, so only Hermes may be
|
||||
* connected for another user or from another root. */
|
||||
const MULTI_USER_SOURCES = new Set(["hermes"]);
|
||||
|
||||
/** `sources connect <id> [<id> ...] | --all-detected [--import]
|
||||
* [--confirm-overrides] [--request-id <uuid>]` and `sources receipt <id>`.
|
||||
* [--confirm-overrides] [--request-id <uuid>]
|
||||
* [--user-id <id>] [--root <path>]` and `sources receipt <id>`.
|
||||
*
|
||||
* Sources may be named positionally or with repeated `--source` (the old
|
||||
* `init --source` spelling is rewritten to this command). */
|
||||
@@ -5154,6 +5217,8 @@ function parseSourcesCommand(rest: readonly string[]): SourcesCommand | null {
|
||||
const sourceIds: string[] = [];
|
||||
const switches = new Set<string>();
|
||||
let requestId: string | undefined;
|
||||
let userId: string | undefined;
|
||||
let root: string | undefined;
|
||||
for (let index = 1; index < tokens.length; index += 1) {
|
||||
const token = tokens[index];
|
||||
if (["--import", "--confirm-overrides", "--all-detected"].includes(token)) {
|
||||
@@ -5161,6 +5226,21 @@ function parseSourcesCommand(rest: readonly string[]): SourcesCommand | null {
|
||||
switches.add(token);
|
||||
continue;
|
||||
}
|
||||
if (token === "--user-id" || token === "--root") {
|
||||
const value = tokens[index + 1];
|
||||
if (value === undefined || value.startsWith("--")) return null;
|
||||
if (token === "--user-id") {
|
||||
if (userId !== undefined || !isUserId(value)) return null;
|
||||
userId = value;
|
||||
} else {
|
||||
if (root !== undefined) return null;
|
||||
const expanded = expandRootPath(value);
|
||||
if (expanded === null) return null;
|
||||
root = expanded;
|
||||
}
|
||||
index += 1;
|
||||
continue;
|
||||
}
|
||||
if (token === "--source" || token === "--request-id") {
|
||||
const value = tokens[index + 1];
|
||||
if (value === undefined || value.startsWith("--")) return null;
|
||||
@@ -5184,10 +5264,23 @@ function parseSourcesCommand(rest: readonly string[]): SourcesCommand | null {
|
||||
const confirmOverrides = switches.has("--confirm-overrides");
|
||||
if (
|
||||
requestId !== undefined &&
|
||||
(!OPERATION_ID.test(requestId) || !importHistory)
|
||||
(!OPERATION_ID.test(requestId) ||
|
||||
!(importHistory || userId !== undefined || root !== undefined))
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
// `--user-id local` is the default and changes nothing.
|
||||
if (userId === DEFAULT_USER_ID) userId = undefined;
|
||||
if (userId !== undefined || root !== undefined) {
|
||||
// One Hermes profile per connect: the user and root name one grant.
|
||||
if (
|
||||
switches.has("--all-detected") ||
|
||||
sourceIds.length !== 1 ||
|
||||
!MULTI_USER_SOURCES.has(sourceIds[0])
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
if (switches.has("--all-detected")) {
|
||||
// One names exact sources, the other means "whatever is detected".
|
||||
if (sourceIds.length > 0 || requestId !== undefined) return null;
|
||||
@@ -5200,9 +5293,24 @@ function parseSourcesCommand(rest: readonly string[]): SourcesCommand | null {
|
||||
importHistory,
|
||||
confirmOverrides,
|
||||
...(requestId === undefined ? {} : { requestId }),
|
||||
...(userId === undefined ? {} : { userId }),
|
||||
...(root === undefined ? {} : { root }),
|
||||
};
|
||||
}
|
||||
|
||||
/** `--root` as an absolute path: a leading `~` is the home directory, a
|
||||
* relative path is taken from the working directory. Core expands nothing. */
|
||||
function expandRootPath(value: string): string | null {
|
||||
if (value.length === 0 || /[\0\r\n]/.test(value)) return null;
|
||||
const expanded =
|
||||
value === "~"
|
||||
? os.homedir()
|
||||
: value.startsWith("~/")
|
||||
? path.join(os.homedir(), value.slice(2))
|
||||
: value;
|
||||
return path.resolve(expanded);
|
||||
}
|
||||
|
||||
function parseGraphWidthCommand(
|
||||
rest: readonly string[],
|
||||
): { decision: GraphWidthDecision; confirmed: boolean } | null {
|
||||
@@ -6596,6 +6704,30 @@ const OWN_DATA_DIR_COMMANDS = new Set([
|
||||
|
||||
/** Remove one `--data-dir <absolute path>` from argv; null when it is
|
||||
* repeated, missing its value, or not an absolute path. */
|
||||
/** Memory commands that honour the global `--user-id`. */
|
||||
const USER_ID_COMMANDS = new Set(["memories", "recall", "profile", "write"]);
|
||||
|
||||
/**
|
||||
* Take `--user-id <id>` out of the arguments. The default user (`local`) is
|
||||
* returned as undefined, so a default request names no user at all and stays
|
||||
* byte-identical to one sent before multi-user support.
|
||||
*/
|
||||
function liftUserIdOption(
|
||||
arguments_: readonly string[],
|
||||
): { arguments_: string[]; userId?: string } | null {
|
||||
const index = arguments_.indexOf("--user-id");
|
||||
if (index === -1) return { arguments_: [...arguments_] };
|
||||
if (arguments_.indexOf("--user-id", index + 1) !== -1) return null;
|
||||
const value = arguments_[index + 1];
|
||||
if (value === undefined || value.startsWith("--") || !isUserId(value)) {
|
||||
return null;
|
||||
}
|
||||
return {
|
||||
arguments_: [...arguments_.slice(0, index), ...arguments_.slice(index + 2)],
|
||||
...(value === DEFAULT_USER_ID ? {} : { userId: value }),
|
||||
};
|
||||
}
|
||||
|
||||
function liftDataDirOption(
|
||||
arguments_: readonly string[],
|
||||
): { arguments_: string[]; dataDir?: string } | null {
|
||||
|
||||
@@ -68,6 +68,12 @@ const JSON_OPTION: CommandOption = {
|
||||
flag: "--json",
|
||||
summary: "以 JSON 输出到 stdout,供脚本与 Agent 消费。",
|
||||
};
|
||||
/** Global to the memory commands: whose memories are read or written. */
|
||||
const USER_ID_OPTION: CommandOption = {
|
||||
flag: "--user-id <id>",
|
||||
summary:
|
||||
"读写哪个用户的记忆,默认 local(App 用的就是 local)。1–64 位字母、数字、点、下划线或连字符;其他用户的记忆来自用 sources connect hermes --user-id 接入的 Hermes profile。",
|
||||
};
|
||||
const WATCH_OPTION: CommandOption = {
|
||||
flag: "--watch",
|
||||
summary: "串行持续刷新;Ctrl-C 只停止观察,不停止 Runtime。",
|
||||
@@ -252,15 +258,25 @@ export const COMMANDS: readonly CommandDescriptor[] = [
|
||||
{
|
||||
name: "sources",
|
||||
summary:
|
||||
"列出来源,直接接入来源,或按 request id 查接入回执。connect 不加 --import 只授权(处理保持冻结:不导入历史、不扫描新会话);加 --import 同时导入历史并开启持续同步,返回 request id。目录来自环境变量覆盖的来源必须加 --confirm-overrides。",
|
||||
"列出来源,直接接入来源,或按 request id 查接入回执。connect 不加 --import 只授权(处理保持冻结:不导入历史、不扫描新会话);加 --import 同时导入历史并开启持续同步,返回 request id。目录来自环境变量覆盖的来源必须加 --confirm-overrides。Hermes 的每个 profile 是一个独立用户:用 --user-id 和 --root 接入,授权并导入历史(会自动排除 ~/.hermes/profiles/**,各用户互不读取)。",
|
||||
usage:
|
||||
"memhub sources list [--json]\n memhub sources connect <id> [<id> ...] [--import] [--confirm-overrides] [--request-id <uuid>] [--json]\n memhub sources connect --all-detected [--import] [--confirm-overrides] [--json]\n memhub sources receipt <request-id> [--json]",
|
||||
"memhub sources list [--json]\n memhub sources connect <id> [<id> ...] [--import] [--confirm-overrides] [--request-id <uuid>] [--json]\n memhub sources connect hermes --user-id <id> --root <path> [--import] [--request-id <uuid>] [--json]\n memhub sources connect --all-detected [--import] [--confirm-overrides] [--json]\n memhub sources receipt <request-id> [--json]",
|
||||
options: [
|
||||
{
|
||||
flag: "--import",
|
||||
summary:
|
||||
"接入后立即导入历史会话并开启持续同步;不加时只授权,处理保持冻结。",
|
||||
},
|
||||
{
|
||||
flag: "--user-id <id>",
|
||||
summary:
|
||||
"只用于 hermes:这条接入属于哪个用户(默认 local)。和 --root 一起接入一个 Hermes profile,例如 --user-id wife --root ~/.hermes/profiles/wife;其他来源只能是 local。",
|
||||
},
|
||||
{
|
||||
flag: "--root <path>",
|
||||
summary:
|
||||
"只用于 hermes:读取的根目录(~ 会展开为主目录)。默认是 Hermes 自己的目录 ~/.hermes。",
|
||||
},
|
||||
{
|
||||
flag: "--all-detected",
|
||||
summary:
|
||||
@@ -373,7 +389,7 @@ export const COMMANDS: readonly CommandDescriptor[] = [
|
||||
summary:
|
||||
"列出、搜索、查看、添加、修改或物理删除记忆(对话、工作、编码三个场景)。add 和 App 的「添加记忆」是同一个入口;delete 不可逆,需 --yes。",
|
||||
usage:
|
||||
"memhub memories list [--scene <all|chat|working|coding>] [--source <id>] [--query <text>] [--status <state>] [--offset <n>] [--limit <n>] [--json]\n memhub memories show <memory-id> [--kind <kind>] [--variant <variant>] [--json]\n memhub memories add <text> [--request-id <uuid>] [--json]\n memhub memories add --content-file <path> [--request-id <uuid>] [--json]\n memhub memories add-status <request-id> [--json]\n memhub memories update <memory-id> --content-file <path> [--kind <kind>] [--variant <variant>] [--task <text>] [--json]\n memhub memories delete <memory-id> [--kind <kind>] [--variant <variant>] [--yes] [--json]",
|
||||
"memhub memories list [--scene <all|chat|working|coding>] [--source <id>] [--query <text>] [--status <state>] [--offset <n>] [--limit <n>] [--user-id <id>] [--json]\n memhub memories show <memory-id> [--kind <kind>] [--variant <variant>] [--user-id <id>] [--json]\n memhub memories add <text> [--request-id <uuid>] [--user-id <id>] [--json]\n memhub memories add --content-file <path> [--request-id <uuid>] [--user-id <id>] [--json]\n memhub memories add-status <request-id> [--user-id <id>] [--json]\n memhub memories update <memory-id> --content-file <path> [--kind <kind>] [--variant <variant>] [--task <text>] [--user-id <id>] [--json]\n memhub memories delete <memory-id> [--kind <kind>] [--variant <variant>] [--yes] [--user-id <id>] [--json]",
|
||||
options: [
|
||||
{
|
||||
flag: "--scene <all|chat|working|coding>",
|
||||
@@ -411,6 +427,7 @@ export const COMMANDS: readonly CommandDescriptor[] = [
|
||||
"add 的幂等键;用同一个 id 重试只会读回第一次的结果,不会重复添加。不给时自动生成并输出。",
|
||||
},
|
||||
DESTRUCTIVE_CONFIRM_OPTION,
|
||||
USER_ID_OPTION,
|
||||
JSON_OPTION,
|
||||
],
|
||||
exit_codes: RUNTIME_EXITS,
|
||||
@@ -418,8 +435,8 @@ export const COMMANDS: readonly CommandDescriptor[] = [
|
||||
{
|
||||
name: "profile",
|
||||
summary: "读取 SDK 组装后的用户长期画像(L0 + 已启用的长期画像补充)。",
|
||||
usage: "memhub profile [--json]",
|
||||
options: [JSON_OPTION],
|
||||
usage: "memhub profile [--user-id <id>] [--json]",
|
||||
options: [USER_ID_OPTION, JSON_OPTION],
|
||||
exit_codes: RUNTIME_EXITS,
|
||||
},
|
||||
{
|
||||
@@ -475,7 +492,7 @@ export const COMMANDS: readonly CommandDescriptor[] = [
|
||||
summary:
|
||||
"从原始对话与已沉淀记忆统一检索上下文;本地只读,不调用模型、不触发补索引。文本输出标明每条是「原始对话摘录」还是「记忆」(JSON 里是 kind: raw|memory)。",
|
||||
usage:
|
||||
"memhub recall <query> [--kind <raw|memory>] [--source <id>] [--limit <n>] [--character-budget <n>] [--timeout-ms <n>] [--json]\n memhub recall --query-file <path> [...]\n memhub recall --stdin [...]",
|
||||
"memhub recall <query> [--kind <raw|memory>] [--source <id>] [--limit <n>] [--character-budget <n>] [--timeout-ms <n>] [--user-id <id>] [--json]\n memhub recall --query-file <path> [...]\n memhub recall --stdin [...]",
|
||||
options: [
|
||||
{
|
||||
flag: "--query-file <path>",
|
||||
@@ -502,6 +519,7 @@ export const COMMANDS: readonly CommandDescriptor[] = [
|
||||
flag: "--timeout-ms <n>",
|
||||
summary: "Core 查询时限 50..30000 毫秒,默认 2000。",
|
||||
},
|
||||
USER_ID_OPTION,
|
||||
JSON_OPTION,
|
||||
],
|
||||
exit_codes: RUNTIME_EXITS,
|
||||
@@ -650,7 +668,7 @@ export const COMMANDS: readonly CommandDescriptor[] = [
|
||||
summary:
|
||||
"提交一段 OpenAI 格式的对话(messages)到写入队列,异步加工成记忆;或按 request id 查询状态。request id 是幂等键:不给时自动生成并输出,重试请带上同一个。新增一条结论式记忆请用 memories add。",
|
||||
usage:
|
||||
"memhub write submit --messages-file <path> --session <id> [--source <id>] [--request-id <id>] [--memory-at <RFC3339>] [--live] [--json]\n memhub write status <request-id> [--json]",
|
||||
"memhub write submit --messages-file <path> --session <id> [--source <id>] [--request-id <id>] [--memory-at <RFC3339>] [--live] [--user-id <id>] [--json]\n memhub write status <request-id> [--user-id <id>] [--json]",
|
||||
options: [
|
||||
{
|
||||
flag: "--messages-file <path>",
|
||||
@@ -666,6 +684,7 @@ export const COMMANDS: readonly CommandDescriptor[] = [
|
||||
},
|
||||
{ flag: "--memory-at <RFC3339>", summary: "对话发生时间,必须带时区。" },
|
||||
{ flag: "--live", summary: "优先处理。" },
|
||||
USER_ID_OPTION,
|
||||
JSON_OPTION,
|
||||
],
|
||||
exit_codes: RUNTIME_EXITS,
|
||||
|
||||
@@ -10,6 +10,8 @@ export interface ContextSearchInput {
|
||||
limit: number;
|
||||
character_budget: number;
|
||||
timeout_ms: number;
|
||||
/** Whose memories to search. Omitted for the default user (`local`). */
|
||||
user_id?: string;
|
||||
}
|
||||
|
||||
/** The CLI's JSON document mirrors Core's SDK-native context schema v2. */
|
||||
|
||||
@@ -12,6 +12,7 @@ const READ_ONLY_PATHS = [
|
||||
"/api/v1/live-sync",
|
||||
"/api/v1/memory-processing",
|
||||
"/api/v1/model-profiles",
|
||||
"/api/v1/source-grants",
|
||||
"/api/v1/sources",
|
||||
"/api/v2/home-summary",
|
||||
"/api/v2/memory-library",
|
||||
@@ -310,6 +311,8 @@ export async function submitMemoryWrite(
|
||||
messages: readonly OpenAIMessage[];
|
||||
memory_at?: string;
|
||||
priority?: "live" | "normal";
|
||||
/** Whose memory this is. Omitted for the default user (`local`). */
|
||||
user_id?: string;
|
||||
},
|
||||
): Promise<MemoryWriteOutcome> {
|
||||
let response: { status: number; data: unknown };
|
||||
@@ -327,6 +330,9 @@ export async function submitMemoryWrite(
|
||||
...(request.priority === undefined
|
||||
? {}
|
||||
: { priority: request.priority }),
|
||||
...(request.user_id === undefined || request.user_id === "local"
|
||||
? {}
|
||||
: { user_id: request.user_id }),
|
||||
},
|
||||
{
|
||||
headers: { Authorization: `Bearer ${attachment.apiToken}` },
|
||||
@@ -469,11 +475,16 @@ export async function deleteModelProfile(
|
||||
export async function readMemoryWrite(
|
||||
attachment: RuntimeAttachmentLike,
|
||||
requestId: string,
|
||||
/** A receipt belongs to the user that submitted it; omit for `local`. */
|
||||
userId?: string,
|
||||
): Promise<MemoryWriteReceipt | null> {
|
||||
try {
|
||||
const response = await axios.get<MemoryWriteReceipt>(
|
||||
`${baseUrlOf(attachment)}/api/v2/memory-writes/${encodeURIComponent(requestId)}`,
|
||||
{
|
||||
...(userId === undefined || userId === "local"
|
||||
? {}
|
||||
: { params: { user_id: userId } }),
|
||||
headers: { Authorization: `Bearer ${attachment.apiToken}` },
|
||||
timeout: 10_000,
|
||||
proxy: false,
|
||||
|
||||
@@ -6,9 +6,10 @@ const WRITE_PATHS = [
|
||||
] as const;
|
||||
/** `POST /api/v2/memories` adds one memory (the app's 添加记忆). */
|
||||
const MEMORY_ADD_PATH = "/api/v2/memories";
|
||||
/** One SDK memory; delete names its namespace in the query string. */
|
||||
/** One SDK memory; delete names its namespace, and a non-default user, in
|
||||
* the query string. */
|
||||
const SDK_MEMORY_PATH =
|
||||
/^\/api\/v2\/memories\/[A-Za-z0-9][\w.-]{0,127}(?:\?kind=(?:chat|experience|fact)(?:&variant=(?:working|coding))?)?$/;
|
||||
/^\/api\/v2\/memories\/[A-Za-z0-9][\w.-]{0,127}(?:\?(?:kind=(?:chat|experience|fact)(?:&variant=(?:working|coding))?(?:&user_id=[A-Za-z0-9._-]{1,64})?|user_id=[A-Za-z0-9._-]{1,64}))?$/;
|
||||
const MODEL_PROBE_PATH =
|
||||
/^\/api\/v1\/model-profiles\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\/probe$/;
|
||||
const MODEL_PROFILE_PATH =
|
||||
|
||||
@@ -110,9 +110,18 @@ export async function connectSourcesWithImport(
|
||||
sourceIds: readonly string[];
|
||||
confirmOverrides: boolean;
|
||||
requestId?: string;
|
||||
/** Whose grant this is. Omitted for the default user (`local`). */
|
||||
userId?: string;
|
||||
/** An absolute root other than the source's candidate (a Hermes
|
||||
* profile). Core reads its identity itself, so the source's candidate
|
||||
* root id and revision are not sent for it. */
|
||||
root?: string;
|
||||
},
|
||||
): Promise<FlowDocument> {
|
||||
const sourceIds = input.sourceIds;
|
||||
// A second user's grant is not a first run, and a custom root has no
|
||||
// candidate the app showed: both are a plain connect.
|
||||
const profile = input.root !== undefined || input.userId !== undefined;
|
||||
if (
|
||||
sourceIds.length < 1 ||
|
||||
sourceIds.length > 32 ||
|
||||
@@ -134,9 +143,14 @@ export async function connectSourcesWithImport(
|
||||
return invalid("source_unavailable");
|
||||
}
|
||||
const items = selected.map(([candidate]) => candidate);
|
||||
const needsOverride = items
|
||||
.filter((item) => item.requires_override_confirmation)
|
||||
.map((item) => item.source_id);
|
||||
// A custom root is the caller's own choice, not the environment override
|
||||
// the confirmation exists for.
|
||||
const needsOverride =
|
||||
input.root !== undefined
|
||||
? []
|
||||
: items
|
||||
.filter((item) => item.requires_override_confirmation)
|
||||
.map((item) => item.source_id);
|
||||
if (needsOverride.length > 0 && !input.confirmOverrides) {
|
||||
return {
|
||||
schema_version: 1,
|
||||
@@ -154,18 +168,22 @@ export async function connectSourcesWithImport(
|
||||
request = buildSourceImportRequest({
|
||||
sourceIds: [...sourceIds],
|
||||
sourceRoots: Object.fromEntries(
|
||||
items.map((item) => [item.source_id, item.candidate_root_id]),
|
||||
items.flatMap((item) =>
|
||||
input.root === undefined
|
||||
? [[item.source_id, item.candidate_root_id]]
|
||||
: [],
|
||||
),
|
||||
),
|
||||
sourceCandidateRevisions: Object.fromEntries(
|
||||
items.flatMap((item) =>
|
||||
item.candidate_revision === undefined
|
||||
item.candidate_revision === undefined || input.root !== undefined
|
||||
? []
|
||||
: [[item.source_id, item.candidate_revision]],
|
||||
),
|
||||
),
|
||||
confirmEnvironmentOverrides: needsOverride.length > 0,
|
||||
fullRescan: false,
|
||||
onboarding: true,
|
||||
onboarding: !profile,
|
||||
enableSync: true,
|
||||
requestId,
|
||||
});
|
||||
@@ -175,7 +193,11 @@ export async function connectSourcesWithImport(
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
const result = await operations.post<unknown>(request.path, request.body);
|
||||
const result = await operations.post<unknown>(request.path, {
|
||||
...request.body,
|
||||
...(input.userId === undefined ? {} : { user_id: input.userId }),
|
||||
...(input.root === undefined ? {} : { root: input.root }),
|
||||
});
|
||||
if (result.state === "rejected") {
|
||||
return rejected(requestId, result.code);
|
||||
}
|
||||
|
||||
@@ -107,7 +107,11 @@ export async function readLogReport(
|
||||
query: LogQuery,
|
||||
): Promise<{
|
||||
events: DiagnosticEventV1[];
|
||||
/** Lines that are not JSON: the only ones `memhub logs` calls damaged. */
|
||||
invalid_lines: number;
|
||||
/** Whole JSON lines the reader leaves out, such as an older build's
|
||||
* records. Nothing is wrong with them, so the text output says nothing. */
|
||||
unrecognized_lines: number;
|
||||
failure?: "log_directory_unreadable";
|
||||
}> {
|
||||
// `readDiagnosticEvents` treats an unreadable logs path as "no events". That
|
||||
@@ -119,6 +123,7 @@ export async function readLogReport(
|
||||
return {
|
||||
events: [],
|
||||
invalid_lines: 0,
|
||||
unrecognized_lines: 0,
|
||||
failure: "log_directory_unreadable",
|
||||
};
|
||||
}
|
||||
@@ -129,6 +134,7 @@ export async function readLogReport(
|
||||
return {
|
||||
events: [],
|
||||
invalid_lines: 0,
|
||||
unrecognized_lines: 0,
|
||||
failure: "log_directory_unreadable",
|
||||
};
|
||||
}
|
||||
|
||||
@@ -14,6 +14,25 @@ import { type CoreReader, RESERVED_MEMORY_SEGMENTS } from "./core-client.js";
|
||||
import type { CoreOperations } from "./core-operations.js";
|
||||
import { PRODUCT } from "./version.js";
|
||||
|
||||
/** A MemHub user id, the same shape Core accepts. `local` is the default. */
|
||||
const USER_ID = /^[A-Za-z0-9._-]{1,64}$/;
|
||||
export const DEFAULT_USER_ID = "local";
|
||||
|
||||
export function isUserId(value: string): boolean {
|
||||
return USER_ID.test(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* The `user_id` to send, or nothing. The default user is never named on the
|
||||
* wire, so a request without `--user-id` stays exactly what it was before
|
||||
* multi-user support and still works against an older Core.
|
||||
*/
|
||||
export function userIdParameters(userId?: string): { user_id?: string } {
|
||||
return userId === undefined || userId === DEFAULT_USER_ID
|
||||
? {}
|
||||
: { user_id: userId };
|
||||
}
|
||||
|
||||
/** Output budget: a long memory is shortened visibly, never dropped silently. */
|
||||
const CONTENT_BUDGET = 240;
|
||||
|
||||
@@ -101,10 +120,12 @@ export interface MemoryDetailDocument {
|
||||
export async function readMemoryPage(
|
||||
reader: CoreReader,
|
||||
query: MemoryQuery,
|
||||
userId?: string,
|
||||
): Promise<MemoryPageDocument> {
|
||||
const parameters: Record<string, string | number> = {
|
||||
offset: query.offset,
|
||||
limit: query.limit,
|
||||
...userIdParameters(userId),
|
||||
};
|
||||
if (query.status !== undefined) parameters.statuses = query.status;
|
||||
if (query.source !== undefined) parameters.source = query.source;
|
||||
@@ -156,8 +177,12 @@ export async function readMemoryDetail(
|
||||
reader: CoreReader,
|
||||
memoryId: string,
|
||||
namespace: MemoryNamespace = {},
|
||||
userId?: string,
|
||||
): Promise<MemoryDetailDocument> {
|
||||
const parameters = namespaceParameters(namespace);
|
||||
const parameters = {
|
||||
...namespaceParameters(namespace),
|
||||
...userIdParameters(userId),
|
||||
};
|
||||
const outcome = await reader<unknown>(
|
||||
`/api/v2/memories/${memoryId}`,
|
||||
Object.keys(parameters).length > 0 ? parameters : undefined,
|
||||
@@ -236,6 +261,7 @@ export async function addMemory(
|
||||
operations: CoreOperations,
|
||||
text: string,
|
||||
requestId: string = randomUUID(),
|
||||
userId?: string,
|
||||
): Promise<MemoryAddDocument> {
|
||||
const base = {
|
||||
schema_version: 2 as const,
|
||||
@@ -259,7 +285,10 @@ export async function addMemory(
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
const result = await operations.post<unknown>(request.path, request.body);
|
||||
const result = await operations.post<unknown>(request.path, {
|
||||
...request.body,
|
||||
...userIdParameters(userId),
|
||||
});
|
||||
if (result.state !== "committed") {
|
||||
return { ...base, state: result.state, code: result.code };
|
||||
}
|
||||
@@ -273,6 +302,7 @@ export async function addMemory(
|
||||
export async function readMemoryAddStatus(
|
||||
reader: CoreReader,
|
||||
requestId: string,
|
||||
userId?: string,
|
||||
): Promise<MemoryAddDocument> {
|
||||
const base = {
|
||||
schema_version: 2 as const,
|
||||
@@ -283,7 +313,11 @@ export async function readMemoryAddStatus(
|
||||
if (!UUID.test(requestId)) {
|
||||
return { ...base, state: "invalid", code: "invalid_request_id" };
|
||||
}
|
||||
const outcome = await reader<unknown>(memoryAddStatusPath(requestId));
|
||||
const user = userIdParameters(userId);
|
||||
const outcome =
|
||||
user.user_id === undefined
|
||||
? await reader<unknown>(memoryAddStatusPath(requestId))
|
||||
: await reader<unknown>(memoryAddStatusPath(requestId), user);
|
||||
if (outcome.state === "not_found") {
|
||||
return { ...base, state: "not_found", code: "memory_add_not_found" };
|
||||
}
|
||||
|
||||
@@ -38,6 +38,21 @@ export interface SourcesDocument {
|
||||
state: ReadState["state"];
|
||||
code?: string;
|
||||
sources: Array<Record<string, unknown>>;
|
||||
/** Every grant of every user (`GET /api/v1/source-grants`). Absent when
|
||||
* Core is too old to list them; `sources` is the default user's view. */
|
||||
grants?: SourceGrant[];
|
||||
}
|
||||
|
||||
/** One source grant. Only Hermes has grants for users other than `local`,
|
||||
* one per Hermes profile root. */
|
||||
export interface SourceGrant {
|
||||
user_id: string;
|
||||
source_id: string;
|
||||
root_label: string | null;
|
||||
root_path: string | null;
|
||||
authorized: boolean;
|
||||
revoked_at: string | null;
|
||||
sync_enabled: boolean;
|
||||
}
|
||||
|
||||
export interface SyncDocument {
|
||||
@@ -86,14 +101,66 @@ export async function readSources(
|
||||
sources: [],
|
||||
};
|
||||
}
|
||||
const grants = await readSourceGrants(reader);
|
||||
return {
|
||||
schema_version: SCHEMA_VERSION,
|
||||
product: PRODUCT,
|
||||
state: "ok",
|
||||
sources: result.data.map(projectSource),
|
||||
// `/api/v1/sources` is the default user's view of every source.
|
||||
sources: result.data.map((source) => ({
|
||||
...projectSource(source),
|
||||
user_id: "local",
|
||||
})),
|
||||
...(grants === null ? {} : { grants }),
|
||||
};
|
||||
}
|
||||
|
||||
/** The grants of every user, or null when Core cannot list them (an older
|
||||
* Core answers 404): the source list is then shown as before. */
|
||||
async function readSourceGrants(
|
||||
reader: CoreReader,
|
||||
): Promise<SourceGrant[] | null> {
|
||||
let outcome: ReadOutcome<unknown>;
|
||||
try {
|
||||
outcome = await reader<unknown>("/api/v1/source-grants");
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
if (outcome.state !== "ok") return null;
|
||||
const value = outcome.data;
|
||||
if (!value || typeof value !== "object" || Array.isArray(value)) return null;
|
||||
const items = (value as { grants?: unknown }).grants;
|
||||
if (!Array.isArray(items)) return null;
|
||||
const grants: SourceGrant[] = [];
|
||||
const text = (candidate: unknown): string | null =>
|
||||
typeof candidate === "string" && candidate.length <= 4096
|
||||
? candidate
|
||||
: null;
|
||||
for (const item of items) {
|
||||
if (!item || typeof item !== "object" || Array.isArray(item)) return null;
|
||||
const grant = item as Record<string, unknown>;
|
||||
if (
|
||||
typeof grant.user_id !== "string" ||
|
||||
!/^[A-Za-z0-9._-]{1,64}$/.test(grant.user_id) ||
|
||||
typeof grant.source_id !== "string" ||
|
||||
!/^[a-z0-9][a-z0-9-]{0,127}$/.test(grant.source_id) ||
|
||||
typeof grant.authorized !== "boolean"
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
grants.push({
|
||||
user_id: grant.user_id,
|
||||
source_id: grant.source_id,
|
||||
root_label: text(grant.root_label),
|
||||
root_path: text(grant.root_path),
|
||||
authorized: grant.authorized,
|
||||
revoked_at: text(grant.revoked_at),
|
||||
sync_enabled: grant.sync_enabled === true,
|
||||
});
|
||||
}
|
||||
return grants;
|
||||
}
|
||||
|
||||
export async function readSync(reader: CoreReader): Promise<SyncDocument> {
|
||||
const result = validated(
|
||||
await reader<unknown>("/api/v1/live-sync"),
|
||||
@@ -231,6 +298,22 @@ export function renderSourcesText(document: SourcesDocument): string {
|
||||
);
|
||||
}
|
||||
if (document.sources.length === 0) lines.push("(没有来源)");
|
||||
// The list above is the default user's (`local`). Another user's grant,
|
||||
// a Hermes profile, is listed with its user so it is not mistaken for it.
|
||||
const others = (document.grants ?? []).filter(
|
||||
(grant) => grant.user_id !== "local",
|
||||
);
|
||||
if (others.length > 0) {
|
||||
lines.push("其他用户的连接:");
|
||||
for (const grant of others) {
|
||||
const name =
|
||||
document.sources.find((source) => source.source_id === grant.source_id)
|
||||
?.display_name ?? grant.source_id;
|
||||
lines.push(
|
||||
`- ${name}(用户 ${grant.user_id}):${grant.authorized ? "已连接" : grant.revoked_at ? "已撤销" : "未连接"};处理${grant.sync_enabled ? "已开启" : "已冻结"}${grant.root_path ? `;目录 ${grant.root_path}` : ""}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
lines.push("");
|
||||
return lines.join("\n");
|
||||
}
|
||||
|
||||
@@ -179,12 +179,72 @@ describe("CLI-07/10 source and synchronization reads", () => {
|
||||
sync_enabled: true,
|
||||
sync_completeness: "additions_only",
|
||||
});
|
||||
expect(requestedPaths()).toEqual(["/api/v1/sources"]);
|
||||
// The grants list answers 404 here, as an older Core does: the source
|
||||
// list is shown as before, every row the default user's.
|
||||
expect(report.sources[0].user_id).toBe("local");
|
||||
expect(report.grants).toBeUndefined();
|
||||
expect(requestedPaths()).toEqual([
|
||||
"/api/v1/sources",
|
||||
"/api/v1/source-grants",
|
||||
]);
|
||||
expect(requestedPaths().some((path) => path.includes("detect"))).toBe(
|
||||
false,
|
||||
);
|
||||
});
|
||||
|
||||
it("shows every user's grant, a Hermes profile with its user", async () => {
|
||||
const grants = {
|
||||
schema_version: 1,
|
||||
grants: [
|
||||
{
|
||||
user_id: "local",
|
||||
source_id: "codex",
|
||||
root_label: ".codex",
|
||||
root_path: "/home/synthetic/.codex",
|
||||
authorized: true,
|
||||
revoked_at: null,
|
||||
sync_enabled: true,
|
||||
},
|
||||
{
|
||||
user_id: "wife",
|
||||
source_id: "hermes",
|
||||
root_label: "wife",
|
||||
root_path: "/home/synthetic/.hermes/profiles/wife",
|
||||
authorized: true,
|
||||
revoked_at: null,
|
||||
sync_enabled: true,
|
||||
},
|
||||
],
|
||||
};
|
||||
transport.get.mockImplementation(async (url: string) =>
|
||||
url === "/api/v1/source-grants"
|
||||
? { status: 200, data: grants }
|
||||
: (RESPONSES.get(url) ?? { status: 404, data: {} }),
|
||||
);
|
||||
let streams = captureStreams();
|
||||
try {
|
||||
expect(await run(["sources", "list", "--json"], {})).toBe(0);
|
||||
} finally {
|
||||
streams.restore();
|
||||
}
|
||||
const report = JSON.parse(streams.stdout());
|
||||
expect(report.grants).toEqual(grants.grants);
|
||||
expect(report.sources[0]).toMatchObject({
|
||||
source_id: "codex",
|
||||
user_id: "local",
|
||||
});
|
||||
streams = captureStreams();
|
||||
try {
|
||||
expect(await run(["sources", "list"], {})).toBe(0);
|
||||
} finally {
|
||||
streams.restore();
|
||||
}
|
||||
expect(streams.stdout()).toContain("其他用户的连接:");
|
||||
expect(streams.stdout()).toContain(
|
||||
"hermes(用户 wife):已连接;处理已开启;目录 /home/synthetic/.hermes/profiles/wife",
|
||||
);
|
||||
});
|
||||
|
||||
it("keeps degraded synchronization distinct and prints its error code", async () => {
|
||||
const streams = captureStreams();
|
||||
try {
|
||||
@@ -552,6 +612,7 @@ describe("CLI-19 progress and issue reads", () => {
|
||||
requestedPaths().every((path) =>
|
||||
[
|
||||
"/api/v1/sources",
|
||||
"/api/v1/source-grants",
|
||||
"/api/v1/live-sync",
|
||||
"/api/v1/import-jobs",
|
||||
"/api/v1/memory-processing",
|
||||
|
||||
@@ -0,0 +1,405 @@
|
||||
import { mkdtemp, writeFile } from "node:fs/promises";
|
||||
import os, { tmpdir } from "node:os";
|
||||
import path from "node:path";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { run } from "./cli.js";
|
||||
|
||||
const transport = vi.hoisted(() => ({
|
||||
get: vi.fn(),
|
||||
post: vi.fn(),
|
||||
put: vi.fn(),
|
||||
patch: vi.fn(),
|
||||
delete: vi.fn(),
|
||||
}));
|
||||
const direct = vi.hoisted(() => ({ get: vi.fn(), post: vi.fn() }));
|
||||
vi.mock("axios", () => ({
|
||||
default: {
|
||||
create: vi.fn(() => transport),
|
||||
get: direct.get,
|
||||
post: direct.post,
|
||||
isAxiosError: () => false,
|
||||
},
|
||||
}));
|
||||
|
||||
const runtime = vi.hoisted(() => ({
|
||||
inspectRuntimeStatus: vi.fn(),
|
||||
discoverRuntimeAttachment: vi.fn(),
|
||||
}));
|
||||
vi.mock("@one-memory/runtime-control", async (importOriginal) => ({
|
||||
...(await importOriginal<typeof import("@one-memory/runtime-control")>()),
|
||||
...runtime,
|
||||
}));
|
||||
|
||||
const REQUEST = "33333333-3333-4333-8333-333333333333";
|
||||
const HERMES = {
|
||||
source_id: "hermes",
|
||||
display_name: "Hermes",
|
||||
storage_kinds: ["sqlite"],
|
||||
available: true,
|
||||
authorized: true,
|
||||
detected: null,
|
||||
root_label: ".hermes",
|
||||
candidate_path: "~/.hermes",
|
||||
candidate_kind: "default",
|
||||
candidate_revision: 0,
|
||||
candidate_root_id: "hermes-root-identity",
|
||||
imported_messages: 0,
|
||||
imported_conversations: 0,
|
||||
capabilities: {
|
||||
historical_import: "implemented",
|
||||
incremental_import: "implemented",
|
||||
live_sync: "implemented",
|
||||
recall: "implemented",
|
||||
installer: "not_implemented",
|
||||
verification: "fixture_only",
|
||||
},
|
||||
sync_completeness: "additions_only",
|
||||
location_state: "found",
|
||||
};
|
||||
const CODEX = {
|
||||
...HERMES,
|
||||
source_id: "codex",
|
||||
display_name: "Codex",
|
||||
candidate_root_id: "codex-root-identity",
|
||||
};
|
||||
|
||||
function addReceipt() {
|
||||
return {
|
||||
evidence_id: "44444444-4444-4444-8444-444444444444",
|
||||
text: "Prefers tea",
|
||||
scope: "all-apps",
|
||||
created_at: "2026-10-27T00:00:00Z",
|
||||
state: "pending",
|
||||
task_id: null,
|
||||
memory_ids: [],
|
||||
error: null,
|
||||
};
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
for (const method of [
|
||||
...Object.values(transport),
|
||||
...Object.values(direct),
|
||||
]) {
|
||||
method.mockReset();
|
||||
}
|
||||
runtime.inspectRuntimeStatus.mockResolvedValue({
|
||||
state: "running",
|
||||
code: "runtime_verified",
|
||||
instanceId: "instance-1",
|
||||
host: "127.0.0.1",
|
||||
port: 8765,
|
||||
version: "1.2.3",
|
||||
});
|
||||
runtime.discoverRuntimeAttachment.mockResolvedValue({
|
||||
instanceId: "instance-1",
|
||||
apiToken: "synthetic-runtime-token",
|
||||
host: "127.0.0.1",
|
||||
port: 8765,
|
||||
servicePid: 4242,
|
||||
health: {
|
||||
status: "ok",
|
||||
service: "one-memory-core",
|
||||
version: "1.2.3",
|
||||
instance_id: "instance-1",
|
||||
},
|
||||
});
|
||||
transport.get.mockImplementation(async (url: string) => {
|
||||
if (url === "/api/v1/sources") {
|
||||
return { status: 200, data: [HERMES, CODEX] };
|
||||
}
|
||||
if (url === "/api/v2/user-profile") {
|
||||
return {
|
||||
status: 200,
|
||||
data: { request_id: "r-1", profile: null, elapsed_ms: 1 },
|
||||
};
|
||||
}
|
||||
return { status: 404, data: {} };
|
||||
});
|
||||
transport.post.mockImplementation(
|
||||
async (url: string, body: Record<string, unknown>) =>
|
||||
url === "/api/v1/sources/connect"
|
||||
? {
|
||||
status: 200,
|
||||
data: {
|
||||
request_id: body.request_id,
|
||||
accepted_source_ids: ["hermes"],
|
||||
failed_sources: {},
|
||||
jobs: [],
|
||||
},
|
||||
}
|
||||
: url === "/api/v2/memories"
|
||||
? { status: 201, data: addReceipt() }
|
||||
: { status: 404, data: {} },
|
||||
);
|
||||
transport.put.mockResolvedValue({ status: 200, data: {} });
|
||||
transport.delete.mockResolvedValue({ status: 204, data: "" });
|
||||
direct.post.mockResolvedValue({
|
||||
status: 202,
|
||||
data: {
|
||||
request_id: REQUEST,
|
||||
state: "accepted",
|
||||
created_at: "T1",
|
||||
updated_at: "T1",
|
||||
},
|
||||
});
|
||||
direct.get.mockResolvedValue({
|
||||
data: {
|
||||
request_id: REQUEST,
|
||||
state: "accepted",
|
||||
created_at: "T1",
|
||||
updated_at: "T1",
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
describe("--user-id on the memory commands", () => {
|
||||
it("passes the user as a query parameter to memory reads and deletes", async () => {
|
||||
await capturedRun(["memories", "list", "--user-id", "wife", "--json"]);
|
||||
expect(transport.get).toHaveBeenCalledWith("/api/v2/memory-library", {
|
||||
params: { offset: 0, limit: 100, user_id: "wife" },
|
||||
});
|
||||
// Global: the flag may come before the subcommand too.
|
||||
await capturedRun(["memories", "--user-id", "wife", "show", "m-1"]);
|
||||
expect(transport.get).toHaveBeenCalledWith("/api/v2/memories/m-1", {
|
||||
params: { user_id: "wife" },
|
||||
});
|
||||
await capturedRun(["memories", "add-status", REQUEST, "--user-id", "wife"]);
|
||||
expect(transport.get).toHaveBeenCalledWith(
|
||||
`/api/v2/memories/additions/${REQUEST}`,
|
||||
{ params: { user_id: "wife" } },
|
||||
);
|
||||
await capturedRun(["profile", "--user-id", "wife", "--json"]);
|
||||
expect(transport.get).toHaveBeenCalledWith("/api/v2/user-profile", {
|
||||
params: { user_id: "wife" },
|
||||
});
|
||||
await capturedRun([
|
||||
"memories",
|
||||
"delete",
|
||||
"fact-1",
|
||||
"--kind",
|
||||
"fact",
|
||||
"--yes",
|
||||
"--user-id",
|
||||
"wife",
|
||||
]);
|
||||
expect(transport.delete).toHaveBeenCalledWith(
|
||||
"/api/v2/memories/fact-1?kind=fact&user_id=wife",
|
||||
undefined,
|
||||
);
|
||||
await capturedRun([
|
||||
"memories",
|
||||
"delete",
|
||||
"m-1",
|
||||
"--yes",
|
||||
"--user-id",
|
||||
"wife",
|
||||
]);
|
||||
expect(transport.delete).toHaveBeenCalledWith(
|
||||
"/api/v2/memories/m-1?user_id=wife",
|
||||
undefined,
|
||||
);
|
||||
await capturedRun(["write", "status", REQUEST, "--user-id", "wife"]);
|
||||
expect(direct.get.mock.calls[0][0]).toBe(
|
||||
`http://127.0.0.1:8765/api/v2/memory-writes/${REQUEST}`,
|
||||
);
|
||||
expect(direct.get.mock.calls[0][1].params).toEqual({ user_id: "wife" });
|
||||
});
|
||||
|
||||
it("passes the user in the body of memory writes and searches", async () => {
|
||||
await capturedRun(["memories", "add", "Prefers tea", "--user-id", "wife"]);
|
||||
expect(transport.post.mock.calls[0][0]).toBe("/api/v2/memories");
|
||||
expect(transport.post.mock.calls[0][1]).toMatchObject({
|
||||
text: "Prefers tea",
|
||||
scope: "all-apps",
|
||||
user_id: "wife",
|
||||
});
|
||||
const directory = await mkdtemp(path.join(tmpdir(), "om-user-id-"));
|
||||
const content = path.join(directory, "content.txt");
|
||||
await writeFile(content, "Prefers green tea\n");
|
||||
await capturedRun([
|
||||
"memories",
|
||||
"update",
|
||||
"m-1",
|
||||
"--content-file",
|
||||
content,
|
||||
"--user-id",
|
||||
"wife",
|
||||
]);
|
||||
expect(transport.put.mock.calls[0][0]).toBe("/api/v2/memories/m-1");
|
||||
expect(transport.put.mock.calls[0][1]).toMatchObject({ user_id: "wife" });
|
||||
await capturedRun(["recall", "tea", "--user-id", "wife", "--json"]);
|
||||
const search = transport.post.mock.calls.find(
|
||||
([url]) => url === "/api/v2/context/search",
|
||||
);
|
||||
expect(search?.[1]).toMatchObject({ query: "tea", user_id: "wife" });
|
||||
const messages = path.join(directory, "messages.json");
|
||||
await writeFile(
|
||||
messages,
|
||||
JSON.stringify([{ role: "user", content: "I like tea" }]),
|
||||
);
|
||||
await capturedRun([
|
||||
"write",
|
||||
"submit",
|
||||
"--messages-file",
|
||||
messages,
|
||||
"--session",
|
||||
"s-1",
|
||||
"--user-id",
|
||||
"wife",
|
||||
]);
|
||||
expect(direct.post.mock.calls[0][0]).toBe(
|
||||
"http://127.0.0.1:8765/api/v2/memory-writes",
|
||||
);
|
||||
expect(direct.post.mock.calls[0][1]).toMatchObject({
|
||||
session_id: "s-1",
|
||||
user_id: "wife",
|
||||
});
|
||||
});
|
||||
|
||||
it("names no user by default or for --user-id local", async () => {
|
||||
for (const extra of [[], ["--user-id", "local"]]) {
|
||||
transport.get.mockClear();
|
||||
transport.post.mockClear();
|
||||
await capturedRun(["memories", "list", ...extra, "--json"]);
|
||||
expect(transport.get).toHaveBeenCalledWith("/api/v2/memory-library", {
|
||||
params: { offset: 0, limit: 100 },
|
||||
});
|
||||
await capturedRun(["profile", ...extra]);
|
||||
expect(transport.get).toHaveBeenCalledWith(
|
||||
"/api/v2/user-profile",
|
||||
undefined,
|
||||
);
|
||||
await capturedRun(["memories", "add", "Prefers tea", ...extra]);
|
||||
expect(transport.post.mock.calls[0][1]).not.toHaveProperty("user_id");
|
||||
await capturedRun(["recall", "tea", ...extra, "--json"]);
|
||||
const search = transport.post.mock.calls.find(
|
||||
([url]) => url === "/api/v2/context/search",
|
||||
);
|
||||
expect(search?.[1]).not.toHaveProperty("user_id");
|
||||
}
|
||||
});
|
||||
|
||||
it("refuses a malformed user id before contacting Core", async () => {
|
||||
for (const value of ["bad:id", "", "x".repeat(65), "a b", "--json"]) {
|
||||
const result = await capturedRun([
|
||||
"memories",
|
||||
"list",
|
||||
"--user-id",
|
||||
value,
|
||||
]);
|
||||
expect(result.code).toBe(2);
|
||||
}
|
||||
expect(
|
||||
(await capturedRun(["profile", "--user-id", "a", "--user-id", "b"])).code,
|
||||
).toBe(2);
|
||||
expect((await capturedRun(["recall", "tea", "--user-id"])).code).toBe(2);
|
||||
expect(transport.get).not.toHaveBeenCalled();
|
||||
expect(transport.post).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe("sources connect for a Hermes profile", () => {
|
||||
it("connects hermes for another user from its own root", async () => {
|
||||
const result = await capturedRun([
|
||||
"sources",
|
||||
"connect",
|
||||
"hermes",
|
||||
"--user-id",
|
||||
"wife",
|
||||
"--root",
|
||||
"~/.hermes/profiles/wife",
|
||||
"--json",
|
||||
]);
|
||||
expect(result.code).toBe(0);
|
||||
expect(transport.post).toHaveBeenCalledTimes(1);
|
||||
const [url, body] = transport.post.mock.calls[0];
|
||||
expect(url).toBe("/api/v1/sources/connect");
|
||||
expect(body).toMatchObject({
|
||||
source_ids: ["hermes"],
|
||||
// Core reads the custom root's identity itself.
|
||||
source_roots: {},
|
||||
source_candidate_revisions: {},
|
||||
onboarding: false,
|
||||
user_id: "wife",
|
||||
root: path.join(os.homedir(), ".hermes/profiles/wife"),
|
||||
});
|
||||
});
|
||||
|
||||
it("keeps a plain connect unchanged and accepts --import with a profile", async () => {
|
||||
await capturedRun(["sources", "connect", "hermes", "--import", "--json"]);
|
||||
const plain = transport.post.mock.calls[0][1];
|
||||
expect(plain).not.toHaveProperty("user_id");
|
||||
expect(plain).not.toHaveProperty("root");
|
||||
expect(plain).toMatchObject({
|
||||
source_roots: { hermes: "hermes-root-identity" },
|
||||
onboarding: true,
|
||||
});
|
||||
transport.post.mockClear();
|
||||
const result = await capturedRun([
|
||||
"sources",
|
||||
"connect",
|
||||
"hermes",
|
||||
"--import",
|
||||
"--user-id",
|
||||
"wife",
|
||||
"--root",
|
||||
"/profiles/wife",
|
||||
"--request-id",
|
||||
REQUEST,
|
||||
"--json",
|
||||
]);
|
||||
expect(result.code).toBe(0);
|
||||
expect(transport.post.mock.calls[0][1]).toMatchObject({
|
||||
request_id: REQUEST,
|
||||
user_id: "wife",
|
||||
root: "/profiles/wife",
|
||||
});
|
||||
});
|
||||
|
||||
it("refuses --user-id and --root for any source but hermes", async () => {
|
||||
for (const argv of [
|
||||
["sources", "connect", "codex", "--user-id", "wife"],
|
||||
["sources", "connect", "codex", "--root", "/somewhere", "--import"],
|
||||
["sources", "connect", "hermes", "codex", "--user-id", "wife"],
|
||||
["sources", "connect", "--all-detected", "--user-id", "wife"],
|
||||
["sources", "connect", "hermes", "--user-id", "bad:id"],
|
||||
]) {
|
||||
const result = await capturedRun(argv);
|
||||
expect(result.code).toBe(2);
|
||||
expect(result.stderr).toContain("hermes");
|
||||
}
|
||||
expect(transport.post).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
async function capturedRun(arguments_: string[]) {
|
||||
let stdout = "";
|
||||
let stderr = "";
|
||||
const stdoutSpy = vi
|
||||
.spyOn(process.stdout, "write")
|
||||
.mockImplementation((chunk) => {
|
||||
stdout += String(chunk);
|
||||
return true;
|
||||
});
|
||||
const stderrSpy = vi
|
||||
.spyOn(process.stderr, "write")
|
||||
.mockImplementation((chunk) => {
|
||||
stderr += String(chunk);
|
||||
return true;
|
||||
});
|
||||
try {
|
||||
return {
|
||||
code: await run(arguments_, {
|
||||
ONE_MEMORY_DATA_DIR: "/synthetic/data",
|
||||
}),
|
||||
stdout,
|
||||
stderr,
|
||||
};
|
||||
} finally {
|
||||
stdoutSpy.mockRestore();
|
||||
stderrSpy.mockRestore();
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,7 @@ dependencies = [
|
||||
"fastapi>=0.141.1",
|
||||
"filelock>=3.32.4",
|
||||
# Public SDK version validated against MemHub's permission and recall contracts.
|
||||
"hy-memory==2.0.6",
|
||||
"hy-memory==2.0.9",
|
||||
# chromadb arrives through the HY SDK; locked here so an unrelated upgrade
|
||||
# cannot move the SDK's store. Built-in embedding consumes numpy directly.
|
||||
"chromadb==1.5.9",
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -5,6 +5,8 @@ from typing import Annotated, Literal
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, Field, model_validator
|
||||
|
||||
from one_memory.user_scope import DEFAULT_USER_ID, UserId
|
||||
|
||||
ContextKind = Literal["raw", "memory"]
|
||||
ContextSearchChannel = Literal["lexical", "semantic"]
|
||||
ContextSemanticStatus = Literal["not_searched", "completed", "failed", "timed_out"]
|
||||
@@ -58,6 +60,8 @@ class SdkContextSearchRequest(ContextSearchRequest):
|
||||
limit: int = Field(default=10, ge=1, le=20, strict=True)
|
||||
schema_limit: int = Field(default=3, ge=0, le=20, strict=True)
|
||||
intention_limit: int = Field(default=1, ge=0, le=20, strict=True)
|
||||
#: Whose memories to search. The App never sends it and reads `local`.
|
||||
user_id: UserId = DEFAULT_USER_ID
|
||||
|
||||
|
||||
class ContextMessage(BaseModel):
|
||||
|
||||
@@ -28,6 +28,7 @@ from one_memory.hy_sdk_types import (
|
||||
parse_sdk_search,
|
||||
)
|
||||
from one_memory.imports.storage import EvidenceStore
|
||||
from one_memory.user_scope import owns
|
||||
|
||||
CALL_EVENT_RESPONSE_BUDGET_MS = 250
|
||||
|
||||
@@ -73,7 +74,7 @@ def sdk_context_router(dependencies: Sequence[Depends]) -> APIRouter:
|
||||
raw = await asyncio.to_thread(
|
||||
client.search,
|
||||
value.query,
|
||||
user_ids=["local"],
|
||||
user_ids=[value.user_id],
|
||||
agent_ids=[],
|
||||
include_sources=list(authority.raw_sources or ()),
|
||||
limit=value.limit,
|
||||
@@ -108,7 +109,7 @@ def sdk_context_router(dependencies: Sequence[Depends]) -> APIRouter:
|
||||
raw = await asyncio.to_thread(
|
||||
client.search,
|
||||
value.query,
|
||||
user_ids=["local"],
|
||||
user_ids=[value.user_id],
|
||||
agent_ids=[],
|
||||
limit=value.limit,
|
||||
min_score=sdk_relevance_floor(),
|
||||
@@ -123,7 +124,12 @@ def sdk_context_router(dependencies: Sequence[Depends]) -> APIRouter:
|
||||
for hit in hits:
|
||||
record = await asyncio.to_thread(client.get, hit.memory_id)
|
||||
memory = None if record is None else HySdkMemoryRecord.from_sdk(record)
|
||||
if memory is None or memory.status != "active":
|
||||
# `get` takes no user: a hit is kept only if it is the caller's.
|
||||
if (
|
||||
memory is None
|
||||
or memory.status != "active"
|
||||
or not owns(memory.user_id, value.user_id)
|
||||
):
|
||||
continue
|
||||
items.append(
|
||||
SdkMemoryContextItem(
|
||||
|
||||
@@ -4,9 +4,10 @@ from __future__ import annotations
|
||||
|
||||
import os
|
||||
import re
|
||||
from collections.abc import Sequence
|
||||
from dataclasses import dataclass
|
||||
|
||||
from one_memory.imports.models import EvidenceContentKind, EvidenceOrigin, EvidenceRecord
|
||||
from one_memory.memory_tasks import segment_evidence_record
|
||||
from one_memory.imports.models import EvidenceContentKind, EvidenceRecord
|
||||
|
||||
MEMORY_TASK_TIMEOUT_MARGIN_SECONDS = 60.0
|
||||
MEMORY_TASK_TIMEOUT_MIN_SECONDS = 180.0
|
||||
@@ -29,10 +30,13 @@ _HOST_ENVELOPE = re.compile(
|
||||
r")",
|
||||
re.IGNORECASE | re.DOTALL,
|
||||
)
|
||||
_WHOLE_TURN_CHARACTERS = 60_000
|
||||
_WINDOW_BYTE_CEILING = 2_000_000
|
||||
_HISTORY_MERGE_TURNS = 8
|
||||
_HISTORY_MERGE_CHARACTERS = 8_000
|
||||
#: The write rule (`turn_batches`): every 5 complete turns, or fewer when
|
||||
#: the next turn would take the batch past 8000 HY-read characters.
|
||||
TURN_BATCH_TURNS = 5
|
||||
TURN_BATCH_CHARACTERS = 8_000
|
||||
#: A live conversation's last turn counts as complete, and a short batch is
|
||||
#: written, after this long without a new message in that conversation.
|
||||
TURN_IDLE_FLUSH_SECONDS = 600
|
||||
|
||||
|
||||
def memory_task_timeout_seconds(model_timeout_seconds: float) -> float:
|
||||
@@ -57,180 +61,83 @@ def _coding_tool_result_max_bytes() -> int:
|
||||
_HY_TOOL_CHARACTERS = _coding_tool_result_max_bytes()
|
||||
|
||||
|
||||
def _conversation_chunks(
|
||||
records: list[EvidenceRecord],
|
||||
*,
|
||||
max_characters: int | None = None,
|
||||
max_turns: int | None = None,
|
||||
completed_only: bool = False,
|
||||
) -> list[tuple[EvidenceRecord, ...]]:
|
||||
"""Build bounded, complete user/assistant/tool windows."""
|
||||
@dataclass(frozen=True)
|
||||
class TurnBatches:
|
||||
"""What a conversation's records become: batches to write, records to keep."""
|
||||
|
||||
#: Each batch is whole turns; every batch holds at least one user message.
|
||||
batches: tuple[tuple[EvidenceRecord, ...], ...]
|
||||
#: Not written yet: the open last turn, complete turns short of a batch,
|
||||
#: or records with no user message before them. Fed back in, in order,
|
||||
#: with the conversation's next records.
|
||||
carry: tuple[EvidenceRecord, ...]
|
||||
|
||||
|
||||
def conversation_turns(records: Sequence[EvidenceRecord]) -> list[tuple[EvidenceRecord, ...]]:
|
||||
"""One user message and every record after it up to the next user message.
|
||||
|
||||
Records before the first user message are not a turn of their own: they
|
||||
join the first turn (an assistant proposal and the user turn adopting it
|
||||
reach HY together). With no user message at all the result is one group
|
||||
that does not open with a user, which `turn_batches` never writes.
|
||||
"""
|
||||
turns: list[list[EvidenceRecord]] = []
|
||||
for record in records:
|
||||
if not _is_hy_turn_record(record):
|
||||
continue
|
||||
pending_proposal = bool(turns) and not _opens_turn(turns[-1][0])
|
||||
if not turns or (_opens_turn(record) and not pending_proposal):
|
||||
# Until the first user message, the group is leading records only.
|
||||
leading = bool(turns) and not any(_opens_turn(item) for item in turns[-1])
|
||||
if not turns or (_opens_turn(record) and not leading):
|
||||
turns.append([record])
|
||||
else:
|
||||
turns[-1].append(record)
|
||||
|
||||
if completed_only and turns and _trailing_user_turn_is_open(turns[-1]):
|
||||
turns.pop()
|
||||
|
||||
whole = _whole_turns() and max_characters is None
|
||||
if whole:
|
||||
size = _hy_read_length
|
||||
max_characters = _WHOLE_TURN_CHARACTERS
|
||||
max_turns = max_turns or 1_000_000
|
||||
else:
|
||||
size = lambda record: len(record.verbatim) # noqa: E731
|
||||
max_characters = max_characters or 12_000
|
||||
max_turns = max_turns or 24
|
||||
|
||||
chunks: list[tuple[EvidenceRecord, ...]] = []
|
||||
for turn in turns:
|
||||
original_question = turn[0] if _opens_turn(turn[0]) else None
|
||||
expanded: list[EvidenceRecord] = []
|
||||
for record in turn:
|
||||
reserve = (
|
||||
size(original_question)
|
||||
if original_question is not None
|
||||
and record is not original_question
|
||||
and size(original_question) < max_characters
|
||||
else 0
|
||||
)
|
||||
segment_limit = max(1, max_characters - reserve)
|
||||
if size(record) <= segment_limit:
|
||||
expanded.append(record)
|
||||
continue
|
||||
spans = _semantic_segment_spans(record.verbatim, segment_limit)
|
||||
expanded.extend(
|
||||
segment_evidence_record(record, index=index, start=start, end=end)
|
||||
if len(spans) > 1
|
||||
else record
|
||||
for index, (start, end) in enumerate(spans)
|
||||
)
|
||||
question = expanded[0] if expanded and _opens_turn(expanded[0]) else None
|
||||
current: list[EvidenceRecord] = []
|
||||
for record in expanded:
|
||||
if current and (
|
||||
len(current) >= max_turns
|
||||
or sum(size(item) for item in current) + size(record) > max_characters
|
||||
or sum(_window_bytes(item) for item in current) + _window_bytes(record)
|
||||
> _WINDOW_BYTE_CEILING
|
||||
):
|
||||
chunks.append(tuple(current))
|
||||
current = (
|
||||
[question]
|
||||
if question is not None
|
||||
and max_turns > 1
|
||||
and size(question) + size(record) <= max_characters
|
||||
else []
|
||||
)
|
||||
current.append(record)
|
||||
chunks.append(tuple(current))
|
||||
return _merge_tool_only_chunks(chunks)
|
||||
return [tuple(turn) for turn in turns]
|
||||
|
||||
|
||||
def _semantic_segment_spans(text: str, limit: int) -> list[tuple[int, int]]:
|
||||
if len(text) <= limit:
|
||||
return [(0, len(text))]
|
||||
spans: list[tuple[int, int]] = []
|
||||
start = 0
|
||||
separators = ("\n\n", "\n", "。", "!", "?", ". ", "! ", "? ")
|
||||
while start < len(text):
|
||||
hard_end = min(len(text), start + limit)
|
||||
if hard_end == len(text):
|
||||
end = hard_end
|
||||
else:
|
||||
floor = start + max(1, limit // 2)
|
||||
candidates = [text.rfind(separator, floor, hard_end) for separator in separators]
|
||||
boundary = max(candidates)
|
||||
if boundary < floor:
|
||||
end = hard_end
|
||||
else:
|
||||
matched = next(
|
||||
separator for separator in separators if text.startswith(separator, boundary)
|
||||
)
|
||||
end = boundary + len(matched)
|
||||
spans.append((start, end))
|
||||
start = end
|
||||
return spans
|
||||
def turn_batches(records: Sequence[EvidenceRecord], *, flush: bool) -> TurnBatches:
|
||||
"""Group a conversation's records into write batches by the turn rule.
|
||||
|
||||
|
||||
def _merge_tool_only_chunks(
|
||||
chunks: list[tuple[EvidenceRecord, ...]],
|
||||
) -> list[tuple[EvidenceRecord, ...]]:
|
||||
if not any(chunk and all(record.role == "tool" for record in chunk) for chunk in chunks):
|
||||
return chunks
|
||||
merged: list[tuple[EvidenceRecord, ...]] = []
|
||||
leading_tools: list[EvidenceRecord] = []
|
||||
for chunk in chunks:
|
||||
if not chunk:
|
||||
continue
|
||||
if all(record.role == "tool" for record in chunk):
|
||||
if merged:
|
||||
merged[-1] = (*merged[-1], *chunk)
|
||||
else:
|
||||
leading_tools.extend(chunk)
|
||||
continue
|
||||
if leading_tools:
|
||||
chunk = (*leading_tools, *chunk)
|
||||
leading_tools = []
|
||||
merged.append(chunk)
|
||||
return [] if leading_tools else merged
|
||||
|
||||
|
||||
def _merge_historical_chunks(
|
||||
chunks: list[tuple[EvidenceRecord, ...]],
|
||||
*,
|
||||
max_turns: int = _HISTORY_MERGE_TURNS,
|
||||
max_characters: int = _HISTORY_MERGE_CHARACTERS,
|
||||
) -> list[tuple[EvidenceRecord, ...]]:
|
||||
if not chunks:
|
||||
return []
|
||||
merged: list[tuple[EvidenceRecord, ...]] = []
|
||||
current: list[EvidenceRecord] = []
|
||||
current_turns = 0
|
||||
- A turn is complete once the next user message follows it; `flush`
|
||||
completes the last one too (history at end of file, or 10 idle minutes).
|
||||
- Every 5 complete turns are one batch. Before 5, a turn that would take
|
||||
the batch past 8000 characters starts the next batch; one turn over
|
||||
8000 is a batch by itself and is never split.
|
||||
- Characters are what HY reads: user/assistant text in full, a tool
|
||||
record up to `_HY_TOOL_CHARACTERS`.
|
||||
- Fewer than 5 complete turns within 8000 are written only on `flush`.
|
||||
- No batch is written without a user message; records with none before
|
||||
them are carried, and dropped on `flush`.
|
||||
"""
|
||||
turns = conversation_turns(records)
|
||||
if turns and not any(_opens_turn(record) for record in turns[0]):
|
||||
# Records with no user message yet: nothing to write until one comes.
|
||||
return TurnBatches(batches=(), carry=() if flush else turns[0])
|
||||
complete = turns if flush else turns[:-1]
|
||||
open_tail = () if flush or not turns else turns[-1]
|
||||
batches: list[tuple[EvidenceRecord, ...]] = []
|
||||
current: list[tuple[EvidenceRecord, ...]] = []
|
||||
current_characters = 0
|
||||
for chunk in chunks:
|
||||
chunk_characters = sum(len(record.verbatim) for record in chunk)
|
||||
if current and (
|
||||
current_turns >= max_turns or current_characters + chunk_characters > max_characters
|
||||
):
|
||||
merged.append(tuple(current))
|
||||
current = []
|
||||
current_turns = 0
|
||||
current_characters = 0
|
||||
current.extend(chunk)
|
||||
current_turns += 1
|
||||
current_characters += chunk_characters
|
||||
if current:
|
||||
merged.append(tuple(current))
|
||||
return merged
|
||||
|
||||
|
||||
def _chunk_input_ref(chunk: tuple[EvidenceRecord, ...]) -> str:
|
||||
user_records = tuple(item for item in chunk if item.role == "user")
|
||||
return user_records[-1].evidence_id if user_records else chunk[-1].evidence_id
|
||||
for turn in complete:
|
||||
characters = sum(_hy_read_length(record) for record in turn)
|
||||
if current and current_characters + characters > TURN_BATCH_CHARACTERS:
|
||||
batches.append(tuple(record for item in current for record in item))
|
||||
current, current_characters = [], 0
|
||||
current.append(turn)
|
||||
current_characters += characters
|
||||
if len(current) >= TURN_BATCH_TURNS or current_characters > TURN_BATCH_CHARACTERS:
|
||||
batches.append(tuple(record for item in current for record in item))
|
||||
current, current_characters = [], 0
|
||||
if current and flush:
|
||||
batches.append(tuple(record for item in current for record in item))
|
||||
current = []
|
||||
carry = tuple(record for item in current for record in item) + tuple(open_tail)
|
||||
return TurnBatches(batches=tuple(batches), carry=carry)
|
||||
|
||||
|
||||
def _opens_turn(record: EvidenceRecord) -> bool:
|
||||
return record.role == "user"
|
||||
|
||||
|
||||
def _trailing_user_turn_is_open(turn: list[EvidenceRecord]) -> bool:
|
||||
if len(turn) != 1:
|
||||
return False
|
||||
opener = turn[0]
|
||||
return (
|
||||
_opens_turn(opener)
|
||||
and opener.content_kind is EvidenceContentKind.RAW_CONVERSATION
|
||||
and not _is_hand_added_memory(opener)
|
||||
)
|
||||
|
||||
|
||||
def _is_hy_turn_record(record: EvidenceRecord) -> bool:
|
||||
if record.role == "tool":
|
||||
return record.content_kind is EvidenceContentKind.RAW_CONVERSATION and bool(
|
||||
@@ -254,23 +161,6 @@ def _is_hy_input_turn(record: EvidenceRecord) -> bool:
|
||||
return _HOST_ENVELOPE.search(source[:2_000]) is None
|
||||
|
||||
|
||||
def _is_hand_added_memory(record: EvidenceRecord) -> bool:
|
||||
meta = record.origin_meta.get(EvidenceOrigin.MANUAL_IMPORT)
|
||||
return (
|
||||
record.source_id == "one-memory-manual"
|
||||
and meta is not None
|
||||
and meta.raw_meta.get("input_mode") == "direct_memory_note"
|
||||
)
|
||||
|
||||
|
||||
def _window_bytes(record: EvidenceRecord) -> int:
|
||||
return len(record.verbatim.encode("utf-8"))
|
||||
|
||||
|
||||
def _whole_turns() -> bool:
|
||||
return os.environ.get("ONE_MEMORY_SPLIT_TURNS", "").strip() != "1"
|
||||
|
||||
|
||||
def _hy_read_length(record: EvidenceRecord) -> int:
|
||||
if record.role == "tool":
|
||||
return min(len(record.verbatim), _HY_TOOL_CHARACTERS)
|
||||
|
||||
@@ -14,7 +14,7 @@ from pathlib import Path
|
||||
from typing import Literal, cast
|
||||
|
||||
HY_MEMORY_DISTRIBUTION = "hy-memory"
|
||||
HY_MEMORY_VERSION = "2.0.6"
|
||||
HY_MEMORY_VERSION = "2.0.9"
|
||||
HY_MEMORY_NAMESPACE = "governed-v2"
|
||||
DiagnosticLogLevel = Literal["error", "warning", "info", "debug"]
|
||||
|
||||
@@ -109,11 +109,30 @@ def apply_diagnostic_log_level(level: DiagnosticLogLevel) -> None:
|
||||
if any(name == root or name.startswith(f"{root}.") for root in _NOISY_LIBRARY_LOGGERS):
|
||||
logging.getLogger(name).setLevel(noisy_level)
|
||||
|
||||
# HY configures its text log once, on the first client, at the level of
|
||||
# that moment. Its file handler must follow later changes too, or a live
|
||||
# switch to debug never reaches hy_memory.log.
|
||||
for handler in logging.getLogger("hy_memory").handlers:
|
||||
if isinstance(handler, logging.FileHandler):
|
||||
handler.setLevel(selected)
|
||||
|
||||
os.environ["MEMORY_LOG_LEVEL"] = logging.getLevelName(selected)
|
||||
os.environ["MEMORY_PIPELINE_TRACE_ENABLED"] = "true" if level == "debug" else "false"
|
||||
_apply_hy_pipeline_policy()
|
||||
os.environ["MEMORY_LOG_LLM_CONTENT"] = "true" if level == "debug" else "false"
|
||||
|
||||
|
||||
def _apply_hy_pipeline_policy() -> None:
|
||||
"""Keep HY's per-step pipeline log and trace off at every level.
|
||||
|
||||
Nothing in MemHub reads them: Core's own diagnostics carry the step
|
||||
timing, and the JSONL/SQLite copies only grew on disk. With both off
|
||||
the SDK skips the per-step bookkeeping altogether.
|
||||
"""
|
||||
|
||||
os.environ["MEMORY_PIPELINE_LOG_ENABLED"] = "false"
|
||||
os.environ["MEMORY_PIPELINE_TRACE_ENABLED"] = "false"
|
||||
|
||||
|
||||
def hy_memory_version() -> str:
|
||||
"""Return the installed SDK version, failing if the pinned dist is absent."""
|
||||
|
||||
@@ -131,7 +150,9 @@ def configure_hy_runtime_environment(data_dir: Path) -> None:
|
||||
os.environ["MEMORY_LOG_DIR"] = str(log_dir)
|
||||
level = diagnostic_log_level(data_dir)
|
||||
os.environ["MEMORY_LOG_LEVEL"] = level.upper()
|
||||
os.environ["MEMORY_PIPELINE_TRACE_ENABLED"] = "true" if level == "debug" else "false"
|
||||
_apply_hy_pipeline_policy()
|
||||
# Still meaningful with the pipeline log off: it decides whether HY's
|
||||
# DEBUG lines in hy_memory.log carry full model prompts and responses.
|
||||
os.environ["MEMORY_LOG_LLM_CONTENT"] = "true" if level == "debug" else "false"
|
||||
os.environ["MEMORY_MEMORY_OPERATIONS_ENABLED"] = "false"
|
||||
os.environ["ANONYMIZED_TELEMETRY"] = "False"
|
||||
|
||||
@@ -82,7 +82,10 @@ def repair_sdk_metadata_from_bundle(
|
||||
database_path: Path,
|
||||
now: datetime | None = None,
|
||||
) -> MetadataRepairResult:
|
||||
"""Restore metadata lost by historical cutovers from their verified bundles."""
|
||||
"""Restore metadata lost by historical cutovers from their verified bundles.
|
||||
|
||||
Offline only: no SDK client may be open on this home (see `_repair_vector`).
|
||||
"""
|
||||
migration_id = _sdk_migration_id(database_path)
|
||||
if migration_id is None:
|
||||
return MetadataRepairResult(None, 0, 0, 0)
|
||||
@@ -329,8 +332,11 @@ def _repair_vector(
|
||||
from one_memory.sdk_vector_reattach import open_offline_chroma
|
||||
|
||||
client = open_offline_chroma(directory)
|
||||
# The SDK client is not open yet (startup has not created its handle), so
|
||||
# this is the only writer; release the store before the SDK takes it.
|
||||
# This must be the only Chroma client on the directory: Chroma shares one
|
||||
# System per path, and this client's close() drops it from under any
|
||||
# other (KeyError on its next lookup). `sdk-migrate` runs it after the
|
||||
# cutover with Runtime stopped; Core's startup fallback runs it while
|
||||
# `hold_stores_offline` keeps every SDK client waiting.
|
||||
try:
|
||||
repaired = 0
|
||||
ids = tuple(mapping)
|
||||
@@ -420,6 +426,7 @@ def _repair_coding(
|
||||
path = root / name
|
||||
if not path.is_file():
|
||||
continue
|
||||
_ensure_coding_schema(path)
|
||||
with closing(sqlite3.connect(path)) as database:
|
||||
tables = {
|
||||
str(row[0])
|
||||
@@ -431,10 +438,14 @@ def _repair_coding(
|
||||
):
|
||||
if table not in tables:
|
||||
continue
|
||||
# _ensure_coding_schema added it; an SDK without the column
|
||||
# (before 2.0.7) leaves the time to the vector repair alone.
|
||||
has_memory_at = "memory_at" in _columns(database, table)
|
||||
rows = database.execute(
|
||||
f"SELECT {identifier},source,created_at FROM {table}"
|
||||
f"SELECT {identifier},source,created_at,"
|
||||
f"{'memory_at' if has_memory_at else 'NULL'} FROM {table}"
|
||||
).fetchall()
|
||||
for memory_id, source, created_at in rows:
|
||||
for memory_id, source, created_at, memory_at in rows:
|
||||
desired = mapping.get(str(memory_id))
|
||||
if desired is None:
|
||||
continue
|
||||
@@ -443,6 +454,13 @@ def _repair_coding(
|
||||
if desired.source and source in {None, "", "default"}:
|
||||
updates.append("source=?")
|
||||
values.append(desired.source)
|
||||
if (
|
||||
has_memory_at
|
||||
and desired.memory_at is not None
|
||||
and memory_at != desired.memory_at
|
||||
):
|
||||
updates.append("memory_at=?")
|
||||
values.append(desired.memory_at)
|
||||
if desired.created_at is not None:
|
||||
desired_created_at = (
|
||||
datetime.fromtimestamp(desired.created_at, tz=UTC)
|
||||
@@ -464,6 +482,32 @@ def _repair_coding(
|
||||
return repaired
|
||||
|
||||
|
||||
def _ensure_coding_schema(path: Path) -> None:
|
||||
"""Bring a Coding/Working store to the installed SDK's schema.
|
||||
|
||||
A migrated home's stores were written by an older SDK and may lack columns
|
||||
the repair writes (``memory_at`` since hy-memory 2.0.7). The repair runs
|
||||
before any SDK client opens them, so the SDK's own schema step is run here
|
||||
rather than repeating its DDL. ``CodingMemoryStore`` has no public
|
||||
schema-only entry point: ``initialize()`` also opens its Chroma
|
||||
collections, which must stay closed during the offline repair. So only its
|
||||
SQLite step runs, on a store object that carries nothing but that path.
|
||||
"""
|
||||
from hy_memory.coding.store import ( # type: ignore[import-untyped]
|
||||
CodingMemoryStore,
|
||||
)
|
||||
|
||||
# Relies on the pinned SDK's internals (hy-memory==2.0.9 is pinned exactly).
|
||||
store = CodingMemoryStore.__new__(CodingMemoryStore)
|
||||
store._db_path = str(path)
|
||||
store._conn = None
|
||||
try:
|
||||
store._init_sqlite_sync()
|
||||
finally:
|
||||
if store._conn is not None:
|
||||
store._conn.close()
|
||||
|
||||
|
||||
def _batches[T](values: list[T] | tuple[T, ...]) -> Iterator[tuple[T, ...]]:
|
||||
for start in range(0, len(values), _BATCH):
|
||||
yield tuple(values[start : start + _BATCH])
|
||||
@@ -36,7 +36,12 @@ from one_memory.hy_graph_admin import (
|
||||
)
|
||||
from one_memory.hy_sdk_types import HySdkRuntimeError
|
||||
from one_memory.local_embedding import LocalEmbedder
|
||||
from one_memory.model_profiles import ModelProfileSnapshot, ProviderKind
|
||||
from one_memory.log_file import confine_hy_sdk_logging
|
||||
from one_memory.model_profiles import (
|
||||
ModelProfileSnapshot,
|
||||
ProviderKind,
|
||||
disable_thinking_params,
|
||||
)
|
||||
|
||||
_RUNTIME_LOCK = threading.RLock()
|
||||
BUILTIN_RETRIEVAL_PROFILE_ID = "builtin-retrieval"
|
||||
@@ -52,6 +57,26 @@ class _RuntimeEntry:
|
||||
|
||||
_RUNTIMES: dict[Path, _RuntimeEntry] = {}
|
||||
|
||||
# Homes whose SDK-owned local stores are held offline for maintenance, and
|
||||
# the client constructions in flight per home. Chroma keeps one process-wide
|
||||
# System per path with no lock around create, lookup and release, so a
|
||||
# second client that opens or closes beside the SDK's pulls the System out
|
||||
# from under it (KeyError on the next lookup). Maintenance therefore runs
|
||||
# only while no SDK client exists for that home, and new clients wait.
|
||||
_STORE_GATE = threading.Condition()
|
||||
_OFFLINE_STORES: set[Path] = set()
|
||||
_OPENING: dict[Path, int] = {}
|
||||
|
||||
# One client constructs at a time per home, its first-time store setup
|
||||
# included. The constructor opens Chroma's `PersistentClient`, and two first
|
||||
# opens on one directory race over that same unlocked System cache. On a
|
||||
# clean install the write drain's client and a UI read client started in the
|
||||
# same second; neither reached `VectorStore ready`, and the first write job
|
||||
# was lost as unknown. Taken inside `acquire_client`, after the offline gate
|
||||
# and never while `_STORE_GATE` is held, so it cannot deadlock with
|
||||
# `hold_stores_offline`, which refuses while `_OPENING` counts a client.
|
||||
_CONSTRUCTING: dict[Path, threading.Lock] = {}
|
||||
|
||||
|
||||
class LocalOnnxEmbeddingService:
|
||||
"""Public ``EmbeddingService`` implementation backed by MemHub's ONNX model."""
|
||||
@@ -244,14 +269,22 @@ def build_sdk_config(
|
||||
embedding_provider, embedding_endpoint, embedding_model, dimensions
|
||||
)
|
||||
|
||||
llm: dict[str, Any] = {
|
||||
"provider": "openai",
|
||||
"model": profile.model,
|
||||
"api_key": generation_key,
|
||||
"base_url": generation_endpoint,
|
||||
}
|
||||
# The SDK sends `extra_body` on every chat call, chat and coding/working
|
||||
# alike. Without it DeepSeek spent the whole agent_max_tokens on thinking
|
||||
# and answered finish_reason=length with no content.
|
||||
thinking_off = disable_thinking_params(profile)
|
||||
if thinking_off:
|
||||
llm["extra_body"] = thinking_off
|
||||
|
||||
return MemoryConfig.from_dict(
|
||||
{
|
||||
"llm": {
|
||||
"provider": "openai",
|
||||
"model": profile.model,
|
||||
"api_key": generation_key,
|
||||
"base_url": generation_endpoint,
|
||||
},
|
||||
"llm": llm,
|
||||
"embedder": {
|
||||
"provider": "openai",
|
||||
"model": (
|
||||
@@ -295,7 +328,60 @@ def acquire_client(
|
||||
data_dir: Path,
|
||||
http_transport_factory: Callable[[], Any] | None = None,
|
||||
) -> ClientLease:
|
||||
"""Construct a public SDK client and return its explicit runtime lease."""
|
||||
"""Construct a public SDK client and return its explicit runtime lease.
|
||||
|
||||
Waits while `hold_stores_offline` holds this home's stores.
|
||||
"""
|
||||
key = data_dir.resolve()
|
||||
with _STORE_GATE:
|
||||
while key in _OFFLINE_STORES:
|
||||
_STORE_GATE.wait()
|
||||
_OPENING[key] = _OPENING.get(key, 0) + 1
|
||||
try:
|
||||
return _acquire_client(
|
||||
profile,
|
||||
api_key=api_key,
|
||||
data_dir=data_dir,
|
||||
http_transport_factory=http_transport_factory,
|
||||
)
|
||||
finally:
|
||||
with _STORE_GATE:
|
||||
_OPENING[key] -= 1
|
||||
if not _OPENING[key]:
|
||||
del _OPENING[key]
|
||||
_STORE_GATE.notify_all()
|
||||
|
||||
|
||||
def hold_stores_offline(data_dir: Path) -> bool:
|
||||
"""Hold this home's SDK stores for offline maintenance; False if a client is open.
|
||||
|
||||
While held, `acquire_client` for the home waits. Every SDK client in Core
|
||||
is built there, so nothing reads or writes Chroma beside the maintenance.
|
||||
`release_stores` ends the hold; it is idempotent.
|
||||
"""
|
||||
key = data_dir.resolve()
|
||||
with _STORE_GATE:
|
||||
with _RUNTIME_LOCK:
|
||||
open_runtime = key in _RUNTIMES
|
||||
if key in _OFFLINE_STORES or _OPENING.get(key) or open_runtime:
|
||||
return False
|
||||
_OFFLINE_STORES.add(key)
|
||||
return True
|
||||
|
||||
|
||||
def release_stores(data_dir: Path) -> None:
|
||||
with _STORE_GATE:
|
||||
_OFFLINE_STORES.discard(data_dir.resolve())
|
||||
_STORE_GATE.notify_all()
|
||||
|
||||
|
||||
def _acquire_client(
|
||||
profile: ModelProfileSnapshot,
|
||||
*,
|
||||
api_key: str | None,
|
||||
data_dir: Path,
|
||||
http_transport_factory: Callable[[], Any] | None,
|
||||
) -> ClientLease:
|
||||
config = build_sdk_config(profile, api_key=api_key, data_dir=data_dir)
|
||||
config.http_transport_factory = http_transport_factory
|
||||
mismatch = inspect_graph_width_mismatch(config)
|
||||
@@ -306,16 +392,23 @@ def acquire_client(
|
||||
embedding_service: LocalOnnxEmbeddingService | None = None
|
||||
if (profile.embedding_provider_kind or profile.provider_kind) is ProviderKind.LOCAL_ONNX:
|
||||
embedding_service = LocalOnnxEmbeddingService()
|
||||
with _STORE_GATE:
|
||||
constructing = _CONSTRUCTING.setdefault(key, threading.Lock())
|
||||
try:
|
||||
client = HyMemoryClient(
|
||||
config=config,
|
||||
mode="ultra" if graph_store_enabled() else "pro",
|
||||
runtime=runtime,
|
||||
embedding_service=embedding_service,
|
||||
)
|
||||
with constructing:
|
||||
client = HyMemoryClient(
|
||||
config=config,
|
||||
mode="ultra" if graph_store_enabled() else "pro",
|
||||
runtime=runtime,
|
||||
embedding_service=embedding_service,
|
||||
)
|
||||
except BaseException:
|
||||
_release_runtime(key)
|
||||
raise
|
||||
finally:
|
||||
# The constructor's first step configures HY's text log (once per
|
||||
# process), so by here it exists whether or not construction succeeded.
|
||||
confine_hy_sdk_logging()
|
||||
return ClientLease(client, key)
|
||||
|
||||
|
||||
|
||||
@@ -151,6 +151,7 @@ class HySdkMemoryRecord(BaseModel):
|
||||
if not content.strip():
|
||||
raise HySdkReadError("hy_memory_record_shape_unknown")
|
||||
status = str(raw.get("status") or "active")
|
||||
memory_at = raw.get("memory_at")
|
||||
created_at = _epoch(raw.get("created_at"))
|
||||
updated_at = _epoch(raw.get("updated_at"))
|
||||
try:
|
||||
@@ -167,6 +168,9 @@ class HySdkMemoryRecord(BaseModel):
|
||||
str(raw["session_id"]) if isinstance(raw.get("session_id"), str) else None
|
||||
),
|
||||
source=str(raw["source"]) if isinstance(raw.get("source"), str) else None,
|
||||
# Conversation time; hy-memory 2.0.7 stores it for experiences
|
||||
# and facts. Older stores leave it unknown.
|
||||
memory_at=memory_at if isinstance(memory_at, int) else None,
|
||||
gmt_created=created_at,
|
||||
scene=cast(MemoryScene, variant),
|
||||
kind=cast(MemoryKindName, kind),
|
||||
|
||||
@@ -31,6 +31,16 @@ class ScanIssueCode(StrEnum):
|
||||
#: import it. Named for the reason, so a reader can tell a setting from
|
||||
#: a record the parser could not use.
|
||||
SUBAGENT_TRACE_DISABLED = "subagent_trace_disabled"
|
||||
#: A host's own notice wearing the user's role — Hermes posts a finished
|
||||
#: background process, and its gateway marks the post as not written by a
|
||||
#: human. Nothing in it is the person's, so it never reaches extraction.
|
||||
INJECTED_MACHINE_NOTICE = "injected_machine_notice"
|
||||
#: The host could not transcribe a voice message and left a placeholder
|
||||
#: naming the audio file. The person did speak, but the placeholder holds
|
||||
#: only a path, which an extractor would otherwise read as a fact.
|
||||
VOICE_PLACEHOLDER = "voice_placeholder"
|
||||
#: A health check such as "Reply with exactly: PONG": no one's words.
|
||||
PROBE = "probe"
|
||||
SOURCE_BUSY = "source_busy"
|
||||
UNSUPPORTED_SCHEMA = "unsupported_schema"
|
||||
BUDGET_EXCEEDED = "budget_exceeded"
|
||||
|
||||
@@ -27,6 +27,7 @@ def discovered_off_loop(
|
||||
*,
|
||||
suffixes: frozenset[str],
|
||||
max_targets: int,
|
||||
excluded_top_directories: frozenset[str] = frozenset(),
|
||||
) -> AsyncIterator[SourceFileDiscoveryEvent]:
|
||||
"""The same walk, for callers that have an event loop to keep free.
|
||||
|
||||
@@ -41,6 +42,7 @@ def discovered_off_loop(
|
||||
grant,
|
||||
suffixes=suffixes,
|
||||
max_targets=max_targets,
|
||||
excluded_top_directories=excluded_top_directories,
|
||||
),
|
||||
scope_key=grant.source_id,
|
||||
)
|
||||
@@ -51,12 +53,15 @@ def discover_source_files(
|
||||
*,
|
||||
suffixes: frozenset[str],
|
||||
max_targets: int,
|
||||
excluded_top_directories: frozenset[str] = frozenset(),
|
||||
) -> Iterator[SourceFileDiscoveryEvent]:
|
||||
"""Metadata only: one lstat per candidate, no open and no per-file grant lookup.
|
||||
|
||||
The grant and root identity are checked once before the walk. Each listed
|
||||
file keeps the grant's check, which runs again when that file is opened;
|
||||
the open itself refuses symlinks and any inode other than the one listed.
|
||||
`excluded_top_directories` are directories directly under the root that
|
||||
are never entered, such as another user's Hermes profiles.
|
||||
"""
|
||||
grant.check_root()
|
||||
inspected = 0
|
||||
@@ -64,8 +69,12 @@ def discover_source_files(
|
||||
grant.root,
|
||||
followlinks=False,
|
||||
):
|
||||
at_root = Path(directory) == grant.root
|
||||
child_directories[:] = sorted(
|
||||
child for child in child_directories if not (Path(directory) / child).is_symlink()
|
||||
child
|
||||
for child in child_directories
|
||||
if not (Path(directory) / child).is_symlink()
|
||||
and not (at_root and child in excluded_top_directories)
|
||||
)
|
||||
for filename in sorted(filenames):
|
||||
if Path(filename).suffix.lower() not in suffixes:
|
||||
|
||||
@@ -252,6 +252,18 @@ class SourceGrantStore:
|
||||
).fetchone()
|
||||
return self._to_receipt(row) if row else None
|
||||
|
||||
def grants(self) -> tuple[tuple[SourceGrantReceipt, str], ...]:
|
||||
"""Every grant of every user, with its granted root path.
|
||||
|
||||
One source may hold several grants, one per `(user_id, source_id)`:
|
||||
a Hermes root for `local` and a Hermes profile for another user.
|
||||
"""
|
||||
with self._connect() as connection:
|
||||
rows = connection.execute(
|
||||
"SELECT * FROM source_grants ORDER BY source_id, user_id"
|
||||
).fetchall()
|
||||
return tuple((self._to_receipt(row), str(row["requested_path"])) for row in rows)
|
||||
|
||||
def require_receipt(self, *, user_id: str, source_id: str) -> SourceGrantReceipt:
|
||||
receipt = self.receipt(user_id=user_id, source_id=source_id)
|
||||
if receipt is None:
|
||||
|
||||
@@ -28,6 +28,7 @@ from one_memory.imports.adapters import (
|
||||
)
|
||||
from one_memory.imports.jobs import ImportJob, ImportJobStore, ImportStatus
|
||||
from one_memory.imports.supervisor import ImportSupervisor
|
||||
from one_memory.user_scope import DEFAULT_USER_ID, lane_key, split_lane
|
||||
|
||||
#: How long an automatic retry will wait for a wedged worker to notice cancel.
|
||||
ABANDON_TIMEOUT_SECONDS = 5.0
|
||||
@@ -64,7 +65,12 @@ class LiveSyncHealth(BaseModel):
|
||||
|
||||
|
||||
class PollingLiveSyncSupervisor:
|
||||
"""Near-live sync that reuses each adapter's persisted incremental cursor."""
|
||||
"""Near-live sync that reuses each adapter's persisted incremental cursor.
|
||||
|
||||
A "source" here is a lane key (`user_scope.lane_key`): the bare source id
|
||||
for `local`, `source@user` for another user's grant. Cursors, jobs and
|
||||
locks are therefore per `(user_id, source_id)`.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
@@ -92,8 +98,22 @@ class PollingLiveSyncSupervisor:
|
||||
}
|
||||
self._locks = {source_id: asyncio.Lock() for source_id in self.source_ids}
|
||||
self._task: asyncio.Task[None] | None = None
|
||||
self._group: asyncio.TaskGroup | None = None
|
||||
self._observers: set[asyncio.Task[None]] = set()
|
||||
|
||||
def ensure_source(self, source_id: str) -> None:
|
||||
"""Follow a lane granted after start, such as a new Hermes profile."""
|
||||
if source_id in self._locks:
|
||||
return
|
||||
self.source_ids = (*self.source_ids, source_id)
|
||||
self._health.setdefault(
|
||||
source_id,
|
||||
LiveSyncHealth(source_id=source_id, mode=self.mode, state=LiveSyncState.IDLE),
|
||||
)
|
||||
self._locks[source_id] = asyncio.Lock()
|
||||
if self._group is not None:
|
||||
self._group.create_task(self._poll_source(source_id))
|
||||
|
||||
def _set_health(self, source_id: str, value: LiveSyncHealth) -> None:
|
||||
previous = self._health.get(source_id)
|
||||
self._health[source_id] = value
|
||||
@@ -152,7 +172,12 @@ class PollingLiveSyncSupervisor:
|
||||
self._watched_sources = frozenset(source_ids)
|
||||
|
||||
def all_health(self) -> tuple[LiveSyncHealth, ...]:
|
||||
return tuple(self.health(source_id) for source_id in self.source_ids)
|
||||
"""`local`'s lanes only: what the App has always been shown."""
|
||||
return tuple(
|
||||
self.health(source_id)
|
||||
for source_id in self.source_ids
|
||||
if split_lane(source_id)[0] == DEFAULT_USER_ID
|
||||
)
|
||||
|
||||
async def initialize_source(
|
||||
self, source_id: str, *, baseline_missing_files: bool = True
|
||||
@@ -164,11 +189,12 @@ class PollingLiveSyncSupervisor:
|
||||
baselines = (
|
||||
await adapter.live_baseline() if isinstance(adapter, _LiveBaselineAdapter) else {}
|
||||
)
|
||||
user_id, bare_source_id = split_lane(source_id)
|
||||
await in_the_background(
|
||||
partial(
|
||||
self.job_store.ensure_live_cursors,
|
||||
user_id="local",
|
||||
source_id=source_id,
|
||||
user_id=user_id,
|
||||
source_id=bare_source_id,
|
||||
baselines=baselines,
|
||||
baseline_missing_files=baseline_missing_files,
|
||||
)
|
||||
@@ -228,7 +254,7 @@ class PollingLiveSyncSupervisor:
|
||||
(
|
||||
job
|
||||
for job in reversed(source_jobs)
|
||||
if job.source_id == source_id
|
||||
if lane_key(job.user_id, job.source_id) == source_id
|
||||
and job.operation == "sync"
|
||||
and job.status
|
||||
in {
|
||||
@@ -277,13 +303,13 @@ class PollingLiveSyncSupervisor:
|
||||
competing = [
|
||||
job
|
||||
for job in source_jobs
|
||||
if job.source_id == source_id
|
||||
if lane_key(job.user_id, job.source_id) == source_id
|
||||
and job.operation != "sync"
|
||||
and job.status
|
||||
in {ImportStatus.PENDING, ImportStatus.RUNNING, ImportStatus.INTERRUPTED}
|
||||
]
|
||||
bounded_history_incomplete = any(
|
||||
job.source_id == source_id
|
||||
lane_key(job.user_id, job.source_id) == source_id
|
||||
and job.operation == "history"
|
||||
and job.history_after is not None
|
||||
and job.status is not ImportStatus.COMPLETED
|
||||
@@ -379,11 +405,12 @@ class PollingLiveSyncSupervisor:
|
||||
await close()
|
||||
|
||||
async def _live_cursors(self, source_id: str, adapter: object) -> dict[str, SourceCursor]:
|
||||
user_id, bare_source_id = split_lane(source_id)
|
||||
cursors = await in_the_background(
|
||||
partial(
|
||||
self.job_store.get_source_cursors,
|
||||
user_id="local",
|
||||
source_id=source_id,
|
||||
user_id=user_id,
|
||||
source_id=bare_source_id,
|
||||
operation="sync",
|
||||
)
|
||||
)
|
||||
@@ -395,8 +422,8 @@ class PollingLiveSyncSupervisor:
|
||||
return await in_the_background(
|
||||
partial(
|
||||
self.job_store.ensure_live_cursors,
|
||||
user_id="local",
|
||||
source_id=source_id,
|
||||
user_id=user_id,
|
||||
source_id=bare_source_id,
|
||||
baselines=baselines,
|
||||
)
|
||||
)
|
||||
@@ -520,9 +547,13 @@ class PollingLiveSyncSupervisor:
|
||||
async def _run(self) -> None:
|
||||
# A source owns its cadence; one large catch-up cannot delay every
|
||||
# other source. The group keeps shutdown responsible for every poller.
|
||||
async with asyncio.TaskGroup() as group:
|
||||
for source_id in self.source_ids:
|
||||
group.create_task(self._poll_source(source_id))
|
||||
try:
|
||||
async with asyncio.TaskGroup() as group:
|
||||
self._group = group
|
||||
for source_id in self.source_ids:
|
||||
group.create_task(self._poll_source(source_id))
|
||||
finally:
|
||||
self._group = None
|
||||
|
||||
async def _poll_source(self, source_id: str) -> None:
|
||||
while True:
|
||||
|
||||
@@ -3,7 +3,7 @@ import logging
|
||||
import math
|
||||
import sqlite3
|
||||
from collections import Counter
|
||||
from collections.abc import Callable
|
||||
from collections.abc import AsyncIterator, Callable
|
||||
from contextlib import suppress
|
||||
from contextvars import ContextVar
|
||||
from dataclasses import dataclass, field
|
||||
@@ -227,14 +227,17 @@ class ImportRunner:
|
||||
heartbeat = asyncio.create_task(self._keep_alive(job_id=job_id, runner_token=runner_token))
|
||||
pending = _PendingSkips()
|
||||
writes = _PendingWrites()
|
||||
stream = adapter.scan(
|
||||
ScanOptions(
|
||||
cursors=cursors,
|
||||
reuse_listing=(
|
||||
job.operation == "history"
|
||||
or (job.operation == "sync" and job.source_id == "workbuddy")
|
||||
),
|
||||
)
|
||||
stream = _owned_by(
|
||||
adapter.scan(
|
||||
ScanOptions(
|
||||
cursors=cursors,
|
||||
reuse_listing=(
|
||||
job.operation == "history"
|
||||
or (job.operation == "sync" and job.source_id == "workbuddy")
|
||||
),
|
||||
)
|
||||
),
|
||||
job.user_id,
|
||||
)
|
||||
slice_cursors = dict(cursors)
|
||||
next_event: asyncio.Future[ScanEvent] | None = None
|
||||
@@ -662,11 +665,15 @@ class ImportRunner:
|
||||
and self.sdk_queue is not None
|
||||
and event.cursor.read_to_end
|
||||
):
|
||||
# History ends at this end of file, so its last turn
|
||||
# and a short batch are written. Live polling keeps
|
||||
# them for the next increment or the idle flush.
|
||||
self.sdk_queue.finalize_sdk_file(
|
||||
user_id=current.user_id,
|
||||
source_id=current.source_id,
|
||||
file_key=event.cursor.file_key,
|
||||
connection=connection,
|
||||
flush=not self._live_import,
|
||||
)
|
||||
continue
|
||||
if self.on_stable_evidence is not None and event.cursor.read_to_end:
|
||||
@@ -920,3 +927,30 @@ async def _settled_work[T](
|
||||
with suppress(asyncio.CancelledError, Exception):
|
||||
task.result()
|
||||
raise
|
||||
|
||||
|
||||
def _owned_by(stream: AsyncIterator[ScanEvent], user_id: str) -> AsyncIterator[ScanEvent]:
|
||||
"""Stamp every scanned record with the user whose root is being read.
|
||||
|
||||
Adapters know nothing of users and emit `local`. Which user a record
|
||||
belongs to is decided by the grant the job reads (a Hermes profile's
|
||||
`state.db` is that profile's user), so the job's user is the record's.
|
||||
`local` passes the stream through untouched.
|
||||
"""
|
||||
if user_id == "local":
|
||||
return stream
|
||||
return _restamped(stream, user_id)
|
||||
|
||||
|
||||
async def _restamped(stream: AsyncIterator[ScanEvent], user_id: str) -> AsyncIterator[ScanEvent]:
|
||||
try:
|
||||
async for event in stream:
|
||||
if isinstance(event, ScannedEvidence) and event.evidence.user_id != user_id:
|
||||
event = event.model_copy(
|
||||
update={"evidence": event.evidence.model_copy(update={"user_id": user_id})}
|
||||
)
|
||||
yield event
|
||||
finally:
|
||||
close = getattr(stream, "aclose", None)
|
||||
if close is not None:
|
||||
await close()
|
||||
@@ -1,4 +1,5 @@
|
||||
import json
|
||||
import re
|
||||
import sqlite3
|
||||
from collections.abc import AsyncIterator, Iterator
|
||||
from functools import partial
|
||||
@@ -87,6 +88,7 @@ class HermesSourceAdapter:
|
||||
self.grant,
|
||||
suffixes=frozenset({".jsonl", ".db"}),
|
||||
max_targets=10_000,
|
||||
excluded_top_directories=frozenset({HERMES_PROFILES_DIRECTORY}),
|
||||
):
|
||||
if isinstance(item, AuthorizedSourceFile) and _is_hermes_target(
|
||||
item.relative_path.as_posix()
|
||||
@@ -100,6 +102,7 @@ class HermesSourceAdapter:
|
||||
self.grant,
|
||||
suffixes=frozenset({".jsonl", ".db"}),
|
||||
max_targets=options.max_scan_targets or 100_000,
|
||||
excluded_top_directories=frozenset({HERMES_PROFILES_DIRECTORY}),
|
||||
):
|
||||
yield ScanProgressEvent(
|
||||
phase=ScanPhase.DISCOVER,
|
||||
@@ -214,7 +217,11 @@ def parse_hermes_rollout_record(
|
||||
message.get("content"),
|
||||
allowed_block_types=frozenset({"text", "input_text", "output_text"}),
|
||||
)
|
||||
text = strip_injected_memory(text)
|
||||
text = strip_injected_memory(text or "")
|
||||
if role == "user":
|
||||
text, injected = gateway_user_text(text)
|
||||
if injected is not None:
|
||||
return ParsedSkip(reason_code=injected, conversation_id=conversation_id)
|
||||
if not text:
|
||||
return ParsedSkip(
|
||||
reason_code=ScanIssueCode.NO_TEXT,
|
||||
@@ -242,8 +249,17 @@ def parse_hermes_rollout_record(
|
||||
)
|
||||
|
||||
|
||||
#: Hermes keeps each extra profile under `<root>/profiles/<name>/`, with its
|
||||
#: own `state.db` and sessions. A profile is another person's history: it is
|
||||
#: read only through its own grant (its own MemHub user), never as part of the
|
||||
#: root above it. Hermes' own export skips this directory for the same reason.
|
||||
HERMES_PROFILES_DIRECTORY = "profiles"
|
||||
|
||||
|
||||
def _is_hermes_target(relative_path: str) -> bool:
|
||||
path = PurePosixPath(relative_path)
|
||||
if path.parts and path.parts[0] == HERMES_PROFILES_DIRECTORY:
|
||||
return False
|
||||
return path.name == "state.db" or (path.suffix == ".jsonl" and "sessions" in path.parts)
|
||||
|
||||
|
||||
@@ -389,6 +405,15 @@ def _read_state_db(
|
||||
# `tool` rows after it hold only what came back.
|
||||
calls = _tool_calls_text(row["tool_calls"]) if role == "assistant" else None
|
||||
content = strip_injected_memory(str(row["content"] or ""))
|
||||
if role == "user":
|
||||
content, injected = gateway_user_text(content)
|
||||
if injected is not None:
|
||||
yield ScanSkip(
|
||||
cursor=cursor,
|
||||
conversation_id=str(row["session_id"]),
|
||||
reason_code=injected,
|
||||
)
|
||||
continue
|
||||
has_text = bool(content.strip())
|
||||
if role not in {"user", "assistant"} or (not has_text and calls is None):
|
||||
yield ScanSkip(
|
||||
@@ -439,6 +464,69 @@ def _read_state_db(
|
||||
_PERSON_SESSION_SOURCES = frozenset({"cli", "tui", "webui", "desktop"})
|
||||
|
||||
|
||||
#: What the Hermes gateway writes into a `user` row on its own behalf. Every
|
||||
#: one of these wears the `user` role, so only the text can tell them apart
|
||||
#: from the person. Read off real `state.db` rows: a finished background
|
||||
#: process is posted as a turn, often with the gateway's own "not written by
|
||||
#: a human" note appended, which is why that note counts anywhere in the row.
|
||||
_BACKGROUND_NOTICE = re.compile(
|
||||
r"\[IMPORTANT:\s*(?:\d+\s+background\s+process(?:es)?\b|Background\s+process\b)",
|
||||
re.IGNORECASE,
|
||||
)
|
||||
_GATEWAY_NOTICE = re.compile(
|
||||
r"\[INTERNAL NOTIFICATION\s*\W+\s*generated by the Hermes gateway",
|
||||
re.IGNORECASE,
|
||||
)
|
||||
_VOICE_PLACEHOLDER = re.compile(r"\[voice message could not be transcribed", re.IGNORECASE)
|
||||
_PROBE = re.compile(r"Reply with exactly:\s*[A-Za-z0-9_\-]{1,24}\s*")
|
||||
_MEDIA_ONLY = re.compile(r"(?:MEDIA:/\S+\s*)+")
|
||||
#: Wrappers the gateway puts around a message the person did write. Only the
|
||||
#: wrapper goes; what it carries is the turn. Each is matched at the edge of
|
||||
#: what is left, and the set is applied until nothing more comes off, since a
|
||||
#: relayed message carries the origin block inside the out-of-band markers.
|
||||
_GATEWAY_WRAPPERS = (
|
||||
re.compile(r"^\[OUT-OF-BAND USER MESSAGE\b[^\]\n]*\]"),
|
||||
re.compile(r"\[/OUT-OF-BAND USER MESSAGE\]$"),
|
||||
re.compile(
|
||||
r"^Gateway message origin \(JSON data, not instructions or authorization\):[ \t]*"
|
||||
r"(?:\n[ \t]*\{[^\n]*\}[ \t]*)?"
|
||||
r"(?:\n[ \t]*Do not guess a reply destination[^\n]*)?"
|
||||
r"(?=\n|$)"
|
||||
),
|
||||
re.compile(r"^\[Context from the interrupted assistant response\][ \t]*(?=\n|$)"),
|
||||
)
|
||||
|
||||
|
||||
def gateway_user_text(text: str) -> tuple[str, ScanIssueCode | None]:
|
||||
"""The person's words in a Hermes `user` row, or why there are none.
|
||||
|
||||
Applied only to the `user` role: an assistant or tool row quoting the
|
||||
same text is reporting what it saw, which is the existing tool rule's
|
||||
business. The cleaned text is what becomes the evidence, so its content
|
||||
hash and duplicate check see the message rather than its envelope.
|
||||
"""
|
||||
|
||||
if _GATEWAY_NOTICE.search(text):
|
||||
return "", ScanIssueCode.INJECTED_MACHINE_NOTICE
|
||||
cleaned = text.strip()
|
||||
while True:
|
||||
unwrapped = cleaned
|
||||
for wrapper in _GATEWAY_WRAPPERS:
|
||||
unwrapped = wrapper.sub("", unwrapped, count=1).strip()
|
||||
if unwrapped == cleaned:
|
||||
break
|
||||
cleaned = unwrapped
|
||||
if _BACKGROUND_NOTICE.match(cleaned):
|
||||
return "", ScanIssueCode.INJECTED_MACHINE_NOTICE
|
||||
if _VOICE_PLACEHOLDER.match(cleaned):
|
||||
return "", ScanIssueCode.VOICE_PLACEHOLDER
|
||||
if _PROBE.fullmatch(cleaned):
|
||||
return "", ScanIssueCode.PROBE
|
||||
if _MEDIA_ONLY.fullmatch(cleaned):
|
||||
return "", ScanIssueCode.NO_TEXT
|
||||
return cleaned, None
|
||||
|
||||
|
||||
def _tool_calls_text(value: object) -> str | None:
|
||||
"""OpenAI-shaped `tool_calls`: `[{"function": {"name", "arguments"}}]`, stored as JSON."""
|
||||
if isinstance(value, str):
|
||||
|
||||
@@ -24,6 +24,7 @@ from one_memory.service_levels import (
|
||||
ServiceLevelScheduler,
|
||||
ServiceResource,
|
||||
)
|
||||
from one_memory.user_scope import lane_key, split_lane
|
||||
|
||||
|
||||
class UnknownSourceError(KeyError):
|
||||
@@ -82,36 +83,45 @@ class ImportSupervisor:
|
||||
self._tasks: dict[str, asyncio.Task[None]] = {}
|
||||
self._drains: dict[str, asyncio.Task[None]] = {}
|
||||
|
||||
def register_adapter(self, adapter: SourceAdapter) -> None:
|
||||
self.adapters[adapter.descriptor.source_id] = adapter
|
||||
def register_adapter(self, adapter: SourceAdapter, *, user_id: str = "local") -> None:
|
||||
"""Adapters are keyed by lane: the source id for `local`, `source@user`
|
||||
for another user's grant of the same source."""
|
||||
self.adapters[lane_key(user_id, adapter.descriptor.source_id)] = adapter
|
||||
|
||||
def unregister_adapter(self, source_id: str) -> None:
|
||||
self.adapters.pop(source_id, None)
|
||||
|
||||
@staticmethod
|
||||
def lane_of(job: ImportJob) -> str:
|
||||
return lane_key(job.user_id, job.source_id)
|
||||
|
||||
def active_job_ids(self) -> frozenset[str]:
|
||||
return frozenset(job_id for job_id, task in self._tasks.items() if not task.done())
|
||||
|
||||
def create_job(
|
||||
self, source_id: str, *, operation: Literal["history", "sync"] = "history"
|
||||
) -> ImportJob:
|
||||
"""`source_id` is a lane key: another user's lane creates that user's job."""
|
||||
if self.source_enabled is not None and not self.source_enabled(source_id):
|
||||
raise SourceAccessError(SourceAccessErrorCode.GRANT_REQUIRED)
|
||||
return self.job_store.create(user_id="local", source_id=source_id, operation=operation)
|
||||
user_id, bare_source_id = split_lane(source_id)
|
||||
return self.job_store.create(user_id=user_id, source_id=bare_source_id, operation=operation)
|
||||
|
||||
def enqueue(self, job_id: str) -> ImportJob:
|
||||
if job_id in self._drains:
|
||||
raise InvalidJobTransitionError("import_cleanup_in_progress")
|
||||
job = self.job_store.require(job_id)
|
||||
if job.source_id not in self.adapters:
|
||||
raise UnknownSourceError(job.source_id)
|
||||
if self.source_enabled is not None and not self.source_enabled(job.source_id):
|
||||
lane = self.lane_of(job)
|
||||
if lane not in self.adapters:
|
||||
raise UnknownSourceError(lane)
|
||||
if self.source_enabled is not None and not self.source_enabled(lane):
|
||||
return job
|
||||
existing = self._tasks.get(job_id)
|
||||
if existing is None or existing.done():
|
||||
self._tasks[job_id] = asyncio.create_task(
|
||||
self._execute(
|
||||
job_id,
|
||||
source_id=job.source_id,
|
||||
source_id=lane,
|
||||
operation=job.operation,
|
||||
)
|
||||
)
|
||||
@@ -130,8 +140,10 @@ class ImportSupervisor:
|
||||
# pending history. Each resumed runner will yield after one bounded slice.
|
||||
for status in (ImportStatus.INTERRUPTED, ImportStatus.PENDING):
|
||||
for job in self.job_store.list_jobs(status=status):
|
||||
if job.source_id in self.adapters:
|
||||
if self.source_enabled is not None and not self.source_enabled(job.source_id):
|
||||
if self.lane_of(job) in self.adapters:
|
||||
if self.source_enabled is not None and not self.source_enabled(
|
||||
self.lane_of(job)
|
||||
):
|
||||
continue
|
||||
if job.status is not ImportStatus.PENDING:
|
||||
self.job_store.queue_for_resume(job.job_id)
|
||||
@@ -227,7 +239,7 @@ class ImportSupervisor:
|
||||
}:
|
||||
return
|
||||
if self.source_enabled is not None and not await in_the_background(
|
||||
self.source_enabled, job.source_id
|
||||
self.source_enabled, self.lane_of(job)
|
||||
):
|
||||
return
|
||||
runner = ImportRunner(
|
||||
@@ -238,7 +250,7 @@ class ImportSupervisor:
|
||||
sdk_queue=self.sdk_queue,
|
||||
sdk_authority=self.sdk_authority,
|
||||
)
|
||||
adapter = self.adapters[job.source_id]
|
||||
adapter = self.adapters[self.lane_of(job)]
|
||||
if self.prepare_adapter is not None:
|
||||
adapter = await self.prepare_adapter(job, adapter)
|
||||
|
||||
@@ -273,7 +285,7 @@ class ImportSupervisor:
|
||||
and retry_attempt < self.max_history_retries
|
||||
and (
|
||||
self.source_enabled is None
|
||||
or await in_the_background(self.source_enabled, current.source_id)
|
||||
or await in_the_background(self.source_enabled, self.lane_of(current))
|
||||
)
|
||||
):
|
||||
delay = self.retry_base_seconds * (2**retry_attempt)
|
||||
|
||||
@@ -7,9 +7,9 @@ between pages. The matches are kept here under an opaque ``window_id`` that
|
||||
the client passes back with the next offset.
|
||||
|
||||
A window is bound to the filters it was searched with (query, source,
|
||||
statuses, scene). It expires after ``ttl_seconds`` without being read, and the
|
||||
store keeps at most ``max_windows`` windows, dropping the least recently read
|
||||
first.
|
||||
statuses, scene) and to the user whose library it searched. It expires after
|
||||
``ttl_seconds`` without being read, and the store keeps at most
|
||||
``max_windows`` windows, dropping the least recently read first.
|
||||
Only memory identities are kept, never content: each page is read fresh from
|
||||
the SDK, so an edit or a delete shows on the next page read.
|
||||
"""
|
||||
@@ -28,13 +28,17 @@ from one_memory.hy_sdk_types import LibraryHit
|
||||
DEFAULT_WINDOW_TTL_SECONDS = 300.0
|
||||
DEFAULT_MAX_WINDOWS = 32
|
||||
|
||||
WindowKey = tuple[str, str | None, tuple[str, ...], str]
|
||||
WindowKey = tuple[str, str | None, tuple[str, ...], str, str]
|
||||
|
||||
|
||||
def window_key(
|
||||
query: str, source: str | None, statuses: list[str], scene: str = "all"
|
||||
query: str,
|
||||
source: str | None,
|
||||
statuses: list[str],
|
||||
scene: str = "all",
|
||||
user_id: str = "local",
|
||||
) -> WindowKey:
|
||||
return (query, source, tuple(sorted(set(statuses))), scene)
|
||||
return (query, source, tuple(sorted(set(statuses))), scene, user_id)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
|
||||
@@ -256,24 +256,19 @@ def record_capture_rejection(
|
||||
|
||||
|
||||
def _submit_live_memory(store: EvidenceStore, record: EvidenceRecord, *, changed: bool) -> None:
|
||||
"""Add the record to its conversation's live assembling row.
|
||||
|
||||
The turn rule decides what is written (`turn_batches`): a turn is
|
||||
complete when the next user message arrives, batches are 5 turns or
|
||||
8000 characters, and Core writes the rest after 10 quiet minutes.
|
||||
"""
|
||||
if not changed or not _sync_open(store.database_path, record.source_id):
|
||||
return
|
||||
from one_memory.conversation_processing import _conversation_chunks
|
||||
from one_memory.memory_server.durable_queue import DurableWriteQueue
|
||||
from one_memory.memory_server.unified_entry import UnifiedWriteIntake
|
||||
|
||||
intake = UnifiedWriteIntake(DurableWriteQueue(store.database_path, evidence=store))
|
||||
for chunk in _conversation_chunks(
|
||||
store.list_conversation(record.evidence_id),
|
||||
max_turns=12,
|
||||
completed_only=True,
|
||||
):
|
||||
if (
|
||||
record in chunk
|
||||
and any(item.role == "user" for item in chunk)
|
||||
and any(item.role == "assistant" for item in chunk)
|
||||
):
|
||||
intake.submit_records(chunk, priority="live")
|
||||
DurableWriteQueue(store.database_path, evidence=store).assemble_live_records(
|
||||
(record,), file_key=f"hook:{record.conversation_id}"
|
||||
)
|
||||
|
||||
|
||||
def _conversation(payload: dict[str, object]) -> str | None:
|
||||
|
||||
@@ -20,8 +20,11 @@ def start_logging_to_file(data_dir: Path) -> Path:
|
||||
nowhere. That includes the loop watchdog: the one report that names what
|
||||
stalled the app was unreadable in exactly the situation it exists for.
|
||||
|
||||
The active diagnostic collection level controls Core, HY SDK, and provider
|
||||
client records. Uvicorn keeps its request log on its own non-propagating
|
||||
The active diagnostic collection level controls Core and provider client
|
||||
records. The HY SDK keeps its own text log, hy_memory.log under the SDK's
|
||||
log directory, and stops propagating here once its first client is built;
|
||||
`confine_hy_sdk_logging` then forwards only its warnings and errors to
|
||||
this file. Uvicorn keeps its request log on its own non-propagating
|
||||
loggers.
|
||||
"""
|
||||
destination = log_path(data_dir)
|
||||
@@ -49,3 +52,53 @@ def start_logging_to_file(data_dir: Path) -> Path:
|
||||
if root.level > logging.INFO or root.level == logging.NOTSET:
|
||||
root.setLevel(logging.INFO)
|
||||
return destination
|
||||
|
||||
|
||||
HY_SDK_LOGGER = "hy_memory"
|
||||
|
||||
|
||||
class _CoreLogForwarder(logging.Handler):
|
||||
"""Hands HY warnings to Core's own core.log handler, rotation and format."""
|
||||
|
||||
def __init__(self, target: logging.Handler) -> None:
|
||||
super().__init__(level=logging.WARNING)
|
||||
self.target = target
|
||||
|
||||
def emit(self, record: logging.LogRecord) -> None:
|
||||
self.target.handle(record)
|
||||
|
||||
|
||||
def confine_hy_sdk_logging() -> None:
|
||||
"""Keep HY's text log in hy_memory.log, with only its problems in core.log.
|
||||
|
||||
HY's first client sets up hy_memory.log plus a stdout and a stderr stream
|
||||
on the "hy_memory" logger, and stops it propagating to the root. Under
|
||||
launchd those streams are service.log and service-error.log, so every SDK
|
||||
line was written twice, and none reached core.log. Drop the streams and
|
||||
forward WARNING and above to core.log, which the Debug panel reads; the
|
||||
SDK keeps conversation text out of those levels.
|
||||
|
||||
Idempotent. Does nothing until HY has configured its logger, because a
|
||||
propagating logger already reaches core.log through the root.
|
||||
"""
|
||||
sdk = logging.getLogger(HY_SDK_LOGGER)
|
||||
if sdk.propagate:
|
||||
return
|
||||
for handler in list(sdk.handlers):
|
||||
if isinstance(handler, logging.StreamHandler) and not isinstance(
|
||||
handler, logging.FileHandler
|
||||
):
|
||||
sdk.removeHandler(handler)
|
||||
if any(isinstance(handler, _CoreLogForwarder) for handler in sdk.handlers):
|
||||
return
|
||||
core = next(
|
||||
(
|
||||
handler
|
||||
for handler in logging.getLogger().handlers
|
||||
if isinstance(handler, RotatingFileHandler)
|
||||
and Path(handler.baseFilename).name == "core.log"
|
||||
),
|
||||
None,
|
||||
)
|
||||
if core is not None:
|
||||
sdk.addHandler(_CoreLogForwarder(core))
|
||||
@@ -11,10 +11,11 @@ import asyncio
|
||||
import sqlite3
|
||||
from collections.abc import Sequence
|
||||
from datetime import UTC, datetime
|
||||
from functools import partial
|
||||
from typing import Any, Literal, cast
|
||||
from uuid import UUID
|
||||
|
||||
from fastapi import APIRouter, BackgroundTasks, HTTPException, Request
|
||||
from fastapi import APIRouter, BackgroundTasks, HTTPException, Query, Request
|
||||
from fastapi.params import Depends
|
||||
from pydantic import BaseModel, ConfigDict, Field, field_validator
|
||||
|
||||
@@ -34,6 +35,7 @@ from one_memory.memory_server.durable_queue import DurableWriteQueue
|
||||
from one_memory.memory_server.unified_entry import UnifiedWriteIntake
|
||||
from one_memory.memory_tasks import MemoryTaskStatus, MemoryTaskStore
|
||||
from one_memory.secrets import SecretStoreUnavailableError
|
||||
from one_memory.user_scope import DEFAULT_USER_ID, USER_ID_PATTERN, UserId
|
||||
|
||||
SOURCE_ID = "one-memory-manual"
|
||||
MemoryAddState = Literal[
|
||||
@@ -53,6 +55,8 @@ class MemoryAddInput(BaseModel):
|
||||
request_id: UUID
|
||||
text: str = Field(min_length=1, max_length=12_000)
|
||||
scope: Literal["all-apps"]
|
||||
#: Whose memory this is. The App never sends it and writes `local`.
|
||||
user_id: UserId = DEFAULT_USER_ID
|
||||
|
||||
@field_validator("text")
|
||||
@classmethod
|
||||
@@ -108,7 +112,11 @@ class MemoryAdditionStore:
|
||||
|
||||
def save(self, addition: MemoryAddInput) -> EvidenceRecord:
|
||||
now = datetime.now(UTC)
|
||||
if addition.user_id != DEFAULT_USER_ID and not self.sdk_authority:
|
||||
# Legacy authority keeps one person's library only.
|
||||
raise ValueError("memory_add_user_requires_sdk_authority")
|
||||
value = EvidenceInput(
|
||||
user_id=addition.user_id,
|
||||
source_id=SOURCE_ID,
|
||||
conversation_id=f"note:{addition.request_id}",
|
||||
message_id=str(addition.request_id),
|
||||
@@ -130,10 +138,18 @@ class MemoryAdditionStore:
|
||||
raise RuntimeError("sdk_memory_add_intake_unavailable")
|
||||
record = materialize_write_input(value, observed_at=now)
|
||||
with main_database(self.write_queue.database_path).read() as database:
|
||||
# No grant row is written here: a missing row means allowed,
|
||||
# only an explicit revocation refuses (same as the claim gate).
|
||||
grant = database.execute(
|
||||
"SELECT revoked_at FROM source_grants WHERE user_id='local' AND source_id=?",
|
||||
(SOURCE_ID,),
|
||||
).fetchone()
|
||||
existing = database.execute(
|
||||
"SELECT request_id FROM memory_note_requests WHERE note_id=?",
|
||||
(record.evidence_id,),
|
||||
).fetchone()
|
||||
if grant is not None and grant[0] is not None:
|
||||
raise ValueError("memory_add_source_revoked")
|
||||
if existing is not None:
|
||||
current = self.require(record.evidence_id)
|
||||
if current.verbatim != record.verbatim:
|
||||
@@ -192,7 +208,7 @@ class MemoryAdditionStore:
|
||||
)
|
||||
return self.require(value.evidence_id)
|
||||
|
||||
def require(self, evidence_id: str) -> EvidenceRecord:
|
||||
def require(self, evidence_id: str, *, user_id: str = DEFAULT_USER_ID) -> EvidenceRecord:
|
||||
if self.sdk_authority:
|
||||
if self.write_queue is None:
|
||||
raise KeyError(evidence_id)
|
||||
@@ -211,11 +227,11 @@ class MemoryAdditionStore:
|
||||
(item for item in payload.input_records if item.evidence_id == evidence_id),
|
||||
None,
|
||||
)
|
||||
if record is None:
|
||||
if record is None or record.user_id != user_id:
|
||||
raise KeyError(evidence_id)
|
||||
return record
|
||||
record = self.evidence.get(evidence_id)
|
||||
if record is None or record.user_id != "local" or record.source_id != SOURCE_ID:
|
||||
if record is None or record.user_id != user_id or record.source_id != SOURCE_ID:
|
||||
raise KeyError(evidence_id)
|
||||
metadata = record.origin_meta.get(EvidenceOrigin.MANUAL_IMPORT)
|
||||
if metadata is None or metadata.raw_meta.get("input_mode") != "direct_memory_note":
|
||||
@@ -229,8 +245,8 @@ class MemoryAdditionStore:
|
||||
raise ValueError("memory_add_source_revoked")
|
||||
return record
|
||||
|
||||
def receipt(self, evidence_id: str) -> MemoryAddReceipt:
|
||||
record = self.require(evidence_id)
|
||||
def receipt(self, evidence_id: str, *, user_id: str = DEFAULT_USER_ID) -> MemoryAddReceipt:
|
||||
record = self.require(evidence_id, user_id=user_id)
|
||||
if self.sdk_authority:
|
||||
assert self.write_queue is not None
|
||||
with main_database(self.write_queue.database_path).read() as database:
|
||||
@@ -292,10 +308,15 @@ class MemoryAdditionStore:
|
||||
error=error,
|
||||
)
|
||||
|
||||
def receipt_for_request(self, request_id: UUID) -> MemoryAddReceipt:
|
||||
def receipt_for_request(
|
||||
self, request_id: UUID, *, user_id: str = DEFAULT_USER_ID
|
||||
) -> MemoryAddReceipt:
|
||||
identity = str(request_id)
|
||||
if self.sdk_authority:
|
||||
# The Evidence id carries the user, so another user's request id
|
||||
# names a different note and reads as not found.
|
||||
probe = EvidenceInput(
|
||||
user_id=user_id,
|
||||
source_id=SOURCE_ID,
|
||||
conversation_id=f"note:{identity}",
|
||||
message_id=identity,
|
||||
@@ -308,16 +329,16 @@ class MemoryAdditionStore:
|
||||
)
|
||||
},
|
||||
)
|
||||
return self.receipt(probe.evidence_id)
|
||||
return self.receipt(probe.evidence_id, user_id=user_id)
|
||||
with main_database(self.evidence.database_path).read() as db:
|
||||
row = db.execute(
|
||||
"SELECT evidence_id FROM evidence WHERE user_id='local' AND source_id=? "
|
||||
"SELECT evidence_id FROM evidence WHERE user_id=? AND source_id=? "
|
||||
"AND conversation_id=? AND message_id=?",
|
||||
(SOURCE_ID, f"note:{identity}", identity),
|
||||
(user_id, SOURCE_ID, f"note:{identity}", identity),
|
||||
).fetchone()
|
||||
if row is None:
|
||||
raise KeyError(identity)
|
||||
return self.receipt(str(row[0]))
|
||||
return self.receipt(str(row[0]), user_id=user_id)
|
||||
|
||||
|
||||
def memory_add_router(dependencies: Sequence[Depends]) -> APIRouter:
|
||||
@@ -368,14 +389,20 @@ def memory_add_router(dependencies: Sequence[Depends]) -> APIRouter:
|
||||
record = await asyncio.to_thread(additions.save, addition)
|
||||
except ValueError as error:
|
||||
raise HTTPException(409, str(error)) from error
|
||||
saved = await asyncio.to_thread(additions.receipt, record.evidence_id)
|
||||
saved = await asyncio.to_thread(
|
||||
partial(additions.receipt, record.evidence_id, user_id=addition.user_id)
|
||||
)
|
||||
background.add_task(schedule, additions, record)
|
||||
return saved
|
||||
|
||||
@router.get("/additions/{request_id}", response_model=MemoryAddReceipt)
|
||||
def memory_add_status(request_id: UUID, request: Request) -> MemoryAddReceipt:
|
||||
def memory_add_status(
|
||||
request_id: UUID,
|
||||
request: Request,
|
||||
user_id: str = Query(default=DEFAULT_USER_ID, pattern=USER_ID_PATTERN),
|
||||
) -> MemoryAddReceipt:
|
||||
try:
|
||||
return store(request).receipt_for_request(request_id)
|
||||
return store(request).receipt_for_request(request_id, user_id=user_id)
|
||||
except KeyError as error:
|
||||
raise HTTPException(404, "memory_add_not_found") from error
|
||||
except ValueError as error:
|
||||
|
||||
@@ -75,7 +75,11 @@ def batch_id(*, internal_session_id: str, first_round: int, last_round: int) ->
|
||||
INTERNAL_SESSION_ID_MAX = 128
|
||||
|
||||
|
||||
def internal_session_id_for(conversation_id: str, source_id: str) -> str:
|
||||
def internal_session_id_for(
|
||||
conversation_id: str,
|
||||
source_id: str,
|
||||
user_id: str = "local",
|
||||
) -> str:
|
||||
"""Normalise a conversation identity to the durable ``internal_session_id``.
|
||||
|
||||
This is the single derivation shared by every Evidence-first intake path.
|
||||
@@ -86,7 +90,17 @@ def internal_session_id_for(conversation_id: str, source_id: str) -> str:
|
||||
per-session serialisation split). Only a value that the contract would have
|
||||
rejected before it ever reached the queue is replaced by the versioned
|
||||
session id, which is deterministic for a given (source, conversation).
|
||||
|
||||
Another user's conversation is always derived with the user in it: two
|
||||
Hermes profiles can hold the same session id, and sharing a session key
|
||||
would serialise and batch one person's turns behind the other's. `local`
|
||||
keeps the identity it always had.
|
||||
"""
|
||||
if user_id != "local":
|
||||
return session_id(
|
||||
harness_id=source_id,
|
||||
external_session_id=f"user:{user_id}|{conversation_id}",
|
||||
)
|
||||
if len(conversation_id) <= INTERNAL_SESSION_ID_MAX:
|
||||
return conversation_id
|
||||
derived = session_id(harness_id=source_id, external_session_id=conversation_id)
|
||||
|
||||
@@ -24,10 +24,11 @@ from collections.abc import Awaitable, Callable
|
||||
from dataclasses import dataclass
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
from typing import Any, Literal
|
||||
from uuid import uuid4
|
||||
|
||||
from one_memory.background_work import in_the_background
|
||||
from one_memory.conversation_processing import TURN_IDLE_FLUSH_SECONDS, turn_batches
|
||||
from one_memory.database_access import main_database
|
||||
from one_memory.diagnostic_log import diagnostic_event
|
||||
from one_memory.imports.models import EvidenceOrigin, EvidenceRecord
|
||||
@@ -51,7 +52,9 @@ from one_memory.memory_tasks import (
|
||||
from one_memory.memory_tasks import (
|
||||
POLICY_STOPPED_CODES as _TASK_POLICY_STOPPED_CODES,
|
||||
)
|
||||
from one_memory.sdk_migration_log import error_chain_text
|
||||
from one_memory.service_levels import ServiceLevel, service_level_context
|
||||
from one_memory.user_scope import DEFAULT_USER_ID, lane_key, other_user_lane_prefix
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -240,14 +243,16 @@ PERMISSION_REQUIRED_CODES = frozenset(
|
||||
)
|
||||
|
||||
#: Model-side codes the SDK write coordinator raises before it can lease a
|
||||
#: client (no active profile, missing credential, secure storage down). They prove
|
||||
#: nothing was sent, so the write parks instead of being lost. Transport codes
|
||||
#: that can arrive after a request was in flight stay `unknown`.
|
||||
#: client (no active profile, missing credential, secure storage down, or the
|
||||
#: client failed to construct). They prove nothing was sent, so the write
|
||||
#: parks instead of being lost. Transport codes that can arrive after a
|
||||
#: request was in flight stay `unknown`.
|
||||
MODEL_NOT_READY_CODES = frozenset(
|
||||
{
|
||||
"model_profile_not_active",
|
||||
"model_secret_unavailable",
|
||||
"secure_storage_unavailable",
|
||||
"hy_memory_client_unavailable",
|
||||
}
|
||||
)
|
||||
|
||||
@@ -291,6 +296,23 @@ _VALID_JOB_STATES = frozenset(
|
||||
#: A row in one of these states can never be moved back to a live one.
|
||||
_TERMINAL_WRITE_STATES = frozenset({COMPLETED, FAILED, DEAD, CANCELLED})
|
||||
|
||||
#: The legacy task rows a write job can be linked to. The SDK cutover drops
|
||||
#: them (`target_schema.LEGACY_TABLES_IN_DROP_ORDER`, inputs before tasks), so
|
||||
#: the job-only paths must not assume any of them.
|
||||
_LEGACY_TASK_LINK_TABLES = ("memory_tasks", "memory_task_inputs", "memory_write_job_evidence")
|
||||
|
||||
|
||||
def _legacy_task_links_present(connection: sqlite3.Connection) -> bool:
|
||||
present = {
|
||||
str(row[0])
|
||||
for row in connection.execute(
|
||||
"SELECT name FROM sqlite_master WHERE type='table' AND name IN (?,?,?)",
|
||||
_LEGACY_TASK_LINK_TABLES,
|
||||
)
|
||||
}
|
||||
return present == set(_LEGACY_TASK_LINK_TABLES)
|
||||
|
||||
|
||||
#: Columns a current schema declares but an older `memory_write_jobs` may lack.
|
||||
#: `CREATE TABLE IF NOT EXISTS` is a no-op for an existing table, so without
|
||||
#: this an upgraded install would keep an older queue that every claim/lease
|
||||
@@ -324,6 +346,27 @@ EVENT_TIME_WINDOW = timedelta(minutes=10)
|
||||
DIRECT_WRITE_SOURCES = frozenset({"one-memory-manual"})
|
||||
|
||||
|
||||
def request_user_id(request: WriteRequest) -> str:
|
||||
"""Whose write this is: its frozen input's user, `local` for id-only rows."""
|
||||
users = {record.user_id for record in request.input_records}
|
||||
if len(users) > 1:
|
||||
raise ValueError("durable_write_multiple_users")
|
||||
return next(iter(users), DEFAULT_USER_ID)
|
||||
|
||||
|
||||
def request_source_key(request: WriteRequest) -> str:
|
||||
"""The FIFO/claim key of a write: its source, per user.
|
||||
|
||||
`local` keeps the bare source id it always had. Another user's Hermes
|
||||
lane is `hermes@user`, so one person's running job never holds the other
|
||||
person's queue and the claim gate answers for that user's grant.
|
||||
"""
|
||||
return lane_key(
|
||||
request_user_id(request),
|
||||
request.source_id or request.internal_session_id,
|
||||
)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class DurableWriteRecord:
|
||||
job_id: str
|
||||
@@ -841,7 +884,7 @@ class DurableWriteQueue:
|
||||
job_id,
|
||||
queue_seq,
|
||||
request.internal_session_id,
|
||||
request.source_id or request.internal_session_id,
|
||||
request_source_key(request),
|
||||
request.batch_identity.batch_id,
|
||||
key,
|
||||
payload,
|
||||
@@ -894,16 +937,32 @@ class DurableWriteQueue:
|
||||
priority: str,
|
||||
connection: sqlite3.Connection,
|
||||
) -> None:
|
||||
"""Append fresh-install input to an assembling queue row.
|
||||
"""Append input to its conversation's assembling queue row.
|
||||
|
||||
The row is not claimable until the scanner commits EOF and finalizes
|
||||
it. No Evidence table is involved.
|
||||
The row is not claimable. `finalize_sdk_file` turns what is complete
|
||||
into write jobs (`turn_batches`) and keeps the rest here for the next
|
||||
increment; `flush_idle_assemblies` writes a live row's rest once the
|
||||
conversation has been quiet. `updated_at` moves only when a record is
|
||||
new or revised, so a re-read does not keep a quiet conversation open.
|
||||
No Evidence table is involved.
|
||||
"""
|
||||
assembly_key = (
|
||||
f"assembly:{record.user_id}:{record.source_id}:"
|
||||
f"{hashlib.sha256(file_key.encode()).hexdigest()}:"
|
||||
f"{hashlib.sha256(record.conversation_id.encode()).hexdigest()}"
|
||||
)
|
||||
session_key = internal_session_id_for(
|
||||
record.conversation_id, record.source_id, record.user_id
|
||||
)
|
||||
written = connection.execute(
|
||||
"SELECT 1 FROM memory_write_jobs AS job, json_each(job.evidence_ids_json) AS ref "
|
||||
"WHERE job.session_key=? AND job.state!='assembling' AND ref.value=? LIMIT 1",
|
||||
(session_key, record.evidence_id),
|
||||
).fetchone()
|
||||
if written is not None:
|
||||
# A re-read of a record a job already holds: staging it again
|
||||
# would regroup it with later turns into a second job.
|
||||
return
|
||||
row = connection.execute(
|
||||
"SELECT job_id,payload_json,priority FROM memory_write_jobs "
|
||||
"WHERE idempotency_key=? AND state='assembling'",
|
||||
@@ -914,8 +973,20 @@ class DurableWriteQueue:
|
||||
payload = json.loads(str(row["payload_json"]))
|
||||
records = list(payload.get("input_records") or [])
|
||||
dumped = record.model_dump(mode="json")
|
||||
if not any(item.get("evidence_id") == record.evidence_id for item in records):
|
||||
index = next(
|
||||
(
|
||||
position
|
||||
for position, item in enumerate(records)
|
||||
if item.get("evidence_id") == record.evidence_id
|
||||
),
|
||||
None,
|
||||
)
|
||||
changed = index is None or records[index] != dumped
|
||||
if index is None:
|
||||
records.append(dumped)
|
||||
elif changed:
|
||||
# A revision replaces the carried copy in place.
|
||||
records[index] = dumped
|
||||
now = datetime.now(UTC).isoformat()
|
||||
payload_json = json.dumps(
|
||||
{
|
||||
@@ -926,15 +997,13 @@ class DurableWriteQueue:
|
||||
sort_keys=True,
|
||||
)
|
||||
if row is not None:
|
||||
merged_priority = "live" if "live" in (row["priority"], priority) else "normal"
|
||||
if not changed and merged_priority == row["priority"]:
|
||||
return
|
||||
connection.execute(
|
||||
"UPDATE memory_write_jobs SET payload_json=?,priority=?,updated_at=? "
|
||||
"WHERE job_id=?",
|
||||
(
|
||||
payload_json,
|
||||
"live" if row["priority"] == "live" or priority == "live" else "normal",
|
||||
now,
|
||||
str(row["job_id"]),
|
||||
),
|
||||
"UPDATE memory_write_jobs SET payload_json=?,priority=?,"
|
||||
"updated_at=CASE WHEN ? THEN ? ELSE updated_at END WHERE job_id=?",
|
||||
(payload_json, merged_priority, int(changed), now, str(row["job_id"])),
|
||||
)
|
||||
return
|
||||
queue_seq = int(
|
||||
@@ -952,8 +1021,8 @@ class DurableWriteQueue:
|
||||
(
|
||||
str(uuid4()),
|
||||
queue_seq,
|
||||
internal_session_id_for(record.conversation_id, record.source_id),
|
||||
record.source_id,
|
||||
session_key,
|
||||
lane_key(record.user_id, record.source_id),
|
||||
assembly_key,
|
||||
assembly_key,
|
||||
payload_json,
|
||||
@@ -982,92 +1051,218 @@ class DurableWriteQueue:
|
||||
source_id: str,
|
||||
file_key: str,
|
||||
connection: sqlite3.Connection,
|
||||
flush: bool = True,
|
||||
) -> tuple[str, ...]:
|
||||
"""Write what one file's assembling rows hold that the turn rule completes.
|
||||
|
||||
`flush` (history at end of file) completes the last turn and writes a
|
||||
short batch; without it (live polling) those stay in the row.
|
||||
"""
|
||||
# Two users' roots can hold a file with the same relative key (each
|
||||
# Hermes profile has its own `state.db`): only this user's rows.
|
||||
assembly_prefix = f"assembly:{user_id}:{source_id}:"
|
||||
rows = connection.execute(
|
||||
"SELECT * FROM memory_write_jobs WHERE state='assembling' "
|
||||
"AND source_key=? ORDER BY queue_seq",
|
||||
(source_id,),
|
||||
"AND source_key=? AND substr(idempotency_key,1,?)=? ORDER BY queue_seq",
|
||||
(lane_key(user_id, source_id), len(assembly_prefix), assembly_prefix),
|
||||
).fetchall()
|
||||
finalized = []
|
||||
finalized: list[str] = []
|
||||
for row in rows:
|
||||
payload = json.loads(str(row["payload_json"]))
|
||||
if payload.get("file_key") != file_key:
|
||||
continue
|
||||
records = tuple(
|
||||
EvidenceRecord.model_validate(item)
|
||||
for item in payload.get("input_records") or []
|
||||
if item.get("user_id") == user_id
|
||||
finalized.extend(
|
||||
self._write_assembled_turns(connection, row, user_id=user_id, flush=flush)
|
||||
)
|
||||
if not records:
|
||||
connection.execute(
|
||||
"DELETE FROM memory_write_jobs WHERE job_id=?",
|
||||
(str(row["job_id"]),),
|
||||
return tuple(finalized)
|
||||
|
||||
def assemble_live_records(
|
||||
self,
|
||||
records: tuple[EvidenceRecord, ...],
|
||||
*,
|
||||
file_key: str,
|
||||
) -> tuple[str, ...]:
|
||||
"""Stage live records and write what is complete, in one transaction.
|
||||
|
||||
For live capture outside the import runner (host hooks): the same
|
||||
assembling rows, turn rule and idle flush as live file polling.
|
||||
"""
|
||||
if not records:
|
||||
return ()
|
||||
first = records[0]
|
||||
with (
|
||||
main_database(self.database_path).write(row_factory=sqlite3.Row) as connection,
|
||||
connection,
|
||||
):
|
||||
for record in records:
|
||||
self.stage_sdk_input(
|
||||
record, file_key=file_key, priority="live", connection=connection
|
||||
)
|
||||
continue
|
||||
first = records[0]
|
||||
session_id = internal_session_id_for(
|
||||
first.conversation_id,
|
||||
first.source_id,
|
||||
)
|
||||
material = ":".join(f"{item.evidence_id}:{item.content_hash}" for item in records)
|
||||
request_id = f"turn:v2:{hashlib.sha256(material.encode()).hexdigest()}"
|
||||
existing = connection.execute(
|
||||
"SELECT job_id FROM memory_write_jobs WHERE idempotency_key=? AND job_id!=?",
|
||||
(request_id, str(row["job_id"])),
|
||||
).fetchone()
|
||||
if existing is not None:
|
||||
connection.execute(
|
||||
"DELETE FROM memory_write_jobs WHERE job_id=?",
|
||||
(str(row["job_id"]),),
|
||||
)
|
||||
finalized.append(str(existing["job_id"]))
|
||||
continue
|
||||
request = WriteRequest(
|
||||
internal_session_id=session_id,
|
||||
return self.finalize_sdk_file(
|
||||
user_id=first.user_id,
|
||||
source_id=first.source_id,
|
||||
batch_identity=BatchIdentity(
|
||||
internal_session_id=session_id,
|
||||
first_round=1,
|
||||
last_round=1,
|
||||
),
|
||||
evidence_ids=[item.evidence_id for item in records],
|
||||
input_records=records,
|
||||
idempotency_key=request_id,
|
||||
priority="live" if str(row["priority"]) == "live" else "normal",
|
||||
event_at=max(
|
||||
(item.created_at for item in records if item.created_at),
|
||||
default=first.imported_at,
|
||||
),
|
||||
time_quality="exact" if any(item.created_at for item in records) else "approximate",
|
||||
file_key=file_key,
|
||||
connection=connection,
|
||||
flush=False,
|
||||
)
|
||||
|
||||
def flush_idle_assemblies(self, *, now: datetime | None = None) -> tuple[str, ...]:
|
||||
"""Write the rest of every live conversation quiet for 10 minutes.
|
||||
|
||||
Its last turn counts as complete and a batch short of 5 turns is
|
||||
written. History rows are left alone: their end of file flushes them.
|
||||
"""
|
||||
current = now or datetime.now(UTC)
|
||||
cutoff = (current - timedelta(seconds=TURN_IDLE_FLUSH_SECONDS)).isoformat()
|
||||
query = (
|
||||
"SELECT * FROM memory_write_jobs WHERE state='assembling' AND priority='live' "
|
||||
"AND updated_at<=? ORDER BY queue_seq"
|
||||
)
|
||||
# A read first: most ticks find nothing and must not take the writer.
|
||||
with main_database(self.database_path).read(row_factory=sqlite3.Row) as connection:
|
||||
if connection.execute(query, (cutoff,)).fetchone() is None:
|
||||
return ()
|
||||
finalized: list[str] = []
|
||||
with (
|
||||
main_database(self.database_path).write(row_factory=sqlite3.Row) as connection,
|
||||
connection,
|
||||
):
|
||||
for row in connection.execute(query, (cutoff,)).fetchall():
|
||||
finalized.extend(self._write_assembled_turns(connection, row, flush=True))
|
||||
return tuple(finalized)
|
||||
|
||||
def _write_assembled_turns(
|
||||
self,
|
||||
connection: sqlite3.Connection,
|
||||
row: sqlite3.Row,
|
||||
*,
|
||||
flush: bool,
|
||||
user_id: str | None = None,
|
||||
) -> list[str]:
|
||||
"""Turn one assembling row's complete batches into jobs; keep the carry."""
|
||||
payload = json.loads(str(row["payload_json"]))
|
||||
records = tuple(
|
||||
EvidenceRecord.model_validate(item)
|
||||
for item in payload.get("input_records") or []
|
||||
if user_id is None or item.get("user_id") == user_id
|
||||
)
|
||||
grouped = turn_batches(records, flush=flush)
|
||||
priority: Literal["live", "normal"] = "live" if str(row["priority"]) == "live" else "normal"
|
||||
job_ids = [
|
||||
self._insert_turn_batch(connection, batch, priority=priority)
|
||||
for batch in grouped.batches
|
||||
]
|
||||
if not grouped.carry:
|
||||
connection.execute(
|
||||
"UPDATE memory_write_jobs SET session_key=?,batch_id=?,"
|
||||
"idempotency_key=?,payload_json=?,evidence_ids_json=?,state='pending',"
|
||||
"event_at=?,time_quality=?,updated_at=? WHERE job_id=?",
|
||||
"DELETE FROM memory_write_jobs WHERE job_id=?", (str(row["job_id"]),)
|
||||
)
|
||||
elif grouped.batches or len(grouped.carry) != len(records):
|
||||
# `updated_at` stays: the idle clock counts new messages only.
|
||||
connection.execute(
|
||||
"UPDATE memory_write_jobs SET payload_json=? WHERE job_id=?",
|
||||
(
|
||||
session_id,
|
||||
request.batch_identity.batch_id,
|
||||
request_id,
|
||||
json.dumps(
|
||||
request.model_dump(
|
||||
mode="json",
|
||||
exclude={"event_at", "time_quality"},
|
||||
),
|
||||
{
|
||||
**payload,
|
||||
"input_records": [
|
||||
record.model_dump(mode="json") for record in grouped.carry
|
||||
],
|
||||
},
|
||||
sort_keys=True,
|
||||
),
|
||||
json.dumps(list(request.evidence_ids)),
|
||||
(
|
||||
request.event_at.isoformat()
|
||||
if request.event_at
|
||||
else datetime.now(UTC).isoformat()
|
||||
),
|
||||
request.time_quality,
|
||||
datetime.now(UTC).isoformat(),
|
||||
str(row["job_id"]),
|
||||
),
|
||||
)
|
||||
finalized.append(str(row["job_id"]))
|
||||
return tuple(finalized)
|
||||
return job_ids
|
||||
|
||||
def _insert_turn_batch(
|
||||
self,
|
||||
connection: sqlite3.Connection,
|
||||
records: tuple[EvidenceRecord, ...],
|
||||
*,
|
||||
priority: Literal["live", "normal"],
|
||||
) -> str:
|
||||
"""One pending job for one batch of whole turns, keyed by its content.
|
||||
|
||||
Same records, same key: a re-read returns the job already written.
|
||||
"""
|
||||
first = records[0]
|
||||
session_id = internal_session_id_for(first.conversation_id, first.source_id, first.user_id)
|
||||
material = ":".join(f"{item.evidence_id}:{item.content_hash}" for item in records)
|
||||
request_id = f"turn:v2:{hashlib.sha256(material.encode()).hexdigest()}"
|
||||
existing = connection.execute(
|
||||
"SELECT job_id FROM memory_write_jobs WHERE idempotency_key=?",
|
||||
(request_id,),
|
||||
).fetchone()
|
||||
if existing is not None:
|
||||
return str(existing["job_id"])
|
||||
request = WriteRequest(
|
||||
internal_session_id=session_id,
|
||||
source_id=first.source_id,
|
||||
batch_identity=BatchIdentity(
|
||||
internal_session_id=session_id,
|
||||
first_round=1,
|
||||
last_round=1,
|
||||
),
|
||||
evidence_ids=[item.evidence_id for item in records],
|
||||
input_records=records,
|
||||
idempotency_key=request_id,
|
||||
priority=priority,
|
||||
)
|
||||
# The batch's time is its first user message's, as for every other job.
|
||||
event_at, time_quality = self._event_time_for_request(request)
|
||||
now = datetime.now(UTC)
|
||||
job_id = str(uuid4())
|
||||
queue_seq = int(
|
||||
connection.execute(
|
||||
"SELECT COALESCE(MAX(queue_seq),0)+1 FROM memory_write_jobs"
|
||||
).fetchone()[0]
|
||||
)
|
||||
event = event_at or now
|
||||
if event.tzinfo is None:
|
||||
event = event.replace(tzinfo=UTC)
|
||||
connection.execute(
|
||||
"INSERT INTO memory_write_jobs ("
|
||||
"job_id,queue_seq,session_key,source_key,batch_id,idempotency_key,"
|
||||
"payload_json,evidence_ids_json,state,priority,attempts,max_attempts,"
|
||||
"lease_owner,lease_expires_at,hy_invoked,last_error,event_at,time_quality,"
|
||||
"created_at,updated_at,sdk_raw_ids_json,sdk_memory_ids_json"
|
||||
") VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
|
||||
(
|
||||
job_id,
|
||||
queue_seq,
|
||||
session_id,
|
||||
lane_key(first.user_id, first.source_id),
|
||||
request.batch_identity.batch_id,
|
||||
request_id,
|
||||
json.dumps(
|
||||
request.model_dump(mode="json", exclude={"event_at", "time_quality"}),
|
||||
sort_keys=True,
|
||||
),
|
||||
json.dumps(list(request.evidence_ids)),
|
||||
PENDING,
|
||||
priority,
|
||||
0,
|
||||
3,
|
||||
None,
|
||||
None,
|
||||
0,
|
||||
None,
|
||||
event.astimezone(UTC).isoformat(),
|
||||
time_quality,
|
||||
now.isoformat(),
|
||||
now.isoformat(),
|
||||
"[]",
|
||||
"[]",
|
||||
),
|
||||
)
|
||||
if not self.sdk_authority:
|
||||
# Legacy catch-up reads coverage from these references.
|
||||
connection.executemany(
|
||||
"INSERT INTO memory_write_job_evidence (job_id, evidence_id) VALUES (?,?)",
|
||||
((job_id, evidence_id) for evidence_id in request.evidence_ids),
|
||||
)
|
||||
return job_id
|
||||
|
||||
def _freeze_request_inputs(self, request: WriteRequest) -> WriteRequest:
|
||||
if request.input_records or self.evidence is None:
|
||||
@@ -1834,21 +2029,24 @@ class DurableWriteQueue:
|
||||
or str(row["last_error"]) in NON_RETRYABLE_POST_WRITE_CODES
|
||||
):
|
||||
return 0
|
||||
connection.execute(
|
||||
"""
|
||||
UPDATE memory_tasks
|
||||
SET status='pending', runner_token=NULL, attempts=0,
|
||||
last_error_code='model_content_manual_retry_requested', updated_at=?
|
||||
WHERE status='unknown' AND task_id IN (
|
||||
SELECT inputs.task_id
|
||||
FROM memory_task_inputs AS inputs, json_each(inputs.refs_json) AS ref
|
||||
JOIN memory_write_job_evidence AS linked
|
||||
ON linked.evidence_id=json_extract(ref.value, '$.evidence_id')
|
||||
WHERE linked.job_id=?
|
||||
# After the SDK cutover the legacy task tables are gone and the
|
||||
# job is the only row to reset.
|
||||
if not self.sdk_authority and _legacy_task_links_present(connection):
|
||||
connection.execute(
|
||||
"""
|
||||
UPDATE memory_tasks
|
||||
SET status='pending', runner_token=NULL, attempts=0,
|
||||
last_error_code='model_content_manual_retry_requested', updated_at=?
|
||||
WHERE status='unknown' AND task_id IN (
|
||||
SELECT inputs.task_id
|
||||
FROM memory_task_inputs AS inputs, json_each(inputs.refs_json) AS ref
|
||||
JOIN memory_write_job_evidence AS linked
|
||||
ON linked.evidence_id=json_extract(ref.value, '$.evidence_id')
|
||||
WHERE linked.job_id=?
|
||||
)
|
||||
""",
|
||||
(now, job_id),
|
||||
)
|
||||
""",
|
||||
(now, job_id),
|
||||
)
|
||||
changed = connection.execute(
|
||||
"""
|
||||
UPDATE memory_write_jobs
|
||||
@@ -1950,9 +2148,7 @@ class DurableWriteQueue:
|
||||
f"WHERE state=? AND job_id IN ({placeholders})",
|
||||
(PENDING, now, UNKNOWN, *job_ids),
|
||||
)
|
||||
if connection.execute(
|
||||
"SELECT 1 FROM sqlite_master WHERE type='table' AND name='memory_tasks'"
|
||||
).fetchone():
|
||||
if _legacy_task_links_present(connection):
|
||||
connection.execute(
|
||||
f"UPDATE memory_tasks SET status=?, runner_token=NULL, "
|
||||
f"last_error_code='model_content_reconciled_absent_retry', updated_at=? "
|
||||
@@ -2010,9 +2206,7 @@ class DurableWriteQueue:
|
||||
f"WHERE state=? AND attempts=1 AND job_id IN ({placeholders})",
|
||||
(PENDING, now, UNKNOWN, *job_ids),
|
||||
)
|
||||
if connection.execute(
|
||||
"SELECT 1 FROM sqlite_master WHERE type='table' AND name='memory_tasks'"
|
||||
).fetchone():
|
||||
if _legacy_task_links_present(connection):
|
||||
connection.execute(
|
||||
f"UPDATE memory_tasks SET status=?, runner_token=NULL, "
|
||||
f"last_error_code='model_content_unknown_auto_retry', updated_at=? "
|
||||
@@ -2029,6 +2223,14 @@ class DurableWriteQueue:
|
||||
def resolve_unknown_from_linked_output(self, job_id: str) -> bool:
|
||||
"""Close an unknown durable row when local governance output proves success."""
|
||||
with main_database(self.database_path).read(row_factory=sqlite3.Row) as connection:
|
||||
if not (
|
||||
_legacy_task_links_present(connection)
|
||||
and connection.execute(
|
||||
"SELECT 1 FROM sqlite_master WHERE type='table' AND name='memory_task_outputs'"
|
||||
).fetchone()
|
||||
):
|
||||
# No legacy governance output survives the SDK cutover.
|
||||
return False
|
||||
row = connection.execute(
|
||||
"""
|
||||
SELECT 1
|
||||
@@ -2319,6 +2521,12 @@ async def execute_job(
|
||||
source_id = request.internal_session_id
|
||||
if source_ids and source_ids != {source_id}:
|
||||
raise RuntimeError("durable_write_multiple_sources")
|
||||
users = {getattr(record, "user_id", DEFAULT_USER_ID) for record in records}
|
||||
if len(users) > 1:
|
||||
raise RuntimeError("durable_write_multiple_users")
|
||||
# The gates answer per user: another user's Hermes job asks about
|
||||
# that user's grant, not `local`'s.
|
||||
source_id = lane_key(next(iter(users), DEFAULT_USER_ID), source_id)
|
||||
|
||||
async def mark() -> bool:
|
||||
return await in_the_background(queue.mark_invoked, job_id, owner=owner)
|
||||
@@ -2411,9 +2619,22 @@ async def execute_job(
|
||||
if code == "model_profile_not_active":
|
||||
logger.debug("durable_write_job_parked job_id=%s error_code=%s", job_id, code)
|
||||
elif state in PARKED_STATES:
|
||||
logger.info("durable_write_job_parked job_id=%s error_code=%s", job_id, code)
|
||||
logger.info(
|
||||
"durable_write_job_parked job_id=%s error_code=%s chain=%s",
|
||||
job_id,
|
||||
code,
|
||||
error_chain_text(error),
|
||||
)
|
||||
else:
|
||||
logger.error("durable_write_job_failed job_id=%s error_code=%s", job_id, code)
|
||||
# Where it failed, as `file:line:function` frames of the exception
|
||||
# chain, never its message: `error_code=AttributeError` alone
|
||||
# named no location.
|
||||
logger.error(
|
||||
"durable_write_job_failed job_id=%s error_code=%s chain=%s",
|
||||
job_id,
|
||||
code,
|
||||
error_chain_text(error),
|
||||
)
|
||||
return
|
||||
await in_the_background(
|
||||
queue.settle,
|
||||
@@ -2500,6 +2721,9 @@ class DurableWriteDrain:
|
||||
#: a model outage clears on its own, and a queue that never goes idle
|
||||
#: must still re-admit the write once the wait has elapsed.
|
||||
self._last_model_readmit: datetime | None = None
|
||||
#: A live conversation quiet for 10 minutes has its last turn and short
|
||||
#: batch written (`flush_idle_assemblies`); checked on the same timer.
|
||||
self._last_assembly_flush: datetime | None = None
|
||||
#: Idle polls do not need to hit the single writer every 50ms. A
|
||||
#: 1m Evidence table makes a miss expensive enough that the tail
|
||||
#: call-event write can be behind it; back off only while idle and
|
||||
@@ -2608,6 +2832,19 @@ class DurableWriteDrain:
|
||||
source_clause = f"AND candidate.source_key IN ({placeholders}) "
|
||||
source_params = tuple(sorted(enabled_sources))
|
||||
direct_sources = ",".join("?" for _ in DIRECT_WRITE_SOURCES)
|
||||
# Another user's hand-added memories are `one-memory-manual@user`:
|
||||
# the same lane ahead of history, still serial per user.
|
||||
direct_lanes = " OR ".join(
|
||||
"substr(candidate.source_key,1,?)=?" for _ in DIRECT_WRITE_SOURCES
|
||||
)
|
||||
direct_lane_params = tuple(
|
||||
value
|
||||
for source in sorted(DIRECT_WRITE_SOURCES)
|
||||
for value in (
|
||||
len(other_user_lane_prefix(source)),
|
||||
other_user_lane_prefix(source),
|
||||
)
|
||||
)
|
||||
# Same eligibility as the window claim below (free lease, no
|
||||
# running job of its source, nothing earlier in its session), but
|
||||
# not bound to the window: a note must not wait for history.
|
||||
@@ -2617,7 +2854,7 @@ class DurableWriteDrain:
|
||||
"WHERE candidate.state=? "
|
||||
"AND (candidate.lease_expires_at IS NULL OR candidate.lease_expires_at < ?) "
|
||||
+ source_clause
|
||||
+ f"AND candidate.source_key IN ({direct_sources}) "
|
||||
+ f"AND (candidate.source_key IN ({direct_sources}) OR {direct_lanes}) "
|
||||
"AND NOT EXISTS ("
|
||||
"SELECT 1 FROM memory_write_jobs AS held "
|
||||
"WHERE held.source_key=candidate.source_key AND held.state=? "
|
||||
@@ -2635,6 +2872,7 @@ class DurableWriteDrain:
|
||||
now.isoformat(),
|
||||
*source_params,
|
||||
*sorted(DIRECT_WRITE_SOURCES),
|
||||
*direct_lane_params,
|
||||
RUNNING,
|
||||
now.isoformat(),
|
||||
*_FIFO_BLOCKING_STATES,
|
||||
@@ -3032,6 +3270,15 @@ class DurableWriteDrain:
|
||||
self.queue.readmit_model_waiters,
|
||||
operation_name="readmit_model_waiters",
|
||||
)
|
||||
if (
|
||||
self._last_assembly_flush is None
|
||||
or tick_started - self._last_assembly_flush >= self._reap_interval
|
||||
):
|
||||
self._last_assembly_flush = tick_started
|
||||
await self._run_idle_maintenance(
|
||||
self.queue.flush_idle_assemblies,
|
||||
operation_name="flush_idle_assemblies",
|
||||
)
|
||||
if self.reconcile_unknown is not None and (
|
||||
self._last_unknown_reconcile is None
|
||||
or tick_started - self._last_unknown_reconcile
|
||||
|
||||
@@ -19,10 +19,11 @@ does the writing. That separation is D52.
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
from datetime import UTC, datetime
|
||||
from typing import Any, Literal, cast
|
||||
|
||||
from fastapi import APIRouter, HTTPException, Request
|
||||
from fastapi import APIRouter, HTTPException, Query, Request
|
||||
from pydantic import BaseModel, ConfigDict, Field
|
||||
|
||||
from one_memory.imports.models import (
|
||||
@@ -43,6 +44,24 @@ from one_memory.memory_server.durable_queue import (
|
||||
)
|
||||
from one_memory.memory_server.registry import WriteRegistry, WriteState
|
||||
from one_memory.memory_server.submit import WriteConflictError
|
||||
from one_memory.user_scope import DEFAULT_USER_ID, USER_ID_PATTERN, UserId
|
||||
|
||||
#: Durable keys of another user's writes start with this. A `local` request
|
||||
#: id never may, so `local` cannot name another user's receipt.
|
||||
_USER_KEY_PREFIX = "user:"
|
||||
|
||||
|
||||
def durable_request_key(request_id: str, user_id: str) -> str:
|
||||
"""The queue key of a caller's request id, namespaced per user.
|
||||
|
||||
Two users choosing the same request id must not collide: the key is the
|
||||
queue's idempotency key and the SDK's `request_id`. `local` keeps its own
|
||||
request id unchanged.
|
||||
"""
|
||||
if user_id == DEFAULT_USER_ID:
|
||||
return request_id
|
||||
digest = hashlib.sha256(request_id.encode()).hexdigest()
|
||||
return f"{_USER_KEY_PREFIX}{user_id}:{digest}"
|
||||
|
||||
|
||||
class MemoryWriteReceipt(BaseModel):
|
||||
@@ -71,6 +90,7 @@ class SdkMemoryWriteInput(BaseModel):
|
||||
messages: list[OpenAIMessage] = Field(min_length=1, max_length=200)
|
||||
memory_at: datetime | None = None
|
||||
priority: Literal["live", "normal"] = "normal"
|
||||
user_id: UserId = DEFAULT_USER_ID
|
||||
|
||||
|
||||
def _registry(
|
||||
@@ -124,6 +144,9 @@ def sdk_router(dependencies: list[Any] | None = None) -> APIRouter:
|
||||
) -> MemoryWriteReceipt:
|
||||
if not any(message.role == "user" for message in payload.messages):
|
||||
raise HTTPException(422, "messages_require_user_turn")
|
||||
if payload.user_id == DEFAULT_USER_ID and payload.request_id.startswith(_USER_KEY_PREFIX):
|
||||
raise HTTPException(422, "request_id_reserved_prefix")
|
||||
request_key = durable_request_key(payload.request_id, payload.user_id)
|
||||
observed_at = payload.memory_at or datetime(1970, 1, 1, tzinfo=UTC)
|
||||
written_by = {
|
||||
"user": WrittenBy.PERSON,
|
||||
@@ -133,6 +156,7 @@ def sdk_router(dependencies: list[Any] | None = None) -> APIRouter:
|
||||
records = tuple(
|
||||
materialize_write_input(
|
||||
EvidenceInput(
|
||||
user_id=payload.user_id,
|
||||
source_id=payload.source_id,
|
||||
conversation_id=payload.session_id,
|
||||
message_id=f"{payload.request_id}:{index}",
|
||||
@@ -157,6 +181,7 @@ def sdk_router(dependencies: list[Any] | None = None) -> APIRouter:
|
||||
internal_session_id = internal_session_id_for(
|
||||
payload.session_id,
|
||||
payload.source_id,
|
||||
payload.user_id,
|
||||
)
|
||||
contract = WriteRequest(
|
||||
internal_session_id=internal_session_id,
|
||||
@@ -168,7 +193,7 @@ def sdk_router(dependencies: list[Any] | None = None) -> APIRouter:
|
||||
),
|
||||
evidence_ids=[record.evidence_id for record in records],
|
||||
input_records=records,
|
||||
idempotency_key=payload.request_id,
|
||||
idempotency_key=request_key,
|
||||
priority=payload.priority,
|
||||
event_at=payload.memory_at,
|
||||
time_quality="exact" if payload.memory_at is not None else "approximate",
|
||||
@@ -178,7 +203,7 @@ def sdk_router(dependencies: list[Any] | None = None) -> APIRouter:
|
||||
except (WriteConflictError, ValueError) as error:
|
||||
raise HTTPException(409, str(error)) from error
|
||||
return MemoryWriteReceipt(
|
||||
request_id=record.request_id,
|
||||
request_id=payload.request_id,
|
||||
state=record.state,
|
||||
created_at=record.created_at,
|
||||
updated_at=record.updated_at,
|
||||
@@ -186,16 +211,24 @@ def sdk_router(dependencies: list[Any] | None = None) -> APIRouter:
|
||||
)
|
||||
|
||||
@instance.get("/{request_id}", response_model=MemoryWriteReceipt)
|
||||
def status(request_id: str, request: Request) -> MemoryWriteReceipt:
|
||||
def status(
|
||||
request_id: str,
|
||||
request: Request,
|
||||
user_id: str = Query(default=DEFAULT_USER_ID, pattern=USER_ID_PATTERN),
|
||||
) -> MemoryWriteReceipt:
|
||||
# A receipt belongs to the user that submitted it. Another user's key
|
||||
# is namespaced, and `local` may not name a namespaced key directly.
|
||||
if user_id == DEFAULT_USER_ID and request_id.startswith(_USER_KEY_PREFIX):
|
||||
raise HTTPException(404, "unknown request id")
|
||||
registry = _registry(request, write=False)
|
||||
try:
|
||||
record = registry.get(request_id)
|
||||
record = registry.get(durable_request_key(request_id, user_id))
|
||||
except DurableQueueSchemaUnavailableError as error:
|
||||
raise HTTPException(503, str(error)) from error
|
||||
if record is None:
|
||||
raise HTTPException(404, "unknown request id")
|
||||
return MemoryWriteReceipt(
|
||||
request_id=record.request_id,
|
||||
request_id=request_id,
|
||||
state=record.state,
|
||||
created_at=record.created_at,
|
||||
updated_at=record.updated_at,
|
||||
|
||||
@@ -271,7 +271,9 @@ def build_request_from_evidence(
|
||||
# Keep the exact conversation id when it fits the 128-char contract;
|
||||
# otherwise derive the versioned session id, so a long branch id does
|
||||
# not reject the whole history import.
|
||||
internal_session_id=internal_session_id_for(seed.conversation_id, seed.source_id),
|
||||
internal_session_id=internal_session_id_for(
|
||||
seed.conversation_id, seed.source_id, seed.user_id
|
||||
),
|
||||
first_round=first_round,
|
||||
last_round=last_round,
|
||||
evidence_ids=list(ordered),
|
||||
|
||||
@@ -28,6 +28,7 @@ from one_memory.memory_server.contracts import (
|
||||
internal_session_id_for,
|
||||
)
|
||||
from one_memory.memory_server.durable_queue import DurableWriteQueue, DurableWriteRecord
|
||||
from one_memory.user_scope import lane_key
|
||||
|
||||
WritePriority = Literal["live", "normal"]
|
||||
|
||||
@@ -74,7 +75,7 @@ class UnifiedWriteIntake:
|
||||
if priority not in {"live", "normal"}:
|
||||
raise ValueError("priority must be 'live' or 'normal'")
|
||||
if self.source_writes_permitted is not None and not self.source_writes_permitted(
|
||||
record.source_id
|
||||
lane_key(record.user_id, record.source_id)
|
||||
):
|
||||
# Freeze is the product's one Harness processing decision. Refuse
|
||||
# new intake so a frozen source creates no new durable write row;
|
||||
@@ -106,7 +107,9 @@ class UnifiedWriteIntake:
|
||||
last_round: int,
|
||||
priority: WritePriority,
|
||||
) -> WriteRequest:
|
||||
session_id = internal_session_id_for(record.conversation_id, record.source_id)
|
||||
session_id = internal_session_id_for(
|
||||
record.conversation_id, record.source_id, record.user_id
|
||||
)
|
||||
event_at, time_quality = _job_time((record,))
|
||||
request_key = (
|
||||
f"entry:{record.evidence_id}"
|
||||
@@ -145,14 +148,14 @@ class UnifiedWriteIntake:
|
||||
):
|
||||
raise ValueError("records must belong to one conversation")
|
||||
if self.source_writes_permitted is not None and not self.source_writes_permitted(
|
||||
first.source_id
|
||||
lane_key(first.user_id, first.source_id)
|
||||
):
|
||||
raise SourceWriteDisabledError(first.evidence_id)
|
||||
material = ":".join(
|
||||
f"{record.evidence_id}:{record.revision}:{record.content_hash}" for record in records
|
||||
)
|
||||
request_id = f"turn:v1:{sha256(material.encode()).hexdigest()}"
|
||||
session_id = internal_session_id_for(first.conversation_id, first.source_id)
|
||||
session_id = internal_session_id_for(first.conversation_id, first.source_id, first.user_id)
|
||||
event_at, time_quality = _job_time(records)
|
||||
durable = self.queue.submit(
|
||||
WriteRequest(
|
||||
|
||||
@@ -1136,20 +1136,31 @@ class ModelProfileStore:
|
||||
)
|
||||
|
||||
|
||||
def disable_thinking_params(
|
||||
profile: ModelProfile | ModelProfileSnapshot,
|
||||
) -> dict[str, object]:
|
||||
"""Provider-specific thinking-off params, empty unless the profile opts in.
|
||||
|
||||
The one mapping for every request Core builds itself and for the SDK's
|
||||
`llm.extra_body`, so a model that thinks on one path thinks on none.
|
||||
"""
|
||||
if not profile.disable_thinking:
|
||||
return {}
|
||||
if profile.provider_kind is ProviderKind.OPENAI_COMPATIBLE:
|
||||
if urlparse(profile.base_url or "").hostname == "api.deepseek.com":
|
||||
return {"thinking": {"type": "disabled"}}
|
||||
return {"reasoning_effort": "none"}
|
||||
if profile.provider_kind is ProviderKind.OLLAMA:
|
||||
return {"think": False}
|
||||
return {}
|
||||
|
||||
|
||||
def attach_disable_thinking(
|
||||
profile: ModelProfile | ModelProfileSnapshot,
|
||||
payload: dict[str, object],
|
||||
) -> dict[str, object]:
|
||||
"""Attach provider-specific thinking-off params when the profile opts in."""
|
||||
if not profile.disable_thinking:
|
||||
return payload
|
||||
if profile.provider_kind is ProviderKind.OPENAI_COMPATIBLE:
|
||||
if urlparse(profile.base_url or "").hostname == "api.deepseek.com":
|
||||
payload["thinking"] = {"type": "disabled"}
|
||||
else:
|
||||
payload["reasoning_effort"] = "none"
|
||||
elif profile.provider_kind is ProviderKind.OLLAMA:
|
||||
payload["think"] = False
|
||||
payload.update(disable_thinking_params(profile))
|
||||
return payload
|
||||
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@ from one_memory.semantic_deadline import (
|
||||
normalize_semantic_hits,
|
||||
search_within,
|
||||
)
|
||||
from one_memory.user_scope import DEFAULT_USER_ID, UserId
|
||||
|
||||
|
||||
def recall_blend_mode() -> str:
|
||||
@@ -67,6 +68,8 @@ class RecallRequest(BaseModel):
|
||||
#: Wall-clock budget for candidate gather + score. Not a substitute for
|
||||
#: raising the old newest-500 page; it is the third parallel budget.
|
||||
timeout_ms: int = Field(default=2_000, ge=50, le=30_000)
|
||||
#: Whose memories to recall. Omitted, it is `local`, the App's library.
|
||||
user_id: UserId = DEFAULT_USER_ID
|
||||
|
||||
|
||||
class RecallHit(BaseModel):
|
||||
|
||||
@@ -32,6 +32,7 @@ from one_memory.model_profiles import (
|
||||
attach_disable_thinking,
|
||||
)
|
||||
from one_memory.secrets import SecretStore, SecretStoreUnavailableError
|
||||
from one_memory.user_scope import owns
|
||||
|
||||
_NO_GROUNDING = "现有记忆不足以回答这个问题。"
|
||||
_GROUNDING_LIMIT = 6
|
||||
@@ -117,7 +118,12 @@ class SdkMemoryAnswerer:
|
||||
if hit.memory_id in seen:
|
||||
continue
|
||||
record = await _get_memory(client, hit.memory_id)
|
||||
if record is None or record.status != "active" or not record.is_latest:
|
||||
if (
|
||||
record is None
|
||||
or record.status != "active"
|
||||
or not record.is_latest
|
||||
or not owns(record.user_id, user_id)
|
||||
):
|
||||
continue
|
||||
seen.add(record.memory_id)
|
||||
found.append(_wire_memory(record))
|
||||
|
||||
@@ -5,7 +5,8 @@ from __future__ import annotations
|
||||
import asyncio
|
||||
import logging
|
||||
import re
|
||||
from contextlib import suppress
|
||||
from collections.abc import AsyncIterator, Callable, Iterable
|
||||
from contextlib import AsyncExitStack, asynccontextmanager, suppress
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
@@ -55,8 +56,11 @@ class SdkMemoryRuntime:
|
||||
write_queue: DurableWriteQueue,
|
||||
extraction_settings: ExtractionSettingsStore,
|
||||
data_dir: Path,
|
||||
user_ids: Callable[[], Iterable[str]] = lambda: ("local",),
|
||||
) -> None:
|
||||
self.coordinator = coordinator
|
||||
#: Every user whose raw layer is kept to the retention window.
|
||||
self.user_ids = user_ids
|
||||
self.read_handle = read_handle
|
||||
self.write_queue = write_queue
|
||||
self.extraction_settings = extraction_settings
|
||||
@@ -71,6 +75,12 @@ class SdkMemoryRuntime:
|
||||
)
|
||||
|
||||
async def prune_expired_raw(self, *, now: datetime | None = None) -> int:
|
||||
deleted = 0
|
||||
for user_id in dict.fromkeys(await asyncio.to_thread(lambda: tuple(self.user_ids()))):
|
||||
deleted += await self._prune_expired_raw_for(user_id, now=now)
|
||||
return deleted
|
||||
|
||||
async def _prune_expired_raw_for(self, user_id: str, *, now: datetime | None) -> int:
|
||||
cutoff = (now or datetime.now(UTC)) - timedelta(days=RAW_RETENTION_DAYS)
|
||||
cutoff_timestamp = int(cutoff.timestamp())
|
||||
client = await self.read_handle.client()
|
||||
@@ -78,7 +88,7 @@ class SdkMemoryRuntime:
|
||||
while True:
|
||||
page = await asyncio.to_thread(
|
||||
client.list_memories,
|
||||
user_id="local",
|
||||
user_id=user_id,
|
||||
layers=["l1_raw"],
|
||||
statuses=list(_RAW_STATUSES),
|
||||
order="asc",
|
||||
@@ -94,8 +104,7 @@ class SdkMemoryRuntime:
|
||||
for row in rows
|
||||
if isinstance(row, dict)
|
||||
and isinstance(row.get("memory_id"), str)
|
||||
and int(row.get("memory_at") or row.get("gmt_created") or 0)
|
||||
< cutoff_timestamp
|
||||
and int(row.get("memory_at") or row.get("gmt_created") or 0) < cutoff_timestamp
|
||||
]
|
||||
if not expired:
|
||||
return deleted
|
||||
@@ -153,55 +162,55 @@ class SdkMemoryRuntime:
|
||||
limit: int = 12,
|
||||
) -> tuple[tuple[str, float], ...]:
|
||||
return tuple(
|
||||
(hit.memory_id, hit.score)
|
||||
for hit in await self.semantic_recall(query, limit=limit)
|
||||
(hit.memory_id, hit.score) for hit in await self.semantic_recall(query, limit=limit)
|
||||
)
|
||||
|
||||
async def graph_snapshot(self, *, limit: int = 300) -> HyGraphSnapshot:
|
||||
async def graph_snapshot(self, *, limit: int = 300, user_id: str = "local") -> HyGraphSnapshot:
|
||||
try:
|
||||
await self.coordinator.active_profile_credentials()
|
||||
except RuntimeError as error:
|
||||
if str(error) == "model_profile_not_active":
|
||||
return HyGraphSnapshot(consulted=False)
|
||||
raise
|
||||
try:
|
||||
client = await self.coordinator.client()
|
||||
except Exception as error:
|
||||
raise HySdkError("hy_memory_graph_read_failed") from error
|
||||
return await asyncio.to_thread(
|
||||
read_graph_snapshot,
|
||||
client,
|
||||
"local",
|
||||
"*",
|
||||
limit,
|
||||
)
|
||||
async with AsyncExitStack() as stack:
|
||||
try:
|
||||
client = await stack.enter_async_context(self.coordinator.use_client())
|
||||
except Exception as error:
|
||||
raise HySdkError("hy_memory_graph_read_failed") from error
|
||||
return await asyncio.to_thread(
|
||||
read_graph_snapshot,
|
||||
client,
|
||||
user_id,
|
||||
"*",
|
||||
limit,
|
||||
)
|
||||
|
||||
async def digest(self) -> dict[str, object]:
|
||||
client = await self.coordinator.client()
|
||||
try:
|
||||
raw = await asyncio.to_thread(
|
||||
client.list_memories,
|
||||
user_id="local",
|
||||
limit=1_000,
|
||||
offset=0,
|
||||
)
|
||||
agent_ids = _agent_ids(raw)
|
||||
digests = [
|
||||
{
|
||||
"agent_id": agent_id,
|
||||
"schemas_recorded": 0,
|
||||
**dict(
|
||||
await asyncio.to_thread(
|
||||
client.evolve,
|
||||
user_id="local",
|
||||
agent_id=agent_id,
|
||||
)
|
||||
),
|
||||
}
|
||||
for agent_id in agent_ids
|
||||
]
|
||||
except Exception as error:
|
||||
raise HySdkError("hy_memory_digest_failed") from error
|
||||
async with self.coordinator.use_client() as client:
|
||||
try:
|
||||
raw = await asyncio.to_thread(
|
||||
client.list_memories,
|
||||
user_id="local",
|
||||
limit=1_000,
|
||||
offset=0,
|
||||
)
|
||||
agent_ids = _agent_ids(raw)
|
||||
digests = [
|
||||
{
|
||||
"agent_id": agent_id,
|
||||
"schemas_recorded": 0,
|
||||
**dict(
|
||||
await asyncio.to_thread(
|
||||
client.evolve,
|
||||
user_id="local",
|
||||
agent_id=agent_id,
|
||||
)
|
||||
),
|
||||
}
|
||||
for agent_id in agent_ids
|
||||
]
|
||||
except Exception as error:
|
||||
raise HySdkError("hy_memory_digest_failed") from error
|
||||
return {
|
||||
"success": all(result.get("success") is not False for result in digests),
|
||||
"scope_count": len(digests),
|
||||
@@ -240,36 +249,39 @@ class SdkMemoryRuntime:
|
||||
decision=decision,
|
||||
)
|
||||
|
||||
async def _mutation_client(self) -> Any:
|
||||
@asynccontextmanager
|
||||
async def _mutation_client(self) -> AsyncIterator[Any]:
|
||||
"""The write client, with no-model and keychain refusals as SDK codes.
|
||||
|
||||
Callers record these against the edit or deletion instead of failing
|
||||
the request that already committed it.
|
||||
"""
|
||||
try:
|
||||
return await self.coordinator.client()
|
||||
except RuntimeError as error:
|
||||
code = str(error)
|
||||
if not _STABLE_CODE.fullmatch(code):
|
||||
code = "hy_memory_client_unavailable"
|
||||
raise HySdkError(code) from error
|
||||
async with AsyncExitStack() as stack:
|
||||
try:
|
||||
client = await stack.enter_async_context(self.coordinator.use_client())
|
||||
except RuntimeError as error:
|
||||
code = str(error)
|
||||
if not _STABLE_CODE.fullmatch(code):
|
||||
code = "hy_memory_client_unavailable"
|
||||
raise HySdkError(code) from error
|
||||
yield client
|
||||
|
||||
async def update_hy_memory(self, memory_id: str, content: str) -> dict[str, object]:
|
||||
client = await self._mutation_client()
|
||||
try:
|
||||
result = await asyncio.to_thread(client.update, memory_id, content)
|
||||
except Exception as error:
|
||||
raise HySdkError("hy_memory_update_failed") from error
|
||||
async with self._mutation_client() as client:
|
||||
try:
|
||||
result = await asyncio.to_thread(client.update, memory_id, content)
|
||||
except Exception as error:
|
||||
raise HySdkError("hy_memory_update_failed") from error
|
||||
if not isinstance(result, dict) or result.get("success") is not True:
|
||||
raise HySdkError("hy_memory_update_rejected")
|
||||
return result
|
||||
|
||||
async def finish_deletion(self, memory_id: str) -> None:
|
||||
client = await self._mutation_client()
|
||||
try:
|
||||
result = await asyncio.to_thread(client.delete, memory_id)
|
||||
except Exception as error:
|
||||
raise HySdkError("hy_memory_delete_failed") from error
|
||||
async with self._mutation_client() as client:
|
||||
try:
|
||||
result = await asyncio.to_thread(client.delete, memory_id)
|
||||
except Exception as error:
|
||||
raise HySdkError("hy_memory_delete_failed") from error
|
||||
if not isinstance(result, dict):
|
||||
raise HySdkError("hy_memory_delete_rejected")
|
||||
error_code = result.get("error_code")
|
||||
|
||||
@@ -125,6 +125,31 @@ def describe_error(error: BaseException) -> dict[str, object]:
|
||||
return {"chain": chain}
|
||||
|
||||
|
||||
_PY_FRAME = re.compile(r"^[A-Za-z0-9_][A-Za-z0-9_.\-]*\.py:\d{1,7}:[A-Za-z_][A-Za-z0-9_]*$")
|
||||
|
||||
|
||||
def error_frames(error: BaseException, *, limit: int = 16) -> tuple[str, ...]:
|
||||
"""The raised error's innermost `file.py:line:function` frames, for a stack field.
|
||||
|
||||
Library frames are kept (a KeyError raised inside Chroma is located by
|
||||
them); `<module>`-style names are dropped because stack fields refuse them.
|
||||
"""
|
||||
chain = describe_error(error)["chain"]
|
||||
frames = chain[0].get("frames", ()) if chain else () # type: ignore[index,union-attr]
|
||||
return tuple(frame for frame in frames if _PY_FRAME.fullmatch(frame))[-limit:]
|
||||
|
||||
|
||||
def error_chain_text(error: BaseException) -> str:
|
||||
"""`describe_error` as one log token: `Type@file:line:function>...;Cause@...`.
|
||||
|
||||
For a plain text log (core.log). Types and frames only, never a message.
|
||||
"""
|
||||
return ";".join(
|
||||
f"{entry['type']}@{'>'.join(entry.get('frames', ()))}" # type: ignore[arg-type]
|
||||
for entry in describe_error(error)["chain"] # type: ignore[union-attr]
|
||||
)
|
||||
|
||||
|
||||
def _build() -> dict[str, object]:
|
||||
from one_memory import __version__
|
||||
|
||||
|
||||
@@ -35,6 +35,8 @@ from one_memory.memory_server.durable_queue import (
|
||||
COMPLETED,
|
||||
FAILED,
|
||||
REAPED_PAYLOAD_MARKER,
|
||||
RUNNING,
|
||||
UNKNOWN,
|
||||
DurableWriteQueue,
|
||||
)
|
||||
from one_memory.memory_server.unified_entry import UnifiedWriteIntake
|
||||
@@ -257,6 +259,7 @@ def prepare_legacy_write_queue_for_cutover(database_path: Path) -> tuple[int, in
|
||||
internal_session_id = internal_session_id_for(
|
||||
source.conversation_id,
|
||||
source.source_id,
|
||||
source.user_id,
|
||||
)
|
||||
queue.submit(
|
||||
WriteRequest(
|
||||
@@ -317,11 +320,45 @@ def move_pending_dispatches(database_path: Path) -> int:
|
||||
return len(conversations)
|
||||
|
||||
|
||||
#: Why a pre-cutover write job that may have reached HY was closed.
|
||||
ABANDONED_AT_CUTOVER = "abandoned_at_cutover"
|
||||
|
||||
|
||||
def settle_legacy_unknown_write_jobs(database_path: Path) -> int:
|
||||
"""Close write jobs the legacy pipeline left possibly-sent. Safe to repeat.
|
||||
|
||||
They ran under the legacy pipeline, so the SDK's `get_write_outcome` knows
|
||||
none of them and the unknown reconcile can never settle them: a migrated
|
||||
1.0.4 home showed 1000+ unknown forever. They are not retried, since they
|
||||
may already have written and the SDK has no idempotency key. `cancelled`,
|
||||
not `failed`: the upgrade ended them, nothing broke, and a `failed` count
|
||||
would put the home in the failed state for good. `hy_invoked` is kept, so
|
||||
no readmission path can pick one up again.
|
||||
"""
|
||||
with main_database(database_path).read(row_factory=sqlite3.Row) as database:
|
||||
if "memory_write_jobs" not in _tables(database):
|
||||
return 0
|
||||
now = datetime.now(UTC).isoformat()
|
||||
with main_database(database_path).write(autocommit=True) as database:
|
||||
# A `running` row already marked invoked would be reclaimed to
|
||||
# `unknown` on the next open, so it is the same case.
|
||||
return int(
|
||||
database.execute(
|
||||
"UPDATE memory_write_jobs SET state=?,lease_owner=NULL,"
|
||||
"lease_expires_at=NULL,last_error=?,updated_at=? "
|
||||
"WHERE state=? OR (state=? AND hy_invoked=1)",
|
||||
(CANCELLED, ABANDONED_AT_CUTOVER, now, UNKNOWN, RUNNING),
|
||||
).rowcount
|
||||
)
|
||||
|
||||
|
||||
def abandoned_legacy_work(database_path: Path) -> dict[str, int]:
|
||||
"""Count legacy work that ends with the cutover instead of blocking it.
|
||||
|
||||
A task that may have reached the SDK is not replayed: the SDK already holds
|
||||
whatever it committed. Pending user deletions are not carried over.
|
||||
Settled write jobs are counted by their code, so a repeated preparation
|
||||
reports the same number.
|
||||
"""
|
||||
abandoned: dict[str, int] = {}
|
||||
with main_database(database_path).read(row_factory=sqlite3.Row) as database:
|
||||
@@ -332,6 +369,11 @@ def abandoned_legacy_work(database_path: Path) -> dict[str, int]:
|
||||
"memory_tasks",
|
||||
"status='unknown' OR (status='running' AND hy_invocation_count > 0)",
|
||||
),
|
||||
(
|
||||
"unknown_write_jobs",
|
||||
"memory_write_jobs",
|
||||
f"state='{CANCELLED}' AND last_error='{ABANDONED_AT_CUTOVER}'",
|
||||
),
|
||||
("deletions", "memory_deletions", "status IN ('pending','running')"),
|
||||
)
|
||||
for name, table, where in checks:
|
||||
@@ -354,6 +396,10 @@ def prepare_legacy_work_for_cutover(
|
||||
running, so a failure names it.
|
||||
"""
|
||||
trace = {} if trace is None else trace
|
||||
# First: a possibly-sent job never runs again, so its input is not
|
||||
# frozen, and input released since must not block the cutover.
|
||||
trace["sub_step"] = "unknown_write_jobs"
|
||||
settle_legacy_unknown_write_jobs(database_path)
|
||||
trace["sub_step"] = "write_queue"
|
||||
frozen, moved = prepare_legacy_write_queue_for_cutover(database_path)
|
||||
trace["sub_step"] = "pending_dispatches"
|
||||
@@ -829,6 +875,7 @@ def run_startup_migration(
|
||||
ignore_errors=True,
|
||||
)
|
||||
discard_failed_state(data_dir)
|
||||
_repair_metadata_after_cutover(data_dir, database_path, log)
|
||||
progress("cleanup")
|
||||
with log.step("cleanup.drop_legacy_tables") as step:
|
||||
dropped = resume_legacy_cleanup(data_dir=data_dir, database_path=database_path)
|
||||
@@ -847,6 +894,25 @@ def run_startup_migration(
|
||||
)
|
||||
|
||||
|
||||
def _repair_metadata_after_cutover(data_dir: Path, database_path: Path, log: _StepLog) -> None:
|
||||
"""Restore lost metadata here, where Runtime is stopped and no Chroma client is open.
|
||||
|
||||
Writes the repair receipt, so Core's startup fallback finds it and does
|
||||
nothing. Authority is already committed: a failure is logged and left to
|
||||
that fallback instead of failing the migration.
|
||||
"""
|
||||
from one_memory.hy_sdk_migration import repair_sdk_metadata_from_bundle
|
||||
|
||||
try:
|
||||
with log.step("cutover.metadata_repair") as step:
|
||||
repair = repair_sdk_metadata_from_bundle(data_dir=data_dir, database_path=database_path)
|
||||
step["mapped"] = repair.mapped
|
||||
step["vector_rows"] = repair.vector_rows
|
||||
step["coding_rows"] = repair.coding_rows
|
||||
except Exception:
|
||||
return
|
||||
|
||||
|
||||
def _restore_after_failure(
|
||||
*,
|
||||
data_dir: Path,
|
||||
|
||||
@@ -3,7 +3,8 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from collections.abc import Awaitable, Callable, Mapping, Sequence
|
||||
from collections.abc import AsyncIterator, Awaitable, Callable, Mapping, Sequence
|
||||
from contextlib import asynccontextmanager
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime
|
||||
from typing import Any
|
||||
@@ -16,6 +17,7 @@ from one_memory.secrets import SecretStore, SecretStoreUnavailableError
|
||||
|
||||
PRODUCT_AGENT_ID = "all-apps"
|
||||
_MESSAGE_ROLES = frozenset({"user", "assistant", "tool"})
|
||||
_CONVERSATION_ROLES = frozenset({"user", "assistant"})
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
@@ -41,12 +43,28 @@ class SdkWriteProcessor:
|
||||
) -> SdkWriteReceipt:
|
||||
if not records:
|
||||
raise RuntimeError("hy_memory_task_without_inputs")
|
||||
extracted = all(_is_extracted_content(record) for record in records)
|
||||
if not extracted and not any(
|
||||
record.content_kind is EvidenceContentKind.RAW_CONVERSATION
|
||||
and record.role in _CONVERSATION_ROLES
|
||||
for record in records
|
||||
):
|
||||
# Nothing to remember: tool records alone. The SDK strips them on
|
||||
# the chat path and answers 400 "content or messages is
|
||||
# required", which used to settle the job as unknown. Completed
|
||||
# with no memories, and the SDK is never called.
|
||||
return SdkWriteReceipt()
|
||||
|
||||
scope = _write_scope(records)
|
||||
metadata = _write_metadata(records, scope)
|
||||
client = await self._client_handle.client()
|
||||
try:
|
||||
client = await self._client_handle.client()
|
||||
except Exception as error:
|
||||
# Before `before_hy_invocation`: nothing was sent, so the job
|
||||
# waits and retries instead of being left `unknown`.
|
||||
raise RuntimeError("hy_memory_client_unavailable") from error
|
||||
|
||||
if all(_is_extracted_content(record) for record in records):
|
||||
if extracted:
|
||||
contents = [sanitize_model_input(record.verbatim) for record in records]
|
||||
await before_hy_invocation()
|
||||
result = await asyncio.to_thread(
|
||||
@@ -113,7 +131,14 @@ class SdkWriteProcessor:
|
||||
|
||||
|
||||
class SdkWriteCoordinator:
|
||||
"""Resolve the active model profile, then execute one public SDK write."""
|
||||
"""Resolve the active model profile, then execute one public SDK write.
|
||||
|
||||
A model profile change retires the previous profile's handle, but only
|
||||
closes it once nothing uses it. Closing it at once tore the client out
|
||||
from under a write still running on it: the SDK cleared its stores and
|
||||
the SharedRuntime closed mid-`add`, the job failed with AttributeError
|
||||
after HY was invoked, and it was left `unknown`.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
@@ -130,6 +155,9 @@ class SdkWriteCoordinator:
|
||||
self.transport_factory = transport_factory
|
||||
self.sedimentation_enabled = sedimentation_enabled
|
||||
self._handles: dict[tuple[str, int], SdkClientHandle] = {}
|
||||
#: Uses in flight per handle; a retired handle closes at zero.
|
||||
self._uses: dict[SdkClientHandle, int] = {}
|
||||
self._retired: set[SdkClientHandle] = set()
|
||||
self._lock = asyncio.Lock()
|
||||
|
||||
async def process_sdk_job(
|
||||
@@ -141,28 +169,30 @@ class SdkWriteCoordinator:
|
||||
) -> SdkWriteReceipt:
|
||||
if not self.sedimentation_enabled():
|
||||
raise RuntimeError("model_sedimentation_disabled")
|
||||
handle = await self._active_handle()
|
||||
return await SdkWriteProcessor(handle).process_sdk_job(
|
||||
records,
|
||||
request_id=request_id,
|
||||
before_hy_invocation=before_hy_invocation,
|
||||
)
|
||||
async with self._active_handle() as handle:
|
||||
return await SdkWriteProcessor(handle).process_sdk_job(
|
||||
records,
|
||||
request_id=request_id,
|
||||
before_hy_invocation=before_hy_invocation,
|
||||
)
|
||||
|
||||
async def inquire_write_outcome(
|
||||
self, request_id: str
|
||||
) -> SdkWriteReceipt | None:
|
||||
return await SdkWriteProcessor(
|
||||
await self._active_handle()
|
||||
).inquire_write_outcome(request_id)
|
||||
async with self._active_handle() as handle:
|
||||
return await SdkWriteProcessor(handle).inquire_write_outcome(request_id)
|
||||
|
||||
async def close(self) -> None:
|
||||
async with self._lock:
|
||||
handles, self._handles = tuple(self._handles.values()), {}
|
||||
handles = (*self._handles.values(), *self._retired)
|
||||
self._handles, self._retired, self._uses = {}, set(), {}
|
||||
await asyncio.gather(*(handle.close() for handle in handles))
|
||||
|
||||
async def client(self) -> Any:
|
||||
"""Return the public SDK client selected by the active write profile."""
|
||||
return await (await self._active_handle()).client()
|
||||
@asynccontextmanager
|
||||
async def use_client(self) -> AsyncIterator[Any]:
|
||||
"""The public SDK client of the active write profile, kept open while used."""
|
||||
async with self._active_handle() as handle:
|
||||
yield await handle.client()
|
||||
|
||||
async def active_profile_credentials(
|
||||
self,
|
||||
@@ -171,7 +201,9 @@ class SdkWriteCoordinator:
|
||||
_identity, profile, secret = await asyncio.to_thread(self._read_active_credentials)
|
||||
return profile, secret
|
||||
|
||||
async def _active_handle(self) -> SdkClientHandle:
|
||||
@asynccontextmanager
|
||||
async def _active_handle(self) -> AsyncIterator[SdkClientHandle]:
|
||||
"""Use the active profile's handle; retire the others, closing the unused."""
|
||||
identity, profile, secret = await asyncio.to_thread(
|
||||
self._read_active_credentials
|
||||
)
|
||||
@@ -185,14 +217,25 @@ class SdkWriteCoordinator:
|
||||
http_transport_factory=self.transport_factory(profile),
|
||||
)
|
||||
self._handles[identity] = handle
|
||||
stale = [
|
||||
value
|
||||
for key, value in self._handles.items()
|
||||
if key != identity
|
||||
]
|
||||
self._retired.update(value for key, value in self._handles.items() if key != identity)
|
||||
self._handles = {identity: handle}
|
||||
await asyncio.gather(*(value.close() for value in stale))
|
||||
return handle
|
||||
self._uses[handle] = self._uses.get(handle, 0) + 1
|
||||
unused = {value for value in self._retired if not self._uses.get(value)}
|
||||
self._retired -= unused
|
||||
await asyncio.gather(*(value.close() for value in unused))
|
||||
try:
|
||||
yield handle
|
||||
finally:
|
||||
# Bookkeeping without an await, so a cancelled caller still
|
||||
# releases its use.
|
||||
remaining = self._uses.get(handle, 0) - 1
|
||||
if remaining > 0:
|
||||
self._uses[handle] = remaining
|
||||
else:
|
||||
self._uses.pop(handle, None)
|
||||
if remaining <= 0 and handle in self._retired:
|
||||
self._retired.discard(handle)
|
||||
await handle.close()
|
||||
|
||||
def _read_active_credentials(
|
||||
self,
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
"""Refuse query parameters a route does not declare.
|
||||
|
||||
FastAPI ignores an unknown query parameter. For a memory read that is a
|
||||
silent widening: `memory-library?user_id=__nobody__` against a Core that did
|
||||
not know `user_id` answered with the whole library, and the caller believed
|
||||
it was isolated. Routes that carry this dependency answer such a request with
|
||||
the same 422 a malformed body gets.
|
||||
"""
|
||||
|
||||
from fastapi import Request
|
||||
from fastapi.dependencies.models import Dependant
|
||||
from fastapi.exceptions import RequestValidationError
|
||||
from fastapi.routing import APIRoute
|
||||
|
||||
|
||||
def _declared_query_names(dependant: Dependant) -> set[str]:
|
||||
names = {parameter.alias for parameter in dependant.query_params}
|
||||
for dependency in dependant.dependencies:
|
||||
names |= _declared_query_names(dependency)
|
||||
return names
|
||||
|
||||
|
||||
async def forbid_unknown_query_params(request: Request) -> None:
|
||||
route = request.scope.get("route")
|
||||
if not isinstance(route, APIRoute):
|
||||
return
|
||||
unknown = sorted(set(request.query_params) - _declared_query_names(route.dependant))
|
||||
if unknown:
|
||||
raise RequestValidationError(
|
||||
[
|
||||
{
|
||||
"type": "extra_forbidden",
|
||||
"loc": ("query", name),
|
||||
"msg": "Extra inputs are not permitted",
|
||||
"input": None,
|
||||
}
|
||||
for name in unknown
|
||||
]
|
||||
)
|
||||
@@ -0,0 +1,65 @@
|
||||
"""Who a memory belongs to: one Runtime, many `user_id`s.
|
||||
|
||||
A user is decided by which source root is read (a Hermes profile is its own
|
||||
`state.db`), never by session or platform fields. The App never names a user
|
||||
and keeps reading and writing `local`; every other harness stays on `local`
|
||||
too. Only Hermes may connect a second root for another user.
|
||||
|
||||
In memory, a source's import lane is keyed by `lane_key(user_id, source_id)`.
|
||||
For `local` that is the bare source id, so every existing key, health record
|
||||
and log line stays byte-identical; another user's lane is `source@user`.
|
||||
`@` cannot occur in a user id or a source id, so the key splits back exactly.
|
||||
"""
|
||||
|
||||
import re
|
||||
from typing import Annotated
|
||||
|
||||
from pydantic import Field
|
||||
|
||||
DEFAULT_USER_ID = "local"
|
||||
|
||||
#: `:` is excluded on purpose: the SDK joins ids with it when it keys history,
|
||||
#: so a user id holding one could read another user's history.
|
||||
USER_ID_PATTERN = r"^[A-Za-z0-9._-]{1,64}$"
|
||||
_USER_ID = re.compile(USER_ID_PATTERN)
|
||||
|
||||
#: The only source whose roots may belong to a user other than `local`.
|
||||
MULTI_USER_SOURCES = frozenset({"hermes"})
|
||||
|
||||
UserId = Annotated[str, Field(min_length=1, max_length=64, pattern=USER_ID_PATTERN)]
|
||||
|
||||
_LANE_SEPARATOR = "@"
|
||||
|
||||
|
||||
def valid_user_id(value: str) -> bool:
|
||||
return bool(_USER_ID.fullmatch(value))
|
||||
|
||||
|
||||
def lane_key(user_id: str, source_id: str) -> str:
|
||||
if user_id == DEFAULT_USER_ID:
|
||||
return source_id
|
||||
return f"{source_id}{_LANE_SEPARATOR}{user_id}"
|
||||
|
||||
|
||||
def other_user_lane_prefix(source_id: str) -> str:
|
||||
"""What every other user's lane of `source_id` starts with."""
|
||||
return f"{source_id}{_LANE_SEPARATOR}"
|
||||
|
||||
|
||||
def split_lane(key: str) -> tuple[str, str]:
|
||||
"""`(user_id, source_id)` of a lane key; a bare source id is `local`'s."""
|
||||
source_id, separator, user_id = key.partition(_LANE_SEPARATOR)
|
||||
if not separator or not valid_user_id(user_id):
|
||||
return DEFAULT_USER_ID, key
|
||||
return user_id, source_id
|
||||
|
||||
|
||||
def owns(record_user_id: str | None, user_id: str) -> bool:
|
||||
"""Whether a stored memory belongs to `user_id`.
|
||||
|
||||
The SDK's `get`/`update`/`delete`/`history`/`get_write_outcome` take no
|
||||
user, so MemHub checks this before acting on an id. A memory written
|
||||
before MemHub named users carries `local` or nothing; both are `local`'s.
|
||||
"""
|
||||
stored = record_user_id or DEFAULT_USER_ID
|
||||
return stored == user_id
|
||||
@@ -79,7 +79,7 @@
|
||||
| `test_database_file_permissions.py` | `contract` | 覆盖 a new main database is readable by its owner alone、the wal sidecars are not left world readable、a database from an older build is tightened on open 场景。 |
|
||||
| `test_db_session_utc.py` | `contract` | The MySQL session must be pinned to UTC. |
|
||||
| `test_derived_echo.py` | `contract` | A host that saves our injection back must not mint a new user decision. |
|
||||
| `test_detailed_logging.py` | `contract` | 覆盖 detailed pipeline logging defaults off and survives restart、live log level applies to full hy and provider call chain、info collects hy info without enabling debug pipeline content 等 7 个场景。 |
|
||||
| `test_detailed_logging.py` | `contract` | 覆盖 detailed logging defaults off and survives restart、hy pipeline log and trace stay off at every level、live level change reaches the hy text log file 等 7 个场景。 |
|
||||
| `test_diagnostic_log.py` | `contract` | 覆盖 high frequency success events are summarized but warnings are not、structured log is private correlated and owner only、arbitrary detail and absolute stack are rejected 等 14 个场景。 |
|
||||
| `test_diagnostic_log_durability.py` | `contract` | 写线程报告的「已落盘」必须是真的。 |
|
||||
| `test_diagnostic_log_off_loop.py` | `contract` | 诊断日志不得在调用线程上做磁盘 I/O。 |
|
||||
@@ -105,6 +105,7 @@
|
||||
| `test_graph_width_guard.py` | `unit` | Graph width mismatch must ask, never silently archive. |
|
||||
| `test_harness_contract_suite.py` | `contract` | A4-09: one contract, ten harnesses. |
|
||||
| `test_health.py` | `contract` | 覆盖 a backfill that fails says so rather than disappearing、a failed catch up leaves a traceback behind、configured model without content consent does not start backfill 等 15 个场景。 |
|
||||
| `test_hermes_gateway_injection.py` | `contract` | Hermes gateway text in `user` rows: notices are skipped, relayed messages unwrapped. |
|
||||
| `test_history_listing_rotation.py` | `contract` | 覆盖 history listing alternates started and untouched files、history listing drains whichever cohort is longer 场景。 |
|
||||
| `test_host_capabilities.py` | `unit` | A plugin does not become realtime on every host or every unknown version. |
|
||||
| `test_hourly_c1_peak_replay_contract.py` | `contract` | Guard the package peak-replay probe's error accounting. |
|
||||
@@ -112,6 +113,7 @@
|
||||
| `test_hy_log_privacy.py` | `contract` | HY SDK records are not rewritten by One Memory's logging setup. |
|
||||
| `test_hy_network_egress.py` | `contract` | Where a real HY client is allowed to send bytes, and where it is not. |
|
||||
| `test_hy_runtime_config.py` | `contract` | MEMORY.md is the audited record of what we send HY; keep it answerable. |
|
||||
| `test_hy_sdk_log_routing.py` | `contract` | HY SDK text logs go to hy_memory.log only, with warnings also in core.log. |
|
||||
| `test_hy_step_trace_switch.py` | `contract` | Rule 15: the per-step HY trace is off unless the debug tier asks for it. |
|
||||
| `test_import_api.py` | `integration` | 覆盖 resume during cleanup returns conflict without internal error、authorize detect import and revoke api flow、protected api rejects missing token and foreign origin 等 5 个场景。 |
|
||||
| `test_import_control_races_review.py` | `contract` | Read-only review repro: real runner/store, synthetic delayed adapter cleanup. |
|
||||
@@ -195,6 +197,7 @@
|
||||
| `test_model_profiles.py` | `contract` | 覆盖 profile uses one model and keeps key out of database、api key updates reuse one stable secret reference、startup cleanup removes only secrets not referenced by current profiles 等 15 个场景。 |
|
||||
| `test_model_request_audit.py` | `contract` | 覆盖 failure is recorded without error message、missing usage stays unknown in detail and zero in summary、records one physical request with provider usage and no content 等 5 个场景。 |
|
||||
| `test_model_request_budget.py` | `contract` | 覆盖 persists failed request reservations without content、parallel leases cannot exceed request limit、unknown or excessive output and body are denied 等 6 个场景。 |
|
||||
| `test_multi_user_runtime.py` | `contract` | One Runtime, many users: `user_id` on the memory API, per-user Hermes profiles. |
|
||||
| `test_mysql_lifecycle.py` | `contract` | A1-07: the decisions Runtime makes around the bundled MySQL. |
|
||||
| `test_mysql_platform.py` | `contract` | A1-07: the machine-facing half of managing the bundled MySQL. |
|
||||
| `test_obs03_health_probe.py` | `contract` | OBS-03: '/health' answered with the web app instead of a health payload. |
|
||||
@@ -245,6 +248,7 @@
|
||||
| `test_schema_gate.py` | `contract` | 覆盖 fresh database records the build schema、future schema refuses to open、legacy database without a ledger is left untouched 场景。 |
|
||||
| `test_schema_migrations.py` | `contract` | 覆盖 evidence store adds source ordinal to previous schema、import store migrates previous job and error schema、old active job index lets a sync sit beside paused history 等 4 个场景。 |
|
||||
| `test_schema_ownership_gate.py` | `contract` | A1-03/A1-04: schema ownership, and the hard part of telling DDL from reads. |
|
||||
| `test_sdk_client_construction.py` | `contract` | SDK clients of one home are constructed one at a time. |
|
||||
| `test_sdk_data_plane_migration.py` | `contract` | SDK-authority migration ledger, aliases and atomic cutover marker. |
|
||||
| `test_sdk_memory_qa.py` | `contract` | 覆盖 sdk ask propagates sdk search failure without legacy fallback、sdk ask revalidates memory before returning extract、sdk ask uses native sdk memory without governance fields 场景。 |
|
||||
| `test_sdk_migration_bundle.py` | `contract` | SDK-authority cutover requires a verified management + SDK data-plane bundle. |
|
||||
@@ -307,4 +311,4 @@
|
||||
| `test_workspace_recall.py` | `contract` | Workspace memories were unreachable from every host; these pin the way in. |
|
||||
| `test_write_queue_lease_renewal.py` | `contract` | Rule 8: a lease must outlive the work it covers, and be renewed while it runs. |
|
||||
|
||||
共 302 个测试文件。
|
||||
共 306 个测试文件。
|
||||
@@ -14,8 +14,8 @@ from pathlib import Path
|
||||
import pytest
|
||||
|
||||
PRIMARY_MARKERS = ("unit", "contract", "integration", "performance", "live")
|
||||
EXPECTED_TEST_FILE_COUNT = 302
|
||||
EXPECTED_TEST_FILES_SHA256 = "07707528025ac134e46c93ebef12335e852df1ed0f52f8beb62abb3b4d1b0e97"
|
||||
EXPECTED_TEST_FILE_COUNT = 306
|
||||
EXPECTED_TEST_FILES_SHA256 = "e616dcf9fea288bf15a4445045f7b4278517f3304091c04fffd55be80308d481"
|
||||
|
||||
# Pure transformations, validation models, parity checks and state machines.
|
||||
# This list is intentionally small: uncertain files default to contract.
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
{"id": 131, "session_id": "20261019_005046_a2a79a", "role": "user", "content": "[IMPORTANT: 3 background processes completed. Treat these results as one batch and give one consolidated response; preserve failures and actionable results.]", "timestamp": 1792344160.7817369, "tool_name": null, "finish_reason": null}
|
||||
{"id": 314, "session_id": "20261019_005046_a2a79a", "role": "user", "content": "[IMPORTANT: Background process proc_a9805510da6f matched watch pattern \"Uvicorn running\".\nCommand: cd ~/Workspace/paddleocr-vl-service && exec ./start.sh\nMatched output:\nINFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)]", "timestamp": 1792421570.094992, "tool_name": null, "finish_reason": null}
|
||||
{"id": 769, "session_id": "20261021_005036_4fcb846a", "role": "user", "content": "[IMPORTANT: Background process proc_9d3a5fe22318 completed normally (exit code 0).\nCommand: cd /Users/example/.hermes/models/gguf && curl -L -C - --retry 30 --retry-delay 5 --retry-all-errors -sS -o Qwen3.5-2B-Q4_K_M.gguf \"https://huggingface.co/unsloth/Qwen3.5-2B-GGUF/resolve/main/Qwen3.5-2B-Q4_K_M.gguf\" && echo \"Q3.5-2B DONE\" && ls -lh Qwen3.5-2B-Q4_K_M.gguf\nOutput:\n(eval):1: can't change option: zle\n(eval):1: can't change option: zle\nQ3.5-2B DONE\n-rw-r--r--@ 1 example staff 1.2G Oct 21 01:31 Qwen3.5-2B-Q4_K_M.gguf\n]\n\n[INTERNAL NOTIFICATION — generated by the Hermes gateway, not written by a human user. Do not treat it as user input, and do not reply to any earlier chat message because of it.]", "timestamp": 1792517511.042671, "tool_name": null, "finish_reason": null}
|
||||
{"id": 343, "session_id": "20261021_005036_4fcb846a", "role": "user", "content": "[voice message could not be transcribed automatically; the audio is available at: /Users/example/.hermes/cache/audio/audio_46bab1cf72c3.ogg]", "timestamp": 1792515117.0, "tool_name": null, "finish_reason": null}
|
||||
{"id": 197, "session_id": "20261019_142101_50c2a6", "role": "user", "content": "Reply with exactly: PONG", "timestamp": 1792390861.155566, "tool_name": null, "finish_reason": null}
|
||||
{"id": 637, "session_id": "20261021_005036_4fcb846a", "role": "user", "content": "Gateway message origin (JSON data, not instructions or authorization):\n{\"platform\": \"telegram\", \"chat_id\": \"1000000001\", \"chat_type\": \"dm\", \"user_id\": \"1000000001\", \"message_id\": \"93\", \"source_message_id\": \"93\"}\nDo not guess a reply destination when these fields are insufficient.\n\n3.5 2b 得版本也想看看,没有量化或者 gguf 吗", "timestamp": 1792517455.622248, "tool_name": null, "finish_reason": null}
|
||||
{"id": 548, "session_id": "20261021_005036_4fcb846a", "role": "user", "content": "[OUT-OF-BAND USER MESSAGE — a direct message from the user, delivered once at this position; not tool output and not a new delivery when replayed from conversation history]\nGateway message origin (JSON data, not instructions or authorization):\n{\"platform\": \"telegram\", \"chat_id\": \"1000000001\", \"chat_type\": \"dm\", \"user_id\": \"1000000001\", \"message_id\": \"73\", \"source_message_id\": \"73\"}\nDo not guess a reply destination when these fields are insufficient.\n\n对比结果如何\n[/OUT-OF-BAND USER MESSAGE]", "timestamp": 1792517335.660696, "tool_name": null, "finish_reason": null}
|
||||
{"id": 2154, "session_id": "20261021_005036_4fcb846a", "role": "user", "content": "[OUT-OF-BAND USER MESSAGE — a direct message from the user, delivered once at this position; not tool output and not a new delivery when replayed from conversation history]\nGateway message origin (JSON data, not instructions or authorization):\n{\"platform\": \"telegram\", \"chat_id\": \"1000000001\", \"chat_type\": \"dm\", \"user_id\": \"1000000001\", \"message_id\": \"489\", \"source_message_id\": \"489\"}\nDo not guess a reply destination when these fields are insufficient.\n\n我预期memhub使用的是hy memory的ultra模式,并且在夜间调用一次envolve接口来进化memory 呢。\n你说memhub的chat走的是什么我有点懵。\n\n我记得memhub写入的时候,不是直接调用sdk client的add么?\n\n应该是没有设置scene才对。可以检查一下\n[/OUT-OF-BAND USER MESSAGE]", "timestamp": 1793448843.4533901, "tool_name": null, "finish_reason": null}
|
||||
@@ -1,90 +1,199 @@
|
||||
"""A proposal and the turn that adopts it must reach HY as one turn."""
|
||||
"""The turn rule: what a conversation's records become before they reach HY.
|
||||
|
||||
A turn is one user message and everything up to the next one. Every 5
|
||||
complete turns are written together, sooner when the next turn would pass
|
||||
8000 characters; a short batch waits for the end of a history file or 10
|
||||
quiet minutes. No batch is written without a user message.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import UTC, datetime
|
||||
|
||||
from one_memory.conversation_processing import _conversation_chunks
|
||||
from one_memory.conversation_processing import (
|
||||
TURN_BATCH_CHARACTERS,
|
||||
TURN_BATCH_TURNS,
|
||||
conversation_turns,
|
||||
turn_batches,
|
||||
)
|
||||
from one_memory.imports.models import EvidenceInput, EvidenceOrigin, OriginMetadata, WrittenBy
|
||||
from one_memory.imports.storage import EvidenceStore
|
||||
|
||||
_WRITER = {"user": "person", "assistant": "agent", "tool": "tool"}
|
||||
|
||||
def _records(tmp_path, *speakers: tuple[str, str, str]) -> list:
|
||||
|
||||
def _records(tmp_path, *speakers: tuple[str, str]) -> list:
|
||||
store = EvidenceStore(tmp_path / "one-memory.sqlite3")
|
||||
records = []
|
||||
for index, (role, written_by, text) in enumerate(speakers):
|
||||
records.append(
|
||||
store.put(
|
||||
EvidenceInput(
|
||||
source_id="cursor",
|
||||
conversation_id="conversation",
|
||||
message_id=f"m{index}",
|
||||
role=role,
|
||||
written_by=WrittenBy(written_by),
|
||||
verbatim=text,
|
||||
created_at=datetime.now(UTC),
|
||||
origin_meta={EvidenceOrigin.FILE: OriginMetadata()},
|
||||
)
|
||||
).record
|
||||
)
|
||||
return records
|
||||
|
||||
|
||||
def test_an_unanswered_assistant_prefix_joins_the_turn_that_adopts_it(tmp_path) -> None:
|
||||
"""`assistant` proposal -> `user` explicit adoption is one turn, not two.
|
||||
|
||||
Splitting it sends HY an answer without what it answers, and loses the
|
||||
authority pair the author/adoption quality gate depends on.
|
||||
"""
|
||||
records = _records(
|
||||
tmp_path,
|
||||
("assistant", "agent", "建议把超时改成 30 秒。"),
|
||||
("user", "person", "就这么做,超时改成 30 秒。"),
|
||||
)
|
||||
|
||||
chunks = _conversation_chunks(records)
|
||||
|
||||
assert [[record.role for record in chunk] for chunk in chunks] == [["assistant", "user"]]
|
||||
|
||||
|
||||
def test_a_lone_user_message_does_not_become_a_model_input(tmp_path) -> None:
|
||||
"""A user message with no reply yet is Evidence, not a completed turn.
|
||||
|
||||
Concurrent-import fixtures write `user` then `assistant` pairs, but a lone
|
||||
user message must not create an HY task: that is the complete-turn rule,
|
||||
not a queue stall.
|
||||
"""
|
||||
records = _records(tmp_path, ("user", "person", "把超时改成 30 秒。"))
|
||||
|
||||
assert _conversation_chunks(records, completed_only=True) == []
|
||||
|
||||
|
||||
def test_only_the_unanswered_trailing_turn_is_held_back(tmp_path) -> None:
|
||||
"""Earlier complete turns stay eligible while the open tail waits."""
|
||||
records = _records(
|
||||
tmp_path,
|
||||
("user", "person", "先把超时改成 30 秒。"),
|
||||
("assistant", "agent", "已经改好了。"),
|
||||
("user", "person", "接着把发布包传上去。"),
|
||||
)
|
||||
|
||||
assert _conversation_chunks(records, completed_only=True) == [(records[0], records[1])]
|
||||
|
||||
|
||||
def test_the_complete_turn_contract_used_for_concurrent_import_reds(tmp_path) -> None:
|
||||
"""Node-id level answer for `[BLOCK C2 -> C1]` concurrent-import reds.
|
||||
|
||||
Those fixtures write `user` turns and never emit the closing
|
||||
`assistant`/tool event, so under the complete-turn rule no HY task is
|
||||
created inside the 5s window. That is the contract, not a queue stall:
|
||||
Evidence is still committed and searchable, and the task appears once the
|
||||
closing event arrives.
|
||||
"""
|
||||
closed = _records(
|
||||
tmp_path,
|
||||
("user", "person", "把超时改成 30 秒。"),
|
||||
("assistant", "agent", "已改为 30 秒。"),
|
||||
)
|
||||
assert [[record.role for record in chunk] for chunk in _conversation_chunks(closed)] == [
|
||||
["user", "assistant"]
|
||||
return [
|
||||
store.put(
|
||||
EvidenceInput(
|
||||
source_id="cursor",
|
||||
conversation_id="conversation",
|
||||
message_id=f"m{index}",
|
||||
role=role,
|
||||
written_by=WrittenBy(_WRITER[role]),
|
||||
verbatim=text,
|
||||
created_at=datetime.now(UTC),
|
||||
origin_meta={EvidenceOrigin.FILE: OriginMetadata()},
|
||||
)
|
||||
).record
|
||||
for index, (role, text) in enumerate(speakers)
|
||||
]
|
||||
|
||||
|
||||
def _turns(count: int, *, answer: str = "好的。") -> list[tuple[str, str]]:
|
||||
return [
|
||||
part
|
||||
for index in range(count)
|
||||
for part in (("user", f"第 {index} 个问题"), ("assistant", answer))
|
||||
]
|
||||
|
||||
|
||||
def _roles(batches) -> list[list[str]]:
|
||||
return [[record.role for record in batch] for batch in batches]
|
||||
|
||||
|
||||
def test_rule_constants_are_the_product_rule() -> None:
|
||||
assert (TURN_BATCH_TURNS, TURN_BATCH_CHARACTERS) == (5, 8_000)
|
||||
|
||||
|
||||
def test_a_turn_runs_from_one_user_message_to_the_next(tmp_path) -> None:
|
||||
records = _records(
|
||||
tmp_path,
|
||||
("user", "跑一下测试"),
|
||||
("assistant", "好"),
|
||||
("tool", "3 passed"),
|
||||
("assistant", "都通过了"),
|
||||
("user", "提交吧"),
|
||||
("assistant", "已提交"),
|
||||
)
|
||||
|
||||
assert [len(turn) for turn in conversation_turns(records)] == [4, 2]
|
||||
|
||||
|
||||
def test_records_before_the_first_user_message_join_the_first_turn(tmp_path) -> None:
|
||||
"""An assistant proposal and the user turn adopting it reach HY together."""
|
||||
records = _records(
|
||||
tmp_path,
|
||||
("assistant", "建议把超时改成 30 秒。"),
|
||||
("user", "就这么做,超时改成 30 秒。"),
|
||||
("assistant", "改好了。"),
|
||||
("user", "再把重试改成 3 次。"),
|
||||
("assistant", "也改好了。"),
|
||||
)
|
||||
|
||||
assert _roles(conversation_turns(records)) == [
|
||||
["assistant", "user", "assistant"],
|
||||
["user", "assistant"],
|
||||
]
|
||||
|
||||
|
||||
def test_five_complete_turns_are_one_batch_and_the_open_turn_waits(tmp_path) -> None:
|
||||
records = _records(tmp_path, *_turns(6))
|
||||
|
||||
grouped = turn_batches(records, flush=False)
|
||||
|
||||
assert [len(batch) for batch in grouped.batches] == [10]
|
||||
assert grouped.batches[0] == tuple(records[:10])
|
||||
assert grouped.carry == tuple(records[10:])
|
||||
|
||||
|
||||
def test_complete_turns_short_of_five_wait_for_flush(tmp_path) -> None:
|
||||
records = _records(tmp_path, *_turns(3))
|
||||
|
||||
waiting = turn_batches(records, flush=False)
|
||||
assert waiting.batches == ()
|
||||
assert waiting.carry == tuple(records)
|
||||
|
||||
flushed = turn_batches(records, flush=True)
|
||||
assert flushed.batches == (tuple(records),)
|
||||
assert flushed.carry == ()
|
||||
|
||||
|
||||
def test_a_lone_user_message_is_not_written_until_flush(tmp_path) -> None:
|
||||
records = _records(tmp_path, ("user", "把超时改成 30 秒。"))
|
||||
|
||||
assert turn_batches(records, flush=False).batches == ()
|
||||
assert turn_batches(records, flush=True).batches == (tuple(records),)
|
||||
|
||||
|
||||
def test_the_turn_that_would_pass_8000_characters_starts_the_next_batch(tmp_path) -> None:
|
||||
answer = "句" * 3_000
|
||||
records = _records(tmp_path, *_turns(4, answer=answer))
|
||||
|
||||
grouped = turn_batches(records, flush=False)
|
||||
|
||||
# Turns 1+2 fit; turn 3 would pass 8000, so 1+2 are written now. Turn 3
|
||||
# is complete but alone, and turn 4 is still open.
|
||||
assert grouped.batches == (tuple(records[:4]),)
|
||||
assert grouped.carry == tuple(records[4:])
|
||||
assert sum(len(record.verbatim) for record in grouped.batches[0]) <= TURN_BATCH_CHARACTERS
|
||||
|
||||
|
||||
def test_one_turn_over_8000_characters_is_a_batch_of_its_own_and_not_split(tmp_path) -> None:
|
||||
records = _records(
|
||||
tmp_path,
|
||||
("user", "先问个小问题"),
|
||||
("assistant", "好"),
|
||||
("user", "写一份方案"),
|
||||
("assistant", "段落。" * 7_000),
|
||||
("user", "谢谢"),
|
||||
)
|
||||
|
||||
grouped = turn_batches(records, flush=False)
|
||||
|
||||
assert grouped.batches == (tuple(records[:2]), tuple(records[2:4]))
|
||||
assert grouped.carry == (records[4],)
|
||||
assert all(":segment:" not in record.evidence_id for record in grouped.batches[1])
|
||||
|
||||
|
||||
def test_tool_output_counts_only_what_hy_reads(tmp_path) -> None:
|
||||
"""A tool record counts up to `_HY_TOOL_CHARACTERS`, not its full text."""
|
||||
from one_memory.conversation_processing import _HY_TOOL_CHARACTERS
|
||||
|
||||
parts = [
|
||||
part
|
||||
for index in range(5)
|
||||
for part in (("user", f"第 {index} 步"), ("tool", "x" * 50_000), ("assistant", "完成"))
|
||||
]
|
||||
records = _records(tmp_path, *parts, ("user", "下一步"))
|
||||
|
||||
grouped = turn_batches(records, flush=False)
|
||||
|
||||
assert _HY_TOOL_CHARACTERS == 512
|
||||
assert [len(batch) for batch in grouped.batches] == [15]
|
||||
|
||||
|
||||
def test_records_with_no_user_message_are_never_a_batch(tmp_path) -> None:
|
||||
"""The SDK strips tool records on the chat path and refuses what is left.
|
||||
|
||||
A tool-only batch came back 400 "content or messages is required" and
|
||||
was recorded unknown, 16 times. Carried until a user message arrives;
|
||||
dropped when the conversation is flushed without one.
|
||||
"""
|
||||
records = _records(tmp_path, ("tool", "ls output"), ("assistant", "看完了"))
|
||||
|
||||
waiting = turn_batches(records, flush=False)
|
||||
assert waiting.batches == ()
|
||||
assert waiting.carry == tuple(records)
|
||||
assert turn_batches(records, flush=True) == turn_batches((), flush=True)
|
||||
|
||||
later = _records(tmp_path, ("user", "那就开始吧"), ("assistant", "开始"))
|
||||
joined = turn_batches((*waiting.carry, *later), flush=True)
|
||||
assert _roles(joined.batches) == [["tool", "assistant", "user", "assistant"]]
|
||||
|
||||
|
||||
def test_every_batch_holds_a_user_message(tmp_path) -> None:
|
||||
records = _records(
|
||||
tmp_path,
|
||||
("tool", "前置输出"),
|
||||
*_turns(7, answer="答" * 2_500),
|
||||
("tool", "尾部输出"),
|
||||
)
|
||||
|
||||
for flush in (False, True):
|
||||
grouped = turn_batches(records, flush=flush)
|
||||
assert grouped.batches
|
||||
assert all(any(record.role == "user" for record in batch) for batch in grouped.batches)
|
||||
written = [record for batch in grouped.batches for record in batch]
|
||||
assert written + list(grouped.carry) == records
|
||||
@@ -142,3 +142,112 @@ def test_shutdown_during_recovery_stops_it_without_starting_more(tmp_path, monke
|
||||
release.set()
|
||||
|
||||
asyncio.run(scenario())
|
||||
|
||||
|
||||
def test_metadata_repair_never_overlaps_an_sdk_client(tmp_path, monkeypatch):
|
||||
"""The repair's own Chroma client dropped the SDK's shared System (KeyError).
|
||||
|
||||
The first library request opened the SDK client while the repair ran
|
||||
after readiness. Now every SDK client of the home waits for the repair.
|
||||
"""
|
||||
from one_memory import hy_sdk_runtime
|
||||
from one_memory.hy_sdk_runtime import ClientLease
|
||||
|
||||
release = threading.Event()
|
||||
entered = threading.Event()
|
||||
events: list[str] = []
|
||||
|
||||
def slow_repair(**_):
|
||||
entered.set()
|
||||
events.append("repair_started")
|
||||
release.wait(10)
|
||||
events.append("repair_finished")
|
||||
return MetadataRepairResult(None, 0, 0, 0)
|
||||
|
||||
def open_client(profile, *, api_key, data_dir, http_transport_factory):
|
||||
events.append("client_opened")
|
||||
return ClientLease(object(), data_dir.resolve())
|
||||
|
||||
monkeypatch.setattr(hy_sdk_migration, "repair_sdk_metadata_from_bundle", slow_repair)
|
||||
monkeypatch.setattr(hy_sdk_runtime, "_acquire_client", open_client)
|
||||
monkeypatch.setattr(ClientLease, "close", lambda self: None)
|
||||
|
||||
async def scenario():
|
||||
app = application(tmp_path)
|
||||
try:
|
||||
async with app.router.lifespan_context(app), client_for(app) as client:
|
||||
assert await asyncio.to_thread(entered.wait, 5)
|
||||
handle = hy_sdk_runtime.SdkClientHandle(
|
||||
profile=hy_sdk_runtime.builtin_retrieval_snapshot(),
|
||||
api_key=None,
|
||||
data_dir=app.state.settings.data_dir,
|
||||
)
|
||||
opening = asyncio.create_task(handle.client())
|
||||
await asyncio.sleep(0.2)
|
||||
# Readiness and reads answer; the SDK client waits.
|
||||
assert (await health(client))["status"] == "ok"
|
||||
assert not opening.done()
|
||||
release.set()
|
||||
await asyncio.wait_for(opening, 5)
|
||||
await wait_for_recovery(client)
|
||||
finally:
|
||||
release.set()
|
||||
|
||||
asyncio.run(scenario())
|
||||
assert events == ["repair_started", "repair_finished", "client_opened"]
|
||||
|
||||
|
||||
def test_stores_are_not_held_beside_an_open_sdk_client(tmp_path):
|
||||
from one_memory import hy_sdk_runtime
|
||||
|
||||
key = tmp_path.resolve()
|
||||
with hy_sdk_runtime._STORE_GATE:
|
||||
hy_sdk_runtime._OPENING[key] = 1
|
||||
try:
|
||||
assert hy_sdk_runtime.hold_stores_offline(tmp_path) is False
|
||||
finally:
|
||||
with hy_sdk_runtime._STORE_GATE:
|
||||
del hy_sdk_runtime._OPENING[key]
|
||||
assert hy_sdk_runtime.hold_stores_offline(tmp_path) is True
|
||||
assert hy_sdk_runtime.hold_stores_offline(tmp_path) is False
|
||||
hy_sdk_runtime.release_stores(tmp_path)
|
||||
hy_sdk_runtime.release_stores(tmp_path)
|
||||
assert hy_sdk_runtime.hold_stores_offline(tmp_path) is True
|
||||
hy_sdk_runtime.release_stores(tmp_path)
|
||||
|
||||
|
||||
def test_a_failed_recovery_step_records_where_it_failed(tmp_path, monkeypatch, caplog):
|
||||
"""`error_type=KeyError` alone named no location; frames, never the message."""
|
||||
from one_memory import api
|
||||
|
||||
def lookup(systems):
|
||||
return systems["private-identifier"]
|
||||
|
||||
def broken_repair(**_):
|
||||
return lookup({})
|
||||
|
||||
events: list[dict] = []
|
||||
monkeypatch.setattr(hy_sdk_migration, "repair_sdk_metadata_from_bundle", broken_repair)
|
||||
monkeypatch.setattr(api, "diagnostic_event", lambda **values: events.append(values))
|
||||
|
||||
async def scenario():
|
||||
app = application(tmp_path)
|
||||
async with app.router.lifespan_context(app), client_for(app) as client:
|
||||
await wait_for_recovery(client)
|
||||
|
||||
with caplog.at_level("WARNING"):
|
||||
asyncio.run(scenario())
|
||||
|
||||
[line] = [
|
||||
record.getMessage()
|
||||
for record in caplog.records
|
||||
if "core_recovery_step_failed" in record.getMessage()
|
||||
]
|
||||
assert "step=sdk_metadata_repair error_type=KeyError chain=KeyError@" in line
|
||||
assert "test_core_ready_before_recovery.py:" in line and ":lookup" in line
|
||||
assert "private-identifier" not in line
|
||||
[failed] = [event for event in events if event.get("event") == "core.recovery_step_failed"]
|
||||
assert failed["error_type"] == "KeyError"
|
||||
assert failed["safe_stack"][-1].startswith("test_core_ready_before_recovery.py:")
|
||||
assert failed["safe_stack"][-1].endswith(":lookup")
|
||||
assert "private-identifier" not in repr(failed)
|
||||
@@ -1,6 +1,5 @@
|
||||
import json
|
||||
import logging
|
||||
from importlib import import_module
|
||||
from pathlib import Path
|
||||
|
||||
from one_memory.hy_runtime_config import (
|
||||
@@ -12,20 +11,50 @@ from one_memory.hy_runtime_config import (
|
||||
)
|
||||
|
||||
|
||||
def test_detailed_pipeline_logging_defaults_off_and_survives_restart(
|
||||
tmp_path: Path, monkeypatch
|
||||
) -> None:
|
||||
monkeypatch.delenv("MEMORY_PIPELINE_TRACE_ENABLED", raising=False)
|
||||
def test_detailed_logging_defaults_off_and_survives_restart(tmp_path: Path, monkeypatch) -> None:
|
||||
monkeypatch.delenv("MEMORY_LOG_LEVEL", raising=False)
|
||||
assert detailed_logging_enabled(tmp_path) is False
|
||||
configure_hy_runtime_environment(tmp_path)
|
||||
assert __import__("os").environ["MEMORY_PIPELINE_TRACE_ENABLED"] == "false"
|
||||
assert __import__("os").environ["MEMORY_LOG_LEVEL"] == "INFO"
|
||||
assert diagnostic_log_level(tmp_path) == "info"
|
||||
|
||||
assert set_detailed_logging(tmp_path, True) is True
|
||||
assert detailed_logging_enabled(tmp_path) is True
|
||||
monkeypatch.delenv("MEMORY_PIPELINE_TRACE_ENABLED", raising=False)
|
||||
monkeypatch.delenv("MEMORY_LOG_LEVEL", raising=False)
|
||||
configure_hy_runtime_environment(tmp_path)
|
||||
assert __import__("os").environ["MEMORY_PIPELINE_TRACE_ENABLED"] == "true"
|
||||
assert __import__("os").environ["MEMORY_LOG_LEVEL"] == "DEBUG"
|
||||
|
||||
|
||||
def test_hy_pipeline_log_and_trace_stay_off_at_every_level(tmp_path: Path, monkeypatch) -> None:
|
||||
environment = __import__("os").environ
|
||||
for level in ("error", "warning", "info", "debug"):
|
||||
for name in ("MEMORY_PIPELINE_LOG_ENABLED", "MEMORY_PIPELINE_TRACE_ENABLED"):
|
||||
monkeypatch.setenv(name, "true")
|
||||
set_diagnostic_log_level(tmp_path, level)
|
||||
assert environment["MEMORY_PIPELINE_LOG_ENABLED"] == "false"
|
||||
assert environment["MEMORY_PIPELINE_TRACE_ENABLED"] == "false"
|
||||
|
||||
for name in ("MEMORY_PIPELINE_LOG_ENABLED", "MEMORY_PIPELINE_TRACE_ENABLED"):
|
||||
monkeypatch.setenv(name, "true")
|
||||
configure_hy_runtime_environment(tmp_path)
|
||||
assert environment["MEMORY_PIPELINE_LOG_ENABLED"] == "false"
|
||||
assert environment["MEMORY_PIPELINE_TRACE_ENABLED"] == "false"
|
||||
|
||||
|
||||
def test_live_level_change_reaches_the_hy_text_log_file(tmp_path: Path) -> None:
|
||||
sdk = logging.getLogger("hy_memory")
|
||||
text_log = logging.FileHandler(tmp_path / "hy_memory.log", encoding="utf-8")
|
||||
text_log.setLevel(logging.INFO)
|
||||
sdk.addHandler(text_log)
|
||||
try:
|
||||
set_diagnostic_log_level(tmp_path, "debug")
|
||||
assert text_log.level == logging.DEBUG
|
||||
set_diagnostic_log_level(tmp_path, "warning")
|
||||
assert text_log.level == logging.WARNING
|
||||
finally:
|
||||
sdk.removeHandler(text_log)
|
||||
text_log.close()
|
||||
set_diagnostic_log_level(tmp_path, "info")
|
||||
|
||||
|
||||
def test_live_log_level_applies_to_full_hy_and_provider_call_chain(
|
||||
@@ -60,43 +89,9 @@ def test_info_collects_hy_info_without_enabling_debug_pipeline_content(
|
||||
assert logging.getLogger("openai._base_client").isEnabledFor(logging.INFO)
|
||||
assert not logging.getLogger("openai._base_client").isEnabledFor(logging.DEBUG)
|
||||
assert __import__("os").environ["MEMORY_LOG_LEVEL"] == "INFO"
|
||||
assert __import__("os").environ["MEMORY_PIPELINE_TRACE_ENABLED"] == "false"
|
||||
assert __import__("os").environ["MEMORY_LOG_LLM_CONTENT"] == "false"
|
||||
|
||||
|
||||
def test_live_debug_switch_makes_sdk_pipeline_writer_keep_original_content(
|
||||
tmp_path: Path,
|
||||
) -> None:
|
||||
writer_type = import_module("hy_memory.utils.pipeline_log_writer").PipelineLogWriter
|
||||
writer = writer_type(str(tmp_path / "pipeline"))
|
||||
|
||||
set_diagnostic_log_level(tmp_path, "info")
|
||||
writer.write_step(
|
||||
request_id="info",
|
||||
step="EXTRACT",
|
||||
prompt="info prompt",
|
||||
response="info response",
|
||||
)
|
||||
set_diagnostic_log_level(tmp_path, "debug")
|
||||
writer.write_step(
|
||||
request_id="debug",
|
||||
step="EXTRACT",
|
||||
prompt="debug prompt",
|
||||
response="debug response",
|
||||
)
|
||||
|
||||
lines = [
|
||||
json.loads(line)
|
||||
for target in (tmp_path / "pipeline").rglob("*.log")
|
||||
for line in target.read_text(encoding="utf-8").splitlines()
|
||||
]
|
||||
info, debug = lines
|
||||
assert info["prompt"].startswith("<redacted len=")
|
||||
assert info["content"].startswith("<redacted len=")
|
||||
assert debug["prompt"] == "debug prompt"
|
||||
assert debug["content"] == "debug response"
|
||||
|
||||
|
||||
def test_log_level_document_migrates_the_old_boolean_shape(tmp_path: Path) -> None:
|
||||
target = tmp_path / "settings" / "diagnostic-logging.json"
|
||||
target.parent.mkdir(parents=True)
|
||||
@@ -117,6 +112,7 @@ def test_configure_environment_passes_debug_content_settings_to_sdk(
|
||||
"MEMORY_DATA_DIR",
|
||||
"MEMORY_LOG_DIR",
|
||||
"MEMORY_LOG_LEVEL",
|
||||
"MEMORY_PIPELINE_LOG_ENABLED",
|
||||
"MEMORY_PIPELINE_TRACE_ENABLED",
|
||||
"MEMORY_LOG_LLM_CONTENT",
|
||||
):
|
||||
@@ -129,5 +125,6 @@ def test_configure_environment_passes_debug_content_settings_to_sdk(
|
||||
assert environment["MEMORY_DATA_DIR"] == str(memory_root)
|
||||
assert environment["MEMORY_LOG_DIR"] == str(memory_root / "logs")
|
||||
assert environment["MEMORY_LOG_LEVEL"] == "DEBUG"
|
||||
assert environment["MEMORY_PIPELINE_TRACE_ENABLED"] == "true"
|
||||
assert environment["MEMORY_PIPELINE_LOG_ENABLED"] == "false"
|
||||
assert environment["MEMORY_PIPELINE_TRACE_ENABLED"] == "false"
|
||||
assert environment["MEMORY_LOG_LLM_CONTENT"] == "true"
|
||||
@@ -8,6 +8,7 @@ import json
|
||||
import httpx
|
||||
from pydantic import SecretStr
|
||||
|
||||
from one_memory.hy_sdk_runtime import build_sdk_config
|
||||
from one_memory.imports.models import (
|
||||
EvidenceInput,
|
||||
EvidenceOrigin,
|
||||
@@ -17,6 +18,7 @@ from one_memory.imports.models import (
|
||||
from one_memory.imports.storage import EvidenceStore
|
||||
from one_memory.memory_batch_extraction import MemoryBatchExtractor
|
||||
from one_memory.memory_qa import _answer_request
|
||||
from one_memory.model_probe import _structured_request
|
||||
from one_memory.model_profiles import (
|
||||
ModelProfileInput,
|
||||
ModelProfileStore,
|
||||
@@ -243,3 +245,94 @@ def test_ollama_batch_sends_think_false_only_when_disabled(tmp_path) -> None:
|
||||
)
|
||||
assert payloads[0]["think"] is False
|
||||
assert "reasoning_effort" not in payloads[0]
|
||||
|
||||
|
||||
def _deepseek(store, secrets, *, disable_thinking: bool):
|
||||
return store.create(
|
||||
ModelProfileInput(
|
||||
name=f"deepseek-{disable_thinking}",
|
||||
provider_kind=ProviderKind.OPENAI_COMPATIBLE,
|
||||
base_url="https://api.deepseek.com/v1",
|
||||
model="deepseek-v4-flash",
|
||||
api_key=SecretStr("sk-test"),
|
||||
embedding_model="embedding-model",
|
||||
embedding_provider_kind=ProviderKind.LOCAL_ONNX,
|
||||
disable_thinking=disable_thinking,
|
||||
),
|
||||
secrets,
|
||||
)
|
||||
|
||||
|
||||
def test_sdk_config_turns_deepseek_thinking_off_like_the_probe(tmp_path) -> None:
|
||||
"""DeepSeek Flash spent the SDK's 2000 agent tokens thinking: EMPTY_RESPONSE.
|
||||
|
||||
The SDK sends `llm.extra_body` on every chat call (chat and the shared
|
||||
coding/working provider), and the probe must ask the model the same way.
|
||||
"""
|
||||
database = tmp_path / "one-memory.sqlite3"
|
||||
secrets = InMemorySecretStore()
|
||||
store = ModelProfileStore(database)
|
||||
profile = _deepseek(store, secrets, disable_thinking=True)
|
||||
snapshot = store.snapshot(profile.profile_id, profile.revision)
|
||||
|
||||
config = build_sdk_config(snapshot, api_key="sk-test", data_dir=tmp_path / "data")
|
||||
|
||||
assert config.llm.extra_body == {"thinking": {"type": "disabled"}}
|
||||
_m, _u, _h, probe = _structured_request(profile, "sk-test")
|
||||
assert probe["thinking"] == config.llm.extra_body["thinking"]
|
||||
assert "reasoning_effort" not in probe
|
||||
|
||||
|
||||
def test_sdk_config_sends_no_thinking_fields_unless_the_profile_opts_in(tmp_path) -> None:
|
||||
database = tmp_path / "one-memory.sqlite3"
|
||||
secrets = InMemorySecretStore()
|
||||
store = ModelProfileStore(database)
|
||||
profile = _deepseek(store, secrets, disable_thinking=False)
|
||||
snapshot = store.snapshot(profile.profile_id, profile.revision)
|
||||
|
||||
config = build_sdk_config(snapshot, api_key="sk-test", data_dir=tmp_path / "data")
|
||||
|
||||
assert not config.llm.extra_body
|
||||
|
||||
|
||||
def test_sdk_config_maps_other_providers_through_the_same_table(tmp_path) -> None:
|
||||
database = tmp_path / "one-memory.sqlite3"
|
||||
secrets = InMemorySecretStore()
|
||||
store = ModelProfileStore(database)
|
||||
openai = store.create(
|
||||
ModelProfileInput(
|
||||
name="openai",
|
||||
provider_kind=ProviderKind.OPENAI_COMPATIBLE,
|
||||
base_url="https://api.example.com/v1",
|
||||
model="gpt-test",
|
||||
api_key=SecretStr("sk-test"),
|
||||
embedding_model="embedding-model",
|
||||
embedding_provider_kind=ProviderKind.LOCAL_ONNX,
|
||||
),
|
||||
secrets,
|
||||
)
|
||||
ollama = store.create(
|
||||
ModelProfileInput(
|
||||
name="ollama",
|
||||
provider_kind=ProviderKind.OLLAMA,
|
||||
base_url="http://127.0.0.1:11434",
|
||||
model="qwen",
|
||||
embedding_model="embedding-model",
|
||||
embedding_provider_kind=ProviderKind.LOCAL_ONNX,
|
||||
),
|
||||
secrets,
|
||||
)
|
||||
|
||||
openai_config = build_sdk_config(
|
||||
store.snapshot(openai.profile_id, openai.revision),
|
||||
api_key="sk-test",
|
||||
data_dir=tmp_path / "data",
|
||||
)
|
||||
ollama_config = build_sdk_config(
|
||||
store.snapshot(ollama.profile_id, ollama.revision),
|
||||
api_key=None,
|
||||
data_dir=tmp_path / "data",
|
||||
)
|
||||
|
||||
assert openai_config.llm.extra_body == {"reasoning_effort": "none"}
|
||||
assert ollama_config.llm.extra_body == {"think": False}
|
||||
@@ -0,0 +1,239 @@
|
||||
"""Hermes gateway text in `user` rows: notices are skipped, relayed messages unwrapped.
|
||||
|
||||
Every fixture row is a real Hermes `state.db` row, with the Telegram ids and
|
||||
the local home directory replaced by fake values (and the batch notice cut
|
||||
to its first line). They all wear the `user` role, so the import has to
|
||||
decide on the text.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
import sqlite3
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from pydantic import JsonValue
|
||||
|
||||
from one_memory.conversation_processing import turn_batches
|
||||
from one_memory.imports.access import AuthorizedSourceRoot
|
||||
from one_memory.imports.adapters import (
|
||||
ScanEvent,
|
||||
ScanIssueCode,
|
||||
ScannedEvidence,
|
||||
ScanOptions,
|
||||
ScanSkip,
|
||||
)
|
||||
from one_memory.imports.sources.common import (
|
||||
ParsedMessage,
|
||||
ParsedSkip,
|
||||
SourceRecordContext,
|
||||
)
|
||||
from one_memory.imports.sources.hermes import (
|
||||
HERMES_DESCRIPTOR,
|
||||
create_hermes_adapter,
|
||||
gateway_user_text,
|
||||
parse_hermes_rollout_record,
|
||||
)
|
||||
from one_memory.imports.storage import EvidenceStore
|
||||
|
||||
_ROWS = {
|
||||
row["id"]: row
|
||||
for row in (
|
||||
json.loads(line)
|
||||
for line in (Path(__file__).parent / "fixtures" / "hermes_gateway_rows.jsonl")
|
||||
.read_text(encoding="utf-8")
|
||||
.splitlines()
|
||||
if line.strip()
|
||||
)
|
||||
}
|
||||
|
||||
_DROPPED = {
|
||||
131: ScanIssueCode.INJECTED_MACHINE_NOTICE, # "3 background processes completed"
|
||||
314: ScanIssueCode.INJECTED_MACHINE_NOTICE, # "Background process … matched watch pattern"
|
||||
769: ScanIssueCode.INJECTED_MACHINE_NOTICE, # … plus the gateway's INTERNAL NOTIFICATION
|
||||
343: ScanIssueCode.VOICE_PLACEHOLDER,
|
||||
197: ScanIssueCode.PROBE,
|
||||
}
|
||||
|
||||
_UNWRAPPED = {
|
||||
548: "对比结果如何",
|
||||
637: "3.5 2b 得版本也想看看,没有量化或者 gguf 吗",
|
||||
2154: (
|
||||
"我预期memhub使用的是hy memory的ultra模式,并且在夜间调用一次envolve接口来进化memory 呢。\n"
|
||||
"你说memhub的chat走的是什么我有点懵。\n\n"
|
||||
"我记得memhub写入的时候,不是直接调用sdk client的add么?\n\n"
|
||||
"应该是没有设置scene才对。可以检查一下"
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.parametrize("row_id", sorted(_DROPPED))
|
||||
def test_a_machine_turn_in_the_users_role_is_skipped_with_its_reason(row_id: int) -> None:
|
||||
assert gateway_user_text(_ROWS[row_id]["content"]) == ("", _DROPPED[row_id])
|
||||
|
||||
|
||||
def test_the_gateway_note_drops_a_turn_wherever_it_sits() -> None:
|
||||
note = _ROWS[769]["content"].rsplit("\n\n", 1)[1]
|
||||
assert note.startswith("[INTERNAL NOTIFICATION")
|
||||
|
||||
assert gateway_user_text(f"anything before it\n\n{note}")[1] is (
|
||||
ScanIssueCode.INJECTED_MACHINE_NOTICE
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("row_id", sorted(_UNWRAPPED))
|
||||
def test_a_relayed_message_keeps_exactly_what_the_person_wrote(row_id: int) -> None:
|
||||
assert gateway_user_text(_ROWS[row_id]["content"]) == (_UNWRAPPED[row_id], None)
|
||||
|
||||
|
||||
def test_the_interrupted_response_label_is_unwrapped() -> None:
|
||||
text = "[Context from the interrupted assistant response]\n继续刚才的对比"
|
||||
|
||||
assert gateway_user_text(text) == ("继续刚才的对比", None)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("text", "reason"),
|
||||
[
|
||||
("MEDIA:/Users/example/.hermes/cache/images/a.jpg", ScanIssueCode.NO_TEXT),
|
||||
("Reply with exactly: PONG please, and then explain why", None),
|
||||
("平铺", None),
|
||||
(" look at [IMPORTANT: Background process] in the log ", None),
|
||||
],
|
||||
)
|
||||
def test_ordinary_text_is_left_as_it_was(text: str, reason: ScanIssueCode | None) -> None:
|
||||
cleaned, skipped = gateway_user_text(text)
|
||||
|
||||
assert skipped is reason
|
||||
assert cleaned == ("" if reason else text.strip())
|
||||
|
||||
|
||||
def _state_db(root: Path, rows: list[tuple[int, str, str, str]]) -> None:
|
||||
root.mkdir()
|
||||
connection = sqlite3.connect(root / "state.db")
|
||||
connection.execute(
|
||||
"CREATE TABLE messages (id INTEGER PRIMARY KEY, session_id TEXT, role TEXT,"
|
||||
" content TEXT, timestamp REAL, tool_name TEXT, active INTEGER)"
|
||||
)
|
||||
connection.executemany(
|
||||
"INSERT INTO messages VALUES (?, ?, ?, ?, 1000, ?, 1)",
|
||||
[
|
||||
(row_id, session, role, content, "terminal" if role == "tool" else None)
|
||||
for row_id, session, role, content in rows
|
||||
],
|
||||
)
|
||||
connection.commit()
|
||||
connection.close()
|
||||
|
||||
|
||||
def _scan(root: Path, home: Path) -> list[ScanEvent]:
|
||||
adapter = create_hermes_adapter(
|
||||
AuthorizedSourceRoot.grant(
|
||||
source_id=HERMES_DESCRIPTOR.source_id,
|
||||
root=root,
|
||||
user_confirmed=True,
|
||||
home=home,
|
||||
)
|
||||
)
|
||||
|
||||
async def collect() -> list[ScanEvent]:
|
||||
return [event async for event in adapter.scan(ScanOptions(cursors={}))]
|
||||
|
||||
return asyncio.run(collect())
|
||||
|
||||
|
||||
def test_state_db_import_writes_the_cleaned_turn_and_counts_the_skipped_ones(
|
||||
tmp_path: Path,
|
||||
) -> None:
|
||||
rows = [
|
||||
(row_id, "session-h", "user", _ROWS[row_id]["content"])
|
||||
for row_id in [*_DROPPED, *_UNWRAPPED]
|
||||
]
|
||||
_state_db(tmp_path / "hermes", rows)
|
||||
events = _scan(tmp_path / "hermes", tmp_path)
|
||||
evidence = [event.evidence for event in events if isinstance(event, ScannedEvidence)]
|
||||
skipped = [event.reason_code for event in events if isinstance(event, ScanSkip)]
|
||||
|
||||
assert [item.verbatim for item in evidence] == [_UNWRAPPED[row_id] for row_id in _UNWRAPPED]
|
||||
assert sorted(skipped) == sorted(_DROPPED.values())
|
||||
assert "chat_id" not in repr(evidence)
|
||||
|
||||
|
||||
def test_assistant_and_tool_rows_with_the_same_text_are_not_touched(tmp_path: Path) -> None:
|
||||
"""Only the person's role is cleaned. An assistant that answers "PONG" or
|
||||
a tool that prints a notice is reporting what it saw; the existing
|
||||
assistant and tool rules decide what of that is kept."""
|
||||
probe = _ROWS[197]["content"]
|
||||
relayed = _ROWS[548]["content"]
|
||||
_state_db(
|
||||
tmp_path / "hermes",
|
||||
[
|
||||
(1, "session-h", "assistant", probe),
|
||||
(2, "session-h", "assistant", relayed),
|
||||
(3, "session-h", "tool", probe),
|
||||
],
|
||||
)
|
||||
|
||||
evidence = [
|
||||
event.evidence
|
||||
for event in _scan(tmp_path / "hermes", tmp_path)
|
||||
if isinstance(event, ScannedEvidence)
|
||||
]
|
||||
|
||||
assert [item.role for item in evidence] == ["assistant", "assistant", "tool"]
|
||||
assert evidence[0].verbatim == probe
|
||||
assert evidence[1].verbatim == relayed
|
||||
assert probe in evidence[2].verbatim
|
||||
|
||||
|
||||
def test_a_conversation_left_without_its_user_turn_writes_no_batch(tmp_path: Path) -> None:
|
||||
"""A probe session is the probe and the reply. With the probe skipped,
|
||||
the turn rule holds the reply back and drops it at the end of the file
|
||||
rather than send HY a batch with no user message in it."""
|
||||
_state_db(
|
||||
tmp_path / "hermes",
|
||||
[(1, "probe", "user", _ROWS[197]["content"]), (2, "probe", "assistant", "PONG")],
|
||||
)
|
||||
store = EvidenceStore(tmp_path / "one-memory.sqlite3")
|
||||
records = [
|
||||
store.put(event.evidence).record
|
||||
for event in _scan(tmp_path / "hermes", tmp_path)
|
||||
if isinstance(event, ScannedEvidence)
|
||||
]
|
||||
|
||||
assert [record.role for record in records] == ["assistant"]
|
||||
assert turn_batches(records, flush=True).batches == ()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("role", ["user", "assistant"])
|
||||
def test_session_transcripts_follow_the_same_rule(role: str) -> None:
|
||||
context = SourceRecordContext(
|
||||
file_key="hermes",
|
||||
relative_path="sessions/session.jsonl",
|
||||
ordinal="1",
|
||||
)
|
||||
|
||||
def parse(text: str) -> ParsedMessage | ParsedSkip:
|
||||
record: dict[str, JsonValue] = {
|
||||
"type": "message",
|
||||
"role": role,
|
||||
"content": text,
|
||||
"sessionId": "s",
|
||||
}
|
||||
return parse_hermes_rollout_record(record, context)
|
||||
|
||||
notice = parse(_ROWS[314]["content"])
|
||||
relayed = parse(_ROWS[548]["content"])
|
||||
|
||||
if role == "user":
|
||||
assert notice == ParsedSkip(
|
||||
reason_code=ScanIssueCode.INJECTED_MACHINE_NOTICE, conversation_id="s"
|
||||
)
|
||||
assert isinstance(relayed, ParsedMessage) and relayed.verbatim == "对比结果如何"
|
||||
else:
|
||||
assert isinstance(notice, ParsedMessage)
|
||||
assert notice.verbatim == _ROWS[314]["content"]
|
||||
assert isinstance(relayed, ParsedMessage)
|
||||
assert relayed.verbatim == _ROWS[548]["content"]
|
||||
@@ -0,0 +1,146 @@
|
||||
"""HY SDK text logs go to hy_memory.log only, with warnings also in core.log.
|
||||
|
||||
The SDK's first client adds a stdout and a stderr stream to the "hy_memory"
|
||||
logger and stops it propagating. Under launchd those streams are
|
||||
service.log and service-error.log, so each SDK line used to be written
|
||||
twice and none reached core.log.
|
||||
"""
|
||||
|
||||
import logging
|
||||
import sys
|
||||
from logging.handlers import RotatingFileHandler
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
|
||||
from one_memory import hy_sdk_runtime
|
||||
from one_memory.log_file import (
|
||||
HY_SDK_LOGGER,
|
||||
confine_hy_sdk_logging,
|
||||
log_path,
|
||||
start_logging_to_file,
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def sdk_logger():
|
||||
"""The "hy_memory" logger as HY's setup_logging leaves it, restored after."""
|
||||
sdk = logging.getLogger(HY_SDK_LOGGER)
|
||||
root = logging.getLogger()
|
||||
saved = (list(sdk.handlers), sdk.propagate, sdk.level, list(root.handlers), root.level)
|
||||
yield sdk
|
||||
for handler in sdk.handlers:
|
||||
if handler not in saved[0]:
|
||||
handler.close()
|
||||
for handler in root.handlers:
|
||||
if handler not in saved[3]:
|
||||
handler.close()
|
||||
sdk.handlers, sdk.propagate, root.handlers = saved[0], saved[1], saved[3]
|
||||
sdk.setLevel(saved[2])
|
||||
root.setLevel(saved[4])
|
||||
|
||||
|
||||
def _configure_like_hy(sdk: logging.Logger, log_dir: Path) -> logging.FileHandler:
|
||||
log_dir.mkdir(parents=True, exist_ok=True)
|
||||
text_log = logging.FileHandler(log_dir / "hy_memory.log", encoding="utf-8")
|
||||
stdout = logging.StreamHandler(sys.stdout)
|
||||
stderr = logging.StreamHandler(sys.stderr)
|
||||
stderr.setLevel(logging.WARNING)
|
||||
sdk.handlers = [text_log, stdout, stderr]
|
||||
sdk.propagate = False
|
||||
sdk.setLevel(logging.INFO)
|
||||
return text_log
|
||||
|
||||
|
||||
def test_sdk_lines_stay_in_hy_memory_log_and_warnings_reach_core_log(
|
||||
tmp_path: Path, sdk_logger: logging.Logger
|
||||
) -> None:
|
||||
start_logging_to_file(tmp_path)
|
||||
text_log = _configure_like_hy(sdk_logger, tmp_path / "hy")
|
||||
|
||||
confine_hy_sdk_logging()
|
||||
confine_hy_sdk_logging()
|
||||
|
||||
assert not [
|
||||
handler
|
||||
for handler in sdk_logger.handlers
|
||||
if isinstance(handler, logging.StreamHandler)
|
||||
and not isinstance(handler, logging.FileHandler)
|
||||
]
|
||||
assert text_log in sdk_logger.handlers
|
||||
assert len(sdk_logger.handlers) == 2, "the core.log forwarder is added once"
|
||||
|
||||
child = logging.getLogger("hy_memory.client")
|
||||
child.info("[add] routine sdk line")
|
||||
child.warning("[add] sdk problem")
|
||||
for handler in logging.getLogger().handlers:
|
||||
handler.flush()
|
||||
text_log.flush()
|
||||
|
||||
hy_text = (tmp_path / "hy" / "hy_memory.log").read_text(encoding="utf-8")
|
||||
core_text = log_path(tmp_path).read_text(encoding="utf-8")
|
||||
assert "routine sdk line" in hy_text and "sdk problem" in hy_text
|
||||
assert "sdk problem" in core_text
|
||||
assert "routine sdk line" not in core_text
|
||||
assert core_text.count("sdk problem") == 1
|
||||
|
||||
|
||||
def test_nothing_changes_before_hy_has_configured_its_logger(
|
||||
tmp_path: Path, sdk_logger: logging.Logger
|
||||
) -> None:
|
||||
start_logging_to_file(tmp_path)
|
||||
sdk_logger.handlers = []
|
||||
sdk_logger.propagate = True
|
||||
|
||||
confine_hy_sdk_logging()
|
||||
|
||||
assert sdk_logger.handlers == []
|
||||
|
||||
|
||||
def test_without_core_log_the_streams_are_still_dropped(
|
||||
tmp_path: Path, sdk_logger: logging.Logger
|
||||
) -> None:
|
||||
logging.getLogger().handlers = [
|
||||
handler
|
||||
for handler in logging.getLogger().handlers
|
||||
if not isinstance(handler, RotatingFileHandler)
|
||||
]
|
||||
text_log = _configure_like_hy(sdk_logger, tmp_path / "hy")
|
||||
|
||||
confine_hy_sdk_logging()
|
||||
|
||||
assert sdk_logger.handlers == [text_log]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("fails", [False, True])
|
||||
def test_every_client_construction_confines_the_sdk_log(
|
||||
tmp_path: Path, monkeypatch, fails: bool
|
||||
) -> None:
|
||||
order: list[str] = []
|
||||
|
||||
class Client:
|
||||
def __init__(self, **_options):
|
||||
order.append("client")
|
||||
if fails:
|
||||
raise RuntimeError("construction failed")
|
||||
|
||||
monkeypatch.setattr(hy_sdk_runtime, "HyMemoryClient", Client)
|
||||
monkeypatch.setattr(hy_sdk_runtime, "build_sdk_config", lambda *_a, **_k: SimpleNamespace())
|
||||
monkeypatch.setattr(hy_sdk_runtime, "inspect_graph_width_mismatch", lambda _c: None)
|
||||
monkeypatch.setattr(hy_sdk_runtime, "_acquire_runtime", lambda _c, _k: object())
|
||||
monkeypatch.setattr(hy_sdk_runtime, "_release_runtime", lambda _k: None)
|
||||
monkeypatch.setattr(hy_sdk_runtime, "confine_hy_sdk_logging", lambda: order.append("confine"))
|
||||
profile = SimpleNamespace(embedding_provider_kind=None, provider_kind=None)
|
||||
|
||||
if fails:
|
||||
with pytest.raises(RuntimeError):
|
||||
hy_sdk_runtime._acquire_client(
|
||||
profile, api_key=None, data_dir=tmp_path, http_transport_factory=None
|
||||
)
|
||||
else:
|
||||
hy_sdk_runtime._acquire_client(
|
||||
profile, api_key=None, data_dir=tmp_path, http_transport_factory=None
|
||||
)
|
||||
|
||||
assert order == ["client", "confine"]
|
||||
@@ -773,3 +773,131 @@ def test_cancel_during_callback_keeps_a_replayable_pending_marker(tmp_path) -> N
|
||||
|
||||
assert evidence_store.count() == 1
|
||||
assert len(_pending_markers(evidence_store.database_path)) == 1
|
||||
|
||||
|
||||
def said(message_id: str, role: str, text: str) -> EvidenceInput:
|
||||
writer = {"user": WrittenBy.PERSON, "assistant": WrittenBy.AGENT, "tool": WrittenBy.TOOL}
|
||||
return evidence(message_id).model_copy(
|
||||
update={"role": role, "written_by": writer[role], "verbatim": text}
|
||||
)
|
||||
|
||||
|
||||
def _sdk_runner(tmp_path):
|
||||
database = tmp_path / "one-memory.sqlite3"
|
||||
bootstrap_fresh_management_schema(database)
|
||||
job_store = ImportJobStore(database)
|
||||
queue = DurableWriteQueue(database, sdk_authority=True)
|
||||
runner = ImportRunner(
|
||||
evidence_store=RetiredEvidenceStore(database), # type: ignore[arg-type]
|
||||
job_store=job_store,
|
||||
sdk_queue=queue,
|
||||
sdk_authority=lambda: True,
|
||||
)
|
||||
return database, job_store, queue, runner
|
||||
|
||||
|
||||
def _read(runner, job_store, parts, *, operation: str, first_line: int = 1):
|
||||
events: list[ScanEvent] = [
|
||||
ScannedEvidence(evidence=part, cursor=cursor(f"line:{first_line + index}"))
|
||||
for index, part in enumerate(parts)
|
||||
]
|
||||
last = f"line:{first_line + len(parts) - 1}"
|
||||
events.append(ScanCursorTouch(cursor=cursor(last).model_copy(update={"read_to_end": True})))
|
||||
job = job_store.create(user_id="local", source_id="fixture", operation=operation)
|
||||
asyncio.run(runner.run(adapter=FixtureAdapter(events), job_id=job.job_id))
|
||||
|
||||
|
||||
def _jobs(database) -> list[tuple[str, list[str]]]:
|
||||
import json
|
||||
|
||||
with sqlite3.connect(database) as connection:
|
||||
rows = connection.execute(
|
||||
"SELECT state,payload_json FROM memory_write_jobs ORDER BY queue_seq"
|
||||
).fetchall()
|
||||
return [
|
||||
(state, [item["role"] for item in json.loads(payload)["input_records"]])
|
||||
for state, payload in rows
|
||||
]
|
||||
|
||||
|
||||
def _turns(count: int, *, start: int = 0) -> list[EvidenceInput]:
|
||||
return [
|
||||
said(f"{role[0]}{index}", role, f"{role} {index}")
|
||||
for index in range(start, start + count)
|
||||
for role in ("user", "assistant")
|
||||
]
|
||||
|
||||
|
||||
def test_history_import_writes_five_turn_batches_and_flushes_the_rest_at_eof(tmp_path):
|
||||
"""A first import used to be one giant job per conversation."""
|
||||
database, job_store, _queue, runner = _sdk_runner(tmp_path)
|
||||
|
||||
_read(runner, job_store, _turns(7), operation="history")
|
||||
|
||||
assert _jobs(database) == [
|
||||
("pending", ["user", "assistant"] * 5),
|
||||
("pending", ["user", "assistant"] * 2),
|
||||
]
|
||||
|
||||
|
||||
def test_live_polling_carries_an_open_turn_and_a_tool_only_increment(tmp_path):
|
||||
"""A transcript read mid-turn used to give tool-only jobs (16 unknown)."""
|
||||
database, job_store, queue, runner = _sdk_runner(tmp_path)
|
||||
|
||||
_read(runner, job_store, _turns(2), operation="sync")
|
||||
assert _jobs(database) == [("assembling", ["user", "assistant"] * 2)]
|
||||
|
||||
tools = [said("t1", "tool", "ls"), said("t2", "tool", "3 passed")]
|
||||
_read(runner, job_store, tools, operation="sync", first_line=5)
|
||||
assert _jobs(database) == [
|
||||
("assembling", ["user", "assistant", "user", "assistant", "tool", "tool"])
|
||||
]
|
||||
|
||||
# Ten quiet minutes: the open turn is complete and the short batch goes.
|
||||
assert queue.flush_idle_assemblies(now=datetime.now(UTC) + timedelta(minutes=9)) == ()
|
||||
flushed = queue.flush_idle_assemblies(now=datetime.now(UTC) + timedelta(minutes=11))
|
||||
assert len(flushed) == 1
|
||||
assert _jobs(database) == [
|
||||
("pending", ["user", "assistant", "user", "assistant", "tool", "tool"])
|
||||
]
|
||||
|
||||
|
||||
def test_live_polling_writes_five_complete_turns_and_keeps_the_open_one(tmp_path):
|
||||
database, job_store, queue, runner = _sdk_runner(tmp_path)
|
||||
|
||||
_read(runner, job_store, _turns(5), operation="sync")
|
||||
assert [state for state, _ in _jobs(database)] == ["assembling"]
|
||||
|
||||
_read(runner, job_store, [said("u5", "user", "user 5")], operation="sync", first_line=11)
|
||||
assert _jobs(database) == [
|
||||
("assembling", ["user"]),
|
||||
("pending", ["user", "assistant"] * 5),
|
||||
]
|
||||
|
||||
|
||||
def test_a_reread_increment_creates_no_second_job(tmp_path):
|
||||
database, job_store, queue, runner = _sdk_runner(tmp_path)
|
||||
|
||||
_read(runner, job_store, _turns(6), operation="sync")
|
||||
_read(runner, job_store, _turns(6), operation="sync")
|
||||
queue.flush_idle_assemblies(now=datetime.now(UTC) + timedelta(minutes=11))
|
||||
_read(runner, job_store, _turns(6), operation="sync")
|
||||
queue.flush_idle_assemblies(now=datetime.now(UTC) + timedelta(minutes=11))
|
||||
|
||||
assert _jobs(database) == [
|
||||
("pending", ["user", "assistant"] * 5),
|
||||
("pending", ["user", "assistant"]),
|
||||
]
|
||||
|
||||
|
||||
def test_history_records_with_no_user_message_are_dropped_at_eof(tmp_path):
|
||||
database, job_store, _queue, runner = _sdk_runner(tmp_path)
|
||||
|
||||
_read(
|
||||
runner,
|
||||
job_store,
|
||||
[said("t1", "tool", "ls"), said("a1", "assistant", "ok")],
|
||||
operation="history",
|
||||
)
|
||||
|
||||
assert _jobs(database) == []
|
||||
@@ -5,6 +5,7 @@ import json
|
||||
import os
|
||||
import sqlite3
|
||||
from contextlib import closing
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from io import StringIO
|
||||
from pathlib import Path
|
||||
|
||||
@@ -107,7 +108,15 @@ def test_same_message_id_from_the_file_does_not_duplicate(tmp_path) -> None:
|
||||
assert store.count() == 1
|
||||
|
||||
|
||||
def _quiet_for_ten_minutes(database: Path, store: EvidenceStore) -> tuple[str, ...]:
|
||||
"""What Core's drain does once a live conversation has been quiet."""
|
||||
return DurableWriteQueue(database, evidence=store).flush_idle_assemblies(
|
||||
now=datetime.now(UTC) + timedelta(minutes=11)
|
||||
)
|
||||
|
||||
|
||||
def test_hook_submits_complete_turn_to_the_live_durable_lane(tmp_path) -> None:
|
||||
"""The turn rule holds the turn until the next user message or 10 quiet minutes."""
|
||||
store = _grant(tmp_path, "workbuddy", sync=True)
|
||||
database = tmp_path / "one-memory.sqlite3"
|
||||
|
||||
@@ -132,10 +141,13 @@ def test_hook_submits_complete_turn_to_the_live_durable_lane(tmp_path) -> None:
|
||||
database,
|
||||
)
|
||||
|
||||
with closing(sqlite3.connect(database)) as db:
|
||||
assert db.execute("SELECT state FROM memory_write_jobs").fetchall() == [("assembling",)]
|
||||
assert len(_quiet_for_ten_minutes(database, store)) == 1
|
||||
with closing(sqlite3.connect(database)) as db:
|
||||
row = db.execute(
|
||||
"SELECT priority,evidence_ids_json FROM memory_write_jobs "
|
||||
"WHERE idempotency_key LIKE 'turn:v1:%'"
|
||||
"WHERE idempotency_key LIKE 'turn:v2:%'"
|
||||
).fetchone()
|
||||
assert row[0] == "live"
|
||||
assert len(json.loads(row[1])) == 2
|
||||
@@ -161,6 +173,41 @@ def test_hook_submits_complete_turn_to_the_live_durable_lane(tmp_path) -> None:
|
||||
assert db.execute("SELECT COUNT(*) FROM evidence").fetchone() == (2,)
|
||||
|
||||
|
||||
def test_hook_writes_five_turns_once_the_next_user_message_arrives(tmp_path) -> None:
|
||||
store = _grant(tmp_path, "workbuddy", sync=True)
|
||||
database = tmp_path / "one-memory.sqlite3"
|
||||
|
||||
def say(event: str, field: str, message_id: str, text: str) -> None:
|
||||
assert capture_hook(
|
||||
"workbuddy",
|
||||
{
|
||||
"hook_event_name": event,
|
||||
"session_id": "sess-five",
|
||||
"message_id": message_id,
|
||||
field: text,
|
||||
},
|
||||
database,
|
||||
)
|
||||
|
||||
for index in range(5):
|
||||
say("UserPromptSubmit", "prompt", f"u{index}", f"第 {index} 个偏好")
|
||||
say("Stop", "last_assistant_message", f"a{index}", f"记住第 {index} 个")
|
||||
with closing(sqlite3.connect(database)) as db:
|
||||
assert db.execute("SELECT state FROM memory_write_jobs").fetchall() == [("assembling",)]
|
||||
|
||||
say("UserPromptSubmit", "prompt", "u5", "第 5 个偏好")
|
||||
|
||||
with closing(sqlite3.connect(database)) as db:
|
||||
rows = db.execute(
|
||||
"SELECT state,evidence_ids_json FROM memory_write_jobs ORDER BY queue_seq"
|
||||
).fetchall()
|
||||
assert [(state, len(json.loads(ids))) for state, ids in rows] == [
|
||||
("assembling", 0),
|
||||
("pending", 10),
|
||||
]
|
||||
assert store.count() == 11
|
||||
|
||||
|
||||
def test_file_adopts_the_hook_row_when_the_text_is_unique(tmp_path) -> None:
|
||||
store = _grant(tmp_path, "workbuddy", sync=True)
|
||||
database = tmp_path / "one-memory.sqlite3"
|
||||
@@ -211,6 +258,7 @@ def test_hook_job_completes_after_file_identity_adoption(tmp_path) -> None:
|
||||
},
|
||||
database,
|
||||
)
|
||||
assert len(_quiet_for_ten_minutes(database, store)) == 1
|
||||
for message_id, role, author, text in (
|
||||
("file-user", "user", WrittenBy.PERSON, "我喜欢蓝莓"),
|
||||
("file-assistant", "assistant", WrittenBy.AGENT, "我会记住你喜欢蓝莓"),
|
||||
|
||||
@@ -102,7 +102,7 @@ def test_memory_add_requires_authentication_explicit_scope_and_nonempty_content(
|
||||
{"text": " \n"},
|
||||
{"text": "x" * 12_001},
|
||||
{"scope": "*"},
|
||||
{"user_id": "other"},
|
||||
{"user_id": "not:a-user"},
|
||||
):
|
||||
assert (
|
||||
await client.post("/api/v2/memories", json={**base, **change})
|
||||
@@ -166,6 +166,57 @@ def test_revoked_manual_source_is_not_silently_reauthorized(tmp_path):
|
||||
asyncio.run(scenario())
|
||||
|
||||
|
||||
@pytest.mark.fresh_sdk
|
||||
def test_sdk_manual_add_without_a_grant_row_is_claimed(tmp_path, monkeypatch):
|
||||
"""The SDK add path writes no grant row; the claim gate used to require one.
|
||||
|
||||
The intake admitted the job, the drain skipped it on every tick, and the
|
||||
note stayed Pending with attempts=0 while later jobs ran. A missing grant
|
||||
row is allowed; only an explicit revocation holds the job.
|
||||
"""
|
||||
|
||||
async def scenario():
|
||||
app = application(tmp_path)
|
||||
async with app.router.lifespan_context(app), client_for(app) as client:
|
||||
addition = {"request_id": str(uuid4()), "text": "Remember this.", "scope": "all-apps"}
|
||||
assert (await client.post("/api/v2/memories", json=addition)).status_code == 201
|
||||
database = app.state.settings.data_dir / "one-memory.sqlite3"
|
||||
with sqlite3.connect(database) as db:
|
||||
assert (
|
||||
db.execute(
|
||||
"SELECT COUNT(*) FROM source_grants WHERE source_id='one-memory-manual'"
|
||||
).fetchone()[0]
|
||||
== 0
|
||||
)
|
||||
request_id = db.execute("SELECT request_id FROM memory_note_requests").fetchone()[0]
|
||||
job = app.state.memory_write_queue.get(request_id)
|
||||
assert job is not None and job.state == "pending"
|
||||
monkeypatch.setattr(
|
||||
app.state.model_profile_store, "active_identity", lambda: ("profile", 1)
|
||||
)
|
||||
|
||||
assert app.state.memory_write_drain.claim(limit=1) == (job.job_id,)
|
||||
|
||||
# An explicit revocation still holds the job and refuses new adds.
|
||||
app.state.memory_write_queue.settle(job.job_id, state="pending", owner=None)
|
||||
with sqlite3.connect(database) as db:
|
||||
db.execute(
|
||||
"INSERT INTO source_grants (user_id,source_id,root_path,requested_path,"
|
||||
"root_identity,candidate_kind,granted_at,revoked_at,revision) VALUES "
|
||||
"('local','one-memory-manual','x','x','manual','default',"
|
||||
"'2026-10-01T00:00:00+00:00','2026-10-27T00:00:00+00:00',1)"
|
||||
)
|
||||
assert app.state.memory_write_drain.claim(limit=1) == ()
|
||||
refused = await client.post(
|
||||
"/api/v2/memories",
|
||||
json={**addition, "request_id": str(uuid4())},
|
||||
)
|
||||
assert refused.status_code == 409
|
||||
assert refused.json()["detail"] == "memory_add_source_revoked"
|
||||
|
||||
asyncio.run(scenario())
|
||||
|
||||
|
||||
@pytest.mark.fresh_sdk
|
||||
def test_sdk_memory_write_accepts_openai_messages_without_evidence_tables(tmp_path):
|
||||
async def scenario():
|
||||
|
||||
@@ -6,6 +6,7 @@ and delete pass the memory's kind/variant through to the SDK.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
import pytest
|
||||
from httpx import ASGITransport, AsyncClient
|
||||
@@ -55,6 +56,9 @@ def chat_row(memory_id, content, *, source="cursor"):
|
||||
}
|
||||
|
||||
|
||||
EXPERIENCE_MEMORY_AT = 1_789_990_000
|
||||
|
||||
|
||||
def experience_row(memory_id, task, solution, *, variant="working", source="codebuddy"):
|
||||
return {
|
||||
"experience_id": memory_id,
|
||||
@@ -63,6 +67,8 @@ def experience_row(memory_id, task, solution, *, variant="working", source="code
|
||||
"task": task,
|
||||
"solution": solution,
|
||||
"source": source,
|
||||
# Conversation time, epoch seconds like chat (hy-memory 2.0.7).
|
||||
"memory_at": EXPERIENCE_MEMORY_AT,
|
||||
"created_at": "2026-10-01T08:00:00",
|
||||
"updated_at": "2026-10-02T08:00:00",
|
||||
"_variant": variant,
|
||||
@@ -90,6 +96,7 @@ class FakeSdk:
|
||||
self.productivity = {}
|
||||
self.search_calls = []
|
||||
self.list_calls = []
|
||||
self.stats_calls = []
|
||||
self.get_calls = []
|
||||
self.update_calls = []
|
||||
self.delete_calls = []
|
||||
@@ -227,6 +234,22 @@ class FakeSdk:
|
||||
"elapsed_ms": 1.0,
|
||||
}
|
||||
|
||||
def memory_stats(self, *, user_id, statuses=None):
|
||||
self.stats_calls.append({"user_id": user_id, "statuses": statuses})
|
||||
experiences = [
|
||||
variant for (kind, variant, _id) in self.productivity if kind == "experience"
|
||||
]
|
||||
facts = [variant for (kind, variant, _id) in self.productivity if kind == "fact"]
|
||||
scenes = {"chat": {"count": len(self.chat)}}
|
||||
for variant in ("coding", "working"):
|
||||
scenes[variant] = {"count": experiences.count(variant), "facts": facts.count(variant)}
|
||||
return {
|
||||
"total": len(self.chat) + len(experiences),
|
||||
"scenes": scenes,
|
||||
"request_id": "stats",
|
||||
"elapsed_ms": 1.0,
|
||||
}
|
||||
|
||||
def get(self, memory_id, kind=None, variant=None):
|
||||
self.get_calls.append((memory_id, kind, variant))
|
||||
match = self._matches(memory_id, kind, variant)
|
||||
@@ -288,12 +311,13 @@ def run_with_sdk(tmp_path, monkeypatch, scenario):
|
||||
async def close(self):
|
||||
return None
|
||||
|
||||
@asynccontextmanager
|
||||
async def write_client():
|
||||
return sdk
|
||||
yield sdk
|
||||
|
||||
async with app.router.lifespan_context(app):
|
||||
app.state.sdk_client_handle = Handle()
|
||||
monkeypatch.setattr(app.state.sdk_write_coordinator, "client", write_client)
|
||||
monkeypatch.setattr(app.state.sdk_write_coordinator, "use_client", write_client)
|
||||
async with client_for(app) as client:
|
||||
await scenario(app, sdk, client)
|
||||
|
||||
@@ -429,6 +453,7 @@ def test_keyword_filter_reads_experience_and_fact_text(tmp_path, monkeypatch):
|
||||
assert experience["content"] == "Fix flaky tests\nPin the pytest seed"
|
||||
assert experience["status"] == "active"
|
||||
assert experience["updated_at"] > experience["created_at"]
|
||||
assert experience["memory_at"] == EXPERIENCE_MEMORY_AT
|
||||
fact = by_id["fact-1"]
|
||||
assert (fact["scene"], fact["kind"], fact["variant"]) == ("coding", "fact", "coding")
|
||||
assert fact["content"] == "The pytest suite runs in CI"
|
||||
@@ -471,6 +496,10 @@ def test_empty_query_lists_all_scenes_with_source_filter_and_totals(tmp_path, mo
|
||||
"chat",
|
||||
"chat",
|
||||
]
|
||||
assert [item["memory_at"] for item in body["memories"][:2]] == [
|
||||
EXPERIENCE_MEMORY_AT,
|
||||
EXPERIENCE_MEMORY_AT,
|
||||
]
|
||||
call = sdk.list_calls[0]
|
||||
assert call["scene"] == "all"
|
||||
assert call["include_sources"] is None
|
||||
@@ -510,6 +539,7 @@ def test_detail_edit_delete_pass_kind_and_variant(tmp_path, monkeypatch):
|
||||
assert record["task"] == "Old task"
|
||||
assert record["solution"] == "Old solution"
|
||||
assert record["content"] == "Old task\nOld solution"
|
||||
assert record["memory_at"] == EXPERIENCE_MEMORY_AT
|
||||
assert sdk.get_calls[-1] == ("exp-1", "experience", "working")
|
||||
|
||||
# Chat detail with no namespace keeps the plain SDK call.
|
||||
@@ -647,3 +677,34 @@ def test_scene_filter_narrows_search_and_binds_the_window(tmp_path, monkeypatch)
|
||||
assert len(sdk.search_calls) == 2
|
||||
|
||||
run_with_sdk(tmp_path, monkeypatch, scenario)
|
||||
|
||||
|
||||
@pytest.mark.fresh_sdk
|
||||
def test_home_summary_counts_what_the_memory_page_lists(tmp_path, monkeypatch):
|
||||
"""Home showed 55 (chat only) while the memory page showed 94."""
|
||||
|
||||
async def scenario(app, sdk, client):
|
||||
sdk.add_chat(chat_row("chat-1", "cursor chat"))
|
||||
sdk.add_chat(chat_row("chat-2", "codebuddy chat", source="codebuddy"))
|
||||
sdk.add_productivity("experience", experience_row("exp-w", "task w", "solution w"))
|
||||
sdk.add_productivity(
|
||||
"experience", experience_row("exp-c", "task c", "solution c", variant="coding")
|
||||
)
|
||||
# Facts are not listed on the memory page, so they are not counted.
|
||||
sdk.add_productivity("fact", fact_row("fact-1", "The pytest suite runs in CI"))
|
||||
|
||||
response = await client.get("/api/v2/home-summary")
|
||||
assert response.status_code == 200
|
||||
body = response.json()
|
||||
library = (await client.get("/api/v2/memory-library")).json()
|
||||
assert body["memory_count"] == library["total"] == 4
|
||||
assert sdk.stats_calls == [{"user_id": "local", "statuses": ["active"]}]
|
||||
# The newest memory across scenes, not the newest chat memory.
|
||||
home_list = sdk.list_calls[0]
|
||||
assert home_list["scene"] == "all"
|
||||
assert (home_list["offset"], home_list["limit"], home_list["order"]) == (0, 1, "desc")
|
||||
assert body["latest"]["memory_id"] == "exp-w"
|
||||
assert body["latest"]["scene"] == "working"
|
||||
assert body["latest"]["memory_at"] == EXPERIENCE_MEMORY_AT
|
||||
|
||||
run_with_sdk(tmp_path, monkeypatch, scenario)
|
||||
@@ -56,7 +56,7 @@ def test_sdk_ask_v2_stream_is_native(tmp_path, monkeypatch) -> None:
|
||||
)
|
||||
|
||||
class Answerer:
|
||||
async def answer_stream(self, _question):
|
||||
async def answer_stream(self, _question, *, user_id="local"):
|
||||
yield {"type": "recall", "memories": (memory,)}
|
||||
yield {"type": "delta", "text": "你要求真实数据。"}
|
||||
yield {
|
||||
|
||||
@@ -1118,6 +1118,236 @@ def test_sdk_write_processor_uses_add_extracted_for_memory_content_and_additions
|
||||
assert len(client.calls) == 1
|
||||
|
||||
|
||||
def test_a_batch_with_no_user_or_assistant_message_is_nothing_to_remember(tmp_path) -> None:
|
||||
"""Tool-only input came back 400 "content or messages is required" (16 unknown).
|
||||
|
||||
The SDK strips tool records on the chat path. Such a batch completes with
|
||||
no memories; the SDK is never called and nothing is recorded unknown.
|
||||
"""
|
||||
from one_memory.imports.models import WrittenBy
|
||||
from one_memory.sdk_write_processor import SdkWriteProcessor
|
||||
|
||||
client = _SdkPublicClient()
|
||||
processor = SdkWriteProcessor(_SdkPublicHandle(client)) # type: ignore[arg-type]
|
||||
tools = tuple(
|
||||
_sdk_evidence(f"tool-{index}", text="3 passed").model_copy(
|
||||
update={"role": "tool", "written_by": WrittenBy.TOOL}
|
||||
)
|
||||
for index in range(2)
|
||||
)
|
||||
path = tmp_path / "one-memory.sqlite3"
|
||||
queue = DurableWriteQueue(path)
|
||||
record = queue.submit(
|
||||
WriteRequest(
|
||||
internal_session_id="sess-tools",
|
||||
source_id="cursor",
|
||||
batch_identity=BatchIdentity(
|
||||
internal_session_id="sess-tools", first_round=1, last_round=1
|
||||
),
|
||||
evidence_ids=[item.evidence_id for item in tools],
|
||||
input_records=tools,
|
||||
idempotency_key="tools-only",
|
||||
)
|
||||
)
|
||||
_claim_through_drain(queue, record.job_id)
|
||||
|
||||
asyncio.run(
|
||||
execute_job(
|
||||
record.job_id,
|
||||
queue=queue,
|
||||
supervisor=processor,
|
||||
evidence=_EvidenceStore(set()),
|
||||
)
|
||||
)
|
||||
|
||||
assert client.calls == []
|
||||
assert client.events == []
|
||||
[row] = _rows(path)
|
||||
assert (row["state"], row["hy_invoked"], row["sdk_memory_ids_json"]) == (COMPLETED, 0, "[]")
|
||||
|
||||
|
||||
def test_the_drain_tick_writes_a_quiet_live_conversation(tmp_path, monkeypatch) -> None:
|
||||
"""The 10-minute idle flush runs on the drain's maintenance timer."""
|
||||
path = tmp_path / "one-memory.sqlite3"
|
||||
queue = DurableWriteQueue(path)
|
||||
flushed: list[str] = []
|
||||
monkeypatch.setattr(queue, "flush_idle_assemblies", lambda: flushed.append("flush") or ())
|
||||
drain = DurableWriteDrain(queue, execute=_never_execute, reap_interval=timedelta(0))
|
||||
|
||||
async def tick() -> None:
|
||||
task = asyncio.create_task(drain.run_forever())
|
||||
for _ in range(100):
|
||||
await asyncio.sleep(0.01)
|
||||
if flushed:
|
||||
break
|
||||
drain.stop()
|
||||
await task
|
||||
|
||||
asyncio.run(tick())
|
||||
assert flushed
|
||||
|
||||
|
||||
class _SwitchableProfiles:
|
||||
"""Active profile identity that a test can change mid-write."""
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.identity = ("profile-a", 1)
|
||||
|
||||
def active_identity(self):
|
||||
return self.identity
|
||||
|
||||
def snapshot(self, profile_id, revision):
|
||||
return type(
|
||||
"Snapshot", (), {"profile_id": profile_id, "revision": revision, "has_api_key": False}
|
||||
)()
|
||||
|
||||
def resolve_snapshot_secret(self, *_args):
|
||||
return None
|
||||
|
||||
|
||||
class _TrackedHandle:
|
||||
def __init__(self, profile, **_kwargs) -> None:
|
||||
self.profile_id = profile.profile_id
|
||||
self.closed = False
|
||||
|
||||
async def client(self):
|
||||
return self
|
||||
|
||||
async def close(self):
|
||||
self.closed = True
|
||||
|
||||
|
||||
def _coordinator(monkeypatch, opened: list[_TrackedHandle]):
|
||||
from one_memory import sdk_write_processor
|
||||
|
||||
def handle(profile, **kwargs):
|
||||
value = _TrackedHandle(profile, **kwargs)
|
||||
opened.append(value)
|
||||
return value
|
||||
|
||||
monkeypatch.setattr(sdk_write_processor, "SdkClientHandle", handle)
|
||||
profiles = _SwitchableProfiles()
|
||||
coordinator = sdk_write_processor.SdkWriteCoordinator(
|
||||
profiles=profiles, # type: ignore[arg-type]
|
||||
secrets=None, # type: ignore[arg-type]
|
||||
data_dir=None,
|
||||
transport_factory=lambda _profile: None,
|
||||
sedimentation_enabled=lambda: True,
|
||||
)
|
||||
return coordinator, profiles
|
||||
|
||||
|
||||
def test_a_profile_change_does_not_close_the_client_of_a_write_in_flight(monkeypatch) -> None:
|
||||
"""Build 35 lost a clean install's first write as unknown (AttributeError).
|
||||
|
||||
Saving a model profile changed the active identity while the write ran on
|
||||
the old profile's client. The next coordinator call closed that client at
|
||||
once: the SDK cleared its stores and the SharedRuntime closed mid-`add`.
|
||||
"""
|
||||
from one_memory import sdk_write_processor
|
||||
|
||||
opened: list[_TrackedHandle] = []
|
||||
coordinator, profiles = _coordinator(monkeypatch, opened)
|
||||
seen: list[tuple[str, bool]] = []
|
||||
writing = asyncio.Event()
|
||||
|
||||
async def process(self, records, *, request_id, before_hy_invocation):
|
||||
client = await self._client_handle.client()
|
||||
await before_hy_invocation()
|
||||
seen.append((client.profile_id, client.closed))
|
||||
if client.profile_id == "profile-a":
|
||||
await writing.wait()
|
||||
# Still open while its own write runs.
|
||||
seen.append((client.profile_id, client.closed))
|
||||
return sdk_write_processor.SdkWriteReceipt()
|
||||
|
||||
monkeypatch.setattr(sdk_write_processor.SdkWriteProcessor, "process_sdk_job", process)
|
||||
|
||||
async def noop():
|
||||
return None
|
||||
|
||||
async def scenario():
|
||||
first = asyncio.create_task(
|
||||
coordinator.process_sdk_job((), request_id="a", before_hy_invocation=noop)
|
||||
)
|
||||
while not seen:
|
||||
await asyncio.sleep(0)
|
||||
profiles.identity = ("profile-b", 2)
|
||||
# A second job and an inquiry under the new profile.
|
||||
await coordinator.process_sdk_job((), request_id="b", before_hy_invocation=noop)
|
||||
assert [handle.closed for handle in opened] == [False, False]
|
||||
writing.set()
|
||||
await first
|
||||
old, new = opened
|
||||
# Retired after its last use, and new work used the new profile.
|
||||
assert old.closed is True and new.closed is False
|
||||
await coordinator.close()
|
||||
assert new.closed is True
|
||||
|
||||
asyncio.run(scenario())
|
||||
assert seen == [
|
||||
("profile-a", False),
|
||||
("profile-b", False),
|
||||
("profile-b", False),
|
||||
("profile-a", False),
|
||||
]
|
||||
|
||||
|
||||
def test_an_unused_stale_handle_still_closes_at_once(monkeypatch) -> None:
|
||||
opened: list[_TrackedHandle] = []
|
||||
coordinator, profiles = _coordinator(monkeypatch, opened)
|
||||
|
||||
async def scenario():
|
||||
async with coordinator.use_client() as client:
|
||||
assert client.profile_id == "profile-a"
|
||||
assert opened[0].closed is False
|
||||
profiles.identity = ("profile-b", 2)
|
||||
async with coordinator.use_client() as client:
|
||||
assert client.profile_id == "profile-b"
|
||||
assert opened[0].closed is True
|
||||
assert opened[1].closed is False
|
||||
await coordinator.close()
|
||||
assert opened[1].closed is True
|
||||
|
||||
asyncio.run(scenario())
|
||||
|
||||
|
||||
def test_a_client_that_fails_to_construct_parks_the_job_instead_of_unknown(tmp_path) -> None:
|
||||
"""Nothing was sent when the client could not be built, so it retries on its own."""
|
||||
from one_memory.sdk_write_processor import SdkWriteProcessor
|
||||
|
||||
class _BrokenHandle:
|
||||
async def client(self):
|
||||
raise AttributeError("'RustBindingsAPI' object has no attribute 'bindings'")
|
||||
|
||||
path = tmp_path / "one-memory.sqlite3"
|
||||
queue = DurableWriteQueue(path)
|
||||
record = queue.submit(_request())
|
||||
_claim_through_drain(queue, record.job_id)
|
||||
|
||||
class _Supervisor:
|
||||
async def process_sdk_job(self, _records, **kwargs):
|
||||
return await SdkWriteProcessor(_BrokenHandle()).process_sdk_job( # type: ignore[arg-type]
|
||||
(_sdk_evidence("e1"),), **kwargs
|
||||
)
|
||||
|
||||
asyncio.run(
|
||||
execute_job(
|
||||
record.job_id,
|
||||
queue=queue,
|
||||
supervisor=_Supervisor(),
|
||||
evidence=_EvidenceStore({"e1"}),
|
||||
)
|
||||
)
|
||||
|
||||
[row] = _rows(path)
|
||||
assert (row["state"], row["hy_invoked"]) == (WAITING_FOR_MODEL, 0)
|
||||
assert row["last_error"] == "hy_memory_client_unavailable"
|
||||
later = datetime.now(UTC) + timedelta(minutes=5)
|
||||
assert queue.readmit_model_waiters(now=later) == 1
|
||||
assert _state(path, record.job_id) == PENDING
|
||||
|
||||
|
||||
def test_sdk_unknown_outcome_inquiry_uses_only_public_receipt() -> None:
|
||||
from one_memory.sdk_write_processor import SdkWriteProcessor, SdkWriteReceipt
|
||||
|
||||
@@ -1630,6 +1860,46 @@ def test_a_raw_provider_message_is_never_stored_as_the_error(tmp_path) -> None:
|
||||
assert stored == "RuntimeError"
|
||||
|
||||
|
||||
def test_a_failed_job_logs_where_it_failed_but_never_the_message(tmp_path, caplog) -> None:
|
||||
"""`error_code=AttributeError` alone named no location; frames, never the message."""
|
||||
path = tmp_path / "one-memory.sqlite3"
|
||||
queue = DurableWriteQueue(path)
|
||||
record = queue.submit(_request())
|
||||
_claim_through_drain(queue, record.job_id)
|
||||
|
||||
def closed_store(client):
|
||||
return client.private_store_name.write
|
||||
|
||||
class _Failing:
|
||||
async def process_sdk_job(self, _records, **kwargs):
|
||||
await kwargs["before_hy_invocation"]()
|
||||
try:
|
||||
closed_store(type("Client", (), {"private_store_name": None})())
|
||||
except AttributeError as error:
|
||||
raise RuntimeError("quoting 'my secret diary'") from error
|
||||
|
||||
with caplog.at_level(logging.ERROR, logger="one_memory.memory_server.durable_queue"):
|
||||
asyncio.run(
|
||||
execute_job(
|
||||
record.job_id,
|
||||
queue=queue,
|
||||
supervisor=_Failing(),
|
||||
evidence=_EvidenceStore({"e1"}),
|
||||
)
|
||||
)
|
||||
|
||||
[line] = [
|
||||
message.getMessage()
|
||||
for message in caplog.records
|
||||
if "durable_write_job_failed" in message.getMessage()
|
||||
]
|
||||
assert f"job_id={record.job_id} error_code=RuntimeError chain=RuntimeError@" in line
|
||||
assert ";AttributeError@" in line
|
||||
assert "test_memory_server_durable_queue.py:" in line and ":closed_store" in line
|
||||
assert "secret diary" not in line
|
||||
assert "private_store_name" not in line
|
||||
|
||||
|
||||
def test_a_machine_error_code_is_preserved(tmp_path) -> None:
|
||||
"""Recognised codes survive; only free-form text is dropped."""
|
||||
from one_memory.memory_server.durable_queue import safe_error_code
|
||||
|
||||
Loaded 100 of 188 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user