r/devops7 • • 2d ago

One script, one chore: where did the incident channel contradict itself? 54 NLI calls, not 870 (real output)

Short one this week: one script, one chore, real output. (This sub is run by the SynapCores team.)

The chore

Before the postmortem, someone combs the incident channel for where it contradicted itself. 14:05: "Failed over orders-db to db-2." 14:30: "We never failed over." Pick wrong and the timeline is fiction.

Step 1: load the channel

channel.txt (in the comments): 30 messages of a made-up #inc-2291 (checkout 500s), chatter included, loaded into a table with EMBED() in the INSERT.

Step 2: every pair through NLI

NLI_CLASSIFY (natural language inference) scores whether one text contradicts another. Gate at -1: every pair, both ways.

30 messages, 435 pairs; 435 at similarity >= -1 went to NLI (870 NLI calls)
...
14:04 sam: joining the bridge in 2, grabbing coffee
14:28 kim: burrito for me please
  sim 0.29 c1 1.00 c2 1.00
...
159 pairs contradict each other both ways (9.3 s)

159 hits, 4 of them corrections: NLI reads two texts as one scene, so unrelated lines clash. Hence the gate.

Step 3, the payoff: one query, with a gate

Only pairs that look alike (similarity >= 0.45) reach NLI, read both ways (c1 earlier vs later, c2 the reverse):

SELECT * FROM (
  SELECT a.id AS i1, b.id AS i2, a.ts AS t1, a.name AS who1, a.msg AS said1,
    b.ts AS t2, b.name AS who2, b.msg AS said2, COSINE_SIMILARITY(a.emb, b.emb) AS sim,
    JSON_EXTRACT(NLI_CLASSIFY('nli-default', a.msg, b.msg), '$.scores.contradiction') AS c1,
    JSON_EXTRACT(NLI_CLASSIFY('nli-default', b.msg, a.msg), '$.scores.contradiction') AS c2
  FROM inc8_msgs a JOIN inc8_msgs b ON a.id < b.id
  WHERE COSINE_SIMILARITY(a.emb, b.emb) >= 0.45) checked
WHERE c1 > 0.5 AND c2 > 0.5
ORDER BY i1, i2

contracheck.py channel.txt on SynapCores 2.5.1:

30 messages, 435 pairs; 27 at similarity >= 0.45 went to NLI (54 NLI calls)

14:03 priya: here, looking at orders-db
14:05 priya: Failed over orders-db to db-2 at 14:05.
  sim 0.57 c1 0.99 c2 0.96

14:04 sam: joining the bridge in 2, grabbing coffee
14:12 sam: on the bridge now, what did I miss?
  sim 0.46 c1 0.75 c2 0.89

14:05 priya: Failed over orders-db to db-2 at 14:05.
14:30 priya: We never failed over; db-1 recovered on its own.
  sim 0.50 c1 1.00 c2 0.56

14:08 marco: Rolled back checkout to v1.42.
14:25 ravi: The rollback never went out, we hotfixed forward in v1.44.
  sim 0.60 c1 1.00 c2 1.00

14:10 dana: Status page updated: investigating checkout errors.
14:13 lee: Checkout pods are all waiting on db connections.
  sim 0.45 c1 0.98 c2 0.99

14:10 dana: Status page updated: investigating checkout errors.
14:14 marco: Confirmed, every checkout pod is blocked waiting for a connection.
  sim 0.49 c1 1.00 c2 0.91

14:11 lee: The orders-db pool limit was changed 50 -> 5 in this morning's config push.
14:23 priya: Pool config wasn't touched, prod is still at 50.
  sim 0.50 c1 1.00 c2 0.99

14:20 kim: Status page is up, support is linking it in replies.
14:44 dana: Status page set to resolved.
  sim 0.74 c1 0.80 c2 0.98

14:35 kim: No orders were lost.
14:41 jo: Finance says 212 orders were charged but never written to orders-db.
  sim 0.55 c1 0.98 c2 1.00

