File size: 8,695 Bytes
34810d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import { writeSync } from "node:fs";
import { redactRegisteredSecretValues } from "../logging/secret-redaction-registry.js";
import { resolveEnabledDebugProxySettings, type DebugProxySettings } from "./env.js";
import { REDACTED_CAPTURE_HEADER_VALUE } from "./header-redaction.js";
import { registerCaptureStoreFinalizer } from "./store-lifecycle.js";
import { getDebugProxyCaptureStore, persistEventPayload, safeJsonString } from "./store.sqlite.js";

const DEBUG_PROXY_FETCH_PATCH_KEY = Symbol.for("openclaw.debugProxy.fetchPatch");

type DebugProxyCaptureStoreLike = Pick<
  ReturnType<typeof getDebugProxyCaptureStore>,
  "upsertSession" | "endSession" | "recordEvent"
> &
  Partial<Pick<ReturnType<typeof getDebugProxyCaptureStore>, "close" | "isClosed">>;

export type DebugProxyCaptureRuntimeDeps = {
  getStore?: () => DebugProxyCaptureStoreLike;
  closeStore?: () => void;
  persistEventPayload?: (
    store: DebugProxyCaptureStoreLike,
    payload: Parameters<typeof persistEventPayload>[1],
  ) => ReturnType<typeof persistEventPayload>;
  safeJsonString?: typeof safeJsonString;
  fetchTarget?: typeof globalThis;
};

export function resolveRuntimeDeps(deps: DebugProxyCaptureRuntimeDeps = {}) {
  return {
    getStore: deps.getStore ?? getDebugProxyCaptureStore,
    closeStore: deps.closeStore,
    persistEventPayload:
      deps.persistEventPayload ??
      ((store, payload) =>
        // SAFETY: The default writer receives real stores; lightweight test stores supply their own writer.
        persistEventPayload(store as ReturnType<typeof getDebugProxyCaptureStore>, payload)),
    safeJsonString: deps.safeJsonString ?? safeJsonString,
    fetchTarget: deps.fetchTarget ?? globalThis,
  };
}

export type CaptureOwner = {
  settings: DebugProxySettings;
  runtime: ReturnType<typeof resolveRuntimeDeps>;
  store: DebugProxyCaptureStoreLike;
  active: boolean;
  pending: Set<() => void>;
  errors: unknown[];
  unregister: () => void;
  admission: CaptureAdmission;
};
type CaptureAdmission = { current?: CaptureOwner };
type CaptureRegistry = {
  owners: Map<string, CaptureOwner>;
  resolved: WeakMap<DebugProxySettings, CaptureAdmission>;
  ambient?: { sessionId: string; dbPath: string; admission: CaptureAdmission };
};
const captureOwners = new WeakMap<
  ReturnType<typeof resolveRuntimeDeps>["getStore"],
  CaptureRegistry
>();

type GlobalFetchPatchedState = {
  originalFetch: typeof globalThis.fetch;
  admission: CaptureAdmission;
};

type GlobalFetchPatchTarget = typeof globalThis & {
  [DEBUG_PROXY_FETCH_PATCH_KEY]?: GlobalFetchPatchedState;
};

const globalFetchPatches = new WeakMap<typeof globalThis.fetch, GlobalFetchPatchedState>();

/** Guarded requests own capture admission, including when given a saved patch. */
export function resolveDebugProxyFetchTransport(
  fetchImpl: typeof globalThis.fetch,
): typeof globalThis.fetch {
  return globalFetchPatches.get(fetchImpl)?.originalFetch ?? fetchImpl;
}

export function hasDebugProxyFetchPatch(
  fetchTarget: GlobalFetchPatchTarget,
  admission: CaptureAdmission,
): boolean {
  return fetchTarget[DEBUG_PROXY_FETCH_PATCH_KEY]?.admission === admission;
}

/** Keep wrapper identity and its admission together for matching-owner teardown. */
export function registerDebugProxyFetchPatch(
  fetchTarget: GlobalFetchPatchTarget,
  originalFetch: typeof globalThis.fetch,
  patchedFetch: typeof globalThis.fetch,
  admission: CaptureAdmission,
): void {
  const patch = { originalFetch, admission };
  fetchTarget[DEBUG_PROXY_FETCH_PATCH_KEY] = patch;
  globalFetchPatches.set(patchedFetch, patch);
  fetchTarget.fetch = patchedFetch;
}

export function uninstallDebugProxyGlobalFetchPatch(
  deps: DebugProxyCaptureRuntimeDeps = {},
  admission?: CaptureAdmission,
): void {
  const fetchTarget: GlobalFetchPatchTarget = resolveRuntimeDeps(deps).fetchTarget;
  const state = fetchTarget[DEBUG_PROXY_FETCH_PATCH_KEY];
  if (!state || (admission && state.admission !== admission)) {
    return;
  }
  fetchTarget.fetch = state.originalFetch;
  delete fetchTarget[DEBUG_PROXY_FETCH_PATCH_KEY];
}

