create-egregore 0.19.33 → 0.20.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (250) hide show
  1. package/assets/egregore-launcher.js +141 -14
  2. package/bin/cli.js +19 -3
  3. package/lib/connect-runtime.js +4 -2
  4. package/lib/local.js +15 -1
  5. package/lib/prime-runtime.js +208 -0
  6. package/lib/setup.js +66 -5
  7. package/package.json +4 -3
  8. package/runtime/codex/.codex/config.toml +3 -0
  9. package/runtime/codex/.codex/skills/archive/SKILL.md +1 -1
  10. package/runtime/codex/.codex/skills/branch/SKILL.md +1 -1
  11. package/runtime/codex/.codex/skills/checkup/SKILL.md +1 -1
  12. package/runtime/codex/.codex/skills/commit/SKILL.md +1 -1
  13. package/runtime/codex/.codex/skills/contribute/SKILL.md +1 -1
  14. package/runtime/codex/.codex/skills/delete-user/SKILL.md +1 -1
  15. package/runtime/codex/.codex/skills/ingest/SKILL.md +1 -1
  16. package/runtime/codex/.codex/skills/ingest-corpus/SKILL.md +29 -0
  17. package/runtime/codex/.codex/skills/ingest-notion/SKILL.md +29 -0
  18. package/runtime/codex/.codex/skills/ingest-user-interview/SKILL.md +1 -1
  19. package/runtime/codex/.codex/skills/issue/SKILL.md +1 -1
  20. package/runtime/codex/.codex/skills/me/SKILL.md +1 -1
  21. package/runtime/codex/.codex/skills/meeting/SKILL.md +1 -1
  22. package/runtime/codex/.codex/skills/note/SKILL.md +1 -1
  23. package/runtime/codex/.codex/skills/notion-connect/SKILL.md +29 -0
  24. package/runtime/codex/.codex/skills/onboarding/SKILL.md +1 -1
  25. package/runtime/codex/.codex/skills/pr/SKILL.md +1 -1
  26. package/runtime/codex/.codex/skills/pull/SKILL.md +1 -1
  27. package/runtime/codex/.codex/skills/push/SKILL.md +1 -1
  28. package/runtime/codex/.codex/skills/reflect/SKILL.md +1 -1
  29. package/runtime/codex/.codex/skills/review-pr/SKILL.md +1 -1
  30. package/runtime/codex/.codex/skills/save/SKILL.md +3 -1
  31. package/runtime/codex/.codex/skills/slack-connect/SKILL.md +1 -1
  32. package/runtime/codex/.codex/skills/summon/SKILL.md +1 -1
  33. package/runtime/codex/.codex/skills/sync-repos/SKILL.md +1 -1
  34. package/runtime/codex/.codex/skills/teams-connect/SKILL.md +29 -0
  35. package/runtime/codex/.codex/skills/telegram-connect/SKILL.md +1 -1
  36. package/runtime/codex/.codex/skills/telemetry/SKILL.md +1 -1
  37. package/runtime/codex/.codex/skills/test/SKILL.md +1 -1
  38. package/runtime/codex/.codex/skills/tutorial/SKILL.md +1 -1
  39. package/runtime/codex/.codex/skills/update/SKILL.md +1 -1
  40. package/runtime/codex/.codex/spec-manifest.json +5 -5
  41. package/runtime/codex/AGENTS.md +41 -35
  42. package/runtime/codex/bin/activity-data.sh +4 -8
  43. package/runtime/codex/bin/agent.sh +17 -5
  44. package/runtime/codex/bin/attendant.sh +13 -0
  45. package/runtime/codex/bin/autosave.sh +27 -1
  46. package/runtime/codex/bin/codex-render-spec.mjs +33 -32
  47. package/runtime/codex/bin/codex-session-start.sh +3 -1
  48. package/runtime/codex/bin/codex-sync-skills.sh +6 -1
  49. package/runtime/codex/bin/graph-batch.sh +25 -6
  50. package/runtime/codex/bin/graph-op.sh +5 -4
  51. package/runtime/codex/bin/graph.sh +31 -5
  52. package/runtime/codex/bin/handoff-pr-backfill.sh +1 -1
  53. package/runtime/codex/bin/handoff-run.sh +8 -3
  54. package/runtime/codex/bin/handoff-save-egregore.sh +19 -7
  55. package/runtime/codex/bin/ingest.py +341 -10
  56. package/runtime/codex/bin/lib/context.sh +26 -1
  57. package/runtime/codex/bin/lib/git-message.sh +37 -0
  58. package/runtime/codex/bin/lib/git-sync.sh +10 -2
  59. package/runtime/codex/bin/lib/greeting.sh +4 -1
  60. package/runtime/codex/bin/lib/worktree-links.sh +25 -0
  61. package/runtime/codex/bin/session-autosave.sh +7 -1
  62. package/runtime/codex/bin/settings.sh +1 -1
  63. package/runtime/codex/manifest.json +10 -1
  64. package/runtime/pi/.codex/config.toml +3 -0
  65. package/runtime/pi/.codex/skills/archive/SKILL.md +1 -1
  66. package/runtime/pi/.codex/skills/branch/SKILL.md +1 -1
  67. package/runtime/pi/.codex/skills/checkup/SKILL.md +1 -1
  68. package/runtime/pi/.codex/skills/commit/SKILL.md +1 -1
  69. package/runtime/pi/.codex/skills/contribute/SKILL.md +1 -1
  70. package/runtime/pi/.codex/skills/delete-user/SKILL.md +1 -1
  71. package/runtime/pi/.codex/skills/ingest/SKILL.md +1 -1
  72. package/runtime/pi/.codex/skills/ingest-corpus/SKILL.md +29 -0
  73. package/runtime/pi/.codex/skills/ingest-notion/SKILL.md +29 -0
  74. package/runtime/pi/.codex/skills/ingest-user-interview/SKILL.md +1 -1
  75. package/runtime/pi/.codex/skills/issue/SKILL.md +1 -1
  76. package/runtime/pi/.codex/skills/me/SKILL.md +1 -1
  77. package/runtime/pi/.codex/skills/meeting/SKILL.md +1 -1
  78. package/runtime/pi/.codex/skills/note/SKILL.md +1 -1
  79. package/runtime/pi/.codex/skills/notion-connect/SKILL.md +29 -0
  80. package/runtime/pi/.codex/skills/onboarding/SKILL.md +1 -1
  81. package/runtime/pi/.codex/skills/pr/SKILL.md +1 -1
  82. package/runtime/pi/.codex/skills/pull/SKILL.md +1 -1
  83. package/runtime/pi/.codex/skills/push/SKILL.md +1 -1
  84. package/runtime/pi/.codex/skills/reflect/SKILL.md +1 -1
  85. package/runtime/pi/.codex/skills/review-pr/SKILL.md +1 -1
  86. package/runtime/pi/.codex/skills/save/SKILL.md +3 -1
  87. package/runtime/pi/.codex/skills/slack-connect/SKILL.md +1 -1
  88. package/runtime/pi/.codex/skills/summon/SKILL.md +1 -1
  89. package/runtime/pi/.codex/skills/sync-repos/SKILL.md +1 -1
  90. package/runtime/pi/.codex/skills/teams-connect/SKILL.md +29 -0
  91. package/runtime/pi/.codex/skills/telegram-connect/SKILL.md +1 -1
  92. package/runtime/pi/.codex/skills/telemetry/SKILL.md +1 -1
  93. package/runtime/pi/.codex/skills/test/SKILL.md +1 -1
  94. package/runtime/pi/.codex/skills/tutorial/SKILL.md +1 -1
  95. package/runtime/pi/.codex/skills/update/SKILL.md +1 -1
  96. package/runtime/pi/.codex/spec-manifest.json +5 -5
  97. package/runtime/pi/.pi/APPEND_SYSTEM.md +57 -33
  98. package/runtime/pi/.pi/extensions/egregore.ts +17 -6
  99. package/runtime/pi/.pi/spec-manifest.json +2 -2
  100. package/runtime/pi/AGENTS.md +41 -35
  101. package/runtime/pi/bin/activity-data.sh +4 -8
  102. package/runtime/pi/bin/agent.sh +17 -5
  103. package/runtime/pi/bin/attendant.sh +13 -0
  104. package/runtime/pi/bin/autosave.sh +27 -1
  105. package/runtime/pi/bin/codex-render-spec.mjs +33 -32
  106. package/runtime/pi/bin/codex-session-start.sh +3 -1
  107. package/runtime/pi/bin/codex-sync-skills.sh +6 -1
  108. package/runtime/pi/bin/graph-batch.sh +25 -6
  109. package/runtime/pi/bin/graph-op.sh +5 -4
  110. package/runtime/pi/bin/graph.sh +31 -5
  111. package/runtime/pi/bin/handoff-pr-backfill.sh +1 -1
  112. package/runtime/pi/bin/handoff-run.sh +8 -3
  113. package/runtime/pi/bin/handoff-save-egregore.sh +19 -7
  114. package/runtime/pi/bin/ingest.py +341 -10
  115. package/runtime/pi/bin/lib/context.sh +26 -1
  116. package/runtime/pi/bin/lib/git-message.sh +37 -0
  117. package/runtime/pi/bin/lib/git-sync.sh +10 -2
  118. package/runtime/pi/bin/lib/greeting.sh +4 -1
  119. package/runtime/pi/bin/lib/worktree-links.sh +25 -0
  120. package/runtime/pi/bin/pi-branch-policy.mjs +138 -1
  121. package/runtime/pi/bin/pi-product-workflows.mjs +35 -7
  122. package/runtime/pi/bin/pi-render-spec.mjs +52 -26
  123. package/runtime/pi/bin/session-autosave.sh +7 -1
  124. package/runtime/pi/bin/settings.sh +1 -1
  125. package/runtime/pi/manifest.json +10 -1
  126. package/runtime/prime/.claude/context/notification-consent.md +66 -0
  127. package/runtime/prime/.codex/config.toml +20 -0
  128. package/runtime/prime/.codex/hooks/branch-guard.js +461 -0
  129. package/runtime/prime/.codex/hooks/search-hint.js +67 -0
  130. package/runtime/prime/.codex/hooks.json +30 -0
  131. package/runtime/prime/.codex/skills/activity/SKILL.md +82 -0
  132. package/runtime/prime/.codex/skills/announce/SKILL.md +67 -0
  133. package/runtime/prime/.codex/skills/archive/SKILL.md +45 -0
  134. package/runtime/prime/.codex/skills/branch/SKILL.md +29 -0
  135. package/runtime/prime/.codex/skills/checkup/SKILL.md +45 -0
  136. package/runtime/prime/.codex/skills/commit/SKILL.md +29 -0
  137. package/runtime/prime/.codex/skills/contribute/SKILL.md +29 -0
  138. package/runtime/prime/.codex/skills/create-skill/SKILL.md +29 -0
  139. package/runtime/prime/.codex/skills/dark-mode/SKILL.md +29 -0
  140. package/runtime/prime/.codex/skills/dashboard/SKILL.md +59 -0
  141. package/runtime/prime/.codex/skills/deep-reflect/SKILL.md +109 -0
  142. package/runtime/prime/.codex/skills/delete-user/SKILL.md +29 -0
  143. package/runtime/prime/.codex/skills/emissary/SKILL.md +45 -0
  144. package/runtime/prime/.codex/skills/handoff/SKILL.md +166 -0
  145. package/runtime/prime/.codex/skills/harvest/QUESTION_PALETTE.md +76 -0
  146. package/runtime/prime/.codex/skills/harvest/SKILL.md +131 -0
  147. package/runtime/prime/.codex/skills/ingest/SKILL.md +29 -0
  148. package/runtime/prime/.codex/skills/ingest-corpus/SKILL.md +29 -0
  149. package/runtime/prime/.codex/skills/ingest-notion/SKILL.md +29 -0
  150. package/runtime/prime/.codex/skills/ingest-user-interview/SKILL.md +45 -0
  151. package/runtime/prime/.codex/skills/invite/SKILL.md +44 -0
  152. package/runtime/prime/.codex/skills/issue/SKILL.md +45 -0
  153. package/runtime/prime/.codex/skills/me/SKILL.md +29 -0
  154. package/runtime/prime/.codex/skills/meeting/SKILL.md +45 -0
  155. package/runtime/prime/.codex/skills/note/SKILL.md +29 -0
  156. package/runtime/prime/.codex/skills/notion-connect/SKILL.md +29 -0
  157. package/runtime/prime/.codex/skills/onboarding/SKILL.md +45 -0
  158. package/runtime/prime/.codex/skills/pr/SKILL.md +29 -0
  159. package/runtime/prime/.codex/skills/pull/SKILL.md +29 -0
  160. package/runtime/prime/.codex/skills/push/SKILL.md +29 -0
  161. package/runtime/prime/.codex/skills/quest/SKILL.md +80 -0
  162. package/runtime/prime/.codex/skills/reflect/SKILL.md +45 -0
  163. package/runtime/prime/.codex/skills/review-pr/SKILL.md +45 -0
  164. package/runtime/prime/.codex/skills/save/SKILL.md +127 -0
  165. package/runtime/prime/.codex/skills/slack-connect/SKILL.md +29 -0
  166. package/runtime/prime/.codex/skills/summon/SKILL.md +45 -0
  167. package/runtime/prime/.codex/skills/sync-repos/SKILL.md +29 -0
  168. package/runtime/prime/.codex/skills/teams-connect/SKILL.md +29 -0
  169. package/runtime/prime/.codex/skills/telegram-connect/SKILL.md +29 -0
  170. package/runtime/prime/.codex/skills/telemetry/SKILL.md +29 -0
  171. package/runtime/prime/.codex/skills/test/SKILL.md +45 -0
  172. package/runtime/prime/.codex/skills/the-spiral/SKILL.md +84 -0
  173. package/runtime/prime/.codex/skills/tui-design/SKILL.md +45 -0
  174. package/runtime/prime/.codex/skills/tutorial/SKILL.md +45 -0
  175. package/runtime/prime/.codex/skills/update/SKILL.md +29 -0
  176. package/runtime/prime/.codex/skills/view/SKILL.md +156 -0
  177. package/runtime/prime/.codex/skills/view/agents/openai.yaml +4 -0
  178. package/runtime/prime/.codex/skills/view/assets/compose-scaffold.html +221 -0
  179. package/runtime/prime/.codex/skills/visual-explain/SKILL.md +45 -0
  180. package/runtime/prime/.codex/skills/wrap/SKILL.md +58 -0
  181. package/runtime/prime/.codex/spec-manifest.json +132 -0
  182. package/runtime/prime/.prime/agent/APPEND_SYSTEM.md +416 -0
  183. package/runtime/prime/.prime/agent/extensions/egregore.ts +460 -0
  184. package/runtime/prime/.prime/agent/settings.json +4 -0
  185. package/runtime/prime/.prime/agent/skills/egregore/SKILL.md +34 -0
  186. package/runtime/prime/.prime/agent/skills/egregore/pyproject.toml +13 -0
  187. package/runtime/prime/.prime/agent/skills/egregore/src/egregore/__init__.py +156 -0
  188. package/runtime/prime/.prime/agent/spec-manifest.json +12 -0
  189. package/runtime/prime/AGENTS.md +488 -0
  190. package/runtime/prime/bin/activity-data.sh +238 -0
  191. package/runtime/prime/bin/agent.sh +787 -0
  192. package/runtime/prime/bin/attendant.sh +707 -0
  193. package/runtime/prime/bin/autosave.sh +112 -0
  194. package/runtime/prime/bin/capture-reconcile.sh +69 -0
  195. package/runtime/prime/bin/capture-run.sh +341 -0
  196. package/runtime/prime/bin/codex-install-skills.sh +78 -0
  197. package/runtime/prime/bin/codex-render-spec.mjs +435 -0
  198. package/runtime/prime/bin/codex-session-start.sh +265 -0
  199. package/runtime/prime/bin/codex-skill-render.mjs +347 -0
  200. package/runtime/prime/bin/codex-sync-skills.sh +237 -0
  201. package/runtime/prime/bin/connect-refresh.sh +33 -0
  202. package/runtime/prime/bin/dashboard-data.sh +178 -0
  203. package/runtime/prime/bin/graph-batch.sh +79 -0
  204. package/runtime/prime/bin/graph-op.sh +1102 -0
  205. package/runtime/prime/bin/graph-wal.sh +229 -0
  206. package/runtime/prime/bin/graph.sh +223 -0
  207. package/runtime/prime/bin/handoff-pr-backfill.sh +86 -0
  208. package/runtime/prime/bin/handoff-run.sh +676 -0
  209. package/runtime/prime/bin/handoff-save-egregore.sh +224 -0
  210. package/runtime/prime/bin/index-handoff.sh +322 -0
  211. package/runtime/prime/bin/ingest-graph.sh +80 -0
  212. package/runtime/prime/bin/ingest-surface.html +495 -0
  213. package/runtime/prime/bin/ingest.py +1011 -0
  214. package/runtime/prime/bin/ingest.sh +279 -0
  215. package/runtime/prime/bin/ingest_graph.py +856 -0
  216. package/runtime/prime/bin/ingest_surface.py +544 -0
  217. package/runtime/prime/bin/lib/artifact-id.sh +46 -0
  218. package/runtime/prime/bin/lib/config.sh +116 -0
  219. package/runtime/prime/bin/lib/context.sh +621 -0
  220. package/runtime/prime/bin/lib/dashboard-artifact.sh +242 -0
  221. package/runtime/prime/bin/lib/ensure-develop.sh +183 -0
  222. package/runtime/prime/bin/lib/git-message.sh +37 -0
  223. package/runtime/prime/bin/lib/git-sync.sh +414 -0
  224. package/runtime/prime/bin/lib/greeting.sh +519 -0
  225. package/runtime/prime/bin/lib/hash.sh +13 -0
  226. package/runtime/prime/bin/lib/identity.sh +213 -0
  227. package/runtime/prime/bin/lib/metrics.sh +374 -0
  228. package/runtime/prime/bin/lib/noncode.sh +31 -0
  229. package/runtime/prime/bin/lib/permissions.sh +106 -0
  230. package/runtime/prime/bin/lib/time.sh +56 -0
  231. package/runtime/prime/bin/lib/worktree-links.sh +79 -0
  232. package/runtime/prime/bin/notify.sh +636 -0
  233. package/runtime/prime/bin/person.py +630 -0
  234. package/runtime/prime/bin/person.sh +448 -0
  235. package/runtime/prime/bin/pi-branch-policy.mjs +312 -0
  236. package/runtime/prime/bin/pi-product-workflows.mjs +394 -0
  237. package/runtime/prime/bin/prime-render-spec.mjs +147 -0
  238. package/runtime/prime/bin/prime-session-start.sh +24 -0
  239. package/runtime/prime/bin/publish-artifact.sh +271 -0
  240. package/runtime/prime/bin/publish-references.sh +144 -0
  241. package/runtime/prime/bin/repo-state.sh +96 -0
  242. package/runtime/prime/bin/search.sh +240 -0
  243. package/runtime/prime/bin/session-autosave.sh +215 -0
  244. package/runtime/prime/bin/session-log.sh +199 -0
  245. package/runtime/prime/bin/settings.sh +304 -0
  246. package/runtime/prime/bin/telemetry.sh +391 -0
  247. package/runtime/prime/bin/worktree-create.sh +128 -0
  248. package/runtime/prime/bin/worktree-remove.sh +50 -0
  249. package/runtime/prime/bin/worktree.sh +198 -0
  250. package/runtime/prime/manifest.json +181 -0
