@ggui-ai/negotiator 0.25.0 → 0.26.0

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.
@@ -1,224 +0,0 @@
1
- /**
2
- * Action-entry member preservation — the deterministic backstop that
3
- * keeps a repair FAITHFUL to what the agent DECLARED on its actions
4
- * (ggui#1421).
5
- *
6
- * The repair tool authors exactly two members per action, `label` and
7
- * `schema` (`SYNTHESIZE_TOOL`); every other member of an
8
- * {@link ActionEntry} — `description`, `example`, `icon`, `confirm`,
9
- * `oneShot`, `nextStep` — is the agent's declaration, and the whole
10
- * one-shot line (ggui#1108 → ggui#1223: the runtime's second-dispatch
11
- * guard, the server's spend record, ui-gen's `useActionSpent` binding)
12
- * keys on `oneShot === true` being ON THE SERVED CONTRACT. A repair that
13
- * re-emits an entry from the tool's answer alone serves a card with no
14
- * guard, silently. So the overlay below puts the draft's declarations
15
- * back on the repaired candidate, deterministically, and NAMES whatever
16
- * it cannot keep:
17
- *
18
- * - `REPAIR_MEMBER_DROPPED` at `actionSpec.<name>.<member>` — a
19
- * declared member the merged contract cannot carry (the gate refuses
20
- * it there, e.g. a `nextStep` whose tool the repair did not
21
- * re-declare, or a member that fails its own schema);
22
- * - `REPAIR_ENTRY_DROPPED` at `actionSpec.<name>` — a draft action the
23
- * candidate no longer carries under that name (renamed or removed),
24
- * which takes every member with it.
25
- *
26
- * A member whose path a DRAFT finding already names is under repair: it
27
- * is not restored, and its absence is named by that finding, at the same
28
- * path. Every other loss is named HERE, at the member's or the entry's
29
- * own path, with the gate's reason in the message — so the invariant an
30
- * agent can rely on is one sentence: a declared member or action missing
31
- * from the served proposal always has a finding at its own path. The
32
- * `CTR_*` codes say what the gate refused; the `REPAIR_*` codes say what
33
- * the served proposal does not carry of the declaration, and why. The
34
- * codes are handshake-decision codes like `COVERAGE_GAP` — declared where
35
- * they are emitted, read by the agent as strings in `validationFindings`.
36
- * Model-independent: the overlay works whatever the repair LLM returns.
37
- */
38
-
39
- import {
40
- actionEntrySchema,
41
- CTR_SCHEMA_INCOMPAT,
42
- lintContract,
43
- type DataContract,
44
- type SuggestionFinding,
45
- } from '@ggui-ai/protocol';
46
-
47
- /** A declared action-entry member the repair could not keep; path `actionSpec.<name>.<member>`. */
48
- export const REPAIR_MEMBER_DROPPED = 'REPAIR_MEMBER_DROPPED' as const;
49
- /** A draft action entry the repair no longer carries under its name; path `actionSpec.<name>`. */
50
- export const REPAIR_ENTRY_DROPPED = 'REPAIR_ENTRY_DROPPED' as const;
51
-
52
- type ActionEntry = NonNullable<DataContract['actionSpec']>[string];
53
- type ActionMember = Exclude<keyof typeof actionEntrySchema.shape, 'label' | 'schema'>;
54
-
55
- /**
56
- * The members the agent declares — the protocol's action-entry schema
57
- * minus the pair the repair tool authors. Derived, never listed, so a
58
- * new schema member is preserved the day it lands.
59
- */
60
- const DECLARED_MEMBERS: readonly ActionMember[] = (
61
- Object.keys(actionEntrySchema.shape) as (keyof typeof actionEntrySchema.shape)[]
62
- ).filter((k): k is ActionMember => k !== 'label' && k !== 'schema');
63
-
64
- function isRecord(value: unknown): value is Record<string, unknown> {
65
- return typeof value === 'object' && value !== null && !Array.isArray(value);
66
- }
67
-
68
- /** The draft's `actionSpec` map when it is one; `undefined` otherwise (the draft is untrusted). */
69
- function draftActionSpec(draft: unknown): Record<string, unknown> | undefined {
70
- if (!isRecord(draft)) return undefined;
71
- const actionSpec = draft['actionSpec'];
72
- return isRecord(actionSpec) ? actionSpec : undefined;
73
- }
74
-
75
- /**
76
- * `entry` without `member`. Removing an optional member from a parsed
77
- * entry leaves a parsed entry, so the strict re-parse re-derives the
78
- * typed shape without a cast.
79
- */
80
- function withoutMember(entry: ActionEntry, member: ActionMember): ActionEntry {
81
- return actionEntrySchema.parse(
82
- Object.fromEntries(Object.entries(entry).filter(([key]) => key !== member)),
83
- );
84
- }
85
-
86
- function memberDropped(name: string, member: ActionMember, reason: string): SuggestionFinding {
87
- return {
88
- code: REPAIR_MEMBER_DROPPED,
89
- severity: 'error',
90
- path: `actionSpec.${name}.${member}`,
91
- message: `the repair could not keep the declared \`${member}\` on action '${name}': ${reason}`,
92
- };
93
- }
94
-
95
- /**
96
- * Whether `finding` is a member drop on one of `entryNames` — matched by
97
- * the exact `actionSpec.<name>.<member>` path per declared member, never
98
- * by a dotted prefix (an action named `a` is not a prefix of `a.b`).
99
- */
100
- export function isMemberDropOf(finding: SuggestionFinding, entryNames: readonly string[]): boolean {
101
- return entryNames.some((name) => DECLARED_MEMBERS.some((member) => finding.path === `actionSpec.${name}.${member}`));
102
- }
103
-
104
- /**
105
- * Overlay the draft's declared members onto the candidate's action
106
- * entries (same name), keeping the candidate's `label` and `schema`. A
107
- * member whose exact path a draft finding names is under repair and
108
- * stays out. Then lint the merged contract and un-restore, until the
109
- * tree is stable, every restored member the gate refuses — at its own
110
- * path, or (for `nextStep`) as `CTR_SCHEMA_INCOMPAT` at the sibling
111
- * `.schema`, the path the compatibility check reports on — naming each
112
- * one, so the returned contract never fails the gate BECAUSE of the
113
- * overlay. A malformed candidate entry (no `label`) is left un-overlaid
114
- * for the loop's own gate to retry; nothing here throws on the model's
115
- * answer. Entries the candidate does not carry get nothing back here —
116
- * see {@link findDroppedActionEntries}.
117
- */
118
- export function restoreDraftActionMembers(
119
- draft: unknown,
120
- candidate: DataContract,
121
- underRepair: ReadonlySet<string>,
122
- ): { readonly contract: DataContract; readonly dropped: readonly SuggestionFinding[] } {
123
- const draftActions = draftActionSpec(draft);
124
- if (draftActions === undefined || candidate.actionSpec === undefined) {
125
- return { contract: candidate, dropped: [] };
126
- }
127
- const dropped: SuggestionFinding[] = [];
128
- /** Per entry, the members the overlay put back. */
129
- const restored = new Map<string, Set<ActionMember>>();
130
- const merged: Record<string, ActionEntry> = {};
131
- for (const [name, entry] of Object.entries(candidate.actionSpec)) {
132
- const draftEntry = Object.hasOwn(draftActions, name) ? draftActions[name] : undefined;
133
- if (!isRecord(draftEntry)) {
134
- merged[name] = entry;
135
- continue;
136
- }
137
- const next: Record<string, unknown> = { ...entry };
138
- const put = new Set<ActionMember>();
139
- for (const member of DECLARED_MEMBERS) {
140
- const value = draftEntry[member];
141
- if (value === undefined || entry[member] !== undefined || underRepair.has(`actionSpec.${name}.${member}`)) continue;
142
- const parsed = actionEntrySchema.shape[member].safeParse(value);
143
- if (!parsed.success) {
144
- dropped.push(memberDropped(name, member, parsed.error.issues[0]?.message ?? 'invalid'));
145
- continue;
146
- }
147
- next[member] = parsed.data;
148
- put.add(member);
149
- }
150
- const reparsed = actionEntrySchema.safeParse(next);
151
- if (put.size === 0 || !reparsed.success) {
152
- // Nothing to put back, or the CANDIDATE's own label/schema are not
153
- // an entry yet — the loop's gate retries that; not this overlay's.
154
- merged[name] = entry;
155
- continue;
156
- }
157
- merged[name] = reparsed.data;
158
- restored.set(name, put);
159
- }
160
- let actionSpec: Record<string, ActionEntry> = merged;
161
- if (restored.size === 0) return { contract: { ...candidate, actionSpec }, dropped };
162
-
163
- // Cross-reference and compatibility errors only exist on the MERGED
164
- // tree. Un-restore exactly the members the gate names — at their own
165
- // path, or a `nextStep` as `CTR_SCHEMA_INCOMPAT` at the sibling
166
- // `.schema`, the path the compatibility check reports on — naming each
167
- // at ITS OWN path with the gate's reason; re-lint, and stop when a pass
168
- // changes nothing. Every pass removes at least one member, so the loop
169
- // is bounded by what was restored.
170
- for (let pass = 0; pass < DECLARED_MEMBERS.length * restored.size + 1; pass += 1) {
171
- const errors = lintContract({ ...candidate, actionSpec }).errors;
172
- let changed = false;
173
- for (const [name, members] of restored) {
174
- for (const member of [...members]) {
175
- const refusal =
176
- errors.find((issue) => issue.path === `actionSpec.${name}.${member}`) ??
177
- (member === 'nextStep'
178
- ? errors.find((issue) => issue.code === CTR_SCHEMA_INCOMPAT && issue.path === `actionSpec.${name}.schema`)
179
- : undefined);
180
- if (refusal === undefined) continue;
181
- const entry = actionSpec[name];
182
- if (entry === undefined) continue;
183
- actionSpec = { ...actionSpec, [name]: withoutMember(entry, member) };
184
- members.delete(member);
185
- dropped.push(memberDropped(name, member, `refused by the gate as ${refusal.code} at ${refusal.path}: ${refusal.message}`));
186
- changed = true;
187
- }
188
- if (members.size === 0) restored.delete(name);
189
- }
190
- if (!changed || restored.size === 0) break;
191
- }
192
- return { contract: { ...candidate, actionSpec }, dropped };
193
- }
194
-
195
- /**
196
- * One `REPAIR_ENTRY_DROPPED` per draft action entry the candidate no
197
- * longer carries under that name — renamed or removed by the repair (or
198
- * pruned by the loop's own placement pass) — always, because the members
199
- * it carried (`oneShot` above all) went with it and nothing else names
200
- * that. When the gate refused the NAME itself (`CTR_DUP_NAME` /
201
- * `CTR_RESERVED_NAME` at `actionSpec.<name>`, so the path is under
202
- * repair) the message says so and does not send the agent back to a
203
- * name the gate will refuse again. Empty when every draft entry survived,
204
- * and for drafts that declare no actions.
205
- */
206
- export function findDroppedActionEntries(
207
- draft: unknown,
208
- candidate: DataContract,
209
- underRepair: ReadonlySet<string>,
210
- ): readonly SuggestionFinding[] {
211
- const draftActions = draftActionSpec(draft);
212
- if (draftActions === undefined) return [];
213
- const kept = candidate.actionSpec ?? {};
214
- return Object.keys(draftActions)
215
- .filter((name) => !Object.hasOwn(kept, name))
216
- .map((name) => ({
217
- code: REPAIR_ENTRY_DROPPED,
218
- severity: 'error',
219
- path: `actionSpec.${name}`,
220
- message: underRepair.has(`actionSpec.${name}`)
221
- ? `the gate refused the action name '${name}' (see the finding at this path) and the repair removed or renamed it; every member it carried, oneShot included, went with it — declare those members again under a name the gate accepts, via ggui_render override or a corrected re-handshake`
222
- : `the repair no longer carries the action '${name}' you declared (renamed or removed); every member it carried, oneShot included, went with it — re-declare it via ggui_render override or a corrected re-handshake`,
223
- }));
224
- }