mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-03 03:36:57 +01:00
fix(hermes): settle closed retry work with unknown usage
Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
1 parent
a978fd5b4a
commit
391e56d8a0
15 files changed
+261
-31
No files matched your search
@@ -4,6 +4,7 @@ export type {
|
||||
AdapterRuntime,
|
||||
UsageSummary,
|
||||
AdapterUsageCheckpoint,
|
||||
AdapterAccountingSettlement,
|
||||
AdapterBillingType,
|
||||
AdapterRuntimeServiceReport,
|
||||
AdapterExecutionResult,
|
||||
|
||||
@@ -40,6 +40,13 @@ export interface UsageSummary {
|
||||
}
|
||||
|
||||
/** Accounting-only snapshot. Never include prompt, response, or credentials. */
|
||||
export interface AdapterAccountingSettlement {
|
||||
schema: "paperclip.accounting.settlement/v1";
|
||||
/** Owned provider work ended; unknown measurements remain explicitly unknown. */
|
||||
providerWorkEnded: true;
|
||||
usageComplete: boolean;
|
||||
}
|
||||
|
||||
export interface AdapterUsageCheckpoint {
|
||||
attemptId?: string;
|
||||
usage?: UsageSummary;
|
||||
@@ -57,6 +64,7 @@ export interface AdapterUsageCheckpoint {
|
||||
cacheAdjustedCostUsd?: number | null;
|
||||
providerRequestId?: string | null;
|
||||
complete: boolean;
|
||||
settlement?: AdapterAccountingSettlement;
|
||||
}
|
||||
|
||||
export type AdapterBillingType =
|
||||
@@ -143,6 +151,7 @@ export interface AdapterExecutionResult {
|
||||
pricingProvenance?: { source: "provider_reported" | "provider_invoice" | "operator" | "rate_card" | "unknown"; version?: string; evidence?: string; inputCentsPerMillion?: string; cachedInputCentsPerMillion?: string; cacheWriteCentsPerMillion?: string; outputCentsPerMillion?: string; serviceTier?: string; contextTier?: "short" | "long" };
|
||||
providerRequestId?: string | null;
|
||||
usageComplete?: boolean;
|
||||
settlement?: AdapterAccountingSettlement;
|
||||
/**
|
||||
* Provider-billed cost after prompt-cache discounts. Adapters should set
|
||||
* this when they expose it separately; otherwise the server treats a
|
||||
|
||||
@@ -117,11 +117,19 @@ extend the task's execution deadline.
|
||||
using it for accounting. Cumulative session cost and native model-price
|
||||
estimates cannot supply this receipt.
|
||||
- Failed, interrupted, unsupported asynchronous and background delegated work
|
||||
leave settlement incomplete. Known positive reported subtotals survive, but
|
||||
leave measurement totals incomplete. Known positive reported subtotals survive, but
|
||||
missing charges remain unpriced. Explicit reported zero is accepted only for
|
||||
a complete receipt. Other provider/protocol routes retain their existing
|
||||
unavailable billed-cost behavior. Complete live billing, including delegated
|
||||
work, remains a qualification gate; these checks do not qualify a provider.
|
||||
- Once the native provider boundary closes, an optional versioned accounting
|
||||
settlement can acknowledge that known subtotal even when tokens or charges
|
||||
are incomplete. Missing token totals stay unknown, incomplete charges stay
|
||||
unpriced, and the existing controller finalization and capture-failure fences
|
||||
still control acknowledgement. Older incomplete receipts remain pending.
|
||||
- Paperclip owns task titles. Hermes keeps its immediate derived session title;
|
||||
its paid background title upgrade is disabled in the managed profile so it
|
||||
cannot start inference after a turn's usage receipt has closed.
|
||||
|
||||
The managed execution middleware applies planning/read-only, protected-path and
|
||||
permission policy to inline, concurrent and delegated native tools. Assigned
|
||||
|
||||
@@ -37,9 +37,9 @@ test('pinned Hermes streams images and semantic completion through Rust PRP and
|
||||
const server = createServer(async (req, res) => {
|
||||
const chunks = []; for await (const chunk of req) chunks.push(chunk);
|
||||
const body = JSON.parse(Buffer.concat(chunks).toString() || '{}');
|
||||
requests.push(body);
|
||||
assert.equal(req.headers.authorization, undefined);
|
||||
if (req.url !== '/v1/chat/completions') { res.writeHead(404).end(); return; }
|
||||
requests.push(body);
|
||||
if (!body.stream) {
|
||||
res.writeHead(200, { 'content-type': 'application/json' });
|
||||
res.end(JSON.stringify({ id: 'title', object: 'chat.completion', model: 'hermes-fixture', choices: [{ index: 0, message: { role: 'assistant', content: 'Fixture' }, finish_reason: 'stop' }] }));
|
||||
@@ -117,6 +117,7 @@ test('pinned Hermes streams images and semantic completion through Rust PRP and
|
||||
assert.ok(events.some(event => JSON.stringify(event.payload).includes('Checking the native runner path.')), 'Reasoning did not cross PRP');
|
||||
assert.ok(events.some(event => JSON.stringify(event.payload).includes('Native Rust')), 'Text did not cross PRP');
|
||||
assert.ok(requests.some(body => body.messages?.some(message => Array.isArray(message.content) && message.content.some(part => part.type === 'image_url' && part.image_url.url === `data:image/png;base64,${imageData}`))), 'Images did not cross TS/Rust/sidecar');
|
||||
assert.ok(requests.every(body => body.stream), 'Managed Hermes started auxiliary title inference');
|
||||
const snapshot = await session.snapshot();
|
||||
assert.equal(snapshot.semanticResult?.reportedWorkDisposition, 'done');
|
||||
assert.equal(snapshot.terminal?.runTerminalState, 'succeeded');
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/** Reviewed native closures. Setup cannot adopt a digest from downloaded files. */
|
||||
export const HERMES_CLOSURES: Readonly<Record<string, string>> = Object.freeze({
|
||||
"darwin-arm64": "9f1af058a963305ccd5c4f1555f2828458f72677ceacea2de456ac503f7212de",
|
||||
"linux-x64": "dd918ec15f5bd8025f3d01c4f2849eac4de8d59e237a520607f9c5f988a94645",
|
||||
"darwin-arm64": "ad1e555296e51be6d20a7234daeb7028570c7ee8fee9d27fb88c4f550491a07f",
|
||||
"linux-x64": "353a942b2a88542db8537611535898ae6088045608b88b4ddab491a6b000b8cf",
|
||||
});
|
||||
@@ -534,9 +534,17 @@ def main():
|
||||
asyncio.run(acp.run_agent(ManagedHermesACPAgent(), use_unstable_protocol=True))
|
||||
|
||||
|
||||
def disable_background_title_inference():
|
||||
# Paperclip owns task titles. Keep Hermes's immediate derived session title,
|
||||
# but prevent a paid title-upgrade thread from outliving the turn receipt.
|
||||
from agent import title_generator
|
||||
title_generator._model_title_upgrade_enabled = lambda: False
|
||||
|
||||
|
||||
def configure_managed_runtime():
|
||||
install_tool_process_policy()
|
||||
install_billing_capture()
|
||||
disable_background_title_inference()
|
||||
from tools import delegate_tool_dispatch
|
||||
delegate_tool_dispatch._dispatch_background = fence_background_dispatch(delegate_tool_dispatch._dispatch_background)
|
||||
from agent import turn_response_check
|
||||
|
||||
@@ -6,12 +6,27 @@ from types import SimpleNamespace
|
||||
from unittest.mock import AsyncMock, Mock, patch
|
||||
|
||||
from acp.schema import ClientCapabilities
|
||||
from bridge import ManagedHermesACPAgent, ManagedSessionManager, ChannelClient, native_answers, question_set, install_no_auth_transport, turn_usage, USAGE_COUNTERS
|
||||
from bridge import ManagedHermesACPAgent, ManagedSessionManager, ChannelClient, native_answers, question_set, install_no_auth_transport, disable_background_title_inference, turn_usage, USAGE_COUNTERS
|
||||
from policy import authorize_tool
|
||||
from billing import TurnBilling
|
||||
|
||||
|
||||
class Accounting(unittest.TestCase):
|
||||
def test_managed_title_keeps_derived_title_without_background_inference(self):
|
||||
from agent import title_generator, memory_provider
|
||||
store = Mock()
|
||||
store.get_session_title.return_value = None
|
||||
store.set_auto_title.return_value = True
|
||||
with patch.object(title_generator, "_model_title_upgrade_enabled"), \
|
||||
patch.object(title_generator, "_auto_title_enabled", return_value=True), \
|
||||
patch.object(memory_provider, "spawn_context_thread") as spawn:
|
||||
disable_background_title_inference()
|
||||
result = title_generator.maybe_auto_title(store, "session", "Investigate the runner retry accounting")
|
||||
self.assertIsNone(result)
|
||||
store.set_auto_title.assert_called_once_with("session", unittest.mock.ANY, source="derived")
|
||||
self.assertTrue(store.set_auto_title.call_args.args[1])
|
||||
spawn.assert_not_called()
|
||||
|
||||
def test_usage_is_per_turn_and_cache_and_thought_are_not_double_counted(self):
|
||||
before = dict.fromkeys(USAGE_COUNTERS, 100)
|
||||
after = {key: value + 5 for key, value in before.items()}
|
||||
|
||||
Reference in new issue
Block a user