File size: 23,471 Bytes
d197cf3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
/** Immutable execution identity context storage and run-admission projection. */
import type { DatabaseSync } from "node:sqlite";
import type { Selectable } from "kysely";
import type { ExecutionIdentityContextV1 } from "../../packages/gateway-protocol/src/index.js";
import { validateExecutionIdentityContextV1 } from "../../packages/gateway-protocol/src/index.js";
import { hasOperatorApprovalReceiptsForRun } from "../gateway/operator-approval-store.js";
import {
  executeSqliteQuerySync,
  executeSqliteQueryTakeFirstSync,
  getNodeSqliteKysely,
} from "../infra/kysely-sync.js";
import { normalizeSqliteNumber } from "../infra/sqlite-number.js";
import { withExistingOpenClawStateDatabaseReadOnly } from "../state/openclaw-state-db-readonly.js";
import { tableExists } from "../state/openclaw-state-db-schema-helpers.js";
import type { DB as OpenClawStateKyselyDatabase } from "../state/openclaw-state-db.generated.js";
import {
  openOpenClawStateDatabase,
  runOpenClawStateWriteTransaction,
  type OpenClawStateDatabaseOptions,
} from "../state/openclaw-state-db.js";
import { createOpenClawStateSchemaEnsurer } from "../state/openclaw-state-feature-schema.js";
import { clearAuditIdentityKeyCacheForDatabase } from "./audit-identity.js";
import { hasExecutionDecisionFactsForRun } from "./execution-decision-facts.js";
import {
  presentExecutionDecisionReceipts,
  type InternalAuditRunInspectResult,
} from "./execution-decision-receipts.js";
import {
  parseExecutionIdentityAdmissionEnvelope,
  parseExecutionIdentityAdmissionWork,
  type ExecutionIdentityAdmissionToken,
} from "./execution-identity-admission.js";
import {
  buildExecutionIdentityContext,
  ensureBoundedExecutionIdentityRef,
  freezeExecutionIdentityContext,
} from "./execution-identity-context-build.js";

type ExecutionIdentityDatabase = Pick<
  OpenClawStateKyselyDatabase,
  "audit_events" | "execution_identity_contexts"
>;
type ExecutionIdentityRow = Selectable<OpenClawStateKyselyDatabase["execution_identity_contexts"]>;

const EXECUTION_IDENTITY_CONTEXT_MAX_BYTES = 16 * 1024;
const EXECUTION_IDENTITY_CONTEXT_RETENTION_MS = 30 * 24 * 60 * 60_000;
const EXECUTION_IDENTITY_CONTEXT_MAX_ROWS = 100_000;
const EXECUTION_IDENTITY_CONTEXT_PRUNE_BATCH_ROWS = 1_024;
const EXECUTION_IDENTITY_HMAC_REF_RE = /^hmac-sha256:v1:[a-f0-9]{32}:[a-f0-9]{64}$/u;

const ensureExecutionIdentityContextSchema = createOpenClawStateSchemaEnsurer({
  table: "execution_identity_contexts",
  endMarker: "  ON execution_identity_contexts (run_id, created_at, execution_id);\n",
  operationLabel: "audit.execution-identity.schema.ensure",
});

type ExecutionIdentityStoreOptions = OpenClawStateDatabaseOptions & {
  now?: number;
  limits?: {
    maxRows: number;
    pruneBatchRows: number;
  };
};

type ExecutionIdentityReadOptions = OpenClawStateDatabaseOptions & {
  now?: number;
};

type ExecutionIdentityContextReadResult =
  | { status: "found"; context: ExecutionIdentityContextV1 }
  | { status: "expired"; runId: string }
  | { status: "missing" }
  | { status: "corrupt"; runId: string; reasonCode: "identity_context_corrupt" };

function executionIdentityDb(db: DatabaseSync) {
  return getNodeSqliteKysely<ExecutionIdentityDatabase>(db);
}

