interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / tools / quietlock / install.sh

131 lines · 5886 bytes Raw Blame History
1#!/bin/sh
2# SPDX-License-Identifier: Apache-2.0
3# Copyright 2026 Drake Stapleton (AIEN / aien-dev)
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# tools/quietlock/install.sh -- install quietlock for the hive (HD-13). Run by the queen, not by workers.
18#
19# sh tools/quietlock/install.sh [--dry-run]
20#
21# Installs three things as ONE transaction:
22# $HOME/.local/bin/quietlock built from tools/quietlock/quietlock.c
23# $HOME/.claude/hooks/quiet-guard.sh copied from tools/quietlock/quiet-guard.sh
24# $HOME/.claude/skills/orchestrate-lanes/lanes.sh patched with tools/quietlock/lanes-quietlock.patch
25#
26# Order:
27# 1. preflight: every source exists, patch(1) present, the patch applies to the LIVE lanes.sh
28# (patch -F 0 --dry-run, no fuzz; refuses if lanes.sh changed since the patch was made), the binary builds.
29# All new files are staged as <target>.tmp.<pid> next to their targets. Nothing live changes.
30# 2. backups: each existing target is copied to <target>.bak.<UTC stamp>.
31# 3. swap: binary, hook, lanes.sh are renamed into place.
32# 4. post-checks. If any step of 3-4 fails, every target is restored from its backup
33# (or removed, if it did not exist before), so the hook and lanes.sh are never half-updated.
34# Temp, .rej and .orig files are always removed on exit. Never touches the quiet flag itself.
35# --dry-run runs step 1 only (it builds into a temp file and deletes it) and changes nothing.
36# Test hook: QUIETLOCK_INSTALL_FAILPOINT=build|patch|swap-hook|swap-lanes|postcheck forces a failure there.
37# POSIX sh; needs cc (or $CC), patch, cmp.
38set -u
39DRY=0
40case "${1:-}" in
41 --dry-run) DRY=1 ;;
42 "") ;;
43 *) echo "usage: sh tools/quietlock/install.sh [--dry-run]" >&2; exit 2 ;;
44esac
45HERE=$(cd "$(dirname "$0")" && pwd)
46SRC=$HERE/quietlock.c
47HOOK_SRC=$HERE/quiet-guard.sh
48PATCH=$HERE/lanes-quietlock.patch
49BIN=$HOME/.local/bin/quietlock
50HOOK=$HOME/.claude/hooks/quiet-guard.sh
51LANES=$HOME/.claude/skills/orchestrate-lanes/lanes.sh
52MARK='HD-13: the quiet flag is a real lock'
53STAMP=$(date -u +%Y%m%dT%H%M%SZ)
54CC=${CC:-cc}
55FP=${QUIETLOCK_INSTALL_FAILPOINT:-}
56T=tmp.$$
57
58say() { echo "install: $*"; }
59fp() { [ "$FP" = "$1" ] && { say "FAILPOINT $1"; return 1; }; return 0; }
60cleanup() {
61 rm -f "$BIN.$T" "$HOOK.$T" "$LANES.$T" "$LANES.$T.rej" "$LANES.$T.orig"
62}
63trap cleanup EXIT
64trap 'exit 130' INT TERM HUP
65
66# ---- 1. preflight (nothing live changes) ----
67for f in "$SRC" "$HOOK_SRC" "$PATCH" "$LANES"; do
68 [ -f "$f" ] || { say "REFUSED: missing $f. Nothing changed."; exit 1; }
69done
70command -v patch >/dev/null 2>&1 || { say "REFUSED: patch(1) not found. Nothing changed."; exit 1; }
71if grep -qF "$MARK" "$LANES"; then
72 LANES_ACTION=skip
73 say "lanes.sh already patched; leaving it"
74elif patch -F 0 --dry-run -s -o /dev/null "$LANES" < "$PATCH" >/dev/null 2>&1; then
75 LANES_ACTION=apply
76 say "lanes.sh patch applies cleanly to the live file"
77else
78 say "REFUSED: lanes-quietlock.patch does not apply to $LANES (lanes.sh changed since the patch was made). Nothing changed."
79 exit 1
80fi
81mkdir -p "$(dirname "$BIN")" "$(dirname "$HOOK")" || { say "REFUSED: cannot create target dirs. Nothing changed."; exit 1; }
82fp build && "$CC" -std=gnu11 -Wall -Wextra -Werror -O2 -D_GNU_SOURCE -o "$BIN.$T" "$SRC" ||
83 { say "REFUSED: quietlock.c does not build. Nothing changed."; exit 1; }
84{ cp "$HOOK_SRC" "$HOOK.$T" && chmod +x "$HOOK.$T"; } || { say "REFUSED: cannot stage hook. Nothing changed."; exit 1; }
85if [ "$LANES_ACTION" = apply ]; then
86 { cp -p "$LANES" "$LANES.$T" && fp patch && patch -F 0 -s -r "$LANES.$T.rej" "$LANES.$T" < "$PATCH" >/dev/null 2>&1; } ||
87 { say "REFUSED: patching the staged lanes.sh copy failed. Nothing changed."; exit 1; }
88fi
89if [ "$DRY" = 1 ]; then
90 say "DRY-RUN complete: patch applies, binary builds, hook staged; nothing changed"
91 exit 0
92fi
93
94# ---- 2. backups ----
95NEW_BIN=1
96NEW_HOOK=1
97if [ -f "$BIN" ]; then
98 cp -p "$BIN" "$BIN.bak.$STAMP" || { say "REFUSED: backup failed. Nothing changed."; exit 1; }
99 NEW_BIN=0
100fi
101if [ -f "$HOOK" ]; then
102 cp -p "$HOOK" "$HOOK.bak.$STAMP" || { say "REFUSED: backup failed. Nothing changed."; exit 1; }
103 NEW_HOOK=0
104fi
105cp -p "$LANES" "$LANES.bak.$STAMP" || { say "REFUSED: backup failed. Nothing changed."; exit 1; }
106
107rollback() {
108 say "ROLLBACK: $1; restoring every target from its backup"
109 if [ "$NEW_BIN" = 1 ]; then rm -f "$BIN"; else cp -p "$BIN.bak.$STAMP" "$BIN"; fi
110 if [ "$NEW_HOOK" = 1 ]; then rm -f "$HOOK"; else cp -p "$HOOK.bak.$STAMP" "$HOOK"; fi
111 cp -p "$LANES.bak.$STAMP" "$LANES"
112 say "rolled back; live files are as before (backups kept as *.bak.$STAMP)"
113 exit 1
114}
115
116# ---- 3. swap ----
117mv -f "$BIN.$T" "$BIN" || rollback "binary swap failed"
118{ fp swap-hook && mv -f "$HOOK.$T" "$HOOK"; } || rollback "hook swap failed"
119if [ "$LANES_ACTION" = apply ]; then
120 { fp swap-lanes && mv -f "$LANES.$T" "$LANES"; } || rollback "lanes.sh swap failed"
121fi
122
123# ---- 4. post-checks ----
124"$BIN" check >/dev/null 2>&1
125rc=$?
126[ "$rc" = 0 ] || [ "$rc" = 75 ] || rollback "installed quietlock does not run (check exit $rc)"
127cmp -s "$HOOK_SRC" "$HOOK" || rollback "hook copy differs from source"
128grep -qF "$MARK" "$LANES" || rollback "lanes.sh is not patched"
129if grep -q 'rm -f "\$FLAG"' "$LANES"; then rollback "lanes.sh still removes the flag with rm"; fi
130fp postcheck || rollback "postcheck failpoint"
131say "done: $BIN, $HOOK, $LANES (backups *.bak.$STAMP)"