Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
basic-memory/src/basic_memory/cli/app.py at dffc9d58fbc9693e43406f486e3f3a9251462206 · basicmachines-co/basic-memory · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Uh oh!
There was an error while loading.
Please reload this page
.
basicmachines-co
/
basic-memory
Public
Notifications
You must be signed in to change notification settings
Fork
275
Star
3.9k
Code
Issues
62
Pull requests
4
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Files
Expand file tree
dffc9d58fbc9693e43406f486e3f3a9251462206
Breadcrumbs
basic-memory
/
src
/
basic_memory
/
cli
/
app.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
166 lines (137 loc) · 6.17 KB
dffc9d5
Breadcrumbs
basic-memory
/
src
/
basic_memory
/
cli
/
app.py
Copy path
Top
File metadata and controls
Code
Blame
166 lines (137 loc) · 6.17 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# This prevents DEBUG logs from appearing on stdout during module-level
# initialization (e.g., template_loader.TemplateLoader() logs at DEBUG level).
from
loguru
import
logger
logger
.
remove
()
from
typing
import
Optional
# noqa: E402
import
typer
# noqa: E402
from
basic_memory
.
cli
.
auto_update
import
maybe_run_periodic_auto_update
# noqa: E402
from
basic_memory
.
cli
.
container
import
CliContainer
,
set_container
# noqa: E402
from
basic_memory
.
cli
.
promo
import
maybe_show_cloud_promo
,
maybe_show_init_line
# noqa: E402
from
basic_memory
.
config
import
init_cli_logging
# noqa: E402
import
logfire
# noqa: E402
def
version_callback
(
value
:
bool
)
->
None
:
"""Show version and exit."""
if
value
:
# pragma: no cover
import
basic_memory
typer
.
echo
(
f"Basic Memory version:
{
basic_memory
.
__version__
}
"
)
raise
typer
.
Exit
()
app
=
typer
.
Typer
(
name
=
"basic-memory"
)
@
app
.
callback
()
def
app_callback
(
ctx
:
typer
.
Context
,
version
:
Optional
[
bool
]
=
typer
.
Option
(
None
,
"--version"
,
"-v"
,
help
=
"Show version and exit."
,
callback
=
version_callback
,
is_eager
=
True
,
),
)
->
None
:
"""Basic Memory - Local-first personal knowledge management."""
command_name
=
ctx
.
invoked_subcommand
or
"root"
# Trigger: a `hook` invocation (the advisory harness front door, SPEC-55).
# Why: the hook verbs need none of the global composition root — they resolve
# config lazily via ConfigManager when they run, the lifecycle verbs
# (session-start/pre-compact) each wrap their body in _run_fail_open, and the
# operator verbs (install/remove) touch no global config at all. Everything
# the callback does here — logging setup (Logfire loads config), the span,
# the container, uvloop — can raise SystemExit on a malformed config
# (ConfigManager reports bad JSON that way), and none of it may abort the verb
# before its own guard: even config-free work (envelope capture, `hook
# install`) must still run.
# Outcome: run that setup best-effort, swallowing (Exception, SystemExit) so a
# broken config surfaces only where it belongs — a lifecycle verb's fail-open
# guard, or an operator verb that needs config. Skip global init and the
# promo/init-line/auto-update messaging (off the session-start/pre-compact hot
# path, out of the brief). KeyboardInterrupt is left to propagate.
if
ctx
.
invoked_subcommand
==
"hook"
:
try
:
init_cli_logging
()
ctx
.
with_resource
(
logfire
.
span
(
f"cli.command.
{
command_name
}
"
,
entrypoint
=
"cli"
,
command_name
=
command_name
,
)
)
container
=
CliContainer
.
create
()
set_container
(
container
)
# uvloop must own the event-loop policy before the hook verbs run
# async search/write/flush through run_with_cleanup's asyncio.run(),
# or a Postgres backend hits the asyncpg engine-dispose race
# (#831/#877). No-op for SQLite, so hook startup stays light.
from
basic_memory
.
db
import
maybe_install_uvloop
maybe_install_uvloop
(
container
.
config
)
except
(
Exception
,
SystemExit
):
pass
return
# Initialize logging for CLI (file only, no stdout)
init_cli_logging
()
ctx
.
with_resource
(
logfire
.
span
(
f"cli.command.
{
command_name
}
"
,
entrypoint
=
"cli"
,
command_name
=
command_name
,
)
)
# --- Composition Root ---
# Create container and read config (single point of config access)
container
=
CliContainer
.
create
()
set_container
(
container
)
# Trigger: Postgres backend resolved at CLI startup, before any asyncio.run().
# Why: uvloop must own the event-loop policy before the loop is created so the
# asyncpg engine-dispose race (#831/#877) cannot fire. No-op for SQLite.
# Outcome: subsequent asyncio.run() calls in CLI commands use uvloop on Postgres.
from
basic_memory
.
db
import
maybe_install_uvloop
maybe_install_uvloop
(
container
.
config
)
# Trigger: first-run init confirmation before command output.
# Why: informational "initialized" message belongs above command results, not in the upsell panel.
# Outcome: one-time plain line printed before the subcommand runs.
maybe_show_init_line
(
ctx
.
invoked_subcommand
)
# Trigger: register post-command messaging callbacks.
# Why: informational/promo/update output belongs below command results.
# Outcome: command output remains primary, with optional follow-up notices afterwards.
def
_post_command_messages
()
->
None
:
maybe_show_cloud_promo
(
ctx
.
invoked_subcommand
)
maybe_run_periodic_auto_update
(
ctx
.
invoked_subcommand
)
ctx
.
call_on_close
(
_post_command_messages
)
# Run initialization for commands that don't use the API
# Skip for 'mcp' command - it has its own lifespan that handles initialization
# Skip for API-using commands (status, sync, etc.) - they handle initialization via deps.py
# Skip for 'reset' command - it manages its own database lifecycle
# Skip for 'man' - it only copies packaged files; a broken local database
# must not block installing the offline docs
# ('hook' returns above, before this point.)
skip_init_commands
=
{
"doctor"
,
"inspect"
,
"man"
,
"mcp"
,
"status"
,
"sync"
,
"project"
,
"config"
,
"tool"
,
"reset"
,
"reindex"
,
"update"
,
"watch"
,
"workspace"
,
}
if
(
not
version
and
ctx
.
invoked_subcommand
is
not
None
and
ctx
.
invoked_subcommand
not
in
skip_init_commands
):
from
basic_memory
.
services
.
initialization
import
ensure_initialization
ensure_initialization
(
container
.
config
)
## import
# Register sub-command groups
import_app
=
typer
.
Typer
(
help
=
"Import data from various sources"
)
app
.
add_typer
(
import_app
,
name
=
"import"
)
claude_app
=
typer
.
Typer
(
help
=
"Import Conversations from Claude JSON export."
)
import_app
.
add_typer
(
claude_app
,
name
=
"claude"
)
## cloud
cloud_app
=
typer
.
Typer
(
help
=
"Access Basic Memory Cloud"
)
app
.
add_typer
(
cloud_app
,
name
=
"cloud"
)
You can’t perform that action at this time.