function parseExecutionIdentityRow(row: ExecutionIdentityRow): ExecutionIdentityContextV1 {
  if (
    typeof row.context_json !== "string" ||
    Buffer.byteLength(row.context_json, "utf8") !== normalizeSqliteNumber(row.context_bytes) ||
    Buffer.byteLength(row.context_json, "utf8") > EXECUTION_IDENTITY_CONTEXT_MAX_BYTES
  ) {
    throw new Error("invalid context payload bounds");
  }
  const parsed = JSON.parse(row.context_json) as unknown;
  if (!validateExecutionIdentityContextV1(parsed)) {
    throw new Error("invalid context payload schema");
  }
  if (
    parsed.contextId !== row.context_id ||
    parsed.executionId !== row.execution_id ||
    parsed.runId !== row.run_id ||
    parsed.createdAt !== normalizeSqliteNumber(row.created_at) ||
    parsed.coverageState !== row.coverage_state ||
    JSON.stringify(parsed) !== row.context_json ||
    !EXECUTION_IDENTITY_HMAC_REF_RE.test(parsed.trustDomain.domainRef) ||
    !EXECUTION_IDENTITY_HMAC_REF_RE.test(parsed.runtimeInstance.runtimeRef)
  ) {
    throw new Error("context payload disagrees with indexed columns");
  }
  return freezeExecutionIdentityContext(parsed);
}

function readRowByExecutionId(
  db: DatabaseSync,
  executionId: string,
): ExecutionIdentityRow | undefined {
  return executeSqliteQueryTakeFirstSync(
    db,
    executionIdentityDb(db)
      .selectFrom("execution_identity_contexts")
      .selectAll()
      .where("execution_id", "=", executionId),
  );
}

function readRowsByRunId(
  db: DatabaseSync,
  runId: string,
  now: number,
  offset: number,
  limit: number,
): ExecutionIdentityRow[] {
  return executeSqliteQuerySync(
    db,
    executionIdentityDb(db)
      .selectFrom("execution_identity_contexts")
      .selectAll()
      .where("run_id", "=", runId)
      .where("created_at", ">=", now - EXECUTION_IDENTITY_CONTEXT_RETENTION_MS)
      .orderBy("created_at", "asc")
      .orderBy("execution_id", "asc")
      .offset(offset)
      .limit(limit),
  ).rows;
}

function deleteExpiredExecutionIdentityContexts(db: DatabaseSync, now: number, limit: number) {
  const kysely = executionIdentityDb(db);
  const expiredIds = kysely
    .selectFrom("execution_identity_contexts")
    .select("context_id")
    .where("created_at", "<", now - EXECUTION_IDENTITY_CONTEXT_RETENTION_MS)
    .orderBy("created_at", "asc")
    .orderBy("context_id", "asc")
    .limit(limit);
  return executeSqliteQuerySync(
    db,
    kysely.deleteFrom("execution_identity_contexts").where("context_id", "in", expiredIds),
  );
}

function pruneExecutionIdentityContextsAfterInsert(
  db: DatabaseSync,
  now: number,
  limits: { maxRows: number; pruneBatchRows: number },
): void {
  const kysely = executionIdentityDb(db);
  const expired = deleteExpiredExecutionIdentityContexts(db, now, limits.pruneBatchRows);
  const expiredCount = Number(expired.numAffectedRows ?? 0n);
  const remainingPruneBudget = Math.max(0, limits.pruneBatchRows - expiredCount);
  if (remainingPruneBudget > 0) {
    // Derive overflow from committed rows inside this transaction. A process-local
    // count misses writes from the Gateway worker or a concurrent direct CLI.
    const retainedIds = kysely
      .selectFrom("execution_identity_contexts")
      .select("context_id")
      .orderBy("created_at", "desc")
      .orderBy("context_id", "desc")
      .limit(limits.maxRows);
    const oldestOverflowIds = kysely
      .selectFrom("execution_identity_contexts")
      .select("context_id")
      .where("context_id", "not in", retainedIds)
      .orderBy("created_at", "asc")
      .orderBy("context_id", "asc")
      .limit(remainingPruneBudget);
    executeSqliteQuerySync(
      db,
      kysely.deleteFrom("execution_identity_contexts").where("context_id", "in", oldestOverflowIds),
    );
  }
}

