Base: codex-main originale (prima del fork aKode)

This commit is contained in:
Andrea
2026-11-09 10:37:42 +01:00
commit 7874033c81
10166 changed files with 2728937 additions and 0 deletions

View File

@@ -0,0 +1,72 @@
use codex_history::CompactionCheckpoint;
use codex_history::RetainedContext;
use codex_protocol::models::ResponseItem;
/// Read-only conversation-history snapshot supplied by the extension host.
///
/// Implementations should retain the host's existing snapshot storage rather than
/// copying response payloads into an extension-owned collection.
pub trait ConversationHistorySnapshot: Send + Sync {
/// Returns the generation of the history captured by this snapshot.
fn history_version(&self) -> u64;
/// Host-owned revision captured with this snapshot. Advances on user messages and
/// history resets, but stays unchanged for compaction and internal context.
fn user_message_revision(&self) -> u64;
/// Changes when host-confirmed assistant evidence can change how Guardian interprets input.
/// Hosts without out-of-band assistant evidence may retain the default.
fn guardian_review_context_revision(&self) -> u64 {
0
}
/// Returns the snapshot's response items in conversation order.
fn items(&self) -> Box<dyn Iterator<Item = &ResponseItem> + Send + '_>;
/// Host-owned retained facts captured atomically with the parent model window.
/// These facts may be available while review still uses a legacy transcript.
fn retained_context(&self) -> Option<&RetainedContext> {
None
}
/// Whether review uses the parent checkpoint and model window instead of a legacy transcript.
/// Checkpoint compatibility is independent of access to retained user evidence.
fn uses_parent_context_for_review(&self) -> bool {
self.retained_context().is_some()
}
/// Whether the host retains a bounded review transcript independently of parent compaction.
/// Such snapshots must never seed reviewers with the parent's opaque checkpoint.
fn uses_independent_review_history(&self) -> bool {
false
}
/// Latest opaque checkpoint, including unusable items, with its recorded producer.
/// Hosts without provenance leave the producer unknown rather than using the live model.
fn latest_compaction(&self) -> Option<CompactionCheckpoint<'_>> {
self.items()
.filter_map(|item| CompactionCheckpoint::from_item(item, /*model_hash*/ None))
.last()
}
/// Original review evidence retained across parent compaction, in conversation order.
/// Hosts without separate retention provide their current history.
fn review_items(&self) -> Box<dyn Iterator<Item = &ResponseItem> + Send + '_> {
self.items()
}
/// Original source metadata stays attached to the exact unshortened history item.
/// Legacy providers cannot establish completeness from a message ID alone.
fn review_items_with_sources(
&self,
) -> Box<dyn Iterator<Item = (&ResponseItem, Option<&codex_history::RetainedSource>)> + Send + '_>
{
Box::new(self.review_items().map(|item| (item, None)))
}
/// Changes whenever offsets into the retained review evidence become invalid.
fn review_history_version(&self) -> u64 {
self.history_version()
}
}

View File

@@ -0,0 +1,38 @@
use codex_protocol::protocol::Event;
/// Extension warning with an explicit thread target and optional turn correlation.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ExtensionWarning {
/// Stable host-owned thread identifier used for delivery.
pub thread_id: String,
/// Stable host-owned turn identifier when the warning arose in a turn callback.
pub turn_id: Option<String>,
/// Concise warning message for the user.
pub message: String,
}
/// Host-provided fire-and-forget sink for extension-generated events.
///
/// Extensions construct protocol events with the correlation id appropriate for
/// the callback they are handling, then leave persistence, ordering, transport
/// fanout, and logging decisions to the host.
pub trait ExtensionEventSink: Send + Sync {
/// Queue one protocol event for host-owned delivery.
fn emit(&self, event: Event);
/// Queue one warning for host-owned delivery.
///
/// Implementations must use [`ExtensionWarning::thread_id`] for routing. The optional
/// [`ExtensionWarning::turn_id`] is correlation metadata and does not identify a thread.
fn emit_warning(&self, warning: ExtensionWarning);
}
/// Event sink used when the host does not expose extension event emission.
#[derive(Debug, Default, Clone, Copy)]
pub struct NoopExtensionEventSink;
impl ExtensionEventSink for NoopExtensionEventSink {
fn emit(&self, _event: Event) {}
fn emit_warning(&self, _warning: ExtensionWarning) {}
}

View File

@@ -0,0 +1,48 @@
/// Host-provided metrics capability for extension-owned behavior.
///
/// Implementations are expected to attach the host's session attribution before
/// forwarding samples to the configured metrics backend.
pub trait ExtensionMetrics: Send + Sync {
/// Increments a counter with optional extension-provided tags.
fn counter(&self, name: &str, inc: i64, tags: &[(&str, &str)]);
/// Records one histogram sample with optional extension-provided tags.
fn histogram(&self, name: &str, value: i64, tags: &[(&str, &str)]);
/// Records a histogram with explicit buckets, preserving host attribution.
/// All callers of the same metric name must use the same boundaries.
fn histogram_with_boundaries(
&self,
name: &str,
value: i64,
boundaries: &[f64],
tags: &[(&str, &str)],
);
}
/// Records a published catalog size, with partial results taking precedence over warnings.
pub fn record_catalog_entries(
metrics: Option<&dyn ExtensionMetrics>,
name: &str,
stage: &str,
entry_count: usize,
is_partial: bool,
warning_count: usize,
) {
let Some(metrics) = metrics else {
return;
};
let status = if is_partial {
"partial"
} else if warning_count > 0 {
"warning"
} else {
"complete"
};
metrics.histogram_with_boundaries(
name,
i64::try_from(entry_count).unwrap_or(i64::MAX),
&[0., 1., 2., 5., 10., 20., 50., 100., 200., 500., 1000.],
&[("stage", stage), ("status", status)],
);
}

View File

@@ -0,0 +1,14 @@
mod conversation_history;
mod events;
mod metrics;
mod response_items;
pub use conversation_history::ConversationHistorySnapshot;
pub use events::ExtensionEventSink;
pub use events::ExtensionWarning;
pub use events::NoopExtensionEventSink;
pub use metrics::ExtensionMetrics;
pub use metrics::record_catalog_entries;
pub use response_items::NoopResponseItemInjector;
pub use response_items::ResponseItemInjectionFuture;
pub use response_items::ResponseItemInjector;

View File

@@ -0,0 +1,33 @@
use std::future::Future;
use std::pin::Pin;
use codex_protocol::models::ResponseInputItem;
/// Future returned when an extension asks the host to inject model-visible input.
pub type ResponseItemInjectionFuture<'a> =
Pin<Box<dyn Future<Output = Result<(), Vec<ResponseInputItem>>> + Send + 'a>>;
/// Host-provided helper for extensions that need to steer the active model turn.
///
/// Implementations should inject the supplied response items into the active turn
/// when one can accept same-turn model input. If injection is unavailable, they
/// return the unchanged items to the caller.
pub trait ResponseItemInjector: Send + Sync {
fn inject_response_items<'a>(
&'a self,
items: Vec<ResponseInputItem>,
) -> ResponseItemInjectionFuture<'a>;
}
/// Injector used when a host does not expose same-turn model steering.
#[derive(Debug, Default, Clone, Copy)]
pub struct NoopResponseItemInjector;
impl ResponseItemInjector for NoopResponseItemInjector {
fn inject_response_items<'a>(
&'a self,
items: Vec<ResponseInputItem>,
) -> ResponseItemInjectionFuture<'a> {
Box::pin(std::future::ready(Err(items)))
}
}