agentfootprint 7.17.0 → 7.18.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.
Files changed (136) hide show
  1. package/AGENTS.md +1 -1
  2. package/CLAUDE.md +2 -2
  3. package/ai-instructions/claude-code/SKILL.md +1 -1
  4. package/dist/core/Agent.js +51 -2
  5. package/dist/core/Agent.js.map +1 -1
  6. package/dist/core/RunnerBase.js +16 -1
  7. package/dist/core/RunnerBase.js.map +1 -1
  8. package/dist/core/agent/AgentBuilder.js +117 -1
  9. package/dist/core/agent/AgentBuilder.js.map +1 -1
  10. package/dist/core/agent/middleware/errors.js +54 -0
  11. package/dist/core/agent/middleware/errors.js.map +1 -0
  12. package/dist/core/agent/middleware/index.js +23 -0
  13. package/dist/core/agent/middleware/index.js.map +1 -0
  14. package/dist/core/agent/middleware/ledger.js +61 -0
  15. package/dist/core/agent/middleware/ledger.js.map +1 -0
  16. package/dist/core/agent/middleware/outcomes.js +66 -0
  17. package/dist/core/agent/middleware/outcomes.js.map +1 -0
  18. package/dist/core/agent/middleware/runChain.js +144 -0
  19. package/dist/core/agent/middleware/runChain.js.map +1 -0
  20. package/dist/core/agent/middleware/types.js +41 -0
  21. package/dist/core/agent/middleware/types.js.map +1 -0
  22. package/dist/core/agent/stages/prepareFinal.js +10 -0
  23. package/dist/core/agent/stages/prepareFinal.js.map +1 -1
  24. package/dist/core/agent/stages/route.js +62 -1
  25. package/dist/core/agent/stages/route.js.map +1 -1
  26. package/dist/core/agent/stages/seed.js +130 -85
  27. package/dist/core/agent/stages/seed.js.map +1 -1
  28. package/dist/core/agent/stages/toolCalls.js +195 -8
  29. package/dist/core/agent/stages/toolCalls.js.map +1 -1
  30. package/dist/core/pause.js +19 -1
  31. package/dist/core/pause.js.map +1 -1
  32. package/dist/esm/core/Agent.d.ts +9 -1
  33. package/dist/esm/core/Agent.js +52 -3
  34. package/dist/esm/core/Agent.js.map +1 -1
  35. package/dist/esm/core/RunnerBase.js +16 -1
  36. package/dist/esm/core/RunnerBase.js.map +1 -1
  37. package/dist/esm/core/agent/AgentBuilder.d.ts +97 -0
  38. package/dist/esm/core/agent/AgentBuilder.js +117 -1
  39. package/dist/esm/core/agent/AgentBuilder.js.map +1 -1
  40. package/dist/esm/core/agent/buildAgentChart.d.ts +2 -2
  41. package/dist/esm/core/agent/middleware/errors.d.ts +51 -0
  42. package/dist/esm/core/agent/middleware/errors.js +50 -0
  43. package/dist/esm/core/agent/middleware/errors.js.map +1 -0
  44. package/dist/esm/core/agent/middleware/index.d.ts +16 -0
  45. package/dist/esm/core/agent/middleware/index.js +16 -0
  46. package/dist/esm/core/agent/middleware/index.js.map +1 -0
  47. package/dist/esm/core/agent/middleware/ledger.d.ts +40 -0
  48. package/dist/esm/core/agent/middleware/ledger.js +57 -0
  49. package/dist/esm/core/agent/middleware/ledger.js.map +1 -0
  50. package/dist/esm/core/agent/middleware/outcomes.d.ts +45 -0
  51. package/dist/esm/core/agent/middleware/outcomes.js +60 -0
  52. package/dist/esm/core/agent/middleware/outcomes.js.map +1 -0
  53. package/dist/esm/core/agent/middleware/runChain.d.ts +104 -0
  54. package/dist/esm/core/agent/middleware/runChain.js +139 -0
  55. package/dist/esm/core/agent/middleware/runChain.js.map +1 -0
  56. package/dist/esm/core/agent/middleware/types.d.ts +191 -0
  57. package/dist/esm/core/agent/middleware/types.js +40 -0
  58. package/dist/esm/core/agent/middleware/types.js.map +1 -0
  59. package/dist/esm/core/agent/stages/prepareFinal.d.ts +10 -0
  60. package/dist/esm/core/agent/stages/prepareFinal.js +10 -0
  61. package/dist/esm/core/agent/stages/prepareFinal.js.map +1 -1
  62. package/dist/esm/core/agent/stages/route.d.ts +29 -0
  63. package/dist/esm/core/agent/stages/route.js +60 -0
  64. package/dist/esm/core/agent/stages/route.js.map +1 -1
  65. package/dist/esm/core/agent/stages/seed.d.ts +16 -1
  66. package/dist/esm/core/agent/stages/seed.js +130 -85
  67. package/dist/esm/core/agent/stages/seed.js.map +1 -1
  68. package/dist/esm/core/agent/stages/toolCalls.d.ts +23 -0
  69. package/dist/esm/core/agent/stages/toolCalls.js +195 -8
  70. package/dist/esm/core/agent/stages/toolCalls.js.map +1 -1
  71. package/dist/esm/core/agent/types.d.ts +18 -0
  72. package/dist/esm/core/pause.d.ts +42 -0
  73. package/dist/esm/core/pause.js +17 -0
  74. package/dist/esm/core/pause.js.map +1 -1
  75. package/dist/esm/events/dispatcher.d.ts +1 -1
  76. package/dist/esm/events/dispatcher.js.map +1 -1
  77. package/dist/esm/events/payloads.d.ts +26 -0
  78. package/dist/esm/events/registry.d.ts +5 -1
  79. package/dist/esm/events/registry.js +4 -0
  80. package/dist/esm/events/registry.js.map +1 -1
  81. package/dist/esm/index.d.ts +2 -1
  82. package/dist/esm/index.js +13 -1
  83. package/dist/esm/index.js.map +1 -1
  84. package/dist/esm/lib/mcp/mcpServe.js +39 -6
  85. package/dist/esm/lib/mcp/mcpServe.js.map +1 -1
  86. package/dist/esm/lib/mcp/types.d.ts +16 -0
  87. package/dist/events/dispatcher.js.map +1 -1
  88. package/dist/events/registry.js +4 -0
  89. package/dist/events/registry.js.map +1 -1
  90. package/dist/index.js +31 -14
  91. package/dist/index.js.map +1 -1
  92. package/dist/lib/mcp/mcpServe.js +39 -6
  93. package/dist/lib/mcp/mcpServe.js.map +1 -1
  94. package/dist/types/core/Agent.d.ts +9 -1
  95. package/dist/types/core/Agent.d.ts.map +1 -1
  96. package/dist/types/core/RunnerBase.d.ts.map +1 -1
  97. package/dist/types/core/agent/AgentBuilder.d.ts +97 -0
  98. package/dist/types/core/agent/AgentBuilder.d.ts.map +1 -1
  99. package/dist/types/core/agent/buildAgentChart.d.ts +2 -2
  100. package/dist/types/core/agent/buildAgentChart.d.ts.map +1 -1
  101. package/dist/types/core/agent/middleware/errors.d.ts +52 -0
  102. package/dist/types/core/agent/middleware/errors.d.ts.map +1 -0
  103. package/dist/types/core/agent/middleware/index.d.ts +17 -0
  104. package/dist/types/core/agent/middleware/index.d.ts.map +1 -0
  105. package/dist/types/core/agent/middleware/ledger.d.ts +41 -0
  106. package/dist/types/core/agent/middleware/ledger.d.ts.map +1 -0
  107. package/dist/types/core/agent/middleware/outcomes.d.ts +46 -0
  108. package/dist/types/core/agent/middleware/outcomes.d.ts.map +1 -0
  109. package/dist/types/core/agent/middleware/runChain.d.ts +105 -0
  110. package/dist/types/core/agent/middleware/runChain.d.ts.map +1 -0
  111. package/dist/types/core/agent/middleware/types.d.ts +192 -0
  112. package/dist/types/core/agent/middleware/types.d.ts.map +1 -0
  113. package/dist/types/core/agent/stages/prepareFinal.d.ts +10 -0
  114. package/dist/types/core/agent/stages/prepareFinal.d.ts.map +1 -1
  115. package/dist/types/core/agent/stages/route.d.ts +29 -0
  116. package/dist/types/core/agent/stages/route.d.ts.map +1 -1
  117. package/dist/types/core/agent/stages/seed.d.ts +16 -1
  118. package/dist/types/core/agent/stages/seed.d.ts.map +1 -1
  119. package/dist/types/core/agent/stages/toolCalls.d.ts +23 -0
  120. package/dist/types/core/agent/stages/toolCalls.d.ts.map +1 -1
  121. package/dist/types/core/agent/types.d.ts +18 -0
  122. package/dist/types/core/agent/types.d.ts.map +1 -1
  123. package/dist/types/core/pause.d.ts +42 -0
  124. package/dist/types/core/pause.d.ts.map +1 -1
  125. package/dist/types/events/dispatcher.d.ts +1 -1
  126. package/dist/types/events/dispatcher.d.ts.map +1 -1
  127. package/dist/types/events/payloads.d.ts +26 -0
  128. package/dist/types/events/payloads.d.ts.map +1 -1
  129. package/dist/types/events/registry.d.ts +5 -1
  130. package/dist/types/events/registry.d.ts.map +1 -1
  131. package/dist/types/index.d.ts +2 -1
  132. package/dist/types/index.d.ts.map +1 -1
  133. package/dist/types/lib/mcp/mcpServe.d.ts.map +1 -1
  134. package/dist/types/lib/mcp/types.d.ts +16 -0
  135. package/dist/types/lib/mcp/types.d.ts.map +1 -1
  136. package/package.json +1 -1
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/core/agent/middleware/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;GAYG;;;AAEH,6CAAiD;AAAxC,oGAAA,KAAK,OAAA;AAAE,kGAAA,GAAG,OAAA;AAAE,mGAAA,IAAI,OAAA;AACzB,yCAA4E;AAAnE,+GAAA,kBAAkB,OAAA"}
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ /**
3
+ * middleware/ledger — INTERNAL. Where a chain's decisions become record.
4
+ *
5
+ * Pattern: one writer for one committed key.
6
+ * Role: core/ layer. `runChain` decides; this file is the only thing
7
+ * that commits what it decided and emits the matching event, so
8
+ * the three call sites cannot record the same fact three
9
+ * different ways.
10
+ * Emits: `agentfootprint.middleware.decision`, one per row.
11
+ *
12
+ * ## Why the original value is committed too
13
+ *
14
+ * A transform makes the trace and the wire disagree — the model saw one
15
+ * string, the run remembers another. Committing the pair, with the
16
+ * middleware's name and its `why` beside it, is what turns that
17
+ * disagreement from a lie into a record. Every slice taken afterwards can
18
+ * then find the moment the value changed and who changed it.
19
+ *
20
+ * This is not a redaction layer, and it is not trying to be one. For the
21
+ * `'input'` phase in particular, the ledger row is the ONLY copy of the
22
+ * pre-scrub text anywhere in the run — the seed stage commits the
23
+ * transformed message and nothing else ever holds the original. If your
24
+ * threat model says the original must not survive in the commit log,
25
+ * configure footprintjs redaction over this key; it scrubs `before` /
26
+ * `after` at write time and the row itself survives, so the run still says
27
+ * a scrub happened and who did it.
28
+ */
29
+ Object.defineProperty(exports, "__esModule", { value: true });
30
+ exports.recordDecisions = void 0;
31
+ const typedEmit_js_1 = require("../../../recorders/core/typedEmit.js");
32
+ /**
33
+ * Append decision rows to the run's ledger and emit one event per row.
34
+ *
35
+ * No-op on an empty list, so an agent with no middleware never writes the
36
+ * key at all and its commit log is the one it always had.
37
+ */
38
+ function recordDecisions(scope, rows) {
39
+ if (rows.length === 0)
40
+ return;
41
+ const prev = scope.middlewareDecisions ?? [];
42
+ // Spread into a plain local array first: a TypedScope array read is a live
43
+ // deep-proxy view, and both the commit and the event payloads below must be
44
+ // detached plain data (RFC-001 'clone' capture under deferred delivery).
45
+ scope.middlewareDecisions = [...prev, ...rows];
46
+ for (const row of rows) {
47
+ (0, typedEmit_js_1.typedEmit)(scope, 'agentfootprint.middleware.decision', {
48
+ middleware: row.middleware,
49
+ at: row.at,
50
+ ...(row.phase !== undefined && { phase: row.phase }),
51
+ ...(row.toolName !== undefined && { toolName: row.toolName }),
52
+ ...(row.toolCallId !== undefined && { toolCallId: row.toolCallId }),
53
+ iteration: row.iteration,
54
+ outcome: row.outcome,
55
+ changed: row.changed,
56
+ ...(row.why !== undefined && { why: row.why }),
57
+ });
58
+ }
59
+ }
60
+ exports.recordDecisions = recordDecisions;
61
+ //# sourceMappingURL=ledger.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ledger.js","sourceRoot":"","sources":["../../../../src/core/agent/middleware/ledger.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;;;AAEH,uEAAiE;AASjE;;;;;GAKG;AACH,SAAgB,eAAe,CAAC,KAAkB,EAAE,IAAmC;IACrF,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO;IAC9B,MAAM,IAAI,GAAI,KAAK,CAAC,mBAAiE,IAAI,EAAE,CAAC;IAC5F,2EAA2E;IAC3E,4EAA4E;IAC5E,yEAAyE;IACzE,KAAK,CAAC,mBAAmB,GAAG,CAAC,GAAG,IAAI,EAAE,GAAG,IAAI,CAAC,CAAC;IAC/C,KAAK,MAAM,GAAG,IAAI,IAAI,EAAE,CAAC;QACvB,IAAA,wBAAS,EAAC,KAAK,EAAE,oCAAoC,EAAE;YACrD,UAAU,EAAE,GAAG,CAAC,UAAU;YAC1B,EAAE,EAAE,GAAG,CAAC,EAAE;YACV,GAAG,CAAC,GAAG,CAAC,KAAK,KAAK,SAAS,IAAI,EAAE,KAAK,EAAE,GAAG,CAAC,KAAK,EAAE,CAAC;YACpD,GAAG,CAAC,GAAG,CAAC,QAAQ,KAAK,SAAS,IAAI,EAAE,QAAQ,EAAE,GAAG,CAAC,QAAQ,EAAE,CAAC;YAC7D,GAAG,CAAC,GAAG,CAAC,UAAU,KAAK,SAAS,IAAI,EAAE,UAAU,EAAE,GAAG,CAAC,UAAU,EAAE,CAAC;YACnE,SAAS,EAAE,GAAG,CAAC,SAAS;YACxB,OAAO,EAAE,GAAG,CAAC,OAAO;YACpB,OAAO,EAAE,GAAG,CAAC,OAAO;YACpB,GAAG,CAAC,GAAG,CAAC,GAAG,KAAK,SAAS,IAAI,EAAE,GAAG,EAAE,GAAG,CAAC,GAAG,EAAE,CAAC;SAC/C,CAAC,CAAC;IACL,CAAC;AACH,CAAC;AApBD,0CAoBC"}
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ /**
3
+ * middleware/outcomes — PUBLIC. The three verbs a middleware speaks.
4
+ *
5
+ * Pattern: Smart constructors over a closed union.
6
+ * Role: core/ layer. `allow` / `deny` / `ask` are the entire vocabulary
7
+ * of both chains, which is why they are short: they are written
8
+ * inside every middleware body, several times, and a governance
9
+ * rule reads better as `deny('writes to prod need a ticket')` than
10
+ * as a namespaced ceremony.
11
+ * Emits: N/A.
12
+ *
13
+ * The `allow` overloads carry a rule: you can pass the value through
14
+ * untouched with no explanation, but you cannot CHANGE it without one. A
15
+ * transform is the only thing here that makes the trace and the wire
16
+ * disagree, so it is the only thing required to say why.
17
+ */
18
+ Object.defineProperty(exports, "__esModule", { value: true });
19
+ exports.ask = exports.deny = exports.allow = void 0;
20
+ function allow(value, why) {
21
+ if (value === undefined)
22
+ return { kind: 'allow' };
23
+ if (typeof why !== 'string' || why.length === 0) {
24
+ throw new Error('allow(value, why): a transform must say why. Pass a short reason — it is what the ' +
25
+ 'ledger shows next to the before/after pair. To pass the value through unchanged, ' +
26
+ 'call allow() with no arguments.');
27
+ }
28
+ return { kind: 'allow', value, why };
29
+ }
30
+ exports.allow = allow;
31
+ /**
32
+ * Refuse the call.
33
+ *
34
+ * For a tool the reason reaches the model verbatim, as the tool's result,
35
+ * and the loop continues — the agent gets to adapt. For a message it
36
+ * surfaces as a `MessageDeniedError`.
37
+ */
38
+ function deny(reason) {
39
+ if (typeof reason !== 'string' || reason.length === 0) {
40
+ throw new Error('deny(reason): a refusal must carry a reason. For a tool it is the text the model ' +
41
+ 'reads and adapts to, so write it for the model, not for a log line.');
42
+ }
43
+ return { kind: 'deny', reason };
44
+ }
45
+ exports.deny = deny;
46
+ /**
47
+ * Suspend the run and put the question to a person. Tool dispatch only —
48
+ * `MessageOutcome` has no `ask` arm, so this cannot be returned from a
49
+ * message middleware.
50
+ *
51
+ * The answer is a decision, not a result: approve and the chain continues
52
+ * and the REAL tool runs; decline and it becomes a denial the model reads.
53
+ * A middleware never gets to write the answer itself.
54
+ */
55
+ function ask(payload) {
56
+ if (payload === null ||
57
+ typeof payload !== 'object' ||
58
+ typeof payload.question !== 'string' ||
59
+ payload.question.length === 0) {
60
+ throw new Error('ask(payload): expected { question, detail? } with a non-empty question. The question ' +
61
+ 'is what a person is shown when the run pauses.');
62
+ }
63
+ return { kind: 'ask', payload };
64
+ }
65
+ exports.ask = ask;
66
+ //# sourceMappingURL=outcomes.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"outcomes.js","sourceRoot":"","sources":["../../../../src/core/agent/middleware/outcomes.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;GAeG;;;AAcH,SAAgB,KAAK,CAAI,KAAS,EAAE,GAAY;IAC9C,IAAI,KAAK,KAAK,SAAS;QAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC;IAClD,IAAI,OAAO,GAAG,KAAK,QAAQ,IAAI,GAAG,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAChD,MAAM,IAAI,KAAK,CACb,oFAAoF;YAClF,mFAAmF;YACnF,iCAAiC,CACpC,CAAC;IACJ,CAAC;IACD,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,GAAG,EAAE,CAAC;AACvC,CAAC;AAVD,sBAUC;AAED;;;;;;GAMG;AACH,SAAgB,IAAI,CAAC,MAAc;IACjC,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACtD,MAAM,IAAI,KAAK,CACb,mFAAmF;YACjF,qEAAqE,CACxE,CAAC;IACJ,CAAC;IACD,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC;AAClC,CAAC;AARD,oBAQC;AAED;;;;;;;;GAQG;AACH,SAAgB,GAAG,CAAC,OAAmB;IACrC,IACE,OAAO,KAAK,IAAI;QAChB,OAAO,OAAO,KAAK,QAAQ;QAC3B,OAAO,OAAO,CAAC,QAAQ,KAAK,QAAQ;QACpC,OAAO,CAAC,QAAQ,CAAC,MAAM,KAAK,CAAC,EAC7B,CAAC;QACD,MAAM,IAAI,KAAK,CACb,uFAAuF;YACrF,gDAAgD,CACnD,CAAC;IACJ,CAAC;IACD,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC;AAClC,CAAC;AAbD,kBAaC"}
@@ -0,0 +1,144 @@
1
+ "use strict";
2
+ /**
3
+ * middleware/runChain — INTERNAL. The one place a chain is walked.
4
+ *
5
+ * Pattern: Chain of Responsibility driver.
6
+ * Role: core/ layer. Both stages (tool dispatch, message boundary) and
7
+ * the MCP server call in here, so the ordering law, the
8
+ * throw-is-a-denial law and the ledger row shape are written once
9
+ * and cannot drift between the three callers.
10
+ * Emits: N/A — it RETURNS rows; the caller commits and emits them, so a
11
+ * middleware still never touches scope.
12
+ *
13
+ * ## The rules this file implements
14
+ *
15
+ * **Order is declaration order.** The chain is walked front to back, and
16
+ * each link is handed the previous link's output — so a scrubber placed
17
+ * first is a scrubber every later rule sees the effect of.
18
+ *
19
+ * **The first non-allow answer wins.** A deny or an ask stops the walk;
20
+ * the links behind it do not run. There is no "second opinion" pass,
21
+ * because a refusal that a later middleware could overturn is not a
22
+ * refusal.
23
+ *
24
+ * **A middleware that throws is a denial, never a pass.** The thrown
25
+ * message becomes the reason. This is the same deny-by-default the
26
+ * permission gate has always applied to a checker that throws: a
27
+ * governance layer whose failure mode is "allow" is not a governance
28
+ * layer.
29
+ */
30
+ Object.defineProperty(exports, "__esModule", { value: true });
31
+ exports.runMessageChain = exports.runToolChain = void 0;
32
+ /** Walk the tool chain. Never throws — a throwing link becomes a denial. */
33
+ async function runToolChain(chain, input) {
34
+ const decisions = [];
35
+ const askPolicy = input.askPolicy ?? 'pause';
36
+ let args = input.args;
37
+ for (let i = input.startIndex ?? 0; i < chain.length; i++) {
38
+ const mw = chain[i];
39
+ const row = (extra) => ({
40
+ middleware: mw.name,
41
+ at: 'tool',
42
+ toolName: input.toolName,
43
+ toolCallId: input.toolCallId,
44
+ iteration: input.iteration,
45
+ outcome: 'allow',
46
+ changed: false,
47
+ ...extra,
48
+ });
49
+ let outcome;
50
+ try {
51
+ outcome = await mw.onToolCall({
52
+ toolName: input.toolName,
53
+ toolCallId: input.toolCallId,
54
+ iteration: input.iteration,
55
+ args,
56
+ history: input.history,
57
+ ...(input.identity && { identity: input.identity }),
58
+ ...(input.signal && { signal: input.signal }),
59
+ });
60
+ }
61
+ catch (err) {
62
+ const reason = `middleware '${mw.name}' threw: ${err instanceof Error ? err.message : String(err)}`;
63
+ decisions.push(row({ outcome: 'deny', why: reason }));
64
+ return { kind: 'deny', reason, middleware: mw.name, args, decisions };
65
+ }
66
+ if (outcome.kind === 'deny') {
67
+ decisions.push(row({ outcome: 'deny', why: outcome.reason }));
68
+ return { kind: 'deny', reason: outcome.reason, middleware: mw.name, args, decisions };
69
+ }
70
+ if (outcome.kind === 'ask') {
71
+ if (askPolicy === 'refuse') {
72
+ const reason = `middleware '${mw.name}' asked a person to decide, and there is no pause here to ` +
73
+ `carry that ask: ${outcome.payload.question}`;
74
+ decisions.push(row({ outcome: 'deny', why: reason }));
75
+ return { kind: 'deny', reason, middleware: mw.name, args, decisions };
76
+ }
77
+ decisions.push(row({ outcome: 'ask', why: outcome.payload.question }));
78
+ return {
79
+ kind: 'ask',
80
+ payload: outcome.payload,
81
+ middleware: mw.name,
82
+ index: i,
83
+ args,
84
+ decisions,
85
+ };
86
+ }
87
+ if (outcome.value !== undefined) {
88
+ const before = args;
89
+ args = outcome.value;
90
+ decisions.push(row({ changed: true, ...(outcome.why && { why: outcome.why }), before, after: args }));
91
+ }
92
+ else {
93
+ decisions.push(row({}));
94
+ }
95
+ }
96
+ return { kind: 'allow', args, decisions };
97
+ }
98
+ exports.runToolChain = runToolChain;
99
+ /** Walk the message chain. Never throws — a throwing link becomes a denial. */
100
+ async function runMessageChain(chain, input) {
101
+ const decisions = [];
102
+ let content = input.content;
103
+ for (const mw of chain) {
104
+ const row = (extra) => ({
105
+ middleware: mw.name,
106
+ at: 'message',
107
+ phase: input.phase,
108
+ iteration: input.iteration,
109
+ outcome: 'allow',
110
+ changed: false,
111
+ ...extra,
112
+ });
113
+ let outcome;
114
+ try {
115
+ outcome = await mw.onMessage({
116
+ phase: input.phase,
117
+ content,
118
+ history: input.history,
119
+ ...(input.identity && { identity: input.identity }),
120
+ ...(input.signal && { signal: input.signal }),
121
+ });
122
+ }
123
+ catch (err) {
124
+ const reason = `middleware '${mw.name}' threw: ${err instanceof Error ? err.message : String(err)}`;
125
+ decisions.push(row({ outcome: 'deny', why: reason }));
126
+ return { kind: 'deny', reason, middleware: mw.name, content, decisions };
127
+ }
128
+ if (outcome.kind === 'deny') {
129
+ decisions.push(row({ outcome: 'deny', why: outcome.reason }));
130
+ return { kind: 'deny', reason: outcome.reason, middleware: mw.name, content, decisions };
131
+ }
132
+ if (outcome.value !== undefined) {
133
+ const before = content;
134
+ content = outcome.value;
135
+ decisions.push(row({ changed: true, ...(outcome.why && { why: outcome.why }), before, after: content }));
136
+ }
137
+ else {
138
+ decisions.push(row({}));
139
+ }
140
+ }
141
+ return { kind: 'allow', content, decisions };
142
+ }
143
+ exports.runMessageChain = runMessageChain;
144
+ //# sourceMappingURL=runChain.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"runChain.js","sourceRoot":"","sources":["../../../../src/core/agent/middleware/runChain.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;;;AAgEH,4EAA4E;AACrE,KAAK,UAAU,YAAY,CAChC,KAAgC,EAChC,KAAqB;IAErB,MAAM,SAAS,GAAyB,EAAE,CAAC;IAC3C,MAAM,SAAS,GAAG,KAAK,CAAC,SAAS,IAAI,OAAO,CAAC;IAC7C,IAAI,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC;IAEtB,KAAK,IAAI,CAAC,GAAG,KAAK,CAAC,UAAU,IAAI,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QAC1D,MAAM,EAAE,GAAG,KAAK,CAAC,CAAC,CAAE,CAAC;QACrB,MAAM,GAAG,GAAG,CAAC,KAAkC,EAAsB,EAAE,CAAC,CAAC;YACvE,UAAU,EAAE,EAAE,CAAC,IAAI;YACnB,EAAE,EAAE,MAAM;YACV,QAAQ,EAAE,KAAK,CAAC,QAAQ;YACxB,UAAU,EAAE,KAAK,CAAC,UAAU;YAC5B,SAAS,EAAE,KAAK,CAAC,SAAS;YAC1B,OAAO,EAAE,OAAO;YAChB,OAAO,EAAE,KAAK;YACd,GAAG,KAAK;SACT,CAAC,CAAC;QAEH,IAAI,OAAoB,CAAC;QACzB,IAAI,CAAC;YACH,OAAO,GAAG,MAAM,EAAE,CAAC,UAAU,CAAC;gBAC5B,QAAQ,EAAE,KAAK,CAAC,QAAQ;gBACxB,UAAU,EAAE,KAAK,CAAC,UAAU;gBAC5B,SAAS,EAAE,KAAK,CAAC,SAAS;gBAC1B,IAAI;gBACJ,OAAO,EAAE,KAAK,CAAC,OAAO;gBACtB,GAAG,CAAC,KAAK,CAAC,QAAQ,IAAI,EAAE,QAAQ,EAAE,KAAK,CAAC,QAAQ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AG,EAAE,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;YAC9D,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,MAAM,EAAE,UAAU,EAAE,EAAE,CAAC,IAAI,EAAE,OAAO,EAAE,SAAS,EAAE,CAAC;QAC3F,CAAC;QAED,IAAI,OAAO,CAAC,KAAK,KAAK,SAAS,EAAE,CAAC;YAChC,MAAM,MAAM,GAAG,OAAO,CAAC;YACvB,OAAO,GAAG,OAAO,CAAC,KAAK,CAAC;YACxB,SAAS,CAAC,IAAI,CACZ,GAAG,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,GAAG,CAAC,OAAO,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,OAAO,CAAC,GAAG,EAAE,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC,CACzF,CAAC;QACJ,CAAC;aAAM,CAAC;YACN,SAAS,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC;QAC1B,CAAC;IACH,CAAC;IAED,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,CAAC;AAC/C,CAAC;AApDD,0CAoDC"}
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ /**
3
+ * middleware/types — PUBLIC. The shape of a governance chain.
4
+ *
5
+ * Pattern: Chain of Responsibility (GoF), with the one dangerous answer
6
+ * removed at the type level.
7
+ * Role: core/ layer. Two chains, one vocabulary:
8
+ *
9
+ * `toolMiddleware` wraps every tool dispatch
10
+ * `messageMiddleware` wraps the message boundary — the input
11
+ * before the model sees it, the output
12
+ * before the caller receives it
13
+ *
14
+ * Emits: N/A. A middleware is a pure decision. It never touches scope,
15
+ * never emits and never writes — the stage does all of that, so
16
+ * "record everything that happened" lives in ONE place rather
17
+ * than being re-implemented per middleware.
18
+ *
19
+ * ## The law that shapes these types
20
+ *
21
+ * **A middleware cannot answer for the tool.** The outcome union has three
22
+ * arms — allow, deny, ask — and no `result` arm. There is no spelling of
23
+ * "here is what the tool would have returned". Whatever the chain decides,
24
+ * the answer the model finally reads is the real tool's output or a
25
+ * refusal. That is not a convention a reviewer has to enforce; it is the
26
+ * absence of a field.
27
+ *
28
+ * **A transform declares itself.** `allow(value, why)` requires a reason
29
+ * whenever it changes something, and the stage commits BOTH the original
30
+ * and the transformed value into the run's ledger. A prompt scrubbed by a
31
+ * middleware that hid its own scrubbing would poison every slice taken
32
+ * afterwards: the trace would show text nobody ever sent.
33
+ *
34
+ * **`ask` exists only where a pause exists.** `ToolOutcome` has it because
35
+ * tool dispatch runs inside a footprintjs pausable stage — the same door
36
+ * `checkIn` and `askHuman` already go through. `MessageOutcome` does not,
37
+ * because the message boundary is a plain stage, and inventing a second
38
+ * pause to give it one would be a worse answer than not offering it.
39
+ */
40
+ Object.defineProperty(exports, "__esModule", { value: true });
41
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../../../src/core/agent/middleware/types.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG"}
@@ -14,6 +14,16 @@
14
14
  *