/** Delete one bounded batch during the existing audit startup/hourly maintenance tick. */
export function pruneExpiredExecutionIdentityContexts(
  params: {
    now?: number;
    database?: OpenClawStateDatabaseOptions;
  } = {},
): number {
  const databaseOptions = params.database ?? {};
  const database = openOpenClawStateDatabase(databaseOptions);
  // Maintenance must not create opt-in storage. First capture owns schema creation;
  // once the table exists, cleanup remains active even after collection is disabled.
  if (!tableExists(database.db, "execution_identity_contexts")) {
    return 0;
  }
  return runOpenClawStateWriteTransaction(
    ({ db }) => {
      const deleted = deleteExpiredExecutionIdentityContexts(
        db,
        params.now ?? Date.now(),
        EXECUTION_IDENTITY_CONTEXT_PRUNE_BATCH_ROWS,
      );
      return Number(deleted.numAffectedRows ?? 0n);
    },
    { ...databaseOptions, database },
    { operationLabel: "audit.execution-identity.context.maintenance" },
  );
}

/** Worker-owned canonicalization and persistence for one accepted admission envelope. */
function persistExecutionIdentityAdmissionEnvelope(
  input: unknown,
  options: ExecutionIdentityStoreOptions = {},
): ExecutionIdentityContextV1 {
  // Structured clone removes the admission-side freeze. Revalidate all bounds
  // before schema/key access so malformed messages never reach persistence.
  const envelope = parseExecutionIdentityAdmissionEnvelope(input);
  ensureExecutionIdentityContextSchema(options);
  const executionId = envelope.executionId;
  const opened = openOpenClawStateDatabase(options);
  // HMAC lookup/key creation and canonical serialization finish before BEGIN.
  // The transaction only rereads the authoritative row and synchronously commits.
  const plannedContext = buildExecutionIdentityContext(opened.db, envelope, {
    contextId: envelope.contextId,
    createdAt: envelope.createdAt,
  });
  const plannedContextJson = JSON.stringify(plannedContext);
  let transactionDatabase: DatabaseSync | undefined;
  try {
    return runOpenClawStateWriteTransaction(
      ({ db }) => {
        transactionDatabase = db;
        const existing = readRowByExecutionId(db, executionId);
        if (existing) {
          const context = parseExecutionIdentityRow(existing);
          // Full canonical bytes, including the captured ID and timestamp, own replay identity.
          // Never rewrite a newly captured envelope to resemble the retained execution context.
          if (plannedContextJson !== existing.context_json) {
            throw new Error("execution identity context conflict for execution");
          }
          return context;
        }
        executeSqliteQuerySync(
          db,
          executionIdentityDb(db)
            .insertInto("execution_identity_contexts")
            .values({
              context_id: plannedContext.contextId,
              execution_id: plannedContext.executionId,
              run_id: plannedContext.runId,
              created_at: plannedContext.createdAt,
              coverage_state: plannedContext.coverageState,
              context_bytes: Buffer.byteLength(plannedContextJson, "utf8"),
              context_json: plannedContextJson,
            }),
        );
        pruneExecutionIdentityContextsAfterInsert(
          db,
          options.now ?? Date.now(),
          options.limits ?? {
            maxRows: EXECUTION_IDENTITY_CONTEXT_MAX_ROWS,
            pruneBatchRows: EXECUTION_IDENTITY_CONTEXT_PRUNE_BATCH_ROWS,
          },
        );
        return plannedContext;
      },
      options,
      { operationLabel: "audit.execution-identity.context.record" },
    );
  } catch (error) {
    if (transactionDatabase) {
      clearAuditIdentityKeyCacheForDatabase(transactionDatabase);
    }
    throw error;
  }
}