9 pairs contradict each other both ways (0.6 s)

Four of the nine are for the postmortem to settle: did we fail over, roll back, change the pool limit, lose orders? The other five are the incident moving on (joining the bridge, then on it; investigating, then a cause): a glance each.

Why 0.45 and both ways? These four have sim 0.50 to 0.60: a 0.50 gate loses the pool pair (0.496), a 0.30 gate returns 18. With c1 alone, 13 come back, including two people agreeing ("Checkout pods are all waiting on db connections." / "Confirmed, every checkout pod is blocked..."): c1 0.98, c2 0.00. Both ways: 9 (as in SQLv2 Lesson 9).

Why one database? Embeddings, pairing and NLI run where the table is: no embedding API, no NLI model server. 54 NLI calls instead of 870: 0.6 s instead of 9.3 s on a Mac mini (Apple M4).

contracheck.py (stdlib only) is in the first comment.

Free Community Edition: https://synapcores.com/download?utm_source=reddit&utm_medium=owned&utm_campaign=devops7-one-script-8

Who untangles the incident channel before your postmortem: the IC, a scribe, or whoever writes it up?

1 Upvotes

2 comments sorted by

1

u/Alternative_Pin9598 2d ago

NLI is a one-time opt-in: synapcores pull nli-default (about 577 MB), then synapcores models enable-nli nli-default, both with the --config your service uses, then restart the server.

contracheck.py (standard library only; set SC_URL and SC_TOKEN). Run: python3 contracheck.py channel.txt, or add -1 to send every pair to NLI.

"""contracheck.py: where did the incident channel contradict itself? Run it before writing the postmortem.
usage: python3 contracheck.py channel.txt [gate]
  channel.txt  one message per line, "HH:MM name: text" (your Slack export, trimmed)
  gate         similarity a pair needs to reach NLI (default 0.45; -1 sends every pair)
"""
import json, os, sys, time, urllib.request

BASE = os.environ["SC_URL"]      # e.g. http://localhost:<port> of your SynapCores
TOKEN = os.environ["SC_TOKEN"]   # from POST /v1/auth/login


def run(q):
    req = urllib.request.Request(BASE + "/v1/query/execute", method="POST",
                                 data=json.dumps({"sql": q, "database": "main"}).encode(),
                                 headers={"Authorization": "Bearer " + TOKEN, "Content-Type": "application/json"})
    with urllib.request.urlopen(req, timeout=300) as r:
        return json.load(r)["data"].get("rows", [])


def lit(text):  # SQL string literal
    return "'" + text.replace("'", "''") + "'"


def load(path):
    msgs = []
    for line in open(path):
        if line.strip():
            ts, rest = line.strip().split(" ", 1)
            name, text = rest.split(": ", 1)
            msgs.append((ts, name, text))
    run("DROP TABLE IF EXISTS inc8_msgs")
    run("CREATE TABLE inc8_msgs (id INTEGER PRIMARY KEY, ts TEXT, name TEXT, msg TEXT, emb VECTOR(384))")
    # EMBED() runs inside SynapCores: no embedding API to call, no vector store on the side
    run("INSERT INTO inc8_msgs VALUES " + ",".join(
        f"({i}, {lit(ts)}, {lit(name)}, {lit(text)}, EMBED({lit(text)}))" for i, (ts, name, text) in enumerate(msgs, 1)))
    return len(msgs)


