From 7b95a54317cdc9e03d578bda31a3e0a737e8205a Mon Sep 17 00:00:00 2001 From: ArvinQi <12555402+ArvinQi@users.noreply.github.com> Date: Mon, 9 Nov 2026 22:06:39 +0800 Subject: [PATCH 01/39] =?UTF-8?q?feat(memory-mcp):=20=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E7=BA=A7=20token=20=E5=AD=98=E5=82=A8=E4=B8=8E=20HTTP=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 按 review 把"实例共享 token"换成"用户级 token"的存储与发放层: - 迁移 026 建 memory_mcp_tokens 表:token 绑定 user,带客户端名、可限定专家 范围、可单独吊销;服务端只存 sha256(明文只在创建时返回一次) - MemoryMcpTokenRepo(create / list_for_user / get_by_token_hash / get_for_user / revoke / touch / any_active)+ RepoBundle / SharedServices 接线 - token 管理 API:POST/GET/DELETE /api/memory-mcp/tokens,创建时逐个校验专家 存在且当前用户可访问,撤销只能撤自己的 - 迁移版本升到 26:同步 8 处 schema 断言 --- src/octop/api/routers/memory_mcp.py | 144 ++++++++++++ src/octop/infra/agents/memory_tokens.py | 39 ++++ .../migrations/026_memory_mcp_tokens.pg.sql | 22 ++ .../db/migrations/026_memory_mcp_tokens.sql | 22 ++ src/octop/infra/db/repos/memory_mcp_tokens.py | 142 ++++++++++++ src/octop/infra/db/services.py | 7 + tests/unit/api/test_memory_mcp_token_api.py | 186 +++++++++++++++ tests/unit/backup/test_system_archive.py | 4 +- tests/unit/db/test_agent_profile_columns.py | 2 +- tests/unit/db/test_clip_thread_title.py | 2 +- tests/unit/db/test_db_pool.py | 14 +- tests/unit/db/test_memory_mcp_tokens.py | 213 ++++++++++++++++++ tests/unit/db/test_published_experts_repo.py | 2 +- tests/unit/db/test_repo_knowledge.py | 2 +- tests/unit/db/test_skill_package_icons.py | 4 +- tests/unit/db/test_skill_packages_repo.py | 2 +- tests/unit/users/test_group_access.py | 2 +- 17 files changed, 792 insertions(+), 17 deletions(-) create mode 100644 src/octop/api/routers/memory_mcp.py create mode 100644 src/octop/infra/agents/memory_tokens.py create mode 100644 src/octop/infra/db/migrations/026_memory_mcp_tokens.pg.sql create mode 100644 src/octop/infra/db/migrations/026_memory_mcp_tokens.sql create mode 100644 src/octop/infra/db/repos/memory_mcp_tokens.py create mode 100644 tests/unit/api/test_memory_mcp_token_api.py create mode 100644 tests/unit/db/test_memory_mcp_tokens.py diff --git a/src/octop/api/routers/memory_mcp.py b/src/octop/api/routers/memory_mcp.py new file mode 100644 index 0000000000..5b253cd153 --- /dev/null +++ b/src/octop/api/routers/memory_mcp.py @@ -0,0 +1,144 @@ +"""User-level API tokens for the memory MCP server. + +Tokens are personal: a caller acts as the user the token was issued to, so +``X-Octop-User-Id`` is never trusted. Only the SHA-256 digest is stored; the +plaintext token is returned once, at creation. +""" + +from __future__ import annotations + +from typing import TYPE_CHECKING + +from fastapi import APIRouter, Depends, status +from pydantic import BaseModel, Field + +from octop.api.common.agent import assert_agent_access_row +from octop.api.deps import current_user, get_server, require_permission +from octop.infra.agents.memory_tokens import generate_token, token_hint +from octop.infra.errors import ErrorCode, OctopError +from octop.infra.server import OctopServer +from octop.infra.users.identity import User +from octop.infra.utils.ulid import new_ulid + +if TYPE_CHECKING: + from octop.infra.db.repos.memory_mcp_tokens import MemoryMcpTokenRow + +router = APIRouter( + prefix="/memory-mcp/tokens", + dependencies=[Depends(require_permission("memory"))], +) + + +class TokenCreateBody(BaseModel): + name: str = Field( + min_length=1, + max_length=64, + description="Client name shown in the token list, e.g. `codex@macbook`.", + ) + expert_ids: list[str] | None = Field( + default=None, + description=( + "Experts this token may use. Null or empty means every expert " + "the issuing user can access." + ), + ) + + +class TokenOut(BaseModel): + token_id: str = Field(description="Public token id used to revoke it.") + name: str = Field(description="Client name supplied at creation.") + expert_ids: list[str] | None = Field( + default=None, + description="Expert allow-list; null means no restriction.", + ) + token_hint: str = Field(description="Masked token for display, e.g. `omcp_abcd…wxyz`.") + created_at: str = Field(description="Creation time (ISO-8601 UTC).") + last_used_at: str | None = Field(default=None, description="Last authenticated use.") + revoked_at: str | None = Field(default=None, description="Revocation time, if revoked.") + + +class TokenCreated(TokenOut): + token: str = Field( + description="Plaintext token. Returned only once; the server keeps only its digest." + ) + + +class TokenRevoked(BaseModel): + revoked: bool = Field(description="True when this call revoked the token.") + + +def _to_out(row: MemoryMcpTokenRow) -> TokenOut: + return TokenOut( + token_id=row.token_id, + name=row.name, + expert_ids=row.expert_ids, + token_hint=row.token_hint, + created_at=row.created_at, + last_used_at=row.last_used_at, + revoked_at=row.revoked_at, + ) + + +@router.post( + "", + status_code=status.HTTP_201_CREATED, + response_model=TokenCreated, + summary="Create a memory MCP token", + description="Issues a personal token for one MCP client. The plaintext token is returned once.", +) +def create_token( + body: TokenCreateBody, + user: User = Depends(current_user), + server: OctopServer = Depends(get_server), +) -> TokenCreated: + assert server.services is not None + repos = server.services.repos + for expert_id in body.expert_ids or []: + row = repos.agent_repo.get(expert_id) + if row is None: + raise OctopError(ErrorCode.AGENT_NOT_FOUND, f"agent {expert_id!r} not found") + assert_agent_access_row(row, user, repos=repos) + + plaintext, token_hash = generate_token() + token_row = repos.memory_mcp_token_repo.create( + user_id=user.id, + name=body.name, + expert_ids=body.expert_ids or None, + token_id=new_ulid(), + token_hash=token_hash, + token_hint=token_hint(plaintext), + ) + return TokenCreated(**_to_out(token_row).model_dump(), token=plaintext) + + +@router.get( + "", + response_model=list[TokenOut], + summary="List my memory MCP tokens", + description="Lists every token issued by the caller, including revoked ones.", +) +def list_tokens( + user: User = Depends(current_user), + server: OctopServer = Depends(get_server), +) -> list[TokenOut]: + assert server.services is not None + rows = server.services.repos.memory_mcp_token_repo.list_for_user(user.id) + return [_to_out(row) for row in rows] + + +@router.delete( + "/{token_id}", + response_model=TokenRevoked, + summary="Revoke a memory MCP token", + description="Revokes one of the caller's own tokens. Other users' tokens are not found.", +) +def revoke_token( + token_id: str, + user: User = Depends(current_user), + server: OctopServer = Depends(get_server), +) -> TokenRevoked: + assert server.services is not None + repo = server.services.repos.memory_mcp_token_repo + if repo.get_for_user(token_id, user_id=user.id) is None: + raise OctopError(ErrorCode.NOT_FOUND, f"memory MCP token {token_id!r} not found") + return TokenRevoked(revoked=repo.revoke(token_id, user_id=user.id)) diff --git a/src/octop/infra/agents/memory_tokens.py b/src/octop/infra/agents/memory_tokens.py new file mode 100644 index 0000000000..2ae6bf6dba --- /dev/null +++ b/src/octop/infra/agents/memory_tokens.py @@ -0,0 +1,39 @@ +"""User-level API token helpers for the memory MCP server. + +Shared by the HTTP router that issues tokens and by MCP request +authentication. Only the SHA-256 hex digest is persisted; the plaintext +token is returned once at creation and never stored. +""" + +from __future__ import annotations + +import hashlib +import hmac +import secrets + +TOKEN_PREFIX = "omcp_" + +_TOKEN_BYTES = 32 +_HINT_HEAD = len(TOKEN_PREFIX) + 4 +_HINT_TAIL = 4 + + +def generate_token() -> tuple[str, str]: + """Return ``(plaintext, sha256_hex)`` for a fresh token.""" + plaintext = f"{TOKEN_PREFIX}{secrets.token_urlsafe(_TOKEN_BYTES)}" + return plaintext, hash_token(plaintext) + + +def hash_token(token: str) -> str: + """SHA-256 hex digest of *token* — the only form that is stored.""" + return hashlib.sha256(token.encode("utf-8")).hexdigest() + + +def token_matches(token: str, token_hash: str) -> bool: + """Constant-time check of *token* against a stored digest.""" + return hmac.compare_digest(hash_token(token), token_hash) + + +def token_hint(token: str) -> str: + """Display form with the middle hidden, e.g. ``omcp_abcd…wxyz``.""" + return f"{token[:_HINT_HEAD]}…{token[-_HINT_TAIL:]}" diff --git a/src/octop/infra/db/migrations/026_memory_mcp_tokens.pg.sql b/src/octop/infra/db/migrations/026_memory_mcp_tokens.pg.sql new file mode 100644 index 0000000000..0cabf44df6 --- /dev/null +++ b/src/octop/infra/db/migrations/026_memory_mcp_tokens.pg.sql @@ -0,0 +1,22 @@ +-- Schema v26: user-level API tokens for the memory MCP server. +-- +-- One row per client token. Only the SHA-256 hex digest is stored; the +-- plaintext token is shown once at creation. NULL expert_ids means the token +-- may use every expert its owner can access. + +CREATE TABLE IF NOT EXISTS memory_mcp_tokens ( + id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, + token_id TEXT NOT NULL UNIQUE, + user_id BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE, + name TEXT NOT NULL, + expert_ids TEXT, + token_hash TEXT NOT NULL UNIQUE, + token_hint TEXT NOT NULL, + created_at TEXT NOT NULL, + last_used_at TEXT, + revoked_at TEXT +); + +CREATE INDEX IF NOT EXISTS idx_memory_mcp_tokens_user ON memory_mcp_tokens(user_id); + +UPDATE _schema_version SET version = 26; diff --git a/src/octop/infra/db/migrations/026_memory_mcp_tokens.sql b/src/octop/infra/db/migrations/026_memory_mcp_tokens.sql new file mode 100644 index 0000000000..5e196202a0 --- /dev/null +++ b/src/octop/infra/db/migrations/026_memory_mcp_tokens.sql @@ -0,0 +1,22 @@ +-- Schema v26: user-level API tokens for the memory MCP server. +-- +-- One row per client token. Only the SHA-256 hex digest is stored; the +-- plaintext token is shown once at creation. NULL expert_ids means the token +-- may use every expert its owner can access. + +CREATE TABLE IF NOT EXISTS memory_mcp_tokens ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + token_id TEXT NOT NULL UNIQUE, + user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE, + name TEXT NOT NULL, + expert_ids TEXT, + token_hash TEXT NOT NULL UNIQUE, + token_hint TEXT NOT NULL, + created_at TEXT NOT NULL, + last_used_at TEXT, + revoked_at TEXT +); + +CREATE INDEX IF NOT EXISTS idx_memory_mcp_tokens_user ON memory_mcp_tokens(user_id); + +UPDATE _schema_version SET version = 26; diff --git a/src/octop/infra/db/repos/memory_mcp_tokens.py b/src/octop/infra/db/repos/memory_mcp_tokens.py new file mode 100644 index 0000000000..60e9ac5132 --- /dev/null +++ b/src/octop/infra/db/repos/memory_mcp_tokens.py @@ -0,0 +1,142 @@ +"""Memory MCP API tokens — one row per user-issued client token.""" + +from __future__ import annotations + +import json +from dataclasses import dataclass +from datetime import UTC, datetime + +from octop.infra.db.pool import DatabasePool +from octop.infra.db.repos._base import DbRow, map_rows + + +def _utc_now_iso() -> str: + return datetime.now(UTC).replace(microsecond=0).isoformat().replace("+00:00", "Z") + + +def _dump_expert_ids(expert_ids: list[str] | None) -> str | None: + return None if expert_ids is None else json.dumps(expert_ids) + + +def _load_expert_ids(raw: object) -> list[str] | None: + if raw is None: + return None + text = str(raw) + if not text: + return None + return [str(item) for item in json.loads(text)] + + +@dataclass(frozen=True) +class MemoryMcpTokenRow: + id: int + token_id: str + user_id: int + name: str + expert_ids: list[str] | None + token_hash: str + token_hint: str + created_at: str + last_used_at: str | None + revoked_at: str | None + + @classmethod + def from_row(cls, r: DbRow) -> MemoryMcpTokenRow: + return cls( + id=int(r["id"]), + token_id=str(r["token_id"]), + user_id=int(r["user_id"]), + name=str(r["name"]), + expert_ids=_load_expert_ids(r["expert_ids"]), + token_hash=str(r["token_hash"]), + token_hint=str(r["token_hint"]), + created_at=str(r["created_at"]), + last_used_at=str(r["last_used_at"]) if r["last_used_at"] is not None else None, + revoked_at=str(r["revoked_at"]) if r["revoked_at"] is not None else None, + ) + + +class MemoryMcpTokenRepo: + def __init__(self, db: DatabasePool) -> None: + self._db = db + + def create( + self, + *, + user_id: int, + name: str, + expert_ids: list[str] | None, + token_id: str, + token_hash: str, + token_hint: str, + ) -> MemoryMcpTokenRow: + with self._db.transaction() as conn: + conn.execute( + "INSERT INTO memory_mcp_tokens(" + "token_id, user_id, name, expert_ids, token_hash, token_hint, created_at" + ") VALUES (?, ?, ?, ?, ?, ?, ?)", + ( + token_id, + user_id, + name, + _dump_expert_ids(expert_ids), + token_hash, + token_hint, + _utc_now_iso(), + ), + ) + row = self.get_by_token_hash(token_hash) + if row is None: + raise RuntimeError(f"memory MCP token insert failed: {token_id}") + return row + + def list_for_user(self, user_id: int) -> list[MemoryMcpTokenRow]: + with self._db.connect() as conn: + rows = conn.execute( + "SELECT * FROM memory_mcp_tokens WHERE user_id = ? " + "ORDER BY created_at DESC, id DESC", + (user_id,), + ).fetchall() + return map_rows(rows, MemoryMcpTokenRow) + + def get_by_token_hash(self, token_hash: str) -> MemoryMcpTokenRow | None: + """Look up by digest; revoked rows are returned so the caller can reject them.""" + with self._db.connect() as conn: + r = conn.execute( + "SELECT * FROM memory_mcp_tokens WHERE token_hash = ?", + (token_hash,), + ).fetchone() + return MemoryMcpTokenRow.from_row(r) if r else None + + def any_active(self) -> bool: + """True when at least one unrevoked token exists (MCP mount gate).""" + with self._db.connect() as conn: + r = conn.execute( + "SELECT 1 FROM memory_mcp_tokens WHERE revoked_at IS NULL LIMIT 1" + ).fetchone() + return r is not None + + def get_for_user(self, token_id: str, *, user_id: int) -> MemoryMcpTokenRow | None: + with self._db.connect() as conn: + r = conn.execute( + "SELECT * FROM memory_mcp_tokens WHERE token_id = ? AND user_id = ?", + (token_id, user_id), + ).fetchone() + return MemoryMcpTokenRow.from_row(r) if r else None + + def revoke(self, token_id: str, *, user_id: int) -> bool: + """Mark the token revoked; ``False`` when it was already revoked.""" + with self._db.transaction() as conn: + cursor = conn.execute( + "UPDATE memory_mcp_tokens SET revoked_at = ? " + "WHERE token_id = ? AND user_id = ? AND revoked_at IS NULL", + (_utc_now_iso(), token_id, user_id), + ) + return int(cursor.rowcount or 0) > 0 + + def touch(self, token_id: str) -> None: + with self._db.transaction() as conn: + conn.execute( + "UPDATE memory_mcp_tokens SET last_used_at = ? WHERE token_id = ?", + (_utc_now_iso(), token_id), + ) diff --git a/src/octop/infra/db/services.py b/src/octop/infra/db/services.py index 3a7e7a6c30..c57405dc86 100644 --- a/src/octop/infra/db/services.py +++ b/src/octop/infra/db/services.py @@ -24,6 +24,7 @@ from octop.infra.db.repos.cron import CronJobRepo from octop.infra.db.repos.invites import InviteRepo from octop.infra.db.repos.knowledge import KnowledgeRepo +from octop.infra.db.repos.memory_mcp_tokens import MemoryMcpTokenRepo from octop.infra.db.repos.proactive_care_config import ProactiveCareConfigRepo from octop.infra.db.repos.profile_avatars import ProfileAvatarRepo from octop.infra.db.repos.providers import ProviderRepo @@ -74,6 +75,7 @@ class RepoBundle: skill_package_repo: SkillPackageRepo published_expert_repo: PublishedExpertRepo knowledge_repo: KnowledgeRepo + memory_mcp_token_repo: MemoryMcpTokenRepo voice_provider_repo: SealedVoiceProviderRepo care_push_repo: CarePushRepo proactive_care_config_repo: ProactiveCareConfigRepo @@ -114,6 +116,7 @@ def from_pool(cls, db: DatabasePool) -> RepoBundle: skill_package_repo=SkillPackageRepo(db), published_expert_repo=PublishedExpertRepo(db), knowledge_repo=KnowledgeRepo(db), + memory_mcp_token_repo=MemoryMcpTokenRepo(db), voice_provider_repo=SealedVoiceProviderRepo(VoiceProviderRepo(db), secret_repo), care_push_repo=CarePushRepo(db), proactive_care_config_repo=ProactiveCareConfigRepo(db), @@ -224,6 +227,10 @@ def published_expert_repo(self) -> PublishedExpertRepo: def knowledge_repo(self) -> KnowledgeRepo: return self.repos.knowledge_repo + @property + def memory_mcp_token_repo(self) -> MemoryMcpTokenRepo: + return self.repos.memory_mcp_token_repo + @property def voice_provider_repo(self) -> SealedVoiceProviderRepo: return self.repos.voice_provider_repo diff --git a/tests/unit/api/test_memory_mcp_token_api.py b/tests/unit/api/test_memory_mcp_token_api.py new file mode 100644 index 0000000000..88b660f254 --- /dev/null +++ b/tests/unit/api/test_memory_mcp_token_api.py @@ -0,0 +1,186 @@ +"""Unit tests for the memory MCP token HTTP adapter.""" + +from __future__ import annotations + +from dataclasses import dataclass +from pathlib import Path +from types import SimpleNamespace +from typing import Any + +import pytest +from fastapi import FastAPI +from fastapi.responses import JSONResponse +from fastapi.testclient import TestClient +from starlette.requests import Request + +from octop.api.deps import current_user, get_server +from octop.api.routers import memory_mcp +from octop.infra.agents.memory_tokens import TOKEN_PREFIX, hash_token +from octop.infra.db.migrate import run_migrations +from octop.infra.db.pool import SqlitePool +from octop.infra.db.repos.users import UserRepo +from octop.infra.db.services import RepoBundle +from octop.infra.errors import OctopError +from octop.infra.users.identity import Role, User +from octop.infra.utils.ulid import new_ulid + + +@dataclass +class Seeded: + db: SqlitePool + repos: RepoBundle + owner: User + other: User + owner_agent: str + other_agent: str + + +def _seed(tmp_path: Path) -> Seeded: + db = SqlitePool(tmp_path / "octop.db") + run_migrations(db) + repos = RepoBundle.from_pool(db) + users = UserRepo(db) + owner_id = users.create( + username="owner", password_hash="h", role="user", permissions=["memory"] + ) + other_id = users.create( + username="other", password_hash="h", role="user", permissions=["memory"] + ) + owner_agent = new_ulid() + other_agent = new_ulid() + repos.agent_repo.create(agent_id=owner_agent, user_id=owner_id, name="owner-bot") + repos.agent_repo.create(agent_id=other_agent, user_id=other_id, name="other-bot") + return Seeded( + db=db, + repos=repos, + owner=User( + id=owner_id, + username="owner", + role=Role.USER, + display_name=None, + permissions=["memory"], + ), + other=User( + id=other_id, + username="other", + role=Role.USER, + display_name=None, + permissions=["memory"], + ), + owner_agent=owner_agent, + other_agent=other_agent, + ) + + +@pytest.fixture +def seeded(tmp_path: Path) -> Seeded: + return _seed(tmp_path) + + +def _client(seeded: Seeded, user: User) -> TestClient: + server = SimpleNamespace(services=SimpleNamespace(repos=seeded.repos)) + app = FastAPI() + app.include_router(memory_mcp.router) + + @app.exception_handler(OctopError) + async def _octop_error(request: Request, exc: OctopError) -> JSONResponse: + return JSONResponse(status_code=exc.status, content=exc.to_envelope()) + + app.dependency_overrides[get_server] = lambda: server + app.dependency_overrides[current_user] = lambda: user + return TestClient(app) + + +def _create_token(client: TestClient, **body: Any) -> dict[str, Any]: + response = client.post("/memory-mcp/tokens", json={"name": "codex@macbook", **body}) + assert response.status_code == 201, response.text + return response.json() + + +def test_create_returns_plaintext_once(seeded: Seeded) -> None: + client = _client(seeded, seeded.owner) + data = _create_token(client, expert_ids=[seeded.owner_agent]) + + token = data["token"] + assert token.startswith(TOKEN_PREFIX) + assert data["token_hint"] == f"{token[:9]}…{token[-4:]}" + assert data["name"] == "codex@macbook" + assert data["expert_ids"] == [seeded.owner_agent] + assert data["revoked_at"] is None + assert data["last_used_at"] is None + + row = seeded.repos.memory_mcp_token_repo.get_for_user(data["token_id"], user_id=seeded.owner.id) + assert row is not None + assert row.token_hash == hash_token(token) + assert row.token_hint == data["token_hint"] + + +def test_list_never_returns_plaintext(seeded: Seeded) -> None: + client = _client(seeded, seeded.owner) + token = _create_token(client)["token"] + + response = client.get("/memory-mcp/tokens") + assert response.status_code == 200 + items = response.json() + assert len(items) == 1 + assert "token" not in items[0] + assert token not in response.text + + +def test_revoke_only_own_token(seeded: Seeded) -> None: + owner_client = _client(seeded, seeded.owner) + token_id = _create_token(owner_client)["token_id"] + + other_client = _client(seeded, seeded.other) + foreign = other_client.delete(f"/memory-mcp/tokens/{token_id}") + assert foreign.status_code == 404 + assert foreign.json()["error"]["code"] == "NOT_FOUND" + row = seeded.repos.memory_mcp_token_repo.get_for_user(token_id, user_id=seeded.owner.id) + assert row is not None + assert row.revoked_at is None + + first = owner_client.delete(f"/memory-mcp/tokens/{token_id}") + assert first.status_code == 200 + assert first.json() == {"revoked": True} + second = owner_client.delete(f"/memory-mcp/tokens/{token_id}") + assert second.status_code == 200 + assert second.json() == {"revoked": False} + + +def test_create_rejects_inaccessible_expert(seeded: Seeded) -> None: + client = _client(seeded, seeded.owner) + + forbidden = client.post( + "/memory-mcp/tokens", + json={"name": "codex@macbook", "expert_ids": [seeded.other_agent]}, + ) + assert forbidden.status_code == 403 + assert forbidden.json()["error"]["code"] == "FORBIDDEN" + + missing = client.post( + "/memory-mcp/tokens", + json={"name": "codex@macbook", "expert_ids": [new_ulid()]}, + ) + assert missing.status_code == 404 + assert missing.json()["error"]["code"] == "AGENT_NOT_FOUND" + + assert seeded.repos.memory_mcp_token_repo.list_for_user(seeded.owner.id) == [] + + +def test_empty_expert_ids_means_unrestricted(seeded: Seeded) -> None: + client = _client(seeded, seeded.owner) + data = _create_token(client, expert_ids=[]) + assert data["expert_ids"] is None + + +def test_create_requires_memory_permission(seeded: Seeded) -> None: + denied = User( + id=seeded.owner.id, + username="owner", + role=Role.USER, + display_name=None, + permissions=[], + ) + response = _client(seeded, denied).post("/memory-mcp/tokens", json={"name": "codex@macbook"}) + assert response.status_code == 403 + assert response.json()["error"]["details"]["permission"] == "memory" diff --git a/tests/unit/backup/test_system_archive.py b/tests/unit/backup/test_system_archive.py index f0612e7cb8..b74ab202a4 100644 --- a/tests/unit/backup/test_system_archive.py +++ b/tests/unit/backup/test_system_archive.py @@ -1178,7 +1178,7 @@ def test_restore_repairs_old_physical_schema_with_current_watermark(tmp_path: Pa "SELECT instance_id, shared FROM connectors WHERE instance_id = 'instance-1'" ).fetchone() - assert result["schema_version"] == 25 + assert result["schema_version"] == 26 assert "shared" in columns assert connector is not None assert connector["shared"] == 0 @@ -1232,7 +1232,7 @@ def test_refuse_newer_schema_backup_before_database_replace( assert excinfo.value.code == ErrorCode.BACKUP_SCHEMA_INCOMPATIBLE assert excinfo.value.details == { "archive_schema_version": 999, - "runtime_schema_version": 25, + "runtime_schema_version": 26, } with pool.connect() as conn: assert ( diff --git a/tests/unit/db/test_agent_profile_columns.py b/tests/unit/db/test_agent_profile_columns.py index 2f8d875de0..adae385dd9 100644 --- a/tests/unit/db/test_agent_profile_columns.py +++ b/tests/unit/db/test_agent_profile_columns.py @@ -119,7 +119,7 @@ def test_migration_007_backfills_profile_columns(tmp_path: Path) -> None: with pool.connect() as conn: version = conn.execute("SELECT version FROM _schema_version").fetchone()[0] row = conn.execute("SELECT * FROM agents WHERE agent_id = ?", ("ag1",)).fetchone() - assert version == 25 + assert version == 26 assert row["template_name"] == "general-assistant" assert row["icon_name"] == "zap" assert row["icon_url"] == "https://cdn.example.com/a.png" diff --git a/tests/unit/db/test_clip_thread_title.py b/tests/unit/db/test_clip_thread_title.py index f52a240f61..54893eae70 100644 --- a/tests/unit/db/test_clip_thread_title.py +++ b/tests/unit/db/test_clip_thread_title.py @@ -82,7 +82,7 @@ def test_migration_003_repairs_stored_hard_cuts(tmp_path: Path) -> None: with pool.connect() as conn: v = conn.execute("SELECT version FROM _schema_version").fetchone()[0] title = conn.execute("SELECT title FROM threads WHERE thread_id = ?", ("t1",)).fetchone()[0] - assert v == 25 + assert v == 26 assert title == "x" * 39 + "…" # Idempotent repair assert repair_all_legacy_thread_titles(pool) == 0 diff --git a/tests/unit/db/test_db_pool.py b/tests/unit/db/test_db_pool.py index adf1ebf605..ec3214f9aa 100644 --- a/tests/unit/db/test_db_pool.py +++ b/tests/unit/db/test_db_pool.py @@ -146,7 +146,7 @@ def test_run_migrations_idempotent(db: SqlitePool): sso_indexes = { r["name"] for r in conn.execute("PRAGMA index_list(sso_providers)").fetchall() } - assert v == 25 + assert v == 26 assert "login_failed_count" in cols assert "login_locked_until" in cols assert {"mfa_version", "mfa_failed_count", "mfa_locked_until", "password_version"}.issubset( @@ -281,7 +281,7 @@ def test_migration_002_idempotent_when_column_already_present(tmp_path: Path) -> with pool.connect() as conn: v = conn.execute("SELECT version FROM _schema_version").fetchone()[0] cron_cols = {r["name"] for r in conn.execute("PRAGMA table_info(cron_jobs)").fetchall()} - assert v == 25 + assert v == 26 assert "mcp_servers" in cron_cols assert "skill_packages" in { r["name"] @@ -424,7 +424,7 @@ def test_stuck_version_6_without_permissions_column_is_repaired(tmp_path: Path) with pool.connect() as conn: cols = {r["name"] for r in conn.execute("PRAGMA table_info(users)").fetchall()} version = conn.execute("SELECT version FROM _schema_version").fetchone()[0] - assert version == 25 + assert version == 26 assert "permissions" in cols @@ -449,7 +449,7 @@ def test_schema_v10_without_projection_tables_is_repaired(tmp_path: Path) -> Non } kb_cols = {r["name"] for r in conn.execute("PRAGMA table_info(knowledge_bases)").fetchall()} cron_cols = {r["name"] for r in conn.execute("PRAGMA table_info(cron_jobs)").fetchall()} - assert version == 25 + assert version == 26 assert {"thread_messages", "thread_history_projection", "trajectory_events"}.issubset( table_names ) @@ -484,7 +484,7 @@ def test_ahead_of_max_schema_version_clamps_to_max(tmp_path: Path) -> None: r["name"] for r in conn.execute("SELECT name FROM sqlite_master WHERE type='table'").fetchall() } - assert version == 25 + assert version == 26 assert "skill_package_id" in pkg_cols assert "published_expert_id" in pub_cols assert "user_invites" in invite_tables @@ -578,7 +578,7 @@ def test_pre_squash_schema_version_clamped_and_knowledge_tables_filled( for row in conn.execute("SELECT name FROM sqlite_master WHERE type='table'").fetchall() } user_cols = {r["name"] for r in conn.execute("PRAGMA table_info(users)").fetchall()} - assert version == 25 + assert version == 26 assert "permissions" in user_cols assert { "published_experts", @@ -784,7 +784,7 @@ def test_v14_to_v15_adds_sso_provider_kind_without_rebuilding(tmp_path: Path) -> bound = conn.execute( "SELECT sso_provider_id FROM users WHERE username = 'sso-admin'" ).fetchone()[0] - assert version == 25 + assert version == 26 assert int(row["id"]) == int(provider_id) assert row["kind"] == "oidc" assert row["extra"] == "{}" diff --git a/tests/unit/db/test_memory_mcp_tokens.py b/tests/unit/db/test_memory_mcp_tokens.py new file mode 100644 index 0000000000..a68b8ff4a5 --- /dev/null +++ b/tests/unit/db/test_memory_mcp_tokens.py @@ -0,0 +1,213 @@ +"""Unit tests for memory MCP token helpers, schema v26, and the repo.""" + +from __future__ import annotations + +import json +from datetime import datetime +from pathlib import Path + +import pytest + +from octop.infra.agents.memory_tokens import ( + TOKEN_PREFIX, + generate_token, + hash_token, + token_hint, + token_matches, +) +from octop.infra.db.migrate import run_migrations +from octop.infra.db.pool import SqlitePool +from octop.infra.db.repos.memory_mcp_tokens import MemoryMcpTokenRepo +from octop.infra.db.repos.users import UserRepo +from octop.infra.utils.ulid import new_ulid + + +@pytest.fixture +def db(tmp_path: Path) -> SqlitePool: + pool = SqlitePool(tmp_path / "octop.db") + run_migrations(pool) + return pool + + +@pytest.fixture +def repo(db: SqlitePool) -> MemoryMcpTokenRepo: + return MemoryMcpTokenRepo(db) + + +def _user(db: SqlitePool, username: str = "alice") -> int: + return UserRepo(db).create(username=username, password_hash="h", role="user") + + +def _issue( + repo: MemoryMcpTokenRepo, + user_id: int, + *, + name: str = "codex@macbook", + expert_ids: list[str] | None = None, +) -> tuple[str, str]: + """Create a token; return ``(plaintext, token_id)``.""" + plaintext, digest = generate_token() + token_id = new_ulid() + repo.create( + user_id=user_id, + name=name, + expert_ids=expert_ids, + token_id=token_id, + token_hash=digest, + token_hint=token_hint(plaintext), + ) + return plaintext, token_id + + +def test_generate_token_prefix_and_digest() -> None: + plaintext, digest = generate_token() + assert plaintext.startswith(TOKEN_PREFIX) + assert len(plaintext) > len(TOKEN_PREFIX) + 32 + assert digest == hash_token(plaintext) + assert len(digest) == 64 + assert plaintext != generate_token()[0] + + +def test_token_matches_compares_digests() -> None: + plaintext, digest = generate_token() + assert token_matches(plaintext, digest) + assert not token_matches(plaintext + "x", digest) + + +def test_token_hint_masks_middle() -> None: + plaintext, _ = generate_token() + hint = token_hint(plaintext) + assert hint == f"{plaintext[:9]}…{plaintext[-4:]}" + assert "…" in hint + assert hint.startswith(TOKEN_PREFIX) + + +def test_schema_v26_creates_table(db: SqlitePool) -> None: + with db.connect() as conn: + version = conn.execute("SELECT version FROM _schema_version").fetchone()[0] + tables = { + r["name"] for r in conn.execute("SELECT name FROM sqlite_master WHERE type='table'") + } + indexes = { + r["name"] for r in conn.execute("PRAGMA index_list(memory_mcp_tokens)").fetchall() + } + columns = { + r["name"] for r in conn.execute("PRAGMA table_info(memory_mcp_tokens)").fetchall() + } + assert version == 26 + assert "memory_mcp_tokens" in tables + assert "idx_memory_mcp_tokens_user" in indexes + assert { + "id", + "token_id", + "user_id", + "name", + "expert_ids", + "token_hash", + "token_hint", + "created_at", + "last_used_at", + "revoked_at", + } == columns + + +def test_create_persists_digest_only(db: SqlitePool, repo: MemoryMcpTokenRepo) -> None: + user_id = _user(db) + plaintext, token_id = _issue(repo, user_id) + + row = repo.get_for_user(token_id, user_id=user_id) + assert row is not None + assert row.token_id == token_id + assert row.user_id == user_id + assert row.name == "codex@macbook" + assert row.expert_ids is None + assert row.token_hash == hash_token(plaintext) + assert row.token_hint == token_hint(plaintext) + assert row.last_used_at is None + assert row.revoked_at is None + datetime.fromisoformat(row.created_at.replace("Z", "+00:00")) + + with db.connect() as conn: + stored = conn.execute( + "SELECT * FROM memory_mcp_tokens WHERE token_id = ?", (token_id,) + ).fetchone() + assert plaintext not in json.dumps(dict(stored)) + + +def test_expert_ids_json_round_trip(db: SqlitePool, repo: MemoryMcpTokenRepo) -> None: + user_id = _user(db) + _, token_id = _issue(repo, user_id, expert_ids=["expert-a", "expert-b"]) + + row = repo.get_for_user(token_id, user_id=user_id) + assert row is not None + assert row.expert_ids == ["expert-a", "expert-b"] + + with db.connect() as conn: + raw = conn.execute( + "SELECT expert_ids FROM memory_mcp_tokens WHERE token_id = ?", (token_id,) + ).fetchone()["expert_ids"] + assert json.loads(raw) == ["expert-a", "expert-b"] + + +def test_list_for_user_newest_first_and_scoped(db: SqlitePool, repo: MemoryMcpTokenRepo) -> None: + alice = _user(db, "alice") + bob = _user(db, "bob") + _, first = _issue(repo, alice, name="first") + _, second = _issue(repo, alice, name="second") + _issue(repo, bob, name="bob-token") + + rows = repo.list_for_user(alice) + assert [row.token_id for row in rows] == [second, first] + assert repo.list_for_user(bob)[0].name == "bob-token" + + +def test_get_by_token_hash_returns_revoked_row(db: SqlitePool, repo: MemoryMcpTokenRepo) -> None: + user_id = _user(db) + plaintext, token_id = _issue(repo, user_id) + assert repo.revoke(token_id, user_id=user_id) is True + + row = repo.get_by_token_hash(hash_token(plaintext)) + assert row is not None + assert row.token_id == token_id + assert row.revoked_at is not None + assert repo.get_by_token_hash(hash_token("omcp_unknown")) is None + + +def test_get_for_user_hides_other_users_tokens(db: SqlitePool, repo: MemoryMcpTokenRepo) -> None: + alice = _user(db, "alice") + bob = _user(db, "bob") + _, token_id = _issue(repo, alice) + assert repo.get_for_user(token_id, user_id=bob) is None + + +def test_revoke_is_idempotent(db: SqlitePool, repo: MemoryMcpTokenRepo) -> None: + alice = _user(db, "alice") + bob = _user(db, "bob") + _, token_id = _issue(repo, alice) + + assert repo.revoke(token_id, user_id=bob) is False + assert repo.revoke(token_id, user_id=alice) is True + assert repo.revoke(token_id, user_id=alice) is False + row = repo.get_for_user(token_id, user_id=alice) + assert row is not None + assert row.revoked_at is not None + + +def test_any_active_ignores_revoked(db: SqlitePool, repo: MemoryMcpTokenRepo) -> None: + user_id = _user(db) + assert repo.any_active() is False + _, token_id = _issue(repo, user_id) + assert repo.any_active() is True + repo.revoke(token_id, user_id=user_id) + assert repo.any_active() is False + + +def test_touch_sets_last_used_at(db: SqlitePool, repo: MemoryMcpTokenRepo) -> None: + user_id = _user(db) + _, token_id = _issue(repo, user_id) + repo.touch(token_id) + + row = repo.get_for_user(token_id, user_id=user_id) + assert row is not None + assert row.last_used_at is not None + datetime.fromisoformat(row.last_used_at.replace("Z", "+00:00")) diff --git a/tests/unit/db/test_published_experts_repo.py b/tests/unit/db/test_published_experts_repo.py index 5bef317516..9c96c6e020 100644 --- a/tests/unit/db/test_published_experts_repo.py +++ b/tests/unit/db/test_published_experts_repo.py @@ -27,7 +27,7 @@ def test_published_experts_table_exists(db: SqlitePool) -> None: v = conn.execute("SELECT version FROM _schema_version").fetchone()[0] cols = {r["name"] for r in conn.execute("PRAGMA table_info(published_experts)").fetchall()} assert "published_experts" in names - assert v == 25 + assert v == 26 assert "published_expert_id" in cols diff --git a/tests/unit/db/test_repo_knowledge.py b/tests/unit/db/test_repo_knowledge.py index 641714afa5..194951a7a0 100644 --- a/tests/unit/db/test_repo_knowledge.py +++ b/tests/unit/db/test_repo_knowledge.py @@ -50,7 +50,7 @@ def test_knowledge_tables_migrated(db: SqlitePool) -> None: "knowledge_bases", "knowledge_documents", }.issubset(names) - assert v == 25 + assert v == 26 assert "knowledge_base_members" not in names cols = {r["name"] for r in conn.execute("PRAGMA table_info(knowledge_bases)").fetchall()} assert "knowledge_base_id" in cols diff --git a/tests/unit/db/test_skill_package_icons.py b/tests/unit/db/test_skill_package_icons.py index 812dbcf25e..a780b998c6 100644 --- a/tests/unit/db/test_skill_package_icons.py +++ b/tests/unit/db/test_skill_package_icons.py @@ -91,7 +91,7 @@ def test_migration_002_idempotent_when_icon_columns_already_present(tmp_path: Pa "SELECT name FROM sqlite_master WHERE type='index' AND tbl_name='skill_packages'" ) } - assert v == 25 + assert v == 26 assert "icon_name" in cols assert "icon_url" in cols assert "skill_package_id" in cols @@ -110,7 +110,7 @@ def test_repair_legacy_schema_adds_icon_columns_at_version_2(tmp_path: Path) -> with pool.connect() as conn: v = conn.execute("SELECT version FROM _schema_version").fetchone()[0] cols = {r["name"] for r in conn.execute("PRAGMA table_info(skill_packages)").fetchall()} - assert v == 25 + assert v == 26 assert "icon_name" in cols assert "icon_url" in cols assert "skill_package_id" in cols diff --git a/tests/unit/db/test_skill_packages_repo.py b/tests/unit/db/test_skill_packages_repo.py index c51096bd9d..d259a323e9 100644 --- a/tests/unit/db/test_skill_packages_repo.py +++ b/tests/unit/db/test_skill_packages_repo.py @@ -28,7 +28,7 @@ def test_skill_packages_table_exists(db: SqlitePool) -> None: v = conn.execute("SELECT version FROM _schema_version").fetchone()[0] cols = {r["name"] for r in conn.execute("PRAGMA table_info(skill_packages)").fetchall()} assert "skill_packages" in names - assert v == 25 + assert v == 26 assert "skill_package_id" in cols diff --git a/tests/unit/users/test_group_access.py b/tests/unit/users/test_group_access.py index 816b6d4a59..d188824fb2 100644 --- a/tests/unit/users/test_group_access.py +++ b/tests/unit/users/test_group_access.py @@ -36,7 +36,7 @@ def test_schema_version_includes_user_groups(repos: RepoBundle) -> None: row["name"] for row in conn.execute("SELECT name FROM sqlite_master WHERE type='table'").fetchall() } - assert version == 25 + assert version == 26 assert {"user_group", "user_group_member", "resource_group_grant"}.issubset(names) From df6a8b03e082aac0971a910c6717a9ac1154e345 Mon Sep 17 00:00:00 2001 From: ArvinQi <12555402+ArvinQi@users.noreply.github.com> Date: Mon, 9 Nov 2026 22:06:44 +0800 Subject: [PATCH 02/39] =?UTF-8?q?feat(memory-mcp):=20MCP=20=E9=89=B4?= =?UTF-8?q?=E6=9D=83=E4=B8=8E=E5=B7=A5=E5=85=B7=E9=9D=A2=E6=8C=89=20review?= =?UTF-8?q?=20=E9=87=8D=E5=81=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 鉴权 - MCP 中间件从 Bearer token 推出用户(sha256 + hmac.compare_digest,忽略 X-Octop-User-Id);每次请求按 owner / 共享专家规则重新校验专家访问权, 并支持 token 的专家范围限制 - 挂载条件:OCTOP_MEMORY_MCP_ENABLED=1 或已存在有效 token;lifespan 串接原 FastAPI lifespan,不再覆盖 工具面 - capture 改走 MemoryRuntime.capture:脱敏、回声保护、角色/长度策略生效; 调用者写进事件 metadata(caller / source_host),不再拼 `说:` - save 改走 MemoryService.save:人工审核调度器能收到通知 - 9 个工具全部 async,阻塞调用一律 asyncio.to_thread - 审核/运维工具(raws/candidates/extract/promote/reject)仅专家所有者 - 外部写入默认不进晋升(跟随专家提炼配置);后台提炼串行、保存任务引用, 不自动启动停用的专家 测试:越权、伪造 header、密钥脱敏、工具不阻塞事件循环、lifespan 串接。 --- src/octop/api/app.py | 8 + src/octop/infra/agents/memory_mcp.py | 1159 ++++++++++++++++++++++++++ tests/unit/agents/test_memory_mcp.py | 577 +++++++++++++ 3 files changed, 1744 insertions(+) create mode 100644 src/octop/infra/agents/memory_mcp.py create mode 100644 tests/unit/agents/test_memory_mcp.py diff --git a/src/octop/api/app.py b/src/octop/api/app.py index 7433712e48..eaa851cd7d 100644 --- a/src/octop/api/app.py +++ b/src/octop/api/app.py @@ -196,6 +196,7 @@ async def acme_http01_challenge(token: str) -> PlainTextResponse: mbti, media_generation, memory, + memory_mcp, memory_portable, memory_storage, mobile, @@ -294,6 +295,7 @@ async def acme_http01_challenge(token: str) -> PlainTextResponse: _RouterMount(workspace.router, "/api", ["workspace"]), _RouterMount(agent_files.router, "/api", ["agent_files"]), _RouterMount(memory.router, "/api", ["memory"]), + _RouterMount(memory_mcp.router, "/api", ["memory"]), _RouterMount(memory_portable.router, "/api", ["memory"]), _RouterMount(memory_storage.router, "/api", ["memory"]), _RouterMount(proactive_care.router, "/api", ["proactive-care"]), @@ -321,6 +323,12 @@ async def acme_http01_challenge(token: str) -> PlainTextResponse: ], ) + # 专家记忆 MCP server:用户级 token 鉴权;未开启或没有 token 时不挂载。 + # lifespan 由 install_memory_mcp 串进现有的 FastAPI lifespan,不覆盖它。 + from octop.infra.agents.memory_mcp import install_memory_mcp + + install_memory_mcp(app, server) + if enable_api_docs: @app.get("/api/docs", include_in_schema=False) diff --git a/src/octop/infra/agents/memory_mcp.py b/src/octop/infra/agents/memory_mcp.py new file mode 100644 index 0000000000..f0fd784a26 --- /dev/null +++ b/src/octop/infra/agents/memory_mcp.py @@ -0,0 +1,1159 @@ +"""Expose Octop expert memory as an MCP server for external agents. + +External agents (coding agents, bots) read and write one expert's memory over MCP +(Streamable HTTP). The endpoint is a single ``/mcp/memory`` mount; the expert is +selected per request with ``X-Octop-Agent-Id`` and the caller is identified by a +**user-bound token** (``Authorization: Bearer omcp_…``) — never by a header the +client can choose. + +Auth model (owner: Octop users, not a shared instance token): + +* each user creates tokens in the dashboard / ``POST /api/memory-mcp/tokens``; + a token can be limited to a set of experts and is revocable per client + (``name`` is the client label, e.g. ``codex@macbook``); +* only the sha256 hash is stored, and verification is a constant-time compare; +* the caller's Octop user comes from the token, and every request re-checks + that the user may access the selected expert (owner / shared-expert rules), + so a token cannot be pointed at somebody else's expert by editing a header. + +Tool surface (``a`` scope of the review split): + +* everyday, any token: ``memory_search`` / ``memory_get`` / ``memory_save`` / + ``memory_capture``; +* review & ops, **expert owner only**: ``memory_raws`` / ``memory_candidates`` / + ``memory_extract`` / ``memory_promote`` / ``memory_reject`` (the dashboard has + the same operations; exposing them to every external agent would let an agent + promote its own writes). + +Write paths go through the octop-memory application layer so behaviour matches +the in-process tools: ``memory_capture`` -> ``MemoryRuntime.capture`` (privacy +redaction, role/min-length policy, recall-echo guard) and ``memory_save`` -> +``MemoryService.save`` (manual-review scheduler). Captured writes land in the L0 +raw layer and are extracted in the background; extraction **promotes only when +the expert's own extraction config asks for it** — by default external writes +wait in the review queue. + +No background extraction starts a stopped expert: an idle expert has no live +``MemoryService`` (and starting it would spend the user's model budget), so the +capture is recorded and extraction is skipped until the expert runs. +""" + +from __future__ import annotations + +import asyncio +import logging +import os +from collections.abc import AsyncIterator, Callable +from contextlib import AsyncExitStack, asynccontextmanager +from contextvars import ContextVar +from typing import Any, get_args + +from mcp.server.fastmcp import FastMCP +from mcp.server.transport_security import TransportSecuritySettings + +from octop.infra.agents.memory.backend import open_memory_kwargs +from octop.infra.agents.memory_tokens import hash_token, token_matches +from octop.infra.server import OctopServer + +logger = logging.getLogger(__name__) + +# 当前 MCP HTTP 请求的绑定状态(由 _TokenAuthMiddleware / _AgentRouter 写入, +# 工具读取)。stateless streamable HTTP 下 mcp SDK 不提供 ctx.request_context, +# 故用 contextvar 在 ASGI 中间件与工具之间传递。 +_current_agent_id: ContextVar[str] = ContextVar("octop_mcp_agent_id", default="") +_current_user_id: ContextVar[int | None] = ContextVar("octop_mcp_user_id", default=None) +_current_user_name: ContextVar[str] = ContextVar("octop_mcp_user_name", default="") +_current_token_name: ContextVar[str] = ContextVar("octop_mcp_token_name", default="") +_current_token_scope: ContextVar[list[str] | None] = ContextVar( + "octop_mcp_token_scope", default=None +) +"""Experts a token is limited to, or ``None`` when unrestricted.""" + +_SEARCH_CORPORA: frozenset[str] = frozenset({"all", "memory", "atom", "raw"}) +"""Corpora ``memory_search`` accepts. ``atom``/``raw`` are layer filters over the +recall pipeline; ``all``/``memory`` are the same pipeline without a filter.""" + +_SNIPPET_CHARS = 200 + +_OWN_CONTENT_PREFIX = "[octop memory " +"""Uniform marker for anything this service puts back into a conversation. + +The terminal hooks built on this server (``d`` scope) inject search hits with this +prefix; ``MemoryRuntime.capture`` filters the upstream recall markers, and this +prefix is what a client could quote back into ``memory_capture``. Kept as a +documented marker only — capture relies on the runtime guard. +""" + +_USER_ROLE = "user_message" +_ASSISTANT_ROLE = "assistant_message" + +_EXTRACT_TASKS: set[asyncio.Task[None]] = set() +"""Strong references to background extraction tasks (``asyncio`` only keeps a weak +one, so an unreferenced task may be garbage-collected mid-flight).""" + +_EXTRACT_LOCKS: dict[str, asyncio.Lock] = {} +"""One lock per expert: extraction must never run twice in parallel for the same +expert (the harness memory middleware may extract the same session concurrently).""" + + +def _resolve_agent_scope(server: OctopServer, agent_id: str) -> tuple[Any, dict[str, Any]]: + """The agent's workspace directory + its parsed ``config_json``.""" + services = server.services + assert services is not None, "server.services required for memory backend" + runtime = getattr(server, "app_runtime", None) + registry = getattr(runtime, "agent_registry", None) if runtime is not None else None + if registry is not None and hasattr(registry, "resolve_workspace_dir"): + workspace = registry.resolve_workspace_dir(agent_id) + else: + paths = getattr(server, "paths", None) or services.paths + workspace = paths.ensure_agent_workspace(agent_id) + + row = services.agent_repo.get(agent_id) + cfg: dict[str, Any] = {} + if row is not None and row.config_json: + import json # noqa: PLC0415 + + try: + parsed = json.loads(row.config_json) + if isinstance(parsed, dict): + cfg = parsed + except json.JSONDecodeError: + cfg = {} + return workspace, cfg + + +def _open_memory(server: OctopServer, agent_id: str) -> Any: + """Open the agent's ``Memory`` instance (sqlite by default, postgres opt-in).""" + from octop_memory.core import Memory # noqa: PLC0415 + + services = server.services + assert services is not None, "server.services required for memory backend" + workspace, cfg = _resolve_agent_scope(server, agent_id) + + ns, backend, backend_config = open_memory_kwargs( + agent_id=agent_id, + cfg=cfg, + octop_config=services.config, + workspace_dir=workspace, + ) + return Memory(namespace=ns, backend=backend, backend_config=backend_config) + + +def _memory_runtime(server: OctopServer, agent_id: str) -> Any: + """Capture/search runtime for this expert. + + Prefers the live ``MemoryService`` runtime so the expert's own capture / + privacy config applies; falls back to a default-configured runtime over the + on-disk memory when the expert is not loaded (redaction is on by default). + """ + from octop_memory.application.runtime import MemoryRuntime # noqa: PLC0415 + + runtime = getattr(server, "app_runtime", None) + registry = getattr(runtime, "agent_registry", None) if runtime is not None else None + service = _live_memory_service(registry, agent_id) if registry is not None else None + service_runtime = getattr(service, "_runtime", None) if service is not None else None + if service_runtime is not None: + return service_runtime + return MemoryRuntime(_open_memory(server, agent_id)) + + +def _dashboard_bridge(memory: Any) -> Any: + """Open the harness-memory JSON-RPC bridge over ``memory``. + + The same adapter the memory panel reaches through + ``api/common/memory_client.call_memory_rpc``, so the MCP and the dashboard + can never drift apart on candidate review semantics. + """ + from octop_memory.adapters.bridge.handlers import Bridge # noqa: PLC0415 + + return Bridge(memory) + + +def _bridge_rpc(bridge: Any, method: str, params: dict[str, Any]) -> dict[str, Any]: + """Call one dashboard RPC and return its result, raising ``ValueError`` on error.""" + + def _invoke() -> dict[str, Any]: + response = bridge.handle({"jsonrpc": "2.0", "id": 1, "method": method, "params": params}) + error = response.get("error") + if error: + raise ValueError(error.get("message") or f"memory rpc {method!r} failed") + return response.get("result") or {} + + return _invoke() + + +def _snippet(text: str) -> str: + """Cap a raw event body so ``memory_search`` hits stay small.""" + body = (text or "").strip() + return body if len(body) <= _SNIPPET_CHARS else body[: _SNIPPET_CHARS - 1].rstrip() + "…" + + +def _lookup_raw_users(memory: Any, hits: list[dict[str, Any]]) -> dict[str, str]: + """``{raw event id: user}`` for the raw hits — the raw layer carries the sender.""" + ids = [ + str(hit.get("source_id")) + for hit in hits + if hit.get("layer") == "raw" and hit.get("source_id") + ] + if not ids: + return {} + try: + events = memory.get_raw_events_by_ids(list(dict.fromkeys(ids))) + except Exception: # noqa: BLE001 - attribution must never break search + logger.warning("memory_search: raw attribution lookup failed", exc_info=True) + return {} + return {str(event.id): str(event.user or "") for event in events} + + +def _prefer_personal(memory: Any, hits: list[dict[str, Any]], caller: str) -> list[dict[str, Any]]: + """Stable-partition hits: *caller*'s first, then everyone else's. + + A preference, not a filter — memory is team-shared. Atoms/pages carry no + sender column (external writes no longer stamp a name into the body), so only + raw hits can be marked personal. + """ + raw_users = _lookup_raw_users(memory, hits) + mine: list[dict[str, Any]] = [] + rest: list[dict[str, Any]] = [] + for hit in hits: + personal = ( + hit.get("layer") == "raw" and raw_users.get(str(hit.get("source_id")), "") == caller + ) + hit["personal"] = personal + if hit.get("layer") == "raw" and str(hit.get("source_id")) in raw_users: + hit["user"] = raw_users[str(hit.get("source_id"))] or None + (mine if personal else rest).append(hit) + return [*mine, *rest] + + +def _prefer_personal_events(events: list[Any], caller: str) -> list[Any]: + """Same preference for raw events (``corpus=raw``): own rows first, stable order.""" + mine = [event for event in events if str(getattr(event, "user", "") or "") == caller] + rest = [event for event in events if str(getattr(event, "user", "") or "") != caller] + return [*mine, *rest] + + +def _pipeline_hits( + memory: Any, + query: str, + max_results: int, + *, + atom_only: bool, + caller: str = "", +) -> list[dict[str, Any]]: + """Run the in-process multi-source search and return its path-carrying hits.""" + from octop_memory.application.runtime import MemoryRuntime # noqa: PLC0415 + + runtime = MemoryRuntime(memory) + result = runtime.memory_search( + { + "query": query, + "maxResults": max_results * 4 if (atom_only or caller) else max_results, + "corpus": "memory", + } + ) + hits = list(result.get("hits") or []) + if atom_only: + hits = [hit for hit in hits if hit.get("layer") == "atom"] + if caller: + hits = _prefer_personal(memory, hits, caller) + return hits[:max_results] + + +def _caller_name() -> str: + """Octop user name resolved from this request's token (never a client header).""" + return _current_user_name.get() or f"user:{_current_user_id.get()}" + + +def _agent_row(server: OctopServer) -> Any: + """The agent row bound to this request (validated by ``_AgentRouter``).""" + services = server.services + assert services is not None, "server.services required for memory MCP" + row = services.agent_repo.get(_current_agent_id.get()) + if row is None: + raise RuntimeError("X-Octop-Agent-Id header not bound to a known expert") + return row + + +def _assert_expert_owner(server: OctopServer) -> None: + """Review/ops tools are expert-owner (or admin) only.""" + row = _agent_row(server) + user_id = _current_user_id.get() + if _is_admin(server, user_id): + return + if row.user_id is None or row.user_id != user_id: + raise PermissionError( + "this tool is limited to the expert owner; external agents may use " + "memory_search / memory_get / memory_save / memory_capture" + ) + + +def _is_admin(server: OctopServer, user_id: int | None) -> bool: + user = _user_of(server, user_id) + return bool(getattr(user, "is_admin", False)) + + +def _user_of(server: OctopServer, user_id: int | None) -> Any: + if user_id is None: + return None + manager = getattr(server, "user_manager", None) + if manager is None: + return None + try: + return manager.get_by_id(user_id) + except Exception: # noqa: BLE001 - auth failures surface as "unknown user" + return None + + +def _user_may_access_agent(server: OctopServer, row: Any, user: Any) -> bool: + """Owner / shared-expert visibility check (same rule as the HTTP routers). + + Kept in ``infra/`` so ``infra/agents`` does not import ``api/``. + """ + if user is None: + return False + if getattr(user, "is_admin", False): + return True + if row.user_id is not None and row.user_id == getattr(user, "id", None): + return True + if int(getattr(row, "is_shared", 0) or 0) != 1: + return False + services = server.services + repos = getattr(services, "repos", None) if services is not None else None + if repos is None: + return False + from octop.infra.users.groups import expert_visible_to # noqa: PLC0415 + + return bool(expert_visible_to(repos, row, user_id=user.id, is_admin=False)) + + +def build_memory_mcp(server: OctopServer) -> FastMCP: + """Build the shared memory MCP app (expert bound per request, not per build).""" + mcp = FastMCP( + "octop-memory", + # Stateless streamable HTTP: every request gets a fresh transport, no + # Mcp-Session-Id tracking, so a server restart cannot orphan client + # sessions with -32600 "Session not found". + stateless_http=True, + # Octop runs behind a reverse proxy (Host is the public domain), so the + # mcp SDK's localhost DNS-rebinding protection does not apply. + transport_security=TransportSecuritySettings(enable_dns_rebinding_protection=False), + ) + # Collapse the streamable-HTTP path to "/" so the endpoint is exactly + # /mcp/memory (the default "/mcp" would make it /mcp/memory/mcp). + mcp.settings.streamable_http_path = "/" + + def _agent_id() -> str: + agent_id = _current_agent_id.get() + if not agent_id: + raise RuntimeError("X-Octop-Agent-Id header not bound to this request") + return agent_id + + # ─── everyday tools (any user token scoped to the expert) ─────────────── + + @mcp.tool() + async def memory_search( + query: str, + max_results: int = 5, + corpus: str = "all", + ) -> dict[str, Any]: + """**检索记忆(读入口)**:全文检索专家记忆,返回带虚拟路径的命中。 + + 每条命中带 ``path``,交给 ``memory_get`` 读全文;只想把背景拉进上下文就 + 用 ``snippet``,要原话/证据用 ``memory_raws``(仅专家所有者)。 + + Args: + query: 自然语言问题/关键词(中文做 n-gram 分词)。 + max_results: 最多返回命中数,默认 5。 + corpus: ``all``(默认,原子优先、原始事件兜底)/ ``memory`` / + ``atom``(只要长期记忆)/ ``raw``(只要原始事件,适合刚 capture + 还没提炼的内容)。 + """ + corpus_value = (corpus or "all").strip().lower() + if corpus_value not in _SEARCH_CORPORA: + raise ValueError( + f"invalid corpus {corpus!r}; expected one of {sorted(_SEARCH_CORPORA)}" + ) + caller = _caller_name() + + def _run() -> dict[str, Any]: + memory = _open_memory(server, _agent_id()) + if corpus_value == "raw": + from octop_memory.application.path_projection import ( # noqa: PLC0415 + raw_to_path, + ) + + events = list( + memory.search_raw(query, limit=max_results * 4 if caller else max_results) + ) + if caller: + events = _prefer_personal_events(events, caller) + hits = [ + { + "path": raw_to_path(event), + "layer": "raw", + "snippet": _snippet(event.content), + "occurred_at": event.timestamp.isoformat(), + "source_id": event.id, + "user": str(event.user or "") or None, + "personal": str(event.user or "") == caller, + } + for event in events[:max_results] + ] + else: + hits = _pipeline_hits( + memory, + query, + max_results, + atom_only=corpus_value == "atom", + caller=caller, + ) + return { + "marker": "[octop memory search]", + "hits": hits, + "total": len(hits), + "corpus": corpus_value, + "caller": caller, + "personal_hits": sum(1 for hit in hits if hit.get("personal")), + "hint": ( + "每条命中自带 path,可交给 memory_get(path) 读全文;" + "命中按「本次调用者相关」优先排序,团队共享内容不过滤" + ), + } + + return await asyncio.to_thread(_run) + + @mcp.tool() + async def memory_get( + path: str, + start: int | None = None, + lines: int | None = None, + ) -> dict[str, Any]: + """**读取记忆全文**:把 ``memory_search`` 命中的虚拟路径解析成 markdown。 + + 支持 ``atom/.md`` / ``page/.md`` / ``raw//.md``; + 长内容用 ``start`` / ``lines`` 分页。 + + Args: + path: 虚拟路径,取自 ``memory_search`` 的 ``hits[].path``。 + start: 可选起始行号(1-based)。 + lines: 可选返回行数。 + """ + from octop_memory.application.runtime import MemoryRuntime # noqa: PLC0415 + + def _run() -> dict[str, Any]: + params: dict[str, Any] = {"path": path} + if start is not None: + params["from"] = start + if lines is not None: + params["lines"] = lines + try: + result = MemoryRuntime(_open_memory(server, _agent_id())).memory_get(params) + except Exception as exc: # stale / mistyped path from a previous call + return { + "path": path, + "error": f"{exc.__class__.__name__}: {exc}", + "hint": ( + "path 形如 atom/.md / page/.md / " + "raw//.md" + ), + } + return { + "marker": "[octop memory get]", + "path": result.get("path", path), + "kind": result.get("kind"), + "content": result.get("excerpt") or "", + "total_lines": result.get("total_lines"), + "from_line": result.get("from_line"), + "to_line": result.get("to_line"), + "truncated": result.get("truncated"), + "metadata": result.get("metadata") or {}, + } + + return await asyncio.to_thread(_run) + + @mcp.tool() + async def memory_save( + content: str, + source: str, + topic: str | None = None, + supersedes: str | None = None, + ) -> dict[str, Any]: + """**直接保存事实**:把一条已知事实写入长期记忆(跳过提炼,立即可召回)。 + + 用于明确、需长期记住的事实(用户偏好、项目约定)。日常对话内容请用 + ``memory_capture`` 交给提炼管线。 + + 旧事实过时了就用 ``supersedes`` 传旧记忆 id:旧记忆标记 deprecated,新事实 + 立即生效。调用者身份由 token 决定,写进 ``metadata.caller``,不再拼进正文。 + + Args: + content: 要记住(或用来替代旧事实)的内容。 + source: 谁记录的(如 ``coding-agent``),用于追溯。 + topic: 可选主题标签。 + supersedes: 可选——被这条新事实取代的旧记忆 id。 + """ + caller = _caller_name() + agent_id = _agent_id() + service = _require_live_service(server, agent_id) + + def _run() -> dict[str, Any]: + metadata: dict[str, Any] = { + "source": source, + "source_host": source, + "caller": caller, + } + deprecated: bool | None = None + memory = getattr(service, "memory", None) + if supersedes: + if memory is None: + raise RuntimeError("memory backend unavailable for supersedes") + deprecated = memory.deprecate_atom( + supersedes, actor="user", note="superseded by memory_save" + ) + metadata["supersedes"] = supersedes + node = service.save(content, topic=topic, metadata=metadata) + result: dict[str, Any] = { + "node_id": node.id, + "content": node.content, + "source": source, + "caller": caller, + } + if supersedes: + result["deprecated"] = deprecated + result["superseded_atom_id"] = supersedes + return result + + return await asyncio.to_thread(_run) + + @mcp.tool() + async def memory_capture( + content: str, + source: str, + session_id: str | None = None, + assistant: str | None = None, + thread_id: str | None = None, + dry_run: bool = False, + extract: bool = True, + ) -> dict[str, Any]: + """**写入原始事件(L0)**:一轮对话或一段记录,交给提炼流水线。 + + ``content`` 是用户侧/事实侧内容,``assistant`` 是助手回复;两者落在同一批 + 事件里,提取器才能判断"助手建议是否被用户接受"。只记助手结论时 ``content`` + 留空字符串。 + + 走 octop-memory 的 capture 应用层:敏感信息自动脱敏、过短/回声内容按策略丢弃。 + 写入后立即能用 ``memory_search(corpus="raw")`` 查到,提炼成长期记忆后才回到常规 + 检索。默认**不自动晋升**:外部写入先进入审核队列,由专家所有者审核。 + + 调用者身份由 token 决定并写进事件 metadata(``caller`` / ``source_host``); + **不要**再把名字写进正文。 + + Args: + content: 用户侧内容(或一条提炼过的事实);只记助手结论时留空。 + source: 谁记录的(如 ``claude-code`` / ``codex``),同时作为 L0 的 host。 + session_id: 可选会话 id;缺省派生 ``ext:{source}:{caller}``,同一段工作 + 请保持稳定。 + assistant: 助手侧回复(可为空)。 + thread_id: 可选线程 id,透传给 L0。 + dry_run: 只解析不写入;返回将要写入的事件。 + extract: 写入后是否在后台触发提炼(默认 True);专家停止时不会自动启动。 + """ + caller = _caller_name() + agent_id = _agent_id() + + def _run() -> dict[str, Any]: + events: list[dict[str, Any]] = [] + payload = {"source": source, "source_host": source, "caller": caller} + if content.strip(): + events.append({"event_type": _USER_ROLE, "content": content, "payload": payload}) + if assistant: + events.append( + {"event_type": _ASSISTANT_ROLE, "content": assistant, "payload": payload} + ) + if not events: + raise ValueError("content and assistant are both empty; nothing to record") + + effective_session = session_id or f"ext:{source or 'mcp'}:{caller}" + if dry_run: + return { + "marker": "[octop memory capture]", + "recorded": False, + "dry_run": True, + "session_id": effective_session, + "events": [ + { + "event_type": event["event_type"], + "content": event["content"], + "caller": caller, + } + for event in events + ], + "note": "dry run: parsed only, nothing written", + } + + runtime = _memory_runtime(server, agent_id) + result = runtime.capture( + { + "session_id": effective_session, + "thread_id": thread_id, + "user": caller, + "host": source or "mcp-external", + "events": events, + } + ) + accepted = int(result.get("accepted") or 0) + return { + "marker": "[octop memory capture]", + "recorded": accepted > 0, + "session_id": effective_session, + "source": source, + "caller": caller, + "accepted": accepted, + "skipped": { + key.removeprefix("skipped_"): value + for key, value in result.items() + if key.startswith("skipped_") and value + }, + "note": ( + "raw (L0) events recorded; visible now via memory_search(corpus=raw), " + "promoted to long-term memory only after review/提炼" + ), + } + + result = await asyncio.to_thread(_run) + if extract and not dry_run and result.get("recorded"): + result["extract_scheduled"] = _schedule_extract( + server, + agent_id, + str(result["session_id"]), + promote=_expert_extract_promote(server, agent_id), + ) + return result + + # ─── review & ops tools (expert owner only) ───────────────────────────── + + @mcp.tool() + async def memory_raws( + query: str | None = None, + session_id: str | None = None, + host: str | None = None, + user: str | None = None, + limit: int = 50, + ) -> dict[str, Any]: + """**查原始事件(仅专家所有者)**:FTS 搜索或结构化过滤 L0 原始事件。 + + Args: + query: FTS 关键词(可选)。 + session_id: 按会话过滤。 + host: 按记录主机过滤(如 ``claude-code``)。 + user: 按调用者过滤。 + limit: 最多返回条数,默认 50。 + """ + _assert_expert_owner(server) + + def _run() -> dict[str, Any]: + memory = _open_memory(server, _agent_id()) + events = ( + memory.search_raw(query, limit=limit) + if query + else memory.list_raw( + session_id=session_id, + host=host, + user=user or _caller_name(), + limit=limit, + ) + ) + return { + "events": [ + { + "event_id": e.id, + "timestamp": e.timestamp.isoformat(), + "session_id": e.session_id, + "user": e.user, + "event_type": e.event_type, + "source": (e.payload or {}).get("source") if e.payload else None, + "content": e.content, + } + for e in events + ], + "count": len(events), + } + + return await asyncio.to_thread(_run) + + @mcp.tool() + async def memory_candidates( + status: str | None = None, + session_id: str | None = None, + limit: int = 50, + ) -> dict[str, Any]: + """**查候选记忆(仅专家所有者)**:列出 L1 候选(默认 pending 队列)。 + + Args: + status: pending / promoted / rejected / needs_review / conflict。 + session_id: 按来源会话过滤。 + limit: 最多返回条数,默认 50。 + """ + _assert_expert_owner(server) + from octop_memory.core import CandidateStatus # noqa: PLC0415 + + status_value: str | None = None + if status: + allowed = set(get_args(CandidateStatus)) + if status not in allowed: + raise ValueError( + f"invalid candidate status {status!r}; expected one of {sorted(allowed)}" + ) + status_value = status + + def _run() -> dict[str, Any]: + candidates = _open_memory(server, _agent_id()).list_candidates( + status=status_value, + session_id=session_id, + limit=limit, + ) + return { + "candidates": [ + { + "candidate_id": c.id, + "status": c.status.value if hasattr(c.status, "value") else str(c.status), + "candidate_type": getattr(c, "candidate_type", None), + "assertion": getattr(c, "assertion", None), + "session_id": getattr(c, "session_id", None), + "confidence": getattr(c, "confidence", None), + } + for c in candidates + ], + "count": len(candidates), + } + + return await asyncio.to_thread(_run) + + @mcp.tool() + async def memory_extract( + session_id: str | None = None, + promote: bool | None = None, + ) -> dict[str, Any]: + """**手动触发提炼(仅专家所有者)**:把 L0 事件提炼为候选。 + + 专家未运行时直接返回错误(不会为了提炼而启动专家,避免意外模型开销)。 + + Args: + session_id: 只提炼该会话;缺省为 ``manual``。 + promote: 是否对候选执行晋升检查;缺省跟随专家自己的提炼配置(外部写入 + 默认进入审核队列)。 + """ + _assert_expert_owner(server) + agent_id = _agent_id() + service = _require_live_service(server, agent_id) + effective_session = session_id or "manual" + effective_promote = ( + _expert_extract_promote(server, agent_id) if promote is None else bool(promote) + ) + + def _run() -> dict[str, Any]: + result = service.extract( + effective_session, + incremental=True, + promote=effective_promote, + regen_pages=False, + ) + if not isinstance(result, dict): + return { + "session_id": effective_session, + "candidates": 0, + "promoted": 0, + "promote": effective_promote, + } + promotion = result.get("promotion") + return { + "session_id": effective_session, + "events_considered": result.get("events_considered", 0), + "candidates": result.get("candidates", 0), + "promoted": promotion.get("promoted", 0) if isinstance(promotion, dict) else 0, + "promote": effective_promote, + "error": result.get("failure_reason"), + "promotion": promotion, + } + + return await asyncio.to_thread(_run) + + @mcp.tool() + async def memory_promote(candidate_ids: list[str]) -> dict[str, Any]: + """**采纳候选(仅专家所有者)**:与记忆面板「采纳」同一套实现。 + + Args: + candidate_ids: 要采纳的候选 id 列表(来自 ``memory_candidates``)。 + """ + _assert_expert_owner(server) + bridge = _dashboard_bridge(_open_memory(server, _agent_id())) + + def _run() -> dict[str, Any]: + totals = dict.fromkeys( + ("promoted", "merged", "conflicts", "needs_review", "dropped", "llm_calls"), + 0, + ) + candidates: list[dict[str, Any]] = [] + errors: list[dict[str, Any]] = [] + for candidate_id in candidate_ids: + try: + detail = _bridge_rpc( + bridge, "promote_candidate", {"candidate_id": candidate_id} + ) + except Exception as exc: # noqa: BLE001 - 逐条隔离,错误原样回给调用方 + errors.append({"candidate_id": candidate_id, "error": str(exc)}) + continue + candidates.append({"candidate_id": candidate_id, **detail}) + for key in totals: + totals[key] += int(detail.get(key) or 0) + return {**totals, "candidates": candidates, "errors": errors} + + return await asyncio.to_thread(_run) + + @mcp.tool() + async def memory_reject( + candidate_id: str, + reason: str = "rejected by external caller", + actor: str | None = None, + ) -> dict[str, Any]: + """**忽略候选(仅专家所有者)**:与记忆面板「忽略」同一套实现。 + + Args: + candidate_id: 候选 id。 + reason: 忽略原因(写入 journal note)。 + actor: 决策者,缺省 ``user``;可选 ``auto`` / ``rule``。 + """ + _assert_expert_owner(server) + params: dict[str, Any] = {"candidate_id": candidate_id} + if reason: + params["reason"] = reason + if actor: + params["actor"] = actor + + def _run() -> dict[str, Any]: + result = _bridge_rpc( + _dashboard_bridge(_open_memory(server, _agent_id())), + "reject_candidate", + params, + ) + return {"ok": True, **result} + + return await asyncio.to_thread(_run) + + return mcp + + +def _memory_service_of(agent: Any) -> Any | None: + """``MemoryService`` on a live harness agent, or ``None`` when memory is off.""" + runtime = getattr(agent, "_memory_runtime", None) + return getattr(runtime, "service", None) if runtime is not None else None + + +def _live_memory_service(registry: Any, agent_id: str) -> Any | None: + """Return the in-process ``MemoryService`` when the expert is already loaded.""" + get_agent = getattr(registry, "get_agent", None) + if not callable(get_agent): + return None + try: + agent = get_agent(agent_id) + except Exception: # noqa: BLE001 — not running / not found / harness down + return None + return _memory_service_of(agent) + + +def _require_live_service(server: OctopServer, agent_id: str) -> Any: + """The expert's live ``MemoryService``; never starts a stopped expert. + + Starting an expert spends the owner's model budget, so the MCP layer refuses + instead (the review asked not to auto-start experts). + """ + runtime = getattr(server, "app_runtime", None) + registry = getattr(runtime, "agent_registry", None) if runtime is not None else None + service = _live_memory_service(registry, agent_id) if registry is not None else None + if service is None: + raise RuntimeError( + f"expert {agent_id!r} is not running, so its MemoryService is unavailable; " + "start the expert (or use memory_capture, which writes L0 without it)" + ) + return service + + +def _expert_extract_promote(server: OctopServer, agent_id: str) -> bool: + """Whether extraction should promote, following the expert's extraction config. + + Default is ``False``: external writes wait in the review queue. When the live + ``MemoryService`` exposes its runtime config, use its ``promote`` value. + """ + runtime = getattr(server, "app_runtime", None) + registry = getattr(runtime, "agent_registry", None) if runtime is not None else None + service = _live_memory_service(registry, agent_id) if registry is not None else None + service_runtime = getattr(service, "_runtime", None) if service is not None else None + cfg_fn = getattr(service_runtime, "_extraction_cfg", None) + if callable(cfg_fn): + try: + return bool(cfg_fn().get("promote", False)) + except Exception: # noqa: BLE001 - a foreign runtime must not break capture + return False + return False + + +def _schedule_extract( + server: OctopServer, + agent_id: str, + session_id: str, + *, + promote: bool, +) -> bool: + """Schedule a background extract (serialized per expert, never auto-starts).""" + runtime = getattr(server, "app_runtime", None) + registry = getattr(runtime, "agent_registry", None) if runtime is not None else None + if registry is None: + return False + service = _live_memory_service(registry, agent_id) + if service is None: + logger.info( + "memory extract skipped for agent %s session %s: expert not running", + agent_id, + session_id, + ) + return False + + async def _run() -> None: + lock = _EXTRACT_LOCKS.setdefault(agent_id, asyncio.Lock()) + async with lock: + try: + result = await asyncio.to_thread( + service.extract, + session_id, + incremental=True, + promote=promote, + regen_pages=True, + ) + except Exception: # noqa: BLE001 - background work must not crash the loop + logger.warning( + "memory extract failed for agent %s session %s", + agent_id, + session_id, + exc_info=True, + ) + return + reason = _extract_failure_reason(result) + if reason: + logger.warning( + "memory extract failed for agent %s session %s: %s", + agent_id, + session_id, + reason, + ) + + try: + task = asyncio.get_running_loop().create_task(_run()) + except RuntimeError: # no running loop (sync test call path) + logger.warning("memory extract not scheduled for agent %s: no running loop", agent_id) + return False + _EXTRACT_TASKS.add(task) + task.add_done_callback(_EXTRACT_TASKS.discard) + return True + + +def _extract_failure_reason(result: Any) -> str | None: + """Return the failure reason reported by ``MemoryService.extract``, if any.""" + if not isinstance(result, dict): + return None + for key in ("failure_reason", "error"): + value = result.get(key) + if isinstance(value, str) and value.strip(): + return value.strip() + return None + + +# ─── ASGI auth: user token -> user, then expert binding + access check ─────── + + +async def _send_json(send: Any, status: int, body: dict[str, Any]) -> None: + import json # noqa: PLC0415 + + payload = json.dumps(body, ensure_ascii=False).encode("utf-8") + await send( + { + "type": "http.response.start", + "status": status, + "headers": [ + (b"content-type", b"application/json; charset=utf-8"), + (b"content-length", str(len(payload)).encode("ascii")), + ], + } + ) + await send({"type": "http.response.body", "body": payload}) + + +def _headers(scope: dict[str, Any]) -> dict[str, str]: + return {k.decode("latin-1").lower(): v.decode("latin-1") for k, v in scope.get("headers", [])} + + +class _TokenAuthMiddleware: + """Resolve the caller from a user-bound bearer token. + + The token is looked up by its sha256 hash and compared in constant time; the + caller identity is the token's owner. ``X-Octop-User-Id`` is deliberately + ignored — a client cannot name somebody else. + """ + + def __init__(self, app: Any, server: OctopServer) -> None: + self._app = app + self._server = server + + async def __call__(self, scope: dict[str, Any], receive: Any, send: Any) -> None: + if scope.get("type") != "http": + await self._app(scope, receive, send) + return + + headers = _headers(scope) + auth = headers.get("authorization", "") + provided = auth[7:].strip() if auth.startswith("Bearer ") else "" + if not provided: + provided = headers.get("x-octop-memory-token", "").strip() + if not provided: + await _send_json(send, 401, {"error": "missing bearer token"}) + return + + services = self._server.services + repo = getattr(getattr(services, "repos", None), "memory_mcp_token_repo", None) + if repo is None: + await _send_json(send, 401, {"error": "token store unavailable"}) + return + + digest = hash_token(provided) + row = repo.get_by_token_hash(digest) + if row is None or not token_matches(provided, row.token_hash): + await _send_json(send, 401, {"error": "invalid token"}) + return + if row.revoked_at: + await _send_json(send, 401, {"error": "token revoked"}) + return + user = _user_of(self._server, row.user_id) + if user is None: + await _send_json(send, 401, {"error": "token owner not found"}) + return + + user_id_cv = _current_user_id.set(int(row.user_id)) + user_name_cv = _current_user_name.set(str(getattr(user, "username", "") or row.user_id)) + token_name_cv = _current_token_name.set(row.name) + scope_cv = _current_token_scope.set(list(row.expert_ids) if row.expert_ids else None) + try: + repo.touch(row.token_id) + await self._app(scope, receive, send) + finally: + _current_token_scope.reset(scope_cv) + _current_token_name.reset(token_name_cv) + _current_user_name.reset(user_name_cv) + _current_user_id.reset(user_id_cv) + + +class _AgentRouter: + """Validate ``X-Octop-Agent-Id`` and the token owner's access to that expert. + + The expert set is not snapshotted: existence / ``enabled`` / ownership are + re-checked per request, so experts created, shared or disabled after process + start take effect immediately. + """ + + def __init__(self, app: Any, server: OctopServer) -> None: + self._app = app + self._server = server + + async def __call__(self, scope: dict[str, Any], receive: Any, send: Any) -> None: + if scope.get("type") != "http": + await self._app(scope, receive, send) + return + + headers = _headers(scope) + agent_id = headers.get("x-octop-agent-id", "").strip() + services = self._server.services + row = services.agent_repo.get(agent_id) if (services is not None and agent_id) else None + if row is None or not row.enabled: + await _send_json(send, 404, {"error": "missing or unknown agent_id (X-Octop-Agent-Id)"}) + return + + user = _user_of(self._server, _current_user_id.get()) + token_scope = _current_token_scope.get() + if token_scope is not None and agent_id not in token_scope: + await _send_json( + send, + 403, + {"error": "this token is not scoped to that expert", "agent_id": agent_id}, + ) + return + if not _user_may_access_agent(self._server, row, user): + await _send_json( + send, + 403, + {"error": "token owner cannot access that expert", "agent_id": agent_id}, + ) + return + + agent_cv = _current_agent_id.set(agent_id) + try: + await self._app(scope, receive, send) + finally: + _current_agent_id.reset(agent_cv) + + +def _chain_lifespan(previous: Any, managers: list[Any]) -> Callable[[Any], Any]: + """Wrap the host lifespan so MCP session managers run without hiding hooks.""" + + @asynccontextmanager + async def _lifespan(app: Any) -> AsyncIterator[None]: + async with AsyncExitStack() as stack: + for manager in managers: + await stack.enter_async_context(manager.run()) + if previous is not None: + await stack.enter_async_context(previous(app)) + yield + + return _lifespan + + +def _memory_mcp_enabled(server: OctopServer) -> bool: + """Mount when explicitly enabled or when at least one token exists.""" + flag = (os.environ.get("OCTOP_MEMORY_MCP_ENABLED") or "").strip().lower() + if flag in {"1", "true", "yes", "on"}: + return True + if flag in {"0", "false", "no", "off"}: + return False + services = server.services + repo = getattr(getattr(services, "repos", None), "memory_mcp_token_repo", None) + if repo is None: + return False + try: + return bool(repo.any_active()) + except Exception: # noqa: BLE001 - a fresh/read-only DB simply means "not yet" + return False + + +def install_memory_mcp(app: Any, server: OctopServer) -> None: + """Mount ``/mcp/memory`` and keep the host lifespan intact. + + Mounts only when ``OCTOP_MEMORY_MCP_ENABLED`` is on or a user token already + exists (fail-closed: an unconfigured instance exposes nothing). The MCP + session managers are chained into the existing FastAPI lifespan instead of + replacing it, so future startup/shutdown hooks keep working. + """ + if not _memory_mcp_enabled(server): + return + services = server.services + assert services is not None, "server.services required for memory MCP mount" + + mcp = build_memory_mcp(server) + # _session_manager is created lazily by streamable_http_app(); read it only + # after building, and drop None entries (stateless mode has none). + streamable_app = mcp.streamable_http_app() + managers = [mgr for mgr in (getattr(mcp, "_session_manager", None),) if mgr is not None] + + app.mount("/mcp/memory", _TokenAuthMiddleware(_AgentRouter(streamable_app, server), server)) + if managers: + app.router.lifespan_context = _chain_lifespan(app.router.lifespan_context, managers) + + +__all__ = ["build_memory_mcp", "install_memory_mcp"] diff --git a/tests/unit/agents/test_memory_mcp.py b/tests/unit/agents/test_memory_mcp.py new file mode 100644 index 0000000000..c510c8998a --- /dev/null +++ b/tests/unit/agents/test_memory_mcp.py @@ -0,0 +1,577 @@ +"""Unit tests for the expert memory MCP server (infra/agents/memory_mcp). + +Covers the post-review surface: user-bound token auth, per-request expert access +checks, capture through ``MemoryRuntime.capture`` (redaction included), save +through ``MemoryService.save``, owner-only review tools, and the lifespan / +mount rules. +""" + +from __future__ import annotations + +import asyncio +import inspect +from types import SimpleNamespace +from typing import Any + +import pytest + +from octop.infra.agents import memory_mcp as mm + +# ─── fakes ────────────────────────────────────────────────────────────────── + + +class _User: + def __init__(self, user_id: int, username: str, *, is_admin: bool = False) -> None: + self.id = user_id + self.username = username + self.is_admin = is_admin + + +class _UserManager: + def __init__(self, users: dict[int, _User]) -> None: + self._users = users + + def get_by_id(self, user_id: int) -> _User | None: + return self._users.get(user_id) + + +class _AgentRow: + def __init__( + self, *, user_id: int | None, enabled: bool = True, is_shared: bool = False + ) -> None: + self.user_id = user_id + self.enabled = enabled + self.is_shared = 1 if is_shared else 0 + self.config_json = None + + +class _AgentRepo: + def __init__(self, rows: dict[str, _AgentRow]) -> None: + self._rows = rows + + def get(self, agent_id: str) -> _AgentRow | None: + return self._rows.get(agent_id) + + +class _TokenRow: + def __init__( + self, + *, + token_id: str = "tok1", + user_id: int = 1, + name: str = "codex@macbook", + expert_ids: list[str] | None = None, + token_hash: str, + revoked_at: str | None = None, + ) -> None: + self.token_id = token_id + self.user_id = user_id + self.name = name + self.expert_ids = expert_ids + self.token_hash = token_hash + self.revoked_at = revoked_at + + +class _TokenRepo: + def __init__(self, rows: dict[str, _TokenRow] | None = None) -> None: + self.rows = rows or {} + self.touched: list[str] = [] + + def get_by_token_hash(self, token_hash: str) -> _TokenRow | None: + return self.rows.get(token_hash) + + def touch(self, token_id: str) -> None: + self.touched.append(token_id) + + def any_active(self) -> bool: + return any(row.revoked_at is None for row in self.rows.values()) + + +def _server( + *, + agents: dict[str, _AgentRow] | None = None, + users: dict[int, _User] | None = None, + token_repo: _TokenRepo | None = None, + app_runtime: Any = None, +) -> Any: + repos = SimpleNamespace( + memory_mcp_token_repo=token_repo or _TokenRepo(), + agent_repo=_AgentRepo(agents or {"A1": _AgentRow(user_id=1)}), + ) + services = SimpleNamespace(repos=repos, agent_repo=repos.agent_repo, config=None, paths=None) + return SimpleNamespace( + services=services, + user_manager=_UserManager(users or {1: _User(1, "alice")}), + app_runtime=app_runtime, + ) + + +def _tools(mcp: Any) -> dict[str, Any]: + return mcp._tool_manager._tools + + +def _run(coro: Any) -> Any: + return asyncio.run(coro) + + +def _call_tool(mcp: Any, name: str, **kwargs: Any) -> Any: + return _run(_tools(mcp)[name].fn(**kwargs)) + + +@pytest.fixture +def bind_agent(): + token = mm._current_agent_id.set("A1") + try: + yield "A1" + finally: + mm._current_agent_id.reset(token) + + +@pytest.fixture +def bind_user(): + user_cv = mm._current_user_id.set(1) + name_cv = mm._current_user_name.set("alice") + try: + yield "alice" + finally: + mm._current_user_name.reset(name_cv) + mm._current_user_id.reset(user_cv) + + +# ─── registration ─────────────────────────────────────────────────────────── + + +def test_build_registers_nine_tools(): + mcp = mm.build_memory_mcp(_server()) + assert set(_tools(mcp)) == { + "memory_search", + "memory_get", + "memory_save", + "memory_capture", + "memory_raws", + "memory_candidates", + "memory_extract", + "memory_promote", + "memory_reject", + } + + +def test_every_tool_is_async(): + """Blocking DB work must run off the event loop (review item 4).""" + mcp = mm.build_memory_mcp(_server()) + for name, tool in _tools(mcp).items(): + assert inspect.iscoroutinefunction(tool.fn), name + + +def test_tools_run_blocking_work_off_the_event_loop(bind_agent, bind_user, monkeypatch): + """Review test ask: a tool call must not block other requests.""" + import threading + + seen: list[int] = [] + + def fake_pipeline(memory, query, max_results, *, atom_only, caller=""): + seen.append(threading.get_ident()) + return [] + + monkeypatch.setattr(mm, "_pipeline_hits", fake_pipeline) + monkeypatch.setattr(mm, "_open_memory", lambda server, agent_id: SimpleNamespace()) + mcp = mm.build_memory_mcp(_server()) + + result = _call_tool(mcp, "memory_search", query="x") + + assert result["hits"] == [] + assert seen and seen[0] != threading.get_ident() + + +# ─── token auth middleware ────────────────────────────────────────────────── + + +def _http_scope(headers: dict[str, str]) -> dict[str, Any]: + return { + "type": "http", + "method": "POST", + "path": "/mcp/memory/", + "headers": [(k.lower().encode(), v.encode()) for k, v in headers.items()], + } + + +async def _capture_response( + middleware: Any, scope: dict[str, Any] +) -> tuple[int, list[dict[str, Any]]]: + status: list[int] = [] + bodies: list[dict[str, Any]] = [] + + async def receive() -> dict[str, Any]: + return {"type": "http.request", "body": b"", "more_body": False} + + async def send(message: dict[str, Any]) -> None: + if message["type"] == "http.response.start": + status.append(int(message["status"])) + elif message["type"] == "http.response.body": + bodies.append(message) + + await middleware(scope, receive, send) + return (status[0] if status else 0), bodies + + +async def test_middleware_rejects_missing_token(): + inner_called = False + + async def inner(scope, receive, send): + nonlocal inner_called + inner_called = True + + mw = mm._TokenAuthMiddleware(inner, _server()) + status, _ = await _capture_response(mw, _http_scope({})) + assert status == 401 + assert inner_called is False + + +async def test_middleware_rejects_unknown_and_revoked_tokens(): + repo = _TokenRepo() + mw = mm._TokenAuthMiddleware(lambda *a: None, _server(token_repo=repo)) + status, _ = await _capture_response(mw, _http_scope({"authorization": "Bearer omcp_unknown"})) + assert status == 401 + + token = "omcp_live" + repo.rows[mm.hash_token(token)] = _TokenRow( + token_hash=mm.hash_token(token), revoked_at="2026-01-01T00:00:00Z" + ) + status, _ = await _capture_response(mw, _http_scope({"authorization": f"Bearer {token}"})) + assert status == 401 + + +async def test_middleware_sets_user_from_token_and_ignores_user_header(): + token = "omcp_live" + repo = _TokenRepo() + repo.rows[mm.hash_token(token)] = _TokenRow(token_hash=mm.hash_token(token)) + seen: dict[str, Any] = {} + + async def inner(scope, receive, send): + seen["user_id"] = mm._current_user_id.get() + seen["user_name"] = mm._current_user_name.get() + seen["token_name"] = mm._current_token_name.get() + + mw = mm._TokenAuthMiddleware(inner, _server(token_repo=repo)) + status, _ = await _capture_response( + mw, + _http_scope( + { + "authorization": f"Bearer {token}", + # a client cannot name somebody else: + "x-octop-user-id": "bob", + } + ), + ) + assert status != 401 + assert seen == {"user_id": 1, "user_name": "alice", "token_name": "codex@macbook"} + assert repo.touched == ["tok1"] + + +# ─── expert binding + access checks ──────────────────────────────────────── + + +async def _run_agent_router(server: Any, agent_id: str) -> tuple[int, bool]: + called = False + + async def inner(scope, receive, send): + nonlocal called + called = True + + router = mm._AgentRouter(inner, server) + status, _ = await _capture_response( + router, + _http_scope({"x-octop-agent-id": agent_id}), + ) + return status, called + + +async def test_agent_router_rejects_unknown_and_disabled_agents(): + server = _server(agents={"A1": _AgentRow(user_id=1, enabled=False)}) + user_cv = mm._current_user_id.set(1) + try: + status, called = await _run_agent_router(server, "missing") + assert status == 404 and called is False + status, called = await _run_agent_router(server, "A1") + assert status == 404 and called is False + finally: + mm._current_user_id.reset(user_cv) + + +async def test_agent_router_enforces_token_scope(): + server = _server(agents={"A1": _AgentRow(user_id=1), "A2": _AgentRow(user_id=1)}) + user_cv = mm._current_user_id.set(1) + scope_cv = mm._current_token_scope.set(["A2"]) + try: + status, called = await _run_agent_router(server, "A1") + assert status == 403 and called is False + status, called = await _run_agent_router(server, "A2") + assert called is True + finally: + mm._current_token_scope.reset(scope_cv) + mm._current_user_id.reset(user_cv) + + +async def test_agent_router_rejects_other_users_expert(): + server = _server(agents={"A1": _AgentRow(user_id=2)}) + user_cv = mm._current_user_id.set(1) + try: + status, called = await _run_agent_router(server, "A1") + assert status == 403 and called is False + finally: + mm._current_user_id.reset(user_cv) + + +async def test_agent_router_allows_shared_expert(monkeypatch): + server = _server(agents={"A1": _AgentRow(user_id=2, is_shared=True)}) + monkeypatch.setattr( + "octop.infra.users.groups.expert_visible_to", lambda *a, **k: True, raising=False + ) + user_cv = mm._current_user_id.set(1) + try: + status, called = await _run_agent_router(server, "A1") + assert called is True + finally: + mm._current_user_id.reset(user_cv) + + +# ─── capture: runtime application layer + redaction ──────────────────────── + + +class _FakeRuntime: + def __init__(self, result: dict[str, Any] | None = None) -> None: + self.calls: list[dict[str, Any]] = [] + self._result = result or {"accepted": 1} + + def capture(self, params: dict[str, Any]) -> dict[str, Any]: + self.calls.append(params) + return self._result + + +def test_capture_goes_through_runtime_with_metadata(bind_agent, bind_user, monkeypatch): + runtime = _FakeRuntime() + monkeypatch.setattr(mm, "_memory_runtime", lambda server, agent_id: runtime) + scheduled: list[tuple[str, str, bool]] = [] + monkeypatch.setattr( + mm, + "_schedule_extract", + lambda server, agent_id, session_id, *, promote: ( + scheduled.append((agent_id, session_id, promote)) or True + ), + ) + mcp = mm.build_memory_mcp(_server()) + + result = _call_tool( + mcp, + "memory_capture", + content="把 MCP 写入路径补齐", + assistant="决定改走 runtime.capture", + source="claude-code", + session_id="s1", + ) + + assert result["recorded"] is True + params = runtime.calls[0] + assert params["host"] == "claude-code" + assert params["session_id"] == "s1" + assert params["user"] == "alice" + events = params["events"] + assert [e["event_type"] for e in events] == ["user_message", "assistant_message"] + # caller goes into metadata, never into the body (review item 9): + assert "alice" not in events[0]["content"] + assert events[0]["payload"] == { + "source": "claude-code", + "source_host": "claude-code", + "caller": "alice", + } + assert scheduled == [("A1", "s1", False)] + + +def test_capture_dry_run_writes_nothing(bind_agent, bind_user, monkeypatch): + runtime = _FakeRuntime() + monkeypatch.setattr(mm, "_memory_runtime", lambda server, agent_id: runtime) + mcp = mm.build_memory_mcp(_server()) + result = _call_tool(mcp, "memory_capture", content="x", source="codex", dry_run=True) + assert result["dry_run"] is True + assert result["recorded"] is False + assert runtime.calls == [] + + +def test_capture_redacts_secrets_end_to_end(bind_agent, bind_user, monkeypatch, tmp_path): + """Review test ask: secrets in captured content must be filtered.""" + from octop_memory.application.runtime import MemoryRuntime + from octop_memory.core import Memory + + memory = Memory( + namespace="mcp-test", + backend="sqlite", + backend_config={"db_path": str(tmp_path / "memory.sqlite")}, + ) + runtime = MemoryRuntime(memory) + monkeypatch.setattr(mm, "_memory_runtime", lambda server, agent_id: runtime) + monkeypatch.setattr(mm, "_schedule_extract", lambda *a, **k: False) + mcp = mm.build_memory_mcp(_server()) + + secret = "sk-abcdefgh12345678" + _call_tool(mcp, "memory_capture", content=f"用这个 key:{secret}", source="codex") + + events = list(memory.list_raw(limit=10)) + assert events, "capture should have written an L0 event" + assert all(secret not in event.content for event in events) + assert "[REDACTED]" in events[0].content + + +# ─── save: MemoryService application layer ──────────────────────────────── + + +class _FakeService: + def __init__(self) -> None: + self.saved: list[dict[str, Any]] = [] + self.memory = SimpleNamespace(deprecate_atom=lambda *a, **k: True) + self._runtime = SimpleNamespace(_extraction_cfg=lambda: {"promote": True}) + + def save(self, content: str, *, topic=None, metadata=None): + self.saved.append({"content": content, "topic": topic, "metadata": metadata}) + return SimpleNamespace(id="n1", content=content) + + +def test_save_goes_through_memory_service(bind_agent, bind_user, monkeypatch): + service = _FakeService() + monkeypatch.setattr(mm, "_require_live_service", lambda server, agent_id: service) + mcp = mm.build_memory_mcp(_server()) + + result = _call_tool(mcp, "memory_save", content="项目约定:先跑 make all", source="codex") + + assert result["node_id"] == "n1" + saved = service.saved[0] + assert saved["content"] == "项目约定:先跑 make all" # no `说:` prefix + assert saved["metadata"] == { + "source": "codex", + "source_host": "codex", + "caller": "alice", + } + + +def test_save_refuses_to_start_a_stopped_expert(bind_agent): + mcp = mm.build_memory_mcp(_server(app_runtime=None)) + with pytest.raises(RuntimeError, match="not running"): + _call_tool(mcp, "memory_save", content="x", source="codex") + + +# ─── review tools are expert-owner only ─────────────────────────────────── + + +@pytest.mark.parametrize("tool", ["memory_raws", "memory_candidates", "memory_promote"]) +def test_ops_tools_are_owner_only(bind_agent, tool): + server = _server(agents={"A1": _AgentRow(user_id=2)}) + mcp = mm.build_memory_mcp(server) + user_cv = mm._current_user_id.set(1) + try: + with pytest.raises(PermissionError): + if tool == "memory_promote": + _call_tool(mcp, tool, candidate_ids=["c1"]) + else: + _call_tool(mcp, tool) + finally: + mm._current_user_id.reset(user_cv) + + +def test_ops_tools_allow_the_owner(bind_agent, bind_user, monkeypatch): + reached = [] + + def fake_open(server, agent_id): + reached.append(agent_id) + return SimpleNamespace(search_raw=lambda *a, **k: []) + + monkeypatch.setattr(mm, "_open_memory", fake_open) + mcp = mm.build_memory_mcp(_server()) + _call_tool(mcp, "memory_raws", query="x") + assert reached == ["A1"] + + +# ─── extract: no auto-start, promote follows the expert config ──────────── + + +def _extract_service(calls: list[dict[str, Any]]) -> Any: + class _Service(_FakeService): + def extract(self, session_id, *, incremental, promote, regen_pages): + calls.append( + { + "session_id": session_id, + "incremental": incremental, + "promote": promote, + "regen_pages": regen_pages, + } + ) + return {"candidates": 2, "promotion": {"promoted": 1}} + + return _Service() + + +def test_extract_follows_expert_promote_config(bind_agent, bind_user, monkeypatch): + calls: list[dict[str, Any]] = [] + monkeypatch.setattr( + mm, "_require_live_service", lambda server, agent_id: _extract_service(calls) + ) + monkeypatch.setattr(mm, "_expert_extract_promote", lambda server, agent_id: True) + mcp = mm.build_memory_mcp(_server()) + result = _call_tool(mcp, "memory_extract", session_id="s1") + assert result["promoted"] == 1 + assert calls[0]["promote"] is True + assert calls[0]["incremental"] is True + + +def test_extract_explicit_promote_false_wins(bind_agent, bind_user, monkeypatch): + calls: list[dict[str, Any]] = [] + monkeypatch.setattr( + mm, "_require_live_service", lambda server, agent_id: _extract_service(calls) + ) + mcp = mm.build_memory_mcp(_server()) + _call_tool(mcp, "memory_extract", session_id="s1", promote=False) + assert calls[0]["promote"] is False + + +def test_expert_promote_reads_the_service_config(): + service = SimpleNamespace(_runtime=SimpleNamespace(_extraction_cfg=lambda: {"promote": True})) + agent = SimpleNamespace(_memory_runtime=SimpleNamespace(service=service)) + registry = SimpleNamespace(get_agent=lambda agent_id: agent) + server = _server(app_runtime=SimpleNamespace(agent_registry=registry)) + assert mm._expert_extract_promote(server, "A1") is True + # no live service -> review queue default + assert mm._expert_extract_promote(_server(app_runtime=None), "A1") is False + + +async def test_schedule_extract_never_starts_a_stopped_expert(monkeypatch): + server = _server(app_runtime=SimpleNamespace(agent_registry=object())) + assert mm._schedule_extract(server, "A1", "s1", promote=False) is False + + +# ─── mount + lifespan ───────────────────────────────────────────────────── + + +def test_mount_is_fail_closed(monkeypatch): + monkeypatch.delenv("OCTOP_MEMORY_MCP_ENABLED", raising=False) + assert mm._memory_mcp_enabled(_server(token_repo=_TokenRepo())) is False + monkeypatch.setenv("OCTOP_MEMORY_MCP_ENABLED", "1") + assert mm._memory_mcp_enabled(_server(token_repo=_TokenRepo())) is True + + +def test_lifespan_chain_keeps_the_previous_hooks(): + from contextlib import asynccontextmanager + + seen: list[str] = [] + + @asynccontextmanager + async def previous(app): + seen.append("previous-enter") + yield + seen.append("previous-exit") + + chained = mm._chain_lifespan(previous, []) + + async def _run_lifespan() -> None: + async with chained(None): + seen.append("body") + + asyncio.run(_run_lifespan()) + assert seen == ["previous-enter", "body", "previous-exit"] From da016b80a36f7a3888e56e87abef68ee1d783dd2 Mon Sep 17 00:00:00 2001 From: ArvinQi <12555402+ArvinQi@users.noreply.github.com> Date: Mon, 9 Nov 2026 22:06:44 +0800 Subject: [PATCH 03/39] =?UTF-8?q?docs(memory-mcp):=20=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E6=8E=A5=E5=85=A5=E6=96=87=E6=A1=A3=E4=B8=8E=20CHANGELOG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - docs/memory-mcp.md 重写为 PR a 的实际形态:用户级 token 的创建/吊销、挂载 开关、9 个工具(4 个日常 + 5 个仅专家所有者)、客户端配置与拆分计划 - CHANGELOG 改为描述用户级 token 与审核队列 --- CHANGELOG.md | 1 + docs/memory-mcp.md | 144 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 145 insertions(+) create mode 100644 docs/memory-mcp.md diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d62200c64..8050af36c7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ ### 新增 - 专家市场默认打开「精选」,用书签标出接入市场前随安装包提供的 22 个专家模板 - 通道卡片与 `/api/health` 展示每个通道的入站、出站、重连和丢弃计数;记忆存储页显示 checkpoint 保留窗口与 VACUUM 提示 +- 专家记忆新增 MCP Server(`/mcp/memory`):外部 Agent / Bot 用用户级 token 检索、写入、采集专家记忆;token 可按客户端命名、限定专家范围并单独吊销,外部写入默认进入审核队列 ### 修复 - 关机时会等正在进行的数据库查询结束再关闭连接,避免 Windows 上测试进程被访问冲突打断 diff --git a/docs/memory-mcp.md b/docs/memory-mcp.md new file mode 100644 index 0000000000..d1a4896fce --- /dev/null +++ b/docs/memory-mcp.md @@ -0,0 +1,144 @@ +# 专家记忆 MCP Server + +Octop 把专家记忆通过 MCP(Model Context Protocol)开放给外部 Agent / Bot:Claude Code、Codex、DSH 这类编码助手可以和 Octop 专家共用同一份长期记忆。 + +- 端点:`/mcp/memory`(Streamable HTTP,**stateless**:每次请求独立,服务重启后客户端不会因为 `Mcp-Session-Id` 失效而报 `-32600 Session not found`) +- 专家选择:请求头 `X-Octop-Agent-Id`(每次请求都可换,新建 / 停用专家即时生效,无需重启) +- 调用者身份:**用户级 token**(`Authorization: Bearer omcp_…`),不是实例共享 token;`X-Octop-User-Id` 不再被信任 + +## 1. 鉴权模型 + +每个 Octop 用户可以有多个 token,每个 token 带一个客户端名字(如 `codex@macbook`),可以: + +- 只允许访问指定的专家(`expert_ids`),不填 = 该用户有权访问的所有专家 +- 单独吊销某一个客户端,不影响其它客户端 +- 追溯每一条记忆是哪个客户端写的(原始事件的 `payload.caller` / `payload.source_host`) + +服务端只保存 token 的 sha256;校验用常量时间比较。**明文只在创建时返回一次**。 + +### 1.1 创建 / 列出 / 吊销 token + +登录 Octop 后调用(`` 是 Octop 的登录 token,与 MCP token 不是一回事): + +```bash +# 创建:返回一次性明文 token +curl -sS -X POST "https:///api/memory-mcp/tokens" \ + -H "Authorization: Bearer " \ + -H "Content-Type: application/json" \ + -d '{"name": "codex@macbook", "expert_ids": ["Z026BY"]}' + +# 列出(不含明文) +curl -sS "https:///api/memory-mcp/tokens" \ + -H "Authorization: Bearer " + +# 吊销 +curl -sS -X DELETE "https:///api/memory-mcp/tokens/" \ + -H "Authorization: Bearer " +``` + +`POST` 响应里的 `token` 字段形如 `omcp_…`,之后只显示 `token_hint`(`omcp_abcd…wxyz`)。 + +### 1.2 开启挂载 + +`/mcp/memory` 默认不挂载(fail-closed,对现有部署零影响),满足任一条件才挂载: + +- 设置环境变量 `OCTOP_MEMORY_MCP_ENABLED=1`(显式开启),或 +- 数据库里已经存在至少一个有效 token + +首次创建 token 后重启一次 Octop 即可生效。 + +## 2. 客户端配置 + +任何支持 Streamable HTTP MCP 的客户端都可以接入,只需三样东西: + +| 配置 | 值 | +|------|----| +| URL | `https:///mcp/memory/`(或自建入口,见 §5) | +| Header `Authorization` | `Bearer omcp_…`(§1.1 创建的那个用户 token) | +| Header `X-Octop-Agent-Id` | 专家 ID(如 `Z026BY`) | + +### 2.1 Claude Code + +```bash +claude mcp add --transport http octop-memory "https:///mcp/memory/" \ + --header "Authorization: Bearer omcp_…" \ + --header "X-Octop-Agent-Id: <专家ID>" +``` + +### 2.2 Codex + +`~/.codex/config.toml`: + +```toml +[mcp_servers.octop-memory] +url = "https:///mcp/memory/" +http_headers = { Authorization = "Bearer omcp_…", "X-Octop-Agent-Id" = "<专家ID>" } +``` + +### 2.3 DSH / Cursor / Kiro / 其它客户端 + +在客户端的 MCP 配置里填 §2 的 URL 与两个 header 即可(DSH 的 `mcpServers` 用 `type: "streamable-http"`)。 + +> 一键安装器与 hooks(提问前自动召回、会话结束自动采集)会在后续 PR 里单独提交,不在本次变更范围内。 + +## 3. 工具清单 + +### 3.1 日常工具(任何 token 可用) + +| 工具 | 作用 | +|------|------| +| `memory_search(query, max_results=5, corpus="all")` | 检索记忆,返回带虚拟 `path` 的命中(`corpus=raw` 只看原始事件) | +| `memory_get(path, start=None, lines=None)` | 把命中里的 `path` 解析成 markdown 全文 | +| `memory_save(content, source, topic=None, supersedes=None)` | 显式保存一条事实(跳过提炼,立即可召回;`supersedes` 用于更正) | +| `memory_capture(content, source, session_id=None, assistant=None, thread_id=None, dry_run=False, extract=True)` | 写原始事件(L0),交给提炼管线;默认**不自动晋升** | + +### 3.2 审核 / 运维工具(**仅专家所有者**) + +| 工具 | 作用 | +|------|------| +| `memory_raws(...)` | 查 L0 原始事件(FTS / 按会话、host、调用者过滤) | +| `memory_candidates(status=None, ...)` | 查 L1 候选(默认 pending) | +| `memory_extract(session_id=None, promote=None)` | 手动触发提炼;专家未运行时直接报错(不会为提炼启动专家) | +| `memory_promote(candidate_ids)` | 采纳候选(与记忆面板同一套实现) | +| `memory_reject(candidate_id, reason=..., actor=None)` | 忽略候选 | + +外部 agent 默认只应该拿到 §3.1 的四个工具;把审核工具(能自己晋升自己写的内容)留给专家所有者。 + +## 4. 记忆分层与选工具 + +| 层 | 内容 | 读 | 写 | +|----|------|----|----| +| L0 原始事件 | 一轮对话 / 一条记录 | `memory_raws`、`memory_search(corpus="raw")` | `memory_capture` | +| L1 候选 | 提炼出的候选断言 | `memory_candidates` | `memory_extract` | +| L2 原子 / L3 主题页 | 长期记忆 | `memory_search`、`memory_get` | `memory_save`、`memory_promote` | + +- 想把一段对话沉淀成记忆 → `memory_capture`,之后到记忆面板审核 +- 已经确定的事实 → `memory_save` +- 想找背景 → `memory_search`;要原文 → `memory_get` / `memory_raws` + +## 5. 部署与自建入口 + +- 反代 / ingress 只需暴露 `/mcp/memory`(以及给用户创建 token 用的 `/api/memory-mcp/tokens`) +- 客户端条目要指向别的地址(如公网入口)时,设置环境变量 `OCTOP_MEMORY_PUBLIC_MCP_URL`,或实例 `config.json` 的 `memory_public_mcp_url` +- token 的专家范围在创建时就固定;要调整请新建 token 并吊销旧的 + +## 6. 行为契约 + +- **脱敏**:`memory_capture` 走 octop-memory 的 capture 应用层,`redact_secrets` 默认开启,API key / token / password 会在入库前被替换 +- **回声保护**:带上游召回标记(`## Memory Recall` / `[memory] Earlier in this workspace`)的内容不会入库 +- **审核队列**:外部写入默认不自动晋升;是否晋升跟随专家自己的提炼配置 +- **不自动启动专家**:专家停止时 capture 仍写 L0,但不会触发提炼、也不会为了提炼把专家拉起来(避免意外模型费用) +- **权限**:token 属于某个用户;每次请求都会重新校验该用户对目标专家的访问权(自己的 / 共享的专家 / 管理员),改 header 无法越权 + +## 7. 拆分说明(review 后的计划) + +原 PR 把记忆 MCP、召回补丁、对话记录解析、终端安装器/hooks 放在了一起。按 review 建议拆成: + +| PR | 内容 | 目标仓库 | 状态 | +|----|------|----------|------| +| a | MCP 核心 + 用户级鉴权(本文档) | Octop | 本 PR | +| b | 召回质量优化(实体后缀 / 强 token 排序) | octop-memory | 待提 | +| c | 对话记录解析(Claude Code / Kiro / DSH → 原始事件) | octop-memory | 待提 | +| d | 客户端 hooks 与安装器 | Octop | 待提 | + +DSH 预设(`cordis.patch.yml`)不进入开源仓库。 From 3683574f57b45744ccdd4fe49ab1f70a81a9e4e8 Mon Sep 17 00:00:00 2001 From: ArvinQi <12555402+ArvinQi@users.noreply.github.com> Date: Mon, 9 Nov 2026 22:40:37 +0800 Subject: [PATCH 04/39] =?UTF-8?q?feat(memory-mcp):=20vendor=20DSH=20?= =?UTF-8?q?=E5=8E=9F=E7=94=9F=E8=AE=B0=E5=BF=86=E6=8F=92=E4=BB=B6=20octop-?= =?UTF-8?q?memory-native=EF=BC=88=E5=BC=80=E6=BA=90=E7=89=88=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit DSH 的记忆接入从 hook 桥接换成进程内原生插件(v0.1.2):每轮开始前召回并注入 共享记忆,轮末上报本轮增量,不再依赖外部子进程 / hook 方言 / `--use-system-ca`。 插件源码随本仓发版(vendored,`link:` 安装,离线可装)。 开源版相比闭源版本副本的差异只有身份与文案:去掉 userId / X-Octop-User-Id、把闭源专有鉴权头换成通用 Bearer 逻辑、README 的内网域名/员工号替换为公开占位符。 --- .../infra/agents/dsh_native_plugin/README.md | 70 ++++ .../agents/dsh_native_plugin/cordis.patch.yml | 30 ++ .../infra/agents/dsh_native_plugin/icon.svg | 7 + .../infra/agents/dsh_native_plugin/index.js | 374 ++++++++++++++++++ .../agents/dsh_native_plugin/lib/text.js | 113 ++++++ .../agents/dsh_native_plugin/locale/en.json | 6 + .../agents/dsh_native_plugin/locale/zh.json | 6 + .../agents/dsh_native_plugin/package.json | 26 ++ 8 files changed, 632 insertions(+) create mode 100644 src/octop/infra/agents/dsh_native_plugin/README.md create mode 100644 src/octop/infra/agents/dsh_native_plugin/cordis.patch.yml create mode 100644 src/octop/infra/agents/dsh_native_plugin/icon.svg create mode 100644 src/octop/infra/agents/dsh_native_plugin/index.js create mode 100644 src/octop/infra/agents/dsh_native_plugin/lib/text.js create mode 100644 src/octop/infra/agents/dsh_native_plugin/locale/en.json create mode 100644 src/octop/infra/agents/dsh_native_plugin/locale/zh.json create mode 100644 src/octop/infra/agents/dsh_native_plugin/package.json diff --git a/src/octop/infra/agents/dsh_native_plugin/README.md b/src/octop/infra/agents/dsh_native_plugin/README.md new file mode 100644 index 0000000000..615cbbc3df --- /dev/null +++ b/src/octop/infra/agents/dsh_native_plugin/README.md @@ -0,0 +1,70 @@ +# octop-memory-native + +DSH(DeepSeek Harness)原生「记忆共享」插件:**每轮开始前自动召回 octop 共享记忆并注入上下文,轮末把本轮的对话增量上报回 octop**,全程在 DSH 进程内完成。 + +它是「记忆共享模式」的 hooks 实现(`~/.dsh/hooks/dsh/*.mjs` + Claude Code hook 桥)的替代品:hooks 方案要背 5 个外部依赖(外部 Node 子进程、会话沙箱写权限、`--use-system-ca`、hook 方言映射、以及和 harness 版本锁死的 hook 桥包),任意一个变化都会让它**静默失效**。本插件只用 harness 的一等公民接缝。 + +## 为什么更稳 + +| | hooks 方案 | 本插件 | +|---|---|---| +| 执行位置 | 外部 shell 子进程(Claude Code hook 方言) | 进程内 Cordis 插件 | +| 版本耦合 | 依赖 `@deepseek-ai/dsh-hooks-claude-code` 精确版本,harness 升级即被闸门 deny | 无 `@deepseek-ai/*` 依赖声明,不进版本闸门 | +| 抓取/写日志 | 受会话沙箱限制,`~/.dsh/hooks` 不可写时静默降级到 `$TMPDIR` | 用 `ctx.logger`,失败可见 | +| CA/代理 | 需要 `NODE_OPTIONS=--use-system-ca` | host 进程已由 dsh-mcp 做进程级 CA 合并 | +| 召回质量 | 整句当 query,服务端短语匹配 → 0 命中 | 关键词扇出(多路单 token 查询 + 合并去重) | +| 状态 | 外部 json offset 文件 | 进程内按 session 去重 + turn 幂等 | + +## 接缝 + +- **召回**:`agent/pre-step`(waterfall,per-agent ctx)。每轮第一个 step、拿到新 user message 后,用关键词扇出查 `memory_search`,命中则以 `createUserMessage({ source: { kind: 'octop-memory-native', form: 'snapshot', sections } })` 注入本轮。做法与内置的 `time-context` 插件同构。 +- **上报**:`session/event` 的 `turn/end`(durable、post-commit)。收集该轮 user/assistant 文本,POST `memory_capture`(text 模式,user/assistant 分字段),按 turn 去重、按 session 幂等。 + 上报/召回前都会先剥掉 Harness 自动注入的运行时长文(`Current runtime context…`、`…`、子会话 delegate 前言),避免把技能清单/工作目录等噪声写进记忆、也避免它们污染召回关键词。 +- 没有新事件类型、不写 session 日志,因此不影响 fork / resume / replay。 + +## 认证 + +认证是**用户级 token**:token 随 `Authorization: Bearer ` 发送(`tokenHeader` 可改名,非 `Authorization` 的头名按原样写入),专家身份由 `X-Octop-Agent-Id` 表达。不再需要、也不会发送任何用户标识头。 + +## 配置 + +行 id:`octop-memory-native`。可在 profile 的 `cordis.patch.yml` 用同 id 覆盖(octop 的 DSH 安装器会自动写): + +```yaml +- id: octop-memory-native + config: + endpoint: https:///mcp/memory/ + token: omcp_xxxxxxxx + tokenHeader: Authorization + agentId: +``` + +`token` 留空时会回退读 `~/.dsh/hooks/config.json`(octop 安装器写的那个),方便从 hooks 方案平滑迁移。 + +主要开关:`recallEnabled` / `captureEnabled` / `recallMaxHits` / `recallMaxChars` / `recallTimeoutMs` / `recallMaxQueries` / `recallCorpus` / `recallDedupeTtlMs` / `captureMaxChars` / `captureTimeoutMs` / `sessionPrefix` / `source`。 + +## 安装 + +本目录就是插件源码(`src/octop/infra/agents/dsh_native_plugin/`),包名是 `octop-memory-native`。 + +```bash +# 1) 装成 profile 的 bundle(本地目录用 link:) +dsh plugin --profile desktop add link:/path/to/octop/src/octop/infra/agents/dsh_native_plugin + +# 2) 把 bundle 名加进该 profile package.json 的 dsh.profile.bundles,然后重启 DSH +``` + +octop 的 DSH 安装器会自动完成上面两步并写入 config。 + +## 验证 + +1. 新会话问一句命中历史记忆的话 → 本轮 request 里能看到 `[octop memory] …` 注入块(`source.kind = octop-memory-native`); +2. 轮末在 octop 记忆库(`memory_search` / `memory_raws`)能看到本轮 `session_id = dsh:` 的记录; +3. 插件日志(host 日志):`octop-memory-native: recall turn=… hits=… fresh=…`; +4. **「装了却没生效」先分清是哪一类**:加载探针 `/tmp/octop-memory-native.log` 在每次模块 import / `apply()` 时写一行 pid。 + - 日志里**没有**当前 DSH 进程的 pid → 插件没被 loader 导入(查 profile `dsh.profile.bundles`、`cordis.patch.yml` 里有没有 `disabled: true`、版本闸门); + - 有 pid 但 host 日志出现 `loaded but NOT configured` → 加载正常、配置缺失(重跑 octop 的 DSH 安装器,或补 profile patch 里的 config 块)。 + +## 回滚 + +从 profile 的 `dsh.profile.bundles` 移除 `octop-memory-native`(或 `dsh plugin --profile desktop remove octop-memory-native`)并重启即可;hooks 方案可随时切回。 diff --git a/src/octop/infra/agents/dsh_native_plugin/cordis.patch.yml b/src/octop/infra/agents/dsh_native_plugin/cordis.patch.yml new file mode 100644 index 0000000000..90a41b449f --- /dev/null +++ b/src/octop/infra/agents/dsh_native_plugin/cordis.patch.yml @@ -0,0 +1,30 @@ +# octop-memory-native bundle:在 profile 根部挂一个 Host 插件。 +# +# 为什么不塞进 agent preset:插件只吃 `agent/created` / `agent/pre-step` / +# `session/event` 三个事件接缝,profile 级注册对**所有 preset**生效,也就不需要 +# 再去 patch 那个手抄的「记忆共享模式」preset(少一处漂移源)。 +# +# token 不写进仓库:由 octop 的 DSH 安装器在 profile 的 cordis.patch.yml 里用同 id +# 覆盖 config,或直接设环境变量后留空走 fallback 文件。 +- insert: + - id: octop-memory-native + name: 'octop-memory-native' + config: + endpoint: '' + token: '' + tokenHeader: Authorization + agentId: '' + configFallbackPath: '~/.dsh/hooks/config.json' + recallEnabled: true + recallMaxHits: 4 + recallMaxChars: 1200 + recallTimeoutMs: 4000 + recallMaxQueries: 4 + recallCorpus: all + recallMinPromptChars: 4 + recallDedupeTtlMs: 300000 + captureEnabled: true + captureTimeoutMs: 15000 + captureMaxChars: 8000 + sessionPrefix: 'dsh:' + source: 'dsh:octop-memory-native' diff --git a/src/octop/infra/agents/dsh_native_plugin/icon.svg b/src/octop/infra/agents/dsh_native_plugin/icon.svg new file mode 100644 index 0000000000..ddb0ff5638 --- /dev/null +++ b/src/octop/infra/agents/dsh_native_plugin/icon.svg @@ -0,0 +1,7 @@ + + + + + + + diff --git a/src/octop/infra/agents/dsh_native_plugin/index.js b/src/octop/infra/agents/dsh_native_plugin/index.js new file mode 100644 index 0000000000..d545946d32 --- /dev/null +++ b/src/octop/infra/agents/dsh_native_plugin/index.js @@ -0,0 +1,374 @@ +/** + * octop-memory-native — DSH 原生「记忆共享」插件(替代 Claude Code hook 桥接)。 + * + * 两个接缝都在进程内,不拉起子进程、不依赖 hook 方言: + * · 召回:`agent/pre-step`(waterfall)在每轮第一个 step 前查一轮 octop memory, + * 有命中就作为一条带 source 的 user message 注入本轮(`createUserMessage`), + * 与 `time-context` 的做法一致。 + * · 上报:监听 `session/event` 的 `turn/end`(durable、post-commit),把该轮的 + * user/assistant 文本增量 POST 给 `memory_capture`;按 turn 去重,失败只告警。 + * + * 设计取舍见 README.md;所有网络失败都必须静默降级,绝不阻塞会话。 + */ + +import fs from 'node:fs' +import os from 'node:os' +import path from 'node:path' +import { randomUUID } from 'node:crypto' +import z from '@deepseek-ai/schemastery' +import { mergeHits, renderHits, stripHarnessContext, tokensFromPrompt } from './lib/text.js' + +/** + * 与 dsh-llm 的 createUserMessage 等价的最小实现:`{...input, role:'user', id}`。 + * + * 不 import `@deepseek-ai/dsh-llm`:该包不在 profile 的 node_modules 里,只能靠 host 的 + * 私有解析映射加载(插件若是 symlink/位于 bundles 目录就会 ERR_MODULE_NOT_FOUND)。 + * 留 `@deepseek-ai/schemastery` 一个依赖即可——它在 profile node_modules 里能正常解析。 + */ +function createUserMessage(input) { + return Object.freeze({ ...input, role: 'user', id: randomUUID() }) +} + +/** 加载探针:确认模块真的被 host 进程 import(排查插件 inactive 用)。 */ +function probe(line) { + try { + fs.appendFileSync('/tmp/octop-memory-native.log', `${new Date().toISOString()} ${line}\n`) + } catch { + // 探针失败不影响插件 + } +} + +probe(`module-load pid=${process.pid}`) + +/** Loader 诊断用的插件名。 */ +export const name = 'octop-memory-native' + +/** 无必需 Service:只用事件接缝,profile 里缺任何可选服务都不影响激活。 */ +export const inject = [] + +/** + * Config 保持扁平:schemastery 的嵌套对象在旧版 Loader 上有兼容风险, + * 而这些都是运维可调参数,扁平更好写 patch。 + */ +export const Config = z.object({ + // ── 连接 ──────────────────────────────────────────────────────────────── + endpoint: z.string().default(''), + token: z.string().default(''), + tokenHeader: z.string().default('Authorization'), + agentId: z.string().default(''), + /** token 为空时回退读这个文件(octop 安装器写的 hooks config.json)。 */ + configFallbackPath: z.string().default('~/.dsh/hooks/config.json'), + // ── 召回 ──────────────────────────────────────────────────────────────── + recallEnabled: z.boolean().default(true), + recallMaxHits: z.number().default(4), + recallMaxChars: z.number().default(1200), + recallTimeoutMs: z.number().default(4000), + /** 关键词扇出:每个 token 单独查一次,再合并去重(服务端是短语匹配,整句必 0 命中)。 */ + recallMaxQueries: z.number().default(4), + recallCorpus: z.string().default('all'), + recallMinPromptChars: z.number().default(4), + recallDedupeTtlMs: z.number().default(300000), + // ── 上报 ──────────────────────────────────────────────────────────────── + captureEnabled: z.boolean().default(true), + captureTimeoutMs: z.number().default(15000), + captureMaxChars: z.number().default(8000), + /** session_id 前缀,与旧 hook 保持一致,避免同会话在服务端变成两条记录。 */ + sessionPrefix: z.string().default('dsh:'), + source: z.string().default('dsh:octop-memory-native'), +}) + +const DAY_MS = 24 * 60 * 60 * 1000 + +export { mergeHits, renderHits, tokensFromPrompt } from './lib/text.js' + +/** 一条 message 里所有 text block 拼接。 */ +function textOf(content) { + if (!Array.isArray(content)) return '' + return content + .filter((block) => block && block.type === 'text' && typeof block.text === 'string') + .map((block) => block.text) + .join('\n') + .trim() +} + +/** 读 fallback 配置文件(hooks 安装器写的那种),失败返回空对象。 */ +function readFallbackConfig(filePath) { + try { + const expanded = filePath.startsWith('~') ? path.join(os.homedir(), filePath.slice(1)) : filePath + return JSON.parse(fs.readFileSync(expanded, 'utf8')) + } catch { + return {} + } +} + +/** 有效连接配置:显式 Config 优先,缺的字段回退 hooks config.json。 */ +export function resolveConnection(config) { + const fallback = readFallbackConfig(config.configFallbackPath || '') + const endpoint = (config.endpoint || fallback.url || '').trim() + const token = (config.token || fallback.token || '').trim() + const tokenHeader = (config.tokenHeader || fallback.tokenHeader || 'Authorization').trim() + const agentId = (config.agentId || fallback.agentId || '').trim() + return { endpoint, token, tokenHeader, agentId } +} + +function baseHeaders(connection) { + return { + 'X-Octop-Agent-Id': connection.agentId, + 'Content-Type': 'application/json', + Accept: 'application/json, text/event-stream', + } +} + +/** + * token 属于用户:``Authorization``(默认)写成 ``Bearer ``,其它头名原样写入。 + * 专家身份只由 ``X-Octop-Agent-Id`` 表达,不再额外发送任何用户标识头。 + */ +function authHeaders(connection) { + const headers = baseHeaders(connection) + if (!connection.token) return headers + const header = connection.tokenHeader || 'Authorization' + headers[header] = header.toLowerCase() === 'authorization' ? `Bearer ${connection.token}` : connection.token + return headers +} + +/** 一次 JSON-RPC 调用;永不抛错,返回 `{ ok, detail, error, status }`。 */ +export async function rpc(connection, method, params, timeoutMs, signal) { + const controller = new AbortController() + const timer = setTimeout(() => controller.abort(new Error('octop-memory-native: timeout')), Math.max(1000, timeoutMs)) + const signals = signal ? [controller.signal, signal] : [controller.signal] + try { + const response = await fetch(connection.endpoint, { + method: 'POST', + headers: authHeaders(connection), + body: JSON.stringify({ jsonrpc: '2.0', id: 1, method, params }), + signal: signals.length > 1 ? AbortSignal.any(signals) : signals[0], + }) + const text = await response.text() + let payload = text + for (const line of text.split('\n')) { + if (line.startsWith('data: ')) { + payload = line.slice(6) + break + } + } + let parsed = {} + try { + parsed = JSON.parse(payload.slice(payload.indexOf('{'), payload.lastIndexOf('}') + 1)) + } catch { + // 保留原始 body 供诊断 + } + return { + ok: response.ok, + status: response.status, + detail: parsed?.result?.structuredContent ?? parsed?.result ?? {}, + error: parsed?.error?.message ?? '', + } + } catch (error) { + return { ok: false, status: 0, detail: {}, error: String(error?.message || error) } + } finally { + clearTimeout(timer) + } +} + +/** 单次 memory_search,返回 hits 数组(失败返回空数组)。 */ +async function searchOnce(connection, config, query, signal) { + const result = await rpc( + connection, + 'tools/call', + { name: 'memory_search', arguments: { query, corpus: config.recallCorpus, max_results: config.recallMaxHits } }, + config.recallTimeoutMs, + signal, + ) + if (!result.ok) return [] + return Array.isArray(result.detail?.hits) ? result.detail.hits : [] +} + +/** 关键词扇出召回:多路单 token 查询 + 整句兜底,合并去重。 */ +export async function recall(connection, config, prompt, signal) { + const queries = tokensFromPrompt(prompt).slice(0, Math.max(1, config.recallMaxQueries)) + if (!queries.length) { + const fallback = String(prompt || '').trim().slice(0, 30) + if (!fallback) return [] + const hits = await searchOnce(connection, config, fallback, signal) + return mergeHits([hits], config.recallMaxHits) + } + const results = await Promise.all(queries.map((query) => searchOnce(connection, config, query, signal))) + return mergeHits(results, config.recallMaxHits) +} + +/** + * 从 session 事件里取出某一轮的对话文本(用于上报)。 + * user 侧跳过本插件注入的 context,assistant 侧只要 text block。 + */ +export function collectTurn(session, turn, injectedKind) { + const userParts = [] + const assistantParts = [] + for (let seq = session.seq - 1; seq >= 0; seq -= 1) { + let event + try { + event = session.eventAt(seq) + } catch { + continue + } + if (!event) continue + if (event.type === 'turn/start' && event.data?.turn === turn) break + if (event.type === 'user/message') { + if (event.data?.source?.kind === injectedKind) continue + const text = stripHarnessContext(textOf(event.data?.content)) + if (text) userParts.push(text) + } else if (event.type === 'assistant/message' && event.data?.turn === turn) { + const text = textOf(event.data?.message?.content) + if (text) assistantParts.push(text) + } + } + return { user: userParts.reverse(), assistant: assistantParts.reverse() } +} + +/** 上报一轮对话:memory_capture(text 模式,user/assistant 分字段)。 */ +async function captureTurn(connection, config, session, turn, logger) { + const { user, assistant } = collectTurn(session, turn, name) + if (!user.length && !assistant.length) return false + const content = user.join('\n\n').slice(0, config.captureMaxChars) + const args = { content, source: config.source, session_id: `${config.sessionPrefix}${session.id}` } + const assistantText = assistant.join('\n\n').slice(0, config.captureMaxChars) + if (assistantText) args.assistant = assistantText + const result = await rpc(connection, 'tools/call', { name: 'memory_capture', arguments: args }, config.captureTimeoutMs) + if (!result.ok) { + logger?.warn?.(`octop-memory-native: capture turn ${turn} failed http=${result.status} ${result.detail?.error || result.error || ''}`) + return false + } + return true +} + +/** 插件入口。 */ +export function apply(ctx, config) { + const state = new Map() // sessionId -> { lastTurn, lastPrompt, lastRecallAt, lastHits:Set } + const logger = ctx.logger + probe(`apply pid=${process.pid}`) + + const note = (line) => { + try { + logger?.info?.(`octop-memory-native: ${line}`) + } catch { + // 日志永远不能影响主流程 + } + } + + const connection = () => resolveConnection(config) + const configured = () => { + const conn = connection() + return Boolean(conn.endpoint && conn.agentId && conn.token) + } + + // ── 召回:每个 agent 的 pre-step ───────────────────────────────────────── + const registerAgent = (agent) => { + if (!agent?.ctx) return + agent.ctx.effect(() => { + const off = agent.ctx.on( + 'agent/pre-step', + async ({ agent: current, messages, turn, step, signal }, next) => { + const decision = await next() + try { + if (!config.recallEnabled || !decision || decision.kind !== 'enter') return decision + if (step !== 1 || signal?.aborted) return decision + const proposed = (messages || []).filter((message) => message?.source?.kind === 'user') + const prompt = proposed + .map((message) => stripHarnessContext(textOf(message.content))) + .filter(Boolean) + .join('\n') + .trim() + if (prompt.length < config.recallMinPromptChars) return decision + + const sessionId = current?.session?.id || '' + const entry = state.get(sessionId) || { lastPrompt: '', lastRecallAt: 0, lastHits: new Set() } + state.set(sessionId, entry) + if (entry.lastPrompt === prompt && Date.now() - entry.lastRecallAt < config.recallDedupeTtlMs) { + return decision + } + entry.lastPrompt = prompt + entry.lastRecallAt = Date.now() + if (!configured()) return decision + + const hits = await recall(connection(), config, prompt, signal) + if (!hits.length) return decision + const text = renderHits(hits, config.recallMaxChars) + if (!text) return decision + const fresh = hits.filter((hit) => !entry.lastHits.has(hit.path || hit.source_id)) + for (const hit of hits) entry.lastHits.add(hit.path || hit.source_id) + if (entry.lastHits.size > 64) entry.lastHits = new Set([...entry.lastHits].slice(-32)) + // 同一批命中重复出现(TTL 内已被注入过)就不再注入,避免噪声 + if (!fresh.length) return decision + note(`recall turn=${turn} hits=${hits.length} fresh=${fresh.length}`) + return { + ...decision, + messages: [ + ...decision.messages, + createUserMessage({ + content: [{ type: 'text', text }], + source: { kind: name, form: 'snapshot', sections: [{ name, text }] }, + }), + ], + } + } catch (error) { + logger?.warn?.(`octop-memory-native: recall failed ${String(error?.message || error)}`) + return decision + } + }, + { prepend: true }, + ) + return () => off?.() + }, 'octop-memory-native: recall listener') + } + + ctx.on('agent/created', ({ agent }) => { + try { + registerAgent(agent) + } catch (error) { + logger?.warn?.(`octop-memory-native: register agent failed ${String(error?.message || error)}`) + } + }) + + // HMR/热装时已存在的 agent 也要覆盖 + try { + for (const agent of ctx.get('agents')?.list?.() || []) registerAgent(agent) + } catch { + // 没有 agents 服务时忽略 + } + + // ── 上报:turn/end(durable、post-commit)──────────────────────────────── + ctx.on('session/event', (session, event) => { + try { + if (!config.captureEnabled || event?.type !== 'turn/end') return + const turn = event.data?.turn + if (typeof turn !== 'number' || !configured()) return + const sessionId = session?.id || '' + const entry = state.get(sessionId) || { lastTurn: 0, lastPrompt: '', lastRecallAt: 0, lastHits: new Set() } + state.set(sessionId, entry) + if (entry.lastTurn >= turn) return + entry.lastTurn = turn + void captureTurn(connection(), config, session, turn, logger).catch((error) => { + logger?.warn?.(`octop-memory-native: capture failed ${String(error?.message || error)}`) + }) + } catch (error) { + logger?.warn?.(`octop-memory-native: capture dispatch failed ${String(error?.message || error)}`) + } + }) + + // 清理会话级缓存,避免长跑进程里无限增长 + ctx.on('session/disposed', (session) => { + try { + state.delete(session?.id) + } catch { + // ignore + } + }) + + note(`loaded endpoint=${config.endpoint || '(fallback)'} recall=${config.recallEnabled} capture=${config.captureEnabled}`) + // 「装了但没配」和「根本没加载」看起来都是"记忆不生效",但排查方向完全不同:前者要重跑 + // octop 安装器 / 补 profile patch 的 config,后者要查 loader。这里明确喊一次。 + if (!configured()) { + logger?.warn?.( + 'octop-memory-native: loaded but NOT configured (endpoint / token / agentId missing) — recall and capture stay inactive. Run the octop memory setup installer or fill the `octop-memory-native` config block in the profile cordis.patch.yml.', + ) + } +} diff --git a/src/octop/infra/agents/dsh_native_plugin/lib/text.js b/src/octop/infra/agents/dsh_native_plugin/lib/text.js new file mode 100644 index 0000000000..d89fb5a07f --- /dev/null +++ b/src/octop/infra/agents/dsh_native_plugin/lib/text.js @@ -0,0 +1,113 @@ +/** + * 纯文本工具:召回关键词抽取、结果合并、注入正文渲染。 + * 这个文件不 import 任何 Harness 包,所以 `node --test` 能直接跑。 + */ + +/** 中文停用词:只用于召回关键词抽取,宁缺勿滥。 */ +const STOPWORDS = new Set([ + '请问', '帮我', '帮忙', '一下', '看看', '看下', '检查', '这个', '那个', '这些', '那些', + '我们', '你们', '他们', '什么', '怎么', '为什么', '哪个', '哪些', '是否', '可以', '能否', + '需要', '想要', '现在', '然后', '还有', '以及', '或者', '如果', '因为', '所以', '但是', + '就是', '这种', '那种', '一个', '一些', '已经', '进行', '相关', '问题', '情况', '内容', +]) + +/** 中文停用词切分:先按停用词切断再取片段,避免把「哪几个/涉及」这类词当关键词。 */ +const CJK_STOP_PATTERN = [...STOPWORDS] + .sort((a, b) => b.length - a.length) + .map((word) => word.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')) + .join('|') +const CJK_STOP_RE = new RegExp(CJK_STOP_PATTERN, 'g') + +/** 把中文长句切成候选实体词:去停用词、按非中文断开、保留 2-8 字片段。 */ +export function cjkChunks(text) { + return String(text || '') + .replace(CJK_STOP_RE, ' ') + .split(/[^\u4e00-\u9fa5]+/) + .map((chunk) => chunk.trim()) + .filter((chunk) => chunk.length >= 2 && chunk.length <= 8) +} + +/** 从一段文本里抽取召回关键词(ASCII 标识符/数字优先,其次中文实体片段)。 */ +export function tokensFromPrompt(text) { + const source = String(text || '') + const ascii = source.match(/[A-Za-z0-9_]+(?:[-./:][A-Za-z0-9_]+)*/g) || [] + const tokens = [] + const push = (value) => { + const token = String(value || '').trim() + if (token.length < 2 || STOPWORDS.has(token)) return + if (!tokens.includes(token)) tokens.push(token) + } + // ASCII 标识符(含 8503、rc.5、fh_stepwise_hotel_tokens、x7mm65)通常是最强信号 + for (const token of ascii) if (/[0-9]|[._/-]/.test(token)) push(token) + for (const token of cjkChunks(source)) push(token) + for (const token of ascii) push(token) + return tokens +} + +/** 合并多路查询结果:按 path 去重,命中次数多的优先,最后按配置截断。 */ +export function mergeHits(results, maxHits) { + const merged = new Map() + for (const hits of results) { + for (const hit of hits || []) { + const key = hit?.path || hit?.source_id + if (!key) continue + const existing = merged.get(key) + if (existing) existing.score += 1 + else merged.set(key, { hit, score: 1 }) + } + } + return [...merged.values()] + .sort((a, b) => b.score - a.score) + .slice(0, Math.max(1, maxHits)) + .map((entry) => entry.hit) +} + +/** 渲染注入正文:带层/时间/片段,末尾给 memory_get 的用法。 */ +export function renderHits(hits, maxChars) { + const lines = [ + '[octop memory] 与本轮相关的共享记忆(来自团队记忆库,按相关度排序):', + ] + let used = lines[0].length + for (const hit of hits) { + const when = String(hit.occurred_at || '').slice(0, 19).replace('T', ' ') + const layer = hit.layer || hit.role_hint || 'memory' + const snippet = String(hit.snippet || '').replace(/\s+/g, ' ').trim() + if (!snippet) continue + const line = `- [${layer}${when ? ` @ ${when}` : ''}] ${snippet}` + if (used + line.length > maxChars) break + lines.push(line) + used += line.length + 1 + } + if (lines.length === 1) return '' + lines.push( + `(需要原文/证据用 memory_get(path=…);与本次工作无关则忽略。)`, + ) + return lines.join('\n') +} + +export { STOPWORDS } + +/** Harness 自动注入的易变上下文:子会话 prompt、系统提示、运行时长文。上报/召回前剥掉。 */ +const VOLATILE_CUT_MARKERS = [ + '\nCurrent runtime context.', + '\n', + '\nYour parent agent id is', + '\nYou are a delegated subagent:', + '\nCurrent DeepSeek Harness implementation checkout:', +] + +/** + * 去掉 Harness 注入的运行时长文,只保留人/agent 真正写的内容。 + * @param {string} text 原始消息文本 + * @returns {string} 清洗后的文本(可能为空) + */ +export function stripHarnessContext(text) { + let out = String(text || '') + out = out.replace(/[\s\S]*?<\/system-reminder>/g, '') + let cut = out.length + for (const marker of VOLATILE_CUT_MARKERS) { + const at = out.indexOf(marker) + if (at >= 0 && at < cut) cut = at + } + return out.slice(0, cut).replace(/[ \t]+/g, ' ').replace(/\n{3,}/g, '\n\n').trim() +} diff --git a/src/octop/infra/agents/dsh_native_plugin/locale/en.json b/src/octop/infra/agents/dsh_native_plugin/locale/en.json new file mode 100644 index 0000000000..6018426d4d --- /dev/null +++ b/src/octop/infra/agents/dsh_native_plugin/locale/en.json @@ -0,0 +1,6 @@ +{ + "meta": { + "title": "Octop Shared Memory (native)", + "description": "Retrieves relevant octop memories before each turn and ships turn transcripts back at turn end, in-process (no Claude Code hook bridge)." + } +} diff --git a/src/octop/infra/agents/dsh_native_plugin/locale/zh.json b/src/octop/infra/agents/dsh_native_plugin/locale/zh.json new file mode 100644 index 0000000000..8d97489b92 --- /dev/null +++ b/src/octop/infra/agents/dsh_native_plugin/locale/zh.json @@ -0,0 +1,6 @@ +{ + "meta": { + "title": "Octop 共享记忆(原生插件)", + "description": "每轮开始前自动召回 octop 共享记忆注入上下文,轮末把对话增量上报,全程在 DSH 进程内完成,不再依赖 Claude Code hook 桥接。" + } +} diff --git a/src/octop/infra/agents/dsh_native_plugin/package.json b/src/octop/infra/agents/dsh_native_plugin/package.json new file mode 100644 index 0000000000..24f3e49d52 --- /dev/null +++ b/src/octop/infra/agents/dsh_native_plugin/package.json @@ -0,0 +1,26 @@ +{ + "name": "octop-memory-native", + "version": "0.1.2", + "private": true, + "description": "DSH 原生「记忆共享」插件:对话开始自动召回 octop 共享记忆,轮末自动上报,无需 Claude Code hook 桥接。", + "type": "module", + "main": "index.js", + "exports": { + ".": "./index.js", + "./locale/*.json": "./locale/*.json", + "./icon": "./icon.svg" + }, + "files": [ + "index.js", + "lib/text.js", + "cordis.patch.yml", + "locale/*.json", + "icon.svg", + "README.md" + ], + "dsh": { + "bundle": { + "patch": "./cordis.patch.yml" + } + } +} From f00ab5c030bbf2124a0b8b0d702eed533ef84b78 Mon Sep 17 00:00:00 2001 From: ArvinQi <12555402+ArvinQi@users.noreply.github.com> Date: Mon, 9 Nov 2026 22:40:37 +0800 Subject: [PATCH 05/39] =?UTF-8?q?feat(memory-mcp):=20=E6=9C=AC=E5=9C=B0?= =?UTF-8?q?=E7=BB=88=E7=AB=AF=E6=8E=A5=E5=85=A5=E2=80=94=E2=80=94=E5=AE=89?= =?UTF-8?q?=E8=A3=85=E5=99=A8=20+=20hooks=20+=20transcript=20=E5=AF=BC?= =?UTF-8?q?=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - `/mcp/memory/setup`、`/uninstall`:检测 Claude Code / Kiro / DSH / Codex / Cursor,写入 hooks(提问前召回、会话结束上报)与 MCP 条目;token 由用户在 运行时粘贴(用户级 omcp_ token),安装器不内置任何凭据 - 项目级安装(`?project=` / `OCTOP_PROJECT_DIR`):项目里只写注册,hook 脚本与 含 token 的 config.json 留在用户目录;Kiro 项目级 MCP 因含凭据而跳过 - Windows 原生 PowerShell 版(`?os=windows`):Read-Host/Read-Secret + .cmd 包装,不需要 Git Bash - DSH 走原生插件(见上一 commit),安装器负责写 bundle/profile 配置并清理旧的 hook 桥接与 preset - `memory_capture` 恢复终端 transcript 形态:本地文件或 gzip+base64 上传,按 终端格式解析成 L0(user/assistant/tool 事件),导入前做密钥脱敏;整份原文 归档到 `{workspace}/.octop/memory-sessions/` - 移除已无引用的 dsh-memory-share preset --- src/octop/infra/agents/claude_transcript.py | 1108 +++++++ src/octop/infra/agents/client_setup.py | 2715 +++++++++++++++++ .../hook_scripts/claude-code/stop-capture.mjs | 114 + .../claude-code/user-prompt-recall.mjs | 51 + .../hook_scripts/codex/stop-capture.mjs | 102 + .../hook_scripts/codex/user-prompt-recall.mjs | 55 + .../hook_scripts/cursor/stop-capture.mjs | 65 + .../cursor/user-prompt-recall.mjs | 56 + .../agents/hook_scripts/dsh/memory-target.mjs | 87 + .../agents/hook_scripts/dsh/stop-capture.mjs | 223 ++ .../hook_scripts/dsh/user-prompt-recall.mjs | 97 + .../agents/hook_scripts/kiro/stop-capture.mjs | 74 + .../hook_scripts/lib/octop-memory-client.mjs | 361 +++ src/octop/infra/agents/memory_mcp.py | 371 ++- .../infra/agents/memory_session_archive.py | 210 ++ tests/unit/agents/test_claude_transcript.py | 830 +++++ tests/unit/agents/test_client_setup.py | 1392 +++++++++ tests/unit/agents/test_hook_scripts.py | 211 ++ tests/unit/agents/test_memory_mcp.py | 121 + .../agents/test_memory_session_archive.py | 137 + 20 files changed, 8373 insertions(+), 7 deletions(-) create mode 100644 src/octop/infra/agents/claude_transcript.py create mode 100644 src/octop/infra/agents/client_setup.py create mode 100644 src/octop/infra/agents/hook_scripts/claude-code/stop-capture.mjs create mode 100644 src/octop/infra/agents/hook_scripts/claude-code/user-prompt-recall.mjs create mode 100644 src/octop/infra/agents/hook_scripts/codex/stop-capture.mjs create mode 100644 src/octop/infra/agents/hook_scripts/codex/user-prompt-recall.mjs create mode 100644 src/octop/infra/agents/hook_scripts/cursor/stop-capture.mjs create mode 100644 src/octop/infra/agents/hook_scripts/cursor/user-prompt-recall.mjs create mode 100644 src/octop/infra/agents/hook_scripts/dsh/memory-target.mjs create mode 100644 src/octop/infra/agents/hook_scripts/dsh/stop-capture.mjs create mode 100644 src/octop/infra/agents/hook_scripts/dsh/user-prompt-recall.mjs create mode 100644 src/octop/infra/agents/hook_scripts/kiro/stop-capture.mjs create mode 100644 src/octop/infra/agents/hook_scripts/lib/octop-memory-client.mjs create mode 100644 src/octop/infra/agents/memory_session_archive.py create mode 100644 tests/unit/agents/test_claude_transcript.py create mode 100644 tests/unit/agents/test_client_setup.py create mode 100644 tests/unit/agents/test_hook_scripts.py create mode 100644 tests/unit/agents/test_memory_session_archive.py diff --git a/src/octop/infra/agents/claude_transcript.py b/src/octop/infra/agents/claude_transcript.py new file mode 100644 index 0000000000..ba34f6fc2b --- /dev/null +++ b/src/octop/infra/agents/claude_transcript.py @@ -0,0 +1,1108 @@ +"""Replay terminal session transcripts (Claude Code, Kiro) into an agent's memory (L0). + +Claude Code already writes a complete session transcript to +``~/.claude/projects//.jsonl``: every user prompt, assistant +reply, tool call and tool result, each with its own timestamp. Re-capturing that +session through ``memory_capture`` cannot reproduce it — a real session has +hundreds of turns and dozens of tool calls — so this module replays the +transcript straight into L0 instead, where the normal extraction pipeline +(``extract`` → candidate → promote → atom) picks it up unchanged. + +Record mapping (record types that carry no conversation content — ``progress`` / +``queue-operation`` / ``last-prompt`` / ``system`` — are ignored): + +============================ ===================== ========================== +Claude Code record L0 ``event_type`` payload +============================ ===================== ========================== +user, ``content`` str/text ``user_message`` — +user, ``tool_result`` block ``tool_result`` tool_use_id, is_error +assistant, ``text`` block ``assistant_message`` — +assistant, ``tool_use`` block ``tool_call`` tool_name, input +assistant, ``thinking`` — skipped (scratchpad) +============================ ===================== ========================== + +Every event carries ``payload["src_key"] = ":"``, which +makes the import idempotent: keys already present in the target session are +skipped, so re-running a file (or a transcript that grew with new turns) only +appends what is missing. +""" + +from __future__ import annotations + +import hashlib +import json +import logging +import uuid +from collections.abc import Callable, Iterator +from dataclasses import dataclass, field, replace +from datetime import UTC, datetime +from pathlib import Path +from typing import TYPE_CHECKING, Any + +if TYPE_CHECKING: + from octop_memory.types import RawEventType + +logger = logging.getLogger(__name__) + +DEFAULT_HOST = "claude-code" +"""``RawEvent.host`` stamped on imported events (``memory_raws(host=…)`` filter).""" + +SESSION_PREFIX = "cc:" +"""L0 session id prefix; one Claude Code session maps onto one octop session.""" + +_MAX_CONTENT_CHARS = 4000 +"""Cap on a stored L0 body. + +A ``tool_result`` can be an entire file or build log, and the extractor prompt +receives every event's ``content`` in one call — so bodies have to stay bounded. +""" + +_MAX_PAYLOAD_CHARS = 4000 +"""Cap on a single serialized payload value (tool inputs are stored verbatim).""" + +_MESSAGE_EVENT_TYPES: frozenset[str] = frozenset({"user_message", "assistant_message"}) +"""The two event types a whole-message marker check may drop.""" + +_TOOL_EVENT_POLICIES: dict[str, frozenset[str]] = { + "all": frozenset(), + "calls": frozenset({"tool_result"}), + "none": frozenset({"tool_call", "tool_result"}), +} +"""``tool_events`` value → the tool event types to drop from L0. + +This is a *landing* policy, not an upload filter: the terminal always ships the whole +transcript (byte-delta, gzip+base64) and the parser reads every record — the policy +only decides which records become L0 rows. The product entry points (the +``memory_capture`` tool and ``octop memory import-claude``) default to ``none``: +L0 raw events feed the extractor and act as the recall fallback layer, and tool +traffic is 79% of a transcript's bytes (``tool_result`` alone 61%), so keeping it +dilutes what the session's memories look like. ``calls`` keeps what the agent did +and drops the output; ``all`` keeps the record whole for callers that want it. +""" + + +def _dropped_tool_types(tool_events: str) -> frozenset[str]: + try: + return _TOOL_EVENT_POLICIES[tool_events] + except KeyError: + raise ValueError( + f"unknown tool_events {tool_events!r}; expected one of {sorted(_TOOL_EVENT_POLICIES)}" + ) from None + + +_EXISTING_SCAN_LIMIT = 100_000 +"""How many existing L0 rows to scan for ``cc_key`` when deduplicating.""" + +_OWN_READ_TOOLS: frozenset[str] = frozenset( + { + "memory_recall", + "memory_search", + "memory_get", + "memory_raws", + "memory_candidates", + } +) +"""Our own memory *read* tools. + +Their tool results restate what is already stored, so importing them back into L0 +would close the "inject → capture → recall again" loop. They are dropped at parse +time — before the marker-based check — because a hook may format its own +injection block and lose the marker. + +``memory_recall`` is no longer exposed by the MCP server; it stays listed so +transcripts recorded by earlier installs still get filtered. +""" + + +def _is_own_read_tool(tool_name: str) -> bool: + """True for our read tools under any client naming. + + The MCP entry may be called ``octop-memory-local`` / ``-prod`` / ``-oauth`` or + anything the user renamed it to, so only the tool name decides: the bare + ``memory_search``, Claude Code's ``mcp____memory_search`` and Kiro's + ``mcp__memory_search``. + """ + tail = tool_name.rsplit("__", 1)[-1] + return tail in _OWN_READ_TOOLS or any(tool_name.endswith(name) for name in _OWN_READ_TOOLS) + + +@dataclass(frozen=True) +class TranscriptEvent: + """One L0 event mapped out of a Claude Code transcript record.""" + + key: str + event_type: RawEventType + content: str + timestamp: datetime + payload: dict[str, Any] + + +@dataclass(frozen=True) +class ParsedSession: + """Result of parsing one ``.jsonl`` transcript.""" + + session_id: str | None + """The Claude Code ``sessionId`` (``None`` when no record carried one).""" + + events: list[TranscriptEvent] + + skipped_own_results: int = 0 + """Tool results dropped because they came from our own memory read tools.""" + skipped_tools: int = 0 + """Tool events dropped by the caller's ``tool_events`` policy.""" + + +@dataclass +class ImportStats: + """Per-file import counters, reported back to the caller.""" + + session_id: str + parsed: int = 0 + imported: int = 0 + skipped_existing: int = 0 + skipped_echo: int = 0 + """Events dropped because their content carried a recall-injection marker.""" + skipped_own_results: int = 0 + """Tool results dropped because they came from our own memory read tools.""" + skipped_tools: int = 0 + """Tool events dropped by the caller's ``tool_events`` policy.""" + sidechain: int = 0 + dry_run: bool = False + warnings: list[str] = field(default_factory=list) + by_type: dict[str, int] = field(default_factory=dict) + """Parsed-event histogram (``event_type`` -> count), for caller verification.""" + + +# --------------------------------------------------------------------------- +# parsing (pure — no writes, no memory instance) +# --------------------------------------------------------------------------- + + +def _iter_records(text: str) -> Iterator[Any]: + for line in text.splitlines(): + line = line.strip() + if not line: + continue + try: + yield json.loads(line) + except json.JSONDecodeError: + logger.debug("claude_transcript: skipping malformed transcript line") + + +def _parse_timestamp(value: Any) -> datetime: + if isinstance(value, (int, float)) and value: + # DSH writes epoch milliseconds (``time``), older hosts seconds. + seconds = value / 1000 if value > 1e11 else float(value) + try: + return datetime.fromtimestamp(seconds, tz=UTC) + except (OverflowError, OSError, ValueError): + return datetime.now(UTC) + if isinstance(value, str) and value: + text = value[:-1] + "+00:00" if value.endswith("Z") else value + try: + parsed = datetime.fromisoformat(text) + except ValueError: + parsed = None + if parsed is not None: + return parsed if parsed.tzinfo else parsed.replace(tzinfo=UTC) + return datetime.now(UTC) + + +def _block_text(value: Any) -> str: + """Flatten a tool-result body (string, or a list of content blocks) to text.""" + if isinstance(value, str): + return value + if isinstance(value, list): + parts = [ + str(block.get("text") or "") + for block in value + if isinstance(block, dict) and block.get("type") == "text" + ] + return "\n".join(part for part in parts if part) + return "" + + +def _cap(text: str, limit: int = _MAX_CONTENT_CHARS) -> str: + body = (text or "").strip() + return body if len(body) <= limit else body[: limit - 1].rstrip() + "…" + + +def _bounded(value: Any) -> Any: + """Keep a payload value structured but bounded (payload is stored verbatim).""" + if isinstance(value, str): + return _cap(value, _MAX_PAYLOAD_CHARS) + try: + encoded = json.dumps(value, ensure_ascii=False, default=str) + except (TypeError, ValueError): + return str(value)[:_MAX_PAYLOAD_CHARS] + if len(encoded) <= _MAX_PAYLOAD_CHARS: + return value + return {"truncated_json": encoded[:_MAX_PAYLOAD_CHARS]} + + +def _blocks(content: Any) -> list[dict[str, Any]]: + """Normalize a message body to a list of content blocks.""" + if isinstance(content, str): + return [{"type": "text", "text": content}] + if isinstance(content, list): + return [block for block in content if isinstance(block, dict)] + return [] + + +def _map_block( + block: dict[str, Any], *, role: str +) -> tuple[RawEventType, str, dict[str, Any]] | None: + """Map one content block to ``(event_type, content, payload extras)``.""" + block_type = block.get("type") + if block_type == "text": + text = str(block.get("text") or "") + if not text.strip(): + return None + event_type: RawEventType = "user_message" if role == "user" else "assistant_message" + return event_type, _cap(text), {} + if block_type == "tool_use": + name = str(block.get("name") or "tool") + args = block.get("input") + summary = json.dumps(args, ensure_ascii=False, default=str) if args else "" + return ( + "tool_call", + _cap(f"{name} {summary}".strip()), + {"tool_use_id": block.get("id"), "tool_name": name, "input": _bounded(args)}, + ) + if block_type == "tool_result": + body = _cap(_block_text(block.get("content"))) + return ( + "tool_result", + body or "(empty tool result)", + {"tool_use_id": block.get("tool_use_id"), "is_error": bool(block.get("is_error"))}, + ) + # ``thinking`` (model scratchpad), images and unknown blocks are not L0 material. + return None + + +def parse_text(text: str) -> ParsedSession: + """Map transcript JSONL *content* to L0 events (pure, no writes). + + This is the entry point for callers that receive the transcript over the + wire (a cloud deployment cannot read the client's ``~/.claude`` directory). + """ + session_id: str | None = None + events: list[TranscriptEvent] = [] + tool_names: dict[str, str] = {} + skipped_own = 0 + for index, record in enumerate(_iter_records(text)): + if not isinstance(record, dict): + continue + record_type = record.get("type") + if record_type not in ("user", "assistant"): + continue + raw_session = record.get("sessionId") + if session_id is None and raw_session: + session_id = str(raw_session) + message = record.get("message") + if not isinstance(message, dict): + continue + role = str(message.get("role") or record_type) + timestamp = _parse_timestamp(record.get("timestamp")) + record_uuid = str(record.get("uuid") or f"line{index}") + sidechain = bool(record.get("isSidechain")) + for block_index, block in enumerate(_blocks(message.get("content"))): + if block.get("type") == "tool_use": + call_id = block.get("id") + if isinstance(call_id, str): + tool_names[call_id] = str(block.get("name") or "") + elif block.get("type") == "tool_result": + call_id = block.get("tool_use_id") + origin = tool_names.get(str(call_id) or "", "") + if origin and _is_own_read_tool(origin): + skipped_own += 1 + continue + mapped = _map_block(block, role=role) + if mapped is None: + continue + event_type, content, extra = mapped + key = f"{record_uuid}:{block_index}" + events.append( + TranscriptEvent( + key=key, + event_type=event_type, + content=content, + timestamp=timestamp, + payload={ + "source": DEFAULT_HOST, + "src_key": key, + "role": role, + "sidechain": sidechain, + **{name: value for name, value in extra.items() if value is not None}, + }, + ) + ) + return ParsedSession(session_id=session_id, events=events, skipped_own_results=skipped_own) + + +def parse_session(path: Path) -> ParsedSession: + """Map a transcript *file* to L0 events (same-host callers).""" + return parse_text(path.read_text(encoding="utf-8")) + + +# --------------------------------------------------------------------------- +# Kiro (``~/.kiro/sessions///messages.jsonl``) +# --------------------------------------------------------------------------- + +_KIRO_KINDS: dict[str, str] = { + "user": "user_message", + "assistant": "assistant_message", + "tool_call": "tool_call", + "tool_result": "tool_result", +} +"""Kiro ``payload.type`` → L0 ``event_type``. + +Everything else Kiro writes (``turn_start`` / ``turn_end`` / ``session_metadata`` / +``usage_summary`` / ``session_event`` / ``pending_interaction`` / +``interaction_resolved``) carries no conversation content. ``ContextualHookInvoked`` +is deliberately absent: that is our own hook invocation — it even carries the hook's +``output`` — i.e. exactly the plumbing that must not become memory. +""" + +_KIRO_SCRATCHPAD: frozenset[str] = frozenset({"Reasoning"}) +"""``assistant.operationType`` that is the model's scratchpad (Kiro's thinking).""" + + +def _map_kiro_payload(payload: dict[str, Any]) -> tuple[RawEventType, str, dict[str, Any]] | None: + kind = payload.get("type") + event_type = _KIRO_KINDS.get(kind) if isinstance(kind, str) else None + if event_type is None: + return None + if event_type in ("user_message", "assistant_message"): + if str(payload.get("operationType") or "") in _KIRO_SCRATCHPAD: + return None + text = str(payload.get("content") or "") + if not text.strip(): + return None + return event_type, _cap(text), {} + if event_type == "tool_call": + name = str(payload.get("toolName") or "tool") + args = payload.get("args") + summary = json.dumps(args, ensure_ascii=False, default=str) if args else "" + return ( + "tool_call", + _cap(f"{name} {summary}".strip()), + { + "tool_use_id": payload.get("toolCallId"), + "tool_name": name, + "input": _bounded(args), + }, + ) + body = _cap(str(payload.get("content") or "")) + return ( + "tool_result", + body or "(empty tool result)", + {"tool_use_id": payload.get("toolCallId"), "success": payload.get("success")}, + ) + + +def parse_kiro_text(text: str) -> ParsedSession: + """Map a Kiro ``messages.jsonl`` to L0 events (pure, no writes). + + The Kiro session id is the **directory** name, not a record field, so callers + pass it explicitly (the hook does: ``session_id="kiro:"``). + """ + events: list[TranscriptEvent] = [] + skipped_own = 0 + for index, record in enumerate(_iter_records(text)): + if not isinstance(record, dict): + continue + payload = record.get("payload") + if not isinstance(payload, dict): + continue + if payload.get("type") == "ContextualHookInvoked": + skipped_own += 1 + continue + mapped = _map_kiro_payload(payload) + if mapped is None: + continue + event_type, content, extra = mapped + record_id = str(record.get("id") or f"line{index}") + key = f"{record_id}:0" + events.append( + TranscriptEvent( + key=key, + event_type=event_type, + content=content, + timestamp=_parse_timestamp(record.get("timestamp")), + payload={ + "source": DEFAULT_HOST, + "src_key": key, + "role": "user" + if event_type in ("user_message", "tool_result") + else "assistant", + "sidechain": False, + **{name: value for name, value in extra.items() if value is not None}, + }, + ) + ) + return ParsedSession(session_id=None, events=events, skipped_own_results=skipped_own) + + +# --------------------------------------------------------------------------- +# DSH (``~/.dsh/sessions///session.jsonl.zstd``) +# --------------------------------------------------------------------------- + +_DSH_TOOL_BLOCK_TYPES: frozenset[str] = frozenset({"tool-call"}) +"""Block types that restate a call already emitted as its own ``tool/call`` record.""" + + +def _dsh_text_blocks(blocks: Any) -> str: + """Join the ``text`` blocks of a DSH content array (images / attachments dropped).""" + if not isinstance(blocks, list): + return "" + parts = [ + str(block.get("text") or "") + for block in blocks + if isinstance(block, dict) and block.get("type") == "text" + ] + return "\n".join(part for part in parts if part) + + +def _dsh_event( + key: str, + event_type: RawEventType, + content: str, + timestamp: datetime, + role: str, + extra: dict[str, Any] | None = None, +) -> TranscriptEvent: + return TranscriptEvent( + key=key, + event_type=event_type, + content=content, + timestamp=timestamp, + payload={ + "source": DEFAULT_HOST, + "src_key": key, + "role": role, + "sidechain": False, + **(extra or {}), + }, + ) + + +def parse_dsh_text(text: str) -> ParsedSession: + """Map a DSH ``session.jsonl`` to L0 events (pure, no writes). + + DSH streams its answer in ``assistant/chunk`` / ``reasoning-chunks`` / + ``text-chunks`` records and then writes the finished message — only the finished + records are mapped, otherwise every token would land in L0 twice. A call also + appears twice (as a ``tool/call`` record and as a ``tool-call`` block inside + ``assistant/message``), so the record wins and the block is skipped by call id. + """ + records = [record for record in _iter_records(text) if isinstance(record, dict)] + canonical_calls = { + str((record.get("data") or {}).get("callId") or "") + for record in records + if record.get("type") == "tool/call" + } + session_id: str | None = None + events: list[TranscriptEvent] = [] + skipped_own = 0 + for index, record in enumerate(records): + record_type = record.get("type") + raw_data = record.get("data") + data: dict[str, Any] = raw_data if isinstance(raw_data, dict) else {} + timestamp = _parse_timestamp(record.get("time")) + seq = record.get("seq", f"line{index}") + if record_type == "session": + session_id = session_id or str(record.get("id") or "") or None + elif record_type == "user/message": + raw_source = data.get("source") + kind = str(raw_source.get("kind") or "") if isinstance(raw_source, dict) else "" + if kind and kind != "user": + skipped_own += 1 + continue + body = _cap(_dsh_text_blocks(data.get("content"))) + if body.strip(): + events.append(_dsh_event(f"{seq}:0", "user_message", body, timestamp, "user")) + elif record_type == "assistant/message": + raw_message = data.get("message") + message: dict[str, Any] = raw_message if isinstance(raw_message, dict) else {} + for block_index, block in enumerate(message.get("content") or []): + if not isinstance(block, dict): + continue + block_type = block.get("type") + if block_type == "text": + body = _cap(str(block.get("text") or "")) + if body.strip(): + events.append( + _dsh_event( + f"{seq}:{block_index}", + "assistant_message", + body, + timestamp, + "assistant", + ) + ) + elif ( + block_type in _DSH_TOOL_BLOCK_TYPES + and str(block.get("id") or "") not in canonical_calls + ): + events.append(_dsh_tool_call(block, f"{seq}:{block_index}", timestamp)) + elif record_type == "tool/call": + events.append( + _dsh_tool_call( + { + "id": data.get("callId"), + "name": data.get("name"), + "arguments": data.get("arguments"), + }, + f"{seq}:0", + timestamp, + ) + ) + elif record_type == "tool/result": + raw_result = data.get("message") + result_message: dict[str, Any] = raw_result if isinstance(raw_result, dict) else {} + for block_index, block in enumerate(result_message.get("content") or []): + if not isinstance(block, dict) or block.get("type") != "tool-result": + continue + body = _cap(_dsh_text_blocks(block.get("content"))) + events.append( + _dsh_event( + f"{seq}:{block_index}", + "tool_result", + body or "(empty tool result)", + timestamp, + "user", + { + "tool_use_id": block.get("toolCallId"), + "is_error": block.get("isError"), + }, + ) + ) + # every other record type is streaming delta / turn-step / metadata noise + return ParsedSession(session_id=session_id, events=events, skipped_own_results=skipped_own) + + +def _dsh_tool_call(block: dict[str, Any], key: str, timestamp: datetime) -> TranscriptEvent: + name = str(block.get("name") or "tool") + raw_args = block.get("arguments") + parsed: Any = raw_args + if isinstance(raw_args, str) and raw_args.strip(): + try: + parsed = json.loads(raw_args) + except json.JSONDecodeError: + parsed = raw_args + summary = json.dumps(parsed, ensure_ascii=False, default=str) if parsed else "" + return _dsh_event( + key, + "tool_call", + _cap(f"{name} {summary}".strip()), + timestamp, + "assistant", + {"tool_use_id": block.get("id"), "tool_name": name, "input": _bounded(parsed)}, + ) + + +# --------------------------------------------------------------------------- +# Codex CLI (``~/.codex/sessions/YYYY/MM/DD/rollout-*.jsonl``) +# --------------------------------------------------------------------------- + +_CODEX_TOOL_ITEM_TYPES: frozenset[str] = frozenset( + {"function_call", "local_shell_call", "custom_tool_call", "web_search_call"} +) +_CODEX_TOOL_OUTPUT_TYPES: frozenset[str] = frozenset( + {"function_call_output", "local_shell_call_output", "custom_tool_call_output"} +) +_CODEX_TEXT_ITEM_TYPES: frozenset[str] = frozenset({"input_text", "output_text", "text"}) +"""Codex rollout records: ``event_msg`` carries the conversation, ``response_item`` +the wire-level items (developer instructions, tool calls, and a copy of the +messages). Both are folded here with a text-level dedupe so a turn is never stored +twice.""" + + +def _codex_item_text(content: Any) -> str: + if isinstance(content, str): + return content + if not isinstance(content, list): + return "" + parts: list[str] = [] + for block in content: + if isinstance(block, dict) and block.get("type") in _CODEX_TEXT_ITEM_TYPES: + text = block.get("text") + if isinstance(text, str) and text.strip(): + parts.append(text) + elif isinstance(block, str) and block.strip(): + parts.append(block) + return "\n".join(parts) + + +def parse_codex_text(text: str) -> ParsedSession: + """Map a Codex rollout JSONL to L0 events (pure, no writes).""" + events: list[TranscriptEvent] = [] + skipped_own = 0 + seen_messages: set[str] = set() + for index, record in enumerate(_iter_records(text)): + if not isinstance(record, dict): + continue + payload = record.get("payload") + if not isinstance(payload, dict): + continue + record_type = str(record.get("type") or "") + payload_type = str(payload.get("type") or "") + record_id = str(payload.get("id") or record.get("id") or f"line{index}") + key = f"{record_id}:0" + event_type = "" + content = "" + extra: dict[str, Any] = {} + + if record_type == "event_msg": + if payload_type == "user_message": + event_type, content = "user_message", str(payload.get("message") or "") + elif payload_type == "agent_message": + event_type, content = "assistant_message", str(payload.get("message") or "") + else: + continue # task_started / token_count / turn_aborted … plumbing + if not content.strip(): + continue + seen_messages.add(content.strip()) + elif record_type == "response_item": + if payload_type in _CODEX_TOOL_ITEM_TYPES: + event_type = "tool_call" + name = str(payload.get("name") or payload_type) + content = f"{name} {json.dumps(payload.get('arguments') or payload.get('action') or {}, ensure_ascii=False)}" + elif payload_type in _CODEX_TOOL_OUTPUT_TYPES: + event_type = "tool_result" + output = payload.get("output") + content = ( + output + if isinstance(output, str) + else json.dumps(output or "", ensure_ascii=False) + ) + elif payload_type == "message": + role = str(payload.get("role") or "") + if role not in ("user", "assistant"): + skipped_own += 1 # developer/system instructions are host plumbing + continue + body = _codex_item_text(payload.get("content")) + if not body.strip(): + continue + event_type = "user_message" if role == "user" else "assistant_message" + content = body + if content.strip() in seen_messages: + skipped_own += 1 # same turn already taken from event_msg + continue + seen_messages.add(content.strip()) + else: + continue # reasoning / other wire items + else: + continue # session_meta / turn_context / world_state + + events.append( + TranscriptEvent( + key=key, + event_type=event_type, + content=content, + timestamp=_parse_timestamp(record.get("timestamp")), + payload={ + "source": DEFAULT_HOST, + "src_key": key, + "role": "user" + if event_type in ("user_message", "tool_result") + else "assistant", + "sidechain": False, + **{name: value for name, value in extra.items() if value is not None}, + }, + ) + ) + return ParsedSession(session_id=None, events=events, skipped_own_results=skipped_own) + + +# --------------------------------------------------------------------------- +# Cursor (``~/.cursor/hooks.json`` -> ``transcript_path``) +# --------------------------------------------------------------------------- + +_CURSOR_ROLE_KEYS: tuple[str, ...] = ("role", "type", "author", "sender") +_CURSOR_TEXT_KEYS: tuple[str, ...] = ("text", "content", "message", "value") + + +def _cursor_role(record: dict[str, Any]) -> str: + for name in _CURSOR_ROLE_KEYS: + value = record.get(name) + if isinstance(value, str) and value.lower() in ( + "user", + "human", + "assistant", + "ai", + "agent", + "bot", + ): + return "user" if value.lower() in ("user", "human") else "assistant" + return "" + + +def _cursor_text(value: Any) -> str: + if isinstance(value, str): + return value + if isinstance(value, list): + return _codex_item_text(value) + if isinstance(value, dict): + for name in _CURSOR_TEXT_KEYS: + if name in value: + nested = _cursor_text(value[name]) + if nested.strip(): + return nested + return "" + + +def _cursor_stable_key(record: dict[str, Any], *, role: str, body: str) -> str: + """Idempotency key that stays stable across byte-delta uploads. + + Cursor agent-transcript lines usually have no ``id`` / ``uuid``. The old + fallback ``line{index}:0`` renumbered from 0 inside every delta the stop + hook ships, so the user turn at the head of each chunk reused ``line0:0`` + and was dropped as ``skipped_existing`` while later assistant lines + (higher indices) still imported. Prefer a real id when present; otherwise + hash ``role`` + body so re-sends dedupe but distinct turns do not collide. + """ + for name in ("id", "uuid"): + value = record.get(name) + if isinstance(value, str) and value.strip(): + return f"{value.strip()}:0" + if value is not None and not isinstance(value, (dict, list)): + text = str(value).strip() + if text: + return f"{text}:0" + digest = hashlib.sha256(f"{role}\0{body}".encode()).hexdigest()[:16] + return f"c{digest}:0" + + +def parse_cursor_text(text: str) -> ParsedSession: + """Map a Cursor transcript to L0 events (pure, no writes). + + The ``stop`` hook's ``transcript_path`` points at + ``~/.cursor/projects//agent-transcripts//.jsonl``. + Each line is ``{"role","message":{"content":[{"type":"text","text":…}]}}``; + tool blocks in that array are dropped. A JSON array or + ``{"messages":[…]}`` / ``{"turns":[…]}`` is still accepted. + + ``src_key`` is content-stable (see :func:`_cursor_stable_key`) so the hook's + byte-delta uploads stay idempotent without colliding across chunks. + """ + events: list[TranscriptEvent] = [] + skipped_own = 0 + records: list[Any] = [] + stripped = text.lstrip() + if stripped.startswith("["): + loaded = json.loads(text) + records = loaded if isinstance(loaded, list) else [] + elif stripped.startswith("{"): + try: + loaded = json.loads(text) + except json.JSONDecodeError: + loaded = None + if isinstance(loaded, dict): + for name in ("messages", "turns", "items", "conversation"): + if isinstance(loaded.get(name), list): + records = loaded[name] + break + else: + records = [loaded] + else: + records = list(_iter_records(text)) # JSONL fallback + else: + records = list(_iter_records(text)) + + for record in records: + if not isinstance(record, dict): + continue + role = _cursor_role(record) + if not role: + skipped_own += 1 # tool/system entries carry no conversational role + continue + body = "" + for name in _CURSOR_TEXT_KEYS: + if name in record: + body = _cursor_text(record[name]) + if body.strip(): + break + if not body.strip(): + continue + key = _cursor_stable_key(record, role=role, body=body) + events.append( + TranscriptEvent( + key=key, + event_type="user_message" if role == "user" else "assistant_message", + content=body, + timestamp=_parse_timestamp( + record.get("timestamp") or record.get("createdAt") or record.get("created_at") + ), + payload={ + "source": DEFAULT_HOST, + "src_key": key, + "role": role, + "sidechain": False, + }, + ) + ) + return ParsedSession(session_id=None, events=events, skipped_own_results=skipped_own) + + +_PARSERS: dict[str, Callable[[str], ParsedSession]] = { + "claude-code": parse_text, + "kiro": parse_kiro_text, + "dsh": parse_dsh_text, + "codex": parse_codex_text, + "cursor": parse_cursor_text, +} +"""``source`` → transcript parser. A terminal's name picks its parser.""" + +SESSION_PREFIXES: dict[str, str] = { + "claude-code": "cc:", + "kiro": "kiro:", + "dsh": "dsh:", + "codex": "codex:", + "cursor": "cursor:", +} +"""L0 session-id prefix per terminal, so one terminal's sessions never collide.""" + +# --------------------------------------------------------------------------- +# import (writes L0 through the target agent's memory store) +# --------------------------------------------------------------------------- + + +USER_MESSAGE = "user_message" +"""The user side of a transcript; used to tell injected context from a reply.""" + + +def attribute_user(content: str, caller: str | None) -> str: + """Prefix ``说:`` so the sender survives into the extracted atom. + + harness-memory serialises only ``event_id / event_type / content / timestamp / + session_id`` for the extractor prompt (``pipeline/extractor/__init__.py``), and + ``AtomCard`` has no user column — so the raw event's ``user`` field never reaches + the model. Writing the sender into the content is the only way "who said it" + survives extraction, stays FTS-searchable and shows up verbatim on recall. + Already-prefixed content is left alone, so a re-send cannot double it. + """ + name = (caller or "").strip() + if not name: + return content + prefix = f"{name}说:" + return content if content.startswith(prefix) else prefix + content + + +def _is_echo(content: str, event_type: str, markers: tuple[str, ...]) -> bool: + """This event is our own injected text coming back, not the user's words. + + Two shapes, because the two roles differ: + + * assistant side — only a block that *starts* with the marker is ours. A reply + that merely quotes the marker (explaining how the hooks work) is real content + and must survive; + * user side — the marker anywhere means it is the context we injected (Claude + Code also prefixes it, e.g. ``Stop hook feedback:``), so it is dropped. + """ + if any(content.lstrip().startswith(marker) for marker in markers): + return True + return event_type == USER_MESSAGE and any(marker in content for marker in markers) + + +def _persist( + memory: Any, + parsed: ParsedSession, + *, + fallback_session: str, + session_id: str | None, + user: str | None, + host: str, + dry_run: bool, + skip_markers: tuple[str, ...] = (), + skip_message_markers: tuple[str, ...] = (), + tool_events: str = "all", + session_prefix: str = SESSION_PREFIX, + redact: Callable[[str], str] | None = None, +) -> ImportStats: + """Deduplicate a parsed session against L0 and append only the missing events. + + Accounting: ``parsed = imported + skipped_existing + skipped_echo + skipped_tools`` + plus the ``skipped_own_results`` the parser already dropped. + """ + from octop_memory.types import RawEvent # noqa: PLC0415 + + resolved_session = session_id or f"{session_prefix}{parsed.session_id or fallback_session}" + stats = ImportStats( + session_id=resolved_session, + parsed=len(parsed.events), + dry_run=dry_run, + skipped_own_results=parsed.skipped_own_results, + ) + + candidates = parsed.events + if skip_markers or skip_message_markers: + kept = [ + event + for event in candidates + # 我们的注入块以标记**开头**;模型回答里只是引用该标记(例如解释接入方式) + # 时必须保留——否则会把正常对话当回声丢掉。 + if not _is_echo(event.content, event.event_type, skip_markers) + # Injected *messages* are plumbing; the same string inside tool output is + # just data (a file being read may quote it), so that check is narrower. + and not ( + event.event_type in _MESSAGE_EVENT_TYPES + and any(marker in event.content for marker in skip_message_markers) + ) + ] + stats.skipped_echo = len(candidates) - len(kept) + candidates = kept + + drop_types = _dropped_tool_types(tool_events) + if drop_types: + kept = [event for event in candidates if event.event_type not in drop_types] + stats.skipped_tools = len(candidates) - len(kept) + candidates = kept + + if redact is not None: + candidates = [replace(event, content=redact(event.content)) for event in candidates] + + stats.sidechain = sum(1 for event in candidates if event.payload.get("sidechain")) + for event in candidates: + stats.by_type[event.event_type] = stats.by_type.get(event.event_type, 0) + 1 + + seen: set[str] = set() + try: + for existing in memory.list_raw(session_id=resolved_session, limit=_EXISTING_SCAN_LIMIT): + # ``cc_key`` is the pre-Kiro name of the same field; old rows keep it. + key = (existing.payload or {}).get("src_key") or (existing.payload or {}).get("cc_key") + if isinstance(key, str): + seen.add(key) + except Exception: # noqa: BLE001 - an audit scan must not lose the import + logger.warning("claude_transcript: existing-event scan failed", exc_info=True) + stats.warnings.append("existing-event scan failed; duplicate check degraded") + + pending = [event for event in candidates if event.key not in seen] + stats.skipped_existing = len(candidates) - len(pending) + if dry_run or not pending: + return stats + + memory.add_raw_batch( + [ + RawEvent( + id=str(uuid.uuid4()), + host=host, + session_id=resolved_session, + thread_id=resolved_session, + user=user, + timestamp=event.timestamp, + event_type=event.event_type, + # Only the human side is attributed (``event_type`` already marks the + # other), and never a sidechain record — those "user" messages are a + # subagent's synthetic prompt, not something the caller said. + content=( + attribute_user(event.content, user) + if event.event_type == USER_MESSAGE and not event.payload.get("sidechain") + else event.content + ), + payload={**event.payload, "source": host}, + ) + for event in pending + ] + ) + stats.imported = len(pending) + return stats + + +def import_content( + memory: Any, + content: str, + *, + session_id: str | None = None, + user: str | None = None, + host: str = DEFAULT_HOST, + dry_run: bool = False, + skip_markers: tuple[str, ...] = (), + skip_message_markers: tuple[str, ...] = (), + tool_events: str = "all", + parser: str = "claude-code", + redact: Callable[[str], str] | None = None, +) -> ImportStats: + """Replay transcript *content* (JSONL text) into L0. + + This is the upload path: a cloud deployment cannot read the client's + ``~/.claude`` directory, so the terminal sends the file body instead. + ``skip_markers`` drops events whose content carries one of those strings + (e.g. this service's own recall-injection markers). + """ + if parser not in _PARSERS: + raise ValueError( + f"unknown transcript parser {parser!r}; expected one of {sorted(_PARSERS)}" + ) + return _persist( + memory, + _PARSERS[parser](content), + fallback_session="upload", + session_id=session_id, + user=user, + host=host, + dry_run=dry_run, + skip_markers=skip_markers, + skip_message_markers=skip_message_markers, + tool_events=tool_events, + session_prefix=SESSION_PREFIXES.get(parser, SESSION_PREFIX), + redact=redact, + ) + + +def import_session( + memory: Any, + path: Path, + *, + session_id: str | None = None, + user: str | None = None, + host: str = DEFAULT_HOST, + dry_run: bool = False, + skip_markers: tuple[str, ...] = (), + skip_message_markers: tuple[str, ...] = (), + tool_events: str = "all", + parser: str = "claude-code", + redact: Callable[[str], str] | None = None, +) -> ImportStats: + """Replay one transcript *file* into L0 (same-host callers). + + ``session_id`` defaults to ``cc:`` so one Claude Code + session maps onto one octop session (extraction groups by session). + """ + if parser not in _PARSERS: + raise ValueError( + f"unknown transcript parser {parser!r}; expected one of {sorted(_PARSERS)}" + ) + # Kiro names every file ``messages.jsonl``; the session id is its parent dir. + fallback = path.parent.name if path.name.startswith("messages") else path.stem + return _persist( + memory, + _PARSERS[parser](path.read_text(encoding="utf-8")), + fallback_session=fallback, + session_id=session_id, + user=user, + host=host, + dry_run=dry_run, + skip_markers=skip_markers, + skip_message_markers=skip_message_markers, + tool_events=tool_events, + session_prefix=SESSION_PREFIXES.get(parser, SESSION_PREFIX), + redact=redact, + ) + + +__all__ = [ + "DEFAULT_HOST", + "SESSION_PREFIX", + "ImportStats", + "ParsedSession", + "TranscriptEvent", + "SESSION_PREFIXES", + "import_content", + "import_session", + "parse_dsh_text", + "parse_kiro_text", + "parse_session", + "parse_text", +] diff --git a/src/octop/infra/agents/client_setup.py b/src/octop/infra/agents/client_setup.py new file mode 100644 index 0000000000..5f067a8336 --- /dev/null +++ b/src/octop/infra/agents/client_setup.py @@ -0,0 +1,2715 @@ +"""Bootstrap a local terminal so it talks to this memory service. + +``GET /mcp/memory/setup`` streams the interactive installer this module renders — no +authentication, because it only hands out a script: the credential is a **user-level MCP +token** (``omcp_…``, created in Octop under ``/api/memory-mcp/tokens``) typed by the +person running it, never fetched:: + + curl -fsSL "https:///mcp/memory/setup" | sh + # → 检测到:1) Claude Code 2) Kiro 3) DSH 4) Codex 5) Cursor + # → 要配置哪些? MCP token: ****(不回显) 专家 ID: XXXX + +Windows uses a native PowerShell installer (no Git Bash, no ``.cmd`` shim):: + + irm "https:///mcp/memory/setup?os=windows" | iex + +Both accept ``?project=`` to register into one project instead of the user profile. +It only *pre-fills* the interactive prompt; non-interactive runs read +``OCTOP_PROJECT_DIR`` (an explicitly empty value means "global"). + +``GET /mcp/memory/uninstall`` is the counterpart — removes hooks / MCP entries / the DSH +native plugin this installer wrote (does not delete server-side memory):: + + curl -fsSL "https:///mcp/memory/uninstall" | OCTOP_TERMINALS=cursor sh + +Nothing is assumed about where an installer runs. The script detects which terminals +exist, asks which to configure, prompts for the MCP token and the expert id (reading +``/dev/tty`` — a piped script must not consume its own stdin), then installs a copy of +the hook scripts next to *each* terminal, in the directory that terminal uses, and +registers them in that terminal's own hook config: + +========================= ========================================= ============================= +terminal hook scripts / plugin config +========================= ========================================= ============================= +Claude Code ``~/.claude/hooks-events/`` ``~/.claude/settings.json`` +Kiro ``~/.kiro/hooks/`` ``~/.kiro/hooks/*.json`` +DSH ``~/.dsh/bundles/octop-memory-native/`` ``~/.dsh/profiles//`` +Codex ``~/.codex/hooks/`` ``~/.codex/config.toml`` +Cursor ``~/.cursor/hooks/`` ``~/.cursor/hooks.json`` +========================= ========================================= ============================= + +DSH is wired through the **native plugin** ``octop-memory-native`` (recall/report run +in the DSH process), not through hook bridging: the installer copies the vendored +plugin, links it into the selected profile's ``node_modules``, registers the +``dependencies`` entry + ``dsh.profile.bundles`` name, and writes the +``cordis.patch.yml`` config block by row id (idempotent). Install *and* uninstall clean +up the legacy ``dsh-memory-share`` preset and the ``~/.dsh/hooks`` bridge so the two +wirings never trigger twice. + +Each hook copy carries its own ``config.json`` (endpoint, token, expert). The token is a +user-level MCP token sent as ``Authorization: Bearer `` plus ``X-Octop-Agent-Id``; +there is no user-id header — the token already identifies the person. The hook roles are +fixed: **UserPromptSubmit / beforeSubmitPrompt → memory_search** (pull background in) +and **Stop / stop → memory_capture** (ship the session transcript). +""" + +from __future__ import annotations + +import base64 +import json +from pathlib import Path + +_HOOKS_SRC = Path(__file__).with_name("hook_scripts") +# Every host gets the host-agnostic transport module next to its own entry scripts. +_LIB_FILES = ("lib/octop-memory-client.mjs",) + +HOST_FILES: dict[str, tuple[str, ...]] = { + "claude-code": ("claude-code/stop-capture.mjs", "claude-code/user-prompt-recall.mjs"), + "kiro": ("kiro/stop-capture.mjs",), + "codex": ("codex/stop-capture.mjs", "codex/user-prompt-recall.mjs"), + "cursor": ("cursor/stop-capture.mjs", "cursor/user-prompt-recall.mjs"), +} +"""Which hook scripts a terminal gets. Each host has its own entry script — no +runtime host detection — so changing one host cannot affect another; the only +shared file is the host-agnostic transport module. + +DSH is deliberately absent: it goes through the native plugin below, so it needs no +hook scripts and no hook config.""" + +_DSH_NATIVE_SRC = Path(__file__).with_name("dsh_native_plugin") +_DSH_NATIVE_FILES: tuple[str, ...] = ( + "package.json", + "index.js", + "lib/text.js", + "cordis.patch.yml", + "locale/zh.json", + "locale/en.json", + "icon.svg", + "README.md", +) +"""DSH 原生记忆插件 ``octop-memory-native`` 的源码副本(vendored)。 + +来自插件仓 v0.1.2,逐字节复制、不在这里改动:插件的召回/上报逻辑与 DSH 版本无关,跟着插件仓 +升级即可。安装器只做「抄进 ``~/.dsh/bundles/octop-memory-native`` + 登记 profile + 写配置块」。 + +升级方式:``cp`` 插件仓的对应文件到这个目录(``tests/`` / ``.git`` 不要带),同步本清单与 +``package.json`` 的版本号,再跑 ``tests/unit/agents/test_client_setup.py``。 + +为什么不装 git URL:``/mcp/memory/setup`` 是 curl | sh 的无鉴权脚本,装 git 依赖要求目标机器 +能访问 git 仓(内网/代理/凭据各不相同);vendored 副本跟着 octop 一起发版,安装只需要目录 +(``link:``),离线也能装完整。代价是插件升级要发一次 octop 版本。""" + +_DSH_NATIVE_NAME = "octop-memory-native" +"""插件目录名、package.json 包名、cordis.patch.yml 行 id、dsh.profile.bundles 名(同一个)。""" + +TERMINALS: tuple[str, ...] = ("claude-code", "kiro", "dsh", "codex", "cursor") +"""Terminals this installer knows how to configure.""" + +# Owned by each terminal, so no cross-terminal directory is invented. +# ``dsh`` 这一项只用于「拆掉旧 hooks 桥接」与卸载,原生插件模式不再往里写脚本。 +HOOKS_DIRS: dict[str, str] = { + "claude-code": "~/.claude/hooks-events", + "kiro": "~/.kiro/hooks", + "dsh": "~/.dsh/hooks", + "codex": "~/.codex/hooks", + "cursor": "~/.cursor/hooks", +} + +_SELECT_PY = r""" +import sys + +# 交互式终端清单:↑/↓ 移动 · 空格 选择 · a 全选 · 回车 确认。 +# 键位与绘制都走 /dev/tty,stdout 只用来回传结果(shell 捕获)。 +# 任何一步不可用(无 tty / 无 termios / 空选择)就返回非 0,让 shell 回退到数字输入。 + +def main() -> int: + items = [item.split("|", 1) for item in sys.argv[1:] if "|" in item] + if not items: + return 3 + try: + import termios + import tty as ttymod + + tty_in = open("/dev/tty", "rb", buffering=0) + tty_out = open("/dev/tty", "w") + fd = tty_in.fileno() + saved = termios.tcgetattr(fd) + except Exception: + return 3 + + chosen = [False] * len(items) + cursor = 0 + + def render(first: bool = False) -> None: + if not first: + tty_out.write("\033[%dA" % (len(items) + 1)) + for index, (_, label) in enumerate(items): + box = "◉" if chosen[index] else "◯" + pointer = "❯" if index == cursor else " " + tty_out.write("\033[2K%s %s %s\r\n" % (pointer, box, label)) + hint = " ↑/↓ 移动 · 空格 选择 · a 全选 · 回车 确认" + if not any(chosen): + hint = " ↑/↓ 移动 · 空格 选择 · a 全选 · 回车 确认(还没选)" + tty_out.write("\033[2K%s\r\n" % hint) + tty_out.flush() + + def read_key() -> str: + key = tty_in.read(1) + if key == b"\x1b": + rest = tty_in.read(2) + return {b"[A": "up", b"[B": "down"}.get(rest, "other") + if key in (b"\r", b"\n"): + return "enter" + if key == b" ": + return "space" + if key in (b"a", b"A"): + return "all" + if key == b"\x03": + return "abort" + if key in (b"k", b"j"): + return "up" if key == b"k" else "down" + return "other" + + try: + ttymod.setraw(fd) + tty_out.write("要配置哪些终端?(可多选)\r\n") + render(first=True) + while True: + key = read_key() + if key == "up": + cursor = (cursor - 1) % len(items) + elif key == "down": + cursor = (cursor + 1) % len(items) + elif key == "space": + chosen[cursor] = not chosen[cursor] + elif key == "all": + turn_on = not all(chosen) + chosen = [turn_on] * len(items) + elif key == "enter": + if not any(chosen): + continue # 一个都没选时不确认,提示留在屏幕上 + break + elif key == "abort": + return 130 + render() + except Exception: + return 3 + finally: + try: + termios.tcsetattr(fd, termios.TCSADRAIN, saved) + except Exception: + pass + tty_out.write("\r\n") + + print(",".join(value for (value, _), on in zip(items, chosen) if on)) + return 0 + + +sys.exit(main()) +""".strip() + +_MERGE_PY = ''' +import base64 +import json +import os +import pathlib +import shlex +import shutil +import subprocess + + +# Windows 的安装器把整份配置写进一个临时 JSON(命令行/环境块长度有限,manifest 很大), +# 这里把它补进 os.environ(真实环境变量优先),之后照旧按环境变量读。 +_ENV_FILE = os.environ.get("OCTOP_ENV_FILE", "") +if _ENV_FILE: + with open(_ENV_FILE, encoding="utf-8") as _env_handle: + for _key, _value in json.load(_env_handle).items(): + os.environ.setdefault(str(_key), str(_value)) + + +# hook 包装脚本按平台生成:POSIX 用 ``.sh``(sh 执行),Windows 用 ``.cmd``(系统 shell +# 执行)。安装器用 OCTOP_HOOK_WRAPPER 明确指定(PS 安装器=cmd,sh 安装器=sh);单独跑本 +# 脚本时按平台猜。 +HOOK_WRAPPER = os.environ.get("OCTOP_HOOK_WRAPPER", "").strip().lower() +if HOOK_WRAPPER not in ("sh", "cmd"): + HOOK_WRAPPER = "cmd" if os.name == "nt" else "sh" + + +SEARCH_PROMPT = ( + "回答前,先用 memory_search 检索与本轮相关的记忆(query 用用户本次输入,corpus=atom," + "max_results=5),把命中内容当作背景;与本次工作无关就忽略,不要重复检索同一个查询。" +) +"""Kiro 的召回提示词——Kiro 不会把 runCommand 的 stdout 注进上下文,只能让模型自己调工具。 + +上报**没有**对应的提示词:让模型"总结这段会话"只会把复述写进 L0,原始会话要由 hook 脚本 +(整段 ``messages.jsonl``)提交,见 ``do_kiro``。 +""" + + +def _load(path): + try: + return json.loads(path.read_text(encoding="utf-8")) + except FileNotFoundError: + return {} + except Exception as exc: # noqa: BLE001 - a broken config must not break setup + print(f" ! {path} 不是合法 JSON({exc});跳过该文件") + return None + + +def _dump(path, data): + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text(json.dumps(data, ensure_ascii=False, indent=2) + "\\n", encoding="utf-8") + + +def _strings(value): + """递归取出 JSON 结构里的所有字符串(判断「这条是不是我们写的」用)。""" + if isinstance(value, str): + yield value + elif isinstance(value, dict): + for item in value.values(): + yield from _strings(item) + elif isinstance(value, list): + for item in value: + yield from _strings(item) + + +def _mentions_dir(value, directory): + """JSON / TOML 文本里是否引用了这个目录(统一成正斜杠再比,Windows 反斜杠也认)。""" + needle = str(directory).replace("\\\\", "/") + return any(needle in text.replace("\\\\", "/") for text in _strings(value)) + + +def _install_hooks(target, host): + """把共享传输模块 + 该宿主的入口脚本写进这个终端自己的 hooks 目录。 + + 脚本与 ``config.json`` 始终留在用户目录(项目级安装也一样):项目里只写「注册」, + 凭据不会出现在仓库里。``config.json`` 的形状就是连接配置本身 + (``url`` / ``token`` / ``tokenHeader`` / ``agentId``),DSH 原生插件也用它当兜底。 + """ + target.mkdir(parents=True, exist_ok=True) + for rel, blob in HOOK_FILES[host].items(): + path = target / rel + path.parent.mkdir(parents=True, exist_ok=True) + path.write_bytes(base64.b64decode(blob)) + config = {"url": HOOK_URL, "token": TOKEN, "tokenHeader": TOKEN_HEADER, "agentId": AGENT} + _dump(target / "config.json", config) + print(f" ✓ hook 脚本({host} 专用,含共享传输模块)+ config.json → {target}") + scratch = "%TEMP%" if os.name == "nt" else "$TMPDIR" + print( + f" · 日志/增量状态:{target}/octop-hook.log、{target}/octop-transcript-state.json" + f"(该目录不可写时退到 {scratch}/octop-hooks 并打印告警)" + ) + + +def _entry(transport=None): + # 用户级 MCP token:``Authorization: Bearer `` + ``X-Octop-Agent-Id``。 + # 「谁问的」由 token 自己表达,不再有第二个身份头。 + headers = { + "X-Octop-Agent-Id": AGENT, + "Authorization": f"Bearer {TOKEN}", + } + entry = {"type": transport or "http", "url": ENTRY_URL, "headers": headers} + return entry + + +def _octop_servers(servers): + """配置里已有的 octop memory 服务名——``local`` / ``prod`` / ``test`` 这类后缀都算。""" + return [name for name in servers if str(name).replace("_", "-").startswith("octop-memory")] + + +def _merge_mcp(path, transport=None): + """登记 MCP 服务:端点一致就复用已有条目,已有别的 octop 条目就不重复添加。 + + 服务名由客户端的配置说了算(同一个 ``octop-memory`` 可以带不同环境后缀、也可以被改名), + 这里只按端点判断;召回/上报 hook 调的地址来自 ``config.json``,与名字无关。 + """ + entry = _entry(transport) + cfg = _load(path) + if cfg is None: + return + servers = cfg.setdefault("mcpServers", {}) + ours = _octop_servers(servers) + reused = next( + (name for name in ours if (servers.get(name) or {}).get("url") == entry["url"]), "" + ) + if reused: + print(f" ✓ MCP 服务 {reused}(已存在且端点一致,复用)→ {path}") + return + if ours: + print(f" · {path} 已有 octop 服务:{', '.join(ours)}(端点与本次 {URL} 不同,保持原样)") + print(" · 模型侧用哪个服务由你决定;召回/上报 hook 用 config.json 里的地址") + return + servers[NAME] = entry + _dump(path, cfg) + print(f" ✓ MCP 服务 {NAME} → {path}") + + +def _claude_hooks(hooks_dir): + """Claude Code 的两条 command hook。 + + - ``UserPromptSubmit`` → 召回脚本:**该事件不支持 ``mcp_tool``**(实测 2.1.263 静默跳过), + 所以用 command hook 跑脚本,脚本直接调 MCP ``memory_search``,stdout 被注入上下文。 + - ``Stop`` → 上报脚本:``Stop`` 触发时本轮还没落盘,脚本会**后台脱离**、等几秒再读 + transcript(见脚本头注释),hook 本身立即返回,不给每轮加延迟。 + ``SessionEnd`` 不能用:那里没有 MCP 客户端上下文(``mcp_tool hooks are not available + for the 'SessionEnd' hook event``)。 + """ + search = {"type": "command", "command": hook_command(f"{hooks_dir}/claude-code/user-prompt-recall.mjs")} + stop = {"type": "command", "command": hook_command(f"{hooks_dir}/claude-code/stop-capture.mjs")} + # **简写形态**:事件直接挂 handler 数组,不套 matcher 组那层。我们的 hook 对所有调用都生效, + # 本来就不需要 matcher;而某些终端的 SDK 侧不保证 ``hooks`` 数组按顺序执行, + # 少一层嵌套更稳(老配置里的嵌套形态仍会被识别并替换,见 _handlers_of)。 + return {"UserPromptSubmit": [search], "Stop": [stop]} + + +_OUR_SCRIPTS = ( + "user-prompt-recall.mjs", + "stop-capture.mjs", + # 包装脚本(配置里引用的是 .sh / Windows 的 .cmd) + "user-prompt-recall.sh", + "stop-capture.sh", + "user-prompt-recall.cmd", + "stop-capture.cmd", + # 早期版本的文件名,重装时一并替换掉 + "octop-user-prompt-recall.mjs", + "octop-stop-capture.mjs", +) + +_OUR_MCP_TOOLS = ("memory_search", "memory_capture") +"""旧版 mcp_tool hook 用的工具名——按工具认,不按 MCP 服务名认。""" + + +def _is_our_handler(handler): + """认我们自己留下的 hook:command 形态按脚本名,旧版 mcp_tool 按工具名。 + + 同一个 ``octop-memory`` 条目可以带不同环境后缀、也可能被用户改名,所以不能拿服务名 + 当判据;工具名才是这段接入的契约。 + """ + handler = handler or {} + if handler.get("type") == "mcp_tool": + return handler.get("tool") in _OUR_MCP_TOOLS + command = handler.get("command") or "" + return any(script in command for script in _OUR_SCRIPTS) + + +def _handlers_of(entry): + """一个条目里的 handler 列表:可能是 matcher 组(``{"hooks": [...]}``)或简写形态的单条。 + + 两种都认 —— 简写是我们自己写的(某些终端的 SDK 侧不保证 ``hooks`` 数组按顺序执行,见 + ``_claude_hooks``),嵌套形态是别人或早期版本写的。 + """ + entry = entry or {} + nested = entry.get("hooks") + if isinstance(nested, list): + return nested + return [entry] + + +def _is_our_group(entry): + """这个 hook 条目是不是我们写的(任一 handler 命中就算)。""" + return any(_is_our_handler(handler) for handler in _handlers_of(entry)) + + +def _upsert_groups(settings, event, groups): + """Drop earlier entries that are ours (either形态), keep every other hook, add ours.""" + hooks = settings.setdefault("hooks", {}) + kept = [entry for entry in (hooks.get(event) or []) if not _is_our_group(entry)] + hooks[event] = kept + list(groups) + + +def do_claude_code(): + # 两条 hook 都是 command 脚本(召回 / 上报),任何模式都要装脚本 + config.json + _install_hooks(CC_DIR, "claude-code") + if IN_PROJECT: + # MCP 走 Claude 的 local scope(~/.claude.json 里按项目路径存,不进仓库); + # hooks 写 settings.local.json(本机私有,不提交),避免没装 octop 的同事 + # 每次提交都被一条指向不存在脚本的 hook 打扰。 + _merge_mcp_local(PROJECT_DIR) + settings = _project(".claude", "settings.local.json") + print(" ✓ Claude Code MCP 走 local scope(本机私有、按项目);hooks → settings.local.json") + print(" · settings.local.json 需要先信任该工作区,Claude Code 才会读取") + _warn_global_also_installed("claude-code") + else: + _merge_mcp(home / ".claude.json") + settings = home / ".claude" / "settings.json" + st = _load(settings) + if st is not None: + for event, groups in _claude_hooks(CC_DIR).items(): + _upsert_groups(st, event, groups) + # 早期版本挂过 SessionEnd(mcp_tool 在那里不可用,会持续报错):清掉我们留下的那条 + _upsert_groups(st, "SessionEnd", []) + if not st["hooks"].get("SessionEnd"): + st["hooks"].pop("SessionEnd", None) + _dump(settings, st) + print(" ✓ Claude Code hooks(UserPromptSubmit → 召回脚本;Stop → 上报脚本,后台延迟等落盘)") + print(f" · → {settings}") + + +def do_kiro(): + """Kiro 的 hook 只有 runCommand / askAgent 两种动作,没有 mcp_tool。 + + 因此: + + * 召回(UserPromptSubmit)用**提示词**(``agent``):Kiro 不会把 runCommand 的 stdout + 注入上下文,只能让模型经 Kiro 的 MCP 客户端调 ``memory_search``; + * 上报(Stop)**始终用 command hook** 跑脚本,把整段 + ``~/.kiro/sessions///messages.jsonl`` 提交进 L0 —— Kiro 侧 + 最有价值的就是**原始的用户/助手消息**,绝不能让模型自己总结。 + + 项目级安装跳过 Kiro 的 MCP 条目(``.kiro/settings/mcp.json`` 自带凭据、会随 git 提交); + hooks(只有命令、没有凭据)照常写进项目。 + """ + if IN_PROJECT: + hooks_dir = _project(".kiro", "hooks") + print(" · Kiro 的 MCP 条目(.kiro/settings/mcp.json)含凭据、会随 git 提交:项目模式跳过") + print(" · 需要 Kiro 的 MCP 就用全局安装(不带 project 的 /mcp/memory/setup)") + _warn_global_also_installed("kiro") + else: + _merge_mcp(home / ".kiro" / "settings" / "mcp.json", transport="streamable_http") + hooks_dir = KIRO_DIR + _install_hooks(KIRO_DIR, "kiro") + + recall = hooks_dir / "memory-recall-on-start.json" + _dump( + recall, + { + "version": "v1", + "hooks": [ + { + "name": "octop memory (UserPromptSubmit)", + "trigger": "UserPromptSubmit", + "enabled": True, + "timeout": 30, + "action": {"type": "agent", "prompt": SEARCH_PROMPT}, + } + ], + }, + ) + print(f" ✓ Kiro 召回 hook(agent 提示词)已写入 {recall}") + + save = hooks_dir / "memory-save-on-stop.json" + action = {"type": "command", "command": hook_command(f"{KIRO_DIR}/kiro/stop-capture.mjs")} + _dump( + save, + { + "version": "v1", + "hooks": [ + { + "name": "octop memory (Stop)", + "trigger": "Stop", + "enabled": True, + "timeout": 120, + "action": action, + } + ], + }, + ) + print(f" ✓ Kiro 上报 hook(command 脚本提交整段 messages.jsonl)已写入 {save}") + + +_CODEX_HOOKS = (("UserPromptSubmit", "user-prompt-recall"), ("Stop", "stop-capture")) +_CURSOR_HOOKS = (("beforeSubmitPrompt", "user-prompt-recall"), ("stop", "stop-capture")) + + +def _toml_string(value): + """TOML 字面串:hook 命令里有双引号、没有单引号,用单引号串就不必转义。""" + return "'" + value + "'" + + +def _toml_blocks(text): + """按表头行列把 TOML 切成块,保留原始文本(首块是文件头/杂项)。""" + blocks = [[]] + for line in text.splitlines(keepends=True): + if line.lstrip().startswith("[") and not line.lstrip().startswith("#"): + blocks.append([]) + blocks[-1].append(line) + return blocks + + +def _codex_upsert(path): + """把两条 hook 挂进 ``~/.codex/config.toml``(幂等)。 + + 结构是 ``[[hooks.]]`` 容器 + ``[[hooks..hooks]]`` 处理项,先删掉我们自己 + 写过的处理项(连它那个空容器),再把新处理项插到该事件最后一组之后 —— 不动已有 hook 的 + 先后位置,因为 codex 的 hook 信任状态按 ``:::`` 记索引。 + """ + text = path.read_text(encoding="utf-8") if path.exists() else "" + blocks, kept = _toml_blocks(text), [] + for block in blocks: + body = "".join(block) + header = block[0].strip() if block else "" + ours = _mentions_dir(body, CODEX_DIR) + if ours and header.startswith("[[hooks.") and header.endswith(".hooks]]"): + event = header[len("[[hooks.") : -len(".hooks]]")] + # 我们自己写的空容器也一起清掉(重装不堆空表) + if kept and kept[-1][0].strip() == f"[[hooks.{event}]]" and not _codex_container_body( + kept[-1] + ): + kept.pop() + continue + kept.append(block) + + additions = {} + for event, script in _CODEX_HOOKS: + handler = ( + f"[[hooks.{event}.hooks]]\\n" + f"command = {_toml_string(hook_command(f'{CODEX_DIR}/codex/{script}.mjs'))}\\n" + f'type = "command"\\n' + ) + insert_at = None + for index, block in enumerate(kept): + header = block[0].strip() if block else "" + if header in (f"[[hooks.{event}]]", f"[[hooks.{event}.hooks]]"): + insert_at = index + 1 + if insert_at is None: + additions[event] = f"[[hooks.{event}]]\\n\\n{handler}" + else: + kept.insert(insert_at, [handler, "\\n"]) + + body = "".join("".join(block) for block in kept).rstrip("\\n") + tail = "\\n\\n".join(additions[event] for event, _ in _CODEX_HOOKS if event in additions) + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text((body + "\\n\\n" if body else "") + tail + "\\n", encoding="utf-8") + + +def _codex_container_body(block): + """容器块是否只有表头(没有处理项)。""" + return not "".join(block[1:]).strip() + + +def _cursor_upsert(path): + """把两条 hook 挂进 ``~/.cursor/hooks.json``(幂等,其它事件与别人的 hook 一律保留)。""" + cfg = _load(path) + if cfg is None: + return + cfg.setdefault("version", 1) + hooks = cfg.setdefault("hooks", {}) + for event, script in _CURSOR_HOOKS: + entries = [ + entry for entry in hooks.get(event) or [] if not _mentions_dir(entry, CURSOR_DIR) + ] + entries.append({"command": hook_command(f"{CURSOR_DIR}/cursor/{script}.mjs")}) + hooks[event] = entries + _dump(path, cfg) + + +def do_codex(): + """Codex:hook 写在 ``~/.codex/config.toml``(``[[hooks.]]`` + ``type="command"``)。 + + codex 对 hook 有信任机制:新写入的 hook 需要用户信任一次才会执行,所以这里只写配置、 + 不伪造 ``hooks.state`` 里的 ``trusted_hash``。项目级写进仓库的 ``.codex/config.toml``, + 该仓库被 Codex 信任后才会加载(未信任只加载 skills)。 + """ + _install_hooks(CODEX_DIR, "codex") + config = _project(".codex", "config.toml") if IN_PROJECT else home / ".codex" / "config.toml" + _codex_upsert(config) + print(f" ✓ Codex hooks(UserPromptSubmit → 召回;Stop → 上报)→ {config}") + print(" · 首次需在 codex 里信任该 hook(否则不执行);自动化可加 --dangerously-bypass-hook-trust") + print(" · Codex 的 MCP 服务由它自己的配置管理,这里不动") + if IN_PROJECT: + print(" · 项目级:仓库需被 Codex 信任后才加载这条配置(未信任只加载 skills)") + _warn_global_also_installed("codex") + + +def do_cursor(): + """Cursor:hook 写在 ``~/.cursor/hooks.json``(``{"version":1,"hooks":{:[{command}]}}``)。""" + _install_hooks(CURSOR_DIR, "cursor") + hooks = _project(".cursor", "hooks.json") if IN_PROJECT else home / ".cursor" / "hooks.json" + _cursor_upsert(hooks) + print(f" ✓ Cursor hooks(beforeSubmitPrompt → 召回;stop → 上报)→ {hooks}") + print(" · Cursor 的 MCP 服务由它自己的配置管理,这里不动") + if IN_PROJECT: + print(" · 项目级 hooks 需要 Cursor 版本支持 .cursor/hooks.json;没生效就改用全局安装") + _warn_global_also_installed("cursor") + + +def _dsh_profile(): + """装哪个 profile:OCTOP_DSH_PROFILE,否则按 web / desktop / default 取第一个存在的。""" + profiles = home / ".dsh" / "profiles" + wanted = os.environ.get("OCTOP_DSH_PROFILE", "") + for name in ([wanted] if wanted else ["web", "desktop", "default"]): + if name and (profiles / name / "package.json").exists(): + return name, profiles / name + return "", pathlib.Path() + + +def _run_dsh_install(bundle, profile, profile_dir): + """把插件装进 profile:优先 dsh CLI,其次直接 pnpm(源码 checkout 常没有 dsh 命令)。 + + 两条路都在 profile 目录里跑包管理器——DSH 的 CLI 也只是把参数转给 profile 里的 pnpm。 + """ + if shutil.which("dsh"): + command = ["dsh", "plugin", "--profile", profile, "add", str(bundle)] + elif shutil.which("pnpm"): + command = ["pnpm", "add", str(bundle)] + else: + print(" · 没找到 dsh / pnpm 命令,无法自动装依赖") + return None + done = subprocess.run(command, cwd=str(profile_dir), capture_output=True, text=True) + if done.returncode != 0: + tail = (done.stderr or done.stdout or "").strip().splitlines() + print(f" ! 装依赖失败({command[0]} 退出码 {done.returncode})" + (f":{tail[-1]}" if tail else "")) + return False + return True + + +def _dsh_bundle_dir(): + """插件目录:~/.dsh/bundles/。""" + return DSH_DIR.parent / "bundles" / NATIVE_NAME + + +def _dsh_native_config(): + """原生插件的连接配置:与客户端 MCP 条目同一套语义(MCP 地址 + 用户级 MCP token)。 + + ``tokenHeader`` 固定 ``Authorization``(值写成 ``Bearer ``);``agentId`` 是专家 ID, + 由插件写成 ``X-Octop-Agent-Id``。身份全由 token 表达,没有额外的标识头。 + """ + return { + "endpoint": ENTRY_URL, + "token": TOKEN, + "tokenHeader": TOKEN_HEADER, + "agentId": AGENT, + } + + +def _dsh_native_config_hint(config): + if not config["token"]: + print(" ! 插件没有凭据(token 为空):召回/上报会 401;重跑并填 MCP token") + elif not config["endpoint"]: + print(" ! 插件没有 endpoint:重跑并确认安装地址") + + +def _yaml_scalar(value): + """YAML 单引号标量:值里的 ``'`` 写成 ``''``(token / URL 都能安全承载)。""" + return "'" + str(value).replace("'", "''") + "'" + + +def _dsh_native_patch_block(config): + """profile ``cordis.patch.yml`` 里的配置块:**只覆盖 config,不再 insert**。 + + 插件自己的 bundle patch 已经 insert 了同 id 的行;这里再 insert 一次会让 Loader 组合出 + 重复 id(真实 profile 上有先例:duplicate ids 直接崩)。所以只写 id + config —— Loader + 按 id 找到已有行、整块替换 config,同 id 已存在时不会重复追加。 + """ + lines = [ + "# >>> octop-memory-native (octop 安装器写入,整块覆盖) >>>", + "# 召回/上报在 DSH 进程内完成,不依赖 ~/.dsh/hooks 的 hook 桥接。", + f"# 回滚:删掉本块,并从该 profile package.json 的 dsh.profile.bundles 移除 {NATIVE_NAME}。", + f"- id: {NATIVE_NAME}", + " config:", + ] + for key in ("endpoint", "token", "tokenHeader", "agentId"): + lines.append(f" {key}: {_yaml_scalar(config.get(key, ''))}") + lines.append(f"# <<< {NATIVE_NAME} <<<") + return "\\n".join(lines) + "\\n" + + +def _is_patch_entry(line, row_id): + """这一行是不是顶层 ``- id: ``(缩进的 insert 行不算:我们只写顶层块)。""" + if not line.startswith("-"): + return False + rest = line[1:].strip() + if not rest.startswith("id:"): + return False + value = rest[len("id:") :].strip() + if " #" in value: + value = value.split(" #", 1)[0] + return value.strip().strip("'").strip('"') == row_id + + +def _patch_entry_bounds(lines, row_id): + """顶层 ``- id: `` 条目的 ``[start, end)``,含紧贴其上的注释与结尾 ``# <<< id <<<``。 + + 注释只在「整段提到本插件」或「紧贴且提到本插件」时算进块里——别人的注释块(通常写在 + 自己条目上方)原样保留。结尾标记兼容早期安装器写的 ``# <<< octop-memory-native <<<``。 + """ + entry = None + for index, line in enumerate(lines): + if _is_patch_entry(line, row_id): + entry = index + break + if entry is None: + return None + start = entry + comments = [] + cursor = entry - 1 + while cursor >= 0: + above = lines[cursor] + if above.strip() == "" or above[:1] in (" ", "\\t"): + break + if not above.lstrip().startswith("#"): + break + comments.insert(0, cursor) + cursor -= 1 + if comments: + joined = "".join(lines[index] for index in comments) + if row_id not in joined: + while comments and row_id not in lines[comments[0]]: + comments.pop(0) + if comments: + start = comments[0] + end = entry + 1 + while end < len(lines) and lines[end][:1] in (" ", "\\t"): + end += 1 # 条目本体:缩进的 config 行 + probe = end + while probe < len(lines) and lines[probe].strip() == "": + probe += 1 + if ( + probe < len(lines) + and lines[probe].strip().startswith("#") + and "<<<" in lines[probe] + and row_id in lines[probe] + ): + end = probe + 1 # 结尾标记(旧版在标记前留了空行,一起清掉) + while end < len(lines) and lines[end].strip() == "": + end += 1 + return start, end + + +def _replace_native_patch(text, block, row_id=None): + """替换/追加配置块;其余内容(别人的条目、注释)逐字保留。""" + row_id = row_id or NATIVE_NAME + lines = text.splitlines(keepends=True) + bounds = _patch_entry_bounds(lines, row_id) + if bounds is None: + base = text.rstrip("\\n") + return (base + "\\n\\n" if base else "") + block + start, end = bounds + tail = "".join(lines[end:]) + return "".join(lines[:start]) + block + ("\\n" if tail else "") + tail + + +def _write_dsh_native_patch(profile_dir, config): + """把配置块写进 profile 的 cordis.patch.yml:同 id 已存在则整块替换(写前备份)。""" + patch = profile_dir / "cordis.patch.yml" + existing = patch.read_text(encoding="utf-8") if patch.exists() else "" + updated = _replace_native_patch(existing, _dsh_native_patch_block(config)) + if updated == existing: + print(f" ✓ DSH profile 的 cordis.patch.yml 已是最新配置块(id: {NATIVE_NAME})→ {patch}") + return + if patch.exists(): + backup = patch.with_name(patch.name + ".octop.bak") + backup.write_text(existing, encoding="utf-8") + print(f" · 原 cordis.patch.yml 已备份 → {backup}") + patch.parent.mkdir(parents=True, exist_ok=True) + patch.write_text(updated, encoding="utf-8") + print(f" ✓ 已写入 DSH profile 的 cordis.patch.yml(id: {NATIVE_NAME},其余内容保留)→ {patch}") + + +def _dsh_native_spec_is_ours(spec, bundle, profile_dir): + """profile 里登记的这个依赖是不是就指向我们的插件目录(link: / file: 两种写法都认)。""" + text = str(spec or "") + if text.startswith("link:"): + target = text[len("link:") :] + elif text.startswith("file:"): + target = text[len("file:") :] + else: + return False + if not os.path.isabs(target): + target = os.path.join(str(profile_dir), target) + try: + return os.path.realpath(target) == os.path.realpath(str(bundle)) + except OSError: + return False + + +def _register_dsh_native(profile_dir, bundle, profile): + """登记 profile:dependencies + dsh.profile.bundles(幂等;已指向本插件就不动)。""" + package = profile_dir / "package.json" + config = _load(package) + if not isinstance(config, dict): + return False + changed = [] + dependencies = config.get("dependencies") + if not isinstance(dependencies, dict): + dependencies = {} + config["dependencies"] = dependencies + if not _dsh_native_spec_is_ours(dependencies.get(NATIVE_NAME), bundle, profile_dir): + dependencies[NATIVE_NAME] = "link:" + str(bundle) + changed.append("dependencies") + dsh = config.get("dsh") + if not isinstance(dsh, dict): + dsh = {} + config["dsh"] = dsh + profile_config = dsh.get("profile") + if not isinstance(profile_config, dict): + profile_config = {} + dsh["profile"] = profile_config + bundles = profile_config.get("bundles") + if not isinstance(bundles, list): + bundles = [] + profile_config["bundles"] = bundles + if NATIVE_NAME not in bundles: + bundles.append(NATIVE_NAME) + changed.append("dsh.profile.bundles") + if not changed: + print(f" ✓ profile「{profile}」已登记 {NATIVE_NAME}(不重复写)") + return True + _dump(package, config) + print(f" ✓ 已登记 profile「{profile}」的 {' + '.join(changed)} → {package}") + return True + + +def _link_dsh_native_module(profile_dir, bundle): + """把插件放进 profile 的 node_modules:包管理器没装成 / 装的是旧副本时自己补。 + + POSIX 用目录软链(与 pnpm 对本地目录的 ``link:`` 语义一致);Windows 建软链需要开发者 + 模式或管理员权限,失败就整目录拷贝 —— Node 只按目录解析,两种都能用。 + """ + modules = profile_dir / "node_modules" + target = modules / NATIVE_NAME + if target.exists() and os.path.realpath(str(target)) == os.path.realpath(str(bundle)): + print(f" ✓ profile 的 node_modules 已指向插件目录 → {target}") + return "ok" + modules.mkdir(parents=True, exist_ok=True) + if target.is_symlink(): + target.unlink() + elif target.exists(): + print(f" · profile 里已有 {NATIVE_NAME} 目录(不是本次的路径):替换成指向插件目录的链接") + shutil.rmtree(target) + try: + os.symlink(str(bundle), str(target), target_is_directory=True) + print(f" ✓ profile 的 node_modules 已链接到插件目录 → {target}") + return "symlink" + except (OSError, NotImplementedError, AttributeError): + pass + try: + shutil.copytree(bundle, target) + print(f" ✓ profile 的 node_modules 已复制插件(无软链权限时的等价做法)→ {target}") + return "copy" + except OSError as exc: + print(f" ! 无法把插件放进 {target}({exc}):DSH 会加载不到插件") + return "" + + +def _install_dsh_native_files(): + """把 vendored 插件写到 ~/.dsh/bundles/(逐字节还原,不在这里改插件代码)。""" + if not NATIVE_FILES: + print(" ! 安装器里没有插件源码(dsh_native_plugin 缺失):跳过 DSH") + return None + bundle = _dsh_bundle_dir() + for name, blob in NATIVE_FILES.items(): + target = bundle / name + target.parent.mkdir(parents=True, exist_ok=True) + target.write_bytes(base64.b64decode(blob)) + print(f" ✓ DSH 原生插件 {NATIVE_NAME} → {bundle}({len(NATIVE_FILES)} 个文件)") + return bundle + + +def _dsh_profile_dirs(): + """所有已有 package.json 的 DSH profile(web / desktop / default / 自定义)。""" + profiles = home / ".dsh" / "profiles" + if not profiles.is_dir(): + return [] + return sorted(path for path in profiles.iterdir() if (path / "package.json").exists()) + + +def _drop_dsh_bundle_name(profile_dir, names): + """从某个 profile 的 dsh.profile.bundles 去掉指定名字(返回实际去掉的)。""" + package = profile_dir / "package.json" + config = _load(package) + if not isinstance(config, dict): + return [] + dsh = config.get("dsh") + profile_config = dsh.get("profile") if isinstance(dsh, dict) else None + bundles = profile_config.get("bundles") if isinstance(profile_config, dict) else None + if not isinstance(bundles, list): + return [] + gone = [name for name in names if name in bundles] + if not gone: + return [] + profile_config["bundles"] = [name for name in bundles if name not in names] + _dump(package, config) + return gone + + +def _remove_dsh_legacy_hooks(): + """从 hooks 方案升级过来:拆掉旧桥接与 preset —— 两套接入同时生效会双触发。""" + removed = [] + bridge = DSH_DIR / "octop-hooks.json" + if bridge.exists(): + bridge.unlink() + removed.append(str(bridge)) + scripts = DSH_DIR / "dsh" # 入口脚本目录;config.json 留着(插件拿它当兜底配置) + if scripts.exists(): + shutil.rmtree(scripts) + removed.append(str(scripts)) + preset = DSH_DIR.parent / "bundles" / "dsh-memory-share" + if preset.exists(): + shutil.rmtree(preset) + removed.append(str(preset)) + for profile_dir in _dsh_profile_dirs(): + for name in _drop_dsh_bundle_name( + profile_dir, ("dsh-memory-share", "octop-dsh-memory-share") + ): + removed.append(f"{profile_dir.name}:{name}") + if not removed: + print(" · 没有旧的 hooks 桥接 / preset bundle(干净安装)") + return + print(" ✓ 已移除旧的 hooks 桥接 / preset bundle(避免与原生插件双触发):") + for item in removed: + print(f" · {item}") + + +def _dsh_mcp_hint(): + """MCP 条目仍由 DSH 自己管理:插件不登记它,这里只把参数打出来方便手动加。""" + print(" · DSH 的 MCP 服务请在它的 MCP 面板/CLI 里添加(该条目由 DSH 自己管理):") + print(f" url: {URL}") + print(f" headers: {json.dumps(_entry()['headers'], ensure_ascii=False)}") + + +def _install_dsh_native(): + """原生插件安装:写插件目录 → 拆旧桥接 → 登记 profile → 写 cordis.patch.yml 配置块。 + + 安装后 DSH 直接可用:插件行由插件自带的 bundle patch(dsh.bundle.patch)insert,profile + 的 cordis.patch.yml 只按同 id 覆盖 config(endpoint / token / agentId)。 + """ + bundle = _install_dsh_native_files() + if bundle is None: + return + _remove_dsh_legacy_hooks() + config = _dsh_native_config() + profile, profile_dir = _dsh_profile() + if not profile: + print(" ! 没找到 DSH profile(~/.dsh/profiles/),跳过自动登记") + print(" · 手动两步:") + print(f" 1) 在 profile 目录执行:pnpm add {bundle}") + print(f" 2) 把 {NATIVE_NAME} 加进该目录 package.json 的 dsh.profile.bundles,重启 DSH") + _dsh_native_config_hint(config) + _dsh_mcp_hint() + return + if _run_dsh_install(bundle, profile, profile_dir) is None: + print(" · 没有 dsh / pnpm 命令:直接补 node_modules 与 package.json 登记") + _link_dsh_native_module(profile_dir, bundle) + _register_dsh_native(profile_dir, bundle, profile) + _write_dsh_native_patch(profile_dir, config) + print( + f" ✓ DSH 原生插件已装进 profile「{profile}」:重启 DSH 后生效" + "(召回/上报在进程内,不再依赖 hook)" + ) + _dsh_native_config_hint(config) + _dsh_mcp_hint() + + +def do_dsh(): + """DSH 记忆接入:装原生插件(进程内召回/上报),不装 hook 桥接、不装 preset。 + + DSH 没有项目级配置:hooks 的 ``configPath`` 是进程级、MCP 在 profile / 全局 patch 里, + 所以项目模式直接跳过(不写项目里的任何文件),且不算失败。 + """ + if IN_PROJECT: + print(" · DSH 没有项目级配置(hooks 是进程级、MCP 在 profile 里):项目模式跳过 DSH") + return + _install_dsh_native() + + +home = pathlib.Path(os.path.expanduser("~")) +URL = os.environ["OCTOP_URL"] +HOOK_URL = os.environ.get("OCTOP_HOOK_URL") or URL # hooks 实际 POST 的地址(直连) +TOKEN = os.environ["OCTOP_TOKEN"] +# 用户级 MCP token 只有一个方言:``Authorization: Bearer ``(身份由 token 表达)。 +TOKEN_HEADER = "Authorization" +ENTRY_URL = URL +NAME = os.environ["OCTOP_SERVER_NAME"] +NODE = os.environ.get("OCTOP_NODE") or "node" # 安装时解析出的绝对路径(非 login shell 也能跑) + +# ---- 安装范围:全局(默认)或只装到某个项目 ---- +# 项目级只在项目里写「注册」(hooks 命令 / MCP 条目);hook 脚本与 config.json(含 token) +# 始终留在用户目录,因此项目里不会出现脚本或凭据。 +PROJECT = (os.environ.get("OCTOP_PROJECT_DIR") or "").strip() +PROJECT_DIR = pathlib.Path(PROJECT).expanduser() if PROJECT else None +IN_PROJECT = PROJECT_DIR is not None +PROJECT = str(PROJECT_DIR) if IN_PROJECT else "" + + +def _project(*parts): + """项目级安装里某个文件/目录的绝对路径(全局模式不会被调用)。""" + return PROJECT_DIR.joinpath(*parts) + + +def _global_hooks_present(host): + """全局配置里是否已经注册过我们的 hook —— 项目+全局都装会双触发。""" + if host == "claude-code": + settings = _load(home / ".claude" / "settings.json") or {} + return any( + _is_our_group(entry) + for entries in (settings.get("hooks") or {}).values() + for entry in (entries or []) + ) + if host == "kiro": + return (home / ".kiro" / "hooks" / "memory-recall-on-start.json").exists() + if host == "codex": + path = home / ".codex" / "config.toml" + return path.exists() and _mentions_dir(path.read_text(encoding="utf-8"), CODEX_DIR) + if host == "cursor": + cfg = _load(home / ".cursor" / "hooks.json") + return cfg is not None and _mentions_dir(cfg, CURSOR_DIR) + return False + + +def _warn_global_also_installed(host): + if not IN_PROJECT or not _global_hooks_present(host): + return + print(f" ! 全局也装过 {host} 的 octop hooks:两处都注册会双触发") + print(" · 只要项目级就先跑全局卸载(不带 project 的 /mcp/memory/uninstall)") + + +def _merge_mcp_local(project): + """Claude Code 的 local scope:``~/.claude.json`` 里按项目路径存 MCP 条目(不进仓库)。""" + key = os.path.realpath(str(project)) + path = home / ".claude.json" + cfg = _load(path) + if cfg is None: + return + projects = cfg.get("projects") + if not isinstance(projects, dict): + projects = cfg["projects"] = {} + bucket = projects.get(key) + if not isinstance(bucket, dict): + bucket = projects[key] = {} + servers = bucket.get("mcpServers") + if not isinstance(servers, dict): + servers = bucket["mcpServers"] = {} + entry = _entry() + ours = _octop_servers(servers) + reused = next( + (name for name in ours if (servers.get(name) or {}).get("url") == entry["url"]), "" + ) + if reused: + print(f" ✓ MCP 服务 {reused}(local scope 已有且端点一致,复用)→ {path}") + return + if ours: + print(f" · 项目 {key} 已有别的 octop 服务:{', '.join(ours)}(端点不同,保持原样)") + return + servers[NAME] = entry + _dump(path, cfg) + print(f" ✓ MCP 服务 {NAME}(Claude Code local scope,项目 {key})→ {path}") + print(" · 本机私有、按项目的条目:不进仓库,也不需要 .mcp.json 审批") + + +def hook_command(script_path): + """一条 command hook 的 ``command``:**独立包装脚本**的路径(写它、返回它)。 + + 不把 node 绝对路径 / ``NODE_OPTIONS`` / 失败兜底那一长串直接塞进 JSON-TOML 字符串 —— + 里面的引号与 ``$`` 很容易把配置写坏,也难读(用户反馈过这一点)。包装脚本负责: + + * 用安装时解析出的 node **绝对路径**跑 ``.mjs``(hook 由终端派生,非 login shell 里 + 没有 nvm 的 node,裸 ``node`` 会找不到); + * ``OCTOP_USE_SYSTEM_CA=1`` 时导出 ``NODE_OPTIONS=--use-system-ca``(Node 22.15+): + Node 自带的证书库不认企业根(macOS 钥匙串 / Windows 证书库里有、Node 没有), + 不加这行 hook 里的 fetch 会失败;已有 ``NODE_OPTIONS`` 时追加,不覆盖; + * 失败时把原因打到 stderr —— command hook 的非 0 退出是**静默**的。 + """ + mjs = pathlib.Path(str(script_path)) + if HOOK_WRAPPER == "cmd": + return _cmd_hook_command(mjs) + return _sh_hook_command(mjs) + + +def _sh_hook_command(mjs): + """POSIX:写 ``.sh`` 包装脚本,配置里只引用它的路径。""" + wrapper = mjs.with_suffix(".sh") + lines = [ + "#!/bin/sh", + "# octop memory hook(安装器生成,勿手改):配置里只引用本文件路径", + ] + if os.environ.get("OCTOP_USE_SYSTEM_CA") == "1": + lines.append('export NODE_OPTIONS="${NODE_OPTIONS:+$NODE_OPTIONS }--use-system-ca"') + run = f"{shlex.quote(str(NODE))} {shlex.quote(str(mjs))}" + failure = 'echo "octop-memory: hook 执行失败(node 路径不对或脚本报错)" >&2' + lines.append(f"{run} || {failure}") + wrapper.parent.mkdir(parents=True, exist_ok=True) + wrapper.write_text("\\n".join(lines) + "\\n", encoding="utf-8") + wrapper.chmod(0o755) + return shlex.quote(str(wrapper)) + + +def _cmd_hook_command(mjs): + """Windows:写 ``.cmd`` 包装脚本,配置里只引用它的路径。 + + Windows 的客户端把 hook 命令交给系统 shell,``cmd.exe`` 能直接跑 ``.cmd`` —— 不要求装 + Git Bash,也不用把带空格/引号的 node 绝对路径塞进 JSON-TOML。 + + 文件内容是 **ASCII**(英文提示):cmd.exe 按系统 ANSI 代码页读批处理文件,中文提示在 + cp936 / cp65001 之间会乱码;落盘编码用 ``mbcs``,路径里的中文用户名才不会被读错。 + """ + wrapper = mjs.with_suffix(".cmd") + lines = [ + "@echo off", + "rem octop memory hook (generated by installer; config only references this file)", + "setlocal", + ] + if os.environ.get("OCTOP_USE_SYSTEM_CA") == "1": + lines += [ + "if defined NODE_OPTIONS (", + ' set "NODE_OPTIONS=%NODE_OPTIONS% --use-system-ca"', + ") else (", + ' set "NODE_OPTIONS=--use-system-ca"', + ")", + ] + lines.append(f'"{NODE}" "{mjs}"') + lines.append("if errorlevel 1 echo octop-memory: hook failed (node path or script error) 1>&2") + wrapper.parent.mkdir(parents=True, exist_ok=True) + wrapper.write_text("\\r\\n".join(lines) + "\\r\\n", encoding=_wrapper_encoding()) + return '"' + str(wrapper) + '"' + + +def _wrapper_encoding(): + """包装脚本的落盘编码:cmd.exe 读系统 ANSI(``mbcs``),POSIX 用 UTF-8。""" + return "mbcs" if os.name == "nt" else "utf-8" + + +AGENT = os.environ["OCTOP_AGENT"] +CC_DIR = pathlib.Path(os.environ["OCTOP_CC_DIR"]).expanduser() +KIRO_DIR = pathlib.Path(os.environ["OCTOP_KIRO_DIR"]).expanduser() +DSH_DIR = pathlib.Path(os.environ["OCTOP_DSH_DIR"]).expanduser() +CODEX_DIR = pathlib.Path(os.environ["OCTOP_CODEX_DIR"]).expanduser() +CURSOR_DIR = pathlib.Path(os.environ["OCTOP_CURSOR_DIR"]).expanduser() +HOOK_FILES = { + host: json.loads(os.environ["OCTOP_FILES_" + host.upper().replace("-", "_")]) + for host in ("claude-code", "kiro", "codex", "cursor") +} +# 原生插件(octop-memory-native)的 vendored 源码:{相对路径: base64},与 hooks 同一套 payload +# 机制(安装器渲染时注入,避免脚本里再抄一份插件代码)。 +NATIVE_FILES = json.loads(os.environ["OCTOP_FILES_NATIVE"]) +NATIVE_NAME = "octop-memory-native" + +for terminal in os.environ["OCTOP_TERMINALS"].split(","): + terminal = terminal.strip() + if not terminal: + continue + print(f"· {terminal}") + { + "claude-code": do_claude_code, + "kiro": do_kiro, + "dsh": do_dsh, + "codex": do_codex, + "cursor": do_cursor, + }[terminal]() + +print(" · hook 角色:用户提交时召回(CC / Codex 的 UserPromptSubmit,Cursor 的 beforeSubmitPrompt);") +print(" 回合结束时上报会话 transcript(Stop / Cursor 的 stop)") +print(f" · hooks 直连 {HOOK_URL};客户端 MCP 条目 {ENTRY_URL}(Authorization: Bearer + X-Octop-Agent-Id)") +print(" · DSH 用原生插件 octop-memory-native 在进程内召回/上报(不装 hooks 桥接)") +print(" · octop memory 接入完成(可重复执行,不会影响其它 MCP/hook 条目)") +'''.strip() + + +def _b64(text: str) -> str: + return base64.b64encode(text.encode("utf-8")).decode("ascii") + + +def _host_manifest(host: str) -> str: + """``{relative path: base64}`` for one host: its entries + the shared lib modules.""" + files = (*_LIB_FILES, *HOST_FILES[host]) + payload = {name: _b64((_HOOKS_SRC / name).read_text(encoding="utf-8")) for name in files} + return json.dumps(payload, ensure_ascii=False, separators=(",", ":")) + + +def _dsh_native_manifest() -> str: + """``{relative path: base64}`` for the vendored native plugin (``octop-memory-native``). + + Tolerant of a checkout that has not vendored the plugin yet: the installer still renders + (``/setup`` must never 500), and the DSH step then refuses with a clear message instead + of writing a half-empty bundle. + """ + payload: dict[str, str] = {} + for name in _DSH_NATIVE_FILES: + path = _DSH_NATIVE_SRC / name + if path.is_file(): + payload[name] = _b64(path.read_text(encoding="utf-8")) + return json.dumps(payload, ensure_ascii=False, separators=(",", ":")) + + +def _server_name(mcp_url: str) -> str: + """``octop-memory-local`` for a loopback endpoint, ``octop-memory`` otherwise.""" + host = mcp_url.split("//", 1)[-1].split("/", 1)[0].split(":", 1)[0].lower() + return "octop-memory-local" if host in ("127.0.0.1", "localhost", "::1") else "octop-memory" + + +def _sh_single_quote(value: str) -> str: + """Always return a single-quoted shell literal (safe for script embedding). + + Unlike ``shlex.quote`` (which may leave safe tokens unquoted), this keeps the + ``VAR='…'`` form used throughout the installer and escapes embedded quotes. + """ + return "'" + str(value).replace("'", "'\"'\"'") + "'" + + +def _ps_single_quote(value: str) -> str: + """PowerShell single-quoted literal: ``'…'``, embedded ``'`` doubled (no interpolation).""" + return "'" + str(value).replace("'", "''") + "'" + + +def _ps_home_rel(path: str) -> str: + r"""``~/.claude/hooks`` → ``.claude\hooks``(PowerShell 里 Join-Path $HOME 的相对段)。""" + return path.replace("~/", "", 1).replace("/", "\\") + + +def render_setup_script( + *, + mcp_url: str, + hook_url: str = "", + agent_id: str = "", + terminals: str = "", + default_token: str = "", + project: str = "", +) -> str: + """Render the interactive installer (POSIX ``sh``). + + ``mcp_url`` is the address registered into the client MCP entries; ``hook_url`` is what + the hooks POST to, defaulting to ``mcp_url`` (a deployment that fronts the MCP mount + with an ingress can point the entries elsewhere). + + ``default_token`` is an **optional prefill** for the user-level MCP token — callers pass + an empty string, because the installer must never ship a credential of its own. The + script falls back to ``OCTOP_MEMORY_TOKEN`` first and only then uses this value. + ``agent_id`` and ``terminals`` (comma list) only prefill the prompts. + + ``project`` is the **prefill** path for a project-scoped install (the interactive + default). Non-interactive runs read ``OCTOP_PROJECT_DIR`` — the environment wins, and an + explicitly empty value forces a global install. Omitted = global. + """ + hook_url = hook_url or mcp_url + token_literal = _sh_single_quote(default_token) if default_token else "''" + # Query-string prefills must be shell-safe (same escaping as DEFAULT_TOKEN). + url_literal = _sh_single_quote(mcp_url) + hook_url_literal = _sh_single_quote(hook_url) + agent_literal = _sh_single_quote(agent_id) if agent_id else "''" + terminals_literal = _sh_single_quote(terminals) if terminals else "''" + project_literal = _sh_single_quote(project) if project else "''" + default_token_line = f"DEFAULT_TOKEN={token_literal}" if default_token else "" + default_token_block = ( + f"""if [ -z "$TOKEN" ] && [ -n {token_literal} ]; then + TOKEN={token_literal} + echo " · token:用安装链接预填的值(个人 MCP token)" +fi""" + if default_token + else "" + ) + return f"""#!/bin/sh +# octop memory — 本地终端接入(hooks 发往 {hook_url};可重复执行) +set -eu + +# Git Bash / MSYS 上也能跑,但 Windows 原生(不需要 Git Bash)请用 PowerShell 版: +# irm "{mcp_url}?os=windows" | iex +case "$(uname -s 2>/dev/null)" in + MINGW*|MSYS*|CYGWIN*) echo " ! 检测到 Windows(Git Bash/MSYS):推荐用原生安装器 irm \\"{mcp_url}?os=windows\\" | iex;这里按 sh 方式继续" >&2 ;; +esac + +# hook 包装脚本用 .sh(与 sh 环境一致);Windows 原生安装器走 ?os=windows 的 PowerShell 版 +OCTOP_HOOK_WRAPPER=sh +export OCTOP_HOOK_WRAPPER + +URL={url_literal} +HOOK_URL={hook_url_literal} +DEFAULT_AGENT={agent_literal} +{default_token_line} +DEFAULT_TERMINALS={terminals_literal} +DEFAULT_PROJECT={project_literal} +CC_DIR='{HOOKS_DIRS["claude-code"]}' +KIRO_DIR='{HOOKS_DIRS["kiro"]}' +DSH_DIR='{HOOKS_DIRS["dsh"]}' +CODEX_DIR='{HOOKS_DIRS["codex"]}' +CURSOR_DIR='{HOOKS_DIRS["cursor"]}' + +# ---- 1) 检测本机装了哪些终端(目录或命令任一命中) +have_claude=0; have_kiro=0; have_dsh=0; have_codex=0; have_cursor=0 +{{ [ -e "$HOME/.claude.json" ] || [ -d "$HOME/.claude" ] || command -v claude >/dev/null 2>&1; }} && have_claude=1 +{{ [ -d "$HOME/.kiro" ] || command -v kiro >/dev/null 2>&1; }} && have_kiro=1 +{{ [ -d "$HOME/.dsh" ] || command -v dsh >/dev/null 2>&1; }} && have_dsh=1 +{{ [ -d "$HOME/.codex" ] || command -v codex >/dev/null 2>&1; }} && have_codex=1 +{{ [ -d "$HOME/.cursor" ] || command -v cursor >/dev/null 2>&1; }} && have_cursor=1 + +DETECTED='' +[ "$have_claude" = 1 ] && DETECTED="$DETECTED claude-code" +[ "$have_kiro" = 1 ] && DETECTED="$DETECTED kiro" +[ "$have_dsh" = 1 ] && DETECTED="$DETECTED dsh" +[ "$have_codex" = 1 ] && DETECTED="$DETECTED codex" +[ "$have_cursor" = 1 ] && DETECTED="$DETECTED cursor" +[ -n "$DETECTED" ] || DETECTED='claude-code' + +echo "octop memory 接入 → {mcp_url}" +echo "检测到:" +[ "$have_claude" = 1 ] && echo " 1) Claude Code (hook 装到 ~/.claude/hooks-events)" +[ "$have_kiro" = 1 ] && echo " 2) Kiro    (hook 装到 ~/.kiro/hooks)" +[ "$have_dsh" = 1 ] && echo " 3) DSH    (装原生插件 octop-memory-native,进程内召回/上报)" +[ "$have_codex" = 1 ] && echo " 4) Codex   (hook 装到 ~/.codex/hooks,配置写 ~/.codex/config.toml)" +[ "$have_cursor" = 1 ] && echo " 5) Cursor   (hook 装到 ~/.cursor/hooks,配置写 ~/.cursor/hooks.json)" + +# 有 controlling tty 才交互:curl | sh 时 stdin 是脚本本身,读它会把脚本吃掉; +# 没有 tty(CI/cron)时依赖环境变量,缺什么就明确报错而不是盲读 stdin。 +HAS_TTY=0 +if (true /dev/null; then HAS_TTY=1; fi + +ask() {{ # ask <提示> <默认值> + if [ "$HAS_TTY" = 1 ]; then + printf '%s' "$1" > /dev/tty 2>/dev/null || true + read -r _answer < /dev/tty || _answer='' + else + _answer='' + fi + if [ -n "$_answer" ]; then printf '%s' "$_answer"; else printf '%s' "$2"; fi +}} + +# ---- 2) 选终端:交互式清单(↑↓ 移动 · 空格 选择 · a 全选 · 回车 确认); +# 没有 tty 或没有 python3 时回退到数字输入。 +TERMINALS="${{OCTOP_TERMINALS:-$DEFAULT_TERMINALS}}" +if [ -z "$TERMINALS" ] && [ "$HAS_TTY" = 1 ]; then + _picked='' + if command -v python3 >/dev/null 2>&1; then + set -- + [ "$have_claude" = 1 ] && set -- "$@" "claude-code|Claude Code (hook 装到 ~/.claude/hooks-events)" + [ "$have_kiro" = 1 ] && set -- "$@" "kiro|Kiro (hook 装到 ~/.kiro/hooks)" + [ "$have_dsh" = 1 ] && set -- "$@" "dsh|DSH (装原生插件 octop-memory-native)" + [ "$have_codex" = 1 ] && set -- "$@" "codex|Codex (hook 装到 ~/.codex/hooks)" + [ "$have_cursor" = 1 ] && set -- "$@" "cursor|Cursor (hook 装到 ~/.cursor/hooks)" + _picked="$(python3 - "$@" 2>/dev/null <<'OCTOP_SELECT' +{_SELECT_PY} +OCTOP_SELECT +)" || _picked='' + fi + if [ -n "$_picked" ]; then + TERMINALS="$_picked" + else + # 回退:数字输入(1,2 / 1 3 / 12 / a=全部;回车 = 全部检测到的;选不中会重问) + _attempt=0 + while [ -z "$TERMINALS" ] && [ "$_attempt" -lt 3 ]; do + _attempt=$((_attempt + 1)) + printf '要配置哪些?(可多选:1,2 / 1 3 / 12 / a=全部,回车=全部检测到的): ' > /dev/tty + read -r _pick < /dev/tty || _pick='' + _chosen='' + case "$_pick" in + ''|a|A|all|ALL|全部) _chosen="$DETECTED" ;; + *) + for _n in $(printf '%s' "$_pick" | tr ',' ' '); do + case "$_n" in + [0-9][0-9]*) _items="$(printf '%s' "$_n" | fold -w1 | tr '\\n' ' ')" ;; + *) _items="$_n" ;; + esac + for _i in $_items; do + case "$_i" in + 1) _add="$([ "$have_claude" = 1 ] && echo claude-code)" ;; + 2) _add="$([ "$have_kiro" = 1 ] && echo kiro)" ;; + 3) _add="$([ "$have_dsh" = 1 ] && echo dsh)" ;; + 4) _add="$([ "$have_codex" = 1 ] && echo codex)" ;; + 5) _add="$([ "$have_cursor" = 1 ] && echo cursor)" ;; + *) _add='' ;; + esac + if [ -z "$_add" ]; then + echo " ? 已忽略:${{_i}}(未检测到或无法识别)" >&2 + else + case ",$_chosen," in *",$_add,"*) ;; *) _chosen="$_chosen,$_add" ;; esac + fi + done + done + ;; + esac + TERMINALS="$(printf '%s' "$_chosen" | sed 's/^,//')" + [ -n "$TERMINALS" ] || echo " ! 没有选中任何已检测到的终端,请重选(1-5,或 a=全部)" >&2 + done + fi +fi + +[ -n "$TERMINALS" ] || {{ echo "没有可配置的终端" >&2; exit 1; }} + +# ---- 2.5) 安装范围:全局(默认)还是只装到某个项目 +# 项目级只在项目里写「注册」(hooks 命令 / MCP 条目);hook 脚本与 config.json(含 token) +# 始终留在本机用户目录,项目里不出现脚本。Kiro 的 MCP 条目自带凭据、DSH 没有项目级配置, +# 这两个在项目模式会被跳过(见合并脚本里的提示)。 +# ?project= 预填只在**交互式**生效(变成默认答案);非交互只认 OCTOP_PROJECT_DIR —— +# 显式置空 = 强制全局。 +if [ "${{OCTOP_PROJECT_DIR+x}}" = x ]; then + PROJECT_DIR="$OCTOP_PROJECT_DIR" +else + PROJECT_DIR='' +fi +if [ -z "$PROJECT_DIR" ] && [ "$HAS_TTY" = 1 ]; then + echo "安装范围:" + echo " 1) 全局(所有项目生效,默认)" + echo " 2) 只装到某个项目(项目里只写注册;脚本和凭据留在本机)" + _default_scope=1 + if [ -n "$DEFAULT_PROJECT" ]; then _default_scope=2; fi + _scope="$(ask "选择 [1/2]: " "$_default_scope")" + case "$_scope" in + 2|p|P|project|项目) + _project_default="${{DEFAULT_PROJECT:-$PWD}}" + PROJECT_DIR="$(ask "项目根目录 [$_project_default]: " "$_project_default")" + ;; + esac +fi +if [ -n "$PROJECT_DIR" ]; then + case "$PROJECT_DIR" in + "~") PROJECT_DIR="$HOME" ;; + "~/"*) PROJECT_DIR="$HOME/${{PROJECT_DIR#\\~/}}" ;; + esac + if [ ! -d "$PROJECT_DIR" ]; then + echo " ! 项目目录不存在(或不是目录):$PROJECT_DIR" >&2 + exit 1 + fi + PROJECT_DIR="$(cd "$PROJECT_DIR" && pwd -P)" + if [ ! -w "$PROJECT_DIR" ]; then + echo " ! 项目目录不可写:$PROJECT_DIR" >&2 + exit 1 + fi + echo " · 安装范围:项目级 → $PROJECT_DIR" + echo " · 项目里只写注册(hooks 命令 / MCP 条目);hook 脚本与 token 留在本机用户目录" + echo " · 不入仓库:Claude Code 走 ~/.claude.json 的 local scope 与 .claude/settings.local.json" + echo " · Codex / Cursor 的项目文件里只有命令(不含凭据)" + echo " · 跳过:Kiro 的 .kiro/settings/mcp.json(含凭据)、DSH(没有项目级配置)" + if [ ! -d "$PROJECT_DIR/.git" ]; then + echo " · 提示:不是 git 仓库根目录(项目级配置仍可用,只是不会进版本库)" + fi +fi +export OCTOP_PROJECT_DIR="$PROJECT_DIR" + +# ---- 3) 凭据:用户级 MCP token(omcp_…),条目与 hooks 共用同一份 +TOKEN="${{OCTOP_MEMORY_TOKEN:-}}" +{default_token_block} +if [ -z "$TOKEN" ] && [ "$HAS_TTY" = 1 ]; then + stty -echo < /dev/tty 2>/dev/null || true + printf 'MCP token(omcp_…,在 Octop 的 /api/memory-mcp/tokens 创建;不回显): ' > /dev/tty + read -r _t < /dev/tty || _t='' + stty echo < /dev/tty 2>/dev/null || true + printf '\\n' > /dev/tty + [ -n "$_t" ] && TOKEN="$_t" +fi +[ -n "$TOKEN" ] || {{ echo "缺少 MCP token:非交互运行时请设 OCTOP_MEMORY_TOKEN=" >&2; exit 1; }} +TOKEN_HEADER='Authorization' +echo " · 客户端 MCP 条目与 hooks:${{URL}}(Authorization: Bearer + X-Octop-Agent-Id)" + +AGENT="${{OCTOP_AGENT_ID:-}}" +if [ -z "$AGENT" ]; then + if [ -n "$DEFAULT_AGENT" ]; then + AGENT="$(ask "专家 ID [$DEFAULT_AGENT]: " "$DEFAULT_AGENT")" + else + AGENT="$(ask "专家 ID: " "")" + fi +fi +[ -n "$AGENT" ] || {{ echo "缺少专家 ID:非交互运行时请设 OCTOP_AGENT_ID=" >&2; exit 1; }} + +command -v node >/dev/null 2>&1 || echo " ! 未找到 node:hook 需要它,请先安装 Node.js" >&2 +command -v python3 >/dev/null 2>&1 || {{ echo " ! 未找到 python3:无法合并终端配置" >&2; exit 1; }} + +# ---- 3.5) 解析 node 绝对路径:hook 由终端派生,常在非 login shell 里跑, +# nvm 的 node 不在 PATH —— 写裸 node 会静默失败(command hook 非 0 退出不报错)。 +NODE_BIN="${{OCTOP_NODE:-}}" +if [ -z "$NODE_BIN" ] && command -v node >/dev/null 2>&1; then + NODE_BIN="$(command -v node)" +fi +if [ -z "$NODE_BIN" ] && [ -d "$HOME/.nvm/versions/node" ]; then + _ver="$(ls -1 "$HOME/.nvm/versions/node" 2>/dev/null | sort | tail -n 1)" + [ -n "$_ver" ] && [ -x "$HOME/.nvm/versions/node/$_ver/bin/node" ] && NODE_BIN="$HOME/.nvm/versions/node/$_ver/bin/node" +fi +for _cand in /opt/homebrew/bin/node /usr/local/bin/node "$HOME/.volta/bin/node" /usr/bin/node; do + if [ -z "$NODE_BIN" ] && [ -x "$_cand" ]; then NODE_BIN="$_cand"; fi +done +if [ -z "$NODE_BIN" ] || [ ! -x "$NODE_BIN" ]; then + NODE_BIN=node + echo " ! 找不到 node 的绝对路径:hook 里只能写裸 node,非 login shell 下可能静默失败" >&2 +else + echo " · node: $NODE_BIN" +fi +export OCTOP_NODE="$NODE_BIN" + +# Node 22.15+ 的 --use-system-ca 读操作系统证书库。企业根不在 Node 自带库里时, +# 不加这个前缀,hook 和下面的端点自检都会 TypeError: fetch failed。 +USE_SYSTEM_CA=0 +if "$NODE_BIN" --use-system-ca -e 'process.exit(0)' >/dev/null 2>&1; then + USE_SYSTEM_CA=1 + echo " · node 使用系统证书库(--use-system-ca)" +else + echo " ! 当前 node 不支持 --use-system-ca(需要 22.15+):企业根证书下 hook 的 fetch 可能失败" >&2 +fi +export OCTOP_USE_SYSTEM_CA="$USE_SYSTEM_CA" +if [ "$USE_SYSTEM_CA" = 1 ]; then + NODE_OPTIONS="${{NODE_OPTIONS:+$NODE_OPTIONS }}--use-system-ca" + export NODE_OPTIONS +fi + +# ---- 4) 先验证这份凭据在该端点可用(免得装完才发现 401) +if [ "$NODE_BIN" != node ] || command -v node >/dev/null 2>&1; then + OCTOP_URL="$HOOK_URL" OCTOP_TOKEN="$TOKEN" OCTOP_AGENT="$AGENT" \\ + "$NODE_BIN" --input-type=module -e ' +const cfg = {{ url: process.env.OCTOP_URL, token: process.env.OCTOP_TOKEN }}; +const headers = {{ "Content-Type": "application/json", Accept: "application/json, text/event-stream", + Authorization: `Bearer ${{cfg.token}}`, "X-Octop-Agent-Id": process.env.OCTOP_AGENT }}; +try {{ + const r = await fetch(cfg.url, {{ method: "POST", headers, signal: AbortSignal.timeout(8000), body: JSON.stringify({{ jsonrpc: "2.0", id: 1, method: "tools/list" }}) }}); + console.log(` · 端点自检(hook 直连):HTTP ${{r.status}}${{r.status === 200 ? "" : "(凭据或地址不对,请检查)"}}`); +}} catch (e) {{ + console.log(` · 端点自检失败:${{e}}(安装仍继续,请自行确认地址)`); +}} +' 2>/dev/null || true +fi + +# ---- 5) 各终端各自安装 hook 脚本 / 插件 + 合并其配置 +OCTOP_URL="$URL" OCTOP_HOOK_URL="$HOOK_URL" OCTOP_TOKEN="$TOKEN" \\ +OCTOP_AGENT="$AGENT" OCTOP_SERVER_NAME='{_server_name(mcp_url)}' \\ +OCTOP_CC_DIR="$CC_DIR" OCTOP_KIRO_DIR="$KIRO_DIR" OCTOP_DSH_DIR="$DSH_DIR" \\ +OCTOP_CODEX_DIR="$CODEX_DIR" OCTOP_CURSOR_DIR="$CURSOR_DIR" \\ +OCTOP_TERMINALS="$TERMINALS" \\ +OCTOP_PROJECT_DIR="$PROJECT_DIR" \\ +OCTOP_FILES_CLAUDE_CODE='{_host_manifest("claude-code")}' \\ +OCTOP_FILES_KIRO='{_host_manifest("kiro")}' \\ +OCTOP_FILES_CODEX='{_host_manifest("codex")}' \\ +OCTOP_FILES_CURSOR='{_host_manifest("cursor")}' \\ +OCTOP_FILES_NATIVE='{_dsh_native_manifest()}' \\ +python3 - <<'OCTOP_PY' +{_MERGE_PY} +OCTOP_PY + +echo " · 重开终端/IDE(Kiro 重载 hooks;Codex 首次需在 codex 里信任该 hook;DSH 重启后加载插件)后生效" +""" + + +_UNINSTALL_PY = r''' +import json +import os +import pathlib +import shutil + + +_ENV_FILE = os.environ.get("OCTOP_ENV_FILE", "") +if _ENV_FILE: + with open(_ENV_FILE, encoding="utf-8") as _env_handle: + for _key, _value in json.load(_env_handle).items(): + os.environ.setdefault(str(_key), str(_value)) + + +home = pathlib.Path(os.path.expanduser("~")) +CC_DIR = pathlib.Path(os.environ["OCTOP_CC_DIR"]).expanduser() +KIRO_DIR = pathlib.Path(os.environ["OCTOP_KIRO_DIR"]).expanduser() +DSH_DIR = pathlib.Path(os.environ["OCTOP_DSH_DIR"]).expanduser() +CODEX_DIR = pathlib.Path(os.environ["OCTOP_CODEX_DIR"]).expanduser() +CURSOR_DIR = pathlib.Path(os.environ["OCTOP_CURSOR_DIR"]).expanduser() + +# ---- 安装范围:全局(默认)或只装到某个项目 ---- +# 项目级卸载只清项目里的「注册」;用户目录里的 hook 脚本与 config.json 留给全局卸载。 +PROJECT = (os.environ.get("OCTOP_PROJECT_DIR") or "").strip() +PROJECT_DIR = pathlib.Path(PROJECT).expanduser() if PROJECT else None +IN_PROJECT = PROJECT_DIR is not None +PROJECT = str(PROJECT_DIR) if IN_PROJECT else "" + + +def _project(*parts): + """项目级产物路径(全局模式不会被调用)。""" + return PROJECT_DIR.joinpath(*parts) + + +_OUR_SCRIPTS = ( + "user-prompt-recall.mjs", + "stop-capture.mjs", + # 包装脚本(配置里引用的是 .sh / Windows 的 .cmd) + "user-prompt-recall.sh", + "stop-capture.sh", + "user-prompt-recall.cmd", + "stop-capture.cmd", + # 早期版本的文件名 + "octop-user-prompt-recall.mjs", + "octop-stop-capture.mjs", +) +_OUR_MCP_TOOLS = ("memory_search", "memory_capture") + + +def _load(path): + if not path.exists(): + return None + try: + return json.loads(path.read_text(encoding="utf-8")) + except Exception as exc: + print(f" ! 读不了 {path}:{exc}") + return None + + +def _dump(path, data): + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text(json.dumps(data, ensure_ascii=False, indent=2) + "\n", encoding="utf-8") + + +def _octop_servers(servers): + return [name for name in servers if str(name).replace("_", "-").startswith("octop-memory")] + + +def _strings(value): + """递归取出 JSON 结构里的所有字符串(判断「这条是不是我们写的」用)。""" + if isinstance(value, str): + yield value + elif isinstance(value, dict): + for item in value.values(): + yield from _strings(item) + elif isinstance(value, list): + for item in value: + yield from _strings(item) + + +def _mentions_dir(value, directory): + """JSON / TOML 文本里是否引用了这个目录(统一成正斜杠再比,Windows 反斜杠也认)。""" + needle = str(directory).replace("\\", "/") + return any(needle in text.replace("\\", "/") for text in _strings(value)) + + +def _handlers_of(entry): + """一个 hook 条目里的处理项:嵌套 matcher 组或简写形态的单条(与安装器一致)。""" + nested = (entry or {}).get("hooks") + if isinstance(nested, list): + return nested + return [entry] + + +def _is_our_handler(handler): + handler = handler or {} + if handler.get("type") == "mcp_tool": + return handler.get("tool") in _OUR_MCP_TOOLS + command = handler.get("command") or "" + return any(script in command for script in _OUR_SCRIPTS) + + +def _rm_tree(path, label): + if not path.exists(): + print(f" · {label} 不存在,跳过 → {path}") + return + shutil.rmtree(path) + print(f" ✓ 已删除 {label} → {path}") + + +def _strip_mcp(path): + cfg = _load(path) + if cfg is None: + return + servers = cfg.get("mcpServers") or {} + ours = _octop_servers(servers) + if not ours: + print(f" · {path} 无 octop-memory* MCP 条目") + return + for name in ours: + servers.pop(name, None) + cfg["mcpServers"] = servers + _dump(path, cfg) + print(f" ✓ 已移除 MCP 服务 {', '.join(ours)} → {path}") + + +def _strip_claude_settings(settings): + """清掉 settings 里我们写的 hook 条目(返回是否有改动)。""" + st = _load(settings) + if st is None: + return False + hooks = st.setdefault("hooks", {}) + changed = False + for event in list(hooks): + kept = [ + group + for group in (hooks.get(event) or []) + if not any(_is_our_handler(h) for h in _handlers_of(group)) + ] + if len(kept) != len(hooks.get(event) or []): + changed = True + if kept: + hooks[event] = kept + else: + hooks.pop(event, None) + changed = True + if changed: + _dump(settings, st) + print(f" ✓ 已从 Claude Code settings 去掉 octop hooks → {settings}") + else: + print(f" · Claude Code settings 无 octop hooks → {settings}") + return changed + + +def _strip_mcp_local(): + """去掉 Claude Code local scope(``~/.claude.json`` 的 projects[<项目>])里的 octop 条目。""" + key = os.path.realpath(str(PROJECT_DIR)) + path = home / ".claude.json" + cfg = _load(path) + if not isinstance(cfg, dict): + return + projects = cfg.get("projects") + if not isinstance(projects, dict): + print(f" · {path} 里没有按项目存的 MCP 条目") + return + bucket = projects.get(key) + servers = bucket.get("mcpServers") if isinstance(bucket, dict) else None + if not isinstance(servers, dict): + print(f" · {path} 的项目 {key} 无 octop-memory* MCP 条目") + return + ours = _octop_servers(servers) + if not ours: + print(f" · {path} 的项目 {key} 无 octop-memory* MCP 条目") + return + for name in ours: + servers.pop(name, None) + if servers: + bucket["mcpServers"] = servers + else: + bucket.pop("mcpServers", None) + _dump(path, cfg) + print(f" ✓ 已移除 Claude Code local scope 的 MCP 服务 {', '.join(ours)}(项目 {key})") + + +def undo_claude_code(): + if IN_PROJECT: + _strip_claude_settings(_project(".claude", "settings.local.json")) + _strip_mcp_local() + print(" · 用户目录里的 hook 脚本保留(全局卸载才清)") + return + _strip_claude_settings(home / ".claude" / "settings.json") + _strip_mcp(home / ".claude.json") + _rm_tree(CC_DIR, "Claude Code hooks 目录") + + +def undo_kiro(): + hooks_dir = _project(".kiro", "hooks") if IN_PROJECT else KIRO_DIR + for name in ("memory-recall-on-start.json", "memory-save-on-stop.json"): + path = hooks_dir / name + if path.exists(): + path.unlink() + print(f" ✓ 已删除 Kiro hook 文件 → {path}") + else: + print(f" · Kiro hook 文件不存在 → {path}") + if IN_PROJECT: + _strip_mcp(_project(".kiro", "settings", "mcp.json")) + print(" · 用户目录里的 hook 脚本保留(全局卸载才清)") + return + _strip_mcp(home / ".kiro" / "settings" / "mcp.json") + _rm_tree(KIRO_DIR, "Kiro hooks 目录") + + +_DSH_NATIVE_NAME = "octop-memory-native" +_DSH_LEGACY_BUNDLES = ("dsh-memory-share", "octop-dsh-memory-share") + + +def _dsh_patch_entry(line): + """这一行是不是顶层 ``- id: octop-memory-native``(缩进的 insert 行不算)。""" + if not line.startswith("-"): + return False + rest = line[1:].strip() + if not rest.startswith("id:"): + return False + value = rest[len("id:") :].strip() + if " #" in value: + value = value.split(" #", 1)[0] + return value.strip().strip("'").strip('"') == _DSH_NATIVE_NAME + + +def _dsh_patch_bounds(lines): + """配置块在 cordis.patch.yml 里的 ``[start, end)``(含块注释与 ``# <<< id <<<`` 结尾标记)。""" + entry = None + for index, line in enumerate(lines): + if _dsh_patch_entry(line): + entry = index + break + if entry is None: + return None + start = entry + comments = [] + cursor = entry - 1 + while cursor >= 0: + above = lines[cursor] + if above.strip() == "" or above[:1] in (" ", "\t"): + break + if not above.lstrip().startswith("#"): + break + comments.insert(0, cursor) + cursor -= 1 + if comments: + joined = "".join(lines[index] for index in comments) + if _DSH_NATIVE_NAME not in joined: + while comments and _DSH_NATIVE_NAME not in lines[comments[0]]: + comments.pop(0) + if comments: + start = comments[0] + end = entry + 1 + while end < len(lines) and lines[end][:1] in (" ", "\t"): + end += 1 # 条目本体:缩进的 config 行 + probe = end + while probe < len(lines) and lines[probe].strip() == "": + probe += 1 + if ( + probe < len(lines) + and lines[probe].strip().startswith("#") + and "<<<" in lines[probe] + and _DSH_NATIVE_NAME in lines[probe] + ): + end = probe + 1 # 结尾标记(旧版在标记前留了空行,一起清掉) + while end < len(lines) and lines[end].strip() == "": + end += 1 + return start, end + + +def _strip_dsh_patch(profile_dir): + """去掉该 profile 里我们写的配置块(别人的条目 / 注释逐字保留)。""" + patch = profile_dir / "cordis.patch.yml" + if not patch.exists(): + return + lines = patch.read_text(encoding="utf-8").splitlines(keepends=True) + bounds = _dsh_patch_bounds(lines) + if bounds is None: + print(f" · {patch} 里没有 octop-memory-native 配置块") + return + start, end = bounds + updated = "".join(lines[:start]) + "".join(lines[end:]) + if updated.strip(): + patch.write_text(updated.rstrip("\n") + "\n", encoding="utf-8") + print(f" ✓ 已从 {patch} 去掉 octop-memory-native 配置块(其余内容保留)") + else: + patch.unlink() + print(f" ✓ 已删除只含 octop-memory-native 配置块的 {patch}") + + +def _strip_dsh_profile(profile_dir): + """去掉 profile 的插件登记:dependencies / node_modules 条目 / dsh.profile.bundles。""" + changed = [] + package = profile_dir / "package.json" + cfg = _load(package) + if isinstance(cfg, dict): + dependencies = cfg.get("dependencies") + if isinstance(dependencies, dict) and _DSH_NATIVE_NAME in dependencies: + dependencies.pop(_DSH_NATIVE_NAME) + changed.append("dependencies") + dsh = cfg.get("dsh") + profile_cfg = dsh.get("profile") if isinstance(dsh, dict) else None + bundles = profile_cfg.get("bundles") if isinstance(profile_cfg, dict) else None + if isinstance(bundles, list): + gone = [ + name for name in (_DSH_NATIVE_NAME,) + _DSH_LEGACY_BUNDLES if name in bundles + ] + if gone: + profile_cfg["bundles"] = [ + name + for name in bundles + if name not in (_DSH_NATIVE_NAME,) + _DSH_LEGACY_BUNDLES + ] + changed.append("dsh.profile.bundles(" + "、".join(gone) + ")") + if changed: + _dump(package, cfg) + module = profile_dir / "node_modules" / _DSH_NATIVE_NAME + if module.is_symlink(): + module.unlink() + changed.append("node_modules 链接") + elif module.exists(): + shutil.rmtree(module) + changed.append("node_modules 目录") + return changed + + +def undo_dsh(): + if IN_PROJECT: + print(" · DSH 没有项目级配置:项目级卸载不需要清它") + return + plugin = home / ".dsh" / "bundles" / _DSH_NATIVE_NAME + if plugin.exists(): + shutil.rmtree(plugin) + print(f" ✓ 已删除 DSH 原生插件 → {plugin}") + profiles = home / ".dsh" / "profiles" + if profiles.is_dir(): + for profile in sorted(profiles.iterdir()): + if not (profile / "package.json").exists(): + continue + changed = _strip_dsh_profile(profile) + if changed: + print(f" ✓ 已从 DSH profile「{profile.name}」去掉 " + "、".join(changed)) + _strip_dsh_patch(profile) + # hooks 回退模式(以及 native 之前的版本)留下的东西 + bridge = DSH_DIR / "octop-hooks.json" + if bridge.exists(): + bridge.unlink() + print(f" ✓ 已删除 DSH 桥接 → {bridge}") + bundle = DSH_DIR.parent / "bundles" / "dsh-memory-share" + if bundle.exists(): + shutil.rmtree(bundle) + print(f" ✓ 已删除 DSH preset bundle → {bundle}") + _rm_tree(DSH_DIR, "DSH hooks 目录") + + +def _toml_blocks(text): + blocks = [[]] + for line in text.splitlines(keepends=True): + if line.lstrip().startswith("[") and not line.lstrip().startswith("#"): + blocks.append([]) + blocks[-1].append(line) + return blocks + + +def undo_codex(): + path = _project(".codex", "config.toml") if IN_PROJECT else home / ".codex" / "config.toml" + if path.exists(): + text = path.read_text(encoding="utf-8") + blocks, kept = _toml_blocks(text), [] + removed = 0 + for block in blocks: + body = "".join(block) + header = block[0].strip() if block else "" + ours = _mentions_dir(body, CODEX_DIR) + if ours and header.startswith("[[hooks.") and header.endswith(".hooks]]"): + event = header[len("[[hooks.") : -len(".hooks]]")] + if kept and kept[-1][0].strip() == f"[[hooks.{event}]]" and not "".join(kept[-1][1:]).strip(): + kept.pop() + removed += 1 + continue + kept.append(block) + if removed: + path.write_text("".join("".join(block) for block in kept), encoding="utf-8") + print(f" ✓ 已从 Codex config.toml 去掉 octop hooks → {path}") + else: + print(f" · Codex config.toml 无 octop hooks → {path}") + else: + print(f" · Codex config.toml 不存在 → {path}") + if IN_PROJECT: + print(" · 用户目录里的 hook 脚本保留(全局卸载才清)") + return + _rm_tree(CODEX_DIR, "Codex hooks 目录") + + +def undo_cursor(): + path = _project(".cursor", "hooks.json") if IN_PROJECT else home / ".cursor" / "hooks.json" + cfg = _load(path) + if cfg is not None: + hooks = cfg.setdefault("hooks", {}) + marker = str(CURSOR_DIR) + changed = False + for event in list(hooks): + entries = hooks.get(event) or [] + kept = [entry for entry in entries if not _mentions_dir(entry, marker)] + if len(kept) != len(entries): + changed = True + if kept: + hooks[event] = kept + else: + hooks.pop(event, None) + changed = True + if changed: + cfg["hooks"] = hooks + _dump(path, cfg) + print(f" ✓ 已从 Cursor hooks.json 去掉 octop hooks → {path}") + else: + print(f" · Cursor hooks.json 无 octop hooks → {path}") + else: + print(f" · Cursor hooks.json 不存在 → {path}") + if IN_PROJECT: + print(" · 用户目录里的 hook 脚本保留(全局卸载才清)") + return + # setup 不为 Cursor 写 MCP,但检索常靠 ~/.cursor/mcp.json 里的 octop-memory* + _strip_mcp(home / ".cursor" / "mcp.json") + _rm_tree(CURSOR_DIR, "Cursor hooks 目录") + + +for terminal in os.environ["OCTOP_TERMINALS"].split(","): + terminal = terminal.strip() + if not terminal: + continue + print(f"· {terminal}") + { + "claude-code": undo_claude_code, + "kiro": undo_kiro, + "dsh": undo_dsh, + "codex": undo_codex, + "cursor": undo_cursor, + }[terminal]() + +print(" · octop memory 卸载完成(可重复执行;服务端已有记忆不会删除)") +print(" · 请重开终端/IDE 后生效") +'''.strip() + + +def render_uninstall_script(*, terminals: str = "", project: str = "") -> str: + """Render the interactive uninstaller (counterpart of ``render_setup_script``). + + No token / agent prompts — only chooses which terminals to scrub (and whether to scrub + the global install or one project's registrations). Safe to re-run; leaves non-octop + hooks and MCP servers alone. + + ``project`` is the **prefill** for a project-scoped uninstall (the interactive default); + non-interactive runs read ``OCTOP_PROJECT_DIR`` (an explicitly empty value = global). + """ + terminals_literal = _sh_single_quote(terminals) if terminals else "''" + project_literal = _sh_single_quote(project) if project else "''" + return f"""#!/bin/sh +# octop memory — 卸载本地终端接入(与 /mcp/memory/setup 相对;可重复执行) +set -eu + +DEFAULT_TERMINALS={terminals_literal} +DEFAULT_PROJECT={project_literal} +CC_DIR='{HOOKS_DIRS["claude-code"]}' +KIRO_DIR='{HOOKS_DIRS["kiro"]}' +DSH_DIR='{HOOKS_DIRS["dsh"]}' +CODEX_DIR='{HOOKS_DIRS["codex"]}' +CURSOR_DIR='{HOOKS_DIRS["cursor"]}' + +have_claude=0; have_kiro=0; have_dsh=0; have_codex=0; have_cursor=0 +{{ [ -e "$HOME/.claude.json" ] || [ -d "$HOME/.claude" ] || command -v claude >/dev/null 2>&1; }} && have_claude=1 +{{ [ -d "$HOME/.kiro" ] || command -v kiro >/dev/null 2>&1; }} && have_kiro=1 +{{ [ -d "$HOME/.dsh" ] || command -v dsh >/dev/null 2>&1; }} && have_dsh=1 +{{ [ -d "$HOME/.codex" ] || command -v codex >/dev/null 2>&1; }} && have_codex=1 +{{ [ -d "$HOME/.cursor" ] || command -v cursor >/dev/null 2>&1; }} && have_cursor=1 + +DETECTED='' +[ "$have_claude" = 1 ] && DETECTED="$DETECTED claude-code" +[ "$have_kiro" = 1 ] && DETECTED="$DETECTED kiro" +[ "$have_dsh" = 1 ] && DETECTED="$DETECTED dsh" +[ "$have_codex" = 1 ] && DETECTED="$DETECTED codex" +[ "$have_cursor" = 1 ] && DETECTED="$DETECTED cursor" +[ -n "$DETECTED" ] || DETECTED='cursor' + +echo "octop memory 卸载" +echo "检测到:" +[ "$have_claude" = 1 ] && echo " 1) Claude Code (清 ~/.claude/hooks-events + settings/MCP)" +[ "$have_kiro" = 1 ] && echo " 2) Kiro    (清 ~/.kiro/hooks + MCP)" +[ "$have_dsh" = 1 ] && echo " 3) DSH    (清原生插件 + profile 登记 + ~/.dsh/hooks 桥接/preset)" +[ "$have_codex" = 1 ] && echo " 4) Codex   (清 ~/.codex/hooks + config.toml 中的 octop hooks)" +[ "$have_cursor" = 1 ] && echo " 5) Cursor   (清 ~/.cursor/hooks + hooks.json;顺带去掉 mcp.json 里的 octop-memory*)" + +HAS_TTY=0 +if (true /dev/null; then HAS_TTY=1; fi + +TERMINALS="${{OCTOP_TERMINALS:-$DEFAULT_TERMINALS}}" +if [ -z "$TERMINALS" ] && [ "$HAS_TTY" = 1 ]; then + _picked='' + if command -v python3 >/dev/null 2>&1; then + set -- + [ "$have_claude" = 1 ] && set -- "$@" "claude-code|Claude Code" + [ "$have_kiro" = 1 ] && set -- "$@" "kiro|Kiro" + [ "$have_dsh" = 1 ] && set -- "$@" "dsh|DSH" + [ "$have_codex" = 1 ] && set -- "$@" "codex|Codex" + [ "$have_cursor" = 1 ] && set -- "$@" "cursor|Cursor" + _picked="$(python3 - "$@" 2>/dev/null <<'OCTOP_SELECT' +{_SELECT_PY} +OCTOP_SELECT +)" || _picked='' + fi + if [ -n "$_picked" ]; then + TERMINALS="$_picked" + else + _attempt=0 + while [ -z "$TERMINALS" ] && [ "$_attempt" -lt 3 ]; do + _attempt=$((_attempt + 1)) + printf '要卸载哪些?(可多选:1,2 / 1 3 / 12 / a=全部,回车=全部检测到的): ' > /dev/tty + read -r _pick < /dev/tty || _pick='' + _chosen='' + case "$_pick" in + ''|a|A|all|ALL|全部) _chosen="$DETECTED" ;; + *) + for _n in $(printf '%s' "$_pick" | tr ',' ' '); do + case "$_n" in + [0-9][0-9]*) _items="$(printf '%s' "$_n" | fold -w1 | tr '\\n' ' ')" ;; + *) _items="$_n" ;; + esac + for _i in $_items; do + case "$_i" in + 1) _add="$([ "$have_claude" = 1 ] && echo claude-code)" ;; + 2) _add="$([ "$have_kiro" = 1 ] && echo kiro)" ;; + 3) _add="$([ "$have_dsh" = 1 ] && echo dsh)" ;; + 4) _add="$([ "$have_codex" = 1 ] && echo codex)" ;; + 5) _add="$([ "$have_cursor" = 1 ] && echo cursor)" ;; + *) _add='' ;; + esac + if [ -z "$_add" ]; then + echo " ? 已忽略:${{_i}}" >&2 + else + case ",$_chosen," in *",$_add,"*) ;; *) _chosen="$_chosen,$_add" ;; esac + fi + done + done + ;; + esac + TERMINALS="$(printf '%s' "$_chosen" | sed 's/^,//')" + [ -n "$TERMINALS" ] || echo " ! 没有选中任何终端,请重选" >&2 + done + fi +fi + +[ -n "$TERMINALS" ] || {{ echo "没有可卸载的终端:非交互请设 OCTOP_TERMINALS=cursor" >&2; exit 1; }} + +# ---- 卸载范围:全局(默认)还是只清某个项目里的注册 +# ?project= 预填只在**交互式**生效(变成默认答案);非交互只认 OCTOP_PROJECT_DIR。 +if [ "${{OCTOP_PROJECT_DIR+x}}" = x ]; then + PROJECT_DIR="$OCTOP_PROJECT_DIR" +else + PROJECT_DIR='' +fi +if [ -z "$PROJECT_DIR" ] && [ "$HAS_TTY" = 1 ]; then + echo "卸载范围:" + echo " 1) 全局(默认)" + echo " 2) 只清某个项目里的注册(用户目录里的脚本保留)" + _default_scope=1 + if [ -n "$DEFAULT_PROJECT" ]; then _default_scope=2; fi + printf '选择 [1/2](默认 %s): ' "$_default_scope" > /dev/tty + read -r _scope < /dev/tty || _scope='' + if [ -z "$_scope" ]; then _scope="$_default_scope"; fi + case "$_scope" in + 2|p|P|project|项目) + if [ -n "$DEFAULT_PROJECT" ]; then + printf '项目根目录 [%s]: ' "$DEFAULT_PROJECT" > /dev/tty + read -r PROJECT_DIR < /dev/tty || PROJECT_DIR='' + if [ -z "$PROJECT_DIR" ]; then PROJECT_DIR="$DEFAULT_PROJECT"; fi + else + printf '项目根目录: ' > /dev/tty + read -r PROJECT_DIR < /dev/tty || PROJECT_DIR='' + fi + if [ -z "$PROJECT_DIR" ]; then + echo " ! 项目根目录不能为空" >&2 + exit 1 + fi + ;; + esac +fi +if [ -n "$PROJECT_DIR" ]; then + case "$PROJECT_DIR" in + "~") PROJECT_DIR="$HOME" ;; + "~/"*) PROJECT_DIR="$HOME/${{PROJECT_DIR#\\~/}}" ;; + esac + if [ ! -d "$PROJECT_DIR" ]; then + echo " ! 项目目录不存在(或不是目录):$PROJECT_DIR" >&2 + exit 1 + fi + PROJECT_DIR="$(cd "$PROJECT_DIR" && pwd -P)" + if [ ! -w "$PROJECT_DIR" ]; then + echo " ! 项目目录不可写:$PROJECT_DIR" >&2 + exit 1 + fi + echo " · 卸载范围:项目级 → ${{PROJECT_DIR}}(用户目录里的 hook 脚本保留)" +fi +export OCTOP_PROJECT_DIR="$PROJECT_DIR" + +command -v python3 >/dev/null 2>&1 || {{ echo " ! 未找到 python3" >&2; exit 1; }} + +OCTOP_CC_DIR="$CC_DIR" OCTOP_KIRO_DIR="$KIRO_DIR" OCTOP_DSH_DIR="$DSH_DIR" \\ +OCTOP_CODEX_DIR="$CODEX_DIR" OCTOP_CURSOR_DIR="$CURSOR_DIR" \\ +OCTOP_TERMINALS="$TERMINALS" \\ +OCTOP_PROJECT_DIR="$PROJECT_DIR" \\ +python3 - <<'OCTOP_PY' +{_UNINSTALL_PY} +OCTOP_PY +""" + + +_PS_SELF_CHECK_JS = r""" +const cfg = { url: process.env.OCTOP_CHECK_URL, token: process.env.OCTOP_CHECK_TOKEN }; +const headers = { "Content-Type": "application/json", Accept: "application/json, text/event-stream", + Authorization: `Bearer ${cfg.token}`, "X-Octop-Agent-Id": process.env.OCTOP_CHECK_AGENT }; +try { + const r = await fetch(cfg.url, { method: "POST", headers, signal: AbortSignal.timeout(8000), body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/list" }) }); + console.log(` · 端点自检(hook 直连):HTTP ${r.status}${r.status === 200 ? "" : "(凭据或地址不对,请检查)"}`); +} catch (e) { + console.log(` · 端点自检失败:${e}(安装仍继续,请自行确认地址)`); +} +""".strip() +"""Windows 安装器的端点自检脚本(与 sh 安装器同一段,环境变量名不同以免和合并逻辑抢)。""" + + +_SETUP_PS_TEMPLATE = r"""# octop memory — Windows 终端接入(hooks 发往 __HOOK_URL_TEXT__;可重复执行) +# 用法(PowerShell):irm "/mcp/memory/setup?os=windows" | iex +# 非交互(CI/批量):先设 $env:OCTOP_MEMORY_TOKEN / $env:OCTOP_AGENT_ID / $env:OCTOP_TERMINALS +# +# 与 POSIX 安装器共用同一套终端宿主和同一份「合并配置」python 程序;差别只在两处: +# * 交互走 PowerShell 的 Read-Host(Windows 没有 POSIX 的终端控制这套做法); +# * hook 包装脚本是 .cmd(OCTOP_HOOK_WRAPPER=cmd),由系统 shell 直接执行 —— 不需要 Git Bash。 +if ($PSVersionTable.PSVersion.Major -lt 5) { throw '需要 PowerShell 5.1 或更高版本' } +& { + $ErrorActionPreference = 'Stop' + + $URL = __URL_LITERAL__ + $HOOK_URL = __HOOK_URL_LITERAL__ + $DEFAULT_AGENT = __DEFAULT_AGENT_LITERAL__ + $DEFAULT_TOKEN = __DEFAULT_TOKEN_LITERAL__ + $DEFAULT_TERMINALS = __DEFAULT_TERMINALS_LITERAL__ + $DEFAULT_PROJECT = __DEFAULT_PROJECT_LITERAL__ + + $CC_DIR = Join-Path $HOME '__CC_DIR_REL__' + $KIRO_DIR = Join-Path $HOME '__KIRO_DIR_REL__' + $DSH_DIR = Join-Path $HOME '__DSH_DIR_REL__' + $CODEX_DIR = Join-Path $HOME '__CODEX_DIR_REL__' + $CURSOR_DIR = Join-Path $HOME '__CURSOR_DIR_REL__' + + $interactive = $true + try { $interactive = -not [Console]::IsInputRedirected } catch { $interactive = $true } + + function Test-Terminal([string]$Name) { + return [bool](Get-Command $Name -ErrorAction SilentlyContinue) + } + + function Read-Secret([string]$Prompt) { + $secure = Read-Host $Prompt -AsSecureString + $bstr = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($secure) + try { return [Runtime.InteropServices.Marshal]::PtrToStringBSTR($bstr) } + finally { [Runtime.InteropServices.Marshal]::ZeroFreeBSTR($bstr) } + } + + # ---- 1) 检测本机装了哪些终端(配置目录或命令任一命中) + $haveClaude = (Test-Path (Join-Path $HOME '.claude.json')) -or (Test-Path (Join-Path $HOME '.claude')) -or (Test-Terminal 'claude') + $haveKiro = (Test-Path (Join-Path $HOME '.kiro')) -or (Test-Terminal 'kiro') + $haveDsh = (Test-Path (Join-Path $HOME '.dsh')) -or (Test-Terminal 'dsh') + $haveCodex = (Test-Path (Join-Path $HOME '.codex')) -or (Test-Terminal 'codex') + $haveCursor = (Test-Path (Join-Path $HOME '.cursor')) -or (Test-Terminal 'cursor') + + # 固定槽位与 POSIX 安装器一致:1 Claude Code 2 Kiro 3 DSH 4 Codex 5 Cursor + $slotName = @{ 1 = 'claude-code'; 2 = 'kiro'; 3 = 'dsh'; 4 = 'codex'; 5 = 'cursor' } + $slotDetected = @{ 1 = $haveClaude; 2 = $haveKiro; 3 = $haveDsh; 4 = $haveCodex; 5 = $haveCursor } + $slotHint = @{ + 1 = 'Claude Code (hook 装到 ~/.claude/hooks-events)' + 2 = 'Kiro (hook 装到 ~/.kiro/hooks)' + 3 = 'DSH (装原生插件 octop-memory-native,进程内召回/上报)' + 4 = 'Codex (hook 装到 ~/.codex/hooks,配置写 ~/.codex/config.toml)' + 5 = 'Cursor (hook 装到 ~/.cursor/hooks,配置写 ~/.cursor/hooks.json)' + } + $detected = @() + foreach ($key in 1, 2, 3, 4, 5) { if ($slotDetected[$key]) { $detected += $slotName[$key] } } + if ($detected.Count -eq 0) { $detected = @('claude-code') } + + Write-Host "octop memory 接入 → $URL" + Write-Host '检测到:' + foreach ($key in 1, 2, 3, 4, 5) { + if ($slotDetected[$key]) { Write-Host (" {0}) {1}" -f $key, $slotHint[$key]) } + } + + # ---- 2) 选终端(1,2 / 1 3 / 12 / a=全部;回车=全部检测到的;选不中会重问) + $terminals = $env:OCTOP_TERMINALS + if (-not $terminals) { $terminals = $DEFAULT_TERMINALS } + if (-not $terminals -and $interactive) { + $attempt = 0 + while (-not $terminals -and $attempt -lt 3) { + $attempt++ + $pick = Read-Host '要配置哪些?(可多选:1,2 / 1 3 / 12 / a=全部,回车=全部检测到的)' + $chosen = @() + if (-not $pick -or $pick -match '^(?i)(a|all|全部)$') { + $chosen = @($detected) + } + else { + $tokens = @() + foreach ($token in ($pick -replace '[,\s]+', ' ').Split(' ')) { + if (-not $token) { continue } + if ($token -match '^[0-9]{2,}$') { + foreach ($char in $token.ToCharArray()) { $tokens += [string]$char } + } + else { $tokens += $token } + } + foreach ($token in $tokens) { + $index = 0 + if (-not [int]::TryParse($token, [ref]$index) -or -not $slotDetected[$index]) { + Write-Host " ? 已忽略:$token(未检测到或无法识别)" + continue + } + $name = $slotName[$index] + if ($chosen -notcontains $name) { $chosen += $name } + } + } + if ($chosen.Count -gt 0) { $terminals = ($chosen -join ',') } + else { Write-Host ' ! 没有选中任何已检测到的终端,请重选(1-5,或 a=全部)' -ForegroundColor Yellow } + } + } + if (-not $terminals) { throw '没有可配置的终端(非交互请先设 $env:OCTOP_TERMINALS)' } + + # ---- 2.5) 安装范围:全局(默认)还是只装到某个项目 + # 项目级只在项目里写「注册」(hooks 命令 / MCP 条目);hook 脚本与 config.json(含 token) + # 始终留在本机用户目录。Kiro 的 MCP 条目自带凭据、DSH 没有项目级配置,这两个在项目模式 + # 会被跳过(见合并脚本里的提示)。 + # ?project= 预填只在**交互式**生效(变成默认答案);非交互只认 OCTOP_PROJECT_DIR。 + # 显式设置(哪怕是空串)就以环境变量为准:空串 = 强制全局,不再交互提问。 + $projectSet = $null -ne $env:OCTOP_PROJECT_DIR + $projectDir = '' + if ($projectSet) { $projectDir = $env:OCTOP_PROJECT_DIR } + if (-not $projectSet -and $interactive) { + Write-Host '安装范围:' + Write-Host ' 1) 全局(所有项目生效,默认)' + Write-Host ' 2) 只装到某个项目(项目里只写注册;脚本和凭据留在本机)' + $defaultScope = '1' + if ($DEFAULT_PROJECT) { $defaultScope = '2' } + $scope = Read-Host "选择 [1/2](回车=$defaultScope)" + if (-not $scope) { $scope = $defaultScope } + if ($scope -in @('2', 'p', 'P', 'project', '项目')) { + $defaultProject = $DEFAULT_PROJECT + if (-not $defaultProject) { $defaultProject = (Get-Location).Path } + $projectDir = Read-Host "项目根目录 [$defaultProject]" + if (-not $projectDir) { $projectDir = $defaultProject } + } + } + if ($projectDir) { + if ($projectDir -eq '~' -or $projectDir -eq '~/' -or $projectDir -eq '~\') { $projectDir = $HOME } + elseif ($projectDir -match '^~[\\/]') { $projectDir = Join-Path $HOME $projectDir.Substring(2) } + if (-not (Test-Path -LiteralPath $projectDir -PathType Container)) { throw "项目目录不存在(或不是目录):$projectDir" } + $projectDir = (Resolve-Path -LiteralPath $projectDir).Path + $probe = Join-Path $projectDir ('.octop-write-probe-' + [guid]::NewGuid().ToString('N')) + try { + [System.IO.File]::WriteAllText($probe, '') + Remove-Item -LiteralPath $probe -Force + } + catch { throw "项目目录不可写:$projectDir" } + Write-Host " · 安装范围:项目级 → $projectDir" + Write-Host ' · 项目里只写注册(hooks 命令 / MCP 条目);hook 脚本与 token 留在本机用户目录' + Write-Host ' · 不入仓库:Claude Code 走 ~\.claude.json 的 local scope 与 .claude\settings.local.json' + Write-Host ' · Codex / Cursor 的项目文件里只有命令(不含凭据)' + Write-Host ' · 跳过:Kiro 的 .kiro\settings\mcp.json(含凭据)、DSH(没有项目级配置)' + if (-not (Test-Path -LiteralPath (Join-Path $projectDir '.git'))) { + Write-Host ' · 提示:不是 git 仓库根目录(项目级配置仍可用,只是不会进版本库)' + } + } + + # ---- 3) 凭据:用户级 MCP token(omcp_…),条目与 hooks 共用同一份 + $localNote = '' + $isLocal = @('127.0.0.1', 'localhost') -contains ([Uri]$HOOK_URL).Host + if ($isLocal) { $localNote = '(本机实例)' } + $token = $env:OCTOP_MEMORY_TOKEN + if (-not $token -and $DEFAULT_TOKEN) { + $token = $DEFAULT_TOKEN + Write-Host ' · token:用安装链接预填的值(个人 MCP token)' + } + if (-not $token -and $interactive) { + $token = Read-Secret 'MCP token(omcp_…,在 Octop 的 /api/memory-mcp/tokens 创建;不回显): ' + } + if (-not $token) { throw '缺少 MCP token:非交互运行时请先设 $env:OCTOP_MEMORY_TOKEN=' } + Write-Host " · hook 直连:$HOOK_URL$localNote" + Write-Host " · 客户端 MCP 条目:$URL(Authorization: Bearer + X-Octop-Agent-Id)" + + $agent = $env:OCTOP_AGENT_ID + if (-not $agent -and $interactive) { + if ($DEFAULT_AGENT) { $agent = Read-Host "专家 ID [$DEFAULT_AGENT]" } else { $agent = Read-Host '专家 ID' } + if (-not $agent) { $agent = $DEFAULT_AGENT } + } + if (-not $agent) { throw '缺少专家 ID:非交互运行时请先设 $env:OCTOP_AGENT_ID=' } + + # ---- 4) 解析 node 绝对路径(hook 由终端派生,非 login shell 里没有 PATH 上的 node) + $nodeBin = $env:OCTOP_NODE + if (-not $nodeBin) { + $nodeCmd = Get-Command node -ErrorAction SilentlyContinue + if ($nodeCmd) { $nodeBin = $nodeCmd.Source } + } + if (-not $nodeBin) { + $candidates = @() + if ($env:ProgramFiles) { $candidates += (Join-Path $env:ProgramFiles 'nodejs\node.exe') } + if (${env:ProgramFiles(x86)}) { $candidates += (Join-Path ${env:ProgramFiles(x86)} 'nodejs\node.exe') } + if ($env:LOCALAPPDATA) { $candidates += (Join-Path $env:LOCALAPPDATA 'Programs\nodejs\node.exe') } + foreach ($cand in $candidates) { + if (-not $nodeBin -and (Test-Path $cand)) { $nodeBin = $cand } + } + } + if (-not $nodeBin) { + $nodeBin = 'node' + Write-Host ' ! 未找到 node:hook 需要它,请先安装 Node.js' -ForegroundColor Yellow + } + else { Write-Host " · node: $nodeBin" } + + # 合并配置要用 python3;Windows 上常见的命令是 py -3 / python + $python = $null + foreach ($spec in @('py -3', 'python', 'python3')) { + $parts = $spec.Split(' ') + $exe = Get-Command $parts[0] -ErrorAction SilentlyContinue + if (-not $exe) { continue } + $pre = @() + if ($parts.Count -gt 1) { $pre = $parts[1..($parts.Count - 1)] } + & $exe.Source @pre -c 'import sys; raise SystemExit(0)' 2>$null + if ($LASTEXITCODE -eq 0) { $python = @($exe.Source) + $pre; break } + } + if (-not $python) { throw '未找到 python3:无法合并终端配置(安装 Python 3 时请勾选 Add python.exe to PATH)' } + + # Node 22.15+ 的 --use-system-ca 读系统证书库:hook 的 fetch 走企业根证书时需要它 + $useSystemCa = 0 + $nodeOk = $false + if ($nodeBin) { $nodeOk = (Test-Path $nodeBin) -or [bool](Get-Command $nodeBin -ErrorAction SilentlyContinue) } + if ($nodeOk) { + try { + & $nodeBin --use-system-ca -e 'process.exit(0)' 2>$null + if ($LASTEXITCODE -eq 0) { + $useSystemCa = 1 + Write-Host ' · node 使用系统证书库(--use-system-ca)' + } + else { + Write-Host ' ! 当前 node 不支持 --use-system-ca(需要 22.15+):企业根证书下 hook 的 fetch 可能失败' -ForegroundColor Yellow + } + } + catch { Write-Host ' ! node 无法执行:跳过端点自检,hook 可能跑不起来' -ForegroundColor Yellow } + } + + # ---- 5) 端点自检(warn-only):先确认这份 token 在该端点可用,免得装完才发现 401 + if ($nodeOk) { + $env:OCTOP_CHECK_URL = $HOOK_URL + $env:OCTOP_CHECK_TOKEN = $token + $env:OCTOP_CHECK_AGENT = $agent + $check = @' +__SELF_CHECK_JS__ +'@ + try { & $nodeBin --input-type=module -e $check } catch { } + } + + # ---- 6) 各终端安装 hook / 插件 + 合并配置(调用与 sh 安装器同一份 python 程序) + $payload = @{ + OCTOP_URL = $URL + OCTOP_HOOK_URL = $HOOK_URL + OCTOP_TOKEN = $token + OCTOP_AGENT = $agent + OCTOP_SERVER_NAME = __SERVER_NAME_LITERAL__ + OCTOP_CC_DIR = $CC_DIR + OCTOP_KIRO_DIR = $KIRO_DIR + OCTOP_DSH_DIR = $DSH_DIR + OCTOP_CODEX_DIR = $CODEX_DIR + OCTOP_CURSOR_DIR = $CURSOR_DIR + OCTOP_TERMINALS = $terminals + OCTOP_PROJECT_DIR = [string]$projectDir + OCTOP_FILES_CLAUDE_CODE = __FILES_CLAUDE_CODE_LITERAL__ + OCTOP_FILES_KIRO = __FILES_KIRO_LITERAL__ + OCTOP_FILES_CODEX = __FILES_CODEX_LITERAL__ + OCTOP_FILES_CURSOR = __FILES_CURSOR_LITERAL__ + OCTOP_FILES_NATIVE = __FILES_NATIVE_LITERAL__ + OCTOP_NODE = $nodeBin + OCTOP_USE_SYSTEM_CA = [string]$useSystemCa + OCTOP_HOOK_WRAPPER = 'cmd' + } + $envFile = [System.IO.Path]::GetTempFileName() + $mergeFile = [System.IO.Path]::GetTempFileName() + try { + $utf8 = New-Object System.Text.UTF8Encoding($false) + [System.IO.File]::WriteAllText($envFile, ($payload | ConvertTo-Json -Compress), $utf8) + $merge = @' +__MERGE_PY__ +'@ + [System.IO.File]::WriteAllText($mergeFile, $merge, $utf8) + $env:OCTOP_ENV_FILE = $envFile + $pyArgs = @() + if ($python.Count -gt 1) { $pyArgs = $python[1..($python.Count - 1)] } + $pythonExe = $python[0] + & $pythonExe @pyArgs $mergeFile + if ($LASTEXITCODE -ne 0) { throw "配置合并失败(python 退出码 $LASTEXITCODE)" } + } + finally { + Remove-Item -LiteralPath $envFile -ErrorAction SilentlyContinue + Remove-Item -LiteralPath $mergeFile -ErrorAction SilentlyContinue + } + # 别把安装器的临时环境变量留在当前 PowerShell 会话里 + Remove-Item Env:OCTOP_ENV_FILE -ErrorAction SilentlyContinue + Remove-Item Env:OCTOP_CHECK_URL, Env:OCTOP_CHECK_TOKEN, Env:OCTOP_CHECK_AGENT -ErrorAction SilentlyContinue + + Write-Host ' · 重开终端/IDE(Kiro 重载 hooks;Codex 首次需在 codex 里信任该 hook;DSH 重启后加载插件)后生效' +} +""" + + +def render_setup_ps1( + *, + mcp_url: str, + hook_url: str = "", + agent_id: str = "", + terminals: str = "", + default_token: str = "", + project: str = "", +) -> str: + """Render the Windows (PowerShell) installer — counterpart of ``render_setup_script``. + + Entry point is ``irm "/mcp/memory/setup?os=windows" | iex``. The terminals, + hook roles and the "merge config" python program are the same as the POSIX installer; + what Windows adds is: + + * interaction through ``Read-Host`` (no ``/dev/tty`` / ``stty``); + * ``.cmd`` hook wrappers (``OCTOP_HOOK_WRAPPER=cmd``) executed by the system shell, so + Git Bash is not required; + * config + manifests passed through a temp JSON (``OCTOP_ENV_FILE``) — the Windows + environment block has a hard length limit. + + ``default_token`` / ``project`` keep the same "optional prefill" meaning as in + ``render_setup_script``; non-interactive runs read ``OCTOP_PROJECT_DIR``. + """ + hook_url = hook_url or mcp_url + replacements = { + "__URL_LITERAL__": _ps_single_quote(mcp_url), + "__HOOK_URL_LITERAL__": _ps_single_quote(hook_url), + "__HOOK_URL_TEXT__": hook_url, + "__DEFAULT_AGENT_LITERAL__": _ps_single_quote(agent_id) if agent_id else "''", + "__DEFAULT_TOKEN_LITERAL__": _ps_single_quote(default_token) if default_token else "''", + "__DEFAULT_PROJECT_LITERAL__": _ps_single_quote(project) if project else "''", + "__DEFAULT_TERMINALS_LITERAL__": _ps_single_quote(terminals) if terminals else "''", + "__CC_DIR_REL__": _ps_home_rel(HOOKS_DIRS["claude-code"]), + "__KIRO_DIR_REL__": _ps_home_rel(HOOKS_DIRS["kiro"]), + "__DSH_DIR_REL__": _ps_home_rel(HOOKS_DIRS["dsh"]), + "__CODEX_DIR_REL__": _ps_home_rel(HOOKS_DIRS["codex"]), + "__CURSOR_DIR_REL__": _ps_home_rel(HOOKS_DIRS["cursor"]), + "__SERVER_NAME_LITERAL__": _ps_single_quote(_server_name(mcp_url)), + "__FILES_CLAUDE_CODE_LITERAL__": _ps_single_quote(_host_manifest("claude-code")), + "__FILES_KIRO_LITERAL__": _ps_single_quote(_host_manifest("kiro")), + "__FILES_CODEX_LITERAL__": _ps_single_quote(_host_manifest("codex")), + "__FILES_CURSOR_LITERAL__": _ps_single_quote(_host_manifest("cursor")), + "__FILES_NATIVE_LITERAL__": _ps_single_quote(_dsh_native_manifest()), + "__SELF_CHECK_JS__": _PS_SELF_CHECK_JS, + "__MERGE_PY__": _MERGE_PY, + } + script = _SETUP_PS_TEMPLATE + for placeholder, value in replacements.items(): + script = script.replace(placeholder, value) + return script + + +_UNINSTALL_PS_TEMPLATE = r"""# octop memory — Windows 卸载本地终端接入(与 /mcp/memory/setup 相对;可重复执行) +# 用法(PowerShell):irm "/mcp/memory/uninstall?os=windows" | iex +# 非交互:先设 $env:OCTOP_TERMINALS=cursor +if ($PSVersionTable.PSVersion.Major -lt 5) { throw '需要 PowerShell 5.1 或更高版本' } +& { + $ErrorActionPreference = 'Stop' + + $DEFAULT_TERMINALS = __DEFAULT_TERMINALS_LITERAL__ + $DEFAULT_PROJECT = __DEFAULT_PROJECT_LITERAL__ + $CC_DIR = Join-Path $HOME '__CC_DIR_REL__' + $KIRO_DIR = Join-Path $HOME '__KIRO_DIR_REL__' + $DSH_DIR = Join-Path $HOME '__DSH_DIR_REL__' + $CODEX_DIR = Join-Path $HOME '__CODEX_DIR_REL__' + $CURSOR_DIR = Join-Path $HOME '__CURSOR_DIR_REL__' + + $interactive = $true + try { $interactive = -not [Console]::IsInputRedirected } catch { $interactive = $true } + + function Test-Terminal([string]$Name) { + return [bool](Get-Command $Name -ErrorAction SilentlyContinue) + } + + $haveClaude = (Test-Path (Join-Path $HOME '.claude.json')) -or (Test-Path (Join-Path $HOME '.claude')) -or (Test-Terminal 'claude') + $haveKiro = (Test-Path (Join-Path $HOME '.kiro')) -or (Test-Terminal 'kiro') + $haveDsh = (Test-Path (Join-Path $HOME '.dsh')) -or (Test-Terminal 'dsh') + $haveCodex = (Test-Path (Join-Path $HOME '.codex')) -or (Test-Terminal 'codex') + $haveCursor = (Test-Path (Join-Path $HOME '.cursor')) -or (Test-Terminal 'cursor') + + $slotName = @{ 1 = 'claude-code'; 2 = 'kiro'; 3 = 'dsh'; 4 = 'codex'; 5 = 'cursor' } + $slotDetected = @{ 1 = $haveClaude; 2 = $haveKiro; 3 = $haveDsh; 4 = $haveCodex; 5 = $haveCursor } + $slotHint = @{ + 1 = 'Claude Code (清 ~/.claude/hooks-events + settings/MCP)' + 2 = 'Kiro (清 ~/.kiro/hooks + MCP)' + 3 = 'DSH (清原生插件 + profile 登记 + ~/.dsh/hooks 桥接/preset)' + 4 = 'Codex (清 ~/.codex/hooks + config.toml 中的 octop hooks)' + 5 = 'Cursor (清 ~/.cursor/hooks + hooks.json)' + } + $detected = @() + foreach ($key in 1, 2, 3, 4, 5) { if ($slotDetected[$key]) { $detected += $slotName[$key] } } + if ($detected.Count -eq 0) { $detected = @('cursor') } + + Write-Host 'octop memory 卸载' + Write-Host '检测到:' + foreach ($key in 1, 2, 3, 4, 5) { + if ($slotDetected[$key]) { Write-Host (" {0}) {1}" -f $key, $slotHint[$key]) } + } + + $terminals = $env:OCTOP_TERMINALS + if (-not $terminals) { $terminals = $DEFAULT_TERMINALS } + if (-not $terminals -and $interactive) { + $attempt = 0 + while (-not $terminals -and $attempt -lt 3) { + $attempt++ + $pick = Read-Host '要卸载哪些?(可多选:1,2 / 1 3 / 12 / a=全部,回车=全部检测到的)' + $chosen = @() + if (-not $pick -or $pick -match '^(?i)(a|all|全部)$') { + $chosen = @($detected) + } + else { + $tokens = @() + foreach ($token in ($pick -replace '[,\s]+', ' ').Split(' ')) { + if (-not $token) { continue } + if ($token -match '^[0-9]{2,}$') { + foreach ($char in $token.ToCharArray()) { $tokens += [string]$char } + } + else { $tokens += $token } + } + foreach ($token in $tokens) { + $index = 0 + if (-not [int]::TryParse($token, [ref]$index) -or -not $slotDetected[$index]) { + Write-Host " ? 已忽略:$token" + continue + } + $name = $slotName[$index] + if ($chosen -notcontains $name) { $chosen += $name } + } + } + if ($chosen.Count -gt 0) { $terminals = ($chosen -join ',') } + else { Write-Host ' ! 没有选中任何终端,请重选(1-5,或 a=全部)' -ForegroundColor Yellow } + } + } + if (-not $terminals) { throw '没有可卸载的终端:非交互请先设 $env:OCTOP_TERMINALS=cursor' } + + # ---- 卸载范围:全局(默认)还是只清某个项目里的注册 + # ?project= 预填只在**交互式**生效(变成默认答案);非交互只认 OCTOP_PROJECT_DIR。 + # 显式设置(哪怕是空串)就以环境变量为准:空串 = 全局,不再交互提问。 + $projectSet = $null -ne $env:OCTOP_PROJECT_DIR + $projectDir = '' + if ($projectSet) { $projectDir = $env:OCTOP_PROJECT_DIR } + if (-not $projectSet -and $interactive) { + Write-Host '卸载范围:' + Write-Host ' 1) 全局(默认)' + Write-Host ' 2) 只清某个项目里的注册(用户目录里的脚本保留)' + $defaultScope = '1' + if ($DEFAULT_PROJECT) { $defaultScope = '2' } + $scope = Read-Host "选择 [1/2](回车=$defaultScope)" + if (-not $scope) { $scope = $defaultScope } + if ($scope -in @('2', 'p', 'P', 'project', '项目')) { + $defaultProject = $DEFAULT_PROJECT + if (-not $defaultProject) { $defaultProject = (Get-Location).Path } + $projectDir = Read-Host "项目根目录 [$defaultProject]" + if (-not $projectDir) { $projectDir = $defaultProject } + } + } + if ($projectDir) { + if ($projectDir -eq '~' -or $projectDir -eq '~/' -or $projectDir -eq '~\') { $projectDir = $HOME } + elseif ($projectDir -match '^~[\\/]') { $projectDir = Join-Path $HOME $projectDir.Substring(2) } + if (-not (Test-Path -LiteralPath $projectDir -PathType Container)) { throw "项目目录不存在(或不是目录):$projectDir" } + $projectDir = (Resolve-Path -LiteralPath $projectDir).Path + Write-Host " · 卸载范围:项目级 → $projectDir(用户目录里的 hook 脚本保留)" + } + + $python = $null + foreach ($spec in @('py -3', 'python', 'python3')) { + $parts = $spec.Split(' ') + $exe = Get-Command $parts[0] -ErrorAction SilentlyContinue + if (-not $exe) { continue } + $pre = @() + if ($parts.Count -gt 1) { $pre = $parts[1..($parts.Count - 1)] } + & $exe.Source @pre -c 'import sys; raise SystemExit(0)' 2>$null + if ($LASTEXITCODE -eq 0) { $python = @($exe.Source) + $pre; break } + } + if (-not $python) { throw '未找到 python3:无法清理终端配置' } + + $payload = @{ + OCTOP_CC_DIR = $CC_DIR + OCTOP_KIRO_DIR = $KIRO_DIR + OCTOP_DSH_DIR = $DSH_DIR + OCTOP_CODEX_DIR = $CODEX_DIR + OCTOP_CURSOR_DIR = $CURSOR_DIR + OCTOP_TERMINALS = $terminals + OCTOP_PROJECT_DIR = [string]$projectDir + } + $envFile = [System.IO.Path]::GetTempFileName() + $scriptFile = [System.IO.Path]::GetTempFileName() + try { + $utf8 = New-Object System.Text.UTF8Encoding($false) + [System.IO.File]::WriteAllText($envFile, ($payload | ConvertTo-Json -Compress), $utf8) + $code = @' +__UNINSTALL_PY__ +'@ + [System.IO.File]::WriteAllText($scriptFile, $code, $utf8) + $env:OCTOP_ENV_FILE = $envFile + $pyArgs = @() + if ($python.Count -gt 1) { $pyArgs = $python[1..($python.Count - 1)] } + $pythonExe = $python[0] + & $pythonExe @pyArgs $scriptFile + if ($LASTEXITCODE -ne 0) { throw "卸载失败(python 退出码 $LASTEXITCODE)" } + } + finally { + Remove-Item -LiteralPath $envFile -ErrorAction SilentlyContinue + Remove-Item -LiteralPath $scriptFile -ErrorAction SilentlyContinue + } + Remove-Item Env:OCTOP_ENV_FILE -ErrorAction SilentlyContinue +} +""" + + +def render_uninstall_ps1(*, terminals: str = "", project: str = "") -> str: + """Render the Windows uninstaller (counterpart of ``render_uninstall_script``). + + ``project`` is the **prefill** for a project-scoped uninstall; non-interactive runs read + ``OCTOP_PROJECT_DIR`` (an explicitly empty value = global). + """ + replacements = { + "__DEFAULT_TERMINALS_LITERAL__": _ps_single_quote(terminals) if terminals else "''", + "__DEFAULT_PROJECT_LITERAL__": _ps_single_quote(project) if project else "''", + "__CC_DIR_REL__": _ps_home_rel(HOOKS_DIRS["claude-code"]), + "__KIRO_DIR_REL__": _ps_home_rel(HOOKS_DIRS["kiro"]), + "__DSH_DIR_REL__": _ps_home_rel(HOOKS_DIRS["dsh"]), + "__CODEX_DIR_REL__": _ps_home_rel(HOOKS_DIRS["codex"]), + "__CURSOR_DIR_REL__": _ps_home_rel(HOOKS_DIRS["cursor"]), + "__UNINSTALL_PY__": _UNINSTALL_PY, + } + script = _UNINSTALL_PS_TEMPLATE + for placeholder, value in replacements.items(): + script = script.replace(placeholder, value) + return script diff --git a/src/octop/infra/agents/hook_scripts/claude-code/stop-capture.mjs b/src/octop/infra/agents/hook_scripts/claude-code/stop-capture.mjs new file mode 100644 index 0000000000..1e01f76cf1 --- /dev/null +++ b/src/octop/infra/agents/hook_scripts/claude-code/stop-capture.mjs @@ -0,0 +1,114 @@ +#!/usr/bin/env node +// Claude Code ``Stop`` hook: ship this session's transcript into octop memory. +// +// Claude-Code-specific in exactly two ways: the payload carries +// ``transcript_path``, and ``Stop`` fires *before* the current turn is flushed — +// so the hook hands the work to a detached worker that polls until an assistant +// record shows up after the last user record. The hook itself returns at once. +import { spawn } from "node:child_process"; +import fs from "node:fs"; +import { fileURLToPath } from "node:url"; +import { capture, isConfigured, loadConfig, makeLogger, sleep } from "../lib/octop-memory-client.mjs"; + +const log = makeLogger("stopcap.claude-code"); +const DELAY_MS = Number(process.env.OCTOP_CAPTURE_DELAY_MS ?? 3000); +const POLL_MS = Number(process.env.OCTOP_CAPTURE_POLL_MS ?? 1000); +const MAX_WAIT_MS = Number(process.env.OCTOP_CAPTURE_MAX_WAIT_MS ?? 20000); +const IS_WORKER = process.argv.includes("--worker"); + +let raw = ""; +if (IS_WORKER) { + raw = Buffer.from(process.env.OCTOP_HOOK_PAYLOAD_B64 || "", "base64").toString("utf8"); +} else { + try { + raw = fs.readFileSync(0, "utf8"); + } catch { + raw = ""; + } + if (process.env.OCTOP_CAPTURE_SYNC !== "1") { + try { + const worker = spawn(process.execPath, [fileURLToPath(import.meta.url), "--worker"], { + detached: true, + stdio: "ignore", + env: { ...process.env, OCTOP_HOOK_PAYLOAD_B64: Buffer.from(raw).toString("base64") }, + }); + worker.unref(); + log(`deferred: 交给后台进程,等本轮落盘(${DELAY_MS}ms 起轮询)`); + process.exit(0); // the hook returns at once; the worker waits, then uploads + } catch (exc) { + log(`detach failed, running inline: ${exc}`); + } + } +} +let hookInput = {}; +try { + if (raw.trim()) hookInput = JSON.parse(raw); +} catch { + log("stdin-not-json"); +} + +const sessionId = hookInput.session_id || ""; +const event = hookInput.hook_event_name || "Stop"; +const transcriptPath = hookInput.transcript_path || ""; +if (!transcriptPath) { + log(`${event} skip: payload has no transcript_path`); + process.exit(0); +} + +/** The turn landed when an assistant record follows the last user record. */ +function turnLanded(path) { + try { + let lastUser = -1; + let lastAssistant = -1; + const lines = fs.readFileSync(path, "utf8").split("\n"); + lines.forEach((line, index) => { + if (!line.trim()) return; + let record; + try { + record = JSON.parse(line); + } catch { + return; + } + if (record.type === "user") lastUser = index; + else if (record.type === "assistant") lastAssistant = index; + }); + return lastAssistant > lastUser; + } catch { + return true; // unreadable -> do not wait + } +} + +if (IS_WORKER) { + if (DELAY_MS > 0) await sleep(DELAY_MS); + const deadline = Date.now() + MAX_WAIT_MS; + let waited = 0; + while (Date.now() < deadline && !turnLanded(transcriptPath)) { + await sleep(POLL_MS); + waited += POLL_MS; + } + if (waited) log(`${event} waited ${waited}ms for the turn to land`); +} + +const config = loadConfig(); +if (!isConfigured(config)) { + log("skip: not configured (need OCTOP_MEMORY_MCP_URL / _TOKEN / _AGENT_ID or config.json)"); + process.exit(0); +} +let buffer; +try { + buffer = fs.readFileSync(transcriptPath); +} catch (exc) { + log(`${event} read failed: ${exc} (${transcriptPath})`); + process.exit(0); +} +await capture({ + config, + log, + event, + source: "claude-code", + sessionId, + transcriptPath, + buffer, + sessionPrefix: "cc:", +}); +process.exit(0); diff --git a/src/octop/infra/agents/hook_scripts/claude-code/user-prompt-recall.mjs b/src/octop/infra/agents/hook_scripts/claude-code/user-prompt-recall.mjs new file mode 100644 index 0000000000..4db3d50999 --- /dev/null +++ b/src/octop/infra/agents/hook_scripts/claude-code/user-prompt-recall.mjs @@ -0,0 +1,51 @@ +#!/usr/bin/env node +// Claude Code ``UserPromptSubmit`` hook: recall before the model answers. +// +// ``mcp_tool`` hooks are silently skipped for UserPromptSubmit (2.1.263), so this +// is a command hook that calls ``memory_search`` itself and prints the block — +// Claude Code injects the stdout as extra context for this turn. +import fs from "node:fs"; +import { callTool, isConfigured, loadConfig, makeLogger } from "../lib/octop-memory-client.mjs"; + +const log = makeLogger("urecall.claude-code"); +const MARKER = "[octop memory search]"; +const payload = (() => { + try { + const raw = fs.readFileSync(0, "utf8"); + return raw.trim() ? JSON.parse(raw) : {}; + } catch { + return {}; + } +})(); + +async function main() { + const prompt = String(payload.prompt || "").trim(); + if (!prompt) { + log("skip: empty prompt"); + return; + } + const config = loadConfig(); + if (!isConfigured(config)) { + log("skip: not configured"); + return; + } + const maxResults = Number(process.env.OCTOP_RECALL_MAX_RESULTS ?? 5); + const result = await callTool(config, "memory_search", { + query: prompt, + corpus: "atom", + max_results: maxResults, + }); + const hits = result.detail?.hits ?? []; + log(`prompt="${prompt.slice(0, 60)}" hits=${hits.length} http=${result.status}`); + if (!hits.length) return; + const lines = [MARKER]; + for (const hit of hits) { + const when = String(hit.occurred_at || "").slice(0, 19); + lines.push(`- [${hit.layer}] (${hit.role_hint ?? hit.layer} @ ${when}) ${hit.snippet}`); + } + lines.push("若需要原文/证据,用 memory_get(path=…) 读全文;与本次工作无关则忽略。"); + process.stdout.write(lines.join("\n") + "\n"); +} + +await main(); +process.exit(0); diff --git a/src/octop/infra/agents/hook_scripts/codex/stop-capture.mjs b/src/octop/infra/agents/hook_scripts/codex/stop-capture.mjs new file mode 100644 index 0000000000..8f89a97b1d --- /dev/null +++ b/src/octop/infra/agents/hook_scripts/codex/stop-capture.mjs @@ -0,0 +1,102 @@ +#!/usr/bin/env node +// Codex CLI ``Stop`` hook: ship this session's rollout transcript into octop memory. +// +// Codex-specific: hooks are declared in ``~/.codex/config.toml`` +// (``[[hooks.Stop]]`` with ``type = "command"``), the payload carries +// ``transcript_path`` (the ``rollout-*.jsonl`` under ``~/.codex/sessions``) and +// ``session_id``, and — like Claude Code — Stop can fire before the turn is +// flushed, so the hook defers to a detached worker and waits for the file to stop +// growing. +import { spawn } from "node:child_process"; +import fs from "node:fs"; +import { fileURLToPath } from "node:url"; +import { capture, isConfigured, loadConfig, makeLogger, sleep } from "../lib/octop-memory-client.mjs"; + +const log = makeLogger("stopcap.codex"); +const DELAY_MS = Number(process.env.OCTOP_CAPTURE_DELAY_MS ?? 3000); +const POLL_MS = Number(process.env.OCTOP_CAPTURE_POLL_MS ?? 1000); +const MAX_WAIT_MS = Number(process.env.OCTOP_CAPTURE_MAX_WAIT_MS ?? 20000); +const IS_WORKER = process.argv.includes("--worker"); + +let raw = ""; +if (IS_WORKER) { + raw = Buffer.from(process.env.OCTOP_HOOK_PAYLOAD_B64 || "", "base64").toString("utf8"); +} else { + try { + raw = fs.readFileSync(0, "utf8"); + } catch { + raw = ""; + } + if (process.env.OCTOP_CAPTURE_SYNC !== "1") { + try { + const worker = spawn(process.execPath, [fileURLToPath(import.meta.url), "--worker"], { + detached: true, + stdio: "ignore", + env: { ...process.env, OCTOP_HOOK_PAYLOAD_B64: Buffer.from(raw).toString("base64") }, + }); + worker.unref(); + log(`deferred: 交给后台进程,等本轮落盘(${DELAY_MS}ms 起轮询)`); + process.exit(0); + } catch (exc) { + log(`detach failed, running inline: ${exc}`); + } + } +} +let hookInput = {}; +try { + if (raw.trim()) hookInput = JSON.parse(raw); +} catch { + log("stdin-not-json"); +} + +const sessionId = hookInput.session_id || ""; +const event = hookInput.hook_event_name || "Stop"; +const transcriptPath = hookInput.transcript_path || ""; +if (!transcriptPath) { + log(`${event} skip: payload has no transcript_path`); + process.exit(0); +} + +if (IS_WORKER) { + if (DELAY_MS > 0) await sleep(DELAY_MS); + const deadline = Date.now() + MAX_WAIT_MS; + let previousSize = -1; + let waited = 0; + while (Date.now() < deadline) { + let size; + try { + size = fs.statSync(transcriptPath).size; + } catch { + break; + } + if (size === previousSize) break; + previousSize = size; + await sleep(POLL_MS); + waited += POLL_MS; + } + if (waited) log(`${event} waited ${waited}ms for the rollout to settle`); +} + +const config = loadConfig(); +if (!isConfigured(config)) { + log("skip: not configured"); + process.exit(0); +} +let buffer; +try { + buffer = fs.readFileSync(transcriptPath); +} catch (exc) { + log(`${event} read failed: ${exc} (${transcriptPath})`); + process.exit(0); +} +await capture({ + config, + log, + event, + source: "codex", + sessionId, + transcriptPath, + buffer, + sessionPrefix: "codex:", +}); +process.exit(0); diff --git a/src/octop/infra/agents/hook_scripts/codex/user-prompt-recall.mjs b/src/octop/infra/agents/hook_scripts/codex/user-prompt-recall.mjs new file mode 100644 index 0000000000..7e657a65f6 --- /dev/null +++ b/src/octop/infra/agents/hook_scripts/codex/user-prompt-recall.mjs @@ -0,0 +1,55 @@ +#!/usr/bin/env node +// Codex CLI ``UserPromptSubmit`` hook: recall before the model answers. +// +// Codex hook output understands ``additionalContext`` (same contract as Claude +// Code), so the recalled block is injected for this turn. +import fs from "node:fs"; +import { callTool, isConfigured, loadConfig, makeLogger } from "../lib/octop-memory-client.mjs"; + +const log = makeLogger("urecall.codex"); +const MARKER = "[octop memory search]"; + +async function main() { + let payload = {}; + try { + const raw = fs.readFileSync(0, "utf8"); + if (raw.trim()) payload = JSON.parse(raw); + } catch { + log("stdin-not-json"); + } + const prompt = String(payload.prompt || "").trim(); + if (!prompt) { + log("skip: empty prompt"); + return; + } + const config = loadConfig(); + if (!isConfigured(config)) { + log("skip: not configured"); + return; + } + const result = await callTool(config, "memory_search", { + query: prompt, + corpus: "atom", + max_results: Number(process.env.OCTOP_RECALL_MAX_RESULTS ?? 5), + }); + const hits = result.detail?.hits ?? []; + log(`prompt="${prompt.slice(0, 60)}" hits=${hits.length} http=${result.status}`); + if (!hits.length) return; + const lines = [MARKER]; + for (const hit of hits) { + const when = String(hit.occurred_at || "").slice(0, 19); + lines.push(`- [${hit.layer}] (${hit.role_hint ?? hit.layer} @ ${when}) ${hit.snippet}`); + } + lines.push("若需要原文/证据,用 memory_get(path=…) 读全文;与本次工作无关则忽略。"); + process.stdout.write( + JSON.stringify({ + hookSpecificOutput: { + hookEventName: "UserPromptSubmit", + additionalContext: lines.join("\n"), + }, + }) + "\n", + ); +} + +await main(); +process.exit(0); diff --git a/src/octop/infra/agents/hook_scripts/cursor/stop-capture.mjs b/src/octop/infra/agents/hook_scripts/cursor/stop-capture.mjs new file mode 100644 index 0000000000..de38e7d0e2 --- /dev/null +++ b/src/octop/infra/agents/hook_scripts/cursor/stop-capture.mjs @@ -0,0 +1,65 @@ +#!/usr/bin/env node +// Cursor ``stop`` hook: ship this session's transcript into octop memory. +// +// Cursor-specific: hooks live in ``~/.cursor/hooks.json`` (``{"version":1, +// "hooks":{"stop":[{"command":…}]}}``) and the payload carries ``transcript_path`` +// plus ``conversation_id`` / ``session_id``. That path is +// ``~/.cursor/projects//agent-transcripts//.jsonl`` +// (one JSON object per line: ``{role, message:{content:[{type,text}]}}``). +// ``~/.cursor/chats/**/store.db`` is protobuf and is not read — if a payload has +// no ``transcript_path``, this logs and skips instead of guessing. +import fs from "node:fs"; +import { capture, isConfigured, loadConfig, makeLogger, readStdinJson, sleep } from "../lib/octop-memory-client.mjs"; + +const log = makeLogger("stopcap.cursor"); +const hookInput = readStdinJson(log); +const event = hookInput.hook_event_name || "stop"; +const sessionId = hookInput.conversation_id || hookInput.session_id || ""; +const transcriptPath = hookInput.transcript_path || ""; +if (!transcriptPath) { + log(`${event} skip: payload has no transcript_path (session=${sessionId})`); + process.exit(0); +} + +const POLL_MS = Number(process.env.OCTOP_CAPTURE_POLL_MS ?? 1000); +const MAX_WAIT_MS = Number(process.env.OCTOP_CAPTURE_MAX_WAIT_MS ?? 20000); +const deadline = Date.now() + MAX_WAIT_MS; +let previousSize = -1; +let waited = 0; +while (Date.now() < deadline) { + let size; + try { + size = fs.statSync(transcriptPath).size; + } catch { + break; + } + if (size === previousSize) break; + previousSize = size; + await sleep(POLL_MS); + waited += POLL_MS; +} +if (waited) log(`${event} waited ${waited}ms for the transcript to settle`); + +const config = loadConfig(); +if (!isConfigured(config)) { + log("skip: not configured"); + process.exit(0); +} +let buffer; +try { + buffer = fs.readFileSync(transcriptPath); +} catch (exc) { + log(`${event} read failed: ${exc} (${transcriptPath})`); + process.exit(0); +} +await capture({ + config, + log, + event, + source: "cursor", + sessionId, + transcriptPath, + buffer, + sessionPrefix: "cursor:", +}); +process.exit(0); diff --git a/src/octop/infra/agents/hook_scripts/cursor/user-prompt-recall.mjs b/src/octop/infra/agents/hook_scripts/cursor/user-prompt-recall.mjs new file mode 100644 index 0000000000..8b662856e4 --- /dev/null +++ b/src/octop/infra/agents/hook_scripts/cursor/user-prompt-recall.mjs @@ -0,0 +1,56 @@ +#!/usr/bin/env node +// Cursor ``beforeSubmitPrompt`` hook: recall before the model answers. +// +// Cursor's payload carries ``prompt`` (and ``conversation_id``/``cwd``); its hook +// output understands ``additionalContext``, so the recalled block is injected for +// this turn. +import fs from "node:fs"; +import { callTool, isConfigured, loadConfig, makeLogger } from "../lib/octop-memory-client.mjs"; + +const log = makeLogger("urecall.cursor"); +const MARKER = "[octop memory search]"; + +async function main() { + let payload = {}; + try { + const raw = fs.readFileSync(0, "utf8"); + if (raw.trim()) payload = JSON.parse(raw); + } catch { + log("stdin-not-json"); + } + const prompt = String(payload.prompt || payload.user_message || "").trim(); + if (!prompt) { + log("skip: empty prompt"); + return; + } + const config = loadConfig(); + if (!isConfigured(config)) { + log("skip: not configured"); + return; + } + const result = await callTool(config, "memory_search", { + query: prompt, + corpus: "atom", + max_results: Number(process.env.OCTOP_RECALL_MAX_RESULTS ?? 5), + }); + const hits = result.detail?.hits ?? []; + log(`prompt="${prompt.slice(0, 60)}" hits=${hits.length} http=${result.status}`); + if (!hits.length) return; + const lines = [MARKER]; + for (const hit of hits) { + const when = String(hit.occurred_at || "").slice(0, 19); + lines.push(`- [${hit.layer}] (${hit.role_hint ?? hit.layer} @ ${when}) ${hit.snippet}`); + } + lines.push("若需要原文/证据,用 memory_get(path=…) 读全文;与本次工作无关则忽略。"); + process.stdout.write( + JSON.stringify({ + hookSpecificOutput: { + hookEventName: "beforeSubmitPrompt", + additionalContext: lines.join("\n"), + }, + }) + "\n", + ); +} + +await main(); +process.exit(0); diff --git a/src/octop/infra/agents/hook_scripts/dsh/memory-target.mjs b/src/octop/infra/agents/hook_scripts/dsh/memory-target.mjs new file mode 100644 index 0000000000..b04e753ffd --- /dev/null +++ b/src/octop/infra/agents/hook_scripts/dsh/memory-target.mjs @@ -0,0 +1,87 @@ +#!/usr/bin/env node +// DSH-only target resolution for the memory hooks. +// +// The installer writes `config.json` next to the scripts, but a DSH whose MCP +// panel already knows the octop server needs no second copy of the credentials. +// Precedence: environment -> `config.json` -> the `octop-memory*` entry of this +// DSH's own MCP server table (`$DSH_HOME/storages/mcp_servers.json`), enabled +// servers first, then prod, local, oauth. `OCTOP_MEMORY_SERVER` names one entry +// explicitly and may pick a disabled one. +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import { loadConfig } from "../lib/octop-memory-client.mjs"; + +const ENV_VAR_NAME = /^[A-Z][A-Z0-9_]*$/; + +function serverTablePath() { + const home = process.env.DSH_HOME || path.join(os.homedir(), ".dsh"); + return path.join(home, "storages", "mcp_servers.json"); +} + +/** A bare env-var name resolves from the environment. */ +function headerValue(value) { + const text = String(value ?? "").trim(); + if (ENV_VAR_NAME.test(text) && process.env[text]) return process.env[text]; + return text; +} + +function headerMap(server) { + const headers = {}; + for (const item of Array.isArray(server.headers) ? server.headers : []) { + if (item && typeof item.name === "string" && item.name) headers[item.name] = headerValue(item.value); + } + return headers; +} + +function rank(server) { + const name = String(server.serverName ?? "").toLowerCase(); + if (name === "octop-memory-prod") return 10; + if (name === "octop-memory" || name === "octop-memory-local") return 20; + if (name === "octop-memory-oauth") return 30; + return 40; +} + +function configFromServer(server) { + const headers = headerMap(server); + const names = Object.keys(headers); + const tokenHeader = + names.find((name) => name.toLowerCase() === "authorization") ?? + names.find((name) => name.toLowerCase() === "x-octop-memory-token") ?? + ""; + return { + endpoint: String(server.url ?? "").trim(), + token: tokenHeader ? headers[tokenHeader].replace(/^Bearer\s+/i, "") : "", + tokenHeader, + agentId: headers["X-Octop-Agent-Id"] ?? "", + }; +} + +/** The usable octop entry of this DSH's MCP server table, or null. */ +export function discoverConfig() { + let servers; + try { + const parsed = JSON.parse(fs.readFileSync(serverTablePath(), "utf8")); + servers = Object.values(parsed?.tables?.servers ?? {}); + } catch { + return null; // no DSH storage here -> nothing to discover + } + const wanted = (process.env.OCTOP_MEMORY_SERVER || "").trim().toLowerCase(); + const candidates = servers + .filter((server) => server && typeof server === "object" && !server.stale) + .filter((server) => /^octop[-_]?memory/i.test(String(server.serverName ?? ""))) + .filter((server) => !wanted || String(server.serverName ?? "").toLowerCase() === wanted) + .sort((a, b) => (wanted ? 0 : Number(!a.enabled) - Number(!b.enabled)) || rank(a) - rank(b)); + for (const server of candidates) { + const config = configFromServer(server); + if (config.endpoint && config.token) return config; + } + return null; +} + +/** What these hooks should talk to. */ +export function resolveConfig() { + const configured = loadConfig(); + if (configured.endpoint && configured.token) return configured; + return discoverConfig() ?? configured; +} diff --git a/src/octop/infra/agents/hook_scripts/dsh/stop-capture.mjs b/src/octop/infra/agents/hook_scripts/dsh/stop-capture.mjs new file mode 100644 index 0000000000..1c24326e73 --- /dev/null +++ b/src/octop/infra/agents/hook_scripts/dsh/stop-capture.mjs @@ -0,0 +1,223 @@ +#!/usr/bin/env node +// DSH ``Stop`` hook (through the hooks bridge): ship this session's +// ``session*.jsonl.zstd`` into octop memory. +// +// Two shipping modes, chosen by probing the endpoint (force one with +// ``OCTOP_CAPTURE_MODE=text|transcript``): +// +// text ``memory_capture`` takes ``assistant`` -> one call per turn with +// the roles in their own fields, so the assistant's answer is not +// stored as a user message. +// transcript no ``assistant`` field -> gzip the raw JSONL delta and let the +// server parse typed events (its DSH transcript path). +// +// DSH-specific: the payload has only ``session_id``, the file is versioned +// (``session.jsonl.zstd`` -> ``session.v3.jsonl.zstd`` -> …) so the newest one +// inside the session directory is used, and it is zstd-compressed — decoded here +// so the server needs no zstd dependency. +import fs from "node:fs"; +import os from "node:os"; +import { + capture, + captureTurns, + decompressZstdContainer, + isConfigured, + loadState, + makeLogger, + newestFile, + readStdinJson, + saveState, + sleep, + supportsAssistant, +} from "../lib/octop-memory-client.mjs"; +import { resolveConfig } from "./memory-target.mjs"; + +const log = makeLogger("stopcap.dsh"); +const event = "Stop"; +const sessionId = readStdinJson(log).session_id || ""; +if (!sessionId) { + log(`${event} skip: payload has no session_id`); + process.exit(0); +} + +const root = process.env.OCTOP_DSH_SESSIONS_ROOT || `${os.homedir()}/.dsh/sessions`; +function findSessionFile(id) { + try { + for (const entry of fs.readdirSync(root, { withFileTypes: true })) { + if (!entry.isDirectory()) continue; + const found = newestFile(`${root}/${entry.name}/${id}`, /^session.*\.jsonl\.zstd$/); + if (found) return found; + } + } catch { + // no DSH install / unreadable root + } + return ""; +} + +const transcriptPath = findSessionFile(sessionId); +if (!transcriptPath) { + log(`${event} skip: no transcript for ${sessionId} under ${root}`); + process.exit(0); +} + +const POLL_MS = Number(process.env.OCTOP_CAPTURE_POLL_MS ?? 1000); +const MAX_WAIT_MS = Number(process.env.OCTOP_CAPTURE_MAX_WAIT_MS ?? 20000); +const deadline = Date.now() + MAX_WAIT_MS; +let previousSize = -1; +let waited = 0; +while (Date.now() < deadline) { + let size; + try { + size = fs.statSync(transcriptPath).size; + } catch { + break; + } + if (size === previousSize) break; + previousSize = size; + await sleep(POLL_MS); + waited += POLL_MS; +} +if (waited) log(`${event} waited ${waited}ms for the file to settle`); + +const config = resolveConfig(); +if (!isConfigured(config)) { + log("skip: not configured"); + process.exit(0); +} +let buffer; +try { + buffer = decompressZstdContainer(fs.readFileSync(transcriptPath)); +} catch (exc) { + log(`${event} zstd decompress failed: ${exc} (${transcriptPath})`); + process.exit(0); +} + +// --- transcript mode: the server parses the records into typed events --- +const forced = (process.env.OCTOP_CAPTURE_MODE || "auto").trim().toLowerCase(); +const asTurns = + forced === "text" ? true : forced === "transcript" ? false : await supportsAssistant(config); +if (!asTurns) { + await capture({ + config, + log, + event, + source: "dsh", + sessionId, + transcriptPath, + buffer, + sessionPrefix: "dsh:", + }); + process.exit(0); +} + +// --- text mode: one call per turn, roles in their own fields --- + +function textBlocks(content) { + if (!Array.isArray(content)) return ""; + return content + .filter((block) => block && block.type === "text" && typeof block.text === "string") + .map((block) => block.text.trim()) + .filter(Boolean) + .join("\n"); +} + +/** Bytes of ``buffer`` that end on a complete JSONL record; a torn tail waits for the next Stop. */ +function completeRecordsEnd() { + const lastNewline = buffer.lastIndexOf(0x0a); + if (lastNewline + 1 >= buffer.length) return lastNewline + 1; + const tail = buffer.subarray(lastNewline + 1).toString("utf8").trim(); + if (!tail) return buffer.length; + try { + JSON.parse(tail); + return buffer.length; + } catch { + return lastNewline + 1; + } +} + +/** + * Group the delta into turns with each turn's end offset, so the offset advances + * one shipped turn at a time. A ``user/message`` starts a turn only when its + * source kind is ``user`` — plugin snapshots, system reminders and hook + * injections share that record type; ``assistant/message`` keeps text blocks only. + */ +function turnBatches(start, end) { + const batches = []; + const closeTurn = (offset) => { + if (batches.length) batches[batches.length - 1].endOffset = offset; + }; + let cursor = start; + while (cursor < end) { + const newline = buffer.indexOf(0x0a, cursor); + const lineEnd = newline === -1 || newline >= end ? end : newline + 1; + const line = buffer + .subarray(cursor, lineEnd - (lineEnd === newline + 1 ? 1 : 0)) + .toString("utf8"); + const lineStart = cursor; + cursor = lineEnd; + if (!line.trim()) continue; + let record; + try { + record = JSON.parse(line); + } catch { + continue; // torn or non-JSON line + } + if (record?.type === "user/message") { + const kind = String(record?.data?.source?.kind || ""); + if (kind && kind !== "user") continue; + closeTurn(lineStart); + const text = textBlocks(record?.data?.content); + batches.push({ user: text ? [text] : [], assistant: [], endOffset: end }); + } else if (record?.type === "assistant/message") { + const text = textBlocks(record?.data?.message?.content); + if (!text) continue; + if (!batches.length) batches.push({ user: [], assistant: [], endOffset: end }); + batches[batches.length - 1].assistant.push(text); + } + } + closeTurn(end); + return batches.filter((batch) => batch.user.length || batch.assistant.length); +} + +const state = loadState(); +const previous = state[sessionId]; +let start = 0; +if (previous && previous.path === transcriptPath && previous.offset <= buffer.length) { + start = previous.offset; + // Only a boundary that still follows a newline is safe to resume from; a + // rewritten/compacted file falls back to a full re-send. + if (start > 0 && start < buffer.length && buffer[start - 1] !== 0x0a) start = 0; +} +const end = completeRecordsEnd(); +if (end <= start) { + log(`${event} session=${sessionId} no new records (offset=${start}/${end}) -> skip`); + process.exit(0); +} +let turns = turnBatches(start, end); +const maxTurns = Number(process.env.OCTOP_CAPTURE_MAX_TURNS ?? 25); +if (turns.length > maxTurns) turns = turns.slice(0, maxTurns); + +if (!turns.length) { + // Tool traffic only: consume it so the next Stop starts after it. + state[sessionId] = { path: transcriptPath, offset: end }; + saveState(state); + log(`${event} session=${sessionId} no user/assistant text in ${end - start} bytes -> offset advanced`); + process.exit(0); +} + +const result = await captureTurns({ + config, + log, + event, + source: "dsh", + sessionId, + transcriptPath, + turns, + sessionPrefix: "dsh:", + startOffset: start, +}); +log( + `${event} session=${sessionId} source=dsh http=${result.status} mode=text ` + + `from=${start} bytes=${end - start} turns=${turns.length} sent=${result.sent} offset=${result.offset}`, +); +process.exit(0); diff --git a/src/octop/infra/agents/hook_scripts/dsh/user-prompt-recall.mjs b/src/octop/infra/agents/hook_scripts/dsh/user-prompt-recall.mjs new file mode 100644 index 0000000000..f40dd4398a --- /dev/null +++ b/src/octop/infra/agents/hook_scripts/dsh/user-prompt-recall.mjs @@ -0,0 +1,97 @@ +#!/usr/bin/env node +// DSH ``UserPromptSubmit`` hook: search memory for the incoming prompt and inject +// the hits as extra context. +// +// The bridge reads context only from structured output, so the answer must be the +// ``hookSpecificOutput`` object below — plain stdout is silently dropped (unlike +// Claude Code, where the claude-code entry script's stdout becomes context). +// +// The store's full-text search is conjunctive: every term must match, so a long +// prompt whose tail is an instruction ("请只回答…") widens the query until nothing +// matches. Try the first sentence, then its shorter prefixes, then the whole +// prompt; only widen the layer to raw events as a last resort. +import { callTool, isConfigured, makeLogger, readStdinJson } from "../lib/octop-memory-client.mjs"; +import { resolveConfig } from "./memory-target.mjs"; + +const MARKER = "[octop memory search] related memories from this workspace, most relevant first:"; +const FOOTER = "若需要原文/证据,用 memory_get(path=…) 读全文;与本次工作无关则忽略。"; + +function candidateQueries(text) { + const candidates = []; + const push = (value) => { + const candidate = String(value || "").trim(); + if (candidate && !candidates.includes(candidate)) candidates.push(candidate); + }; + const firstSentence = + text.split(/[。!?!?;;\n\r]+/).map((part) => part.trim()).find(Boolean) ?? text; + push(firstSentence); + push(firstSentence.slice(0, 60)); + push(firstSentence.slice(0, 30)); + push(text.slice(0, 60)); + return candidates; +} + +async function main() { + const log = makeLogger("urecall.dsh"); + const prompt = String(readStdinJson(log).prompt || "").trim(); + if (!prompt) { + log("skip: empty prompt"); + return; + } + const config = resolveConfig(); + if (!isConfigured(config)) { + log("skip: not configured"); + return; + } + + const maxResults = Number(process.env.OCTOP_RECALL_MAX_RESULTS ?? 5); + const candidates = candidateQueries(prompt); + let hits = []; + let query = candidates[0]; + let corpus = "atom"; + for (const candidate of candidates) { + const result = await callTool(config, "memory_search", { + query: candidate, + corpus: corpus, + max_results: maxResults, + }); + hits = result.detail?.hits ?? []; + if (hits.length) { + query = candidate; + break; + } + } + if (!hits.length) { + // Fresh facts stay in the raw layer until extraction promotes them. + corpus = "all"; + const result = await callTool(config, "memory_search", { + query: candidates[0], + corpus: corpus, + max_results: maxResults, + }); + hits = result.detail?.hits ?? []; + } + log( + `prompt="${prompt.slice(0, 60)}" query="${query.slice(0, 40)}" ` + + `corpus=${corpus} hits=${hits.length}`, + ); + if (!hits.length) return; + + const lines = [MARKER]; + for (const hit of hits) { + const when = String(hit.occurred_at || "").slice(0, 19); + lines.push(`- [${hit.layer}] (${hit.role_hint ?? hit.layer}${when ? ` @ ${when}` : ""}) ${hit.snippet}`); + } + lines.push(FOOTER); + process.stdout.write( + JSON.stringify({ + hookSpecificOutput: { + hookEventName: "UserPromptSubmit", + additionalContext: lines.join("\n"), + }, + }), + ); +} + +await main(); +process.exit(0); diff --git a/src/octop/infra/agents/hook_scripts/kiro/stop-capture.mjs b/src/octop/infra/agents/hook_scripts/kiro/stop-capture.mjs new file mode 100644 index 0000000000..58b78ad9bb --- /dev/null +++ b/src/octop/infra/agents/hook_scripts/kiro/stop-capture.mjs @@ -0,0 +1,74 @@ +#!/usr/bin/env node +// Kiro ``Stop`` hook: ship this session's ``messages.jsonl`` into octop memory. +// +// Kiro-specific: the payload has no file path (only ``sess_…``), so the session +// file is located as ``///messages.jsonl``. +import fs from "node:fs"; +import os from "node:os"; +import { capture, isConfigured, loadConfig, makeLogger, readStdinJson, sleep } from "../lib/octop-memory-client.mjs"; + +const log = makeLogger("stopcap.kiro"); +const event = "Stop"; +const sessionId = readStdinJson(log).session_id || ""; +if (!sessionId) { + log(`${event} skip: payload has no session_id`); + process.exit(0); +} + +const root = process.env.OCTOP_KIRO_SESSIONS_ROOT || `${os.homedir()}/.kiro/sessions`; +function findSessionFile(id) { + try { + for (const entry of fs.readdirSync(root, { withFileTypes: true })) { + if (!entry.isDirectory()) continue; + const candidate = `${root}/${entry.name}/${id}/messages.jsonl`; + if (fs.existsSync(candidate)) return candidate; + } + } catch { + // no Kiro install / unreadable root + } + return ""; +} + +const transcriptPath = findSessionFile(sessionId); +if (!transcriptPath) { + log(`${event} skip: no transcript for ${sessionId} under ${root}`); + process.exit(0); +} + +// Kiro's Stop also fires before the file settles: wait until its size stops growing. +const POLL_MS = Number(process.env.OCTOP_CAPTURE_POLL_MS ?? 1000); +const MAX_WAIT_MS = Number(process.env.OCTOP_CAPTURE_MAX_WAIT_MS ?? 20000); +const deadline = Date.now() + MAX_WAIT_MS; +let previousSize = -1; +let waited = 0; +while (Date.now() < deadline) { + let size; + try { + size = fs.statSync(transcriptPath).size; + } catch { + break; + } + if (size === previousSize) break; + previousSize = size; + await sleep(POLL_MS); + waited += POLL_MS; +} +if (waited) log(`${event} waited ${waited}ms for the file to settle`); + +const config = loadConfig(); +if (!isConfigured(config)) { + log("skip: not configured"); + process.exit(0); +} +const buffer = fs.readFileSync(transcriptPath); +await capture({ + config, + log, + event, + source: "kiro", + sessionId, + transcriptPath, + buffer, + sessionPrefix: "kiro:", +}); +process.exit(0); diff --git a/src/octop/infra/agents/hook_scripts/lib/octop-memory-client.mjs b/src/octop/infra/agents/hook_scripts/lib/octop-memory-client.mjs new file mode 100644 index 0000000000..1b9e7a9614 --- /dev/null +++ b/src/octop/infra/agents/hook_scripts/lib/octop-memory-client.mjs @@ -0,0 +1,361 @@ +// Shared transport for every octop-memory host hook. +// +// Host-independent on purpose: this module knows how to talk to the memory MCP +// (config, auth, incremental offsets, logging, gzip, DSH's zstd container) and +// nothing about *which* terminal called it. Each host gets its own entry script +// under ``/`` that decides what to read and which ``source`` / +// ``session_id`` prefix to use — so changing one host never touches another. +// +// Config precedence: environment variable -> ``config.json`` next to the entry +// script (the /mcp/memory/setup installer writes that file, so a hooked-up +// terminal needs no secret in its hook command line). +import fs from "node:fs"; +import os from "node:os"; +import zlib from "node:zlib"; +import { fileURLToPath } from "node:url"; +import path from "node:path"; + +const ENTRY_DIR = path.dirname(fileURLToPath(import.meta.url)); +const HOOK_DIR = path.dirname(ENTRY_DIR); // .../hook_scripts (or the host dir) + +// Where this hook may write. Claude Code runs hooks unsandboxed, so the hook +// directory next to the scripts works. DSH runs the same command hooks inside a +// workspace-write sandbox whose writable roots are the session workspace, /tmp +// and the process temp dir: a $HOME hook directory stays readable there but every +// log/state write is denied — silently, because both are best-effort. Probe once +// and fall back to a private temp directory so the incremental offset and the +// diagnostic log survive in both worlds. +function resolveScratchDir() { + for (const dir of [HOOK_DIR, `${os.tmpdir()}/octop-hooks`]) { + const probe = `${dir}/.octop-write-probe`; + try { + fs.mkdirSync(dir, { recursive: true, mode: 0o700 }); + fs.writeFileSync(probe, ""); + fs.unlinkSync(probe); + return dir; + } catch { + // not writable here -> try the next candidate + } + } + return HOOK_DIR; +} + +export const paths = (() => { + const scratchDir = resolveScratchDir(); + if (scratchDir !== HOOK_DIR) { + process.stderr.write( + `octop-memory: ${HOOK_DIR} 不可写,日志与增量状态改写到 ${scratchDir}\n`, + ); + } + return { + hookDir: HOOK_DIR, + configPath: `${HOOK_DIR}/config.json`, + scratchDir, + logPath: `${scratchDir}/octop-hook.log`, + statePath: `${scratchDir}/octop-transcript-state.json`, + }; +})(); + +export function loadConfig() { + let fromFile = {}; + try { + fromFile = JSON.parse(fs.readFileSync(paths.configPath, "utf8")); + } catch { + // no config file (env-only setups keep working) + } + return { + endpoint: process.env.OCTOP_MEMORY_MCP_URL || fromFile.url || "", + token: process.env.OCTOP_MEMORY_MCP_TOKEN || fromFile.token || "", + agentId: process.env.OCTOP_AGENT_ID || fromFile.agentId || "", + // Configurable header name; ``Authorization`` (the default) takes a bearer. + tokenHeader: process.env.OCTOP_TOKEN_HEADER || fromFile.tokenHeader || "Authorization", + }; +} + +function baseHeaders(config) { + return { + "X-Octop-Agent-Id": config.agentId, + "Content-Type": "application/json", + Accept: "application/json, text/event-stream", + }; +} + +export function authHeaders(config) { + // One configurable header carries the token: ``Authorization`` (the default) + // takes ``Bearer ``, any other header name is written verbatim. + // The token belongs to a user, so the agent is identified only by + // ``X-Octop-Agent-Id``: no user header is sent. + const headers = baseHeaders(config); + const header = config.tokenHeader || "Authorization"; + headers[header] = header.toLowerCase() === "authorization" ? `Bearer ${config.token}` : config.token; + return headers; +} + +/** + * Whether these hooks have somewhere to send to: an endpoint, an agent, and a token. + */ +export function isConfigured(config) { + return !!config.endpoint && !!config.agentId && !!config.token; +} + +/** One logger per host: ``tag`` is what shows up in the shared log file. */ +export function makeLogger(tag) { + return (line) => { + try { + fs.appendFileSync(paths.logPath, `[${new Date().toISOString()}] ${tag}: ${line}\n`, { + mode: 0o600, + }); + } catch { + // logging must never break the hook + } + }; +} + +export function loadState() { + try { + return JSON.parse(fs.readFileSync(paths.statePath, "utf8")); + } catch { + return {}; + } +} + +export function saveState(state) { + try { + fs.writeFileSync(paths.statePath, JSON.stringify(state), { mode: 0o600 }); + } catch { + // a lost offset only costs one redundant (idempotent) upload + } +} + +/** Read the hook payload from stdin (the host hands it over as JSON). */ +export function readStdinJson(log) { + try { + const raw = fs.readFileSync(0, "utf8"); + return raw.trim() ? JSON.parse(raw) : {}; + } catch { + log("stdin-not-json"); + return {}; + } +} + +// Decode DSH's session container: it is a concatenation of independently +// decodable Zstandard frames (one header frame, then one frame per durable +// batch). Node's zstd API stops after the first frame, so a whole-file +// ``zstdDecompressSync`` silently returns only the session header. Walk the +// frames structurally and decode each complete one; a torn final frame is left +// for the next Stop to pick up. +export function decompressZstdContainer(buffer) { + const ZSTD_MAGIC = 0xfd2fb528; + const parts = []; + let offset = 0; + while (offset < buffer.length) { + const start = offset; + if (buffer.length - offset < 4) break; + if (buffer.readUInt32LE(offset) !== ZSTD_MAGIC) { + throw new Error(`invalid Zstandard frame magic at byte ${offset}`); + } + offset += 4; + if (offset === buffer.length) break; + const descriptor = buffer.readUInt8(offset); + offset += 1; + if ((descriptor & 0x18) !== 0) { + throw new Error(`reserved Zstandard frame-header bit at byte ${offset - 1}`); + } + const contentSizeFlag = descriptor >>> 6; + const singleSegment = (descriptor & 0x20) !== 0; + const checksum = (descriptor & 0x04) !== 0; + const dictionaryFlag = descriptor & 0x03; + const dictionaryBytes = dictionaryFlag === 3 ? 4 : dictionaryFlag; + const contentSizeBytes = contentSizeFlag === 0 ? (singleSegment ? 1 : 0) : 1 << contentSizeFlag; + const remainingHeaderBytes = (singleSegment ? 0 : 1) + dictionaryBytes + contentSizeBytes; + if (buffer.length - offset < remainingHeaderBytes) break; + offset += remainingHeaderBytes; + + let torn = false; + for (;;) { + if (buffer.length - offset < 3) { + torn = true; + break; + } + const blockHeader = buffer.readUIntLE(offset, 3); + offset += 3; + const lastBlock = (blockHeader & 1) !== 0; + const blockType = (blockHeader >>> 1) & 0x03; + const blockSize = blockHeader >>> 3; + if (blockType === 0x03) throw new Error(`reserved Zstandard block type at byte ${offset - 3}`); + const payloadBytes = blockType === 0x01 ? 1 : blockSize; + if (buffer.length - offset < payloadBytes) { + torn = true; + break; + } + offset += payloadBytes; + if (lastBlock) break; + } + if (torn) break; + if (checksum) { + if (buffer.length - offset < 4) break; + offset += 4; + } + parts.push(zlib.zstdDecompressSync(buffer.subarray(start, offset))); + } + return Buffer.concat(parts); +} + +/** Call one memory MCP tool; returns ``{ok, status, detail, raw}`` (never throws). */ +export async function callTool(config, name, args) { + return rpc(config, "tools/call", { name, arguments: args }); +} + +/** One raw JSON-RPC call; returns ``{ok, status, detail, error, raw}`` (never throws). */ +export async function rpc(config, method, params) { + const response = await fetch(config.endpoint, { + method: "POST", + headers: authHeaders(config), + body: JSON.stringify({ jsonrpc: "2.0", id: 1, method, params }), + }); + const text = await response.text(); + let payload = text; + for (const line of text.split("\n")) { + if (line.startsWith("data: ")) { + payload = line.slice(6); + break; + } + } + let parsed = {}; + try { + parsed = JSON.parse(payload.slice(payload.indexOf("{"), payload.lastIndexOf("}") + 1)); + } catch { + // keep the raw body for diagnostics + } + return { + ok: response.ok, + status: response.status, + detail: parsed?.result?.structuredContent ?? parsed?.result ?? {}, + error: parsed?.error?.message ?? "", + raw: text, + }; +} + +/** + * Whether this endpoint's ``memory_capture`` takes the assistant side as its own + * field (``assistant``) instead of expecting one merged ``content``. + */ +export async function supportsAssistant(config) { + const result = await rpc(config, "tools/list", {}); + const tool = (result.detail?.tools ?? []).find((entry) => entry?.name === "memory_capture"); + return Object.prototype.hasOwnProperty.call(tool?.inputSchema?.properties ?? {}, "assistant"); +} + +/** + * Ship already-grouped conversation turns with the roles in their own fields: + * ``content`` = the user side, ``assistant`` = the assistant side. + * + * Each accepted turn advances the per-``session_id`` offset, so a failure or a + * cap resumes at the next turn instead of re-sending the whole session. + */ +export async function captureTurns(ctx) { + const { config, log, event, source, sessionId, transcriptPath, turns, sessionPrefix } = ctx; + const state = loadState(); + let sent = 0; + let status = 0; + let offset = ctx.startOffset ?? 0; + for (const turn of turns) { + const args = { content: turn.user.join("\n\n"), source }; + if (sessionId) args.session_id = `${sessionPrefix}${sessionId}`; + if (turn.assistant.length) args.assistant = turn.assistant.join("\n\n"); + const result = await callTool(config, "memory_capture", args); + status = result.status; + if (!result.ok) { + log( + `${event} session=${sessionId} turn ${sent + 1} http=${status} ` + + `err=${result.detail?.error ?? result.error ?? ""} -> offset kept at ${offset}`, + ); + break; + } + sent += 1; + offset = turn.endOffset; + if (sessionId) { + state[sessionId] = { path: transcriptPath, offset }; + saveState(state); + } + } + return { sent, status, offset }; +} + +/** + * Ship the *new* bytes of a session file to ``memory_capture``. + * + * The offset is per ``session_id`` and only advances after a 2xx; a failed + * upload is retried in full next time, and the server dedupes on record keys, so + * re-sending is free. + */ +export async function capture(ctx) { + const { config, log, event, source, sessionId, transcriptPath, buffer, sessionPrefix } = ctx; + const state = loadState(); + const previous = sessionId ? state[sessionId] : undefined; + + let start = 0; + if (previous && previous.path === transcriptPath && previous.offset <= buffer.length) { + start = previous.offset; + // Only a boundary that still follows a newline is safe to resume from; a + // rewritten/compacted file falls back to a full (idempotent) re-send. + if (start > 0 && start < buffer.length && buffer[start - 1] !== 0x0a) start = 0; + } + if (start >= buffer.length) { + log(`${event} session=${sessionId} no new bytes (offset=${start}) -> skip`); + return { skipped: true }; + } + + const delta = buffer.subarray(start); + const whole = start === 0; + let blob; + try { + blob = zlib.gzipSync(delta).toString("base64"); + } catch (exc) { + log(`${event} gzip failed: ${exc}`); + return { failed: true }; + } + + const args = { content: blob, source }; + if (sessionId) args.session_id = `${sessionPrefix}${sessionId}`; + // Opt-in only: the server default is "none" (tool traffic is most of a + // transcript's bytes and distorts both extraction and raw-layer recall), so set + // this to "all"/"calls" only for a terminal that needs the full record. + if (process.env.OCTOP_TOOL_EVENTS) args.tool_events = process.env.OCTOP_TOOL_EVENTS; + + try { + const result = await callTool(config, "memory_capture", args); + const detail = result.detail ?? {}; + log( + `${event} session=${sessionId} source=${source} http=${result.status} ` + + `mode=${whole ? "full" : "delta"} from=${start} bytes=${delta.length} gz=${blob.length} ` + + `parsed=${detail.parsed} imported=${detail.imported} skipped=${detail.skipped_existing} ` + + `extract=${detail.extract_scheduled} err=${detail.error ?? result.error ?? ""}`, + ); + if (result.ok && sessionId) { + state[sessionId] = { path: transcriptPath, offset: buffer.length }; + saveState(state); + } + return { status: result.status, detail }; + } catch (exc) { + log(`${event} upload failed: ${exc}`); + return { failed: true }; + } +} + +/** Latest mtime order helper for hosts that discover their session file. */ +export function newestFile(dir, pattern) { + try { + return fs + .readdirSync(dir) + .filter((name) => pattern.test(name)) + .map((name) => ({ name, path: `${dir}/${name}`, mtime: fs.statSync(`${dir}/${name}`).mtimeMs })) + .sort((a, b) => b.mtime - a.mtime)[0]?.path ?? ""; + } catch { + return ""; + } +} + +export function sleep(ms) { + return new Promise((resolve) => setTimeout(resolve, ms)); +} diff --git a/src/octop/infra/agents/memory_mcp.py b/src/octop/infra/agents/memory_mcp.py index f0fd784a26..5ca9bffe8b 100644 --- a/src/octop/infra/agents/memory_mcp.py +++ b/src/octop/infra/agents/memory_mcp.py @@ -75,14 +75,123 @@ _SNIPPET_CHARS = 200 +_TRANSCRIPT_SOURCES: dict[str, str] = { + "claude-code": "claude-code", + "kiro": "kiro", + "dsh": "dsh", + "codex": "codex", + "cursor": "cursor", +} +"""``source`` values whose payload is a whole session transcript, not one turn.""" + +_TRANSCRIPT_PATH_ROOTS: tuple[str, ...] = ( + "~/.claude/projects", + "~/.kiro/sessions", + "~/.dsh/sessions", + "~/.codex/sessions", + "~/.cursor", +) +"""Where ``memory_capture(transcript_path=…)`` may read from (local hooks only).""" + +_GZIP_B64_PREFIX = "H4sI" +"""``base64(gzip(…))`` always starts with this; a JSONL body always starts with ``{``.""" + +_HOST_PLUMBING_MARKERS: tuple[str, ...] = ( + "", + "", + "Base directory for this skill:", +) +"""Text the terminal injects on its own account; dropped from transcript imports.""" + _OWN_CONTENT_PREFIX = "[octop memory " -"""Uniform marker for anything this service puts back into a conversation. +"""Uniform marker for anything this service puts back into a conversation.""" + +_LEGACY_ECHO_MARKERS: tuple[str, ...] = ( + "## Memory Recall", + "[memory] Earlier in this workspace", + "[octop-memory hook]", +) +"""Older recall markers still present in transcripts already on disk.""" + +_SKIP_CONTENT_MARKERS: tuple[str, ...] = (_OWN_CONTENT_PREFIX, *_LEGACY_ECHO_MARKERS) +"""Content carrying any of these is our own plumbing, never L0 evidence.""" + + +def _looks_like_transcript(text: str) -> bool: + """True when *text* plausibly is JSONL rather than one piece of prose.""" + return text.lstrip().startswith("{") + + +def _read_transcript_file(raw_path: str) -> str: + """Read a transcript file handed over by a local ``mcp_tool`` hook.""" + from pathlib import Path as _Path # noqa: PLC0415 + + candidate = _Path(raw_path).expanduser() + try: + resolved = candidate.resolve(strict=True) + except OSError as exc: + raise ValueError(f"transcript_path is not readable: {raw_path!r} ({exc})") from exc + allowed = [(_Path(root).expanduser().resolve()) for root in _TRANSCRIPT_PATH_ROOTS] + if not any(resolved == root or root in resolved.parents for root in allowed): + raise ValueError( + "transcript_path must live under a terminal session store " + f"({', '.join(_TRANSCRIPT_PATH_ROOTS)}); got {raw_path!r}" + ) + if resolved.suffix == ".zstd": + raise ValueError( + "compressed transcripts must be sent as gzip+base64 content " + "(the terminal's hook script does this); transcript_path supports plain JSONL" + ) + return resolved.read_text(encoding="utf-8", errors="replace") + + +def _transcript_redactor(server: OctopServer, agent_id: str) -> Callable[[str], str] | None: + """Secret redaction for transcript uploads, using the expert's privacy config. + + ``claude_transcript`` writes raw events directly (tool events included), so it + cannot reuse ``MemoryRuntime.capture``'s filtering; this hook applies the same + ``redact_secrets`` rule to each event body before it is stored. + """ + runtime = _memory_runtime(server, agent_id) + privacy_fn = getattr(runtime, "_privacy_cfg", None) + if not callable(privacy_fn): + return None + try: + privacy_cfg = privacy_fn() + except Exception: # noqa: BLE001 - redaction is best effort, never fail capture + return None + if not isinstance(privacy_cfg, dict): + return None + from octop_memory.application.runtime import _redact_text # noqa: PLC0415 + + return lambda text: str(_redact_text(text, privacy_cfg)) + + +def _archive_uploaded_session( + server: OctopServer, + agent_id: str, + *, + text: str, + source: str, + session_id: str, + user: str | None, +) -> str | None: + """Append this upload to the agent's markdown archive of the raw session.""" + try: + from octop.infra.agents.memory_session_archive import ( # noqa: PLC0415 + ARCHIVE_DIRNAME, + append_session, + ) + from octop.infra.agents.workspace import host_system_dir # noqa: PLC0415 + + workspace, cfg = _resolve_agent_scope(server, agent_id) + session_dir = host_system_dir(workspace, cfg) / ARCHIVE_DIRNAME + path = append_session(session_dir, session_id=session_id, text=text, host=source, user=user) + return str(path) if path else None + except Exception: # noqa: BLE001 - the archive must never break capture + logger.warning("memory_capture: session archive failed", exc_info=True) + return None -The terminal hooks built on this server (``d`` scope) inject search hits with this -prefix; ``MemoryRuntime.capture`` filters the upstream recall markers, and this -prefix is what a client could quote back into ``memory_capture``. Kept as a -documented marker only — capture relies on the runtime guard. -""" _USER_ROLE = "user_message" _ASSISTANT_ROLE = "assistant_message" @@ -535,6 +644,8 @@ async def memory_capture( thread_id: str | None = None, dry_run: bool = False, extract: bool = True, + transcript_path: str | None = None, + tool_events: str = "none", ) -> dict[str, Any]: """**写入原始事件(L0)**:一轮对话或一段记录,交给提炼流水线。 @@ -558,11 +669,80 @@ async def memory_capture( thread_id: 可选线程 id,透传给 L0。 dry_run: 只解析不写入;返回将要写入的事件。 extract: 写入后是否在后台触发提炼(默认 True);专家停止时不会自动启动。 + transcript_path: 仅终端 hook 使用:读本机 transcript 文件(只允许终端会话目录)。 + tool_events: transcript 形态下是否落工具事件(none / calls / all)。 """ caller = _caller_name() agent_id = _agent_id() def _run() -> dict[str, Any]: + # ---- terminal transcript: ``source`` names the terminal, it names the parser + nonlocal content + if transcript_path and source in _TRANSCRIPT_SOURCES: + content = _read_transcript_file(transcript_path) + if source in _TRANSCRIPT_SOURCES and ( + content.startswith(_GZIP_B64_PREFIX) or _looks_like_transcript(content) + ): + from octop.infra.agents.claude_transcript import import_content # noqa: PLC0415 + + text = content + if text.startswith(_GZIP_B64_PREFIX): + import base64 # noqa: PLC0415 + import gzip # noqa: PLC0415 + + try: + text = gzip.decompress(base64.b64decode(text)).decode("utf-8", "replace") + except Exception as exc: # noqa: BLE001 - surface a usable message + raise ValueError( + f"content starts like gzip+base64 but cannot be decoded: {exc}" + ) from exc + + memory = _open_memory(server, agent_id) + stats = import_content( + memory, + text, + session_id=session_id, + user=caller, + host=source, + dry_run=dry_run, + skip_markers=_SKIP_CONTENT_MARKERS, + skip_message_markers=_HOST_PLUMBING_MARKERS, + tool_events=tool_events, + parser=_TRANSCRIPT_SOURCES[source], + redact=_transcript_redactor(server, agent_id), + ) + archive = None + if not dry_run: + archive = _archive_uploaded_session( + server, + agent_id, + text=text, + source=source, + session_id=stats.session_id, + user=caller, + ) + return { + "marker": "[octop memory capture]", + "recorded": bool(stats.imported) and not dry_run, + "session_id": stats.session_id, + "session_archive": archive, + "parsed": stats.parsed, + "imported": stats.imported, + "skipped_existing": stats.skipped_existing, + "skipped_echo": stats.skipped_echo, + "skipped_own_results": stats.skipped_own_results, + "skipped_tools": stats.skipped_tools, + "tool_events": tool_events, + "events_by_type": stats.by_type, + "sidechain": stats.sidechain, + "dry_run": dry_run, + "source": source, + "caller": caller, + "warnings": stats.warnings, + "note": ( + "transcript parsed into L0 raw events; extraction runs off the request path" + ), + } events: list[dict[str, Any]] = [] payload = {"source": source, "source_host": source, "caller": caller} if content.strip(): @@ -1100,6 +1280,176 @@ async def __call__(self, scope: dict[str, Any], receive: Any, send: Any) -> None _current_agent_id.reset(agent_cv) +async def _send_text( + send: Any, status: int, body: str, *, content_type: str = "text/plain; charset=utf-8" +) -> None: + payload = body.encode("utf-8") + await send( + { + "type": "http.response.start", + "status": status, + "headers": [ + (b"content-type", content_type.encode("ascii")), + (b"content-length", str(len(payload)).encode("ascii")), + ], + } + ) + await send({"type": "http.response.body", "body": payload}) + + +def _instance_config() -> dict[str, Any]: + """The instance ``config.json`` as a dict (empty when missing/unreadable).""" + import json # noqa: PLC0415 + + from octop.infra.utils.paths import PathLayout # noqa: PLC0415 + + try: + raw = PathLayout.from_env().config.read_text(encoding="utf-8") + loaded = json.loads(raw) + except Exception: # noqa: BLE001 - a missing/odd config must not break /setup + return {} + return loaded if isinstance(loaded, dict) else {} + + +_LOOPBACK_HOSTS = frozenset({"127.0.0.1", "localhost", "::1"}) +"""本机地址:只有它没有 TLS 前置,按请求的 scheme 走 http。""" + + +def _host_without_port(host: str) -> str: + """``host``(可能带 ``:port`` 或 IPv6 方括号)里的裸主机名,小写。""" + value = host.strip() + if value.startswith("["): + return value[1:].split("]", 1)[0].lower() + return value.split(":", 1)[0].lower() + + +def _host_mount_url(scheme: str, host: str) -> str: + """本实例自己的挂载点(hooks 永远打它)。""" + if _host_without_port(host) in _LOOPBACK_HOSTS: + return f"{scheme or 'http'}://{host}/mcp/memory/" + return f"https://{host}/mcp/memory/" + + +def _default_mcp_url(scheme: str, host: str) -> str: + """客户端条目默认该连的地址:本实例自己的挂载点。""" + return _host_mount_url(scheme, host) + + +def _public_mcp_url() -> str | None: + """显式覆盖的客户端条目地址(部署可选)。""" + from_env = os.environ.get("OCTOP_MEMORY_PUBLIC_MCP_URL", "").strip() + if from_env: + return from_env + value = str(_instance_config().get("memory_public_mcp_url") or "").strip() + return value or None + + +_PS_INSTALLER_OS_VALUES = ("windows", "win", "win32", "win64", "powershell", "pwsh", "ps1") +"""``?os=`` 的取值:这些返回 PowerShell 版安装器/卸载器(不区分大小写)。""" + + +def _wants_powershell(query: dict[str, str]) -> bool: + """``/setup``、``/uninstall`` 用 ``?os=windows`` 选 PowerShell 版(默认仍是 sh)。""" + for key in ("os", "platform", "format", "shell"): + if (query.get(key) or "").strip().lower() in _PS_INSTALLER_OS_VALUES: + return True + return False + + +class _InstallerApp: + """ASGI app for ``/setup``: renders the local-client installer script. + + It hands out no credential — the script asks the user for their own MCP token + (``omcp_…``) at run time — so it deliberately sits outside the token check. + """ + + def __init__(self, server: OctopServer) -> None: + self._server = server + + async def __call__(self, scope: dict[str, Any], receive: Any, send: Any) -> None: + from urllib.parse import parse_qsl # noqa: PLC0415 + + if scope.get("type") != "http": + return + if str(scope.get("method", "GET")).upper() not in ("GET", "HEAD"): + await _send_text(send, 405, "GET only\n") + return + + from octop.infra.agents.client_setup import ( # noqa: PLC0415 + render_setup_ps1, + render_setup_script, + ) + + query = dict(parse_qsl(scope.get("query_string", b"").decode("utf-8"))) + headers = _headers(scope) + scheme = headers.get("x-forwarded-proto", "").split(",")[0].strip().lower() + host = headers.get("host", "127.0.0.1") + instance_url = _host_mount_url(scheme, host) + render = render_setup_ps1 if _wants_powershell(query) else render_setup_script + script = render( + mcp_url=query.get("url") or _public_mcp_url() or _default_mcp_url(scheme, host), + hook_url=query.get("hook_url") or instance_url, + agent_id=(query.get("agent") or "").strip(), + terminals=(query.get("terminals") or "").strip(), + # 不内置任何凭据:安装时由用户粘贴自己的用户级 token(review 要求)。 + default_token="", + project=(query.get("project") or "").strip(), + ) + await _send_text(send, 200, script, content_type="text/x-shellscript; charset=utf-8") + + +class _UninstallerApp: + """ASGI app for ``/uninstall``: counterpart of ``/setup`` (also unauthenticated).""" + + def __init__(self, server: OctopServer) -> None: + self._server = server + + async def __call__(self, scope: dict[str, Any], receive: Any, send: Any) -> None: + from urllib.parse import parse_qsl # noqa: PLC0415 + + if scope.get("type") != "http": + return + if str(scope.get("method", "GET")).upper() not in ("GET", "HEAD"): + await _send_text(send, 405, "GET only\n") + return + + from octop.infra.agents.client_setup import ( # noqa: PLC0415 + render_uninstall_ps1, + render_uninstall_script, + ) + + query = dict(parse_qsl(scope.get("query_string", b"").decode("utf-8"))) + render = render_uninstall_ps1 if _wants_powershell(query) else render_uninstall_script + script = render( + terminals=(query.get("terminals") or "").strip(), + project=(query.get("project") or "").strip(), + ) + await _send_text(send, 200, script, content_type="text/x-shellscript; charset=utf-8") + + +class _SetupFirstApp: + """Serve ``/setup`` and ``/uninstall`` before the token check, then MCP.""" + + def __init__(self, installer: Any, uninstaller: Any, inner: Any) -> None: + self._installer = installer + self._uninstaller = uninstaller + self._inner = inner + + async def __call__(self, scope: dict[str, Any], receive: Any, send: Any) -> None: + if scope.get("type") == "http": + path = str(scope.get("path", "")) + root = str(scope.get("root_path", "")) + relative = path[len(root) :] if root and path.startswith(root) else path + cleaned = relative.rstrip("/") + if cleaned == "/setup": + await self._installer(scope, receive, send) + return + if cleaned == "/uninstall": + await self._uninstaller(scope, receive, send) + return + await self._inner(scope, receive, send) + + def _chain_lifespan(previous: Any, managers: list[Any]) -> Callable[[Any], Any]: """Wrap the host lifespan so MCP session managers run without hiding hooks.""" @@ -1151,7 +1501,14 @@ def install_memory_mcp(app: Any, server: OctopServer) -> None: streamable_app = mcp.streamable_http_app() managers = [mgr for mgr in (getattr(mcp, "_session_manager", None),) if mgr is not None] - app.mount("/mcp/memory", _TokenAuthMiddleware(_AgentRouter(streamable_app, server), server)) + app.mount( + "/mcp/memory", + _SetupFirstApp( + _InstallerApp(server), + _UninstallerApp(server), + _TokenAuthMiddleware(_AgentRouter(streamable_app, server), server), + ), + ) if managers: app.router.lifespan_context = _chain_lifespan(app.router.lifespan_context, managers) diff --git a/src/octop/infra/agents/memory_session_archive.py b/src/octop/infra/agents/memory_session_archive.py new file mode 100644 index 0000000000..2ae219bd98 --- /dev/null +++ b/src/octop/infra/agents/memory_session_archive.py @@ -0,0 +1,210 @@ +"""Server-side markdown archive of the raw sessions uploaded to ``memory_capture``. + +L0 keeps only what the extractor and recall need (user / assistant messages), so the +tool traffic and the model's scratchpad are dropped there. This writer keeps the +*uploaded* session as readable markdown next to the agent's memory store, so the full +record stays available for later review: + + {workspace}/.octop/memory-sessions/.md + +The client uploads byte-deltas of the transcript, so the file is **append-only**: each +upload renders its new records and adds them; re-sends (``mode=full`` after a lost +offset state) are deduplicated by the record key written as an HTML comment, which is +invisible when rendered. Failures must never break capture — callers treat them as +best-effort and log. +""" + +from __future__ import annotations + +import json +import re +from collections.abc import Iterator +from datetime import UTC, datetime +from pathlib import Path +from typing import Any + +# Reuse the transcript parser's record iteration and block normalization: the archive +# must agree with what the parser sees, and a second copy of those rules would drift. +from octop.infra.agents.claude_transcript import _block_text, _blocks, _iter_records + +ARCHIVE_DIRNAME = "memory-sessions" +"""Subdirectory of the agent system dir (``{workspace}/.octop/``) holding the archives.""" + +_KEY_MARKER = "" +_KEY_PATTERN = re.compile(r"") + +_MAX_BODY_CHARS = 20000 +"""Per-record cap in the markdown body — much looser than L0's 4000. + +The archive is for human inspection, so it keeps far more of a long tool result than +the L0 row does; it still bounds a single record so one 5MB build log cannot dominate +the file. +""" + + +def _now() -> str: + return datetime.now(UTC).astimezone().strftime("%Y-%m-%d %H:%M:%S") + + +def _cap(text: str, limit: int = _MAX_BODY_CHARS) -> str: + body = (text or "").strip() + return body if len(body) <= limit else body[: limit - 1].rstrip() + "…" + + +def _fence(body: str, language: str) -> str: + """Fence *body*, widening the fence when the body itself contains backticks.""" + fence = "```" + while fence in body: + fence += "`" + return f"{fence}{language}\n{body}\n{fence}" + + +def _record_role(record: dict[str, Any]) -> str: + return str(record.get("type") or "record") + + +def _render_record(key: str, record: dict[str, Any], index: int) -> str: + """One transcript record → one markdown section (all blocks, nothing dropped).""" + message = record.get("message") + message = message if isinstance(message, dict) else {} + role = str(message.get("role") or _record_role(record)) + stamp = record.get("timestamp") + when = "" + if isinstance(stamp, str) and stamp: + when = stamp.replace("T", " ").replace("Z", "")[:19] + lines: list[str] = [f"## {index} · {when or _now()} · {role}"] + lines.append(_KEY_MARKER.format(key)) + lines.append("") + sidechain = "(sidechain)" if record.get("isSidechain") else "" + if sidechain: + lines.append(sidechain) + lines.append("") + + blocks = _blocks(message.get("content")) + if not blocks: + raw = record.get("content") + if isinstance(raw, str) and raw.strip(): + blocks = [{"type": "text", "text": raw}] + for block in blocks: + kind = block.get("type") + if kind == "text": + body = _cap(str(block.get("text") or "")) + if body: + lines.append(body) + lines.append("") + elif kind == "thinking": + body = _cap(str(block.get("thinking") or block.get("text") or "")) + if body: + lines.append("*(thinking)*") + lines.append("") + lines.append("\n".join(f"> {line}" for line in body.splitlines()) or "> ") + lines.append("") + elif kind == "tool_use": + name = str(block.get("name") or "tool") + args = block.get("input") + body = json.dumps(args, ensure_ascii=False, indent=2, default=str) if args else "" + lines.append(f"**tool_call · {name}**") + lines.append("") + if body: + lines.append(_fence(_cap(body), "json")) + lines.append("") + elif kind == "tool_result": + name = str(block.get("name") or "") + body = _cap(_block_text(block.get("content"))) + flag = "(error)" if block.get("is_error") else "" + lines.append(f"**tool_result{flag}{' · ' + name if name else ''}**") + lines.append("") + if body: + lines.append(_fence(body, "text")) + lines.append("") + else: + lines.append(f"*({kind or 'unknown'} block)*") + lines.append("") + return "\n".join(lines).rstrip() + "\n" + + +def iter_conversation_records(text: str) -> Iterator[tuple[str, dict[str, Any]]]: + """Yield ``(record key, record)`` for the conversation records in *text*. + + Only ``user`` / ``assistant`` records — the transcript's bookkeeping records + (``mode`` / ``cost-state`` / ``attachment`` / ``file-history-snapshot``) are not + conversation. The key is the record's ``uuid`` — the same identity L0 + deduplicates on. + """ + for index, record in enumerate(_iter_records(text), start=1): + if not isinstance(record, dict): + continue + if str(record.get("type") or "") not in ("user", "assistant"): + continue + key = str(record.get("uuid") or f"line{index}") + yield key, record + + +def iter_sections(text: str) -> Iterator[tuple[str, str]]: + """Yield ``(record key, markdown section)`` for every conversation record in *text*. + + Every block is kept: text, thinking, ``tool_use`` and ``tool_result``. + """ + for index, (key, record) in enumerate(iter_conversation_records(text), start=1): + yield key, _render_record(key, record, index) + + +def _header(*, session_id: str, host: str, user: str | None) -> str: + rows = [f"# {host} session {session_id}", "", f"- host: {host}", f"- session: {session_id}"] + if user: + rows.append(f"- user: {user}") + rows += [ + f"- created: {_now()}", + "- source: `memory_capture` 上传的原始 transcript(含工具调用与结果、thinking)", + "", + "---", + "", + ] + return "\n".join(rows) + + +def archive_path(session_dir: Path, session_id: str) -> Path: + safe = re.sub(r"[^0-9A-Za-z._-]+", "_", session_id).strip("_") or "session" + return session_dir / f"{safe}.md" + + +def append_session( + session_dir: Path, + *, + session_id: str, + text: str, + host: str, + user: str | None = None, +) -> Path | None: + """Append the records of one uploaded chunk to the session's markdown archive. + + Returns the file path when something was written, ``None`` when the chunk carried + no new record (a re-send) — so callers can skip without special-casing. + """ + path = archive_path(session_dir, session_id) + existing = "" + if path.exists(): + existing = path.read_text(encoding="utf-8", errors="replace") + seen = set(_KEY_PATTERN.findall(existing)) + index = len(seen) + + sections: list[str] = [] + for key, record in iter_conversation_records(text): + if key in seen: + continue + seen.add(key) + index += 1 + sections.append(_render_record(key, record, index)) + if not sections: + return None + + session_dir.mkdir(parents=True, exist_ok=True) + body = "\n".join(section.rstrip() + "\n" for section in sections) + if existing: + with path.open("a", encoding="utf-8") as handle: + handle.write("\n" + body) + else: + path.write_text( + _header(session_id=session_id, host=host, user=user) + body, encoding="utf-8" + ) + return path diff --git a/tests/unit/agents/test_claude_transcript.py b/tests/unit/agents/test_claude_transcript.py new file mode 100644 index 0000000000..dd8d38f9a5 --- /dev/null +++ b/tests/unit/agents/test_claude_transcript.py @@ -0,0 +1,830 @@ +"""Unit tests for the Claude Code transcript importer (infra/agents/claude_transcript).""" + +from __future__ import annotations + +import json +from pathlib import Path + +import pytest +from octop_memory.core import Memory + +from octop.infra.agents.claude_transcript import ( + import_content, + import_session, + parse_dsh_text, + parse_kiro_text, + parse_session, +) + +_SESSION = "sess-abc" +_TS = "2026-09-22T05:00:00.000Z" + + +def _record(**kwargs): + base = {"sessionId": _SESSION, "timestamp": _TS, "isSidechain": False} + base.update(kwargs) + return base + + +def _write(tmp_path: Path, records: list[dict]) -> Path: + path = tmp_path / "session.jsonl" + path.write_text( + "\n".join(json.dumps(record, ensure_ascii=False) for record in records), encoding="utf-8" + ) + return path + + +def _turn_records() -> list[dict]: + return [ + {"type": "queue-operation", "sessionId": _SESSION, "operation": "enqueue"}, + _record(type="progress", uuid="p1"), + _record(type="user", uuid="u1", message={"role": "user", "content": "帮我加一个导入命令"}), + _record( + type="assistant", + uuid="a1", + message={ + "role": "assistant", + "content": [ + {"type": "thinking", "thinking": "model scratchpad"}, + {"type": "text", "text": "好的,先看现有 CLI 结构"}, + { + "type": "tool_use", + "id": "toolu_1", + "name": "Read", + "input": {"file_path": "src/octop/cli/main.py"}, + }, + ], + }, + ), + _record( + type="user", + uuid="u2", + message={ + "role": "user", + "content": [ + { + "type": "tool_result", + "tool_use_id": "toolu_1", + "content": [{"type": "text", "text": "@click.group ..."}], + "is_error": False, + } + ], + }, + ), + ] + + +@pytest.fixture +def agent_memory(tmp_path: Path) -> Memory: + return Memory( + namespace="agent_TEST", + backend="sqlite", + backend_config={"db_path": str(tmp_path / "memory.sqlite")}, + ) + + +def test_parse_maps_conversation_records(tmp_path: Path) -> None: + parsed = parse_session(_write(tmp_path, _turn_records())) + + assert parsed.session_id == _SESSION + # thinking / progress / queue-operation carry no L0 content + assert [event.event_type for event in parsed.events] == [ + "user_message", + "assistant_message", + "tool_call", + "tool_result", + ] + # keys use the ORIGINAL block index (thinking keeps index 0) so they stay + # stable if the importer later changes which block types it maps + assert [event.key for event in parsed.events] == ["u1:0", "a1:1", "a1:2", "u2:0"] + + user, assistant, tool_call, tool_result = parsed.events + assert user.content == "帮我加一个导入命令" + assert assistant.content == "好的,先看现有 CLI 结构" + assert tool_call.content.startswith("Read ") + assert tool_call.payload["tool_name"] == "Read" + assert tool_call.payload["tool_use_id"] == "toolu_1" + assert tool_call.payload["input"] == {"file_path": "src/octop/cli/main.py"} + assert tool_result.content == "@click.group ..." + assert tool_result.payload["tool_use_id"] == "toolu_1" + assert tool_result.payload["is_error"] is False + assert user.timestamp.year == 2026 and user.timestamp.tzinfo is not None + # role/session markers survive on every event + assert {event.payload["role"] for event in parsed.events} == {"user", "assistant"} + assert all(event.payload["src_key"] == event.key for event in parsed.events) + + +def test_parse_skips_non_conversation_records(tmp_path: Path) -> None: + parsed = parse_session(_write(tmp_path, _turn_records())) + assert not [event for event in parsed.events if event.payload["src_key"].startswith("p1")] + + +def test_import_writes_l0_and_is_idempotent(tmp_path: Path, agent_memory: Memory) -> None: + path = _write(tmp_path, _turn_records()) + + first = import_session(agent_memory, path) + assert first.session_id == f"cc:{_SESSION}" + assert first.parsed == 4 + assert first.imported == 4 + assert first.skipped_existing == 0 + + stored = agent_memory.list_raw(session_id=f"cc:{_SESSION}", limit=100) + assert len(stored) == 4 + assert {raw.event_type for raw in stored} == { + "user_message", + "assistant_message", + "tool_call", + "tool_result", + } + assert all(raw.host == "claude-code" for raw in stored) + assert all(raw.thread_id == f"cc:{_SESSION}" for raw in stored) + + again = import_session(agent_memory, path) + assert again.imported == 0 + assert again.skipped_existing == 4 + assert len(agent_memory.list_raw(session_id=f"cc:{_SESSION}", limit=100)) == 4 + + +def test_import_appends_only_new_turns(tmp_path: Path, agent_memory: Memory) -> None: + records = _turn_records() + path = _write(tmp_path, records) + import_session(agent_memory, path) + + records.append( + _record(type="user", uuid="u3", message={"role": "user", "content": "继续,加上 --dry-run"}) + ) + path = _write(tmp_path, records) + + stats = import_session(agent_memory, path) + assert stats.imported == 1 + assert stats.skipped_existing == 4 + + +def test_import_dry_run_writes_nothing(tmp_path: Path, agent_memory: Memory) -> None: + path = _write(tmp_path, _turn_records()) + + stats = import_session(agent_memory, path, dry_run=True) + assert stats.imported == 0 + assert stats.parsed == 4 + assert agent_memory.list_raw(session_id=f"cc:{_SESSION}", limit=100) == [] + + +def test_import_caps_long_tool_results(tmp_path: Path, agent_memory: Memory) -> None: + huge = "x" * 20_000 + records = [ + _record( + type="user", + uuid="u1", + message={ + "role": "user", + "content": [ + {"type": "tool_result", "tool_use_id": "t", "content": huge, "is_error": False} + ], + }, + ) + ] + import_session(agent_memory, _write(tmp_path, records)) + + stored = agent_memory.list_raw(session_id=f"cc:{_SESSION}", limit=10) + assert len(stored) == 1 + assert len(stored[0].content) == 4000 + + +def test_user_attribution_is_stored(tmp_path: Path, agent_memory: Memory) -> None: + import_session(agent_memory, _write(tmp_path, _turn_records()), user="alice") + stored = agent_memory.list_raw(session_id=f"cc:{_SESSION}", limit=10) + assert {raw.user for raw in stored} == {"alice"} + + +@pytest.mark.parametrize( + "tool_name", + [ + "memory_search", + "mcp__octop-memory-local__memory_search", + "mcp__octop-memory-prod__memory_search", + "mcp__octop-memory-oauth__memory_search", + "mcp__whatever-the-user-called-it__memory_recall", + "mcp_octop_memory_local_memory_search", + "mcp_octop_memory_prod_memory_get", + ], +) +def test_our_read_tools_are_recognized_under_any_mcp_name(tool_name: str) -> None: + """MCP 服务名可能是 local / prod / oauth 或用户自取的名字:只按工具名认。""" + from octop.infra.agents.claude_transcript import _is_own_read_tool + + assert _is_own_read_tool(tool_name) + # 写工具(memory_capture / memory_save)不是「读工具」,别把用户自己的调用误丢 + assert not _is_own_read_tool("mcp__octop-memory-prod__memory_capture") + assert not _is_own_read_tool("Bash") + + +def test_parse_drops_our_own_memory_read_results(tmp_path: Path) -> None: + """Re-importing our own recall output would close the echo loop.""" + records = [ + _record( + type="assistant", + uuid="a1", + message={ + "role": "assistant", + "content": [ + { + "type": "tool_use", + "id": "t1", + "name": "mcp__octop-memory-local__memory_search", + "input": {"query": "x"}, + }, + {"type": "tool_use", "id": "t2", "name": "Bash", "input": {"command": "ls"}}, + ], + }, + ), + _record( + type="user", + uuid="u1", + message={ + "role": "user", + "content": [ + { + "type": "tool_result", + "tool_use_id": "t1", + "content": '{"hits": [{"snippet": "旧记忆"}]}', + "is_error": False, + }, + { + "type": "tool_result", + "tool_use_id": "t2", + "content": "file list", + "is_error": False, + }, + ], + }, + ), + ] + + parsed = parse_session(_write(tmp_path, records)) + + assert [event.event_type for event in parsed.events] == [ + "tool_call", + "tool_call", + "tool_result", + ] + assert parsed.events[-1].content == "file list" + assert parsed.skipped_own_results == 1 + + +def test_import_content_drops_marked_events(agent_memory: Memory) -> None: + """A recalled block must not be written back into L0 as new evidence.""" + records = [ + _record(type="user", uuid="u1", message={"role": "user", "content": "正常提问"}), + _record( + type="user", + uuid="u2", + message={"role": "user", "content": "## Memory Recall\n- [atom] 旧记忆块"}, + ), + ] + text = "\n".join(json.dumps(record, ensure_ascii=False) for record in records) + + stats = import_content(agent_memory, text, skip_markers=("## Memory Recall",)) + + assert stats.parsed == 2 + assert stats.skipped_echo == 1 + assert stats.imported == 1 + stored = agent_memory.list_raw(session_id=stats.session_id, limit=10) + assert [raw.content for raw in stored] == ["正常提问"] + + +def test_user_messages_are_attributed_to_the_caller(agent_memory: Memory) -> None: + """transcript 形态也要让发送者进正文:提取器 prompt 看不到 raw 的 ``user`` 列。""" + records = [ + _record(type="user", uuid="u1", message={"role": "user", "content": "帮我加一个导入命令"}), + _record( + type="assistant", + uuid="a1", + message={"role": "assistant", "content": [{"type": "text", "text": "好的"}]}, + ), + ] + text = "\n".join(json.dumps(record, ensure_ascii=False) for record in records) + + stats = import_content(agent_memory, text, session_id="cc:s1", user="alice") + + by_type = {raw.event_type: raw for raw in agent_memory.list_raw(session_id="cc:s1", limit=10)} + assert by_type["user_message"].content == "alice说:帮我加一个导入命令" + assert by_type["assistant_message"].content == "好的" # 助手侧不加前缀 + assert by_type["user_message"].user == "alice" # raw 的 user 列照旧 + assert stats.imported == 2 + + # 重发(offset 丢失后的 mode=full)靠 src_key 去重,也不会把前缀叠成两层 + again = import_content(agent_memory, text, session_id="cc:s1", user="alice") + assert again.imported == 0 + + +def test_sidechain_user_records_are_not_attributed(agent_memory: Memory) -> None: + """子代理记录里的 user 消息是系统合成提示,不是调用者说过的话。""" + records = [ + _record(type="user", uuid="u1", message={"role": "user", "content": "真实提问"}), + _record( + type="user", + uuid="u2", + isSidechain=True, + message={"role": "user", "content": "子代理提示"}, + ), + ] + text = "\n".join(json.dumps(record, ensure_ascii=False) for record in records) + + import_content(agent_memory, text, session_id="cc:s2", user="alice") + + contents = {raw.content for raw in agent_memory.list_raw(session_id="cc:s2", limit=10)} + assert "alice说:真实提问" in contents + assert "子代理提示" in contents + assert "alice说:子代理提示" not in contents + + +# --------------------------------------------------------------------------- +# Kiro (``~/.kiro/sessions///messages.jsonl``) +# --------------------------------------------------------------------------- + +_KIRO_RECORDS = [ + { + "id": "r1", + "timestamp": _TS, + "payload": {"type": "user", "content": "Kiro 里问的问题", "source": "chat"}, + }, + {"id": "r2", "timestamp": _TS, "payload": {"type": "turn_start", "executionId": "e1"}}, + { + "id": "r3", + "timestamp": _TS, + "payload": { + "type": "ContextualHookInvoked", + "hookId": "h", + "output": "[octop memory search] 旧记忆", + }, + }, + { + "id": "r4", + "timestamp": _TS, + "payload": { + "type": "tool_call", + "toolCallId": "c1", + "toolName": "readFiles", + "args": {"files": ["a.ts"]}, + }, + }, + { + "id": "r5", + "timestamp": _TS, + "payload": { + "type": "tool_result", + "toolCallId": "c1", + "content": "file body", + "success": True, + }, + }, + { + "id": "r6", + "timestamp": _TS, + "payload": {"type": "assistant", "content": "scratchpad", "operationType": "Reasoning"}, + }, + { + "id": "r7", + "timestamp": _TS, + "payload": {"type": "assistant", "content": "这是回答", "operationType": "Say"}, + }, + {"id": "r8", "timestamp": _TS, "payload": {"type": "turn_end", "stopReason": "end_turn"}}, +] + + +def _kiro_text() -> str: + return "\n".join(json.dumps(r, ensure_ascii=False) for r in _KIRO_RECORDS) + + +def test_parse_kiro_maps_records() -> None: + parsed = parse_kiro_text(_kiro_text()) + + assert [e.event_type for e in parsed.events] == [ + "user_message", + "tool_call", + "tool_result", + "assistant_message", + ] + assert parsed.events[3].content == "这是回答" # Reasoning 是草稿,跳过 + assert parsed.events[1].payload["tool_name"] == "readFiles" + assert parsed.events[2].payload["success"] is True + # ContextualHookInvoked 是我们自己 hook 的调用记录(连 output 都在里面) + assert parsed.skipped_own_results == 1 + assert all(e.event_type != "tool_result" or "旧记忆" not in e.content for e in parsed.events) + + +def test_import_content_kiro_is_idempotent(agent_memory: Memory) -> None: + first = import_content( + agent_memory, _kiro_text(), session_id="kiro:s1", host="kiro", parser="kiro" + ) + assert first.session_id == "kiro:s1" + assert first.imported == 4 + assert first.by_type == { + "user_message": 1, + "tool_call": 1, + "tool_result": 1, + "assistant_message": 1, + } + + again = import_content( + agent_memory, _kiro_text(), session_id="kiro:s1", host="kiro", parser="kiro" + ) + assert again.imported == 0 + assert again.skipped_existing == 4 + + +def test_import_content_rejects_unknown_parser(agent_memory: Memory) -> None: + with pytest.raises(ValueError, match="unknown transcript parser"): + import_content(agent_memory, _kiro_text(), parser="no-such-host") + + +# --------------------------------------------------------------------------- +# DSH (``~/.dsh/sessions///session.jsonl.zstd``) +# --------------------------------------------------------------------------- + +_DSH_MS = 1788952324078 +_DSH_RECORDS = [ + {"type": "session", "version": 0, "id": "session-x", "createdAt": _DSH_MS, "cwd": "/tmp"}, + { + "type": "user/message", + "seq": 1, + "time": _DSH_MS, + "data": {"content": [{"type": "text", "text": "真实提问"}], "source": {"kind": "user"}}, + }, + { + "type": "user/message", + "seq": 2, + "time": _DSH_MS, + "data": { + "content": [ + {"type": "text", "text": "Current runtime context. This snapshot supersedes…"} + ], + "source": {"kind": "plugin"}, + }, + }, + {"type": "assistant/chunk", "seq": 3, "time": _DSH_MS, "data": {"text": "流式增量"}}, + { + "type": "assistant/message", + "seq": 4, + "time": _DSH_MS, + "data": { + "turn": 1, + "step": 1, + "message": { + "role": "assistant", + "content": [ + {"type": "reasoning", "text": "草稿"}, + {"type": "text", "text": "回答正文"}, + { + "type": "tool-call", + "id": "call_1", + "name": "bash", + "arguments": '{"command": "ls"}', + }, + ], + }, + }, + }, + { + "type": "tool/call", + "seq": 5, + "time": _DSH_MS, + "data": { + "turn": 1, + "step": 1, + "callId": "call_1", + "name": "bash", + "arguments": '{"command": "ls"}', + }, + }, + { + "type": "tool/result", + "seq": 6, + "time": _DSH_MS, + "data": { + "turn": 1, + "step": 1, + "message": { + "source": {"kind": "tool", "callId": "call_1"}, + "content": [ + { + "type": "tool-result", + "toolCallId": "call_1", + "content": [{"type": "text", "text": "file list"}], + "isError": False, + } + ], + }, + }, + }, + {"type": "turn/end", "seq": 7, "time": _DSH_MS, "data": {"turn": 1, "stopReason": "end_turn"}}, +] + + +def _dsh_text() -> str: + return "\n".join(json.dumps(r, ensure_ascii=False) for r in _DSH_RECORDS) + + +def test_parse_dsh_maps_records() -> None: + parsed = parse_dsh_text(_dsh_text()) + + assert parsed.session_id == "session-x" + assert [e.event_type for e in parsed.events] == [ + "user_message", + "assistant_message", + "tool_call", + "tool_result", + ] + # 流式增量与 Reasoning 草稿都不入库 + assert all("流式增量" not in e.content for e in parsed.events) + assert all("草稿" not in e.content for e in parsed.events) + # 同一个 call 既在 assistant/message 的 tool-call 块里、又是独立 tool/call 记录 —— 只留一条 + assert [e.content for e in parsed.events if e.event_type == "tool_call"] == [ + 'bash {"command": "ls"}' + ] + assert parsed.events[3].content == "file list" + assert parsed.events[3].payload["is_error"] is False + # plugin / skill-catalog / agent-instructions 注入是管道,不是用户说的话 + assert parsed.skipped_own_results == 1 + + +def test_import_content_dsh_uses_terminal_prefix(agent_memory: Memory) -> None: + stats = import_content(agent_memory, _dsh_text(), host="dsh", parser="dsh") + + assert stats.session_id == "dsh:session-x" + assert stats.imported == 4 + assert stats.by_type == { + "user_message": 1, + "assistant_message": 1, + "tool_call": 1, + "tool_result": 1, + } + + +def test_echo_rule_differs_by_role() -> None: + """回声判定按角色区分:助手侧看是否以标记开头,用户侧看标记是否出现。""" + from octop.infra.agents.claude_transcript import _is_echo + + markers = ("[octop memory search]", "[octop memory capture]") + # 助手侧:只是引用标记的回答必须保留 + assert not _is_echo( + "靠两个 hook:召回把 [octop memory search] 块注入上下文", "assistant_message", markers + ) + assert _is_echo("[octop memory search] related memories…", "assistant_message", markers) + # 用户侧:出现标记就是我们注入的上下文(CC 会加 "Stop hook feedback:" 前缀) + assert _is_echo( + "Stop hook feedback:\n[octop memory search] 请记录要点", "user_message", markers + ) + assert not _is_echo("正常提问", "user_message", markers) + + +def test_a_reply_that_quotes_our_marker_survives(agent_memory: Memory) -> None: + """模型回答只是**引用**召回标记时必须入库——否则解释接入方式的对话会被当回声丢掉。""" + records = [ + _record(type="user", uuid="u1", message={"role": "user", "content": "记忆接入怎么做的?"}), + _record( + type="assistant", + uuid="a1", + message={ + "role": "assistant", + "content": [ + { + "type": "text", + "text": "靠两个 hook:召回会把 [octop memory search] 块注入上下文。", + } + ], + }, + ), + ] + text = "\n".join(json.dumps(record, ensure_ascii=False) for record in records) + + stats = import_content(agent_memory, text, skip_markers=("[octop memory search]",)) + + assert stats.skipped_echo == 0 + assert stats.imported == 2 + stored = agent_memory.list_raw(session_id=stats.session_id, limit=10) + assert any("[octop memory search]" in raw.content for raw in stored) + + +def test_codex_rollout_maps_to_events() -> None: + """Codex rollout:event_msg 带对话,response_item 带工具与(重复的)消息副本。""" + from octop.infra.agents.claude_transcript import parse_codex_text + + records = [ + { + "timestamp": "2026-08-08T13:47:31.000Z", + "type": "session_meta", + "payload": {"id": "s-1", "cwd": "/tmp", "cli_version": "0.142.5"}, + }, + { + "timestamp": "2026-08-08T13:47:31.163Z", + "type": "event_msg", + "payload": {"type": "user_message", "message": "现在是什么模型\n"}, + }, + { + "timestamp": "2026-08-08T13:47:32.000Z", + "type": "response_item", + "payload": { + "type": "message", + "role": "developer", + "content": [{"type": "input_text", "text": ""}], + }, + }, + { + "timestamp": "2026-08-08T13:47:33.000Z", + "type": "response_item", + "payload": { + "type": "message", + "role": "assistant", + "id": "a1", + "content": [{"type": "output_text", "text": "我是 Codex。"}], + }, + }, + { + "timestamp": "2026-08-08T13:47:34.000Z", + "type": "response_item", + "payload": { + "type": "function_call", + "id": "t1", + "name": "shell", + "arguments": '{"command":"ls"}', + }, + }, + { + "timestamp": "2026-08-08T13:47:35.000Z", + "type": "response_item", + "payload": {"type": "function_call_output", "id": "t2", "output": "a.txt"}, + }, + { + "timestamp": "2026-08-08T13:47:36.000Z", + "type": "turn_context", + "payload": {"cwd": "/tmp", "model": "x"}, + }, + ] + text = "\n".join(json.dumps(record, ensure_ascii=False) for record in records) + + parsed = parse_codex_text(text) + + assert [event.event_type for event in parsed.events] == [ + "user_message", + "assistant_message", + "tool_call", + "tool_result", + ] + assert parsed.events[0].content == "现在是什么模型\n" + assert parsed.events[1].content == "我是 Codex。" + assert parsed.events[2].content.startswith("shell ") + assert parsed.events[3].content == "a.txt" + assert parsed.skipped_own_results == 1 # developer instructions 是宿主管线 + + +def test_codex_does_not_store_the_same_turn_twice() -> None: + """event_msg 与 response_item 会带同一段文本:只入库一次。""" + from octop.infra.agents.claude_transcript import parse_codex_text + + records = [ + { + "timestamp": "2026-08-08T13:47:31.163Z", + "type": "event_msg", + "payload": {"type": "user_message", "message": "同一个问题"}, + }, + { + "timestamp": "2026-08-08T13:47:31.900Z", + "type": "response_item", + "payload": { + "type": "message", + "role": "user", + "id": "u9", + "content": [{"type": "input_text", "text": "同一个问题"}], + }, + }, + ] + text = "\n".join(json.dumps(record, ensure_ascii=False) for record in records) + + parsed = parse_codex_text(text) + + assert [event.content for event in parsed.events] == ["同一个问题"] + assert parsed.skipped_own_results == 1 + + +def test_cursor_transcript_shapes_are_tolerated() -> None: + """Cursor 的 transcript_path 格式未公开:JSONL / 数组 / {messages:[]} 都要能读。""" + from octop.infra.agents.claude_transcript import parse_cursor_text + + pairs = [("user", "怎么接入?"), ("assistant", "两条 hook 就够了。")] + shapes = [ + "\n".join(json.dumps({"role": r, "content": c}, ensure_ascii=False) for r, c in pairs), + json.dumps([{"role": r, "text": c} for r, c in pairs], ensure_ascii=False), + json.dumps( + {"messages": [{"role": r, "content": [{"type": "text", "text": c}]} for r, c in pairs]}, + ensure_ascii=False, + ), + ] + + for text in shapes: + parsed = parse_cursor_text(text) + assert [event.event_type for event in parsed.events] == [ + "user_message", + "assistant_message", + ], text[:60] + assert parsed.events[0].content == "怎么接入?" + + +def test_cursor_agent_transcript_jsonl_keeps_text_and_drops_tool_blocks() -> None: + """stop 交给的真实文件:一行一个 {role, message:{content:[{type,text}]}}。""" + from octop.infra.agents.claude_transcript import parse_cursor_text + + lines = [ + { + "role": "user", + "message": {"content": [{"type": "text", "text": "怎么接入?"}]}, + }, + { + "role": "assistant", + "message": { + "content": [ + {"type": "text", "text": "两条 hook 就够了。"}, + {"type": "tool_use", "name": "read", "input": {"path": "a"}}, + ] + }, + }, + ] + text = "\n".join(json.dumps(line, ensure_ascii=False) for line in lines) + + parsed = parse_cursor_text(text) + + assert [event.event_type for event in parsed.events] == [ + "user_message", + "assistant_message", + ] + assert parsed.events[0].content == "怎么接入?" + assert parsed.events[1].content == "两条 hook 就够了。" + + +def test_cursor_skips_entries_without_a_conversational_role() -> None: + from octop.infra.agents.claude_transcript import parse_cursor_text + + text = json.dumps( + [ + {"role": "user", "content": "问题"}, + {"kind": "toolCall", "tool": "read", "args": {"path": "a"}}, + {"role": "assistant", "content": "回答"}, + ], + ensure_ascii=False, + ) + + parsed = parse_cursor_text(text) + + assert [event.event_type for event in parsed.events] == ["user_message", "assistant_message"] + assert parsed.skipped_own_results == 1 + + +def test_cursor_delta_uploads_do_not_collide_on_line_index_src_key() -> None: + """stop hook ships byte deltas; each chunk used to renumber from line0. + + Distinct user turns at the head of consecutive deltas must get distinct + ``src_key`` values, otherwise the later user message is dropped as + ``skipped_existing`` while later assistant lines still import. + """ + from octop.infra.agents.claude_transcript import parse_cursor_text + + def _line(role: str, text: str) -> str: + return json.dumps( + {"role": role, "message": {"content": [{"type": "text", "text": text}]}}, + ensure_ascii=False, + ) + + delta_a = "\n".join( + [ + _line("user", "上一轮用户问题"), + _line("assistant", "上一轮助手回答"), + ] + ) + delta_b = "\n".join( + [ + _line("user", "原始素材只有回复,没有用户消息"), + _line("assistant", "这是助手的长回复,超过十二个字符。"), + ] + ) + + keys_a = {event.payload["src_key"] for event in parse_cursor_text(delta_a).events} + parsed_b = parse_cursor_text(delta_b) + keys_b = [event.payload["src_key"] for event in parsed_b.events] + + assert parsed_b.events[0].event_type == "user_message" + assert parsed_b.events[0].content == "原始素材只有回复,没有用户消息" + assert keys_b[0] not in keys_a + assert keys_b[0].startswith("c") and keys_b[0].endswith(":0") + # Re-parsing the same delta stays idempotent. + assert parse_cursor_text(delta_b).events[0].payload["src_key"] == keys_b[0] + + +def test_cursor_prefers_explicit_record_id_for_src_key() -> None: + from octop.infra.agents.claude_transcript import parse_cursor_text + + text = json.dumps( + {"id": "msg_abc", "role": "user", "content": "有稳定 id"}, + ensure_ascii=False, + ) + parsed = parse_cursor_text(text) + assert parsed.events[0].payload["src_key"] == "msg_abc:0" diff --git a/tests/unit/agents/test_client_setup.py b/tests/unit/agents/test_client_setup.py new file mode 100644 index 0000000000..cb176954eb --- /dev/null +++ b/tests/unit/agents/test_client_setup.py @@ -0,0 +1,1392 @@ +"""Unit tests for the interactive local-terminal installer generator.""" + +from __future__ import annotations + +import ast +import json +import os +import subprocess +import tomllib +from pathlib import Path + +import pytest + +from octop.infra.agents.client_setup import ( + _DSH_NATIVE_FILES, + _DSH_NATIVE_NAME, + _DSH_NATIVE_SRC, + _MERGE_PY, + _SELECT_PY, + _UNINSTALL_PY, + HOOKS_DIRS, + TERMINALS, + render_setup_ps1, + render_setup_script, + render_uninstall_ps1, + render_uninstall_script, +) + +_URL = "http://127.0.0.1:8088/mcp/memory/" +_TOKEN = "omcp_test_token" + +posix_only = pytest.mark.skipif(os.name != "posix", reason="假的 dsh 命令用 POSIX 可执行脚本") + + +def _script(**overrides) -> str: + kwargs = {"mcp_url": _URL} + kwargs.update(overrides) + return render_setup_script(**kwargs) + + +# ---------------------------------------------------------------- 生成脚本的静态形态 + + +def test_installer_detects_the_local_terminals() -> None: + script = _script() + + assert '[ -e "$HOME/.claude.json" ] || [ -d "$HOME/.claude" ] || command -v claude' in script + assert '[ -d "$HOME/.kiro" ] || command -v kiro' in script + assert '[ -d "$HOME/.dsh" ] || command -v dsh' in script + assert 'echo " 1) Claude Code' in script + assert 'echo " 2) Kiro' in script + assert 'echo " 3) DSH' in script + + +def test_installer_asks_for_a_user_mcp_token_not_a_user_id() -> None: + """凭据是**用户级 MCP token**:只问 token + 专家 ID,不再问「用户标识」。""" + script = _script() + + # 交互走 /dev/tty:curl | sh 时 stdin 是脚本本身,直接 read 会把脚本吃掉 + assert "if (true /dev/null; then HAS_TTY=1; fi" in script + assert script.count("< /dev/tty") >= 3 # 探测 + 选择 + token + 专家 ID + assert "要配置哪些?" in script + assert "专家 ID: " in script + # token 提示说明去哪儿创建(omcp_…),且不回显 + assert "MCP token(omcp_…,在 Octop 的 /api/memory-mcp/tokens 创建;不回显)" in script + assert "stty -echo" in script + # 没有 tty 时不盲读 stdin,而是明确要求环境变量 + assert 'TOKEN="${OCTOP_MEMORY_TOKEN:-}"' in script + assert "非交互运行时请设 OCTOP_MEMORY_TOKEN" in script + assert "非交互运行时请设 OCTOP_AGENT_ID" in script + # 「用户标识」这一整套概念消失:token 就代表「谁问的」 + assert "OCTOP_USER_ID" not in script + assert "X-Octop-User-Id" not in script + assert "用户标识" not in script + assert "userId" not in script + # 也不再有第二份凭据 / OAuth 前置 / token 方言 + assert "OCTOP_ENTRY_TOKEN" not in script + assert "OCTOP_DISABLE_OAUTH" not in script + assert "个人令牌" not in script + assert "OCTOP_TOKEN_HEADER" not in script + + +def test_terminal_pick_allows_multiselect() -> None: + """五个选项可多选:1,2 / 1 3 / 12 / a=全部,回车=全部检测到的。""" + script = _script() + + assert "可多选:1,2 / 1 3 / 12 / a=全部" in script + assert "''|a|A|all|ALL|全部) _chosen=\"$DETECTED\"" in script + assert "fold -w1" in script # 连写数字 12/13 按位拆开 + assert 'case ",$_chosen," in *",$_add,"*' in script # 去重 + assert "已忽略" in script # 未检测到/无法识别 + assert "没有选中任何已检测到的终端,请重选" in script # 选不中会重问 + assert 'while [ -z "$TERMINALS" ]' in script + + +def test_installer_embeds_no_credential() -> None: + """没内置默认值时不内嵌密钥:token 由运行的人粘贴自己的。""" + script = _script() + + assert "DEFAULT_TOKEN=" not in script + assert "Bearer " in script # 只在运行时按输入拼头,不是写死的 token + # 只有一个 header 方言:Authorization → Bearer + assert "TOKEN_HEADER='Authorization'" in script + assert 'TOKEN_HEADER="${OCTOP_TOKEN_HEADER:-Authorization}"' not in script + + +def test_default_token_is_an_optional_prefill() -> None: + """``default_token`` 只是可选预填;环境变量仍然优先,没预填就现场问。""" + script = _script(default_token="omcp_prefilled") + + assert "DEFAULT_TOKEN='omcp_prefilled'" in script + assert "if [ -z \"$TOKEN\" ] && [ -n 'omcp_prefilled' ]; then" in script + assert "用安装链接预填的值" in script + # 预填只填 TOKEN;凭据仍是运行人自己的 + assert "OCTOP_MEMORY_TOKEN:-" in script + + +def test_known_values_only_prefill_prompts() -> None: + script = _script(agent_id="RY9RQC", terminals="kiro") + + assert "DEFAULT_AGENT='RY9RQC'" in script + assert "DEFAULT_TERMINALS='kiro'" in script + + +def test_setup_script_shell_quotes_query_prefills() -> None: + script = _script(agent_id="x';id", terminals="cursor';id", project="/tmp/p';id") + # Classic POSIX single-quote escape: ' → '"'"' + assert "DEFAULT_AGENT='x'\"'\"';id'" in script + assert "DEFAULT_TERMINALS='cursor'\"'\"';id'" in script + assert "DEFAULT_PROJECT='/tmp/p'\"'\"';id'" in script + assert "DEFAULT_AGENT='x';id'" not in script + assert "DEFAULT_TERMINALS='cursor';id'" not in script + + +def test_each_terminal_gets_its_own_hooks_directory() -> None: + script = _script() + + assert "CC_DIR='~/.claude/hooks-events'" in script + assert "KIRO_DIR='~/.kiro/hooks'" in script + assert "DSH_DIR='~/.dsh/hooks'" in script + assert "CODEX_DIR='~/.codex/hooks'" in script + assert "CURSOR_DIR='~/.cursor/hooks'" in script + assert HOOKS_DIRS == { + "claude-code": "~/.claude/hooks-events", + "kiro": "~/.kiro/hooks", + "dsh": "~/.dsh/hooks", + "codex": "~/.codex/hooks", + "cursor": "~/.cursor/hooks", + } + + +def test_claude_code_uses_two_command_hooks() -> None: + """Claude Code:召回与上报都必须是 command hook。 + + - ``UserPromptSubmit`` 上 `mcp_tool` 会被静默跳过(实测 2.1.263); + - ``Stop`` 用脚本,脚本自己后台脱离+延迟,等本轮落盘后再读 transcript + (``SessionEnd`` 不可用:那里没有 MCP 客户端上下文)。 + """ + script = _script() + + assert ( + 'search = {"type": "command", "command": hook_command(f"{hooks_dir}/claude-code/user-prompt-recall.mjs")}' + in script + ) + assert ( + 'stop = {"type": "command", "command": hook_command(f"{hooks_dir}/claude-code/stop-capture.mjs")}' + in script + ) + assert "该事件不支持 ``mcp_tool``" in script + assert "SessionEnd`` 不能用" in script + # 简写形态:事件直接挂 handler 数组,不套 matcher 组那层 + assert 'return {"UserPromptSubmit": [search], "Stop": [stop]}' in script + # 收尾提示按事件名列出两端角色(Cursor 的事件名不同,故不能只写 UserPromptSubmit/Stop) + assert "用户提交时召回" in script and "memory_search" in script + assert "回合结束时上报" in script and "memory_capture" in script + assert "Cursor 的 beforeSubmitPrompt" in script + + +def test_claude_code_clears_the_legacy_session_end_hook() -> None: + """早期版本挂过 SessionEnd(mcp_tool 在那里不可用、会持续报错):重装时要清掉。""" + script = _script() + + assert '_upsert_groups(st, "SessionEnd", [])' in script + assert 'st["hooks"].pop("SessionEnd", None)' in script + assert 'hooks["SessionEnd"]' not in script # 不再定义 + + +def test_previous_inline_hook_command_is_replaced_by_the_wrapper(tmp_path) -> None: + """老配置里那串内联 node 命令(含 ``.mjs`` 与转义字符)重装时换成包装脚本,不留重复。""" + legacy = ( + "/usr/bin/node /home/u/.claude/hooks-events/claude-code/stop-capture.mjs " + '|| echo "octop-memory: hook 执行失败(node 路径不对或脚本报错)" >&2' + ) + home, _ = _run_claude_code_installer( + tmp_path, {}, {"hooks": {"Stop": [{"hooks": [{"type": "command", "command": legacy}]}]}} + ) + stops = json.loads((home / ".claude/settings.json").read_text(encoding="utf-8"))["hooks"][ + "Stop" + ] + assert len(stops) == 1 # 老命令被替换,没有并列 + assert stops[0]["type"] == "command" # 换成简写形态(事件直接挂 handler) + command = stops[0]["command"] + assert command.endswith("claude-code/stop-capture.sh") + assert ".mjs" not in command # 配置里不再有那串长命令 + assert (home / ".claude/hooks-events/claude-code/stop-capture.sh").exists() + + +def test_foreign_hook_groups_are_kept_next_to_ours(tmp_path) -> None: + """别人的 hook(matcher 组或裸 handler)都要原样留着,只动我们自己那两条。""" + settings = { + "hooks": { + "Stop": [ + {"matcher": "*", "hooks": [{"type": "command", "command": "git-ai checkpoint"}]}, + {"type": "command", "command": "/usr/local/bin/other-check"}, + ] + } + } + home, _ = _run_claude_code_installer(tmp_path, {}, settings) + stops = json.loads((home / ".claude/settings.json").read_text(encoding="utf-8"))["hooks"][ + "Stop" + ] + + assert stops[0] == { + "matcher": "*", + "hooks": [{"type": "command", "command": "git-ai checkpoint"}], + } + assert stops[1] == {"type": "command", "command": "/usr/local/bin/other-check"} + assert stops[2]["command"].endswith("claude-code/stop-capture.sh") # 我们的追加在最后 + + +def test_hook_scripts_are_installed_for_every_hook_terminal() -> None: + """四个 hook 宿主各装一份脚本 + config.json;DSH 走原生插件,不装 hook 脚本。""" + script = _script() + + assert "SCRIPT_OK" not in script + assert "CAPTURE_PROMPT" not in script # 上报绝不走"让模型总结"的提示词 + assert "def do_claude_code():\n # 两条 hook 都是 command 脚本" in script + assert '_install_hooks(CC_DIR, "claude-code")' in script + assert '_install_hooks(KIRO_DIR, "kiro")' in script + assert '_install_hooks(CODEX_DIR, "codex")' in script + assert '_install_hooks(CURSOR_DIR, "cursor")' in script + # DSH 不再装 hooks(原生插件在进程内做召回/上报) + assert '_install_hooks(DSH_DIR, "dsh")' not in script + assert '"dsh": ("dsh/stop-capture.mjs"' not in script + + +def test_kiro_capture_is_always_a_command_hook() -> None: + """Kiro:召回用提示词(Kiro 只支持 runCommand/askAgent);上报始终用脚本提交整段 jsonl。""" + script = _script() + + kiro = script[script.index("def do_kiro():") : script.index("_CODEX_HOOKS = ")] + # 召回仍然是 agent 提示词(runCommand 的 stdout 不会被注入上下文) + assert '"action": {"type": "agent", "prompt": SEARCH_PROMPT}' in kiro + # 上报只有 command 一种形态,没有提示词兜底 + assert ( + '{"type": "command", "command": hook_command(f"{KIRO_DIR}/kiro/stop-capture.mjs")}' in kiro + ) + assert '"type": "agent", "prompt": CAPTURE_PROMPT' not in kiro + assert "提交整段 messages.jsonl" in kiro + assert "Kiro 上报 hook" in script + + +def test_prompts_carry_the_role_instructions() -> None: + script = _script() + + assert "先用 memory_search 检索" in script + # 上报没有提示词:模型"总结会话"不算捕获原始会话 + assert "用 memory_capture 记录" not in script + + +def test_generated_script_embeds_each_hosts_scripts() -> None: + """按宿主分别内嵌:共享传输模块 + 该宿主的入口脚本。""" + script = _script() + + assert "OCTOP_FILES_CLAUDE_CODE=" in script + assert "OCTOP_FILES_KIRO=" in script + assert "OCTOP_FILES_CODEX=" in script and "OCTOP_FILES_CURSOR=" in script + assert "OCTOP_FILES_DSH=" not in script # 没有 dsh hook 脚本可装 + assert "OCTOP_FILES_PRESET=" not in script # preset 方案已废弃 + assert "HOOK_FILES = {" in script and "OCTOP_FILES_" in script + assert "lib/octop-memory-client.mjs" in script + assert "claude-code/stop-capture.mjs" in script and "kiro/stop-capture.mjs" in script + assert "codex/user-prompt-recall.mjs" in script and "codex/stop-capture.mjs" in script + assert "cursor/user-prompt-recall.mjs" in script and "cursor/stop-capture.mjs" in script + # 旧版的单文件变量不再使用 + assert "OCTOP_STOP_B64" not in script and "OCTOP_RECALL_B64" not in script + + +def test_dsh_native_plugin_manifest_is_vendored() -> None: + """原生插件按 ``_DSH_NATIVE_FILES`` 逐字节内嵌(安装器不依赖插件仓/git)。""" + script = _script() + + assert "OCTOP_FILES_NATIVE=" in script + for name in _DSH_NATIVE_FILES: + assert (_DSH_NATIVE_SRC / name).is_file(), f"缺少 vendored 插件文件:{name}" + # 清单里的每个文件都被打进脚本(base64,脚本里看不到明文插件源码) + assert _DSH_NATIVE_NAME == "octop-memory-native" + assert "def _install_dsh_native_files():" in script + assert "def _install_dsh_native():" in script + + +def test_dsh_native_install_registers_profile_and_patch_block() -> None: + """DSH:写插件目录 → 登记 dependencies / dsh.profile.bundles → 同 id 覆盖 cordis.patch.yml。""" + script = _script() + + assert "def _write_dsh_native_patch(profile_dir, config):" in script + assert "def _replace_native_patch(text, block, row_id=None):" in script + assert "def _register_dsh_native(profile_dir, bundle, profile):" in script + assert 'dependencies[NATIVE_NAME] = "link:" + str(bundle)' in script + assert "bundles.append(NATIVE_NAME)" in script + # 配置块只覆盖 config,不再 insert(重复 id 会让 Loader 直接崩) + assert "插件自己的 bundle patch 已经 insert 了同 id 的行" in script + assert '"dsh", "plugin", "--profile", profile, "add", str(bundle)' in script + assert '"pnpm", "add", str(bundle)' in script # 源码 checkout 常没有 dsh 命令 + assert "没找到 DSH profile" in script + assert "重启 DSH" in script + + +def test_dsh_native_config_block_uses_the_user_token_shape() -> None: + """配置块里的连接信息 = 用户级 MCP token 形状(endpoint/token/tokenHeader/agentId)。""" + script = _script() + + block = script[script.index("def _dsh_native_patch_block(config):") :] + block = block[: block.index("def _is_patch_entry(")] + assert 'for key in ("endpoint", "token", "tokenHeader", "agentId"):' in block + assert "userId" not in block + config = script[script.index("def _dsh_native_config():") : script.index("def _yaml_scalar(")] + assert '"tokenHeader": TOKEN_HEADER' in config + assert "userId" not in config + + +def test_dsh_install_cleans_the_legacy_hook_bridge_and_preset() -> None: + """两套接法同时生效会双触发:升级路径先拆旧桥接 / preset bundle。""" + script = _script() + + assert "def _remove_dsh_legacy_hooks():" in script + assert '"dsh-memory-share", "octop-dsh-memory-share"' in script + bridge_cleanup = script[ + script.index("def _remove_dsh_legacy_hooks():") : script.index("def _dsh_mcp_hint():") + ] + assert 'DSH_DIR / "octop-hooks.json"' in bridge_cleanup + assert "shutil.rmtree(scripts)" in bridge_cleanup + + +def test_loopback_targets_use_authorization_bearer() -> None: + """条目与 hooks 都是 ``Authorization: Bearer `` + ``X-Octop-Agent-Id``。""" + script = _script() + + assert '"Authorization": f"Bearer {TOKEN}"' in script + assert 'headers = {\n "X-Octop-Agent-Id": AGENT,' in script + assert "客户端 MCP 条目与 hooks:" in script + assert "Authorization: Bearer + X-Octop-Agent-Id" in script + + +def test_node_is_resolved_to_an_absolute_path() -> None: + """hook 由终端派生、常在非 login shell 里跑:node 必须解析成绝对路径。""" + script = _script() + + assert 'NODE_BIN="${OCTOP_NODE:-}"' in script + assert "command -v node" in script + assert "$HOME/.nvm/versions/node" in script # nvm 是最常见的坑 + assert "/opt/homebrew/bin/node" in script and "/usr/local/bin/node" in script + assert "找不到 node 的绝对路径" in script # 都找不到时明确告警 + assert 'export OCTOP_NODE="$NODE_BIN"' in script + + +def test_hook_commands_use_the_resolved_node_and_report_failures() -> None: + script = _script() + + assert 'NODE = os.environ.get("OCTOP_NODE") or "node"' in script + assert "def hook_command(script_path):" in script + assert 'hook_command(f"{hooks_dir}/claude-code/stop-capture.mjs")' in script + assert 'hook_command(f"{KIRO_DIR}/kiro/stop-capture.mjs")' in script + # 非 0 退出不再静默:包装脚本把原因打到 stderr + assert "failure = 'echo \"octop-memory: hook 执行失败" in script + # 企业根在系统证书库里、不在 Node 自带库里:Node 22.15+ 才加 --use-system-ca + assert "OCTOP_USE_SYSTEM_CA" in script + assert "--use-system-ca" in script + assert 'NODE_OPTIONS="${NODE_OPTIONS:+$NODE_OPTIONS }--use-system-ca"' in script + + +def test_hook_wrapper_follows_the_platform() -> None: + """POSIX 安装器写 ``.sh``;Windows 安装器写 ``.cmd``(不需要 Git Bash)。""" + script = _script() + + assert "def _sh_hook_command(mjs):" in script + assert "def _cmd_hook_command(mjs):" in script + assert "OCTOP_HOOK_WRAPPER=sh" in script # sh 安装器显式指定 + assert 'wrapper = mjs.with_suffix(".cmd")' in script + assert "if errorlevel 1 echo octop-memory: hook failed" in script + + +def test_installer_names_the_log_and_state_location() -> None: + """日志与增量状态就落在终端自己的 hooks 目录(该目录不可写才退到临时目录)。""" + script = _script() + + assert "日志/增量状态:" in script + assert "{target}/octop-hook.log" in script + assert "{target}/octop-transcript-state.json" in script + assert "退到 {scratch}/octop-hooks" in script + assert '"%TEMP%" if os.name == "nt" else "$TMPDIR"' in script # POSIX / Windows + + +def test_mcp_entry_is_per_terminal_and_keeps_other_entries() -> None: + script = _script() + + assert 'home / ".claude.json"' in script + assert 'home / ".kiro" / "settings" / "mcp.json"' in script + assert "def _octop_servers(servers):" in script # 按 octop-memory* 认已有服务,不认具体名字 + assert "保持原样" in script + assert "if not any(" in script + + +def test_legacy_mcp_tool_hooks_are_recognized_by_tool_not_by_server_name() -> None: + """旧版 mcp_tool hook 的清理按工具名认:服务名可能是 local / prod 或改过名。""" + script = _script() + + assert '_OUR_MCP_TOOLS = ("memory_search", "memory_capture")' in script + assert 'handler.get("tool") in _OUR_MCP_TOOLS' in script + assert 'handler.get("server") == NAME' not in script + + +def test_endpoint_self_check_is_warn_only() -> None: + script = _script() + + assert "端点自检" in script + assert "安装仍继续" in script + # 自检与 hook 共用同一次探测;支持时先 export NODE_OPTIONS,再去 fetch + assert 'if "$NODE_BIN" --use-system-ca -e' in script + assert "export NODE_OPTIONS" in script + assert "AbortSignal.timeout(8000)" in script + # 自检也只发 Authorization Bearer + 专家头 + assert "Authorization: `Bearer ${cfg.token}`" in script + + +def test_server_name_follows_the_endpoint() -> None: + assert "OCTOP_SERVER_NAME='octop-memory-local'" in _script() + remote = _script(mcp_url="https://octop.example.com/mcp/") + assert "OCTOP_SERVER_NAME='octop-memory'" in remote + assert "octop-memory-local" not in remote + + +def test_generator_knows_only_the_supported_terminals() -> None: + assert TERMINALS == ("claude-code", "kiro", "dsh", "codex", "cursor") + + +def test_generated_shell_and_embedded_python_are_valid() -> None: + """脚本是 shell + 内嵌 python 拼出来的:两半都要能独立解析。""" + ast.parse(_MERGE_PY) + ast.parse(_UNINSTALL_PY) + ast.parse(_SELECT_PY) + assert "sys.exit(main())" in _SELECT_PY + + +def test_codex_and_cursor_are_detected_and_offered() -> None: + """两个宿主都要进检测与菜单。""" + script = _script() + + assert '[ -d "$HOME/.codex" ] || command -v codex' in script + assert '[ -d "$HOME/.cursor" ] || command -v cursor' in script + assert 'echo " 4) Codex' in script and 'echo " 5) Cursor' in script + assert '"codex|Codex' in script and '"cursor|Cursor' in script + assert "请重选(1-5,或 a=全部)" in script + assert "4) _add=" in script and "5) _add=" in script + assert '"codex": do_codex' in script and '"cursor": do_cursor' in script + + +def test_terminal_selection_is_an_interactive_checklist() -> None: + """选终端是清单式:↑↓ 移动 · 空格 选择 · a 全选 · 回车 确认。""" + script = _script() + + assert "OCTOP_SELECT" in script + assert "↑/↓ 移动 · 空格 选择 · a 全选 · 回车 确认" in script + assert 'if key == b"\\x1b":' in script # 方向键 ESC [ A/B + assert 'return {b"[A": "up", b"[B": "down"}' in script + assert 'if key == b" ":' in script # 空格切换 + assert "setraw(fd)" in script # raw 模式读单键 + assert 'box = "◉" if chosen[index] else "◯"' in script # 勾选状态可见 + # 一个都没选时回车不确认;无 tty / python3 不可用则回退数字输入 + assert "if not any(chosen):" in script + assert "可多选:1,2 / 1 3 / 12 / a=全部" in script + + +def test_installer_mentions_the_windows_native_installer() -> None: + """sh 版在 Windows(Git Bash/MSYS)上提示改用 PowerShell 原生安装器。""" + script = _script() + + assert "MINGW*|MSYS*|CYGWIN*" in script + assert '?os=windows" | iex' in script + + +def test_no_internal_only_vocabulary_leaks() -> None: + """脱敏底线:安装器不能出现内部产品 / 内网域名 / 内部 token 方言。""" + scripts = ( + _script(), + render_uninstall_script(), + render_setup_ps1(mcp_url=_URL), + render_uninstall_ps1(), + ) + # 内部产品 / 内网域名 / 内部 token 方言:故意拼起来,免得本文件自己被这些词命中。 + forbidden = ( + "chengxiao" + "bang", + "\u7a0b\u5c0f\u5e2e", + "ctrip" + "corp", + "a" + "da_", + "x-" + "bbzai", + "trip" + ".com", + "OCTOP_USER_ID", + "X-Octop-User-Id", + "userId", + "OCTOP_ENTRY_TOKEN", + "OCTOP_DISABLE_OAUTH", + ) + for script in scripts: + for word in forbidden: + assert word not in script, f"安装器泄漏了内部概念:{word}" + + +# ---------------------------------------------------------------- 真跑生成的脚本 + + +def test_generated_installer_actually_runs(tmp_path) -> None: + """把生成的安装脚本真跑一遍(沙箱 HOME)。 + + 脚本是 shell + 内嵌 python 拼出来的:语法正确不等于能跑,拼装错误(变量改名、 + 参数漏传)只有执行才会发现。 + """ + home, result = _run_terminals( + tmp_path, + "claude-code,kiro,dsh,codex,cursor", + prepare=lambda home: _dsh_profile(home), + ) + + # 每个 hook 宿主只有自己的入口脚本 + 一份共享传输模块 + config.json + assert (home / ".claude/hooks-events/claude-code/stop-capture.mjs").exists() + assert (home / ".claude/hooks-events/claude-code/user-prompt-recall.mjs").exists() + assert (home / ".kiro/hooks/kiro/stop-capture.mjs").exists() + assert (home / ".codex/hooks/codex/stop-capture.mjs").exists() + assert (home / ".codex/hooks/codex/user-prompt-recall.mjs").exists() + assert (home / ".cursor/hooks/cursor/stop-capture.mjs").exists() + assert (home / ".cursor/hooks/cursor/user-prompt-recall.mjs").exists() + for hooks_dir in ( + ".claude/hooks-events", + ".kiro/hooks", + ".codex/hooks", + ".cursor/hooks", + ): + assert (home / hooks_dir / "lib/octop-memory-client.mjs").exists() + assert (home / hooks_dir / "config.json").exists() + # DSH 不装 hooks:它拿原生插件 + assert not (home / ".dsh/hooks/dsh").exists() + # 宿主之间不串 + assert not (home / ".claude/hooks-events/kiro").exists() + assert not (home / ".kiro/hooks/claude-code").exists() + # Codex / Cursor 的 hook 配置只引用包装脚本路径(长命令收在 .sh 里) + assert ".codex/hooks/codex/stop-capture.sh" in (home / ".codex/config.toml").read_text( + encoding="utf-8" + ) + cursor_hooks = json.loads((home / ".cursor/hooks.json").read_text(encoding="utf-8")) + assert ( + "cursor/hooks/cursor/user-prompt-recall.sh" + in cursor_hooks["hooks"]["beforeSubmitPrompt"][0]["command"] + ) + assert "cursor/hooks/cursor/stop-capture.sh" in cursor_hooks["hooks"]["stop"][0]["command"] + # 每个宿主的包装脚本:可执行,内容是「绝对 node 路径 + .mjs + 失败兜底」 + for hooks_dir, host in ( + (".claude/hooks-events", "claude-code"), + (".kiro/hooks", "kiro"), + (".codex/hooks", "codex"), + (".cursor/hooks", "cursor"), + ): + wrapper = home / hooks_dir / host / "stop-capture.sh" + text = wrapper.read_text(encoding="utf-8") + assert text.startswith("#!/bin/sh") + assert f"{host}/stop-capture.mjs" in text + assert "hook 执行失败" in text # 非 0 退出不再静默 + assert os.access(wrapper, os.X_OK) + if "node 使用系统证书库" in result.stdout: + assert "--use-system-ca" in text + # DSH:原生插件落盘 + profile 登记 + cordis.patch.yml 配置块 + bundle = home / ".dsh/bundles/octop-memory-native" + for name in _DSH_NATIVE_FILES: + assert (bundle / name).is_file() + profile = home / ".dsh/profiles/web" + package = json.loads((profile / "package.json").read_text(encoding="utf-8")) + assert package["dependencies"]["octop-memory-native"] == f"link:{bundle}" + assert package["dsh"]["profile"]["bundles"] == [ + "@deepseek-ai/dsh-base", + "octop-memory-native", + ] + assert (profile / "node_modules/octop-memory-native").is_symlink() + patch = (profile / "cordis.patch.yml").read_text(encoding="utf-8") + assert patch.count("- id: octop-memory-native") == 1 # 跑两遍也不重复 + assert "endpoint:" in patch and "tokenHeader:" in patch + assert "userId" not in patch + assert "不依赖 ~/.dsh/hooks 的 hook 桥接" in patch + + +_CODEX_EXISTING = """model = "gpt-5" + +[[hooks.PreToolUse]] + +[[hooks.PreToolUse.hooks]] +command = "/usr/local/bin/git-ai checkpoint codex" +type = "command" + +[[hooks.Stop]] + +[[hooks.Stop.hooks]] +command = "/usr/local/bin/git-ai checkpoint codex" +type = "command" + +[hooks.state."CONFIG:stop:0:0"] +enabled = true +trusted_hash = "sha256:deadbeef" +""" + + +def _dsh_profile(home: Path, bundles: tuple[str, ...] = ("@deepseek-ai/dsh-base",)) -> Path: + """沙箱里的 DSH profile(``~/.dsh/profiles/web/package.json``)。""" + profile = home / ".dsh/profiles/web" + profile.mkdir(parents=True, exist_ok=True) + (profile / "package.json").write_text( + json.dumps( + {"name": "profile-web", "dsh": {"profile": {"bundles": list(bundles)}}}, indent=2 + ) + + "\n", + encoding="utf-8", + ) + return profile + + +def _run_terminals(tmp_path, terminals: str, prepare=None, extra_env=None, home_name="home"): + """按给定终端跑两遍安装器(第二遍验证幂等),返回 (HOME, 最后一次输出)。""" + home = tmp_path / home_name + home.mkdir(parents=True, exist_ok=True) + if prepare is not None: + prepare(home) + script = tmp_path / f"setup-{home_name}.sh" + script.write_text(_script(), encoding="utf-8") + env = { + **os.environ, + "HOME": str(home), + "OCTOP_TERMINALS": terminals, + "OCTOP_MEMORY_TOKEN": _TOKEN, + "OCTOP_AGENT_ID": "AGT1", + **(extra_env or {}), + } + result = None + for _ in range(2): + result = subprocess.run( + ["sh", str(script)], env=env, capture_output=True, text=True, timeout=300 + ) + assert result.returncode == 0, result.stdout + result.stderr + assert result is not None + return home, result + + +def _run_uninstall(tmp_path, home: Path, terminals: str, extra_env=None): + script = tmp_path / "uninstall.sh" + script.write_text(render_uninstall_script(), encoding="utf-8") + env = { + **os.environ, + "HOME": str(home), + "OCTOP_TERMINALS": terminals, + **(extra_env or {}), + } + result = subprocess.run( + ["sh", str(script)], env=env, capture_output=True, text=True, timeout=120 + ) + assert result.returncode == 0, result.stdout + result.stderr + return result + + +def test_config_and_entry_carry_the_user_mcp_token(tmp_path) -> None: + """config.json 与 MCP 条目都是「MCP 地址 + 用户 token + 专家 ID」,没有 userId。""" + home, _ = _run_terminals(tmp_path, "claude-code") + + config = json.loads((home / ".claude/hooks-events/config.json").read_text(encoding="utf-8")) + assert config == { + "url": _URL, + "token": _TOKEN, + "tokenHeader": "Authorization", + "agentId": "AGT1", + } + + entry = json.loads((home / ".claude.json").read_text(encoding="utf-8"))["mcpServers"][ + "octop-memory-local" + ] + assert entry["url"] == _URL + assert entry["headers"] == { + "X-Octop-Agent-Id": "AGT1", + "Authorization": f"Bearer {_TOKEN}", + } + + +def test_entry_defaults_to_the_instance_mount(tmp_path) -> None: + """没有声明公网地址时,条目与 hooks 都落在实例自己的挂载点。""" + instance = "https://octop.example.com/mcp/memory/" + home = tmp_path / "home" + (home / ".claude").mkdir(parents=True) + script = tmp_path / "setup.sh" + script.write_text(_script(mcp_url=instance), encoding="utf-8") + result = subprocess.run( + ["sh", str(script)], + env={ + **os.environ, + "HOME": str(home), + "OCTOP_TERMINALS": "claude-code", + "OCTOP_MEMORY_TOKEN": "shared-token", + "OCTOP_AGENT_ID": "AGT1", + }, + capture_output=True, + text=True, + timeout=180, + ) + + assert result.returncode == 0, result.stdout + result.stderr + servers = json.loads((home / ".claude.json").read_text(encoding="utf-8"))["mcpServers"] + entry = next(iter(servers.values())) + assert entry["url"] == instance + assert entry["headers"] == { + "X-Octop-Agent-Id": "AGT1", + "Authorization": "Bearer shared-token", + } + + +def test_declared_public_url_becomes_the_entry_address(tmp_path) -> None: + """条目地址与 hooks 地址可以分开:条目走部署声明的公网地址,hooks 打实例挂载点。""" + public = "https://octop-memory.example.com/mcp/memory/" + direct = "https://octop.example.com/mcp/memory/" + home = tmp_path / "home" + (home / ".claude").mkdir(parents=True) + script = tmp_path / "setup.sh" + script.write_text(_script(mcp_url=public, hook_url=direct), encoding="utf-8") + result = subprocess.run( + ["sh", str(script)], + env={ + **os.environ, + "HOME": str(home), + "OCTOP_TERMINALS": "claude-code", + "OCTOP_MEMORY_TOKEN": "shared-token", + "OCTOP_AGENT_ID": "AGT1", + }, + capture_output=True, + text=True, + timeout=180, + ) + + assert result.returncode == 0, result.stdout + result.stderr + servers = json.loads((home / ".claude.json").read_text(encoding="utf-8"))["mcpServers"] + name, entry = next(iter(servers.items())) + assert name == "octop-memory" + assert entry["url"] == public + config = json.loads((home / ".claude/hooks-events/config.json").read_text(encoding="utf-8")) + assert config["url"] == direct and config["token"] == "shared-token" + + +def test_without_a_token_the_installer_still_demands_one(tmp_path) -> None: + """无 tty + 没给环境变量 → 明确报错,不盲读 stdin。""" + script = tmp_path / "setup.sh" + script.write_text(_script(), encoding="utf-8") + home = tmp_path / "home" + (home / ".claude").mkdir(parents=True) + env = { + **os.environ, + "HOME": str(home), + "OCTOP_TERMINALS": "claude-code", + "OCTOP_AGENT_ID": "AGT1", + } + env.pop("OCTOP_MEMORY_TOKEN", None) + result = subprocess.run( + ["sh", str(script)], env=env, capture_output=True, text=True, timeout=180 + ) + + assert result.returncode != 0 + assert "缺少 MCP token" in result.stderr + assert "DEFAULT_TOKEN=" not in _script() # 没预填值就不该有赋值 + + +def test_codex_hooks_join_the_existing_config_without_clobbering_it(tmp_path) -> None: + """~/.codex/config.toml 已有别人的 hook:只追加我们的,别的字段与信任状态原样保留。""" + + def prepare(home): + (home / ".codex").mkdir(parents=True) + config = home / ".codex/config.toml" + (config).write_text(_CODEX_EXISTING.replace("CONFIG", str(config)), encoding="utf-8") + + home, _ = _run_terminals(tmp_path, "codex", prepare) + cfg = tomllib.loads((home / ".codex/config.toml").read_text(encoding="utf-8")) + hooks = cfg["hooks"] + + def commands(event): + return [entry["command"] for group in hooks[event] for entry in group.get("hooks", [])] + + assert cfg["model"] == "gpt-5" # 其它字段不动 + assert len(commands("PreToolUse")) == 1 and "git-ai" in commands("PreToolUse")[0] + assert len(commands("Stop")) == 2 # 别人的 + 我们的(跑了两遍也不重复) + assert any("codex/stop-capture.sh" in command for command in commands("Stop")) + assert len(commands("UserPromptSubmit")) == 1 + assert any("codex/user-prompt-recall.sh" in command for command in commands("UserPromptSubmit")) + assert list(hooks["state"]) == [f"{home}/.codex/config.toml:stop:0:0"] # 信任状态保留 + + +def test_cursor_hooks_join_the_existing_json_without_clobbering_it(tmp_path) -> None: + """~/.cursor/hooks.json 其它事件(如 git-ai 的 postToolUse)原样保留。""" + + def prepare(home): + (home / ".cursor").mkdir(parents=True) + (home / ".cursor/hooks.json").write_text( + json.dumps( + {"hooks": {"postToolUse": [{"command": "git-ai cursor"}]}, "version": 1}, indent=2 + ) + + "\n", + encoding="utf-8", + ) + + home, _ = _run_terminals(tmp_path, "cursor", prepare) + cfg = json.loads((home / ".cursor/hooks.json").read_text(encoding="utf-8")) + + assert cfg["version"] == 1 + assert cfg["hooks"]["postToolUse"] == [{"command": "git-ai cursor"}] + assert len(cfg["hooks"]["beforeSubmitPrompt"]) == 1 # 幂等:跑两遍仍只有一条 + assert ( + "cursor/hooks/cursor/user-prompt-recall.sh" + in cfg["hooks"]["beforeSubmitPrompt"][0]["command"] + ) + assert len(cfg["hooks"]["stop"]) == 1 + assert "cursor/hooks/cursor/stop-capture.sh" in cfg["hooks"]["stop"][0]["command"] + + +# ---------------------------------------------------------------- DSH 原生插件 + + +@posix_only +def test_dsh_native_install_is_idempotent(tmp_path) -> None: + """DSH:插件目录、profile 登记、配置块都幂等(跑两遍结果一致)。""" + + def prepare(home): + (home / ".dsh").mkdir(parents=True) + _dsh_profile(home) + + home, _ = _run_terminals(tmp_path, "dsh", prepare) + bundle = home / ".dsh/bundles/octop-memory-native" + profile = home / ".dsh/profiles/web" + + for name in _DSH_NATIVE_FILES: + assert (bundle / name).is_file() + # 插件是从 vendored 副本逐字节还原的 + for name in _DSH_NATIVE_FILES: + assert (bundle / name).read_bytes() == (_DSH_NATIVE_SRC / name).read_bytes() + + package = json.loads((profile / "package.json").read_text(encoding="utf-8")) + assert package["dependencies"] == {"octop-memory-native": f"link:{bundle}"} + assert package["dsh"]["profile"]["bundles"] == ["@deepseek-ai/dsh-base", "octop-memory-native"] + assert (profile / "node_modules/octop-memory-native").is_symlink() + + patch = (profile / "cordis.patch.yml").read_text(encoding="utf-8") + assert patch.count("- id: octop-memory-native") == 1 + assert "# <<< octop-memory-native <<<" in patch + # 配置块 = 用户级 token 形状(无 userId) + assert " endpoint: 'http://127.0.0.1:8088/mcp/memory/'" in patch + assert " token: 'omcp_test_token'" in patch + assert " tokenHeader: 'Authorization'" in patch + assert " agentId: 'AGT1'" in patch + assert "userId" not in patch + + +@posix_only +def test_dsh_native_install_keeps_other_patch_entries(tmp_path) -> None: + """profile 的 cordis.patch.yml 里别人的条目 / 注释逐字保留,我们只追加自己的块。""" + existing = ( + "# 用户自己的 patch\n" + "- insert:\n" + " - id: some-other-plugin\n" + " name: 'some-other-plugin'\n" + ) + + def prepare(home): + (home / ".dsh").mkdir(parents=True) + profile = _dsh_profile(home) + (profile / "cordis.patch.yml").write_text(existing, encoding="utf-8") + + home, _ = _run_terminals(tmp_path, "dsh", prepare) + patch = (home / ".dsh/profiles/web/cordis.patch.yml").read_text(encoding="utf-8") + + assert patch.startswith(existing) + assert patch.count("- id: some-other-plugin") == 1 + assert patch.count("- id: octop-memory-native") == 1 + # 覆盖前备份原文件 + assert (home / ".dsh/profiles/web/cordis.patch.yml.octop.bak").read_text( + encoding="utf-8" + ) == existing + + +@posix_only +def test_dsh_install_removes_the_legacy_bridge_and_preset(tmp_path) -> None: + """从 hooks 方案升级:旧桥接 / preset bundle / 脚本目录都拆掉,避免双触发。""" + + def prepare(home): + (home / ".dsh").mkdir(parents=True) + shim = {"name": "profile-web", "dsh": {"profile": {"bundles": ["dsh-memory-share"]}}} + profile = home / ".dsh/profiles/web" + profile.mkdir(parents=True) + (profile / "package.json").write_text(json.dumps(shim, indent=2) + "\n", encoding="utf-8") + preset = home / ".dsh/bundles/dsh-memory-share" + preset.mkdir(parents=True) + (preset / "package.json").write_text("{}", encoding="utf-8") + (home / ".dsh/hooks").mkdir(parents=True) + (home / ".dsh/hooks/octop-hooks.json").write_text("{}", encoding="utf-8") + (home / ".dsh/hooks/dsh").mkdir() + (home / ".dsh/hooks/dsh/stop-capture.mjs").write_text("//", encoding="utf-8") + + home, result = _run_terminals(tmp_path, "dsh", prepare) + profile = home / ".dsh/profiles/web" + + assert not (home / ".dsh/bundles/dsh-memory-share").exists() + assert not (home / ".dsh/hooks/octop-hooks.json").exists() + assert not (home / ".dsh/hooks/dsh").exists() + assert json.loads((profile / "package.json").read_text(encoding="utf-8"))["dsh"]["profile"][ + "bundles" + ] == ["octop-memory-native"] + + +@posix_only +def test_dsh_prefers_the_cli_and_still_registers_the_profile(tmp_path) -> None: + """有 dsh CLI 时优先用它装依赖;登记与配置块仍然由安装器写好(幂等)。""" + + def prepare(home): + (home / ".dsh").mkdir(parents=True) + _dsh_profile(home) + + fake_bin = tmp_path / "bin" + fake_bin.mkdir() + shim = fake_bin / "dsh" + shim.write_text('#!/bin/sh\nprintf \'%s\\n\' "$@" >> "$0.args"\n', encoding="utf-8") + shim.chmod(0o755) + + home, _ = _run_terminals( + tmp_path, + "dsh", + prepare, + extra_env={"PATH": f"{fake_bin}{os.pathsep}{os.environ['PATH']}"}, + ) + profile = home / ".dsh/profiles/web" + + calls = (fake_bin / "dsh.args").read_text(encoding="utf-8") + assert "plugin" in calls and "--profile" in calls and "web" in calls + assert str(home / ".dsh/bundles/octop-memory-native") in calls + assert not (fake_bin / "pnpm.args").exists() # 有 dsh 就不必自己调 pnpm + assert ( + "octop-memory-native" + in json.loads((profile / "package.json").read_text(encoding="utf-8"))["dsh"]["profile"][ + "bundles" + ] + ) + + +@posix_only +def test_dsh_uninstall_removes_the_native_plugin(tmp_path) -> None: + """卸载:插件目录 / profile 登记 / node_modules 条目 / 配置块都清掉。""" + + def prepare(home): + (home / ".dsh").mkdir(parents=True) + _dsh_profile(home) + + home, _ = _run_terminals(tmp_path, "dsh", prepare) + profile = home / ".dsh/profiles/web" + assert (home / ".dsh/bundles/octop-memory-native").exists() + + _run_uninstall(tmp_path, home, "dsh") + + assert not (home / ".dsh/bundles/octop-memory-native").exists() + assert not (profile / "node_modules/octop-memory-native").exists() + package = json.loads((profile / "package.json").read_text(encoding="utf-8")) + assert "octop-memory-native" not in package["dependencies"] + assert package["dsh"]["profile"]["bundles"] == ["@deepseek-ai/dsh-base"] # 别人的不动 + # 只含我们配置块的 patch 文件被删掉 + assert not (profile / "cordis.patch.yml").exists() + + +def test_dsh_is_skipped_in_project_mode(tmp_path) -> None: + """DSH 没有项目级配置:项目模式跳过(不算失败,也不写 DSH 文件)。""" + + def prepare(home): + (home / ".dsh").mkdir(parents=True) + _dsh_profile(home) + + project = tmp_path / "proj" + project.mkdir() + home, result = _run_terminals( + tmp_path, + "claude-code,dsh", + prepare, + extra_env={"OCTOP_PROJECT_DIR": str(project)}, + ) + + assert "项目模式跳过 DSH" in result.stdout + assert not (home / ".dsh/bundles/octop-memory-native").exists() + assert not (home / ".dsh/profiles/web/cordis.patch.yml").exists() + assert (home / ".claude/hooks-events/config.json").exists() # 其它终端照装 + + +# ---------------------------------------------------------------- 项目级安装 + + +def test_project_install_registers_into_the_project_and_keeps_scripts_in_home(tmp_path) -> None: + """项目模式:项目里只写注册;hook 脚本与含 token 的 config.json 留在用户目录。""" + + def prepare(home): + (home / ".claude").mkdir(parents=True) + (home / ".kiro").mkdir(parents=True) + (home / ".codex").mkdir(parents=True) + (home / ".cursor").mkdir(parents=True) + + project = tmp_path / "proj" + project.mkdir() + (project / ".git").mkdir() + home, result = _run_terminals( + tmp_path, + "claude-code,kiro,codex,cursor", + prepare, + extra_env={"OCTOP_PROJECT_DIR": str(project)}, + ) + + assert f"安装范围:项目级 → {project.resolve()}" in result.stdout + # 注册落在项目里 + settings = json.loads((project / ".claude/settings.local.json").read_text(encoding="utf-8")) + assert "UserPromptSubmit" in settings["hooks"] and "Stop" in settings["hooks"] + assert "codex/stop-capture.sh" in (project / ".codex/config.toml").read_text(encoding="utf-8") + cursor = json.loads((project / ".cursor/hooks.json").read_text(encoding="utf-8")) + assert "beforeSubmitPrompt" in cursor["hooks"] and "stop" in cursor["hooks"] + # Kiro 的 hooks 是命令(无凭据)→ 写进项目 + assert (project / ".kiro/hooks/memory-save-on-stop.json").exists() + # 脚本与凭据留在 HOME + for hooks_dir in (".claude/hooks-events", ".kiro/hooks", ".codex/hooks", ".cursor/hooks"): + assert (home / hooks_dir / "config.json").exists() + assert (home / ".claude/hooks-events/claude-code/stop-capture.mjs").exists() + # 项目里没有任何凭据 + for path in project.rglob("*"): + if path.is_file(): + assert _TOKEN not in path.read_text(encoding="utf-8"), path + + +def test_project_mode_skips_the_kiro_mcp_entry(tmp_path) -> None: + """Kiro 的 MCP 条目自带凭据、会随 git 提交:项目模式跳过并提示。""" + + def prepare(home): + (home / ".kiro").mkdir(parents=True) + + project = tmp_path / "proj" + project.mkdir() + home, result = _run_terminals( + tmp_path, "kiro", prepare, extra_env={"OCTOP_PROJECT_DIR": str(project)} + ) + + assert "项目模式跳过" in result.stdout + assert not (project / ".kiro/settings/mcp.json").exists() + # hooks(只有命令)照旧进项目,脚本留在 HOME + assert (project / ".kiro/hooks/memory-recall-on-start.json").exists() + assert (home / ".kiro/hooks/config.json").exists() + + +def test_claude_code_project_mode_uses_the_local_scope(tmp_path) -> None: + """Claude Code 的 MCP 走 ``~/.claude.json`` 的 local scope(按项目路径存,不进仓库)。""" + + def prepare(home): + (home / ".claude").mkdir(parents=True) + + project = tmp_path / "proj" + project.mkdir() + home, _ = _run_terminals( + tmp_path, "claude-code", prepare, extra_env={"OCTOP_PROJECT_DIR": str(project)} + ) + + projects = json.loads((home / ".claude.json").read_text(encoding="utf-8"))["projects"] + key = os.path.realpath(str(project)) + assert key in projects + entry = projects[key]["mcpServers"]["octop-memory-local"] + assert entry["headers"]["Authorization"] == f"Bearer {_TOKEN}" + assert not (project / ".mcp.json").exists() # 不写仓库里的 .mcp.json + + +def test_project_env_wins_over_the_query_prefill(tmp_path) -> None: + """``project=`` 只是交互式预填:非交互只认 OCTOP_PROJECT_DIR,显式置空 = 全局。""" + home = tmp_path / "home" + (home / ".claude").mkdir(parents=True) + project = tmp_path / "prefilled" + project.mkdir() + target = tmp_path / "from-env" + target.mkdir() + script = tmp_path / "setup.sh" + script.write_text(_script(project=str(project)), encoding="utf-8") + base_env = { + **os.environ, + "HOME": str(home), + "OCTOP_TERMINALS": "claude-code", + "OCTOP_MEMORY_TOKEN": _TOKEN, + "OCTOP_AGENT_ID": "AGT1", + } + + # 环境变量优先:装到 from-env,而不是 query 预填的 prefilled + result = subprocess.run( + ["sh", str(script)], + env={**base_env, "OCTOP_PROJECT_DIR": str(target)}, + capture_output=True, + text=True, + timeout=180, + ) + assert result.returncode == 0, result.stdout + result.stderr + assert (target / ".claude/settings.local.json").exists() + assert not (project / ".claude").exists() + + # 显式置空 = 强制全局 + home2 = tmp_path / "home2" + (home2 / ".claude").mkdir(parents=True) + result = subprocess.run( + ["sh", str(script)], + env={**base_env, "HOME": str(home2), "OCTOP_PROJECT_DIR": ""}, + capture_output=True, + text=True, + timeout=180, + ) + assert result.returncode == 0, result.stdout + result.stderr + assert "安装范围:项目级" not in result.stdout + assert (home2 / ".claude/settings.json").exists() # 全局安装 + + +def test_project_uninstall_only_clears_the_registrations(tmp_path) -> None: + """项目级卸载只清项目的注册,用户目录里的 hook 脚本保留给全局卸载。""" + + def prepare(home): + (home / ".claude").mkdir(parents=True) + (home / ".codex").mkdir(parents=True) + + project = tmp_path / "proj" + project.mkdir() + home, _ = _run_terminals( + tmp_path, + "claude-code,codex", + prepare, + extra_env={"OCTOP_PROJECT_DIR": str(project)}, + ) + assert (project / ".claude/settings.local.json").exists() + + _run_uninstall(tmp_path, home, "claude-code,codex", {"OCTOP_PROJECT_DIR": str(project)}) + + settings = json.loads((project / ".claude/settings.local.json").read_text(encoding="utf-8")) + assert settings["hooks"] == {} + assert "[[hooks." not in (project / ".codex/config.toml").read_text(encoding="utf-8") + # 用户目录里的脚本与 config.json 还在 + assert (home / ".claude/hooks-events/claude-code/stop-capture.mjs").exists() + assert (home / ".claude/hooks-events/config.json").exists() + + +# ---------------------------------------------------------------- 卸载 + + +def test_uninstall_script_mentions_cursor_and_needs_no_token() -> None: + script = render_uninstall_script() + assert script.startswith("#!/bin/sh") + assert "octop memory 卸载" in script + assert "OCTOP_TERMINALS" in script + assert "Cursor" in script + assert "OCTOP_MEMORY_TOKEN" not in script + assert "undo_cursor" in script or "CURSOR_DIR" in script + # 卸载也支持项目级范围 + assert "OCTOP_PROJECT_DIR" in script + assert "卸载范围" in script + + +def test_uninstall_script_shell_quotes_terminals_and_project_query() -> None: + """HTTP ``terminals`` / ``project`` must not break out of the shell assignment.""" + script = render_uninstall_script( + terminals="cursor'; curl evil.example", project="/p'; curl evil.example" + ) + assert "DEFAULT_TERMINALS='cursor'\"'\"'; curl evil.example'" in script + assert "DEFAULT_PROJECT='/p'\"'\"'; curl evil.example'" in script + assert "DEFAULT_TERMINALS='cursor'; curl evil.example'" not in script + + +def test_cursor_uninstall_removes_hooks_keeps_others(tmp_path) -> None: + """setup → uninstall:octop hooks/目录清掉,别人的 hook 与无关 MCP 保留。""" + + def prepare(home): + (home / ".cursor").mkdir(parents=True) + (home / ".cursor/hooks.json").write_text( + json.dumps( + {"hooks": {"postToolUse": [{"command": "git-ai cursor"}]}, "version": 1}, + indent=2, + ) + + "\n", + encoding="utf-8", + ) + (home / ".cursor/mcp.json").write_text( + json.dumps( + { + "mcpServers": { + "octop-memory": {"url": "https://example/mcp"}, + "other": {"url": "https://other"}, + } + }, + indent=2, + ) + + "\n", + encoding="utf-8", + ) + + home, _ = _run_terminals(tmp_path, "cursor", prepare) + assert (home / ".cursor/hooks/cursor/stop-capture.mjs").exists() + + _run_uninstall(tmp_path, home, "cursor") + + cfg = json.loads((home / ".cursor/hooks.json").read_text(encoding="utf-8")) + assert cfg["hooks"]["postToolUse"] == [{"command": "git-ai cursor"}] + assert "beforeSubmitPrompt" not in cfg["hooks"] + assert "stop" not in cfg["hooks"] + assert not (home / ".cursor/hooks").exists() + + mcp = json.loads((home / ".cursor/mcp.json").read_text(encoding="utf-8")) + assert "octop-memory" not in mcp["mcpServers"] + assert mcp["mcpServers"]["other"]["url"] == "https://other" + + +def _run_claude_code_installer(tmp_path, claude_json: dict, settings: dict | None = None): + """只装 Claude Code:沙箱 HOME + 预置的 .claude.json / settings.json。""" + home = tmp_path / "home" + (home / ".claude").mkdir(parents=True) + (home / ".claude.json").write_text(json.dumps(claude_json, indent=2) + "\n", encoding="utf-8") + if settings is not None: + (home / ".claude/settings.json").write_text( + json.dumps(settings, indent=2) + "\n", encoding="utf-8" + ) + script = tmp_path / "setup.sh" + script.write_text(_script(), encoding="utf-8") + result = subprocess.run( + ["sh", str(script)], + env={ + **os.environ, + "HOME": str(home), + "OCTOP_TERMINALS": "claude-code", + "OCTOP_MEMORY_TOKEN": _TOKEN, + "OCTOP_AGENT_ID": "AGT1", + }, + capture_output=True, + text=True, + timeout=180, + ) + assert result.returncode == 0, result.stdout + result.stderr + return home, result + + +def test_mcp_entry_is_added_when_no_octop_server_exists(tmp_path) -> None: + home, _ = _run_claude_code_installer( + tmp_path, {"mcpServers": {"other-mcp": {"type": "http", "url": "http://other/"}}} + ) + servers = json.loads((home / ".claude.json").read_text(encoding="utf-8"))["mcpServers"] + assert "octop-memory-local" in servers # 全新安装才新建条目 + assert "other-mcp" in servers # 别人的条目原样保留 + + +def test_mcp_entry_reuses_an_existing_octop_entry_with_the_same_endpoint(tmp_path) -> None: + """已有同一个端点的 octop 服务(名字可能是 prod / 自定义)→ 复用,不新增条目。""" + home, result = _run_claude_code_installer( + tmp_path, + {"mcpServers": {"octop-memory-prod": {"type": "http", "url": _URL}}}, + ) + servers = json.loads((home / ".claude.json").read_text(encoding="utf-8"))["mcpServers"] + assert list(servers) == ["octop-memory-prod"] + assert "复用" in result.stdout + + +def test_mcp_entry_does_not_add_a_duplicate_for_a_different_octop_endpoint(tmp_path) -> None: + """已有别的 octop 端点时保持原样:不按服务名硬塞第二个 octop 服务。""" + home, result = _run_claude_code_installer( + tmp_path, + { + "mcpServers": { + "octop-memory-other": {"type": "http", "url": "https://other.example/mcp/"} + } + }, + ) + servers = json.loads((home / ".claude.json").read_text(encoding="utf-8"))["mcpServers"] + assert list(servers) == ["octop-memory-other"] + assert "保持原样" in result.stdout + + +def test_legacy_mcp_tool_hook_under_another_server_name_is_replaced(tmp_path) -> None: + """旧版 mcp_tool hook 的服务名可能是 octop-memory-prod:按工具名清掉它。""" + legacy = { + "hooks": { + "UserPromptSubmit": [ + { + "hooks": [ + { + "type": "mcp_tool", + "server": "octop-memory-prod", + "tool": "memory_search", + "input": {"query": "${prompt}"}, + } + ] + } + ] + } + } + home, _ = _run_claude_code_installer(tmp_path, {"mcpServers": {}}, settings=legacy) + entries = json.loads((home / ".claude/settings.json").read_text(encoding="utf-8"))["hooks"][ + "UserPromptSubmit" + ] + assert len(entries) == 1 # 旧 mcp_tool 那条被替换,只剩我们新写的 + assert entries[0]["type"] == "command" # 简写形态:事件直接挂 handler + assert not any(entry.get("type") == "mcp_tool" for entry in entries) + + +# ---------------------------------------------------------------- Windows(PowerShell) + + +def test_powershell_installer_renders_a_native_installer() -> None: + """``?os=windows`` 是原生 PowerShell 安装器:不需要 Git Bash,也不写 .cmd 包装链。""" + script = render_setup_ps1(mcp_url=_URL, agent_id="AGT1", terminals="cursor") + + assert "$PSVersionTable.PSVersion.Major" in script + assert "Read-Host" in script and "Read-Secret" in script # 不回显 + assert "Join-Path $HOME '.claude\\hooks-events'" in script + assert "Join-Path $HOME '.cursor\\hooks'" in script + assert "$DEFAULT_AGENT = 'AGT1'" in script + assert "$DEFAULT_TERMINALS = 'cursor'" in script + # 同一个 Python 合并程序 + .cmd 包装脚本 + assert "OCTOP_HOOK_WRAPPER = 'cmd'" in script + assert "def _cmd_hook_command(mjs):" in script + # 五个终端与 POSIX 槽位一致 + assert "{ 1 = 'claude-code'; 2 = 'kiro'; 3 = 'dsh'; 4 = 'codex'; 5 = 'cursor' }" in script + assert "1-5" in script + # 用户级 token 的提示与没有 userId + assert "MCP token(omcp_…,在 Octop 的 /api/memory-mcp/tokens 创建;不回显)" in script + assert "OCTOP_USER_ID" not in script and "X-Octop-User-Id" not in script + # 项目级范围 + assert "OCTOP_PROJECT_DIR" in script and "安装范围" in script + # 环境块长度:配置走临时 JSON + assert "OCTOP_ENV_FILE" in script + assert "GetTempFileName" in script + # 没有留下未替换的占位符 + assert "__URL_LITERAL__" not in script and "__MERGE_PY__" not in script + + +def test_powershell_uninstaller_renders() -> None: + script = render_uninstall_ps1(terminals="cursor", project="/tmp/proj") + + assert "$PSVersionTable.PSVersion.Major" in script + assert "$DEFAULT_TERMINALS = 'cursor'" in script + assert "$DEFAULT_PROJECT = '/tmp/proj'" in script + assert "undo_cursor" in script + assert "卸载范围" in script + assert "OCTOP_ENV_FILE" in script + assert "__UNINSTALL_PY__" not in script and "__DEFAULT_PROJECT_LITERAL__" not in script + + +def test_powershell_quoting_escapes_single_quotes() -> None: + """PowerShell 单引号串里 ``'`` 要写成 ``''``(不然后面的语句被吃掉)。""" + script = render_setup_ps1(mcp_url=_URL, agent_id="a'b", terminals="cursor'x") + + assert "$DEFAULT_AGENT = 'a''b'" in script + assert "$DEFAULT_TERMINALS = 'cursor''x'" in script + + +def test_powershell_project_scope_honours_an_explicitly_empty_env() -> None: + """与 sh 安装器一致:显式设为空串 = 强制全局,不再交互提问。""" + script = render_setup_ps1(mcp_url=_URL) + assert "$projectSet = $null -ne $env:OCTOP_PROJECT_DIR" in script + assert "if (-not $projectSet -and $interactive)" in script + uninstall = render_uninstall_ps1() + assert "$projectSet = $null -ne $env:OCTOP_PROJECT_DIR" in uninstall + assert "if (-not $projectSet -and $interactive)" in uninstall + + +def test_powershell_installer_embeds_every_host_and_the_native_plugin() -> None: + script = render_setup_ps1(mcp_url=_URL) + + assert "OCTOP_FILES_CLAUDE_CODE = " in script + assert "OCTOP_FILES_KIRO = " in script + assert "OCTOP_FILES_CODEX = " in script and "OCTOP_FILES_CURSOR = " in script + assert "OCTOP_FILES_NATIVE = " in script + assert "OCTOP_FILES_DSH = " not in script + assert "OCTOP_FILES_PRESET = " not in script + + +def test_vendored_plugin_files_exist_for_every_entry_in_the_manifest() -> None: + """清单与 vendored 目录必须一致(升级插件时同步两处)。""" + missing = [name for name in _DSH_NATIVE_FILES if not (_DSH_NATIVE_SRC / name).is_file()] + assert missing == [] + package = json.loads((_DSH_NATIVE_SRC / "package.json").read_text(encoding="utf-8")) + assert package["name"] == _DSH_NATIVE_NAME diff --git a/tests/unit/agents/test_hook_scripts.py b/tests/unit/agents/test_hook_scripts.py new file mode 100644 index 0000000000..a26705c153 --- /dev/null +++ b/tests/unit/agents/test_hook_scripts.py @@ -0,0 +1,211 @@ +"""Hook scripts are data files, but their layout and write locations are a contract.""" + +from __future__ import annotations + +import json +import shutil +import subprocess +from pathlib import Path + +import pytest + +_HOOKS = Path(__file__).parents[3] / "src" / "octop" / "infra" / "agents" / "hook_scripts" +_LIB = _HOOKS / "lib" / "octop-memory-client.mjs" +_NODE = shutil.which("node") or shutil.which("nodejs") +node_required = pytest.mark.skipif(_NODE is None, reason="需要 node 才能执行 .mjs 断言") +_HOSTS = { + "claude-code": ("stop-capture.mjs", "user-prompt-recall.mjs"), + "kiro": ("stop-capture.mjs",), + "dsh": ("stop-capture.mjs", "user-prompt-recall.mjs", "memory-target.mjs"), + "codex": ("stop-capture.mjs", "user-prompt-recall.mjs"), + "cursor": ("stop-capture.mjs", "user-prompt-recall.mjs"), +} + + +def test_every_host_has_its_own_entry_scripts() -> None: + """每个宿主一个入口脚本:一个宿主的改动不会碰到别人。""" + for host, names in _HOSTS.items(): + for name in names: + assert (_HOOKS / host / name).exists(), f"{host}/{name} 缺失" + + +def test_entries_do_not_detect_the_host_at_runtime() -> None: + """宿主差异体现在安装时选的脚本里,脚本内部不再做宿主嗅探。""" + for host, names in _HOSTS.items(): + if not (_HOOKS / host).exists(): + continue + for name in names: + path = _HOOKS / host / name + if not path.exists(): + continue + text = path.read_text(encoding="utf-8") + for sniff in ("findDshSessionFile", "kiroRoot", "dshRoot", "sourceOverride"): + assert sniff not in text, f"{host}/{name} 仍在运行时探测宿主({sniff})" + + +def test_transport_module_is_host_agnostic() -> None: + """共享模块只做传输(配置/鉴权/偏移/日志/压缩),不认识任何宿主。""" + text = _LIB.read_text(encoding="utf-8") + + assert "export async function capture" in text + assert "export async function callTool" in text + assert "decompressZstdContainer" in text + # 认证是用户级 token:token 走 Authorization: Bearer,专家身份只走 X-Octop-Agent-Id, + # 不再有 userId / X-Octop-User-Id 这套旧标识。 + assert '"X-Octop-Agent-Id"' in text + assert "`Bearer ${config.token}`" in text + assert "X-Octop-User-Id" not in text + assert "OCTOP_USER_ID" not in text + assert "userId" not in text + for host_word in ("claude-code", "kiro", "dsh", "codex", "cursor", "transcript_path"): + assert host_word not in text, f"共享模块不应出现宿主细节:{host_word}" + + +def test_no_hook_sends_a_user_identity() -> None: + """用户身份由 token 表达,任何宿主都不再读/发 X-Octop-User-Id。""" + for path in sorted(_HOOKS.rglob("*.mjs")): + text = path.read_text(encoding="utf-8") + for stale in ("X-Octop-User-Id", "OCTOP_USER_ID", "userId"): + assert stale not in text, f"{path.relative_to(_HOOKS)} 仍在用旧用户标识({stale})" + + +def test_every_entry_uses_the_shared_configured_guard() -> None: + """「有没有配好」只由共享模块的 isConfigured() 判,入口脚本别各写一套。""" + assert "export function isConfigured" in _LIB.read_text(encoding="utf-8") + for host, names in _HOSTS.items(): + for name in names: + text = (_HOOKS / host / name).read_text(encoding="utf-8") + if "not configured" not in text: + continue + assert "isConfigured(config)" in text, f"{host}/{name} 没用共享守卫" + assert "!config.token" not in text, f"{host}/{name} 自己拼了 token 判断" + + +def test_scratch_dir_prefers_the_hook_directory() -> None: + """日志/增量状态默认写 hooks 目录,不可写才退到临时目录。""" + text = _LIB.read_text(encoding="utf-8") + assert "for (const dir of [HOOK_DIR," in text + assert "${scratchDir}/octop-hook.log" in text + + +def test_falling_back_to_tmp_is_announced() -> None: + """DSH 的 workspace-write 沙盒里 $HOME 只读,回退必须可见而不是静默。""" + text = _LIB.read_text(encoding="utf-8") + assert "if (scratchDir !== HOOK_DIR)" in text + assert "不可写,日志与增量状态改写到" in text + + +def test_claude_code_capture_defers_to_a_detached_worker() -> None: + """Stop 时本轮还没落盘:后台脱离 + 轮询到「最后一条 user 之后有 assistant」。""" + text = (_HOOKS / "claude-code" / "stop-capture.mjs").read_text(encoding="utf-8") + + assert "OCTOP_CAPTURE_DELAY_MS" in text and "const IS_WORKER" in text + assert 'spawn(process.execPath, [fileURLToPath(import.meta.url), "--worker"]' in text + assert "detached: true" in text and "worker.unref()" in text + assert "OCTOP_HOOK_PAYLOAD_B64" in text + assert "turnLanded" in text + assert '"cc:"' in text or 'sessionPrefix: "cc:"' in text + + +def test_kiro_and_dsh_capture_are_self_contained() -> None: + """Kiro 只认 messages.jsonl;DSH 只认 session*.jsonl.zstd 且自带解压。""" + kiro = (_HOOKS / "kiro" / "stop-capture.mjs").read_text(encoding="utf-8") + dsh = (_HOOKS / "dsh" / "stop-capture.mjs").read_text(encoding="utf-8") + + assert "messages.jsonl" in kiro and '"kiro:"' in kiro + assert "/.kiro/sessions" in kiro + assert "decompressZstdContainer" in dsh and '"dsh:"' in dsh + assert "/.dsh/sessions" in dsh and "session.*\\.jsonl\\.zstd" in dsh + + +def test_dsh_recall_answers_in_the_bridge_dialect() -> None: + """DSH 桥只从 hookSpecificOutput.additionalContext 取上下文:纯 stdout 会被丢弃。""" + text = (_HOOKS / "dsh" / "user-prompt-recall.mjs").read_text(encoding="utf-8") + + assert "hookSpecificOutput" in text + assert 'hookEventName: "UserPromptSubmit"' in text + assert "additionalContext" in text + + +def test_dsh_capture_probes_the_endpoint_before_sending_turns() -> None: + """能分开角色就按轮上报(content + assistant),否则退回服务端解析的 transcript 模式。""" + text = (_HOOKS / "dsh" / "stop-capture.mjs").read_text(encoding="utf-8") + + assert "supportsAssistant" in text and "captureTurns" in text + assert "OCTOP_CAPTURE_MODE" in text and "OCTOP_CAPTURE_MAX_TURNS" in text + assert 'kind && kind !== "user"' in text # 插件注入/系统提醒不算用户提问 + assert 'source: "dsh"' in text + + +# ── 用户级 token 的真实行为(直接 import 共享模块跑一遍,而不是只读文本) ────────── + +_PROBE = """ +import { authHeaders, isConfigured, loadConfig } from __LIB__; +process.env.OCTOP_MEMORY_MCP_URL = "https://memory.example.com/mcp/memory/"; +process.env.OCTOP_MEMORY_MCP_TOKEN = "omcp_env_token"; +process.env.OCTOP_AGENT_ID = "expert-1"; +process.env.OCTOP_USER_ID = "should-be-ignored"; +const config = loadConfig(); +const withToken = (token, tokenHeader) => ({ ...config, token, tokenHeader }); +process.stdout.write(JSON.stringify({ + config, + bearer: authHeaders(withToken("omcp_abc123", "Authorization")), + custom: authHeaders(withToken("shared-token", "X-Octop-Memory-Token")), + configured: isConfigured(config), + tokenlessConfigured: isConfigured(withToken("", "Authorization")), +})); +""" + + +@pytest.fixture(scope="module") +def probe() -> dict[str, object]: + """在一个 node 进程里跑 loadConfig + authHeaders,返回真实结果。""" + script = _PROBE.replace("__LIB__", json.dumps(_LIB.as_uri())) + result = subprocess.run( + [_NODE or "node", "--input-type=module", "-e", script], + capture_output=True, + text=True, + timeout=60, + ) + assert result.returncode == 0, result.stderr + parsed: dict[str, object] = json.loads(result.stdout) + return parsed + + +@node_required +def test_config_no_longer_reads_a_user_id(probe: dict[str, object]) -> None: + """config 里没有 userId:OCTOP_USER_ID / config.json 的 userId 一律不再读。""" + config = probe["config"] + + assert isinstance(config, dict) + assert "userId" not in config + assert config["endpoint"] == "https://memory.example.com/mcp/memory/" + assert config["agentId"] == "expert-1" + + +@node_required +def test_auth_headers_send_a_user_token_and_the_agent_id(probe: dict[str, object]) -> None: + """Authorization 带 Bearer ,专家身份走 X-Octop-Agent-Id,没有用户头。""" + headers = probe["bearer"] + + assert isinstance(headers, dict) + assert headers["Authorization"] == "Bearer omcp_abc123" + assert headers["X-Octop-Agent-Id"] == "expert-1" + assert not [name for name in headers if name.lower() == "x-octop-user-id"] + + +@node_required +def test_a_custom_token_header_is_written_verbatim(probe: dict[str, object]) -> None: + """tokenHeader 可改名:非 Authorization 时原样写该头(不加 Bearer 前缀)。""" + headers = probe["custom"] + + assert isinstance(headers, dict) + assert headers["X-Octop-Memory-Token"] == "shared-token" + assert "Authorization" not in headers + + +@node_required +def test_a_tokenless_config_is_not_configured(probe: dict[str, object]) -> None: + """没配 token 时 isConfigured 为 false:入口脚本在本轮直接跳过,不会发请求。""" + assert probe["configured"] is True + assert probe["tokenlessConfigured"] is False diff --git a/tests/unit/agents/test_memory_mcp.py b/tests/unit/agents/test_memory_mcp.py index c510c8998a..953e878988 100644 --- a/tests/unit/agents/test_memory_mcp.py +++ b/tests/unit/agents/test_memory_mcp.py @@ -421,6 +421,127 @@ def test_capture_redacts_secrets_end_to_end(bind_agent, bind_user, monkeypatch, assert "[REDACTED]" in events[0].content +# ─── capture: terminal transcript uploads ───────────────────────────────── + + +def _claude_transcript() -> str: + import json + + return ( + json.dumps( + { + "type": "user", + "uuid": "u1", + "sessionId": "s1", + "timestamp": "2026-09-22T05:00:00Z", + "message": {"role": "user", "content": "先看看 CLI 结构"}, + } + ) + + "\n" + + json.dumps( + { + "type": "assistant", + "uuid": "a1", + "sessionId": "s1", + "timestamp": "2026-09-22T05:00:05Z", + "message": { + "role": "assistant", + "content": [{"type": "text", "text": "好的,先读文件"}], + }, + } + ) + ) + + +def _real_memory(tmp_path): + from octop_memory.core import Memory + + return Memory( + namespace="mcp-transcript-test", + backend="sqlite", + backend_config={"db_path": str(tmp_path / "memory.sqlite")}, + ) + + +def test_capture_transcript_parses_typed_events(bind_agent, bind_user, monkeypatch, tmp_path): + memory = _real_memory(tmp_path) + monkeypatch.setattr(mm, "_open_memory", lambda server, agent_id: memory) + monkeypatch.setattr(mm, "_archive_uploaded_session", lambda *a, **k: None) + monkeypatch.setattr(mm, "_schedule_extract", lambda *a, **k: False) + mcp = mm.build_memory_mcp(_server()) + + result = _call_tool( + mcp, + "memory_capture", + content=_claude_transcript(), + source="claude-code", + session_id="cc:s1", + ) + + assert result["imported"] == 2 + events = list(memory.list_raw(session_id="cc:s1", limit=10)) + assert {event.event_type for event in events} == {"user_message", "assistant_message"} + + +def test_capture_gzip_transcript_is_decoded(bind_agent, bind_user, monkeypatch, tmp_path): + import base64 + import gzip + + memory = _real_memory(tmp_path) + monkeypatch.setattr(mm, "_open_memory", lambda server, agent_id: memory) + monkeypatch.setattr(mm, "_archive_uploaded_session", lambda *a, **k: None) + monkeypatch.setattr(mm, "_schedule_extract", lambda *a, **k: False) + mcp = mm.build_memory_mcp(_server()) + + payload = base64.b64encode(gzip.compress(_claude_transcript().encode())).decode() + result = _call_tool( + mcp, + "memory_capture", + content=payload, + source="claude-code", + session_id="cc:s2", + ) + assert result["imported"] == 2 + + +def test_capture_transcript_redacts_secrets(bind_agent, bind_user, monkeypatch, tmp_path): + memory = _real_memory(tmp_path) + monkeypatch.setattr(mm, "_open_memory", lambda server, agent_id: memory) + monkeypatch.setattr(mm, "_archive_uploaded_session", lambda *a, **k: None) + monkeypatch.setattr(mm, "_schedule_extract", lambda *a, **k: False) + mcp = mm.build_memory_mcp(_server()) + + secret = "sk-abcdefgh12345678" + transcript = _claude_transcript().replace("先看看 CLI 结构", f"先看看 {secret}") + _call_tool( + mcp, + "memory_capture", + content=transcript, + source="claude-code", + session_id="cc:s3", + ) + events = list(memory.list_raw(session_id="cc:s3", limit=10)) + assert all(secret not in event.content for event in events) + + +async def test_setup_script_never_embeds_a_token(monkeypatch, tmp_path): + monkeypatch.setenv("OCTOP_HOME", str(tmp_path)) + monkeypatch.setenv("OCTOP_MEMORY_MCP_TOKEN", "omcp_instance_secret") + app = mm._InstallerApp(_server()) + scope = _http_scope({"host": "octop.example.com"}) + scope["method"] = "GET" + status, bodies = await _capture_response(app, scope) + assert status == 200 + body = b"".join(part.get("body", b"") for part in bodies).decode() + assert "omcp_instance_secret" not in body + + +def test_powershell_selection(): + assert mm._wants_powershell({"os": "windows"}) is True + assert mm._wants_powershell({"shell": "pwsh"}) is True + assert mm._wants_powershell({}) is False + + # ─── save: MemoryService application layer ──────────────────────────────── diff --git a/tests/unit/agents/test_memory_session_archive.py b/tests/unit/agents/test_memory_session_archive.py new file mode 100644 index 0000000000..6a6f415f6e --- /dev/null +++ b/tests/unit/agents/test_memory_session_archive.py @@ -0,0 +1,137 @@ +"""The raw-session markdown archive: L0 keeps messages, the archive keeps everything.""" + +from __future__ import annotations + +import json + +from octop.infra.agents.memory_session_archive import ( + _MAX_BODY_CHARS, + append_session, + archive_path, +) + + +def _jsonl(records: list[dict]) -> str: + return "\n".join(json.dumps(record, ensure_ascii=False) for record in records) + + +def _user(uuid: str, text: str = "跑一下测试", session: str = "s1") -> dict: + return { + "type": "user", + "uuid": uuid, + "sessionId": session, + "timestamp": "2026-09-29T02:00:00Z", + "message": {"role": "user", "content": text}, + } + + +def _assistant(uuid: str, blocks: list[dict], session: str = "s1") -> dict: + return { + "type": "assistant", + "uuid": uuid, + "sessionId": session, + "timestamp": "2026-09-29T02:00:05Z", + "message": {"role": "assistant", "content": blocks}, + } + + +def test_archive_renders_every_block_not_just_messages(tmp_path): + """L0 会丢 thinking / tool 事件,归档必须都留着。""" + text = _jsonl( + [ + _user("u1"), + _assistant( + "a1", + [ + {"type": "thinking", "thinking": "先想一下"}, + {"type": "text", "text": "我跑一下"}, + { + "type": "tool_use", + "id": "t1", + "name": "Bash", + "input": {"command": "pytest -q"}, + }, + { + "type": "tool_result", + "tool_use_id": "t1", + "content": "1 failed", + }, + ], + ), + ] + ) + + path = append_session(tmp_path, session_id="cc:s1", text=text, host="claude-code", user="alice") + + assert path == archive_path(tmp_path, "cc:s1") + assert path.name == "cc_s1.md" # 文件名净化 + body = path.read_text(encoding="utf-8") + assert "# claude-code session cc:s1" in body + assert "- user: alice" in body + assert "跑一下测试" in body + assert "*(thinking)*" in body and "先想一下" in body + assert "**tool_call · Bash**" in body and '"command": "pytest -q"' in body + assert "**tool_result" in body and "1 failed" in body + assert body.count("") == 1 + assert after_delta.count("") == 1 + assert after_delta.count("# claude-code session cc:s2") == 1 # 头只写一次 + + +def test_archive_keeps_long_tool_output_beyond_the_l0_cap(tmp_path): + """归档给人看:单条上限比 L0 的 4000 宽松得多。""" + body_text = "line\n" * 2000 # ~10KB,超过 L0 的 4000 + text = _jsonl( + [ + _user("u1"), + _assistant( + "a1", + [{"type": "tool_result", "tool_use_id": "t1", "content": body_text}], + ), + ] + ) + + path = append_session(tmp_path, session_id="cc:s3", text=text, host="claude-code") + body = path.read_text(encoding="utf-8") + + assert body.count("line") >= 1500 # 远多于 4000 字符 + assert len(body) <= _MAX_BODY_CHARS * 2 # 仍然有界 + + +def test_archive_skips_non_records_and_keeps_thinking_optional(tmp_path): + """非 user/assistant 记录(mode / cost-state / attachment)不进归档。""" + text = _jsonl( + [ + {"type": "mode", "mode": "acceptEdits"}, + _user("u1", text="问了"), + {"type": "cost-state", "total": 1}, + ] + ) + + path = append_session(tmp_path, session_id="cc:s4", text=text, host="claude-code") + body = path.read_text(encoding="utf-8") + + assert "问了" in body + assert "acceptEdits" not in body + assert body.count("" -_KEY_PATTERN = re.compile(r"") - -_MAX_BODY_CHARS = 20000 -"""Per-record cap in the markdown body — much looser than L0's 4000. - -The archive is for human inspection, so it keeps far more of a long tool result than -the L0 row does; it still bounds a single record so one 5MB build log cannot dominate -the file. -""" - - -def _now() -> str: - return datetime.now(UTC).astimezone().strftime("%Y-%m-%d %H:%M:%S") - - -def _cap(text: str, limit: int = _MAX_BODY_CHARS) -> str: - body = (text or "").strip() - return body if len(body) <= limit else body[: limit - 1].rstrip() + "…" - - -def _fence(body: str, language: str) -> str: - """Fence *body*, widening the fence when the body itself contains backticks.""" - fence = "```" - while fence in body: - fence += "`" - return f"{fence}{language}\n{body}\n{fence}" - - -def _record_role(record: dict[str, Any]) -> str: - return str(record.get("type") or "record") - - -def _render_record(key: str, record: dict[str, Any], index: int) -> str: - """One transcript record → one markdown section (all blocks, nothing dropped).""" - message = record.get("message") - message = message if isinstance(message, dict) else {} - role = str(message.get("role") or _record_role(record)) - stamp = record.get("timestamp") - when = "" - if isinstance(stamp, str) and stamp: - when = stamp.replace("T", " ").replace("Z", "")[:19] - lines: list[str] = [f"## {index} · {when or _now()} · {role}"] - lines.append(_KEY_MARKER.format(key)) - lines.append("") - sidechain = "(sidechain)" if record.get("isSidechain") else "" - if sidechain: - lines.append(sidechain) - lines.append("") - - blocks = _blocks(message.get("content")) - if not blocks: - raw = record.get("content") - if isinstance(raw, str) and raw.strip(): - blocks = [{"type": "text", "text": raw}] - for block in blocks: - kind = block.get("type") - if kind == "text": - body = _cap(str(block.get("text") or "")) - if body: - lines.append(body) - lines.append("") - elif kind == "thinking": - body = _cap(str(block.get("thinking") or block.get("text") or "")) - if body: - lines.append("*(thinking)*") - lines.append("") - lines.append("\n".join(f"> {line}" for line in body.splitlines()) or "> ") - lines.append("") - elif kind == "tool_use": - name = str(block.get("name") or "tool") - args = block.get("input") - body = json.dumps(args, ensure_ascii=False, indent=2, default=str) if args else "" - lines.append(f"**tool_call · {name}**") - lines.append("") - if body: - lines.append(_fence(_cap(body), "json")) - lines.append("") - elif kind == "tool_result": - name = str(block.get("name") or "") - body = _cap(_block_text(block.get("content"))) - flag = "(error)" if block.get("is_error") else "" - lines.append(f"**tool_result{flag}{' · ' + name if name else ''}**") - lines.append("") - if body: - lines.append(_fence(body, "text")) - lines.append("") - else: - lines.append(f"*({kind or 'unknown'} block)*") - lines.append("") - return "\n".join(lines).rstrip() + "\n" - - -def iter_conversation_records(text: str) -> Iterator[tuple[str, dict[str, Any]]]: - """Yield ``(record key, record)`` for the conversation records in *text*. - - Only ``user`` / ``assistant`` records — the transcript's bookkeeping records - (``mode`` / ``cost-state`` / ``attachment`` / ``file-history-snapshot``) are not - conversation. The key is the record's ``uuid`` — the same identity L0 - deduplicates on. - """ - for index, record in enumerate(_iter_records(text), start=1): - if not isinstance(record, dict): - continue - if str(record.get("type") or "") not in ("user", "assistant"): - continue - key = str(record.get("uuid") or f"line{index}") - yield key, record - - -def iter_sections(text: str) -> Iterator[tuple[str, str]]: - """Yield ``(record key, markdown section)`` for every conversation record in *text*. - - Every block is kept: text, thinking, ``tool_use`` and ``tool_result``. - """ - for index, (key, record) in enumerate(iter_conversation_records(text), start=1): - yield key, _render_record(key, record, index) - - -def _header(*, session_id: str, host: str, user: str | None) -> str: - rows = [f"# {host} session {session_id}", "", f"- host: {host}", f"- session: {session_id}"] - if user: - rows.append(f"- user: {user}") - rows += [ - f"- created: {_now()}", - "- source: `memory_capture` 上传的原始 transcript(含工具调用与结果、thinking)", - "", - "---", - "", - ] - return "\n".join(rows) - - -def archive_path(session_dir: Path, session_id: str) -> Path: - safe = re.sub(r"[^0-9A-Za-z._-]+", "_", session_id).strip("_") or "session" - return session_dir / f"{safe}.md" - - -def append_session( - session_dir: Path, - *, - session_id: str, - text: str, - host: str, - user: str | None = None, -) -> Path | None: - """Append the records of one uploaded chunk to the session's markdown archive. - - Returns the file path when something was written, ``None`` when the chunk carried - no new record (a re-send) — so callers can skip without special-casing. - """ - path = archive_path(session_dir, session_id) - existing = "" - if path.exists(): - existing = path.read_text(encoding="utf-8", errors="replace") - seen = set(_KEY_PATTERN.findall(existing)) - index = len(seen) - - sections: list[str] = [] - for key, record in iter_conversation_records(text): - if key in seen: - continue - seen.add(key) - index += 1 - sections.append(_render_record(key, record, index)) - if not sections: - return None - - session_dir.mkdir(parents=True, exist_ok=True) - body = "\n".join(section.rstrip() + "\n" for section in sections) - if existing: - with path.open("a", encoding="utf-8") as handle: - handle.write("\n" + body) - else: - path.write_text( - _header(session_id=session_id, host=host, user=user) + body, encoding="utf-8" - ) - return path diff --git a/tests/unit/agents/test_claude_transcript.py b/tests/unit/agents/test_claude_transcript.py deleted file mode 100644 index dd8d38f9a5..0000000000 --- a/tests/unit/agents/test_claude_transcript.py +++ /dev/null @@ -1,830 +0,0 @@ -"""Unit tests for the Claude Code transcript importer (infra/agents/claude_transcript).""" - -from __future__ import annotations - -import json -from pathlib import Path - -import pytest -from octop_memory.core import Memory - -from octop.infra.agents.claude_transcript import ( - import_content, - import_session, - parse_dsh_text, - parse_kiro_text, - parse_session, -) - -_SESSION = "sess-abc" -_TS = "2026-09-22T05:00:00.000Z" - - -def _record(**kwargs): - base = {"sessionId": _SESSION, "timestamp": _TS, "isSidechain": False} - base.update(kwargs) - return base - - -def _write(tmp_path: Path, records: list[dict]) -> Path: - path = tmp_path / "session.jsonl" - path.write_text( - "\n".join(json.dumps(record, ensure_ascii=False) for record in records), encoding="utf-8" - ) - return path - - -def _turn_records() -> list[dict]: - return [ - {"type": "queue-operation", "sessionId": _SESSION, "operation": "enqueue"}, - _record(type="progress", uuid="p1"), - _record(type="user", uuid="u1", message={"role": "user", "content": "帮我加一个导入命令"}), - _record( - type="assistant", - uuid="a1", - message={ - "role": "assistant", - "content": [ - {"type": "thinking", "thinking": "model scratchpad"}, - {"type": "text", "text": "好的,先看现有 CLI 结构"}, - { - "type": "tool_use", - "id": "toolu_1", - "name": "Read", - "input": {"file_path": "src/octop/cli/main.py"}, - }, - ], - }, - ), - _record( - type="user", - uuid="u2", - message={ - "role": "user", - "content": [ - { - "type": "tool_result", - "tool_use_id": "toolu_1", - "content": [{"type": "text", "text": "@click.group ..."}], - "is_error": False, - } - ], - }, - ), - ] - - -@pytest.fixture -def agent_memory(tmp_path: Path) -> Memory: - return Memory( - namespace="agent_TEST", - backend="sqlite", - backend_config={"db_path": str(tmp_path / "memory.sqlite")}, - ) - - -def test_parse_maps_conversation_records(tmp_path: Path) -> None: - parsed = parse_session(_write(tmp_path, _turn_records())) - - assert parsed.session_id == _SESSION - # thinking / progress / queue-operation carry no L0 content - assert [event.event_type for event in parsed.events] == [ - "user_message", - "assistant_message", - "tool_call", - "tool_result", - ] - # keys use the ORIGINAL block index (thinking keeps index 0) so they stay - # stable if the importer later changes which block types it maps - assert [event.key for event in parsed.events] == ["u1:0", "a1:1", "a1:2", "u2:0"] - - user, assistant, tool_call, tool_result = parsed.events - assert user.content == "帮我加一个导入命令" - assert assistant.content == "好的,先看现有 CLI 结构" - assert tool_call.content.startswith("Read ") - assert tool_call.payload["tool_name"] == "Read" - assert tool_call.payload["tool_use_id"] == "toolu_1" - assert tool_call.payload["input"] == {"file_path": "src/octop/cli/main.py"} - assert tool_result.content == "@click.group ..." - assert tool_result.payload["tool_use_id"] == "toolu_1" - assert tool_result.payload["is_error"] is False - assert user.timestamp.year == 2026 and user.timestamp.tzinfo is not None - # role/session markers survive on every event - assert {event.payload["role"] for event in parsed.events} == {"user", "assistant"} - assert all(event.payload["src_key"] == event.key for event in parsed.events) - - -def test_parse_skips_non_conversation_records(tmp_path: Path) -> None: - parsed = parse_session(_write(tmp_path, _turn_records())) - assert not [event for event in parsed.events if event.payload["src_key"].startswith("p1")] - - -def test_import_writes_l0_and_is_idempotent(tmp_path: Path, agent_memory: Memory) -> None: - path = _write(tmp_path, _turn_records()) - - first = import_session(agent_memory, path) - assert first.session_id == f"cc:{_SESSION}" - assert first.parsed == 4 - assert first.imported == 4 - assert first.skipped_existing == 0 - - stored = agent_memory.list_raw(session_id=f"cc:{_SESSION}", limit=100) - assert len(stored) == 4 - assert {raw.event_type for raw in stored} == { - "user_message", - "assistant_message", - "tool_call", - "tool_result", - } - assert all(raw.host == "claude-code" for raw in stored) - assert all(raw.thread_id == f"cc:{_SESSION}" for raw in stored) - - again = import_session(agent_memory, path) - assert again.imported == 0 - assert again.skipped_existing == 4 - assert len(agent_memory.list_raw(session_id=f"cc:{_SESSION}", limit=100)) == 4 - - -def test_import_appends_only_new_turns(tmp_path: Path, agent_memory: Memory) -> None: - records = _turn_records() - path = _write(tmp_path, records) - import_session(agent_memory, path) - - records.append( - _record(type="user", uuid="u3", message={"role": "user", "content": "继续,加上 --dry-run"}) - ) - path = _write(tmp_path, records) - - stats = import_session(agent_memory, path) - assert stats.imported == 1 - assert stats.skipped_existing == 4 - - -def test_import_dry_run_writes_nothing(tmp_path: Path, agent_memory: Memory) -> None: - path = _write(tmp_path, _turn_records()) - - stats = import_session(agent_memory, path, dry_run=True) - assert stats.imported == 0 - assert stats.parsed == 4 - assert agent_memory.list_raw(session_id=f"cc:{_SESSION}", limit=100) == [] - - -def test_import_caps_long_tool_results(tmp_path: Path, agent_memory: Memory) -> None: - huge = "x" * 20_000 - records = [ - _record( - type="user", - uuid="u1", - message={ - "role": "user", - "content": [ - {"type": "tool_result", "tool_use_id": "t", "content": huge, "is_error": False} - ], - }, - ) - ] - import_session(agent_memory, _write(tmp_path, records)) - - stored = agent_memory.list_raw(session_id=f"cc:{_SESSION}", limit=10) - assert len(stored) == 1 - assert len(stored[0].content) == 4000 - - -def test_user_attribution_is_stored(tmp_path: Path, agent_memory: Memory) -> None: - import_session(agent_memory, _write(tmp_path, _turn_records()), user="alice") - stored = agent_memory.list_raw(session_id=f"cc:{_SESSION}", limit=10) - assert {raw.user for raw in stored} == {"alice"} - - -@pytest.mark.parametrize( - "tool_name", - [ - "memory_search", - "mcp__octop-memory-local__memory_search", - "mcp__octop-memory-prod__memory_search", - "mcp__octop-memory-oauth__memory_search", - "mcp__whatever-the-user-called-it__memory_recall", - "mcp_octop_memory_local_memory_search", - "mcp_octop_memory_prod_memory_get", - ], -) -def test_our_read_tools_are_recognized_under_any_mcp_name(tool_name: str) -> None: - """MCP 服务名可能是 local / prod / oauth 或用户自取的名字:只按工具名认。""" - from octop.infra.agents.claude_transcript import _is_own_read_tool - - assert _is_own_read_tool(tool_name) - # 写工具(memory_capture / memory_save)不是「读工具」,别把用户自己的调用误丢 - assert not _is_own_read_tool("mcp__octop-memory-prod__memory_capture") - assert not _is_own_read_tool("Bash") - - -def test_parse_drops_our_own_memory_read_results(tmp_path: Path) -> None: - """Re-importing our own recall output would close the echo loop.""" - records = [ - _record( - type="assistant", - uuid="a1", - message={ - "role": "assistant", - "content": [ - { - "type": "tool_use", - "id": "t1", - "name": "mcp__octop-memory-local__memory_search", - "input": {"query": "x"}, - }, - {"type": "tool_use", "id": "t2", "name": "Bash", "input": {"command": "ls"}}, - ], - }, - ), - _record( - type="user", - uuid="u1", - message={ - "role": "user", - "content": [ - { - "type": "tool_result", - "tool_use_id": "t1", - "content": '{"hits": [{"snippet": "旧记忆"}]}', - "is_error": False, - }, - { - "type": "tool_result", - "tool_use_id": "t2", - "content": "file list", - "is_error": False, - }, - ], - }, - ), - ] - - parsed = parse_session(_write(tmp_path, records)) - - assert [event.event_type for event in parsed.events] == [ - "tool_call", - "tool_call", - "tool_result", - ] - assert parsed.events[-1].content == "file list" - assert parsed.skipped_own_results == 1 - - -def test_import_content_drops_marked_events(agent_memory: Memory) -> None: - """A recalled block must not be written back into L0 as new evidence.""" - records = [ - _record(type="user", uuid="u1", message={"role": "user", "content": "正常提问"}), - _record( - type="user", - uuid="u2", - message={"role": "user", "content": "## Memory Recall\n- [atom] 旧记忆块"}, - ), - ] - text = "\n".join(json.dumps(record, ensure_ascii=False) for record in records) - - stats = import_content(agent_memory, text, skip_markers=("## Memory Recall",)) - - assert stats.parsed == 2 - assert stats.skipped_echo == 1 - assert stats.imported == 1 - stored = agent_memory.list_raw(session_id=stats.session_id, limit=10) - assert [raw.content for raw in stored] == ["正常提问"] - - -def test_user_messages_are_attributed_to_the_caller(agent_memory: Memory) -> None: - """transcript 形态也要让发送者进正文:提取器 prompt 看不到 raw 的 ``user`` 列。""" - records = [ - _record(type="user", uuid="u1", message={"role": "user", "content": "帮我加一个导入命令"}), - _record( - type="assistant", - uuid="a1", - message={"role": "assistant", "content": [{"type": "text", "text": "好的"}]}, - ), - ] - text = "\n".join(json.dumps(record, ensure_ascii=False) for record in records) - - stats = import_content(agent_memory, text, session_id="cc:s1", user="alice") - - by_type = {raw.event_type: raw for raw in agent_memory.list_raw(session_id="cc:s1", limit=10)} - assert by_type["user_message"].content == "alice说:帮我加一个导入命令" - assert by_type["assistant_message"].content == "好的" # 助手侧不加前缀 - assert by_type["user_message"].user == "alice" # raw 的 user 列照旧 - assert stats.imported == 2 - - # 重发(offset 丢失后的 mode=full)靠 src_key 去重,也不会把前缀叠成两层 - again = import_content(agent_memory, text, session_id="cc:s1", user="alice") - assert again.imported == 0 - - -def test_sidechain_user_records_are_not_attributed(agent_memory: Memory) -> None: - """子代理记录里的 user 消息是系统合成提示,不是调用者说过的话。""" - records = [ - _record(type="user", uuid="u1", message={"role": "user", "content": "真实提问"}), - _record( - type="user", - uuid="u2", - isSidechain=True, - message={"role": "user", "content": "子代理提示"}, - ), - ] - text = "\n".join(json.dumps(record, ensure_ascii=False) for record in records) - - import_content(agent_memory, text, session_id="cc:s2", user="alice") - - contents = {raw.content for raw in agent_memory.list_raw(session_id="cc:s2", limit=10)} - assert "alice说:真实提问" in contents - assert "子代理提示" in contents - assert "alice说:子代理提示" not in contents - - -# --------------------------------------------------------------------------- -# Kiro (``~/.kiro/sessions///messages.jsonl``) -# --------------------------------------------------------------------------- - -_KIRO_RECORDS = [ - { - "id": "r1", - "timestamp": _TS, - "payload": {"type": "user", "content": "Kiro 里问的问题", "source": "chat"}, - }, - {"id": "r2", "timestamp": _TS, "payload": {"type": "turn_start", "executionId": "e1"}}, - { - "id": "r3", - "timestamp": _TS, - "payload": { - "type": "ContextualHookInvoked", - "hookId": "h", - "output": "[octop memory search] 旧记忆", - }, - }, - { - "id": "r4", - "timestamp": _TS, - "payload": { - "type": "tool_call", - "toolCallId": "c1", - "toolName": "readFiles", - "args": {"files": ["a.ts"]}, - }, - }, - { - "id": "r5", - "timestamp": _TS, - "payload": { - "type": "tool_result", - "toolCallId": "c1", - "content": "file body", - "success": True, - }, - }, - { - "id": "r6", - "timestamp": _TS, - "payload": {"type": "assistant", "content": "scratchpad", "operationType": "Reasoning"}, - }, - { - "id": "r7", - "timestamp": _TS, - "payload": {"type": "assistant", "content": "这是回答", "operationType": "Say"}, - }, - {"id": "r8", "timestamp": _TS, "payload": {"type": "turn_end", "stopReason": "end_turn"}}, -] - - -def _kiro_text() -> str: - return "\n".join(json.dumps(r, ensure_ascii=False) for r in _KIRO_RECORDS) - - -def test_parse_kiro_maps_records() -> None: - parsed = parse_kiro_text(_kiro_text()) - - assert [e.event_type for e in parsed.events] == [ - "user_message", - "tool_call", - "tool_result", - "assistant_message", - ] - assert parsed.events[3].content == "这是回答" # Reasoning 是草稿,跳过 - assert parsed.events[1].payload["tool_name"] == "readFiles" - assert parsed.events[2].payload["success"] is True - # ContextualHookInvoked 是我们自己 hook 的调用记录(连 output 都在里面) - assert parsed.skipped_own_results == 1 - assert all(e.event_type != "tool_result" or "旧记忆" not in e.content for e in parsed.events) - - -def test_import_content_kiro_is_idempotent(agent_memory: Memory) -> None: - first = import_content( - agent_memory, _kiro_text(), session_id="kiro:s1", host="kiro", parser="kiro" - ) - assert first.session_id == "kiro:s1" - assert first.imported == 4 - assert first.by_type == { - "user_message": 1, - "tool_call": 1, - "tool_result": 1, - "assistant_message": 1, - } - - again = import_content( - agent_memory, _kiro_text(), session_id="kiro:s1", host="kiro", parser="kiro" - ) - assert again.imported == 0 - assert again.skipped_existing == 4 - - -def test_import_content_rejects_unknown_parser(agent_memory: Memory) -> None: - with pytest.raises(ValueError, match="unknown transcript parser"): - import_content(agent_memory, _kiro_text(), parser="no-such-host") - - -# --------------------------------------------------------------------------- -# DSH (``~/.dsh/sessions///session.jsonl.zstd``) -# --------------------------------------------------------------------------- - -_DSH_MS = 1788952324078 -_DSH_RECORDS = [ - {"type": "session", "version": 0, "id": "session-x", "createdAt": _DSH_MS, "cwd": "/tmp"}, - { - "type": "user/message", - "seq": 1, - "time": _DSH_MS, - "data": {"content": [{"type": "text", "text": "真实提问"}], "source": {"kind": "user"}}, - }, - { - "type": "user/message", - "seq": 2, - "time": _DSH_MS, - "data": { - "content": [ - {"type": "text", "text": "Current runtime context. This snapshot supersedes…"} - ], - "source": {"kind": "plugin"}, - }, - }, - {"type": "assistant/chunk", "seq": 3, "time": _DSH_MS, "data": {"text": "流式增量"}}, - { - "type": "assistant/message", - "seq": 4, - "time": _DSH_MS, - "data": { - "turn": 1, - "step": 1, - "message": { - "role": "assistant", - "content": [ - {"type": "reasoning", "text": "草稿"}, - {"type": "text", "text": "回答正文"}, - { - "type": "tool-call", - "id": "call_1", - "name": "bash", - "arguments": '{"command": "ls"}', - }, - ], - }, - }, - }, - { - "type": "tool/call", - "seq": 5, - "time": _DSH_MS, - "data": { - "turn": 1, - "step": 1, - "callId": "call_1", - "name": "bash", - "arguments": '{"command": "ls"}', - }, - }, - { - "type": "tool/result", - "seq": 6, - "time": _DSH_MS, - "data": { - "turn": 1, - "step": 1, - "message": { - "source": {"kind": "tool", "callId": "call_1"}, - "content": [ - { - "type": "tool-result", - "toolCallId": "call_1", - "content": [{"type": "text", "text": "file list"}], - "isError": False, - } - ], - }, - }, - }, - {"type": "turn/end", "seq": 7, "time": _DSH_MS, "data": {"turn": 1, "stopReason": "end_turn"}}, -] - - -def _dsh_text() -> str: - return "\n".join(json.dumps(r, ensure_ascii=False) for r in _DSH_RECORDS) - - -def test_parse_dsh_maps_records() -> None: - parsed = parse_dsh_text(_dsh_text()) - - assert parsed.session_id == "session-x" - assert [e.event_type for e in parsed.events] == [ - "user_message", - "assistant_message", - "tool_call", - "tool_result", - ] - # 流式增量与 Reasoning 草稿都不入库 - assert all("流式增量" not in e.content for e in parsed.events) - assert all("草稿" not in e.content for e in parsed.events) - # 同一个 call 既在 assistant/message 的 tool-call 块里、又是独立 tool/call 记录 —— 只留一条 - assert [e.content for e in parsed.events if e.event_type == "tool_call"] == [ - 'bash {"command": "ls"}' - ] - assert parsed.events[3].content == "file list" - assert parsed.events[3].payload["is_error"] is False - # plugin / skill-catalog / agent-instructions 注入是管道,不是用户说的话 - assert parsed.skipped_own_results == 1 - - -def test_import_content_dsh_uses_terminal_prefix(agent_memory: Memory) -> None: - stats = import_content(agent_memory, _dsh_text(), host="dsh", parser="dsh") - - assert stats.session_id == "dsh:session-x" - assert stats.imported == 4 - assert stats.by_type == { - "user_message": 1, - "assistant_message": 1, - "tool_call": 1, - "tool_result": 1, - } - - -def test_echo_rule_differs_by_role() -> None: - """回声判定按角色区分:助手侧看是否以标记开头,用户侧看标记是否出现。""" - from octop.infra.agents.claude_transcript import _is_echo - - markers = ("[octop memory search]", "[octop memory capture]") - # 助手侧:只是引用标记的回答必须保留 - assert not _is_echo( - "靠两个 hook:召回把 [octop memory search] 块注入上下文", "assistant_message", markers - ) - assert _is_echo("[octop memory search] related memories…", "assistant_message", markers) - # 用户侧:出现标记就是我们注入的上下文(CC 会加 "Stop hook feedback:" 前缀) - assert _is_echo( - "Stop hook feedback:\n[octop memory search] 请记录要点", "user_message", markers - ) - assert not _is_echo("正常提问", "user_message", markers) - - -def test_a_reply_that_quotes_our_marker_survives(agent_memory: Memory) -> None: - """模型回答只是**引用**召回标记时必须入库——否则解释接入方式的对话会被当回声丢掉。""" - records = [ - _record(type="user", uuid="u1", message={"role": "user", "content": "记忆接入怎么做的?"}), - _record( - type="assistant", - uuid="a1", - message={ - "role": "assistant", - "content": [ - { - "type": "text", - "text": "靠两个 hook:召回会把 [octop memory search] 块注入上下文。", - } - ], - }, - ), - ] - text = "\n".join(json.dumps(record, ensure_ascii=False) for record in records) - - stats = import_content(agent_memory, text, skip_markers=("[octop memory search]",)) - - assert stats.skipped_echo == 0 - assert stats.imported == 2 - stored = agent_memory.list_raw(session_id=stats.session_id, limit=10) - assert any("[octop memory search]" in raw.content for raw in stored) - - -def test_codex_rollout_maps_to_events() -> None: - """Codex rollout:event_msg 带对话,response_item 带工具与(重复的)消息副本。""" - from octop.infra.agents.claude_transcript import parse_codex_text - - records = [ - { - "timestamp": "2026-08-08T13:47:31.000Z", - "type": "session_meta", - "payload": {"id": "s-1", "cwd": "/tmp", "cli_version": "0.142.5"}, - }, - { - "timestamp": "2026-08-08T13:47:31.163Z", - "type": "event_msg", - "payload": {"type": "user_message", "message": "现在是什么模型\n"}, - }, - { - "timestamp": "2026-08-08T13:47:32.000Z", - "type": "response_item", - "payload": { - "type": "message", - "role": "developer", - "content": [{"type": "input_text", "text": ""}], - }, - }, - { - "timestamp": "2026-08-08T13:47:33.000Z", - "type": "response_item", - "payload": { - "type": "message", - "role": "assistant", - "id": "a1", - "content": [{"type": "output_text", "text": "我是 Codex。"}], - }, - }, - { - "timestamp": "2026-08-08T13:47:34.000Z", - "type": "response_item", - "payload": { - "type": "function_call", - "id": "t1", - "name": "shell", - "arguments": '{"command":"ls"}', - }, - }, - { - "timestamp": "2026-08-08T13:47:35.000Z", - "type": "response_item", - "payload": {"type": "function_call_output", "id": "t2", "output": "a.txt"}, - }, - { - "timestamp": "2026-08-08T13:47:36.000Z", - "type": "turn_context", - "payload": {"cwd": "/tmp", "model": "x"}, - }, - ] - text = "\n".join(json.dumps(record, ensure_ascii=False) for record in records) - - parsed = parse_codex_text(text) - - assert [event.event_type for event in parsed.events] == [ - "user_message", - "assistant_message", - "tool_call", - "tool_result", - ] - assert parsed.events[0].content == "现在是什么模型\n" - assert parsed.events[1].content == "我是 Codex。" - assert parsed.events[2].content.startswith("shell ") - assert parsed.events[3].content == "a.txt" - assert parsed.skipped_own_results == 1 # developer instructions 是宿主管线 - - -def test_codex_does_not_store_the_same_turn_twice() -> None: - """event_msg 与 response_item 会带同一段文本:只入库一次。""" - from octop.infra.agents.claude_transcript import parse_codex_text - - records = [ - { - "timestamp": "2026-08-08T13:47:31.163Z", - "type": "event_msg", - "payload": {"type": "user_message", "message": "同一个问题"}, - }, - { - "timestamp": "2026-08-08T13:47:31.900Z", - "type": "response_item", - "payload": { - "type": "message", - "role": "user", - "id": "u9", - "content": [{"type": "input_text", "text": "同一个问题"}], - }, - }, - ] - text = "\n".join(json.dumps(record, ensure_ascii=False) for record in records) - - parsed = parse_codex_text(text) - - assert [event.content for event in parsed.events] == ["同一个问题"] - assert parsed.skipped_own_results == 1 - - -def test_cursor_transcript_shapes_are_tolerated() -> None: - """Cursor 的 transcript_path 格式未公开:JSONL / 数组 / {messages:[]} 都要能读。""" - from octop.infra.agents.claude_transcript import parse_cursor_text - - pairs = [("user", "怎么接入?"), ("assistant", "两条 hook 就够了。")] - shapes = [ - "\n".join(json.dumps({"role": r, "content": c}, ensure_ascii=False) for r, c in pairs), - json.dumps([{"role": r, "text": c} for r, c in pairs], ensure_ascii=False), - json.dumps( - {"messages": [{"role": r, "content": [{"type": "text", "text": c}]} for r, c in pairs]}, - ensure_ascii=False, - ), - ] - - for text in shapes: - parsed = parse_cursor_text(text) - assert [event.event_type for event in parsed.events] == [ - "user_message", - "assistant_message", - ], text[:60] - assert parsed.events[0].content == "怎么接入?" - - -def test_cursor_agent_transcript_jsonl_keeps_text_and_drops_tool_blocks() -> None: - """stop 交给的真实文件:一行一个 {role, message:{content:[{type,text}]}}。""" - from octop.infra.agents.claude_transcript import parse_cursor_text - - lines = [ - { - "role": "user", - "message": {"content": [{"type": "text", "text": "怎么接入?"}]}, - }, - { - "role": "assistant", - "message": { - "content": [ - {"type": "text", "text": "两条 hook 就够了。"}, - {"type": "tool_use", "name": "read", "input": {"path": "a"}}, - ] - }, - }, - ] - text = "\n".join(json.dumps(line, ensure_ascii=False) for line in lines) - - parsed = parse_cursor_text(text) - - assert [event.event_type for event in parsed.events] == [ - "user_message", - "assistant_message", - ] - assert parsed.events[0].content == "怎么接入?" - assert parsed.events[1].content == "两条 hook 就够了。" - - -def test_cursor_skips_entries_without_a_conversational_role() -> None: - from octop.infra.agents.claude_transcript import parse_cursor_text - - text = json.dumps( - [ - {"role": "user", "content": "问题"}, - {"kind": "toolCall", "tool": "read", "args": {"path": "a"}}, - {"role": "assistant", "content": "回答"}, - ], - ensure_ascii=False, - ) - - parsed = parse_cursor_text(text) - - assert [event.event_type for event in parsed.events] == ["user_message", "assistant_message"] - assert parsed.skipped_own_results == 1 - - -def test_cursor_delta_uploads_do_not_collide_on_line_index_src_key() -> None: - """stop hook ships byte deltas; each chunk used to renumber from line0. - - Distinct user turns at the head of consecutive deltas must get distinct - ``src_key`` values, otherwise the later user message is dropped as - ``skipped_existing`` while later assistant lines still import. - """ - from octop.infra.agents.claude_transcript import parse_cursor_text - - def _line(role: str, text: str) -> str: - return json.dumps( - {"role": role, "message": {"content": [{"type": "text", "text": text}]}}, - ensure_ascii=False, - ) - - delta_a = "\n".join( - [ - _line("user", "上一轮用户问题"), - _line("assistant", "上一轮助手回答"), - ] - ) - delta_b = "\n".join( - [ - _line("user", "原始素材只有回复,没有用户消息"), - _line("assistant", "这是助手的长回复,超过十二个字符。"), - ] - ) - - keys_a = {event.payload["src_key"] for event in parse_cursor_text(delta_a).events} - parsed_b = parse_cursor_text(delta_b) - keys_b = [event.payload["src_key"] for event in parsed_b.events] - - assert parsed_b.events[0].event_type == "user_message" - assert parsed_b.events[0].content == "原始素材只有回复,没有用户消息" - assert keys_b[0] not in keys_a - assert keys_b[0].startswith("c") and keys_b[0].endswith(":0") - # Re-parsing the same delta stays idempotent. - assert parse_cursor_text(delta_b).events[0].payload["src_key"] == keys_b[0] - - -def test_cursor_prefers_explicit_record_id_for_src_key() -> None: - from octop.infra.agents.claude_transcript import parse_cursor_text - - text = json.dumps( - {"id": "msg_abc", "role": "user", "content": "有稳定 id"}, - ensure_ascii=False, - ) - parsed = parse_cursor_text(text) - assert parsed.events[0].payload["src_key"] == "msg_abc:0" diff --git a/tests/unit/agents/test_client_setup.py b/tests/unit/agents/test_client_setup.py deleted file mode 100644 index 6b7f9f4d3d..0000000000 --- a/tests/unit/agents/test_client_setup.py +++ /dev/null @@ -1,1398 +0,0 @@ -"""Unit tests for the interactive local-terminal installer generator.""" - -from __future__ import annotations - -import ast -import json -import os -import subprocess -import tomllib -from pathlib import Path - -import pytest - -from octop.infra.agents.client_setup import ( - _DSH_NATIVE_FILES, - _DSH_NATIVE_NAME, - _DSH_NATIVE_SRC, - _MERGE_PY, - _SELECT_PY, - _UNINSTALL_PY, - HOOKS_DIRS, - TERMINALS, - render_setup_ps1, - render_setup_script, - render_uninstall_ps1, - render_uninstall_script, -) - -_URL = "http://127.0.0.1:8088/mcp/memory/" -_TOKEN = "omcp_test_token" - -posix_only = pytest.mark.skipif(os.name != "posix", reason="假的 dsh 命令用 POSIX 可执行脚本") - - -def _script(**overrides) -> str: - kwargs = {"mcp_url": _URL} - kwargs.update(overrides) - return render_setup_script(**kwargs) - - -# ---------------------------------------------------------------- 生成脚本的静态形态 - - -def test_installer_detects_the_local_terminals() -> None: - script = _script() - - assert '[ -e "$HOME/.claude.json" ] || [ -d "$HOME/.claude" ] || command -v claude' in script - assert '[ -d "$HOME/.kiro" ] || command -v kiro' in script - assert '[ -d "$HOME/.dsh" ] || command -v dsh' in script - assert 'echo " 1) Claude Code' in script - assert 'echo " 2) Kiro' in script - assert 'echo " 3) DSH' in script - - -def test_installer_asks_for_a_user_mcp_token_not_a_user_id() -> None: - """凭据是**用户级 MCP token**(token 绑定专家):只问 token,不问专家 ID / 用户标识。""" - script = _script() - - # 交互走 /dev/tty:curl | sh 时 stdin 是脚本本身,直接 read 会把脚本吃掉 - assert "if (true /dev/null; then HAS_TTY=1; fi" in script - assert script.count("< /dev/tty") >= 2 # 探测 + 选择 + token - assert "要配置哪些?" in script - assert "专家 ID" not in script # token 绑定专家,不再问专家 ID - # token 提示说明去哪儿创建(omcp_…),且不回显 - assert "MCP token(omcp_…,在 Octop 的 /api/memory-mcp/tokens 创建;不回显)" in script - assert "stty -echo" in script - # 没有 tty 时不盲读 stdin,而是明确要求环境变量 - assert 'TOKEN="${OCTOP_MEMORY_TOKEN:-}"' in script - assert "非交互运行时请设 OCTOP_MEMORY_TOKEN" in script - assert "OCTOP_AGENT" not in script # 没有专家 ID 这个必填项 - # 「用户标识」这一整套概念消失:token 就代表「谁问的」 - assert "OCTOP_USER_ID" not in script - assert "X-Octop-User-Id" not in script - assert "用户标识" not in script - assert "userId" not in script - # 也不再有第二份凭据 / OAuth 前置 / token 方言 - assert "OCTOP_ENTRY_TOKEN" not in script - assert "OCTOP_DISABLE_OAUTH" not in script - assert "个人令牌" not in script - assert "OCTOP_TOKEN_HEADER" not in script - - -def test_terminal_pick_allows_multiselect() -> None: - """五个选项可多选:1,2 / 1 3 / 12 / a=全部,回车=全部检测到的。""" - script = _script() - - assert "可多选:1,2 / 1 3 / 12 / a=全部" in script - assert "''|a|A|all|ALL|全部) _chosen=\"$DETECTED\"" in script - assert "fold -w1" in script # 连写数字 12/13 按位拆开 - assert 'case ",$_chosen," in *",$_add,"*' in script # 去重 - assert "已忽略" in script # 未检测到/无法识别 - assert "没有选中任何已检测到的终端,请重选" in script # 选不中会重问 - assert 'while [ -z "$TERMINALS" ]' in script - - -def test_installer_embeds_no_credential() -> None: - """没内置默认值时不内嵌密钥:token 由运行的人粘贴自己的。""" - script = _script() - - assert "DEFAULT_TOKEN=" not in script - assert "Bearer " in script # 只在运行时按输入拼头,不是写死的 token - # 只有一个 header 方言:Authorization → Bearer - assert "TOKEN_HEADER='Authorization'" in script - assert 'TOKEN_HEADER="${OCTOP_TOKEN_HEADER:-Authorization}"' not in script - - -def test_default_token_is_an_optional_prefill() -> None: - """``default_token`` 只是可选预填;环境变量仍然优先,没预填就现场问。""" - script = _script(default_token="omcp_prefilled") - - assert "DEFAULT_TOKEN='omcp_prefilled'" in script - assert "if [ -z \"$TOKEN\" ] && [ -n 'omcp_prefilled' ]; then" in script - assert "用安装链接预填的值" in script - # 预填只填 TOKEN;凭据仍是运行人自己的 - assert "OCTOP_MEMORY_TOKEN:-" in script - - -def test_known_values_only_prefill_prompts() -> None: - """``terminals`` 只是预填;``agent_id`` 保留形参但**被忽略**(token 已绑定专家)。""" - script = _script(agent_id="RY9RQC", terminals="kiro") - - assert "DEFAULT_TERMINALS='kiro'" in script - assert "DEFAULT_AGENT" not in script - assert "RY9RQC" not in script - - -def test_setup_script_shell_quotes_query_prefills() -> None: - script = _script(terminals="cursor';id", project="/tmp/p';id") - # Classic POSIX single-quote escape: ' → '"'"' - assert "DEFAULT_TERMINALS='cursor'\"'\"';id'" in script - assert "DEFAULT_PROJECT='/tmp/p'\"'\"';id'" in script - assert "DEFAULT_TERMINALS='cursor';id'" not in script - - -def test_each_terminal_gets_its_own_hooks_directory() -> None: - script = _script() - - assert "CC_DIR='~/.claude/hooks-events'" in script - assert "KIRO_DIR='~/.kiro/hooks'" in script - assert "DSH_DIR='~/.dsh/hooks'" in script - assert "CODEX_DIR='~/.codex/hooks'" in script - assert "CURSOR_DIR='~/.cursor/hooks'" in script - assert HOOKS_DIRS == { - "claude-code": "~/.claude/hooks-events", - "kiro": "~/.kiro/hooks", - "dsh": "~/.dsh/hooks", - "codex": "~/.codex/hooks", - "cursor": "~/.cursor/hooks", - } - - -def test_claude_code_uses_two_command_hooks() -> None: - """Claude Code:召回与上报都必须是 command hook。 - - - ``UserPromptSubmit`` 上 `mcp_tool` 会被静默跳过(实测 2.1.263); - - ``Stop`` 用脚本,脚本自己后台脱离+延迟,等本轮落盘后再读 transcript - (``SessionEnd`` 不可用:那里没有 MCP 客户端上下文)。 - """ - script = _script() - - assert ( - 'search = {"type": "command", "command": hook_command(f"{hooks_dir}/claude-code/user-prompt-recall.mjs")}' - in script - ) - assert ( - 'stop = {"type": "command", "command": hook_command(f"{hooks_dir}/claude-code/stop-capture.mjs")}' - in script - ) - assert "该事件不支持 ``mcp_tool``" in script - assert "SessionEnd`` 不能用" in script - # Claude Code 要求 matcher 组里再套 hooks 数组 - assert '"UserPromptSubmit": [{"hooks": [search]}],' in script - assert '"Stop": [{"hooks": [stop]}],' in script - # 收尾提示按事件名列出两端角色(Cursor 的事件名不同,故不能只写 UserPromptSubmit/Stop) - assert "用户提交时召回" in script and "memory_search" in script - assert "回合结束时上报" in script and "memory_capture" in script - assert "Cursor 的 beforeSubmitPrompt" in script - - -def test_claude_code_clears_the_legacy_session_end_hook() -> None: - """早期版本挂过 SessionEnd(mcp_tool 在那里不可用、会持续报错):重装时要清掉。""" - script = _script() - - assert '_upsert_groups(st, "SessionEnd", [])' in script - assert 'st["hooks"].pop("SessionEnd", None)' in script - assert 'hooks["SessionEnd"]' not in script # 不再定义 - - -def test_previous_inline_hook_command_is_replaced_by_the_wrapper(tmp_path) -> None: - """老配置里那串内联 node 命令(含 ``.mjs`` 与转义字符)重装时换成包装脚本,不留重复。""" - legacy = ( - "/usr/bin/node /home/u/.claude/hooks-events/claude-code/stop-capture.mjs " - '|| echo "octop-memory: hook 执行失败(node 路径不对或脚本报错)" >&2' - ) - home, _ = _run_claude_code_installer( - tmp_path, {}, {"hooks": {"Stop": [{"hooks": [{"type": "command", "command": legacy}]}]}} - ) - stops = json.loads((home / ".claude/settings.json").read_text(encoding="utf-8"))["hooks"][ - "Stop" - ] - assert len(stops) == 1 # 老命令被替换,没有并列 - assert stops[0]["hooks"][0]["type"] == "command" # matcher 组里套 handler - command = stops[0]["hooks"][0]["command"] - assert command.endswith("claude-code/stop-capture.sh") - assert ".mjs" not in command # 配置里不再有那串长命令 - assert (home / ".claude/hooks-events/claude-code/stop-capture.sh").exists() - - -def test_foreign_hook_groups_are_kept_next_to_ours(tmp_path) -> None: - """别人的 hook(matcher 组或裸 handler)都要原样留着,只动我们自己那两条。""" - settings = { - "hooks": { - "Stop": [ - {"matcher": "*", "hooks": [{"type": "command", "command": "git-ai checkpoint"}]}, - {"type": "command", "command": "/usr/local/bin/other-check"}, - ] - } - } - home, _ = _run_claude_code_installer(tmp_path, {}, settings) - stops = json.loads((home / ".claude/settings.json").read_text(encoding="utf-8"))["hooks"][ - "Stop" - ] - - assert stops[0] == { - "matcher": "*", - "hooks": [{"type": "command", "command": "git-ai checkpoint"}], - } - assert stops[1] == {"type": "command", "command": "/usr/local/bin/other-check"} - assert stops[2]["hooks"][0]["command"].endswith( - "claude-code/stop-capture.sh" - ) # 我们的追加在最后 - - -def test_hook_scripts_are_installed_for_every_hook_terminal() -> None: - """四个 hook 宿主各装一份脚本 + config.json;DSH 走原生插件,不装 hook 脚本。""" - script = _script() - - assert "SCRIPT_OK" not in script - assert "CAPTURE_PROMPT" not in script # 上报绝不走"让模型总结"的提示词 - assert "def do_claude_code():\n # 两条 hook 都是 command 脚本" in script - assert '_install_hooks(CC_DIR, "claude-code")' in script - assert '_install_hooks(KIRO_DIR, "kiro")' in script - assert '_install_hooks(CODEX_DIR, "codex")' in script - assert '_install_hooks(CURSOR_DIR, "cursor")' in script - # DSH 不再装 hooks(原生插件在进程内做召回/上报) - assert '_install_hooks(DSH_DIR, "dsh")' not in script - assert '"dsh": ("dsh/stop-capture.mjs"' not in script - - -def test_kiro_capture_is_always_a_command_hook() -> None: - """Kiro:召回用提示词(Kiro 只支持 runCommand/askAgent);上报始终用脚本提交整段 jsonl。""" - script = _script() - - kiro = script[script.index("def do_kiro():") : script.index("_CODEX_HOOKS = ")] - # 召回仍然是 agent 提示词(runCommand 的 stdout 不会被注入上下文) - assert '"action": {"type": "agent", "prompt": SEARCH_PROMPT}' in kiro - # 上报只有 command 一种形态,没有提示词兜底 - assert ( - '{"type": "command", "command": hook_command(f"{KIRO_DIR}/kiro/stop-capture.mjs")}' in kiro - ) - assert '"type": "agent", "prompt": CAPTURE_PROMPT' not in kiro - assert "提交整段 messages.jsonl" in kiro - assert "Kiro 上报 hook" in script - - -def test_prompts_carry_the_role_instructions() -> None: - script = _script() - - assert "先用 memory_search 检索" in script - # 上报没有提示词:模型"总结会话"不算捕获原始会话 - assert "用 memory_capture 记录" not in script - - -def test_generated_script_embeds_each_hosts_scripts() -> None: - """按宿主分别内嵌:共享传输模块 + 该宿主的入口脚本。""" - script = _script() - - assert "OCTOP_FILES_CLAUDE_CODE=" in script - assert "OCTOP_FILES_KIRO=" in script - assert "OCTOP_FILES_CODEX=" in script and "OCTOP_FILES_CURSOR=" in script - assert "OCTOP_FILES_DSH=" not in script # 没有 dsh hook 脚本可装 - assert "OCTOP_FILES_PRESET=" not in script # preset 方案已废弃 - assert "HOOK_FILES = {" in script and "OCTOP_FILES_" in script - assert "lib/octop-memory-client.mjs" in script - assert "claude-code/stop-capture.mjs" in script and "kiro/stop-capture.mjs" in script - assert "codex/user-prompt-recall.mjs" in script and "codex/stop-capture.mjs" in script - assert "cursor/user-prompt-recall.mjs" in script and "cursor/stop-capture.mjs" in script - # 旧版的单文件变量不再使用 - assert "OCTOP_STOP_B64" not in script and "OCTOP_RECALL_B64" not in script - - -def test_dsh_native_plugin_manifest_is_vendored() -> None: - """原生插件按 ``_DSH_NATIVE_FILES`` 逐字节内嵌(安装器不依赖插件仓/git)。""" - script = _script() - - assert "OCTOP_FILES_NATIVE=" in script - for name in _DSH_NATIVE_FILES: - assert (_DSH_NATIVE_SRC / name).is_file(), f"缺少 vendored 插件文件:{name}" - # 清单里的每个文件都被打进脚本(base64,脚本里看不到明文插件源码) - assert _DSH_NATIVE_NAME == "octop-memory-native" - assert "def _install_dsh_native_files():" in script - assert "def _install_dsh_native():" in script - - -def test_dsh_native_install_registers_profile_and_patch_block() -> None: - """DSH:写插件目录 → 登记 dependencies / dsh.profile.bundles → 同 id 覆盖 cordis.patch.yml。""" - script = _script() - - assert "def _write_dsh_native_patch(profile_dir, config):" in script - assert "def _replace_native_patch(text, block, row_id=None):" in script - assert "def _register_dsh_native(profile_dir, bundle, profile):" in script - assert 'dependencies[NATIVE_NAME] = "link:" + str(bundle)' in script - assert "bundles.append(NATIVE_NAME)" in script - # 配置块只覆盖 config,不再 insert(重复 id 会让 Loader 直接崩) - assert "插件自己的 bundle patch 已经 insert 了同 id 的行" in script - assert '"dsh", "plugin", "--profile", profile, "add", str(bundle)' in script - assert '"pnpm", "add", str(bundle)' in script # 源码 checkout 常没有 dsh 命令 - assert "没找到 DSH profile" in script - assert "重启 DSH" in script - - -def test_dsh_native_config_block_uses_the_user_token_shape() -> None: - """配置块里的连接信息 = 用户级 MCP token 形状(endpoint/token/tokenHeader,无 agentId)。""" - script = _script() - - block = script[script.index("def _dsh_native_patch_block(config):") :] - block = block[: block.index("def _is_patch_entry(")] - assert 'for key in ("endpoint", "token", "tokenHeader"):' in block - assert "userId" not in block and "agentId" not in block - config = script[script.index("def _dsh_native_config():") : script.index("def _yaml_scalar(")] - assert '"tokenHeader": TOKEN_HEADER' in config - assert "userId" not in config - - -def test_dsh_install_cleans_the_legacy_hook_bridge_and_preset() -> None: - """两套接法同时生效会双触发:升级路径先拆旧桥接 / preset bundle。""" - script = _script() - - assert "def _remove_dsh_legacy_hooks():" in script - assert '"dsh-memory-share", "octop-dsh-memory-share"' in script - bridge_cleanup = script[ - script.index("def _remove_dsh_legacy_hooks():") : script.index("def _dsh_mcp_hint():") - ] - assert 'DSH_DIR / "octop-hooks.json"' in bridge_cleanup - assert "shutil.rmtree(scripts)" in bridge_cleanup - - -def test_entries_use_authorization_bearer_only() -> None: - """条目与 hooks 只用 ``Authorization: Bearer ``(token 绑定专家)。""" - script = _script() - - assert '"Authorization": f"Bearer {TOKEN}"' in script - assert "X-Octop-Agent-Id" not in script - assert "客户端 MCP 条目与 hooks:" in script - assert "只用 Authorization: Bearer " in script - - -def test_node_is_resolved_to_an_absolute_path() -> None: - """hook 由终端派生、常在非 login shell 里跑:node 必须解析成绝对路径。""" - script = _script() - - assert 'NODE_BIN="${OCTOP_NODE:-}"' in script - assert "command -v node" in script - assert "$HOME/.nvm/versions/node" in script # nvm 是最常见的坑 - assert "/opt/homebrew/bin/node" in script and "/usr/local/bin/node" in script - assert "找不到 node 的绝对路径" in script # 都找不到时明确告警 - assert 'export OCTOP_NODE="$NODE_BIN"' in script - - -def test_hook_commands_use_the_resolved_node_and_report_failures() -> None: - script = _script() - - assert 'NODE = os.environ.get("OCTOP_NODE") or "node"' in script - assert "def hook_command(script_path):" in script - assert 'hook_command(f"{hooks_dir}/claude-code/stop-capture.mjs")' in script - assert 'hook_command(f"{KIRO_DIR}/kiro/stop-capture.mjs")' in script - # 非 0 退出不再静默:包装脚本把原因打到 stderr - assert "failure = 'echo \"octop-memory: hook 执行失败" in script - # 企业根在系统证书库里、不在 Node 自带库里:Node 22.15+ 才加 --use-system-ca - assert "OCTOP_USE_SYSTEM_CA" in script - assert "--use-system-ca" in script - assert 'NODE_OPTIONS="${NODE_OPTIONS:+$NODE_OPTIONS }--use-system-ca"' in script - - -def test_hook_wrapper_follows_the_platform() -> None: - """POSIX 安装器写 ``.sh``;Windows 安装器写 ``.cmd``(不需要 Git Bash)。""" - script = _script() - - assert "def _sh_hook_command(mjs):" in script - assert "def _cmd_hook_command(mjs):" in script - assert "OCTOP_HOOK_WRAPPER=sh" in script # sh 安装器显式指定 - assert 'wrapper = mjs.with_suffix(".cmd")' in script - assert "if errorlevel 1 echo octop-memory: hook failed" in script - - -def test_installer_names_the_log_and_state_location() -> None: - """日志与增量状态就落在终端自己的 hooks 目录(该目录不可写才退到临时目录)。""" - script = _script() - - assert "日志/增量状态:" in script - assert "{target}/octop-hook.log" in script - assert "{target}/octop-transcript-state.json" in script - assert "退到 {scratch}/octop-hooks" in script - assert '"%TEMP%" if os.name == "nt" else "$TMPDIR"' in script # POSIX / Windows - - -def test_mcp_entry_is_per_terminal_and_keeps_other_entries() -> None: - script = _script() - - assert 'home / ".claude.json"' in script - assert 'home / ".kiro" / "settings" / "mcp.json"' in script - assert "def _octop_servers(servers):" in script # 按 octop-memory* 认已有服务,不认具体名字 - assert "保持原样" in script - assert "if not any(" in script - - -def test_legacy_mcp_tool_hooks_are_recognized_by_tool_not_by_server_name() -> None: - """旧版 mcp_tool hook 的清理按工具名认:服务名可能是 local / prod 或改过名。""" - script = _script() - - assert '_OUR_MCP_TOOLS = ("memory_search", "memory_capture")' in script - assert 'handler.get("tool") in _OUR_MCP_TOOLS' in script - assert 'handler.get("server") == NAME' not in script - - -def test_endpoint_self_check_is_warn_only() -> None: - script = _script() - - assert "端点自检" in script - assert "安装仍继续" in script - # 自检与 hook 共用同一次探测;支持时先 export NODE_OPTIONS,再去 fetch - assert 'if "$NODE_BIN" --use-system-ca -e' in script - assert "export NODE_OPTIONS" in script - assert "AbortSignal.timeout(8000)" in script - # 自检也只发 Authorization Bearer + 专家头 - assert "Authorization: `Bearer ${cfg.token}`" in script - - -def test_server_name_follows_the_endpoint() -> None: - assert "OCTOP_SERVER_NAME='octop-memory-local'" in _script() - remote = _script(mcp_url="https://octop.example.com/mcp/") - assert "OCTOP_SERVER_NAME='octop-memory'" in remote - assert "octop-memory-local" not in remote - - -def test_generator_knows_only_the_supported_terminals() -> None: - assert TERMINALS == ("claude-code", "kiro", "dsh", "codex", "cursor") - - -def test_generated_shell_and_embedded_python_are_valid() -> None: - """脚本是 shell + 内嵌 python 拼出来的:两半都要能独立解析。""" - ast.parse(_MERGE_PY) - ast.parse(_UNINSTALL_PY) - ast.parse(_SELECT_PY) - assert "sys.exit(main())" in _SELECT_PY - - -def test_codex_and_cursor_are_detected_and_offered() -> None: - """两个宿主都要进检测与菜单。""" - script = _script() - - assert '[ -d "$HOME/.codex" ] || command -v codex' in script - assert '[ -d "$HOME/.cursor" ] || command -v cursor' in script - assert 'echo " 4) Codex' in script and 'echo " 5) Cursor' in script - assert '"codex|Codex' in script and '"cursor|Cursor' in script - assert "请重选(1-5,或 a=全部)" in script - assert "4) _add=" in script and "5) _add=" in script - assert '"codex": do_codex' in script and '"cursor": do_cursor' in script - - -def test_terminal_selection_is_an_interactive_checklist() -> None: - """选终端是清单式:↑↓ 移动 · 空格 选择 · a 全选 · 回车 确认。""" - script = _script() - - assert "OCTOP_SELECT" in script - assert "↑/↓ 移动 · 空格 选择 · a 全选 · 回车 确认" in script - assert 'if key == b"\\x1b":' in script # 方向键 ESC [ A/B - assert 'return {b"[A": "up", b"[B": "down"}' in script - assert 'if key == b" ":' in script # 空格切换 - assert "setraw(fd)" in script # raw 模式读单键 - assert 'box = "◉" if chosen[index] else "◯"' in script # 勾选状态可见 - # 一个都没选时回车不确认;无 tty / python3 不可用则回退数字输入 - assert "if not any(chosen):" in script - assert "可多选:1,2 / 1 3 / 12 / a=全部" in script - - -def test_installer_mentions_the_windows_native_installer() -> None: - """sh 版在 Windows(Git Bash/MSYS)上提示改用 PowerShell 原生安装器。""" - script = _script() - - assert "MINGW*|MSYS*|CYGWIN*" in script - assert '?os=windows" | iex' in script - - -def test_the_mcp_token_is_the_only_credential() -> None: - """token 绑定专家:渲染出的脚本 / 配置里没有专家 ID 与专家头。""" - scripts = ( - _script(), - render_uninstall_script(), - render_setup_ps1(mcp_url=_URL), - render_uninstall_ps1(), - ) - for script in scripts: - for word in ("X-Octop-Agent-Id", "agentId", "OCTOP_AGENT", "专家 ID", "DEFAULT_AGENT"): - assert word not in script, f"安装器还在用专家 ID:{word}" - - -def test_no_internal_only_vocabulary_leaks() -> None: - """脱敏底线:安装器不能出现废弃的身份头或第二套凭据。""" - scripts = ( - _script(), - render_uninstall_script(), - render_setup_ps1(mcp_url=_URL), - render_uninstall_ps1(), - ) - forbidden = ( - "OCTOP_USER_ID", - "X-Octop-User-Id", - "userId", - "OCTOP_ENTRY_TOKEN", - "OCTOP_DISABLE_OAUTH", - ) - for script in scripts: - for word in forbidden: - assert word not in script, f"安装器泄漏了废弃概念:{word}" - - -# ---------------------------------------------------------------- 真跑生成的脚本 - - -def test_generated_installer_actually_runs(tmp_path) -> None: - """把生成的安装脚本真跑一遍(沙箱 HOME)。 - - 脚本是 shell + 内嵌 python 拼出来的:语法正确不等于能跑,拼装错误(变量改名、 - 参数漏传)只有执行才会发现。 - """ - home, result = _run_terminals( - tmp_path, - "claude-code,kiro,dsh,codex,cursor", - prepare=lambda home: _dsh_profile(home), - ) - - # 每个 hook 宿主只有自己的入口脚本 + 一份共享传输模块 + config.json - assert (home / ".claude/hooks-events/claude-code/stop-capture.mjs").exists() - assert (home / ".claude/hooks-events/claude-code/user-prompt-recall.mjs").exists() - assert (home / ".kiro/hooks/kiro/stop-capture.mjs").exists() - assert (home / ".codex/hooks/codex/stop-capture.mjs").exists() - assert (home / ".codex/hooks/codex/user-prompt-recall.mjs").exists() - assert (home / ".cursor/hooks/cursor/stop-capture.mjs").exists() - assert (home / ".cursor/hooks/cursor/user-prompt-recall.mjs").exists() - for hooks_dir in ( - ".claude/hooks-events", - ".kiro/hooks", - ".codex/hooks", - ".cursor/hooks", - ): - assert (home / hooks_dir / "lib/octop-memory-client.mjs").exists() - assert (home / hooks_dir / "config.json").exists() - # DSH 不装 hooks:它拿原生插件 - assert not (home / ".dsh/hooks/dsh").exists() - # 宿主之间不串 - assert not (home / ".claude/hooks-events/kiro").exists() - assert not (home / ".kiro/hooks/claude-code").exists() - # Codex / Cursor 的 hook 配置只引用包装脚本路径(长命令收在 .sh 里) - assert ".codex/hooks/codex/stop-capture.sh" in (home / ".codex/config.toml").read_text( - encoding="utf-8" - ) - cursor_hooks = json.loads((home / ".cursor/hooks.json").read_text(encoding="utf-8")) - assert ( - "cursor/hooks/cursor/user-prompt-recall.sh" - in cursor_hooks["hooks"]["beforeSubmitPrompt"][0]["command"] - ) - assert "cursor/hooks/cursor/stop-capture.sh" in cursor_hooks["hooks"]["stop"][0]["command"] - # 每个宿主的包装脚本:可执行,内容是「绝对 node 路径 + .mjs + 失败兜底」 - for hooks_dir, host in ( - (".claude/hooks-events", "claude-code"), - (".kiro/hooks", "kiro"), - (".codex/hooks", "codex"), - (".cursor/hooks", "cursor"), - ): - wrapper = home / hooks_dir / host / "stop-capture.sh" - text = wrapper.read_text(encoding="utf-8") - assert text.startswith("#!/bin/sh") - assert f"{host}/stop-capture.mjs" in text - assert "hook 执行失败" in text # 非 0 退出不再静默 - assert os.access(wrapper, os.X_OK) - if "node 使用系统证书库" in result.stdout: - assert "--use-system-ca" in text - # DSH:原生插件落盘 + profile 登记 + cordis.patch.yml 配置块 - bundle = home / ".dsh/bundles/octop-memory-native" - for name in _DSH_NATIVE_FILES: - assert (bundle / name).is_file() - profile = home / ".dsh/profiles/web" - package = json.loads((profile / "package.json").read_text(encoding="utf-8")) - assert package["dependencies"]["octop-memory-native"] == f"link:{bundle}" - assert package["dsh"]["profile"]["bundles"] == [ - "@deepseek-ai/dsh-base", - "octop-memory-native", - ] - assert (profile / "node_modules/octop-memory-native").is_symlink() - patch = (profile / "cordis.patch.yml").read_text(encoding="utf-8") - assert patch.count("- id: octop-memory-native") == 1 # 跑两遍也不重复 - assert "endpoint:" in patch and "tokenHeader:" in patch - assert "userId" not in patch and "agentId" not in patch - assert "不依赖 ~/.dsh/hooks 的 hook 桥接" in patch - - -_CODEX_EXISTING = """model = "gpt-5" - -[[hooks.PreToolUse]] - -[[hooks.PreToolUse.hooks]] -command = "/usr/local/bin/git-ai checkpoint codex" -type = "command" - -[[hooks.Stop]] - -[[hooks.Stop.hooks]] -command = "/usr/local/bin/git-ai checkpoint codex" -type = "command" - -[hooks.state."CONFIG:stop:0:0"] -enabled = true -trusted_hash = "sha256:deadbeef" -""" - - -def _dsh_profile(home: Path, bundles: tuple[str, ...] = ("@deepseek-ai/dsh-base",)) -> Path: - """沙箱里的 DSH profile(``~/.dsh/profiles/web/package.json``)。""" - profile = home / ".dsh/profiles/web" - profile.mkdir(parents=True, exist_ok=True) - (profile / "package.json").write_text( - json.dumps( - {"name": "profile-web", "dsh": {"profile": {"bundles": list(bundles)}}}, indent=2 - ) - + "\n", - encoding="utf-8", - ) - return profile - - -def _run_terminals(tmp_path, terminals: str, prepare=None, extra_env=None, home_name="home"): - """按给定终端跑两遍安装器(第二遍验证幂等),返回 (HOME, 最后一次输出)。""" - home = tmp_path / home_name - home.mkdir(parents=True, exist_ok=True) - if prepare is not None: - prepare(home) - script = tmp_path / f"setup-{home_name}.sh" - script.write_text(_script(), encoding="utf-8") - env = { - **os.environ, - "HOME": str(home), - "OCTOP_TERMINALS": terminals, - "OCTOP_MEMORY_TOKEN": _TOKEN, - **(extra_env or {}), - } - result = None - for _ in range(2): - result = subprocess.run( - ["sh", str(script)], env=env, capture_output=True, text=True, timeout=300 - ) - assert result.returncode == 0, result.stdout + result.stderr - assert result is not None - return home, result - - -def _run_uninstall(tmp_path, home: Path, terminals: str, extra_env=None): - script = tmp_path / "uninstall.sh" - script.write_text(render_uninstall_script(), encoding="utf-8") - env = { - **os.environ, - "HOME": str(home), - "OCTOP_TERMINALS": terminals, - **(extra_env or {}), - } - result = subprocess.run( - ["sh", str(script)], env=env, capture_output=True, text=True, timeout=120 - ) - assert result.returncode == 0, result.stdout + result.stderr - return result - - -def test_config_and_entry_carry_the_user_mcp_token(tmp_path) -> None: - """config.json 与 MCP 条目只带「MCP 地址 + 用户 token」:没有 agentId、没有身份头。""" - home, _ = _run_terminals(tmp_path, "claude-code") - - config = json.loads((home / ".claude/hooks-events/config.json").read_text(encoding="utf-8")) - assert config == { - "url": _URL, - "token": _TOKEN, - "tokenHeader": "Authorization", - "client": "claude-code", - } - assert "agentId" not in config - - entry = json.loads((home / ".claude.json").read_text(encoding="utf-8"))["mcpServers"][ - "octop-memory-local" - ] - assert entry["url"] == _URL - assert entry["headers"] == {"Authorization": f"Bearer {_TOKEN}"} - - -def test_entry_defaults_to_the_instance_mount(tmp_path) -> None: - """没有声明公网地址时,条目与 hooks 都落在实例自己的挂载点。""" - instance = "https://octop.example.com/mcp/memory/" - home = tmp_path / "home" - (home / ".claude").mkdir(parents=True) - script = tmp_path / "setup.sh" - script.write_text(_script(mcp_url=instance), encoding="utf-8") - result = subprocess.run( - ["sh", str(script)], - env={ - **os.environ, - "HOME": str(home), - "OCTOP_TERMINALS": "claude-code", - "OCTOP_MEMORY_TOKEN": "shared-token", - }, - capture_output=True, - text=True, - timeout=180, - ) - - assert result.returncode == 0, result.stdout + result.stderr - servers = json.loads((home / ".claude.json").read_text(encoding="utf-8"))["mcpServers"] - entry = next(iter(servers.values())) - assert entry["url"] == instance - assert entry["headers"] == {"Authorization": "Bearer shared-token"} - - -def test_declared_public_url_becomes_the_entry_address(tmp_path) -> None: - """条目地址与 hooks 地址可以分开:条目走部署声明的公网地址,hooks 打实例挂载点。""" - public = "https://octop-memory.example.com/mcp/memory/" - direct = "https://octop.example.com/mcp/memory/" - home = tmp_path / "home" - (home / ".claude").mkdir(parents=True) - script = tmp_path / "setup.sh" - script.write_text(_script(mcp_url=public, hook_url=direct), encoding="utf-8") - result = subprocess.run( - ["sh", str(script)], - env={ - **os.environ, - "HOME": str(home), - "OCTOP_TERMINALS": "claude-code", - "OCTOP_MEMORY_TOKEN": "shared-token", - }, - capture_output=True, - text=True, - timeout=180, - ) - - assert result.returncode == 0, result.stdout + result.stderr - servers = json.loads((home / ".claude.json").read_text(encoding="utf-8"))["mcpServers"] - name, entry = next(iter(servers.items())) - assert name == "octop-memory" - assert entry["url"] == public - config = json.loads((home / ".claude/hooks-events/config.json").read_text(encoding="utf-8")) - assert config["url"] == direct and config["token"] == "shared-token" - - -def test_without_a_token_the_installer_still_demands_one(tmp_path) -> None: - """无 tty + 没给环境变量 → 明确报错,不盲读 stdin。""" - script = tmp_path / "setup.sh" - script.write_text(_script(), encoding="utf-8") - home = tmp_path / "home" - (home / ".claude").mkdir(parents=True) - env = { - **os.environ, - "HOME": str(home), - "OCTOP_TERMINALS": "claude-code", - } - env.pop("OCTOP_MEMORY_TOKEN", None) - result = subprocess.run( - ["sh", str(script)], env=env, capture_output=True, text=True, timeout=180 - ) - - assert result.returncode != 0 - assert "缺少 MCP token" in result.stderr - assert "DEFAULT_TOKEN=" not in _script() # 没预填值就不该有赋值 - - -def test_codex_hooks_join_the_existing_config_without_clobbering_it(tmp_path) -> None: - """~/.codex/config.toml 已有别人的 hook:只追加我们的,别的字段与信任状态原样保留。""" - - def prepare(home): - (home / ".codex").mkdir(parents=True) - config = home / ".codex/config.toml" - (config).write_text(_CODEX_EXISTING.replace("CONFIG", str(config)), encoding="utf-8") - - home, _ = _run_terminals(tmp_path, "codex", prepare) - cfg = tomllib.loads((home / ".codex/config.toml").read_text(encoding="utf-8")) - hooks = cfg["hooks"] - - def commands(event): - return [entry["command"] for group in hooks[event] for entry in group.get("hooks", [])] - - assert cfg["model"] == "gpt-5" # 其它字段不动 - assert len(commands("PreToolUse")) == 1 and "git-ai" in commands("PreToolUse")[0] - assert len(commands("Stop")) == 2 # 别人的 + 我们的(跑了两遍也不重复) - assert any("codex/stop-capture.sh" in command for command in commands("Stop")) - assert len(commands("UserPromptSubmit")) == 1 - assert any("codex/user-prompt-recall.sh" in command for command in commands("UserPromptSubmit")) - assert list(hooks["state"]) == [f"{home}/.codex/config.toml:stop:0:0"] # 信任状态保留 - - -def test_cursor_hooks_join_the_existing_json_without_clobbering_it(tmp_path) -> None: - """~/.cursor/hooks.json 其它事件(如 git-ai 的 postToolUse)原样保留。""" - - def prepare(home): - (home / ".cursor").mkdir(parents=True) - (home / ".cursor/hooks.json").write_text( - json.dumps( - {"hooks": {"postToolUse": [{"command": "git-ai cursor"}]}, "version": 1}, indent=2 - ) - + "\n", - encoding="utf-8", - ) - - home, _ = _run_terminals(tmp_path, "cursor", prepare) - cfg = json.loads((home / ".cursor/hooks.json").read_text(encoding="utf-8")) - - assert cfg["version"] == 1 - assert cfg["hooks"]["postToolUse"] == [{"command": "git-ai cursor"}] - assert len(cfg["hooks"]["beforeSubmitPrompt"]) == 1 # 幂等:跑两遍仍只有一条 - assert ( - "cursor/hooks/cursor/user-prompt-recall.sh" - in cfg["hooks"]["beforeSubmitPrompt"][0]["command"] - ) - assert len(cfg["hooks"]["stop"]) == 1 - assert "cursor/hooks/cursor/stop-capture.sh" in cfg["hooks"]["stop"][0]["command"] - - -# ---------------------------------------------------------------- DSH 原生插件 - - -@posix_only -def test_dsh_native_install_is_idempotent(tmp_path) -> None: - """DSH:插件目录、profile 登记、配置块都幂等(跑两遍结果一致)。""" - - def prepare(home): - (home / ".dsh").mkdir(parents=True) - _dsh_profile(home) - - home, _ = _run_terminals(tmp_path, "dsh", prepare) - bundle = home / ".dsh/bundles/octop-memory-native" - profile = home / ".dsh/profiles/web" - - for name in _DSH_NATIVE_FILES: - assert (bundle / name).is_file() - # 插件是从 vendored 副本逐字节还原的 - for name in _DSH_NATIVE_FILES: - assert (bundle / name).read_bytes() == (_DSH_NATIVE_SRC / name).read_bytes() - - package = json.loads((profile / "package.json").read_text(encoding="utf-8")) - assert package["dependencies"] == {"octop-memory-native": f"link:{bundle}"} - assert package["dsh"]["profile"]["bundles"] == ["@deepseek-ai/dsh-base", "octop-memory-native"] - assert (profile / "node_modules/octop-memory-native").is_symlink() - - patch = (profile / "cordis.patch.yml").read_text(encoding="utf-8") - assert patch.count("- id: octop-memory-native") == 1 - assert "# <<< octop-memory-native <<<" in patch - # 配置块 = 用户级 token 形状(无 userId / agentId) - assert " endpoint: 'http://127.0.0.1:8088/mcp/memory/'" in patch - assert " token: 'omcp_test_token'" in patch - assert " tokenHeader: 'Authorization'" in patch - assert "userId" not in patch and "agentId" not in patch # token 绑定专家 - - -@posix_only -def test_dsh_native_install_keeps_other_patch_entries(tmp_path) -> None: - """profile 的 cordis.patch.yml 里别人的条目 / 注释逐字保留,我们只追加自己的块。""" - existing = ( - "# 用户自己的 patch\n" - "- insert:\n" - " - id: some-other-plugin\n" - " name: 'some-other-plugin'\n" - ) - - def prepare(home): - (home / ".dsh").mkdir(parents=True) - profile = _dsh_profile(home) - (profile / "cordis.patch.yml").write_text(existing, encoding="utf-8") - - home, _ = _run_terminals(tmp_path, "dsh", prepare) - patch = (home / ".dsh/profiles/web/cordis.patch.yml").read_text(encoding="utf-8") - - assert patch.startswith(existing) - assert patch.count("- id: some-other-plugin") == 1 - assert patch.count("- id: octop-memory-native") == 1 - # 覆盖前备份原文件 - assert (home / ".dsh/profiles/web/cordis.patch.yml.octop.bak").read_text( - encoding="utf-8" - ) == existing - - -@posix_only -def test_dsh_install_removes_the_legacy_bridge_and_preset(tmp_path) -> None: - """从 hooks 方案升级:旧桥接 / preset bundle / 脚本目录都拆掉,避免双触发。""" - - def prepare(home): - (home / ".dsh").mkdir(parents=True) - shim = {"name": "profile-web", "dsh": {"profile": {"bundles": ["dsh-memory-share"]}}} - profile = home / ".dsh/profiles/web" - profile.mkdir(parents=True) - (profile / "package.json").write_text(json.dumps(shim, indent=2) + "\n", encoding="utf-8") - preset = home / ".dsh/bundles/dsh-memory-share" - preset.mkdir(parents=True) - (preset / "package.json").write_text("{}", encoding="utf-8") - (home / ".dsh/hooks").mkdir(parents=True) - (home / ".dsh/hooks/octop-hooks.json").write_text("{}", encoding="utf-8") - (home / ".dsh/hooks/dsh").mkdir() - (home / ".dsh/hooks/dsh/stop-capture.mjs").write_text("//", encoding="utf-8") - - home, result = _run_terminals(tmp_path, "dsh", prepare) - profile = home / ".dsh/profiles/web" - - assert not (home / ".dsh/bundles/dsh-memory-share").exists() - assert not (home / ".dsh/hooks/octop-hooks.json").exists() - assert not (home / ".dsh/hooks/dsh").exists() - assert json.loads((profile / "package.json").read_text(encoding="utf-8"))["dsh"]["profile"][ - "bundles" - ] == ["octop-memory-native"] - - -@posix_only -def test_dsh_prefers_the_cli_and_still_registers_the_profile(tmp_path) -> None: - """有 dsh CLI 时优先用它装依赖;登记与配置块仍然由安装器写好(幂等)。""" - - def prepare(home): - (home / ".dsh").mkdir(parents=True) - _dsh_profile(home) - - fake_bin = tmp_path / "bin" - fake_bin.mkdir() - shim = fake_bin / "dsh" - shim.write_text('#!/bin/sh\nprintf \'%s\\n\' "$@" >> "$0.args"\n', encoding="utf-8") - shim.chmod(0o755) - - home, _ = _run_terminals( - tmp_path, - "dsh", - prepare, - extra_env={"PATH": f"{fake_bin}{os.pathsep}{os.environ['PATH']}"}, - ) - profile = home / ".dsh/profiles/web" - - calls = (fake_bin / "dsh.args").read_text(encoding="utf-8") - assert "plugin" in calls and "--profile" in calls and "web" in calls - assert str(home / ".dsh/bundles/octop-memory-native") in calls - assert not (fake_bin / "pnpm.args").exists() # 有 dsh 就不必自己调 pnpm - assert ( - "octop-memory-native" - in json.loads((profile / "package.json").read_text(encoding="utf-8"))["dsh"]["profile"][ - "bundles" - ] - ) - - -@posix_only -def test_dsh_uninstall_removes_the_native_plugin(tmp_path) -> None: - """卸载:插件目录 / profile 登记 / node_modules 条目 / 配置块都清掉。""" - - def prepare(home): - (home / ".dsh").mkdir(parents=True) - _dsh_profile(home) - - home, _ = _run_terminals(tmp_path, "dsh", prepare) - profile = home / ".dsh/profiles/web" - assert (home / ".dsh/bundles/octop-memory-native").exists() - - _run_uninstall(tmp_path, home, "dsh") - - assert not (home / ".dsh/bundles/octop-memory-native").exists() - assert not (profile / "node_modules/octop-memory-native").exists() - package = json.loads((profile / "package.json").read_text(encoding="utf-8")) - assert "octop-memory-native" not in package["dependencies"] - assert package["dsh"]["profile"]["bundles"] == ["@deepseek-ai/dsh-base"] # 别人的不动 - # 只含我们配置块的 patch 文件被删掉 - assert not (profile / "cordis.patch.yml").exists() - - -def test_dsh_is_skipped_in_project_mode(tmp_path) -> None: - """DSH 没有项目级配置:项目模式跳过(不算失败,也不写 DSH 文件)。""" - - def prepare(home): - (home / ".dsh").mkdir(parents=True) - _dsh_profile(home) - - project = tmp_path / "proj" - project.mkdir() - home, result = _run_terminals( - tmp_path, - "claude-code,dsh", - prepare, - extra_env={"OCTOP_PROJECT_DIR": str(project)}, - ) - - assert "项目模式跳过 DSH" in result.stdout - assert not (home / ".dsh/bundles/octop-memory-native").exists() - assert not (home / ".dsh/profiles/web/cordis.patch.yml").exists() - assert (home / ".claude/hooks-events/config.json").exists() # 其它终端照装 - - -# ---------------------------------------------------------------- 项目级安装 - - -def test_project_install_registers_into_the_project_and_keeps_scripts_in_home(tmp_path) -> None: - """项目模式:项目里只写注册;hook 脚本与含 token 的 config.json 留在用户目录。""" - - def prepare(home): - (home / ".claude").mkdir(parents=True) - (home / ".kiro").mkdir(parents=True) - (home / ".codex").mkdir(parents=True) - (home / ".cursor").mkdir(parents=True) - - project = tmp_path / "proj" - project.mkdir() - (project / ".git").mkdir() - home, result = _run_terminals( - tmp_path, - "claude-code,kiro,codex,cursor", - prepare, - extra_env={"OCTOP_PROJECT_DIR": str(project)}, - ) - - assert f"安装范围:项目级 → {project.resolve()}" in result.stdout - # 注册落在项目里 - settings = json.loads((project / ".claude/settings.local.json").read_text(encoding="utf-8")) - assert "UserPromptSubmit" in settings["hooks"] and "Stop" in settings["hooks"] - assert "codex/stop-capture.sh" in (project / ".codex/config.toml").read_text(encoding="utf-8") - cursor = json.loads((project / ".cursor/hooks.json").read_text(encoding="utf-8")) - assert "beforeSubmitPrompt" in cursor["hooks"] and "stop" in cursor["hooks"] - # Kiro 的 hooks 是命令(无凭据)→ 写进项目 - assert (project / ".kiro/hooks/memory-save-on-stop.json").exists() - # 脚本与凭据留在 HOME - for hooks_dir in (".claude/hooks-events", ".kiro/hooks", ".codex/hooks", ".cursor/hooks"): - assert (home / hooks_dir / "config.json").exists() - assert (home / ".claude/hooks-events/claude-code/stop-capture.mjs").exists() - # 项目里没有任何凭据 - for path in project.rglob("*"): - if path.is_file(): - assert _TOKEN not in path.read_text(encoding="utf-8"), path - - -def test_project_mode_skips_the_kiro_mcp_entry(tmp_path) -> None: - """Kiro 的 MCP 条目自带凭据、会随 git 提交:项目模式跳过并提示。""" - - def prepare(home): - (home / ".kiro").mkdir(parents=True) - - project = tmp_path / "proj" - project.mkdir() - home, result = _run_terminals( - tmp_path, "kiro", prepare, extra_env={"OCTOP_PROJECT_DIR": str(project)} - ) - - assert "项目模式跳过" in result.stdout - assert not (project / ".kiro/settings/mcp.json").exists() - # hooks(只有命令)照旧进项目,脚本留在 HOME - assert (project / ".kiro/hooks/memory-recall-on-start.json").exists() - assert (home / ".kiro/hooks/config.json").exists() - - -def test_claude_code_project_mode_uses_the_local_scope(tmp_path) -> None: - """Claude Code 的 MCP 走 ``~/.claude.json`` 的 local scope(按项目路径存,不进仓库)。""" - - def prepare(home): - (home / ".claude").mkdir(parents=True) - - project = tmp_path / "proj" - project.mkdir() - home, _ = _run_terminals( - tmp_path, "claude-code", prepare, extra_env={"OCTOP_PROJECT_DIR": str(project)} - ) - - projects = json.loads((home / ".claude.json").read_text(encoding="utf-8"))["projects"] - key = os.path.realpath(str(project)) - assert key in projects - entry = projects[key]["mcpServers"]["octop-memory-local"] - assert entry["headers"]["Authorization"] == f"Bearer {_TOKEN}" - assert not (project / ".mcp.json").exists() # 不写仓库里的 .mcp.json - - -def test_project_env_wins_over_the_query_prefill(tmp_path) -> None: - """``project=`` 只是交互式预填:非交互只认 OCTOP_PROJECT_DIR,显式置空 = 全局。""" - home = tmp_path / "home" - (home / ".claude").mkdir(parents=True) - project = tmp_path / "prefilled" - project.mkdir() - target = tmp_path / "from-env" - target.mkdir() - script = tmp_path / "setup.sh" - script.write_text(_script(project=str(project)), encoding="utf-8") - base_env = { - **os.environ, - "HOME": str(home), - "OCTOP_TERMINALS": "claude-code", - "OCTOP_MEMORY_TOKEN": _TOKEN, - } - - # 环境变量优先:装到 from-env,而不是 query 预填的 prefilled - result = subprocess.run( - ["sh", str(script)], - env={**base_env, "OCTOP_PROJECT_DIR": str(target)}, - capture_output=True, - text=True, - timeout=180, - ) - assert result.returncode == 0, result.stdout + result.stderr - assert (target / ".claude/settings.local.json").exists() - assert not (project / ".claude").exists() - - # 显式置空 = 强制全局 - home2 = tmp_path / "home2" - (home2 / ".claude").mkdir(parents=True) - result = subprocess.run( - ["sh", str(script)], - env={**base_env, "HOME": str(home2), "OCTOP_PROJECT_DIR": ""}, - capture_output=True, - text=True, - timeout=180, - ) - assert result.returncode == 0, result.stdout + result.stderr - assert "安装范围:项目级" not in result.stdout - assert (home2 / ".claude/settings.json").exists() # 全局安装 - - -def test_project_uninstall_only_clears_the_registrations(tmp_path) -> None: - """项目级卸载只清项目的注册,用户目录里的 hook 脚本保留给全局卸载。""" - - def prepare(home): - (home / ".claude").mkdir(parents=True) - (home / ".codex").mkdir(parents=True) - - project = tmp_path / "proj" - project.mkdir() - home, _ = _run_terminals( - tmp_path, - "claude-code,codex", - prepare, - extra_env={"OCTOP_PROJECT_DIR": str(project)}, - ) - assert (project / ".claude/settings.local.json").exists() - - _run_uninstall(tmp_path, home, "claude-code,codex", {"OCTOP_PROJECT_DIR": str(project)}) - - settings = json.loads((project / ".claude/settings.local.json").read_text(encoding="utf-8")) - assert settings["hooks"] == {} - assert "[[hooks." not in (project / ".codex/config.toml").read_text(encoding="utf-8") - # 用户目录里的脚本与 config.json 还在 - assert (home / ".claude/hooks-events/claude-code/stop-capture.mjs").exists() - assert (home / ".claude/hooks-events/config.json").exists() - - -# ---------------------------------------------------------------- 卸载 - - -def test_uninstall_script_mentions_cursor_and_needs_no_token() -> None: - script = render_uninstall_script() - assert script.startswith("#!/bin/sh") - assert "octop memory 卸载" in script - assert "OCTOP_TERMINALS" in script - assert "Cursor" in script - assert "OCTOP_MEMORY_TOKEN" not in script - assert "undo_cursor" in script or "CURSOR_DIR" in script - # 卸载也支持项目级范围 - assert "OCTOP_PROJECT_DIR" in script - assert "卸载范围" in script - - -def test_uninstall_script_shell_quotes_terminals_and_project_query() -> None: - """HTTP ``terminals`` / ``project`` must not break out of the shell assignment.""" - script = render_uninstall_script( - terminals="cursor'; curl evil.example", project="/p'; curl evil.example" - ) - assert "DEFAULT_TERMINALS='cursor'\"'\"'; curl evil.example'" in script - assert "DEFAULT_PROJECT='/p'\"'\"'; curl evil.example'" in script - assert "DEFAULT_TERMINALS='cursor'; curl evil.example'" not in script - - -def test_cursor_uninstall_removes_hooks_keeps_others(tmp_path) -> None: - """setup → uninstall:octop hooks/目录清掉,别人的 hook 与无关 MCP 保留。""" - - def prepare(home): - (home / ".cursor").mkdir(parents=True) - (home / ".cursor/hooks.json").write_text( - json.dumps( - {"hooks": {"postToolUse": [{"command": "git-ai cursor"}]}, "version": 1}, - indent=2, - ) - + "\n", - encoding="utf-8", - ) - (home / ".cursor/mcp.json").write_text( - json.dumps( - { - "mcpServers": { - "octop-memory": {"url": "https://example/mcp"}, - "other": {"url": "https://other"}, - } - }, - indent=2, - ) - + "\n", - encoding="utf-8", - ) - - home, _ = _run_terminals(tmp_path, "cursor", prepare) - assert (home / ".cursor/hooks/cursor/stop-capture.mjs").exists() - - _run_uninstall(tmp_path, home, "cursor") - - cfg = json.loads((home / ".cursor/hooks.json").read_text(encoding="utf-8")) - assert cfg["hooks"]["postToolUse"] == [{"command": "git-ai cursor"}] - assert "beforeSubmitPrompt" not in cfg["hooks"] - assert "stop" not in cfg["hooks"] - assert not (home / ".cursor/hooks").exists() - - mcp = json.loads((home / ".cursor/mcp.json").read_text(encoding="utf-8")) - assert "octop-memory" not in mcp["mcpServers"] - assert mcp["mcpServers"]["other"]["url"] == "https://other" - - -def _run_claude_code_installer(tmp_path, claude_json: dict, settings: dict | None = None): - """只装 Claude Code:沙箱 HOME + 预置的 .claude.json / settings.json。""" - home = tmp_path / "home" - (home / ".claude").mkdir(parents=True) - (home / ".claude.json").write_text(json.dumps(claude_json, indent=2) + "\n", encoding="utf-8") - if settings is not None: - (home / ".claude/settings.json").write_text( - json.dumps(settings, indent=2) + "\n", encoding="utf-8" - ) - script = tmp_path / "setup.sh" - script.write_text(_script(), encoding="utf-8") - result = subprocess.run( - ["sh", str(script)], - env={ - **os.environ, - "HOME": str(home), - "OCTOP_TERMINALS": "claude-code", - "OCTOP_MEMORY_TOKEN": _TOKEN, - }, - capture_output=True, - text=True, - timeout=180, - ) - assert result.returncode == 0, result.stdout + result.stderr - return home, result - - -def test_mcp_entry_is_added_when_no_octop_server_exists(tmp_path) -> None: - home, _ = _run_claude_code_installer( - tmp_path, {"mcpServers": {"other-mcp": {"type": "http", "url": "http://other/"}}} - ) - servers = json.loads((home / ".claude.json").read_text(encoding="utf-8"))["mcpServers"] - assert "octop-memory-local" in servers # 全新安装才新建条目 - assert "other-mcp" in servers # 别人的条目原样保留 - - -def test_mcp_entry_reuses_an_existing_octop_entry_with_the_same_endpoint(tmp_path) -> None: - """已有同一个端点的 octop 服务(名字可能是 prod / 自定义)→ 复用,不新增条目。""" - home, result = _run_claude_code_installer( - tmp_path, - {"mcpServers": {"octop-memory-prod": {"type": "http", "url": _URL}}}, - ) - servers = json.loads((home / ".claude.json").read_text(encoding="utf-8"))["mcpServers"] - assert list(servers) == ["octop-memory-prod"] - assert "复用" in result.stdout - - -def test_mcp_entry_does_not_add_a_duplicate_for_a_different_octop_endpoint(tmp_path) -> None: - """已有别的 octop 端点时保持原样:不按服务名硬塞第二个 octop 服务。""" - home, result = _run_claude_code_installer( - tmp_path, - { - "mcpServers": { - "octop-memory-other": {"type": "http", "url": "https://other.example/mcp/"} - } - }, - ) - servers = json.loads((home / ".claude.json").read_text(encoding="utf-8"))["mcpServers"] - assert list(servers) == ["octop-memory-other"] - assert "保持原样" in result.stdout - - -def test_legacy_mcp_tool_hook_under_another_server_name_is_replaced(tmp_path) -> None: - """旧版 mcp_tool hook 的服务名可能是 octop-memory-prod:按工具名清掉它。""" - legacy = { - "hooks": { - "UserPromptSubmit": [ - { - "hooks": [ - { - "type": "mcp_tool", - "server": "octop-memory-prod", - "tool": "memory_search", - "input": {"query": "${prompt}"}, - } - ] - } - ] - } - } - home, _ = _run_claude_code_installer(tmp_path, {"mcpServers": {}}, settings=legacy) - entries = json.loads((home / ".claude/settings.json").read_text(encoding="utf-8"))["hooks"][ - "UserPromptSubmit" - ] - assert len(entries) == 1 # 旧 mcp_tool 那条被替换,只剩我们新写的 - assert entries[0]["hooks"][0]["type"] == "command" # matcher 组里套 handler - assert not any(entry.get("type") == "mcp_tool" for entry in entries) - - -# ---------------------------------------------------------------- Windows(PowerShell) - - -def test_powershell_installer_renders_a_native_installer() -> None: - """``?os=windows`` 是原生 PowerShell 安装器:不需要 Git Bash,也不写 .cmd 包装链。""" - # agent_id 形参保留但被忽略 - script = render_setup_ps1(mcp_url=_URL, agent_id="AGT1", terminals="cursor") - - assert "$PSVersionTable.PSVersion.Major" in script - assert "Read-Host" in script and "Read-Secret" in script # 不回显 - assert "Join-Path $HOME '.claude\\hooks-events'" in script - assert "Join-Path $HOME '.cursor\\hooks'" in script - assert "$DEFAULT_TERMINALS = 'cursor'" in script - # token 绑定专家:没有专家 ID 可填,agent_id 形参被忽略 - assert "DEFAULT_AGENT" not in script and "专家 ID" not in script - assert "OCTOP_AGENT" not in script - # 同一个 Python 合并程序 + .cmd 包装脚本 - assert "OCTOP_HOOK_WRAPPER = 'cmd'" in script - assert "def _cmd_hook_command(mjs):" in script - # 五个终端与 POSIX 槽位一致 - assert "{ 1 = 'claude-code'; 2 = 'kiro'; 3 = 'dsh'; 4 = 'codex'; 5 = 'cursor' }" in script - assert "1-5" in script - # 用户级 token 的提示与没有 userId - assert "MCP token(omcp_…,在 Octop 的 /api/memory-mcp/tokens 创建;不回显)" in script - assert "OCTOP_USER_ID" not in script and "X-Octop-User-Id" not in script - # 项目级范围 - assert "OCTOP_PROJECT_DIR" in script and "安装范围" in script - # 环境块长度:配置走临时 JSON - assert "OCTOP_ENV_FILE" in script - assert "GetTempFileName" in script - # 没有留下未替换的占位符 - assert "__URL_LITERAL__" not in script and "__MERGE_PY__" not in script - - -def test_powershell_uninstaller_renders() -> None: - script = render_uninstall_ps1(terminals="cursor", project="/tmp/proj") - - assert "$PSVersionTable.PSVersion.Major" in script - assert "$DEFAULT_TERMINALS = 'cursor'" in script - assert "$DEFAULT_PROJECT = '/tmp/proj'" in script - assert "undo_cursor" in script - assert "卸载范围" in script - assert "OCTOP_ENV_FILE" in script - assert "__UNINSTALL_PY__" not in script and "__DEFAULT_PROJECT_LITERAL__" not in script - - -def test_powershell_quoting_escapes_single_quotes() -> None: - """PowerShell 单引号串里 ``'`` 要写成 ``''``(不然后面的语句被吃掉)。""" - script = render_setup_ps1( - mcp_url=_URL, - agent_id="a'b", # 形参保留但忽略:不应出现在脚本里 - terminals="cursor'x", - project="/tmp/p'x", - ) - - assert "$DEFAULT_TERMINALS = 'cursor''x'" in script - assert "$DEFAULT_PROJECT = '/tmp/p''x'" in script - assert "DEFAULT_AGENT" not in script - - -def test_powershell_project_scope_honours_an_explicitly_empty_env() -> None: - """与 sh 安装器一致:显式设为空串 = 强制全局,不再交互提问。""" - script = render_setup_ps1(mcp_url=_URL) - assert "$projectSet = $null -ne $env:OCTOP_PROJECT_DIR" in script - assert "if (-not $projectSet -and $interactive)" in script - uninstall = render_uninstall_ps1() - assert "$projectSet = $null -ne $env:OCTOP_PROJECT_DIR" in uninstall - assert "if (-not $projectSet -and $interactive)" in uninstall - - -def test_powershell_installer_embeds_every_host_and_the_native_plugin() -> None: - script = render_setup_ps1(mcp_url=_URL) - - assert "OCTOP_FILES_CLAUDE_CODE = " in script - assert "OCTOP_FILES_KIRO = " in script - assert "OCTOP_FILES_CODEX = " in script and "OCTOP_FILES_CURSOR = " in script - assert "OCTOP_FILES_NATIVE = " in script - assert "OCTOP_FILES_DSH = " not in script - assert "OCTOP_FILES_PRESET = " not in script - - -def test_vendored_plugin_files_exist_for_every_entry_in_the_manifest() -> None: - """清单与 vendored 目录必须一致(升级插件时同步两处)。""" - missing = [name for name in _DSH_NATIVE_FILES if not (_DSH_NATIVE_SRC / name).is_file()] - assert missing == [] - package = json.loads((_DSH_NATIVE_SRC / "package.json").read_text(encoding="utf-8")) - assert package["name"] == _DSH_NATIVE_NAME diff --git a/tests/unit/agents/test_hook_scripts.py b/tests/unit/agents/test_hook_scripts.py deleted file mode 100644 index 59ca8b0cf6..0000000000 --- a/tests/unit/agents/test_hook_scripts.py +++ /dev/null @@ -1,220 +0,0 @@ -"""Hook scripts are data files, but their layout and write locations are a contract.""" - -from __future__ import annotations - -import json -import shutil -import subprocess -from pathlib import Path - -import pytest - -_HOOKS = Path(__file__).parents[3] / "src" / "octop" / "infra" / "agents" / "hook_scripts" -_LIB = _HOOKS / "lib" / "octop-memory-client.mjs" -_PLUGIN = Path(__file__).parents[3] / "src" / "octop" / "infra" / "agents" / "dsh_native_plugin" -_NODE = shutil.which("node") or shutil.which("nodejs") -node_required = pytest.mark.skipif(_NODE is None, reason="需要 node 才能执行 .mjs 断言") -_HOSTS = { - "claude-code": ("stop-capture.mjs", "user-prompt-recall.mjs"), - "kiro": ("stop-capture.mjs",), - "dsh": ("stop-capture.mjs", "user-prompt-recall.mjs", "memory-target.mjs"), - "codex": ("stop-capture.mjs", "user-prompt-recall.mjs"), - "cursor": ("stop-capture.mjs", "user-prompt-recall.mjs"), -} - - -def test_every_host_has_its_own_entry_scripts() -> None: - """每个宿主一个入口脚本:一个宿主的改动不会碰到别人。""" - for host, names in _HOSTS.items(): - for name in names: - assert (_HOOKS / host / name).exists(), f"{host}/{name} 缺失" - - -def test_entries_do_not_detect_the_host_at_runtime() -> None: - """宿主差异体现在安装时选的脚本里,脚本内部不再做宿主嗅探。""" - for host, names in _HOSTS.items(): - if not (_HOOKS / host).exists(): - continue - for name in names: - path = _HOOKS / host / name - if not path.exists(): - continue - text = path.read_text(encoding="utf-8") - for sniff in ("findDshSessionFile", "kiroRoot", "dshRoot", "sourceOverride"): - assert sniff not in text, f"{host}/{name} 仍在运行时探测宿主({sniff})" - - -def test_transport_module_is_host_agnostic() -> None: - """共享模块只做传输(配置/鉴权/偏移/日志/压缩),不认识任何宿主。""" - text = _LIB.read_text(encoding="utf-8") - - assert "export async function capture" in text - assert "export async function callTool" in text - assert "decompressZstdContainer" in text - # 认证是用户级 token:token 走 Authorization: Bearer;token 在服务端已绑定专家, - # 所以客户端不再发送任何身份头(用户标识或专家标识都不传)。 - assert "`Bearer ${config.token}`" in text - for identity in ("X-Octop-Agent-Id", "X-Octop-User-Id", "OCTOP_AGENT_ID", "OCTOP_USER_ID"): - assert identity not in text, f"共享模块仍发身份头:{identity}" - assert "agentId" not in text and "userId" not in text - for host_word in ("claude-code", "kiro", "dsh", "codex", "cursor", "transcript_path"): - assert host_word not in text, f"共享模块不应出现宿主细节:{host_word}" - - -def test_no_hook_or_plugin_sends_an_identity_header() -> None: - """token 已绑定专家:hooks 与原生插件都不出现专家头/用户头及其配置项。""" - sources = [*sorted(_HOOKS.rglob("*.mjs")), _PLUGIN / "index.js", *_PLUGIN.glob("lib/*.js")] - assert sources, "没找到任何 hook/plugin 源文件" - for path in sources: - text = path.read_text(encoding="utf-8") - for stale in ( - "X-Octop-Agent-Id", - "X-Octop-User-Id", - "OCTOP_AGENT_ID", - "OCTOP_USER_ID", - "agentId", - "userId", - ): - assert stale not in text, f"{path.name} 仍在用旧的客户端身份标识({stale})" - - -def test_every_entry_uses_the_shared_configured_guard() -> None: - """「有没有配好」只由共享模块的 isConfigured() 判,入口脚本别各写一套。""" - assert "export function isConfigured" in _LIB.read_text(encoding="utf-8") - for host, names in _HOSTS.items(): - for name in names: - text = (_HOOKS / host / name).read_text(encoding="utf-8") - if "not configured" not in text: - continue - assert "isConfigured(config)" in text, f"{host}/{name} 没用共享守卫" - assert "!config.token" not in text, f"{host}/{name} 自己拼了 token 判断" - - -def test_scratch_dir_prefers_the_hook_directory() -> None: - """日志/增量状态默认写 hooks 目录,不可写才退到临时目录。""" - text = _LIB.read_text(encoding="utf-8") - assert "for (const dir of [HOOK_DIR," in text - assert "${scratchDir}/octop-hook.log" in text - - -def test_falling_back_to_tmp_is_announced() -> None: - """DSH 的 workspace-write 沙盒里 $HOME 只读,回退必须可见而不是静默。""" - text = _LIB.read_text(encoding="utf-8") - assert "if (scratchDir !== HOOK_DIR)" in text - assert "不可写,日志与增量状态改写到" in text - - -def test_claude_code_capture_defers_to_a_detached_worker() -> None: - """Stop 时本轮还没落盘:后台脱离 + 轮询到「最后一条 user 之后有 assistant」。""" - text = (_HOOKS / "claude-code" / "stop-capture.mjs").read_text(encoding="utf-8") - - assert "OCTOP_CAPTURE_DELAY_MS" in text and "const IS_WORKER" in text - assert 'spawn(process.execPath, [fileURLToPath(import.meta.url), "--worker"]' in text - assert "detached: true" in text and "worker.unref()" in text - assert "OCTOP_HOOK_PAYLOAD_B64" in text - assert "turnLanded" in text - assert '"cc:"' in text or 'sessionPrefix: "cc:"' in text - - -def test_kiro_and_dsh_capture_are_self_contained() -> None: - """Kiro 只认 messages.jsonl;DSH 只认 session*.jsonl.zstd 且自带解压。""" - kiro = (_HOOKS / "kiro" / "stop-capture.mjs").read_text(encoding="utf-8") - dsh = (_HOOKS / "dsh" / "stop-capture.mjs").read_text(encoding="utf-8") - - assert "messages.jsonl" in kiro and '"kiro:"' in kiro - assert "/.kiro/sessions" in kiro - assert "decompressZstdContainer" in dsh and '"dsh:"' in dsh - assert "/.dsh/sessions" in dsh and "session.*\\.jsonl\\.zstd" in dsh - - -def test_dsh_recall_answers_in_the_bridge_dialect() -> None: - """DSH 桥只从 hookSpecificOutput.additionalContext 取上下文:纯 stdout 会被丢弃。""" - text = (_HOOKS / "dsh" / "user-prompt-recall.mjs").read_text(encoding="utf-8") - - assert "hookSpecificOutput" in text - assert 'hookEventName: "UserPromptSubmit"' in text - assert "additionalContext" in text - - -def test_dsh_capture_probes_the_endpoint_before_sending_turns() -> None: - """能分开角色就按轮上报(content + assistant),否则退回服务端解析的 transcript 模式。""" - text = (_HOOKS / "dsh" / "stop-capture.mjs").read_text(encoding="utf-8") - - assert "supportsAssistant" in text and "captureTurns" in text - assert "OCTOP_CAPTURE_MODE" in text and "OCTOP_CAPTURE_MAX_TURNS" in text - assert 'kind && kind !== "user"' in text # 插件注入/系统提醒不算用户提问 - assert 'source: "dsh"' in text - - -# ── 用户级 token 的真实行为(直接 import 共享模块跑一遍,而不是只读文本) ────────── - -_PROBE = """ -import { authHeaders, isConfigured, loadConfig } from __LIB__; -process.env.OCTOP_MEMORY_MCP_URL = "https://memory.example.com/mcp/memory/"; -process.env.OCTOP_MEMORY_MCP_TOKEN = "omcp_env_token"; -process.env.OCTOP_AGENT_ID = "expert-1"; -process.env.OCTOP_USER_ID = "should-be-ignored"; -const config = loadConfig(); -const withToken = (token, tokenHeader) => ({ ...config, token, tokenHeader }); -process.stdout.write(JSON.stringify({ - config, - bearer: authHeaders(withToken("omcp_abc123", "Authorization")), - custom: authHeaders(withToken("shared-token", "X-Octop-Memory-Token")), - configured: isConfigured(config), - tokenlessConfigured: isConfigured(withToken("", "Authorization")), -})); -""" - - -@pytest.fixture(scope="module") -def probe() -> dict[str, object]: - """在一个 node 进程里跑 loadConfig + authHeaders,返回真实结果。""" - script = _PROBE.replace("__LIB__", json.dumps(_LIB.as_uri())) - result = subprocess.run( - [_NODE or "node", "--input-type=module", "-e", script], - capture_output=True, - text=True, - timeout=60, - ) - assert result.returncode == 0, result.stderr - parsed: dict[str, object] = json.loads(result.stdout) - return parsed - - -@node_required -def test_config_no_longer_reads_an_identity(probe: dict[str, object]) -> None: - """config 里没有 userId / agentId:即使设了 OCTOP_USER_ID、OCTOP_AGENT_ID 也不读。""" - config = probe["config"] - - assert isinstance(config, dict) - assert "userId" not in config - assert "agentId" not in config - assert config["endpoint"] == "https://memory.example.com/mcp/memory/" - assert config["token"] == "omcp_env_token" - - -@node_required -def test_auth_headers_send_only_the_token(probe: dict[str, object]) -> None: - """Authorization 带 Bearer ;不再有任何身份头(专家/用户都不发)。""" - headers = probe["bearer"] - - assert isinstance(headers, dict) - assert headers["Authorization"] == "Bearer omcp_abc123" - assert not [name for name in headers if name.lower() in ("x-octop-agent-id", "x-octop-user-id")] - - -@node_required -def test_a_custom_token_header_is_written_verbatim(probe: dict[str, object]) -> None: - """tokenHeader 可改名:非 Authorization 时原样写该头(不加 Bearer 前缀)。""" - headers = probe["custom"] - - assert isinstance(headers, dict) - assert headers["X-Octop-Memory-Token"] == "shared-token" - assert "Authorization" not in headers - - -@node_required -def test_a_tokenless_config_is_not_configured(probe: dict[str, object]) -> None: - """没配 token 时 isConfigured 为 false:入口脚本在本轮直接跳过,不会发请求。""" - assert probe["configured"] is True - assert probe["tokenlessConfigured"] is False diff --git a/tests/unit/agents/test_memory_mcp.py b/tests/unit/agents/test_memory_mcp.py index 5c260bd661..6399beba93 100644 --- a/tests/unit/agents/test_memory_mcp.py +++ b/tests/unit/agents/test_memory_mcp.py @@ -710,35 +710,6 @@ def test_capture_uses_the_token_name_as_source_host(bind_agent, bind_user, monke # ─── capture: terminal transcript uploads ───────────────────────────────── -def _claude_transcript() -> str: - import json - - return ( - json.dumps( - { - "type": "user", - "uuid": "u1", - "sessionId": "s1", - "timestamp": "2026-09-22T05:00:00Z", - "message": {"role": "user", "content": "先看看 CLI 的整体结构,再决定怎么改"}, - } - ) - + "\n" - + json.dumps( - { - "type": "assistant", - "uuid": "a1", - "sessionId": "s1", - "timestamp": "2026-09-22T05:00:05Z", - "message": { - "role": "assistant", - "content": [{"type": "text", "text": "好的,我先把相关的文件读一遍再动手"}], - }, - } - ) - ) - - def _real_memory(tmp_path): from octop_memory.core import Memory @@ -749,87 +720,6 @@ def _real_memory(tmp_path): ) -def test_capture_transcript_parses_typed_events(bind_agent, bind_user, monkeypatch, tmp_path): - memory = _real_memory(tmp_path) - monkeypatch.setattr(mm, "_open_memory", lambda server, agent_id: memory) - monkeypatch.setattr(mm, "_archive_uploaded_session", lambda *a, **k: None) - monkeypatch.setattr(mm, "_schedule_extract", lambda *a, **k: False) - mcp = mm.build_memory_mcp(_server()) - - result = _call_tool( - mcp, - "memory_capture", - content=_claude_transcript(), - source="claude-code", - session_id="cc:s1", - ) - - assert result["imported"] == 2 - events = list(memory.list_raw(session_id="cc:s1", limit=10)) - assert {event.event_type for event in events} == {"user_message", "assistant_message"} - - -def test_capture_gzip_transcript_is_decoded(bind_agent, bind_user, monkeypatch, tmp_path): - import base64 - import gzip - - memory = _real_memory(tmp_path) - monkeypatch.setattr(mm, "_open_memory", lambda server, agent_id: memory) - monkeypatch.setattr(mm, "_archive_uploaded_session", lambda *a, **k: None) - monkeypatch.setattr(mm, "_schedule_extract", lambda *a, **k: False) - mcp = mm.build_memory_mcp(_server()) - - payload = base64.b64encode(gzip.compress(_claude_transcript().encode())).decode() - result = _call_tool( - mcp, - "memory_capture", - content=payload, - source="claude-code", - session_id="cc:s2", - ) - assert result["imported"] == 2 - - -def test_capture_transcript_redacts_secrets(bind_agent, bind_user, monkeypatch, tmp_path): - memory = _real_memory(tmp_path) - monkeypatch.setattr(mm, "_open_memory", lambda server, agent_id: memory) - monkeypatch.setattr(mm, "_archive_uploaded_session", lambda *a, **k: None) - monkeypatch.setattr(mm, "_schedule_extract", lambda *a, **k: False) - mcp = mm.build_memory_mcp(_server()) - - secret = "sk-abcdefgh12345678" - transcript = _claude_transcript().replace( - "先看看 CLI 的整体结构,再决定怎么改", f"先看看 {secret} 的整体结构,再决定怎么改" - ) - _call_tool( - mcp, - "memory_capture", - content=transcript, - source="claude-code", - session_id="cc:s3", - ) - events = list(memory.list_raw(session_id="cc:s3", limit=10)) - assert all(secret not in event.content for event in events) - - -async def test_setup_script_never_embeds_a_token(monkeypatch, tmp_path): - monkeypatch.setenv("OCTOP_HOME", str(tmp_path)) - monkeypatch.setenv("OCTOP_MEMORY_MCP_TOKEN", "omcp_instance_secret") - app = mm._InstallerApp(_server()) - scope = _http_scope({"host": "octop.example.com"}) - scope["method"] = "GET" - status, bodies = await _capture_response(app, scope) - assert status == 200 - body = b"".join(part.get("body", b"") for part in bodies).decode() - assert "omcp_instance_secret" not in body - - -def test_powershell_selection(): - assert mm._wants_powershell({"os": "windows"}) is True - assert mm._wants_powershell({"shell": "pwsh"}) is True - assert mm._wants_powershell({}) is False - - # ─── save: MemoryService application layer ──────────────────────────────── diff --git a/tests/unit/agents/test_memory_session_archive.py b/tests/unit/agents/test_memory_session_archive.py deleted file mode 100644 index 6a6f415f6e..0000000000 --- a/tests/unit/agents/test_memory_session_archive.py +++ /dev/null @@ -1,137 +0,0 @@ -"""The raw-session markdown archive: L0 keeps messages, the archive keeps everything.""" - -from __future__ import annotations - -import json - -from octop.infra.agents.memory_session_archive import ( - _MAX_BODY_CHARS, - append_session, - archive_path, -) - - -def _jsonl(records: list[dict]) -> str: - return "\n".join(json.dumps(record, ensure_ascii=False) for record in records) - - -def _user(uuid: str, text: str = "跑一下测试", session: str = "s1") -> dict: - return { - "type": "user", - "uuid": uuid, - "sessionId": session, - "timestamp": "2026-09-29T02:00:00Z", - "message": {"role": "user", "content": text}, - } - - -def _assistant(uuid: str, blocks: list[dict], session: str = "s1") -> dict: - return { - "type": "assistant", - "uuid": uuid, - "sessionId": session, - "timestamp": "2026-09-29T02:00:05Z", - "message": {"role": "assistant", "content": blocks}, - } - - -def test_archive_renders_every_block_not_just_messages(tmp_path): - """L0 会丢 thinking / tool 事件,归档必须都留着。""" - text = _jsonl( - [ - _user("u1"), - _assistant( - "a1", - [ - {"type": "thinking", "thinking": "先想一下"}, - {"type": "text", "text": "我跑一下"}, - { - "type": "tool_use", - "id": "t1", - "name": "Bash", - "input": {"command": "pytest -q"}, - }, - { - "type": "tool_result", - "tool_use_id": "t1", - "content": "1 failed", - }, - ], - ), - ] - ) - - path = append_session(tmp_path, session_id="cc:s1", text=text, host="claude-code", user="alice") - - assert path == archive_path(tmp_path, "cc:s1") - assert path.name == "cc_s1.md" # 文件名净化 - body = path.read_text(encoding="utf-8") - assert "# claude-code session cc:s1" in body - assert "- user: alice" in body - assert "跑一下测试" in body - assert "*(thinking)*" in body and "先想一下" in body - assert "**tool_call · Bash**" in body and '"command": "pytest -q"' in body - assert "**tool_result" in body and "1 failed" in body - assert body.count("") == 1 - assert after_delta.count("") == 1 - assert after_delta.count("# claude-code session cc:s2") == 1 # 头只写一次 - - -def test_archive_keeps_long_tool_output_beyond_the_l0_cap(tmp_path): - """归档给人看:单条上限比 L0 的 4000 宽松得多。""" - body_text = "line\n" * 2000 # ~10KB,超过 L0 的 4000 - text = _jsonl( - [ - _user("u1"), - _assistant( - "a1", - [{"type": "tool_result", "tool_use_id": "t1", "content": body_text}], - ), - ] - ) - - path = append_session(tmp_path, session_id="cc:s3", text=text, host="claude-code") - body = path.read_text(encoding="utf-8") - - assert body.count("line") >= 1500 # 远多于 4000 字符 - assert len(body) <= _MAX_BODY_CHARS * 2 # 仍然有界 - - -def test_archive_skips_non_records_and_keeps_thinking_optional(tmp_path): - """非 user/assistant 记录(mode / cost-state / attachment)不进归档。""" - text = _jsonl( - [ - {"type": "mode", "mode": "acceptEdits"}, - _user("u1", text="问了"), - {"type": "cost-state", "total": 1}, - ] - ) - - path = append_session(tmp_path, session_id="cc:s4", text=text, host="claude-code") - body = path.read_text(encoding="utf-8") - - assert "问了" in body - assert "acceptEdits" not in body - assert body.count("