sediment-api 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sediment_api/__init__.py +6 -0
- sediment_api/config.py +230 -0
- sediment_api/database.py +43 -0
- sediment_api/deps.py +191 -0
- sediment_api/main.py +212 -0
- sediment_api/mirror_gc.py +151 -0
- sediment_api/reports/__init__.py +9 -0
- sediment_api/reports/abandonment_report.py +123 -0
- sediment_api/reports/attribution_share_report.py +298 -0
- sediment_api/reports/dataset_diagnostics.py +311 -0
- sediment_api/reports/label_confidence_inspection.py +438 -0
- sediment_api/reports/lifecycle_report.py +73 -0
- sediment_api/reports/merge_retention_report.py +165 -0
- sediment_api/reports/model_report.py +1534 -0
- sediment_api/reports/precision_report.py +182 -0
- sediment_api/reports/recovery_yield_report.py +132 -0
- sediment_api/routers/__init__.py +3 -0
- sediment_api/routers/ci_vendor.py +149 -0
- sediment_api/routers/forge.py +452 -0
- sediment_api/routers/gateway.py +137 -0
- sediment_api/routers/otlp.py +120 -0
- sediment_api/routers/query.py +1010 -0
- sediment_api/routers/reports.py +116 -0
- sediment_api/routers/v1.py +170 -0
- sediment_api/services/__init__.py +2 -0
- sediment_api/services/operational_reports.py +391 -0
- sediment_api/worker.py +242 -0
- sediment_api/workers.py +382 -0
- sediment_api-0.1.0.dist-info/METADATA +20 -0
- sediment_api-0.1.0.dist-info/RECORD +32 -0
- sediment_api-0.1.0.dist-info/WHEEL +4 -0
- sediment_api-0.1.0.dist-info/licenses/LICENSE +661 -0
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
|
+
"""Bounded operational-report HTTP routes."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
from dataclasses import asdict
|
|
7
|
+
from datetime import timedelta
|
|
8
|
+
from typing import Any, Literal
|
|
9
|
+
|
|
10
|
+
from fastapi import APIRouter, Depends, HTTPException, Request, Response
|
|
11
|
+
from pydantic import AwareDatetime, BaseModel
|
|
12
|
+
from sediment_export import OperationalReportScope
|
|
13
|
+
|
|
14
|
+
from ..deps import verify_operator_token
|
|
15
|
+
|
|
16
|
+
router = APIRouter(tags=["reports"])
|
|
17
|
+
|
|
18
|
+
_MAX_COHORT_DAYS = 31
|
|
19
|
+
_MAX_INFERENCE_CALLS = 50_000
|
|
20
|
+
_MAX_REPORT_RESPONSE_BYTES = 64 * 1024 * 1024
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class ReportScopeResponse(BaseModel):
|
|
24
|
+
"""The explicit bounds applied to one operational report."""
|
|
25
|
+
|
|
26
|
+
cohort_start: AwareDatetime
|
|
27
|
+
cohort_end: AwareDatetime
|
|
28
|
+
as_of: AwareDatetime
|
|
29
|
+
max_inference_calls: Literal[50_000]
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class ReportEnvelope(BaseModel):
|
|
33
|
+
"""Version 1 operational-report response."""
|
|
34
|
+
|
|
35
|
+
schema_version: Literal[1] = 1
|
|
36
|
+
scope: ReportScopeResponse
|
|
37
|
+
report: dict[str, Any]
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def _report_response(envelope: ReportEnvelope) -> Response:
|
|
41
|
+
content = envelope.model_dump_json().encode()
|
|
42
|
+
if len(content) > _MAX_REPORT_RESPONSE_BYTES:
|
|
43
|
+
raise HTTPException(
|
|
44
|
+
status_code=409,
|
|
45
|
+
detail="report response exceeds the fixed limit",
|
|
46
|
+
)
|
|
47
|
+
return Response(content=content, media_type="application/json")
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
@router.get(
|
|
51
|
+
"/model-outcomes",
|
|
52
|
+
response_model=ReportEnvelope,
|
|
53
|
+
responses={
|
|
54
|
+
503: {
|
|
55
|
+
"description": "Database unavailable, or work capacity or execution budget exceeded."
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
)
|
|
59
|
+
async def model_outcomes_report(
|
|
60
|
+
cohort_start: AwareDatetime,
|
|
61
|
+
cohort_end: AwareDatetime,
|
|
62
|
+
as_of: AwareDatetime,
|
|
63
|
+
request: Request,
|
|
64
|
+
_: None = Depends(verify_operator_token),
|
|
65
|
+
) -> Response:
|
|
66
|
+
"""Return a bounded model-outcome report."""
|
|
67
|
+
|
|
68
|
+
scope = _build_scope(cohort_start, cohort_end, as_of)
|
|
69
|
+
return await request.app.state.workers.run(
|
|
70
|
+
"model-report", ReportScopeResponse(**asdict(scope)).model_dump(mode="json")
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def _build_scope(
|
|
75
|
+
cohort_start: AwareDatetime,
|
|
76
|
+
cohort_end: AwareDatetime,
|
|
77
|
+
as_of: AwareDatetime,
|
|
78
|
+
) -> OperationalReportScope:
|
|
79
|
+
if cohort_end - cohort_start > timedelta(days=_MAX_COHORT_DAYS):
|
|
80
|
+
raise HTTPException(
|
|
81
|
+
status_code=422,
|
|
82
|
+
detail=f"report cohort must not exceed {_MAX_COHORT_DAYS} days",
|
|
83
|
+
)
|
|
84
|
+
try:
|
|
85
|
+
return OperationalReportScope(
|
|
86
|
+
cohort_start=cohort_start,
|
|
87
|
+
cohort_end=cohort_end,
|
|
88
|
+
as_of=as_of,
|
|
89
|
+
max_inference_calls=_MAX_INFERENCE_CALLS,
|
|
90
|
+
)
|
|
91
|
+
except ValueError as error:
|
|
92
|
+
raise HTTPException(status_code=422, detail=str(error)) from error
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
@router.get(
|
|
96
|
+
"/accepted-work-lifecycle",
|
|
97
|
+
response_model=ReportEnvelope,
|
|
98
|
+
responses={
|
|
99
|
+
503: {
|
|
100
|
+
"description": "Database unavailable, or work capacity or execution budget exceeded."
|
|
101
|
+
}
|
|
102
|
+
},
|
|
103
|
+
)
|
|
104
|
+
async def accepted_work_lifecycle_report(
|
|
105
|
+
cohort_start: AwareDatetime,
|
|
106
|
+
cohort_end: AwareDatetime,
|
|
107
|
+
as_of: AwareDatetime,
|
|
108
|
+
request: Request,
|
|
109
|
+
_: None = Depends(verify_operator_token),
|
|
110
|
+
) -> Response:
|
|
111
|
+
"""Return a bounded accepted-work lifecycle report."""
|
|
112
|
+
|
|
113
|
+
scope = _build_scope(cohort_start, cohort_end, as_of)
|
|
114
|
+
return await request.app.state.workers.run(
|
|
115
|
+
"lifecycle-report", ReportScopeResponse(**asdict(scope)).model_dump(mode="json")
|
|
116
|
+
)
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
|
+
"""
|
|
3
|
+
Read-only v1 endpoints for the remote CLI (ADR 0001).
|
|
4
|
+
|
|
5
|
+
GET /v1/me — auth probe: which tenant does this bearer token serve, and
|
|
6
|
+
what API version (the same string /health reports). The
|
|
7
|
+
CLI uses it to validate ``sediment login`` and to warn on
|
|
8
|
+
version skew between client and server.
|
|
9
|
+
|
|
10
|
+
GET /v1/facts — the counts ``sediment facts`` prints (``cmd_facts``), as
|
|
11
|
+
JSON. Presentation-only: COUNT(*) through ``FactStore``'s
|
|
12
|
+
quarantine-excluding defaults; no derivation output is
|
|
13
|
+
computed or persisted.
|
|
14
|
+
|
|
15
|
+
GET /v1/facts/session/{session_id} — counts for one session, used to verify
|
|
16
|
+
a client's own writes without trusting unrelated org totals.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
from __future__ import annotations
|
|
20
|
+
|
|
21
|
+
from typing import Any
|
|
22
|
+
|
|
23
|
+
from fastapi import APIRouter, Depends, HTTPException
|
|
24
|
+
from sediment_core import FactStore, FactTable, NonEmptyId
|
|
25
|
+
|
|
26
|
+
from .. import __version__
|
|
27
|
+
from ..config import settings
|
|
28
|
+
from ..deps import CredentialIdentity, get_store, verify_operator_token, verify_token
|
|
29
|
+
|
|
30
|
+
router = APIRouter(tags=["v1"])
|
|
31
|
+
|
|
32
|
+
_SESSION_FACT_TABLES = (
|
|
33
|
+
FactTable.INFERENCE_CALLS,
|
|
34
|
+
FactTable.DEVELOPER_DECISIONS,
|
|
35
|
+
FactTable.EDIT_OBSERVATIONS,
|
|
36
|
+
FactTable.REJECTED_EDITS,
|
|
37
|
+
FactTable.RETRY_LINKAGES,
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
@router.get("/me")
|
|
42
|
+
def me(identity: CredentialIdentity = Depends(verify_token)) -> dict[str, str]:
|
|
43
|
+
"""Auth probe for ``sediment login``: the deployment's tenant and the API
|
|
44
|
+
version, reported verbatim so the client can detect skew."""
|
|
45
|
+
return {
|
|
46
|
+
"org_id": settings.org_id,
|
|
47
|
+
"version": __version__,
|
|
48
|
+
"authority": identity.authority,
|
|
49
|
+
"client_id": identity.client_id,
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
@router.get("/facts")
|
|
54
|
+
def facts(
|
|
55
|
+
_: None = Depends(verify_operator_token),
|
|
56
|
+
store: FactStore = Depends(get_store),
|
|
57
|
+
) -> dict[str, Any]:
|
|
58
|
+
"""One entry per ``FactTable``: total rows and the derivation-facing
|
|
59
|
+
("visible" = not quarantined) count. Sessions are upserted metadata,
|
|
60
|
+
not quarantinable facts, so they carry a bare count with no visible
|
|
61
|
+
column — the same shape ``sediment facts`` prints."""
|
|
62
|
+
tables: dict[str, dict[str, int]] = {}
|
|
63
|
+
for table in FactTable:
|
|
64
|
+
tables[table.value] = {
|
|
65
|
+
"total": store.count_facts(
|
|
66
|
+
settings.org_id, table, include_quarantined=True
|
|
67
|
+
),
|
|
68
|
+
"visible": store.count_facts(settings.org_id, table),
|
|
69
|
+
}
|
|
70
|
+
return {
|
|
71
|
+
"sessions": store.count_sessions(settings.org_id),
|
|
72
|
+
"tables": tables,
|
|
73
|
+
# quarantine_revision is the org's provenance high-water mark (MAX
|
|
74
|
+
# quarantine-log rowid, 0 when none) — already an int from the store.
|
|
75
|
+
"quarantine_revision": store.quarantine_revision(settings.org_id),
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
@router.get("/facts/session/{session_id}")
|
|
80
|
+
def session_facts(
|
|
81
|
+
session_id: NonEmptyId,
|
|
82
|
+
_: None = Depends(verify_operator_token),
|
|
83
|
+
store: FactStore = Depends(get_store),
|
|
84
|
+
) -> dict[str, Any]:
|
|
85
|
+
"""Fact counts for one session, so a client can verify its own writes
|
|
86
|
+
without treating unrelated organization-wide totals as evidence."""
|
|
87
|
+
tables = {
|
|
88
|
+
table.value: {
|
|
89
|
+
"total": store.count_session_facts(
|
|
90
|
+
settings.org_id, table, session_id, include_quarantined=True
|
|
91
|
+
),
|
|
92
|
+
"visible": store.count_session_facts(settings.org_id, table, session_id),
|
|
93
|
+
}
|
|
94
|
+
for table in _SESSION_FACT_TABLES
|
|
95
|
+
}
|
|
96
|
+
return {"session_id": session_id, "tables": tables}
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
@router.get("/facts/session/{session_id}/inference-calls")
|
|
100
|
+
def session_inference_calls(
|
|
101
|
+
session_id: NonEmptyId,
|
|
102
|
+
_: None = Depends(verify_operator_token),
|
|
103
|
+
store: FactStore = Depends(get_store),
|
|
104
|
+
) -> dict[str, Any]:
|
|
105
|
+
"""Return the narrow fields needed to reconcile one Session's usage.
|
|
106
|
+
|
|
107
|
+
Prompts, responses, raw provider payloads, and user identity stay absent.
|
|
108
|
+
"""
|
|
109
|
+
try:
|
|
110
|
+
calls = store.read_inference_call_reconciliation(settings.org_id, session_id)
|
|
111
|
+
except ValueError as error:
|
|
112
|
+
raise HTTPException(status_code=409, detail=str(error)) from error
|
|
113
|
+
return {
|
|
114
|
+
"session_id": session_id,
|
|
115
|
+
"inference_calls": [
|
|
116
|
+
{
|
|
117
|
+
"inference_call_id": call.inference_call_id,
|
|
118
|
+
"model_call_id": call.model_call_id,
|
|
119
|
+
"gateway_provider": call.gateway_provider,
|
|
120
|
+
"model_provider": call.model_provider,
|
|
121
|
+
"model": call.model,
|
|
122
|
+
"input_tokens": call.input_tokens,
|
|
123
|
+
"output_tokens": call.output_tokens,
|
|
124
|
+
"duration_ms": call.duration_ms,
|
|
125
|
+
}
|
|
126
|
+
for call in calls
|
|
127
|
+
],
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
@router.get("/facts/session/{session_id}/compatibility-evidence")
|
|
132
|
+
def session_compatibility_evidence(
|
|
133
|
+
session_id: NonEmptyId,
|
|
134
|
+
_: None = Depends(verify_operator_token),
|
|
135
|
+
store: FactStore = Depends(get_store),
|
|
136
|
+
) -> dict[str, Any]:
|
|
137
|
+
"""Return bounded decision, Edit observation, and inference join fields."""
|
|
138
|
+
try:
|
|
139
|
+
calls = store.read_compatibility_inference_evidence(settings.org_id, session_id)
|
|
140
|
+
decisions, observations = store.read_compatibility_evidence(
|
|
141
|
+
settings.org_id, session_id
|
|
142
|
+
)
|
|
143
|
+
except ValueError as error:
|
|
144
|
+
raise HTTPException(status_code=409, detail=str(error)) from error
|
|
145
|
+
return {
|
|
146
|
+
"session_id": session_id,
|
|
147
|
+
"inference_calls": [
|
|
148
|
+
{
|
|
149
|
+
"tool_call_ids": call.tool_call_ids,
|
|
150
|
+
}
|
|
151
|
+
for call in calls
|
|
152
|
+
],
|
|
153
|
+
"developer_decisions": [
|
|
154
|
+
{
|
|
155
|
+
"agent_harness": decision.agent_harness,
|
|
156
|
+
"accepted": decision.accepted,
|
|
157
|
+
"explicit": decision.explicit,
|
|
158
|
+
"interaction_mode": decision.interaction_mode,
|
|
159
|
+
"call_id": decision.call_id,
|
|
160
|
+
}
|
|
161
|
+
for decision in decisions
|
|
162
|
+
],
|
|
163
|
+
"edit_observations": [
|
|
164
|
+
{
|
|
165
|
+
"agent_harness": observation.agent_harness,
|
|
166
|
+
"call_id": observation.call_id,
|
|
167
|
+
}
|
|
168
|
+
for observation in observations
|
|
169
|
+
],
|
|
170
|
+
}
|
|
@@ -0,0 +1,391 @@
|
|
|
1
|
+
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
|
+
"""Bounded, read-only operational report assembly."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
from collections import Counter, defaultdict
|
|
7
|
+
import json
|
|
8
|
+
from dataclasses import asdict, dataclass, replace
|
|
9
|
+
from datetime import timedelta
|
|
10
|
+
from math import ceil
|
|
11
|
+
|
|
12
|
+
from sediment_core import FactStore, OrgId
|
|
13
|
+
from sediment_core.store import CIOutcomeProjection
|
|
14
|
+
from sediment_derive import (
|
|
15
|
+
CommitRef,
|
|
16
|
+
MirrorManager,
|
|
17
|
+
derive_abandonment,
|
|
18
|
+
derive_attribution_result,
|
|
19
|
+
derive_fate_result,
|
|
20
|
+
four_gram_containment,
|
|
21
|
+
bind_session_commit_keys_result,
|
|
22
|
+
read_repository_context,
|
|
23
|
+
)
|
|
24
|
+
from sediment_derive.repository_identity import (
|
|
25
|
+
CommitKey,
|
|
26
|
+
IdentifiedRepositoryKey,
|
|
27
|
+
RepositoryContext,
|
|
28
|
+
repository_read_key,
|
|
29
|
+
)
|
|
30
|
+
from sediment_derive.attribution import tokenize
|
|
31
|
+
from sediment_derive.inference_call import render_scoring_text
|
|
32
|
+
from sediment_export import (
|
|
33
|
+
AcceptedWorkLifecycleReport,
|
|
34
|
+
AttributedCompletion,
|
|
35
|
+
AttributedCompletionPolicy,
|
|
36
|
+
ModelOutcomeReportResult,
|
|
37
|
+
OperationalReportScope,
|
|
38
|
+
OutcomeReportPolicy,
|
|
39
|
+
assemble_attributed_completions_result,
|
|
40
|
+
build_abandonment_summary,
|
|
41
|
+
build_model_report_result,
|
|
42
|
+
derive_model_report_attribution_share,
|
|
43
|
+
generate_accepted_work_lifecycle_report,
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
from sediment_export.outcome_report import with_model_assembly_diagnostics
|
|
47
|
+
|
|
48
|
+
from ..reports.attribution_share_report import (
|
|
49
|
+
attribution_share_alert_payload,
|
|
50
|
+
attribution_share_row_payload,
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
_SUPPORTING_FACT_LIMIT = 50_000
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
@dataclass(frozen=True)
|
|
57
|
+
class LifecycleReportRequest:
|
|
58
|
+
"""Inputs for one accepted-work lifecycle report."""
|
|
59
|
+
|
|
60
|
+
org_id: OrgId
|
|
61
|
+
scope: OperationalReportScope | None = None
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
@dataclass(frozen=True)
|
|
65
|
+
class LifecycleReportEnvelope:
|
|
66
|
+
"""The request scope and its canonical lifecycle artifact."""
|
|
67
|
+
|
|
68
|
+
scope: OperationalReportScope | None
|
|
69
|
+
report: AcceptedWorkLifecycleReport
|
|
70
|
+
|
|
71
|
+
def to_json(self) -> str:
|
|
72
|
+
"""Serialize the canonical report payload without delivery-side state."""
|
|
73
|
+
|
|
74
|
+
return json.dumps(asdict(self.report), sort_keys=True)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def generate_lifecycle_report(
|
|
78
|
+
store: FactStore,
|
|
79
|
+
mirrors: MirrorManager,
|
|
80
|
+
request: LifecycleReportRequest,
|
|
81
|
+
) -> LifecycleReportEnvelope:
|
|
82
|
+
"""Generate one lifecycle report without persistence or presentation."""
|
|
83
|
+
|
|
84
|
+
report = generate_accepted_work_lifecycle_report(
|
|
85
|
+
store,
|
|
86
|
+
mirrors,
|
|
87
|
+
request.org_id,
|
|
88
|
+
scope=request.scope,
|
|
89
|
+
)
|
|
90
|
+
return LifecycleReportEnvelope(scope=request.scope, report=report)
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
@dataclass(frozen=True)
|
|
94
|
+
class OperationalModelReport:
|
|
95
|
+
"""Canonical report plus scoped inputs needed by CLI-only comparisons."""
|
|
96
|
+
|
|
97
|
+
result: ModelOutcomeReportResult
|
|
98
|
+
completions: list
|
|
99
|
+
attributed_completions: list[AttributedCompletion]
|
|
100
|
+
repository_context: RepositoryContext | None = None
|
|
101
|
+
ci_population: tuple[CIOutcomeProjection, ...] = ()
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def base_model_report_payload(result: ModelOutcomeReportResult) -> dict[str, object]:
|
|
105
|
+
return {
|
|
106
|
+
"rows": [asdict(row) for row in result.rows],
|
|
107
|
+
"stratification": [asdict(check) for check in result.stratification],
|
|
108
|
+
"signal_funnel": [asdict(row) for row in result.signal_funnel],
|
|
109
|
+
"abandonment": asdict(result.abandonment),
|
|
110
|
+
"fate_skipped": result.fate_skipped,
|
|
111
|
+
"repository_skipped": result.repository_skipped,
|
|
112
|
+
"ci_skipped": result.ci_skipped,
|
|
113
|
+
"fate_provenance": (
|
|
114
|
+
asdict(result.fate_provenance)
|
|
115
|
+
if result.fate_provenance is not None
|
|
116
|
+
else None
|
|
117
|
+
),
|
|
118
|
+
"attribution_share": [
|
|
119
|
+
attribution_share_row_payload(row) for row in result.attribution_share
|
|
120
|
+
],
|
|
121
|
+
"attribution_alerts": [
|
|
122
|
+
attribution_share_alert_payload(alert)
|
|
123
|
+
for alert in result.attribution_alerts
|
|
124
|
+
],
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def generate_operational_model_report(
|
|
129
|
+
store: FactStore,
|
|
130
|
+
mirrors: MirrorManager,
|
|
131
|
+
org_id: str,
|
|
132
|
+
scope: OperationalReportScope,
|
|
133
|
+
*,
|
|
134
|
+
assembly_policy: AttributedCompletionPolicy | None = None,
|
|
135
|
+
report_policy: OutcomeReportPolicy | None = None,
|
|
136
|
+
include_trends: bool = False,
|
|
137
|
+
) -> OperationalModelReport:
|
|
138
|
+
"""Build a scoped model report from one Fact and mirror snapshot."""
|
|
139
|
+
|
|
140
|
+
assembly_policy = assembly_policy or AttributedCompletionPolicy()
|
|
141
|
+
with store.read_snapshot() as snapshot:
|
|
142
|
+
repository_context = read_repository_context(
|
|
143
|
+
snapshot, org_id, as_of=scope.as_of
|
|
144
|
+
)
|
|
145
|
+
ci_population = snapshot.read_ci_outcome_projections(
|
|
146
|
+
org_id, captured_through=scope.as_of, limit=_SUPPORTING_FACT_LIMIT
|
|
147
|
+
)
|
|
148
|
+
summaries = snapshot.read_inference_call_summaries(
|
|
149
|
+
org_id,
|
|
150
|
+
observed_between=(scope.cohort_start, scope.cohort_end),
|
|
151
|
+
limit=scope.max_inference_calls,
|
|
152
|
+
)
|
|
153
|
+
call_ids = {item.inference_call_id for item in summaries}
|
|
154
|
+
completions = snapshot.read_report_inference_calls(
|
|
155
|
+
org_id, inference_call_ids=call_ids
|
|
156
|
+
)
|
|
157
|
+
session_ids = {item.session_id for item in summaries}
|
|
158
|
+
decisions = snapshot.read_decisions(
|
|
159
|
+
org_id,
|
|
160
|
+
captured_through=scope.as_of,
|
|
161
|
+
session_ids=session_ids,
|
|
162
|
+
limit=_SUPPORTING_FACT_LIMIT,
|
|
163
|
+
)
|
|
164
|
+
decision_identities = (
|
|
165
|
+
snapshot.read_inference_call_identities(
|
|
166
|
+
org_id, observed_through=scope.as_of, limit=_SUPPORTING_FACT_LIMIT
|
|
167
|
+
)
|
|
168
|
+
if any(decision.call_id is not None for decision in decisions)
|
|
169
|
+
else []
|
|
170
|
+
)
|
|
171
|
+
observations = snapshot.read_edit_observations(
|
|
172
|
+
org_id,
|
|
173
|
+
captured_through=scope.as_of,
|
|
174
|
+
session_ids=session_ids,
|
|
175
|
+
limit=_SUPPORTING_FACT_LIMIT,
|
|
176
|
+
)
|
|
177
|
+
share_pushes = snapshot.read_pushes(
|
|
178
|
+
org_id,
|
|
179
|
+
captured_between=(
|
|
180
|
+
scope.cohort_start - timedelta(days=30),
|
|
181
|
+
scope.as_of + timedelta(microseconds=1),
|
|
182
|
+
),
|
|
183
|
+
limit=_SUPPORTING_FACT_LIMIT,
|
|
184
|
+
)
|
|
185
|
+
pushes = [
|
|
186
|
+
push
|
|
187
|
+
for push in share_pushes
|
|
188
|
+
if push.captured_at
|
|
189
|
+
>= scope.cohort_start
|
|
190
|
+
- timedelta(
|
|
191
|
+
minutes=assembly_policy.attribution.post_push_grace_period_minutes
|
|
192
|
+
)
|
|
193
|
+
]
|
|
194
|
+
# Observation Facts are metadata. Read their bounded population once so
|
|
195
|
+
# legacy observations can retain an exact identified source-Push anchor.
|
|
196
|
+
all_commit_observations = snapshot.read_session_commit_observations(
|
|
197
|
+
org_id,
|
|
198
|
+
as_of=scope.as_of,
|
|
199
|
+
limit=_SUPPORTING_FACT_LIMIT,
|
|
200
|
+
)
|
|
201
|
+
commit_observations = [
|
|
202
|
+
item for item in all_commit_observations if item.session_id in session_ids
|
|
203
|
+
]
|
|
204
|
+
bindings = bind_session_commit_keys_result(
|
|
205
|
+
commit_observations,
|
|
206
|
+
org_id,
|
|
207
|
+
as_of=scope.as_of,
|
|
208
|
+
repository_context=repository_context,
|
|
209
|
+
)
|
|
210
|
+
note_sessions = defaultdict(set)
|
|
211
|
+
session_commits = defaultdict(set)
|
|
212
|
+
for commit, session_id in bindings.bindings:
|
|
213
|
+
note_sessions[commit].add(session_id)
|
|
214
|
+
session_commits[session_id].add(
|
|
215
|
+
CommitRef(
|
|
216
|
+
repo=repository_context.repo_for(commit.repository),
|
|
217
|
+
commit_sha=commit.commit_sha,
|
|
218
|
+
repository_identity=commit.repository.identity
|
|
219
|
+
if isinstance(commit.repository, IdentifiedRepositoryKey)
|
|
220
|
+
else None,
|
|
221
|
+
)
|
|
222
|
+
)
|
|
223
|
+
note_session_ids_by_commit = {
|
|
224
|
+
key: frozenset(value) for key, value in note_sessions.items()
|
|
225
|
+
}
|
|
226
|
+
candidates = [
|
|
227
|
+
(call, tokenize(render_scoring_text(call))) for call in completions
|
|
228
|
+
]
|
|
229
|
+
resolved_pushes = [
|
|
230
|
+
(push, repository_context.resolve_fact(push)) for push in share_pushes
|
|
231
|
+
]
|
|
232
|
+
push_skips = Counter(
|
|
233
|
+
resolved.reason for _, resolved in resolved_pushes if resolved.key is None
|
|
234
|
+
)
|
|
235
|
+
keys = {
|
|
236
|
+
resolved.key for _, resolved in resolved_pushes if resolved.key is not None
|
|
237
|
+
}
|
|
238
|
+
with mirrors.read_repository_snapshot(keys):
|
|
239
|
+
share_commits = set()
|
|
240
|
+
for push, resolved in resolved_pushes:
|
|
241
|
+
if resolved.key is None:
|
|
242
|
+
continue
|
|
243
|
+
mirror = mirrors.open_repository(resolved.key)
|
|
244
|
+
if mirror is not None:
|
|
245
|
+
share_commits.update(
|
|
246
|
+
CommitKey(resolved.key, sha)
|
|
247
|
+
for sha in mirror.list_push_commits(
|
|
248
|
+
push, assembly_policy.attribution.max_commits_per_push
|
|
249
|
+
)
|
|
250
|
+
)
|
|
251
|
+
share_bindings = bind_session_commit_keys_result(
|
|
252
|
+
all_commit_observations,
|
|
253
|
+
org_id,
|
|
254
|
+
as_of=scope.as_of,
|
|
255
|
+
repository_context=repository_context,
|
|
256
|
+
)
|
|
257
|
+
share_notes = defaultdict(set)
|
|
258
|
+
for commit, session_id in share_bindings.bindings:
|
|
259
|
+
if commit in share_commits:
|
|
260
|
+
share_notes[commit].add(session_id)
|
|
261
|
+
share_note_session_ids_by_commit = {
|
|
262
|
+
key: frozenset(value) for key, value in share_notes.items()
|
|
263
|
+
}
|
|
264
|
+
attribution = derive_attribution_result(
|
|
265
|
+
snapshot,
|
|
266
|
+
mirrors,
|
|
267
|
+
org_id,
|
|
268
|
+
assembly_policy.attribution,
|
|
269
|
+
pushes=pushes,
|
|
270
|
+
candidates=candidates,
|
|
271
|
+
note_session_ids_by_commit=note_session_ids_by_commit,
|
|
272
|
+
repository_context=repository_context,
|
|
273
|
+
as_of=scope.as_of,
|
|
274
|
+
)
|
|
275
|
+
commits = {
|
|
276
|
+
repository_context.commit_key(
|
|
277
|
+
item.org_id,
|
|
278
|
+
item.repo,
|
|
279
|
+
item.commit_sha,
|
|
280
|
+
repository_identity=item.repository_identity,
|
|
281
|
+
)
|
|
282
|
+
for item in attribution.attributions
|
|
283
|
+
}
|
|
284
|
+
ci_outcomes = snapshot.read_ci_outcomes(
|
|
285
|
+
org_id,
|
|
286
|
+
captured_through=scope.as_of,
|
|
287
|
+
repository_commits={
|
|
288
|
+
(repository_read_key(key.repository), key.commit_sha)
|
|
289
|
+
for key in commits
|
|
290
|
+
if key is not None
|
|
291
|
+
},
|
|
292
|
+
limit=_SUPPORTING_FACT_LIMIT,
|
|
293
|
+
)
|
|
294
|
+
abandonment = derive_abandonment(
|
|
295
|
+
snapshot,
|
|
296
|
+
mirrors,
|
|
297
|
+
org_id,
|
|
298
|
+
assembly_policy.abandonment,
|
|
299
|
+
attributions=attribution.attributions,
|
|
300
|
+
decisions=decisions,
|
|
301
|
+
pushes=pushes,
|
|
302
|
+
completions=completions,
|
|
303
|
+
as_of=scope.as_of,
|
|
304
|
+
session_commits=dict(session_commits),
|
|
305
|
+
session_commit_observations=commit_observations,
|
|
306
|
+
repository_context=repository_context,
|
|
307
|
+
)
|
|
308
|
+
assembly = assemble_attributed_completions_result(
|
|
309
|
+
snapshot,
|
|
310
|
+
mirrors,
|
|
311
|
+
org_id,
|
|
312
|
+
assembly_policy,
|
|
313
|
+
attributions=attribution.attributions,
|
|
314
|
+
completions=completions,
|
|
315
|
+
decisions=decisions,
|
|
316
|
+
decision_identities=decision_identities,
|
|
317
|
+
edit_observations=observations,
|
|
318
|
+
ci_outcomes=ci_outcomes,
|
|
319
|
+
ci_population=ci_population,
|
|
320
|
+
abandonment=abandonment,
|
|
321
|
+
session_commit_observations=commit_observations,
|
|
322
|
+
as_of=scope.as_of,
|
|
323
|
+
repository_context=repository_context,
|
|
324
|
+
)
|
|
325
|
+
since_days = max(
|
|
326
|
+
1,
|
|
327
|
+
ceil((scope.cohort_end - scope.cohort_start).total_seconds() / 86_400),
|
|
328
|
+
)
|
|
329
|
+
attribution_share, attribution_alerts = (
|
|
330
|
+
derive_model_report_attribution_share(
|
|
331
|
+
snapshot,
|
|
332
|
+
mirrors,
|
|
333
|
+
org_id,
|
|
334
|
+
since_days,
|
|
335
|
+
now=scope.as_of,
|
|
336
|
+
pushes=share_pushes,
|
|
337
|
+
candidate_limit=_SUPPORTING_FACT_LIMIT,
|
|
338
|
+
note_session_ids_by_commit=share_note_session_ids_by_commit,
|
|
339
|
+
repository_context=repository_context,
|
|
340
|
+
)
|
|
341
|
+
)
|
|
342
|
+
quarantine_revision = snapshot.quarantine_revision(org_id)
|
|
343
|
+
fate = derive_fate_result(
|
|
344
|
+
observations,
|
|
345
|
+
four_gram_containment,
|
|
346
|
+
quarantine_revision=quarantine_revision,
|
|
347
|
+
)
|
|
348
|
+
result = build_model_report_result(
|
|
349
|
+
completions,
|
|
350
|
+
assembly.rows,
|
|
351
|
+
ci_population=ci_population,
|
|
352
|
+
scope=scope,
|
|
353
|
+
repository_context=repository_context,
|
|
354
|
+
decisions=decisions,
|
|
355
|
+
decision_identities=decision_identities,
|
|
356
|
+
policy=report_policy,
|
|
357
|
+
include_trends=include_trends,
|
|
358
|
+
attribution_share=attribution_share,
|
|
359
|
+
attribution_alerts=attribution_alerts,
|
|
360
|
+
abandonment=build_abandonment_summary(assembly),
|
|
361
|
+
abandonment_is_scoped=True,
|
|
362
|
+
quarantine_revision=quarantine_revision,
|
|
363
|
+
fate_result=fate,
|
|
364
|
+
)
|
|
365
|
+
result = with_model_assembly_diagnostics(result, assembly)
|
|
366
|
+
skipped = dict(result.repository_skipped)
|
|
367
|
+
if push_skips:
|
|
368
|
+
skipped["push_sources"] = dict(sorted(push_skips.items()))
|
|
369
|
+
if bindings.skipped:
|
|
370
|
+
skipped["preload_session_observations"] = dict(bindings.skipped)
|
|
371
|
+
result = replace(result, repository_skipped=skipped)
|
|
372
|
+
|
|
373
|
+
return OperationalModelReport(
|
|
374
|
+
result, summaries, assembly.rows, repository_context, tuple(ci_population)
|
|
375
|
+
)
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
def model_report_payload(
|
|
379
|
+
store: FactStore,
|
|
380
|
+
mirrors: MirrorManager,
|
|
381
|
+
org_id: str,
|
|
382
|
+
scope: OperationalReportScope,
|
|
383
|
+
**kwargs,
|
|
384
|
+
) -> dict[str, object]:
|
|
385
|
+
"""Return the canonical base JSON payload for one scoped model report."""
|
|
386
|
+
|
|
387
|
+
return base_model_report_payload(
|
|
388
|
+
generate_operational_model_report(
|
|
389
|
+
store, mirrors, org_id, scope, **kwargs
|
|
390
|
+
).result
|
|
391
|
+
)
|