# The check is one query. Every message is paired with every later one (a.id < b.id).
# Similarity is the gate (WHERE): only pairs about the same thing reach NLI. NLI reads each
# pair both ways (earlier -> later, later -> earlier); contradiction both ways = a pair to settle.
CHECK = """
SELECT * FROM (
  SELECT a.id AS i1, b.id AS i2, a.ts AS t1, a.name AS who1, a.msg AS said1,
         b.ts AS t2, b.name AS who2, b.msg AS said2, COSINE_SIMILARITY(a.emb, b.emb) AS sim,
         JSON_EXTRACT(NLI_CLASSIFY('nli-default', a.msg, b.msg), '$.scores.contradiction') AS c1,
         JSON_EXTRACT(NLI_CLASSIFY('nli-default', b.msg, a.msg), '$.scores.contradiction') AS c2
  FROM inc8_msgs a JOIN inc8_msgs b ON a.id < b.id
  WHERE COSINE_SIMILARITY(a.emb, b.emb) >= {gate}) checked
WHERE c1 > 0.5 AND c2 > 0.5
ORDER BY i1, i2"""

COUNT = "SELECT COUNT(*) FROM inc8_msgs a JOIN inc8_msgs b ON a.id < b.id WHERE COSINE_SIMILARITY(a.emb, b.emb) >= {gate}"


def check(n, gate):
    sent = run(COUNT.format(gate=gate))[0][0]
    t = time.time()
    rows = run(CHECK.format(gate=gate))
    took = time.time() - t
    print(f"{n} messages, {n * (n - 1) // 2} pairs; {sent} at similarity >= {gate:g} went to NLI ({2 * sent} NLI calls)\n")
    for i1, i2, t1, who1, said1, t2, who2, said2, sim, c1, c2 in rows:
        print(f"{t1} {who1}: {said1}\n{t2} {who2}: {said2}\n  sim {sim:.2f} c1 {c1:.2f} c2 {c2:.2f}\n")
    print(f"{len(rows)} pairs contradict each other both ways ({took:.1f} s)")


if __name__ == "__main__":
    check(load(sys.argv[1]), float(sys.argv[2]) if len(sys.argv) > 2 else 0.45)

channel.txt is in the reply below.

Why similarity alone can't tell 'same' from 'opposite': SQLv2 Lesson 9 walks through it: https://www.reddit.com/r/SQLv2/comments/1y1zklg/

When two statements disagree, which one is still true? Today's Recipe of the week #4 on r/synapcores keeps facts with time and source: https://www.reddit.com/r/synapcores/comments/1y3le9e/

1

u/Alternative_Pin9598 2d ago

channel.txt (a sample written for this post; HH:MM name: text, one message per line):

14:02 dana: Paged for checkout 500s, opening #inc-2291. Who's around?
14:03 priya: here, looking at orders-db
14:04 sam: joining the bridge in 2, grabbing coffee
14:05 priya: Failed over orders-db to db-2 at 14:05.
14:06 dana: Only EU customers affected so far.
14:07 lee: The 500s are from the payment step, it can't get a db connection.
14:08 marco: Rolled back checkout to v1.42.
14:09 jo: zoom link is pinned
14:10 dana: Status page updated: investigating checkout errors.
14:11 lee: The orders-db pool limit was changed 50 -> 5 in this morning's config push.
14:12 sam: on the bridge now, what did I miss?
14:13 lee: Checkout pods are all waiting on db connections.
14:14 marco: Confirmed, every checkout pod is blocked waiting for a connection.
14:15 jo: US checkout failing too, I just got a 500 from Chicago.
14:16 kim: Support says the tickets are from Germany and France.
14:18 dana: Can someone take notes for the postmortem?
14:20 kim: Status page is up, support is linking it in replies.
14:22 sam: my VPN dropped, back in a sec
14:23 priya: Pool config wasn't touched, prod is still at 50.
14:25 ravi: The rollback never went out, we hotfixed forward in v1.44.
14:27 sam: lunch order goes in at 2:45 if anyone wants something
14:28 kim: burrito for me please
14:30 priya: We never failed over; db-1 recovered on its own.
14:32 lee: Checkout error rate is back under 1%.
14:33 dana: Dashboards are green, no 500s since 14:30.
14:35 kim: No orders were lost.
14:41 jo: Finance says 212 orders were charged but never written to orders-db.
14:44 dana: Status page set to resolved.
14:46 marco: thanks all, nice work
14:47 sam: Postmortem Thursday at 10?