Skip to content

fix(loop): guard empty vector-memory block against set -e crash - #11

Open
allenter wants to merge 3 commits into
MaxMiksa:mainfrom
allenter:pr/fix-loop-first-run-crash
Open

fix(loop): guard empty vector-memory block against set -e crash#11
allenter wants to merge 3 commits into
MaxMiksa:mainfrom
allenter:pr/fix-loop-first-run-crash

Conversation

@allenter

@allenter allenter commented Aug 2, 2026

Copy link
Copy Markdown

PR: fix(loop): 修复首次运行时空向量记忆导致 set -e 崩溃

类型:bug fix | 影响:任何全新部署首次运行必挂 | 1 行改动

问题描述

全新 clone 后首次运行 make start,循环在 Cycle #1 启动瞬间静默退出(exit 1),无任何 cycle 结果写入日志。

复现步骤

  1. git clone 仓库(无 memories/vault/
  2. make start
  3. 观察到:Cycle #1 [START] 后进程立刻以 exit 1 退出,logs/ 无 cycle 日志

根因

scripts/core/auto-loop.sh 主循环构建 FULL_PROMPT 时:

FULL_PROMPT="$PROMPT
...
$([ -n "$MEMORY_BLOCK" ] && printf '\n\n---\n## Highly-relevant past memory ... %s\n' "$MEMORY_BLOCK")
..."

首次运行时 memories/vault/ 为空,vault_retrieve_prompt 返回空字符串 → MEMORY_BLOCK=""。此时 [ -n "$MEMORY_BLOCK" ] 为假,&& 短路,命令替换 $(...) 返回 exit code 1。而脚本顶部启用了 set -euo pipefail,非零的命令替换导致整个脚本以 exit 1 终止。

修复

-$([ -n "$MEMORY_BLOCK" ] && printf '\n\n---\n## Highly-relevant past memory (from vector vault; use as context, trust current consensus)\n\n%s\n' "$MEMORY_BLOCK")
+$([ -n "$MEMORY_BLOCK" ] && printf '\n\n---\n## Highly-relevant past memory (from vector vault; use as context, trust current consensus)\n\n%s\n' "$MEMORY_BLOCK" || true)

|| true 保证空块时命令替换返回 0,非空块时行为完全不变。

验证

  • MEMORY_BLOCK:脚本正常通过 prompt 构建,进入循环
  • 非空 MEMORY_BLOCK:记忆块正常注入 prompt,格式不变
  • 实机:修复后循环正常运行完 3 个 cycle(首次运行即可自举)

变更文件

  • scripts/core/auto-loop.sh(1 行)

测试建议


Co-Authored-By: Claude noreply@anthropic.com

allenter and others added 3 commits August 2, 2026 08:50
Layer a zero-dependency, pure-Python vector memory store (memories/vault/)
on top of the existing single-file consensus baton:

- scripts/core/memory_vault.py: chunk consensus + docs, index into
  memories/vault/index.json (TF-IDF char n-grams + cosine similarity,
  no external deps), and semantic search with -top-k / min-score.
  Backend swappable: replace _embed_chunk() to plug in ChromaDB or a
  model embedding.
- auto-loop.sh: each cycle auto-retrieves the top relevant historical
  blocks (keyed off Next Action) and injects them into the prompt as
  '## Highly-relevant past memory'; after a successful/soft-timeout
  cycle it indexes the latest consensus + docs into the vault.
- .gitignore: ignore memories/vault/* runtime data.
- Docs updated: README(EN/ZH), CLAUDE.md, PROMPT.md, INDEX.md.

consensus.md remains the authoritative running-state baton; the vault
adds long-term recall of decisions/context that consensus collapses.
Add a Memory Vault panel to the control deck:

- server.py: new GET /api/vault endpoint. Reads memories/vault/index.json
  directly (no subprocess) and returns stats (chunk/source/term counts,
  size, last-indexed), per-source breakdown, latest entries, plus optional
  in-process cosine semantic search via ?q=.
- app.js: fetchVault() renderer with stat cards, a canvas bar chart of
  chunks per source, latest-entry list, and a live semantic-search box
  (Enter or button) showing scored hits with source tags.
- index.html: Memory Vault section between Consensus and Recent Log.
- styles.css: dark-theme styles for stats, canvas, entries, tags.

Pure stdlib on the server side; canvas drawn client-side.
Fresh clones crash on the very first cycle: with an empty vector vault
MEMORY_BLOCK is "", so the `$([ -n "$MEMORY_BLOCK" ] && printf ...)`
command substitution returns 1 and set -euo pipefail kills the loop
right after Cycle MaxMiksa#1 [START], before any cycle log is written.

Append `|| true` so the substitution is a no-op when the block is empty
and behaves exactly as before otherwise. Verified with empty and
non-empty blocks, and a fresh-clone run completing 3 cycles.

Co-Authored-By: Claude <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant