chore: Sprachmittler OpenCode starter kit

This commit is contained in:
schihei committed 2026-11-26 11:59:38 +01:00
commit 374085ec2f
94 files changed
+4563

No files matched your search

+45
View File
@@ -0,0 +1,45 @@
#!/usr/bin/env bash
# Installs developer tools if missing and checks Ollama, the model and OpenCode. Safe to re-run.
set -euo pipefail
[[ "$(uname -s)" == "Darwin" && "$(uname -m)" == "arm64" ]] || { echo "Sprachmittler requires an Apple Silicon Mac." >&2; exit 1; }
xcode-select -p >/dev/null 2>&1 || { echo "Install Xcode 16+ and run: xcode-select --install" >&2; exit 1; }
if ! command -v brew >/dev/null; then
echo "Homebrew not found: install from https://brew.sh or install the tools below manually." >&2
exit 1
fi
for tool in xcodegen swiftformat swiftlint xcbeautify ripgrep jq; do
bin=$tool; [[ $tool == ripgrep ]] && bin=rg
if command -v "$bin" >/dev/null; then echo "✓ $tool"; else echo "→ installing $tool"; brew install "$tool"; fi
done
if command -v ollama >/dev/null; then
echo "✓ ollama $(ollama --version 2>/dev/null | awk '{print $NF}')"
if curl -fsS --max-time 3 http://127.0.0.1:11434/api/version >/dev/null 2>&1; then
echo "✓ ollama server reachable on 127.0.0.1:11434"
if ollama list 2>/dev/null | grep -qi 'hy-mt3'; then
echo "✓ Hy-MT3 model installed:"
ollama list 2>/dev/null | grep -i 'hy-mt3' | awk '{print " " $1 " " $3 " " $4}'
else
echo "! no Hy-MT3 model installed. Pull one:"
echo " ollama pull hf.co/tencent/Hy-MT3-7B-GGUF:Q4_K_M # about 5.15 GB"
echo " ollama pull hf.co/tencent/Hy-MT3-1.8B-GGUF:Q4_K_M # about 1.35 GB (Fast)"
fi
else
echo "! ollama server not running: start the Ollama app or run 'ollama serve'"
fi
else
echo "! ollama not found: install from https://ollama.com/download"
fi
if command -v opencode >/dev/null; then
v=$(opencode --version 2>/dev/null | grep -Eo '[0-9]+\.[0-9]+\.[0-9]+' | head -1 || true)
if [[ "${v%%.*}" == "2" ]]; then echo "✓ opencode $v"; else echo "! opencode ${v:-unknown}: this kit targets OpenCode 2.x (docs/OPENCODE_V2.md)"; fi
else
echo "! opencode not found: see https://opencode.ai/docs/ (this kit targets 2.x)"
fi
swift --version 2>/dev/null | head -1
echo "Bootstrap complete. Next: make doctor && make generate && make verify"
+70
View File
@@ -0,0 +1,70 @@
#!/usr/bin/env bash
# Records real Ollama responses into test fixtures (T1.1). Synthetic input only (SP-12).
# Usage: scripts/capture-fixtures.sh [base_url]
set -euo pipefail
cd "$(dirname "$0")/.."
BASE="${1:-http://127.0.0.1:11434}"
OUT="Packages/SprachmittlerKit/Tests/SprachmittlerOllamaTests/Fixtures"
mkdir -p "$OUT"
command -v jq >/dev/null || { echo "jq required (brew install jq)" >&2; exit 1; }
j() { curl -sS -H 'Content-Type: application/json' "$@"; }
EN="The quick brown fox jumps over the lazy dog. Please open https://example.com/docs?lang=en and set the limit to {{limit}}."
DE="Größenänderungen und Straßen sind wichtig. Bitte öffne https://example.de/hilfe und setze das Limit auf {{limit}}."
MD="# Release notes
Open **Settings** and set \`max_tokens\` to {{limit}}. See [the docs](https://example.com/docs)."
echo "→ version / tags / ps"
j "$BASE/api/version" | tee "$OUT/version.json" | jq -c .
j "$BASE/api/tags" > "$OUT/tags.json"
j "$BASE/api/ps" > "$OUT/ps.json"
models=$(jq -r '.models[].name' "$OUT/tags.json" | grep -i 'hy-mt3' || true)
[[ -z "$models" ]] && { echo "No Hy-MT3 model installed. See docs/HYMT3_MODEL.md, e.g. ollama pull hf.co/tencent/Hy-MT3-7B-GGUF:Q4_K_M" >&2; exit 1; }
prompt() { # $1 target, $2 text
printf 'Translate the following text into %s. Note that you should **only output the translated result without any additional explanation**:\n\n%s' "$1" "$2"
}
body() { # $1 model, $2 target, $3 text, $4 num_predict
jq -nc --arg m "$1" --arg p "$(prompt "$2" "$3")" --argjson n "$4" \
'{model:$m, messages:[{role:"user",content:$p}], stream:true, keep_alive:"5m",
options:{temperature:0.7, top_p:0.6, top_k:20, repeat_penalty:1.05, num_predict:$n, num_ctx:8192}}'
}
meta_models="[]"
for m in $models; do
safe=$(echo "$m" | sed 's#[/:]#_#g')
echo "→ show $m"
j -X POST "$BASE/api/show" -d "$(jq -nc --arg m "$m" '{model:$m}')" \
| jq '{capabilities, details, parameters, template, model_info: (.model_info // {} | with_entries(select(.key|test("context_length|block_count|embedding_length"))))}' \
> "$OUT/show-$safe.json"
echo " capabilities: $(jq -c '.capabilities' "$OUT/show-$safe.json")"
digest=$(jq -r --arg m "$m" '.models[] | select(.name==$m) | .digest[0:12]' "$OUT/tags.json")
meta_models=$(jq -c --arg m "$m" --arg d "$digest" '. + [{model:$m,digest:$d}]' <<<"$meta_models")
echo "→ chat $m [en→de]"
j -N "$BASE/api/chat" -d "$(body "$m" German "$EN" 1024)" > "$OUT/chat-$safe-en-de.ndjson"
echo "→ chat $m [de→en]"
j -N "$BASE/api/chat" -d "$(body "$m" English "$DE" 1024)" > "$OUT/chat-$safe-de-en.ndjson"
echo "→ chat $m [markdown]"
j -N "$BASE/api/chat" -d "$(body "$m" German "$MD" 1024)" > "$OUT/chat-$safe-markdown.ndjson"
echo "→ chat $m [truncated]"
j -N "$BASE/api/chat" -d "$(body "$m" German "$EN" 16)" > "$OUT/chat-$safe-truncated.ndjson"
echo " done_reason: $(tail -1 "$OUT/chat-$safe-truncated.ndjson" | jq -r '.done_reason // .error')"
done
echo "→ error case"
code=$(curl -sS -o "$OUT/error-missing-model.json" -w '%{http_code}' -H 'Content-Type: application/json' "$BASE/api/chat" \
-d '{"model":"sprachmittler/does-not-exist","messages":[{"role":"user","content":"x"}],"stream":false}')
echo " missing model: HTTP $code"
jq -n --arg date "$(date -u +%Y-%m-%dT%H:%M:%SZ)" --arg hw "$(sysctl -n machdep.cpu.brand_string 2>/dev/null || uname -m)" \
--arg mem "$(sysctl -n hw.memsize 2>/dev/null || echo unknown)" --slurpfile v "$OUT/version.json" --argjson models "$meta_models" \
'{date:$date, ollama:$v[0].version, hardware:$hw, memoryBytes:$mem, input:"synthetic EN/DE/Markdown", models:$models}' > "$OUT/capture-meta.json"
echo "→ checking fixture sizes"
find "$OUT" -type f -size +200k -print | while IFS= read -r f; do echo "! larger than 200 KB: $f"; done
echo "Done. Review $OUT, then close the VERIFY items in docs/OLLAMA_TRANSLATION_API.md."
+20
View File
@@ -0,0 +1,20 @@
#!/usr/bin/env bash
# Regenerates MANIFEST.sha256 for the kit. Excludes generated, private and VCS files.
set -euo pipefail
cd "$(dirname "$0")/.."
{
find . -type f \
-not -path './.git/*' \
-not -path './.build/*' \
-not -path './Packages/SprachmittlerKit/.build/*' \
-not -path './Sprachmittler.xcodeproj/*' \
-not -path './evaluation/corpus/*' \
-not -path './evaluation/results/*' \
-not -name 'MANIFEST.sha256' \
-not -name '.DS_Store' \
| LC_ALL=C sort \
| while IFS= read -r f; do shasum -a 256 "$f"; done
} > MANIFEST.sha256
echo "✓ MANIFEST.sha256: $(wc -l < MANIFEST.sha256 | tr -d ' ') files"
+56
View File
@@ -0,0 +1,56 @@
#!/usr/bin/env bash
# `make doctor`: checks that the OpenCode configuration is native 2.x (docs/OPENCODE_V2.md, ADR-010).
set -uo pipefail
cd "$(dirname "$0")/.."
fail=0
report() { echo "✗ $1"; fail=1; }
if command -v opencode >/dev/null; then
v=$(opencode --version 2>/dev/null | grep -Eo '[0-9]+\.[0-9]+\.[0-9]+' | head -1 || true)
[[ "${v%%.*}" == "2" ]] && echo "✓ opencode $v" || report "opencode ${v:-unknown}: major version must be 2"
else
echo "! opencode not installed: static checks only"
fi
command -v jq >/dev/null || { echo "jq required (brew install jq)" >&2; exit 1; }
jq -e . opencode.json >/dev/null && echo "✓ opencode.json parses" || report "opencode.json is not valid JSON"
# V1 keys that 2.x replaced (https://opencode.ai/docs/migrate-v1)
for k in permission tools temperature; do
jq -e --arg k "$k" 'has($k)' opencode.json >/dev/null 2>&1 && report "opencode.json: V1 key '$k'"
done
jq -e '.permissions | type == "array"' opencode.json >/dev/null 2>&1 || report "opencode.json: 'permissions' must be a list"
jq -e '[.permissions[]? | .action] | index("bash")' opencode.json >/dev/null 2>&1 && report "opencode.json: action 'bash' (V2 uses 'shell')"
# Folder rules: keep a dir/** twin next to every dir/* rule (wildcard depth is not documented)
while IFS= read -r r; do
[[ -z "$r" ]] && continue
jq -e --arg t "${r%\*}**" '[.permissions[]?.resource] | index($t)' opencode.json >/dev/null || report "opencode.json: '$r' has no '${r%\*}**' twin"
done < <(jq -r '.permissions[]?.resource | select(endswith("/*"))' opencode.json)
for f in .opencode/agents/*.md; do
while IFS= read -r r; do
[[ -z "$r" ]] && continue
grep -Fq "resource: \"${r%\*}**\"" "$f" || report "$f: '$r' has no '${r%\*}**' twin"
done < <(grep -Eo 'resource: "[^"]+/\*"' "$f" | sed -E 's/resource: "(.*)"/\1/')
done
for f in .opencode/agents/*.md; do
fm=$(awk 'NR==1&&/^---$/{f=1;next} f&&/^---$/{exit} f' "$f")
echo "$fm" | grep -Eq '^(permission|tools|temperature|bash):' && report "$f: V1 frontmatter key"
echo "$fm" | grep -Eq '^description:' || report "$f: missing description"
done
for f in architect swift-reviewer test-engineer translation-evaluator; do [[ -f .opencode/agents/$f.md ]] || report "missing agent $f"; done
for f in adr status verify plan review next task manual-test evaluate; do [[ -f .opencode/commands/$f.md ]] || report "missing command $f"; done
for s in translation-quality selection-capture; do
f=.opencode/skills/$s/SKILL.md
[[ -f $f ]] || { report "missing skill $s"; continue; }
grep -Eq "^name: $s\$" "$f" || report "$f: name must match folder"
done
if command -v opencode >/dev/null && opencode debug config >/dev/null 2>&1; then
echo "✓ opencode debug config resolves"
elif command -v opencode >/dev/null; then
echo "! 'opencode debug config' unavailable or failed: check manually"
fi
[[ $fail -eq 0 ]] && echo "✓ OpenCode 2.x configuration looks native"
exit $fail
+69
View File
@@ -0,0 +1,69 @@
#!/usr/bin/env bash
# Static checks for the invariants in docs/SECURITY_PRIVACY.md (SP-1 … SP-15).
set -uo pipefail
cd "$(dirname "$0")/.."
PKGSRC="Packages/SprachmittlerKit/Sources"
SRC="$PKGSRC App"
fail=0
if ! command -v rg >/dev/null; then echo "warning: ripgrep (rg) not installed, privacy checks skipped (brew install ripgrep)"; exit 0; fi
report() { echo "✗ $1"; fail=1; }
hits() { rg -n "$@" 2>/dev/null || true; }
# SP-1: no folder watching / background file access
h=$(hits 'FSEventStream|makeFileSystemObjectSource|NSFilePresenter' $SRC)
[[ -n "$h" ]] && { report "SP-1: file watching API found"; echo "$h"; }
# SP-2: selected text, translations, pasteboard strings, app names never logged in clear
h=$(hits 'Log(ger)?\.[a-z]+\(.*\\\((text|translation|prompt|string|pasteboard|app|url|path|fileName)[^)]*\)' $SRC | rg -v 'privacy: *\.private|\.count|\.length' || true)
[[ -n "$h" ]] && { report "SP-2: possible content logging without privacy: .private"; echo "$h"; }
h=$(hits '^\s*(print|debugPrint|NSLog)\(' $SRC -g '!**/sprachmittler-cli/**')
[[ -n "$h" ]] && { report "SP-2: print/NSLog outside the CLI"; echo "$h"; }
# SP-3: no telemetry or crash-reporting SDKs
h=$(hits -i 'firebase|crashlytics|sentry|amplitude|mixpanel|segment\.io|appcenter|bugsnag|datadog' Packages/SprachmittlerKit/Package.swift project.yml $SRC)
[[ -n "$h" ]] && { report "SP-3: telemetry or analytics dependency"; echo "$h"; }
# SP-4: networking only in SprachmittlerOllama
h=$(hits 'URLSession|URLRequest\(' $SRC -g '!**/SprachmittlerOllama/**' -g '!**/SprachmittlerTestSupport/**')
[[ -n "$h" ]] && { report "SP-4: networking outside SprachmittlerOllama"; echo "$h"; }
# SP-5: no arbitrary loads
if rg -n 'NSAllowsArbitraryLoads' App/Info.plist >/dev/null 2>&1; then report "SP-5: NSAllowsArbitraryLoads present"; fi
# SP-6: ephemeral session, no shared session
h=$(hits 'URLSession\.shared|URLSessionConfiguration\.default' $PKGSRC -g '!**/SprachmittlerTestSupport/**')
[[ -n "$h" ]] && { report "SP-6: shared/default URLSession (use .ephemeral)"; echo "$h"; }
# SP-7: no text in defaults or keychain
h=$(hits -i '(UserDefaults|Keychain|SecItemAdd).*(text|translation|source|pasteboard)' $SRC || true)
[[ -n "$h" ]] && { report "SP-7: possible text persistence"; echo "$h"; }
# SP-10: exactly the app-sandbox key, set to false; no unused usage strings
ENT=App/Sprachmittler.entitlements
expected=$'com.apple.security.app-sandbox'
actual=$(rg -o '<key>[^<]+</key>' "$ENT" 2>/dev/null | sed -E 's#</?key>##g' | sort)
[[ "$actual" == "$expected" ]] || { report "SP-10: entitlements differ from the allowed set"; diff <(echo "$expected") <(echo "$actual"); }
rg -U -q '<key>com.apple.security.app-sandbox</key>\s*<false/>' "$ENT" || report "SP-10: app-sandbox must be false (ADR-003)"
if rg -n 'NS(Microphone|Camera|ScreenCapture|AppleEvents|Accessibility|InputMonitoring)UsageDescription' App/Info.plist >/dev/null 2>&1; then report "SP-10: unexpected usage description in Info.plist"; fi
if ! rg -n '<key>LSUIElement</key>' App/Info.plist >/dev/null 2>&1; then report "Shape: LSUIElement missing; Sprachmittler is a menu-bar agent"; fi
# SP-11 / ADR-002: prompt strings only in SprachmittlerEngines
h=$(hits '"(Translate the following text into|only output the translated result|Please accurately translate the following|### Task)' $PKGSRC -g '!**/SprachmittlerEngines/**')
[[ -n "$h" ]] && { report "ADR-002: model prompt literal outside SprachmittlerEngines"; echo "$h"; }
# Permissions: no screen, camera, microphone, Apple Events or global key tap
h=$(hits 'CGWindowListCreateImage|CGDisplayStream|SCStream|AVCaptureDevice|NSAppleScript|CGEventTapCreate' $SRC)
[[ -n "$h" ]] && { report "Permissions: forbidden capture or key-tap API found"; echo "$h"; }
# Architectural boundaries (SwiftLint also enforces these)
h=$(hits 'NSPasteboard' $PKGSRC -g '!**/SprachmittlerCapture/**' -g '!**/SprachmittlerTestSupport/**')
[[ -n "$h" ]] && { report "Boundary: NSPasteboard outside SprachmittlerCapture"; echo "$h"; }
h=$(hits 'AXUIElement|AXIsProcessTrusted|kAXSelectedText' $PKGSRC -g '!**/SprachmittlerCapture/**' -g '!**/SprachmittlerTestSupport/**')
[[ -n "$h" ]] && { report "Boundary: Accessibility outside SprachmittlerCapture"; echo "$h"; }
# No write of text to disk (settings only; no file writes yet)
h=$(hits '(write\(to:|FileHandle.*write|createFile)' $PKGSRC -g '!**/SprachmittlerTestSupport/**' -g '!**/sprachmittler-cli/**')
[[ -n "$h" ]] && { report "Persistence: file write found outside the CLI"; echo "$h"; }
[[ $fail -eq 0 ]] && echo "✓ privacy checks passed"
exit $fail
+58
View File
@@ -0,0 +1,58 @@
#!/usr/bin/env bash
# Quick end-to-end check: translate one fixed synthetic sentence with an installed Hy-MT3 model.
# Usage: scripts/smoke-ollama.sh [model-tag]
set -euo pipefail
cd "$(dirname "$0")/.."
BASE="${OLLAMA_BASE:-http://127.0.0.1:11434}"
TEXT="The quick brown fox jumps over the lazy dog. Please open https://example.com and set the limit to 10."
TARGET="German"
ver=$(curl -fsS --max-time 3 "$BASE/api/version" | jq -r .version) || { echo "✗ Ollama not reachable at $BASE"; exit 1; }
echo "✓ Ollama $ver"
models=$(curl -fsS "$BASE/api/tags" | jq -r '.models[].name' | grep -i 'hy-mt3' || true)
if [[ -z "$models" ]]; then
echo "✗ no Hy-MT3 model installed. Pull one:"
echo " ollama pull hf.co/tencent/Hy-MT3-7B-GGUF:Q4_K_M"
echo " ollama pull hf.co/tencent/Hy-MT3-1.8B-GGUF:Q4_K_M"
exit 1
fi
MODEL="${1:-}"
if [[ -z "$MODEL" ]]; then
MODEL=$(echo "$models" | grep -E '7B-GGUF' | head -1 || true)
[[ -z "$MODEL" ]] && MODEL=$(echo "$models" | head -1)
fi
if ! echo "$models" | grep -Fxq "$MODEL"; then
echo "✗ model not installed: $MODEL"
echo "$models" | sed 's/^/ installed: /'
exit 1
fi
PROMPT=$(printf 'Translate the following text into %s. Note that you should **only output the translated result without any additional explanation**:\n\n%s' "$TARGET" "$TEXT")
BODY=$(jq -nc --arg m "$MODEL" --arg p "$PROMPT" '{model:$m, messages:[{role:"user",content:$p}], stream:false, keep_alive:"30m",
options:{temperature:0.7, top_p:0.6, top_k:20, repeat_penalty:1.05, num_predict:512, num_ctx:8192}}')
res=$(curl -fsS "$BASE/api/chat" -H 'Content-Type: application/json' -d "$BODY")
content=$(jq -r '.message.content // ""' <<<"$res")
if [[ -z "${content//[[:space:]]/}" ]]; then
echo "✗ $MODEL returned an empty translation"
exit 1
fi
total=$(jq -r '(.total_duration // 0) / 1e9' <<<"$res")
load=$(jq -r '(.load_duration // 0) / 1e9' <<<"$res")
prompt_tokens=$(jq -r '.prompt_eval_count // 0' <<<"$res")
out_tokens=$(jq -r '.eval_count // 0' <<<"$res")
eval_secs=$(jq -r '(.eval_duration // 0) / 1e9' <<<"$res")
rate=$(awk -v t="$out_tokens" -v s="$eval_secs" 'BEGIN { if (s > 0) printf "%.2f", t/s; else print "?" }')
echo "✓ $MODEL"
echo "--- translation ---"
printf '%s\n' "$content" | head -c 800
printf '\n--- metrics ---\n'
printf 'total %.2fs · load %.2fs · prompt %s tokens · output %s tokens · %s tok/s\n' "$total" "$load" "$prompt_tokens" "$out_tokens" "$rate"
if ! grep -qi 'fuchs' <<<"$content"; then
echo "! expected 'Fuchs' in the German translation; review the output"
fi
+44
View File
@@ -0,0 +1,44 @@
#!/usr/bin/env bash
# Full verification gate. Must be green before a task is marked done (AGENTS.md).
set -euo pipefail
cd "$(dirname "$0")/.."
PKG=Packages/SprachmittlerKit
step() { printf '\n\033[1m▶ %s\033[0m\n' "$*"; }
step "Format check"
if command -v swiftformat >/dev/null; then swiftformat App "$PKG" --lint; else echo "skip: swiftformat not installed"; fi
step "Lint"
if command -v swiftlint >/dev/null; then swiftlint lint --strict --quiet App "$PKG/Sources"; else echo "skip: swiftlint not installed"; fi
step "Shell syntax"
for f in scripts/*.sh; do bash -n "$f"; done
echo "✓ scripts parse"
step "JSON and plist syntax"
for f in opencode.json evaluation/corpus.schema.json evaluation/corpus.example.json; do
jq -e . "$f" >/dev/null
done
plutil -lint App/Info.plist App/Sprachmittler.entitlements >/dev/null
echo "✓ JSON and plists parse"
step "Privacy checks"
scripts/privacy-check.sh
step "Package build"
swift build --package-path "$PKG"
step "Package tests"
swift test --package-path "$PKG" --skip SprachmittlerLiveTests
step "App build"
if command -v xcodegen >/dev/null && command -v xcodebuild >/dev/null; then
xcodegen generate --spec project.yml --quiet
xcodebuild -project Sprachmittler.xcodeproj -scheme Sprachmittler -configuration Debug \
-derivedDataPath .build/xcode -destination 'platform=macOS,arch=arm64' -quiet build
else
echo "skip: xcodegen/xcodebuild not available (make bootstrap)"
fi
printf '\n\033[32m✔ verify passed\033[0m\n'