export function isDebugProxyGlobalFetchPatchInstalled(): boolean {
  const fetchTarget: GlobalFetchPatchTarget = globalThis;
  return Boolean(fetchTarget[DEBUG_PROXY_FETCH_PATCH_KEY]);
}

function captureOwnerKey(settings: DebugProxySettings): string {
  // dbPath is the root-derived capture locator, not the shared database route.
  // Implicit session IDs survive state-root changes, so both identify an owner.
  return JSON.stringify([settings.dbPath, settings.sessionId]);
}

export function reportCapturePersistenceFailure(owner: CaptureOwner, error: unknown): void {
  owner.errors.push(error);
  // The earlier SQLite exit hook swallows close errors. Report synchronously
  // here before it closes the store; diagnostics must not interrupt settlement.
  try {
    const message = redactRegisteredSecretValues(
      error instanceof Error ? error.message : String(error),
      () => REDACTED_CAPTURE_HEADER_VALUE,
    );
    writeSync(2, `[proxy-capture] Capture persistence failed: ${message}\n`);
  } catch {
    // Preserve the original failure even if the diagnostic sink is unavailable.
  }
}

function finishCaptureOwner(owner: CaptureOwner): void {
  if (!owner.active) {
    return;
  }
  owner.active = false;
  owner.admission.current = undefined;
  const registry = captureOwners.get(owner.runtime.getStore)!;
  registry.owners.delete(captureOwnerKey(owner.settings));
  uninstallDebugProxyGlobalFetchPatch(owner.runtime, owner.admission);
  owner.unregister();
  for (const finish of owner.pending) {
    finish();
  }
  try {
    if (!owner.store.isClosed) {
      owner.store.endSession(owner.settings.sessionId);
    }
  } catch (error) {
    reportCapturePersistenceFailure(owner, error);
  }
  if (owner.errors.length) {
    throw new AggregateError(owner.errors.splice(0), "Capture session finalization failed.");
  }
}

export function resolveCaptureOwner(
  settings: DebugProxySettings,
  runtime: ReturnType<typeof resolveRuntimeDeps>,
  options: { initialize?: boolean; explicit?: boolean } = {},
): CaptureOwner | undefined {
  let registry = captureOwners.get(runtime.getStore);
  if (!registry) {
    registry = { owners: new Map(), resolved: new WeakMap() };
    captureOwners.set(runtime.getStore, registry);
  }
  const key = captureOwnerKey(settings);
  let owner = registry.owners.get(key);
  if (!owner) {
    // Explicit settings own their lifetime; ambient capture observes current
    // configuration. Keep only its current marker, not retired IDs or stores.
    const prior = options.explicit
      ? registry.resolved.get(settings)
      : registry.ambient?.sessionId === settings.sessionId &&
          registry.ambient.dbPath === settings.dbPath
        ? registry.ambient.admission
        : undefined;
    if (!options.initialize && prior) {
      return prior.current;
    }
    const store = runtime.getStore();
    if (store.isClosed) {
      return undefined;
    }
    owner = {
      settings,
      runtime,
      store,
      active: true,
      pending: new Set(),
      errors: [],
      unregister: () => {},
      admission: {},
    };
    owner.admission.current = owner;
    const retainedOwner = owner;
    owner.unregister = registerCaptureStoreFinalizer(store, () =>
      finishCaptureOwner(retainedOwner),
    );
    registry.owners.set(key, owner);
  }
  if (options.explicit) {
    registry.resolved.set(settings, owner.admission);
  } else {
    registry.ambient = {
      sessionId: settings.sessionId,
      dbPath: settings.dbPath,
      admission: owner.admission,
    };
  }
  return owner;
}

// Finalization closes the session and restores the fetch patch before closing
// the cached store, preventing later normal requests from being captured.
export function finalizeDebugProxyCapture(
  resolved?: DebugProxySettings,
  deps: DebugProxyCaptureRuntimeDeps = {},
): void {
  const settings = resolveEnabledDebugProxySettings(resolved);
  if (!settings) {
    return;
  }
  const runtime = resolveRuntimeDeps(deps);
  const owner = captureOwners.get(runtime.getStore)?.owners.get(captureOwnerKey(settings));
  if (owner) {
    uninstallDebugProxyGlobalFetchPatch(deps, owner.admission);
  }
  if (!owner?.active) {
    return;
  }
  const errors: unknown[] = [];
  try {
    finishCaptureOwner(owner);
  } catch (error) {
    errors.push(error);
  }
  try {
    if (owner.runtime.closeStore) {
      owner.runtime.closeStore();
    } else {
      owner.store.close?.();
    }
  } catch (error) {
    errors.push(error);
  }
  if (errors.length) {
    throw new AggregateError(errors, "Capture finalization failed.");
  }
}