Files
zeekay 1b367e2b62 chore: rebase the tree onto upstream master
The tree was a debranded snapshot of upstream taken around 2026-02-20, and
upstream has moved about five months since. This replaces it with upstream
master, mechanically re-derived: bin/debrand does the rename, and the
pipeline and squashed migrations are restored from the shipped tree.

Not a merge. The previous import replaced the tree wholesale in a single
commit, so there is no shared content history to merge against. Reproducing
the rename on the new tree is what actually converges.

What survives: .hanzo/workflows, the Dockerfiles and bin/docker* entrypoints,
compose files, LLM.md, and insights/migrations. The squashed initial
migration is load-bearing — the live database records it by name, so
upstream's ~918 individual migrations cannot come back.

Nine products are dropped as unreferenced. Twenty-one more that we do not run
are kept anyway because the core app still imports them; unpicking those is a
refactor and does not belong in a rebase.

This base does not build yet, and the reasons are not in the rename:
upstream now wants Python 3.13.13 against our 3.12.12 and Django 5.2 against
our 4.2, and the dependency manifests still name upstream packages that we
republish under our own names. Those are decisions, not renames.
2026-08-01 15:44:40 -07:00

67 lines
2.8 KiB
TypeScript

import { CompatMessage } from './types'
// Heuristic mirrors the AI observability skill script `extract_conversation.py` at
// `products/ai_observability/skills/exploring-llm-traces/scripts/` — keep in sync.
/**
* Provider-specific transport metadata that lives on typed content parts but
* does NOT change what the user sees. Stripped before signing so that the
* same user-visible message dedups across turns even if the caller changes
* its cache hint, the SDK adds a verification signature, or routing metadata
* differs between echoes.
*
* - `cache_control` — Anthropic cache hints on text parts; vary across turns.
* - `signature` — Anthropic crypto signature on `thinking` parts; varies between echoes.
* - `caller` — routing metadata on `tool_use` parts; not user-visible.
*
* The replacer fires recursively for every key during serialisation, so these
* fields are dropped at any nesting level.
*/
const TRANSPORT_METADATA_KEYS = new Set(['cache_control', 'signature', 'caller'])
function isPlainTextPart(part: unknown): boolean {
return (
typeof part === 'object' &&
part !== null &&
(part as Record<string, unknown>).type === 'text' &&
typeof (part as Record<string, unknown>).text === 'string'
)
}
function normalizeSignatureField(key: string, value: unknown): unknown {
if (TRANSPORT_METADATA_KEYS.has(key)) {
return undefined
}
// Converge text-only typed-parts with their flat-string equivalent. SDKs
// round-trip the same assistant reply between `content: 'Hello'` (OpenAI
// flat-string) and `content: [{type: 'text', text: 'Hello'}]` (typed parts);
// without this convergence the signatures diverge and the message re-renders.
// Only flatten all-text arrays — mixed content (text + tool_use) has no
// flat-string equivalent.
if (key === 'content' && Array.isArray(value) && value.length > 0 && value.every(isPlainTextPart)) {
return value.map((part) => (part as { text: string }).text).join('')
}
return value
}
/**
* Stable string hash for a normalized message. Keys on role, content,
* tool_calls, tool_call_id, and the synthetic tools list — ignoring transport
* metadata (see `TRANSPORT_METADATA_KEYS`) and converging text-only typed-parts
* arrays with their flat-string equivalent (see `normalizeSignatureField`).
*/
export function messageSignature(message: CompatMessage): string {
// JSON.stringify preserves order
return JSON.stringify(
{
role: message.role ?? '',
content: message.content ?? '',
tool_calls: message.tool_calls ?? null,
tool_call_id: message.tool_call_id ?? '',
tools: (message as { tools?: unknown }).tools ?? null,
},
// Call normalizeSignatureField for each field above:
normalizeSignatureField
)
}