akm-opencode 0.9.202808220049 → 0.9.1202608250804
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -2
- package/index.ts +1164 -5
- package/package.json +1 -1
- package/shared/memory-events.ts +4 -0
package/README.md
CHANGED
|
@@ -36,11 +36,12 @@ The plugin subscribes to OpenCode lifecycle events. Hook failures are logged thr
|
|
|
36
36
|
| `session.updated` | Backfills hints and the workflow summary for a session the plugin has not prepared yet. It does not re-run session-created work. |
|
|
37
37
|
| `chat.message` | Records feedback or memory intent and can schedule non-blocking curation for a substantive prompt. The curate is fire-and-forget: its result is injected on a later turn rather than delaying this one. |
|
|
38
38
|
| `experimental.chat.system.transform` | Injects the AKM guidance and cached curated context into the system prompt. The host rebuilds the system prompt on every request, so these blocks are re-injected each turn — including after a compaction — rather than once per session. |
|
|
39
|
-
| `tool.execute.
|
|
39
|
+
| `tool.execute.before` | The format-declaration write gate (`AKM_WRITE_GATE`, `observe` by default). Blocks the first `edit`/`write` to an existing file that declares a format your stash documents and hands the model the ref to read. Once per file per session, released unconditionally on the retry. |
|
|
40
|
+
| `tool.execute.after` | Tracks concepts used by AKM tools, records deduplicated feedback, and checkpoints session observations. It also records what a file the session `read` declares about its own format, which is what the write gate above keys on. `write` is deliberately not a source: a file the session created is not one it needs the stash to explain. The create itself is recorded on the write's `tool.execute.before` pass, so a later read-back of that file cannot re-arm the gate. |
|
|
40
41
|
| `shell.env` | Exposes `AKM_PROJECT`, `AKM_PLUGIN_VERSION`, and the resolved `AKM_BUNDLE_DIR` to shell tools. |
|
|
41
42
|
| `session.idle` | Runs interval-gated memory extraction (`AKM_AUTO_MEMORY=0` disables it). Fires after every turn, so it is rate-limited. |
|
|
42
43
|
| `session.compacted` | Records a post-compaction event. |
|
|
43
|
-
| `session.deleted` | Refreshes the local AKM index, then drops all per-session state and the temporary curation file. |
|
|
44
|
+
| `session.deleted` | Refreshes the local AKM index, warns once if the write gate saw write-path tool calls but never acted on any, then drops all per-session state and the temporary curation file. |
|
|
44
45
|
|
|
45
46
|
The session observation buffer that retrospective feedback reads from survives every non-terminal event: it is bounded by `AKM_SESSION_BUFFER_MAX_ENTRIES` and dropped only on `session.deleted`. Discarding it at `session.idle` would empty it between turns, so "thanks, that worked" would credit nothing in exactly the sessions that used the most assets.
|
|
46
47
|
|
|
@@ -70,6 +71,7 @@ Every kill switch below is opt-out and reads the same way: only the literal `0`
|
|
|
70
71
|
| `AKM_AUTO_HINTS` | `1` | Set to `0` to skip the per-session `akm hints` call. The missing-bundle warning is deliberately not gated on this: it explains why the stash is empty in the first place. |
|
|
71
72
|
| `AKM_AUTO_MEMORY` | `1` | Set to `0` to disable automatic memory harvesting — the interval-gated `akm proposal extract` on `session.idle`, which is the whole of that harvest here. The Claude plugin honours the same variable for its `SessionEnd` extract, so one setting covers both harnesses. |
|
|
72
73
|
| `AKM_INDEX_ON_SESSION_END` | `1` | Set to `0` to skip the `akm index` refresh. It runs only on `session.deleted` — never on `session.idle`, which fires after every turn. |
|
|
74
|
+
| `AKM_WRITE_GATE` | `observe` | The format-declaration write gate (#99). When a file the session **read** declares a format your stash documents — an `apiVersion:` namespace, a `yaml-language-server` pragma, a `$schema` key, an XML root namespace — and that asset has not been opened this session, the FIRST `edit`/`write` to that file is blocked once and the model is told which ref to read. It ships in `observe`: everything runs and the would-fire count lands in the ledger, but nothing is blocked. Set `enforce` to block, or `off`/`0` to disable it entirely. An unrecognized value is a configuration error — it logs one error per process and the gate refuses to run rather than guessing a default. It never fires on a file this session created: the create is recorded when it happens — a `write` to a path this session has not read, or an `edit` with an empty `oldString` — and that path stays insulated for the rest of the session, so reading back the model's own output does not re-arm it (ledger reason `session-created`). It self-disables when the AKM CLI does not resolve, and it is inert on `apply_patch` (which carries no file path) — that case logs one warning per process rather than failing quietly. |
|
|
73
75
|
|
|
74
76
|
### Scope
|
|
75
77
|
|
package/index.ts
CHANGED
|
@@ -58,6 +58,76 @@ const AKM_PENDING_PROPOSAL_TIMEOUT_MS = Math.max(500, (Number(process.env.AKM_PE
|
|
|
58
58
|
const AKM_CURATE_LIMIT = Math.max(1, Number(process.env.AKM_CURATE_LIMIT ?? "5") || 5)
|
|
59
59
|
const AKM_CURATE_MIN_CHARS = Math.max(1, Number(process.env.AKM_CURATE_MIN_CHARS ?? "16") || 16)
|
|
60
60
|
const AKM_CURATE_TIMEOUT_MS = Math.max(1_000, (Number(process.env.AKM_CURATE_TIMEOUT ?? "8") || 8) * 1_000)
|
|
61
|
+
// --- write gate (#99) -------------------------------------------------------
|
|
62
|
+
// #94 and #95 both moved engagement by rewording the prompt, and both left the
|
|
63
|
+
// one cell that matters untouched: editing a file whose format the model does
|
|
64
|
+
// NOT know sat at 20% (4/20) while the create-shaped equivalent hit 96%.
|
|
65
|
+
// Splitting the Harbor A/B tasks by whether the akm arm ever called a tool put
|
|
66
|
+
// a number on why a third rewording is not the answer — mean paired reward
|
|
67
|
+
// delta was -0.011 on the 29 tasks where no akm_* tool was called and +0.561 on
|
|
68
|
+
// the 19 where one was. Injected context is worth approximately zero; a tool
|
|
69
|
+
// call is worth everything. So this is the first akm behaviour that removes the
|
|
70
|
+
// wrong action instead of adding an argument for the right one: when a file the
|
|
71
|
+
// session READ declares a format the stash documents and that asset has not
|
|
72
|
+
// been opened this session, the first edit/write to it throws, and the model
|
|
73
|
+
// receives the gate message as the edit tool's error result. Under the shipped
|
|
74
|
+
// default (`observe`) that last step is recorded and not taken — see
|
|
75
|
+
// resolveWriteGateMode().
|
|
76
|
+
// "invalid" is a resolved state of the setting, not a mode anyone can ask for:
|
|
77
|
+
// it is what an unrecognized AKM_WRITE_GATE value becomes so the misconfiguration
|
|
78
|
+
// travels all the way into the ledger instead of dissolving into a default.
|
|
79
|
+
type GateMode = "off" | "observe" | "enforce" | "invalid"
|
|
80
|
+
// The raw value that failed to resolve, kept only so the once-per-process warn
|
|
81
|
+
// below can quote what the operator actually typed.
|
|
82
|
+
let writeGateInvalidValue: string | undefined
|
|
83
|
+
function resolveWriteGateMode(raw: string | undefined): GateMode {
|
|
84
|
+
writeGateInvalidValue = undefined
|
|
85
|
+
const v = (raw ?? "").trim().toLowerCase()
|
|
86
|
+
// Unset ships as `observe`: ledger-only, no behaviour change. #99 measured
|
|
87
|
+
// the problem; it did not measure this gate's effect on reward, and the
|
|
88
|
+
// promotion to `enforce` is a decision a train-slice histogram of `write_gate`
|
|
89
|
+
// reasons has to justify. Defaulting to `enforce` would have inverted the
|
|
90
|
+
// agreed rollout by making stage 2 the thing that ships.
|
|
91
|
+
if (!v) return "observe"
|
|
92
|
+
if (v === "off" || v === "0") return "off"
|
|
93
|
+
if (v === "observe") return "observe"
|
|
94
|
+
if (v === "enforce" || v === "1") return "enforce"
|
|
95
|
+
// Never silently fall back to a default. A typo here (`enfroce`, `on`, `true`)
|
|
96
|
+
// would otherwise produce a histogram in a mode nobody chose, which is the
|
|
97
|
+
// failure this whole feature's ledger exists to make impossible. Same
|
|
98
|
+
// treatment as apply_patch below — one loud warning per process plus a typed
|
|
99
|
+
// skip on every watched call — and it refuses to run rather than guessing.
|
|
100
|
+
writeGateInvalidValue = raw
|
|
101
|
+
return "invalid"
|
|
102
|
+
}
|
|
103
|
+
// `let`, not `const`, only so __resetWriteGateForTests() can re-read the env the
|
|
104
|
+
// way __resetResolvedAkmForTests() re-resolves the CLI: one process runs every
|
|
105
|
+
// test, and a mode captured at import would pin the first test's env for all of
|
|
106
|
+
// them. Nothing in the plugin reassigns it.
|
|
107
|
+
let AKM_WRITE_GATE: GateMode = resolveWriteGateMode(process.env.AKM_WRITE_GATE)
|
|
108
|
+
const WRITE_GATE_HEAD_BYTES = 4096
|
|
109
|
+
const WRITE_GATE_RESOLVE_TIMEOUT_MS = 750
|
|
110
|
+
const WRITE_GATE_INFLIGHT_WAIT_MS = 400
|
|
111
|
+
const WRITE_GATE_DESC_CHARS = 240
|
|
112
|
+
const WRITE_GATE_MESSAGE_CHARS = 600
|
|
113
|
+
const WRITE_GATE_SESSION_PATH_CAP = 64
|
|
114
|
+
const WRITE_GATE_IDENTITY_CACHE_CAP = 256
|
|
115
|
+
// A negative resolution is a statement about the stash at one instant, and the
|
|
116
|
+
// stash changes under a live session — `akm import`, `akm clone`, a sync that
|
|
117
|
+
// lands the very asset the gate would have pointed at. The first version cached
|
|
118
|
+
// "no" for the life of the process, which outlives many sessions, so a token
|
|
119
|
+
// that started resolving five minutes later could never resolve again. Positive
|
|
120
|
+
// resolutions stay permanent: an asset that exists keeps existing, and a
|
|
121
|
+
// drifted one-line description is not worth re-running a search for.
|
|
122
|
+
const WRITE_GATE_NEGATIVE_TTL_MS = 5 * 60 * 1000
|
|
123
|
+
// Exactly the opencode 1.18 write-path tool ids, verified against the installed
|
|
124
|
+
// binary's tool schemas: edit -> {filePath, oldString, newString},
|
|
125
|
+
// write -> {content, filePath}, apply_patch -> {patchText}. `patch` and
|
|
126
|
+
// `multiedit` are NOT opencode tool ids (they are Claude Code's) and listing
|
|
127
|
+
// them would be dead weight; apply_patch replaces edit+write on `gpt-*`
|
|
128
|
+
// non-oss non-gpt-4 models, so omitting it would make the gate dark for a
|
|
129
|
+
// whole model family rather than merely inert.
|
|
130
|
+
const WATCHED_WRITE_TOOLS = new Set(["edit", "write", "apply_patch"])
|
|
61
131
|
// 13: "Memory leaks" — sessionBuffer previously grew without bound for the
|
|
62
132
|
// life of a session (a long-running session accumulates one entry per
|
|
63
133
|
// observed tool ref / memory intent). Cap it drop-oldest, matching the
|
|
@@ -129,7 +199,7 @@ const AKM_WORKFLOW_INSTRUCTION = [
|
|
|
129
199
|
"",
|
|
130
200
|
"Use AKM as a reusable knowledge and workflow stash.",
|
|
131
201
|
"",
|
|
132
|
-
"Before writing
|
|
202
|
+
"Before writing or editing anything whose exact syntax or keys you are not certain of (a file already in the workspace included):",
|
|
133
203
|
"1. Use `akm_curate` with a query that includes the current project name/domain (primary discovery). Fall back to `akm_search` only when you already know an asset exists and need its exact ref.",
|
|
134
204
|
"2. Use `akm_show <ref>` before relying on an asset.",
|
|
135
205
|
"3. Record `akm_feedback` after the result is known.",
|
|
@@ -397,6 +467,15 @@ function clearSessionState(sessionID: string): void {
|
|
|
397
467
|
sessionLastExtractAt.delete(sessionID)
|
|
398
468
|
pendingProposalSummaryCache.delete(sessionID)
|
|
399
469
|
retrospectiveState.delete(sessionID)
|
|
470
|
+
// #99 write gate: four more session-keyed maps, torn down here for the same
|
|
471
|
+
// reason as the rest — a re-created session must not inherit a stale latch
|
|
472
|
+
// (which would silently disable the gate) or a stale file identity, and must
|
|
473
|
+
// not inherit a create record either: a NEW session editing that same path is
|
|
474
|
+
// editing a file it did not write.
|
|
475
|
+
sessionFileIdentity.delete(sessionID)
|
|
476
|
+
sessionGateLatched.delete(sessionID)
|
|
477
|
+
sessionShownRefs.delete(sessionID)
|
|
478
|
+
sessionCreatedPaths.delete(sessionID)
|
|
400
479
|
}
|
|
401
480
|
|
|
402
481
|
// Test-only: expose the curated tmp-file directory so tests can assert file
|
|
@@ -951,6 +1030,15 @@ function extractToolRefs(
|
|
|
951
1030
|
if (hit && typeof hit === "object") addMatches((hit as Record<string, unknown>).ref)
|
|
952
1031
|
}
|
|
953
1032
|
}
|
|
1033
|
+
// akmCurate returns { query, summary, items } — not `hits` — so without this
|
|
1034
|
+
// branch a curate call yields no refs at all and nothing downstream (the
|
|
1035
|
+
// #99 already-shown credit, tool_observation, the feedback buffer) can see
|
|
1036
|
+
// what the model was handed.
|
|
1037
|
+
if (Array.isArray(o.items)) {
|
|
1038
|
+
for (const item of o.items) {
|
|
1039
|
+
if (item && typeof item === "object") addMatches((item as Record<string, unknown>).ref)
|
|
1040
|
+
}
|
|
1041
|
+
}
|
|
954
1042
|
if (toolName === "akm_remember" && typeof o.ref === "string") addMatches(o.ref)
|
|
955
1043
|
}
|
|
956
1044
|
|
|
@@ -971,10 +1059,962 @@ function extractAkmRefsFromAllArgs(args: Record<string, unknown>): string[] {
|
|
|
971
1059
|
return [...refs]
|
|
972
1060
|
}
|
|
973
1061
|
|
|
1062
|
+
// --- write gate: state (#99) ------------------------------------------------
|
|
1063
|
+
// The four maps below are per-session, keyed by OpenCode sessionID exactly like
|
|
1064
|
+
// sessionHints et al, and torn down in clearSessionState() — the file's single
|
|
1065
|
+
// teardown point.
|
|
1066
|
+
|
|
1067
|
+
// What a `read` told us about one file. Recorded even when it declared NOTHING,
|
|
1068
|
+
// because "read it, no declaration" and "never read it" are different answers
|
|
1069
|
+
// and the gate has to be able to say which one it is.
|
|
1070
|
+
type FileObservation = {
|
|
1071
|
+
tokens: string[]
|
|
1072
|
+
// False when the read output did not carry the envelope this module parses —
|
|
1073
|
+
// see readOutputRecognized(). Only consulted when `tokens` is empty.
|
|
1074
|
+
recognized: boolean
|
|
1075
|
+
}
|
|
1076
|
+
const sessionFileIdentity = new Map<string, Map<string, FileObservation>>()
|
|
1077
|
+
const sessionGateLatched = new Map<string, Set<string>>()
|
|
1078
|
+
const sessionShownRefs = new Map<string, Set<string>>()
|
|
1079
|
+
|
|
1080
|
+
// Paths this session CREATED. Permanent for the life of the session, and the
|
|
1081
|
+
// reason it has to be permanent is the whole of the #99 round-3 defect: the
|
|
1082
|
+
// previous insulation was "the gate only acts where a `read` observed the
|
|
1083
|
+
// file", which held right up until the model VERIFIED ITS OWN OUTPUT. Write
|
|
1084
|
+
// /app/service.yaml, read it back, fix it up — the read-back writes an
|
|
1085
|
+
// observation for a path this session invented, the gate re-arms, and the
|
|
1086
|
+
// blocked edit lands in the middle of the fictional-create (96%) and real-create
|
|
1087
|
+
// (29%) cells whose attribution the create/edit split exists to protect.
|
|
1088
|
+
// Reproduced end to end against enforce mode before this map existed.
|
|
1089
|
+
const sessionCreatedPaths = new Map<string, Set<string>>()
|
|
1090
|
+
|
|
1091
|
+
type Resolution =
|
|
1092
|
+
// `cause` splits the two answers the first version collapsed into one word:
|
|
1093
|
+
// the search returned NOTHING for this token (the stash has no such asset, or
|
|
1094
|
+
// the index is stale/empty) versus it returned hits and none of them DECLARED
|
|
1095
|
+
// the format (the ranker generated candidates, the classifier rejected them
|
|
1096
|
+
// all). Those are a coverage problem and a precision problem respectively, and
|
|
1097
|
+
// a histogram that cannot separate them cannot be acted on.
|
|
1098
|
+
//
|
|
1099
|
+
// #99 review, blocker A: while hyphenated and dotted tokens were structurally
|
|
1100
|
+
// unmatchable, every one of them landed in the precision bucket — so the
|
|
1101
|
+
// highest-volume bucket of the stage-1 histogram was mis-labelled, and that
|
|
1102
|
+
// histogram is the instrument the promote-to-enforce decision reads. The
|
|
1103
|
+
// matcher is fixed; the bucket is renamed to say what it now means.
|
|
1104
|
+
| { status: "resolved"; ref: string; description: string }
|
|
1105
|
+
| { status: "none"; cause: "no-search-hits" | "no-declaration" }
|
|
1106
|
+
| { status: "error"; reason: "search-timeout" | "search-error" }
|
|
1107
|
+
|
|
1108
|
+
// Process-wide, not per-session: a format token resolves to the same asset for
|
|
1109
|
+
// every session in the process, and caching the NEGATIVE and ERROR answers too
|
|
1110
|
+
// is what keeps a miss at one query per process instead of one per edit. The
|
|
1111
|
+
// negative and error entries expire (WRITE_GATE_NEGATIVE_TTL_MS); a resolved
|
|
1112
|
+
// one never does.
|
|
1113
|
+
type CacheEntry = { resolution: Resolution; expiresAt: number }
|
|
1114
|
+
const identityCache = new Map<string, CacheEntry>()
|
|
1115
|
+
const identityInflight = new Map<string, Promise<Resolution>>()
|
|
1116
|
+
|
|
1117
|
+
// The ONLY read path into identityCache. Expiry is evaluated on read rather
|
|
1118
|
+
// than on a timer so nothing has to hold the process open, and the entry is
|
|
1119
|
+
// deleted on the way out so the next `read` of a file declaring that token
|
|
1120
|
+
// re-warms it.
|
|
1121
|
+
function cachedResolution(token: string): Resolution | undefined {
|
|
1122
|
+
const entry = identityCache.get(token)
|
|
1123
|
+
if (!entry) return undefined
|
|
1124
|
+
if (entry.expiresAt <= Date.now()) {
|
|
1125
|
+
identityCache.delete(token)
|
|
1126
|
+
return undefined
|
|
1127
|
+
}
|
|
1128
|
+
return entry.resolution
|
|
1129
|
+
}
|
|
1130
|
+
|
|
1131
|
+
// Inert-latch bookkeeping. Without it this feature can ship completely dead —
|
|
1132
|
+
// every ledger event still looks healthy, because "no event" is exactly what a
|
|
1133
|
+
// broken read-output parse produces. See the session.deleted warn below.
|
|
1134
|
+
let gateEverActed = false
|
|
1135
|
+
let gateWatchedInvocations = 0
|
|
1136
|
+
let gateInertWarned = false
|
|
1137
|
+
let applyPatchWarned = false
|
|
1138
|
+
let writeGateModeWarned = false
|
|
1139
|
+
const gateSkipReasons = new Map<string, number>()
|
|
1140
|
+
|
|
1141
|
+
type GateReason =
|
|
1142
|
+
| "disabled"
|
|
1143
|
+
| "invalid-mode"
|
|
1144
|
+
| "akm-unresolved"
|
|
1145
|
+
| "apply-patch-unsupported"
|
|
1146
|
+
| "no-file-path"
|
|
1147
|
+
| "create-not-edit"
|
|
1148
|
+
// This session CREATED this path earlier in the session, so every later write
|
|
1149
|
+
// to it — including one that follows a read-back of the model's own output —
|
|
1150
|
+
// is create work, not an edit to pre-existing content. Deliberately its own
|
|
1151
|
+
// word rather than folded into `file-not-read`: an analyst filtering the
|
|
1152
|
+
// stage-1 histogram has to be able to prove the create cells are insulated,
|
|
1153
|
+
// and "no read record" and "we watched this session invent the file" are
|
|
1154
|
+
// different claims (#99 review round 3).
|
|
1155
|
+
| "session-created"
|
|
1156
|
+
| "latched"
|
|
1157
|
+
// The three causes the single `no-identity` used to conflate, in the order
|
|
1158
|
+
// the gate can tell them apart: the session never read this file / it read it
|
|
1159
|
+
// and our parser did not recognize the output / it read it and the file
|
|
1160
|
+
// declares no format authority. Only the last one is the correct-at-zero
|
|
1161
|
+
// real-tool cell; the middle one is the parse bug stage 1 exists to catch.
|
|
1162
|
+
| "file-not-read"
|
|
1163
|
+
| "read-output-unrecognized"
|
|
1164
|
+
| "no-identity"
|
|
1165
|
+
| "resolution-pending"
|
|
1166
|
+
| "no-search-hits"
|
|
1167
|
+
// "hits came back and not one of them DECLARED the format". Named for what it
|
|
1168
|
+
// now means: while hyphenated/dotted tokens were unmatchable this bucket also
|
|
1169
|
+
// collected every structurally-dead token, so the busiest bar of the stage-1
|
|
1170
|
+
// histogram measured a matcher bug rather than a precision result (#99 review,
|
|
1171
|
+
// blocker A).
|
|
1172
|
+
| "no-declaring-asset"
|
|
1173
|
+
| "search-timeout"
|
|
1174
|
+
| "search-error"
|
|
1175
|
+
| "already-shown"
|
|
1176
|
+
| "observe"
|
|
1177
|
+
| "fired"
|
|
1178
|
+
|
|
1179
|
+
type GateDecision = { filePath: string; token: string; ref: string; description: string }
|
|
1180
|
+
|
|
1181
|
+
// Test-only: drop the process-wide resolution caches and the inert-latch
|
|
1182
|
+
// counters, and re-read AKM_WRITE_GATE from the env. Same reason
|
|
1183
|
+
// __resetResolvedAkmForTests() exists — one process runs the whole suite, so a
|
|
1184
|
+
// cache or a mode captured by the first test would otherwise decide the rest.
|
|
1185
|
+
// Deliberately does NOT touch the session-keyed maps: those are torn down by
|
|
1186
|
+
// clearSessionState() on session.deleted, and one test drives the gate against
|
|
1187
|
+
// an identity recorded before the caches were dropped.
|
|
1188
|
+
function __resetWriteGateForTests(): void {
|
|
1189
|
+
AKM_WRITE_GATE = resolveWriteGateMode(process.env.AKM_WRITE_GATE)
|
|
1190
|
+
identityCache.clear()
|
|
1191
|
+
identityInflight.clear()
|
|
1192
|
+
gateSkipReasons.clear()
|
|
1193
|
+
gateEverActed = false
|
|
1194
|
+
gateWatchedInvocations = 0
|
|
1195
|
+
gateInertWarned = false
|
|
1196
|
+
applyPatchWarned = false
|
|
1197
|
+
writeGateModeWarned = false
|
|
1198
|
+
gateLedgerWriteWarned = false
|
|
1199
|
+
}
|
|
1200
|
+
|
|
1201
|
+
// --- write gate: pure functions (#99) ---------------------------------------
|
|
1202
|
+
|
|
1203
|
+
// Kubernetes' own built-in API groups, the two generic schema hosts, and the
|
|
1204
|
+
// code-hosting/CDN labels that serve OTHER people's schemas. Every one of these
|
|
1205
|
+
// identifies a format the model already knows or a host that is not an
|
|
1206
|
+
// authority at all, so letting them through would spend a blocked edit on
|
|
1207
|
+
// nothing. This is a public, principled exclusion list, not a fit to any
|
|
1208
|
+
// benchmark corpus. `json-schema` / `schemastore` appear alongside their `.org`
|
|
1209
|
+
// forms because the URL reduction below strips the TLD before the stoplist is
|
|
1210
|
+
// consulted; the hosting labels are the residual guard for the case where BOTH
|
|
1211
|
+
// halves of a schema URL are generic (`.../schema.json` on raw.githubusercontent
|
|
1212
|
+
// .com), which names nothing and must therefore yield nothing.
|
|
1213
|
+
const WRITE_GATE_IDENTITY_STOPLIST = new Set([
|
|
1214
|
+
"core", "apps", "batch", "policy", "rbac", "networking", "storage", "node", "events", "discovery",
|
|
1215
|
+
"json-schema.org", "json-schema", "schemastore.org", "schemastore",
|
|
1216
|
+
"githubusercontent", "github", "gitlab", "bitbucket", "sourceforge",
|
|
1217
|
+
"jsdelivr", "unpkg", "amazonaws", "cloudfront", "googleapis",
|
|
1218
|
+
])
|
|
1219
|
+
|
|
1220
|
+
// Schema-document filenames that name the file's ROLE for its publisher rather
|
|
1221
|
+
// than the format it describes. When the path stem is one of these the domain
|
|
1222
|
+
// is the more specific half of the URL, which is the only case the host label
|
|
1223
|
+
// is read at all.
|
|
1224
|
+
const WRITE_GATE_GENERIC_SCHEMA_STEMS = new Set([
|
|
1225
|
+
"schema", "schemas", "config", "configuration", "settings", "index", "main", "default",
|
|
1226
|
+
])
|
|
1227
|
+
|
|
1228
|
+
// A file that DOCUMENTS a format is not a file IN that format.
|
|
1229
|
+
//
|
|
1230
|
+
// #99 review: extractFormatIdentity() scanned the first 4KB of ANY file with no
|
|
1231
|
+
// type restriction, so a README quoting `apiVersion: inkwell/v2` in an example
|
|
1232
|
+
// declared inkwell — and the gate then told the user, about their README, that
|
|
1233
|
+
// "this file declares inkwell". Wrong file, false assertion, blocked edit.
|
|
1234
|
+
// Excluded by extension because that is exactly where the quoting happens: an
|
|
1235
|
+
// example lives in a doc, and a doc is named like one. Deliberately NOT
|
|
1236
|
+
// code-fence tracking — fences are a markdown construct, so excluding the
|
|
1237
|
+
// markdown subsumes it, and a second mechanism for the same case is a second
|
|
1238
|
+
// thing to keep correct.
|
|
1239
|
+
const WRITE_GATE_PROSE_EXTENSIONS = new Set([
|
|
1240
|
+
".md", ".markdown", ".mdx", ".rst", ".txt", ".adoc", ".asciidoc",
|
|
1241
|
+
])
|
|
1242
|
+
|
|
1243
|
+
// `/` is permitted because the apiVersion extractor emits the WHOLE declared
|
|
1244
|
+
// string (`inkwell/v2`) as its most specific key. The classifier compares whole
|
|
1245
|
+
// normalized fields, so a slashed or dotted key is matchable; it is only the
|
|
1246
|
+
// old segment-splitting matcher that made them structurally dead (#99 review,
|
|
1247
|
+
// blocker A).
|
|
1248
|
+
const WRITE_GATE_TOKEN_RE = /^[a-z0-9][a-z0-9._/-]{2,63}$/
|
|
1249
|
+
|
|
1250
|
+
// A number is not a format identity. `v1` was the only shape this caught, which
|
|
1251
|
+
// is why the XML root-namespace extractor emitted `4.0.0` for a maven pom and
|
|
1252
|
+
// `2003` for an msbuild project — a version and a year, offered to the user as
|
|
1253
|
+
// the name of their file's format (#99 review, blocker B).
|
|
1254
|
+
const WRITE_GATE_VERSION_RE = /^v?\d+(\.\d+)*$/
|
|
1255
|
+
|
|
1256
|
+
/**
|
|
1257
|
+
* Reduce a $schema value to the ONE token that identifies the schema itself.
|
|
1258
|
+
*
|
|
1259
|
+
* #99 review: the first version read the registrable host label FIRST, so a
|
|
1260
|
+
* compose file carrying
|
|
1261
|
+
* `# yaml-language-server: $schema=https://raw.githubusercontent.com/compose-spec/compose-spec/master/schema/compose-spec.json`
|
|
1262
|
+
* reduced to `githubusercontent` — a CDN, not a schema authority, and nonsense
|
|
1263
|
+
* as a stash query. The rule this module states is "a file that names its own
|
|
1264
|
+
* schema AUTHORITY is telling you where to look", so read the most specific
|
|
1265
|
+
* self-naming part first: the schema DOCUMENT's own name
|
|
1266
|
+
* (compose-spec.json -> `compose-spec`, ./schemas/inkwell.schema.json ->
|
|
1267
|
+
* `inkwell`), and fall back to the publishing DOMAIN only when the document
|
|
1268
|
+
* name is generic and therefore names nothing
|
|
1269
|
+
* (https://opencode.ai/config.json -> `opencode`). Generic on both halves
|
|
1270
|
+
* reduces to nothing at all, which is the honest answer.
|
|
1271
|
+
*/
|
|
1272
|
+
function reduceSchemaReference(raw: string): string | undefined {
|
|
1273
|
+
const value = raw.replace(/^["']|["'],?$/g, "").trim()
|
|
1274
|
+
if (!value) return undefined
|
|
1275
|
+
const urlMatch = /^[a-z][a-z0-9+.-]*:\/\/([^/?#]+)([^?#]*)/i.exec(value)
|
|
1276
|
+
const pathPart = urlMatch ? urlMatch[2]! : value.split(/[?#]/)[0]!
|
|
1277
|
+
const base = pathPart.split("/").filter(Boolean).pop() ?? ""
|
|
1278
|
+
const stem = (base.split(".")[0] ?? "").toLowerCase()
|
|
1279
|
+
if (stem && !WRITE_GATE_GENERIC_SCHEMA_STEMS.has(stem)) return stem
|
|
1280
|
+
if (!urlMatch) return undefined
|
|
1281
|
+
const host = urlMatch[1]!.split("@").pop()!.split(":")[0]!
|
|
1282
|
+
const labels = host.split(".").filter(Boolean)
|
|
1283
|
+
if (labels.length === 0) return undefined
|
|
1284
|
+
return labels.length >= 2 ? labels[labels.length - 2] : labels[0]
|
|
1285
|
+
}
|
|
1286
|
+
|
|
1287
|
+
/**
|
|
1288
|
+
* Extract the format-identity tokens a file DECLARES ABOUT ITSELF from the head
|
|
1289
|
+
* of its content.
|
|
1290
|
+
*
|
|
1291
|
+
* Exactly four extractors, one per way a file can name the authority for its
|
|
1292
|
+
* OWN format: an `apiVersion:` namespace, a `# yaml-language-server: $schema=`
|
|
1293
|
+
* pragma, a `$schema` key, and an XML root namespace.
|
|
1294
|
+
*
|
|
1295
|
+
* The exclusion below is the load-bearing half of this function. Filename and
|
|
1296
|
+
* extension conventions (docker-compose.yml, Dockerfile, *.tf) and
|
|
1297
|
+
* namespaced-looking values in non-identity keys (`image: worker:v3.0.1`,
|
|
1298
|
+
* `model: opencode/bigpickle`) are DELIBERATELY NOT extractors. That exclusion
|
|
1299
|
+
* is the entire reason the gate cannot raise the real/known-tool edit cell,
|
|
1300
|
+
* which measured 0/35 in the #99 A/B and is CORRECT at zero — the model knows
|
|
1301
|
+
* docker compose, and blocking an edit to consult a stash there is wasted work.
|
|
1302
|
+
* The product rule underneath: a file that names its own schema authority is
|
|
1303
|
+
* telling you where to look; a file identified only by a well-known filename is
|
|
1304
|
+
* one the model already knows.
|
|
1305
|
+
*/
|
|
1306
|
+
function extractFormatIdentity(head: string, filePath?: string): string[] {
|
|
1307
|
+
if (typeof head !== "string" || !head) return []
|
|
1308
|
+
// See WRITE_GATE_PROSE_EXTENSIONS: prose describes formats, it does not
|
|
1309
|
+
// declare one.
|
|
1310
|
+
if (typeof filePath === "string" && WRITE_GATE_PROSE_EXTENSIONS.has(path.extname(filePath).toLowerCase())) return []
|
|
1311
|
+
// opencode's `read` wraps file bodies as
|
|
1312
|
+
// `<path>…</path>\n<type>file</type>\n<content>\n1: …` (verified against the
|
|
1313
|
+
// installed 1.18 binary), so scan only past <content> when present.
|
|
1314
|
+
const contentAt = head.indexOf("<content>")
|
|
1315
|
+
const body = (contentAt >= 0 ? head.slice(contentAt + "<content>".length) : head).slice(0, WRITE_GATE_HEAD_BYTES)
|
|
1316
|
+
const raw: string[] = []
|
|
1317
|
+
for (const rawLine of body.split("\n")) {
|
|
1318
|
+
// `read` prefixes EVERY line with its line number (`1: apiVersion:
|
|
1319
|
+
// inkwell/v2`). Dropping this strip is the cheapest way to ship a plugin
|
|
1320
|
+
// that is plausibly, silently inert — no token, no event, no gate, clean
|
|
1321
|
+
// logs. Guarded by a dedicated test against a captured trajectory string.
|
|
1322
|
+
const line = rawLine.replace(/^\s*\d+:\s?/, "")
|
|
1323
|
+
|
|
1324
|
+
const apiVersion = /^\s*apiVersion:\s*["']?([A-Za-z0-9._-]+)\/([A-Za-z0-9._-]+)/.exec(line)
|
|
1325
|
+
if (apiVersion) {
|
|
1326
|
+
const namespace = apiVersion[1]!
|
|
1327
|
+
// Stoplisted on the NAMESPACE, before the keys are built. `apps` is on the
|
|
1328
|
+
// list but `apps/v1` is not, so checking only the finished tokens would
|
|
1329
|
+
// let Kubernetes' own API groups straight back in through the specific
|
|
1330
|
+
// key.
|
|
1331
|
+
if (WRITE_GATE_IDENTITY_STOPLIST.has(namespace.toLowerCase())) continue
|
|
1332
|
+
// Most specific FIRST, and never reduced ahead of the search: `inkwell/v2`
|
|
1333
|
+
// is what the file actually declares, `inkwell` is the fallback, and
|
|
1334
|
+
// gateDecision takes the first key that resolves.
|
|
1335
|
+
//
|
|
1336
|
+
// The old first-dot-label push (platform.acme.com -> `platform`) is gone.
|
|
1337
|
+
// It existed only because the segment-splitting matcher could never match
|
|
1338
|
+
// a dotted token; the classifier now compares whole normalized fields, so
|
|
1339
|
+
// `platform.acme.com` is matchable directly and the lossy reduction has no
|
|
1340
|
+
// job left. Keeping it would keep manufacturing generic English words —
|
|
1341
|
+
// `platform`, `monitoring`, `networking` — and offering them to the user
|
|
1342
|
+
// as the name of their file's format (#99 review, blockers A and C).
|
|
1343
|
+
raw.push(`${namespace}/${apiVersion[2]!}`)
|
|
1344
|
+
raw.push(namespace)
|
|
1345
|
+
continue
|
|
1346
|
+
}
|
|
1347
|
+
|
|
1348
|
+
const yamlLanguageServer = /^\s*#\s*yaml-language-server:\s*\$schema=(\S+)/.exec(line)
|
|
1349
|
+
if (yamlLanguageServer) {
|
|
1350
|
+
raw.push(reduceSchemaReference(yamlLanguageServer[1]!) ?? "")
|
|
1351
|
+
continue
|
|
1352
|
+
}
|
|
1353
|
+
|
|
1354
|
+
const schemaKey = /^\s*["']?\$schema["']?\s*[:=]\s*["']?(\S+)/.exec(line)
|
|
1355
|
+
if (schemaKey) {
|
|
1356
|
+
raw.push(reduceSchemaReference(schemaKey[1]!) ?? "")
|
|
1357
|
+
continue
|
|
1358
|
+
}
|
|
1359
|
+
|
|
1360
|
+
// XML root only, and only the DEFAULT namespace, and only through the same
|
|
1361
|
+
// reduction every other extractor uses.
|
|
1362
|
+
//
|
|
1363
|
+
// #99 review, blocker B: this extractor took the LAST path segment of the
|
|
1364
|
+
// namespace URI raw. Probed against real files that produced `4.0.0` for a
|
|
1365
|
+
// maven pom, `2003` for an msbuild project and `android` for an Android
|
|
1366
|
+
// layout — a version, a year and an operating system, each offered to the
|
|
1367
|
+
// user as the name of their file's format. Two fixes, both structural:
|
|
1368
|
+
// - `xmlns:foo=` is a PREFIX binding for a vocabulary the document
|
|
1369
|
+
// BORROWS (an Android layout borrows the android namespace; its own
|
|
1370
|
+
// format is the layout schema). Only a default `xmlns=` names the
|
|
1371
|
+
// document's own format, so only that one is read. This is what kills
|
|
1372
|
+
// `android`, and it kills it by meaning rather than by denylist.
|
|
1373
|
+
// - the URI goes through reduceSchemaReference(), so the version-shaped
|
|
1374
|
+
// stems fall to WRITE_GATE_VERSION_RE instead of being pushed verbatim.
|
|
1375
|
+
// A pom therefore declares NOTHING, which is the honest answer: nothing
|
|
1376
|
+
// in that URI names the format in a way a stash query could use.
|
|
1377
|
+
if (/^\s*<[A-Za-z_]/.test(line)) {
|
|
1378
|
+
const xmlns = /(?:^|\s)xmlns\s*=\s*["']([^"']+)["']/.exec(line)
|
|
1379
|
+
if (xmlns) {
|
|
1380
|
+
raw.push(reduceSchemaReference(xmlns[1]!) ?? "")
|
|
1381
|
+
continue
|
|
1382
|
+
}
|
|
1383
|
+
}
|
|
1384
|
+
|
|
1385
|
+
// DROPPED, #99 review: `[tool.<name>]` in pyproject.toml and a
|
|
1386
|
+
// `#!/usr/bin/env <interp>` shebang were extractors here and neither one is
|
|
1387
|
+
// a schema authority. `[tool.ruff]` names a TOOL that reads a section of a
|
|
1388
|
+
// file whose format is PEP 518's, and `tsx` names an INTERPRETER, not the
|
|
1389
|
+
// format of the script it runs. Both violated the rule this function is
|
|
1390
|
+
// built on — a file that names its own schema authority is telling you
|
|
1391
|
+
// where to look — so the rule and the code now agree instead of the rule
|
|
1392
|
+
// being aspirational. The four that remain (apiVersion, a
|
|
1393
|
+
// yaml-language-server pragma, a `$schema` key, an XML root namespace) each
|
|
1394
|
+
// name the authority for the WHOLE file.
|
|
1395
|
+
}
|
|
1396
|
+
|
|
1397
|
+
const out: string[] = []
|
|
1398
|
+
for (const candidate of raw) {
|
|
1399
|
+
const token = candidate.toLowerCase()
|
|
1400
|
+
if (!WRITE_GATE_TOKEN_RE.test(token)) continue
|
|
1401
|
+
if (WRITE_GATE_VERSION_RE.test(token)) continue
|
|
1402
|
+
if (WRITE_GATE_IDENTITY_STOPLIST.has(token)) continue
|
|
1403
|
+
if (out.includes(token)) continue
|
|
1404
|
+
out.push(token)
|
|
1405
|
+
if (out.length === 3) break
|
|
1406
|
+
}
|
|
1407
|
+
return out
|
|
1408
|
+
}
|
|
1409
|
+
|
|
1410
|
+
/**
|
|
1411
|
+
* Did this `read` result carry the envelope extractFormatIdentity() is written
|
|
1412
|
+
* against?
|
|
1413
|
+
*
|
|
1414
|
+
* #99 review: the ledger reason `no-identity` conflated three different things,
|
|
1415
|
+
* and one of them was the bug stage 1 exists to catch. "The session never read
|
|
1416
|
+
* this file", "the file declares no format authority" (the real/known-tool
|
|
1417
|
+
* cell, correct at zero) and "our parser did not recognize what `read`
|
|
1418
|
+
* returned" all produced the same word, so the histogram could not tell a
|
|
1419
|
+
* correct zero from a broken parse — the exact failure mode where every other
|
|
1420
|
+
* signal still looks healthy. This is the third cause, made checkable: opencode
|
|
1421
|
+
* 1.18 `read` returns `<path>…</path>\n<type>file</type>\n<content>\n1: …`,
|
|
1422
|
+
* so an output with no `<content>` marker is one this parser was not written
|
|
1423
|
+
* for, whatever else it may be.
|
|
1424
|
+
*/
|
|
1425
|
+
function readOutputRecognized(head: unknown): boolean {
|
|
1426
|
+
return typeof head === "string" && head.includes("<content>")
|
|
1427
|
+
}
|
|
1428
|
+
|
|
1429
|
+
/**
|
|
1430
|
+
* Normalize an identity field the way akm's own indexer normalizes a tag:
|
|
1431
|
+
* hyphen/underscore to space, case folded, whitespace collapsed — and `/` and
|
|
1432
|
+
* `.` preserved verbatim. Preserving those two is the whole of the blocker-A
|
|
1433
|
+
* fix: the previous matcher split fields on /[^a-z0-9]+/ and compared segments,
|
|
1434
|
+
* so a needle containing `/` or `.` could never equal any segment and a needle
|
|
1435
|
+
* containing `-` could never equal one either. `compose-spec` — the single
|
|
1436
|
+
* largest product of reduceSchemaReference(), the decision-4 headline fix — was
|
|
1437
|
+
* therefore unmatchable against an asset literally named `compose-spec`.
|
|
1438
|
+
*/
|
|
1439
|
+
function normalizeIdentityField(value: string): string {
|
|
1440
|
+
return value.toLowerCase().replace(/[_-]+/g, " ").replace(/\s+/g, " ").trim()
|
|
1441
|
+
}
|
|
1442
|
+
|
|
1443
|
+
/**
|
|
1444
|
+
* The classifier, deliberately separate from the ranker.
|
|
1445
|
+
*
|
|
1446
|
+
* akmSearch is a candidate GENERATOR: it will happily return `signwell-automation`
|
|
1447
|
+
* for the query "inkwell" with a high score, because that is what a relevance
|
|
1448
|
+
* ranker is for. Blocking an edit on that would be a false positive that costs a
|
|
1449
|
+
* real user a real round-trip, so the decision to block is made here.
|
|
1450
|
+
*
|
|
1451
|
+
* The rule is DECLARATION, not mention: a hit authorizes the gate only when the
|
|
1452
|
+
* asset's whole normalized `name` equals the whole normalized key. One field,
|
|
1453
|
+
* one comparison, no second way in.
|
|
1454
|
+
*
|
|
1455
|
+
* #99 review, blocker C: the rule before this one was word-membership across
|
|
1456
|
+
* ref/name/tags, and akm SYNTHESIZES tags from the title slug when frontmatter
|
|
1457
|
+
* supplies none — `knowledge/presence-svg-animation-complexity` carries
|
|
1458
|
+
* ["presence","svg","animation","complexity"] with no `tags:` of its own. So
|
|
1459
|
+
* "does a top-5 asset carry this word as a tag" degenerated into "does its title
|
|
1460
|
+
* contain this word", which is the fuzzy match this function's contract says it
|
|
1461
|
+
* excludes. Measured against a real 23k-entry stash, that rule fired on 15 of 34
|
|
1462
|
+
* single-word tokens real files produce, every one of them wrong.
|
|
1463
|
+
*
|
|
1464
|
+
* #99 review round 3: narrowing that to "an AUTHORED tag" was not enough, and
|
|
1465
|
+
* for a reason the authored/synthesized split cannot reach — a hand-written tag
|
|
1466
|
+
* is a TOPIC label, so an asset about jamstack storefronts genuinely carries
|
|
1467
|
+
* `vercel`, and an asset about catalog import/export genuinely carries `xml`.
|
|
1468
|
+
* Both tags are authored and neither is a claim to BE that format. Measured
|
|
1469
|
+
* against the same real stash, the tag clause was the sole authorizer on every
|
|
1470
|
+
* remaining false fire — `vercel`/`netlify` -> jamstack-storefront, `xml` -> two
|
|
1471
|
+
* Salesforce/catalog assets, `jest` -> a mocking memory, `rollup` -> a bundling
|
|
1472
|
+
* memory — so the clause is gone rather than narrowed again. The benchmark's own
|
|
1473
|
+
* true positive does not need it: the key ladder emits `inkwell/v2` and then
|
|
1474
|
+
* `inkwell`, and the fixture asset is NAMED `inkwell`, so it resolves on the
|
|
1475
|
+
* fallback key (measured against harbor/stashes/inkwell, not argued).
|
|
1476
|
+
*
|
|
1477
|
+
* Never reads hit.score, hit.description, hit.tags or hit.ref. Score is the
|
|
1478
|
+
* ranker's output and reading it re-couples the two. A description branch is how "the
|
|
1479
|
+
* inkwell format" in prose smuggles a fuzzy match back in. `ref` is a PATH: its
|
|
1480
|
+
* interior segments are containers the author chose for filing, so
|
|
1481
|
+
* `.../docker-homelab/references/networking` would authorize the gate for every
|
|
1482
|
+
* file declaring `networking.k8s.io/v1`. The ref is still what the gate message
|
|
1483
|
+
* cites — it is just not evidence.
|
|
1484
|
+
*/
|
|
1485
|
+
function assetDeclaresFormat(key: string, hit: { name?: string }): "name" | null {
|
|
1486
|
+
const needle = typeof key === "string" ? normalizeIdentityField(key) : ""
|
|
1487
|
+
if (!needle) return null
|
|
1488
|
+
const name = typeof hit?.name === "string" ? hit.name : ""
|
|
1489
|
+
return name && normalizeIdentityField(name) === needle ? "name" : null
|
|
1490
|
+
}
|
|
1491
|
+
|
|
1492
|
+
/**
|
|
1493
|
+
* The asset's one-line description is inlined ON PURPOSE. It is already in the
|
|
1494
|
+
* search hit (free), and it is what manufactures the experience of uncertainty
|
|
1495
|
+
* that prompt sentences could not: the #99 trajectory failed because a file it
|
|
1496
|
+
* could already read made the task feel self-sufficient. Belt and braces — a
|
|
1497
|
+
* model that refuses the gate and simply retries the edit may still have been
|
|
1498
|
+
* handed the answer. Do NOT trim it to "force" a tool call: reward is the
|
|
1499
|
+
* objective, engagement is only the proxy.
|
|
1500
|
+
*/
|
|
1501
|
+
function formatGateMessage(filePath: string, token: string, ref: string, description: string): string {
|
|
1502
|
+
const build = (desc: string) => {
|
|
1503
|
+
const cited = desc ? ` — "${desc}"` : ""
|
|
1504
|
+
return `AKM: ${filePath} declares \`${token}\`. Your stash documents this format at \`${ref}\`${cited}.`
|
|
1505
|
+
+ ` You have not opened it this session. Call akm_show with ref "${ref}", then repeat this edit.`
|
|
1506
|
+
+ " This gate fires once per file per session; repeating this edit unchanged will proceed."
|
|
1507
|
+
}
|
|
1508
|
+
const trimmed = description.replace(/\s+/g, " ").trim().slice(0, WRITE_GATE_DESC_CHARS)
|
|
1509
|
+
let message = build(trimmed)
|
|
1510
|
+
if (message.length > WRITE_GATE_MESSAGE_CHARS) {
|
|
1511
|
+
// Shrink the flexible part (the description) before touching the
|
|
1512
|
+
// instruction; the trailing "call akm_show / retry" sentence is the whole
|
|
1513
|
+
// point of the message and must survive a long path or ref.
|
|
1514
|
+
const overflow = message.length - WRITE_GATE_MESSAGE_CHARS
|
|
1515
|
+
message = build(trimmed.slice(0, Math.max(0, trimmed.length - overflow)))
|
|
1516
|
+
}
|
|
1517
|
+
return message.length > WRITE_GATE_MESSAGE_CHARS ? `${message.slice(0, WRITE_GATE_MESSAGE_CHARS - 1)}…` : message
|
|
1518
|
+
}
|
|
1519
|
+
|
|
1520
|
+
// --- write gate: resolution (#99) -------------------------------------------
|
|
1521
|
+
|
|
1522
|
+
function rememberResolution(token: string, resolution: Resolution): Resolution {
|
|
1523
|
+
if (identityCache.size >= WRITE_GATE_IDENTITY_CACHE_CAP) {
|
|
1524
|
+
const oldest = identityCache.keys().next()
|
|
1525
|
+
if (!oldest.done) identityCache.delete(oldest.value)
|
|
1526
|
+
}
|
|
1527
|
+
identityCache.set(token, {
|
|
1528
|
+
resolution,
|
|
1529
|
+
expiresAt: resolution.status === "resolved" ? Number.POSITIVE_INFINITY : Date.now() + WRITE_GATE_NEGATIVE_TTL_MS,
|
|
1530
|
+
})
|
|
1531
|
+
return resolution
|
|
1532
|
+
}
|
|
1533
|
+
|
|
1534
|
+
const WRITE_GATE_TIMEOUT = Symbol("akm-write-gate-timeout")
|
|
1535
|
+
|
|
1536
|
+
function raceWithTimeout<T>(promise: Promise<T>, ms: number): Promise<T | typeof WRITE_GATE_TIMEOUT> {
|
|
1537
|
+
let timer: ReturnType<typeof setTimeout> | undefined
|
|
1538
|
+
const timeout = new Promise<typeof WRITE_GATE_TIMEOUT>((resolve) => {
|
|
1539
|
+
timer = setTimeout(() => resolve(WRITE_GATE_TIMEOUT), ms)
|
|
1540
|
+
// Never hold the process open for a gate timer.
|
|
1541
|
+
;(timer as { unref?: () => void }).unref?.()
|
|
1542
|
+
})
|
|
1543
|
+
return Promise.race([promise, timeout]).finally(() => {
|
|
1544
|
+
if (timer) clearTimeout(timer)
|
|
1545
|
+
})
|
|
1546
|
+
}
|
|
1547
|
+
|
|
1548
|
+
/**
|
|
1549
|
+
* Resolve one format token to the stash asset that documents it. Memoized on
|
|
1550
|
+
* identityCache and de-duped through identityInflight, so a session that reads
|
|
1551
|
+
* six inkwell files costs one search. Uses the same in-process akmSearch the
|
|
1552
|
+
* akm_search tool calls; `warmIndexInBackground()` already ran at
|
|
1553
|
+
* session.created, so a warm local search is ~130ms against a multi-second
|
|
1554
|
+
* model round-trip. Never rejects.
|
|
1555
|
+
*/
|
|
1556
|
+
async function resolveIdentity(client: LogCapableClient, token: string): Promise<Resolution> {
|
|
1557
|
+
const cached = cachedResolution(token)
|
|
1558
|
+
if (cached) return cached
|
|
1559
|
+
const inflight = identityInflight.get(token)
|
|
1560
|
+
if (inflight) return inflight
|
|
1561
|
+
|
|
1562
|
+
const pending = (async (): Promise<Resolution> => {
|
|
1563
|
+
try {
|
|
1564
|
+
const raced = await raceWithTimeout(
|
|
1565
|
+
// skipLogging: this search is the PLUGIN's, not the model's. Without the
|
|
1566
|
+
// flag the gate writes akm_search usage events on every read, feeding
|
|
1567
|
+
// akm's own utility scores and feedback ranking from a search the model
|
|
1568
|
+
// never made — and doing it on the treatment arm only, which is exactly
|
|
1569
|
+
// the contamination an observe-mode stage-1 rollout exists to avoid. The
|
|
1570
|
+
// model-initiated akm_search tool path deliberately keeps logging.
|
|
1571
|
+
Promise.resolve(akmSearch({ query: token, limit: 5, source: "local", skipLogging: true })),
|
|
1572
|
+
WRITE_GATE_RESOLVE_TIMEOUT_MS,
|
|
1573
|
+
)
|
|
1574
|
+
if (raced === WRITE_GATE_TIMEOUT) return rememberResolution(token, { status: "error", reason: "search-timeout" })
|
|
1575
|
+
const hits = Array.isArray((raced as SearchResponse | undefined)?.hits) ? (raced as SearchResponse).hits! : []
|
|
1576
|
+
for (const hit of hits) {
|
|
1577
|
+
if (!assetDeclaresFormat(token, hit as { name?: string })) continue
|
|
1578
|
+
const ref = typeof hit.ref === "string" ? hit.ref : ""
|
|
1579
|
+
if (!ref) continue
|
|
1580
|
+
return rememberResolution(token, {
|
|
1581
|
+
status: "resolved",
|
|
1582
|
+
ref,
|
|
1583
|
+
description: typeof hit.description === "string" ? hit.description : "",
|
|
1584
|
+
})
|
|
1585
|
+
}
|
|
1586
|
+
return rememberResolution(token, { status: "none", cause: hits.length === 0 ? "no-search-hits" : "no-declaration" })
|
|
1587
|
+
} catch (error: unknown) {
|
|
1588
|
+
void writePluginLog(client, "warn", "AKM write gate resolution failed", {
|
|
1589
|
+
subsystem: "write-gate",
|
|
1590
|
+
token,
|
|
1591
|
+
error: formatCliError(error),
|
|
1592
|
+
})
|
|
1593
|
+
return rememberResolution(token, { status: "error", reason: "search-error" })
|
|
1594
|
+
} finally {
|
|
1595
|
+
identityInflight.delete(token)
|
|
1596
|
+
}
|
|
1597
|
+
})()
|
|
1598
|
+
// Only register as in-flight if it is actually still in flight: a synchronous
|
|
1599
|
+
// throw from akmSearch settles `pending` before this line runs, and parking a
|
|
1600
|
+
// settled promise here would leave a Map entry nothing ever clears.
|
|
1601
|
+
if (!cachedResolution(token)) identityInflight.set(token, pending)
|
|
1602
|
+
return pending
|
|
1603
|
+
}
|
|
1604
|
+
|
|
1605
|
+
// --- write gate: session bookkeeping (#99) ----------------------------------
|
|
1606
|
+
|
|
1607
|
+
function resolveGatePath(directory: string | undefined, filePath: string): string {
|
|
1608
|
+
return path.resolve(directory ?? process.cwd(), filePath)
|
|
1609
|
+
}
|
|
1610
|
+
|
|
1611
|
+
// Records the observation even when it found no tokens: an entry here is the
|
|
1612
|
+
// evidence that this session SAW this file's pre-existing content, which is what
|
|
1613
|
+
// the gate's create/edit distinction turns on, and the empty-token case is also
|
|
1614
|
+
// what separates "declares nothing" from "never read".
|
|
1615
|
+
function noteFileIdentity(sessionID: string | undefined, absPath: string, observation: FileObservation): void {
|
|
1616
|
+
if (!sessionID) return
|
|
1617
|
+
const perFile = sessionFileIdentity.get(sessionID) ?? new Map<string, FileObservation>()
|
|
1618
|
+
if (!perFile.has(absPath) && perFile.size >= WRITE_GATE_SESSION_PATH_CAP) {
|
|
1619
|
+
const oldest = perFile.keys().next()
|
|
1620
|
+
if (!oldest.done) perFile.delete(oldest.value)
|
|
1621
|
+
}
|
|
1622
|
+
perFile.set(absPath, observation)
|
|
1623
|
+
sessionFileIdentity.set(sessionID, perFile)
|
|
1624
|
+
}
|
|
1625
|
+
|
|
1626
|
+
function noteShownRefs(sessionID: string | undefined, refs: string[]): void {
|
|
1627
|
+
if (!sessionID || refs.length === 0) return
|
|
1628
|
+
const shown = sessionShownRefs.get(sessionID) ?? new Set<string>()
|
|
1629
|
+
for (const ref of refs) {
|
|
1630
|
+
if (!shown.has(ref) && shown.size >= WRITE_GATE_SESSION_PATH_CAP) {
|
|
1631
|
+
const oldest = shown.values().next()
|
|
1632
|
+
if (!oldest.done) shown.delete(oldest.value)
|
|
1633
|
+
}
|
|
1634
|
+
shown.add(ref)
|
|
1635
|
+
}
|
|
1636
|
+
sessionShownRefs.set(sessionID, shown)
|
|
1637
|
+
}
|
|
1638
|
+
|
|
1639
|
+
/**
|
|
1640
|
+
* Is this call the session AUTHORING content at `absPath` rather than editing
|
|
1641
|
+
* content that was already there?
|
|
1642
|
+
*
|
|
1643
|
+
* Two shapes, and they are discriminated differently because the tools offer
|
|
1644
|
+
* different evidence. `write` carries {filePath, content} and is byte-identical
|
|
1645
|
+
* for a create and for a full overwrite, so the discriminator cannot be the
|
|
1646
|
+
* inputs — it is `observed`: a write to a path this session never READ is a path
|
|
1647
|
+
* whose pre-existing content this session never saw, so nothing it reads back
|
|
1648
|
+
* afterwards can be anything but its own output. `edit` with an empty
|
|
1649
|
+
* `oldString` is opencode 1.18's own create-this-file form (it is rejected
|
|
1650
|
+
* outright on a file that already exists), which is input-level evidence and
|
|
1651
|
+
* needs no read record at all.
|
|
1652
|
+
*/
|
|
1653
|
+
function isSessionCreate(tool: string, args: Record<string, unknown>, observed: FileObservation | undefined): boolean {
|
|
1654
|
+
if (tool === "write") return !observed
|
|
1655
|
+
return tool === "edit" && args.oldString === ""
|
|
1656
|
+
}
|
|
1657
|
+
|
|
1658
|
+
function noteSessionCreated(sessionID: string, absPath: string): void {
|
|
1659
|
+
const created = sessionCreatedPaths.get(sessionID) ?? new Set<string>()
|
|
1660
|
+
if (!created.has(absPath) && created.size >= WRITE_GATE_SESSION_PATH_CAP) {
|
|
1661
|
+
const oldest = created.values().next()
|
|
1662
|
+
if (!oldest.done) created.delete(oldest.value)
|
|
1663
|
+
}
|
|
1664
|
+
created.add(absPath)
|
|
1665
|
+
sessionCreatedPaths.set(sessionID, created)
|
|
1666
|
+
}
|
|
1667
|
+
|
|
1668
|
+
function latchGate(sessionID: string, absPath: string): void {
|
|
1669
|
+
const latched = sessionGateLatched.get(sessionID) ?? new Set<string>()
|
|
1670
|
+
if (!latched.has(absPath) && latched.size >= WRITE_GATE_SESSION_PATH_CAP) {
|
|
1671
|
+
const oldest = latched.values().next()
|
|
1672
|
+
if (!oldest.done) latched.delete(oldest.value)
|
|
1673
|
+
}
|
|
1674
|
+
latched.add(absPath)
|
|
1675
|
+
sessionGateLatched.set(sessionID, latched)
|
|
1676
|
+
}
|
|
1677
|
+
|
|
1678
|
+
/**
|
|
1679
|
+
* Record what a file the session just READ declares about its own format, and
|
|
1680
|
+
* warm the resolution for any token we have not seen. Fire-and-forget: the
|
|
1681
|
+
* search must never sit on a tool's return path.
|
|
1682
|
+
*
|
|
1683
|
+
* `read` is the only caller. #99 review: `write` output used to be an identity
|
|
1684
|
+
* source too, on the "write a file, then edit it" argument, and that is exactly
|
|
1685
|
+
* the write-then-revise CREATE trajectory — crediting it made a file the
|
|
1686
|
+
* session had just invented indistinguishable from one that already existed,
|
|
1687
|
+
* and put the gate inside the fictional-create (96%) and real-create (29%)
|
|
1688
|
+
* cells. Movement there could then no longer be read as noise, confounding
|
|
1689
|
+
* attribution across three of the four cells this change is measured through.
|
|
1690
|
+
*
|
|
1691
|
+
* Round 3: an observation is still recorded for a path the session created —
|
|
1692
|
+
* this function does not know, and should not have to know, which paths those
|
|
1693
|
+
* are. The insulation lives at the decision instead (sessionCreatedPaths), which
|
|
1694
|
+
* is what makes it survive the model reading back its own output.
|
|
1695
|
+
*/
|
|
1696
|
+
function observeFileIdentity(
|
|
1697
|
+
client: LogCapableClient,
|
|
1698
|
+
sessionID: string | undefined,
|
|
1699
|
+
directory: string | undefined,
|
|
1700
|
+
filePath: unknown,
|
|
1701
|
+
head: unknown,
|
|
1702
|
+
): void {
|
|
1703
|
+
if (typeof filePath !== "string" || !filePath) return
|
|
1704
|
+
const tokens = extractFormatIdentity(typeof head === "string" ? head : "", filePath)
|
|
1705
|
+
noteFileIdentity(sessionID, resolveGatePath(directory, filePath), {
|
|
1706
|
+
tokens,
|
|
1707
|
+
recognized: readOutputRecognized(head),
|
|
1708
|
+
})
|
|
1709
|
+
for (const token of tokens) {
|
|
1710
|
+
if (cachedResolution(token) || identityInflight.has(token)) continue
|
|
1711
|
+
void (async () => {
|
|
1712
|
+
try {
|
|
1713
|
+
await resolveIdentity(client, token)
|
|
1714
|
+
} catch {
|
|
1715
|
+
// resolveIdentity never rejects; belt-and-braces so a future change
|
|
1716
|
+
// cannot turn this into an unhandled rejection on the read path.
|
|
1717
|
+
}
|
|
1718
|
+
})()
|
|
1719
|
+
}
|
|
1720
|
+
}
|
|
1721
|
+
|
|
1722
|
+
// --- write gate: decision (#99) ---------------------------------------------
|
|
1723
|
+
|
|
1724
|
+
// One loud complaint per process when the ledger itself cannot be written.
|
|
1725
|
+
//
|
|
1726
|
+
// #99 review: this is the one subsystem whose entire purpose IS the ledger, and
|
|
1727
|
+
// appendMemoryEvent() returns {ok:false} rather than throwing — so a read-only
|
|
1728
|
+
// state dir, a full disk or a bad mode produced an EMPTY histogram, which is
|
|
1729
|
+
// byte-for-byte what "the gate never fired" looks like. The promote-to-enforce
|
|
1730
|
+
// decision would then be made against a file nothing ever reached. Once per
|
|
1731
|
+
// process, matching this file's existing convention for structural faults
|
|
1732
|
+
// (applyPatchWarned, writeGateModeWarned): the condition is persistent, so
|
|
1733
|
+
// repeating it on every write would bury everything else in the log.
|
|
1734
|
+
let gateLedgerWriteWarned = false
|
|
1735
|
+
|
|
1736
|
+
function emitWriteGate(
|
|
1737
|
+
client: LogCapableClient,
|
|
1738
|
+
input: { tool: string; sessionID?: string; callID?: string },
|
|
1739
|
+
directory: string | undefined,
|
|
1740
|
+
filePath: string | undefined,
|
|
1741
|
+
reason: GateReason,
|
|
1742
|
+
status: "ok" | "skipped" | "failed",
|
|
1743
|
+
refs?: string[],
|
|
1744
|
+
// The KEY that resolved, on the paths where one did. The key ladder tries
|
|
1745
|
+
// `inkwell/v2` before `inkwell`, so without this an analyst reading the
|
|
1746
|
+
// histogram cannot tell a specific declaration from a bare-namespace
|
|
1747
|
+
// fallback — and that is the difference between a strong hit and a coincidence.
|
|
1748
|
+
token?: string,
|
|
1749
|
+
): void {
|
|
1750
|
+
if (status !== "ok") gateSkipReasons.set(reason, (gateSkipReasons.get(reason) ?? 0) + 1)
|
|
1751
|
+
const written = writeStructuredEvent({
|
|
1752
|
+
event: "write_gate",
|
|
1753
|
+
sessionId: input.sessionID,
|
|
1754
|
+
scope: buildEventScope(input.sessionID, directory, input.tool),
|
|
1755
|
+
input: { tool: input.tool, callID: input.callID, reason, mode: AKM_WRITE_GATE, filePath, token },
|
|
1756
|
+
refs,
|
|
1757
|
+
outcome: { status },
|
|
1758
|
+
})
|
|
1759
|
+
if (written.ok || gateLedgerWriteWarned) return
|
|
1760
|
+
gateLedgerWriteWarned = true
|
|
1761
|
+
void writePluginLog(client, "error", "AKM write gate ledger write failed", {
|
|
1762
|
+
subsystem: "write-gate",
|
|
1763
|
+
sessionID: input.sessionID,
|
|
1764
|
+
reason,
|
|
1765
|
+
path: OPENCODE_EVENT_LOG,
|
|
1766
|
+
error: written.error,
|
|
1767
|
+
consequence: "write_gate events are being dropped; an empty stage-1 histogram is indistinguishable from a gate that never fired",
|
|
1768
|
+
})
|
|
1769
|
+
}
|
|
1770
|
+
|
|
1771
|
+
/**
|
|
1772
|
+
* Decide whether this write-path tool call is blocked. Returns null for every
|
|
1773
|
+
* non-fire path.
|
|
1774
|
+
*
|
|
1775
|
+
* INVARIANT: every watched-tool invocation emits EXACTLY ONE `write_gate` event
|
|
1776
|
+
* with a named reason. There is no branch that declines to gate without leaving
|
|
1777
|
+
* a typed record of why, so a run where the #99 cell did not move is
|
|
1778
|
+
* diagnosable from the ledger alone — did the gate fire and get ignored, or did
|
|
1779
|
+
* it never fire? That distinction is the difference between a finding and a bug.
|
|
1780
|
+
*/
|
|
1781
|
+
async function gateDecision(
|
|
1782
|
+
client: LogCapableClient,
|
|
1783
|
+
input: { tool: string; sessionID: string; callID: string },
|
|
1784
|
+
output: { args?: unknown },
|
|
1785
|
+
): Promise<GateDecision | null> {
|
|
1786
|
+
gateWatchedInvocations += 1
|
|
1787
|
+
const directory = typeof (input as { directory?: unknown }).directory === "string"
|
|
1788
|
+
? (input as { directory?: string }).directory
|
|
1789
|
+
: undefined
|
|
1790
|
+
const args = (output?.args ?? {}) as Record<string, unknown>
|
|
1791
|
+
|
|
1792
|
+
// Checked before "off" so a misconfiguration is never reported as a
|
|
1793
|
+
// deliberate kill switch. resolveWriteGateMode() refuses to guess; this is
|
|
1794
|
+
// where the refusal becomes visible on every watched call.
|
|
1795
|
+
if (AKM_WRITE_GATE === "invalid") {
|
|
1796
|
+
if (!writeGateModeWarned) {
|
|
1797
|
+
writeGateModeWarned = true
|
|
1798
|
+
void writePluginLog(client, "error", "AKM write gate disabled: unrecognized AKM_WRITE_GATE value", {
|
|
1799
|
+
subsystem: "write-gate",
|
|
1800
|
+
sessionID: input.sessionID,
|
|
1801
|
+
value: writeGateInvalidValue,
|
|
1802
|
+
expected: "off | observe | enforce",
|
|
1803
|
+
reason: "an unrecognized value is a configuration error, not a request for the default mode",
|
|
1804
|
+
})
|
|
1805
|
+
}
|
|
1806
|
+
emitWriteGate(client, input, directory, undefined, "invalid-mode", "skipped")
|
|
1807
|
+
return null
|
|
1808
|
+
}
|
|
1809
|
+
if (AKM_WRITE_GATE === "off") {
|
|
1810
|
+
emitWriteGate(client, input, directory, undefined, "disabled", "skipped")
|
|
1811
|
+
return null
|
|
1812
|
+
}
|
|
1813
|
+
// A plugin that cannot reach the stash must never block an edit. This is the
|
|
1814
|
+
// single most important self-disable: akm missing or the wrong version is a
|
|
1815
|
+
// normal state on a fresh machine, and a blocked edit there is pure cost.
|
|
1816
|
+
if (akmResolutionFailed) {
|
|
1817
|
+
emitWriteGate(client, input, directory, undefined, "akm-unresolved", "skipped")
|
|
1818
|
+
return null
|
|
1819
|
+
}
|
|
1820
|
+
// apply_patch carries `patchText` and no `filePath`, so the gate is
|
|
1821
|
+
// STRUCTURALLY blind on the gpt-* model family. Parsing the patch envelope to
|
|
1822
|
+
// recover paths is deliberately out of scope; pretending the gate is live
|
|
1823
|
+
// there would be exactly the silent degradation this codebase forbids, so it
|
|
1824
|
+
// is one loud warning per process plus a typed skip on every call.
|
|
1825
|
+
if (input.tool === "apply_patch") {
|
|
1826
|
+
if (!applyPatchWarned) {
|
|
1827
|
+
applyPatchWarned = true
|
|
1828
|
+
void writePluginLog(client, "warn", "AKM write gate inert for apply_patch", {
|
|
1829
|
+
subsystem: "write-gate",
|
|
1830
|
+
toolName: input.tool,
|
|
1831
|
+
sessionID: input.sessionID,
|
|
1832
|
+
reason: "apply_patch carries patchText and no filePath; the gate cannot resolve a target file",
|
|
1833
|
+
})
|
|
1834
|
+
}
|
|
1835
|
+
emitWriteGate(client, input, directory, undefined, "apply-patch-unsupported", "skipped")
|
|
1836
|
+
return null
|
|
1837
|
+
}
|
|
1838
|
+
// `filePath` (not `path`) on edit/write/read — confirmed against the
|
|
1839
|
+
// installed opencode 1.18 tool schemas. A `path`-only args object must
|
|
1840
|
+
// produce this typed reason, not a crash and not a silent return.
|
|
1841
|
+
if (typeof args.filePath !== "string" || !args.filePath) {
|
|
1842
|
+
emitWriteGate(client, input, directory, undefined, "no-file-path", "skipped")
|
|
1843
|
+
return null
|
|
1844
|
+
}
|
|
1845
|
+
const absPath = resolveGatePath(directory, args.filePath)
|
|
1846
|
+
|
|
1847
|
+
// #99 review: the create cells have to be insulated, and the discriminator
|
|
1848
|
+
// has to come from what these tools actually hand the hook.
|
|
1849
|
+
//
|
|
1850
|
+
// edit -> { filePath, oldString, newString }. `oldString` is a claim about
|
|
1851
|
+
// text that must ALREADY be in the file. opencode 1.18 rejects an
|
|
1852
|
+
// empty one outright on an existing file ("oldString cannot be
|
|
1853
|
+
// empty when editing an existing file. Provide the exact text to
|
|
1854
|
+
// replace, or use write for an intentional full-file replacement")
|
|
1855
|
+
// and treats it as create-this-file otherwise. The inputs alone
|
|
1856
|
+
// discriminate, so read them.
|
|
1857
|
+
// write -> { filePath, content }. Byte-identical for a create and for a
|
|
1858
|
+
// full overwrite; nothing in the inputs says whether the path
|
|
1859
|
+
// existed a moment ago. The inputs CANNOT discriminate here.
|
|
1860
|
+
//
|
|
1861
|
+
// So the rule that holds for BOTH is not an input test but an evidence test:
|
|
1862
|
+
// gate only where this session has already observed the file's PRE-EXISTING
|
|
1863
|
+
// content. The oldString check is the extra, input-level create signal that
|
|
1864
|
+
// `edit` — and only `edit` — actually offers.
|
|
1865
|
+
//
|
|
1866
|
+
// #99 review round 3: reading that evidence off the CURRENT call was not
|
|
1867
|
+
// enough. "No read record for this path" is a fact about right now, and a
|
|
1868
|
+
// model that verifies its own output erases it — write /app/service.yaml,
|
|
1869
|
+
// read it back, then fix it up, and the read-back writes an observation for a
|
|
1870
|
+
// path this session invented. Reproduced in enforce mode: BLOCKED, on a create.
|
|
1871
|
+
// So the create is RECORDED when it happens and the record is what the gate
|
|
1872
|
+
// consults from then on, for the rest of the session.
|
|
1873
|
+
const observed = sessionFileIdentity.get(input.sessionID)?.get(absPath)
|
|
1874
|
+
if (isSessionCreate(input.tool, args, observed)) noteSessionCreated(input.sessionID, absPath)
|
|
1875
|
+
|
|
1876
|
+
if (input.tool === "edit" && typeof args.oldString === "string" && args.oldString === "") {
|
|
1877
|
+
emitWriteGate(client, input, directory, absPath, "create-not-edit", "skipped")
|
|
1878
|
+
return null
|
|
1879
|
+
}
|
|
1880
|
+
|
|
1881
|
+
if (sessionCreatedPaths.get(input.sessionID)?.has(absPath)) {
|
|
1882
|
+
emitWriteGate(client, input, directory, absPath, "session-created", "skipped")
|
|
1883
|
+
return null
|
|
1884
|
+
}
|
|
1885
|
+
|
|
1886
|
+
if (sessionGateLatched.get(input.sessionID)?.has(absPath)) {
|
|
1887
|
+
emitWriteGate(client, input, directory, absPath, "latched", "skipped")
|
|
1888
|
+
return null
|
|
1889
|
+
}
|
|
1890
|
+
|
|
1891
|
+
if (!observed) {
|
|
1892
|
+
// An edit to a file this session never opened and never wrote — the model
|
|
1893
|
+
// is editing from knowledge it got somewhere else. Creates no longer land
|
|
1894
|
+
// here; they land on `session-created` above. Zero cost, no I/O.
|
|
1895
|
+
emitWriteGate(client, input, directory, absPath, "file-not-read", "skipped")
|
|
1896
|
+
return null
|
|
1897
|
+
}
|
|
1898
|
+
const tokens = observed.tokens
|
|
1899
|
+
if (tokens.length === 0) {
|
|
1900
|
+
// The whole real/known-tool cell lands on `no-identity` — the file declares
|
|
1901
|
+
// no authority and that zero is correct. `read-output-unrecognized` is the
|
|
1902
|
+
// other thing that used to hide in that word: the read output was not the
|
|
1903
|
+
// shape this module parses, so the extractor could not have worked and a
|
|
1904
|
+
// clean-looking ledger would have been a lie.
|
|
1905
|
+
emitWriteGate(client, input, directory, absPath, observed.recognized ? "no-identity" : "read-output-unrecognized", "skipped")
|
|
1906
|
+
return null
|
|
1907
|
+
}
|
|
1908
|
+
|
|
1909
|
+
let resolved: { token: string; resolution: Extract<Resolution, { status: "resolved" }> } | undefined
|
|
1910
|
+
let noneCause: "no-search-hits" | "no-declaration" | undefined
|
|
1911
|
+
let errorReason: "search-timeout" | "search-error" | undefined
|
|
1912
|
+
let pendingToken: string | undefined
|
|
1913
|
+
for (const token of tokens) {
|
|
1914
|
+
const cached = cachedResolution(token)
|
|
1915
|
+
if (cached?.status === "resolved") {
|
|
1916
|
+
resolved = { token, resolution: cached }
|
|
1917
|
+
break
|
|
1918
|
+
}
|
|
1919
|
+
// "hits came back, none declared it" is the more specific answer, so it wins
|
|
1920
|
+
// the report when a file declares several keys that miss for different
|
|
1921
|
+
// reasons.
|
|
1922
|
+
if (cached?.status === "none") { noneCause = cached.cause === "no-declaration" ? "no-declaration" : noneCause ?? cached.cause; continue }
|
|
1923
|
+
if (cached?.status === "error") { errorReason = cached.reason; continue }
|
|
1924
|
+
if (identityInflight.has(token)) pendingToken ??= token
|
|
1925
|
+
}
|
|
1926
|
+
|
|
1927
|
+
if (!resolved && pendingToken) {
|
|
1928
|
+
// Bounded, and only ever awaits an ALREADY-RUNNING resolve started by the
|
|
1929
|
+
// read hook. It never STARTS one: a search on the blocking path would put
|
|
1930
|
+
// akm's latency in front of every edit the user makes.
|
|
1931
|
+
const inflight = identityInflight.get(pendingToken)
|
|
1932
|
+
if (inflight) {
|
|
1933
|
+
const raced = await raceWithTimeout(inflight, WRITE_GATE_INFLIGHT_WAIT_MS)
|
|
1934
|
+
if (raced !== WRITE_GATE_TIMEOUT && raced.status === "resolved") resolved = { token: pendingToken, resolution: raced }
|
|
1935
|
+
else if (raced !== WRITE_GATE_TIMEOUT && raced.status === "none") noneCause = raced.cause === "no-declaration" ? "no-declaration" : noneCause ?? raced.cause
|
|
1936
|
+
else if (raced !== WRITE_GATE_TIMEOUT && raced.status === "error") errorReason = raced.reason
|
|
1937
|
+
}
|
|
1938
|
+
}
|
|
1939
|
+
|
|
1940
|
+
if (!resolved) {
|
|
1941
|
+
if (noneCause) {
|
|
1942
|
+
// "the search returned nothing" and "it returned hits and none of them
|
|
1943
|
+
// declared the format" are a coverage problem and a precision problem. One
|
|
1944
|
+
// word for both told the rollout nothing about which one to fix.
|
|
1945
|
+
emitWriteGate(client, input, directory, absPath, noneCause === "no-search-hits" ? "no-search-hits" : "no-declaring-asset", "skipped")
|
|
1946
|
+
} else if (errorReason) {
|
|
1947
|
+
emitWriteGate(client, input, directory, absPath, errorReason, "failed")
|
|
1948
|
+
} else {
|
|
1949
|
+
emitWriteGate(client, input, directory, absPath, "resolution-pending", "skipped")
|
|
1950
|
+
}
|
|
1951
|
+
return null
|
|
1952
|
+
}
|
|
1953
|
+
|
|
1954
|
+
if (sessionShownRefs.get(input.sessionID)?.has(resolved.resolution.ref)) {
|
|
1955
|
+
emitWriteGate(client, input, directory, absPath, "already-shown", "skipped", [resolved.resolution.ref], resolved.token)
|
|
1956
|
+
return null
|
|
1957
|
+
}
|
|
1958
|
+
|
|
1959
|
+
// Latch BEFORE returning the decision, so release is unconditional and
|
|
1960
|
+
// livelock is impossible by construction: the model can always get its edit
|
|
1961
|
+
// through by repeating it. A latch conditioned on compliance would be a trap.
|
|
1962
|
+
latchGate(input.sessionID, absPath)
|
|
1963
|
+
gateEverActed = true
|
|
1964
|
+
if (AKM_WRITE_GATE === "observe") {
|
|
1965
|
+
// Stage 1 of the rollout: everything runs, nothing is blocked, and the
|
|
1966
|
+
// would-fire count is readable off the ledger before an eval slice is spent.
|
|
1967
|
+
emitWriteGate(client, input, directory, absPath, "observe", "ok", [resolved.resolution.ref], resolved.token)
|
|
1968
|
+
return null
|
|
1969
|
+
}
|
|
1970
|
+
emitWriteGate(client, input, directory, absPath, "fired", "ok", [resolved.resolution.ref], resolved.token)
|
|
1971
|
+
return {
|
|
1972
|
+
filePath: args.filePath,
|
|
1973
|
+
token: resolved.token,
|
|
1974
|
+
ref: resolved.resolution.ref,
|
|
1975
|
+
description: resolved.resolution.description,
|
|
1976
|
+
}
|
|
1977
|
+
}
|
|
1978
|
+
|
|
1979
|
+
// Warn once per process if watched write tools were seen and the gate never
|
|
1980
|
+
// acted on any of them. Every OTHER signal in this design looks healthy in that
|
|
1981
|
+
// state — the events are all there, they just all say "skipped" — so without
|
|
1982
|
+
// this the feature can ship dead and nobody notices.
|
|
1983
|
+
function warnIfWriteGateInert(client: LogCapableClient): void {
|
|
1984
|
+
// Not a warning when the operator turned the gate off — "never acted" is the
|
|
1985
|
+
// requested behaviour there, not a symptom. Nor on `invalid`, which already
|
|
1986
|
+
// produced its own, louder error; a second warning would just bury it.
|
|
1987
|
+
if (AKM_WRITE_GATE === "off" || AKM_WRITE_GATE === "invalid") return
|
|
1988
|
+
if (gateInertWarned || gateEverActed || gateWatchedInvocations === 0) return
|
|
1989
|
+
gateInertWarned = true
|
|
1990
|
+
void writePluginLog(client, "warn", "AKM write gate never acted", {
|
|
1991
|
+
subsystem: "write-gate",
|
|
1992
|
+
mode: AKM_WRITE_GATE,
|
|
1993
|
+
watchedInvocations: gateWatchedInvocations,
|
|
1994
|
+
skipReasons: Object.fromEntries(gateSkipReasons),
|
|
1995
|
+
})
|
|
1996
|
+
}
|
|
1997
|
+
|
|
1998
|
+
// NOTE, recorded so the next author does not re-derive it: `tool.execute.after`
|
|
1999
|
+
// also offers a result-mutation channel — the object it receives IS the object
|
|
2000
|
+
// returned as the tool result, so appending to `output.output` on a completed
|
|
2001
|
+
// edit would deliver the same message non-blockingly. That is the fallback if a
|
|
2002
|
+
// future opencode build changes how a thrown hook error is surfaced. It is NOT
|
|
2003
|
+
// implemented; one comment, not a second mechanism.
|
|
2004
|
+
|
|
2005
|
+
// The trigger sentence used to read "Before writing anything from scratch",
|
|
2006
|
+
// which literally excludes the largest class of tasks retrieval helps with:
|
|
2007
|
+
// editing a file whose conventions the model does not know. Measured across
|
|
2008
|
+
// 138 Harbor A/B trials (issue #94), engagement on edit-shaped tasks was
|
|
2009
|
+
// 0/24 (eval) and 3/57 (train) versus 48% and 38% on create-shaped tasks from
|
|
2010
|
+
// the same families — three edit tasks scored 0.00 on BOTH arms because the
|
|
2011
|
+
// model invented keys for a file it had just read. A visible file makes a task
|
|
2012
|
+
// look self-sufficient, so the trigger has to say outright that seeing a file
|
|
2013
|
+
// is not knowing its schema.
|
|
974
2014
|
const AKM_HINTS_PREFIX = [
|
|
975
2015
|
"# AKM is available in this session",
|
|
976
2016
|
"",
|
|
977
|
-
"You have an AKM stash on this machine. Before writing
|
|
2017
|
+
"You have an AKM stash on this machine. Before writing **or editing** a config file, manifest, schema, or command for any tool, format, or API whose exact syntax or keys you are not certain of, call `akm_curate` with a task description to find relevant assets with LLM-reranked relevance scores. A file already being present in the workspace is not evidence that you know its schema — the values may be given to you while the key names and nesting are not, so check the stash for that format's conventions before you edit it.",
|
|
978
2018
|
"",
|
|
979
2019
|
"**Choosing the right lookup command:**",
|
|
980
2020
|
"",
|
|
@@ -1983,6 +3023,9 @@ const akmPlugin: Plugin = async ({ client, worktree, directory }) => {
|
|
|
1983
3023
|
// tmp file) so a re-created session does not inherit stale
|
|
1984
3024
|
// hints/curation and the tmp file does not leak (13: "Memory leaks").
|
|
1985
3025
|
if (type === "session.deleted") {
|
|
3026
|
+
// #99: the gate is the one akm feature whose total failure looks
|
|
3027
|
+
// exactly like normal operation in the ledger, so say so out loud.
|
|
3028
|
+
warnIfWriteGateInert(logClient)
|
|
1986
3029
|
clearSessionState(sid)
|
|
1987
3030
|
}
|
|
1988
3031
|
}
|
|
@@ -2045,7 +3088,17 @@ const akmPlugin: Plugin = async ({ client, worktree, directory }) => {
|
|
|
2045
3088
|
sessionWorkflow.get(sid) ? formatWorkflowContext(sessionWorkflow.get(sid)!) : "",
|
|
2046
3089
|
!proposalSummary.unsupported && proposalSummary.count > 0 ? formatPendingProposalContext(proposalSummary.count) : "",
|
|
2047
3090
|
]
|
|
2048
|
-
|
|
3091
|
+
// ONE entry, not N. OpenCode maps each `system` entry to its own system
|
|
3092
|
+
// message, and chat templates that require a single leading system
|
|
3093
|
+
// message reject the request outright — "Jinja Exception: System
|
|
3094
|
+
// message must be at the beginning", surfacing as an opaque provider
|
|
3095
|
+
// HTTP 500 that hits only sessions with the plugin installed (#96;
|
|
3096
|
+
// reproduced with a bare two-system-message request on
|
|
3097
|
+
// qwen3.6-35b-a3b and devstral-small-2-2512, no akm involved).
|
|
3098
|
+
// Budgeting is unchanged and still happens per block, so joining can
|
|
3099
|
+
// only re-seam blocks applyContextBudget already kept.
|
|
3100
|
+
const budgeted = applyContextBudget(blocks)
|
|
3101
|
+
if (budgeted.length > 0) output.system.push(budgeted.join("\n\n"))
|
|
2049
3102
|
} catch (error: unknown) {
|
|
2050
3103
|
await logHookFailure(logClient, "experimental.chat.system.transform", error)
|
|
2051
3104
|
}
|
|
@@ -2120,7 +3173,7 @@ const akmPlugin: Plugin = async ({ client, worktree, directory }) => {
|
|
|
2120
3173
|
}
|
|
2121
3174
|
})()
|
|
2122
3175
|
} else {
|
|
2123
|
-
const hint = "Need more AKM context? Use `akm_search` or `akm_curate` before writing
|
|
3176
|
+
const hint = "Need more AKM context? Use `akm_search` or `akm_curate` before writing or editing a file whose exact syntax you are not certain of."
|
|
2124
3177
|
writeStructuredEvent({
|
|
2125
3178
|
event: "prompt_recall",
|
|
2126
3179
|
sessionId: input.sessionID,
|
|
@@ -2201,6 +3254,61 @@ const akmPlugin: Plugin = async ({ client, worktree, directory }) => {
|
|
|
2201
3254
|
})
|
|
2202
3255
|
}
|
|
2203
3256
|
},
|
|
3257
|
+
// #99: the format-declaration write gate. This is the first akm hook that
|
|
3258
|
+
// changes what the agent DOES rather than only what it knows, and the
|
|
3259
|
+
// structure below is the load-bearing part.
|
|
3260
|
+
//
|
|
3261
|
+
// Facts re-verified here against the installed opencode 1.18 binary, banked
|
|
3262
|
+
// so nobody re-derives them:
|
|
3263
|
+
// - `Plugin.trigger` is `for (const h of hooks) yield* Effect.promise(async () => h(input, output))`,
|
|
3264
|
+
// called from inside the tool's own `Effect.runPromise(Effect.gen(...))`.
|
|
3265
|
+
// A rejection therefore reaches the model as a `tool-error` part
|
|
3266
|
+
// (`case"tool-error":{yield*N(c.id,c.error??Error(c.message))}`), with
|
|
3267
|
+
// `error.message` intact — reproduced end to end against effect
|
|
3268
|
+
// 4.0.0-beta.83. "A plugin hook cannot block a tool call" is FALSE.
|
|
3269
|
+
// - Arg names: edit `{filePath, oldString, newString}`, write
|
|
3270
|
+
// `{content, filePath}`, read `{filePath, offset, limit}`, apply_patch
|
|
3271
|
+
// `{patchText}`. It is `filePath`, never `path`.
|
|
3272
|
+
// - The tool registry filter is
|
|
3273
|
+
// `k = modelID.includes("gpt-") && !includes("oss") && !includes("gpt-4")`;
|
|
3274
|
+
// apply_patch is registered when `k`, edit and write when `!k`. So on
|
|
3275
|
+
// that model family apply_patch is the ONLY write tool.
|
|
3276
|
+
// - `read` returns `<path>…</path>\n<type>file</type>\n<content>\n` with
|
|
3277
|
+
// every line prefixed `N: `.
|
|
3278
|
+
// - The in-process akmSearch hit carries `description` and `tags`; the
|
|
3279
|
+
// CLI's own output shaping drops both, the library return value does not.
|
|
3280
|
+
//
|
|
3281
|
+
// The throw sits OUTSIDE the try/catch on purpose. Every other hook body in
|
|
3282
|
+
// this file wraps itself in `try { … } catch { logHookFailure }` by
|
|
3283
|
+
// convention; a throw placed inside that wrapper would be swallowed, the
|
|
3284
|
+
// gate would never fire, and the ledger would stay perfectly clean while
|
|
3285
|
+
// the feature did nothing. Verified end to end against the installed
|
|
3286
|
+
// opencode 1.18 / effect 4.0.0-beta.83: Plugin.trigger runs each hook as
|
|
3287
|
+
// `Effect.promise(async () => hook(input, output))` inside the tool's own
|
|
3288
|
+
// `Effect.runPromise(Effect.gen(...))`, and a rejection there surfaces with
|
|
3289
|
+
// `error.message` verbatim, which the session turns into a `tool-error`
|
|
3290
|
+
// part the model reads. (Recorded because the opposite — "a hook cannot
|
|
3291
|
+
// block a tool call" — was asserted as verified during design and is false.)
|
|
3292
|
+
//
|
|
3293
|
+
// A plugin-internal fault must NOT block a user's edit, so everything that
|
|
3294
|
+
// can throw for our own reasons stays inside the catch and returns.
|
|
3295
|
+
"tool.execute.before": async (input, output) => {
|
|
3296
|
+
let decision: GateDecision | null = null
|
|
3297
|
+
try {
|
|
3298
|
+
if (!WATCHED_WRITE_TOOLS.has(input.tool)) return
|
|
3299
|
+
decision = await gateDecision(logClient, input, output)
|
|
3300
|
+
} catch (error: unknown) {
|
|
3301
|
+
await logHookFailure(logClient, "tool.execute.before", error, {
|
|
3302
|
+
toolName: input?.tool,
|
|
3303
|
+
sessionID: input?.sessionID,
|
|
3304
|
+
callID: input?.callID,
|
|
3305
|
+
})
|
|
3306
|
+
return
|
|
3307
|
+
}
|
|
3308
|
+
if (decision) {
|
|
3309
|
+
throw new Error(formatGateMessage(decision.filePath, decision.token, decision.ref, decision.description))
|
|
3310
|
+
}
|
|
3311
|
+
},
|
|
2204
3312
|
"tool.execute.after": async (input, output) => {
|
|
2205
3313
|
try {
|
|
2206
3314
|
const isAkmTool = input.tool.startsWith("akm_")
|
|
@@ -2238,6 +3346,22 @@ const akmPlugin: Plugin = async ({ client, worktree, directory }) => {
|
|
|
2238
3346
|
}
|
|
2239
3347
|
}
|
|
2240
3348
|
|
|
3349
|
+
// #99 write gate, read side. `read` is the tool that precedes every
|
|
3350
|
+
// trajectory in the failing cell: the model reads /app/service.yaml,
|
|
3351
|
+
// then edits it from guesswork. Recording what the file declares about
|
|
3352
|
+
// itself here is what lets the gate on the NEXT edit be file-anchored
|
|
3353
|
+
// instead of another sentence asking the model to go looking.
|
|
3354
|
+
if (input.tool === "read") {
|
|
3355
|
+
observeFileIdentity(logClient, input.sessionID, directory, (input.args as Record<string, unknown>)?.filePath, output.output)
|
|
3356
|
+
}
|
|
3357
|
+
// `write` is deliberately NOT an identity source. It used to be, on a
|
|
3358
|
+
// "write a file, then edit it" argument, and that shape is a CREATE: the
|
|
3359
|
+
// content the model would be gated on is content it just invented, so
|
|
3360
|
+
// the gate would have reached into the two create cells (#99 review).
|
|
3361
|
+
// See observeFileIdentity() for the full reasoning. The create is
|
|
3362
|
+
// instead RECORDED on the write's `tool.execute.before` pass, which the
|
|
3363
|
+
// runtime always runs for a watched tool — see isSessionCreate().
|
|
3364
|
+
|
|
2241
3365
|
if (!isAkmTool) return
|
|
2242
3366
|
|
|
2243
3367
|
const parsed = parseToolOutput(output.output)
|
|
@@ -2269,6 +3393,29 @@ const akmPlugin: Plugin = async ({ client, worktree, directory }) => {
|
|
|
2269
3393
|
}
|
|
2270
3394
|
|
|
2271
3395
|
const toolRefs = extractToolRefs(input.tool, input.args as Record<string, unknown>, parsed)
|
|
3396
|
+
// #99: a ref the model has already opened must never buy it a blocked
|
|
3397
|
+
// edit. Without this the compliant model gets re-blocked for doing
|
|
3398
|
+
// exactly what the gate asked.
|
|
3399
|
+
//
|
|
3400
|
+
// akm_curate counts as well as akm_show (#99 review). Curate is the
|
|
3401
|
+
// PRIMARY lookup command this plugin's own guidance tells the model to
|
|
3402
|
+
// reach for, and its result carries the ref and the one-line
|
|
3403
|
+
// description the gate message would have handed over — a model that
|
|
3404
|
+
// curated has already done the lookup. Crediting only akm_show made the
|
|
3405
|
+
// gate fire on the compliant create-shaped trajectory, which is one of
|
|
3406
|
+
// the cells whose movement has to stay readable as noise.
|
|
3407
|
+
//
|
|
3408
|
+
// ...and only when the lookup SUCCEEDED. extractToolRefs() reads
|
|
3409
|
+
// `args.ref` as well as the output, so an akm_show for a ref that does
|
|
3410
|
+
// not exist — `{ok:false,error:"not found"}` — used to credit the model
|
|
3411
|
+
// with having opened it. That put a row in the ledger asserting an
|
|
3412
|
+
// outcome that did not happen, and it is precisely the row an analyst
|
|
3413
|
+
// reads as "the model complied" (#99 review). classifyToolFeedback()
|
|
3414
|
+
// already types a failed akm call as negative; reuse it rather than
|
|
3415
|
+
// inventing a second notion of failure.
|
|
3416
|
+
if ((input.tool === "akm_show" || input.tool === "akm_curate") && feedback !== "negative") {
|
|
3417
|
+
noteShownRefs(input.sessionID, toolRefs)
|
|
3418
|
+
}
|
|
2272
3419
|
noteRecentRefs(input.sessionID, toolRefs)
|
|
2273
3420
|
writeStructuredEvent({
|
|
2274
3421
|
event: "tool_observation",
|
|
@@ -2469,7 +3616,14 @@ const akmPlugin: Plugin = async ({ client, worktree, directory }) => {
|
|
|
2469
3616
|
},
|
|
2470
3617
|
}),
|
|
2471
3618
|
akm_curate: tool({
|
|
2472
|
-
|
|
3619
|
+
// Led with the mechanism ("describe the task in natural language and
|
|
3620
|
+
// this returns the top matches"), which reads as project/asset
|
|
3621
|
+
// discovery and lost to built-in read/glob/skill on edit-shaped tasks:
|
|
3622
|
+
// across seven models screened on one akm-relevant task, five made
|
|
3623
|
+
// zero akm_* calls while curation was demonstrably available (#95).
|
|
3624
|
+
// Leading with the decision — when to reach for this instead of just
|
|
3625
|
+
// reading the file — is what it has to win on.
|
|
3626
|
+
description: "Reach for this BEFORE writing or editing a config file, manifest, schema, or command for any tool, format, or API whose exact syntax or keys you are not certain of — including a file already present in the workspace, since having read a file does not mean you know its schema. PRIMARY discovery entry point for the stash: describe the task in natural language and this returns the top matches as a ranked list. Pass a hit's ref to akm_show before relying on it, then record akm_feedback once the result is known.",
|
|
2473
3627
|
args: {
|
|
2474
3628
|
query: tool.schema.string().describe("Task, topic, or natural-language description of what you want to do."),
|
|
2475
3629
|
type: tool.schema.enum(ASSET_TYPES as unknown as [string, ...string[]]).optional().describe("Optional asset type filter."),
|
|
@@ -2513,4 +3667,9 @@ const akmPlugin: Plugin = async ({ client, worktree, directory }) => {
|
|
|
2513
3667
|
export const AkmPlugin = Object.assign(akmPlugin, {
|
|
2514
3668
|
__resetResolvedAkmForTests,
|
|
2515
3669
|
__curatedDirForTests,
|
|
3670
|
+
__resetWriteGateForTests,
|
|
3671
|
+
__extractFormatIdentity: extractFormatIdentity,
|
|
3672
|
+
__assetDeclaresFormat: assetDeclaresFormat,
|
|
3673
|
+
__formatGateMessage: formatGateMessage,
|
|
3674
|
+
__watchedWriteTools: WATCHED_WRITE_TOOLS,
|
|
2516
3675
|
})
|
package/package.json
CHANGED
package/shared/memory-events.ts
CHANGED
|
@@ -25,6 +25,10 @@ export type AkmMemoryEventType =
|
|
|
25
25
|
| "subagent_started"
|
|
26
26
|
| "post_compact_summary"
|
|
27
27
|
| "feedback_recorded"
|
|
28
|
+
// The opencode write gate (#99) emits exactly one of these per watched
|
|
29
|
+
// write-tool invocation, with a named reason in `input.reason`. One name, not
|
|
30
|
+
// two: fire and skip are the same event with different outcome.status.
|
|
31
|
+
| "write_gate"
|
|
28
32
|
|
|
29
33
|
export type AkmMemoryEvent = {
|
|
30
34
|
version: 1
|