15
15
  * Pure function — no closure over Agent class state. Imported and
16
16
  * passed directly to `flowChart(...)` in buildAgentChart.
17
+ *
18
+ * NOT the seam for `messageMiddleware`'s `'output'` half, deliberately.
19
+ * This stage runs inside the Final BRANCH subflow, whose state does not
20
+ * merge back into the run (a branch mount hands its outputMapper the
21
+ * branch's RESULT, not its scope). Rows filed here would land in an
22
+ * isolated commit log and never reach `snapshot.sharedState`, splitting
23
+ * one ledger across two places. The chain runs one stage earlier instead
24
+ * — in the Route decider, in the main chart — and rewrites
25
+ * `llmLatestContent`, which is the value this stage copies. See
26
+ * `stages/route.ts`.
17
27
  */
18
28
  Object.defineProperty(exports, "__esModule", { value: true });
19
29
  exports.prepareFinalStage = void 0;
@@ -1 +1 @@
1
- {"version":3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W;KAC3C,CAAC,CAAC;AACL,CAAC,CAAC;AAlCW,QAAA,iBAAiB,qBAkC5B"}
@@ -14,8 +14,10 @@
14
14
  * passed directly to `addDeciderFunction(...)` in buildAgentChart.
15
15
  */
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.routeDeciderStage = void 0;
17
+ exports.buildRouteDeciderStage = exports.routeDeciderStage = void 0;
18
18
  const typedEmit_js_1 = require("../../../recorders/core/typedEmit.js");
19
+ const runChain_js_1 = require("../middleware/runChain.js");
20
+ const ledger_js_1 = require("../middleware/ledger.js");
19
21
  const routeDeciderStage = (scope) => {
20
22
  const toolCalls = scope.llmLatestToolCalls;
21
23
  const iteration = scope.iteration;
@@ -33,4 +35,63 @@ const routeDeciderStage = (scope) => {
33
35
  return chosen;
34
36
  };
35
37
  exports.routeDeciderStage = routeDeciderStage;
38
+ /**
39
+ * Build the Route decider, optionally carrying the `'output'` half of the
40
+ * message chain.
41
+ *
42
+ * ## Why the chain runs HERE and not in PrepareFinal
43
+ *
44
+ * PrepareFinal is where `finalContent` is set, which makes it look like the
45
+ * output seam. It is not, for one structural reason: it lives inside the Final
46
+ * BRANCH subflow, and a branch mount hands its `outputMapper` the branch's
47
+ * RESULT, not its scope — so nothing that stage writes reaches the run's
48
+ * shared state. Ledger rows filed there would sit in an isolated commit log,
49
+ * and one ledger would be split across two places a reader has to know about.
50
+ *
51
+ * The decider is one stage earlier, in the main chart, and it is the first
52
+ * moment the run KNOWS this turn is the final answer. Its writes commit before
53
+ * the branch resolves (footprintjs commits a decider stage ahead of matching
54
+ * the branch), and the value it rewrites — `llmLatestContent` — is exactly
55
+ * what PrepareFinal copies into `finalContent`. So the caller, the committed
56
+ * record, the `newMessages` the memory writes persist, and the ledger row all
57
+ * carry the same string. One seam, one answer.
58
+ *
59
+ * The `'tool-calls'` path is untouched: a turn that is still calling tools has
60
+ * not produced an answer, and running an output rule over a half-finished turn
61
+ * would be running it over something that is not the output.
62
+ *
63
+ * Empty chain → the exact synchronous decider this file has always exported.
64
+ */
65
+ function buildRouteDeciderStage(messageMiddleware) {
66
+ const chain = messageMiddleware ?? [];
67
+ if (chain.length === 0)
68
+ return exports.routeDeciderStage;
69
+ return async (scope) => {
70
+ const chosen = (0, exports.routeDeciderStage)(scope);
71
+ if (chosen !== 'final')
72
+ return chosen;
73
+ const verdict = await (0, runChain_js_1.runMessageChain)(chain, {
74
+ phase: 'output',
75
+ content: scope.llmLatestContent,
76
+ history: [...scope.history],
77
+ iteration: scope.iteration,
78
+ ...(scope.runIdentity && { identity: scope.runIdentity }),
79
+ });
80
+ (0, ledger_js_1.recordDecisions)(scope, verdict.decisions);
81
+ if (verdict.kind === 'deny') {
82
+ // The answer is NOT released. It stays in the commit log where the run
83
+ // put it, under whatever redaction the run configured, and the boundary
84
+ // raises instead of returning — handing the caller a refusal in place of
85
+ // an answer is the one substitution they must never make unknowingly.
86
+ scope.messageDeniedReason = verdict.reason;
87
+ scope.messageDeniedPhase = 'output';
88
+ scope.messageDeniedBy = verdict.middleware;
89
+ }
90
+ // Committed either way: on a refusal this is what was withheld, and the
91
+ // ledger row beside it says who withheld it and why.
92
+ scope.llmLatestContent = verdict.content;
93
+ return chosen;
94
+ };
95
+ }
96
+ exports.buildRouteDeciderStage = buildRouteDeciderStage;
36
97
  //# sourceMappingURL=route.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"route.js","sourceRoot":"","sources":["../../../../src/core/agent/stages/route.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;GAaG;;;AAGH,uEAAiE;AAK1D,MAAM,iBAAiB,GAAG,CAAC,KAA6B,EAAe,EAAE;IAC9E,MAAM,SAAS,GAAG,KAAK,CAAC,kBAAiD,CAAC;IAC1E,MAAM,SAAS,GAAG,KAAK,CAAC,SAAmB,CAAC;IAC5C,MAAM,MAAM,GACV,SAAS,CAAC,MAAM,GAAG,CAAC,IAAI,SAAS,GAAG,KAAK,CAAC,aAAa,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,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