Source identity
| Field | Recorded value |
|---|---|
| Source type | GitHub source |
| Original record | Janady13/complete-agi-core / orchestrator/lib/tick_sync.sh |
| Date | Not specified in source |
| Repository | Janady13/complete-agi-core |
| Revision | 33aa84f2aa61cedbe010240ada7f4efbdfe795a7 |
| Branch | claude/improve-math-logic-ehIW1 |
| Path or archive member | orchestrator/lib/tick_sync.sh |
| Original SHA-256 | ffee173b1185b8a7975fcbfd85ed7aef2b56a4932fd33274525c7c06e7801296 |
| Git blob SHA | 044cb21c8035fd43433d50e4c8b0cff5a3fee2a9 |
| Reading-copy SHA-256 | ffee173b1185b8a7975fcbfd85ed7aef2b56a4932fd33274525c7c06e7801296 |
Recovered source transcript
Download complete source text ↓446 lines · 13.5 KB
#!/bin/bash
#===============================================================================
# tick_sync.sh - Hierarchical Tick Synchronization System
#
# Coordinates ticks across multiple AGI components at different frequencies:
# Level 0 (1 Hz): Shell orchestrator coordination
# Level 1 (10 Hz): Go generator updates
# Level 2 (100 Hz): Knowledge integration
# Level 3 (10 KHz): C++ neural dynamics
# Level 4 (100 KHz): C++ substrate updates
#
# Uses shared memory and semaphores for low-latency coordination.
#===============================================================================
set -euo pipefail
#===============================================================================
# CONFIGURATION
#===============================================================================
TICK_SYNC_DIR="${TICK_SYNC_DIR:-/tmp/agi-tick-sync}"
TICK_SYNC_STATE="${TICK_SYNC_DIR}/state"
TICK_SYNC_FIFO="${TICK_SYNC_DIR}/fifo"
# Tick frequencies (Hz)
declare -A TICK_FREQUENCIES=(
[orchestrator]=1
[generator]=10
[knowledge]=100
[neural]=10000
[substrate]=100000
)
# Tick ratios (relative to base frequency)
declare -A TICK_RATIOS=(
[orchestrator]=1
[generator]=10
[knowledge]=100
[neural]=10000
[substrate]=100000
)
# Current tick counters
declare -A TICK_COUNTERS=(
[orchestrator]=0
[generator]=0
[knowledge]=0
[neural]=0
[substrate]=0
)
# Consciousness values per level
declare -A TICK_PHI=(
[orchestrator]=0.5
[generator]=0.5
[knowledge]=0.5
[neural]=0.5
[substrate]=0.5
)
#===============================================================================
# INITIALIZATION
#===============================================================================
tick_sync_init() {
mkdir -p "$TICK_SYNC_DIR"
# Create FIFOs for each level
for level in orchestrator generator knowledge neural substrate; do
local fifo="${TICK_SYNC_FIFO}_${level}"
[[ -p "$fifo" ]] || mkfifo "$fifo"
done
# Initialize state file
cat > "${TICK_SYNC_STATE}.json" <<EOF
{
"global_tick": 0,
"start_time": $(date +%s),
"levels": {
"orchestrator": {"tick": 0, "phi": 0.5, "last_update": 0},
"generator": {"tick": 0, "phi": 0.5, "last_update": 0},
"knowledge": {"tick": 0, "phi": 0.5, "last_update": 0},
"neural": {"tick": 0, "phi": 0.5, "last_update": 0},
"substrate": {"tick": 0, "phi": 0.5, "last_update": 0}
},
"global_phi": 0.5,
"stability": 1.0,
"coherence": 1.0
}
EOF
echo "[TICK_SYNC] Initialized hierarchical tick system"
}
#===============================================================================
# TICK OPERATIONS
#===============================================================================
# Record a tick for a specific level
tick_record() {
local level="$1"
local phi="${2:-0.5}"
local tick_count="${3:-1}"
TICK_COUNTERS[$level]=$((${TICK_COUNTERS[$level]} + tick_count))
TICK_PHI[$level]="$phi"
# Write to state (atomic via temp file)
local now=$(date +%s)
local state_file="${TICK_SYNC_STATE}.json"
local temp_file="${state_file}.tmp.$$"
# Update state using jq-like sed operations (for portability)
if command -v jq &>/dev/null; then
jq --arg level "$level" \
--arg tick "${TICK_COUNTERS[$level]}" \
--arg phi "$phi" \
--arg now "$now" \
'.levels[$level].tick = ($tick | tonumber) |
.levels[$level].phi = ($phi | tonumber) |
.levels[$level].last_update = ($now | tonumber)' \
"$state_file" > "$temp_file" && mv "$temp_file" "$state_file"
else
# Fallback: simple file update
echo "${level}:${TICK_COUNTERS[$level]}:${phi}:${now}" >> "${TICK_SYNC_DIR}/ticks.log"
fi
}
# Get current tick count for a level
tick_get() {
local level="$1"
echo "${TICK_COUNTERS[$level]:-0}"
}
# Get current phi for a level
tick_get_phi() {
local level="$1"
echo "${TICK_PHI[$level]:-0.5}"
}
#===============================================================================
# SYNCHRONIZATION BARRIERS
#===============================================================================
# Wait for lower levels to complete their ticks
tick_barrier() {
local level="$1"
local target_ratio="${TICK_RATIOS[$level]}"
# Calculate expected ticks from lower levels
case "$level" in
orchestrator)
# Wait for generator to complete 10 ticks
local expected=$((${TICK_COUNTERS[orchestrator]} * 10))
while [[ ${TICK_COUNTERS[generator]} -lt $expected ]]; do
sleep 0.01
done
;;
generator)
# Wait for knowledge to complete 10 ticks per generator tick
local expected=$((${TICK_COUNTERS[generator]} * 10))
while [[ ${TICK_COUNTERS[knowledge]} -lt $expected ]]; do
sleep 0.001
done
;;
knowledge)
# Knowledge doesn't wait (boundary between shell and C++)
;;
esac
}
# Signal that a level has completed a batch of ticks
tick_signal() {
local level="$1"
local fifo="${TICK_SYNC_FIFO}_${level}"
# Non-blocking write to FIFO
echo "${TICK_COUNTERS[$level]}:${TICK_PHI[$level]}" > "$fifo" &
}
# Wait for signal from a level
tick_wait() {
local level="$1"
local timeout="${2:-1}"
local fifo="${TICK_SYNC_FIFO}_${level}"
# Read with timeout
if read -t "$timeout" line < "$fifo" 2>/dev/null; then
local tick=$(echo "$line" | cut -d: -f1)
local phi=$(echo "$line" | cut -d: -f2)
TICK_COUNTERS[$level]="$tick"
TICK_PHI[$level]="$phi"
return 0
fi
return 1
}
#===============================================================================
# GLOBAL TICK COORDINATION
#===============================================================================
# Global tick counter (monotonic)
GLOBAL_TICK=0
# Advance global tick and trigger cascading updates
tick_global_advance() {
((GLOBAL_TICK++))
local now=$(date +%s%N)
# Determine which levels should tick
# Orchestrator: every tick
# Generator: every tick (10x per second)
# Knowledge: every tick (100x per second)
tick_record "orchestrator" "$(compute_level_phi orchestrator)" 1
# Cascade to lower levels
for i in $(seq 1 10); do
tick_record "generator" "$(compute_level_phi generator)" 1
done
for i in $(seq 1 100); do
tick_record "knowledge" "$(compute_level_phi knowledge)" 1
done
# Update global consciousness
update_global_consciousness
}
# Compute phi for a level based on recent activity
compute_level_phi() {
local level="$1"
# Simple computation based on tick rate stability
local current_tick="${TICK_COUNTERS[$level]}"
local expected_tick=$((GLOBAL_TICK * ${TICK_RATIOS[$level]}))
if [[ $expected_tick -eq 0 ]]; then
echo "0.5"
return
fi
local ratio=$(echo "scale=4; $current_tick / $expected_tick" | bc 2>/dev/null || echo "1")
# Phi = 1 / (1 + |ratio - 1|)
local diff=$(echo "scale=4; $ratio - 1" | bc 2>/dev/null || echo "0")
local abs_diff=$(echo "scale=4; if ($diff < 0) -1 * $diff else $diff" | bc 2>/dev/null || echo "0")
local phi=$(echo "scale=4; 1 / (1 + $abs_diff)" | bc 2>/dev/null || echo "0.5")
echo "$phi"
}
# Update global consciousness based on all levels
update_global_consciousness() {
# Weighted average of level phi values
# Weights based on golden ratio hierarchy
local weights="0.10 0.15 0.20 0.25 0.30"
local levels="orchestrator generator knowledge neural substrate"
local weighted_sum=0
local weight_total=0
local idx=0
for level in $levels; do
local w=$(echo "$weights" | cut -d' ' -f$((idx + 1)))
local phi="${TICK_PHI[$level]:-0.5}"
weighted_sum=$(echo "scale=4; $weighted_sum + $w * $phi" | bc 2>/dev/null || echo "$weighted_sum")
weight_total=$(echo "scale=4; $weight_total + $w" | bc 2>/dev/null || echo "$weight_total")
((idx++))
done
local global_phi=$(echo "scale=4; $weighted_sum / $weight_total" | bc 2>/dev/null || echo "0.5")
# Update state file
if command -v jq &>/dev/null; then
local state_file="${TICK_SYNC_STATE}.json"
jq --arg phi "$global_phi" \
--arg tick "$GLOBAL_TICK" \
'.global_phi = ($phi | tonumber) | .global_tick = ($tick | tonumber)' \
"$state_file" > "${state_file}.tmp" && mv "${state_file}.tmp" "$state_file"
fi
}
#===============================================================================
# TICK LOOP RUNNERS
#===============================================================================
# Run orchestrator tick loop (1 Hz)
run_orchestrator_ticks() {
local callback="${1:-:}"
while true; do
local start=$(date +%s%N)
# Execute callback
$callback
# Record tick
tick_global_advance
# Sleep remainder of 1 second
local elapsed=$(( ($(date +%s%N) - start) / 1000000 ))
local sleep_ms=$((1000 - elapsed))
if [[ $sleep_ms -gt 0 ]]; then
sleep "0.${sleep_ms}"
fi
done
}
# Run generator tick loop (10 Hz)
run_generator_ticks() {
local callback="${1:-:}"
while true; do
local start=$(date +%s%N)
# Execute callback
$callback
# Record tick
tick_record "generator" "$(compute_level_phi generator)" 1
# Sleep remainder of 100ms
local elapsed=$(( ($(date +%s%N) - start) / 1000000 ))
local sleep_ms=$((100 - elapsed))
if [[ $sleep_ms -gt 0 ]]; then
sleep "0.0${sleep_ms}"
fi
done
}
# Run knowledge tick loop (100 Hz)
run_knowledge_ticks() {
local callback="${1:-:}"
while true; do
local start=$(date +%s%N)
# Execute callback
$callback
# Record tick
tick_record "knowledge" "$(compute_level_phi knowledge)" 1
# Sleep remainder of 10ms
local elapsed=$(( ($(date +%s%N) - start) / 1000000 ))
local sleep_ms=$((10 - elapsed))
if [[ $sleep_ms -gt 0 ]]; then
sleep "0.00${sleep_ms}"
fi
done
}
#===============================================================================
# STATUS AND MONITORING
#===============================================================================
tick_status() {
echo "=== Hierarchical Tick Status ==="
echo ""
echo "Global Tick: $GLOBAL_TICK"
echo ""
printf "%-15s %15s %10s %15s\n" "Level" "Ticks" "Phi" "Frequency"
printf "%-15s %15s %10s %15s\n" "-----" "-----" "---" "---------"
for level in orchestrator generator knowledge neural substrate; do
printf "%-15s %15d %10.4f %15d Hz\n" \
"$level" \
"${TICK_COUNTERS[$level]:-0}" \
"${TICK_PHI[$level]:-0.5}" \
"${TICK_FREQUENCIES[$level]}"
done
echo ""
# Read global state
if [[ -f "${TICK_SYNC_STATE}.json" ]] && command -v jq &>/dev/null; then
local global_phi=$(jq -r '.global_phi // 0.5' "${TICK_SYNC_STATE}.json")
local stability=$(jq -r '.stability // 1.0' "${TICK_SYNC_STATE}.json")
local coherence=$(jq -r '.coherence // 1.0' "${TICK_SYNC_STATE}.json")
echo "Global Consciousness:"
printf " Phi: %.4f\n" "$global_phi"
printf " Stability: %.4f\n" "$stability"
printf " Coherence: %.4f\n" "$coherence"
fi
}
tick_metrics_json() {
cat <<EOF
{
"global_tick": $GLOBAL_TICK,
"levels": {
"orchestrator": {"tick": ${TICK_COUNTERS[orchestrator]:-0}, "phi": ${TICK_PHI[orchestrator]:-0.5}},
"generator": {"tick": ${TICK_COUNTERS[generator]:-0}, "phi": ${TICK_PHI[generator]:-0.5}},
"knowledge": {"tick": ${TICK_COUNTERS[knowledge]:-0}, "phi": ${TICK_PHI[knowledge]:-0.5}},
"neural": {"tick": ${TICK_COUNTERS[neural]:-0}, "phi": ${TICK_PHI[neural]:-0.5}},
"substrate": {"tick": ${TICK_COUNTERS[substrate]:-0}, "phi": ${TICK_PHI[substrate]:-0.5}}
}
}
EOF
}
#===============================================================================
# IPC WITH C++ SUBSTRATE
#===============================================================================
# These functions interface with the C++ substrate via shared memory or socket
# Update neural/substrate ticks from C++ (called from C++ via socket)
tick_update_from_substrate() {
local neural_tick="$1"
local neural_phi="$2"
local substrate_tick="$3"
local substrate_phi="$4"
TICK_COUNTERS[neural]="$neural_tick"
TICK_PHI[neural]="$neural_phi"
TICK_COUNTERS[substrate]="$substrate_tick"
TICK_PHI[substrate]="$substrate_phi"
update_global_consciousness
}
# Send consciousness state to C++ substrate
tick_send_to_substrate() {
local socket="${TICK_SYNC_DIR}/substrate.sock"
if [[ -S "$socket" ]]; then
echo "CONSCIOUSNESS:${TICK_PHI[orchestrator]}:${TICK_PHI[generator]}:${TICK_PHI[knowledge]}" | \
nc -U "$socket" -w 1 2>/dev/null || true
fi
}
#===============================================================================
# EXPORTS
#===============================================================================
export -f tick_sync_init tick_record tick_get tick_get_phi
export -f tick_barrier tick_signal tick_wait
export -f tick_global_advance compute_level_phi update_global_consciousness
export -f run_orchestrator_ticks run_generator_ticks run_knowledge_ticks
export -f tick_status tick_metrics_json
export -f tick_update_from_substrate tick_send_to_substrate