Compare commits
91
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8d0a069950 | ||
|
|
cdb44658cf | ||
|
|
2ab04291a9 | ||
|
|
80d3b55723 | ||
|
|
b5ec0861f0 | ||
|
|
47e938da1b | ||
|
|
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 | ||
|
|
dd3809a34b | ||
|
|
efe7df4048 | ||
|
|
072e0e5e90 | ||
|
|
a0221bc434 | ||
|
|
6718ba8bdf |
No files matched your search
@@ -4,6 +4,32 @@ Hy Memory and MemHub are versioned and released independently.
|
||||
|
||||
## Unreleased
|
||||
|
||||
## 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 +94,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)!
|
||||
|
||||
@@ -191,6 +191,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` | 是否启用历史记录 |
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -80,7 +80,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 +98,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
|
||||
@@ -753,16 +754,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,6 +777,10 @@ 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]
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -172,6 +172,20 @@ result = client.add_extracted(
|
||||
该入口仍走 SDK reconcile/持久化;MemHub 只做管理面门禁与可选
|
||||
`memory_id` 记录。
|
||||
|
||||
应用的「添加记忆」、MCP `MemHub_write`(经 broker `POST /memories` 以本机可信身份转发)
|
||||
共用同一个本机 HTTP 合同,直接新增一条记忆:
|
||||
|
||||
```text
|
||||
POST /api/v2/memories {request_id, text, scope: "all-apps"}
|
||||
GET /api/v2/memories/additions/{request_id} # 该次新增的状态
|
||||
```
|
||||
|
||||
两者返回同一回执(`evidence_id`、`text`、`scope`、`created_at`、`state`、`task_id`、
|
||||
`memory_ids`、`error`)。同一 `request_id` 重放返回原回执;内容不同返回 409
|
||||
`memory_add_conflict`,未知 `request_id` 返回 404 `memory_add_not_found`。新增走持久
|
||||
写入队列的优先通道,最终调用 `add_extracted` + reconcile。带外部读取信封的请求
|
||||
一律拒绝(403 `external_endpoint_denied`)。
|
||||
|
||||
## Search:搜索记忆
|
||||
|
||||
```python
|
||||
|
||||
+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` 会停止后续正文记录,但不会
|
||||
自动删除此前已写入的调试文件。
|
||||
|
||||
具体行为以当前源码、网络请求实现和发布说明为准。发现实现与本说明不一致时,请按
|
||||
|
||||
+7
-6
@@ -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` 中、且通过公开校验的制品才属于
|
||||
@@ -109,15 +109,16 @@ Node、Host Bridge 或模型。用户显式执行 `memhub install` 后,CLI 才
|
||||
Python Core 和运行所需的 Python 依赖位于 MemHub 受管 Runtime,不写入系统 Python。
|
||||
MemHub 的 SQLite 数据默认位于 `~/.one-memory`,不在 npm 包或 Runtime 版本目录中。
|
||||
`npm uninstall` 只移除 npm CLI;`memhub uninstall` 移除服务、受管 Runtime 和 CLI 链接,
|
||||
默认保留 Memory。只有独立的 `memhub delete-data` 在 Runtime 停止且二次确认精确路径后
|
||||
才会删除数据。
|
||||
默认保留 Memory。只有独立的 `memhub delete-data` 在 Runtime 停止、显式加 `--yes` 并用
|
||||
`--confirm <数据目录>` 再写一遍同一个目录后才会删除数据(不加参数只预览要删除的目录)。`memhub stop` 停止受管服务时也会关闭
|
||||
登录自动启动,再次 `memhub start` 会恢复。
|
||||
|
||||
当前平台能力和安装边界以源码、CI 与发布说明为准。未完成真机验收的平台不会仅凭跨平台
|
||||
测试宣称可用。
|
||||
|
||||
## 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
|
||||
|
||||
|
||||
@@ -30,9 +30,7 @@ const REQUIRED_COMMAND_SURFACE = [
|
||||
"sync",
|
||||
"memories",
|
||||
"profile",
|
||||
"calls",
|
||||
"notes",
|
||||
"graph",
|
||||
"graph-width",
|
||||
"models",
|
||||
"host",
|
||||
"doctor",
|
||||
@@ -50,19 +48,17 @@ const REQUIRED_COMMAND_SURFACE = [
|
||||
"uninstall",
|
||||
"delete-data",
|
||||
"recall",
|
||||
"setup",
|
||||
"scan",
|
||||
"progress",
|
||||
"jobs",
|
||||
"path",
|
||||
"scan-roots",
|
||||
"adapter",
|
||||
"write",
|
||||
"task",
|
||||
"serve",
|
||||
];
|
||||
|
||||
const REQUIRED_MARKERS = [
|
||||
"backup_restore_confirmation_required",
|
||||
"backup_restore_confirmation_mismatch",
|
||||
"backup_restore_runtime_state_unknown",
|
||||
"backup_restore_maintenance_identity_invalid",
|
||||
"runtime_service_activation_unverified",
|
||||
@@ -79,16 +75,18 @@ const REQUIRED_MARKERS = [
|
||||
* exact literal the catalog usage string contains.
|
||||
*/
|
||||
const REQUIRED_SUBCOMMANDS = [
|
||||
{ command: "calls", marker: "memhub calls clear" },
|
||||
{ command: "notes", marker: "memhub notes add" },
|
||||
{ command: "notes", marker: "memhub notes process" },
|
||||
{ command: "memories", marker: "memhub memories add" },
|
||||
{ command: "memories", marker: "memhub memories add-status" },
|
||||
{ command: "memories", marker: "memhub memories update" },
|
||||
{ command: "memories", marker: "memhub memories delete" },
|
||||
{ command: "write", marker: "memhub write submit" },
|
||||
{ command: "write", marker: "memhub write status" },
|
||||
{ command: "graph", marker: "memhub graph width" },
|
||||
{ command: "graph-width", marker: "memhub graph-width --decision" },
|
||||
{ command: "models", marker: "memhub models add" },
|
||||
{ command: "models", marker: "memhub models test" },
|
||||
{ command: "models", marker: "memhub models edit" },
|
||||
{ command: "models", marker: "memhub models activate" },
|
||||
{ command: "sources", marker: "memhub sources connect" },
|
||||
{ command: "jobs", marker: "memhub jobs start" },
|
||||
{ command: "jobs", marker: "memhub jobs <pause|resume|cancel>" },
|
||||
];
|
||||
@@ -100,57 +98,30 @@ const REQUIRED_SUBCOMMANDS = [
|
||||
* counterexamples on the packaged Runtime") had no machine-checkable probe.
|
||||
* These run without `--lifecycle`, so they are safe against any package.
|
||||
*/
|
||||
const PROFILE_ID = "44444444-4444-4444-8444-444444444444";
|
||||
const STOPPED_DESTRUCTIVE_PROBES = [
|
||||
{ name: "memories_add", args: ["memories", "add", "synthetic memory"] },
|
||||
{
|
||||
name: "calls_clear_preview",
|
||||
args: ["calls", "clear", "--event", "11111111-1111-4111-8111-111111111111"],
|
||||
},
|
||||
{
|
||||
name: "calls_clear_apply",
|
||||
args: [
|
||||
"calls",
|
||||
"clear",
|
||||
"--event",
|
||||
"11111111-1111-4111-8111-111111111111",
|
||||
"--confirm",
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "calls_clear_all_apply",
|
||||
args: ["calls", "clear", "--all", "--confirm"],
|
||||
},
|
||||
{
|
||||
name: "notes_process_apply",
|
||||
args: [
|
||||
"notes",
|
||||
"process",
|
||||
"33333333-3333-4333-8333-333333333333",
|
||||
"--confirm",
|
||||
],
|
||||
name: "memories_delete_apply",
|
||||
args: ["memories", "delete", "m-1", "--yes"],
|
||||
},
|
||||
{
|
||||
name: "graph_width_apply",
|
||||
args: ["graph", "width", "--decision", "keep_existing", "--confirm"],
|
||||
args: ["graph-width", "--decision", "keep_existing"],
|
||||
},
|
||||
{
|
||||
name: "models_activate_apply",
|
||||
args: [
|
||||
"models",
|
||||
"activate",
|
||||
"44444444-4444-4444-8444-444444444444",
|
||||
"--confirm",
|
||||
],
|
||||
name: "graph_width_rebuild_apply",
|
||||
args: ["graph-width", "--decision", "rebuild_with_new", "--yes"],
|
||||
},
|
||||
{ name: "models_activate_apply", args: ["models", "activate", PROFILE_ID] },
|
||||
{ name: "models_test", args: ["models", "test", PROFILE_ID] },
|
||||
{
|
||||
name: "models_remove_apply",
|
||||
args: ["models", "remove", PROFILE_ID, "--yes"],
|
||||
},
|
||||
{
|
||||
name: "models_edit_apply",
|
||||
args: [
|
||||
"models",
|
||||
"edit",
|
||||
"44444444-4444-4444-8444-444444444444",
|
||||
"--config-file",
|
||||
"/nonexistent/patch.json",
|
||||
"--confirm",
|
||||
],
|
||||
name: "sources_connect_import",
|
||||
args: ["sources", "connect", "codex", "--import"],
|
||||
},
|
||||
];
|
||||
|
||||
|
||||
@@ -119,6 +119,7 @@ describe("A19 batch execution", () => {
|
||||
readerFor([]),
|
||||
operations,
|
||||
planned.notFound,
|
||||
{ confirmOverrides: false },
|
||||
);
|
||||
// Exactly one request for two sources — the batch endpoint, per contract.
|
||||
expect(post).toHaveBeenCalledTimes(1);
|
||||
@@ -165,6 +166,7 @@ describe("A19 batch execution", () => {
|
||||
readerFor([]),
|
||||
operations,
|
||||
planned.notFound,
|
||||
{ confirmOverrides: false },
|
||||
);
|
||||
expect(document.state).toBe("partial");
|
||||
const byId = Object.fromEntries(
|
||||
@@ -206,6 +208,7 @@ describe("A19 batch execution", () => {
|
||||
readerFor([]),
|
||||
operations,
|
||||
[],
|
||||
{ confirmOverrides: false },
|
||||
);
|
||||
expect(document.sources.every((row) => row.status === "failed")).toBe(true);
|
||||
expect(
|
||||
@@ -249,6 +252,7 @@ describe("A19 batch execution", () => {
|
||||
readerFor([]),
|
||||
operations,
|
||||
[],
|
||||
{ confirmOverrides: false },
|
||||
);
|
||||
const busy = document.sources.find((row) => row.source_id === "cursor");
|
||||
expect(document.state).toBe("partial");
|
||||
@@ -288,6 +292,7 @@ describe("A19 batch execution", () => {
|
||||
readerFor([]),
|
||||
operations,
|
||||
planned.notFound,
|
||||
{ confirmOverrides: false },
|
||||
);
|
||||
const notFoundRow = document.sources.find(
|
||||
(row) => row.source_id === "cursor",
|
||||
@@ -296,3 +301,82 @@ describe("A19 batch execution", () => {
|
||||
expect(document.not_found_source_ids).toContain("cursor");
|
||||
});
|
||||
});
|
||||
|
||||
describe("environment-override sources need --confirm-overrides", () => {
|
||||
it("leaves them out and names them unless confirmed", async () => {
|
||||
const planned = await planBatchImport(
|
||||
readerFor([
|
||||
found("codex"),
|
||||
found("dsh", {
|
||||
candidate_kind: "environment_override",
|
||||
requires_override_confirmation: true,
|
||||
}),
|
||||
]),
|
||||
);
|
||||
if (planned.state !== "preview") throw new Error("expected preview");
|
||||
const post = vi.fn(
|
||||
async (_path: string, body: Record<string, unknown>) => ({
|
||||
state: "committed" as const,
|
||||
data: {
|
||||
request_id: body.request_id,
|
||||
accepted_source_ids: body.source_ids,
|
||||
failed_sources: {},
|
||||
jobs: [],
|
||||
},
|
||||
}),
|
||||
);
|
||||
const operations = { post } as unknown as CoreOperations;
|
||||
const skipped = await executeBatchImport(
|
||||
planned.candidates,
|
||||
readerFor([]),
|
||||
operations,
|
||||
planned.notFound,
|
||||
{ confirmOverrides: false },
|
||||
);
|
||||
expect(post.mock.calls[0]![1]).toMatchObject({
|
||||
source_ids: ["codex"],
|
||||
confirm_environment_overrides: false,
|
||||
});
|
||||
expect(skipped.override_required_source_ids).toEqual(["dsh"]);
|
||||
expect(renderBatchScanText(skipped)).toContain("--confirm-overrides");
|
||||
|
||||
const confirmed = await executeBatchImport(
|
||||
planned.candidates,
|
||||
readerFor([]),
|
||||
operations,
|
||||
planned.notFound,
|
||||
{ confirmOverrides: true },
|
||||
);
|
||||
expect(post.mock.calls[1]![1]).toMatchObject({
|
||||
source_ids: ["codex", "dsh"],
|
||||
confirm_environment_overrides: true,
|
||||
});
|
||||
expect(confirmed.override_required_source_ids).toBeUndefined();
|
||||
});
|
||||
|
||||
it("refuses without a request when every detected source needs confirmation", async () => {
|
||||
const planned = await planBatchImport(
|
||||
readerFor([
|
||||
found("dsh", {
|
||||
candidate_kind: "environment_override",
|
||||
requires_override_confirmation: true,
|
||||
}),
|
||||
]),
|
||||
);
|
||||
if (planned.state !== "preview") throw new Error("expected preview");
|
||||
const post = vi.fn();
|
||||
const document = await executeBatchImport(
|
||||
planned.candidates,
|
||||
readerFor([]),
|
||||
{ post } as unknown as CoreOperations,
|
||||
planned.notFound,
|
||||
{ confirmOverrides: false },
|
||||
);
|
||||
expect(post).not.toHaveBeenCalled();
|
||||
expect(document).toMatchObject({
|
||||
state: "rejected",
|
||||
code: "environment_override_confirmation_required",
|
||||
});
|
||||
expect(batchScanExitCode(document)).toBe(1);
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,5 @@
|
||||
/** A19: one-shot scan/import of every *detected* source.
|
||||
/** A19: one-shot connect/import of every *detected* source
|
||||
* (`sources connect --all-detected --import`).
|
||||
*
|
||||
* The engine already takes many sources in a single `POST /api/v1/sources/connect`
|
||||
* (`source_ids` is a 1..32 tuple) and answers per source. The CLI used to reject
|
||||
@@ -37,13 +38,16 @@ export interface BatchSourceRow {
|
||||
export interface BatchScanDocument {
|
||||
schema_version: 1;
|
||||
product: string;
|
||||
command: "scan" | "init";
|
||||
command: "sources connect";
|
||||
state: "committed" | "partial" | "rejected" | "unknown" | "no_sources";
|
||||
request_id: string | null;
|
||||
accepted_source_ids: string[];
|
||||
sources: BatchSourceRow[];
|
||||
/** `location_state != found` rows are listed so the user can see what was skipped. */
|
||||
not_found_source_ids: string[];
|
||||
/** Detected sources whose location comes from an environment override and
|
||||
* that were left out because `--confirm-overrides` was not given. */
|
||||
override_required_source_ids?: string[];
|
||||
code?: string;
|
||||
}
|
||||
|
||||
@@ -118,12 +122,39 @@ export async function planBatchImport(reader: CoreReader): Promise<
|
||||
|
||||
/** Run the batch connect once and fold the per-source receipt into rows. */
|
||||
export async function executeBatchImport(
|
||||
candidates: readonly SourceConnectionCandidate[],
|
||||
detected: readonly SourceConnectionCandidate[],
|
||||
reader: CoreReader,
|
||||
operations: CoreOperations,
|
||||
notFound: readonly string[],
|
||||
options: { confirmOverrides: boolean },
|
||||
): Promise<BatchScanDocument> {
|
||||
const requestId = randomUUID();
|
||||
// An environment-override location is a choice the user has to confirm:
|
||||
// without `--confirm-overrides` those sources are left out and named.
|
||||
const skipped = options.confirmOverrides
|
||||
? []
|
||||
: detected
|
||||
.filter((candidate) => candidate.requires_override_confirmation)
|
||||
.map((candidate) => candidate.source_id);
|
||||
const overrideFields =
|
||||
skipped.length > 0 ? { override_required_source_ids: skipped } : {};
|
||||
const candidates = detected.filter(
|
||||
(candidate) => !skipped.includes(candidate.source_id),
|
||||
);
|
||||
if (candidates.length === 0) {
|
||||
return {
|
||||
schema_version: 1,
|
||||
product: PRODUCT,
|
||||
command: "sources connect",
|
||||
state: "rejected",
|
||||
request_id: null,
|
||||
accepted_source_ids: [],
|
||||
sources: [],
|
||||
not_found_source_ids: [...notFound],
|
||||
...overrideFields,
|
||||
code: "environment_override_confirmation_required",
|
||||
};
|
||||
}
|
||||
// Same shared builder as the single-source import and the Web console, so a
|
||||
// batch connect cannot drift from either.
|
||||
const request = buildSourceImportRequest({
|
||||
@@ -141,9 +172,9 @@ export async function executeBatchImport(
|
||||
: [[candidate.source_id, candidate.candidate_revision]],
|
||||
),
|
||||
),
|
||||
confirmEnvironmentOverrides: candidates.some(
|
||||
(candidate) => candidate.requires_override_confirmation,
|
||||
),
|
||||
confirmEnvironmentOverrides:
|
||||
options.confirmOverrides &&
|
||||
candidates.some((candidate) => candidate.requires_override_confirmation),
|
||||
fullRescan: false,
|
||||
onboarding: true,
|
||||
enableSync: true,
|
||||
@@ -154,7 +185,7 @@ export async function executeBatchImport(
|
||||
return {
|
||||
schema_version: 1,
|
||||
product: PRODUCT,
|
||||
command: "scan",
|
||||
command: "sources connect",
|
||||
state: result.state,
|
||||
request_id: requestId,
|
||||
accepted_source_ids: [],
|
||||
@@ -168,6 +199,7 @@ export async function executeBatchImport(
|
||||
failure_reason: result.code,
|
||||
})),
|
||||
not_found_source_ids: [...notFound],
|
||||
...overrideFields,
|
||||
code: result.code,
|
||||
};
|
||||
}
|
||||
@@ -254,7 +286,7 @@ export async function executeBatchImport(
|
||||
return {
|
||||
schema_version: 1,
|
||||
product: PRODUCT,
|
||||
command: "scan",
|
||||
command: "sources connect",
|
||||
state:
|
||||
failedCount === 0
|
||||
? "committed"
|
||||
@@ -270,6 +302,7 @@ export async function executeBatchImport(
|
||||
accepted_source_ids: accepted,
|
||||
sources: rows,
|
||||
not_found_source_ids: [...notFound],
|
||||
...overrideFields,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -301,5 +334,10 @@ export function renderBatchScanText(document: BatchScanDocument): string {
|
||||
: document.state === "partial"
|
||||
? `部分完成:${document.accepted_source_ids.length} 个成功,其余见上方`
|
||||
: `未完成:${document.code ?? "unknown"}`;
|
||||
return `${summary}\n${lines.join("\n")}\n`;
|
||||
const overrides = document.override_required_source_ids ?? [];
|
||||
const hint =
|
||||
overrides.length > 0
|
||||
? `\n未接入(目录来自环境变量覆盖,需确认):${overrides.join("、")}\n确认目录无误后加 --confirm-overrides 重新运行。`
|
||||
: "";
|
||||
return `${summary}\n${lines.join("\n")}${hint}\n`;
|
||||
}
|
||||
@@ -0,0 +1,371 @@
|
||||
// Contract fixes from the CLI review: exit codes, where JSON goes, the global
|
||||
// `--data-dir`, and the write idempotency key.
|
||||
import { mkdtemp, writeFile } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import path from "node:path";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { run } from "./cli.js";
|
||||
import { COMMANDS } from "./command-catalog.js";
|
||||
|
||||
const transport = vi.hoisted(() => ({
|
||||
get: vi.fn(),
|
||||
post: vi.fn(),
|
||||
put: vi.fn(),
|
||||
patch: vi.fn(),
|
||||
delete: vi.fn(),
|
||||
request: vi.fn(),
|
||||
}));
|
||||
vi.mock("axios", () => ({
|
||||
default: {
|
||||
create: vi.fn(() => transport),
|
||||
get: transport.get,
|
||||
post: transport.post,
|
||||
request: transport.request,
|
||||
isAxiosError: () => false,
|
||||
},
|
||||
}));
|
||||
|
||||
const runtime = vi.hoisted(() => ({
|
||||
inspectRuntimeStatus: vi.fn(),
|
||||
discoverRuntimeAttachment: vi.fn(),
|
||||
inspectBackgroundHealth: vi.fn(),
|
||||
}));
|
||||
vi.mock("@one-memory/runtime-control", async (importOriginal) => ({
|
||||
...(await importOriginal<typeof import("@one-memory/runtime-control")>()),
|
||||
...runtime,
|
||||
}));
|
||||
|
||||
const ATTACHED = {
|
||||
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",
|
||||
},
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
for (const method of Object.values(transport)) 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(ATTACHED);
|
||||
runtime.inspectBackgroundHealth.mockResolvedValue(null);
|
||||
});
|
||||
|
||||
describe("--data-dir is a global option", () => {
|
||||
it("points a Runtime-attached command at that data directory", async () => {
|
||||
transport.get.mockResolvedValue({
|
||||
status: 200,
|
||||
data: { request_id: "r", profile: null, elapsed_ms: 1 },
|
||||
});
|
||||
const result = await capturedRun(
|
||||
["profile", "--data-dir", "/synthetic/other", "--json"],
|
||||
{ ONE_MEMORY_DATA_DIR: "/synthetic/default" },
|
||||
);
|
||||
expect(result.code).toBe(0);
|
||||
expect(runtime.inspectRuntimeStatus).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ dataDir: "/synthetic/other" }),
|
||||
);
|
||||
});
|
||||
|
||||
it("is accepted by every command the catalog used to advertise it on", async () => {
|
||||
runtime.inspectRuntimeStatus.mockResolvedValue({
|
||||
state: "stopped",
|
||||
code: "runtime_not_running",
|
||||
});
|
||||
runtime.discoverRuntimeAttachment.mockResolvedValue(null);
|
||||
for (const argv of [
|
||||
["profile"],
|
||||
["scan-roots", "list"],
|
||||
["adapter", "list"],
|
||||
["scan", "--source", "codex"],
|
||||
["write", "status", "r-1"],
|
||||
["backup", "create", "--target", "/synthetic/data/backups/a"],
|
||||
["memories", "list"],
|
||||
]) {
|
||||
const result = await capturedRun([
|
||||
...argv,
|
||||
"--data-dir",
|
||||
"/synthetic/other",
|
||||
"--json",
|
||||
]);
|
||||
expect(result.code, argv.join(" ")).toBe(3);
|
||||
}
|
||||
});
|
||||
|
||||
it("refuses a relative or repeated --data-dir", async () => {
|
||||
for (const argv of [
|
||||
["profile", "--data-dir", "relative"],
|
||||
["profile", "--data-dir", "/a", "--data-dir", "/b"],
|
||||
["profile", "--data-dir"],
|
||||
]) {
|
||||
expect((await capturedRun(argv)).code).toBe(2);
|
||||
}
|
||||
expect(runtime.inspectRuntimeStatus).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe("Core failures are exit 1, not usage errors", () => {
|
||||
it("adapter probe reports a Core refusal with its code on stdout", async () => {
|
||||
transport.request.mockResolvedValue({
|
||||
status: 409,
|
||||
data: { detail: "source_not_authorized" },
|
||||
});
|
||||
const result = await capturedRun([
|
||||
"adapter",
|
||||
"probe",
|
||||
"--source",
|
||||
"codex",
|
||||
"--json",
|
||||
]);
|
||||
expect(result.code).toBe(1);
|
||||
expect(result.stderr).toBe("");
|
||||
expect(JSON.parse(result.stdout)).toMatchObject({
|
||||
command: "adapter probe",
|
||||
state: "failed",
|
||||
code: "source_not_authorized",
|
||||
exit_code: 1,
|
||||
});
|
||||
});
|
||||
|
||||
it("adapter probe prints readable text without --json", async () => {
|
||||
transport.request.mockResolvedValue({
|
||||
status: 200,
|
||||
data: { source: "codex", available: true },
|
||||
});
|
||||
const result = await capturedRun(["adapter", "probe", "--source", "codex"]);
|
||||
expect(result.code).toBe(0);
|
||||
expect(result.stdout).toContain("来源 codex 的探测结果");
|
||||
expect(result.stdout).toContain("available: true");
|
||||
});
|
||||
|
||||
it("scan-roots add posts directly and reports a refusal as exit 1", async () => {
|
||||
transport.request.mockResolvedValue({
|
||||
status: 409,
|
||||
data: { detail: "scan_root_conflict" },
|
||||
});
|
||||
const result = await capturedRun([
|
||||
"scan-roots",
|
||||
"add",
|
||||
"--root",
|
||||
"/synthetic/root",
|
||||
"--source",
|
||||
"codex",
|
||||
"--json",
|
||||
]);
|
||||
expect(result.code).toBe(1);
|
||||
expect(transport.request).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
method: "post",
|
||||
url: "http://127.0.0.1:8765/api/v1/scan-roots",
|
||||
data: { root: "/synthetic/root", source: "codex", recursive: true },
|
||||
}),
|
||||
);
|
||||
expect(JSON.parse(result.stdout).code).toBe("scan_root_conflict");
|
||||
});
|
||||
|
||||
it("the old `path add` spelling still works", async () => {
|
||||
transport.request.mockResolvedValue({ status: 200, data: { ok: true } });
|
||||
const result = await capturedRun([
|
||||
"path",
|
||||
"remove",
|
||||
"--root",
|
||||
"/synthetic/root",
|
||||
"--yes",
|
||||
]);
|
||||
expect(result.code).toBe(0);
|
||||
expect(transport.request.mock.calls[0][0]).toMatchObject({
|
||||
method: "delete",
|
||||
data: { root: "/synthetic/root" },
|
||||
});
|
||||
});
|
||||
|
||||
it("scan --source runs directly", async () => {
|
||||
transport.post.mockResolvedValue({
|
||||
status: 200,
|
||||
data: { job_id: "j1", source_id: "codex", status: "pending" },
|
||||
});
|
||||
const result = await capturedRun(["scan", "--source", "codex", "--json"]);
|
||||
expect(result.code).toBe(0);
|
||||
expect(JSON.parse(result.stdout)).toMatchObject({
|
||||
command: "scan",
|
||||
state: "committed",
|
||||
scan: { job_id: "j1" },
|
||||
});
|
||||
// A scan without a source is a usage error, not a preview.
|
||||
expect((await capturedRun(["scan"])).code).toBe(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe("write submit keeps its idempotency key", () => {
|
||||
async function messagesFile(): Promise<string> {
|
||||
const directory = await mkdtemp(path.join(tmpdir(), "memhub-write-"));
|
||||
const file = path.join(directory, "messages.json");
|
||||
await writeFile(file, JSON.stringify([{ role: "user", content: "hi" }]));
|
||||
return file;
|
||||
}
|
||||
|
||||
it("generates a request id, sends it and prints it with a retry hint", async () => {
|
||||
transport.post.mockImplementation(
|
||||
async (_url: string, body: { request_id: string }) => ({
|
||||
status: 200,
|
||||
data: {
|
||||
request_id: body.request_id,
|
||||
state: "accepted",
|
||||
created_at: "T",
|
||||
updated_at: "T",
|
||||
},
|
||||
}),
|
||||
);
|
||||
const result = await capturedRun([
|
||||
"write",
|
||||
"submit",
|
||||
"--session",
|
||||
"s1",
|
||||
"--messages-file",
|
||||
await messagesFile(),
|
||||
]);
|
||||
expect(result.code).toBe(0);
|
||||
const sent = transport.post.mock.calls[0][1].request_id as string;
|
||||
expect(sent).toMatch(/^[0-9a-f-]{36}$/);
|
||||
expect(result.stdout).toContain(`request_id: ${sent}`);
|
||||
expect(result.stdout).toContain(`--request-id ${sent}`);
|
||||
});
|
||||
|
||||
it("reports a 409 conflict as JSON on stdout with the request id", async () => {
|
||||
transport.post.mockResolvedValue({
|
||||
status: 409,
|
||||
data: { detail: "memory_write_conflict" },
|
||||
});
|
||||
const result = await capturedRun([
|
||||
"write",
|
||||
"submit",
|
||||
"--session",
|
||||
"s1",
|
||||
"--messages-file",
|
||||
await messagesFile(),
|
||||
"--request-id",
|
||||
"req-1",
|
||||
"--json",
|
||||
]);
|
||||
expect(result.code).toBe(1);
|
||||
expect(result.stderr).toBe("");
|
||||
expect(JSON.parse(result.stdout)).toMatchObject({
|
||||
command: "write submit",
|
||||
state: "rejected",
|
||||
request_id: "req-1",
|
||||
code: "memory_write_conflict",
|
||||
});
|
||||
});
|
||||
|
||||
it("write status of an unknown id is exit 1 with state not_found", async () => {
|
||||
transport.get.mockResolvedValue({ status: 404, data: {} });
|
||||
const error = Object.assign(new Error("404"), {
|
||||
response: { status: 404 },
|
||||
});
|
||||
transport.get.mockRejectedValue(error);
|
||||
const result = await capturedRun(["task", "--id", "nope", "--json"]);
|
||||
// `axios.isAxiosError` is mocked false, so the read error surfaces as a
|
||||
// failed read; either way it is exit 1, never a silent exit 0.
|
||||
expect(result.code).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe("doctor and ui use exit 3 for a stopped Runtime", () => {
|
||||
it("doctor exits 3 when the Runtime is stopped", async () => {
|
||||
runtime.inspectRuntimeStatus.mockResolvedValue({
|
||||
state: "stopped",
|
||||
code: "runtime_not_running",
|
||||
});
|
||||
const result = await capturedRun(["doctor", "--json"]);
|
||||
expect(result.code).toBe(3);
|
||||
expect(JSON.parse(result.stdout).schema_version).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe("catalog exit codes", () => {
|
||||
it("lists exit 2 for every command, since every parser can reject", () => {
|
||||
for (const command of COMMANDS) {
|
||||
expect(command.exit_codes, command.name).toContain(2);
|
||||
}
|
||||
});
|
||||
|
||||
it("lists exit 3 for every command that attaches to the Runtime", () => {
|
||||
for (const name of [
|
||||
"status",
|
||||
"doctor",
|
||||
"init",
|
||||
"sources",
|
||||
"sync",
|
||||
"progress",
|
||||
"jobs",
|
||||
"models",
|
||||
"memories",
|
||||
"profile",
|
||||
"graph-width",
|
||||
"host",
|
||||
"recall",
|
||||
"scan-roots",
|
||||
"adapter",
|
||||
"scan",
|
||||
"write",
|
||||
"backup",
|
||||
"ui",
|
||||
"onboarding",
|
||||
]) {
|
||||
const command = COMMANDS.find((entry) => entry.name === name);
|
||||
expect(command?.exit_codes, name).toContain(3);
|
||||
}
|
||||
});
|
||||
|
||||
it("does not claim exit 3 or 4 for lifecycle commands that never return them", () => {
|
||||
for (const name of ["start", "stop", "restart"]) {
|
||||
const command = COMMANDS.find((entry) => entry.name === name);
|
||||
expect(command?.exit_codes).toEqual([0, 1, 2]);
|
||||
}
|
||||
});
|
||||
|
||||
it("says stop turns login auto-start off for a managed service", () => {
|
||||
const stop = COMMANDS.find((entry) => entry.name === "stop");
|
||||
expect(stop?.summary).toContain("登录时也不会自动启动");
|
||||
});
|
||||
});
|
||||
|
||||
async function capturedRun(
|
||||
arguments_: string[],
|
||||
environment: NodeJS.ProcessEnv = { ONE_MEMORY_DATA_DIR: "/synthetic/data" },
|
||||
) {
|
||||
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_, environment), stdout, stderr };
|
||||
} finally {
|
||||
stdoutSpy.mockRestore();
|
||||
stderrSpy.mockRestore();
|
||||
}
|
||||
}
|
||||
@@ -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-")),
|
||||
|
||||
+196
-22
@@ -1,4 +1,11 @@
|
||||
import { mkdtemp, readdir, readFile, realpath, rm } from "node:fs/promises";
|
||||
import {
|
||||
mkdir,
|
||||
mkdtemp,
|
||||
readdir,
|
||||
readFile,
|
||||
realpath,
|
||||
rm,
|
||||
} from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import path from "node:path";
|
||||
import {
|
||||
@@ -2004,16 +2011,11 @@ describe("stopped Runtime is exit 3, not a usage error", () => {
|
||||
["path", "list", "--json"],
|
||||
["adapter", "list", "--json"],
|
||||
["task", "--id", "nope", "--json"],
|
||||
[
|
||||
"write",
|
||||
"submit",
|
||||
"--session",
|
||||
"s",
|
||||
"--messages-file",
|
||||
"/tmp/messages.json",
|
||||
"--json",
|
||||
],
|
||||
["backup", "create", "--target", "/tmp/x", "--yes", "--json"],
|
||||
["write", "status", "nope", "--json"],
|
||||
["backup", "create", "--target", "/tmp/x", "--json"],
|
||||
["scan-roots", "add", "--root", "/tmp/x", "--json"],
|
||||
["scan", "--source", "codex", "--json"],
|
||||
["onboarding", "reset", "--json"],
|
||||
])("%s ... --json exits 3 with runtime_not_running", async (...argv) => {
|
||||
const streams = captureStreams();
|
||||
let code: number;
|
||||
@@ -2023,7 +2025,9 @@ describe("stopped Runtime is exit 3, not a usage error", () => {
|
||||
streams.restore();
|
||||
}
|
||||
expect(code!).toBe(3);
|
||||
const document = JSON.parse(streams.stderr());
|
||||
// A result, not an error: on stdout like every other --json document.
|
||||
expect(streams.stderr()).toBe("");
|
||||
const document = JSON.parse(streams.stdout());
|
||||
expect(document).toMatchObject({
|
||||
code: "runtime_not_running",
|
||||
exit_code: 3,
|
||||
@@ -2032,6 +2036,48 @@ describe("stopped Runtime is exit 3, not a usage error", () => {
|
||||
expect(document.status).not.toBe("usage_error");
|
||||
});
|
||||
|
||||
it("write submit validates its messages file before attaching, then exits 3", async () => {
|
||||
const directory = await mkdtemp(path.join(tmpdir(), "memhub-write-"));
|
||||
const file = path.join(directory, "messages.json");
|
||||
await import("node:fs/promises").then(({ writeFile }) =>
|
||||
writeFile(file, JSON.stringify([{ role: "user", content: "hi" }])),
|
||||
);
|
||||
const streams = captureStreams();
|
||||
let missing: number;
|
||||
let code: number;
|
||||
try {
|
||||
missing = await run(
|
||||
[
|
||||
"write",
|
||||
"submit",
|
||||
"--session",
|
||||
"s",
|
||||
"--messages-file",
|
||||
path.join(directory, "absent.json"),
|
||||
"--json",
|
||||
],
|
||||
{},
|
||||
);
|
||||
code = await run(
|
||||
[
|
||||
"write",
|
||||
"submit",
|
||||
"--session",
|
||||
"s",
|
||||
"--messages-file",
|
||||
file,
|
||||
"--json",
|
||||
],
|
||||
{},
|
||||
);
|
||||
} finally {
|
||||
streams.restore();
|
||||
await rm(directory, { recursive: true, force: true });
|
||||
}
|
||||
expect(missing!).toBe(2);
|
||||
expect(code!).toBe(3);
|
||||
});
|
||||
|
||||
it.each(["pause", "resume"] as const)(
|
||||
"parses sync %s as the direct sync-set command",
|
||||
async (action) => {
|
||||
@@ -2704,7 +2750,8 @@ describe("ui wires the Core session contract without token leakage", () => {
|
||||
} finally {
|
||||
output.restore();
|
||||
}
|
||||
expect(code!).toBe(4);
|
||||
// A stopped Runtime is exit 3, like every other command.
|
||||
expect(code!).toBe(3);
|
||||
const document = JSON.parse(output.value());
|
||||
expect(document).toMatchObject({
|
||||
schema_version: 1,
|
||||
@@ -2733,6 +2780,120 @@ describe("ui wires the Core session contract without token leakage", () => {
|
||||
// refuse while the Runtime owns the data dir, stop+call Core from a
|
||||
// maintenance process, and never report success from a malformed response.
|
||||
describe("backup restore drives the Core managed-restore contract", () => {
|
||||
it("previews without --yes, exits 0 and starts nothing", async () => {
|
||||
const maintenance = vi.fn();
|
||||
const output = captureStdout();
|
||||
let code: number;
|
||||
try {
|
||||
code = await run(
|
||||
["backup", "restore", "--source", "/tmp/x", "--json"],
|
||||
{},
|
||||
{ runMaintenanceRestore: maintenance },
|
||||
);
|
||||
} finally {
|
||||
output.restore();
|
||||
}
|
||||
expect(code!).toBe(0);
|
||||
expect(maintenance).not.toHaveBeenCalled();
|
||||
expect(JSON.parse(output.value())).toMatchObject({
|
||||
command: "backup restore",
|
||||
state: "preview",
|
||||
target: "/tmp/x",
|
||||
applied: false,
|
||||
next_step: "memhub backup restore --source /tmp/x --yes --confirm /tmp/x",
|
||||
});
|
||||
});
|
||||
|
||||
it("deletes data only when --confirm repeats the data directory", async () => {
|
||||
const home = await mkdtemp(path.join(tmpdir(), "memhub-delete-home-"));
|
||||
const dataDir = path.join(home, ".one-memory");
|
||||
await mkdir(path.join(dataDir, "logs"), { recursive: true });
|
||||
const deleteData = async (...extra: string[]) => {
|
||||
const output = captureStdout();
|
||||
try {
|
||||
const code = await run(
|
||||
["delete-data", "--data-dir", dataDir, ...extra, "--json"],
|
||||
{ HOME: home },
|
||||
);
|
||||
return { code, document: JSON.parse(output.value()) };
|
||||
} finally {
|
||||
output.restore();
|
||||
}
|
||||
};
|
||||
try {
|
||||
const refused = await deleteData("--yes");
|
||||
expect(refused.code).toBe(4);
|
||||
expect(refused.document).toMatchObject({
|
||||
state: "blocked",
|
||||
code: "data_deletion_confirmation_required",
|
||||
});
|
||||
expect(await readdir(dataDir)).toEqual(["logs"]);
|
||||
|
||||
const deleted = await deleteData("--yes", "--confirm", dataDir);
|
||||
expect(deleted.code).toBe(0);
|
||||
expect(await readdir(dataDir).catch(() => [])).toEqual([]);
|
||||
} finally {
|
||||
await rm(home, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it("refuses --yes without repeating the backup path", async () => {
|
||||
const maintenance = vi.fn();
|
||||
const output = captureStdout();
|
||||
let code: number;
|
||||
try {
|
||||
code = await run(
|
||||
["backup", "restore", "--source", "/tmp/x", "--yes", "--json"],
|
||||
{},
|
||||
{ runMaintenanceRestore: maintenance },
|
||||
);
|
||||
} finally {
|
||||
output.restore();
|
||||
}
|
||||
expect(code!).toBe(4);
|
||||
expect(maintenance).not.toHaveBeenCalled();
|
||||
expect(JSON.parse(output.value())).toMatchObject({
|
||||
state: "rejected",
|
||||
code: "backup_restore_confirmation_required",
|
||||
next_step:
|
||||
"恢复需要再写一遍备份路径:memhub backup restore --source /tmp/x --yes --confirm /tmp/x",
|
||||
});
|
||||
});
|
||||
|
||||
it("restores with --yes and the repeated backup path", async () => {
|
||||
const maintenance = vi.fn(async () => ({
|
||||
state: "restored" as const,
|
||||
result: {
|
||||
state: "restored" as const,
|
||||
restoredBytes: 1,
|
||||
previousBackupPath: "/tmp/pre.sqlite3",
|
||||
sourceSchemaVersion: 34,
|
||||
},
|
||||
}));
|
||||
const output = captureStdout();
|
||||
let code: number;
|
||||
try {
|
||||
code = await run(
|
||||
[
|
||||
"backup",
|
||||
"restore",
|
||||
"--source",
|
||||
"/tmp/x",
|
||||
"--yes",
|
||||
"--confirm",
|
||||
"/tmp/x",
|
||||
"--json",
|
||||
],
|
||||
{},
|
||||
{ runMaintenanceRestore: maintenance },
|
||||
);
|
||||
} finally {
|
||||
output.restore();
|
||||
}
|
||||
expect(code!).toBe(0);
|
||||
expect(maintenance).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("rejects while the Runtime is running, with a distinct exit code", async () => {
|
||||
runtimeMocks.inspectRuntimeStatus.mockResolvedValueOnce({
|
||||
state: "running",
|
||||
@@ -2849,7 +3010,7 @@ describe("backup restore drives the Core managed-restore contract", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("requires --confirm to repeat the exact source path", async () => {
|
||||
it("refuses a legacy --confirm that does not repeat the source path", async () => {
|
||||
const maintenance = vi.fn();
|
||||
const output = captureStdout();
|
||||
let code: number;
|
||||
@@ -2877,7 +3038,7 @@ describe("backup restore drives the Core managed-restore contract", () => {
|
||||
command: "backup",
|
||||
verb: "restore",
|
||||
state: "rejected",
|
||||
code: "backup_restore_confirmation_required",
|
||||
code: "backup_restore_confirmation_mismatch",
|
||||
});
|
||||
});
|
||||
|
||||
@@ -3029,7 +3190,7 @@ describe("init onboarding", () => {
|
||||
|
||||
// Boolean flags were idempotent, but accepting them twice hid a duplicated
|
||||
// argv fragment that could also have duplicated a value-bearing flag.
|
||||
it.each(["--import", "--full-rescan", "--confirm-overrides", "--dry-run"])(
|
||||
it.each(["--import", "--confirm-overrides"])(
|
||||
"refuses a repeated init %s flag",
|
||||
async (flag) => {
|
||||
expect(await run(["init", "--source", "codex", flag, flag], {})).toBe(2);
|
||||
@@ -3102,12 +3263,25 @@ describe("init onboarding", () => {
|
||||
},
|
||||
);
|
||||
|
||||
it("accepts --dry-run together with --import", async () => {
|
||||
// Parsing must accept the pair; it stops at the Runtime check, which is
|
||||
// not a usage failure.
|
||||
expect(
|
||||
await run(["init", "--source", "codex", "--import", "--dry-run"], {}),
|
||||
).not.toBe(2);
|
||||
// `--dry-run` and `--full-rescan` never did anything (P0-2/P0-3): they are
|
||||
// gone rather than accepted as no-ops.
|
||||
it.each(["--dry-run", "--full-rescan"])(
|
||||
"refuses the removed no-op %s flag",
|
||||
async (flag) => {
|
||||
expect(
|
||||
await run(["init", "--source", "codex", "--import", flag], {}),
|
||||
).toBe(2);
|
||||
},
|
||||
);
|
||||
|
||||
it("still parses the old `init --source --import` spelling", async () => {
|
||||
// It stops at the Runtime check (exit 3), which is not a usage failure.
|
||||
const streams = captureStreams();
|
||||
try {
|
||||
expect(await run(["init", "--source", "codex", "--import"], {})).toBe(3);
|
||||
} finally {
|
||||
streams.restore();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
+1822
-2033
File diff suppressed because it is too large.
Load diff
@@ -58,10 +58,22 @@ const CONFIRM_OPTION: CommandOption = {
|
||||
flag: "--yes",
|
||||
summary: "确认执行;不加该参数时只输出计划,不做任何改动。",
|
||||
};
|
||||
/** The one confirmation convention, for irreversible operations only. */
|
||||
const DESTRUCTIVE_CONFIRM_OPTION: CommandOption = {
|
||||
flag: "--yes",
|
||||
summary:
|
||||
"确认这次不可逆操作;不加时只说明将要做什么,不做任何改动,exit 0。(旧的 --confirm 仍作为别名接受。)",
|
||||
};
|
||||
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。",
|
||||
@@ -70,21 +82,6 @@ const INTERVAL_OPTION: CommandOption = {
|
||||
flag: "--interval <seconds>",
|
||||
summary: "刷新间隔,默认 5 秒,范围 1..300;必须与 --watch 一起使用。",
|
||||
};
|
||||
const PLAN_FILE_OPTION: CommandOption = {
|
||||
flag: "--plan-file <path>",
|
||||
summary: "读取先前 --json 保存的有界计划文件。",
|
||||
};
|
||||
const EXPLICIT_CONFIRM_OPTION: CommandOption = {
|
||||
flag: "--confirm",
|
||||
summary: "确认执行计划文件逐字描述的操作;缺少时绝不写入。",
|
||||
};
|
||||
// Same flag, but for commands that name their target directly instead of
|
||||
// going through a saved plan file; the plan-file wording would be wrong here.
|
||||
const DIRECT_CONFIRM_OPTION: CommandOption = {
|
||||
flag: "--confirm",
|
||||
summary: "确认执行上面预览的这次操作;缺少时绝不写入。",
|
||||
};
|
||||
|
||||
const SERVICE_OPTIONS: readonly CommandOption[] = [
|
||||
PLATFORM_OPTION,
|
||||
HOME_OPTION,
|
||||
@@ -154,55 +151,77 @@ const INSTALL_OPTIONS: readonly CommandOption[] = [
|
||||
const INSTALL_USAGE =
|
||||
"[--source <path> | --release-manifest-url <https-url>] [--channel <stable|prerelease>] [--install-root <path>] [--home <path>] [--bin-dir <path>] [--data-dir <path>] [--uid <id>] [--xdg-config-home <path>] [--no-shell-path] [--shell-profile <path>] [--preserve-data-dir] [--allow-downgrade] [--yes]";
|
||||
|
||||
const USAGE_EXITS = [EXIT_OK, EXIT_FAILED, EXIT_USAGE] as const;
|
||||
const RUNTIME_EXITS = [
|
||||
EXIT_OK,
|
||||
EXIT_FAILED,
|
||||
EXIT_USAGE,
|
||||
EXIT_RUNTIME_STOPPED,
|
||||
] as const;
|
||||
|
||||
const KIND_OPTION: CommandOption = {
|
||||
flag: "--kind <chat|experience|fact>",
|
||||
summary:
|
||||
"记忆所在的命名空间;工作/编码场景的经验和事实需要同时给 --variant。list 输出里每条都带 kind/variant。",
|
||||
};
|
||||
const VARIANT_OPTION: CommandOption = {
|
||||
flag: "--variant <working|coding>",
|
||||
summary:
|
||||
"经验或事实属于工作还是编码场景;必须和 --kind experience|fact 一起用。",
|
||||
};
|
||||
|
||||
export const COMMANDS: readonly CommandDescriptor[] = [
|
||||
{
|
||||
name: "serve",
|
||||
summary: "在前台启动完整 Runtime(Core 与 Host Gateway),不注册系统服务。",
|
||||
usage: "memhub serve",
|
||||
options: [],
|
||||
exit_codes: [EXIT_OK, EXIT_FAILED],
|
||||
exit_codes: USAGE_EXITS,
|
||||
},
|
||||
{
|
||||
name: "start",
|
||||
summary: "启动本机 Runtime;已有通过身份校验的实例时只连接,不重复启动。",
|
||||
summary:
|
||||
"启动本机 Runtime;已有通过身份校验的实例时只连接,不重复启动。受管服务会同时恢复登录时自动启动。",
|
||||
usage: "memhub start [--json]",
|
||||
options: [JSON_OPTION],
|
||||
exit_codes: [EXIT_OK, EXIT_FAILED, EXIT_RUNTIME_STOPPED],
|
||||
exit_codes: USAGE_EXITS,
|
||||
},
|
||||
{
|
||||
name: "stop",
|
||||
summary: "停止通过身份校验的本机 Runtime;身份不明确时不发送信号。",
|
||||
summary:
|
||||
"停止通过身份校验的本机 Runtime;身份不明确时不发送信号。受管服务(launchd/systemd、App)停止后登录时也不会自动启动,直到再次运行 memhub start。",
|
||||
usage: "memhub stop [--json]",
|
||||
options: [JSON_OPTION],
|
||||
exit_codes: [EXIT_OK, EXIT_FAILED],
|
||||
exit_codes: USAGE_EXITS,
|
||||
},
|
||||
{
|
||||
name: "restart",
|
||||
summary: "先停止再启动本机 Runtime,两步串行执行。",
|
||||
usage: "memhub restart [--json]",
|
||||
options: [JSON_OPTION],
|
||||
exit_codes: [EXIT_OK, EXIT_FAILED],
|
||||
exit_codes: USAGE_EXITS,
|
||||
},
|
||||
{
|
||||
name: "status",
|
||||
summary: "只读查询 Runtime 运行状态;不启动服务、不迁移数据、不改配置。",
|
||||
usage: "memhub status [--watch] [--interval <seconds>] [--json]",
|
||||
options: [WATCH_OPTION, INTERVAL_OPTION, JSON_OPTION],
|
||||
exit_codes: [EXIT_OK, EXIT_FAILED, EXIT_RUNTIME_STOPPED],
|
||||
exit_codes: RUNTIME_EXITS,
|
||||
},
|
||||
{
|
||||
name: "doctor",
|
||||
summary: "按诊断口径做同样的只读检查;默认不修复任何东西。",
|
||||
summary:
|
||||
"按诊断口径做只读检查并给出修复建议;默认不修复任何东西。Runtime 未运行时 exit 3。",
|
||||
usage: "memhub doctor [--json]",
|
||||
options: [JSON_OPTION],
|
||||
exit_codes: [EXIT_OK, EXIT_FAILED, EXIT_RUNTIME_STOPPED],
|
||||
exit_codes: RUNTIME_EXITS,
|
||||
},
|
||||
{
|
||||
name: "logs",
|
||||
summary:
|
||||
"读取本机脱敏诊断日志;logs export 会把排查包直接存到下载目录。默认显示最近一次会话的 200 条事件。",
|
||||
"读取本机脱敏诊断日志;logs export 会把排查包(含系统崩溃报告)直接存到下载目录。默认显示最近一次会话的 200 条事件。",
|
||||
usage:
|
||||
"memhub logs export [--json]\nmemhub logs [--since <15m|2h|7d|RFC3339>] [--session <id>] [--trace <id>] [--incident <id>] [--operation <id>] [--request <id>] [--job <id>] [--task <id>] [--component <name>] [--event <name>] [--errors-only] [--tail <1..5000>] [--follow] [--json]",
|
||||
"memhub logs export [--json]\n memhub logs [--since <15m|2h|7d|RFC3339>] [--session <id>] [--trace <id>] [--incident <id>] [--operation <id>] [--request <id>] [--job <id>] [--task <id>] [--component <name>] [--event <name>] [--errors-only] [--tail <1..5000>] [--follow] [--json]",
|
||||
options: [
|
||||
{
|
||||
flag: "--since <time>",
|
||||
@@ -226,64 +245,56 @@ export const COMMANDS: readonly CommandDescriptor[] = [
|
||||
"以 NDJSON 输出到 stdout(每行一个脱敏事件;非单 JSON 文档),供脚本与 Agent 消费。",
|
||||
},
|
||||
],
|
||||
exit_codes: [EXIT_OK, EXIT_FAILED, EXIT_USAGE],
|
||||
exit_codes: USAGE_EXITS,
|
||||
},
|
||||
{
|
||||
name: "init",
|
||||
summary:
|
||||
"首次接入:选择来源 → 选择是否导入历史。接入即开启该 Harness 的处理;后续可用 sync pause/resume 冻结或恢复。在终端且不带 --source 时进入交互引导(默认全选);非终端或加 --json 时不读 stdin、只列出可用来源和下一步。--all-detected 一次接入所有已检测来源。",
|
||||
"首次接入引导。在终端里(未加 --json)进入交互向导:选择来源 → 是否开启处理 → 配置模型。非终端或加 --json 时不读 stdin,只列出可接入的来源和下一步命令。脚本接入请用 sources connect。",
|
||||
usage: "memhub init [--json]",
|
||||
options: [JSON_OPTION],
|
||||
exit_codes: RUNTIME_EXITS,
|
||||
},
|
||||
{
|
||||
name: "sources",
|
||||
summary:
|
||||
"列出来源,直接接入来源,或按 request id 查接入回执。connect 不加 --import 只授权(处理保持冻结:不导入历史、不扫描新会话);加 --import 同时导入历史并开启持续同步,返回 request id。目录来自环境变量覆盖的来源必须加 --confirm-overrides。Hermes 的每个 profile 是一个独立用户:用 --user-id 和 --root 接入,授权并导入历史(会自动排除 ~/.hermes/profiles/**,各用户互不读取)。",
|
||||
usage:
|
||||
"memhub init [--json]\n memhub init --source <id> [--source <id> ...] [--import] [--dry-run] [--full-rescan] [--confirm-overrides] [--json]\n memhub init --all-detected [--import] [--dry-run] [--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: "--source <id>",
|
||||
summary: "要接入的来源,可重复;终端下不给则进入交互引导。",
|
||||
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:
|
||||
"接入并(加 --import 时)导入全部 location_state=found 的来源。",
|
||||
},
|
||||
{
|
||||
flag: "--import",
|
||||
summary: "接入后立即导入该来源的历史会话(默认只接入不导入)。",
|
||||
},
|
||||
{
|
||||
flag: "--dry-run",
|
||||
summary: "只出计划,不接入也不导入;用于先看会做什么。",
|
||||
},
|
||||
{
|
||||
flag: "--full-rescan",
|
||||
summary: "导入时忽略既有游标做全量重扫。",
|
||||
"接入全部已检测到(location_state=found)的来源;与具体来源互斥。",
|
||||
},
|
||||
{
|
||||
flag: "--confirm-overrides",
|
||||
summary: "确认接受环境覆盖类来源(如 DSH_HOME 指定的目录)。",
|
||||
summary:
|
||||
"确认接受目录来自环境变量覆盖的来源(如 DSH_HOME 指定的目录);不加时这些来源被拒绝(指定来源时)或跳过并列出(--all-detected 时)。",
|
||||
},
|
||||
JSON_OPTION,
|
||||
],
|
||||
exit_codes: [EXIT_OK, EXIT_FAILED, EXIT_USAGE, EXIT_RUNTIME_STOPPED],
|
||||
},
|
||||
{
|
||||
name: "setup",
|
||||
summary: "生成无头首次导入计划;只读取已有来源,不探测目录、不写授权。",
|
||||
usage: "memhub setup plan --source <id> [--source <id> ...] [--json]",
|
||||
options: [
|
||||
{
|
||||
flag: "--source <id>",
|
||||
summary: "计划导入的来源,可重复 1..32 次。",
|
||||
flag: "--request-id <uuid>",
|
||||
summary:
|
||||
"--import 的幂等键;用同一个 id 重试只会读回第一次的回执,不会重复导入。",
|
||||
},
|
||||
JSON_OPTION,
|
||||
],
|
||||
exit_codes: [EXIT_OK, EXIT_FAILED, EXIT_USAGE, EXIT_RUNTIME_STOPPED],
|
||||
},
|
||||
{
|
||||
name: "sources",
|
||||
summary: "列出来源、执行已确认的导入计划。",
|
||||
usage:
|
||||
"memhub sources list [--json]\n memhub sources import --plan-file <path> --confirm [--json]\n memhub sources receipt <request-id> [--json]",
|
||||
options: [PLAN_FILE_OPTION, EXPLICIT_CONFIRM_OPTION, JSON_OPTION],
|
||||
exit_codes: [EXIT_OK, EXIT_FAILED, EXIT_USAGE, EXIT_RUNTIME_STOPPED],
|
||||
exit_codes: RUNTIME_EXITS,
|
||||
},
|
||||
{
|
||||
name: "sync",
|
||||
@@ -297,7 +308,7 @@ export const COMMANDS: readonly CommandDescriptor[] = [
|
||||
},
|
||||
JSON_OPTION,
|
||||
],
|
||||
exit_codes: [EXIT_OK, EXIT_FAILED, EXIT_USAGE, EXIT_RUNTIME_STOPPED],
|
||||
exit_codes: RUNTIME_EXITS,
|
||||
},
|
||||
{
|
||||
name: "progress",
|
||||
@@ -318,17 +329,17 @@ export const COMMANDS: readonly CommandDescriptor[] = [
|
||||
INTERVAL_OPTION,
|
||||
JSON_OPTION,
|
||||
],
|
||||
exit_codes: [EXIT_OK, EXIT_FAILED, EXIT_USAGE, EXIT_RUNTIME_STOPPED],
|
||||
exit_codes: RUNTIME_EXITS,
|
||||
},
|
||||
{
|
||||
name: "jobs",
|
||||
summary:
|
||||
"启动或控制单个导入任务:start 创建 history/sync 作业;pause/resume/cancel 控制已有作业。所有写操作先出计划,必须显式 --confirm;list/show 为只读。",
|
||||
"查看或控制导入任务:start 创建 history/sync 任务;pause/resume/cancel 控制已有任务。都直接执行。",
|
||||
usage:
|
||||
"memhub jobs list [--json]\n" +
|
||||
" memhub jobs show <job-id> [--json]\n" +
|
||||
" memhub jobs start --source <id> [--operation <history|sync>] [--confirm] [--json]\n" +
|
||||
" memhub jobs <pause|resume|cancel> --job <id> [--confirm] [--json]",
|
||||
" memhub jobs start --source <id> [--operation <history|sync>] [--json]\n" +
|
||||
" memhub jobs <pause|resume|cancel> --job <id> [--json]",
|
||||
options: [
|
||||
{
|
||||
flag: "--source <id>",
|
||||
@@ -336,123 +347,126 @@ export const COMMANDS: readonly CommandDescriptor[] = [
|
||||
},
|
||||
{
|
||||
flag: "--operation <history|sync>",
|
||||
summary: "start 的导入类型,默认 history。",
|
||||
summary:
|
||||
"start 的导入类型,默认 history;sync 与 scan --source 相同,立即增量同步一次。",
|
||||
},
|
||||
{
|
||||
flag: "--job <id>",
|
||||
summary: "pause/resume/cancel 的目标导入任务。",
|
||||
},
|
||||
DIRECT_CONFIRM_OPTION,
|
||||
JSON_OPTION,
|
||||
],
|
||||
exit_codes: [EXIT_OK, EXIT_FAILED, EXIT_USAGE, EXIT_RUNTIME_STOPPED],
|
||||
exit_codes: RUNTIME_EXITS,
|
||||
},
|
||||
{
|
||||
name: "models",
|
||||
summary:
|
||||
"交互配置模型,或生成配置/探测计划、执行已确认操作与查询回执;脚本模式的凭据只从受控文件读取。",
|
||||
"管理生成模型配置。add/test 直接提交并返回操作 ID(异步,用 receipt 查结果);edit/activate 直接执行;remove 不可逆,需 --yes。<profile-id>/<operation-id> 都是 UUID;脚本模式的凭据只从受控文件读取。",
|
||||
usage:
|
||||
"memhub models list [--json]\n memhub models show <profile-id> [--json]\n memhub models plan --config-file <path> [--json]\n memhub models configure --plan-file <path> --confirm [--api-key-file <path>] [--json]\n memhub models receipt <operation-id> [--json]\n memhub models edit <profile-id> --config-file <path> [--confirm] [--api-key-file <path>] [--json]\n memhub models activate <profile-id> [--confirm] [--json]\n memhub models probe-plan <profile-id> [--json]\n memhub models probe --plan-file <path> --confirm [--json]\n memhub models probe-receipt <operation-id> [--json]\n memhub models remove <profile-id> [--confirm] [--json]",
|
||||
"memhub models list [--json]\n memhub models show <profile-id> [--json]\n memhub models add --config-file <path> [--api-key-file <path>] [--activate] [--json]\n memhub models test <profile-id> [--json]\n memhub models receipt <operation-id> [--json]\n memhub models edit <profile-id> --config-file <path> [--api-key-file <path>] [--json]\n memhub models activate <profile-id> [--json]\n memhub models remove <profile-id> [--yes] [--json]",
|
||||
alternative_usage: "memhub models setup",
|
||||
options: [
|
||||
{
|
||||
flag: "--config-file <path>",
|
||||
summary: "不含 api_key 的有界 JSON 模型配置。",
|
||||
summary:
|
||||
"有界 JSON 模型配置,不能含 api_key;edit 的补丁必须带 expected_revision。",
|
||||
},
|
||||
PLAN_FILE_OPTION,
|
||||
EXPLICIT_CONFIRM_OPTION,
|
||||
{
|
||||
flag: "--api-key-file <path>",
|
||||
summary: "从 regular file 读取凭据;内容不进入参数、输出或错误。",
|
||||
summary: "从属主专用的普通文件读取凭据;内容不进入参数、输出或错误。",
|
||||
},
|
||||
{
|
||||
flag: "--activate",
|
||||
summary: "add 成功后把新配置设为当前使用。",
|
||||
},
|
||||
DESTRUCTIVE_CONFIRM_OPTION,
|
||||
JSON_OPTION,
|
||||
],
|
||||
exit_codes: [EXIT_OK, EXIT_FAILED, EXIT_USAGE, EXIT_RUNTIME_STOPPED],
|
||||
exit_codes: RUNTIME_EXITS,
|
||||
},
|
||||
{
|
||||
name: "memories",
|
||||
summary: "通过 SDK 数据面列出、查看、修改或物理删除记忆。",
|
||||
summary:
|
||||
"列出、搜索、查看、添加、修改或物理删除记忆(对话、工作、编码三个场景)。add 和 App 的「添加记忆」是同一个入口;delete 不可逆,需 --yes。",
|
||||
usage:
|
||||
"memhub memories list [--status <state>] [--offset <n>] [--limit <n>] [--json]\n memhub memories show <memory-id> [--json]\n memhub memories update <memory-id> --content-file <path> [--confirm] [--json]\n memhub memories delete <memory-id> [--confirm] [--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>",
|
||||
summary: "只列一个场景,默认 all。working/coding 列的是该场景的经验。",
|
||||
},
|
||||
{ flag: "--source <id>", summary: "只列某个来源写入的记忆。" },
|
||||
{
|
||||
flag: "--query <text>",
|
||||
summary:
|
||||
"关键词搜索(只搜 active 记忆,在最相关的一批里匹配所有关键词,输出 search_window 时表示可能少计)。",
|
||||
},
|
||||
{
|
||||
flag: "--status <state>",
|
||||
summary: `记忆状态,取值 ${MEMORY_STATUSES.join("、")}。`,
|
||||
summary: `记忆状态,取值 ${MEMORY_STATUSES.join("、")},默认 active。`,
|
||||
},
|
||||
{ flag: "--offset <n>", summary: "跳过前 n 条,默认 0。" },
|
||||
{
|
||||
flag: "--limit <n>",
|
||||
summary: "本页最多返回多少条,默认 100,上限 500。",
|
||||
},
|
||||
KIND_OPTION,
|
||||
VARIANT_OPTION,
|
||||
{
|
||||
flag: "--content-file <path>",
|
||||
summary:
|
||||
"从有界普通文件读取 update 的新正文;预览只显示摘要,--confirm 时才写入。",
|
||||
"从有界普通文件读取正文:add 的新记忆(≤12000 字),或 update 的新内容(经验为 solution)。",
|
||||
},
|
||||
DIRECT_CONFIRM_OPTION,
|
||||
{
|
||||
flag: "--task <text>",
|
||||
summary: "update 经验(--kind experience)时同时改写 task。",
|
||||
},
|
||||
{
|
||||
flag: "--request-id <uuid>",
|
||||
summary:
|
||||
"add 的幂等键;用同一个 id 重试只会读回第一次的结果,不会重复添加。不给时自动生成并输出。",
|
||||
},
|
||||
DESTRUCTIVE_CONFIRM_OPTION,
|
||||
USER_ID_OPTION,
|
||||
JSON_OPTION,
|
||||
],
|
||||
exit_codes: [EXIT_OK, EXIT_FAILED, EXIT_USAGE, EXIT_RUNTIME_STOPPED],
|
||||
exit_codes: RUNTIME_EXITS,
|
||||
},
|
||||
{
|
||||
name: "profile",
|
||||
summary: "读取 SDK 组装后的用户长期画像(L0 + 已启用的长期画像补充)。",
|
||||
usage: "memhub profile [--json]",
|
||||
options: [DATA_DIR_OPTION, JSON_OPTION],
|
||||
exit_codes: [EXIT_OK, EXIT_FAILED, EXIT_USAGE, EXIT_RUNTIME_STOPPED],
|
||||
},
|
||||
{
|
||||
name: "calls",
|
||||
summary:
|
||||
"清除本机调用记录;不可逆删除,默认只出计划,必须显式 --confirm 才执行。",
|
||||
usage:
|
||||
"memhub calls clear --event <id> [--event <id> ...] [--confirm] [--json]\n memhub calls clear --all [--confirm] [--json]",
|
||||
"读取 SDK 组装后的用户长期画像(L0 + 已启用的长期画像补充);--versions 列出 L0 画像的版本链(最新在前,* 为当前版本)。",
|
||||
usage: "memhub profile [--versions] [--user-id <id>] [--json]",
|
||||
options: [
|
||||
{
|
||||
flag: "--event <id>",
|
||||
summary: "要清除的调用记录事件 ID,可重复,最多 100 条。",
|
||||
flag: "--versions",
|
||||
summary:
|
||||
"列出 L0 基本信息的版本链(get_user_basic_info),用于找出覆盖了正确值的错误版本。",
|
||||
},
|
||||
{
|
||||
flag: "--all",
|
||||
summary: "清除全部调用记录;与 --event 互斥。",
|
||||
},
|
||||
DIRECT_CONFIRM_OPTION,
|
||||
USER_ID_OPTION,
|
||||
JSON_OPTION,
|
||||
],
|
||||
exit_codes: [EXIT_OK, EXIT_FAILED, EXIT_USAGE, EXIT_RUNTIME_STOPPED],
|
||||
exit_codes: RUNTIME_EXITS,
|
||||
},
|
||||
{
|
||||
name: "notes",
|
||||
name: "graph-width",
|
||||
summary:
|
||||
"端内新增记忆(memory-notes):从普通文件新增,或显式确认后处理已有笔记。",
|
||||
"嵌入维度变化、图谱宽度不匹配时的处置决定。keep_existing 与 export_first 直接执行;rebuild_with_new 会归档旧图谱并重建,需 --yes。",
|
||||
usage:
|
||||
"memhub notes add --content-file <path> [--json]\n memhub notes apply --plan-file <path> --content-file <path> --confirm [--json]\n memhub notes process <note-id> [--confirm] [--json]",
|
||||
options: [
|
||||
{
|
||||
flag: "--content-file <path>",
|
||||
summary: "从有界普通文件读取新增正文;计划只保存摘要,执行时重新校验。",
|
||||
},
|
||||
PLAN_FILE_OPTION,
|
||||
DIRECT_CONFIRM_OPTION,
|
||||
JSON_OPTION,
|
||||
],
|
||||
exit_codes: [EXIT_OK, EXIT_FAILED, EXIT_USAGE, EXIT_RUNTIME_STOPPED],
|
||||
},
|
||||
{
|
||||
name: "graph",
|
||||
summary: "图谱宽度不匹配时的用户决定;默认只出计划,--confirm 后才执行。",
|
||||
usage:
|
||||
"memhub graph width --decision <keep_existing|rebuild_with_new|export_first> [--confirm] [--json]",
|
||||
"memhub graph-width --decision <keep_existing|export_first|rebuild_with_new> [--yes] [--json]",
|
||||
options: [
|
||||
{
|
||||
flag: "--decision <decision>",
|
||||
summary:
|
||||
"keep_existing(保持)、export_first(先导出)、rebuild_with_new(导出后重建)。",
|
||||
"keep_existing(保持现有图谱)、export_first(先导出)、rebuild_with_new(导出后归档旧图谱并按新维度重建)。",
|
||||
},
|
||||
{
|
||||
...DESTRUCTIVE_CONFIRM_OPTION,
|
||||
summary: "rebuild_with_new 必须加;其它决定不需要。",
|
||||
},
|
||||
DIRECT_CONFIRM_OPTION,
|
||||
JSON_OPTION,
|
||||
],
|
||||
exit_codes: [EXIT_OK, EXIT_FAILED, EXIT_USAGE, EXIT_RUNTIME_STOPPED],
|
||||
exit_codes: RUNTIME_EXITS,
|
||||
},
|
||||
{
|
||||
name: "host",
|
||||
@@ -480,14 +494,14 @@ export const COMMANDS: readonly CommandDescriptor[] = [
|
||||
},
|
||||
JSON_OPTION,
|
||||
],
|
||||
exit_codes: [EXIT_OK, EXIT_FAILED, EXIT_USAGE, EXIT_RUNTIME_STOPPED],
|
||||
exit_codes: RUNTIME_EXITS,
|
||||
},
|
||||
{
|
||||
name: "recall",
|
||||
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>",
|
||||
@@ -514,23 +528,24 @@ export const COMMANDS: readonly CommandDescriptor[] = [
|
||||
flag: "--timeout-ms <n>",
|
||||
summary: "Core 查询时限 50..30000 毫秒,默认 2000。",
|
||||
},
|
||||
USER_ID_OPTION,
|
||||
JSON_OPTION,
|
||||
],
|
||||
exit_codes: [EXIT_OK, EXIT_FAILED, EXIT_USAGE, EXIT_RUNTIME_STOPPED],
|
||||
exit_codes: RUNTIME_EXITS,
|
||||
},
|
||||
{
|
||||
name: "service-plan",
|
||||
summary: "生成用户级服务计划;只输出计划,不写文件也不执行命令。",
|
||||
usage: `memhub service-plan ${SERVICE_USAGE}`,
|
||||
options: SERVICE_OPTIONS,
|
||||
exit_codes: [EXIT_OK, EXIT_PLAN_BLOCKED],
|
||||
exit_codes: [EXIT_OK, EXIT_USAGE, EXIT_PLAN_BLOCKED],
|
||||
},
|
||||
{
|
||||
name: "service-install",
|
||||
summary: "预览用户级服务安装;只有 --yes 才写入服务文件并注册。",
|
||||
usage: `memhub service-install ${SERVICE_USAGE} [--yes]`,
|
||||
options: [...SERVICE_OPTIONS, CONFIRM_OPTION],
|
||||
exit_codes: [EXIT_OK, EXIT_FAILED, EXIT_PLAN_BLOCKED],
|
||||
exit_codes: [EXIT_OK, EXIT_FAILED, EXIT_USAGE, EXIT_PLAN_BLOCKED],
|
||||
},
|
||||
{
|
||||
name: "service-uninstall",
|
||||
@@ -538,15 +553,15 @@ export const COMMANDS: readonly CommandDescriptor[] = [
|
||||
"预览用户级服务卸载;只有 --yes 才注销并删除逐字匹配的受管服务文件,不删数据。",
|
||||
usage: `memhub service-uninstall ${SERVICE_USAGE} [--yes]`,
|
||||
options: [...SERVICE_OPTIONS, CONFIRM_OPTION],
|
||||
exit_codes: [EXIT_OK, EXIT_FAILED, EXIT_PLAN_BLOCKED],
|
||||
exit_codes: [EXIT_OK, EXIT_FAILED, EXIT_USAGE, EXIT_PLAN_BLOCKED],
|
||||
},
|
||||
{
|
||||
name: "install",
|
||||
summary:
|
||||
"预览发行目录安装、memhub/memhub-cli/one-memory 短链接、PATH 条目与用户服务计划;只有 --yes 才执行并在失败时回滚。",
|
||||
"预览发行目录安装、memhub/memhub-cli/one-memory 短链接、PATH 条目与用户服务计划;只有 --yes 才执行。记忆数据迁移开始前失败会回滚;迁移开始后失败保留新版本、拒绝启动,并输出迁移日志与反馈地址。",
|
||||
usage: `memhub install ${INSTALL_USAGE}`,
|
||||
options: INSTALL_OPTIONS,
|
||||
exit_codes: [EXIT_OK, EXIT_FAILED, EXIT_PLAN_BLOCKED],
|
||||
exit_codes: [EXIT_OK, EXIT_FAILED, EXIT_USAGE, EXIT_PLAN_BLOCKED],
|
||||
},
|
||||
{
|
||||
name: "uninstall",
|
||||
@@ -559,24 +574,31 @@ export const COMMANDS: readonly CommandDescriptor[] = [
|
||||
{
|
||||
name: "upgrade",
|
||||
summary:
|
||||
"预览升级;只有 --yes 才切换受管版本并重启服务,失败时恢复旧版本,不删数据。",
|
||||
"预览升级;只有 --yes 才切换受管版本并重启服务,不删数据。记忆数据迁移开始前失败会恢复旧版本;迁移开始后失败保留新版本、拒绝启动,并输出迁移日志与反馈地址(不会回滚到旧版本)。",
|
||||
usage: `memhub upgrade ${INSTALL_USAGE}`,
|
||||
options: INSTALL_OPTIONS,
|
||||
exit_codes: [EXIT_OK, EXIT_FAILED, EXIT_PLAN_BLOCKED],
|
||||
exit_codes: [EXIT_OK, EXIT_FAILED, EXIT_USAGE, EXIT_PLAN_BLOCKED],
|
||||
},
|
||||
{
|
||||
name: "delete-data",
|
||||
summary:
|
||||
"独立、二次确认地删除 One Memory Home(数据)。这是唯一会删除数据的命令;uninstall 不会。默认只出预览;需 Runtime 已停止,并显式 --confirm <path> 复述该目录。",
|
||||
"删除 MemHub 数据目录(记忆、来源、配置),不可逆。这是唯一会删除数据的命令;uninstall 不会。需要 Runtime 已停止;不加参数时只预览要删除的目录;删除需要 --yes 并用 --confirm 再写一遍同一个目录。",
|
||||
usage:
|
||||
"memhub delete-data [--data-dir <path>] [--yes --confirm <path>] [--json]",
|
||||
options: [
|
||||
DATA_DIR_OPTION,
|
||||
CONFIRM_OPTION,
|
||||
{
|
||||
flag: "--data-dir <path>",
|
||||
summary: "要删除的数据目录绝对路径;默认当前使用的数据目录。",
|
||||
},
|
||||
{
|
||||
...DESTRUCTIVE_CONFIRM_OPTION,
|
||||
summary:
|
||||
"删除时必须加,且要同时给出 --confirm <path>;不加时只预览,exit 0。",
|
||||
},
|
||||
{
|
||||
flag: "--confirm <path>",
|
||||
summary:
|
||||
"第二次确认:逐字复述预览里的数据目录;缺少它时即使 --yes 也不删除。",
|
||||
"删除时必须加:再写一遍要删除的数据目录,与预览中的目录完全一致才执行。",
|
||||
},
|
||||
JSON_OPTION,
|
||||
],
|
||||
@@ -588,113 +610,150 @@ export const COMMANDS: readonly CommandDescriptor[] = [
|
||||
"onboarding reset 回到首次运行的引导流程。只清除引导完成状态:不删记忆数据,也不需要卸载重装。",
|
||||
usage: "memhub onboarding reset [--json]",
|
||||
options: [JSON_OPTION],
|
||||
exit_codes: [EXIT_OK, EXIT_FAILED, EXIT_USAGE],
|
||||
exit_codes: RUNTIME_EXITS,
|
||||
},
|
||||
{
|
||||
name: "help",
|
||||
summary: "显示总帮助或单个命令的用法。",
|
||||
usage: "memhub help [command] [--json]",
|
||||
options: [JSON_OPTION],
|
||||
exit_codes: [EXIT_OK],
|
||||
exit_codes: [EXIT_OK, EXIT_USAGE],
|
||||
},
|
||||
{
|
||||
name: "version",
|
||||
summary: "显示 CLI 版本。",
|
||||
usage: "memhub version [--json]",
|
||||
options: [JSON_OPTION],
|
||||
exit_codes: [EXIT_OK],
|
||||
exit_codes: [EXIT_OK, EXIT_USAGE],
|
||||
},
|
||||
{
|
||||
name: "path",
|
||||
name: "scan-roots",
|
||||
summary:
|
||||
"查看或登记被扫描的目录(scan roots)。list 只读;add/remove 预览后加 --yes 才提交;均已接入 Runtime 的 scan-roots 端点。",
|
||||
"查看或登记被扫描的目录(scan roots)。add/remove 直接执行。(旧名 path 仍可用。)",
|
||||
usage:
|
||||
"memhub path list [--json]\n" +
|
||||
" memhub path add --root <path> [--source <id>] [--no-recursive] [--yes]\n" +
|
||||
" memhub path remove --root <path> [--yes]",
|
||||
options: [DATA_DIR_OPTION, JSON_OPTION, CONFIRM_OPTION],
|
||||
exit_codes: [EXIT_OK, EXIT_USAGE, EXIT_FAILED, EXIT_PLAN_BLOCKED],
|
||||
"memhub scan-roots list [--json]\n" +
|
||||
" memhub scan-roots add --root <path> [--source <id>] [--no-recursive] [--json]\n" +
|
||||
" memhub scan-roots remove --root <path> [--json]",
|
||||
options: [
|
||||
{ flag: "--root <path>", summary: "要登记或移除的目录。" },
|
||||
{
|
||||
flag: "--source <id>",
|
||||
summary: "把目录绑定到某个来源;不给时由 Runtime 自动识别。",
|
||||
},
|
||||
{ flag: "--no-recursive", summary: "只扫描该目录本身,不进入子目录。" },
|
||||
JSON_OPTION,
|
||||
],
|
||||
exit_codes: RUNTIME_EXITS,
|
||||
},
|
||||
{
|
||||
name: "adapter",
|
||||
summary:
|
||||
"查看 Harness Adapter 的就绪状态与最近一次探测结果(list),或现场探测某个来源(probe --source <id>);两者均为只读,已接入 Runtime 的 adapters/探测端点。",
|
||||
"查看 Harness Adapter 的就绪状态与最近一次探测结果(list),或现场探测某个来源(probe --source <id>);两者均为只读。",
|
||||
usage:
|
||||
"memhub adapter list [--json]\n" +
|
||||
" memhub adapter probe --source <id> [--json]",
|
||||
options: [DATA_DIR_OPTION, JSON_OPTION],
|
||||
exit_codes: [EXIT_OK, EXIT_USAGE, EXIT_FAILED],
|
||||
options: [
|
||||
{ flag: "--source <id>", summary: "probe 的目标来源。" },
|
||||
JSON_OPTION,
|
||||
],
|
||||
exit_codes: RUNTIME_EXITS,
|
||||
},
|
||||
{
|
||||
name: "scan",
|
||||
summary:
|
||||
"立即扫描;默认只输出计划,加 --yes 才真正执行。指定 --source 扫描单个来源,--all 一次扫描/导入全部已检测来源(Runtime 按来源寻址,不接受裸路径)。",
|
||||
usage:
|
||||
"memhub scan [--source <id>] [--full] [--yes] [--json]\n memhub scan --all [--yes] [--json]",
|
||||
"立即增量扫描一个已连接并开启同步的来源,返回导入任务(与 jobs start --operation sync 相同)。接入全部来源请用 sources connect --all-detected --import。",
|
||||
usage: "memhub scan --source <id> [--json]",
|
||||
options: [
|
||||
{
|
||||
flag: "--source <id>",
|
||||
summary: "按单个来源扫描;与 --all 互斥。",
|
||||
summary: "要扫描的来源。",
|
||||
},
|
||||
{
|
||||
flag: "--all",
|
||||
summary:
|
||||
"处理全部 location_state=found 的来源,按来源逐条回执;部分成功返回非 0。",
|
||||
},
|
||||
{
|
||||
flag: "--full",
|
||||
summary: "忽略既有游标做全量重扫;默认只读取增量。",
|
||||
},
|
||||
DATA_DIR_OPTION,
|
||||
JSON_OPTION,
|
||||
CONFIRM_OPTION,
|
||||
],
|
||||
exit_codes: [EXIT_OK, EXIT_USAGE, EXIT_FAILED, EXIT_PLAN_BLOCKED],
|
||||
exit_codes: RUNTIME_EXITS,
|
||||
},
|
||||
{
|
||||
name: "write",
|
||||
summary: "提交 OpenAI messages 到 SDK 写入队列,或按 request id 查询状态。",
|
||||
usage:
|
||||
"memhub write submit --messages-file <path> --session <id> [--source <id>] [--request-id <id>] [--memory-at <RFC3339>] [--live] [--json]\n memhub write status --id <request-id> [--json]",
|
||||
options: [DATA_DIR_OPTION, JSON_OPTION],
|
||||
exit_codes: [EXIT_OK, EXIT_USAGE, EXIT_FAILED],
|
||||
},
|
||||
{
|
||||
name: "task",
|
||||
summary:
|
||||
"兼容别名:按 request id 查询写入状态;新脚本请使用 write status。",
|
||||
usage: "memhub task --id <request-id> [--json]",
|
||||
options: [DATA_DIR_OPTION, JSON_OPTION],
|
||||
exit_codes: [EXIT_OK, EXIT_USAGE, EXIT_FAILED],
|
||||
"提交一段 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] [--user-id <id>] [--json]\n memhub write status <request-id> [--user-id <id>] [--json]",
|
||||
options: [
|
||||
{
|
||||
flag: "--messages-file <path>",
|
||||
summary:
|
||||
"OpenAI messages 数组(或 {messages:[…]}),1..200 条,role 为 user/assistant/tool,至少一条 user。",
|
||||
},
|
||||
{ flag: "--session <id>", summary: "会话 ID。" },
|
||||
{ flag: "--source <id>", summary: "来源应用,默认 memhub-cli。" },
|
||||
{
|
||||
flag: "--request-id <id>",
|
||||
summary:
|
||||
"幂等键:同 id 同内容不会重复入队,同 id 不同内容被拒绝(409)。",
|
||||
},
|
||||
{ flag: "--memory-at <RFC3339>", summary: "对话发生时间,必须带时区。" },
|
||||
{ flag: "--live", summary: "优先处理。" },
|
||||
USER_ID_OPTION,
|
||||
JSON_OPTION,
|
||||
],
|
||||
exit_codes: RUNTIME_EXITS,
|
||||
},
|
||||
{
|
||||
name: "backup",
|
||||
summary:
|
||||
"备份与恢复 MemHub 数据(两阶段)。create 调用 Runtime 的备份端点;restore 会覆盖现有数据,要求 Runtime 已停止且 `--confirm` 逐字重复 `--source`,随后由非 Runtime 维护态 Core 完成受管校验、保护性备份和原子替换。",
|
||||
"备份与恢复主库(来源、写入队列、配置)。注意:不包含记忆本身——SDK 记忆库 hy-memory/ 不在备份里,restore 也不会替换它。create 的目标要放在 <数据目录>/backups/ 下才能被 restore 使用;restore 覆盖现有主库,需 Runtime 已停止,并加 --yes 与 --confirm <同一个备份路径>。",
|
||||
usage:
|
||||
"memhub backup create --target <path> [--yes] [--json]\n" +
|
||||
" memhub backup restore --source <path> --yes --confirm <path> [--json]",
|
||||
"memhub backup create --target <path> [--json]\n" +
|
||||
" memhub backup restore --source <path> [--yes --confirm <path>] [--json]",
|
||||
options: [
|
||||
DATA_DIR_OPTION,
|
||||
JSON_OPTION,
|
||||
CONFIRM_OPTION,
|
||||
{
|
||||
flag: "--target <path>",
|
||||
summary: "create 的备份文件路径,必须在数据目录内。",
|
||||
},
|
||||
{
|
||||
flag: "--source <path>",
|
||||
summary:
|
||||
"restore 的备份文件,必须是 <数据目录>/backups/ 下的受管备份。",
|
||||
},
|
||||
{
|
||||
...DESTRUCTIVE_CONFIRM_OPTION,
|
||||
summary:
|
||||
"restore 必须加,且要同时给出 --confirm <path>;create 不需要。",
|
||||
},
|
||||
{
|
||||
flag: "--confirm <path>",
|
||||
summary: "restore 要求逐字重复 --source 路径;不一致时不做任何改动。",
|
||||
summary:
|
||||
"restore 必须加:再写一遍 --source 的备份路径,完全一致才执行。",
|
||||
},
|
||||
JSON_OPTION,
|
||||
],
|
||||
exit_codes: [EXIT_OK, EXIT_USAGE, EXIT_FAILED, EXIT_PLAN_BLOCKED],
|
||||
exit_codes: [...RUNTIME_EXITS, EXIT_PLAN_BLOCKED],
|
||||
},
|
||||
{
|
||||
name: "ui",
|
||||
summary:
|
||||
"用系统默认浏览器打开 Runtime 托管的 Web Console:先向 Core 换取一次性 loopback 登录码,再交给系统浏览器由 Core 置 HttpOnly/SameSite=Strict session;CLI 不向浏览器暴露裸 Core token。Core 未提供该合同时如实返回 ui_session_contract_unavailable(exit 4)。",
|
||||
"用系统默认浏览器打开 Runtime 托管的 Web Console:先向 Core 换取一次性 loopback 登录码,再交给系统浏览器由 Core 置 HttpOnly/SameSite=Strict session;CLI 不向浏览器暴露裸 Core token。Runtime 未运行时 exit 3;Core 未提供该合同时如实返回 ui_session_contract_unavailable(exit 4)。",
|
||||
usage: "memhub ui [--json]",
|
||||
options: [JSON_OPTION],
|
||||
exit_codes: [EXIT_OK, EXIT_FAILED, EXIT_USAGE, EXIT_PLAN_BLOCKED],
|
||||
exit_codes: [...RUNTIME_EXITS, EXIT_PLAN_BLOCKED],
|
||||
},
|
||||
];
|
||||
|
||||
/** Old names that still run but are not listed: `path` → `scan-roots`,
|
||||
* `task` → `write status`, `graph width` → `graph-width`. */
|
||||
export const HIDDEN_COMMAND_ALIASES: Readonly<Record<string, string>> = {
|
||||
path: "scan-roots",
|
||||
task: "write",
|
||||
graph: "graph-width",
|
||||
};
|
||||
|
||||
export function findCommand(name: string): CommandDescriptor | undefined {
|
||||
return COMMANDS.find((command) => command.name === name);
|
||||
}
|
||||
|
||||
/** A listed command, or the command a hidden alias now runs as. */
|
||||
export function findCommandOrAlias(
|
||||
name: string,
|
||||
): CommandDescriptor | undefined {
|
||||
return findCommand(HIDDEN_COMMAND_ALIASES[name] ?? name);
|
||||
}
|
||||
@@ -69,7 +69,6 @@ const COMMANDS = [
|
||||
"doctor",
|
||||
"logs",
|
||||
"init",
|
||||
"setup",
|
||||
"sources",
|
||||
"sync",
|
||||
"progress",
|
||||
@@ -77,10 +76,7 @@ const COMMANDS = [
|
||||
"models",
|
||||
"memories",
|
||||
"profile",
|
||||
// C6 App BLOCK (25f4ecf): UI write operations that had no CLI equivalent.
|
||||
"calls",
|
||||
"notes",
|
||||
"graph",
|
||||
"graph-width",
|
||||
"host",
|
||||
"recall",
|
||||
"service-plan",
|
||||
@@ -93,19 +89,20 @@ const COMMANDS = [
|
||||
"onboarding",
|
||||
"help",
|
||||
"version",
|
||||
// A6 product commands. Appended, not inserted: the catalog is a stable
|
||||
// contract, so existing names must not shift position.
|
||||
"path",
|
||||
"scan-roots",
|
||||
"adapter",
|
||||
"scan",
|
||||
"write",
|
||||
"task",
|
||||
"backup",
|
||||
// `ui` is declared but blocked on a Core session contract; it is listed so
|
||||
// `help` documents it and the blocked shape is machine-readable.
|
||||
"ui",
|
||||
] as const;
|
||||
|
||||
/** Old names that still run and still answer `--help`, but are not listed. */
|
||||
const HIDDEN_ALIASES = [
|
||||
["path", "scan-roots"],
|
||||
["task", "write"],
|
||||
] as const;
|
||||
|
||||
describe("CLI-22 shared output contract", () => {
|
||||
describe("help", () => {
|
||||
it.each([["help"], ["--help"], ["-h"]])(
|
||||
@@ -146,6 +143,56 @@ describe("CLI-22 shared output contract", () => {
|
||||
},
|
||||
);
|
||||
|
||||
it.each(HIDDEN_ALIASES)(
|
||||
"documents the hidden alias %s as %s and keeps it out of the listing",
|
||||
async (alias, target) => {
|
||||
const streams = captureStreams();
|
||||
try {
|
||||
expect(await run(["help", alias], {})).toBe(0);
|
||||
expect(await run([alias, "--help"], {})).toBe(0);
|
||||
} finally {
|
||||
streams.restore();
|
||||
}
|
||||
expect(streams.stdout()).toContain(`${PRODUCT} ${target}`);
|
||||
const listing = captureStreams();
|
||||
try {
|
||||
expect(await run(["help", "--json"], {})).toBe(0);
|
||||
} finally {
|
||||
listing.restore();
|
||||
}
|
||||
const names = JSON.parse(listing.stdout()).commands.map(
|
||||
(entry: { name: string }) => entry.name,
|
||||
);
|
||||
expect(names).not.toContain(alias);
|
||||
},
|
||||
);
|
||||
|
||||
it("lists no removed command", async () => {
|
||||
const streams = captureStreams();
|
||||
try {
|
||||
expect(await run(["help", "--json"], {})).toBe(0);
|
||||
} finally {
|
||||
streams.restore();
|
||||
}
|
||||
const catalog = streams.stdout();
|
||||
for (const removed of [
|
||||
'"name":"setup"',
|
||||
'"name":"notes"',
|
||||
'"name":"calls"',
|
||||
'"name":"graph"',
|
||||
"--plan-file",
|
||||
"probe-plan",
|
||||
"probe-receipt",
|
||||
"models configure",
|
||||
"sources import",
|
||||
"--full-rescan",
|
||||
"--dry-run",
|
||||
"--full]",
|
||||
]) {
|
||||
expect(catalog).not.toContain(removed);
|
||||
}
|
||||
});
|
||||
|
||||
it("never executes the documented command", async () => {
|
||||
for (const command of COMMANDS) {
|
||||
for (const argv of [
|
||||
@@ -201,27 +248,63 @@ describe("CLI-22 shared output contract", () => {
|
||||
expect(payload.commands).toBeUndefined();
|
||||
});
|
||||
|
||||
it("documents the destructive restore confirmation explicitly", async () => {
|
||||
it("documents the one --yes convention for destructive commands", async () => {
|
||||
const general = captureStreams();
|
||||
try {
|
||||
expect(await run(["help"], {})).toBe(0);
|
||||
} finally {
|
||||
general.restore();
|
||||
}
|
||||
expect(general.stdout()).toContain("确认约定");
|
||||
for (const [command, usage] of [
|
||||
[
|
||||
"backup",
|
||||
"memhub backup restore --source <path> [--yes --confirm <path>]",
|
||||
],
|
||||
["memories", "memhub memories delete <memory-id>"],
|
||||
["models", "memhub models remove <profile-id> [--yes]"],
|
||||
[
|
||||
"delete-data",
|
||||
"memhub delete-data [--data-dir <path>] [--yes --confirm <path>]",
|
||||
],
|
||||
["graph-width", "[--yes]"],
|
||||
] as const) {
|
||||
const streams = captureStreams();
|
||||
try {
|
||||
expect(await run(["help", command], {})).toBe(0);
|
||||
} finally {
|
||||
streams.restore();
|
||||
}
|
||||
expect(streams.stdout()).toContain(usage);
|
||||
// Only the two commands with no way back repeat the target path.
|
||||
if (command !== "backup" && command !== "delete-data") {
|
||||
expect(streams.stdout()).not.toContain("--confirm <path>");
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
it("says backup does not include the memories themselves", async () => {
|
||||
const streams = captureStreams();
|
||||
try {
|
||||
expect(await run(["help", "backup"], {})).toBe(0);
|
||||
} finally {
|
||||
streams.restore();
|
||||
}
|
||||
expect(streams.stdout()).toContain(
|
||||
"memhub backup restore --source <path> --yes --confirm <path>",
|
||||
);
|
||||
expect(streams.stdout()).toContain("逐字重复 --source");
|
||||
expect(streams.stdout()).toContain("不包含记忆本身");
|
||||
});
|
||||
|
||||
it("does not attach restore's confirmation flag to path", async () => {
|
||||
const streams = captureStreams();
|
||||
try {
|
||||
expect(await run(["help", "path"], {})).toBe(0);
|
||||
} finally {
|
||||
streams.restore();
|
||||
it("does not promise a rollback after the migration started", async () => {
|
||||
for (const command of ["install", "upgrade"]) {
|
||||
const streams = captureStreams();
|
||||
try {
|
||||
expect(await run(["help", command], {})).toBe(0);
|
||||
} finally {
|
||||
streams.restore();
|
||||
}
|
||||
expect(streams.stdout()).toContain("迁移开始后失败保留新版本");
|
||||
expect(streams.stdout()).not.toContain("失败时恢复旧版本");
|
||||
expect(streams.stdout()).not.toContain("失败时回滚");
|
||||
}
|
||||
expect(streams.stdout()).not.toContain("--confirm <path>");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -545,22 +628,23 @@ describe("documented commands", () => {
|
||||
models: [
|
||||
"list",
|
||||
"show",
|
||||
"plan",
|
||||
"configure",
|
||||
"add",
|
||||
"test",
|
||||
"receipt",
|
||||
"probe-plan",
|
||||
"probe",
|
||||
"probe-receipt",
|
||||
"edit",
|
||||
"activate",
|
||||
"remove",
|
||||
],
|
||||
// Source processing is one Freeze/Thaw switch under `sync`; the obsolete
|
||||
// independent read/write `sources access` command must not return here.
|
||||
sources: ["list", "import", "receipt"],
|
||||
sources: ["list", "connect", "receipt"],
|
||||
jobs: ["list", "show", "start", "<pause|resume|cancel>"],
|
||||
// `sync <pause|resume>` is documented as one alternation, so it is
|
||||
// checked as an entry rather than as two literal subcommands.
|
||||
sync: ["status", "<pause|resume>"],
|
||||
memories: ["list", "show", "update", "delete"],
|
||||
memories: ["list", "show", "add", "add-status", "update", "delete"],
|
||||
"scan-roots": ["list", "add", "remove"],
|
||||
write: ["submit", "status"],
|
||||
host: ["list", "status", "install", "uninstall", "sync"],
|
||||
};
|
||||
|
||||
|
||||
@@ -133,6 +133,20 @@ describe("unified context search", () => {
|
||||
expect(output).not.toMatch(/^- raw |memory-1|raw-1|采用状态/m);
|
||||
});
|
||||
|
||||
it("labels raw hits as original-conversation excerpts, not memories", async () => {
|
||||
const streams = captureStreams();
|
||||
try {
|
||||
expect(await run(["recall", "synthetic question"])).toBe(0);
|
||||
} finally {
|
||||
streams.restore();
|
||||
}
|
||||
const output = streams.stdout();
|
||||
expect(output).toContain(
|
||||
"- [原始对话摘录 · codex] Synthetic raw citation.",
|
||||
);
|
||||
expect(output).toContain("- [记忆] Synthetic SDK memory.");
|
||||
});
|
||||
|
||||
it("queries the SDK context endpoint directly and preserves item ids", async () => {
|
||||
const streams = captureStreams();
|
||||
try {
|
||||
|
||||
@@ -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. */
|
||||
@@ -23,6 +25,7 @@ export interface ContextSearchDocument {
|
||||
| "unavailable"
|
||||
| "denied"
|
||||
| "not_found"
|
||||
| "rejected"
|
||||
| "invalid";
|
||||
code?: string;
|
||||
request_id?: string;
|
||||
@@ -97,6 +100,9 @@ export function renderContextText(document: ContextSearchDocument): string {
|
||||
if (document.state === "unavailable") return "暂时无法查记忆,请稍后重试。\n";
|
||||
if (document.state === "denied") return "当前应用没有查记忆权限。\n";
|
||||
if (document.state === "invalid") return "查记忆返回了无法识别的结果。\n";
|
||||
if (document.state === "rejected") {
|
||||
return `查记忆被拒绝(${document.code}),请检查参数。\n`;
|
||||
}
|
||||
const lines = [
|
||||
document.state === "empty"
|
||||
? "没有查到相关记忆。"
|
||||
@@ -104,8 +110,14 @@ export function renderContextText(document: ContextSearchDocument): string {
|
||||
? "查到以下内容,部分内容还在整理中:"
|
||||
: "查到以下相关内容:",
|
||||
];
|
||||
// A raw hit is an excerpt of an original conversation, not a distilled
|
||||
// memory; say which one each line is so it is not read as a conclusion.
|
||||
for (const item of document.items) {
|
||||
lines.push(`- ${String(item.content)}`);
|
||||
const label =
|
||||
item.kind === "raw"
|
||||
? `[原始对话摘录 · ${String(item.source)}]`
|
||||
: "[记忆]";
|
||||
lines.push(`- ${label} ${String(item.content)}`);
|
||||
}
|
||||
if (document.truncated) lines.push("结果已按请求预算截断。");
|
||||
return `${lines.join("\n")}\n`;
|
||||
|
||||
@@ -12,12 +12,17 @@ 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",
|
||||
"/api/v2/user-profile",
|
||||
"/api/v2/user-profile/versions",
|
||||
] as const;
|
||||
const SDK_MEMORY_DETAIL = /^\/api\/v2\/memories\/[A-Za-z0-9][\w.-]{0,127}$/;
|
||||
/** Status of one `POST /api/v2/memories` addition, by its request id. */
|
||||
const MEMORY_ADDITION =
|
||||
/^\/api\/v2\/memories\/additions\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
||||
|
||||
/** Contract (C0, H25): the one-time loopback code exchange for `memhub ui`.
|
||||
* It is a POST because it mints a short-lived code; the CLI sends the bearer
|
||||
@@ -79,9 +84,15 @@ export const RESERVED_MEMORY_SEGMENTS = ["ask", "graph"] as const;
|
||||
/** A response larger than this is refused before it is buffered. */
|
||||
const MAX_RESPONSE_BYTES = 4 * 1024 * 1024;
|
||||
|
||||
/** `rejected` is Core refusing the request itself (400/409/422: a bad filter,
|
||||
* an ambiguous id). It is not "Core is busy" — only a transport failure or a
|
||||
* 5xx is `unavailable`. */
|
||||
export type ReadOutcome<T> =
|
||||
| { state: "ok"; data: T }
|
||||
| { state: "denied" | "unavailable" | "not_found"; code: string };
|
||||
| {
|
||||
state: "denied" | "unavailable" | "not_found" | "rejected";
|
||||
code: string;
|
||||
};
|
||||
|
||||
export type CoreReader = <T>(
|
||||
path: string,
|
||||
@@ -140,10 +151,27 @@ export function createCoreReader(attachment: {
|
||||
if (response.status === 404) {
|
||||
return { state: "not_found" as const, code: "http_404" };
|
||||
}
|
||||
if ([400, 409, 422].includes(response.status)) {
|
||||
return {
|
||||
state: "rejected" as const,
|
||||
code: detailCode(response.data, response.status),
|
||||
};
|
||||
}
|
||||
return { state: "unavailable" as const, code: `http_${response.status}` };
|
||||
};
|
||||
}
|
||||
|
||||
/** Core's stable `detail` code when it sent one, else the bare status. */
|
||||
function detailCode(value: unknown, status: number): string {
|
||||
const detail =
|
||||
value && typeof value === "object" && !Array.isArray(value)
|
||||
? (value as { detail?: unknown }).detail
|
||||
: undefined;
|
||||
return typeof detail === "string" && /^[a-z0-9_.:-]{1,128}$/.test(detail)
|
||||
? detail
|
||||
: `http_${status}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Context search is semantically read-only but intentionally uses POST so the
|
||||
* private query and filters are carried in a bounded body, never in a URL.
|
||||
@@ -192,6 +220,7 @@ export function isReadOnlyPath(path: string): boolean {
|
||||
if ((READ_ONLY_PATHS as readonly string[]).includes(path)) return true;
|
||||
if (IMPORT_JOB.test(path)) return true;
|
||||
if (OPERATION_RECEIPT.test(path)) return true;
|
||||
if (MEMORY_ADDITION.test(path)) return true;
|
||||
if (!SDK_MEMORY_DETAIL.test(path)) return false;
|
||||
const segment = path.slice("/api/v2/memories/".length);
|
||||
return !(RESERVED_MEMORY_SEGMENTS as readonly string[]).includes(segment);
|
||||
@@ -199,8 +228,10 @@ export function isReadOnlyPath(path: string): boolean {
|
||||
|
||||
/** A write accepted by the unified Memory Server entry.
|
||||
*
|
||||
* `request_id` is what the caller keeps and queries later. Retrying is unsafe:
|
||||
* HY has no idempotency key, so a resubmit duplicates the memory.
|
||||
* `request_id` is the idempotency key: Core keys the write on it, so a retry
|
||||
* with the same id and the same messages is answered from the first write, and
|
||||
* the same id with different messages is refused (409). A retry without the
|
||||
* original id is a new write.
|
||||
*/
|
||||
export interface MemoryWriteReceipt {
|
||||
request_id: string;
|
||||
@@ -267,6 +298,11 @@ function baseUrlOf(attachment: RuntimeAttachmentLike): string {
|
||||
return `http://${attachment.host}:${attachment.port}`;
|
||||
}
|
||||
|
||||
export type MemoryWriteOutcome =
|
||||
| { state: "accepted"; receipt: MemoryWriteReceipt }
|
||||
| { state: "rejected"; code: string; http_status: number }
|
||||
| { state: "unknown"; code: string };
|
||||
|
||||
export async function submitMemoryWrite(
|
||||
attachment: RuntimeAttachmentLike,
|
||||
request: {
|
||||
@@ -276,29 +312,54 @@ 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<MemoryWriteReceipt> {
|
||||
const response = await axios.post<MemoryWriteReceipt>(
|
||||
`${baseUrlOf(attachment)}/api/v2/memory-writes`,
|
||||
{
|
||||
request_id: request.request_id,
|
||||
session_id: request.session,
|
||||
source_id: request.source,
|
||||
messages: [...request.messages],
|
||||
...(request.memory_at === undefined
|
||||
? {}
|
||||
: { memory_at: request.memory_at }),
|
||||
...(request.priority === undefined ? {} : { priority: request.priority }),
|
||||
},
|
||||
{
|
||||
headers: { Authorization: `Bearer ${attachment.apiToken}` },
|
||||
timeout: 15_000,
|
||||
proxy: false,
|
||||
maxRedirects: 0,
|
||||
maxContentLength: MAX_RESPONSE_BYTES,
|
||||
},
|
||||
);
|
||||
return response.data;
|
||||
): Promise<MemoryWriteOutcome> {
|
||||
let response: { status: number; data: unknown };
|
||||
try {
|
||||
response = await axios.post<MemoryWriteReceipt>(
|
||||
`${baseUrlOf(attachment)}/api/v2/memory-writes`,
|
||||
{
|
||||
request_id: request.request_id,
|
||||
session_id: request.session,
|
||||
source_id: request.source,
|
||||
messages: [...request.messages],
|
||||
...(request.memory_at === undefined
|
||||
? {}
|
||||
: { memory_at: request.memory_at }),
|
||||
...(request.priority === undefined
|
||||
? {}
|
||||
: { priority: request.priority }),
|
||||
...(request.user_id === undefined || request.user_id === "local"
|
||||
? {}
|
||||
: { user_id: request.user_id }),
|
||||
},
|
||||
{
|
||||
headers: { Authorization: `Bearer ${attachment.apiToken}` },
|
||||
timeout: 15_000,
|
||||
proxy: false,
|
||||
maxRedirects: 0,
|
||||
maxContentLength: MAX_RESPONSE_BYTES,
|
||||
validateStatus: () => true,
|
||||
},
|
||||
);
|
||||
} catch {
|
||||
// The request may or may not have reached Core; the same request id makes
|
||||
// a retry safe.
|
||||
return { state: "unknown", code: "transport_result_unknown" };
|
||||
}
|
||||
if (response.status >= 200 && response.status < 300) {
|
||||
return { state: "accepted", receipt: response.data as MemoryWriteReceipt };
|
||||
}
|
||||
if (response.status >= 400 && response.status < 500) {
|
||||
return {
|
||||
state: "rejected",
|
||||
code: detailCode(response.data, response.status),
|
||||
http_status: response.status,
|
||||
};
|
||||
}
|
||||
return { state: "unknown", code: `http_${response.status}_result_unknown` };
|
||||
}
|
||||
|
||||
/** Ask Runtime to scan one source now, returning the import job it started.
|
||||
@@ -415,11 +476,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,
|
||||
@@ -495,48 +561,52 @@ export async function readScanRoots(
|
||||
}
|
||||
}
|
||||
|
||||
/** A direct Core write whose refusal carries Core's stable code. */
|
||||
export type CoreWriteResult =
|
||||
| { ok: true; data: unknown }
|
||||
| { ok: false; code: string };
|
||||
|
||||
async function coreWrite(
|
||||
attachment: RuntimeAttachmentLike,
|
||||
method: "post" | "delete",
|
||||
pathname: string,
|
||||
body: Record<string, unknown>,
|
||||
timeout = 10_000,
|
||||
): Promise<CoreWriteResult> {
|
||||
let response: { status: number; data: unknown };
|
||||
try {
|
||||
response = await axios.request({
|
||||
method,
|
||||
url: `${baseUrlOf(attachment)}${pathname}`,
|
||||
data: body,
|
||||
headers: { Authorization: `Bearer ${attachment.apiToken}` },
|
||||
timeout,
|
||||
proxy: false,
|
||||
maxRedirects: 0,
|
||||
maxContentLength: MAX_RESPONSE_BYTES,
|
||||
validateStatus: () => true,
|
||||
});
|
||||
} catch {
|
||||
return { ok: false, code: "transport_failed" };
|
||||
}
|
||||
if (response.status >= 200 && response.status < 300) {
|
||||
return { ok: true, data: response.data };
|
||||
}
|
||||
return { ok: false, code: detailCode(response.data, response.status) };
|
||||
}
|
||||
|
||||
export async function addScanRoot(
|
||||
attachment: RuntimeAttachmentLike,
|
||||
body: { root: string; source?: string; recursive?: boolean },
|
||||
): Promise<unknown | null> {
|
||||
try {
|
||||
const response = await axios.post<unknown>(
|
||||
`${baseUrlOf(attachment)}/api/v1/scan-roots`,
|
||||
body,
|
||||
{
|
||||
headers: { Authorization: `Bearer ${attachment.apiToken}` },
|
||||
timeout: 10_000,
|
||||
proxy: false,
|
||||
maxRedirects: 0,
|
||||
maxContentLength: MAX_RESPONSE_BYTES,
|
||||
},
|
||||
);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
return { _error: error instanceof Error ? error.message : String(error) };
|
||||
}
|
||||
): Promise<CoreWriteResult> {
|
||||
return coreWrite(attachment, "post", "/api/v1/scan-roots", body);
|
||||
}
|
||||
|
||||
export async function removeScanRoot(
|
||||
attachment: RuntimeAttachmentLike,
|
||||
body: { root: string },
|
||||
): Promise<unknown | null> {
|
||||
try {
|
||||
const response = await axios.delete<unknown>(
|
||||
`${baseUrlOf(attachment)}/api/v1/scan-roots`,
|
||||
{
|
||||
headers: { Authorization: `Bearer ${attachment.apiToken}` },
|
||||
data: body,
|
||||
timeout: 10_000,
|
||||
proxy: false,
|
||||
maxRedirects: 0,
|
||||
maxContentLength: MAX_RESPONSE_BYTES,
|
||||
},
|
||||
);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
return { _error: error instanceof Error ? error.message : String(error) };
|
||||
}
|
||||
): Promise<CoreWriteResult> {
|
||||
return coreWrite(attachment, "delete", "/api/v1/scan-roots", body);
|
||||
}
|
||||
|
||||
export async function readAdapters(
|
||||
@@ -562,21 +632,12 @@ export async function readAdapters(
|
||||
export async function probeAdapter(
|
||||
attachment: RuntimeAttachmentLike,
|
||||
source: string,
|
||||
): Promise<unknown | null> {
|
||||
try {
|
||||
const response = await axios.post<unknown>(
|
||||
`${baseUrlOf(attachment)}/api/v1/adapters/probe`,
|
||||
{ source },
|
||||
{
|
||||
headers: { Authorization: `Bearer ${attachment.apiToken}` },
|
||||
timeout: 15_000,
|
||||
proxy: false,
|
||||
maxRedirects: 0,
|
||||
maxContentLength: MAX_RESPONSE_BYTES,
|
||||
},
|
||||
);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
return { _error: error instanceof Error ? error.message : String(error) };
|
||||
}
|
||||
): Promise<CoreWriteResult> {
|
||||
return coreWrite(
|
||||
attachment,
|
||||
"post",
|
||||
"/api/v1/adapters/probe",
|
||||
{ source },
|
||||
15_000,
|
||||
);
|
||||
}
|
||||
@@ -4,9 +4,12 @@ const WRITE_PATHS = [
|
||||
"/api/v1/sources/connect",
|
||||
"/api/v1/model-profiles",
|
||||
] as const;
|
||||
const MEMORY_WRITE_PATH =
|
||||
/^\/api\/v1\/memories(?:\/[A-Za-z0-9][\w.-]{0,127}\/(?:correct|scopes|actions\/delete))?$/;
|
||||
const SDK_MEMORY_PATH = /^\/api\/v2\/memories\/[A-Za-z0-9][\w.-]{0,127}$/;
|
||||
/** `POST /api/v2/memories` adds one memory (the app's 添加记忆). */
|
||||
const MEMORY_ADD_PATH = "/api/v2/memories";
|
||||
/** 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))?(?:&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 =
|
||||
@@ -22,18 +25,7 @@ const SOURCE_REVOKE_PATH =
|
||||
const SOURCE_IMPORT_JOB_PATH = "/api/v1/import-jobs";
|
||||
const IMPORT_JOB_ACTION_PATH =
|
||||
/^\/api\/v1\/import-jobs\/[A-Za-z0-9][\w.-]{0,127}\/(?:pause|resume|cancel)$/;
|
||||
const CALL_EVENTS_CLEAR_PATH = "/api/v1/call-events/clear";
|
||||
const CALL_EVENTS_CLEAR_ALL_PATH = "/api/v1/call-events/clear-all";
|
||||
const IMPORT_FILE_COMMIT =
|
||||
/^\/api\/v1\/import-files\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\/commit$/;
|
||||
const IMPORT_FILE =
|
||||
/^\/api\/v1\/import-files\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
||||
const MEMORY_NOTES_PATH = "/api/v1/memory-notes";
|
||||
const MEMORY_NOTE_PROCESS =
|
||||
/^\/api\/v1\/memory-notes\/[A-Za-z0-9][\w.:-]{0,127}\/process$/;
|
||||
const EXTRACTION_SETTINGS_PATH = "/api/v1/extraction-settings";
|
||||
const GRAPH_WIDTH_PATH = "/api/v1/graph-width";
|
||||
const MANUAL_EXTRACT_PATH = "/api/v1/memory-processing/manual-extract";
|
||||
const ONBOARDING_COMPLETE_PATH = "/api/v1/onboarding/complete";
|
||||
const MAX_BYTES = 4 * 1024 * 1024;
|
||||
|
||||
@@ -70,18 +62,6 @@ export interface CorePatchOperations {
|
||||
): Promise<OperationOutcome<T>>;
|
||||
}
|
||||
|
||||
export interface CoreUploadOperations {
|
||||
uploadImportFile<T>(
|
||||
input: {
|
||||
bytes: Uint8Array;
|
||||
filename: string;
|
||||
inputKind: "raw_conversation" | "memory_content";
|
||||
operationId: string;
|
||||
},
|
||||
signal?: AbortSignal,
|
||||
): Promise<OperationOutcome<T>>;
|
||||
}
|
||||
|
||||
export function createCoreOperations(attachment: {
|
||||
host: string;
|
||||
port: number;
|
||||
@@ -90,8 +70,7 @@ export function createCoreOperations(attachment: {
|
||||
}): CoreOperations &
|
||||
CoreDeleteOperations &
|
||||
CorePutOperations &
|
||||
CorePatchOperations &
|
||||
CoreUploadOperations {
|
||||
CorePatchOperations {
|
||||
const client = axios.create({
|
||||
baseURL: `http://${attachment.host}:${attachment.port}`,
|
||||
headers: { Authorization: `Bearer ${attachment.apiToken}` },
|
||||
@@ -102,18 +81,6 @@ export function createCoreOperations(attachment: {
|
||||
maxBodyLength: 128 * 1024,
|
||||
validateStatus: () => true,
|
||||
});
|
||||
// Uploads carry up to 128 MiB of raw octet-stream, so they need a separate
|
||||
// body limit and a deadline that leaves room for parsing on the Core side.
|
||||
const uploadClient = axios.create({
|
||||
baseURL: `http://${attachment.host}:${attachment.port}`,
|
||||
headers: { Authorization: `Bearer ${attachment.apiToken}` },
|
||||
timeout: 120_000,
|
||||
proxy: false,
|
||||
maxRedirects: 0,
|
||||
maxContentLength: MAX_BYTES,
|
||||
maxBodyLength: 128 * 1024 * 1024,
|
||||
validateStatus: () => true,
|
||||
});
|
||||
return {
|
||||
async post<T>(
|
||||
path: string,
|
||||
@@ -123,19 +90,13 @@ export function createCoreOperations(attachment: {
|
||||
if (
|
||||
!(WRITE_PATHS as readonly string[]).includes(path) &&
|
||||
path !== SOURCE_IMPORT_JOB_PATH &&
|
||||
!MEMORY_WRITE_PATH.test(path) &&
|
||||
path !== MEMORY_ADD_PATH &&
|
||||
!MODEL_PROBE_PATH.test(path) &&
|
||||
!SOURCE_SYNC_PATH.test(path) &&
|
||||
!IMPORT_JOB_ACTION_PATH.test(path) &&
|
||||
!SOURCE_AUTHORIZE_PATH.test(path) &&
|
||||
!SOURCE_REVOKE_PATH.test(path) &&
|
||||
path !== CALL_EVENTS_CLEAR_PATH &&
|
||||
path !== CALL_EVENTS_CLEAR_ALL_PATH &&
|
||||
!IMPORT_FILE_COMMIT.test(path) &&
|
||||
path !== MEMORY_NOTES_PATH &&
|
||||
!MEMORY_NOTE_PROCESS.test(path) &&
|
||||
path !== GRAPH_WIDTH_PATH &&
|
||||
path !== MANUAL_EXTRACT_PATH &&
|
||||
path !== ONBOARDING_COMPLETE_PATH &&
|
||||
!MODEL_PROFILE_ACTIVATE.test(path)
|
||||
) {
|
||||
@@ -171,7 +132,7 @@ export function createCoreOperations(attachment: {
|
||||
body: Record<string, unknown>,
|
||||
signal?: AbortSignal,
|
||||
) {
|
||||
if (path !== EXTRACTION_SETTINGS_PATH && !SDK_MEMORY_PATH.test(path)) {
|
||||
if (!SDK_MEMORY_PATH.test(path) || path.includes("?")) {
|
||||
throw new Error("core_operation_path_not_allowed");
|
||||
}
|
||||
let response: { status: number; data: unknown };
|
||||
@@ -233,7 +194,7 @@ export function createCoreOperations(attachment: {
|
||||
};
|
||||
},
|
||||
async delete<T>(path: string, signal?: AbortSignal) {
|
||||
if (!IMPORT_FILE.test(path) && !SDK_MEMORY_PATH.test(path)) {
|
||||
if (!SDK_MEMORY_PATH.test(path) && !MODEL_PROFILE_PATH.test(path)) {
|
||||
throw new Error("core_operation_path_not_allowed");
|
||||
}
|
||||
let response: { status: number; data: unknown };
|
||||
@@ -257,51 +218,6 @@ export function createCoreOperations(attachment: {
|
||||
code: `http_${response.status}_result_unknown`,
|
||||
};
|
||||
},
|
||||
async uploadImportFile<T>(
|
||||
input: {
|
||||
bytes: Uint8Array;
|
||||
filename: string;
|
||||
inputKind: "raw_conversation" | "memory_content";
|
||||
operationId: string;
|
||||
},
|
||||
signal?: AbortSignal,
|
||||
) {
|
||||
const query = new URLSearchParams({
|
||||
filename: input.filename,
|
||||
input_kind: input.inputKind,
|
||||
operation_id: input.operationId,
|
||||
});
|
||||
let response: { status: number; data: unknown };
|
||||
try {
|
||||
response = await uploadClient.post(
|
||||
`/api/v1/import-files?${query.toString()}`,
|
||||
input.bytes,
|
||||
{
|
||||
headers: {
|
||||
"Content-Type": "application/octet-stream",
|
||||
"Content-Length": String(input.bytes.byteLength),
|
||||
},
|
||||
...(signal ? { signal } : {}),
|
||||
},
|
||||
);
|
||||
} catch {
|
||||
return { state: "unknown", code: "transport_result_unknown" };
|
||||
}
|
||||
if (response.status >= 200 && response.status < 300) {
|
||||
return { state: "committed", data: response.data as T };
|
||||
}
|
||||
if (response.status >= 400 && response.status < 500) {
|
||||
return {
|
||||
state: "rejected",
|
||||
code: safeErrorCode(response.data, response.status),
|
||||
http_status: response.status,
|
||||
};
|
||||
}
|
||||
return {
|
||||
state: "unknown",
|
||||
code: `http_${response.status}_result_unknown`,
|
||||
};
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -23,39 +23,26 @@ function accountHomeDirectory(): string | undefined {
|
||||
async function main(): Promise<number> {
|
||||
const arguments_ = process.argv.slice(2);
|
||||
const command = arguments_[0];
|
||||
const action = arguments_[1];
|
||||
// A preview (`--yes` absent) must not create or write any diagnostic file:
|
||||
// the contract is that it only emits a plan. Diagnostics for these commands
|
||||
// are initialized only once the caller has confirmed the mutation.
|
||||
const confirmed = arguments_.includes("--yes")
|
||||
? true
|
||||
: arguments_.includes("--confirm");
|
||||
const confirmed = arguments_.includes("--yes");
|
||||
const installLike = [
|
||||
"install",
|
||||
"upgrade",
|
||||
"service-install",
|
||||
"service-uninstall",
|
||||
].includes(command ?? "");
|
||||
const planFileApply =
|
||||
(command === "sources" && action === "import") ||
|
||||
(command === "models" && (action === "configure" || action === "probe")) ||
|
||||
(command === "processing" && action === "apply") ||
|
||||
(command === "memories" && action === "apply");
|
||||
const isPreview = (installLike || planFileApply) && !confirmed;
|
||||
const mutating =
|
||||
[
|
||||
"start",
|
||||
"stop",
|
||||
"restart",
|
||||
"install",
|
||||
"upgrade",
|
||||
"service-install",
|
||||
"service-uninstall",
|
||||
].includes(command ?? "") ||
|
||||
(command === "sources" && action === "import") ||
|
||||
(command === "models" && ["configure", "probe"].includes(action ?? "")) ||
|
||||
(command === "processing" && action === "apply") ||
|
||||
(command === "memories" && action === "apply");
|
||||
const isPreview = installLike && !confirmed;
|
||||
const mutating = [
|
||||
"start",
|
||||
"stop",
|
||||
"restart",
|
||||
"install",
|
||||
"upgrade",
|
||||
"service-install",
|
||||
"service-uninstall",
|
||||
].includes(command ?? "");
|
||||
let diagnostics: DiagnosticLogger | null = null;
|
||||
if (mutating && !isPreview) {
|
||||
try {
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
/**
|
||||
* `memhub graph-width`: the user's decision when the embedding width changed
|
||||
* and the memory graph no longer matches it (`POST /api/v1/graph-width`).
|
||||
*
|
||||
* `keep_existing` and `export_first` change nothing irreversible and run
|
||||
* directly. `rebuild_with_new` archives the current graph and rebuilds it, so
|
||||
* the CLI asks for `--yes` before sending it.
|
||||
*/
|
||||
|
||||
import { parseGraphWidthDecisionReceipt } from "@one-memory/operations";
|
||||
import type { CoreOperations } from "./core-operations.js";
|
||||
import { PRODUCT } from "./version.js";
|
||||
|
||||
export const GRAPH_WIDTH_DECISIONS = [
|
||||
"keep_existing",
|
||||
"rebuild_with_new",
|
||||
"export_first",
|
||||
] as const;
|
||||
export type GraphWidthDecision = (typeof GRAPH_WIDTH_DECISIONS)[number];
|
||||
|
||||
/** The one decision that cannot be taken back. */
|
||||
export function graphWidthDecisionIsDestructive(
|
||||
decision: GraphWidthDecision,
|
||||
): boolean {
|
||||
return decision === "rebuild_with_new";
|
||||
}
|
||||
|
||||
export type GraphWidthDocument =
|
||||
| {
|
||||
schema_version: 1;
|
||||
product: string;
|
||||
command: "graph-width";
|
||||
state: "committed";
|
||||
decision: GraphWidthDecision;
|
||||
receipt: Record<string, unknown>;
|
||||
}
|
||||
| {
|
||||
schema_version: 1;
|
||||
product: string;
|
||||
command: "graph-width";
|
||||
state: "rejected" | "unknown";
|
||||
decision: GraphWidthDecision;
|
||||
code: string;
|
||||
};
|
||||
|
||||
export async function decideGraphWidth(
|
||||
decision: GraphWidthDecision,
|
||||
operations: CoreOperations,
|
||||
): Promise<GraphWidthDocument> {
|
||||
const base = {
|
||||
schema_version: 1 as const,
|
||||
product: PRODUCT,
|
||||
command: "graph-width" as const,
|
||||
decision,
|
||||
};
|
||||
const result = await operations.post<unknown>("/api/v1/graph-width", {
|
||||
decision,
|
||||
});
|
||||
if (result.state !== "committed") {
|
||||
return { ...base, state: result.state, code: result.code };
|
||||
}
|
||||
const receipt = parseGraphWidthDecisionReceipt(result.data);
|
||||
if (receipt === null) {
|
||||
return { ...base, state: "unknown", code: "graph_width_receipt_invalid" };
|
||||
}
|
||||
return {
|
||||
...base,
|
||||
state: "committed",
|
||||
receipt: {
|
||||
archived_path: receipt.archived_path ?? null,
|
||||
exported_path: receipt.exported_path ?? null,
|
||||
mismatch: receipt.mismatch ?? null,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function graphWidthExitCode(document: GraphWidthDocument): number {
|
||||
return document.state === "committed" ? 0 : 1;
|
||||
}
|
||||
|
||||
export function renderGraphWidthText(document: GraphWidthDocument): string {
|
||||
if (document.state === "committed") {
|
||||
const lines = [`已执行图谱宽度决定:${document.decision}`];
|
||||
if (document.receipt.exported_path) {
|
||||
lines.push(`导出:${document.receipt.exported_path}`);
|
||||
}
|
||||
if (document.receipt.archived_path) {
|
||||
lines.push(`旧图谱已归档:${document.receipt.archived_path}`);
|
||||
}
|
||||
return `${lines.join("\n")}\n`;
|
||||
}
|
||||
return `图谱宽度决定未完成:${document.state}(${document.code})\n`;
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -1,4 +1,4 @@
|
||||
import { createHash, randomUUID } from "node:crypto";
|
||||
import { randomUUID } from "node:crypto";
|
||||
import type { ImportJob, ModelProfile } from "@one-memory/contracts";
|
||||
import {
|
||||
buildModelProfileCreateRequest,
|
||||
@@ -26,45 +26,7 @@ const PROVIDERS = [
|
||||
"local_onnx",
|
||||
] as const;
|
||||
|
||||
interface SourceImportPlanBody {
|
||||
schema_version: 1;
|
||||
kind: "source_import";
|
||||
request_id: string;
|
||||
source_ids: string[];
|
||||
source_roots: Record<string, string>;
|
||||
source_candidate_revisions: Record<string, number>;
|
||||
confirm_environment_overrides: boolean;
|
||||
full_rescan: false;
|
||||
onboarding: true;
|
||||
}
|
||||
|
||||
export type SourceImportPlan = SourceImportPlanBody & { digest: string };
|
||||
|
||||
interface ModelCreatePlanBody {
|
||||
schema_version: 1;
|
||||
kind: "model_profile_create";
|
||||
operation_id: string;
|
||||
config: Record<string, unknown>;
|
||||
}
|
||||
|
||||
export type ModelCreatePlan = ModelCreatePlanBody & { digest: string };
|
||||
|
||||
/** A removal only needs an id: there is no config to review, and inventing one
|
||||
* to fit the create shape would misrepresent what the command does.
|
||||
*/
|
||||
export interface ModelRemovePlan {
|
||||
schema_version: 1;
|
||||
kind: "model_profile_remove";
|
||||
target: string;
|
||||
}
|
||||
|
||||
export type FlowDocument =
|
||||
| {
|
||||
schema_version: 1;
|
||||
product: string;
|
||||
state: "preview";
|
||||
plan: SourceImportPlan | ModelCreatePlan | ModelRemovePlan;
|
||||
}
|
||||
| {
|
||||
schema_version: 1;
|
||||
product: string;
|
||||
@@ -133,10 +95,33 @@ function classifySourceImport(
|
||||
return committed(operationId, receipt);
|
||||
}
|
||||
|
||||
export async function planSourceImport(
|
||||
/**
|
||||
* Connect sources and import their history in one direct request.
|
||||
*
|
||||
* Reads the current candidates, refuses a source whose location comes from an
|
||||
* environment override unless the caller confirmed overrides, and posts one
|
||||
* `/sources/connect` (grant + history import + live sync). `requestId` is the
|
||||
* idempotency key: re-running with the same id reads the first receipt.
|
||||
*/
|
||||
export async function connectSourcesWithImport(
|
||||
reader: CoreReader,
|
||||
sourceIds: readonly string[],
|
||||
operations: CoreOperations,
|
||||
input: {
|
||||
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 ||
|
||||
@@ -145,6 +130,8 @@ export async function planSourceImport(
|
||||
) {
|
||||
return invalid("invalid_source_selection");
|
||||
}
|
||||
const requestId = input.requestId ?? randomUUID();
|
||||
if (!UUID.test(requestId)) return invalid("invalid_request_id");
|
||||
const outcome = await reader<unknown>("/api/v1/sources");
|
||||
if (outcome.state !== "ok") return fromReadFailure(outcome);
|
||||
const candidates = validateSourceConnectionCandidates(outcome.data);
|
||||
@@ -156,69 +143,72 @@ export async function planSourceImport(
|
||||
return invalid("source_unavailable");
|
||||
}
|
||||
const items = selected.map(([candidate]) => candidate);
|
||||
const body: SourceImportPlanBody = {
|
||||
schema_version: 1,
|
||||
kind: "source_import",
|
||||
request_id: randomUUID(),
|
||||
source_ids: [...sourceIds],
|
||||
source_roots: Object.fromEntries(
|
||||
items.map((item) => [item.source_id, item.candidate_root_id]),
|
||||
),
|
||||
source_candidate_revisions: Object.fromEntries(
|
||||
items.flatMap((item) =>
|
||||
item.candidate_revision === undefined
|
||||
? []
|
||||
: [[item.source_id, item.candidate_revision]],
|
||||
),
|
||||
),
|
||||
confirm_environment_overrides: items.some(
|
||||
(item) => item.requires_override_confirmation,
|
||||
),
|
||||
full_rescan: false,
|
||||
onboarding: true,
|
||||
};
|
||||
return preview(withDigest(body));
|
||||
}
|
||||
|
||||
export async function executeSourceImport(
|
||||
value: unknown,
|
||||
reader: CoreReader,
|
||||
operations: CoreOperations,
|
||||
): Promise<FlowDocument> {
|
||||
const plan = validateSourcePlan(value);
|
||||
if (!plan) return invalid("source_plan_invalid");
|
||||
// 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,
|
||||
product: PRODUCT,
|
||||
state: "rejected",
|
||||
operation_id: requestId,
|
||||
code: "environment_override_confirmation_required",
|
||||
receipt: { override_required_source_ids: needsOverride },
|
||||
};
|
||||
}
|
||||
// The shared builder owns the connect body's field set and coalescing, so
|
||||
// the CLI and the Web console send the same request by construction.
|
||||
let request: ReturnType<typeof buildSourceImportRequest>;
|
||||
try {
|
||||
request = buildSourceImportRequest({
|
||||
sourceIds: plan.source_ids,
|
||||
sourceRoots: plan.source_roots,
|
||||
sourceCandidateRevisions: plan.source_candidate_revisions,
|
||||
confirmEnvironmentOverrides: plan.confirm_environment_overrides,
|
||||
fullRescan: plan.full_rescan,
|
||||
onboarding: plan.onboarding,
|
||||
sourceIds: [...sourceIds],
|
||||
sourceRoots: Object.fromEntries(
|
||||
items.flatMap((item) =>
|
||||
input.root === undefined
|
||||
? [[item.source_id, item.candidate_root_id]]
|
||||
: [],
|
||||
),
|
||||
),
|
||||
sourceCandidateRevisions: Object.fromEntries(
|
||||
items.flatMap((item) =>
|
||||
item.candidate_revision === undefined || input.root !== undefined
|
||||
? []
|
||||
: [[item.source_id, item.candidate_revision]],
|
||||
),
|
||||
),
|
||||
confirmEnvironmentOverrides: needsOverride.length > 0,
|
||||
fullRescan: false,
|
||||
onboarding: !profile,
|
||||
enableSync: true,
|
||||
requestId: plan.request_id,
|
||||
requestId,
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof SourceImportInputError) {
|
||||
return invalid("source_plan_invalid");
|
||||
return invalid("invalid_source_selection");
|
||||
}
|
||||
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(plan.request_id, result.code);
|
||||
return rejected(requestId, result.code);
|
||||
}
|
||||
if (result.state === "committed") {
|
||||
const receipt = validateSourceReceipt(result.data, plan.request_id);
|
||||
if (!receipt) return unknown(plan.request_id, "source_receipt_invalid");
|
||||
return classifySourceImport(plan.request_id, receipt);
|
||||
const receipt = validateSourceReceipt(result.data, requestId);
|
||||
if (!receipt) return unknown(requestId, "source_receipt_invalid");
|
||||
return classifySourceImport(requestId, receipt);
|
||||
}
|
||||
const recovered = await readSourceReceipt(reader, plan.request_id);
|
||||
const recovered = await readSourceReceipt(reader, requestId);
|
||||
if (recovered.state === "unknown" || recovered.state === "not_found") {
|
||||
return unknown(plan.request_id, result.code);
|
||||
return unknown(requestId, result.code);
|
||||
}
|
||||
// A recovered receipt must be classified the same way as a fresh one: an
|
||||
// all-rejected or all-busy replay is still not success. Only a terminal
|
||||
@@ -262,26 +252,23 @@ export async function readSourceReceipt(
|
||||
return committed(requestId, receipt);
|
||||
}
|
||||
|
||||
export function planModelProfile(value: unknown): FlowDocument {
|
||||
const config = validateModelConfig(value);
|
||||
if (!config) return invalid("model_config_invalid");
|
||||
const body: ModelCreatePlanBody = {
|
||||
schema_version: 1,
|
||||
kind: "model_profile_create",
|
||||
operation_id: randomUUID(),
|
||||
config,
|
||||
};
|
||||
return preview(withDigest(body));
|
||||
}
|
||||
|
||||
export async function executeModelProfile(
|
||||
/**
|
||||
* Create a model profile directly from a bounded config (never containing the
|
||||
* key) plus an optional key read from a controlled file.
|
||||
*
|
||||
* `operationId` is Core's creation idempotency key; the receipt can be read
|
||||
* later with `readModelReceipt`.
|
||||
*/
|
||||
export async function createModelProfile(
|
||||
value: unknown,
|
||||
apiKey: string | undefined,
|
||||
reader: CoreReader,
|
||||
operations: CoreOperations,
|
||||
operationId: string = randomUUID(),
|
||||
): Promise<FlowDocument> {
|
||||
const plan = validateModelPlan(value);
|
||||
if (!plan) return invalid("model_plan_invalid");
|
||||
const config = validateModelConfig(value);
|
||||
if (!config) return invalid("model_config_invalid");
|
||||
if (!UUID.test(operationId)) return invalid("invalid_operation_id");
|
||||
// The shared create builder owns the body's allow-list, bounds and the
|
||||
// creation_operation_id/api_key handling, so the CLI and the Web console
|
||||
// send the same create request.
|
||||
@@ -289,31 +276,31 @@ export async function executeModelProfile(
|
||||
try {
|
||||
request = buildModelProfileCreateRequest(
|
||||
{
|
||||
...(plan.config as Record<string, unknown>),
|
||||
...config,
|
||||
...(apiKey === undefined ? {} : { api_key: apiKey }),
|
||||
} as Parameters<typeof buildModelProfileCreateRequest>[0],
|
||||
plan.operation_id,
|
||||
operationId,
|
||||
);
|
||||
} catch (error) {
|
||||
if (error instanceof ModelProfileInputError) {
|
||||
return invalid("model_plan_invalid");
|
||||
return invalid("model_config_invalid");
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
const result = await operations.post<unknown>(request.path, request.body);
|
||||
if (result.state === "rejected") {
|
||||
return rejected(plan.operation_id, result.code);
|
||||
return rejected(operationId, result.code);
|
||||
}
|
||||
if (result.state === "committed") {
|
||||
const profile = validateModelProfile(result.data);
|
||||
return profile
|
||||
? committed(plan.operation_id, projectProfile(profile))
|
||||
: unknown(plan.operation_id, "model_receipt_invalid");
|
||||
? committed(operationId, projectProfile(profile))
|
||||
: unknown(operationId, "model_receipt_invalid");
|
||||
}
|
||||
const recovered = await readModelReceipt(reader, plan.operation_id);
|
||||
const recovered = await readModelReceipt(reader, operationId);
|
||||
return recovered.state === "committed"
|
||||
? { ...recovered, state: "recovered" }
|
||||
: unknown(plan.operation_id, result.code);
|
||||
: unknown(operationId, result.code);
|
||||
}
|
||||
|
||||
export async function readModelReceipt(
|
||||
@@ -362,27 +349,6 @@ export function parseApiKey(bytes: Buffer | null): string | undefined {
|
||||
}
|
||||
|
||||
export function renderFlowText(document: FlowDocument): string {
|
||||
if (document.state === "preview") {
|
||||
const plan = document.plan;
|
||||
// A removal has no config digest or operation id to confirm: the target id
|
||||
// *is* the thing being reviewed. Rendering fields it does not have would
|
||||
// print "undefined".
|
||||
if (plan.kind === "model_profile_remove") {
|
||||
return [
|
||||
`预览:${plan.kind}`,
|
||||
`目标配置:${plan.target}`,
|
||||
"未执行任何写操作。加 --confirm 才会真正删除。",
|
||||
"",
|
||||
].join("\n");
|
||||
}
|
||||
return [
|
||||
`预览:${plan.kind}`,
|
||||
`操作 ID:${"request_id" in plan ? plan.request_id : plan.operation_id}`,
|
||||
`确认摘要:${plan.digest}`,
|
||||
"未执行任何写操作。请把 --json 输出保存为 plan 文件,再用对应命令加 --confirm。",
|
||||
"",
|
||||
].join("\n");
|
||||
}
|
||||
const lines = [
|
||||
`操作状态:${document.state}`,
|
||||
`操作 ID:${document.operation_id ?? "无"}`,
|
||||
@@ -407,98 +373,20 @@ export function renderFlowText(document: FlowDocument): string {
|
||||
if (document.state === "partial" && Array.isArray(accepted)) {
|
||||
lines.push(` 已接入:${accepted.join("、")}`);
|
||||
}
|
||||
const overrides = document.receipt.override_required_source_ids;
|
||||
if (Array.isArray(overrides)) {
|
||||
lines.push(
|
||||
` 这些来源的目录来自环境变量覆盖,需要确认:${overrides.join("、")}`,
|
||||
" 确认目录无误后加 --confirm-overrides 重新运行。",
|
||||
);
|
||||
}
|
||||
}
|
||||
lines.push("");
|
||||
return lines.join("\n");
|
||||
}
|
||||
|
||||
export function flowExitCode(document: FlowDocument): number {
|
||||
return ["preview", "committed", "recovered"].includes(document.state) ? 0 : 1;
|
||||
}
|
||||
|
||||
export function inspectExecutionPlan(value: unknown): {
|
||||
kind: "source_import" | "model_profile_create";
|
||||
operationId: string;
|
||||
digest: string;
|
||||
} | null {
|
||||
const source = validateSourcePlan(value);
|
||||
if (source) {
|
||||
return {
|
||||
kind: source.kind,
|
||||
operationId: source.request_id,
|
||||
digest: source.digest,
|
||||
};
|
||||
}
|
||||
const model = validateModelPlan(value);
|
||||
return model
|
||||
? {
|
||||
kind: model.kind,
|
||||
operationId: model.operation_id,
|
||||
digest: model.digest,
|
||||
}
|
||||
: null;
|
||||
}
|
||||
|
||||
function validateSourcePlan(value: unknown): SourceImportPlan | null {
|
||||
const plan = record(value);
|
||||
const requestId = asString(plan?.request_id);
|
||||
if (
|
||||
plan?.schema_version !== 1 ||
|
||||
plan.kind !== "source_import" ||
|
||||
!UUID.test(requestId) ||
|
||||
!Array.isArray(plan.source_ids) ||
|
||||
plan.source_ids.length < 1 ||
|
||||
plan.source_ids.length > 32 ||
|
||||
plan.source_ids.some(
|
||||
(id) => typeof id !== "string" || !SOURCE_ID.test(id),
|
||||
) ||
|
||||
new Set(plan.source_ids).size !== plan.source_ids.length ||
|
||||
plan.full_rescan !== false ||
|
||||
plan.onboarding !== true ||
|
||||
typeof plan.confirm_environment_overrides !== "boolean" ||
|
||||
typeof plan.digest !== "string"
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
const sourceIds = plan.source_ids as string[];
|
||||
const roots = stringMap(plan.source_roots, sourceIds);
|
||||
const revisions = numberMap(plan.source_candidate_revisions, sourceIds);
|
||||
if (!roots || !revisions) return null;
|
||||
const body: SourceImportPlanBody = {
|
||||
schema_version: 1,
|
||||
kind: "source_import",
|
||||
request_id: requestId,
|
||||
source_ids: [...sourceIds],
|
||||
source_roots: roots,
|
||||
source_candidate_revisions: revisions,
|
||||
confirm_environment_overrides: plan.confirm_environment_overrides,
|
||||
full_rescan: false,
|
||||
onboarding: true,
|
||||
};
|
||||
const expected = withDigest(body);
|
||||
return timingSafeText(expected.digest, plan.digest) ? expected : null;
|
||||
}
|
||||
|
||||
function validateModelPlan(value: unknown): ModelCreatePlan | null {
|
||||
const plan = record(value);
|
||||
const operationId = asString(plan?.operation_id);
|
||||
if (
|
||||
plan?.schema_version !== 1 ||
|
||||
plan.kind !== "model_profile_create" ||
|
||||
!UUID.test(operationId) ||
|
||||
typeof plan.digest !== "string"
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
const config = validateModelConfig(plan.config);
|
||||
if (!config) return null;
|
||||
const expected = withDigest<ModelCreatePlanBody>({
|
||||
schema_version: 1,
|
||||
kind: "model_profile_create",
|
||||
operation_id: operationId,
|
||||
config,
|
||||
});
|
||||
return timingSafeText(expected.digest, plan.digest) ? expected : null;
|
||||
return ["committed", "recovered"].includes(document.state) ? 0 : 1;
|
||||
}
|
||||
|
||||
function validateModelConfig(value: unknown): Record<string, unknown> | null {
|
||||
@@ -644,37 +532,6 @@ function projectJob(job: ImportJob): Record<string, unknown> {
|
||||
};
|
||||
}
|
||||
|
||||
function withDigest<T extends object>(body: T): T & { digest: string } {
|
||||
return { ...body, digest: digest(body) };
|
||||
}
|
||||
|
||||
function digest(value: unknown): string {
|
||||
return createHash("sha256").update(canonical(value)).digest("hex");
|
||||
}
|
||||
|
||||
function canonical(value: unknown): string {
|
||||
if (Array.isArray(value)) return `[${value.map(canonical).join(",")}]`;
|
||||
if (value && typeof value === "object") {
|
||||
return `{${Object.keys(value)
|
||||
.sort()
|
||||
.map(
|
||||
(key) =>
|
||||
`${JSON.stringify(key)}:${canonical((value as Record<string, unknown>)[key])}`,
|
||||
)
|
||||
.join(",")}}`;
|
||||
}
|
||||
return JSON.stringify(value);
|
||||
}
|
||||
|
||||
function preview(plan: SourceImportPlan | ModelCreatePlan): FlowDocument {
|
||||
return {
|
||||
schema_version: 1,
|
||||
product: PRODUCT,
|
||||
state: "preview",
|
||||
plan,
|
||||
};
|
||||
}
|
||||
|
||||
function committed(
|
||||
operationId: string,
|
||||
receipt: Record<string, unknown>,
|
||||
@@ -767,43 +624,3 @@ function optionalInteger(
|
||||
(value as number) <= maximum)
|
||||
);
|
||||
}
|
||||
|
||||
function stringMap(
|
||||
value: unknown,
|
||||
keys: readonly string[],
|
||||
): Record<string, string> | null {
|
||||
const map = record(value);
|
||||
if (
|
||||
!map ||
|
||||
Object.keys(map).length !== keys.length ||
|
||||
keys.some((key) => !boundedText(map[key], 256))
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
return Object.fromEntries(keys.map((key) => [key, map[key] as string]));
|
||||
}
|
||||
|
||||
function numberMap(
|
||||
value: unknown,
|
||||
allowedKeys: readonly string[],
|
||||
): Record<string, number> | null {
|
||||
const map = record(value);
|
||||
if (
|
||||
!map ||
|
||||
Object.entries(map).some(
|
||||
([key, revision]) =>
|
||||
!allowedKeys.includes(key) ||
|
||||
!Number.isSafeInteger(revision) ||
|
||||
(revision as number) < 0,
|
||||
)
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
return Object.fromEntries(
|
||||
Object.entries(map).map(([key, revision]) => [key, revision as number]),
|
||||
);
|
||||
}
|
||||
|
||||
function timingSafeText(expected: string, actual: unknown): boolean {
|
||||
return typeof actual === "string" && expected === actual;
|
||||
}
|
||||
@@ -1,64 +0,0 @@
|
||||
import path from "node:path";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { initPlanAttemptPath } from "./cli.js";
|
||||
import { interactiveInitAttemptPath } from "./init-attempt.js";
|
||||
|
||||
// Regression: `init --source <id> --import` used to pass the literal string
|
||||
// `"init"` to `claimPlanAttempt`, which resolved to `<cwd>/init.one-memory-attempt`.
|
||||
// Two consequences: the fence was shared across unrelated runs in the same
|
||||
// directory, and any unrelated file named `init` could make a valid import
|
||||
// report a misleading usage error (exit 2) instead of reading its receipt.
|
||||
describe("init import attempt fence", () => {
|
||||
it("lives under the data dir, not the current working directory", () => {
|
||||
const fence = initPlanAttemptPath(
|
||||
"/tmp/example data",
|
||||
"11111111-2222-4333-8444-555555555555",
|
||||
);
|
||||
expect(fence).toBe(
|
||||
path.join(
|
||||
"/tmp/example data",
|
||||
".cli-init-attempt-11111111-2222-4333-8444-555555555555",
|
||||
),
|
||||
);
|
||||
expect(path.isAbsolute(fence)).toBe(true);
|
||||
expect(fence).not.toBe("init");
|
||||
// Directly under the data dir: no nested directory that could fail to be
|
||||
// created and turn a valid import into a usage error.
|
||||
expect(path.dirname(fence)).toBe("/tmp/example data");
|
||||
});
|
||||
|
||||
it("is per data dir and per operation, so installs/runs cannot collide", () => {
|
||||
const a = initPlanAttemptPath("/tmp/data-a", "op-1");
|
||||
const b = initPlanAttemptPath("/tmp/data-b", "op-1");
|
||||
const c = initPlanAttemptPath("/tmp/data-a", "op-2");
|
||||
expect(new Set([a, b, c]).size).toBe(3);
|
||||
});
|
||||
});
|
||||
|
||||
// QA-CLI R3 F1: the interactive wizard passed a CWD-relative singleton
|
||||
// (`init` or `<journalDir>/init`) to `claimPlanAttempt`. A stale
|
||||
// `init.one-memory-attempt` in the working directory then made every later
|
||||
// `memhub init` fail with "交互计划无效 / exit 2".
|
||||
describe("interactive init attempt fence", () => {
|
||||
it("lives under the data dir and is unique per operation", () => {
|
||||
const fence = interactiveInitAttemptPath(
|
||||
"/tmp/data",
|
||||
"11111111-2222-4333-8444-555555555555",
|
||||
);
|
||||
expect(fence).toBe(
|
||||
path.join(
|
||||
"/tmp/data",
|
||||
".cli-init-attempt-11111111-2222-4333-8444-555555555555",
|
||||
),
|
||||
);
|
||||
expect(path.isAbsolute(fence)).toBe(true);
|
||||
expect(fence).not.toBe("init");
|
||||
});
|
||||
|
||||
it("does not reuse one fence for two distinct plans", () => {
|
||||
const first = interactiveInitAttemptPath("/tmp/data", "op-1");
|
||||
const second = interactiveInitAttemptPath("/tmp/data", "op-2");
|
||||
expect(first).not.toBe(second);
|
||||
});
|
||||
});
|
||||
@@ -1,16 +0,0 @@
|
||||
import path from "node:path";
|
||||
|
||||
/** Where the interactive `init` wizard keeps its durable attempt fence.
|
||||
*
|
||||
* The wizard originally passed a CWD-relative singleton (`init`) to
|
||||
* `claimPlanAttempt`. That shared one fence across unrelated runs and let a
|
||||
* stale `init.one-memory-attempt` in the working directory turn every later
|
||||
* wizard run into "交互计划无效 / exit 2". Keep the fence under the Runtime's
|
||||
* data dir, keyed by the operation id, exactly like `init --source --import`.
|
||||
*/
|
||||
export function interactiveInitAttemptPath(
|
||||
dataDir: string,
|
||||
operationId: string,
|
||||
): string {
|
||||
return path.join(dataDir, `.cli-init-attempt-${operationId}`);
|
||||
}
|
||||
@@ -418,9 +418,9 @@ describe("A2 dual-mode init", () => {
|
||||
});
|
||||
|
||||
// QA-CLI R3 F1: a stale CWD `init.one-memory-attempt` used to poison every
|
||||
// later wizard run. The fence now lives under the isolated data dir and is
|
||||
// keyed by the operation id.
|
||||
it("ignores a stale CWD init fence and keeps its fence under the data dir", async () => {
|
||||
// later wizard run. There are no plan files or attempt fences any more: the
|
||||
// connect request id is the idempotency key Core keeps.
|
||||
it("ignores a stale CWD init fence and writes no fence files", async () => {
|
||||
transport.get.mockImplementation(async (url: string) =>
|
||||
url === "/api/v1/sources"
|
||||
? { status: 200, data: [candidate("codex", "Codex")] }
|
||||
@@ -471,7 +471,7 @@ describe("A2 dual-mode init", () => {
|
||||
const dataEntries = await readdir(dataDir);
|
||||
expect(
|
||||
dataEntries.some((entry) => entry.startsWith(".cli-init-attempt-")),
|
||||
).toBe(true);
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it("keeps selected Harnesses frozen without creating history jobs", async () => {
|
||||
@@ -506,7 +506,7 @@ describe("A2 dual-mode init", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("A19 init --all-detected", () => {
|
||||
describe("A19 sources connect --all-detected (old spelling: init --all-detected)", () => {
|
||||
it("connects every found source in one batch call without prompting", async () => {
|
||||
transport.get.mockImplementation(async (url: string) =>
|
||||
url === "/api/v1/sources"
|
||||
@@ -556,7 +556,7 @@ describe("A19 init --all-detected", () => {
|
||||
expect(prompter.asked).toBe(0);
|
||||
expect(code).toBe(0);
|
||||
const document = JSON.parse(streams.value());
|
||||
expect(document.command).toBe("init");
|
||||
expect(document.command).toBe("sources connect");
|
||||
expect(document.state).toBe("committed");
|
||||
expect(
|
||||
document.sources.map((row: { source_id: string }) => row.source_id),
|
||||
|
||||
@@ -4,18 +4,11 @@ import type {
|
||||
CoreDeleteOperations,
|
||||
CoreOperations,
|
||||
} from "./core-operations.js";
|
||||
import {
|
||||
executeModelProfile,
|
||||
type FlowDocument,
|
||||
planModelProfile,
|
||||
} from "./headless-flows.js";
|
||||
import { createModelProfile, type FlowDocument } from "./headless-flows.js";
|
||||
import { approvalFingerprint, listHosts, readHost } from "./host-flow.js";
|
||||
import { parseSelection, type WizardPrompter } from "./init-wizard.js";
|
||||
import { executeModelProbe, planModelProbe } from "./model-probe-flow.js";
|
||||
import {
|
||||
executeModelProfileActivate,
|
||||
planModelProfileActivate,
|
||||
} from "./model-write-flows.js";
|
||||
import { startModelProbe } from "./model-probe-flow.js";
|
||||
import { activateModelProfile } from "./model-write-flows.js";
|
||||
|
||||
type ProviderPreset = {
|
||||
id: string;
|
||||
@@ -359,30 +352,26 @@ export async function runInteractiveModelSetup(
|
||||
);
|
||||
})
|
||||
: new Map<string, string>();
|
||||
const planned = planModelProfile({
|
||||
name: preset.label,
|
||||
provider_kind: preset.provider_kind,
|
||||
base_url: baseUrl,
|
||||
model,
|
||||
embedding_provider_kind: "local_onnx",
|
||||
embedding_model: "bge-small-zh-v1.5",
|
||||
embedding_dimensions: 512,
|
||||
timeout_seconds: 90,
|
||||
max_concurrency: 4,
|
||||
disable_thinking: true,
|
||||
});
|
||||
if (
|
||||
planned.state !== "preview" ||
|
||||
planned.plan.kind !== "model_profile_create"
|
||||
) {
|
||||
return { state: "failed", code: "model_config_invalid" };
|
||||
}
|
||||
const created = await executeModelProfile(
|
||||
planned.plan,
|
||||
const created = await createModelProfile(
|
||||
{
|
||||
name: preset.label,
|
||||
provider_kind: preset.provider_kind,
|
||||
base_url: baseUrl,
|
||||
model,
|
||||
embedding_provider_kind: "local_onnx",
|
||||
embedding_model: "bge-small-zh-v1.5",
|
||||
embedding_dimensions: 512,
|
||||
timeout_seconds: 90,
|
||||
max_concurrency: 4,
|
||||
disable_thinking: true,
|
||||
},
|
||||
apiKey,
|
||||
clients.reader,
|
||||
clients.operations,
|
||||
);
|
||||
if (created.state === "invalid") {
|
||||
return { state: "failed", code: "model_config_invalid" };
|
||||
}
|
||||
const profile = receiptProfile(created);
|
||||
if (profile === null) {
|
||||
prompter.write("模型配置未保存。");
|
||||
@@ -391,18 +380,11 @@ export async function runInteractiveModelSetup(
|
||||
}
|
||||
|
||||
try {
|
||||
const probePlan = await planModelProbe(
|
||||
const probed = await startModelProbe(
|
||||
clients.reader,
|
||||
clients.operations,
|
||||
profile.profile_id,
|
||||
);
|
||||
const probed =
|
||||
probePlan.state === "preview"
|
||||
? await executeModelProbe(
|
||||
probePlan.plan,
|
||||
clients.reader,
|
||||
clients.operations,
|
||||
)
|
||||
: probePlan;
|
||||
if (!probePassed(probed)) {
|
||||
await clients.operations.delete(
|
||||
`/api/v1/model-profiles/${encodeURIComponent(profile.profile_id)}`,
|
||||
@@ -421,17 +403,8 @@ export async function runInteractiveModelSetup(
|
||||
throw error;
|
||||
}
|
||||
|
||||
const activation = planModelProfileActivate({
|
||||
profileId: profile.profile_id,
|
||||
});
|
||||
if (
|
||||
activation.state !== "preview" ||
|
||||
activation.plan.kind !== "model_profile_activate"
|
||||
) {
|
||||
return { state: "failed", code: "model_activate_plan_invalid" };
|
||||
}
|
||||
const activated = await executeModelProfileActivate(
|
||||
activation.plan,
|
||||
const activated = await activateModelProfile(
|
||||
profile.profile_id,
|
||||
clients.operations,
|
||||
);
|
||||
if (activated.state !== "committed") {
|
||||
|
||||
@@ -101,7 +101,7 @@ describe("memhub jobs", () => {
|
||||
expect(transport.post).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("start previews without a POST", async () => {
|
||||
it("start posts directly, without a preview step", async () => {
|
||||
const output = captureStdout();
|
||||
let code: number;
|
||||
try {
|
||||
@@ -110,14 +110,14 @@ describe("memhub jobs", () => {
|
||||
output.restore();
|
||||
}
|
||||
expect(code!).toBe(0);
|
||||
expect(transport.post).not.toHaveBeenCalled();
|
||||
expect(JSON.parse(output.value())).toMatchObject({
|
||||
state: "preview",
|
||||
plan: { source_id: "codex", operation: "history" },
|
||||
});
|
||||
expect(transport.post).toHaveBeenCalledWith(
|
||||
"/api/v1/import-jobs",
|
||||
{ source_id: "codex", operation: "history" },
|
||||
undefined,
|
||||
);
|
||||
});
|
||||
|
||||
it("start --confirm posts the exact Core request", async () => {
|
||||
it("start still accepts a legacy --confirm", async () => {
|
||||
const output = captureStdout();
|
||||
let code: number;
|
||||
try {
|
||||
@@ -146,7 +146,7 @@ describe("memhub jobs", () => {
|
||||
});
|
||||
|
||||
it.each(["pause", "resume", "cancel"] as const)(
|
||||
"%s previews without a POST",
|
||||
"%s posts the action endpoint directly",
|
||||
async (action) => {
|
||||
const output = captureStdout();
|
||||
let code: number;
|
||||
@@ -156,11 +156,11 @@ describe("memhub jobs", () => {
|
||||
output.restore();
|
||||
}
|
||||
expect(code!).toBe(0);
|
||||
expect(transport.post).not.toHaveBeenCalled();
|
||||
expect(JSON.parse(output.value())).toMatchObject({
|
||||
state: "preview",
|
||||
plan: { job_id: "job-1", action },
|
||||
});
|
||||
expect(transport.post).toHaveBeenCalledWith(
|
||||
`/api/v1/import-jobs/job-1/${action}`,
|
||||
{},
|
||||
undefined,
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
|
||||
@@ -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",
|
||||
};
|
||||
}
|
||||
|
||||
@@ -0,0 +1,583 @@
|
||||
import { mkdtemp, writeFile } from "node:fs/promises";
|
||||
import { 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(),
|
||||
}));
|
||||
vi.mock("axios", () => ({
|
||||
default: { create: vi.fn(() => transport), get: vi.fn() },
|
||||
}));
|
||||
|
||||
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 EVIDENCE = "44444444-4444-4444-8444-444444444444";
|
||||
|
||||
function addReceipt(state = "pending", memoryIds: string[] = []) {
|
||||
return {
|
||||
evidence_id: EVIDENCE,
|
||||
text: "Prefers tea",
|
||||
scope: "all-apps",
|
||||
created_at: "2026-10-27T00:00:00Z",
|
||||
state,
|
||||
task_id: null,
|
||||
memory_ids: memoryIds,
|
||||
error: null,
|
||||
};
|
||||
}
|
||||
|
||||
function memoryRecord(overrides: Record<string, unknown> = {}) {
|
||||
return {
|
||||
memory_id: "exp-1",
|
||||
content: "deploy\nrun make",
|
||||
layer: "experience",
|
||||
status: "active",
|
||||
is_latest: true,
|
||||
supersedes: [],
|
||||
superseded_by: [],
|
||||
tags: [],
|
||||
owner: null,
|
||||
source_raw_ids: [],
|
||||
user_id: "local",
|
||||
agent_id: "",
|
||||
session_id: null,
|
||||
source: "codebuddy",
|
||||
memory_at: null,
|
||||
gmt_created: 1_790_000_000,
|
||||
scene: "working",
|
||||
kind: "experience",
|
||||
variant: "working",
|
||||
task: "deploy",
|
||||
solution: "run make",
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
transport.get.mockReset();
|
||||
transport.post.mockReset();
|
||||
transport.put.mockReset();
|
||||
transport.patch.mockReset();
|
||||
transport.delete.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",
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
describe("memories add", () => {
|
||||
it("adds one memory directly and prints the receipt with its request id", async () => {
|
||||
transport.post.mockResolvedValue({ status: 201, data: addReceipt() });
|
||||
const result = await capturedRun([
|
||||
"memories",
|
||||
"add",
|
||||
"Prefers tea",
|
||||
"--json",
|
||||
]);
|
||||
expect(result.code).toBe(0);
|
||||
const [url, body] = transport.post.mock.calls[0];
|
||||
expect(url).toBe("/api/v2/memories");
|
||||
expect(body).toMatchObject({ text: "Prefers tea", scope: "all-apps" });
|
||||
expect(body.request_id).toMatch(/^[0-9a-f-]{36}$/);
|
||||
expect(JSON.parse(result.stdout)).toMatchObject({
|
||||
schema_version: 2,
|
||||
command: "memories add",
|
||||
state: "accepted",
|
||||
request_id: body.request_id,
|
||||
receipt: { evidence_id: EVIDENCE, state: "pending" },
|
||||
});
|
||||
});
|
||||
|
||||
it("reuses --request-id so a retry is idempotent, and says so in text", async () => {
|
||||
transport.post.mockResolvedValue({ status: 201, data: addReceipt() });
|
||||
const result = await capturedRun([
|
||||
"memories",
|
||||
"add",
|
||||
"Prefers tea",
|
||||
"--request-id",
|
||||
REQUEST,
|
||||
]);
|
||||
expect(result.code).toBe(0);
|
||||
expect(transport.post.mock.calls[0][1].request_id).toBe(REQUEST);
|
||||
expect(result.stdout).toContain(`request_id: ${REQUEST}`);
|
||||
expect(result.stdout).toContain(`memhub memories add-status ${REQUEST}`);
|
||||
});
|
||||
|
||||
it("reads the text from a bounded file", async () => {
|
||||
const directory = await mkdtemp(path.join(tmpdir(), "om-memory-add-"));
|
||||
const file = path.join(directory, "memory.txt");
|
||||
await writeFile(file, "Prefers tea\n", { mode: 0o600 });
|
||||
transport.post.mockResolvedValue({ status: 201, data: addReceipt() });
|
||||
const result = await capturedRun([
|
||||
"memories",
|
||||
"add",
|
||||
"--content-file",
|
||||
file,
|
||||
"--json",
|
||||
]);
|
||||
expect(result.code).toBe(0);
|
||||
expect(transport.post.mock.calls[0][1].text).toBe("Prefers tea");
|
||||
});
|
||||
|
||||
it("refuses bad input before contacting Core", async () => {
|
||||
for (const argv of [
|
||||
["memories", "add"],
|
||||
["memories", "add", "a", "--content-file", "/tmp/x"],
|
||||
["memories", "add", "a", "--request-id", "not-a-uuid"],
|
||||
["memories", "add", " "],
|
||||
["memories", "add", "x".repeat(12_001)],
|
||||
]) {
|
||||
expect((await capturedRun(argv)).code).toBe(2);
|
||||
}
|
||||
expect(transport.post).not.toHaveBeenCalled();
|
||||
expect(runtime.inspectRuntimeStatus).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("reports a Core conflict as rejected, on stdout, exit 1", async () => {
|
||||
transport.post.mockResolvedValue({
|
||||
status: 409,
|
||||
data: { detail: "memory_add_conflict" },
|
||||
});
|
||||
const result = await capturedRun([
|
||||
"memories",
|
||||
"add",
|
||||
"Other text",
|
||||
"--request-id",
|
||||
REQUEST,
|
||||
"--json",
|
||||
]);
|
||||
expect(result.code).toBe(1);
|
||||
expect(JSON.parse(result.stdout)).toMatchObject({
|
||||
state: "rejected",
|
||||
code: "memory_add_conflict",
|
||||
request_id: REQUEST,
|
||||
});
|
||||
});
|
||||
|
||||
it("never reports a malformed success body as accepted", async () => {
|
||||
transport.post.mockResolvedValue({
|
||||
status: 201,
|
||||
data: { evidence_id: "x" },
|
||||
});
|
||||
const result = await capturedRun(["memories", "add", "tea", "--json"]);
|
||||
expect(result.code).toBe(1);
|
||||
expect(JSON.parse(result.stdout).state).toBe("unknown");
|
||||
});
|
||||
|
||||
it("answers a stopped Runtime with exit 3 and writes nothing", async () => {
|
||||
runtime.inspectRuntimeStatus.mockResolvedValue({
|
||||
state: "stopped",
|
||||
code: "runtime_not_running",
|
||||
});
|
||||
const result = await capturedRun(["memories", "add", "tea", "--json"]);
|
||||
expect(result.code).toBe(3);
|
||||
expect(JSON.parse(result.stdout).code).toBe("runtime_not_running");
|
||||
expect(transport.post).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe("memories add-status", () => {
|
||||
it("reads the addition by request id", async () => {
|
||||
transport.get.mockResolvedValue({
|
||||
status: 200,
|
||||
data: addReceipt("ready", ["m-1"]),
|
||||
});
|
||||
const result = await capturedRun([
|
||||
"memories",
|
||||
"add-status",
|
||||
REQUEST,
|
||||
"--json",
|
||||
]);
|
||||
expect(result.code).toBe(0);
|
||||
expect(transport.get).toHaveBeenCalledWith(
|
||||
`/api/v2/memories/additions/${REQUEST}`,
|
||||
undefined,
|
||||
);
|
||||
expect(JSON.parse(result.stdout)).toMatchObject({
|
||||
command: "memories add-status",
|
||||
state: "accepted",
|
||||
receipt: { state: "ready", memory_ids: ["m-1"] },
|
||||
});
|
||||
});
|
||||
|
||||
it("reports an unknown request id as not_found, exit 1", async () => {
|
||||
transport.get.mockResolvedValue({
|
||||
status: 404,
|
||||
data: { detail: "memory_add_not_found" },
|
||||
});
|
||||
const result = await capturedRun(["memories", "status", REQUEST]);
|
||||
expect(result.code).toBe(1);
|
||||
expect(result.stdout).toContain("未找到该 request id");
|
||||
});
|
||||
|
||||
it("refuses a non-UUID request id", async () => {
|
||||
expect((await capturedRun(["memories", "add-status", "abc"])).code).toBe(2);
|
||||
expect(transport.get).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe("memories update / delete pass the namespace", () => {
|
||||
it("updates an experience with kind, variant and task, directly", async () => {
|
||||
const directory = await mkdtemp(path.join(tmpdir(), "om-memory-update-"));
|
||||
const file = path.join(directory, "solution.txt");
|
||||
await writeFile(file, "run make deploy\n", { mode: 0o600 });
|
||||
transport.put.mockResolvedValue({ status: 200, data: memoryRecord() });
|
||||
const result = await capturedRun([
|
||||
"memories",
|
||||
"update",
|
||||
"exp-1",
|
||||
"--content-file",
|
||||
file,
|
||||
"--kind",
|
||||
"experience",
|
||||
"--variant",
|
||||
"working",
|
||||
"--task",
|
||||
"deploy",
|
||||
"--json",
|
||||
]);
|
||||
expect(result.code).toBe(0);
|
||||
expect(transport.put).toHaveBeenCalledWith(
|
||||
"/api/v2/memories/exp-1",
|
||||
{
|
||||
content: "run make deploy",
|
||||
kind: "experience",
|
||||
variant: "working",
|
||||
task: "deploy",
|
||||
},
|
||||
undefined,
|
||||
);
|
||||
expect(JSON.parse(result.stdout)).toMatchObject({
|
||||
schema_version: 2,
|
||||
state: "committed",
|
||||
operation: "update",
|
||||
});
|
||||
});
|
||||
|
||||
it("refuses --task on anything but an experience, and a lone --variant", async () => {
|
||||
for (const argv of [
|
||||
["memories", "update", "m-1", "--content-file", "/tmp/x", "--task", "t"],
|
||||
[
|
||||
"memories",
|
||||
"update",
|
||||
"m-1",
|
||||
"--content-file",
|
||||
"/tmp/x",
|
||||
"--kind",
|
||||
"fact",
|
||||
"--variant",
|
||||
"coding",
|
||||
"--task",
|
||||
"t",
|
||||
],
|
||||
["memories", "delete", "m-1", "--variant", "coding"],
|
||||
["memories", "delete", "m-1", "--kind", "chat", "--variant", "coding"],
|
||||
["memories", "show", "m-1", "--kind", "nope"],
|
||||
]) {
|
||||
expect((await capturedRun(argv)).code).toBe(2);
|
||||
}
|
||||
expect(transport.put).not.toHaveBeenCalled();
|
||||
expect(transport.delete).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("previews a delete without --yes and changes nothing", async () => {
|
||||
const result = await capturedRun([
|
||||
"memories",
|
||||
"delete",
|
||||
"fact-1",
|
||||
"--kind",
|
||||
"fact",
|
||||
"--variant",
|
||||
"coding",
|
||||
"--json",
|
||||
]);
|
||||
expect(result.code).toBe(0);
|
||||
expect(JSON.parse(result.stdout)).toMatchObject({
|
||||
schema_version: 2,
|
||||
command: "memories delete",
|
||||
state: "preview",
|
||||
target: "fact-1",
|
||||
applied: false,
|
||||
});
|
||||
expect(JSON.parse(result.stdout).next_step).toContain(
|
||||
"--kind fact --variant coding --yes",
|
||||
);
|
||||
expect(transport.delete).not.toHaveBeenCalled();
|
||||
expect(runtime.inspectRuntimeStatus).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("deletes with --yes (or the legacy --confirm) and names the namespace", async () => {
|
||||
transport.delete.mockResolvedValue({ status: 204, data: "" });
|
||||
for (const flag of ["--yes", "--confirm"]) {
|
||||
const result = await capturedRun([
|
||||
"memories",
|
||||
"delete",
|
||||
"fact-1",
|
||||
"--kind",
|
||||
"fact",
|
||||
"--variant",
|
||||
"coding",
|
||||
flag,
|
||||
"--json",
|
||||
]);
|
||||
expect(result.code).toBe(0);
|
||||
expect(JSON.parse(result.stdout)).toMatchObject({
|
||||
schema_version: 2,
|
||||
state: "committed",
|
||||
operation: "delete",
|
||||
});
|
||||
}
|
||||
expect(transport.delete).toHaveBeenCalledWith(
|
||||
"/api/v2/memories/fact-1?kind=fact&variant=coding",
|
||||
undefined,
|
||||
);
|
||||
});
|
||||
|
||||
it("explains an ambiguous id instead of a bare code", async () => {
|
||||
transport.delete.mockResolvedValue({
|
||||
status: 409,
|
||||
data: { detail: "memory_id_ambiguous" },
|
||||
});
|
||||
const result = await capturedRun(["memories", "delete", "dup", "--yes"]);
|
||||
expect(result.code).toBe(1);
|
||||
expect(result.stdout).toContain("--kind");
|
||||
});
|
||||
|
||||
it("shows a memory in its namespace", async () => {
|
||||
transport.get.mockResolvedValue({ status: 200, data: memoryRecord() });
|
||||
const result = await capturedRun([
|
||||
"memories",
|
||||
"show",
|
||||
"exp-1",
|
||||
"--kind",
|
||||
"experience",
|
||||
"--variant",
|
||||
"working",
|
||||
"--json",
|
||||
]);
|
||||
expect(result.code).toBe(0);
|
||||
expect(transport.get).toHaveBeenCalledWith("/api/v2/memories/exp-1", {
|
||||
params: { kind: "experience", variant: "working" },
|
||||
});
|
||||
const document = JSON.parse(result.stdout);
|
||||
expect(document.schema_version).toBe(2);
|
||||
expect(document).not.toHaveProperty("evidence");
|
||||
expect(document).not.toHaveProperty("history");
|
||||
expect(document.memory).toMatchObject({
|
||||
kind: "experience",
|
||||
variant: "working",
|
||||
task: "deploy",
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("memories list filters", () => {
|
||||
const page = {
|
||||
memories: [memoryRecord()],
|
||||
total: 1,
|
||||
limit: 100,
|
||||
offset: 0,
|
||||
search_window: 200,
|
||||
window_id: "w1",
|
||||
window_reset: false,
|
||||
};
|
||||
|
||||
it("sends --scene, --source and --query to the library", async () => {
|
||||
transport.get.mockResolvedValue({ status: 200, data: page });
|
||||
const result = await capturedRun([
|
||||
"memories",
|
||||
"list",
|
||||
"--scene",
|
||||
"working",
|
||||
"--source",
|
||||
"codebuddy",
|
||||
"--query",
|
||||
"deploy",
|
||||
"--json",
|
||||
]);
|
||||
expect(result.code).toBe(0);
|
||||
expect(transport.get).toHaveBeenCalledWith("/api/v2/memory-library", {
|
||||
params: {
|
||||
offset: 0,
|
||||
limit: 100,
|
||||
scene: "working",
|
||||
source: "codebuddy",
|
||||
query: "deploy",
|
||||
},
|
||||
});
|
||||
const document = JSON.parse(result.stdout);
|
||||
expect(document.schema_version).toBe(2);
|
||||
expect(document.page).toMatchObject({
|
||||
state: "ok",
|
||||
scene: "working",
|
||||
query: "deploy",
|
||||
search_window: 200,
|
||||
});
|
||||
expect(document.memories[0]).toMatchObject({
|
||||
kind: "experience",
|
||||
variant: "working",
|
||||
});
|
||||
});
|
||||
|
||||
it("omits scene for the default all", async () => {
|
||||
transport.get.mockResolvedValue({ status: 200, data: page });
|
||||
await capturedRun(["memories", "list", "--scene", "all", "--json"]);
|
||||
expect(transport.get.mock.calls[0][1].params).not.toHaveProperty("scene");
|
||||
});
|
||||
|
||||
it("validates --status, --scene and --query locally instead of reporting a busy backend", async () => {
|
||||
for (const argv of [
|
||||
["memories", "list", "--status", "bogus"],
|
||||
["memories", "list", "--scene", "normal"],
|
||||
["memories", "list", "--query", "x", "--status", "archived"],
|
||||
["memories", "list", "--source", "Bad Source"],
|
||||
]) {
|
||||
expect((await capturedRun(argv)).code).toBe(2);
|
||||
}
|
||||
expect(transport.get).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("reports a Core 422 as rejected, not as 后台正忙", async () => {
|
||||
transport.get.mockResolvedValue({
|
||||
status: 422,
|
||||
data: { detail: "sdk_search_supports_active_status_only" },
|
||||
});
|
||||
const result = await capturedRun(["memories", "list"]);
|
||||
expect(result.code).toBe(1);
|
||||
expect(result.stdout).not.toContain("后台正忙");
|
||||
expect(result.stdout).toContain("sdk_search_supports_active_status_only");
|
||||
});
|
||||
});
|
||||
|
||||
describe("graph-width", () => {
|
||||
it("runs a non-destructive decision directly", async () => {
|
||||
transport.post.mockResolvedValue({
|
||||
status: 200,
|
||||
data: { archived_path: null, exported_path: "/tmp/x", mismatch: null },
|
||||
});
|
||||
const result = await capturedRun([
|
||||
"graph-width",
|
||||
"--decision",
|
||||
"export_first",
|
||||
"--json",
|
||||
]);
|
||||
expect(result.code).toBe(0);
|
||||
expect(transport.post).toHaveBeenCalledWith(
|
||||
"/api/v1/graph-width",
|
||||
{ decision: "export_first" },
|
||||
undefined,
|
||||
);
|
||||
expect(JSON.parse(result.stdout).state).toBe("committed");
|
||||
});
|
||||
|
||||
it("previews rebuild_with_new without --yes and applies it with --yes", async () => {
|
||||
const preview = await capturedRun([
|
||||
"graph-width",
|
||||
"--decision",
|
||||
"rebuild_with_new",
|
||||
"--json",
|
||||
]);
|
||||
expect(preview.code).toBe(0);
|
||||
expect(JSON.parse(preview.stdout)).toMatchObject({
|
||||
state: "preview",
|
||||
applied: false,
|
||||
});
|
||||
expect(transport.post).not.toHaveBeenCalled();
|
||||
transport.post.mockResolvedValue({
|
||||
status: 200,
|
||||
data: { archived_path: "/tmp/old", exported_path: null, mismatch: null },
|
||||
});
|
||||
const applied = await capturedRun([
|
||||
"graph",
|
||||
"width",
|
||||
"--decision",
|
||||
"rebuild_with_new",
|
||||
"--yes",
|
||||
"--json",
|
||||
]);
|
||||
expect(applied.code).toBe(0);
|
||||
expect(transport.post).toHaveBeenCalledWith(
|
||||
"/api/v1/graph-width",
|
||||
{ decision: "rebuild_with_new" },
|
||||
undefined,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("removed commands", () => {
|
||||
it.each([
|
||||
["notes", "add", "--content-file", "/tmp/x"],
|
||||
["notes", "process", REQUEST, "--confirm"],
|
||||
["calls", "clear", "--all", "--confirm"],
|
||||
["processing", "extract", "--evidence", REQUEST, "--confirm"],
|
||||
["extraction", "set", "--window", "6m", "--confirm"],
|
||||
["import-file", "upload", "--file", "/tmp/x", "--confirm"],
|
||||
])("%s is an unknown command that writes nothing", async (...argv) => {
|
||||
const result = await capturedRun(argv as string[]);
|
||||
expect(result.code).toBe(2);
|
||||
expect(result.stderr).toContain("未知命令");
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,38 @@
|
||||
import type { SdkMemoryPage, SdkMemoryRecord } from "@one-memory/contracts";
|
||||
import { randomUUID } from "node:crypto";
|
||||
import type {
|
||||
MemoryAddReceipt,
|
||||
SdkMemoryPage,
|
||||
SdkMemoryRecord,
|
||||
} from "@one-memory/contracts";
|
||||
import {
|
||||
buildMemoryAddRequest,
|
||||
MemoryAddInputError,
|
||||
memoryAddStatusPath,
|
||||
parseMemoryAddReceipt,
|
||||
} from "@one-memory/operations";
|
||||
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;
|
||||
|
||||
@@ -38,42 +69,74 @@ export function parseMemoryContent(bytes: Buffer): string | null {
|
||||
: null;
|
||||
}
|
||||
|
||||
export const MEMORY_SCENES = ["all", "chat", "working", "coding"] as const;
|
||||
export type MemoryScene = (typeof MEMORY_SCENES)[number];
|
||||
export const MEMORY_KINDS = ["chat", "experience", "fact"] as const;
|
||||
export type MemoryKind = (typeof MEMORY_KINDS)[number];
|
||||
export const MEMORY_VARIANTS = ["working", "coding"] as const;
|
||||
export type MemoryVariant = (typeof MEMORY_VARIANTS)[number];
|
||||
|
||||
/** Which SDK namespace a memory lives in. A chat memory needs neither; a
|
||||
* working/coding experience or fact needs both so Core does not have to guess
|
||||
* (an id present in two namespaces is refused as ambiguous). */
|
||||
export interface MemoryNamespace {
|
||||
kind?: MemoryKind;
|
||||
variant?: MemoryVariant;
|
||||
}
|
||||
|
||||
export function namespaceParameters(
|
||||
namespace: MemoryNamespace,
|
||||
): Record<string, string> {
|
||||
return {
|
||||
...(namespace.kind === undefined ? {} : { kind: namespace.kind }),
|
||||
...(namespace.variant === undefined ? {} : { variant: namespace.variant }),
|
||||
};
|
||||
}
|
||||
|
||||
export interface MemoryQuery {
|
||||
query?: string;
|
||||
status?: string;
|
||||
source?: string;
|
||||
scene?: MemoryScene;
|
||||
offset: number;
|
||||
limit: number;
|
||||
}
|
||||
|
||||
/** Every `memories` document is schema 2: list/show share it with
|
||||
* add/update/delete, and the v1-era `evidence`/`history` fields are gone. */
|
||||
export interface MemoryPageDocument {
|
||||
schema_version: 1;
|
||||
schema_version: 2;
|
||||
product: string;
|
||||
page: Record<string, unknown>;
|
||||
memories: Record<string, unknown>[];
|
||||
}
|
||||
|
||||
export interface MemoryDetailDocument {
|
||||
schema_version: 1;
|
||||
schema_version: 2;
|
||||
product: string;
|
||||
memory: Record<string, unknown>;
|
||||
evidence: Record<string, unknown>[];
|
||||
history: Record<string, unknown>[];
|
||||
}
|
||||
|
||||
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;
|
||||
if (query.query !== undefined) parameters.query = query.query;
|
||||
if (query.scene !== undefined && query.scene !== "all") {
|
||||
parameters.scene = query.scene;
|
||||
}
|
||||
const outcome = await reader<unknown>("/api/v2/memory-library", parameters);
|
||||
if (outcome.state !== "ok") {
|
||||
return {
|
||||
schema_version: 1,
|
||||
schema_version: 2,
|
||||
product: PRODUCT,
|
||||
page: { state: outcome.state, code: outcome.code },
|
||||
memories: [],
|
||||
@@ -82,7 +145,7 @@ export async function readMemoryPage(
|
||||
const page = sdkMemoryPage(outcome.data);
|
||||
if (page === null) {
|
||||
return {
|
||||
schema_version: 1,
|
||||
schema_version: 2,
|
||||
product: PRODUCT,
|
||||
page: { state: "invalid", code: "response_invalid" },
|
||||
memories: [],
|
||||
@@ -90,15 +153,21 @@ export async function readMemoryPage(
|
||||
}
|
||||
const items = page.memories;
|
||||
return {
|
||||
schema_version: 1,
|
||||
schema_version: 2,
|
||||
product: PRODUCT,
|
||||
page: {
|
||||
state: "ok",
|
||||
scene: query.scene ?? "all",
|
||||
...(query.query === undefined ? {} : { query: query.query }),
|
||||
total: page.total,
|
||||
offset: page.offset,
|
||||
limit: page.limit,
|
||||
returned: items.length,
|
||||
has_more: page.offset + items.length < page.total,
|
||||
// A keyword search only looks among this many most relevant memories.
|
||||
...(typeof page.search_window === "number"
|
||||
? { search_window: page.search_window }
|
||||
: {}),
|
||||
},
|
||||
memories: items.map((item) => describeMemory(item)),
|
||||
};
|
||||
@@ -107,42 +176,208 @@ export async function readMemoryPage(
|
||||
export async function readMemoryDetail(
|
||||
reader: CoreReader,
|
||||
memoryId: string,
|
||||
namespace: MemoryNamespace = {},
|
||||
userId?: string,
|
||||
): Promise<MemoryDetailDocument> {
|
||||
const outcome = await reader<unknown>(`/api/v2/memories/${memoryId}`);
|
||||
const parameters = {
|
||||
...namespaceParameters(namespace),
|
||||
...userIdParameters(userId),
|
||||
};
|
||||
const outcome = await reader<unknown>(
|
||||
`/api/v2/memories/${memoryId}`,
|
||||
Object.keys(parameters).length > 0 ? parameters : undefined,
|
||||
);
|
||||
if (outcome.state !== "ok") {
|
||||
return {
|
||||
schema_version: 1,
|
||||
schema_version: 2,
|
||||
product: PRODUCT,
|
||||
memory: { state: outcome.state, code: outcome.code },
|
||||
evidence: [],
|
||||
history: [],
|
||||
};
|
||||
}
|
||||
const memory = sdkMemory(outcome.data);
|
||||
if (memory === null || memory.memory_id !== memoryId) {
|
||||
return {
|
||||
schema_version: 1,
|
||||
schema_version: 2,
|
||||
product: PRODUCT,
|
||||
memory: { state: "invalid", code: "response_invalid" },
|
||||
evidence: [],
|
||||
history: [],
|
||||
};
|
||||
}
|
||||
return {
|
||||
schema_version: 1,
|
||||
schema_version: 2,
|
||||
product: PRODUCT,
|
||||
memory: { state: "ok", ...describeMemory(memory) },
|
||||
evidence: [],
|
||||
history: [],
|
||||
};
|
||||
}
|
||||
|
||||
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
||||
/** The same bound the app's 添加记忆 box applies. */
|
||||
export const MEMORY_ADD_MAX_CHARACTERS = 12_000;
|
||||
|
||||
export interface MemoryAddDocument {
|
||||
schema_version: 2;
|
||||
product: string;
|
||||
command: "memories add" | "memories add-status";
|
||||
state: "accepted" | "rejected" | "unknown" | "not_found" | "invalid";
|
||||
request_id: string;
|
||||
code?: string;
|
||||
receipt?: Record<string, unknown>;
|
||||
}
|
||||
|
||||
export function isRequestId(value: string): boolean {
|
||||
return UUID.test(value);
|
||||
}
|
||||
|
||||
/** A memory text: non-blank, bounded, no control characters except tab and
|
||||
* newline. */
|
||||
export function validMemoryText(text: string): boolean {
|
||||
return (
|
||||
text.trim().length > 0 &&
|
||||
[...text].length <= MEMORY_ADD_MAX_CHARACTERS &&
|
||||
![...text].some((character) => {
|
||||
const code = character.charCodeAt(0);
|
||||
return code <= 8 || (code >= 11 && code <= 31) || code === 127;
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
function addReceipt(receipt: MemoryAddReceipt): Record<string, unknown> {
|
||||
return {
|
||||
evidence_id: receipt.evidence_id,
|
||||
state: receipt.state,
|
||||
created_at: receipt.created_at,
|
||||
task_id: receipt.task_id,
|
||||
memory_ids: receipt.memory_ids,
|
||||
error: receipt.error,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Add one memory directly, the same request as the app's 添加记忆.
|
||||
*
|
||||
* `requestId` is Core's idempotency key: retrying with the same id and text
|
||||
* returns the first receipt; the same id with other text is refused.
|
||||
*/
|
||||
export async function addMemory(
|
||||
operations: CoreOperations,
|
||||
text: string,
|
||||
requestId: string = randomUUID(),
|
||||
userId?: string,
|
||||
): Promise<MemoryAddDocument> {
|
||||
const base = {
|
||||
schema_version: 2 as const,
|
||||
product: PRODUCT,
|
||||
command: "memories add" as const,
|
||||
request_id: requestId,
|
||||
};
|
||||
if (!validMemoryText(text) || !UUID.test(requestId)) {
|
||||
return { ...base, state: "invalid", code: "memory_add_invalid" };
|
||||
}
|
||||
let request: ReturnType<typeof buildMemoryAddRequest>;
|
||||
try {
|
||||
request = buildMemoryAddRequest({
|
||||
request_id: requestId,
|
||||
text,
|
||||
scope: "all-apps",
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof MemoryAddInputError) {
|
||||
return { ...base, state: "invalid", code: "memory_add_invalid" };
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
const result = await operations.post<unknown>(request.path, {
|
||||
...request.body,
|
||||
...userIdParameters(userId),
|
||||
});
|
||||
if (result.state !== "committed") {
|
||||
return { ...base, state: result.state, code: result.code };
|
||||
}
|
||||
const receipt = parseMemoryAddReceipt(result.data);
|
||||
return receipt === null
|
||||
? { ...base, state: "unknown", code: "memory_add_receipt_invalid" }
|
||||
: { ...base, state: "accepted", receipt: addReceipt(receipt) };
|
||||
}
|
||||
|
||||
/** Read how one addition is doing, by the request id it was submitted with. */
|
||||
export async function readMemoryAddStatus(
|
||||
reader: CoreReader,
|
||||
requestId: string,
|
||||
userId?: string,
|
||||
): Promise<MemoryAddDocument> {
|
||||
const base = {
|
||||
schema_version: 2 as const,
|
||||
product: PRODUCT,
|
||||
command: "memories add-status" as const,
|
||||
request_id: requestId,
|
||||
};
|
||||
if (!UUID.test(requestId)) {
|
||||
return { ...base, state: "invalid", code: "invalid_request_id" };
|
||||
}
|
||||
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" };
|
||||
}
|
||||
if (outcome.state !== "ok") {
|
||||
return {
|
||||
...base,
|
||||
state: outcome.state === "rejected" ? "rejected" : "unknown",
|
||||
code: outcome.code,
|
||||
};
|
||||
}
|
||||
const receipt = parseMemoryAddReceipt(outcome.data);
|
||||
return receipt === null
|
||||
? { ...base, state: "unknown", code: "memory_add_receipt_invalid" }
|
||||
: { ...base, state: "accepted", receipt: addReceipt(receipt) };
|
||||
}
|
||||
|
||||
export function memoryAddExitCode(document: MemoryAddDocument): number {
|
||||
return document.state === "accepted" ? 0 : 1;
|
||||
}
|
||||
|
||||
export function renderMemoryAddText(document: MemoryAddDocument): string {
|
||||
if (document.state === "accepted" && document.receipt) {
|
||||
return [
|
||||
`request_id: ${document.request_id}`,
|
||||
`evidence_id: ${document.receipt.evidence_id}`,
|
||||
`state: ${document.receipt.state}`,
|
||||
...((document.receipt.memory_ids as string[]).length > 0
|
||||
? [`memory_ids: ${(document.receipt.memory_ids as string[]).join(",")}`]
|
||||
: []),
|
||||
document.command === "memories add"
|
||||
? `查询进度:memhub memories add-status ${document.request_id};重试请带上同一个 --request-id,不会重复添加。`
|
||||
: "",
|
||||
"",
|
||||
]
|
||||
.filter((line, index, all) => line !== "" || index === all.length - 1)
|
||||
.join("\n");
|
||||
}
|
||||
if (document.state === "not_found") {
|
||||
return `未找到该 request id:${document.request_id}\n`;
|
||||
}
|
||||
return [
|
||||
`添加记忆未完成:${document.state}(${document.code})`,
|
||||
document.state === "unknown"
|
||||
? `结果未知;可以用同一个 --request-id ${document.request_id} 安全重试。`
|
||||
: "",
|
||||
"",
|
||||
]
|
||||
.filter((line, index, all) => line !== "" || index === all.length - 1)
|
||||
.join("\n");
|
||||
}
|
||||
|
||||
export function renderMemoryPageText(document: MemoryPageDocument): string {
|
||||
const page = document.page;
|
||||
if (page.state !== "ok") {
|
||||
if (page.state === "unavailable") {
|
||||
return "后台正忙,记忆列表这次没查完,不代表没有记忆,请稍后再试\n";
|
||||
}
|
||||
if (page.state === "rejected") {
|
||||
return `记忆查询被拒绝(${page.code}),请检查参数。\n`;
|
||||
}
|
||||
return `记忆查询未完成:${page.state}(${page.code})\n`;
|
||||
}
|
||||
const lines = [
|
||||
@@ -150,9 +385,16 @@ export function renderMemoryPageText(document: MemoryPageDocument): string {
|
||||
page.has_more ? ",还有更多,请用 --offset 继续" : ""
|
||||
}`,
|
||||
];
|
||||
if (typeof page.search_window === "number") {
|
||||
lines.push(`(搜索只在最相关的 ${page.search_window} 条里匹配关键词)`);
|
||||
}
|
||||
for (const memory of document.memories) {
|
||||
const namespace =
|
||||
memory.kind === "chat"
|
||||
? ""
|
||||
: ` kind=${memory.kind} variant=${memory.variant}`;
|
||||
lines.push(
|
||||
`- ${memory.memory_id} [${memory.scene}] ${memory.status}/${memory.layer} source=${memory.source ?? "未知"}`,
|
||||
`- ${memory.memory_id} [${memory.scene}] ${memory.status}/${memory.layer} source=${memory.source ?? "未知"}${namespace}`,
|
||||
` ${memory.content}${memory.content_truncated ? ` …(共 ${memory.content_characters} 字)` : ""}`,
|
||||
);
|
||||
}
|
||||
@@ -167,6 +409,9 @@ export function renderMemoryDetailText(document: MemoryDetailDocument): string {
|
||||
if (memory.state === "unavailable") {
|
||||
return "后台正忙,记忆详情这次没查完,不代表没有这条记忆,请稍后再试\n";
|
||||
}
|
||||
if (memory.code === "memory_id_ambiguous") {
|
||||
return "这个 ID 同时存在于多个场景;请加 --kind(以及 --variant)指明是哪一条。\n";
|
||||
}
|
||||
return `记忆读取未完成:${memory.state}(${memory.code})\n`;
|
||||
}
|
||||
const lines = [
|
||||
@@ -204,6 +449,9 @@ function describeMemory(memory: SdkMemoryRecord): Record<string, unknown> {
|
||||
tags: memory.tags ?? [],
|
||||
memory_at: memory.memory_at,
|
||||
gmt_created: memory.gmt_created,
|
||||
...(memory.task === undefined || memory.task === null
|
||||
? {}
|
||||
: { task: memory.task }),
|
||||
content: budgeted.text,
|
||||
content_truncated: budgeted.truncated,
|
||||
content_characters: budgeted.characters,
|
||||
@@ -221,6 +469,9 @@ function sdkMemoryPage(value: unknown): SdkMemoryPage | null {
|
||||
total: value.total,
|
||||
limit: value.limit,
|
||||
offset: value.offset,
|
||||
...(integer(value.search_window)
|
||||
? { search_window: value.search_window }
|
||||
: {}),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -3,9 +3,9 @@ import { describe, expect, it, vi } from "vitest";
|
||||
import type { CoreReader } from "./core-client.js";
|
||||
import type { CoreOperations } from "./core-operations.js";
|
||||
import {
|
||||
executeModelProbe,
|
||||
planModelProbe,
|
||||
modelProbeExitCode,
|
||||
readModelProbeReceipt,
|
||||
startModelProbe,
|
||||
} from "./model-probe-flow.js";
|
||||
|
||||
const PROFILE_ID = "22222222-2222-4222-8222-222222222222";
|
||||
@@ -83,67 +83,101 @@ function completed(operationId: string) {
|
||||
};
|
||||
}
|
||||
|
||||
describe("model probe operation flow", () => {
|
||||
it("plans against the exact current profile revision", async () => {
|
||||
const reader = vi.fn().mockResolvedValue({ state: "ok", data: [PROFILE] });
|
||||
const document = await planModelProbe(reader as CoreReader, PROFILE_ID);
|
||||
expect(document.state).toBe("preview");
|
||||
if (document.state !== "preview") throw new Error("expected preview");
|
||||
expect(document.plan).toMatchObject({
|
||||
kind: "model_profile_probe",
|
||||
profile_id: PROFILE_ID,
|
||||
expected_profile_revision: 3,
|
||||
});
|
||||
expect(document.plan.operation_id).toMatch(
|
||||
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/,
|
||||
);
|
||||
expect(document.plan.digest).toMatch(/^[0-9a-f]{64}$/);
|
||||
});
|
||||
const OPERATION_ID = "33333333-3333-4333-8333-333333333333";
|
||||
|
||||
it("proves operation support with schema-1 not_found before posting", async () => {
|
||||
const planned = await planModelProbe(
|
||||
vi.fn().mockResolvedValue({ state: "ok", data: [PROFILE] }) as CoreReader,
|
||||
PROFILE_ID,
|
||||
);
|
||||
if (planned.state !== "preview") throw new Error("expected preview");
|
||||
const operationId = planned.plan.operation_id;
|
||||
const reader = vi
|
||||
.fn()
|
||||
.mockResolvedValueOnce({ state: "ok", data: notFound(operationId) });
|
||||
/** Reader that answers the profile list, then the queued operation reads. */
|
||||
function readerWith(...operationReads: unknown[]) {
|
||||
const reader = vi.fn(async (path: string) => {
|
||||
if (path === "/api/v1/model-profiles") {
|
||||
return { state: "ok", data: [PROFILE] };
|
||||
}
|
||||
return operationReads.shift() ?? { state: "not_found", code: "http_404" };
|
||||
});
|
||||
return reader;
|
||||
}
|
||||
|
||||
describe("model probe operation flow", () => {
|
||||
it("probes the exact current profile revision once support is proven", async () => {
|
||||
const reader = readerWith({ state: "ok", data: notFound(OPERATION_ID) });
|
||||
const post = vi
|
||||
.fn()
|
||||
.mockResolvedValue({ state: "committed", data: completed(operationId) });
|
||||
.mockResolvedValue({ state: "committed", data: completed(OPERATION_ID) });
|
||||
|
||||
const result = await executeModelProbe(
|
||||
planned.plan,
|
||||
reader as CoreReader,
|
||||
const result = await startModelProbe(
|
||||
reader as unknown as CoreReader,
|
||||
{ post } as CoreOperations,
|
||||
PROFILE_ID,
|
||||
OPERATION_ID,
|
||||
);
|
||||
|
||||
expect(result.state).toBe("completed");
|
||||
expect(reader).toHaveBeenCalledWith(
|
||||
`/api/v1/model-probe-operations/${operationId}`,
|
||||
`/api/v1/model-probe-operations/${OPERATION_ID}`,
|
||||
);
|
||||
expect(post).toHaveBeenCalledWith(
|
||||
`/api/v1/model-profiles/${PROFILE_ID}/probe`,
|
||||
{ operation_id: operationId, expected_profile_revision: 3 },
|
||||
{ operation_id: OPERATION_ID, expected_profile_revision: 3 },
|
||||
);
|
||||
});
|
||||
|
||||
it("does not post when the Core lacks the exact operation contract", async () => {
|
||||
const planned = await planModelProbe(
|
||||
vi.fn().mockResolvedValue({ state: "ok", data: [PROFILE] }) as CoreReader,
|
||||
it("generates its own operation id and treats a pending probe as submitted", async () => {
|
||||
const post = vi.fn(
|
||||
async (_path: string, body: Record<string, unknown>) => ({
|
||||
state: "committed",
|
||||
data: {
|
||||
...notFound(String(body.operation_id)),
|
||||
profile_id: PROFILE_ID,
|
||||
expected_profile_revision: 3,
|
||||
state: "pending",
|
||||
stale: false,
|
||||
},
|
||||
}),
|
||||
);
|
||||
const reader = vi.fn(async (path: string) =>
|
||||
path === "/api/v1/model-profiles"
|
||||
? { state: "ok", data: [PROFILE] }
|
||||
: {
|
||||
state: "ok",
|
||||
data: notFound(path.split("/").at(-1) ?? ""),
|
||||
},
|
||||
);
|
||||
const result = await startModelProbe(
|
||||
reader as unknown as CoreReader,
|
||||
{ post } as unknown as CoreOperations,
|
||||
PROFILE_ID,
|
||||
);
|
||||
if (planned.state !== "preview") throw new Error("expected preview");
|
||||
expect(result.state).toBe("pending");
|
||||
expect(result.operation_id).toMatch(
|
||||
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/,
|
||||
);
|
||||
expect(modelProbeExitCode(result)).toBe(0);
|
||||
});
|
||||
|
||||
it("refuses an unknown profile without posting", async () => {
|
||||
const post = vi.fn();
|
||||
const result = await executeModelProbe(
|
||||
planned.plan,
|
||||
vi.fn().mockResolvedValue({
|
||||
const result = await startModelProbe(
|
||||
readerWith() as unknown as CoreReader,
|
||||
{ post } as CoreOperations,
|
||||
"44444444-4444-4444-8444-444444444444",
|
||||
OPERATION_ID,
|
||||
);
|
||||
expect(result).toMatchObject({
|
||||
state: "not_found",
|
||||
code: "model_profile_not_found",
|
||||
});
|
||||
expect(post).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("does not post when the Core lacks the exact operation contract", async () => {
|
||||
const post = vi.fn();
|
||||
const result = await startModelProbe(
|
||||
readerWith({
|
||||
state: "not_found",
|
||||
code: "http_404",
|
||||
}) as CoreReader,
|
||||
}) as unknown as CoreReader,
|
||||
{ post } as CoreOperations,
|
||||
PROFILE_ID,
|
||||
OPERATION_ID,
|
||||
);
|
||||
expect(result).toMatchObject({
|
||||
state: "disabled",
|
||||
@@ -153,19 +187,15 @@ describe("model probe operation flow", () => {
|
||||
});
|
||||
|
||||
it("does not post when a legacy Core returns a non-operation body", async () => {
|
||||
const planned = await planModelProbe(
|
||||
vi.fn().mockResolvedValue({ state: "ok", data: [PROFILE] }) as CoreReader,
|
||||
PROFILE_ID,
|
||||
);
|
||||
if (planned.state !== "preview") throw new Error("expected preview");
|
||||
const post = vi.fn();
|
||||
const result = await executeModelProbe(
|
||||
planned.plan,
|
||||
vi.fn().mockResolvedValue({
|
||||
const result = await startModelProbe(
|
||||
readerWith({
|
||||
state: "ok",
|
||||
data: { detail: "legacy_route_shape" },
|
||||
}) as CoreReader,
|
||||
}) as unknown as CoreReader,
|
||||
{ post } as CoreOperations,
|
||||
PROFILE_ID,
|
||||
OPERATION_ID,
|
||||
);
|
||||
expect(result).toMatchObject({
|
||||
state: "disabled",
|
||||
@@ -175,24 +205,19 @@ describe("model probe operation flow", () => {
|
||||
});
|
||||
|
||||
it("reconciles an unknown POST by GET and never treats not_found as replay permission", async () => {
|
||||
const planned = await planModelProbe(
|
||||
vi.fn().mockResolvedValue({ state: "ok", data: [PROFILE] }) as CoreReader,
|
||||
PROFILE_ID,
|
||||
const reader = readerWith(
|
||||
{ state: "ok", data: notFound(OPERATION_ID) },
|
||||
{ state: "ok", data: notFound(OPERATION_ID) },
|
||||
);
|
||||
if (planned.state !== "preview") throw new Error("expected preview");
|
||||
const operationId = planned.plan.operation_id;
|
||||
const reader = vi
|
||||
.fn()
|
||||
.mockResolvedValueOnce({ state: "ok", data: notFound(operationId) })
|
||||
.mockResolvedValueOnce({ state: "ok", data: notFound(operationId) });
|
||||
const post = vi.fn().mockResolvedValue({
|
||||
state: "unknown",
|
||||
code: "transport_result_unknown",
|
||||
});
|
||||
const result = await executeModelProbe(
|
||||
planned.plan,
|
||||
reader as CoreReader,
|
||||
const result = await startModelProbe(
|
||||
reader as unknown as CoreReader,
|
||||
{ post } as CoreOperations,
|
||||
PROFILE_ID,
|
||||
OPERATION_ID,
|
||||
);
|
||||
expect(result).toMatchObject({
|
||||
state: "unknown",
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
import { createHash, randomUUID } from "node:crypto";
|
||||
import { randomUUID } from "node:crypto";
|
||||
import type { CoreReader, ReadOutcome } from "./core-client.js";
|
||||
import type { CoreOperations } from "./core-operations.js";
|
||||
import { validateModelProfiles } from "./core-responses.js";
|
||||
import { PRODUCT } from "./version.js";
|
||||
|
||||
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
||||
const DIGEST = /^[0-9a-f]{64}$/;
|
||||
const RECEIPT_KEYS = new Set([
|
||||
"schema_version",
|
||||
"operation_id",
|
||||
@@ -59,23 +58,7 @@ const CODES = new Set([
|
||||
"embedding_unavailable",
|
||||
]);
|
||||
|
||||
interface ModelProbePlanBody {
|
||||
schema_version: 1;
|
||||
kind: "model_profile_probe";
|
||||
operation_id: string;
|
||||
profile_id: string;
|
||||
expected_profile_revision: number;
|
||||
}
|
||||
|
||||
export type ModelProbePlan = ModelProbePlanBody & { digest: string };
|
||||
|
||||
export type ModelProbeFlowDocument =
|
||||
| {
|
||||
schema_version: 1;
|
||||
product: string;
|
||||
state: "preview";
|
||||
plan: ModelProbePlan;
|
||||
}
|
||||
| {
|
||||
schema_version: 1;
|
||||
product: string;
|
||||
@@ -91,11 +74,25 @@ export type ModelProbeFlowDocument =
|
||||
code: string;
|
||||
};
|
||||
|
||||
export async function planModelProbe(
|
||||
/**
|
||||
* Start one capability probe of a model profile (one real model request).
|
||||
*
|
||||
* Reads the profile's current revision so Core can refuse a probe of a
|
||||
* profile that changed meanwhile, checks that this Core tracks probe
|
||||
* operations (an old Core would start an untracked request), then submits it.
|
||||
* The probe runs asynchronously: the answer is usually `pending`, and
|
||||
* `models receipt <operation-id>` reads the result.
|
||||
*/
|
||||
export async function startModelProbe(
|
||||
reader: CoreReader,
|
||||
operations: CoreOperations,
|
||||
profileId: string,
|
||||
operationId: string = randomUUID(),
|
||||
): Promise<ModelProbeFlowDocument> {
|
||||
if (!UUID.test(profileId)) return failure("invalid", "invalid_profile_id");
|
||||
if (!UUID.test(operationId)) {
|
||||
return failure("invalid", "invalid_probe_operation_id");
|
||||
}
|
||||
const outcome = await reader<unknown>("/api/v1/model-profiles");
|
||||
if (outcome.state !== "ok") return fromReadFailure(outcome);
|
||||
const profiles = validateModelProfiles(outcome.data);
|
||||
@@ -105,71 +102,46 @@ export async function planModelProbe(
|
||||
);
|
||||
if (matches.length !== 1)
|
||||
return failure("not_found", "model_profile_not_found");
|
||||
const profile = matches[0];
|
||||
if (!Number.isSafeInteger(profile.revision) || profile.revision < 1) {
|
||||
const revision = matches[0].revision;
|
||||
if (!Number.isSafeInteger(revision) || revision < 1) {
|
||||
return failure("invalid", "model_profile_revision_invalid");
|
||||
}
|
||||
const plan = withDigest({
|
||||
schema_version: 1 as const,
|
||||
kind: "model_profile_probe" as const,
|
||||
operation_id: randomUUID(),
|
||||
profile_id: profileId,
|
||||
expected_profile_revision: profile.revision,
|
||||
});
|
||||
return { schema_version: 1, product: PRODUCT, state: "preview", plan };
|
||||
}
|
||||
|
||||
export async function executeModelProbe(
|
||||
value: unknown,
|
||||
reader: CoreReader,
|
||||
operations: CoreOperations,
|
||||
): Promise<ModelProbeFlowDocument> {
|
||||
const plan = validatePlan(value);
|
||||
if (!plan) return failure("invalid", "model_probe_plan_invalid");
|
||||
|
||||
// A 404 or a legacy/malformed body is not support. Some old Core versions
|
||||
// ignore the operation body and start an untracked external model request.
|
||||
const support = await reader<unknown>(operationPath(plan.operation_id));
|
||||
const support = await reader<unknown>(operationPath(operationId));
|
||||
if (support.state !== "ok") {
|
||||
return failure(
|
||||
"disabled",
|
||||
"model_probe_operations_unsupported",
|
||||
plan.operation_id,
|
||||
operationId,
|
||||
);
|
||||
}
|
||||
const supportReceipt = validateReceipt(support.data, plan.operation_id);
|
||||
const supportReceipt = validateReceipt(support.data, operationId);
|
||||
if (supportReceipt?.state !== "not_found") {
|
||||
return failure(
|
||||
"disabled",
|
||||
"model_probe_operations_unsupported",
|
||||
plan.operation_id,
|
||||
operationId,
|
||||
);
|
||||
}
|
||||
|
||||
const outcome = await operations.post<unknown>(
|
||||
`/api/v1/model-profiles/${plan.profile_id}/probe`,
|
||||
{
|
||||
operation_id: plan.operation_id,
|
||||
expected_profile_revision: plan.expected_profile_revision,
|
||||
},
|
||||
const posted = await operations.post<unknown>(
|
||||
`/api/v1/model-profiles/${profileId}/probe`,
|
||||
{ operation_id: operationId, expected_profile_revision: revision },
|
||||
);
|
||||
if (outcome.state === "rejected") {
|
||||
return failure("rejected", outcome.code, plan.operation_id);
|
||||
if (posted.state === "rejected") {
|
||||
return failure("rejected", posted.code, operationId);
|
||||
}
|
||||
if (outcome.state === "committed") {
|
||||
return receiptDocument(
|
||||
outcome.data,
|
||||
plan.operation_id,
|
||||
plan.profile_id,
|
||||
plan.expected_profile_revision,
|
||||
);
|
||||
if (posted.state === "committed") {
|
||||
return receiptDocument(posted.data, operationId, profileId, revision);
|
||||
}
|
||||
const reconciled = await readModelProbeReceipt(reader, plan.operation_id);
|
||||
const reconciled = await readModelProbeReceipt(reader, operationId);
|
||||
return reconciled.state === "not_found"
|
||||
? failure(
|
||||
"unknown",
|
||||
"model_probe_receipt_not_found_after_attempt",
|
||||
plan.operation_id,
|
||||
operationId,
|
||||
)
|
||||
: reconciled;
|
||||
}
|
||||
@@ -190,32 +162,24 @@ export async function readModelProbeReceipt(
|
||||
return receiptDocument(outcome.data, operationId);
|
||||
}
|
||||
|
||||
export function inspectModelProbePlan(value: unknown): ModelProbePlan | null {
|
||||
return validatePlan(value);
|
||||
}
|
||||
|
||||
export function modelProbeExitCode(document: ModelProbeFlowDocument): number {
|
||||
return ["preview", "completed"].includes(document.state) ? 0 : 1;
|
||||
// `pending` is a submitted probe still running: the submit succeeded.
|
||||
return ["completed", "pending"].includes(document.state) ? 0 : 1;
|
||||
}
|
||||
|
||||
export function renderModelProbeText(document: ModelProbeFlowDocument): string {
|
||||
if (document.state === "preview") {
|
||||
return [
|
||||
"预览:模型能力探测",
|
||||
`模型配置:${document.plan.profile_id}`,
|
||||
`预期版本:${document.plan.expected_profile_revision}`,
|
||||
`操作 ID:${document.plan.operation_id}`,
|
||||
`确认摘要:${document.plan.digest}`,
|
||||
"未发起模型请求。请保存 --json 输出,再用 models probe --plan-file <path> --confirm。",
|
||||
"",
|
||||
].join("\n");
|
||||
}
|
||||
return [
|
||||
const lines = [
|
||||
`探测状态:${document.state}`,
|
||||
`操作 ID:${document.operation_id ?? "无"}`,
|
||||
"code" in document ? `代码:${document.code}` : "已返回有界操作回执。",
|
||||
"",
|
||||
].join("\n");
|
||||
];
|
||||
if (document.state === "pending" && document.operation_id) {
|
||||
lines.push(
|
||||
`探测在后台进行;用 memhub models receipt ${document.operation_id} 查看结果。`,
|
||||
);
|
||||
}
|
||||
lines.push("");
|
||||
return lines.join("\n");
|
||||
}
|
||||
|
||||
function receiptDocument(
|
||||
@@ -249,31 +213,6 @@ function receiptDocument(
|
||||
};
|
||||
}
|
||||
|
||||
function validatePlan(value: unknown): ModelProbePlan | null {
|
||||
const plan = record(value);
|
||||
if (
|
||||
plan?.schema_version !== 1 ||
|
||||
plan.kind !== "model_profile_probe" ||
|
||||
typeof plan.operation_id !== "string" ||
|
||||
!UUID.test(plan.operation_id) ||
|
||||
typeof plan.profile_id !== "string" ||
|
||||
!UUID.test(plan.profile_id) ||
|
||||
!positiveInteger(plan.expected_profile_revision) ||
|
||||
typeof plan.digest !== "string" ||
|
||||
!DIGEST.test(plan.digest)
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
const expected = withDigest({
|
||||
schema_version: 1 as const,
|
||||
kind: "model_profile_probe" as const,
|
||||
operation_id: plan.operation_id,
|
||||
profile_id: plan.profile_id,
|
||||
expected_profile_revision: plan.expected_profile_revision,
|
||||
});
|
||||
return expected.digest === plan.digest ? expected : null;
|
||||
}
|
||||
|
||||
function validateReceipt(
|
||||
value: unknown,
|
||||
operationId: string,
|
||||
@@ -409,27 +348,6 @@ function failure(
|
||||
};
|
||||
}
|
||||
|
||||
function withDigest<T extends object>(body: T): T & { digest: string } {
|
||||
return {
|
||||
...body,
|
||||
digest: createHash("sha256").update(canonical(body)).digest("hex"),
|
||||
};
|
||||
}
|
||||
|
||||
function canonical(value: unknown): string {
|
||||
if (Array.isArray(value)) return `[${value.map(canonical).join(",")}]`;
|
||||
if (value && typeof value === "object") {
|
||||
return `{${Object.keys(value)
|
||||
.sort()
|
||||
.map(
|
||||
(key) =>
|
||||
`${JSON.stringify(key)}:${canonical((value as Record<string, unknown>)[key])}`,
|
||||
)
|
||||
.join(",")}}`;
|
||||
}
|
||||
return JSON.stringify(value);
|
||||
}
|
||||
|
||||
function record(value: unknown): Record<string, unknown> | null {
|
||||
return value !== null && typeof value === "object" && !Array.isArray(value)
|
||||
? (value as Record<string, unknown>)
|
||||
|
||||
@@ -41,7 +41,7 @@ export type ModelReportDocument =
|
||||
| {
|
||||
schema_version: 1;
|
||||
product: string;
|
||||
state: "denied" | "unavailable" | "not_found" | "invalid";
|
||||
state: "denied" | "unavailable" | "not_found" | "invalid" | "rejected";
|
||||
code: string;
|
||||
};
|
||||
|
||||
@@ -136,7 +136,7 @@ function project(profile: ModelProfile): SafeModelProfile {
|
||||
}
|
||||
|
||||
function failure(
|
||||
state: "denied" | "unavailable" | "not_found" | "invalid",
|
||||
state: "denied" | "unavailable" | "not_found" | "invalid" | "rejected",
|
||||
code: string,
|
||||
): ModelReportDocument {
|
||||
return { schema_version: 1, product: PRODUCT, state, code };
|
||||
|
||||
@@ -126,29 +126,12 @@ describe("models activate", () => {
|
||||
expect(runtime.inspectRuntimeStatus).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("previews without contacting Core", async () => {
|
||||
const result = await capturedRun(["models", "activate", PROFILE, "--json"]);
|
||||
expect(result.code).toBe(0);
|
||||
const document = JSON.parse(result.stdout);
|
||||
expect(document.state).toBe("preview");
|
||||
expect(document.plan.kind).toBe("model_profile_activate");
|
||||
// Activation has no CAS body, so the plan must not pretend to carry one.
|
||||
expect(document.plan).not.toHaveProperty("expected_revision");
|
||||
expect(transport.post).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("posts the activate endpoint with --confirm and reports the profile", async () => {
|
||||
it("posts the activate endpoint directly and reports the profile", async () => {
|
||||
transport.post.mockResolvedValue({
|
||||
status: 200,
|
||||
data: profileResponse(),
|
||||
});
|
||||
const result = await capturedRun([
|
||||
"models",
|
||||
"activate",
|
||||
PROFILE,
|
||||
"--confirm",
|
||||
"--json",
|
||||
]);
|
||||
const result = await capturedRun(["models", "activate", PROFILE, "--json"]);
|
||||
expect(result.code).toBe(0);
|
||||
expect(transport.post).toHaveBeenCalledWith(
|
||||
`/api/v1/model-profiles/${PROFILE}/activate`,
|
||||
@@ -196,31 +179,22 @@ describe("models activate", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("models edit", () => {
|
||||
it("previews a patch from a config file without writing", async () => {
|
||||
const directory = await mkdtemp(path.join(tmpdir(), "om-model-edit-"));
|
||||
const config = path.join(directory, "patch.json");
|
||||
await writeFile(
|
||||
config,
|
||||
JSON.stringify({ expected_revision: 4, name: "Renamed" }),
|
||||
{ mode: 0o600 },
|
||||
);
|
||||
const result = await capturedRun([
|
||||
"models",
|
||||
"edit",
|
||||
PROFILE,
|
||||
"--config-file",
|
||||
config,
|
||||
"--json",
|
||||
]);
|
||||
describe("models remove", () => {
|
||||
it("previews without --yes and changes nothing", async () => {
|
||||
const result = await capturedRun(["models", "remove", PROFILE, "--json"]);
|
||||
expect(result.code).toBe(0);
|
||||
const document = JSON.parse(result.stdout);
|
||||
expect(document.state).toBe("preview");
|
||||
expect(document.plan.patch).toEqual({ name: "Renamed" });
|
||||
expect(transport.patch).not.toHaveBeenCalled();
|
||||
expect(JSON.parse(result.stdout)).toMatchObject({
|
||||
command: "models remove",
|
||||
state: "preview",
|
||||
target: PROFILE,
|
||||
applied: false,
|
||||
});
|
||||
expect(runtime.inspectRuntimeStatus).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it("sends the exact patch body with the CAS revision on confirm", async () => {
|
||||
describe("models edit", () => {
|
||||
it("sends the exact patch body with the CAS revision directly", async () => {
|
||||
const directory = await mkdtemp(path.join(tmpdir(), "om-model-edit-"));
|
||||
const config = path.join(directory, "patch.json");
|
||||
await writeFile(
|
||||
@@ -242,7 +216,6 @@ describe("models edit", () => {
|
||||
PROFILE,
|
||||
"--config-file",
|
||||
config,
|
||||
"--confirm",
|
||||
"--json",
|
||||
]);
|
||||
expect(result.code).toBe(0);
|
||||
|
||||
@@ -3,14 +3,12 @@
|
||||
* activate (`POST /model-profiles/{id}/activate`) and edit
|
||||
* (`PATCH /model-profiles/{id}`).
|
||||
*
|
||||
* Core owns the semantics. These flows reuse the same two-phase convention as
|
||||
* `models configure`: plan without `--confirm` (or produce a plan file), then
|
||||
* apply exactly that plan. The edit body mirrors the Web console's
|
||||
* `ModelProfilePatch`, including `expected_revision` as the compare-and-swap
|
||||
* guard Core requires.
|
||||
* Core owns the semantics. Both run directly: activation is reversible (activate
|
||||
* another profile) and an edit is guarded by `expected_revision`, the
|
||||
* compare-and-swap Core requires, mirroring the Web console's
|
||||
* `ModelProfilePatch`.
|
||||
*/
|
||||
|
||||
import { createHash, randomUUID } from "node:crypto";
|
||||
import type { ModelProfile } from "@one-memory/contracts";
|
||||
import {
|
||||
buildModelProfileUpdateRequest,
|
||||
@@ -22,29 +20,7 @@ import { PRODUCT } from "./version.js";
|
||||
|
||||
const PROFILE_ID = /^[A-Za-z0-9][\w.-]{0,127}$/;
|
||||
|
||||
export type ModelWritePlan =
|
||||
| {
|
||||
schema_version: 1;
|
||||
kind: "model_profile_activate";
|
||||
profile_id: string;
|
||||
digest: string;
|
||||
}
|
||||
| {
|
||||
schema_version: 1;
|
||||
kind: "model_profile_edit";
|
||||
profile_id: string;
|
||||
expected_revision: number;
|
||||
patch: Record<string, unknown>;
|
||||
digest: string;
|
||||
};
|
||||
|
||||
export type ModelWriteDocument =
|
||||
| {
|
||||
schema_version: 1;
|
||||
product: string;
|
||||
state: "preview";
|
||||
plan: ModelWritePlan;
|
||||
}
|
||||
| {
|
||||
schema_version: 1;
|
||||
product: string;
|
||||
@@ -60,38 +36,6 @@ export type ModelWriteDocument =
|
||||
code: string;
|
||||
};
|
||||
|
||||
function canonical(value: unknown): string {
|
||||
if (Array.isArray(value)) return `[${value.map(canonical).join(",")}]`;
|
||||
if (value !== null && typeof value === "object") {
|
||||
return `{${Object.entries(value as Record<string, unknown>)
|
||||
.sort(([left], [right]) => left.localeCompare(right))
|
||||
.map(([key, item]) => `${JSON.stringify(key)}:${canonical(item)}`)
|
||||
.join(",")}}`;
|
||||
}
|
||||
if (value === undefined) return "null";
|
||||
return JSON.stringify(value);
|
||||
}
|
||||
|
||||
function digestOf(value: unknown): string {
|
||||
return createHash("sha256").update(canonical(value)).digest("hex");
|
||||
}
|
||||
|
||||
function preview(
|
||||
plan:
|
||||
| Omit<
|
||||
Extract<ModelWritePlan, { kind: "model_profile_activate" }>,
|
||||
"digest"
|
||||
>
|
||||
| Omit<Extract<ModelWritePlan, { kind: "model_profile_edit" }>, "digest">,
|
||||
): ModelWriteDocument {
|
||||
return {
|
||||
schema_version: 1,
|
||||
product: PRODUCT,
|
||||
state: "preview",
|
||||
plan: { ...plan, digest: digestOf(plan) } as ModelWritePlan,
|
||||
};
|
||||
}
|
||||
|
||||
function invalid(code: string): ModelWriteDocument {
|
||||
return { schema_version: 1, product: PRODUCT, state: "invalid", code };
|
||||
}
|
||||
@@ -122,87 +66,45 @@ function projectProfile(profile: ModelProfile): Record<string, unknown> {
|
||||
};
|
||||
}
|
||||
|
||||
// --- activate -------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Activation has no request body, so the plan carries only the target. There
|
||||
* is deliberately no `expected_revision`: Core would never read one, and
|
||||
* recording a check it does not perform would be a fake guard.
|
||||
*/
|
||||
export function planModelProfileActivate(input: {
|
||||
profileId: string;
|
||||
}): ModelWriteDocument {
|
||||
return PROFILE_ID.test(input.profileId)
|
||||
? preview({
|
||||
schema_version: 1,
|
||||
kind: "model_profile_activate",
|
||||
profile_id: input.profileId,
|
||||
})
|
||||
: invalid("model_profile_activate_input_invalid");
|
||||
}
|
||||
|
||||
export async function executeModelProfileActivate(
|
||||
plan: Extract<ModelWritePlan, { kind: "model_profile_activate" }>,
|
||||
/** Make `profileId` the active profile. Activation has no request body, so
|
||||
* there is no revision for Core to check. */
|
||||
export async function activateModelProfile(
|
||||
profileId: string,
|
||||
operations: CoreOperations,
|
||||
): Promise<ModelWriteDocument> {
|
||||
if (!PROFILE_ID.test(profileId)) {
|
||||
return invalid("model_profile_activate_input_invalid");
|
||||
}
|
||||
const result = await operations.post<unknown>(
|
||||
`/api/v1/model-profiles/${encodeURIComponent(plan.profile_id)}/activate`,
|
||||
`/api/v1/model-profiles/${encodeURIComponent(profileId)}/activate`,
|
||||
{},
|
||||
);
|
||||
return modelProfileResult(plan.profile_id, result);
|
||||
return modelProfileResult(profileId, result);
|
||||
}
|
||||
|
||||
// --- edit -----------------------------------------------------------------
|
||||
|
||||
export function planModelProfileEdit(input: {
|
||||
profileId: string;
|
||||
patch: unknown;
|
||||
}): ModelWriteDocument {
|
||||
// The shared builder owns patch validation (14-key allow-list,
|
||||
// expected_revision CAS, field bounds, api_key/clear_api_key exclusivity).
|
||||
// A rejected patch is `invalid`, never a plan the caller could confirm.
|
||||
let request: ReturnType<typeof buildModelProfileUpdateRequest>;
|
||||
try {
|
||||
request = buildModelProfileUpdateRequest(input.profileId, input.patch);
|
||||
} catch (error) {
|
||||
if (error instanceof ModelProfileInputError) {
|
||||
return invalid("model_profile_edit_input_invalid");
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
const patch = request.body as Record<string, unknown>;
|
||||
if (!PROFILE_ID.test(input.profileId)) {
|
||||
/** Apply a bounded patch (carrying `expected_revision`) to one profile; the
|
||||
* key, when given, comes from a controlled file and never from the patch. */
|
||||
export async function editModelProfile(
|
||||
profileId: string,
|
||||
patch: unknown,
|
||||
apiKey: string | undefined,
|
||||
operations: CorePatchOperations,
|
||||
): Promise<ModelWriteDocument> {
|
||||
if (
|
||||
!PROFILE_ID.test(profileId) ||
|
||||
patch === null ||
|
||||
typeof patch !== "object" ||
|
||||
Array.isArray(patch) ||
|
||||
"api_key" in patch
|
||||
) {
|
||||
return invalid("model_profile_edit_input_invalid");
|
||||
}
|
||||
return preview({
|
||||
schema_version: 1,
|
||||
kind: "model_profile_edit",
|
||||
profile_id: input.profileId,
|
||||
expected_revision: patch.expected_revision as number,
|
||||
// `api_key` is read from a controlled file at apply time, exactly like
|
||||
// `models configure`; the plan never carries the credential. The revision
|
||||
// is a top-level field, so it is stripped from the nested patch rather
|
||||
// than being duplicated in both places.
|
||||
patch: Object.fromEntries(
|
||||
Object.entries(patch).filter(
|
||||
([key]) => key !== "api_key" && key !== "expected_revision",
|
||||
),
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
export async function executeModelProfileEdit(
|
||||
plan: Extract<ModelWritePlan, { kind: "model_profile_edit" }>,
|
||||
operations: CorePatchOperations,
|
||||
apiKey: string | undefined,
|
||||
): Promise<ModelWriteDocument> {
|
||||
// Re-validate through the shared builder at apply time too, so a hand-edited
|
||||
// plan cannot smuggle an out-of-bounds patch past the write.
|
||||
// The shared builder owns patch validation (allow-list, expected_revision
|
||||
// CAS, field bounds, api_key/clear_api_key exclusivity).
|
||||
let request: ReturnType<typeof buildModelProfileUpdateRequest>;
|
||||
try {
|
||||
request = buildModelProfileUpdateRequest(plan.profile_id, {
|
||||
...plan.patch,
|
||||
expected_revision: plan.expected_revision,
|
||||
request = buildModelProfileUpdateRequest(profileId, {
|
||||
...(patch as Record<string, unknown>),
|
||||
...(apiKey === undefined ? {} : { api_key: apiKey }),
|
||||
});
|
||||
} catch (error) {
|
||||
@@ -212,7 +114,7 @@ export async function executeModelProfileEdit(
|
||||
throw error;
|
||||
}
|
||||
const result = await operations.patch<unknown>(request.path, request.body);
|
||||
return modelProfileResult(plan.profile_id, result);
|
||||
return modelProfileResult(profileId, result);
|
||||
}
|
||||
|
||||
function modelProfileResult(
|
||||
@@ -254,69 +156,11 @@ function modelProfileResult(
|
||||
return committed(operationId, profile);
|
||||
}
|
||||
|
||||
export function validateModelWritePlan(value: unknown): ModelWritePlan | null {
|
||||
if (value === null || typeof value !== "object" || Array.isArray(value)) {
|
||||
return null;
|
||||
}
|
||||
const candidate = value as Record<string, unknown>;
|
||||
if (candidate.schema_version !== 1 || typeof candidate.kind !== "string") {
|
||||
return null;
|
||||
}
|
||||
const { digest, ...withoutDigest } = candidate;
|
||||
if (typeof digest !== "string" || digest !== digestOf(withoutDigest)) {
|
||||
return null;
|
||||
}
|
||||
switch (candidate.kind) {
|
||||
case "model_profile_activate":
|
||||
return typeof candidate.profile_id === "string" &&
|
||||
PROFILE_ID.test(candidate.profile_id) &&
|
||||
Object.keys(candidate).length === 3
|
||||
? (candidate as unknown as ModelWritePlan)
|
||||
: null;
|
||||
case "model_profile_edit": {
|
||||
if (
|
||||
typeof candidate.profile_id !== "string" ||
|
||||
!PROFILE_ID.test(candidate.profile_id) ||
|
||||
!Number.isSafeInteger(candidate.expected_revision)
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
// The shared builder is the single judge of a patch body; the plan must
|
||||
// not carry `api_key` (that is read from a controlled file at apply).
|
||||
let built: ReturnType<typeof buildModelProfileUpdateRequest>;
|
||||
try {
|
||||
built = buildModelProfileUpdateRequest(candidate.profile_id, {
|
||||
...(candidate.patch as Record<string, unknown>),
|
||||
expected_revision: candidate.expected_revision,
|
||||
});
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
if ("api_key" in built.body) return null;
|
||||
return Object.keys(candidate).length === 6
|
||||
? (candidate as unknown as ModelWritePlan)
|
||||
: null;
|
||||
}
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export function modelWriteExitCode(document: ModelWriteDocument): number {
|
||||
return ["preview", "committed"].includes(document.state) ? 0 : 1;
|
||||
return document.state === "committed" ? 0 : 1;
|
||||
}
|
||||
|
||||
export function renderModelWriteText(document: ModelWriteDocument): string {
|
||||
if (document.state === "preview") {
|
||||
const plan = document.plan;
|
||||
return [
|
||||
`预览:${plan.kind}`,
|
||||
`目标配置:${plan.profile_id}`,
|
||||
`确认摘要:${plan.digest}`,
|
||||
"未执行任何写操作。请把 --json 输出保存为 plan 文件,再用对应命令加 --confirm。",
|
||||
"",
|
||||
].join("\n");
|
||||
}
|
||||
if (document.state === "committed") {
|
||||
return `已保存模型配置:${document.receipt.profile_id}\nrevision=${document.receipt.revision}\n`;
|
||||
}
|
||||
|
||||
@@ -1,57 +0,0 @@
|
||||
import { constants } from "node:fs";
|
||||
import { type FileHandle, open } from "node:fs/promises";
|
||||
import { readBoundedJson } from "./bounded-file.js";
|
||||
|
||||
export type PlanAttempt = {
|
||||
schema_version: 1;
|
||||
operation_id: string;
|
||||
plan_digest: string;
|
||||
};
|
||||
|
||||
/**
|
||||
* Durable client fence written before a mutating request. Once present, the
|
||||
* same plan may only query its operation receipt; it is never blindly posted
|
||||
* again after an unknown result.
|
||||
*/
|
||||
export async function claimPlanAttempt(
|
||||
planFile: string,
|
||||
attempt: PlanAttempt,
|
||||
): Promise<"first" | "prior" | "invalid"> {
|
||||
const journal = `${planFile}.one-memory-attempt`;
|
||||
let handle: FileHandle;
|
||||
try {
|
||||
handle = await open(
|
||||
journal,
|
||||
constants.O_WRONLY |
|
||||
constants.O_CREAT |
|
||||
constants.O_EXCL |
|
||||
(constants.O_NOFOLLOW ?? 0),
|
||||
0o600,
|
||||
);
|
||||
} catch (error) {
|
||||
if ((error as NodeJS.ErrnoException).code !== "EEXIST") return "invalid";
|
||||
const current = await readBoundedJson(journal, 1024);
|
||||
return sameAttempt(current, attempt) ? "prior" : "invalid";
|
||||
}
|
||||
try {
|
||||
await handle.writeFile(`${JSON.stringify(attempt)}\n`, "utf8");
|
||||
await handle.sync();
|
||||
return "first";
|
||||
} catch {
|
||||
return "invalid";
|
||||
} finally {
|
||||
await handle.close().catch(() => undefined);
|
||||
}
|
||||
}
|
||||
|
||||
function sameAttempt(value: unknown, expected: PlanAttempt): boolean {
|
||||
return (
|
||||
value !== null &&
|
||||
typeof value === "object" &&
|
||||
!Array.isArray(value) &&
|
||||
(value as Record<string, unknown>).schema_version === 1 &&
|
||||
(value as Record<string, unknown>).operation_id === expected.operation_id &&
|
||||
(value as Record<string, unknown>).plan_digest === expected.plan_digest &&
|
||||
Object.keys(value).length === 3
|
||||
);
|
||||
}
|
||||
@@ -104,7 +104,7 @@ describe("package matrix harness", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("fails closed on every destructive UI-write command without a Runtime", {
|
||||
it("fails closed on every write command without a Runtime", {
|
||||
timeout: 120_000,
|
||||
}, async () => {
|
||||
const runtimeDist = await mkdtemp(
|
||||
@@ -140,7 +140,7 @@ describe("package matrix harness", () => {
|
||||
expect(document.state).toBe("verified");
|
||||
const results = document.commands.stopped_destructive.results;
|
||||
// Every destructive command fails closed while Runtime is stopped.
|
||||
expect(Object.keys(results)).toHaveLength(7);
|
||||
expect(Object.keys(results)).toHaveLength(8);
|
||||
for (const [name, result] of Object.entries(results)) {
|
||||
expect(result, `${name} did not fail closed`).toEqual({
|
||||
exit: 3,
|
||||
@@ -164,7 +164,7 @@ describe("package matrix harness", () => {
|
||||
const shim = [
|
||||
"#!/usr/bin/env node",
|
||||
"const a=process.argv.slice(2);",
|
||||
"if(['calls','notes','graph','models','memories'].includes(a[0])){",
|
||||
"if(['graph-width','models','memories','sources'].includes(a[0])){",
|
||||
' process.stdout.write(\'{"schema_version":1,"product":"memhub","state":"committed","operation_id":"x","receipt":{}}\\n\');',
|
||||
" process.exit(0);",
|
||||
"}",
|
||||
@@ -190,7 +190,7 @@ describe("package matrix harness", () => {
|
||||
expect(failure?.state).toBe("failed");
|
||||
expect(
|
||||
failure?.failures.filter((item) => item.includes("did not fail closed")),
|
||||
).toHaveLength(7);
|
||||
).toHaveLength(8);
|
||||
});
|
||||
|
||||
it("can require an expected bundle SHA and reports bundle metadata", {
|
||||
@@ -291,9 +291,7 @@ describe("package matrix harness", () => {
|
||||
"sync",
|
||||
"memories",
|
||||
"profile",
|
||||
"calls",
|
||||
"notes",
|
||||
"graph",
|
||||
"graph-width",
|
||||
"models",
|
||||
"host",
|
||||
"doctor",
|
||||
@@ -311,14 +309,12 @@ describe("package matrix harness", () => {
|
||||
"uninstall",
|
||||
"delete-data",
|
||||
"recall",
|
||||
"setup",
|
||||
"scan",
|
||||
"progress",
|
||||
"jobs",
|
||||
"path",
|
||||
"scan-roots",
|
||||
"adapter",
|
||||
"write",
|
||||
"task",
|
||||
"serve",
|
||||
];
|
||||
const catalog = {
|
||||
|
||||
@@ -145,13 +145,15 @@ describe("memhub short-command contract", () => {
|
||||
[command.usage, command.alternative_usage ?? ""].join("\n"),
|
||||
).join("\n");
|
||||
for (const shortForm of [
|
||||
"memhub calls clear",
|
||||
"memhub notes add",
|
||||
"memhub notes process",
|
||||
"memhub memories add",
|
||||
"memhub memories add-status",
|
||||
"memhub models setup",
|
||||
"memhub models add",
|
||||
"memhub models test",
|
||||
"memhub models activate",
|
||||
"memhub models edit",
|
||||
"memhub graph width",
|
||||
"memhub graph-width",
|
||||
"memhub sources connect",
|
||||
"memhub write submit",
|
||||
"memhub write status",
|
||||
"memhub memories update",
|
||||
@@ -159,6 +161,10 @@ describe("memhub short-command contract", () => {
|
||||
]) {
|
||||
expect(flattened).toContain(shortForm);
|
||||
}
|
||||
// No "note" concept and no plan files on the CLI surface.
|
||||
for (const removed of ["memhub notes", "memhub calls", "--plan-file"]) {
|
||||
expect(flattened).not.toContain(removed);
|
||||
}
|
||||
expect(COMMANDS.some((command) => command.name === "permission")).toBe(
|
||||
false,
|
||||
);
|
||||
@@ -211,7 +217,7 @@ describe("memhub short-command contract", () => {
|
||||
// but an earlier catalog text still claimed they were "尚不可用". A user
|
||||
// reading `help path` would not try a command that works.
|
||||
it("does not call the wired path/adapter commands unavailable", () => {
|
||||
for (const name of ["path", "adapter"]) {
|
||||
for (const name of ["scan-roots", "adapter"]) {
|
||||
const command = COMMANDS.find((entry) => entry.name === name);
|
||||
expect(command).toBeDefined();
|
||||
const text = [
|
||||
@@ -277,7 +283,7 @@ describe("memhub short-command contract", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("keeps plan-file apply previews free of diagnostic side effects", {
|
||||
it("keeps destructive previews free of diagnostic side effects", {
|
||||
timeout: 20_000,
|
||||
}, async () => {
|
||||
expect(
|
||||
@@ -291,9 +297,10 @@ describe("memhub short-command contract", () => {
|
||||
);
|
||||
try {
|
||||
for (const [index, argv] of [
|
||||
["sources", "import", "--plan-file", path.join(root, "plan.json")],
|
||||
["models", "configure", "--plan-file", path.join(root, "plan.json")],
|
||||
["memories", "apply", "--plan-file", path.join(root, "plan.json")],
|
||||
["memories", "delete", "m-1"],
|
||||
["models", "remove", "44444444-4444-4444-8444-444444444444"],
|
||||
["backup", "restore", "--source", path.join(root, "backup.sqlite3")],
|
||||
["graph-width", "--decision", "rebuild_with_new"],
|
||||
].entries()) {
|
||||
const dataDir = path.join(root, `data-${index}`);
|
||||
await import("node:fs/promises").then(({ mkdir }) =>
|
||||
@@ -305,8 +312,8 @@ describe("memhub short-command contract", () => {
|
||||
stdio: "pipe",
|
||||
});
|
||||
} catch {
|
||||
// The missing plan or stopped Runtime is expected; the guard is
|
||||
// that even the failure path did not add a diagnostic file.
|
||||
// A failure path is acceptable; the guard is that no preview adds
|
||||
// a diagnostic file.
|
||||
}
|
||||
expect(await readdir(dataDir)).toEqual([]);
|
||||
}
|
||||
|
||||
@@ -1,122 +1,56 @@
|
||||
// A6: the rules behind the six product commands.
|
||||
// The rules behind the commands that attach to the Runtime directly.
|
||||
//
|
||||
// The parsers are where the contract actually lives — "needs --yes to change
|
||||
// anything" and "no write without evidence" are decisions, not formatting — so
|
||||
// they are tested directly rather than only through the CLI.
|
||||
// The parsers are where the contract actually lives — "only irreversible
|
||||
// operations ask for --yes" and "unknown flags are rejected" are decisions,
|
||||
// not formatting — so they are tested directly rather than only through the
|
||||
// CLI.
|
||||
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { parseOpenAIMessages } from "./core-client.js";
|
||||
import {
|
||||
asPlanDocument,
|
||||
describe as describePlan,
|
||||
parseProductCommand,
|
||||
parseBackupCommand,
|
||||
parseScanCommand,
|
||||
parseScanRootsCommand,
|
||||
parseTaskCommand,
|
||||
parseWriteCommand,
|
||||
restoreConfirmationMatches,
|
||||
} from "./product-flows.js";
|
||||
|
||||
describe("mutating commands are two-phase", () => {
|
||||
it("plans without --yes", () => {
|
||||
const parsed = parseProductCommand("scan", ["--root", "/tmp/a"]);
|
||||
expect(parsed?.confirmed).toBe(false);
|
||||
});
|
||||
|
||||
it("confirms with --yes", () => {
|
||||
const parsed = parseProductCommand("scan", ["--root", "/tmp/a", "--yes"]);
|
||||
expect(parsed?.confirmed).toBe(true);
|
||||
});
|
||||
|
||||
it("says what it would do", () => {
|
||||
const parsed = parseProductCommand("scan", ["--root", "/tmp/a"]);
|
||||
// `?.plan!` would let a null parse pass silently; assert the parse first.
|
||||
if (parsed === null) {
|
||||
throw new Error("scan with a root must parse");
|
||||
}
|
||||
expect(describePlan(parsed.plan)).toContain("scan: /tmp/a");
|
||||
});
|
||||
|
||||
it("marks a plan as not applied", () => {
|
||||
// Assert the parse itself: `?.` let a null result satisfy this test for as
|
||||
// long as `backup create` was unparseable.
|
||||
const parsed = parseProductCommand("backup", [
|
||||
"create",
|
||||
"--target",
|
||||
"/tmp/b",
|
||||
]);
|
||||
if (parsed === null) {
|
||||
throw new Error("backup create must parse");
|
||||
}
|
||||
expect(parsed.plan.verb).toBe("create");
|
||||
expect(asPlanDocument(parsed.plan).applied).toBe(false);
|
||||
});
|
||||
|
||||
it("parses the verb for every command that takes one", () => {
|
||||
// `path add` / `path remove` / `backup create` were rejected outright
|
||||
// before the verb was skipped in parsing.
|
||||
describe("scan-roots", () => {
|
||||
it("reads the root, source and recursive flag off add", () => {
|
||||
expect(
|
||||
parseProductCommand("path", ["add", "--root", "/tmp/x"])?.plan.action,
|
||||
).toBe("add scan root");
|
||||
expect(
|
||||
parseProductCommand("path", ["remove", "--root", "/tmp/x"])?.plan.action,
|
||||
).toBe("remove scan root");
|
||||
expect(
|
||||
parseProductCommand("backup", ["restore", "--source", "/tmp/b"])?.plan
|
||||
.action,
|
||||
).toBe("restore backup");
|
||||
parseScanRootsCommand([
|
||||
"add",
|
||||
"--root",
|
||||
"/tmp/x",
|
||||
"--source",
|
||||
"codex",
|
||||
"--no-recursive",
|
||||
]),
|
||||
).toEqual({
|
||||
kind: "add",
|
||||
root: "/tmp/x",
|
||||
source: "codex",
|
||||
recursive: false,
|
||||
});
|
||||
expect(parseScanRootsCommand(["add", "--root", "/tmp/x"])).toEqual({
|
||||
kind: "add",
|
||||
root: "/tmp/x",
|
||||
recursive: true,
|
||||
});
|
||||
});
|
||||
|
||||
it("defaults a scan to all registered roots", () => {
|
||||
const parsed = parseProductCommand("scan", []);
|
||||
expect(parsed?.plan.target).toBe("all registered roots");
|
||||
});
|
||||
});
|
||||
|
||||
describe("unknown flags are rejected", () => {
|
||||
it("rejects a typo'd flag rather than ignoring it", () => {
|
||||
// A silently ignored --yes would turn a real apply into a plan, and the
|
||||
// user would believe the change had been made.
|
||||
expect(parseProductCommand("scan", ["--yesx"])).toBeNull();
|
||||
expect(parseProductCommand("scan", ["--root"])).toBeNull();
|
||||
expect(parseProductCommand("scan", ["--root", "--yes"])).toBeNull();
|
||||
});
|
||||
|
||||
it("rejects an unknown verb for backup", () => {
|
||||
it("distinguishes remove from add and refuses add-only flags on remove", () => {
|
||||
expect(parseScanRootsCommand(["remove", "--root", "/tmp/x"])).toEqual({
|
||||
kind: "remove",
|
||||
root: "/tmp/x",
|
||||
});
|
||||
expect(
|
||||
parseProductCommand("backup", ["make", "--target", "/tmp/b"]),
|
||||
parseScanRootsCommand(["remove", "--root", "/tmp/x", "--no-recursive"]),
|
||||
).toBeNull();
|
||||
});
|
||||
|
||||
// `path` and `scan` share one parser. Before this guard, an unknown verb was
|
||||
// silently treated as `path add` or a default `scan`, so `--yes` executed a
|
||||
// different operation than the one the caller named.
|
||||
it("rejects an unknown verb for path without turning it into add", () => {
|
||||
expect(
|
||||
parseProductCommand("path", ["frobnicate", "--root", "/tmp/x", "--yes"]),
|
||||
).toBeNull();
|
||||
});
|
||||
|
||||
it("rejects an unknown verb for scan without turning it into all", () => {
|
||||
expect(
|
||||
parseProductCommand("scan", ["frobnicate", "--all", "--yes"]),
|
||||
).toBeNull();
|
||||
expect(parseProductCommand("scan", ["frobnicate", "--yes"])).toBeNull();
|
||||
});
|
||||
|
||||
it("still accepts the documented verbless scan", () => {
|
||||
expect(parseProductCommand("scan", ["--all", "--yes"])?.plan.all).toBe(
|
||||
true,
|
||||
);
|
||||
expect(
|
||||
parseProductCommand("scan", ["--source", "codex"])?.plan.sourceId,
|
||||
).toBe("codex");
|
||||
});
|
||||
|
||||
it("rejects flags that the named verb does not consume", () => {
|
||||
// These were accepted and then silently dropped. An operator who typed
|
||||
// `path remove --source codex` would believe the source filter applied.
|
||||
expect(
|
||||
parseProductCommand("path", [
|
||||
parseScanRootsCommand([
|
||||
"remove",
|
||||
"--root",
|
||||
"/tmp/x",
|
||||
@@ -124,137 +58,103 @@ describe("unknown flags are rejected", () => {
|
||||
"codex",
|
||||
]),
|
||||
).toBeNull();
|
||||
});
|
||||
|
||||
it("is direct, but keeps accepting a legacy --yes", () => {
|
||||
expect(parseScanRootsCommand(["add", "--root", "/tmp/x", "--yes"])).toEqual(
|
||||
{ kind: "add", root: "/tmp/x", recursive: true },
|
||||
);
|
||||
});
|
||||
|
||||
it("rejects unknown verbs, typos and repeated flags", () => {
|
||||
expect(parseScanRootsCommand(["create", "--root", "/tmp/x"])).toBeNull();
|
||||
expect(parseScanRootsCommand(["add", "--rot", "/tmp/x"])).toBeNull();
|
||||
expect(parseScanRootsCommand(["add"])).toBeNull();
|
||||
expect(
|
||||
parseProductCommand("path", [
|
||||
"remove",
|
||||
"--root",
|
||||
"/tmp/x",
|
||||
"--no-recursive",
|
||||
]),
|
||||
parseScanRootsCommand(["add", "--root", "/a", "--root", "/b"]),
|
||||
).toBeNull();
|
||||
expect(
|
||||
parseProductCommand("path", ["add", "--root", "/tmp/x", "--full"]),
|
||||
parseScanRootsCommand(["add", "--root", "/a", "--yes", "--yes"]),
|
||||
).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("scan", () => {
|
||||
it("needs a source: there is no endpoint that scans a bare path", () => {
|
||||
expect(parseScanCommand(["--source", "codex"])).toEqual({
|
||||
kind: "source",
|
||||
sourceId: "codex",
|
||||
});
|
||||
expect(parseScanCommand([])).toBeNull();
|
||||
expect(parseScanCommand(["--root", "/tmp/a"])).toBeNull();
|
||||
});
|
||||
|
||||
it("no longer accepts --full, which was never sent to Core", () => {
|
||||
expect(parseScanCommand(["--source", "codex", "--full"])).toBeNull();
|
||||
});
|
||||
|
||||
it("keeps --all as a hidden alias and a legacy --yes", () => {
|
||||
expect(parseScanCommand(["--all", "--yes"])).toEqual({ kind: "all" });
|
||||
expect(parseScanCommand(["--all", "--source", "codex"])).toBeNull();
|
||||
expect(parseScanCommand(["--source", "codex", "--yes"])).toEqual({
|
||||
kind: "source",
|
||||
sourceId: "codex",
|
||||
});
|
||||
});
|
||||
|
||||
it("rejects a verb, a bad source id and repeated flags", () => {
|
||||
expect(parseScanCommand(["all"])).toBeNull();
|
||||
expect(parseScanCommand(["--source", "Bad Id"])).toBeNull();
|
||||
expect(
|
||||
parseScanCommand(["--source", "codex", "--source", "cursor"]),
|
||||
).toBeNull();
|
||||
expect(
|
||||
parseProductCommand("backup", [
|
||||
"create",
|
||||
"--target",
|
||||
"/tmp/b",
|
||||
"--source",
|
||||
"/tmp/s",
|
||||
]),
|
||||
parseScanCommand(["--source", "codex", "--json", "--json"]),
|
||||
).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("backup", () => {
|
||||
it("creates directly and requires a target", () => {
|
||||
expect(parseBackupCommand(["create", "--target", "/tmp/b"])).toEqual({
|
||||
verb: "create",
|
||||
target: "/tmp/b",
|
||||
});
|
||||
expect(parseBackupCommand(["create"])).toBeNull();
|
||||
expect(
|
||||
parseProductCommand("backup", [
|
||||
"restore",
|
||||
"--source",
|
||||
"/tmp/s",
|
||||
"--target",
|
||||
"/tmp/t",
|
||||
]),
|
||||
parseBackupCommand(["create", "--target", "/tmp/b", "--source", "/x"]),
|
||||
).toBeNull();
|
||||
});
|
||||
|
||||
// A later value silently won before this guard. For `scan --source` that
|
||||
// means one extra shell/make variable can change which source is scanned
|
||||
// while the operator believes both were applied.
|
||||
it("rejects repeated value-bearing flags instead of letting the last one win", () => {
|
||||
it("restores only with --yes and requires a source", () => {
|
||||
expect(parseBackupCommand(["restore", "--source", "/tmp/b"])).toEqual({
|
||||
verb: "restore",
|
||||
source: "/tmp/b",
|
||||
confirmed: false,
|
||||
});
|
||||
expect(
|
||||
parseProductCommand("scan", ["--source", "codex", "--source", "cursor"]),
|
||||
).toBeNull();
|
||||
expect(
|
||||
parseProductCommand("path", [
|
||||
"add",
|
||||
"--root",
|
||||
"/tmp/a",
|
||||
"--root",
|
||||
"/tmp/b",
|
||||
]),
|
||||
).toBeNull();
|
||||
expect(
|
||||
parseProductCommand("backup", [
|
||||
"create",
|
||||
"--target",
|
||||
"/tmp/a",
|
||||
"--target",
|
||||
"/tmp/b",
|
||||
]),
|
||||
).toBeNull();
|
||||
parseBackupCommand(["restore", "--source", "/tmp/b", "--yes"]),
|
||||
).toMatchObject({ confirmed: true });
|
||||
expect(parseBackupCommand(["restore", "--yes"])).toBeNull();
|
||||
});
|
||||
|
||||
it("rejects repeated boolean flags instead of treating them as idempotent", () => {
|
||||
expect(parseProductCommand("scan", ["--all", "--all", "--yes"])).toBeNull();
|
||||
expect(
|
||||
parseProductCommand("scan", ["--full", "--full", "--yes"]),
|
||||
).toBeNull();
|
||||
expect(
|
||||
parseProductCommand("path", [
|
||||
"add",
|
||||
"--root",
|
||||
"/tmp/a",
|
||||
"--no-recursive",
|
||||
"--no-recursive",
|
||||
]),
|
||||
).toBeNull();
|
||||
expect(
|
||||
parseProductCommand("backup", [
|
||||
"create",
|
||||
"--target",
|
||||
"/tmp/a",
|
||||
"--yes",
|
||||
"--yes",
|
||||
]),
|
||||
).toBeNull();
|
||||
});
|
||||
|
||||
it("rejects a repeated global --json spelling", () => {
|
||||
expect(
|
||||
parseProductCommand("scan", ["--root", "/tmp/a", "--json", "--json"]),
|
||||
).toBeNull();
|
||||
expect(
|
||||
parseProductCommand("backup", [
|
||||
"create",
|
||||
"--target",
|
||||
"/tmp/a",
|
||||
"--json",
|
||||
"--json",
|
||||
]),
|
||||
).toBeNull();
|
||||
});
|
||||
|
||||
it("requires a source when restoring", () => {
|
||||
expect(
|
||||
parseProductCommand("backup", ["restore", "--target", "/tmp/b"]),
|
||||
).toBeNull();
|
||||
});
|
||||
|
||||
it("parses the explicit restore confirmation path", () => {
|
||||
const parsed = parseProductCommand("backup", [
|
||||
it("accepts the legacy --confirm <path> and checks it lexically", () => {
|
||||
const parsed = parseBackupCommand([
|
||||
"restore",
|
||||
"--source",
|
||||
"/tmp/backups/a.sqlite3",
|
||||
"/tmp/b",
|
||||
"--yes",
|
||||
"--confirm",
|
||||
"/tmp/backups/a.sqlite3",
|
||||
"/tmp/b",
|
||||
]);
|
||||
expect(parsed).not.toBeNull();
|
||||
expect(parsed?.confirmPath).toBe("/tmp/backups/a.sqlite3");
|
||||
expect(
|
||||
restoreConfirmationMatches("/tmp/backups/a.sqlite3", parsed?.confirmPath),
|
||||
).toBe(true);
|
||||
expect(
|
||||
restoreConfirmationMatches("/tmp/backups/a.sqlite3", undefined),
|
||||
).toBe(false);
|
||||
expect(
|
||||
restoreConfirmationMatches(
|
||||
"/tmp/backups/a.sqlite3",
|
||||
"/tmp/backups/../backups/a.sqlite3",
|
||||
),
|
||||
).toBe(false);
|
||||
expect(parsed).toMatchObject({ confirmPath: "/tmp/b" });
|
||||
expect(restoreConfirmationMatches("/tmp/b", "/tmp/b")).toBe(true);
|
||||
expect(restoreConfirmationMatches("/tmp/b", undefined)).toBe(true);
|
||||
expect(restoreConfirmationMatches("/tmp/b", "/tmp/./b")).toBe(false);
|
||||
});
|
||||
|
||||
it("requires a target when creating a backup", () => {
|
||||
expect(parseProductCommand("backup", ["create"])).toBeNull();
|
||||
it("rejects an unknown verb", () => {
|
||||
expect(parseBackupCommand(["delete", "--target", "/tmp/b"])).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -305,11 +205,19 @@ describe("write", () => {
|
||||
).toBeNull();
|
||||
});
|
||||
|
||||
it("parses status under write", () => {
|
||||
it("parses status under write, positional or --id", () => {
|
||||
expect(parseWriteCommand(["status", "--id", "request-1"])).toEqual({
|
||||
kind: "status",
|
||||
id: "request-1",
|
||||
});
|
||||
expect(parseWriteCommand(["status", "request-1"])).toEqual({
|
||||
kind: "status",
|
||||
id: "request-1",
|
||||
});
|
||||
});
|
||||
|
||||
it("rejects a repeated --session instead of letting the last one win", () => {
|
||||
expect(parseWriteCommand([...base, "--session", "s2"])).toBeNull();
|
||||
});
|
||||
|
||||
it("marks --live as priority live", () => {
|
||||
@@ -366,53 +274,3 @@ describe("task", () => {
|
||||
expect(parseTaskCommand(["--id", "req-1", "--json", "--json"])).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("path add and remove carry what the endpoint needs", () => {
|
||||
it("reads the source and recursive flag off the path add", () => {
|
||||
const parsed = parseProductCommand("path", [
|
||||
"add",
|
||||
"--root",
|
||||
"/tmp/scanme",
|
||||
"--source",
|
||||
"codex",
|
||||
"--no-recursive",
|
||||
]);
|
||||
// `?.plan!` let a null parse pass silently, so assert the parse itself.
|
||||
if (parsed === null) {
|
||||
throw new Error("path add must parse");
|
||||
}
|
||||
expect(parsed.plan.action).toBe("add scan root");
|
||||
expect(parsed.plan.target).toBe("/tmp/scanme");
|
||||
expect(parsed.plan.detail).toContain("source=codex");
|
||||
expect(parsed.plan.detail).toContain("recursive=false");
|
||||
});
|
||||
|
||||
it("distinguishes remove from add", () => {
|
||||
const parsed = parseProductCommand("path", [
|
||||
"remove",
|
||||
"--root",
|
||||
"/tmp/scanme",
|
||||
]);
|
||||
if (parsed === null) {
|
||||
throw new Error("path remove must parse");
|
||||
}
|
||||
// The action is the discriminator at apply time: add and remove share a
|
||||
// `--root`, so a mis-set action would register the directory it meant to delete.
|
||||
expect(parsed.plan.action).toBe("remove scan root");
|
||||
});
|
||||
|
||||
it("does not silently drop --yes", () => {
|
||||
const parsed = parseProductCommand("path", [
|
||||
"add",
|
||||
"--root",
|
||||
"/tmp/a",
|
||||
"--yes",
|
||||
]);
|
||||
expect(parsed?.confirmed).toBe(true);
|
||||
// Without this, a typo'd `--yes` would fall through to a plan and the user
|
||||
// would believe the change had been made.
|
||||
|
||||
const without = parseProductCommand("path", ["add", "--root", "/tmp/a"]);
|
||||
expect(without?.confirmed).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -1,258 +1,163 @@
|
||||
// A6: the product commands Runtime exposes on the CLI.
|
||||
// Parsers for the commands that attach to the Runtime directly:
|
||||
// `scan-roots` (alias `path`), `scan`, `backup`, `write`.
|
||||
//
|
||||
// These are the six commands the objective names: path, adapter, scan, write,
|
||||
// task, backup. Two rules run through all of them:
|
||||
// Rules shared by all of them:
|
||||
//
|
||||
// - **Mutating commands are two-phase.** Without `--yes` they print a plan and
|
||||
// change nothing. This is the convention the existing install/service
|
||||
// commands already follow, and it is why `scan` and `backup` default to a
|
||||
// plan rather than quietly doing work.
|
||||
// - **Output is machine-readable.** `--json` emits one document on stdout so an
|
||||
// agent can consume it; the human form is text. Never both, so a caller that
|
||||
// parses stdout is not surprised by prose.
|
||||
// - **Only irreversible operations ask for `--yes`.** `backup restore`
|
||||
// overwrites the database, so without `--yes` it previews and changes
|
||||
// nothing. Registering a scan root, scanning a source, creating a backup and
|
||||
// submitting a write are direct. A legacy `--yes` on a direct command is
|
||||
// accepted and ignored so older scripts keep working.
|
||||
// - **Unknown or repeated flags are rejected**, never ignored: a dropped typo
|
||||
// would silently change what the command does.
|
||||
// - **Output is machine-readable.** `--json` emits one document on stdout.
|
||||
//
|
||||
// The commands talk to the Runtime's HTTP API, not to the database and not to
|
||||
// HY. That is the A6 rule: CLI and Electron are clients of the same Runtime.
|
||||
// the SDK: CLI and desktop are clients of the same Runtime.
|
||||
|
||||
/** What a mutating command would do, before it is allowed to do it. */
|
||||
export interface Plan {
|
||||
readonly action: string;
|
||||
readonly target: string;
|
||||
readonly detail?: string;
|
||||
/** Which form of the command this plan is for, when one name has several.
|
||||
*
|
||||
* `backup` has `create` and `restore`, and applying the wrong one is not a
|
||||
* harmless mistake — so the verb travels with the plan the user confirmed.
|
||||
*/
|
||||
readonly verb?: string;
|
||||
/** The source the Runtime endpoint addresses, when the plan names one.
|
||||
*
|
||||
* Carried on the plan rather than re-parsed at apply time so the plan the
|
||||
* user confirmed and the request actually sent cannot disagree.
|
||||
*/
|
||||
readonly sourceId?: string;
|
||||
/** `scan --all`: every source Runtime actually found, not a named one. */
|
||||
readonly all?: boolean;
|
||||
export type ScanRootsCommand =
|
||||
| {
|
||||
readonly kind: "add";
|
||||
readonly root: string;
|
||||
readonly source?: string;
|
||||
readonly recursive: boolean;
|
||||
}
|
||||
| { readonly kind: "remove"; readonly root: string };
|
||||
|
||||
export type ScanCommand =
|
||||
| { readonly kind: "source"; readonly sourceId: string }
|
||||
/** Hidden alias of `sources connect --all-detected --import`. */
|
||||
| { readonly kind: "all" };
|
||||
|
||||
export type BackupCommand =
|
||||
| { readonly verb: "create"; readonly target: string }
|
||||
| {
|
||||
readonly verb: "restore";
|
||||
readonly source: string;
|
||||
readonly confirmed: boolean;
|
||||
/** Legacy `--confirm <path>`: accepted, and must repeat `--source`. */
|
||||
readonly confirmPath?: string;
|
||||
};
|
||||
|
||||
interface ParsedFlags {
|
||||
readonly values: Map<string, string>;
|
||||
readonly switches: Set<string>;
|
||||
}
|
||||
|
||||
export function planAddRoot(
|
||||
root: string,
|
||||
source: string | undefined,
|
||||
recursive: boolean,
|
||||
): Plan {
|
||||
return {
|
||||
action: "add scan root",
|
||||
target: root,
|
||||
detail: `source=${source ?? "auto"} recursive=${recursive}`,
|
||||
};
|
||||
}
|
||||
|
||||
export function planRemoveRoot(root: string): Plan {
|
||||
return { action: "remove scan root", target: root };
|
||||
}
|
||||
|
||||
export function planScan(root: string | undefined, full: boolean): Plan {
|
||||
return {
|
||||
action: "scan",
|
||||
target: root ?? "all registered roots",
|
||||
detail: `full=${full}`,
|
||||
};
|
||||
}
|
||||
|
||||
export function planScanSource(sourceId: string, full: boolean): Plan {
|
||||
return {
|
||||
action: "scan",
|
||||
target: `source ${sourceId}`,
|
||||
detail: `full=${full}`,
|
||||
sourceId,
|
||||
};
|
||||
}
|
||||
|
||||
export function planScanAll(full: boolean): Plan {
|
||||
return {
|
||||
action: "scan",
|
||||
target: "all detected sources",
|
||||
detail: `full=${full}`,
|
||||
all: true,
|
||||
};
|
||||
}
|
||||
|
||||
export function planBackup(target: string): Plan {
|
||||
return { action: "create backup", target, verb: "create" };
|
||||
}
|
||||
|
||||
export function planRestore(source: string): Plan {
|
||||
return { action: "restore backup", target: source, verb: "restore" };
|
||||
}
|
||||
|
||||
export interface ProductCommand {
|
||||
readonly plan: Plan;
|
||||
readonly confirmed: boolean;
|
||||
/** For destructive verbs with a named artifact, the exact value repeated by
|
||||
* the user. `backup restore` requires this to match `--source`. */
|
||||
readonly confirmPath?: string;
|
||||
}
|
||||
|
||||
/** Parse a mutating product command.
|
||||
*
|
||||
* Unknown flags are rejected rather than ignored: a typo'd `--yes` that is
|
||||
* silently dropped would turn a real apply into a plan, and the user would
|
||||
* believe the change had been made.
|
||||
*/
|
||||
export function parseProductCommand(
|
||||
name: string,
|
||||
/** Read `--flag value` pairs and bare switches; null on anything unknown,
|
||||
* repeated or missing its value. */
|
||||
function parseFlags(
|
||||
args: readonly string[],
|
||||
): ProductCommand | null {
|
||||
let root: string | undefined;
|
||||
let source: string | undefined;
|
||||
let target: string | undefined;
|
||||
let recursive = true;
|
||||
let full = false;
|
||||
let all = false;
|
||||
let confirmed = false;
|
||||
let confirmPath: string | undefined;
|
||||
const seen = new Set<string>();
|
||||
const seenValues = new Set<string>();
|
||||
|
||||
const flags = new Set([
|
||||
"--root",
|
||||
"--source",
|
||||
"--target",
|
||||
"--confirm",
|
||||
"--no-recursive",
|
||||
"--full",
|
||||
"--all",
|
||||
"--yes",
|
||||
"--json",
|
||||
]);
|
||||
|
||||
// The first token is the verb for commands that have one (`path add`,
|
||||
// `backup create`). It used to fall into the flag loop and be rejected as an
|
||||
// unknown flag, which made every one of these commands permanently
|
||||
// unparseable — `path add --root X` printed "参数无效" even though `help`
|
||||
// documented it. The tests missed this because they asserted
|
||||
// `parsed?.plan!` and null satisfies `?.` + `!`.
|
||||
const verb = args[0];
|
||||
const start = verb !== undefined && !verb.startsWith("--") ? 1 : 0;
|
||||
|
||||
for (let index = start; index < args.length; index += 1) {
|
||||
valueFlags: readonly string[],
|
||||
switchFlags: readonly string[],
|
||||
): ParsedFlags | null {
|
||||
const values = new Map<string, string>();
|
||||
const switches = new Set<string>();
|
||||
for (let index = 0; index < args.length; index += 1) {
|
||||
const arg = args[index];
|
||||
if (!flags.has(arg)) return null;
|
||||
// A repeated flag is a malformed argv, not an idempotent spelling. The
|
||||
// other command parsers already apply this rule to --json as well.
|
||||
if (seen.has(arg)) return null;
|
||||
seen.add(arg);
|
||||
if (arg === "--yes") {
|
||||
confirmed = true;
|
||||
continue;
|
||||
}
|
||||
if (arg === "--confirm") {
|
||||
const value = args[index + 1];
|
||||
if (value === undefined || value.startsWith("--")) return null;
|
||||
if (confirmPath !== undefined) return null;
|
||||
confirmPath = value;
|
||||
index += 1;
|
||||
continue;
|
||||
}
|
||||
if (arg === "--json") continue;
|
||||
if (arg === "--no-recursive") {
|
||||
recursive = false;
|
||||
continue;
|
||||
}
|
||||
if (arg === "--full") {
|
||||
full = true;
|
||||
continue;
|
||||
}
|
||||
if (arg === "--all") {
|
||||
all = true;
|
||||
if (switchFlags.includes(arg)) {
|
||||
if (switches.has(arg)) return null;
|
||||
switches.add(arg);
|
||||
continue;
|
||||
}
|
||||
if (!valueFlags.includes(arg) || values.has(arg)) return null;
|
||||
const value = args[index + 1];
|
||||
if (value === undefined || value.startsWith("--")) return null;
|
||||
if (seenValues.has(arg)) return null;
|
||||
seenValues.add(arg);
|
||||
values.set(arg, value);
|
||||
index += 1;
|
||||
if (arg === "--root") root = value;
|
||||
else if (arg === "--source") source = value;
|
||||
else target = value;
|
||||
}
|
||||
return { values, switches };
|
||||
}
|
||||
|
||||
if (name === "path") {
|
||||
if (confirmPath !== undefined) return null;
|
||||
if (target !== undefined) return null;
|
||||
if (root === undefined) return null;
|
||||
if (source === undefined && args.includes("--source")) return null;
|
||||
if (seen.has("--full") || seen.has("--all")) return null;
|
||||
if (args[0] !== "add" && args[0] !== "remove") return null;
|
||||
if (
|
||||
args[0] === "remove" &&
|
||||
(seen.has("--source") || seen.has("--no-recursive"))
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
const SOURCE_ID = /^[a-z0-9][a-z0-9-]{0,127}$/;
|
||||
|
||||
/** `scan-roots add --root <path> [--source <id>] [--no-recursive]` and
|
||||
* `scan-roots remove --root <path>` (`list` is answered separately). */
|
||||
export function parseScanRootsCommand(
|
||||
args: readonly string[],
|
||||
): ScanRootsCommand | null {
|
||||
const verb = args[0];
|
||||
if (verb === "add") {
|
||||
const flags = parseFlags(
|
||||
args.slice(1),
|
||||
["--root", "--source"],
|
||||
["--no-recursive", "--yes", "--json"],
|
||||
);
|
||||
const root = flags?.values.get("--root");
|
||||
if (!flags || root === undefined) return null;
|
||||
const source = flags.values.get("--source");
|
||||
if (source !== undefined && !SOURCE_ID.test(source)) return null;
|
||||
return {
|
||||
plan:
|
||||
args[0] === "add"
|
||||
? planAddRoot(root, source, recursive)
|
||||
: planRemoveRoot(root),
|
||||
confirmed,
|
||||
kind: "add",
|
||||
root,
|
||||
...(source === undefined ? {} : { source }),
|
||||
recursive: !flags.switches.has("--no-recursive"),
|
||||
};
|
||||
}
|
||||
if (name === "scan") {
|
||||
if (confirmPath !== undefined) return null;
|
||||
if (target !== undefined) return null;
|
||||
if (seen.has("--no-recursive")) return null;
|
||||
if (args[0] !== undefined && !args[0].startsWith("--")) return null;
|
||||
// `--all` means "every source Runtime actually found", so naming one too
|
||||
// would make the command mean two different things.
|
||||
if (all && source !== undefined) return null;
|
||||
if (all) return { plan: planScanAll(full), confirmed };
|
||||
// A source is the thing the Runtime scan endpoint addresses. A bare root is
|
||||
// still accepted for planning (it means "all registered roots") but cannot
|
||||
// be applied, because there is no endpoint that scans a raw path — the CLI
|
||||
// must not pretend it can.
|
||||
if (source === undefined) return { plan: planScan(root, full), confirmed };
|
||||
return { plan: planScanSource(source, full), confirmed };
|
||||
if (verb === "remove") {
|
||||
const flags = parseFlags(args.slice(1), ["--root"], ["--yes", "--json"]);
|
||||
const root = flags?.values.get("--root");
|
||||
return flags && root !== undefined ? { kind: "remove", root } : null;
|
||||
}
|
||||
if (name === "backup") {
|
||||
const verb = args[0];
|
||||
if (verb !== "create" && verb !== "restore") return null;
|
||||
if (seen.has("--full") || seen.has("--all") || seen.has("--no-recursive")) {
|
||||
return null;
|
||||
}
|
||||
if (verb === "restore") {
|
||||
if (source === undefined) return null;
|
||||
if (target !== undefined) return null;
|
||||
return {
|
||||
plan: planRestore(source),
|
||||
confirmed,
|
||||
...(confirmPath === undefined ? {} : { confirmPath }),
|
||||
};
|
||||
}
|
||||
if (confirmPath !== undefined) return null;
|
||||
if (source !== undefined) return null;
|
||||
if (target === undefined) return null;
|
||||
return null;
|
||||
}
|
||||
|
||||
/** `scan --source <id>`. A scan names a source: the Runtime has no endpoint
|
||||
* that scans a bare path, so a missing `--source` is a usage error rather
|
||||
* than a preview of something that can never run. */
|
||||
export function parseScanCommand(args: readonly string[]): ScanCommand | null {
|
||||
const flags = parseFlags(args, ["--source"], ["--all", "--yes", "--json"]);
|
||||
if (!flags) return null;
|
||||
const source = flags.values.get("--source");
|
||||
if (flags.switches.has("--all")) {
|
||||
return source === undefined ? { kind: "all" } : null;
|
||||
}
|
||||
return source !== undefined && SOURCE_ID.test(source)
|
||||
? { kind: "source", sourceId: source }
|
||||
: null;
|
||||
}
|
||||
|
||||
/** `backup create --target <path>` and
|
||||
* `backup restore --source <path> [--yes]`. */
|
||||
export function parseBackupCommand(
|
||||
args: readonly string[],
|
||||
): BackupCommand | null {
|
||||
const verb = args[0];
|
||||
if (verb === "create") {
|
||||
const flags = parseFlags(args.slice(1), ["--target"], ["--yes", "--json"]);
|
||||
const target = flags?.values.get("--target");
|
||||
return flags && target !== undefined ? { verb: "create", target } : null;
|
||||
}
|
||||
if (verb === "restore") {
|
||||
const flags = parseFlags(
|
||||
args.slice(1),
|
||||
["--source", "--confirm"],
|
||||
["--yes", "--json"],
|
||||
);
|
||||
const source = flags?.values.get("--source");
|
||||
if (!flags || source === undefined) return null;
|
||||
const confirmPath = flags.values.get("--confirm");
|
||||
return {
|
||||
plan: planBackup(target),
|
||||
confirmed,
|
||||
verb: "restore",
|
||||
source,
|
||||
confirmed: flags.switches.has("--yes"),
|
||||
...(confirmPath === undefined ? {} : { confirmPath }),
|
||||
};
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Whether `--confirm <path>` names the same restore source.
|
||||
/** Whether a legacy `--confirm <path>` names the same restore source.
|
||||
*
|
||||
* The comparison is lexical on purpose: the CLI must not resolve symlinks or
|
||||
* touch the filesystem before Core has validated the managed source. A
|
||||
* different string is a different confirmation, so a typo or a path escape
|
||||
* can never be applied as a side effect of `--yes`.
|
||||
* different string is a different confirmation.
|
||||
*/
|
||||
export function restoreConfirmationMatches(
|
||||
source: string,
|
||||
confirmPath: string | undefined,
|
||||
): boolean {
|
||||
return confirmPath === source;
|
||||
return confirmPath === undefined || confirmPath === source;
|
||||
}
|
||||
|
||||
export interface WriteSubmitCommand {
|
||||
@@ -272,41 +177,33 @@ export interface WriteStatusCommand {
|
||||
|
||||
export type WriteCommand = WriteSubmitCommand | WriteStatusCommand;
|
||||
|
||||
/** Parse SDK-native `write submit|status`. */
|
||||
/** Parse `write submit ...` and `write status <request-id>` (or `--id`). */
|
||||
export function parseWriteCommand(
|
||||
args: readonly string[],
|
||||
): WriteCommand | null {
|
||||
if (args.filter((item) => item === "--json").length > 1) return null;
|
||||
const meaningful = args.filter((item) => item !== "--json");
|
||||
if (meaningful[0] === "status") {
|
||||
const status = parseTaskCommand(meaningful.slice(1));
|
||||
const rest = meaningful.slice(1);
|
||||
if (rest.length === 1 && !rest[0].startsWith("--")) {
|
||||
return { kind: "status", id: rest[0] };
|
||||
}
|
||||
const status = parseTaskCommand(rest);
|
||||
return status === null ? null : { kind: "status", id: status.id };
|
||||
}
|
||||
// `write --session ...` without `submit` stays accepted for older scripts.
|
||||
const input = meaningful[0] === "submit" ? meaningful.slice(1) : meaningful;
|
||||
let session: string | undefined;
|
||||
let source = "memhub-cli";
|
||||
let messagesFile: string | undefined;
|
||||
let requestId: string | undefined;
|
||||
let memoryAt: string | undefined;
|
||||
let priority: "live" | "normal" | undefined;
|
||||
|
||||
for (let index = 0; index < input.length; index += 1) {
|
||||
const arg = input[index];
|
||||
if (arg === "--live") {
|
||||
priority = "live";
|
||||
continue;
|
||||
}
|
||||
const value = input[index + 1];
|
||||
if (value === undefined || value.startsWith("--")) return null;
|
||||
index += 1;
|
||||
if (arg === "--session") session = value;
|
||||
else if (arg === "--source") source = value;
|
||||
else if (arg === "--messages-file") messagesFile = value;
|
||||
else if (arg === "--request-id") requestId = value;
|
||||
else if (arg === "--memory-at") memoryAt = value;
|
||||
else return null;
|
||||
}
|
||||
|
||||
const flags = parseFlags(
|
||||
input,
|
||||
["--session", "--source", "--messages-file", "--request-id", "--memory-at"],
|
||||
["--live"],
|
||||
);
|
||||
if (!flags) return null;
|
||||
const session = flags.values.get("--session");
|
||||
const source = flags.values.get("--source") ?? "memhub-cli";
|
||||
const messagesFile = flags.values.get("--messages-file");
|
||||
const requestId = flags.values.get("--request-id");
|
||||
const memoryAt = flags.values.get("--memory-at");
|
||||
if (session === undefined || messagesFile === undefined || !source)
|
||||
return null;
|
||||
if (
|
||||
@@ -323,39 +220,23 @@ export function parseWriteCommand(
|
||||
...(requestId === undefined ? {} : { request_id: requestId }),
|
||||
...(memoryAt === undefined ? {} : { memory_at: memoryAt }),
|
||||
};
|
||||
return priority === undefined ? command : { ...command, priority };
|
||||
return flags.switches.has("--live")
|
||||
? { ...command, priority: "live" }
|
||||
: command;
|
||||
}
|
||||
|
||||
export interface TaskCommand {
|
||||
readonly id: string;
|
||||
}
|
||||
|
||||
/** `--id <request-id>`: the legacy `task` spelling of `write status`. */
|
||||
export function parseTaskCommand(args: readonly string[]): TaskCommand | null {
|
||||
if (args.filter((item) => item === "--json").length > 1) return null;
|
||||
let id: string | undefined;
|
||||
for (let index = 0; index < args.length; index += 1) {
|
||||
const arg = args[index];
|
||||
if (arg === "--json") continue;
|
||||
if (arg !== "--id") return null;
|
||||
const value = args[index + 1];
|
||||
if (value === undefined || value.startsWith("--")) return null;
|
||||
index += 1;
|
||||
id = value;
|
||||
}
|
||||
const flags = parseFlags(
|
||||
args.filter((item) => item !== "--json"),
|
||||
["--id"],
|
||||
[],
|
||||
);
|
||||
const id = flags?.values.get("--id");
|
||||
return id === undefined ? null : { id };
|
||||
}
|
||||
|
||||
/** The human form. Kept separate from the JSON form on purpose. */
|
||||
export function describe(plan: Plan): string {
|
||||
const line = `${plan.action}: ${plan.target}`;
|
||||
return plan.detail ? `${line} (${plan.detail})` : line;
|
||||
}
|
||||
|
||||
/** The machine form: one document, nothing else on stdout. */
|
||||
export function asPlanDocument(plan: Plan): {
|
||||
schema_version: 1;
|
||||
plan: Plan;
|
||||
applied: boolean;
|
||||
} {
|
||||
return { schema_version: 1, plan, applied: false };
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
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(),
|
||||
}));
|
||||
vi.mock("axios", () => ({
|
||||
default: {
|
||||
create: vi.fn(() => transport),
|
||||
get: vi.fn(),
|
||||
post: vi.fn(),
|
||||
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 VERSIONS = {
|
||||
user_id: "wife",
|
||||
request_id: "r-versions",
|
||||
elapsed_ms: 2,
|
||||
chains: [
|
||||
{
|
||||
memory_id: "l0-head",
|
||||
content: "The user's name is Fei.",
|
||||
status: "active",
|
||||
memory_at: null,
|
||||
gmt_created: 1_790_000_100,
|
||||
basic_info: { name: "Fei" },
|
||||
versions: [
|
||||
{
|
||||
memory_id: "l0-head",
|
||||
content: "The user's name is Fei.",
|
||||
memory_at: null,
|
||||
gmt_created: 1_790_000_100,
|
||||
current: true,
|
||||
},
|
||||
{
|
||||
memory_id: "l0-old",
|
||||
content: "The user's name is 费天翔.",
|
||||
memory_at: null,
|
||||
gmt_created: 1_790_000_000,
|
||||
current: false,
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
for (const method of Object.values(transport)) 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) =>
|
||||
url === "/api/v2/user-profile/versions"
|
||||
? { status: 200, data: VERSIONS }
|
||||
: { status: 404, data: {} },
|
||||
);
|
||||
});
|
||||
|
||||
describe("memhub profile --versions", () => {
|
||||
it("reads the L0 version chain for the named user", async () => {
|
||||
const result = await capturedRun([
|
||||
"profile",
|
||||
"--versions",
|
||||
"--user-id",
|
||||
"wife",
|
||||
"--json",
|
||||
]);
|
||||
expect(result.code).toBe(0);
|
||||
expect(transport.get).toHaveBeenCalledWith(
|
||||
"/api/v2/user-profile/versions",
|
||||
{ params: { user_id: "wife" } },
|
||||
);
|
||||
const document = JSON.parse(result.stdout);
|
||||
expect(document.state).toBe("ok");
|
||||
expect(document.chains[0].versions).toHaveLength(2);
|
||||
});
|
||||
|
||||
it("marks the current version in text and names no user for local", async () => {
|
||||
const result = await capturedRun(["profile", "--versions"]);
|
||||
expect(result.code).toBe(0);
|
||||
const [url, config] = transport.get.mock.calls[0] ?? [];
|
||||
expect(url).toBe("/api/v2/user-profile/versions");
|
||||
expect(config?.params?.user_id).toBeUndefined();
|
||||
expect(result.stdout).toContain("* l0-head");
|
||||
expect(result.stdout).toContain(" l0-old");
|
||||
expect(result.stdout).toContain("费天翔");
|
||||
});
|
||||
|
||||
it("refuses an unknown flag and a repeated --versions", async () => {
|
||||
expect((await capturedRun(["profile", "--version-chain"])).code).toBe(2);
|
||||
expect(
|
||||
(await capturedRun(["profile", "--versions", "--versions"])).code,
|
||||
).toBe(2);
|
||||
expect(transport.get).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("reports a malformed answer as invalid", async () => {
|
||||
transport.get.mockResolvedValue({ status: 200, data: { chains: "x" } });
|
||||
const result = await capturedRun(["profile", "--versions", "--json"]);
|
||||
expect(result.code).toBe(1);
|
||||
expect(JSON.parse(result.stdout).code).toBe("response_invalid");
|
||||
});
|
||||
});
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -878,8 +878,11 @@ describe("CLI-21 memory reads", () => {
|
||||
layer: "L2_FACT",
|
||||
status: "active",
|
||||
});
|
||||
expect(report.evidence).toEqual([]);
|
||||
expect(report.history).toEqual([]);
|
||||
// Schema 2, like add/update/delete; the v1-era always-empty
|
||||
// `evidence`/`history` fields are gone.
|
||||
expect(report.schema_version).toBe(2);
|
||||
expect(report).not.toHaveProperty("evidence");
|
||||
expect(report).not.toHaveProperty("history");
|
||||
});
|
||||
|
||||
// Core registers /api/v2/memories/graph before /{memory_id}, and that static
|
||||
@@ -1137,14 +1140,16 @@ describe("backup create target refusal is rejected, not usage_error", () => {
|
||||
let code: number;
|
||||
try {
|
||||
code = await run(
|
||||
["backup", "create", "--target", "/tmp/outside.db", "--yes", "--json"],
|
||||
["backup", "create", "--target", "/tmp/outside.db", "--json"],
|
||||
{},
|
||||
);
|
||||
} finally {
|
||||
streams.restore();
|
||||
}
|
||||
expect(code!).toBe(1);
|
||||
const document = JSON.parse(streams.stderr());
|
||||
// A result, on stdout like every --json document.
|
||||
expect(streams.stderr()).toBe("");
|
||||
const document = JSON.parse(streams.stdout());
|
||||
expect(document.status).not.toBe("usage_error");
|
||||
expect(document).toMatchObject({
|
||||
command: "backup",
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
@@ -341,7 +424,11 @@ function validated<T>(
|
||||
):
|
||||
| { state: "ok"; data: T }
|
||||
| { state: Exclude<ReadState["state"], "ok">; code: string } {
|
||||
if (outcome.state !== "ok") return outcome;
|
||||
if (outcome.state !== "ok") {
|
||||
// A Core refusal of the request itself reads as an invalid answer here.
|
||||
const state = outcome.state === "rejected" ? "invalid" : outcome.state;
|
||||
return { state, code: outcome.code };
|
||||
}
|
||||
const data = validate(outcome.data);
|
||||
return data === null
|
||||
? { state: "invalid", code: "response_invalid" }
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -102,6 +102,10 @@ export function uiSessionFailure(code: string): { code: string } {
|
||||
|
||||
export function uiExitCode(result: UiResult): number {
|
||||
if (result.state === "opened") return 0;
|
||||
// A stopped Runtime is the same exit 3 every other command reports.
|
||||
if (result.state === "blocked" && result.code === "runtime_not_running") {
|
||||
return 3;
|
||||
}
|
||||
if (result.state === "blocked") return 4;
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -1,479 +0,0 @@
|
||||
import { mkdtemp, writeFile } from "node:fs/promises";
|
||||
import { 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(),
|
||||
}));
|
||||
vi.mock("axios", () => ({
|
||||
default: { create: vi.fn(() => transport), get: vi.fn() },
|
||||
}));
|
||||
|
||||
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 EVENT = "11111111-1111-4111-8111-111111111111";
|
||||
const UPLOAD = "22222222-2222-4222-8222-222222222222";
|
||||
const NOTE = "33333333-3333-4333-8333-333333333333";
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
// clearAllMocks keeps prior mock implementations, so a mockResolvedValue
|
||||
// from an earlier test would leak into the next one and fake its response.
|
||||
transport.get.mockReset();
|
||||
transport.post.mockReset();
|
||||
transport.put.mockReset();
|
||||
transport.patch.mockReset();
|
||||
transport.delete.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",
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
describe("calls clear", () => {
|
||||
it("reports runtime_stopped (exit 3) without attempting a write", async () => {
|
||||
runtime.inspectRuntimeStatus.mockResolvedValue({
|
||||
state: "stopped",
|
||||
code: "runtime_not_running",
|
||||
});
|
||||
const result = await capturedRun([
|
||||
"calls",
|
||||
"clear",
|
||||
"--all",
|
||||
"--confirm",
|
||||
"--json",
|
||||
]);
|
||||
expect(result.code).toBe(3);
|
||||
expect(JSON.parse(result.stdout).code).toBe("runtime_not_running");
|
||||
expect(transport.post).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("previews without contacting Core", async () => {
|
||||
const result = await capturedRun([
|
||||
"calls",
|
||||
"clear",
|
||||
"--event",
|
||||
EVENT,
|
||||
"--json",
|
||||
]);
|
||||
expect(result.code).toBe(0);
|
||||
const document = JSON.parse(result.stdout);
|
||||
expect(document.state).toBe("preview");
|
||||
expect(document.plan.kind).toBe("call_events_clear");
|
||||
expect(transport.post).not.toHaveBeenCalled();
|
||||
expect(transport.delete).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("sends the exact Core body with --confirm", async () => {
|
||||
transport.post.mockResolvedValue({ status: 200, data: { removed: 1 } });
|
||||
const result = await capturedRun([
|
||||
"calls",
|
||||
"clear",
|
||||
"--event",
|
||||
EVENT,
|
||||
"--confirm",
|
||||
"--json",
|
||||
]);
|
||||
expect(result.code).toBe(0);
|
||||
expect(transport.post).toHaveBeenCalledWith(
|
||||
"/api/v1/call-events/clear",
|
||||
{ event_ids: [EVENT], user_confirmed: true },
|
||||
undefined,
|
||||
);
|
||||
expect(JSON.parse(result.stdout).state).toBe("committed");
|
||||
});
|
||||
|
||||
it("clears all without naming events", async () => {
|
||||
transport.post.mockResolvedValue({ status: 200, data: { removed: 4 } });
|
||||
const result = await capturedRun([
|
||||
"calls",
|
||||
"clear",
|
||||
"--all",
|
||||
"--confirm",
|
||||
"--json",
|
||||
]);
|
||||
expect(result.code).toBe(0);
|
||||
expect(transport.post).toHaveBeenCalledWith(
|
||||
"/api/v1/call-events/clear-all",
|
||||
{ user_confirmed: true },
|
||||
undefined,
|
||||
);
|
||||
});
|
||||
|
||||
it("rejects a malformed event id without contacting Core", async () => {
|
||||
const result = await capturedRun([
|
||||
"calls",
|
||||
"clear",
|
||||
"--event",
|
||||
"not-a-uuid",
|
||||
"--json",
|
||||
]);
|
||||
expect(result.code).toBe(2);
|
||||
expect(transport.post).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("never reports committed for a malformed success body", async () => {
|
||||
transport.post.mockResolvedValue({ status: 200, data: { wrong: true } });
|
||||
const result = await capturedRun([
|
||||
"calls",
|
||||
"clear",
|
||||
"--event",
|
||||
EVENT,
|
||||
"--confirm",
|
||||
"--json",
|
||||
]);
|
||||
expect(result.code).toBe(1);
|
||||
const document = JSON.parse(result.stdout);
|
||||
expect(document.state).toBe("unknown");
|
||||
expect(document.code).toBe("ui_write_receipt_invalid");
|
||||
});
|
||||
});
|
||||
|
||||
describe.skip("retired import-file", () => {
|
||||
it("never uploads without --confirm", async () => {
|
||||
const directory = await mkdtemp(path.join(tmpdir(), "om-import-"));
|
||||
const file = path.join(directory, "export.json");
|
||||
await writeFile(file, '{"conversations":[]}', { mode: 0o600 });
|
||||
const result = await capturedRun([
|
||||
"import-file",
|
||||
"upload",
|
||||
"--file",
|
||||
file,
|
||||
"--json",
|
||||
]);
|
||||
expect(result.code).toBe(0);
|
||||
const document = JSON.parse(result.stdout);
|
||||
expect(document.state).toBe("preview");
|
||||
expect(document.plan.operation_id).toBeDefined();
|
||||
expect(transport.post).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("uploads the file with a deterministic operation id on confirm", async () => {
|
||||
const directory = await mkdtemp(path.join(tmpdir(), "om-import-"));
|
||||
const file = path.join(directory, "export.json");
|
||||
await writeFile(file, '{"conversations":[]}', { mode: 0o600 });
|
||||
transport.post.mockResolvedValue({
|
||||
status: 201,
|
||||
data: {
|
||||
schema_version: 1,
|
||||
operation_id: "ignored-here",
|
||||
state: "completed",
|
||||
upload_id: UPLOAD,
|
||||
upload_state: "staged",
|
||||
},
|
||||
});
|
||||
const preview = await capturedRun([
|
||||
"import-file",
|
||||
"upload",
|
||||
"--file",
|
||||
file,
|
||||
"--json",
|
||||
]);
|
||||
const plan = JSON.parse(preview.stdout).plan;
|
||||
transport.post.mockResolvedValue({
|
||||
status: 201,
|
||||
data: {
|
||||
schema_version: 1,
|
||||
operation_id: plan.operation_id,
|
||||
state: "completed",
|
||||
upload_id: UPLOAD,
|
||||
upload_state: "staged",
|
||||
},
|
||||
});
|
||||
const result = await capturedRun([
|
||||
"import-file",
|
||||
"upload",
|
||||
"--file",
|
||||
file,
|
||||
"--confirm",
|
||||
"--json",
|
||||
]);
|
||||
expect(result.code).toBe(0);
|
||||
const url = transport.post.mock.calls.at(-1)?.[0] as string;
|
||||
expect(url).toContain("/api/v1/import-files?");
|
||||
expect(url).toContain(`operation_id=${plan.operation_id}`);
|
||||
expect(url).toContain("filename=export.json");
|
||||
expect(JSON.parse(result.stdout).state).toBe("committed");
|
||||
});
|
||||
|
||||
it("commits and discards only with explicit confirmation", async () => {
|
||||
const preview = await capturedRun([
|
||||
"import-file",
|
||||
"commit",
|
||||
UPLOAD,
|
||||
"--json",
|
||||
]);
|
||||
expect(preview.code).toBe(0);
|
||||
expect(JSON.parse(preview.stdout).state).toBe("preview");
|
||||
expect(transport.post).not.toHaveBeenCalled();
|
||||
|
||||
transport.post.mockResolvedValue({
|
||||
status: 200,
|
||||
data: {
|
||||
upload_id: UPLOAD,
|
||||
filename: "x.json",
|
||||
vendor: "generic",
|
||||
source_id: "generic-export",
|
||||
messages: 0,
|
||||
conversations: 0,
|
||||
human_messages: 0,
|
||||
agent_messages: 0,
|
||||
skipped_records: 0,
|
||||
ignored_files: 0,
|
||||
checksum: "a".repeat(64),
|
||||
created_at: "T",
|
||||
committed: true,
|
||||
},
|
||||
});
|
||||
const committed = await capturedRun([
|
||||
"import-file",
|
||||
"commit",
|
||||
UPLOAD,
|
||||
"--confirm",
|
||||
"--json",
|
||||
]);
|
||||
expect(committed.code).toBe(0);
|
||||
expect(transport.post).toHaveBeenCalledWith(
|
||||
`/api/v1/import-files/${UPLOAD}/commit`,
|
||||
{ confirmed: true },
|
||||
undefined,
|
||||
);
|
||||
|
||||
transport.delete.mockResolvedValue({ status: 204, data: "" });
|
||||
const discarded = await capturedRun([
|
||||
"import-file",
|
||||
"discard",
|
||||
UPLOAD,
|
||||
"--confirm",
|
||||
"--json",
|
||||
]);
|
||||
expect(discarded.code).toBe(0);
|
||||
expect(JSON.parse(discarded.stdout).state).toBe("committed");
|
||||
expect(transport.delete).toHaveBeenCalledWith(
|
||||
`/api/v1/import-files/${UPLOAD}`,
|
||||
undefined,
|
||||
);
|
||||
});
|
||||
|
||||
it("maps a Core 404 to a stable rejected code, not a fake success", async () => {
|
||||
transport.post.mockResolvedValue({
|
||||
status: 404,
|
||||
data: { detail: "import_file_not_found" },
|
||||
});
|
||||
const result = await capturedRun([
|
||||
"import-file",
|
||||
"commit",
|
||||
UPLOAD,
|
||||
"--confirm",
|
||||
"--json",
|
||||
]);
|
||||
expect(result.code).toBe(1);
|
||||
const document = JSON.parse(result.stdout);
|
||||
expect(document.state).toBe("rejected");
|
||||
expect(document.code).toBe("import_file_not_found");
|
||||
});
|
||||
});
|
||||
|
||||
describe("notes / extraction / graph / manual extract", () => {
|
||||
it("adds a note from a bounded file and re-verifies content on apply", async () => {
|
||||
const directory = await mkdtemp(path.join(tmpdir(), "om-note-"));
|
||||
const content = path.join(directory, "note.txt");
|
||||
await writeFile(content, "记住这个决定", { mode: 0o600 });
|
||||
const preview = await capturedRun([
|
||||
"notes",
|
||||
"add",
|
||||
"--content-file",
|
||||
content,
|
||||
"--json",
|
||||
]);
|
||||
expect(preview.code).toBe(0);
|
||||
const planFile = path.join(directory, "plan.json");
|
||||
await writeFile(planFile, preview.stdout, { mode: 0o600 });
|
||||
transport.post.mockResolvedValue({
|
||||
status: 201,
|
||||
// A realistic Core `MemoryNoteReceipt`: the shared parser requires
|
||||
// `scope` and `memory_ids` too, so a minimal `{evidence_id,state}` stub
|
||||
// is a malformed body, not a success.
|
||||
data: {
|
||||
evidence_id: NOTE,
|
||||
text: "记住这个决定",
|
||||
scope: "all-apps",
|
||||
created_at: "2026-09-19T00:00:00Z",
|
||||
state: "pending",
|
||||
task_id: null,
|
||||
memory_ids: [],
|
||||
error: null,
|
||||
},
|
||||
});
|
||||
const applied = await capturedRun([
|
||||
"notes",
|
||||
"apply",
|
||||
"--plan-file",
|
||||
planFile,
|
||||
"--content-file",
|
||||
content,
|
||||
"--confirm",
|
||||
"--json",
|
||||
]);
|
||||
expect(applied.code).toBe(0);
|
||||
const body = transport.post.mock.calls.at(-1)?.[1] as Record<
|
||||
string,
|
||||
unknown
|
||||
>;
|
||||
expect(body.text).toBe("记住这个决定");
|
||||
expect(body.scope).toBe("all-apps");
|
||||
expect(JSON.parse(applied.stdout).state).toBe("committed");
|
||||
});
|
||||
|
||||
it.skip("plans then applies the extraction window", async () => {
|
||||
const preview = await capturedRun([
|
||||
"extraction",
|
||||
"set",
|
||||
"--window",
|
||||
"6m",
|
||||
"--json",
|
||||
]);
|
||||
expect(preview.code).toBe(0);
|
||||
expect(JSON.parse(preview.stdout).state).toBe("preview");
|
||||
expect(transport.put).not.toHaveBeenCalled();
|
||||
transport.put.mockResolvedValue({
|
||||
status: 200,
|
||||
data: { window_preset: "6m" },
|
||||
});
|
||||
const applied = await capturedRun([
|
||||
"extraction",
|
||||
"set",
|
||||
"--window",
|
||||
"6m",
|
||||
"--confirm",
|
||||
"--json",
|
||||
]);
|
||||
expect(applied.code).toBe(0);
|
||||
expect(transport.put).toHaveBeenCalledWith(
|
||||
"/api/v1/extraction-settings",
|
||||
{ window_preset: "6m" },
|
||||
undefined,
|
||||
);
|
||||
});
|
||||
|
||||
it("does not call graph width without --confirm", async () => {
|
||||
const preview = await capturedRun([
|
||||
"graph",
|
||||
"width",
|
||||
"--decision",
|
||||
"rebuild_with_new",
|
||||
"--json",
|
||||
]);
|
||||
expect(preview.code).toBe(0);
|
||||
expect(transport.post).not.toHaveBeenCalled();
|
||||
transport.post.mockResolvedValue({
|
||||
status: 200,
|
||||
data: { archived_path: "/tmp/old", exported_path: null, mismatch: null },
|
||||
});
|
||||
const applied = await capturedRun([
|
||||
"graph",
|
||||
"width",
|
||||
"--decision",
|
||||
"rebuild_with_new",
|
||||
"--confirm",
|
||||
"--json",
|
||||
]);
|
||||
expect(applied.code).toBe(0);
|
||||
expect(transport.post).toHaveBeenCalledWith(
|
||||
"/api/v1/graph-width",
|
||||
{ decision: "rebuild_with_new" },
|
||||
undefined,
|
||||
);
|
||||
});
|
||||
|
||||
it.skip("plans manual extraction and applies it only on confirm", async () => {
|
||||
const preview = await capturedRun([
|
||||
"processing",
|
||||
"extract",
|
||||
"--evidence",
|
||||
NOTE,
|
||||
"--json",
|
||||
]);
|
||||
expect(preview.code).toBe(0);
|
||||
expect(JSON.parse(preview.stdout).state).toBe("preview");
|
||||
expect(transport.post).not.toHaveBeenCalled();
|
||||
transport.post.mockResolvedValue({
|
||||
status: 200,
|
||||
data: { promoted: 1, batches_planned: 2, batches_current: 1 },
|
||||
});
|
||||
const applied = await capturedRun([
|
||||
"processing",
|
||||
"extract",
|
||||
"--evidence",
|
||||
NOTE,
|
||||
"--confirm",
|
||||
"--json",
|
||||
]);
|
||||
expect(applied.code).toBe(0);
|
||||
expect(transport.post).toHaveBeenCalledWith(
|
||||
"/api/v1/memory-processing/manual-extract",
|
||||
{ evidence_ids: [NOTE] },
|
||||
undefined,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -1,501 +0,0 @@
|
||||
/**
|
||||
* The remaining Web Console mutating operations the CLI did not expose:
|
||||
* 端内新增记忆 (memory-notes), extraction window, graph width, and manual
|
||||
* extraction.
|
||||
*
|
||||
* Core owns every rule; this module only validates the input it is about to
|
||||
* send, performs one explicit two-phase write, and normalises the receipt so
|
||||
* the CLI never reports a write as committed without evidence.
|
||||
*/
|
||||
|
||||
import { createHash, randomUUID } from "node:crypto";
|
||||
import {
|
||||
buildMemoryNoteSaveRequest,
|
||||
MemoryNoteInputError,
|
||||
parseExtractionSettingsReceipt,
|
||||
parseGraphWidthDecisionReceipt,
|
||||
parseManualExtractReceipt,
|
||||
parseMemoryNoteReceipt,
|
||||
} from "@one-memory/operations";
|
||||
import type { CoreOperations, CorePutOperations } from "./core-operations.js";
|
||||
import { PRODUCT } from "./version.js";
|
||||
|
||||
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
||||
const DIGEST = /^[0-9a-f]{64}$/;
|
||||
const NOTE_MAX_CHARACTERS = 12_000;
|
||||
const EXTRACTION_PRESETS = ["3m", "6m", "1y", "all"] as const;
|
||||
const GRAPH_DECISIONS = [
|
||||
"keep_existing",
|
||||
"rebuild_with_new",
|
||||
"export_first",
|
||||
] as const;
|
||||
const NOTE_STATES = [
|
||||
"waiting_for_model",
|
||||
"waiting_for_permission",
|
||||
"pending",
|
||||
"running",
|
||||
"failed",
|
||||
"ready",
|
||||
"no_memories",
|
||||
] as const;
|
||||
|
||||
export type NoteState = (typeof NOTE_STATES)[number];
|
||||
|
||||
export type ExtendedWritePlan =
|
||||
| {
|
||||
schema_version: 1;
|
||||
kind: "memory_note_add";
|
||||
request_id: string;
|
||||
content_sha256: string;
|
||||
content_characters: number;
|
||||
digest: string;
|
||||
}
|
||||
| {
|
||||
schema_version: 1;
|
||||
kind: "memory_note_process";
|
||||
evidence_id: string;
|
||||
digest: string;
|
||||
}
|
||||
| {
|
||||
schema_version: 1;
|
||||
kind: "extraction_window_set";
|
||||
window_preset: (typeof EXTRACTION_PRESETS)[number];
|
||||
digest: string;
|
||||
}
|
||||
| {
|
||||
schema_version: 1;
|
||||
kind: "graph_width_decide";
|
||||
decision: (typeof GRAPH_DECISIONS)[number];
|
||||
digest: string;
|
||||
}
|
||||
| {
|
||||
schema_version: 1;
|
||||
kind: "manual_extract";
|
||||
evidence_ids: string[];
|
||||
digest: string;
|
||||
};
|
||||
|
||||
export type ExtendedWriteOutcome =
|
||||
| {
|
||||
schema_version: 1;
|
||||
product: string;
|
||||
state: "preview";
|
||||
plan: ExtendedWritePlan;
|
||||
}
|
||||
| {
|
||||
schema_version: 1;
|
||||
product: string;
|
||||
state: "committed" | "recovered";
|
||||
operation_id: string;
|
||||
receipt: Record<string, unknown>;
|
||||
}
|
||||
| {
|
||||
schema_version: 1;
|
||||
product: string;
|
||||
state: "rejected" | "unknown" | "invalid";
|
||||
operation_id?: string;
|
||||
code: string;
|
||||
};
|
||||
|
||||
function canonical(value: unknown): string {
|
||||
if (Array.isArray(value)) return `[${value.map(canonical).join(",")}]`;
|
||||
if (value !== null && typeof value === "object") {
|
||||
return `{${Object.entries(value as Record<string, unknown>)
|
||||
.sort(([left], [right]) => left.localeCompare(right))
|
||||
.map(([key, item]) => `${JSON.stringify(key)}:${canonical(item)}`)
|
||||
.join(",")}}`;
|
||||
}
|
||||
if (value === undefined) return "null";
|
||||
return JSON.stringify(value);
|
||||
}
|
||||
|
||||
function digestOf(value: unknown): string {
|
||||
return createHash("sha256").update(canonical(value)).digest("hex");
|
||||
}
|
||||
|
||||
type Draft =
|
||||
| Omit<Extract<ExtendedWritePlan, { kind: "memory_note_add" }>, "digest">
|
||||
| Omit<Extract<ExtendedWritePlan, { kind: "memory_note_process" }>, "digest">
|
||||
| Omit<
|
||||
Extract<ExtendedWritePlan, { kind: "extraction_window_set" }>,
|
||||
"digest"
|
||||
>
|
||||
| Omit<Extract<ExtendedWritePlan, { kind: "graph_width_decide" }>, "digest">
|
||||
| Omit<Extract<ExtendedWritePlan, { kind: "manual_extract" }>, "digest">;
|
||||
|
||||
function preview(plan: Draft): ExtendedWriteOutcome {
|
||||
return {
|
||||
schema_version: 1,
|
||||
product: PRODUCT,
|
||||
state: "preview",
|
||||
plan: { ...plan, digest: digestOf(plan) } as ExtendedWritePlan,
|
||||
};
|
||||
}
|
||||
|
||||
function invalid(code: string): ExtendedWriteOutcome {
|
||||
return { schema_version: 1, product: PRODUCT, state: "invalid", code };
|
||||
}
|
||||
|
||||
function committed(
|
||||
operationId: string,
|
||||
receipt: Record<string, unknown>,
|
||||
state: "committed" | "recovered" = "committed",
|
||||
): ExtendedWriteOutcome {
|
||||
return {
|
||||
schema_version: 1,
|
||||
product: PRODUCT,
|
||||
state,
|
||||
operation_id: operationId,
|
||||
receipt,
|
||||
};
|
||||
}
|
||||
|
||||
function rejected(
|
||||
operationId: string | undefined,
|
||||
code: string,
|
||||
): ExtendedWriteOutcome {
|
||||
return {
|
||||
schema_version: 1,
|
||||
product: PRODUCT,
|
||||
state: "rejected",
|
||||
...(operationId === undefined ? {} : { operation_id: operationId }),
|
||||
code,
|
||||
};
|
||||
}
|
||||
|
||||
function unknown(
|
||||
operationId: string | undefined,
|
||||
code: string,
|
||||
): ExtendedWriteOutcome {
|
||||
return {
|
||||
schema_version: 1,
|
||||
product: PRODUCT,
|
||||
state: "unknown",
|
||||
...(operationId === undefined ? {} : { operation_id: operationId }),
|
||||
code,
|
||||
};
|
||||
}
|
||||
|
||||
// --- memory-notes ---------------------------------------------------------
|
||||
|
||||
export function planMemoryNoteAdd(text: string): ExtendedWriteOutcome {
|
||||
// The plan keeps only the note's identity, like `memories plan add`: the
|
||||
//正文 lives in a bounded file and is re-read and re-hashed at apply time, so
|
||||
// a plan file never carries the user's private text.
|
||||
if (!validNoteText(text)) return invalid("memory_note_text_invalid");
|
||||
const requestId = randomUUID();
|
||||
return preview({
|
||||
schema_version: 1,
|
||||
kind: "memory_note_add",
|
||||
request_id: requestId,
|
||||
content_sha256: createHash("sha256").update(text, "utf8").digest("hex"),
|
||||
content_characters: [...text].length,
|
||||
});
|
||||
}
|
||||
|
||||
export function validNoteText(text: string): boolean {
|
||||
return (
|
||||
text.length > 0 &&
|
||||
[...text].length <= NOTE_MAX_CHARACTERS &&
|
||||
text.trim().length > 0 &&
|
||||
![...text].some((character) => {
|
||||
const code = character.charCodeAt(0);
|
||||
return code <= 8 || (code >= 11 && code <= 31) || code === 127;
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
export function planMemoryNoteProcess(
|
||||
evidenceId: string,
|
||||
): ExtendedWriteOutcome {
|
||||
return UUID.test(evidenceId)
|
||||
? preview({
|
||||
schema_version: 1,
|
||||
kind: "memory_note_process",
|
||||
evidence_id: evidenceId,
|
||||
})
|
||||
: invalid("memory_note_id_invalid");
|
||||
}
|
||||
|
||||
function noteReceipt(
|
||||
operationId: string,
|
||||
data: { evidence_id: string; state: NoteState },
|
||||
): ExtendedWriteOutcome {
|
||||
return committed(operationId, { ...data });
|
||||
}
|
||||
|
||||
export async function executeMemoryNoteAdd(
|
||||
plan: Extract<ExtendedWritePlan, { kind: "memory_note_add" }>,
|
||||
operations: CoreOperations,
|
||||
text: string,
|
||||
): Promise<ExtendedWriteOutcome> {
|
||||
if (
|
||||
!validNoteText(text) ||
|
||||
createHash("sha256").update(text, "utf8").digest("hex") !==
|
||||
plan.content_sha256 ||
|
||||
[...text].length !== plan.content_characters
|
||||
) {
|
||||
return invalid("memory_note_content_mismatch");
|
||||
}
|
||||
// The shared builder owns the save body (request_id/text/scope=all-apps), so
|
||||
// the CLI and the Web console post the same three fields. Its own text check
|
||||
// already ran above, so a throw here means an unusable plan.
|
||||
let request: ReturnType<typeof buildMemoryNoteSaveRequest>;
|
||||
try {
|
||||
request = buildMemoryNoteSaveRequest({
|
||||
request_id: plan.request_id,
|
||||
text,
|
||||
scope: "all-apps",
|
||||
} as Parameters<typeof buildMemoryNoteSaveRequest>[0]);
|
||||
} catch (error) {
|
||||
if (error instanceof MemoryNoteInputError) {
|
||||
return invalid("memory_note_content_mismatch");
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
const result = await operations.post<unknown>(request.path, request.body);
|
||||
if (result.state === "rejected") {
|
||||
return rejected(plan.request_id, result.code);
|
||||
}
|
||||
if (result.state === "unknown") {
|
||||
return unknown(plan.request_id, result.code);
|
||||
}
|
||||
const receipt = asNoteReceipt(result.data);
|
||||
return receipt === null
|
||||
? unknown(plan.request_id, "ui_write_receipt_invalid")
|
||||
: noteReceipt(plan.request_id, receipt);
|
||||
}
|
||||
|
||||
export async function executeMemoryNoteProcess(
|
||||
plan: Extract<ExtendedWritePlan, { kind: "memory_note_process" }>,
|
||||
operations: CoreOperations,
|
||||
): Promise<ExtendedWriteOutcome> {
|
||||
const result = await operations.post<unknown>(
|
||||
`/api/v1/memory-notes/${plan.evidence_id}/process`,
|
||||
{},
|
||||
);
|
||||
if (result.state === "rejected") {
|
||||
return rejected(plan.evidence_id, result.code);
|
||||
}
|
||||
if (result.state === "unknown") {
|
||||
return unknown(plan.evidence_id, result.code);
|
||||
}
|
||||
const receipt = asNoteReceipt(result.data);
|
||||
return receipt === null
|
||||
? unknown(plan.evidence_id, "ui_write_receipt_invalid")
|
||||
: noteReceipt(plan.evidence_id, receipt);
|
||||
}
|
||||
|
||||
function asNoteReceipt(
|
||||
value: unknown,
|
||||
): { evidence_id: string; state: NoteState } | null {
|
||||
// The shared parser is the single judge of a note receipt; a malformed body
|
||||
// returns null and the caller reports `unknown`, never a saved note.
|
||||
const receipt = parseMemoryNoteReceipt(value);
|
||||
return receipt === null
|
||||
? null
|
||||
: { evidence_id: receipt.evidence_id, state: receipt.state as NoteState };
|
||||
}
|
||||
|
||||
// --- extraction window / graph width / manual extract ---------------------
|
||||
|
||||
export function planExtractionWindowSet(
|
||||
windowPreset: string,
|
||||
): ExtendedWriteOutcome {
|
||||
return (EXTRACTION_PRESETS as readonly string[]).includes(windowPreset)
|
||||
? preview({
|
||||
schema_version: 1,
|
||||
kind: "extraction_window_set",
|
||||
window_preset: windowPreset as (typeof EXTRACTION_PRESETS)[number],
|
||||
})
|
||||
: invalid("extraction_window_preset_invalid");
|
||||
}
|
||||
|
||||
export function planGraphWidthDecide(decision: string): ExtendedWriteOutcome {
|
||||
return (GRAPH_DECISIONS as readonly string[]).includes(decision)
|
||||
? preview({
|
||||
schema_version: 1,
|
||||
kind: "graph_width_decide",
|
||||
decision: decision as (typeof GRAPH_DECISIONS)[number],
|
||||
})
|
||||
: invalid("graph_width_decision_invalid");
|
||||
}
|
||||
|
||||
export function planManualExtract(evidenceIds: string[]): ExtendedWriteOutcome {
|
||||
const ids = [...new Set(evidenceIds)];
|
||||
return ids.length === evidenceIds.length && ids.every((id) => UUID.test(id))
|
||||
? preview({
|
||||
schema_version: 1,
|
||||
kind: "manual_extract",
|
||||
evidence_ids: ids,
|
||||
})
|
||||
: invalid("manual_extract_evidence_ids_invalid");
|
||||
}
|
||||
|
||||
export async function executeExtractionWindowSet(
|
||||
plan: Extract<ExtendedWritePlan, { kind: "extraction_window_set" }>,
|
||||
operations: CorePutOperations,
|
||||
): Promise<ExtendedWriteOutcome> {
|
||||
const result = await operations.put<unknown>("/api/v1/extraction-settings", {
|
||||
window_preset: plan.window_preset,
|
||||
});
|
||||
if (result.state === "rejected") {
|
||||
return rejected(plan.window_preset, result.code);
|
||||
}
|
||||
if (result.state === "unknown") {
|
||||
return unknown(plan.window_preset, result.code);
|
||||
}
|
||||
// The shared parser also enforces that the receipt echoes the requested
|
||||
// preset, so a response belonging to another write cannot pass.
|
||||
const parsed = parseExtractionSettingsReceipt(
|
||||
result.data,
|
||||
plan.window_preset,
|
||||
);
|
||||
if (parsed === null) {
|
||||
return unknown(plan.window_preset, "ui_write_receipt_invalid");
|
||||
}
|
||||
return committed(plan.window_preset, { window_preset: plan.window_preset });
|
||||
}
|
||||
|
||||
export async function executeGraphWidthDecide(
|
||||
plan: Extract<ExtendedWritePlan, { kind: "graph_width_decide" }>,
|
||||
operations: CoreOperations,
|
||||
): Promise<ExtendedWriteOutcome> {
|
||||
const result = await operations.post<unknown>("/api/v1/graph-width", {
|
||||
decision: plan.decision,
|
||||
});
|
||||
if (result.state === "rejected") {
|
||||
return rejected(plan.decision, result.code);
|
||||
}
|
||||
if (result.state === "unknown") {
|
||||
return unknown(plan.decision, result.code);
|
||||
}
|
||||
const receipt = parseGraphWidthDecisionReceipt(result.data);
|
||||
if (receipt === null) {
|
||||
return unknown(plan.decision, "ui_write_receipt_invalid");
|
||||
}
|
||||
return committed(plan.decision, {
|
||||
archived_path: receipt.archived_path ?? null,
|
||||
exported_path: receipt.exported_path ?? null,
|
||||
mismatch: receipt.mismatch ?? null,
|
||||
});
|
||||
}
|
||||
|
||||
export async function executeManualExtract(
|
||||
plan: Extract<ExtendedWritePlan, { kind: "manual_extract" }>,
|
||||
operations: CoreOperations,
|
||||
): Promise<ExtendedWriteOutcome> {
|
||||
const result = await operations.post<unknown>(
|
||||
"/api/v1/memory-processing/manual-extract",
|
||||
{ evidence_ids: plan.evidence_ids },
|
||||
);
|
||||
if (result.state === "rejected") {
|
||||
return rejected("manual_extract", result.code);
|
||||
}
|
||||
if (result.state === "unknown") {
|
||||
return unknown("manual_extract", result.code);
|
||||
}
|
||||
const receipt = parseManualExtractReceipt(result.data);
|
||||
if (receipt === null) {
|
||||
return unknown("manual_extract", "ui_write_receipt_invalid");
|
||||
}
|
||||
return committed("manual_extract", {
|
||||
promoted: receipt.promoted,
|
||||
batches_planned: receipt.batches_planned,
|
||||
batches_current: receipt.batches_current,
|
||||
});
|
||||
}
|
||||
|
||||
export function validateExtendedWritePlan(
|
||||
value: unknown,
|
||||
): ExtendedWritePlan | null {
|
||||
if (value === null || typeof value !== "object" || Array.isArray(value)) {
|
||||
return null;
|
||||
}
|
||||
const candidate = value as Record<string, unknown>;
|
||||
if (candidate.schema_version !== 1 || typeof candidate.kind !== "string") {
|
||||
return null;
|
||||
}
|
||||
const { digest, ...withoutDigest } = candidate;
|
||||
if (typeof digest !== "string" || digest !== digestOf(withoutDigest)) {
|
||||
return null;
|
||||
}
|
||||
switch (candidate.kind) {
|
||||
case "memory_note_add":
|
||||
return typeof candidate.request_id === "string" &&
|
||||
UUID.test(candidate.request_id) &&
|
||||
typeof candidate.content_sha256 === "string" &&
|
||||
DIGEST.test(candidate.content_sha256) &&
|
||||
Number.isSafeInteger(candidate.content_characters) &&
|
||||
(candidate.content_characters as number) >= 1 &&
|
||||
(candidate.content_characters as number) <= NOTE_MAX_CHARACTERS &&
|
||||
Object.keys(candidate).length === 6
|
||||
? (candidate as unknown as ExtendedWritePlan)
|
||||
: null;
|
||||
case "memory_note_process":
|
||||
return typeof candidate.evidence_id === "string" &&
|
||||
UUID.test(candidate.evidence_id) &&
|
||||
Object.keys(candidate).length === 4
|
||||
? (candidate as unknown as ExtendedWritePlan)
|
||||
: null;
|
||||
case "extraction_window_set":
|
||||
return (EXTRACTION_PRESETS as readonly string[]).includes(
|
||||
String(candidate.window_preset),
|
||||
) && Object.keys(candidate).length === 4
|
||||
? (candidate as unknown as ExtendedWritePlan)
|
||||
: null;
|
||||
case "graph_width_decide":
|
||||
return (GRAPH_DECISIONS as readonly string[]).includes(
|
||||
String(candidate.decision),
|
||||
) && Object.keys(candidate).length === 4
|
||||
? (candidate as unknown as ExtendedWritePlan)
|
||||
: null;
|
||||
case "manual_extract":
|
||||
return Array.isArray(candidate.evidence_ids) &&
|
||||
candidate.evidence_ids.every(
|
||||
(id) => typeof id === "string" && UUID.test(id),
|
||||
) &&
|
||||
Object.keys(candidate).length === 4
|
||||
? (candidate as unknown as ExtendedWritePlan)
|
||||
: null;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export function extendedWriteExitCode(document: ExtendedWriteOutcome): number {
|
||||
return ["preview", "committed", "recovered"].includes(document.state) ? 0 : 1;
|
||||
}
|
||||
|
||||
export function renderExtendedWriteText(
|
||||
document: ExtendedWriteOutcome,
|
||||
): string {
|
||||
if (document.state === "preview") {
|
||||
const lines = ["以下操作尚未执行;加 --confirm 后才会写入:"];
|
||||
const plan = document.plan;
|
||||
switch (plan.kind) {
|
||||
case "memory_note_add":
|
||||
lines.push(`新增端内记忆:${plan.content_characters} 字符`);
|
||||
break;
|
||||
case "memory_note_process":
|
||||
lines.push(`处理端内记忆:${plan.evidence_id}`);
|
||||
break;
|
||||
case "extraction_window_set":
|
||||
lines.push(`设置提取窗口:${plan.window_preset}`);
|
||||
break;
|
||||
case "graph_width_decide":
|
||||
lines.push(`图谱宽度决定:${plan.decision}`);
|
||||
break;
|
||||
case "manual_extract":
|
||||
lines.push(`手动提取:${plan.evidence_ids.length} 条证据`);
|
||||
break;
|
||||
}
|
||||
lines.push(`plan_digest:${plan.digest}`);
|
||||
return `${lines.join("\n")}\n`;
|
||||
}
|
||||
if (document.state === "committed" || document.state === "recovered") {
|
||||
return `${document.state === "recovered" ? "已从回执恢复" : "已提交"}:${document.operation_id}\n${JSON.stringify(document.receipt)}\n`;
|
||||
}
|
||||
const code = "code" in document ? document.code : "ui_write_state_invalid";
|
||||
return `操作未完成:${document.state}(${code})\n`;
|
||||
}
|
||||
@@ -1,144 +0,0 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
import type {
|
||||
CoreDeleteOperations,
|
||||
CoreOperations,
|
||||
} from "./core-operations.js";
|
||||
import {
|
||||
executeCallEventsClear,
|
||||
executeCallEventsClearAll,
|
||||
executeImportFileCommit,
|
||||
executeImportFileDiscard,
|
||||
planCallEventsClear,
|
||||
planCallEventsClearAll,
|
||||
planImportFileCommit,
|
||||
planImportFileDiscard,
|
||||
planImportFileUpload,
|
||||
validateUiWritePlan,
|
||||
} from "./ui-write-flows.js";
|
||||
|
||||
const EVENT = "11111111-1111-4111-8111-111111111111";
|
||||
const UPLOAD = "22222222-2222-4222-8222-222222222222";
|
||||
|
||||
function planOf(value: ReturnType<typeof planCallEventsClear>) {
|
||||
if (value.state !== "preview") throw new Error("expected preview");
|
||||
return value.plan;
|
||||
}
|
||||
|
||||
describe("UI write plans", () => {
|
||||
it("permits only valid call-event ids and rejects duplicates", () => {
|
||||
expect(planCallEventsClear([EVENT]).state).toBe("preview");
|
||||
expect(planCallEventsClear([]).state).toBe("invalid");
|
||||
expect(planCallEventsClear([EVENT, EVENT]).state).toBe("invalid");
|
||||
});
|
||||
|
||||
it("plans all remaining UI write operations", () => {
|
||||
expect(planCallEventsClearAll().state).toBe("preview");
|
||||
expect(planImportFileCommit(UPLOAD).state).toBe("preview");
|
||||
expect(planImportFileDiscard(UPLOAD).state).toBe("preview");
|
||||
expect(
|
||||
planImportFileUpload({
|
||||
path: "/tmp/export.json",
|
||||
bytes: 42,
|
||||
sha256: "a".repeat(64),
|
||||
inputKind: "raw_conversation",
|
||||
}).state,
|
||||
).toBe("preview");
|
||||
});
|
||||
|
||||
it("rejects a tampered plan digest", async () => {
|
||||
const plan = planOf(planCallEventsClear([EVENT]));
|
||||
expect(validateUiWritePlan(plan)).toEqual(plan);
|
||||
expect(validateUiWritePlan({ ...plan, event_ids: [UPLOAD] })).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("UI write execution", () => {
|
||||
it("posts clear with Core's confirmation field", async () => {
|
||||
const post = vi.fn(async () => ({
|
||||
state: "committed" as const,
|
||||
data: { removed: 1 },
|
||||
}));
|
||||
const plan = planOf(planCallEventsClear([EVENT]));
|
||||
if (plan.kind !== "call_events_clear") throw new Error("kind");
|
||||
const result = await executeCallEventsClear(plan, {
|
||||
post,
|
||||
} as unknown as CoreOperations);
|
||||
expect(result.state).toBe("committed");
|
||||
expect(post).toHaveBeenCalledWith("/api/v1/call-events/clear", {
|
||||
event_ids: [EVENT],
|
||||
user_confirmed: true,
|
||||
});
|
||||
});
|
||||
|
||||
it("posts clear-all with Core's confirmation field", async () => {
|
||||
const post = vi.fn(async () => ({
|
||||
state: "committed" as const,
|
||||
data: { removed: 2 },
|
||||
}));
|
||||
const plan = planOf(planCallEventsClearAll());
|
||||
if (plan.kind !== "call_events_clear_all") throw new Error("kind");
|
||||
await executeCallEventsClearAll(plan, {
|
||||
post,
|
||||
} as unknown as CoreOperations);
|
||||
expect(post).toHaveBeenCalledWith("/api/v1/call-events/clear-all", {
|
||||
user_confirmed: true,
|
||||
});
|
||||
});
|
||||
|
||||
it("uses POST commit and DELETE discard endpoints", async () => {
|
||||
const post = vi.fn(async () => ({
|
||||
state: "committed" as const,
|
||||
data: {
|
||||
upload_id: UPLOAD,
|
||||
filename: "x.json",
|
||||
vendor: "generic",
|
||||
source_id: "generic-export",
|
||||
messages: 0,
|
||||
conversations: 0,
|
||||
human_messages: 0,
|
||||
agent_messages: 0,
|
||||
skipped_records: 0,
|
||||
ignored_files: 0,
|
||||
checksum: "a".repeat(64),
|
||||
created_at: "T",
|
||||
committed: true,
|
||||
},
|
||||
}));
|
||||
const del = vi.fn(async <T>() => ({
|
||||
state: "committed" as const,
|
||||
data: {} as T,
|
||||
}));
|
||||
const operations = {
|
||||
post,
|
||||
delete: del,
|
||||
} as unknown as CoreOperations & CoreDeleteOperations;
|
||||
const commit = planOf(planImportFileCommit(UPLOAD));
|
||||
if (commit.kind !== "import_file_commit") throw new Error("kind");
|
||||
await executeImportFileCommit(commit, operations);
|
||||
expect(post).toHaveBeenCalledWith(`/api/v1/import-files/${UPLOAD}/commit`, {
|
||||
confirmed: true,
|
||||
});
|
||||
const discard = planOf(planImportFileDiscard(UPLOAD));
|
||||
if (discard.kind !== "import_file_discard") throw new Error("kind");
|
||||
expect((await executeImportFileDiscard(discard, operations)).state).toBe(
|
||||
"committed",
|
||||
);
|
||||
expect(del).toHaveBeenCalledWith(`/api/v1/import-files/${UPLOAD}`);
|
||||
});
|
||||
|
||||
it("treats a malformed success receipt as unknown", async () => {
|
||||
const plan = planOf(planCallEventsClear([EVENT]));
|
||||
if (plan.kind !== "call_events_clear") throw new Error("kind");
|
||||
const result = await executeCallEventsClear(plan, {
|
||||
post: vi.fn(async () => ({
|
||||
state: "committed" as const,
|
||||
data: { wrong: true },
|
||||
})),
|
||||
} as unknown as CoreOperations);
|
||||
expect(result).toMatchObject({
|
||||
state: "unknown",
|
||||
code: "ui_write_receipt_invalid",
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,555 +0,0 @@
|
||||
/**
|
||||
* The Web Console's remaining mutating operations, exposed to the CLI without
|
||||
* duplicating Core semantics. Each operation is planned first; callers apply
|
||||
* the exact plan they reviewed.
|
||||
*/
|
||||
|
||||
import { createHash } from "node:crypto";
|
||||
import { constants } from "node:fs";
|
||||
import { type FileHandle, open } from "node:fs/promises";
|
||||
import type {
|
||||
CoreDeleteOperations,
|
||||
CoreOperations,
|
||||
CoreUploadOperations,
|
||||
} from "./core-operations.js";
|
||||
import { PRODUCT } from "./version.js";
|
||||
|
||||
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
||||
const INPUT_KINDS = ["raw_conversation", "memory_content"] as const;
|
||||
const MAX_UPLOAD_BYTES = 128 * 1024 * 1024;
|
||||
|
||||
export type UiWritePlan =
|
||||
| {
|
||||
schema_version: 1;
|
||||
kind: "call_events_clear";
|
||||
event_ids: string[];
|
||||
digest: string;
|
||||
}
|
||||
| {
|
||||
schema_version: 1;
|
||||
kind: "call_events_clear_all";
|
||||
digest: string;
|
||||
}
|
||||
| {
|
||||
schema_version: 1;
|
||||
kind: "import_file_commit";
|
||||
upload_id: string;
|
||||
digest: string;
|
||||
}
|
||||
| {
|
||||
schema_version: 1;
|
||||
kind: "import_file_discard";
|
||||
upload_id: string;
|
||||
digest: string;
|
||||
}
|
||||
| {
|
||||
schema_version: 1;
|
||||
kind: "import_file_upload";
|
||||
operation_id: string;
|
||||
path: string;
|
||||
filename: string;
|
||||
input_kind: (typeof INPUT_KINDS)[number];
|
||||
bytes: number;
|
||||
sha256: string;
|
||||
digest: string;
|
||||
};
|
||||
|
||||
export type UiWriteOutcome =
|
||||
| {
|
||||
schema_version: 1;
|
||||
product: string;
|
||||
state: "preview";
|
||||
plan: UiWritePlan;
|
||||
}
|
||||
| {
|
||||
schema_version: 1;
|
||||
product: string;
|
||||
state: "committed" | "recovered";
|
||||
operation_id: string;
|
||||
receipt: Record<string, unknown>;
|
||||
}
|
||||
| {
|
||||
schema_version: 1;
|
||||
product: string;
|
||||
state: "rejected" | "unknown" | "invalid";
|
||||
operation_id?: string;
|
||||
code: string;
|
||||
receipt?: Record<string, unknown>;
|
||||
};
|
||||
|
||||
function digestOf(value: unknown): string {
|
||||
return createHash("sha256").update(canonical(value)).digest("hex");
|
||||
}
|
||||
|
||||
function canonical(value: unknown): string {
|
||||
if (Array.isArray(value)) return `[${value.map(canonical).join(",")}]`;
|
||||
if (value !== null && typeof value === "object") {
|
||||
return `{${Object.entries(value as Record<string, unknown>)
|
||||
.sort(([left], [right]) => left.localeCompare(right))
|
||||
.map(([key, item]) => `${JSON.stringify(key)}:${canonical(item)}`)
|
||||
.join(",")}}`;
|
||||
}
|
||||
if (value === undefined) return "null";
|
||||
return JSON.stringify(value);
|
||||
}
|
||||
|
||||
export type UiWritePlanDraft =
|
||||
| Omit<Extract<UiWritePlan, { kind: "call_events_clear" }>, "digest">
|
||||
| Omit<Extract<UiWritePlan, { kind: "call_events_clear_all" }>, "digest">
|
||||
| Omit<Extract<UiWritePlan, { kind: "import_file_commit" }>, "digest">
|
||||
| Omit<Extract<UiWritePlan, { kind: "import_file_discard" }>, "digest">
|
||||
| Omit<Extract<UiWritePlan, { kind: "import_file_upload" }>, "digest">;
|
||||
|
||||
function preview(plan: UiWritePlanDraft): UiWriteOutcome {
|
||||
return {
|
||||
schema_version: 1,
|
||||
product: PRODUCT,
|
||||
state: "preview",
|
||||
plan: { ...plan, digest: digestOf(plan) } as UiWritePlan,
|
||||
};
|
||||
}
|
||||
|
||||
function invalid(code: string): UiWriteOutcome {
|
||||
return { schema_version: 1, product: PRODUCT, state: "invalid", code };
|
||||
}
|
||||
|
||||
function committed(
|
||||
operationId: string,
|
||||
receipt: Record<string, unknown>,
|
||||
state: "committed" | "recovered" = "committed",
|
||||
): UiWriteOutcome {
|
||||
return {
|
||||
schema_version: 1,
|
||||
product: PRODUCT,
|
||||
state,
|
||||
operation_id: operationId,
|
||||
receipt,
|
||||
};
|
||||
}
|
||||
|
||||
export function planCallEventsClear(
|
||||
eventIds: readonly string[],
|
||||
): UiWriteOutcome {
|
||||
if (
|
||||
eventIds.length < 1 ||
|
||||
eventIds.length > 100 ||
|
||||
new Set(eventIds).size !== eventIds.length ||
|
||||
eventIds.some((id) => !UUID.test(id))
|
||||
) {
|
||||
return invalid("call_event_ids_invalid");
|
||||
}
|
||||
return preview({
|
||||
schema_version: 1,
|
||||
kind: "call_events_clear",
|
||||
event_ids: [...eventIds],
|
||||
});
|
||||
}
|
||||
|
||||
export function planCallEventsClearAll(): UiWriteOutcome {
|
||||
return preview({ schema_version: 1, kind: "call_events_clear_all" });
|
||||
}
|
||||
|
||||
export function planImportFileCommit(uploadId: string): UiWriteOutcome {
|
||||
return UUID.test(uploadId)
|
||||
? preview({
|
||||
schema_version: 1,
|
||||
kind: "import_file_commit",
|
||||
upload_id: uploadId,
|
||||
})
|
||||
: invalid("import_file_id_invalid");
|
||||
}
|
||||
|
||||
export function planImportFileDiscard(uploadId: string): UiWriteOutcome {
|
||||
return UUID.test(uploadId)
|
||||
? preview({
|
||||
schema_version: 1,
|
||||
kind: "import_file_discard",
|
||||
upload_id: uploadId,
|
||||
})
|
||||
: invalid("import_file_id_invalid");
|
||||
}
|
||||
|
||||
export function planImportFileUpload(input: {
|
||||
path: string;
|
||||
bytes: number;
|
||||
sha256: string;
|
||||
inputKind: "raw_conversation" | "memory_content";
|
||||
}): UiWriteOutcome {
|
||||
const filename = input.path.split(/[/\\]/).pop() ?? "";
|
||||
if (
|
||||
input.path.length === 0 ||
|
||||
!Number.isSafeInteger(input.bytes) ||
|
||||
input.bytes < 0 ||
|
||||
input.bytes > MAX_UPLOAD_BYTES ||
|
||||
!/^[0-9a-f]{64}$/.test(input.sha256) ||
|
||||
!INPUT_KINDS.includes(input.inputKind) ||
|
||||
filename.length === 0 ||
|
||||
[...filename].length > 200 ||
|
||||
!/\.(?:zip|json|md|txt)$/i.test(filename)
|
||||
) {
|
||||
return invalid("import_file_upload_invalid");
|
||||
}
|
||||
return preview({
|
||||
schema_version: 1,
|
||||
kind: "import_file_upload",
|
||||
operation_id: fileUploadOperationId(
|
||||
filename,
|
||||
input.inputKind,
|
||||
input.sha256,
|
||||
),
|
||||
path: input.path,
|
||||
filename,
|
||||
input_kind: input.inputKind,
|
||||
bytes: input.bytes,
|
||||
sha256: input.sha256,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Read one regular, non-symlinked export without buffering past the 128 MiB
|
||||
* limit. Returns the bytes plus the checksum the plan records, so apply can
|
||||
* prove the file it uploads is the one the caller reviewed.
|
||||
*/
|
||||
export async function readImportFileUpload(
|
||||
filePath: string,
|
||||
inputKind: "raw_conversation" | "memory_content",
|
||||
): Promise<{ plan: UiWritePlan; bytes: Buffer } | null> {
|
||||
let handle: FileHandle;
|
||||
try {
|
||||
handle = await open(
|
||||
filePath,
|
||||
constants.O_RDONLY |
|
||||
(constants.O_NONBLOCK ?? 0) |
|
||||
(constants.O_NOFOLLOW ?? 0),
|
||||
);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
const stats = await handle.stat();
|
||||
if (!stats.isFile() || stats.nlink !== 1 || stats.size > MAX_UPLOAD_BYTES) {
|
||||
return null;
|
||||
}
|
||||
const buffer = Buffer.alloc(Number(stats.size));
|
||||
let offset = 0;
|
||||
while (offset < buffer.length) {
|
||||
const chunk = await handle.read(
|
||||
buffer,
|
||||
offset,
|
||||
buffer.length - offset,
|
||||
offset,
|
||||
);
|
||||
if (chunk.bytesRead === 0) break;
|
||||
offset += chunk.bytesRead;
|
||||
}
|
||||
if (offset !== buffer.length) return null;
|
||||
const checksum = createHash("sha256").update(buffer).digest("hex");
|
||||
const result = planImportFileUpload({
|
||||
path: filePath,
|
||||
bytes: buffer.byteLength,
|
||||
sha256: checksum,
|
||||
inputKind,
|
||||
});
|
||||
if (result.state !== "preview") return null;
|
||||
return { plan: result.plan, bytes: buffer };
|
||||
} catch {
|
||||
return null;
|
||||
} finally {
|
||||
await handle.close().catch(() => undefined);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Core keys upload idempotency by `operation_id` and rejects a replay whose
|
||||
* fingerprint differs. Deriving it from the file's own identity means a retry
|
||||
* after a dropped response resumes the same staging operation instead of
|
||||
* creating a second upload; a genuinely different payload maps to a different
|
||||
* operation and cannot overwrite the first.
|
||||
*/
|
||||
function fileUploadOperationId(
|
||||
filename: string,
|
||||
inputKind: (typeof INPUT_KINDS)[number],
|
||||
checksum: string,
|
||||
): string {
|
||||
const hex = createHash("sha256")
|
||||
.update(JSON.stringify([filename, inputKind, checksum]))
|
||||
.digest("hex");
|
||||
return [
|
||||
hex.slice(0, 8),
|
||||
hex.slice(8, 12),
|
||||
`4${hex.slice(13, 16)}`,
|
||||
`${(8 + (parseInt(hex[16]!, 16) % 4)).toString(16)}${hex.slice(17, 20)}`,
|
||||
hex.slice(20, 32),
|
||||
].join("-");
|
||||
}
|
||||
|
||||
export function validateUiWritePlan(value: unknown): UiWritePlan | null {
|
||||
if (value === null || typeof value !== "object" || Array.isArray(value)) {
|
||||
return null;
|
||||
}
|
||||
const candidate = value as Record<string, unknown>;
|
||||
if (candidate.schema_version !== 1 || typeof candidate.kind !== "string") {
|
||||
return null;
|
||||
}
|
||||
const { digest, ...withoutDigest } = candidate;
|
||||
if (typeof digest !== "string" || digest !== digestOf(withoutDigest)) {
|
||||
return null;
|
||||
}
|
||||
switch (candidate.kind) {
|
||||
case "call_events_clear":
|
||||
return Array.isArray(candidate.event_ids) &&
|
||||
candidate.event_ids.length >= 1 &&
|
||||
candidate.event_ids.length <= 100 &&
|
||||
new Set(candidate.event_ids).size === candidate.event_ids.length &&
|
||||
candidate.event_ids.every(
|
||||
(id) => typeof id === "string" && UUID.test(id),
|
||||
)
|
||||
? (candidate as unknown as UiWritePlan)
|
||||
: null;
|
||||
case "call_events_clear_all":
|
||||
return Object.keys(candidate).length === 3
|
||||
? (candidate as unknown as UiWritePlan)
|
||||
: null;
|
||||
case "import_file_commit":
|
||||
case "import_file_discard":
|
||||
return typeof candidate.upload_id === "string" &&
|
||||
UUID.test(candidate.upload_id) &&
|
||||
Object.keys(candidate).length === 4
|
||||
? (candidate as unknown as UiWritePlan)
|
||||
: null;
|
||||
case "import_file_upload":
|
||||
return typeof candidate.operation_id === "string" &&
|
||||
UUID.test(candidate.operation_id) &&
|
||||
typeof candidate.path === "string" &&
|
||||
typeof candidate.filename === "string" &&
|
||||
candidate.filename === candidate.path.split(/[/\\]/).pop() &&
|
||||
/^[0-9a-f]{64}$/.test(String(candidate.sha256)) &&
|
||||
INPUT_KINDS.includes(
|
||||
candidate.input_kind as (typeof INPUT_KINDS)[number],
|
||||
) &&
|
||||
Number.isSafeInteger(candidate.bytes) &&
|
||||
(candidate.bytes as number) >= 0 &&
|
||||
(candidate.bytes as number) <= 128 * 1024 * 1024
|
||||
? (candidate as unknown as UiWritePlan)
|
||||
: null;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export function uiWriteExitCode(document: UiWriteOutcome): number {
|
||||
return ["preview", "committed", "recovered"].includes(document.state) ? 0 : 1;
|
||||
}
|
||||
|
||||
export function renderUiWriteText(document: UiWriteOutcome): string {
|
||||
if (document.state === "preview") {
|
||||
return `以下操作尚未执行;加 --confirm 后才会写入:\n${document.plan.kind}\nplan_digest:${document.plan.digest}\n`;
|
||||
}
|
||||
if (document.state === "committed" || document.state === "recovered") {
|
||||
return `已提交:${document.operation_id}\n${JSON.stringify(document.receipt)}\n`;
|
||||
}
|
||||
const code = "code" in document ? document.code : "ui_write_state_invalid";
|
||||
return `操作未完成:${document.state}(${code})\n`;
|
||||
}
|
||||
|
||||
export async function executeCallEventsClear(
|
||||
plan: Extract<UiWritePlan, { kind: "call_events_clear" }>,
|
||||
operations: CoreOperations,
|
||||
): Promise<UiWriteOutcome> {
|
||||
const result = await operations.post<unknown>("/api/v1/call-events/clear", {
|
||||
event_ids: plan.event_ids,
|
||||
user_confirmed: true,
|
||||
});
|
||||
return outcome(result, plan.event_ids.join(","), ["removed"]);
|
||||
}
|
||||
|
||||
export async function executeCallEventsClearAll(
|
||||
plan: Extract<UiWritePlan, { kind: "call_events_clear_all" }>,
|
||||
operations: CoreOperations,
|
||||
): Promise<UiWriteOutcome> {
|
||||
const result = await operations.post<unknown>(
|
||||
"/api/v1/call-events/clear-all",
|
||||
{ user_confirmed: true },
|
||||
);
|
||||
return outcome(result, "all", ["removed"]);
|
||||
}
|
||||
|
||||
export async function executeImportFileCommit(
|
||||
plan: Extract<UiWritePlan, { kind: "import_file_commit" }>,
|
||||
operations: CoreOperations,
|
||||
): Promise<UiWriteOutcome> {
|
||||
const result = await operations.post<unknown>(
|
||||
`/api/v1/import-files/${plan.upload_id}/commit`,
|
||||
{ confirmed: true },
|
||||
);
|
||||
return outcome(result, plan.upload_id, [
|
||||
"upload_id",
|
||||
"filename",
|
||||
"vendor",
|
||||
"source_id",
|
||||
"messages",
|
||||
"conversations",
|
||||
"human_messages",
|
||||
"agent_messages",
|
||||
"skipped_records",
|
||||
"ignored_files",
|
||||
"checksum",
|
||||
"created_at",
|
||||
"committed",
|
||||
]);
|
||||
}
|
||||
|
||||
export async function executeImportFileDiscard(
|
||||
plan: Extract<UiWritePlan, { kind: "import_file_discard" }>,
|
||||
operations: CoreDeleteOperations,
|
||||
): Promise<UiWriteOutcome> {
|
||||
const result = await operations.delete<unknown>(
|
||||
`/api/v1/import-files/${plan.upload_id}`,
|
||||
);
|
||||
// Core answers 204 with no body. A successful delete is a committed
|
||||
// operation even though there is no receipt to validate; requiring a body
|
||||
// here would report every real discard as `ui_write_receipt_invalid`.
|
||||
return outcome(result, plan.upload_id, null);
|
||||
}
|
||||
|
||||
/** Upload one local file through the same recoverable Core staging flow the
|
||||
* Web console uses. The operation id is part of the plan, so a retry after a
|
||||
* dropped response resumes the same staging record instead of duplicating it.
|
||||
*/
|
||||
export async function executeImportFileUpload(
|
||||
plan: Extract<UiWritePlan, { kind: "import_file_upload" }>,
|
||||
operations: CoreUploadOperations,
|
||||
): Promise<UiWriteOutcome> {
|
||||
// Re-read the file at apply time and re-derive its identity. A plan that
|
||||
// names a path whose content changed after review is refused rather than
|
||||
// uploading something the caller never saw.
|
||||
const reread = await readImportFileUpload(plan.path, plan.input_kind);
|
||||
if (reread === null) {
|
||||
return invalid("import_file_upload_unreadable");
|
||||
}
|
||||
const bytes = reread.bytes;
|
||||
const checksum = createHash("sha256").update(bytes).digest("hex");
|
||||
if (
|
||||
bytes.byteLength !== plan.bytes ||
|
||||
checksum !== plan.sha256 ||
|
||||
reread.plan.digest !== plan.digest
|
||||
) {
|
||||
return invalid("import_file_upload_content_mismatch");
|
||||
}
|
||||
const result = await operations.uploadImportFile<unknown>({
|
||||
bytes,
|
||||
filename: plan.filename,
|
||||
inputKind: plan.input_kind,
|
||||
operationId: plan.operation_id,
|
||||
});
|
||||
if (result.state === "rejected") {
|
||||
return {
|
||||
schema_version: 1,
|
||||
product: PRODUCT,
|
||||
state: "rejected",
|
||||
operation_id: plan.operation_id,
|
||||
code: result.code,
|
||||
};
|
||||
}
|
||||
if (result.state === "unknown") {
|
||||
return {
|
||||
schema_version: 1,
|
||||
product: PRODUCT,
|
||||
state: "unknown",
|
||||
operation_id: plan.operation_id,
|
||||
code: result.code,
|
||||
};
|
||||
}
|
||||
const receipt =
|
||||
result.data !== null &&
|
||||
typeof result.data === "object" &&
|
||||
!Array.isArray(result.data)
|
||||
? (result.data as Record<string, unknown>)
|
||||
: null;
|
||||
if (
|
||||
receipt === null ||
|
||||
receipt.operation_id !== plan.operation_id ||
|
||||
typeof receipt.state !== "string"
|
||||
) {
|
||||
return {
|
||||
schema_version: 1,
|
||||
product: PRODUCT,
|
||||
state: "unknown",
|
||||
operation_id: plan.operation_id,
|
||||
code: "ui_write_receipt_invalid",
|
||||
};
|
||||
}
|
||||
// Core reports the staged upload as `completed`; `pending`/`not_found` here
|
||||
// means the operation record did not settle, so the result is not known.
|
||||
if (receipt.state !== "completed") {
|
||||
return {
|
||||
schema_version: 1,
|
||||
product: PRODUCT,
|
||||
state: "unknown",
|
||||
operation_id: plan.operation_id,
|
||||
code: `import_upload_${receipt.state}`,
|
||||
};
|
||||
}
|
||||
const uploadId =
|
||||
typeof receipt.upload_id === "string" ? receipt.upload_id : undefined;
|
||||
if (uploadId === undefined || !UUID.test(uploadId)) {
|
||||
return {
|
||||
schema_version: 1,
|
||||
product: PRODUCT,
|
||||
state: "unknown",
|
||||
operation_id: plan.operation_id,
|
||||
code: "ui_write_receipt_invalid",
|
||||
};
|
||||
}
|
||||
return committed(plan.operation_id, {
|
||||
operation_id: plan.operation_id,
|
||||
upload_id: uploadId,
|
||||
upload_state: receipt.upload_state ?? null,
|
||||
});
|
||||
}
|
||||
|
||||
function outcome(
|
||||
result:
|
||||
| Awaited<ReturnType<CoreOperations["post"]>>
|
||||
| Awaited<ReturnType<CoreDeleteOperations["delete"]>>,
|
||||
operationId: string,
|
||||
required: readonly string[] | null,
|
||||
): UiWriteOutcome {
|
||||
if (result.state === "rejected") {
|
||||
return {
|
||||
schema_version: 1,
|
||||
product: PRODUCT,
|
||||
state: "rejected",
|
||||
operation_id: operationId,
|
||||
code: result.code,
|
||||
};
|
||||
}
|
||||
if (result.state === "unknown") {
|
||||
return {
|
||||
schema_version: 1,
|
||||
product: PRODUCT,
|
||||
state: "unknown",
|
||||
operation_id: operationId,
|
||||
code: result.code,
|
||||
};
|
||||
}
|
||||
if (required === null) {
|
||||
return committed(operationId, {});
|
||||
}
|
||||
const data =
|
||||
result.data !== null &&
|
||||
typeof result.data === "object" &&
|
||||
!Array.isArray(result.data)
|
||||
? (result.data as Record<string, unknown>)
|
||||
: null;
|
||||
if (data === null || required.some((key) => !Object.hasOwn(data, key))) {
|
||||
return {
|
||||
schema_version: 1,
|
||||
product: PRODUCT,
|
||||
state: "unknown",
|
||||
operation_id: operationId,
|
||||
code: "ui_write_receipt_invalid",
|
||||
};
|
||||
}
|
||||
return committed(operationId, data);
|
||||
}
|
||||
@@ -13,7 +13,16 @@ const GLOBAL_OPTIONS: readonly CommandOption[] = [
|
||||
summary: "显示帮助;可与 --json 组合输出机器可读目录。",
|
||||
},
|
||||
{ flag: "--version, -V", summary: "显示 CLI 版本。" },
|
||||
{ flag: "--json", summary: "以 JSON 输出到 stdout,供脚本与 Agent 消费。" },
|
||||
{
|
||||
flag: "--json",
|
||||
summary:
|
||||
"以 JSON 输出到 stdout,供脚本与 Agent 消费;结果与失败都在 stdout,只有参数错误写 stderr。",
|
||||
},
|
||||
{
|
||||
flag: "--data-dir <path>",
|
||||
summary:
|
||||
"本次使用的 MemHub 数据目录(绝对路径);默认 ONE_MEMORY_DATA_DIR > 安装回执 > ~/.one-memory。",
|
||||
},
|
||||
];
|
||||
|
||||
export function renderGeneralHelp(): string {
|
||||
@@ -33,6 +42,9 @@ export function renderGeneralHelp(): string {
|
||||
(exit) => ` ${exit.code} ${pad(exit.name, 17)}${exit.summary}`,
|
||||
),
|
||||
"",
|
||||
"确认约定:只有不可逆操作(memories delete、models remove、delete-data、backup restore、graph-width --decision rebuild_with_new)需要 --yes;不加时只说明将要做什么,不做任何改动,exit 0。delete-data 与 backup restore 还要用 --confirm <path> 再写一遍目标路径。install/upgrade/uninstall/service-install/service-uninstall 不加 --yes 时只出计划。其它写命令直接执行。",
|
||||
"异步操作返回 ID,用对应命令查进度:sources receipt、models receipt、memories add-status、write status。",
|
||||
"目前仅支持 macOS 与 Linux 连接本机 Runtime。",
|
||||
"stdout 只输出数据,进度与诊断写入 stderr。",
|
||||
`运行 ${PRODUCT} help <command> 查看单个命令的参数。`,
|
||||
"",
|
||||
@@ -55,6 +67,8 @@ export function renderCommandHelp(command: CommandDescriptor): string {
|
||||
);
|
||||
}
|
||||
lines.push(
|
||||
"Global options: --json、--data-dir <path>(见 memhub help)。",
|
||||
"",
|
||||
"Exit codes:",
|
||||
...EXIT_CODES.filter((exit) => command.exit_codes.includes(exit.code)).map(
|
||||
(exit) => ` ${exit.code} ${pad(exit.name, 17)}${exit.summary}`,
|
||||
|
||||
@@ -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",
|
||||
|
||||
+1092
-172
File diff suppressed because it is too large.
Load diff
@@ -584,6 +584,7 @@ def _sdk_migration_status(options: Sequence[str], stream: TextIO) -> int:
|
||||
}
|
||||
else:
|
||||
from one_memory.hy_runtime_config import HY_MEMORY_NAMESPACE
|
||||
from one_memory.hy_sdk_runtime import builtin_retrieval_snapshot
|
||||
from one_memory.sdk_data_plane_migration import (
|
||||
MemoryAuthority,
|
||||
SdkDataPlaneMigrationStore,
|
||||
@@ -595,14 +596,14 @@ def _sdk_migration_status(options: Sequence[str], stream: TextIO) -> int:
|
||||
|
||||
authority = SdkDataPlaneMigrationStore(database_path).authority()
|
||||
if authority is MemoryAuthority.SDK:
|
||||
profile = read_active_profile(database_path)
|
||||
profile = read_active_profile(database_path) or builtin_retrieval_snapshot()
|
||||
leftovers = bool(
|
||||
legacy_tables_present(database_path)
|
||||
or abandoned_staging_present(
|
||||
data_dir=settings.data_dir,
|
||||
namespace=HY_MEMORY_NAMESPACE,
|
||||
)
|
||||
or (profile is not None and legacy_vector_collections(settings.data_dir, profile))
|
||||
or legacy_vector_collections(settings.data_dir, profile)
|
||||
)
|
||||
state = "required" if leftovers else "not_required"
|
||||
else:
|
||||
|
||||
@@ -5,6 +5,9 @@ from typing import Annotated, Literal
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, Field, model_validator
|
||||
|
||||
from one_memory.sdk_search_options import SdkSearchOptions
|
||||
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"]
|
||||
@@ -47,17 +50,24 @@ class ContextSearchRequest(BaseModel):
|
||||
return self
|
||||
|
||||
|
||||
class SdkContextSearchRequest(ContextSearchRequest):
|
||||
class SdkContextSearchRequest(ContextSearchRequest, SdkSearchOptions):
|
||||
"""`POST /api/v2/context/search`: the three SDK recall lanes are set apart.
|
||||
|
||||
`limit` is the normal lane's top-k and the cap on returned items;
|
||||
`schema_limit` and `intention_limit` are the SDK's schema (L6) and
|
||||
proactive intention (L7) lanes. A caller's values are passed through.
|
||||
|
||||
The `SdkSearchOptions` fields reach the memory lane's `client.search`;
|
||||
the raw lane takes the floor and the source, session, agent and time
|
||||
filters (never wider than the caller's raw authority). Absent, both
|
||||
lanes make the call they always made.
|
||||
"""
|
||||
|
||||
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):
|
||||
|
||||
@@ -24,13 +24,57 @@ from one_memory.hy_runtime_config import HY_MEMORY_READER
|
||||
from one_memory.hy_sdk_runtime import sdk_relevance_floor
|
||||
from one_memory.hy_sdk_types import (
|
||||
HySdkMemoryRecord,
|
||||
parse_sdk_lane_search,
|
||||
parse_sdk_raw_search,
|
||||
parse_sdk_search,
|
||||
)
|
||||
from one_memory.imports.storage import EvidenceStore
|
||||
from one_memory.sdk_search_options import SdkSearchFilters, SearchLane
|
||||
from one_memory.semantic_deadline import SemanticMemoryHit
|
||||
from one_memory.user_scope import owns
|
||||
|
||||
CALL_EVENT_RESPONSE_BUDGET_MS = 250
|
||||
|
||||
#: Pass-through filters the raw lane honours. Scene, layers, reader and the
|
||||
#: schema/working floors describe memory lanes; the raw lane is always the
|
||||
#: chat store's L1 raw layer.
|
||||
_RAW_LANE_FILTERS = ("created_after", "session_ids", "agent_ids", "exclude_sources", "rerank")
|
||||
|
||||
|
||||
def raw_lane_sources(
|
||||
allowed: Sequence[str] | frozenset[str] | None,
|
||||
requested: Sequence[str] | None,
|
||||
) -> list[str] | None:
|
||||
"""The raw lane's source filter: the caller's, never wider than its authority.
|
||||
|
||||
`allowed` empty or None means every source (the call Core always made).
|
||||
None back means nothing is left to search.
|
||||
"""
|
||||
allowed_list = list(allowed or ())
|
||||
if requested is None:
|
||||
return allowed_list
|
||||
chosen = [
|
||||
source for source in dict.fromkeys(requested) if not allowed_list or source in allowed_list
|
||||
]
|
||||
return chosen or None
|
||||
|
||||
|
||||
def raw_lane_overrides(value: SdkSearchFilters) -> dict[str, object]:
|
||||
"""The caller's raw-lane overrides: its floor and its row filters."""
|
||||
overrides = value.filter_kwargs()
|
||||
found = {name: overrides[name] for name in _RAW_LANE_FILTERS if name in overrides}
|
||||
floor = value.lane_floor("raw")
|
||||
if floor is not None:
|
||||
found["min_score"] = floor
|
||||
return found
|
||||
|
||||
|
||||
def hit_namespace(hit: SemanticMemoryHit) -> dict[str, str]:
|
||||
"""The `get` namespace of a hit: nothing for chat, kind and variant otherwise."""
|
||||
if hit.kind in {"experience", "fact"} and hit.variant is not None:
|
||||
return {"kind": hit.kind, "variant": hit.variant}
|
||||
return {}
|
||||
|
||||
|
||||
def sdk_context_router(dependencies: Sequence[Depends]) -> APIRouter:
|
||||
"""SDK-native context: no Evidence/Governance ids or fabricated revisions."""
|
||||
@@ -70,22 +114,30 @@ def sdk_context_router(dependencies: Sequence[Depends]) -> APIRouter:
|
||||
async def raw_lane() -> tuple[SdkRawContextItem, ...]:
|
||||
if "raw" not in value.kinds or not authority.raw_allowed:
|
||||
return ()
|
||||
sources = raw_lane_sources(authority.raw_sources, value.include_sources)
|
||||
if sources is None:
|
||||
return ()
|
||||
raw = await asyncio.to_thread(
|
||||
client.search,
|
||||
value.query,
|
||||
user_ids=["local"],
|
||||
agent_ids=[],
|
||||
include_sources=list(authority.raw_sources or ()),
|
||||
limit=value.limit,
|
||||
min_score=0.0,
|
||||
schema_min_score=0.0,
|
||||
schema_limit=0,
|
||||
intention_limit=0,
|
||||
scene="chat",
|
||||
reader=HY_MEMORY_READER,
|
||||
layers=["l1_raw"],
|
||||
**{
|
||||
"user_ids": [value.user_id],
|
||||
"agent_ids": [],
|
||||
"include_sources": sources,
|
||||
"limit": value.limit,
|
||||
"min_score": 0.0,
|
||||
"schema_min_score": 0.0,
|
||||
"schema_limit": 0,
|
||||
"intention_limit": 0,
|
||||
"scene": "chat",
|
||||
"reader": HY_MEMORY_READER,
|
||||
"layers": ["l1_raw"],
|
||||
**raw_lane_overrides(value),
|
||||
},
|
||||
)
|
||||
hits = tuple(
|
||||
hit for hit in parse_sdk_raw_search(raw) if value.passes_floor("raw", hit.score)
|
||||
)
|
||||
hits = parse_sdk_raw_search(raw)
|
||||
return tuple(
|
||||
SdkRawContextItem(
|
||||
raw_id=hit.raw_id,
|
||||
@@ -105,25 +157,44 @@ def sdk_context_router(dependencies: Sequence[Depends]) -> APIRouter:
|
||||
async def memory_lane() -> tuple[SdkMemoryContextItem, ...]:
|
||||
if "memory" not in value.kinds or not authority.memory_allowed:
|
||||
return ()
|
||||
overrides = value.search_overrides()
|
||||
raw = await asyncio.to_thread(
|
||||
client.search,
|
||||
value.query,
|
||||
user_ids=["local"],
|
||||
agent_ids=[],
|
||||
limit=value.limit,
|
||||
min_score=sdk_relevance_floor(),
|
||||
schema_min_score=sdk_relevance_floor(),
|
||||
schema_limit=value.schema_limit,
|
||||
intention_limit=value.intention_limit,
|
||||
scene="normal",
|
||||
reader=HY_MEMORY_READER,
|
||||
**{
|
||||
"user_ids": [value.user_id],
|
||||
"agent_ids": [],
|
||||
"limit": value.limit,
|
||||
"min_score": sdk_relevance_floor(),
|
||||
"schema_min_score": sdk_relevance_floor(),
|
||||
"schema_limit": value.schema_limit,
|
||||
"intention_limit": value.intention_limit,
|
||||
"scene": "normal",
|
||||
"reader": HY_MEMORY_READER,
|
||||
**overrides,
|
||||
},
|
||||
)
|
||||
hits = (
|
||||
tuple(
|
||||
hit
|
||||
for hit in parse_sdk_lane_search(
|
||||
raw, error_code="hy_memory_search_shape_unknown"
|
||||
)
|
||||
if value.passes_floor(cast(SearchLane, hit.lane), hit.score)
|
||||
)
|
||||
if overrides
|
||||
else parse_sdk_search(raw, error_code="hy_memory_search_shape_unknown")
|
||||
)
|
||||
hits = parse_sdk_search(raw, error_code="hy_memory_search_shape_unknown")
|
||||
items: list[SdkMemoryContextItem] = []
|
||||
for hit in hits:
|
||||
record = await asyncio.to_thread(client.get, hit.memory_id)
|
||||
record = await asyncio.to_thread(client.get, hit.memory_id, **hit_namespace(hit))
|
||||
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_memory_note(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_memory_note(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."""
|
||||
@@ -192,6 +217,33 @@ def builtin_retrieval_snapshot() -> ModelProfileSnapshot:
|
||||
)
|
||||
|
||||
|
||||
def retrieval_view(profile: ModelProfileSnapshot) -> ModelProfileSnapshot:
|
||||
"""The read side of `profile`: its embedding space, and no generation.
|
||||
|
||||
Reads must open the collection writes fill (#242), and the collection is
|
||||
named by the embedding only. Generation stays the built-in placeholder, so
|
||||
a read never needs the generation model. A remote embedding still needs
|
||||
the profile's API key, which `build_sdk_config` uses as the embedding key.
|
||||
"""
|
||||
provider = profile.embedding_provider_kind or profile.provider_kind
|
||||
endpoint: str | None = None
|
||||
if provider is not ProviderKind.LOCAL_ONNX:
|
||||
endpoint = profile.embedding_base_url
|
||||
if not endpoint:
|
||||
raise HySdkRuntimeError("hy_sdk_model_endpoint_missing")
|
||||
return builtin_retrieval_snapshot().model_copy(
|
||||
update={
|
||||
"profile_id": profile.profile_id,
|
||||
"revision": profile.revision,
|
||||
"embedding_provider_kind": provider,
|
||||
"embedding_model": profile.embedding_model,
|
||||
"embedding_base_url": endpoint,
|
||||
"embedding_dimensions": profile.embedding_dimensions,
|
||||
"has_api_key": profile.has_api_key,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def sdk_relevance_floor() -> float:
|
||||
raw = os.environ.get("ONE_MEMORY_HY_RELEVANCE_FLOOR", "").strip()
|
||||
if not raw:
|
||||
@@ -224,10 +276,8 @@ def build_sdk_config(
|
||||
embedding_key = None
|
||||
dimensions = profile.embedding_dimensions or BUILTIN_EMBEDDING_DIMENSIONS
|
||||
else:
|
||||
embedding_endpoint = _openai_endpoint(
|
||||
embedding_provider,
|
||||
profile.embedding_base_url or profile.base_url,
|
||||
)
|
||||
# Never the generation endpoint: the collection name hashes this one.
|
||||
embedding_endpoint = _openai_endpoint(embedding_provider, profile.embedding_base_url)
|
||||
embedding_key = "ollama" if embedding_provider is ProviderKind.OLLAMA else api_key
|
||||
if embedding_provider is ProviderKind.OPENAI_COMPATIBLE and not embedding_key:
|
||||
raise HySdkRuntimeError("hy_sdk_embedding_api_key_missing")
|
||||
@@ -244,14 +294,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 +353,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 +417,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)
|
||||
|
||||
|
||||
@@ -374,7 +492,7 @@ def vector_collection_bases(profile: ModelProfileSnapshot) -> tuple[str, tuple[s
|
||||
endpoint, dimensions = None, profile.embedding_dimensions or BUILTIN_EMBEDDING_DIMENSIONS
|
||||
legacy_endpoint = "local-onnx"
|
||||
else:
|
||||
endpoint = _openai_endpoint(provider, profile.embedding_base_url or profile.base_url)
|
||||
endpoint = _openai_endpoint(provider, profile.embedding_base_url)
|
||||
dimensions = profile.embedding_dimensions
|
||||
legacy_endpoint = endpoint
|
||||
legacy_model = profile.embedding_model or "bge-small-zh-v1.5"
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from collections.abc import Mapping
|
||||
from datetime import UTC, datetime
|
||||
from typing import Literal, NamedTuple, cast
|
||||
@@ -151,6 +152,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 +169,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),
|
||||
@@ -229,6 +234,10 @@ class HySdkMemoryPage(BaseModel):
|
||||
raise HySdkReadError("hy_memory_list_shape_unknown")
|
||||
if raw.get("scene") == "all":
|
||||
return cls._parse_all_scenes(raw, limit=limit, offset=offset, skip_invalid=skip_invalid)
|
||||
if raw.get("scene") == "productivity":
|
||||
return cls._parse_experiences(
|
||||
raw, limit=limit, offset=offset, skip_invalid=skip_invalid
|
||||
)
|
||||
rows: list[object] = []
|
||||
total = 0
|
||||
for bucket_name, rows_name in (("vdb", "memories"), ("graph", "nodes")):
|
||||
@@ -325,6 +334,51 @@ class HySdkMemoryPage(BaseModel):
|
||||
skipped,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def _parse_experiences(
|
||||
cls,
|
||||
raw: Mapping[str, object],
|
||||
*,
|
||||
limit: int,
|
||||
offset: int,
|
||||
skip_invalid: bool,
|
||||
) -> tuple[HySdkMemoryPage, int]:
|
||||
"""``list_memories(scene="working"|"coding")``: that scene's experiences.
|
||||
|
||||
The SDK names the scene once, in ``variant``; each row is a bare
|
||||
experience, so the kind and variant are added before parsing.
|
||||
"""
|
||||
rows = raw.get("experiences")
|
||||
total = raw.get("total")
|
||||
variant = raw.get("variant")
|
||||
if (
|
||||
not isinstance(rows, list)
|
||||
or not isinstance(total, int)
|
||||
or isinstance(total, bool)
|
||||
or total < 0
|
||||
or variant not in {"working", "coding"}
|
||||
):
|
||||
raise HySdkReadError("hy_memory_list_shape_unknown")
|
||||
memories: list[HySdkMemoryRecord] = []
|
||||
skipped = 0
|
||||
for row in rows:
|
||||
try:
|
||||
if not isinstance(row, Mapping):
|
||||
raise HySdkReadError("hy_memory_record_shape_unknown")
|
||||
record = HySdkMemoryRecord.from_sdk(
|
||||
{**row, "kind": "experience", "variant": variant}
|
||||
)
|
||||
except HySdkReadError:
|
||||
if not skip_invalid:
|
||||
raise
|
||||
skipped += 1
|
||||
continue
|
||||
memories.append(record)
|
||||
return (
|
||||
cls(memories=tuple(memories), total=total, limit=limit, offset=offset),
|
||||
skipped,
|
||||
)
|
||||
|
||||
|
||||
class HySdkRawRecord(BaseModel):
|
||||
model_config = ConfigDict(frozen=True, extra="forbid")
|
||||
@@ -338,6 +392,8 @@ class HySdkRawRecord(BaseModel):
|
||||
memory_at: int | None = None
|
||||
gmt_created: int | None = None
|
||||
custom: dict[str, object] = Field(default_factory=dict)
|
||||
#: active, or shadow once pro extracted it; None when the SDK gave none.
|
||||
status: str | None = None
|
||||
|
||||
@classmethod
|
||||
def from_sdk(cls, raw: object) -> HySdkRawRecord:
|
||||
@@ -364,11 +420,34 @@ class HySdkRawRecord(BaseModel):
|
||||
raw.get("gmt_created") if isinstance(raw.get("gmt_created"), int) else None
|
||||
),
|
||||
custom=dict(custom),
|
||||
status=str(raw["status"]) if isinstance(raw.get("status"), str) else None,
|
||||
)
|
||||
except (KeyError, TypeError, ValueError) as error:
|
||||
raise HySdkReadError("hy_memory_raw_shape_unknown") from error
|
||||
|
||||
|
||||
_RAW_TURN_LINE = re.compile(r"^\[(user|assistant|tool|system)\]:\s?(.*)$")
|
||||
|
||||
|
||||
def raw_turn_messages(content: str) -> tuple[tuple[str, str], ...]:
|
||||
"""The `(role, text)` messages of one SDK raw turn.
|
||||
|
||||
The SDK stores a turn as `[user]: …\\n[assistant]: …`; a line without a
|
||||
role prefix continues the message above it. Content that names no role
|
||||
at all is one message of unknown role (`""`).
|
||||
"""
|
||||
messages: list[list[str]] = []
|
||||
for line in content.splitlines():
|
||||
match = _RAW_TURN_LINE.match(line)
|
||||
if match is not None:
|
||||
messages.append([match.group(1), match.group(2)])
|
||||
elif messages:
|
||||
messages[-1][1] += "\n" + line
|
||||
else:
|
||||
messages.append(["", line])
|
||||
return tuple((role, text) for role, text in messages)
|
||||
|
||||
|
||||
class HySdkRawHit(HySdkRawRecord):
|
||||
score: float = Field(ge=0, le=1)
|
||||
|
||||
@@ -463,6 +542,96 @@ class HySdkUserProfile(BaseModel):
|
||||
)
|
||||
|
||||
|
||||
class HySdkProfileVersion(BaseModel):
|
||||
"""One version on an L0 basic-info chain."""
|
||||
|
||||
model_config = ConfigDict(frozen=True, extra="forbid")
|
||||
|
||||
memory_id: str
|
||||
content: str
|
||||
#: Conversation time as the SDK renders it on a chain (epoch or text).
|
||||
memory_at: int | str | None = None
|
||||
gmt_created: int | None = None
|
||||
#: The chain head: the version the profile shows now.
|
||||
current: bool = False
|
||||
|
||||
|
||||
class HySdkProfileChain(BaseModel):
|
||||
"""One L0 basic-info head and its versions, latest first."""
|
||||
|
||||
model_config = ConfigDict(frozen=True, extra="forbid")
|
||||
|
||||
memory_id: str
|
||||
content: str
|
||||
status: str
|
||||
memory_at: int | None = None
|
||||
gmt_created: int | None = None
|
||||
#: The structured fields this head holds (`basic_info_kv`).
|
||||
basic_info: dict[str, object] = Field(default_factory=dict)
|
||||
versions: tuple[HySdkProfileVersion, ...]
|
||||
|
||||
|
||||
class HySdkProfileVersions(BaseModel):
|
||||
"""`get_user_basic_info`: the long-term profile's L0 version chains."""
|
||||
|
||||
model_config = ConfigDict(frozen=True, extra="forbid")
|
||||
|
||||
request_id: str
|
||||
chains: tuple[HySdkProfileChain, ...]
|
||||
elapsed_ms: float = Field(ge=0)
|
||||
|
||||
@classmethod
|
||||
def from_sdk(cls, raw: object) -> HySdkProfileVersions:
|
||||
if not isinstance(raw, Mapping):
|
||||
raise HySdkReadError("hy_memory_profile_versions_shape_unknown")
|
||||
request_id = raw.get("request_id")
|
||||
rows = raw.get("memories")
|
||||
elapsed_ms = raw.get("elapsed_ms")
|
||||
if (
|
||||
not isinstance(request_id, str)
|
||||
or not isinstance(rows, list)
|
||||
or not isinstance(elapsed_ms, int | float)
|
||||
or isinstance(elapsed_ms, bool)
|
||||
):
|
||||
raise HySdkReadError("hy_memory_profile_versions_shape_unknown")
|
||||
chains: list[HySdkProfileChain] = []
|
||||
for row in rows:
|
||||
if not isinstance(row, Mapping) or not isinstance(row.get("memory_id"), str):
|
||||
raise HySdkReadError("hy_memory_profile_versions_shape_unknown")
|
||||
head_id = str(row["memory_id"])
|
||||
chain = row.get("evolution_chain")
|
||||
entries = chain if isinstance(chain, list) and chain else [{**row, "node_id": head_id}]
|
||||
versions = tuple(
|
||||
HySdkProfileVersion(
|
||||
memory_id=str(entry.get("node_id") or entry.get("memory_id")),
|
||||
content=str(entry.get("content") or ""),
|
||||
memory_at=(
|
||||
entry.get("memory_at")
|
||||
if isinstance(entry.get("memory_at"), int | str)
|
||||
and not isinstance(entry.get("memory_at"), bool)
|
||||
else None
|
||||
),
|
||||
gmt_created=_epoch(entry.get("gmt_created")),
|
||||
current=str(entry.get("node_id") or entry.get("memory_id")) == head_id,
|
||||
)
|
||||
for entry in entries
|
||||
if isinstance(entry, Mapping) and (entry.get("node_id") or entry.get("memory_id"))
|
||||
)
|
||||
basic_info = row.get("basic_info_kv")
|
||||
chains.append(
|
||||
HySdkProfileChain(
|
||||
memory_id=head_id,
|
||||
content=str(row.get("content") or ""),
|
||||
status=str(row.get("status") or "active"),
|
||||
memory_at=_epoch(row.get("memory_at")),
|
||||
gmt_created=_epoch(row.get("gmt_created")),
|
||||
basic_info=dict(basic_info) if isinstance(basic_info, Mapping) else {},
|
||||
versions=versions,
|
||||
)
|
||||
)
|
||||
return cls(request_id=request_id, chains=tuple(chains), elapsed_ms=float(elapsed_ms))
|
||||
|
||||
|
||||
def parse_sdk_search(raw: object, *, error_code: str) -> tuple[SemanticMemoryHit, ...]:
|
||||
rows = _reply_rows(raw, error_code)
|
||||
found: list[SemanticMemoryHit] = []
|
||||
@@ -489,6 +658,120 @@ def parse_sdk_search(raw: object, *, error_code: str) -> tuple[SemanticMemoryHit
|
||||
return tuple(found)
|
||||
|
||||
|
||||
def parse_sdk_lane_search(raw: object, *, error_code: str) -> tuple[SemanticMemoryHit, ...]:
|
||||
"""Every scene's search reply as hits that name their lane and namespace.
|
||||
|
||||
`parse_sdk_search` reads the chat reply only and forgets which lane a hit
|
||||
came from. A caller may now pick the scene and a floor per lane, so this
|
||||
reads all three reply shapes:
|
||||
|
||||
- chat: `memories` as `{schema, proactive, normal}` (or one list, normal);
|
||||
- working/coding: `facts` and `experiences` of that variant;
|
||||
- all: one fused `memories` list whose rows carry `scene`, `kind` and
|
||||
their own lane's score (`lane_score`). The fused `score` is a rank
|
||||
fusion (1/(60+rank)), not a relevance, so the lane score is reported.
|
||||
|
||||
Highest score first, first occurrence of an id kept.
|
||||
"""
|
||||
if not isinstance(raw, Mapping):
|
||||
raise HySdkReadError(error_code)
|
||||
scene = raw.get("scene")
|
||||
found: list[SemanticMemoryHit] = []
|
||||
if scene == "all":
|
||||
rows = raw.get("memories")
|
||||
if not isinstance(rows, list) or any(not isinstance(row, Mapping) for row in rows):
|
||||
raise HySdkReadError(error_code)
|
||||
for row in rows:
|
||||
hit = _lane_hit(row, scene=str(row.get("scene") or "chat"), score_key="lane_score")
|
||||
if hit is not None:
|
||||
found.append(hit)
|
||||
elif scene in ("productivity", "working", "coding"):
|
||||
variant = raw.get("variant") if raw.get("variant") in {"working", "coding"} else None
|
||||
lane = variant or (scene if scene in {"working", "coding"} else "working")
|
||||
for key, kind in (("facts", "fact"), ("experiences", "experience")):
|
||||
rows = raw.get(key) or []
|
||||
if not isinstance(rows, list) or any(not isinstance(row, Mapping) for row in rows):
|
||||
raise HySdkReadError(error_code)
|
||||
for row in rows:
|
||||
hit = _lane_hit({**row, "kind": kind}, scene=lane, score_key="score")
|
||||
if hit is not None:
|
||||
found.append(hit)
|
||||
else:
|
||||
memories = raw.get("memories")
|
||||
if isinstance(memories, Mapping):
|
||||
if set(memories) != {"schema", "proactive", "normal"}:
|
||||
raise HySdkReadError(error_code)
|
||||
channels = tuple(memories.items())
|
||||
elif isinstance(memories, list):
|
||||
channels = (("normal", memories),)
|
||||
else:
|
||||
raise HySdkReadError(error_code)
|
||||
for lane, rows in channels:
|
||||
if not isinstance(rows, list) or any(not isinstance(row, Mapping) for row in rows):
|
||||
raise HySdkReadError(error_code)
|
||||
for row in rows:
|
||||
hit = _lane_hit(row, scene="chat", score_key="score", lane=str(lane))
|
||||
if hit is not None:
|
||||
found.append(hit)
|
||||
ordered: list[SemanticMemoryHit] = []
|
||||
seen: set[tuple[str, str, str | None]] = set()
|
||||
for hit in sorted(found, key=lambda value: value.score, reverse=True):
|
||||
identity = (hit.memory_id, hit.kind, hit.variant)
|
||||
if identity in seen:
|
||||
continue
|
||||
seen.add(identity)
|
||||
ordered.append(hit)
|
||||
return tuple(ordered)
|
||||
|
||||
|
||||
def _lane_hit(
|
||||
row: Mapping[str, object],
|
||||
*,
|
||||
scene: str,
|
||||
score_key: str,
|
||||
lane: str | None = None,
|
||||
) -> SemanticMemoryHit | None:
|
||||
kind = str(row.get("kind")) if row.get("kind") in {"experience", "fact"} else "chat"
|
||||
if kind == "chat":
|
||||
memory_id = row.get("memory_id") or row.get("id")
|
||||
variant = None
|
||||
hit_lane = lane or "normal"
|
||||
else:
|
||||
id_key = "experience_id" if kind == "experience" else "fact_id"
|
||||
memory_id = row.get("memory_id") or row.get(id_key)
|
||||
variant = scene if scene in {"working", "coding"} else None
|
||||
if variant is None:
|
||||
return None
|
||||
hit_lane = variant
|
||||
if not isinstance(memory_id, str) or not memory_id:
|
||||
return None
|
||||
value = row.get(score_key)
|
||||
if not isinstance(value, int | float) or isinstance(value, bool):
|
||||
value = row.get("score")
|
||||
score = (
|
||||
max(0.0, min(1.0, float(value)))
|
||||
if isinstance(value, int | float) and not isinstance(value, bool)
|
||||
else 0.0
|
||||
)
|
||||
if kind != "chat":
|
||||
return SemanticMemoryHit(
|
||||
memory_id=memory_id, score=score, lane=hit_lane, kind=kind, variant=variant
|
||||
)
|
||||
content = row.get("content")
|
||||
member_ids, formatting_complete = _formatted_member_ids(row)
|
||||
return SemanticMemoryHit(
|
||||
memory_id=memory_id,
|
||||
score=score,
|
||||
formatted_content=(
|
||||
content.strip() if isinstance(content, str) and content.strip() else None
|
||||
),
|
||||
member_ids=member_ids,
|
||||
sdk_formatted=True,
|
||||
formatting_complete=formatting_complete,
|
||||
lane=hit_lane,
|
||||
)
|
||||
|
||||
|
||||
class LibraryHit(NamedTuple):
|
||||
"""One keyword match in a library search window, with its namespace."""
|
||||
|
||||
|
||||
@@ -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,8 +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). 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.
|
||||
"""
|
||||
@@ -27,11 +28,24 @@ 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, ...]]
|
||||
#: (query, source, statuses, scene, user, other filters as sorted name/values pairs)
|
||||
WindowKey = tuple[
|
||||
str, str | None, tuple[str, ...], str, str, tuple[tuple[str, tuple[str, ...]], ...]
|
||||
]
|
||||
|
||||
|
||||
def window_key(query: str, source: str | None, statuses: list[str]) -> WindowKey:
|
||||
return (query, source, tuple(sorted(set(statuses))))
|
||||
def window_key(
|
||||
query: str,
|
||||
source: str | None,
|
||||
statuses: list[str],
|
||||
scene: str = "all",
|
||||
user_id: str = "local",
|
||||
filters: dict[str, list[str]] | None = None,
|
||||
) -> WindowKey:
|
||||
extra = tuple(
|
||||
sorted((name, tuple(sorted(set(values)))) for name, values in (filters or {}).items())
|
||||
)
|
||||
return (query, source, tuple(sorted(set(statuses))), scene, user_id, extra)
|
||||
|
||||
|
||||
@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))
|
||||
+109
-142
@@ -1,12 +1,21 @@
|
||||
"""Add one memory directly: `POST /api/v2/memories` and its status read.
|
||||
|
||||
The public API has no "notes": a hand-added memory is filed as manual Evidence
|
||||
and turned into memories by the durable write queue. Storage keeps its
|
||||
pre-rename identifiers (`memory_note_requests`, source `one-memory-manual`,
|
||||
`input_mode=direct_memory_note`, conversation `note:<request_id>`) because they
|
||||
are persisted and feed evidence ids; renaming them would change stored data.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
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
|
||||
|
||||
@@ -26,9 +35,10 @@ 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"
|
||||
MemoryNoteState = Literal[
|
||||
MemoryAddState = Literal[
|
||||
"waiting_for_model",
|
||||
"waiting_for_permission",
|
||||
"pending",
|
||||
@@ -39,36 +49,38 @@ MemoryNoteState = Literal[
|
||||
]
|
||||
|
||||
|
||||
class MemoryNoteInput(BaseModel):
|
||||
class MemoryAddInput(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
|
||||
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
|
||||
def meaningful_text(cls, value: str) -> str:
|
||||
if not value.strip():
|
||||
raise ValueError("memory_note_empty")
|
||||
raise ValueError("memory_add_empty")
|
||||
return value
|
||||
|
||||
|
||||
class MemoryNoteReceipt(BaseModel):
|
||||
class MemoryAddReceipt(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
|
||||
evidence_id: str
|
||||
text: str
|
||||
scope: Literal["all-apps"] = "all-apps"
|
||||
created_at: datetime
|
||||
state: MemoryNoteState
|
||||
state: MemoryAddState
|
||||
task_id: str | None = None
|
||||
memory_ids: list[str] = Field(default_factory=list)
|
||||
error: str | None = None
|
||||
|
||||
|
||||
class LegacyMemoryNoteReader:
|
||||
"""Read pre-cutover note receipts without retaining any write runtime."""
|
||||
class LegacyMemoryAdditionReader:
|
||||
"""Read pre-cutover addition receipts without retaining any write runtime."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
@@ -79,7 +91,7 @@ class LegacyMemoryNoteReader:
|
||||
self.governance = governance
|
||||
|
||||
|
||||
class MemoryNoteStore:
|
||||
class MemoryAdditionStore:
|
||||
def __init__(
|
||||
self,
|
||||
evidence: EvidenceStore,
|
||||
@@ -92,44 +104,56 @@ class MemoryNoteStore:
|
||||
self.evidence = evidence
|
||||
self.processor = processor
|
||||
# D52: the production composition root passes a UnifiedWriteIntake so a
|
||||
# manual note reaches the durable queue like every other entry. It stays
|
||||
# manual addition reaches the durable queue like every other entry. It stays
|
||||
# optional so isolated tests can keep constructing the store directly.
|
||||
self.intake = intake
|
||||
self.sdk_authority = sdk_authority
|
||||
self.write_queue = write_queue
|
||||
|
||||
def save(self, note: MemoryNoteInput) -> EvidenceRecord:
|
||||
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:{note.request_id}",
|
||||
message_id=str(note.request_id),
|
||||
conversation_id=f"note:{addition.request_id}",
|
||||
message_id=str(addition.request_id),
|
||||
role="user",
|
||||
written_by=WrittenBy.PERSON,
|
||||
created_at=now,
|
||||
verbatim=note.text,
|
||||
verbatim=addition.text,
|
||||
origin_meta={
|
||||
EvidenceOrigin.MANUAL_IMPORT: OriginMetadata(
|
||||
raw_meta={
|
||||
"input_mode": "direct_memory_note",
|
||||
"recall_scope": note.scope,
|
||||
"recall_scope": addition.scope,
|
||||
}
|
||||
)
|
||||
},
|
||||
)
|
||||
if self.sdk_authority:
|
||||
if self.intake is None or self.write_queue is None:
|
||||
raise RuntimeError("sdk_memory_note_intake_unavailable")
|
||||
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:
|
||||
raise ValueError("memory_note_request_conflict")
|
||||
raise ValueError("memory_add_conflict")
|
||||
return current
|
||||
receipt = self.intake.submit_records(
|
||||
(record,),
|
||||
@@ -159,13 +183,13 @@ class MemoryNoteStore:
|
||||
(SOURCE_ID,),
|
||||
).fetchone()
|
||||
if grant and grant[0] is not None:
|
||||
raise ValueError("memory_note_source_revoked")
|
||||
raise ValueError("memory_add_source_revoked")
|
||||
existing = db.execute(
|
||||
"SELECT verbatim FROM evidence WHERE evidence_id=?", (value.evidence_id,)
|
||||
).fetchone()
|
||||
if existing:
|
||||
if existing[0] != note.text:
|
||||
raise ValueError("memory_note_request_conflict")
|
||||
if existing[0] != addition.text:
|
||||
raise ValueError("memory_add_conflict")
|
||||
else:
|
||||
self.evidence.put(value, connection=db)
|
||||
db.execute(
|
||||
@@ -184,7 +208,7 @@ class MemoryNoteStore:
|
||||
)
|
||||
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)
|
||||
@@ -203,11 +227,11 @@ class MemoryNoteStore:
|
||||
(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":
|
||||
@@ -218,11 +242,11 @@ class MemoryNoteStore:
|
||||
(SOURCE_ID,),
|
||||
).fetchone()
|
||||
if grant is None or grant[0] is not None:
|
||||
raise ValueError("memory_note_source_revoked")
|
||||
raise ValueError("memory_add_source_revoked")
|
||||
return record
|
||||
|
||||
def receipt(self, evidence_id: str) -> MemoryNoteReceipt:
|
||||
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:
|
||||
@@ -232,7 +256,7 @@ class MemoryNoteStore:
|
||||
).fetchone()[0]
|
||||
durable = self.write_queue.get(str(request_id))
|
||||
durable_state = durable.state if durable is not None else "failed"
|
||||
mapped: MemoryNoteState = {
|
||||
mapped: MemoryAddState = {
|
||||
"pending": "pending",
|
||||
"running": "running",
|
||||
"waiting_for_model": "waiting_for_model",
|
||||
@@ -241,7 +265,7 @@ class MemoryNoteStore:
|
||||
"ready" if durable is not None and durable.sdk_memory_ids else "no_memories"
|
||||
),
|
||||
}.get(durable_state, "failed")
|
||||
return MemoryNoteReceipt(
|
||||
return MemoryAddReceipt(
|
||||
evidence_id=evidence_id,
|
||||
text=sanitize_model_input(record.verbatim),
|
||||
created_at=record.created_at or record.imported_at,
|
||||
@@ -250,7 +274,7 @@ class MemoryNoteStore:
|
||||
error=durable.detail if durable is not None else "write_missing",
|
||||
)
|
||||
if self.processor is None:
|
||||
raise RuntimeError("legacy_memory_note_processor_unavailable")
|
||||
raise RuntimeError("legacy_memory_add_processor_unavailable")
|
||||
task = self.processor.tasks.task_for_evidence(evidence_id)
|
||||
memory_ids = []
|
||||
if task:
|
||||
@@ -258,7 +282,7 @@ class MemoryNoteStore:
|
||||
memory = self.processor.governance.get(output.memory_id, user_id="local")
|
||||
if memory and memory.status is MemoryStatus.CONFIRMED:
|
||||
memory_ids.append(memory.memory_id)
|
||||
state: MemoryNoteState
|
||||
state: MemoryAddState
|
||||
if task is None or task.status == MemoryTaskStatus.WAITING_FOR_MODEL:
|
||||
state = "waiting_for_model"
|
||||
elif task.status == MemoryTaskStatus.WAITING_FOR_PERMISSION:
|
||||
@@ -274,7 +298,7 @@ class MemoryNoteStore:
|
||||
else:
|
||||
state = "failed"
|
||||
error = task.last_error_code if task else None
|
||||
return MemoryNoteReceipt(
|
||||
return MemoryAddReceipt(
|
||||
evidence_id=record.evidence_id,
|
||||
text=sanitize_model_input(record.verbatim),
|
||||
created_at=record.created_at or record.imported_at,
|
||||
@@ -284,36 +308,15 @@ class MemoryNoteStore:
|
||||
error=error,
|
||||
)
|
||||
|
||||
def list(self) -> list[MemoryNoteReceipt]:
|
||||
if self.sdk_authority:
|
||||
assert self.write_queue is not None
|
||||
with main_database(self.write_queue.database_path).read() as database:
|
||||
ids = [
|
||||
str(row[0])
|
||||
for row in database.execute(
|
||||
"SELECT note_id FROM memory_note_requests ORDER BY created_at DESC LIMIT 50"
|
||||
)
|
||||
]
|
||||
return [self.receipt(key) for key in ids]
|
||||
with main_database(self.evidence.database_path).read() as db:
|
||||
ids = [
|
||||
row[0]
|
||||
for row in db.execute(
|
||||
"SELECT e.evidence_id,o.raw_meta_json FROM evidence e "
|
||||
"JOIN source_grants g USING(user_id,source_id) "
|
||||
"JOIN evidence_origins o USING(evidence_id) WHERE e.user_id='local' "
|
||||
"AND e.source_id=? AND g.revoked_at IS NULL AND o.origin='manual_import' "
|
||||
"ORDER BY e.imported_at DESC,e.evidence_id LIMIT 50",
|
||||
(SOURCE_ID,),
|
||||
)
|
||||
if json.loads(row[1]).get("input_mode") == "direct_memory_note"
|
||||
]
|
||||
return [self.receipt(key) for key in ids]
|
||||
|
||||
def receipt_for_request(self, request_id: UUID) -> MemoryNoteReceipt:
|
||||
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,
|
||||
@@ -326,119 +329,83 @@ class MemoryNoteStore:
|
||||
)
|
||||
},
|
||||
)
|
||||
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_note_router(dependencies: Sequence[Depends]) -> APIRouter:
|
||||
router = APIRouter(
|
||||
prefix="/api/v1/memory-notes", tags=["memory-notes"], dependencies=dependencies
|
||||
)
|
||||
def memory_add_router(dependencies: Sequence[Depends]) -> APIRouter:
|
||||
"""`POST /api/v2/memories` and `GET /api/v2/memories/additions/{request_id}`.
|
||||
|
||||
def store(request: Request) -> MemoryNoteStore:
|
||||
return cast(MemoryNoteStore, request.app.state.memory_note_store)
|
||||
Both paths are outside the single-segment `/api/v2/memories/{memory_id}`
|
||||
routes; external (enveloped) callers are refused by
|
||||
`require_external_endpoint` because neither is a broker read route.
|
||||
"""
|
||||
router = APIRouter(prefix="/api/v2/memories", tags=["memories"], dependencies=dependencies)
|
||||
|
||||
async def schedule(
|
||||
notes: MemoryNoteStore,
|
||||
record: EvidenceRecord,
|
||||
*,
|
||||
readmit: bool = False,
|
||||
) -> MemoryNoteReceipt:
|
||||
def store(request: Request) -> MemoryAdditionStore:
|
||||
return cast(MemoryAdditionStore, request.app.state.memory_addition_store)
|
||||
|
||||
async def schedule(additions: MemoryAdditionStore, record: EvidenceRecord) -> None:
|
||||
try:
|
||||
if notes.sdk_authority:
|
||||
return await asyncio.to_thread(notes.receipt, record.evidence_id)
|
||||
if notes.processor is None:
|
||||
raise RuntimeError("legacy_memory_note_processor_unavailable")
|
||||
if notes.intake is None:
|
||||
if additions.sdk_authority:
|
||||
return
|
||||
if additions.processor is None:
|
||||
raise RuntimeError("legacy_memory_add_processor_unavailable")
|
||||
if additions.intake is None:
|
||||
# Isolated tests construct the store without a queue.
|
||||
await notes.processor.enqueue_conversation_off_thread(record)
|
||||
await additions.processor.enqueue_conversation_off_thread(record)
|
||||
else:
|
||||
# Production: submit to the durable queue first. The drain runs
|
||||
# the same supervisor path, so create/process semantics are
|
||||
# unchanged; replaying the same request returns the original
|
||||
# `entry:<evidence_id>` job instead of a second HY call.
|
||||
if readmit:
|
||||
# `/{evidence_id}/process` is the explicit retry signal.
|
||||
# It also revives pre-upgrade permission waiters.
|
||||
await asyncio.to_thread(notes.intake.queue.readmit_parked)
|
||||
submitted = await asyncio.to_thread(
|
||||
notes.intake.submit_evidence,
|
||||
# Production: submit to the durable queue. Replaying the same
|
||||
# request returns the original `entry:<evidence_id>` job
|
||||
# instead of a second HY call.
|
||||
await asyncio.to_thread(
|
||||
additions.intake.submit_evidence,
|
||||
record,
|
||||
first_round=1,
|
||||
last_round=1,
|
||||
priority="live",
|
||||
)
|
||||
if readmit:
|
||||
await asyncio.to_thread(
|
||||
notes.intake.readmit_cancelled_evidence,
|
||||
record,
|
||||
job_id=submitted.record.job_id,
|
||||
first_round=1,
|
||||
last_round=1,
|
||||
priority="live",
|
||||
)
|
||||
except SecretStoreUnavailableError:
|
||||
saved = await asyncio.to_thread(notes.receipt, record.evidence_id)
|
||||
return saved.model_copy(
|
||||
update={"state": "waiting_for_model", "error": "secure_storage_unavailable"}
|
||||
)
|
||||
return await asyncio.to_thread(notes.receipt, record.evidence_id)
|
||||
# The receipt already reports the saved text; the queue retries
|
||||
# once secure storage is back.
|
||||
return
|
||||
|
||||
@router.post("", response_model=MemoryNoteReceipt, status_code=201)
|
||||
async def create(
|
||||
note: MemoryNoteInput,
|
||||
@router.post("", response_model=MemoryAddReceipt, status_code=201)
|
||||
async def add_memory(
|
||||
addition: MemoryAddInput,
|
||||
request: Request,
|
||||
background: BackgroundTasks,
|
||||
) -> MemoryNoteReceipt:
|
||||
notes = store(request)
|
||||
) -> MemoryAddReceipt:
|
||||
additions = store(request)
|
||||
try:
|
||||
record = await asyncio.to_thread(notes.save, note)
|
||||
record = await asyncio.to_thread(additions.save, addition)
|
||||
except ValueError as error:
|
||||
raise HTTPException(409, str(error)) from error
|
||||
saved = await asyncio.to_thread(notes.receipt, record.evidence_id)
|
||||
background.add_task(schedule, notes, record)
|
||||
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("", response_model=list[MemoryNoteReceipt])
|
||||
def listing(request: Request) -> list[MemoryNoteReceipt]:
|
||||
return store(request).list()
|
||||
|
||||
@router.get("/operations/{request_id}", response_model=MemoryNoteReceipt)
|
||||
def operation(request_id: UUID, request: Request) -> MemoryNoteReceipt:
|
||||
@router.get("/additions/{request_id}", response_model=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_note_operation_not_found") from error
|
||||
|
||||
@router.get("/{evidence_id}", response_model=MemoryNoteReceipt)
|
||||
def get(evidence_id: str, request: Request) -> MemoryNoteReceipt:
|
||||
try:
|
||||
return store(request).receipt(evidence_id)
|
||||
except KeyError as error:
|
||||
raise HTTPException(404, "memory_note_not_found") from error
|
||||
raise HTTPException(404, "memory_add_not_found") from error
|
||||
except ValueError as error:
|
||||
raise HTTPException(409, str(error)) from error
|
||||
|
||||
@router.post("/{evidence_id}/process", response_model=MemoryNoteReceipt)
|
||||
async def process(evidence_id: str, request: Request) -> MemoryNoteReceipt:
|
||||
notes = store(request)
|
||||
try:
|
||||
record = await asyncio.to_thread(notes.require, evidence_id)
|
||||
except KeyError as error:
|
||||
raise HTTPException(404, "memory_note_not_found") from error
|
||||
except ValueError as error:
|
||||
raise HTTPException(409, str(error)) from error
|
||||
# This route is the explicit "the condition that parked it may have
|
||||
# changed" signal (the person just granted consent or fixed the model),
|
||||
# which is when `readmit_parked` is meant to run.
|
||||
return await schedule(notes, record, readmit=True)
|
||||
|
||||
return router
|
||||
Loaded 100 of 267 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user