Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ OPENCLAW_ANTHROPIC_API_KEY=sk-ant-api03-your-key-here
OPENCLAW_GATEWAY_TOKEN=your-gateway-token-here
# OpenCode API key for OpenCode Go access via cliproxyapi
OPENCODE_API_KEY=your-opencode-api-key-here
# Paperclip database URL
DATABASE_URL=postgres://user:password@host:5432/paperclip
3 changes: 1 addition & 2 deletions config/paperclip/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ let
template = "${./config.template.json}";
sed = "${pkgs.gnused}/bin/sed";
database_mode = if host.isKyber then "postgres" else "embedded-postgres";
database_connection_string =
if host.isKyber then "postgres://postgres:postgres@localhost:5432/paperclip" else "";
database_connection_string = "";
deployment_mode = if host.isKyber then "authenticated" else "local_trusted";
Comment on lines 16 to 18

Copilot AI Apr 4, 2026

Copy link

Choose a reason for hiding this comment

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

database_mode is postgres on Kyber but database_connection_string is now always an empty string. Unless DATABASE_URL is guaranteed to be present at hydrate time, this produces an invalid config. Either provide a sane Kyber default here or enforce/validate that DATABASE_URL is set (and fail/warn clearly) during hydration.

Copilot uses AI. Check for mistakes.
host = if host.isKyber then "0.0.0.0" else "127.0.0.1";
allowed_hostname = if host.isKyber then "paperclip.shunkakinoki.com" else "";
Expand Down
26 changes: 19 additions & 7 deletions config/paperclip/hydrate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,38 @@ set -euo pipefail
INSTANCE_DIR="@instance_dir@"
CONFIG="${INSTANCE_DIR}/config.json"
TEMPLATE="@template@"
ENV_FILE="${HOME}/dotfiles/.env"

# Source .env if it exists (for DATABASE_URL)
if [ -f "$ENV_FILE" ]; then
set -a
# shellcheck source=/dev/null
. "$ENV_FILE"
set +a
fi

mkdir -p "${INSTANCE_DIR}"

# Use DATABASE_URL from env if set, otherwise use the Nix default
DB_CONNECTION="${DATABASE_URL:-@database_connection_string@}"

Copilot AI Apr 4, 2026

Copy link

Choose a reason for hiding this comment

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

On Kyber, database_mode is set to postgres but if DATABASE_URL is missing (and the Nix default is empty), the script will still render a config with an empty connectionString, likely causing Paperclip to fail later with a hard-to-debug error. Recommend explicitly checking for a non-empty DATABASE_URL on Kyber and exiting early with a clear warning (similar to other hydrate scripts) instead of producing an invalid config.

Suggested change
# On Kyber, postgres mode requires a non-empty connection string.
# Fail early with a clear error instead of rendering an invalid config.
# shellcheck disable=SC2050
if [ "@is_kyber@" = "true" ] && [ -z "$DB_CONNECTION" ]; then
echo "error: DATABASE_URL is required on Kyber when using postgres, but no non-empty value was provided." >&2
echo "Set DATABASE_URL in ${ENV_FILE} or provide a non-empty Nix database connection string." >&2
exit 1
fi

Copilot uses AI. Check for mistakes.
@sed@ \
-e "s|__DATABASE_MODE__|@database_mode@|g" \
-e "s|__DATABASE_CONNECTION_STRING__|@database_connection_string@|g" \
-e "s|__DATABASE_CONNECTION_STRING__|${DB_CONNECTION}|g" \

@cubic-dev-ai cubic-dev-ai Bot Apr 4, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2: Escape DB_CONNECTION before using it in the sed replacement; unescaped &/| can corrupt the rendered database URL in config.json.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At config/paperclip/hydrate.sh, line 24:

<comment>Escape `DB_CONNECTION` before using it in the sed replacement; unescaped `&`/`|` can corrupt the rendered database URL in `config.json`.</comment>

<file context>
@@ -4,26 +4,38 @@ set -euo pipefail
 @sed@ \
   -e "s|__DATABASE_MODE__|@database_mode@|g" \
-  -e "s|__DATABASE_CONNECTION_STRING__|@database_connection_string@|g" \
+  -e "s|__DATABASE_CONNECTION_STRING__|${DB_CONNECTION}|g" \
   -e "s|__DEPLOYMENT_MODE__|@deployment_mode@|g" \
   -e "s|__HOST__|@host@|g" \
