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
1 change: 1 addition & 0 deletions home-manager/services/cliproxyapi/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ let
startScript = pkgs.replaceVars ./scripts/start.sh {
aws = "${pkgs.awscli2}/bin/aws";
sed = "${pkgs.gnused}/bin/sed";
rsync = "${pkgs.rsync}/bin/rsync";
};

# Wrapper script that runs start.sh with docker group permissions
Expand Down
7 changes: 7 additions & 0 deletions home-manager/services/cliproxyapi/scripts/backup-auth.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ BACKUP_DIR="s3://cliproxyapi/backup/auths/"
MAIN_DIR="s3://cliproxyapi/auths/"
AUTH_DIR="$CONFIG_DIR/objectstore/auths"
CCS_AUTH_DIR="$HOME/.ccs/cliproxy/auth"
DOTFILES_AUTH_DIR="$HOME/dotfiles/objectstore/auths"

# STEP 1: Pull from R2 to local (captures files created by cliproxyapi directly in R2)
mkdir -p "$AUTH_DIR"
Expand Down Expand Up @@ -37,6 +38,12 @@ if [ -d "$CCS_AUTH_DIR" ] && [ -n "$(ls -A "$CCS_AUTH_DIR" 2>/dev/null)" ]; then
echo "✅ Synced from ccs auth dir to local cache" >&2
fi

# STEP 3: Recover missing files from git-tracked dotfiles backup (macOS only; Linux relies on R2)

Copilot AI Jan 1, 2026

Copy link

Choose a reason for hiding this comment

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

The comment labels the recovery step as "STEP 3" but the subsequent sync operations (lines 47-77) are not labeled as "STEP 4". For consistency with the existing step numbering scheme (STEP 1 and STEP 2), consider adding a "# STEP 4:" comment before line 47 or removing the step numbering from the new STEP 3 comment.

Copilot uses AI. Check for mistakes.
if [ "$(uname)" = "Darwin" ] && [ -d "$DOTFILES_AUTH_DIR" ] && [ -n "$(ls -A "$DOTFILES_AUTH_DIR" 2>/dev/null)" ]; then
@rsync@ -a --ignore-existing "$DOTFILES_AUTH_DIR/" "$AUTH_DIR/"
echo "✅ Recovered missing auths from dotfiles backup (macOS)" >&2
Comment on lines +43 to +44

Copilot AI Jan 1, 2026

Copy link

Choose a reason for hiding this comment

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

The rsync command lacks error handling, which could cause the script to exit due to set -euo pipefail at line 5. If rsync fails for any reason (permissions, I/O errors, etc.), the backup process will fail. Consider adding error handling similar to the R2 operations, for example: @rsync@ -a --ignore-existing "$DOTFILES_AUTH_DIR/" "$AUTH_DIR/" && echo "✅ Recovered missing auths from dotfiles backup (macOS)" >&2 || echo "⚠️ Failed to recover from dotfiles backup" >&2 || true

Copilot uses AI. Check for mistakes.
fi
Comment on lines +42 to +45

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

This logic block is very similar to the one in start.sh (lines 54-57). To avoid code duplication and improve maintainability, consider extracting this logic into a common helper script. The main difference is the success message, which could be passed as an argument or handled by the calling script.

Additionally, using ls to check if a directory is empty is not fully robust, as it can misbehave with certain filenames. A more reliable method is to use find.

Suggested change
if [ "$(uname)" = "Darwin" ] && [ -d "$DOTFILES_AUTH_DIR" ] && [ -n "$(ls -A "$DOTFILES_AUTH_DIR" 2>/dev/null)" ]; then
@rsync@ -a --ignore-existing "$DOTFILES_AUTH_DIR/" "$AUTH_DIR/"
echo "✅ Recovered missing auths from dotfiles backup (macOS)" >&2
fi
if [ "$(uname)" = "Darwin" ] && [ -d "$DOTFILES_AUTH_DIR" ] && [ -n "$(find "$DOTFILES_AUTH_DIR" -mindepth 1 -maxdepth 1 -print -quit)" ]; then
@rsync@ -a --ignore-existing "$DOTFILES_AUTH_DIR/" "$AUTH_DIR/"
echo "✅ Recovered missing auths from dotfiles backup (macOS)" >&2
fi


