#!/usr/bin/env bash
# Calls the Dorsal HTTP API and prints the JSON response.
#
#   dorsal.sh summaries '{"query":"..."}'     search; the body can also come on stdin
#   dorsal.sh set '{"ids":["..."]}'           full records, at most 25 ids per call
#   dorsal.sh neighbors '{"query":"..."}'     relationships of one named object
#   dorsal.sh protocol dorsal investigate     method for a "dorsal ..." request (or improve, audit)
#   dorsal.sh protocol decision-verification  method for checking a decision
#   dorsal.sh protocol audit [audit|fix] [sections]  audit method; sections comma-separated (core, memory, ...)
#   dorsal.sh index                           endpoints and body schemas
#
# Needs DORSAL_API_KEY, or the key on the first line of ~/.dorsal/api-key. DORSAL_API_URL overrides the base URL
# (default https://api.dorsal.us).
# The key goes to curl through a file descriptor, so it never appears in a command line or the process list.
# Retries twice on 429 and 503, waiting for Retry-After (at most 30 seconds). Exits 0 only on HTTP 200.
# Results go to stdout and errors to stderr, so an error stays visible when stdout is piped into jq.
# Before a POST leaves the machine, a privacy check reads the body (see privacy_check). When it finds a file path,
# URL, key, email, commit hash, setting, private host, or a name, branch, commit subject or .env value from the git
# repository the agent works in, it prints a privacy_block error, sends nothing and exits 3.
set -uo pipefail
export LC_ALL=C

usage() {
    printf '%s\n' '{"error":{"type":"usage_error","message":"Usage: dorsal.sh summaries|set|neighbors <json body or stdin> | protocol dorsal <investigate|improve|audit> | protocol decision-verification | protocol audit [audit|fix] [core,memory,remediation_boundary,support,all] | index"}}' >&2
    exit 2
}

# ---- Privacy check ---------------------------------------------------------------------------------------------
# Runs on this machine, after the body is built and before curl. Bash 3.2, git, awk and tr only.
# Every free-text value is screened for file paths, URLs, keys and tokens, emails, commit hashes, code syntax,
# NAME=value settings, private hosts, line breaks and pasted-length text. Then the query is compared with the git repository
# containing the working directory: tracked file names and folder paths, names defined in its code, the branch, the last
# 50 commit subjects and values in its .env files. Local data is only compared; none of it is sent or printed
# (a matched name or commit phrase is echoed back to the agent, a key or .env value never is).

pc_ext='(ts|tsx|js|jsx|mjs|cjs|py|go|rs|rb|php|java|kt|swift|cs|cpp|cc|c|h|hpp|json|yaml|yml|toml|sh|bash|zsh|sql|md|mdx|env|css|scss|html|vue|svelte|lock|ini|cfg|conf|xml|gradle|tf|proto|graphql|prisma|dart|ex|exs|lua|ipynb|txt|log|csv)'
pc_re_url='[A-Za-z][A-Za-z0-9+.-]*://[^[:space:]]*'
pc_re_abs='(^|[^A-Za-z0-9_.:/~-])/[A-Za-z0-9_.@~+-]+/[^[:space:]]*'
pc_re_abs_file="(^|[^A-Za-z0-9_.:/~-])/[A-Za-z0-9_@-]+\\.${pc_ext}([^A-Za-z0-9]|\$)"
pc_re_home='(^|[^A-Za-z0-9_])(~|\$HOME|\$\{HOME\}|%USERPROFILE%|%APPDATA%|%HOMEPATH%)[/\\][^[:space:]]*'
pc_re_win='(^|[^A-Za-z0-9])[A-Za-z]:\\[^[:space:]]*'
pc_re_rel='(^|[^A-Za-z0-9_.])\.\.?/[A-Za-z0-9_.@-][^[:space:]]*'
pc_re_file="[A-Za-z0-9_.@-]+/[A-Za-z0-9_.@/-]*\\.${pc_ext}([^A-Za-z0-9]|\$)"
pc_re_folder='[A-Za-z0-9_.@-]+/[A-Za-z0-9_.@-]+/[A-Za-z0-9_.@-]+'
pc_re_email='[A-Za-z0-9._%+-]+@[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*\.[A-Za-z]{2,}'
pc_re_sha='(^|[^0-9a-fA-F])[0-9a-f]{40}([^0-9a-fA-F]|$)'
pc_re_uuid='(^|[^0-9a-fA-F])[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}([^0-9a-fA-F]|$)'
pc_re_key='(^|[^A-Za-z0-9])(sk-[A-Za-z0-9_-]{16,}|gh[pousr]_[A-Za-z0-9]{20,}|github_pat_[A-Za-z0-9_]{20,}|(AKIA|ASIA)[0-9A-Z]{16}|xox[abposr]-[A-Za-z0-9-]{10,}|AIza[0-9A-Za-z_-]{30,}|-----BEGIN|eyJ[A-Za-z0-9_-]{8,}\.eyJ[A-Za-z0-9_-]{8,}|(sk|pk|rk)_(live|test)_[A-Za-z0-9]{8,}|whsec_[A-Za-z0-9]{8,}|glpat-[A-Za-z0-9_-]{16,}|npm_[A-Za-z0-9]{30,}|hf_[A-Za-z0-9]{30,}|SG\.[A-Za-z0-9_-]{16,}\.)'
pc_re_setting='(^|[^A-Za-z0-9_])[A-Z][A-Z0-9_]{2,}=[^[:space:]]'
pc_re_cred='(password|passwd|secret|token|api[_-]?key|apikey)[[:space:]]*[=:][[:space:]]*[^[:space:]]{4,}'
pc_re_lochost='(^|[^A-Za-z0-9.-])(localhost|127\.0\.0\.1|0\.0\.0\.0):[0-9]{2,5}'
pc_re_ip='(^|[^0-9.])(10\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}|192\.168\.[0-9]{1,3}\.[0-9]{1,3}|172\.(1[6-9]|2[0-9]|3[01])\.[0-9]{1,3}\.[0-9]{1,3})([^0-9]|$)'
pc_re_privdns='(^|[^A-Za-z0-9.-])[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*\.(internal|local|lan|corp|intranet|home\.arpa|svc|cluster\.local)([^A-Za-z0-9-]|$)'
pc_re_idprefix='^([a-z_]+:[^[:space:]]|gh-|chunk_|[0-9]{8}T[0-9]{6}Z?_)'
pc_re_code='=>|===|!==|[^=!<>]==[^=]|&&|\|\||\{[^}]*\}|(^|[^A-Za-z])(const|let|var|def|function|import|export|class)[[:space:]]+[A-Za-z_$][A-Za-z0-9_$]*[[:space:]]*[=({:]|[A-Za-z_$][A-Za-z0-9_$.]*\([^)]*\)[[:space:]]*;|(^|[^A-Za-z])return[[:space:]]+[^[:space:]]+;'