</file context>
Suggested change
-e "s|__DATABASE_CONNECTION_STRING__|${DB_CONNECTION}|g" \
-e "s|__DATABASE_CONNECTION_STRING__|$(printf '%s' "$DB_CONNECTION" | sed 's/[&|]/\\&/g')|g" \
Fix with Cubic

-e "s|__DEPLOYMENT_MODE__|@deployment_mode@|g" \
Comment on lines 22 to 25

Copilot AI Apr 4, 2026

Copy link

Choose a reason for hiding this comment

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

DB_CONNECTION is interpolated directly into the sed replacement. If DATABASE_URL contains &, backslashes, or the delimiter, sed will treat those specially and can corrupt the generated JSON (e.g., query params like ...&sslmode=...). Escape the replacement value (or switch to a templating approach that safely JSON-encodes the value) before substituting into the template.

Copilot uses AI. Check for mistakes.
-e "s|__HOST__|@host@|g" \
-e "s|__ALLOWED_HOSTNAME__|@allowed_hostname@|g" \
"$TEMPLATE" >"$CONFIG"
chmod 600 "$CONFIG"

# Create paperclip database on docker-postgres if needed
# Create paperclip database on k8s postgres if needed
# @is_kyber@ is substituted by Nix replaceVars
# shellcheck disable=SC2050
if [ "@is_kyber@" = "true" ]; then
if command -v docker >/dev/null 2>&1 && docker container inspect postgres >/dev/null 2>&1; then
if ! docker exec postgres psql -U postgres -lqt | cut -d \| -f 1 | grep -qw paperclip; then
docker exec postgres createdb -U postgres paperclip
echo "Created paperclip database" >&2
if [ "@is_kyber@" = "true" ] && [ -n "$DB_CONNECTION" ]; then
if command -v psql >/dev/null 2>&1; then
DB_BASE="${DB_CONNECTION%/*}/postgres"

@cubic-dev-ai cubic-dev-ai Bot Apr 4, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1: Preserve URL query parameters when rewriting the DB name to /postgres; currently required options can be dropped from DB_BASE.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At config/paperclip/hydrate.sh, line 36:

<comment>Preserve URL query parameters when rewriting the DB name to `/postgres`; currently required options can be dropped from `DB_BASE`.</comment>

<file context>
@@ -4,26 +4,38 @@ set -euo pipefail
-      echo "Created paperclip database" >&2
+if [ "@is_kyber@" = "true" ] && [ -n "$DB_CONNECTION" ]; then
+  if command -v psql >/dev/null 2>&1; then
+    DB_BASE="${DB_CONNECTION%/*}/postgres"
+    if ! psql "$DB_BASE" -tAc "SELECT 1 FROM pg_database WHERE datname='paperclip'" 2>/dev/null | grep -q 1; then
+      psql "$DB_BASE" -c "CREATE DATABASE paperclip" 2>/dev/null && echo "Created paperclip database" >&2
</file context>
Suggested change
DB_BASE="${DB_CONNECTION%/*}/postgres"
DB_NO_QUERY="${DB_CONNECTION%%\?*}"
DB_QUERY="${DB_CONNECTION#"$DB_NO_QUERY"}"
DB_BASE="${DB_NO_QUERY%/*}/postgres${DB_QUERY}"
Fix with Cubic

Copilot AI Apr 4, 2026

Copy link

Choose a reason for hiding this comment

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

DB_BASE="${DB_CONNECTION%/*}/postgres" drops any query string/options from DATABASE_URL (e.g., ?sslmode=require), which can make the subsequent psql connection fail even though the main connection string works. Consider preserving connection options when deriving the base DB connection (or parse the URL more robustly).

Suggested change
DB_BASE="${DB_CONNECTION%/*}/postgres"
DB_QUERY=""
DB_WITHOUT_QUERY="$DB_CONNECTION"
if [ "${DB_CONNECTION#*\?}" != "$DB_CONNECTION" ]; then
DB_QUERY="?${DB_CONNECTION#*\?}"
DB_WITHOUT_QUERY="${DB_CONNECTION%%\?*}"
fi
DB_BASE="${DB_WITHOUT_QUERY%/*}/postgres${DB_QUERY}"

Copilot uses AI. Check for mistakes.
if ! psql "$DB_BASE" -tAc "SELECT 1 FROM pg_database WHERE datname='paperclip'" 2>/dev/null | grep -q 1; then
psql "$DB_BASE" -c "CREATE DATABASE paperclip" 2>/dev/null && echo "Created paperclip database" >&2
Comment on lines +36 to +38

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The logic for constructing DB_BASE by stripping the last component of the URL is fragile. If the DATABASE_URL contains query parameters (e.g., ?sslmode=require), they will be lost when appending /postgres, which might cause connection failures if the server requires those parameters to connect.