# Check if auth directory has files
if [ -d "$AUTH_DIR" ] && [ -n "$(ls -A "$AUTH_DIR" 2>/dev/null)" ]; then
echo "Syncing auth files to R2..." >&2
Expand Down
7 changes: 7 additions & 0 deletions home-manager/services/cliproxyapi/scripts/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CONFIG_DIR="$HOME/.cli-proxy-api"
TEMPLATE="$CONFIG_DIR/config.template.yaml"
CONFIG="$CONFIG_DIR/config.yaml"
AUTH_DIR="$CONFIG_DIR/objectstore/auths"
DOTFILES_AUTH_DIR="$HOME/dotfiles/objectstore/auths"
# Use explicit path since $HOME may not be set correctly in launchd context
ENV_FILE="${HOME:-/Users/shunkakinoki}/dotfiles/.env"

Expand Down Expand Up @@ -49,6 +50,12 @@ if [ -n "${OBJECTSTORE_ENDPOINT:-}" ] && [ -n "${OBJECTSTORE_ACCESS_KEY:-}" ]; t
"s3://cliproxyapi/backup/auths/" \
"$AUTH_DIR/" 2>/dev/null && echo "✅ Pulled from R2 backup/auths/" >&2 || true

# Recover missing files from git-tracked dotfiles backup (macOS only; Linux Docker uses R2)
if [ "$(uname)" = "Darwin" ] && [ -d "$DOTFILES_AUTH_DIR" ] && [ -n "$(ls -A "$DOTFILES_AUTH_DIR" 2>/dev/null)" ]; then

@cubic-dev-ai cubic-dev-ai Bot Jan 1, 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: The dotfiles recovery logic is nested inside the objectstore credentials conditional block. This means recovery from dotfiles backup will never run on macOS when OBJECTSTORE_ENDPOINT or OBJECTSTORE_ACCESS_KEY are unset - precisely the clean/local-only setup scenario where this fallback recovery is most needed. Consider moving this block outside the objectstore credentials gate so it can bootstrap auth files even when R2 credentials are missing.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At home-manager/services/cliproxyapi/scripts/start.sh, line 54:

<comment>The dotfiles recovery logic is nested inside the objectstore credentials conditional block. This means recovery from dotfiles backup will never run on macOS when `OBJECTSTORE_ENDPOINT` or `OBJECTSTORE_ACCESS_KEY` are unset - precisely the clean/local-only setup scenario where this fallback recovery is most needed. Consider moving this block outside the objectstore credentials gate so it can bootstrap auth files even when R2 credentials are missing.</comment>

<file context>
@@ -49,6 +50,12 @@ if [ -n &quot;${OBJECTSTORE_ENDPOINT:-}&quot; ] &amp;&amp; [ -n &quot;${OBJECTSTORE_ACCESS_KEY:-}&quot; ]; t
     &quot;$AUTH_DIR/&quot; 2&gt;/dev/null &amp;&amp; echo &quot;✅ Pulled from R2 backup/auths/&quot; &gt;&amp;2 || true
 
+  # Recover missing files from git-tracked dotfiles backup (macOS only; Linux Docker uses R2)
+  if [ &quot;$(uname)&quot; = &quot;Darwin&quot; ] &amp;&amp; [ -d &quot;$DOTFILES_AUTH_DIR&quot; ] &amp;&amp; [ -n &quot;$(ls -A &quot;$DOTFILES_AUTH_DIR&quot; 2&gt;/dev/null)&quot; ]; then
+    @rsync@ -a --ignore-existing &quot;$DOTFILES_AUTH_DIR/&quot; &quot;$AUTH_DIR/&quot;
+    echo &quot;✅ Bootstrapped auth files from dotfiles backup (macOS)&quot; &gt;&amp;2
</file context>
Fix with Cubic

@rsync@ -a --ignore-existing "$DOTFILES_AUTH_DIR/" "$AUTH_DIR/"
echo "✅ Bootstrapped auth files from dotfiles backup (macOS)" >&2
Comment on lines +53 to +56

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Run dotfiles recovery even without objectstore creds

