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
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 2026-01-16 - [Shell Dependency Check Overhead]
**Learning:** `lib/core/common.sh` functions like `safe_json_get_key` re-check dependencies (`command -v jq`) on every invocation. In tight loops, this adds significant overhead (~13% in micro-benchmark).
**Action:** Cache dependency checks in global variables (e.g., `_JSON_PROCESSOR_CACHE`) when the script is sourced, rather than checking inside hot functions.
## 2025-02-18 - [Bash Subshell Caching]
**Learning:** Caching detection results in a shell function (e.g. `get_json_processor`) is ineffective if the function is commonly called inside command substitution `$(...)`, as variables set in the subshell are lost.
**Action:** Detect and export the cached value at the library source time (parent shell) so subshells inherit it.
Expand Down
33 changes: 25 additions & 8 deletions lib/core/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,16 @@ safe_json_get_key() {
return 1
fi

_detect_json_processor
local processor="$_JSON_PROCESSOR_CACHE"

if [ "$processor" = "none" ]; then
echo "$default"
return 1
local processor="${_JSON_PROCESSOR_CACHE:-}"
if [ -z "$processor" ]; then
if command -v jq &> /dev/null; then
processor="jq"
elif command -v python3 &> /dev/null; then
processor="python3"
else
echo "$default"
return 1
fi
fi

if [ "$processor" = "jq" ]; then
Expand Down Expand Up @@ -132,8 +136,13 @@ safe_json_write() {
if echo "$json_content" > "$temp_file" 2>&1; then
# Validate JSON before moving
local validation_failed=0
_detect_json_processor
local processor="$_JSON_PROCESSOR_CACHE"
local processor="${_JSON_PROCESSOR_CACHE:-}"

# Fallback if not cached
if [ -z "$processor" ]; then
if command_exists jq; then processor="jq";
elif command_exists python3; then processor="python3"; fi
fi

if [ "$processor" = "jq" ]; then
if ! jq . "$temp_file" > /dev/null 2>&1; then
Expand Down Expand Up @@ -248,5 +257,13 @@ get_json_processor() {
return 0
}

# Initialize JSON processor cache when sourced
if [ -z "${_JSON_PROCESSOR_CACHE:-}" ]; then
if command_exists jq; then
export _JSON_PROCESSOR_CACHE="jq"
elif command_exists python3; then
export _JSON_PROCESSOR_CACHE="python3"
fi
fi
# Auto-detect on source to ensure subshells inherit the cache
_detect_json_processor
Loading