pc_json_text() {
    local s=$1
    s=${s//\\/\\\\}
    s=${s//\"/\\\"}
    s=${s//[[:cntrl:]]/ }
    printf '%s' "${s:0:120}"
}

# privacy_block <rule> <match or empty> <sentence>
privacy_block() {
    local match="" m=$2
    while [ -n "$m" ] && [[ $m =~ ^[^A-Za-z0-9~/._@-] ]]; do m=${m:1}; done
    [ -n "$m" ] && match=",\"match\":\"$(pc_json_text "$m")\""
    printf '{"error":{"type":"privacy_block","rule":"%s"%s,"message":"Not sent. %s Rewrite it with public names only (product, library, error type, method) and retry."}}\n' "$1" "$match" "$3" >&2
    return 1
}

# pc_patterns <value> <query|id>: returns 1 after printing a block on a hit.
pc_patterns() {
    local v=$1 kind=$2 lower w tmp segs
    # Documented default paths of public tools, written from ~ (~/.claude.json, ~/.codex/config.toml), name a
    # tool's file, not the user's machine. A path with a user name in it (/Users/sam/.claude.json) is still refused.
    [ "$kind" = query ] && v=$(printf '%s' "$v" | sed -E 's#(^|[^A-Za-z0-9_])(~|\$HOME)/(\.claude\.json|\.claude/settings(\.local)?\.json|\.claude/CLAUDE\.md|\.codex/config\.toml|\.codex/AGENTS\.md|\.cursor/mcp\.json|\.gitconfig|\.zshrc|\.bashrc|\.bash_profile|\.profile|\.npmrc|\.ssh/config|\.aws/config|\.aws/credentials|\.kube/config|\.docker/config\.json)([^A-Za-z0-9_./-]|$)#\1public_tool_path\5#g')
    if [[ $v =~ $pc_re_url ]]; then privacy_block url "${BASH_REMATCH[0]}" "The request contains a URL."; return; fi
    if [[ $v =~ $pc_re_home ]] || [[ $v =~ $pc_re_win ]]; then privacy_block file_path "${BASH_REMATCH[0]}" "The request contains a file path."; return; fi
    if [ "$kind" = query ]; then
        # Product names that end in .js (Next.js, Node.js) are names, not files.
        w=$(printf '%s' "$v" | sed -E -e 's/(^|[^A-Za-z0-9_.-])([Nn]ext|[Nn]ode|[Vv]ue|[Nn]uxt|[Ee]xpress|[Tt]hree|[Dd]3|[Cc]hart|[Rr]eact|[Aa]ngular|[Ee]mber|[Bb]ackbone|[Ss]olid|[Nn]est|[Aa]lpine|[Pp]5|[Tt]ensorflow|[Pp]ixi|[Bb]abylon|[Mm]oment|[Ss]ocket|[Pp]react|[Mm]eteor|[Kk]oa|[Ff]astify|[Ee]lectron|[Dd]eno|[Bb]un|[Ss]velte|[Rr]emix|[Gg]atsby|[Hh]api|[Ss]ails|[Mm]ithril|[Aa]nime|[Vv]ideo|[Ll]eaflet|[Ss]ignal|[Tt]ransformers|[Ll]angchain|[Ll]lamaindex|[Oo]pen[Aa][Ii])\.js([^A-Za-z0-9_]|$)/\1\2_js\3/g' -e 's#(^|[^A-Za-z0-9_./-])(notifications|tools|resources|prompts|sampling|roots|completion|logging|elicitation)(/[a-z_]+)+([^A-Za-z0-9_./-]|$)#\1mcp_method\4#g')
        if [[ $v =~ $pc_re_abs ]] || [[ $v =~ $pc_re_abs_file ]] || [[ $v =~ $pc_re_rel ]] || [[ $w =~ $pc_re_file ]]; then
            privacy_block file_path "${BASH_REMATCH[0]}" "The request contains a file path."; return
        fi
        # Two or more slashes: a path when a segment carries . _ - a digit or camelCase, or it starts at a code folder.
        # Plain word lists such as add/update/delete or TCP/UDP/QUIC pass.
        local words seg
        read -r -a words <<< "$w"
        for tmp in ${words[@]+"${words[@]}"}; do
            [[ $tmp =~ $pc_re_folder ]] || continue
            tmp=${BASH_REMATCH[0]}
            if [[ $tmp =~ ^(src|lib|app|apps|packages|pkg|cmd|internal|components|pages|api|server|client|test|tests|scripts|config|utils|services|modules|public|static|assets|docs|bin|dist|build|tools|hooks|routes|models|controllers|views|handlers|migrations|db|var|etc|usr|home|users|tmp|opt)/ ]]; then
                privacy_block file_path "$tmp" "The request contains a file path."; return
            fi
            IFS=/ read -r -a segs <<< "$tmp"
            for seg in "${segs[@]}"; do
                if [[ $seg =~ [._0-9-] ]] || [[ $seg =~ [a-z][A-Z] ]]; then privacy_block file_path "$tmp" "The request contains a file path."; return; fi
            done
        done
    elif [[ $v =~ $pc_re_abs ]] && ! [[ $v =~ ^(chunk|doc): ]]; then
        privacy_block file_path "${BASH_REMATCH[0]}" "The request contains a file path."; return
    fi
    if [[ $v =~ $pc_re_email ]]; then privacy_block email "" "The request contains an email address."; return; fi
    if [[ $v =~ $pc_re_sha ]]; then privacy_block commit_hash "${BASH_REMATCH[0]}" "The request contains a commit hash."; return; fi
    if [[ $v =~ $pc_re_key ]]; then privacy_block key "" "The request contains something shaped like a key or token."; return; fi
    # Chunk and doc ids name files in public repositories and sites, including UUID folders and .local.md files.
    [ "$kind" = id ] && [[ $v =~ ^(chunk|doc): ]] && return 0
    if [[ $v =~ $pc_re_uuid ]]; then privacy_block key "" "The request contains an identifier value (UUID)."; return; fi
    if [ "$kind" = query ]; then
        for w in $(printf '%s' "$v" | tr -c 'A-Za-z0-9+/=_-' ' '); do
            [ ${#w} -ge 32 ] || continue
            [[ $w =~ [0-9] ]] && [[ $w =~ [A-Za-z] ]] || continue
            tmp=${w//[^_-]/}
            [ ${#tmp} -le 2 ] || continue
            privacy_block key "" "The request contains something shaped like a key or token."; return
        done
        # A pip pin (package==1.2.3) is a package spec, not code.
        tmp=$(printf '%s' "$v" | sed -E 's/([A-Za-z0-9_.-])==([0-9])/\1 \2/g')
        if [[ $tmp =~ $pc_re_code ]]; then privacy_block code "" "The request contains code."; return; fi
        if [[ $v =~ $pc_re_setting ]]; then privacy_block setting "" "The request contains a setting with its value (NAME=value)."; return; fi
        lower=$(printf '%s' "$v" | tr 'A-Z' 'a-z')
        if [[ $lower =~ $pc_re_cred ]]; then privacy_block setting "" "The request contains a credential setting with its value."; return; fi
    fi
    if [[ $v =~ $pc_re_lochost ]] || [[ $v =~ $pc_re_ip ]] || [[ $v =~ $pc_re_privdns ]]; then
        privacy_block private_host "${BASH_REMATCH[0]}" "The request contains a private host or IP address."; return
    fi
    return 0
}

# One awk pass over the JSON body. Prints K<TAB>key for each top-level key, S<TAB>key<TAB>value for every string
# (escapes decoded; nested values are listed under their top-level key), NL<TAB>key when a string holds a line
# break, NEST<TAB>key for an object below the top level, and ERR when the body is not a JSON object.
read -r -d '' PC_JSON_AWK <<'AWK'
function hexval(h,   k, v, d) { v = 0; for (k = 1; k <= 4; k++) { d = index("0123456789abcdef", substr(h, k, 1)); if (d == 0) return -1; v = v * 16 + d - 1 } return v }
function readstr(   val, rest, c, e, h, cp) {
    val = ""; gnl = 0; i++
    while (i <= n) {
        rest = substr(s, i)
        if (match(rest, /^[^"\\\t\r\n]+/)) { val = val substr(rest, 1, RLENGTH); i += RLENGTH; continue }
        c = substr(s, i, 1)
        if (c == "\"") { gval = val; return 1 }
        if (c == "\\") {
            e = substr(s, i + 1, 1)
            if (e == "n" || e == "r" || e == "b" || e == "f") { gnl = 1; val = val " "; i += 2 }
            else if (e == "t") { val = val " "; i += 2 }
            else if (e == "\"" || e == "\\" || e == "/") { val = val e; i += 2 }
            else if (e == "u") {
                h = tolower(substr(s, i + 2, 4)); cp = hexval(h)
                if (cp < 0) return 0
                if (cp < 32) { gnl = 1; val = val " " }
                else if (cp < 128) val = val sprintf("%c", cp)
                else if (cp < 2048) val = val sprintf("%c%c", 192 + int(cp / 64), 128 + cp % 64)
                else val = val sprintf("%c%c%c", 224 + int(cp / 4096), 128 + int(cp / 64) % 64, 128 + cp % 64)
                i += 6
            } else return 0
            continue
        }
        if (c == "\n" || c == "\r") gnl = 1
        val = val " "; i++
    }
    return 0
}
BEGIN { RS = "\001" }
{ s = s $0 }
END {
    gsub(/\t/, " ", s)
    n = length(s); i = 1; depth = 0; err = 0; key = ""
    while (i <= n) {
        c = substr(s, i, 1)
        if (c == " " || c == "\n" || c == "\r") { i++; continue }
        if (c == "{" || c == "[") {
            if (depth == 0 && c != "{") { err = 1; break }
            if (depth >= 1 && c == "{") print "NEST\t" key
            depth++; st[depth] = c; ex[depth] = (c == "{") ? "key" : "val"; i++; continue
        }
        if (c == "}" || c == "]") {
            depth--; if (depth < 0) { err = 1; break }
            if (depth >= 1 && st[depth] == "{") ex[depth] = "comma"
            i++; if (depth == 0) break; continue
        }
        if (depth == 0) { err = 1; break }
        if (c == ":") { ex[depth] = "val"; i++; continue }
        if (c == ",") { if (st[depth] == "{") ex[depth] = "key"; i++; continue }
        if (c == "\"") {
            if (!readstr()) { err = 1; break }
            if (depth == 1 && ex[1] == "key") { key = gval; print "K\t" key; ex[1] = "colon" }
            else {
                print "S\t" key "\t" gval
                if (gnl) print "NL\t" key
                if (st[depth] == "{") ex[depth] = (ex[depth] == "key") ? "colon" : "comma"
            }
            i++; continue
        }
        if (match(substr(s, i), /^[-+0-9.eEtrufalsn]+/)) { if (st[depth] == "{") ex[depth] = "comma"; i += RLENGTH; continue }
        err = 1; break
    }
    if (!err) { while (i <= n) { c = substr(s, i, 1); if (c != " " && c != "\n" && c != "\r") { err = 1; break } i++ } }
    if (err || depth != 0) print "ERR"
}
AWK

# The local pass: reads the values (V<TAB>Q|I<TAB>value), tracked paths, git log lines and .env files, prints the first
# hit as H<TAB>rule<TAB>match (match empty for .env values), else T<TAB>token for each name worth a definition search.
read -r -d '' PC_LOCAL_AWK <<'AWK'
function identshaped(t) { return t ~ /^[A-Za-z_][A-Za-z0-9_]*$/ && (t ~ /[a-z][A-Z]/ || t ~ /_/ || (t ~ /[0-9]/ && t ~ /[A-Za-z]/)) }
function folderish(t) { return t ~ /^[A-Za-z0-9][A-Za-z0-9_.@-]*$/ && (t ~ /[-_]/ || t ~ /[a-z][A-Z]/ || (t ~ /[0-9]/ && t ~ /[A-Za-z]/)) }
function hit(rule, m) { print "H\t" rule "\t" m; done = 1; exit }
function distinctive(c) { return (length(c) >= 8 && (c ~ /[0-9]/ || c ~ /[-_.:@\/]/ || c ~ /[a-z][A-Z]/ || c ~ /[A-Z][a-z]*[A-Z]/)) || length(c) >= 16 }
function stopval(c, l) {
    l = tolower(c)
    if (l in STOP) return 1
    if (l ~ /^[0-9.:]+$/) return 1
    if (l ~ /^(us|eu|ap|sa|ca|me|af|il|mx)-[a-z]+-[0-9]$/ || l ~ /^[a-z]+-[a-z]+[0-9]$/) return 1
    if (c ~ /^[A-Z][a-z]+\/[A-Za-z_]+$/) return 1
    return 0
}
function envcheck(c, secretish,   j, w) {
    if (length(c) < 8 || stopval(c)) return
    if (distinctive(c)) { for (j = 1; j <= nq; j++) if (index(qv[j], c)) hit("env_value", "") }
    if (secretish || (length(c) >= 16 && c ~ /[0-9]/ && c ~ /[A-Za-z]/ && c !~ / /)) {
        for (k2 = 1; k2 + 7 <= length(c); k2++) {
            w = substr(c, k2, 8)
            if (w ~ /^[a-z_-]+$/ || w ~ /^[0-9]+$/) continue
            for (j = 1; j <= nq; j++) if (index(qv[j], w)) hit("env_value", "")
        }
    }
}
function envurl(v, secretish,   rest, auth, hostport, p, segs, m, q, kv, a, b) {
    rest = v; sub(/^[A-Za-z][A-Za-z0-9+.-]*:\/\//, "", rest)
    p = index(rest, "/"); hostport = (p ? substr(rest, 1, p - 1) : rest); rest = (p ? substr(rest, p) : "")
    p = index(hostport, "@")
    if (p) { auth = substr(hostport, 1, p - 1); hostport = substr(hostport, p + 1); a = index(auth, ":"); if (a) { envcheck(substr(auth, 1, a - 1), 0); envcheck(substr(auth, a + 1), 1) } else envcheck(auth, 1) }
    sub(/:[0-9]+$/, "", hostport); envcheck(hostport, 0)
    m = split(hostport, segs, "."); for (b = 1; b <= m; b++) envcheck(segs[b], 0)
    q = index(rest, "?"); if (q) { kv = substr(rest, q + 1); rest = substr(rest, 1, q - 1) } else kv = ""
    m = split(rest, segs, "/"); for (b = 1; b <= m; b++) envcheck(segs[b], 0)
    m = split(kv, segs, "&"); for (b = 1; b <= m; b++) { a = index(segs[b], "="); if (a) envcheck(substr(segs[b], a + 1), secretish) }
}
BEGIN {
    split("package.json package-lock.json yarn.lock pnpm-lock.yaml pnpm-workspace.yaml bun.lockb tsconfig.json tsconfig.base.json jsconfig.json readme.md readme license license.md changelog.md contributing.md security.md codeowners dockerfile docker-compose.yml docker-compose.yaml compose.yml compose.yaml makefile procfile .gitignore .dockerignore .env .env.example .env.local .env.development .env.production .env.test .npmrc .nvmrc .prettierrc .prettierrc.json .eslintrc.json .eslintrc.js .eslintrc.cjs eslint.config.js eslint.config.mjs prettier.config.js biome.json turbo.json nx.json lerna.json renovate.json index.ts index.tsx index.js index.mjs index.html main.py main.go main.rs main.ts main.js app.py app.ts app.js app.tsx server.ts server.js setup.py setup.cfg pyproject.toml requirements.txt poetry.lock pipfile pipfile.lock go.mod go.sum cargo.toml cargo.lock gemfile gemfile.lock next.config.js next.config.mjs next.config.ts next-env.d.ts vite.config.ts vite.config.js vitest.config.ts vitest.setup.ts jest.config.js jest.config.ts jest.setup.js setuptests.ts playwright.config.ts playwright.config.js tailwind.config.js tailwind.config.ts postcss.config.js postcss.config.mjs webpack.config.js babel.config.js claude.md agents.md skill.md settings.json settings.local.json config.toml config.json config.yaml config.yml middleware.ts middleware.js page.tsx page.ts page.js layout.tsx layout.js route.ts route.js schema.prisma vercel.json netlify.toml wrangler.toml fly.toml pom.xml build.gradle build.gradle.kts settings.gradle manifest.json mcp.json .mcp.json global.css globals.css __init__.py conftest.py manage.py wsgi.py asgi.py urls.py models.py views.py settings.py utils.py utils.ts helpers.ts types.ts constants.ts lib.rs mod.rs build.rs package.swift action.yml action.yaml ci.yml release.yml dependabot.yml", a, " ")
    for (k in a) GENERIC[a[k]] = 1
    split("node_modules __tests__ __mocks__ src-tauri e2e .github .claude .codex .vscode .idea .next .venv venv __pycache__ i18n l10n v1 v2 v3 api-docs end-to-end", a, " ")
    for (k in a) GENDIR[a[k]] = 1
    split("getServerSideProps getStaticProps getStaticPaths getInitialProps generateMetadata generateStaticParams generateViewport generateImageMetadata generateSitemaps createClient createServerClient createBrowserClient createContext createStore createSlice createApi createRouter createTRPCRouter createTRPCContext publicProcedure protectedProcedure authOptions authConfig nextConfig defineConfig useAuth useSession getServerSession signIn signOut getSession onRequest __init__ __main__ __name__ componentDidMount componentDidUpdate componentWillUnmount shouldComponentUpdate getSnapshotBeforeUpdate componentDidCatch getDerivedStateFromProps getDerivedStateFromError connectedCallback disconnectedCallback attributeChangedCallback adoptedCallback toString toJSON valueOf hashCode compareTo onCreate onDestroy onStart onStop onResume onPause viewDidLoad viewWillAppear ngOnInit ngOnDestroy ngOnChanges ngAfterViewInit hitTest handleEvent handleRequest get_queryset get_context_data get_object get_serializer get_serializer_class get_serializer_context perform_create perform_update perform_destroy form_valid form_invalid get_success_url get_form_kwargs get_permissions has_permission has_object_permission setUpClass tearDownClass setUpTestData get_absolute_url save_model to_representation to_internal_value training_step validation_step configure_optimizers setup_method teardown_method maxDuration preferredRegion fetchCache dynamicParams experimental_ppr reactStrictMode serverExternalPackages outputFileTracingRoot poweredByHeader trailingSlash basePath assetPrefix transpilePackages serverActions typedRoutes cacheComponents cacheLife cacheTag unstable_cache revalidateTag revalidatePath updateTag clientLoader clientAction shouldRevalidate HydrateFallback ErrorBoundary ALLOWED_HOSTS INSTALLED_APPS ROOT_URLCONF WSGI_APPLICATION ASGI_APPLICATION DEFAULT_AUTO_FIELD AUTH_USER_MODEL STATIC_URL STATIC_ROOT MEDIA_URL MEDIA_ROOT SECRET_KEY CSRF_TRUSTED_ORIGINS CORS_ALLOWED_ORIGINS SESSION_COOKIE_SECURE CSRF_COOKIE_SECURE SECURE_PROXY_SSL_HEADER DEFAULT_FILE_STORAGE REST_FRAMEWORK CELERY_BROKER_URL CELERY_RESULT_BACKEND USE_TZ TIME_ZONE LANGUAGE_CODE EMAIL_BACKEND on_failure on_retry on_success after_return pytest_configure pytest_addoption pytest_collection_modifyitems pytest_runtest_setup pytest_sessionstart pytest_sessionfinish pytest_generate_tests setUp tearDown asyncSetUp asyncTearDown ServeHTTP MarshalJSON UnmarshalJSON MarshalText UnmarshalText TestMain useDebounce useThrottle useLocalStorage useMediaQuery useInterval usePrevious useOnClickOutside useClickOutside useWindowSize useIsMobile useCopyToClipboard useEventListener useFetch useToggle beforeMount beforeUpdate beforeUnmount onMounted onUnmounted onBeforeUnmount onCreateView onViewCreated onAttach onDetach onSaveInstanceState onActivityResult viewDidAppear viewWillDisappear viewDidDisappear create_app get_db get_current_user get_settings", a, " ")
    for (k in a) FRAMEWORK[a[k]] = 1
    split("true false null none yes no development production staging preview test testing localhost http https postgres postgresql mysql mariadb redis mongodb sqlite utf8 utf-8 utf8mb4 info debug warn warning error changeme password default public private", a, " ")
    for (k in a) STOP[a[k]] = 1
    branch = ENVIRON["PC_BRANCH"]
}
FILENAME == ENVIRON["PC_VALS"] {
    split($0, p, "\t"); kind = p[2]; v = substr($0, length(p[1]) + length(p[2]) + 3)
    if (kind == "I") { m = split(v, toks, /[^A-Za-z0-9]+/); for (k = 1; k <= m; k++) if (length(toks[k]) >= 6 && toks[k] ~ /[a-z][A-Z]/ && !(toks[k] in FRAMEWORK)) CAND[toks[k]] = 1; next }
    nq++; qv[nq] = v; ql[nq] = tolower(v); qn[nq] = ql[nq]; gsub(/[^a-z0-9]+/, " ", qn[nq])
    nw = split(v, words, /[[:space:],;()"'`]+/)
    for (w = 1; w <= nw; w++) if (index(words[w], "/")) {
        ns = split(words[w], ss, "/")
        for (k = 1; k <= ns; k++) { x = ss[k]; sub(/[.@-]+$/, "", x); sub(/^[.@-]+/, "", x); if (x != "") QS[tolower(x)] = x }
    }
    m = split(v, toks, /[^A-Za-z0-9_.@-]+/)
    for (k = 1; k <= m; k++) {
        t = toks[k]; sub(/[.@-]+$/, "", t); sub(/^[.@-]+/, "", t)
        if (t == "") continue
        QT[tolower(t)] = t; if (index(t, ".")) { st0 = t; sub(/\.[^.]*$/, "", st0); if (st0 != "") QT[tolower(st0)] = st0 }
        m2 = split(t, sub2, /[.@-]+/)
        for (k3 = 1; k3 <= m2; k3++) if (length(sub2[k3]) >= 6 && identshaped(sub2[k3]) && !(sub2[k3] in FRAMEWORK) && sub2[k3] !~ /^test_/ && sub2[k3] !~ /^__.*__$/) CAND[sub2[k3]] = 1
        if (length(t) >= 6 && identshaped(t) && !(t in FRAMEWORK) && t !~ /^test_/ && t !~ /^__.*__$/) CAND[t] = 1
    }
    next
}
FILENAME == ENVIRON["PC_FILES"] {
    m = split($0, seg, "/")
    base = seg[m]; lb = tolower(base)
    if ((lb in QT) && !(lb in GENERIC) && (index(base, ".") || (length(base) >= 6 && folderish(base)))) hit("repo_file_name", QT[lb])
    stem = base; sub(/\.[^.]*$/, "", stem); ls = tolower(stem)
    if (stem != base && length(stem) >= 6 && folderish(stem) && (ls in QT) && !(lb in GENERIC) && !(ls in GENERIC)) hit("repo_file_name", QT[ls])
    for (k = 1; k < m; k++) { ld = tolower(seg[k]); if (!(ld in SEENDIR)) { SEENDIR[ld] = 1; if ((ld in QS) && folderish(seg[k]) && !(ld in GENDIR) && length(seg[k]) >= 4) hit("repo_folder_path", QS[ld]) } }
    next
}
FILENAME == ENVIRON["PC_LOG"] {
    if (substr($0, 1, 2) != "S\t") next
    subj = tolower(substr($0, 3)); gsub(/[^a-z0-9]+/, " ", subj); sub(/^ +/, "", subj); sub(/ +$/, "", subj)
    if (subj ~ /^(merge|revert merge|bump|initial commit|update readme)/ || length(subj) < 30) next
    for (j = 1; j <= nq; j++) for (k = 1; k + 29 <= length(subj); k++) { w = substr(subj, k, 30); if (index(qn[j], w)) hit("commit_text", w) }
    next
}
{
    line = $0; sub(/\r$/, "", line); sub(/^[ \t]*export[ \t]+/, "", line)
    if (line ~ /^[ \t]*#/ || index(line, "=") == 0) next
    ek = substr(line, 1, index(line, "=") - 1); gsub(/[ \t]/, "", ek)
    ev = substr(line, index(line, "=") + 1); sub(/^[ \t]+/, "", ev)
    if (ev ~ /^"/) { sub(/^"/, "", ev); sub(/".*$/, "", ev) } else if (ev ~ /^'/) { sub(/^'/, "", ev); sub(/'.*$/, "", ev) } else { sub(/[ \t]+#.*$/, "", ev); sub(/[ \t]+$/, "", ev) }
    if (ev == "") next
    secretish = (toupper(ek) ~ /KEY|SECRET|TOKEN|PASSWORD|PASS|PWD|CREDENTIAL|PRIVATE|DSN|AUTH|SIGNING|WEBHOOK|SALT|COOKIE|SESSION/)
    envcheck(ev, secretish)
    if (ev ~ /^[A-Za-z][A-Za-z0-9+.-]*:\/\//) envurl(ev, secretish)
    next
}
END {
    if (done) exit
    if (branch != "" && branch !~ /^(main|master|dev|develop|development|trunk|staging|production|release)$/ && length(branch) >= 4) {
        lbr = tolower(branch); m = split(lbr, bs, "/"); last = bs[m]
        for (j = 1; j <= nq; j++) { if (index(ql[j], lbr)) { print "H\tbranch_name\t" branch; exit } if (length(last) >= 8 && index(ql[j], last)) { print "H\tbranch_name\t" last; exit } }
    }
    for (t in CAND) print "T\t" t
}
AWK

# privacy_check <endpoint> <body>: returns 1 after printing a block on a hit.
privacy_check() {
    local endpoint=$1 body=$2 rc
    pc_tmp=""
    privacy_check_run "$endpoint" "$body"; rc=$?
    [ -n "$pc_tmp" ] && rm -rf "$pc_tmp"
    return $rc
}

privacy_check_run() {
    local endpoint=$1 body=$2 parsed line kind rest key val allowed root branch toks tok hits re
    if [ ${#body} -gt 16384 ]; then privacy_block too_long "" "The request body is over 16 KB, which usually means pasted code, logs or chat text."; return; fi
    case "$endpoint" in
        summaries) allowed=" query total page_size cursor event_types object_types sources since until repo " ;;
        set) allowed=" ids " ;;
        neighbors) allowed=" query include_events hops direction total target_type rel_type event_types sources since until " ;;
    esac
    parsed=$(printf '%s' "$body" | awk "$PC_JSON_AWK")
    pc_tmp=$(mktemp -d 2>/dev/null) || pc_tmp=""
    local vals="${pc_tmp:-/dev/null}/vals"
    [ -n "$pc_tmp" ] && : > "$vals"
    if printf '%s\n' "$parsed" | grep -q '^ERR$'; then
        # Not a JSON object: the server rejects it, but screen the raw text before it leaves.
        pc_patterns "$body" query || return 1
        return 0
    fi
    while IFS= read -r line; do
        [ -n "$line" ] || continue
        kind=${line%%$'\t'*}; rest=${line#*$'\t'}
        case "$kind" in
            K) case "$allowed" in *" $rest "*) ;; *) privacy_block unexpected_field "$rest" "The request has a field this endpoint does not accept."; return ;; esac ;;
            NEST) privacy_block unexpected_field "$rest" "The request nests an object inside a field."; return ;;
            NL) privacy_block line_break "" "The request contains a line break, which usually means pasted code, logs or chat text."; return ;;
            S)
                key=${rest%%$'\t'*}; val=${rest#*$'\t'}
                case "$key" in
                    query)
                        if [ "$endpoint" = neighbors ] && [[ $val =~ $pc_re_idprefix ]]; then
                            pc_id "$val" || return 1
                            [ -n "$pc_tmp" ] && printf 'V\tI\t%s\n' "$val" >> "$vals"
                        else
                            tok=${val//[$'\200'-$'\277']/}
                            if [ ${#tok} -gt 500 ]; then privacy_block too_long "" "The query is over 500 characters, which usually means pasted text. Write a short search phrase."; return; fi
                            pc_patterns "$val" query || return 1
                            [ -n "$pc_tmp" ] && printf 'V\tQ\t%s\n' "$val" >> "$vals"
                        fi ;;
                    ids)
                        pc_id "$val" || return 1
                        [ -n "$pc_tmp" ] && printf 'V\tI\t%s\n' "$val" >> "$vals" ;;
                    repo)
                        re='^([A-Za-z0-9_.-]{1,100}/[A-Za-z0-9_.-]{1,100}|repo:[A-Za-z0-9_.-]{1,200})$'
                        [[ $val =~ $re ]] || { privacy_block invalid_value "repo" "repo must be owner/name of a public GitHub repository."; return; } ;;
                    cursor)
                        re='^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+$'
                        [ ${#val} -le 4096 ] && [[ $val =~ $re ]] || { privacy_block invalid_value "cursor" "cursor must be the next_cursor value returned by the previous page."; return; } ;;
                    since|until)
                        re='^[0-9]{4}-[0-9]{2}-[0-9]{2}([T ][0-9]{2}:[0-9]{2}(:[0-9]{2}(\.[0-9]{1,9})?)?(Z|[+-][0-9]{2}:?[0-9]{2})?)?$'
                        [[ $val =~ $re ]] || { privacy_block invalid_value "$key" "$key must be an ISO date such as 2026-11-01."; return; } ;;
                    *)
                        re='^[a-z][a-z0-9_]{0,60}$|^[0-9]{1,6}$|^(true|false)$'
                        [[ $val =~ $re ]] || { privacy_block invalid_value "$key" "$key takes one of the listed type names."; return; } ;;
                esac ;;
        esac
    done <<EOF
$parsed
EOF
    [ -n "$pc_tmp" ] && [ -s "$vals" ] || return 0
    root=$(git rev-parse --show-toplevel 2>/dev/null) || return 0
    [ -n "$root" ] || return 0
    branch=$(git -C "$root" symbolic-ref --short -q HEAD 2>/dev/null)
    git -C "$root" ls-files 2>/dev/null > "$pc_tmp/files"
    git -C "$root" log -50 --no-merges --format='S%x09%s' 2>/dev/null > "$pc_tmp/log"
    local envs=() e
    for e in "$root"/.env* "$PWD"/.env*; do [ -f "$e" ] && [ -r "$e" ] && envs+=("$e"); done
    hits=$(PC_BRANCH="$branch" PC_VALS="$vals" PC_FILES="$pc_tmp/files" PC_LOG="$pc_tmp/log" awk "$PC_LOCAL_AWK" "$vals" "$pc_tmp/files" "$pc_tmp/log" ${envs[@]+"${envs[@]}"} 2>/dev/null)
    line=$(printf '%s\n' "$hits" | grep -m1 $'^H\t')
    if [ -n "$line" ]; then
        rest=${line#H$'\t'}; kind=${rest%%$'\t'*}; val=${rest#*$'\t'}
        case "$kind" in
            repo_file_name) privacy_block "$kind" "$val" "The request contains a file name from this repository." ;;
            repo_folder_path) privacy_block "$kind" "$val" "The request contains a path into this repository." ;;
            commit_text) privacy_block "$kind" "$val" "The request contains text from this repository's commit messages." ;;
            branch_name) privacy_block "$kind" "$val" "The request contains this repository's branch name." ;;
            *) privacy_block env_value "" "The request contains a value from this repository's .env file." ;;
        esac
        return
    fi
    toks=$(printf '%s\n' "$hits" | sed -n $'s/^T\t//p' | head -40 | tr '\n' '|')
    toks=${toks%|}
    [ -n "$toks" ] || return 0
    # Fast literal pass first: only files that contain one of the names get the definition search.
    local excl=(':!*.md' ':!*.mdx' ':!*.txt' ':!*.json' ':!*.jsonl' ':!*.csv' ':!*.lock' ':!*.svg' ':!*.map' ':!*.min.js' ':!*.log')
    local lit=() t2
    IFS='|' read -r -a toklist <<< "$toks"
    for t2 in "${toklist[@]}"; do lit+=(-e "$t2"); done
    git -C "$root" grep -l -I -w -F "${lit[@]}" -- . "${excl[@]}" 2>/dev/null | head -200 > "$pc_tmp/cand"
    [ -s "$pc_tmp/cand" ] || return 0
    local cand=()
    while IFS= read -r t2; do cand+=("$t2"); done < "$pc_tmp/cand"
    # Declarations, module-level constants and method definitions; indented const/let/var locals are skipped.
    # Lifecycle and override names from common frameworks are skipped in the
    # local pass (FRAMEWORK), so a file that implements them does not block a question about them.
    # A bare assignment (name = value) counts only for names that are not all-lowercase snake_case: framework
    # config files assign public setting names that way (Celery's worker_prefetch_multiplier = 1).
    local atoks="" t3 assign=()
    for t3 in "${toklist[@]}"; do [[ $t3 =~ ^[a-z][a-z0-9]*(_[a-z0-9]+)+$ ]] || atoks="${atoks:+$atoks|}$t3"; done
    [ -n "$atoks" ] && assign=(-e "^[[:space:]]*(export[[:space:]]+)?($atoks)[[:space:]]*=([^=>]|\$)")
    tok=$(git -C "$root" grep -I -h -o -E \
        -e "(^|[^A-Za-z0-9_\$])(function|class|type|interface|struct|enum|func|fn|trait|impl|module|record|message|service|rpc|object|protocol|typealias|macro)[[:space:]]+[*&]?($toks)([^A-Za-z0-9_\$]|\$)" \
        -e "^(export[[:space:]]+)?(declare[[:space:]]+)?(const|let|var|val)[[:space:]]+($toks)([^A-Za-z0-9_\$]|\$)" \
        -e "(^|[^A-Za-z0-9_])def[[:space:]]+($toks)[[:space:]]*\(" \
        -e "^[[:space:]]+(public[[:space:]]+|private[[:space:]]+|protected[[:space:]]+)?(async[[:space:]]+)?(static[[:space:]]+)?($toks)[[:space:]]*\([^)]*\)[[:space:]]*(:[^{]*)?\{" \
        -e "func[[:space:]]*\([^)]*\)[[:space:]]*($toks)[[:space:]]*\(" \
        ${assign[@]+"${assign[@]}"} \
        -- "${cand[@]}" 2>/dev/null \
        | head -1 | grep -o -E "(^|[^A-Za-z0-9_])($toks)([^A-Za-z0-9_]|\$)" | head -1 | tr -cd 'A-Za-z0-9_')
    if [ -n "$tok" ]; then privacy_block repo_identifier "$tok" "The request contains a name defined in this repository's code."; return; fi
    return 0
}

# pc_id <value>: a set id or id-shaped neighbors query must look like the ids Dorsal returns.
pc_id() {
    local v=$1 re
    if [ ${#v} -gt 300 ]; then privacy_block invalid_id "" "An id is longer than the ids Dorsal returns. Pass ids from a summaries or neighbors result unchanged."; return; fi
    case "$v" in
        chunk:*) re='^chunk:[^"\\[:cntrl:]]+$' ;;
        doc:*) re='^doc:[A-Za-z0-9.-]+\.[A-Za-z]{2,}(/[^"\\[:cntrl:][:space:]]*)?$' ;;
        *) re='^[A-Za-z0-9][^"\\/[:cntrl:][:space:]]*$' ;;
    esac
    [[ $v =~ $re ]] || { privacy_block invalid_id "$v" "This id does not look like an id Dorsal returns. Pass ids from a summaries or neighbors result unchanged."; return; }
    pc_patterns "$v" id
}

base="${DORSAL_API_URL:-https://api.dorsal.us}"
base="${base%/}"
key_file="${HOME:-}/.dorsal/api-key"
if [ -z "${DORSAL_API_KEY:-}" ] && [ -n "${HOME:-}" ] && [ -r "$key_file" ]; then
    IFS= read -r DORSAL_API_KEY < "$key_file" || true
    DORSAL_API_KEY="${DORSAL_API_KEY//[[:space:]]/}"
fi
if [ -z "${DORSAL_API_KEY:-}" ]; then
    printf '%s\n' '{"error":{"type":"setup_error","message":"No Dorsal API key. Ask the user to get one at https://godorsal.com/docs/skill and save it in ~/.dorsal/api-key, or export DORSAL_API_KEY. Never ask them to paste the key into the chat."}}' >&2
    exit 2
fi

method=GET
body=""
case "${1:-}" in
    summaries|set|neighbors)
        method=POST
        path="/v1/$1"
        if [ $# -ge 2 ]; then body="$2"; else body="$(cat)"; fi
        [ -n "$body" ] || usage
        ;;
    protocol)
        case "${2:-}" in
            dorsal)
                case "${3:-investigate}" in investigate|improve|audit) ;; *) usage ;; esac
                path="/v1/protocols/dorsal?mode=${3:-investigate}"
                ;;
            decision-verification) path="/v1/protocols/decision-verification" ;;
            audit)
                phase="${3:-audit}"
                case "$phase" in audit|fix) ;; *) usage ;; esac
                # The skill's install folder names the client: ~/.codex/skills or ~/.claude/skills.
                case "$0" in */.codex/*) client=codex-skill ;; *) client=claude-code-skill ;; esac
                path="/v1/protocols/audit?phase=$phase&client=$client"
                if [ -n "${4:-}" ]; then
                    case "$4" in *[!a-z_,]*) usage ;; esac
                    path="$path&sections=$4"
                fi
                ;;
            *) usage ;;
        esac
        ;;
    index) path="/v1" ;;
    *) usage ;;
esac

if [ "$method" = POST ]; then
    privacy_check "$1" "$body" || exit 3
fi

out="$(mktemp)"
headers="$(mktemp)"
trap 'rm -f "$out" "$headers"' EXIT

attempt=1
while :; do
    args=(-sS --max-time 60 -o "$out" -D "$headers" -w '%{http_code}')
    # The process substitution must sit on the curl command itself; its descriptor closes after that command.
    if [ "$method" = POST ]; then
        status="$(printf '%s' "$body" | curl "${args[@]}" -K <(printf 'header = "Authorization: Bearer %s"\n' "$DORSAL_API_KEY") -H 'Content-Type: application/json' --data-binary @- "$base$path")" || status="000"
    else
        status="$(curl "${args[@]}" -K <(printf 'header = "Authorization: Bearer %s"\n' "$DORSAL_API_KEY") "$base$path")" || status="000"
    fi
    if { [ "$status" = 429 ] || [ "$status" = 503 ]; } && [ "$attempt" -lt 3 ]; then
        wait="$(awk 'tolower($1) == "retry-after:" { gsub("\r", "", $2); print $2 }' "$headers")"
        case "$wait" in ''|*[!0-9]*) wait=5 ;; esac
        [ "$wait" -gt 30 ] && wait=30
        sleep "$wait"
        attempt=$((attempt + 1))
        continue
    fi
    break
done

if [ "$status" = "000" ]; then
    printf '{"error":{"type":"connection_error","message":"No HTTP response from %s (curl error above). Check DORSAL_API_URL and the network."}}\n' "$base" >&2
    exit 1
fi
if [ "$status" = 200 ]; then
    cat "$out"
    printf '\n'
    exit 0
fi
cat "$out" >&2
printf '\n' >&2
exit 1