Because the dotfiles recovery block is nested inside the if [ -n "${OBJECTSTORE_ENDPOINT:-}" ] && [ -n "${OBJECTSTORE_ACCESS_KEY:-}" ] check, it never runs on macOS when objectstore credentials are unset. That defeats the new “recover from dotfiles backup” path for clean/local-only setups (exactly when you’d want it), leaving AUTH_DIR empty and causing downstream auth lookups to fail until another sync runs. Consider moving the dotfiles recovery outside the objectstore gate or relaxing the condition so it can seed auth files even when R2 creds are missing.

Useful? React with 👍 / 👎.

Comment on lines +55 to +56

@cubic-dev-ai cubic-dev-ai Bot Jan 1, 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: Missing error handling for rsync command. Under set -euo pipefail, if rsync fails (permission issues, disk full, etc.), the entire startup script will exit and the service won't start. This should follow the same pattern as the aws s3 sync commands with || true to gracefully continue on failure.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At home-manager/services/cliproxyapi/scripts/start.sh, line 55:

<comment>Missing error handling for rsync command. Under `set -euo pipefail`, if rsync fails (permission issues, disk full, etc.), the entire startup script will exit and the service won&#39;t start. This should follow the same pattern as the aws s3 sync commands with `|| true` to gracefully continue on failure.</comment>

<file context>
@@ -49,6 +50,12 @@ if [ -n &quot;${OBJECTSTORE_ENDPOINT:-}&quot; ] &amp;&amp; [ -n &quot;${OBJECTSTORE_ACCESS_KEY:-}&quot; ]; t
 
+  # Recover missing files from git-tracked dotfiles backup (macOS only; Linux Docker uses R2)
+  if [ &quot;$(uname)&quot; = &quot;Darwin&quot; ] &amp;&amp; [ -d &quot;$DOTFILES_AUTH_DIR&quot; ] &amp;&amp; [ -n &quot;$(ls -A &quot;$DOTFILES_AUTH_DIR&quot; 2&gt;/dev/null)&quot; ]; then
+    @rsync@ -a --ignore-existing &quot;$DOTFILES_AUTH_DIR/&quot; &quot;$AUTH_DIR/&quot;
+    echo &quot;✅ Bootstrapped auth files from dotfiles backup (macOS)&quot; &gt;&amp;2
+  fi
</file context>
Suggested change
@rsync@ -a --ignore-existing "$DOTFILES_AUTH_DIR/" "$AUTH_DIR/"
echo "✅ Bootstrapped auth files from dotfiles backup (macOS)" >&2
@rsync@ -a --ignore-existing "$DOTFILES_AUTH_DIR/" "$AUTH_DIR/" && echo "✅ Bootstrapped auth files from dotfiles backup (macOS)" >&2 || true
Fix with Cubic

fi

Comment on lines +55 to +58

Copilot AI Jan 1, 2026

Copy link

Choose a reason for hiding this comment

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

The rsync command lacks error handling, which could cause the script to exit due to set -euo pipefail at line 5. If rsync fails for any reason (permissions, I/O errors, etc.), the entire startup process will fail. Consider adding error handling similar to the R2 operations, for example: @rsync@ -a --ignore-existing "$DOTFILES_AUTH_DIR/" "$AUTH_DIR/" && echo "✅ Bootstrapped auth files from dotfiles backup (macOS)" >&2 || echo "⚠️ Failed to bootstrap from dotfiles backup" >&2 || true

Suggested change
@rsync@ -a --ignore-existing "$DOTFILES_AUTH_DIR/" "$AUTH_DIR/"
echo "✅ Bootstrapped auth files from dotfiles backup (macOS)" >&2
fi
@rsync@ -a --ignore-existing "$DOTFILES_AUTH_DIR/" "$AUTH_DIR/" && echo "✅ Bootstrapped auth files from dotfiles backup (macOS)" >&2 || echo "⚠️ Failed to bootstrap from dotfiles backup" >&2 || true
fi

Copilot uses AI. Check for mistakes.
# CRITICAL: Always sync local auth files back to R2 after pulling
# This ensures any files that exist locally but not in R2 get uploaded,
# preventing "key does not exist" errors when cliproxyapi reads from object storage
Expand Down
Loading