/** A durable recovery retry may only confirm the originally captured execution. */
function verifyExecutionIdentityAdmissionRetry(
  token: ExecutionIdentityAdmissionToken,
  options: ExecutionIdentityReadOptions = {},
): ExecutionIdentityContextV1 {
  const { db } = openOpenClawStateDatabase(options);
  if (!tableExists(db, "execution_identity_contexts")) {
    throw new Error("execution identity recovery evidence unavailable");
  }
  const existing = readRowByExecutionId(db, token.executionId);
  if (!existing) {
    // Never reconstruct identity from the later runtime after an ambiguous restart.
    throw new Error("execution identity recovery evidence unavailable");
  }
  const context = parseExecutionIdentityRow(existing);
  if (
    context.contextId !== token.contextId ||
    context.executionId !== token.executionId ||
    context.runId !== token.runId ||
    context.createdAt !== token.createdAt
  ) {
    throw new Error("execution identity context conflict for execution");
  }
  return context;
}

/** Worker-owned persistence/verification for one accepted bounded queue item. */
export function processExecutionIdentityAdmissionWork(
  input: unknown,
  options: ExecutionIdentityStoreOptions = {},
): ExecutionIdentityContextV1 {
  const work = parseExecutionIdentityAdmissionWork(input);
  return work.kind === "capture"
    ? persistExecutionIdentityAdmissionEnvelope(work.envelope, options)
    : verifyExecutionIdentityAdmissionRetry(work.token, options);
}

/** Read one exact execution while turning malformed rows into typed diagnostics. */
function readExecutionIdentityContextByExecutionId(
  executionId: string,
  options: ExecutionIdentityReadOptions = {},
): ExecutionIdentityContextReadResult {
  const normalizedExecutionId = ensureBoundedExecutionIdentityRef(executionId, "execution id");
  return (
    withExistingOpenClawStateDatabaseReadOnly(({ db }) => {
      if (!tableExists(db, "execution_identity_contexts")) {
        return { status: "missing" } as const;
      }
      const row = readRowByExecutionId(db, normalizedExecutionId);
      if (!row) {
        return { status: "missing" } as const;
      }
      const createdAt = normalizeSqliteNumber(row.created_at);
      if (
        createdAt !== undefined &&
        createdAt < (options.now ?? Date.now()) - EXECUTION_IDENTITY_CONTEXT_RETENTION_MS
      ) {
        // The indexed timestamp may explain availability, but expired context JSON
        // must never be parsed or projected while bounded maintenance catches up.
        return { status: "expired", runId: row.run_id } as const;
      }
      try {
        return { status: "found", context: parseExecutionIdentityRow(row) } as const;
      } catch {
        return {
          status: "corrupt",
          runId: row.run_id,
          reasonCode: "identity_context_corrupt",
        } as const;
      }
    }, options) ?? { status: "missing" }
  );
}

function unavailableResult(params: {
  selector: { runId: string } | { executionId: string };
  resolvedRunId?: string;
  runStatus: "known" | "unknown";
  state: "unknown" | "unsupported";
  reasonCode: string;
  missingEvidence: string[];
  remediation: Array<{ code: string; text: string }>;
}): InternalAuditRunInspectResult {
  const run: InternalAuditRunInspectResult["run"] =
    "executionId" in params.selector
      ? {
          executionId: params.selector.executionId,
          ...(params.resolvedRunId ? { runId: params.resolvedRunId } : {}),
          status: params.runStatus,
        }
      : {
          runId: params.resolvedRunId ?? params.selector.runId,
          status: params.runStatus,
        };
  return {
    schemaVersion: 1,
    run,
    identity: {
      state: params.state,
      reasonCode: params.reasonCode,
      missingEvidence: params.missingEvidence,
      remediation: params.remediation,
    },
    decisions: [],
    decisionDisplays: [],
    coverage: { state: params.state, missingEvidence: params.missingEvidence },
  };
}

