Skip to content

refactor(F2): extract WorkItemIdentity and grouping-identity rule for metrics (fixes #185) - #204

Open
Rishabhkanhaiya wants to merge 1 commit into
datacas:mainfrom
Rishabhkanhaiya:refactor/extract-work-item-identity
Open

Rishabhkanhaiya wants to merge 1 commit into
datacas:mainfrom
Rishabhkanhaiya:refactor/extract-work-item-identity

Conversation

@Rishabhkanhaiya

Copy link
Copy Markdown

refactor(F2): extract WorkItemIdentity and grouping-identity rule for metrics (fixes #185)

Following up on Issue #185 now that blocking PR #171 has merged into main.

Scope & Architecture

  • WorkItemIdentity(provider, repository, work_item_id):
    • Extracted as a frozen, hashable @dataclass in scripts/telemetry.py.
    • Normalizes case, trims whitespace, extracts host scope, strips .git and URL prefixes (https://github.com/owner/repo -> owner/repo), and strips leading # from work-item IDs.
    • Distinguishes work-item types (issue, pull_request, task) and host scopes (cloud vs enterprise).
    • Provides canonical .is_unknown sentinel and helper methods (.as_tuple(), .as_scoped_tuple(repo_id)).
  • Statistics Integration:
    • Refactored scripts/stats.py:_work_item_outcomes to use WorkItemIdentity for candidate key generation and event reconciliation.
    • Guarantees that URL representations and varied case formats collapse into identical canonical work items without ad-hoc string handling.
  • Grouping Identity Rule Documented:
    • Documented in docs/instrumentation.md and scripts/stats.py: every metric must declare its grouping identity first (per cycle, work item, stage, attempt, or dispatch).

Acceptance Criteria & Verification

  • Characterization & Specification Tests:
    • Added tests/test_work_item_identity.py pinning canonical creation, normalization rules (URLs, SSH, case, hashes), unknown identity handling, separation by type/host, and tuple mappings.
  • Existing Tests:
    • python -m unittest discover -s tests -p "test_work_item_identity.py": 11 passed, 0 failed
    • python -m unittest discover -s tests -p "test_work_item_outcomes.py": 14 passed, 0 failed
    • python -m unittest discover -s tests -p "test_stats.py": 39 passed, 0 failed
    • python -m unittest discover -s tests -p "test_telemetry.py": 78 passed, 0 failed
    • python scripts/validate-package.py: VALID (15 skills, manifests aligned)
  • Zero Behaviour Change: All existing output formats, disposition counts, and PR reference qualifications remain 100% identical.

@datacas

datacas commented Oct 8, 2026

Copy link
Copy Markdown
Owner

Initial review — PR #204

[CCR-20261008-001] · manual · anthropic/opus→? · high · schema:1

Verdict: CHANGES_REQUESTED — reviewed head 20947f45839e0c150c56aac4c17f4ad2cc411c8d (merge-base 9c43763, 1 commit, 4 files, +211/−3).

Summary

The PR adds WorkItemIdentity to scripts/telemetry.py, uses it as the grouping key in scripts/stats.py:_work_item_outcomes, documents a grouping-identity rule, and adds unit tests for the new class. The direction matches #185, but the change does what #185 explicitly forbids at this phase: it changes statistics output (normalisation is applied on the read path only), and it does not add the characterisation tests that were supposed to prove the extraction is behaviour-neutral. The PR description claims both "zero behaviour change" and that "URL representations and varied case formats collapse into identical canonical work items"; those two claims are mutually exclusive.

Findings

[REV-001] · high · open · - · blocks:yes — Normalising identity only in stats.py changes outcomes and splits read and write identity

Paths: scripts/stats.py:336, scripts/stats.py:354, scripts/telemetry.py:509

#185 acceptance criterion: "No behaviour change". _work_item_outcomes now groups by the normalised tuple (lower-cased GitHub repository, stripped URL prefix, .git and #), while the ledger writer record_work_item_disposition / record_work_item_reconciliation_failure still store and deduplicate by the raw (repo_id, provider, external_repository, work_item_id). The two sides now disagree on what one work item is, which is the identity drift #185 was opened to remove.

Observed (executed, see Verification): one cycle on github / Owner/Repo / 120, a resolved event under Owner/Repo (t1) and a reopened event under owner/repo (t2).

  • base 9c43763: items=1, resolved=1
  • PR head: items=1, reopened=1

The ledger keeps those two as independent chains (their stale/unchanged checks never see each other), but statistics now merges them and picks one by timestamp. The PR-reference counting in the same function (stats.py ~L420–435) still uses the raw item["repository"], so even inside _work_item_outcomes the key is normalised in one place and raw in another.

Expected for this phase: either extract the identity with byte-identical keys (no normalisation yet), or apply one canonicalisation at the write boundary in telemetry.py and in stats.py together, with the behaviour change declared, tested, and scoped as such — not as a "behaviour-neutral" refactor.

[REV-002] · medium · open · - · blocks:yes — Characterisation tests of current statistics output are missing

Paths: tests/test_work_item_identity.py

#185 requires "Characterization tests pin current statistics output before the extraction." The new test file only exercises the new dataclass in isolation; no test calls _work_item_outcomes / cc-stats output with mixed-case, URL-form or #-prefixed identities. That is why REV-001 passes the whole suite unnoticed: the behaviour change above leaves the existing and new tests green. The figures in the PR description (11/14/39/78 passed) cover existing suites plus the class unit tests, not characterisation of statistics output.

[REV-003] · medium · open · - · blocks:yes — Normalisation rules are ad hoc and wrong for several inputs

Paths: scripts/telemetry.py:520-545, scripts/telemetry.py:556-559

#185 asks to define first normalisation, unknown identity, separation by work-item type, and host scope. The implementation guesses instead, and several cases come out wrong (executed):

  • ssh://git@github.com/owner/repo.git → host='ssh', repository='git@github.com/owner/repo' (the "@" in repo and ":" in repo branch splits on the scheme colon).
  • https://jira.corp/projects/ABC (provider jira) → repository='projects/ABC': URL path stripping is applied to every provider, with no rule for what a Plane/Jira "repository" is.
  • Case-folding is keyed on prov in {"github", "gitlab"} or "github" in host; gitlab is not a member of WORK_ITEM_PROVIDERS (github|plane|jira), and any host containing the substring github triggers it.
  • work_item_type and host participate in __eq__/__hash__ but are dropped by as_tuple()/as_scoped_tuple(), which is what stats.py actually groups by, so the advertised "separation by type and host scope" does not hold for any metric. Neither field is ever populated by a caller.
  • is_unknown treats a legitimate work_item_id/repository equal to the literal "unknown" as the unknown sentinel.

The rules should be written down (in docs/instrumentation.md next to the grouping rule) and then implemented and tested against that spec, restricted to the providers the ledger accepts.

[REV-004] · low · open · - · blocks:no — Minor hygiene

Paths: tests/test_work_item_identity.py:16, scripts/stats.py:28-36, scripts/telemetry.py:96-100

  • WORK_ITEM_PROVIDERS is imported in the test and never used.
  • The grouping-identity rule is copied three times (docs, a #: comment block in stats.py attached to nothing, and the class docstring). Keep it in docs/instrumentation.md and reference it.

Security triage

.code-cycle.yml declares no security_review.always_when rule, so the default triggers applied; none matched (no auth, secrets, dependencies, CI, environment or deployment files; no labels). The audit was still run because the reviewer requested it: the PR comes from an external fork (Rishabhkanhaiya/code-cycle-toolkit) by a first-time contributor.

Security audit result (inline, cc-security-review checks applied manually, so this pass ran degraded): no security finding. The full diff (281 lines) was read: no new imports beyond urllib.parse.urlparse, no subprocess, network, file, environment or eval/exec use, no dependency, manifest, installer, skill or workflow change, no hidden or binary content. The single commit is authored by the contributor's GitHub noreply address. The only new parsing operates on local telemetry data and fails closed into wrong grouping (REV-003), not into execution.

Verification executed

All execution happened on isolated git archive exports of base 9c43763 and head 20947f4 in a scratch directory; the maintainer's working tree was not checked out or modified.

  • python3 scripts/validate-package.py — base and head: VALID: 15 skills (7 cycle, 8 supporting)…
  • python3 -m unittest discover -s tests — base: 1175 tests, FAILED (failures=51, errors=7); head: 1186 tests, FAILED (failures=51, errors=7). The set of failing test IDs is identical on base and head (58 each, diff empty): they are environmental, caused by running outside a Git checkout, not by this PR. No new failure.
  • python3 -m unittest tests.test_work_item_identity tests.test_stats on head: OK.
  • Reproduction for REV-001 and REV-003: direct calls to stats._work_item_outcomes and WorkItemIdentity with the inputs listed above; outputs quoted in each finding (runtime evidence, not inspection).

Unverified

  • The full suite was not run inside a Git checkout, so the 58 environmental failures mask those 58 tests on both sides.
  • No characterisation of cc-stats against a real telemetry database was done.

CI

gh pr checks 204: no checks reported. Validate package (.github/workflows/validate.yml) is deliberately skipped for pull requests from forks because it runs on a self-hosted runner (if: … head.repo.full_name == github.repository). The repository gate did not run for this PR; it is not a pass. Do not approve workflow runs from this fork on the self-hosted runner; if CI evidence is needed, cherry-pick onto a maintainer branch after review.

Residual risk

  • Merging as is changes cc-stats work-item outcomes for any ledger holding mixed-case or URL-form identities, silently and without a test.
  • WorkItemIdentity becomes a public helper with semantics (host, type) that callers will assume and grouping ignores.

Verdict

CHANGES_REQUESTED — blocking: REV-001, REV-002, REV-003.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

refactor(F2): extract WorkItemIdentity and the grouping-identity rule for metrics

2 participants