A more robust approach is to use the full connection string and override the database name using the -d flag in psql. According to the psql documentation, the -d flag overrides the database name specified in a connection URI, preserving all other parameters like host, port, and options.

Suggested change
DB_BASE="${DB_CONNECTION%/*}/postgres"
if ! psql "$DB_BASE" -tAc "SELECT 1 FROM pg_database WHERE datname='paperclip'" 2>/dev/null | grep -q 1; then
psql "$DB_BASE" -c "CREATE DATABASE paperclip" 2>/dev/null && echo "Created paperclip database" >&2
if ! psql "$DB_CONNECTION" -d postgres -tAc "SELECT 1 FROM pg_database WHERE datname='paperclip'" 2>/dev/null | grep -q 1; then
psql "$DB_CONNECTION" -d postgres -c "CREATE DATABASE paperclip" 2>/dev/null && echo "Created paperclip database" >&2
fi

fi

Copilot AI Apr 4, 2026

Copy link

Choose a reason for hiding this comment

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

If psql is not installed, database auto-provisioning is silently skipped. Since this PR relies on provisioning against the k8s Postgres, consider emitting a warning when @is_kyber@ is true but psql is missing (or ensure psql is available via the Nix environment) so failures are discoverable.

Suggested change
fi
fi
else
echo "Warning: psql is not installed; skipping automatic paperclip database provisioning for kyber deployment" >&2

Copilot uses AI. Check for mistakes.
fi
fi
7 changes: 1 addition & 6 deletions home-manager/modules/paperclip/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
let
inherit (inputs) host;
homeDir = config.home.homeDirectory;
instanceDir = "${homeDir}/.paperclip/instances/default";
in
# Only enable on kyber (server host)
lib.mkIf host.isKyber {
Expand All @@ -22,12 +21,8 @@ lib.mkIf host.isKyber {
systemd.user.services.paperclip = {
Unit = {
Description = "Paperclip AI agent orchestration platform";
After = [
"network-online.target"
"docker-postgres.service"
];
After = [ "network-online.target" ];
Wants = [ "network-online.target" ];
Requires = [ "docker-postgres.service" ];
};
Service = {
Type = "simple";
Expand Down
21 changes: 14 additions & 7 deletions spec/paperclip_hydrate_spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ The output should include 'set -euo pipefail'
End
End

Describe 'secret loading'
It 'reads DATABASE_URL from dotfiles .env'
When run bash -c "grep 'DATABASE_URL' '$SCRIPT'"
The output should include 'DATABASE_URL'
End

It 'sources the env file'
When run bash -c "grep 'ENV_FILE' '$SCRIPT'"
The output should include 'dotfiles/.env'
End
End

Describe 'config generation'
It 'uses sed to substitute values in template'
When run bash -c "grep '@sed@' '$SCRIPT'"
Expand Down Expand Up @@ -44,13 +56,8 @@ End
End

Describe 'database provisioning'
It 'creates paperclip database on docker-postgres'
When run bash -c "grep 'createdb' '$SCRIPT'"
The output should include 'paperclip'
End

It 'checks if database already exists before creating'
When run bash -c "grep 'grep -qw paperclip' '$SCRIPT'"
It 'creates paperclip database if missing'
When run bash -c "grep 'CREATE DATABASE' '$SCRIPT'"

@cubic-dev-ai cubic-dev-ai Bot Apr 4, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2: This assertion doesn't actually test the "if missing" condition; it only checks that a CREATE DATABASE statement exists. The spec can pass even if database creation is unconditional.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At spec/paperclip_hydrate_spec.sh, line 60:

<comment>This assertion doesn't actually test the "if missing" condition; it only checks that a CREATE DATABASE statement exists. The spec can pass even if database creation is unconditional.</comment>

<file context>
@@ -44,13 +56,8 @@ End
-It 'checks if database already exists before creating'
-When run bash -c "grep 'grep -qw paperclip' '$SCRIPT'"
+It 'creates paperclip database if missing'
+When run bash -c "grep 'CREATE DATABASE' '$SCRIPT'"
 The output should include 'paperclip'
 End
</file context>
Fix with Cubic

The output should include 'paperclip'
End

Expand Down
Loading