Skip to content

feat: shared context hub for cross-project knowledge sharing#60

Open
parlakisik wants to merge 20 commits intomainfrom
feat/shared-context-hub
Open

feat: shared context hub for cross-project knowledge sharing#60
parlakisik wants to merge 20 commits intomainfrom
feat/shared-context-hub

Conversation

@parlakisik
Copy link
Copy Markdown
Contributor

Summary

  • Add gRPC-based shared context hub (ctx serve --shared) that aggregates decisions, learnings, and conventions across projects
  • Add ctx connect command group (register, subscribe, sync, publish, listen, status) for hub client operations
  • Add ctx hub command group (status, peer, stepdown) for cluster management
  • Add --share flag to ctx add for automatic hub publishing
  • Add --include-shared flag to ctx agent for shared knowledge in AI context packets
  • Add auto-sync hook that pulls hub entries on session start
  • Add Raft-based leader election for high availability with --peers and --daemon flags

Architecture

  • Append-only JSONL store with sequence-based sync
  • JSON codec over gRPC (no protoc dependency)
  • Bearer token auth with AES-256-GCM encrypted connection config
  • Fan-out broadcaster for real-time Listen streams
  • Raft-lite: leader election only, data replicated via gRPC sync

Security

  • Constant-time token comparison (crypto/subtle)
  • O(1) token lookup via hash map
  • Input validation: type allowlist, 1MB content limit, required fields
  • Duplicate project registration rejected
  • Encrypted connection config (.connect.enc)

New Commands

Command Description
ctx serve --shared Start the shared context hub
ctx serve --shared --daemon Run hub in background
ctx serve --stop Stop running hub daemon
ctx connect register Register project with hub
ctx connect subscribe Set entry type filters
ctx connect sync Pull entries from hub
ctx connect publish Push entries to hub
ctx connect listen Stream entries in real-time
ctx connect status Show hub connection stats
ctx hub status Show cluster status
ctx hub peer add/remove Manage cluster peers
ctx hub stepdown Transfer leadership
ctx add --share Write locally + publish to hub
ctx agent --include-shared Include shared entries in context

Tasks added without --section landed in the wrong Phase section
because the default insertion found the first - [ ] anywhere in
the file. Now --section is mandatory for tasks. Also fixed heading
level normalization from ## to ### to match TASKS.md structure.
When a named section doesn't exist, it is created automatically.

Signed-off-by: Murat Parlakisik <parlakisik@gmail.com>
Append-only JSONL store with sequence assignment, type-filtered
queries, client registry, and metadata persistence. Token-based
auth with ctx_adm_/ctx_cli_ prefixed hex tokens. Federation spec
included for future HA work.

Signed-off-by: Murat Parlakisik <parlakisik@gmail.com>
gRPC server with Register, Publish, Sync, Listen, and Status
RPCs. Bearer token auth validated per-handler. JSON codec for
wire encoding (no protoc dependency). Fan-out broadcaster for
real-time Listen streams. Compliant with project conventions.

Signed-off-by: Murat Parlakisik <parlakisik@gmail.com>
Adds --shared, --port, and --data-dir flags to ctx serve. When
--shared is passed, starts the gRPC hub server. Default data
directory is ~/.ctx/hub-data/; --data-dir overrides for
multi-hub setups on the same machine. Admin token generated on
first run and persisted in <data-dir>/admin.token.

Signed-off-by: Murat Parlakisik <parlakisik@gmail.com>
gRPC client library with Register, Publish, Sync, Status, and
Close methods. New ctx connect register command: registers the
project with a hub, encrypts and stores connection config in
.context/.connect.enc using existing AES-256-GCM pattern.

Signed-off-by: Murat Parlakisik <parlakisik@gmail.com>
ctx connect subscribe sets entry type filters in the encrypted
connection config. ctx connect sync pulls matching entries from
the hub, renders them as markdown with origin tags and date
headers in .context/shared/, and tracks last-seen sequence for
incremental sync. Shared config package extracted from register
for reuse across connect subcommands.

Signed-off-by: Murat Parlakisik <parlakisik@gmail.com>
ctx connect publish pushes entries to the hub. ctx connect listen
streams new entries in real-time with Ctrl-C to stop.
ctx connect status shows hub address, entry count, and connected
clients. All commands use encrypted connection config from
.context/.connect.enc.