function unavailableIdentityContext(
  selector: { runId: string } | { executionId: string },
  remediation: { code: string; text: string },
  resolvedRunId?: string,
): InternalAuditRunInspectResult {
  return unavailableResult({
    selector,
    resolvedRunId,
    runStatus: "known",
    state: "unsupported",
    reasonCode: "identity_context_unavailable",
    missingEvidence: ["identity.context"],
    remediation: [remediation],
  });
}

function inspectExactExecution(
  params: { executionId: string; decisionCursor?: string; decisionLimit?: number },
  options: ExecutionIdentityReadOptions,
): InternalAuditRunInspectResult {
  const executionId = ensureBoundedExecutionIdentityRef(params.executionId, "execution id");
  const selector = { executionId };
  const contextResult = readExecutionIdentityContextByExecutionId(executionId, options);
  if (contextResult.status === "found") {
    return presentExecutionDecisionReceipts({
      context: contextResult.context,
      decisionCursor: params.decisionCursor,
      decisionLimit: params.decisionLimit,
      options,
    });
  }
  if (contextResult.status === "corrupt") {
    return unavailableResult({
      selector,
      resolvedRunId: contextResult.runId,
      runStatus: "known",
      state: "unknown",
      reasonCode: contextResult.reasonCode,
      missingEvidence: ["identity.context.valid"],
      remediation: [
        {
          code: "inspect_state_integrity",
          text: "Run openclaw doctor and inspect the shared state database before trusting this execution.",
        },
      ],
    });
  }
  if (contextResult.status === "expired") {
    return unavailableIdentityContext(
      selector,
      {
        code: "run_again_after_expiry",
        text: "This execution's identity context is outside the 30-day retention window; run the operation again to record a new context.",
      },
      contextResult.runId,
    );
  }
  return unavailableResult({
    selector,
    runStatus: "unknown",
    state: "unknown",
    reasonCode: "execution_not_found",
    missingEvidence: ["identity.context"],
    remediation: [
      {
        code: "verify_execution_id",
        text: "Verify the exact execution id; absence of best-effort identity evidence is not proof that no run occurred.",
      },
    ],
  });
}

function hasAnyRunContext(db: DatabaseSync, runId: string): boolean {
  return Boolean(
    executeSqliteQueryTakeFirstSync(
      db,
      executionIdentityDb(db)
        .selectFrom("execution_identity_contexts")
        .select("context_id")
        .where("run_id", "=", runId)
        .limit(1),
    ),
  );
}

function hasRetainedAuditRun(db: DatabaseSync, runId: string, now: number): boolean {
  if (!tableExists(db, "audit_events")) {
    return false;
  }
  return Boolean(
    executeSqliteQueryTakeFirstSync(
      db,
      executionIdentityDb(db)
        .selectFrom("audit_events")
        .select("sequence")
        .where("run_id", "=", runId)
        .where("occurred_at", ">=", now - EXECUTION_IDENTITY_CONTEXT_RETENTION_MS)
        .where("kind", "!=", "message")
        .limit(1),
    ),
  );
}

