samai-8b-M8 / artifacts /m15_scripts /zpatch_ws_v4.py
tchbcb's picture
R9 scripts: zpatch_ws_v4.py
3817c4f verified
Raw History Blame Contribute Delete
1.83 kB
import os
WS = "/tmp/k12/src/web_stream.go"
def rd(p): return open(p).read()
def wr(p, s): open(p, "w").write(s)
def ws_tmpl(ind):
return (
ind + '// [ANTI-LOOP FIX 2026-09-22]\n'
+ ind + 'alSig, _ := json.Marshal(args)\n'
+ ind + 'if blocked, aln := antiLoopCheck(conv.ID, tc.Function.Name+"|"+truncate(string(alSig), 400)); blocked {\n'
+ ind + '\tlog.Printf("[ANTI-LOOP] blocked repeat #%d %s", aln, truncate(tc.Function.Name, 40))\n'
+ ind + '\tblockMsg := "HARD SYSTEM BLOCK: this exact tool call has already been executed " + fmt.Sprintf("%d", aln) + " times in a row with unchanged context. The path is DEAD. You MUST now either (a) switch to a completely different tool/source/approach, or (b) if your evidence is sufficient, output the final answer on the last line in the format: FINAL ANSWER: <answer>. Do NOT repeat this call."\n'
+ ind + '\thistory = append(history, LLMMessage{Role: "tool", Content: blockMsg, ToolCallID: tc.ID})\n'
+ ind + '\tconv.Messages = append(conv.Messages, WebChatMessage{Role: "tool", ToolName: tc.Function.Name, ToolArgs: args, ToolResult: blockMsg, ToolOK: false, ToolCallID: tc.ID, Timestamp: nextConvTimestamp(conv)})\n'
+ ind + '\tcontinue\n'
+ ind + '}\n')
code = rd(WS)
assert "antiLoopCheck(" not in code, "ws not clean"
needle = "result := loopToolCtx.Execute(tc.Function.Name, args)"
assert code.count("nextConvTimestamp(") >= 2
prev = 0
for n in range(2):
pos = code.find(needle, prev)
assert pos > 0, "ws site %d missing" % (n + 1)
ls = code.rfind("\n", 0, pos) + 1
le = code.find("\n", pos)
line = code[ls:le]
ind = line[:len(line) - len(line.lstrip("\t"))]
blk = ws_tmpl(ind)
print("WS_SITE%d ind=%d" % (n + 1, len(ind)))
code = code[:ls] + blk + code[ls:]
prev = ls + len(blk) + len(needle)
wr(WS, code)
print("WS_V4_PATCH_DONE")