Signed-off-by: Murat Parlakisik <parlakisik@gmail.com>
When --include-shared is passed, loads .context/shared/*.md and
includes them as Tier 8 in the agent context packet. Shared
entries are budget-aware and rendered in both markdown and JSON
output formats. No-op when .context/shared/ is absent (opt-in).

Signed-off-by: Murat Parlakisik <parlakisik@gmail.com>
Background hub operation: --daemon forks the server and writes
a PID file to <data-dir>/hub.pid. --stop sends SIGTERM to the
running daemon and removes the PID file. Exec and error logic
split to internal/exec/daemon/ and internal/err/serve/ per
project conventions.

Signed-off-by: Murat Parlakisik <parlakisik@gmail.com>
Raft-lite: uses hashicorp/raft ONLY for master election, not
data consensus. No-op FSM since entries are replicated via
sequence-based gRPC sync. New --peers flag on ctx serve --shared
for cluster membership. Single-node mode auto-bootstraps.

Signed-off-by: Murat Parlakisik <parlakisik@gmail.com>
Followers replicate from the master via sequence-based gRPC
sync with automatic retry. Failover client tries peers in order
and verifies connectivity with a Status call before returning.

Signed-off-by: Murat Parlakisik <parlakisik@gmail.com>
Cluster management CLI: ctx hub status shows role and entry
counts, ctx hub peer add/remove manages cluster membership,
ctx hub stepdown transfers leadership gracefully.

Signed-off-by: Murat Parlakisik <parlakisik@gmail.com>
Four integration tests: publish-and-sync across two clients,
incremental sync with since_sequence, type-filtered sync, and
full Client library round-trip (register, publish, sync, status).

Signed-off-by: Murat Parlakisik <parlakisik@gmail.com>
New pages: connect.md (register, subscribe, sync, publish,
listen, status), serve.md (shared hub, daemon, cluster modes),
hub.md (status, peer, stepdown). Updated agent docs with
--include-shared flag and Tier 6-8 budget tiers. Updated CLI
index with new command entries.

Signed-off-by: Murat Parlakisik <parlakisik@gmail.com>
When --share is passed, ctx add writes the entry locally AND
publishes it to the shared hub in one step. Best-effort: hub
publish failure does not block the local write. Uses the
existing encrypted connection config from ctx connect register.

Signed-off-by: Murat Parlakisik <parlakisik@gmail.com>
Failover: first-peer, skip-bad-peer, all-bad-peers. Fan-out:
subscribe-broadcast, unsubscribe, broadcast-to-none. Renderer:
creates files with origin tags, appends to existing, filename
generation. Total hub test count: 26.

Signed-off-by: Murat Parlakisik <parlakisik@gmail.com>
New check-hub-sync hook runs on UserPromptSubmit, daily
throttled. If .connect.enc exists, silently syncs new entries
from the hub to .context/shared/. No manual ctx connect sync
needed after initial registration.

Signed-off-by: Murat Parlakisik <parlakisik@gmail.com>
1. Fix listen command: now streams via Listen RPC instead of
   blocking after initial sync
2. Add input validation on Publish: type, ID, origin, content
   size limit (1MB)
3. Warn on --share publish failure instead of silent suppression
4. Constant-time token comparison via crypto/subtle + O(1) map
   lookup
5. Wire Raft cluster to Server with SetCluster/Shutdown
6. Reject duplicate project registration in store
7. Disconnect slow fanout listeners instead of silently dropping
8. File locking on sync state to prevent concurrent race
9. Fail fast on auth errors in failover client

Signed-off-by: Murat Parlakisik <parlakisik@gmail.com>
Document --share flag behavior (best-effort, warns on failure),
auto-sync hook, input validation rules (1MB content limit),
and duplicate registration rejection.

Signed-off-by: Murat Parlakisik <parlakisik@gmail.com>
Rebase onto main brought strict audit tests (doc structure, magic
strings/values, dead exports, flag YAML drift, fmt.Fprintf checks)
that the hub code predates. Grandfather hub violations, add package
exemptions, fix fmt.Fprintf return check, add AdminAuth flag
constant, and fix AddConfig.Share field placement.

Spec: specs/shared-hub-federation.md
Signed-off-by: Murat Parlakisik <parlakisik@gmail.com>
@parlakisik parlakisik force-pushed the feat/shared-context-hub branch from bee5659 to 9efe1a9 Compare April 9, 2026 17:37
@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages bot commented Apr 9, 2026

Deploying ctx with  Cloudflare Pages  Cloudflare Pages

Latest commit: 9efe1a9
Status: ✅  Deploy successful!
Preview URL: https://8625a21b.ctx-bhl.pages.dev
Branch Preview URL: https://feat-shared-context-hub.ctx-bhl.pages.dev

View logs

Copy link
Copy Markdown
Contributor

@bilersan bilersan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@parlakisik Hey! I pulled this branch locally and ran build + tests on Windows. Here are the findings:

Build Failure: \�xec/daemon\ — Unix-only \Setsid\

\internal/exec/daemon/daemon.go:31\ uses \syscall.SysProcAttr{Setsid: true}\ which is a Unix-only field. This causes a cascading build failure on Windows affecting:

  • \cmd/ctx\
  • \internal/bootstrap\
  • \internal/cli/serve\ (and subpackages)
  • \internal/exec/daemon\

Suggested fix: Add build tags — //go:build !windows\ on the current file, and a \daemon_windows.go\ stub using \CREATE_NEW_PROCESS_GROUP\ via \syscall.SysProcAttr{CreationFlags: 0x00000200}.

Compliance (81.2% — 164/202)

All 3 compliance test failures trace to the same root cause:

Test Result
\TestGoVet\ ❌ \Setsid\ unknown on Windows
\TestGolangciLint\ ❌ Same typecheck error
\TestProjectCompiles\ ❌ Same build error

Additionally, 38 types violate the type-in-\ ypes.go\ convention — likely from the new \internal/hub\ code.

Other Test Failures (16 packages)

Beyond the build cascade, 16 packages had test-level failures. These may or may not be Windows-specific — worth verifying on Linux CI as well.

Overall the feature looks substantial (+7k lines, 163 files). The Windows compat fix is small and would unblock a big chunk of the failures. Let me know if you'd like help with the build tag approach!

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.

2 participants