@@ -0,0 +1,787 @@
1
+ #!/usr/bin/env bash
2
+ # Runtime-neutral Egregore agent bridge.
3
+ #
4
+ # This script exposes the Git-backed memory protocol without requiring
5
+ # Claude Code hooks or slash-command skills. Any agent runtime that can run
6
+ # shell commands inside an Egregore checkout can use it to sync memory,
7
+ # create handoffs, ask questions, answer questions, and inspect activity.
8
+ set -euo pipefail
9
+
10
+ SCRIPT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
11
+ MAIN_PROJECT_DIR="$SCRIPT_DIR"
12
+ if [ -f "$SCRIPT_DIR/.git" ]; then
13
+ WT_GITDIR=$(sed 's/^gitdir: //' "$SCRIPT_DIR/.git" 2>/dev/null || true)
14
+ [ -n "$WT_GITDIR" ] && MAIN_PROJECT_DIR=$(cd "$WT_GITDIR/../../.." 2>/dev/null && pwd || echo "$SCRIPT_DIR")
15
+ fi
16
+ if [ -f "$SCRIPT_DIR/bin/lib/worktree-links.sh" ]; then
17
+ source "$SCRIPT_DIR/bin/lib/worktree-links.sh" >/dev/null 2>/dev/null || true
18
+ egregore_link_shared_state "$SCRIPT_DIR" "$MAIN_PROJECT_DIR" >/dev/null 2>/dev/null || true
19
+ fi
20
+ CONFIG="$SCRIPT_DIR/egregore.json"
21
+ MEMORY_DIR="${EGREGORE_MEMORY_DIR:-$SCRIPT_DIR/memory}"
22
+ if [ -f "$SCRIPT_DIR/bin/lib/config.sh" ]; then
23
+ # shellcheck source=bin/lib/config.sh
24
+ source "$SCRIPT_DIR/bin/lib/config.sh"
25
+ fi
26
+ # Shared commit/PR message mechanics (trailers + skeleton body shape).
27
+ # shellcheck source=bin/lib/git-message.sh
28
+ . "$SCRIPT_DIR/bin/lib/git-message.sh" 2>/dev/null || true
29
+ type egregore_commit >/dev/null 2>&1 || egregore_commit() {
30
+ local gd="$1" m="$3"; shift 3; git -C "$gd" commit -m "$m" "$@"
31
+ }
32
+ type egregore_pr_skeleton >/dev/null 2>&1 || egregore_pr_skeleton() {
33
+ printf '## What\n%s\n\n## Why\n%s\n\n## Verification\n%s\n\n%s\n' "$1" "$2" "$3" "$4"
34
+ }
35
+
36
+ usage() {
37
+ cat <<'EOF'
38
+ Usage: agent.sh <command> [options]
39
+
40
+ Runtime-neutral commands:
41
+ protocol Print the portable agent protocol
42
+ sync Pull latest shared memory
43
+ activity [--for <person>] Show recent handoffs and questions
44
+ search "query" [-n N] [--fast|--semantic]
45
+ Hybrid recall over shared memory (qmd)
46
+ people List known people from memory/people
47
+ branch --topic TEXT Create or reuse a task branch/worktree
48
+ save [--message TEXT] [--pr-body TEXT|--pr-body-file PATH]
49
+ Commit and push current repo + memory changes;
50
+ PR body follows .claude/context/pr-format.md
51
+ (auto-generated skeleton if not provided)
52
+ autosave [status|on|off|scope|publish|merge] Personal ambient-save settings + consent
53
+ wrap --topic T --summary TEXT Write a session wrap to memory/wraps
54
+ handoff --from A --to B --topic T [--intent action|feedback|fyi]
55
+ [--body TEXT|--body-file PATH] [--no-push] [--no-publish] [--no-notify] [--json]
56
+ ask --from A --to B --topic T --question TEXT [--no-push]
57
+ [--harvest-id ID --harvest-session-id ID --turn N
58
+ --question-intent TEXT --context-mode blind|disclosed|comparative]
59
+ answer --from A --question PATH --body TEXT [--no-push]
60
+
61
+ The script uses the existing memory/ repository and keeps Claude Code
62
+ behavior unchanged.
63
+ EOF
64
+ }
65
+
66
+ die() {
67
+ echo "agent.sh: $*" >&2
68
+ exit 1
69
+ }
70
+
71
+ resolve_base_branch() {
72
+ local base
73
+ if ! base=$(_get_base_branch); then
74
+ echo "agent.sh: could not resolve the configured base branch" >&2
75
+ return 1
76
+ fi
77
+ echo "$base"
78
+ }
79
+
80
+ slugify() {
81
+ echo "$1" \
82
+ | tr '[:upper:]' '[:lower:]' \
83
+ | sed -E 's/[^a-z0-9]+/-/g' \
84
+ | sed -E 's/^-+|-+$//g' \
85
+ | cut -c1-60
86
+ }
87
+
88
+ # Reject control characters that would break YAML frontmatter when interpolated.
89
+ reject_newlines() {
90
+ case "$2" in
91
+ *$'\n'*|*$'\r'*) die "invalid $1: must not contain newlines" ;;
92
+ esac
93
+ }
94
+
95
+ # Emit a value as a JSON-encoded string (valid YAML), so colons, quotes,
96
+ # and other YAML metacharacters in user-supplied values cannot inject keys.
97
+ yaml_str() {
98
+ jq -n --arg v "$1" '$v'
99
+ }
100
+
101
+ ensure_memory() {
102
+ [ -d "$MEMORY_DIR" ] || die "memory directory not found: $MEMORY_DIR"
103
+ }
104
+
105
+ is_memory_git() {
106
+ git -C "$MEMORY_DIR" rev-parse --is-inside-work-tree >/dev/null 2>&1
107
+ }
108
+
109
+ memory_branch() {
110
+ git -C "$MEMORY_DIR" branch --show-current 2>/dev/null || echo "main"
111
+ }
112
+
113
+ sync_memory() {
114
+ ensure_memory
115
+ if is_memory_git && git -C "$MEMORY_DIR" remote get-url origin >/dev/null 2>&1; then
116
+ local branch
117
+ branch="$(memory_branch)"
118
+ [ -z "$branch" ] && branch="main"
119
+ git -C "$MEMORY_DIR" pull --rebase origin "$branch" --quiet 2>/dev/null || true
120
+ fi
121
+ }
122
+
123
+ save_memory() {
124
+ local message="$1"
125
+ shift
126
+ local no_push="${NO_PUSH:-0}"
127
+
128
+ ensure_memory
129
+ if ! is_memory_git; then
130
+ return 0
131
+ fi
132
+
133
+ git -C "$MEMORY_DIR" add "$@" >/dev/null 2>&1 || true
134
+ if ! git -C "$MEMORY_DIR" diff --cached --quiet 2>/dev/null; then
135
+ git -C "$MEMORY_DIR" commit -m "$message" --quiet 2>/dev/null || true
136
+ fi
137
+
138
+ [ "$no_push" = "1" ] && return 0
139
+ git -C "$MEMORY_DIR" remote get-url origin >/dev/null 2>&1 || return 0
140
+
141
+ local branch
142
+ branch="$(memory_branch)"
143
+ [ -z "$branch" ] && branch="main"
144
+ for _try in 1 2 3; do
145
+ if git -C "$MEMORY_DIR" pull --rebase origin "$branch" --quiet 2>/dev/null \
146
+ && git -C "$MEMORY_DIR" push origin "$branch" --quiet 2>/dev/null; then
147
+ return 0
148
+ fi
149
+ sleep 1
150
+ done
151
+ die "could not push memory after 3 attempts"
152
+ }
153
+
154
+ person_from_state() {
155
+ if [ -f "$SCRIPT_DIR/.egregore-state.json" ]; then
156
+ jq -r '.github_username // .name // empty' "$SCRIPT_DIR/.egregore-state.json" 2>/dev/null || true
157
+ fi
158
+ }
159
+
160
+ current_author() {
161
+ local author
162
+ author="$(person_from_state)"
163
+ [ -n "$author" ] || author="$(git -C "$SCRIPT_DIR" config user.name 2>/dev/null | awk '{print tolower($1)}')"
164
+ [ -n "$author" ] || author="agent"
165
+ echo "$author"
166
+ }
167
+
168
+ worktree_for_branch() {
169
+ local branch="$1"
170
+ git -C "$MAIN_PROJECT_DIR" worktree list --porcelain 2>/dev/null | awk -v ref="refs/heads/$branch" '
171
+ $1 == "worktree" { path = $2 }
172
+ $1 == "branch" && $2 == ref { print path; exit }
173
+ '
174
+ }
175
+
176
+ base_ref() {
177
+ local base
178
+ base="$(resolve_base_branch)" || return 1
179
+ git -C "$MAIN_PROJECT_DIR" fetch origin "$base" --quiet 2>/dev/null || true
180
+ if git -C "$MAIN_PROJECT_DIR" show-ref --verify --quiet "refs/remotes/origin/$base" 2>/dev/null; then
181
+ echo "origin/$base"
182
+ elif git -C "$MAIN_PROJECT_DIR" show-ref --verify --quiet "refs/heads/$base" 2>/dev/null; then
183
+ echo "$base"
184
+ else
185
+ echo "agent.sh: configured base branch '$base' does not exist locally or on origin" >&2
186
+ return 1
187
+ fi
188
+ }
189
+
190
+ cmd_protocol() {
191
+ cat <<'EOF'
192
+ Egregore portable agent protocol
193
+
194
+ 1. Work inside an Egregore checkout with a linked memory/ repository.
195
+ 2. Run `bin/agent.sh sync` before reading shared state.
196
+ 3. Read `memory/people/` for collaborators.
197
+ 4. Use `bin/agent.sh handoff` to leave structured context for another agent.
198
+ 5. Use `bin/agent.sh ask` and `bin/agent.sh answer` for asynchronous questions.
199
+ 6. Run `bin/agent.sh activity --for <person>` to see pending work.
200
+ 7. Run `bin/agent.sh search "<query>"` to recall shared memory (decisions, handoffs, knowledge) before answering questions about past work.
201
+ 8. Run `bin/agent.sh branch --topic "<what you are working on>"` before code changes when you need a task branch/worktree.
202
+ 9. Run `bin/agent.sh save` and `bin/agent.sh wrap` for portable save/wrap flows.
203
+
204
+ This protocol is runtime-neutral. Claude Code may still use .claude hooks and
205
+ skills, while Codex or another agent can call this script directly.
206
+ EOF
207
+ }
208
+
209
+ cmd_people() {
210
+ ensure_memory
211
+ local people_dir="$MEMORY_DIR/people"
212
+ [ -d "$people_dir" ] || return 0
213
+ local f github display
214
+ for f in "$people_dir"/*.md; do
215
+ [ -f "$f" ] || continue
216
+ github="$(basename "$f" .md)"
217
+ display="$(sed -nE 's/^name:[[:space:]]*(.*)$/\1/p; s/^# (.*)$/\1/p' "$f" | head -1)"
218
+ [ -z "$display" ] && display="$github"
219
+ printf '%s\t%s\n' "$github" "$display"
220
+ done
221
+ }
222
+
223
+ cmd_sync() {
224
+ sync_memory
225
+ echo "synced: $MEMORY_DIR"
226
+ }
227
+
228
+ cmd_activity() {
229
+ ensure_memory
230
+ local for_person=""
231
+ local limit=12
232
+ while [ $# -gt 0 ]; do
233
+ case "$1" in
234
+ --for) for_person="${2:-}"; shift 2 ;;
235
+ --limit) limit="${2:-12}"; shift 2 ;;
236
+ *) die "unknown activity option: $1" ;;
237
+ esac
238
+ done
239
+
240
+ echo "Handoffs"
241
+ local index="$MEMORY_DIR/handoffs/index.md"
242
+ if [ -f "$index" ]; then
243
+ if [ -n "$for_person" ]; then
244
+ grep -i "handoff to ${for_person}" "$index" 2>/dev/null | head -n "$limit" || true
245
+ else
246
+ grep '^- ' "$index" 2>/dev/null | head -n "$limit" || true
247
+ fi
248
+ fi
249
+
250
+ echo
251
+ echo "Questions"
252
+ local qdir="$MEMORY_DIR/knowledge/questions"
253
+ [ -d "$qdir" ] || return 0
254
+ local q from to topic status
255
+ while IFS= read -r q; do
256
+ from="$(sed -nE 's/^from:[[:space:]]*(.*)$/\1/p' "$q" | head -1)"
257
+ to="$(sed -nE 's/^to:[[:space:]]*(.*)$/\1/p' "$q" | head -1)"
258
+ topic="$(sed -nE 's/^topic:[[:space:]]*(.*)$/\1/p' "$q" | head -1)"
259
+ status="$(sed -nE 's/^status:[[:space:]]*(.*)$/\1/p' "$q" | head -1)"
260
+ [ -z "$status" ] && status="pending"
261
+ if [ -n "$for_person" ] && [ "$(echo "$to" | tr '[:upper:]' '[:lower:]')" != "$(echo "$for_person" | tr '[:upper:]' '[:lower:]')" ]; then
262
+ continue
263
+ fi
264
+ printf '%s\n' "- ${from} -> ${to} [${status}]: ${topic} (${q#$MEMORY_DIR/})"
265
+ done < <(find "$qdir" -type f -name '*.md' | sort -r | head -n "$limit")
266
+ }
267
+
268
+ cmd_branch() {
269
+ local topic="" author="" branch_type="" slug="" branch="" wt_path="" existing_wt="" base=""
270
+
271
+ while [ $# -gt 0 ]; do
272
+ case "$1" in
273
+ --topic|--description) topic="${2:-}"; shift 2 ;;
274
+ --from|--author) author="${2:-}"; shift 2 ;;
275
+ --type) branch_type="${2:-}"; shift 2 ;;
276
+ *) die "unknown branch option: $1" ;;
277
+ esac
278
+ done
279
+
280
+ [ -n "$topic" ] || topic="$(date +%Y-%m-%d)"
281
+ [ -n "$author" ] || author="$(current_author)"
282
+
283
+ if [ -z "$branch_type" ]; then
284
+ case "$(echo "$topic" | tr '[:upper:]' '[:lower:]')" in
285
+ *fix*|*bug*|*broken*|*crash*) branch_type="bugfix" ;;
286
+ *feature*|*add*|*implement*|*new*) branch_type="feature" ;;
287
+ *) branch_type="dev" ;;
288
+ esac
289
+ fi
290
+ case "$branch_type" in
291
+ dev|feature|bugfix) ;;
292
+ *) die "invalid --type: $branch_type (valid: dev|feature|bugfix)" ;;
293
+ esac
294
+
295
+ slug="$(slugify "$topic")"
296
+ [ -n "$slug" ] || slug="$(date +%Y-%m-%d)"
297
+ case "$branch_type" in
298
+ dev) branch="dev/${author}/${slug}" ;;
299
+ feature) branch="feature/${slug}" ;;
300
+ bugfix) branch="bugfix/${slug}" ;;
301
+ esac
302
+
303
+ base="$(base_ref)" || die "could not find a safe branch point"
304
+
305
+ if [ -f "$SCRIPT_DIR/.git" ]; then
306
+ if git -C "$SCRIPT_DIR" show-ref --verify --quiet "refs/heads/$branch" 2>/dev/null; then
307
+ git -C "$SCRIPT_DIR" checkout "$branch" --quiet
308
+ else
309
+ git -C "$SCRIPT_DIR" checkout -b "$branch" "$base" --quiet
310
+ fi
311
+ egregore_link_shared_state "$SCRIPT_DIR" "$MAIN_PROJECT_DIR" >/dev/null 2>/dev/null || true
312
+ echo "branch: $branch"
313
+ echo "worktree: $SCRIPT_DIR"
314
+ return 0
315
+ fi
316
+
317
+ existing_wt="$(worktree_for_branch "$branch")"
318
+ if [ -n "$existing_wt" ] && [ -d "$existing_wt" ]; then
319
+ bash "$MAIN_PROJECT_DIR/bin/worktree.sh" setup "$existing_wt" "$MAIN_PROJECT_DIR" >/dev/null 2>/dev/null || true
320
+ echo "branch: $branch"
321
+ echo "worktree: $existing_wt"
322
+ return 0
323
+ fi
324
+
325
+ if ! git -C "$MAIN_PROJECT_DIR" show-ref --verify --quiet "refs/heads/$branch" 2>/dev/null; then
326
+ git -C "$MAIN_PROJECT_DIR" branch "$branch" "$base" >/dev/null 2>&1 || die "failed to create branch $branch"
327
+ fi
328
+
329
+ wt_path="$MAIN_PROJECT_DIR/.claude/worktrees/$slug"
330
+ if [ -e "$wt_path" ]; then
331
+ die "worktree path already exists: $wt_path"
332
+ fi
333
+
334
+ mkdir -p "$MAIN_PROJECT_DIR/.claude/worktrees"
335
+ git -C "$MAIN_PROJECT_DIR" worktree add "$wt_path" "$branch" --quiet 2>/dev/null || die "failed to create worktree at $wt_path"
336
+ bash "$MAIN_PROJECT_DIR/bin/worktree.sh" setup "$wt_path" "$MAIN_PROJECT_DIR" >/dev/null 2>/dev/null || true
337
+
338
+ echo "branch: $branch"
339
+ echo "worktree: $wt_path"
340
+ }
341
+
342
+ ensure_working_branch() {
343
+ local topic="${1:-$(date +%Y-%m-%d)}"
344
+ local author branch slug base
345
+ branch="$(git -C "$SCRIPT_DIR" branch --show-current 2>/dev/null || echo "")"
346
+ case "$branch" in
347
+ dev/*|feature/*|bugfix/*) echo "$branch"; return 0 ;;
348
+ esac
349
+
350
+ author="$(current_author)"
351
+ slug="$(slugify "$topic")"
352
+ [ -n "$slug" ] || slug="$(date +%Y-%m-%d)"
353
+ branch="dev/${author}/${slug}"
354
+ base="$(base_ref)" || die "could not find a safe branch point"
355
+
356
+ if git -C "$SCRIPT_DIR" show-ref --verify --quiet "refs/heads/$branch" 2>/dev/null; then
357
+ git -C "$SCRIPT_DIR" checkout "$branch" --quiet || die "failed to checkout $branch"
358
+ else
359
+ git -C "$SCRIPT_DIR" checkout -b "$branch" "$base" --quiet || die "failed to create $branch"
360
+ fi
361
+ echo "$branch"
362
+ }
363
+
364
+ build_pr_body() {
365
+ # Compliant fallback PR body (spec: .claude/context/pr-format.md) built
366
+ # from the commit log + diff when the calling agent supplies none.
367
+ local topic="${1:-portable save}" base commits stat
368
+ base="$(resolve_base_branch)" || return 1
369
+ commits="$(git -C "$SCRIPT_DIR" log --pretty='- %s' "origin/$base..HEAD" 2>/dev/null | head -8)"
370
+ [ -n "$commits" ] || commits="- (commit list unavailable — see the Files tab)"
371
+ stat="$(git -C "$SCRIPT_DIR" diff --shortstat "origin/$base...HEAD" 2>/dev/null | sed 's/^ *//')"
372
+ egregore_pr_skeleton \
373
+ "$commits" \
374
+ "Shell-agent session save — topic: $topic. Body auto-generated by bin/agent.sh; pass --pr-body for a hand-written description." \
375
+ "Not verified — auto-generated skeleton; review the diff before merging.${stat:+ ($stat)}" \
376
+ "🤖 Saved via bin/agent.sh"
377
+ }
378
+
379
+ cmd_save() {
380
+ local message="" topic="" no_pr=0 branch base pushed="false" committed="false" pr_url="" existing_pr="" pr_number="" non_md="" merged="false" pr_body="" pr_body_file=""
381
+
382
+ while [ $# -gt 0 ]; do
383
+ case "$1" in
384
+ --message|-m) message="${2:-}"; shift 2 ;;
385
+ --topic) topic="${2:-}"; shift 2 ;;
386
+ --pr-body) pr_body="${2:-}"; shift 2 ;;
387
+ --pr-body-file) pr_body_file="${2:-}"; shift 2 ;;
388
+ --no-pr) no_pr=1; shift ;;
389
+ *) die "unknown save option: $1" ;;
390
+ esac
391
+ done
392
+
393
+ [ -n "$topic" ] || topic="portable save"
394
+ [ -n "$message" ] || message="chore(save): $topic"
395
+ base="$(resolve_base_branch)" || die "save stopped before Git changes"
396
+
397
+ if [ -d "$MEMORY_DIR" ]; then
398
+ save_memory "$message" "."
399
+ echo "memory: synced"
400
+ fi
401
+
402
+ branch="$(git -C "$SCRIPT_DIR" branch --show-current 2>/dev/null || echo "")"
403
+ if [ -n "$(git -C "$SCRIPT_DIR" status --porcelain 2>/dev/null | head -1)" ]; then
404
+ branch="$(ensure_working_branch "$topic")"
405
+ git -C "$SCRIPT_DIR" add -A >/dev/null 2>&1 || die "git add failed"
406
+ if ! git -C "$SCRIPT_DIR" diff --cached --quiet 2>/dev/null; then
407
+ egregore_commit "$SCRIPT_DIR" "$SCRIPT_DIR" "$message" --quiet || die "git commit failed"
408
+ committed="true"
409
+ fi
410
+ fi
411
+ [ -n "$branch" ] || branch="$(git -C "$SCRIPT_DIR" branch --show-current 2>/dev/null || echo "")"
412
+
413
+ if [ -n "$branch" ] && git -C "$SCRIPT_DIR" remote get-url origin >/dev/null 2>&1; then
414
+ if git -C "$SCRIPT_DIR" push -u origin "$branch" --quiet 2>/dev/null; then
415
+ pushed="true"
416
+ else
417
+ die "push failed for $branch; commits are safe locally"
418
+ fi
419
+ fi
420
+
421
+ if [ "$no_pr" = "0" ] && [ "$pushed" = "true" ] && command -v gh >/dev/null 2>&1; then
422
+ existing_pr="$(gh pr list --head "$branch" --state open --json url --jq '.[0].url // empty' 2>/dev/null || true)"
423
+ if [ -n "$existing_pr" ]; then
424
+ pr_url="$existing_pr"
425
+ else
426
+ if [ -z "$pr_body" ] && [ -n "$pr_body_file" ] && [ -f "$pr_body_file" ]; then
427
+ pr_body="$(cat "$pr_body_file")"
428
+ fi
429
+ [ -n "$pr_body" ] || pr_body="$(build_pr_body "$topic")"
430
+ pr_url="$(gh pr create --base "$base" --head "$branch" --title "${message%%$'\n'*}" --body "$pr_body" 2>/dev/null || true)"
431
+ fi
432
+ fi
433
+
434
+ # Non-coding saves auto-merge to the configured base (same rule as /save and the
435
+ # handoff helper; policy in bin/lib/noncode.sh). `--auto` needs repo
436
+ # auto-merge + pending required checks; without branch protection it always
437
+ # errors, so fall back to a direct merge.
438
+ if [ -n "$pr_url" ]; then
439
+ # shellcheck source=bin/lib/noncode.sh
440
+ . "$SCRIPT_DIR/bin/lib/noncode.sh" 2>/dev/null || true
441
+ type noncode_blockers >/dev/null 2>&1 || noncode_blockers() {
442
+ git -C "${2:-.}" diff "${1:-origin/$base}...HEAD" --name-only 2>/dev/null | grep -v '\.md$' | head -1 || true
443
+ }
444
+ pr_number="${pr_url##*/}"
445
+ non_code="$(noncode_blockers "origin/$base" "$SCRIPT_DIR")"
446
+ if [ -z "$non_code" ] && [ -n "$pr_number" ]; then
447
+ if gh pr merge "$pr_number" --auto --merge >/dev/null 2>&1 \
448
+ || gh pr merge "$pr_number" --merge >/dev/null 2>&1; then
449
+ merged="true"
450
+ fi
451
+ fi
452
+ fi
453
+
454
+ echo "branch: ${branch:-none}"
455
+ echo "commit: $committed"
456
+ echo "push: $pushed"
457
+ [ -n "$pr_url" ] && echo "pr: $pr_url"
458
+ [ "$merged" = "true" ] && echo "merge: non-coding, merged to $base"
459
+ return 0
460
+ }
461
+
462
+ cmd_wrap() {
463
+ ensure_memory
464
+ local from="" topic="" summary="" body="" body_file="" no_push=0
465
+
466
+ while [ $# -gt 0 ]; do
467
+ case "$1" in
468
+ --from) from="${2:-}"; shift 2 ;;
469
+ --topic) topic="${2:-}"; shift 2 ;;
470
+ --summary) summary="${2:-}"; shift 2 ;;
471
+ --body) body="${2:-}"; shift 2 ;;
472
+ --body-file) body_file="${2:-}"; shift 2 ;;
473
+ --no-push) no_push=1; shift ;;
474
+ *) die "unknown wrap option: $1" ;;
475
+ esac
476
+ done
477
+
478
+ [ -n "$from" ] || from="$(current_author)"
479
+ [ -n "$topic" ] || die "wrap requires --topic"
480
+ [ -n "$summary" ] || die "wrap requires --summary"
481
+ if [ -n "$body_file" ]; then
482
+ [ -f "$body_file" ] || die "body file not found: $body_file"
483
+ body="$(cat "$body_file")"
484
+ elif [ -z "$body" ] && [ ! -t 0 ]; then
485
+ body="$(cat)"
486
+ fi
487
+ [ -n "$body" ] || body="$summary"
488
+
489
+ local tmpd result rel session_id branch
490
+ local capture_args=(--mode personal)
491
+ tmpd="$(mktemp -d -t egregore-agent-wrap-XXXXXX)"
492
+ trap 'rm -rf "$tmpd"' RETURN
493
+ session_id="$(cat "$SCRIPT_DIR/.egregore-session-id" 2>/dev/null || true)"
494
+ branch="$(git -C "$SCRIPT_DIR" branch --show-current 2>/dev/null || true)"
495
+ [ "$no_push" = "1" ] && capture_args+=(--no-push)
496
+
497
+ printf '%s\n' "$body" |
498
+ TMPDIR="$tmpd" bash "$SCRIPT_DIR/bin/capture-run.sh" \
499
+ "${capture_args[@]}" \
500
+ --author "$from" \
501
+ --topic "$topic" \
502
+ --summary "$summary" \
503
+ --session-id "$session_id" \
504
+ --branch "$branch" >/dev/null
505
+
506
+ result="$tmpd/capture-run-result.json"
507
+ rel="$(jq -r '.file // empty' "$result" 2>/dev/null || true)"
508
+ [ -n "$rel" ] || die "wrap was not created"
509
+ echo "wrap: memory/$rel"
510
+
511
+ # Session close: non-coding core-repo changes save + merge themselves
512
+ # (gated inside session-autosave.sh — code is never auto-committed).
513
+ if [ "$no_push" = "0" ]; then
514
+ ( bash "$SCRIPT_DIR/bin/session-autosave.sh" --dir "$SCRIPT_DIR" >/dev/null 2>&1 & ) >/dev/null 2>&1
515
+ fi
516
+ }
517
+
518
+ cmd_handoff() {
519
+ ensure_memory
520
+ local from="" to="" topic="" body="" body_file="" project="" intent="action"
521
+ local no_publish=0 no_notify=0 json=0 body_supplied=0 content_mode="generated"
522
+ NO_PUSH=0
523
+
524
+ while [ $# -gt 0 ]; do
525
+ case "$1" in
526
+ --from) from="${2:-}"; shift 2 ;;
527
+ --to) to="${2:-}"; shift 2 ;;
528
+ --topic) topic="${2:-}"; shift 2 ;;
529
+ --body) body="${2:-}"; shift 2 ;;
530
+ --body-file) body_file="${2:-}"; shift 2 ;;
531
+ --project) project="${2:-}"; shift 2 ;;
532
+ --intent) intent="${2:-}"; shift 2 ;;
533
+ --no-push) NO_PUSH=1; shift ;;
534
+ --no-publish) no_publish=1; shift ;;
535
+ --no-notify) no_notify=1; shift ;;
536
+ --json) json=1; shift ;;
537
+ *) die "unknown handoff option: $1" ;;
538
+ esac
539
+ done
540
+
541
+ [ -z "$from" ] && from="$(person_from_state)"
542
+ [ -n "$from" ] || die "handoff requires --from"
543
+ [ -n "$topic" ] || die "handoff requires --topic"
544
+ case "$intent" in
545
+ action|feedback|fyi) ;;
546
+ *) die "invalid --intent: $intent (action|feedback|fyi)" ;;
547
+ esac
548
+ if [ -n "$body_file" ]; then
549
+ [ -f "$body_file" ] || die "body file not found: $body_file"
550
+ body="$(cat "$body_file")"
551
+ body_supplied=1
552
+ elif [ -z "$body" ] && [ ! -t 0 ]; then
553
+ body="$(cat)"
554
+ [ -n "$body" ] && body_supplied=1
555
+ elif [ -n "$body" ]; then
556
+ body_supplied=1
557
+ fi
558
+ [ "$body_supplied" = "1" ] && content_mode="supplied"
559
+
560
+ local today tmpd result result_out rel stdout_file
561
+ local handoff_args=()
562
+ today="$(date +%Y-%m-%d)"
563
+ tmpd="$(mktemp -d -t egregore-agent-handoff-XXXXXX)"
564
+ trap 'rm -rf "$tmpd"' RETURN
565
+ result_out="$(mktemp -t egregore-agent-handoff-result-XXXXXX.json)"
566
+ stdout_file="$tmpd/stdout"
567
+
568
+ [ -n "$to" ] && handoff_args+=(--recipient "$to")
569
+ [ -n "$project" ] && handoff_args+=(--project "$project")
570
+ handoff_args+=(--intent "$intent" --content-mode "$content_mode")
571
+ [ "$content_mode" = "generated" ] && handoff_args+=(--include-session-artifacts)
572
+ [ "$NO_PUSH" = "1" ] && handoff_args+=(--no-push)
573
+ [ "$no_publish" = "1" ] && handoff_args+=(--no-publish)
574
+ [ "$no_notify" = "1" ] && handoff_args+=(--no-notify)
575
+
576
+ {
577
+ printf '%s\n' '---'
578
+ printf 'capture_schema: egregore-capture/v1\n'
579
+ printf 'capture_mode: addressed\n'
580
+ printf 'kind: addressed\n'
581
+ printf 'content_mode: %s\n' "$content_mode"
582
+ printf 'from: %s\n' "$(yaml_str "$from")"
583
+ [ -n "$to" ] && printf 'addressed_to: %s\n' "$(yaml_str "$to")"
584
+ printf 'date: %s\n' "$today"
585
+ printf 'topic: %s\n' "$(yaml_str "$topic")"
586
+ printf 'intent: %s\n' "$intent"
587
+ [ -n "$project" ] && printf 'project: %s\n' "$(yaml_str "$project")"
588
+ printf '%s\n\n' '---'
589
+ if [ "$content_mode" = "supplied" ]; then
590
+ printf '%s\n' "$body"
591
+ else
592
+ printf '# Handoff: %s\n\n' "$topic"
593
+ printf '## Briefing\n\nNo briefing was supplied.\n\n'
594
+ printf '## Next Steps\n\n1. Continue from this handoff using the shared Egregore memory protocol.\n'
595
+ fi
596
+ } | TMPDIR="$tmpd" bash "$SCRIPT_DIR/bin/capture-run.sh" \
597
+ --mode addressed \
598
+ --author "$from" \
599
+ --topic "$topic" \
600
+ "${handoff_args[@]}" \
601
+ > "$stdout_file"
602
+
603
+ result="$tmpd/handoff-run-result.json"
604
+ rel="$(jq -r '.file // empty' "$result" 2>/dev/null || true)"
605
+ [ -n "$rel" ] || die "handoff was not created"
606
+ cp "$result" "$result_out"
607
+ if [ "$json" = "1" ]; then
608
+ cat "$result_out"
609
+ return 0
610
+ fi
611
+ [ -s "$stdout_file" ] && sed 's/^/status: /' "$stdout_file"
612
+ echo "handoff: memory/$rel"
613
+ echo "result: $result_out"
614
+
615
+ # Parity with the Claude /handoff skill: save core-repo session work in the
616
+ # background (non-coding diffs auto-merge to the configured base; code stays
617
+ # in a PR).
618
+ if [ "$NO_PUSH" = "0" ]; then
619
+ ( bash "$SCRIPT_DIR/bin/handoff-save-egregore.sh" "$from" "$topic" --repo-dir "$SCRIPT_DIR" >/dev/null 2>&1 & ) >/dev/null 2>&1
620
+ fi
621
+ }
622
+
623
+ cmd_ask() {
624
+ ensure_memory
625
+ local from="" to="" topic="" question=""
626
+ local harvest_id="" harvest_session_id="" turn="" question_intent="" context_mode=""
627
+ NO_PUSH=0
628
+
629
+ while [ $# -gt 0 ]; do
630
+ case "$1" in
631
+ --from) from="${2:-}"; shift 2 ;;
632
+ --to) to="${2:-}"; shift 2 ;;
633
+ --topic) topic="${2:-}"; shift 2 ;;
634
+ --question) question="${2:-}"; shift 2 ;;
635
+ --harvest-id) harvest_id="${2:-}"; shift 2 ;;
636
+ --harvest-session-id) harvest_session_id="${2:-}"; shift 2 ;;
637
+ --turn) turn="${2:-}"; shift 2 ;;
638
+ --question-intent) question_intent="${2:-}"; shift 2 ;;
639
+ --context-mode) context_mode="${2:-}"; shift 2 ;;
640
+ --no-push) NO_PUSH=1; shift ;;
641
+ *) die "unknown ask option: $1" ;;
642
+ esac
643
+ done
644
+
645
+ [ -z "$from" ] && from="$(person_from_state)"
646
+ [ -n "$from" ] || die "ask requires --from"
647
+ [ -n "$to" ] || die "ask requires --to"
648
+ [ -n "$topic" ] || die "ask requires --topic"
649
+ [ -n "$question" ] || die "ask requires --question"
650
+
651
+ if [ -n "$context_mode" ]; then
652
+ case "$context_mode" in
653
+ blind|disclosed|comparative) ;;
654
+ *) die "invalid --context-mode: $context_mode (valid: blind|disclosed|comparative)" ;;
655
+ esac
656
+ fi
657
+
658
+ if [ -n "$turn" ]; then
659
+ case "$turn" in
660
+ ''|*[!0-9]*) die "invalid --turn: must be a non-negative integer" ;;
661
+ esac
662
+ fi
663
+
664
+ reject_newlines "--from" "$from"
665
+ reject_newlines "--to" "$to"
666
+ reject_newlines "--topic" "$topic"
667
+ reject_newlines "--question" "$question"
668
+ reject_newlines "--harvest-id" "$harvest_id"
669
+ reject_newlines "--harvest-session-id" "$harvest_session_id"
670
+ reject_newlines "--question-intent" "$question_intent"
671
+
672
+ local now slug rel abs n
673
+ now="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
674
+ slug="$(slugify "$topic")"
675
+ mkdir -p "$MEMORY_DIR/knowledge/questions"
676
+ rel="knowledge/questions/$(date +%Y-%m-%d)-${from}-to-${to}-${slug}.md"
677
+ abs="$MEMORY_DIR/$rel"
678
+ n=2
679
+ while [ -e "$abs" ]; do
680
+ rel="knowledge/questions/$(date +%Y-%m-%d)-${from}-to-${to}-${slug}-${n}.md"
681
+ abs="$MEMORY_DIR/$rel"
682
+ n=$((n + 1))
683
+ done
684
+
685
+ {
686
+ echo "---"
687
+ echo "from: $from"
688
+ echo "to: $to"
689
+ echo "topic: $topic"
690
+ echo "status: pending"
691
+ echo "created: $now"
692
+ [ -n "$harvest_id" ] && echo "harvest_id: $(yaml_str "$harvest_id")"
693
+ [ -n "$harvest_session_id" ] && echo "harvest_session_id: $(yaml_str "$harvest_session_id")"
694
+ [ -n "$turn" ] && echo "turn: $turn"
695
+ [ -n "$question_intent" ] && echo "question_intent: $(yaml_str "$question_intent")"
696
+ [ -n "$context_mode" ] && echo "context_mode: $context_mode"
697
+ echo "---"
698
+ echo ""
699
+ echo "## Questions"
700
+ echo ""
701
+ echo "- $question"
702
+ } > "$abs"
703
+
704
+ save_memory "Question: ${topic} (to ${to})" "$rel"
705
+ echo "question: memory/$rel"
706
+ }
707
+
708
+ cmd_answer() {
709
+ ensure_memory
710
+ local from="" question_path="" body=""
711
+ NO_PUSH=0
712
+
713
+ while [ $# -gt 0 ]; do
714
+ case "$1" in
715
+ --from) from="${2:-}"; shift 2 ;;
716
+ --question) question_path="${2:-}"; shift 2 ;;
717
+ --body) body="${2:-}"; shift 2 ;;
718
+ --no-push) NO_PUSH=1; shift ;;
719
+ *) die "unknown answer option: $1" ;;
720
+ esac
721
+ done
722
+
723
+ [ -z "$from" ] && from="$(person_from_state)"
724
+ [ -n "$from" ] || die "answer requires --from"
725
+ [ -n "$question_path" ] || die "answer requires --question"
726
+ [ -n "$body" ] || die "answer requires --body"
727
+
728
+ local abs rel now tmp
729
+ case "$question_path" in
730
+ "$MEMORY_DIR"/*) abs="$question_path" ;;
731
+ memory/*) abs="$MEMORY_DIR/${question_path#memory/}" ;;
732
+ *) abs="$MEMORY_DIR/$question_path" ;;
733
+ esac
734
+ [ -f "$abs" ] || die "question file not found: $question_path"
735
+ rel="${abs#$MEMORY_DIR/}"
736
+ now="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
737
+ tmp="$abs.tmp.$$"
738
+
739
+ awk -v from="$from" -v now="$now" '
740
+ BEGIN { in_fm = 0; added = 0; saw_status = 0 }
741
+ NR == 1 && $0 == "---" { in_fm = 1; print; next }
742
+ in_fm && /^status:/ { print "status: answered"; saw_status = 1; next }
743
+ in_fm && $0 == "---" {
744
+ if (!saw_status) print "status: answered"
745
+ if (!added) {
746
+ print "answered_by: " from
747
+ print "answered_at: " now
748
+ added = 1
749
+ }
750
+ in_fm = 0
751
+ print
752
+ next
753
+ }
754
+ { print }
755
+ ' "$abs" > "$tmp" && mv "$tmp" "$abs"
756
+
757
+ {
758
+ printf '\n## Answer\n\n'
759
+ printf '**From**: %s\n' "$from"
760
+ printf '**At**: %s\n\n' "$now"
761
+ printf '%s\n' "$body"
762
+ } >> "$abs"
763
+
764
+ save_memory "Answer question: ${rel}" "$rel"
765
+ echo "answered: memory/$rel"
766
+ }
767
+
768
+ cmd_search() {
769
+ bash "$SCRIPT_DIR/bin/search.sh" query "$@"
770
+ }
771
+
772
+ case "${1:-}" in
773
+ --help|-h|help|"") usage ;;
774
+ protocol) shift; cmd_protocol "$@" ;;
775
+ sync) shift; cmd_sync "$@" ;;
776
+ activity) shift; cmd_activity "$@" ;;
777
+ search) shift; cmd_search "$@" ;;
778
+ people) shift; cmd_people "$@" ;;
779
+ branch) shift; cmd_branch "$@" ;;
780
+ save) shift; cmd_save "$@" ;;
781
+ autosave) shift; bash "$SCRIPT_DIR/bin/autosave.sh" "$@" ;;
782
+ wrap) shift; cmd_wrap "$@" ;;
783
+ handoff) shift; cmd_handoff "$@" ;;
784
+ ask) shift; cmd_ask "$@" ;;
785
+ answer) shift; cmd_answer "$@" ;;
786
+ *) die "unknown command: $1" ;;
787
+ esac