function inspectRunSelector(
  params: {
    runId: string;
    executionOffset?: number;
    executionLimit?: number;
    decisionCursor?: string;
    decisionLimit?: number;
  },
  options: ExecutionIdentityReadOptions,
): InternalAuditRunInspectResult {
  const runId = ensureBoundedExecutionIdentityRef(params.runId, "run id");
  const now = options.now ?? Date.now();
  const inspected = withExistingOpenClawStateDatabaseReadOnly<
    InternalAuditRunInspectResult | undefined
  >(({ db }) => {
    const firstMatches = tableExists(db, "execution_identity_contexts")
      ? readRowsByRunId(db, runId, now, 0, 2)
      : [];
    if (firstMatches.length === 1) {
      let context: ExecutionIdentityContextV1;
      try {
        context = parseExecutionIdentityRow(firstMatches[0]!);
      } catch {
        return unavailableResult({
          selector: { runId },
          runStatus: "known",
          state: "unknown",
          reasonCode: "identity_context_corrupt",
          missingEvidence: ["identity.context.valid"],
          remediation: [
            {
              code: "inspect_state_integrity",
              text: "Run openclaw doctor and inspect the shared state database before trusting this run.",
            },
          ],
        });
      }
      return presentExecutionDecisionReceipts({
        context,
        decisionCursor: params.decisionCursor,
        decisionLimit: params.decisionLimit,
        options,
      });
    }
    if (firstMatches.length > 1) {
      const offset = params.executionOffset ?? 0;
      const limit = params.executionLimit ?? 50;
      const page = readRowsByRunId(db, runId, now, offset, limit + 1);
      const candidates = page.slice(0, limit).map((row) => ({
        executionId: row.execution_id,
        contextId: row.context_id,
        createdAt: normalizeSqliteNumber(row.created_at) ?? 0,
      }));
      return {
        schemaVersion: 1,
        run: { runId, status: "known" },
        identity: {
          state: "ambiguous",
          reasonCode: "execution_selection_required",
          candidates,
          missingEvidence: ["execution.selection"],
          remediation: [
            {
              code: "select_execution_id",
              text: "Select one candidate with openclaw audit --execution <id> --explain.",
            },
          ],
        },
        decisions: [],
        decisionDisplays: [],
        coverage: { state: "unknown", missingEvidence: ["execution.selection"] },
        ...(page.length > limit ? { nextExecutionCursor: String(offset + limit) } : {}),
      };
    }
    if (
      hasOperatorApprovalReceiptsForRun({ runId, nowMs: now, databaseOptions: options }) ||
      hasExecutionDecisionFactsForRun({ runId, now, database: options })
    ) {
      return unavailableResult({
        selector: { runId },
        runStatus: "known",
        state: "unknown",
        reasonCode: "decision_context_link_missing",
        missingEvidence: ["identity.context", "decision.context_link"],
        remediation: [
          {
            code: "record_new_identity_context",
            text: "Confirm execution identity collection is enabled, then run and request the action again to record a linked context.",
          },
        ],
      });
    }
    if (tableExists(db, "execution_identity_contexts") && hasAnyRunContext(db, runId)) {
      return unavailableIdentityContext(
        { runId },
        {
          code: "run_again_after_expiry",
          text: "This run's retained identity contexts are outside the 30-day window; run the operation again to record a new execution.",
        },
      );
    }
    try {
      if (hasRetainedAuditRun(db, runId, now)) {
        return unavailableIdentityContext(
          { runId },
          {
            code: "record_new_identity_context",
            text: "Confirm audit collection is enabled and the Gateway is current, then run the operation again to record a new execution context.",
          },
        );
      }
    } catch {
      return unavailableResult({
        selector: { runId },
        runStatus: "unknown",
        state: "unknown",
        reasonCode: "run_evidence_unreadable",
        missingEvidence: ["run.record", "identity.context"],
        remediation: [
          {
            code: "inspect_state_integrity",
            text: "Run openclaw doctor and retry the run inspection.",
          },
        ],
      });
    }
    return undefined;
  }, options);
  if (inspected) {
    return inspected;
  }
  return unavailableResult({
    selector: { runId },
    runStatus: "unknown",
    state: "unknown",
    reasonCode: "run_not_found",
    missingEvidence: ["run.record", "identity.context"],
    remediation: [
      {
        code: "verify_run_id",
        text: "Verify the run id; absence of best-effort audit activity is not proof of no run.",
      },
    ],
  });
}

/** Inspect one exact execution or discover bounded executions for a run correlation. */
export function inspectExecutionIdentityRun(
  params:
    | {
        runId: string;
        executionOffset?: number;
        executionLimit?: number;
        decisionCursor?: string;
        decisionLimit?: number;
      }
    | { executionId: string; decisionCursor?: string; decisionLimit?: number },
  options: ExecutionIdentityReadOptions = {},
): InternalAuditRunInspectResult {
  return "executionId" in params
    ? inspectExactExecution(params, options)
    : inspectRunSelector(params, options);
}