-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapplier.py
More file actions
3368 lines (3010 loc) · 146 KB
/
Copy pathapplier.py
File metadata and controls
3368 lines (3010 loc) · 146 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""Applier: dispatches mutations to AcliClient and writes per-pass flat-JSON manifest.
TODO(follow-up): this module is 586 lines, exceeding the 500-line module-size
threshold. The intended split is:
- mapping_io.py — _load_mapping, _write_mapping_atomic, _write_mapping_json_atomic,
_persist_field_provenance
- retry.py — _call_with_retry, JiraAPIError, RetryExhaustedError
- dispatchers.py — create_one, update_one, delete_one
leaving applier.py with just the public apply() orchestrator + RescheduleError +
_handle_failed_write_result. The refactor was deferred from PR #290 because the
mechanical move + import-graph fixup is too large for the current PR. Track via
a follow-up bug ticket before the next applier-touching change.
"""
from __future__ import annotations
import contextlib
import importlib.util
import json
import logging
import os
import sys
import tempfile
import time
import urllib.error
from dataclasses import dataclass
from pathlib import Path
from typing import Any, Callable
logger = logging.getLogger(__name__)
# Loop-breaker marker (mirrors inbound_differ.RECONCILER_MARKER).
# Outbound comments embed this token so inbound passes — including the
# bootstrap in _apply_inbound_create — can skip our own echoes.
_RECONCILER_MARKER_APPLIER = "<!-- dso:reconciler-echo -->"
# Typed-mutation dispatch layer.
#
# The applier was originally written as a single batch-style apply(mutations,
# pass_id, ...) routine over dict-shaped mutations. The narrow-applier-matrix
# story introduces a typed Mutation value object (mutation.Mutation with
# MutationDirection / MutationAction enums) and a per-leaf dispatch registry
# (_LEAVES) so callers can route a single Mutation through exactly one
# direction/action handler.
#
# The two surfaces coexist:
# - apply(mutation: Mutation, *, client=None) -> ApplyResult
# Typed single-mutation dispatch via _LEAVES.
# - apply(mutations: list[dict], pass_id, repo_root=None) -> Path
# Legacy batch dispatch (manifest writer + HEAD-drift guard).
#
# Selection is by argument type at the top of apply().
_MutationModule = (
None # late-loaded mutation module; written by _load_mutation_module()
)
_ErrorsModule = None # late-loaded _errors module; written by _load_errors_module()
@dataclass(frozen=True, slots=True)
class ApplyResult:
"""Result of a typed-mutation apply() dispatch.
direction/action mirror the Mutation that was dispatched, so callers can
confirm which leaf executed without re-reading the input. payload carries
any leaf-specific return data (empty dict by default for the stub leaves).
"""
direction: Any
action: Any
payload: dict[str, Any]
_MUTATION_KEY = "plugins.dso.scripts.dso_reconciler.mutation"
def _load_mutation_module():
"""Lazy-load the mutation module under the canonical dotted sys.modules key.
Uses the SAME key (``plugins.dso.scripts.dso_reconciler.mutation``) as
invariants.py and differ.py so ``Mutation`` / ``MutationDirection`` /
``MutationAction`` retain a single class identity across the reconciler.
Previously each caller loaded under its own private key, producing distinct
class objects per module — ``isinstance`` and ``is`` comparisons silently
crossed boundaries and routed mutations to the wrong leaf.
"""
global _MutationModule
if _MutationModule is not None:
return _MutationModule
if _MUTATION_KEY in sys.modules:
_MutationModule = sys.modules[_MUTATION_KEY]
return _MutationModule
mut_path = Path(__file__).parent / "mutation.py"
spec = importlib.util.spec_from_file_location(_MUTATION_KEY, mut_path)
if spec is None:
raise FileNotFoundError(f"mutation.py not found at {mut_path}")
mod = importlib.util.module_from_spec(spec)
sys.modules[_MUTATION_KEY] = mod
spec.loader.exec_module(mod) # type: ignore[union-attr]
_MutationModule = mod
return mod
def _load_errors_module():
"""Lazy-load _errors module."""
global _ErrorsModule
if _ErrorsModule is not None:
return _ErrorsModule
err_path = Path(__file__).parent / "_errors.py"
spec = importlib.util.spec_from_file_location("dso_reconciler_errors", err_path)
if spec is None:
raise FileNotFoundError(f"_errors.py not found at {err_path}")
mod = importlib.util.module_from_spec(spec)
sys.modules.setdefault("dso_reconciler_errors", mod)
spec.loader.exec_module(mod) # type: ignore[union-attr]
_ErrorsModule = mod
return mod
# Re-export error classes so callers can import them from applier.py.
# Internal uses still go through _load_errors_module() to preserve lazy-load
# semantics; these module-level names exist for the public import surface.
_errors_module = _load_errors_module()
StatusMappingError = _errors_module.StatusMappingError
DirectionMismatchError = _errors_module.DirectionMismatchError
UnknownActionError = _errors_module.UnknownActionError
DsoIdLabelWriteError = _errors_module.DsoIdLabelWriteError
# Subject prefixes considered "benign" for HEAD-drift tolerance — i.e.,
# external writers that don't conflict with in-flight outbound mutations.
# Bug f058: parallel Claude sessions running `dso ticket transition` /
# `dso ticket create` / etc. emit `ticket: <VERB>` commits to the tickets
# branch during a reconciler pass. The suggestion subsystem emits
# `suggestion: RECORD`. Other reconciler passes emit `acquire lock` /
# `release lock`. Competing outbound writes emit `pass_record: <pass_id>`
# — the original concern the drift detector was built for — and remain
# non-benign.
_BENIGN_DRIFT_PREFIXES: tuple[str, ...] = (
"ticket:",
"suggestion:",
"acquire lock",
"release lock",
)
def _drift_is_benign(subject: str) -> bool:
"""Return True if a commit subject indicates a benign external writer.
Used by ``_apply_batch``'s drift detector to distinguish ticket-CLI
auto-commits and pass-lock metadata from competing reconciler outbound
writes. Benign drift refreshes ``head_pin`` and continues; non-benign
drift raises HeadDriftError as before.
"""
if not subject:
return False
return any(subject.startswith(p) for p in _BENIGN_DRIFT_PREFIXES)
def _get_commit_subject(repo_root, commit_sha: str) -> str:
"""Return the subject line of *commit_sha* in repo_root, or "" on error.
Failures here are non-fatal — when we can't read the subject, the
caller treats the drift as non-benign (fail-closed) and raises
HeadDriftError as the strict detector originally did.
"""
import subprocess as _sp
if not commit_sha:
return ""
try:
result = _sp.run(
["git", "-C", str(repo_root), "log", "-1", commit_sha, "--format=%s"],
capture_output=True,
text=True,
check=False,
timeout=5,
)
except (OSError, _sp.SubprocessError):
return ""
if result.returncode != 0:
return ""
return result.stdout.strip()
def _direction_guard(mutation, expected_direction) -> None:
"""Defense-in-depth: assert mutation.direction matches the leaf's declared
direction. In normal flow _LEAVES lookup already routes correctly; this
raises DirectionMismatchError if a leaf is invoked directly with the wrong
direction (e.g. via the test harness bypassing _LEAVES).
Compare by string value rather than identity. The reconciler loads
mutation.py multiple times via importlib (once per importing module), and
each load creates a distinct MutationDirection enum class. Two enum
members with the same value but from different class instances are NOT
identity-equal, so ``is not`` would fire spuriously on filtered passes
where a Mutation built under one module load reaches a leaf imported
under another.
"""
expected_val = expected_direction.value
actual_val = getattr(mutation.direction, "value", mutation.direction)
if expected_val != actual_val:
errs = _load_errors_module()
raise errs.DirectionMismatchError(
f"leaf expects direction={expected_val!s}, got direction={actual_val!s}"
)
# ---------------------------------------------------------------------------
# Per-leaf stub handlers.
#
# Each leaf:
# 1. Calls _direction_guard() with its own declared direction (defense-in-depth).
# 2. Performs the leaf-specific side effect (currently stubbed — real ACLI
# wiring lands in a follow-on task).
# 3. Returns an ApplyResult.
# ---------------------------------------------------------------------------
def _apply_outbound_create(mutation, *, client=None, repo_root=None) -> ApplyResult:
mut_mod = _load_mutation_module()
_direction_guard(mutation, mut_mod.MutationDirection.outbound)
if client is None:
# Stub path: preserved for tests that don't exercise the I/O leaf.
return ApplyResult(mutation.direction, mutation.action, {})
payload = dict(mutation.payload)
try:
_call_with_retry(client.create_issue, payload)
except Exception:
# Rollback path: if a Jira issue was (likely) created before the failure
# surfaced, delete it via the same retry helper so transient delete
# failures are also retried. Swallow any rollback error so the ORIGINAL
# create exception is what re-raises to the caller.
key = payload.get("key_hint") or mutation.target
try:
_call_with_retry(client.delete_issue, key)
except Exception: # noqa: BLE001
# Best-effort rollback: swallow delete errors so the original
# create exception propagates to the caller unchanged.
pass
raise
return ApplyResult(mutation.direction, mutation.action, {})
# Allowlist of fields that can be pushed outbound via update_issue. Other
# fields in the changed_fields set are silently dropped — pushing arbitrary
# fields outbound is a higher-blast-radius change that lands in a follow-up
# story. Status is governed separately by DSO_RECONCILER_STATUS_GATING.
# "parent" is intentionally included but routed to client.set_parent
# (REST PUT /rest/api/3/issue/{key} {"fields":{"parent":{"key":K}}}) rather
# than client.update_issue — ACLI edit does not support reparenting
# (ticket 8b25-ae7a-efc3-47f6).
_OUTBOUND_UPDATE_ALLOWLIST = frozenset(
{"summary", "description", "assignee", "priority", "status", "parent"}
)
def _route_status_via_draft5(mutation, *, client=None):
"""Stub for status routing via draft5 protocol.
The final implementation of outbound status push (transition mapping,
workflow-state lookup, etc.) lands in a later epic. v1 just acknowledges
the dispatch so the gating contract is exercised end-to-end.
"""
# Intentionally a no-op stub. Real impl arrives with the status-push story.
return None
def _apply_outbound_update(mutation, *, client=None, repo_root=None) -> ApplyResult:
"""v1 outbound update — push allowlisted fields, labels, and comments.
Behavior:
- Reads ``mutation.payload['changed_fields']`` (falls back to
``mutation.payload`` itself for callers that pass a flat dict).
- Filters the field set to ``_OUTBOUND_UPDATE_ALLOWLIST``; non-allowlisted
fields are silently dropped (no side-effects on those fields).
- Pushes the allowlisted fields via ``client.update_issue``
using the F3-pinned ``update_issue(jira_key, **fields)`` signature,
routed through ``_call_with_retry``.
- Dispatches ``payload['labels']`` (list of {action, label} dicts) via
``client.add_label`` / ``client.remove_label``, matching update_one.
Label failures are logged but non-fatal (scalar update already succeeded).
- Dispatches ``payload['comments']`` (list of {body} dicts) via
``client.add_comment``, matching update_one. Comment failures are logged
but non-fatal.
- Emits a WARNING when the effective work set (allowed fields + labels +
comments) is empty — prevents a silent no-op masquerading as success
when the mutation carries only non-allowlisted fields.
"""
mut_mod = _load_mutation_module()
_direction_guard(mutation, mut_mod.MutationDirection.outbound)
if client is None:
# Stub path: preserved for tests that don't exercise the I/O leaf.
return ApplyResult(mutation.direction, mutation.action, {})
payload = dict(mutation.payload or {})
changed_fields = payload.get("changed_fields")
if changed_fields is None:
changed_fields = payload
# Bug 85a1 (Gap 8): status outbound is now first-class. The previous
# DSO_RECONCILER_STATUS_GATING gate has been removed — status flows
# through ``client.update_issue`` which routes status to
# ``transition_issue`` (REST POST /transitions). The legacy
# ``_route_status_via_draft5`` no-op stub is unused.
# status stays in changed_fields and is forwarded below.
# Filter to allowlist. Non-allowlisted fields are silently dropped.
# "parent" is extracted before forwarding to update_issue — ACLI edit
# does not support reparenting; route via client.set_parent (REST PUT).
allowed = {
k: v for k, v in changed_fields.items() if k in _OUTBOUND_UPDATE_ALLOWLIST
}
# Route parent reparent via client.set_parent (ticket 8b25).
parent_key = allowed.pop("parent", None)
if parent_key is not None:
try:
_call_with_retry(client.set_parent, mutation.target, parent_key)
except urllib.error.HTTPError as exc:
# Hierarchy guard (ticket 8b25): on this next-gen project only an
# Epic may be a parent; a Task→Task reparent (and any other unmet
# hierarchy constraint) is rejected by Jira with HTTP 400 carrying
# a misleading "same project" message. Treat any 400 as a
# hierarchy rejection: WARN + continue the pass. Non-400 errors
# keep the legacy generic-warning behaviour (still non-fatal).
if exc.code == 400:
logger.warning(
"parent sync skipped: Jira hierarchy rejected %s→%s",
mutation.target,
parent_key,
)
else:
logger.warning(
"_apply_outbound_update: set_parent failed for %s parent=%r: %r",
mutation.target,
parent_key,
exc,
)
except Exception as exc: # noqa: BLE001
logger.warning(
"_apply_outbound_update: set_parent failed for %s parent=%r: %r",
mutation.target,
parent_key,
exc,
)
if allowed:
_call_with_retry(client.update_issue, mutation.target, **allowed)
# Dispatch label mutations: add_label / remove_label per entry.
# Mirrors update_one's label-dispatch logic (bug 87e4) for the typed-leaf path.
# Gap fix (bugs 3b5f / 85a1): _apply_outbound_update previously ignored
# payload['labels'] entirely, causing label changes to silently no-op when
# this leaf was invoked directly (single typed-mutation dispatch path).
labels = payload.get("labels") or []
labels_applied: list[str] = []
if isinstance(labels, list):
for entry in labels:
if not isinstance(entry, dict):
continue
action = entry.get("action")
label_name = entry.get("label", "")
if not label_name:
continue
try:
if action == "add":
_call_with_retry(client.add_label, mutation.target, label_name)
labels_applied.append(f"+{label_name}")
elif action == "remove":
_call_with_retry(client.remove_label, mutation.target, label_name)
labels_applied.append(f"-{label_name}")
except Exception as exc: # noqa: BLE001
logger.warning(
"_apply_outbound_update: label %s failed for %s label=%r: %r",
action,
mutation.target,
label_name,
exc,
)
# Dispatch comment mutations: add_comment per entry.
# Mirrors update_one's comment-dispatch logic (bug 87e4) for the typed-leaf path.
# Gap fix (bugs 3b5f / 85a1): payload['comments'] was also silently dropped.
# Note: outbound comment bodies are pre-decorated with RECONCILER_MARKER by
# the outbound differ (_diff_comments → _decorate_outbound_comment) before
# being placed in the mutation payload. The applier emits them as-is — no
# decoration happens here to avoid double-decoration.
comments = payload.get("comments") or []
comments_applied: int = 0
comment_errors: list[str] = []
if isinstance(comments, list):
for entry in comments:
if not isinstance(entry, dict):
continue
body = entry.get("body", "")
if not body:
continue
try:
_call_with_retry(client.add_comment, mutation.target, body)
comments_applied += 1
except Exception as exc: # noqa: BLE001
# Bug 6afc-20ee-84e5-4dd5: non-fatal, but surface in the result
# payload so a swallowed comment failure is observable in the
# outcome instead of vanishing into the log.
comment_errors.append(f"add_comment failed: {exc!s}")
logger.warning(
"_apply_outbound_update: add_comment failed for %s: %r",
mutation.target,
exc,
)
# Loud skip guard: warn when the effective work set is entirely empty so
# callers can distinguish a genuine no-op (no diff) from a misconfigured
# mutation that carried only non-allowlisted fields.
# parent_key is counted as work even when popped from allowed (ticket 8b25).
if (
not allowed
and not labels_applied
and not comments_applied
and parent_key is None
):
logger.warning(
"_apply_outbound_update: no-op for %s — changed_fields %r "
"produced zero allowlisted fields and no labels/comments; "
"verify mutation payload is not empty or mis-keyed",
mutation.target,
list(changed_fields.keys()) if changed_fields else [],
)
result_payload: dict[str, Any] = {
"fields_pushed": sorted(allowed.keys()),
"labels_applied": labels_applied,
"comments_applied": comments_applied,
}
if comment_errors:
result_payload["comment_errors"] = comment_errors
if parent_key is not None:
result_payload["parent_set"] = parent_key
return ApplyResult(mutation.direction, mutation.action, result_payload)
def _apply_outbound_delete(mutation, *, client=None, repo_root=None) -> ApplyResult:
"""Outbound delete: route through the legacy batch path's delete_one()
when a client is supplied. Typed-mutation callers can also drive a direct
delete via this leaf.
"""
mut_mod = _load_mutation_module()
_direction_guard(mutation, mut_mod.MutationDirection.outbound)
if client is None:
# Stub path: preserved for tests that don't exercise the I/O leaf.
return ApplyResult(mutation.direction, mutation.action, {})
try:
_call_with_retry(client.delete_issue, mutation.target)
except JiraAPIError as exc:
if getattr(exc, "status_code", None) == 404:
# Already-gone is the post-state we want — treat as success.
return ApplyResult(
mutation.direction, mutation.action, {"already_gone": True}
)
raise
return ApplyResult(
mutation.direction, mutation.action, {"deleted": mutation.target}
)
def _apply_outbound_probe(mutation, *, client=None, repo_root=None) -> ApplyResult:
"""Outbound probe: read-only sanity check via client.get_issue when supplied.
Returns the probe outcome (key + present flag) in the result payload so
upstream callers can branch on the live Jira state.
"""
mut_mod = _load_mutation_module()
_direction_guard(mutation, mut_mod.MutationDirection.outbound)
if client is None or not hasattr(client, "get_issue"):
return ApplyResult(mutation.direction, mutation.action, {})
try:
info = _call_with_retry(client.get_issue, mutation.target)
return ApplyResult(
mutation.direction,
mutation.action,
{"present": True, "issue": info if isinstance(info, dict) else {}},
)
except JiraAPIError as exc:
if getattr(exc, "status_code", None) in (404, 410, 403):
return ApplyResult(mutation.direction, mutation.action, {"present": False})
raise
def _apply_outbound_conflict(mutation, *, client=None, repo_root=None) -> ApplyResult:
"""Outbound conflict: emit a structured conflict-marker comment on the Jira
issue when a client is supplied. Conflicts are durable signals — the
follow-on is consumed by reconcile_once via the standard suppress_pair
channel so the same pair is not retried mid-pass.
"""
mut_mod = _load_mutation_module()
_direction_guard(mutation, mut_mod.MutationDirection.outbound)
payload = dict(mutation.payload or {})
if client is not None and hasattr(client, "add_comment"):
try:
_call_with_retry(
client.add_comment,
mutation.target,
f"reconciler conflict detected: {payload.get('reason', 'unspecified')}",
)
except Exception:
# Best-effort comment; do not propagate — the suppress_pair
# follow-on still informs reconcile_once to drop further work.
pass
follow_on = {
"kind": "suppress_pair",
"local_id": payload.get("local_id", ""),
"jira_key": mutation.target,
}
return ApplyResult(mutation.direction, mutation.action, {"follow_on": follow_on})
# ---------------------------------------------------------------------------
# Inbound leaf-body helpers (story bd19-d744-b8c7-4079)
#
# Inbound leaves write local ticket-tracker events directly because the local
# CLI is the authoritative reader and we want deterministic file-shape control.
# Event files follow the format documented at
# ${CLAUDE_PLUGIN_ROOT}/docs/ticket-system-v3-architecture.md and mirrored
# throughout the tracker dir as <ticket_id>/<ts>-<uuid>-<EVENT>.json.
# ---------------------------------------------------------------------------
# Map Jira issuetype -> local ticket_type. Anything else falls through to 'task'.
_JIRA_TYPE_MAP: dict[str, str] = {
"Bug": "bug",
"Story": "story",
"Task": "task",
"Epic": "epic",
"Sub-task": "task",
}
_JIRA_PRIORITY_MAP: dict[str, int] = {
"Highest": 0,
"High": 1,
"Medium": 2,
"Low": 3,
"Lowest": 4,
}
_VALID_PRIORITY_RANGE = range(0, 5) # 0-4 inclusive
# Local status vocabulary (source of truth: ticket_reducer/_processors.py:process_status).
# Listed here for documentation / debug purposes only — the typed-mutation
# inbound path no longer uses value-membership to decide whether to invoke
# the Jira→local mapper (see _apply_inbound_update). Kept as a module
# constant so any future check is consistent with the reducer.
_LOCAL_STATUS_VALUES: tuple[str, ...] = (
"open",
"in_progress",
"blocked",
"closed",
"cancelled",
"done",
)
def _resolve_priority(raw_pri: Any) -> int:
"""Convert a Jira priority (name-string or int) to a local 0-4 integer.
Integers outside 0-4 are clamped to the default (2 / Medium).
Unrecognised name strings also fall back to 2.
"""
if isinstance(raw_pri, int):
return raw_pri if raw_pri in _VALID_PRIORITY_RANGE else 2
pri_name = _extract_name(raw_pri)
return _JIRA_PRIORITY_MAP.get(pri_name, 2)
def _jira_key_to_local_id(jira_key: str) -> str:
"""DIG-123 -> jira-dig-123. Idempotent for already-prefixed local ids."""
if jira_key.startswith("jira-"):
return jira_key
return "jira-" + jira_key.lower()
def _jira_status_to_local(jira_status: str) -> str:
"""Reverse-map a Jira status to a local status using config.local_to_jira_status.
Ambiguous reverse mappings (multiple local statuses → same Jira status) are
resolved by lexicographic ordering of the local key, documented in
Implementation Notes of story bd19.
"""
if not jira_status:
return "open"
try:
# Late-load config without polluting module namespace.
config_path = Path(__file__).parent / "config.py"
spec = importlib.util.spec_from_file_location(
"dso_reconciler_config", config_path
)
if spec is None or spec.loader is None:
return "open"
cfg_mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(cfg_mod) # type: ignore[union-attr]
mapping = getattr(cfg_mod, "local_to_jira_status", {}) or {}
except Exception:
return "open"
candidates = sorted(local for local, jira in mapping.items() if jira == jira_status)
return candidates[0] if candidates else "open"
def _event_meta() -> tuple[int, str, str, str]:
"""Return (timestamp_ns, uuid4_str, env_id, author) for a new event."""
import time as _time
import uuid as _uuid
return (
_time.time_ns(),
str(_uuid.uuid4()),
os.environ.get("DSO_ENV_ID", "reconciler"),
os.environ.get("DSO_AUTHOR", "reconciler"),
)
def _resolve_tracker_dir(repo_root: Path | None) -> Path:
"""Resolve the .tickets-tracker directory. Honours TICKETS_TRACKER_DIR env."""
override = os.environ.get("TICKETS_TRACKER_DIR")
if override:
return Path(override)
if repo_root is None:
repo_root = Path(__file__).parents[4]
return Path(repo_root) / ".tickets-tracker" # tickets-boundary-ok
def _read_latest_status(tracker_dir: Path, ticket_id: str) -> str:
"""Return the latest status recorded for ``ticket_id`` (default ``"open"``).
Mirrors the reducer's STATUS-processing semantics (see
ticket_reducer/_processors.py:process_status):
the reducer initialises ``state["status"]`` to ``"open"`` and advances it
only when a STATUS event arrives whose own ``current_status`` field
matches the current state. Reading the latest written ``data["status"]``
here gives the inbound leaf the value that the reducer would have in
state right before our new STATUS event lands, so the new event's
``current_status`` is the PREVIOUS state (not the new one).
Tolerant of missing tickets and unreadable event files — returns
``"open"`` in either case, matching the reducer's initial state.
"""
ticket_dir = tracker_dir / ticket_id
if not ticket_dir.is_dir():
return "open"
latest_status = "open"
for ef in sorted(ticket_dir.glob("*.json")):
try:
event = json.loads(ef.read_text(encoding="utf-8"))
except Exception: # noqa: BLE001
continue
if not isinstance(event, dict):
continue
if event.get("event_type") == "STATUS":
latest_status = event.get("data", {}).get("status", "") or latest_status
return latest_status
def _write_event_file(
tracker_dir: Path, ticket_id: str, event_type: str, data: dict[str, Any]
) -> Path:
"""Write a single ticket event JSON file. Returns the path written."""
ts, uuid_str, env_id, author = _event_meta()
event = {
"timestamp": ts,
"uuid": uuid_str,
"event_type": event_type,
"env_id": env_id,
"author": author,
"data": data,
}
ticket_dir = tracker_dir / ticket_id
ticket_dir.mkdir(parents=True, exist_ok=True)
fname = f"{ts}-{uuid_str}-{event_type}.json"
out = ticket_dir / fname
out.write_text(json.dumps(event, ensure_ascii=False), encoding="utf-8")
return out
def _extract_name(val, default=""):
"""Extract .name or .displayName from a nested Jira field object.
Jira REST API returns many fields as nested objects (e.g.
``{"name": "Bug", "id": "10002"}``). This helper extracts the human-readable
name, falling back to the raw value when it is already a string.
"""
if isinstance(val, dict):
return val.get("name") or val.get("displayName") or default
return val or default
_ADF_KEY_APPLIER = "plugins.dso.scripts.dso_reconciler.adf"
_AdfModule_Applier = None
def _load_adf_module():
"""Lazy-load the sibling adf module (mirrors inbound_differ._load_adf)."""
global _AdfModule_Applier
if _AdfModule_Applier is not None:
return _AdfModule_Applier
if _ADF_KEY_APPLIER in sys.modules:
_AdfModule_Applier = sys.modules[_ADF_KEY_APPLIER]
return _AdfModule_Applier
adf_path = Path(__file__).parent / "adf.py"
spec = importlib.util.spec_from_file_location(_ADF_KEY_APPLIER, adf_path)
if spec is None or spec.loader is None:
raise FileNotFoundError(f"adf.py not found at {adf_path}")
mod = importlib.util.module_from_spec(spec)
sys.modules[_ADF_KEY_APPLIER] = mod
spec.loader.exec_module(mod) # type: ignore[union-attr]
_AdfModule_Applier = mod
return mod
_TICKET_REDUCER_MODULE = None
def _load_ticket_reducer():
"""Lazy-load the ticket_reducer subpackage from ../../ (scripts dir).
Used by ``_apply_inbound_update`` to read the current tag list before
applying an inbound label diff (bug 57b0). Loaded lazily so test
contexts that never hit the labels branch are not forced to import
the reducer package.
"""
global _TICKET_REDUCER_MODULE
if _TICKET_REDUCER_MODULE is not None:
return _TICKET_REDUCER_MODULE
# This file lives at <plugin_scripts_dir>/dso_reconciler/applier.py;
# walk two parents up to reach the scripts dir containing ticket_reducer/.
scripts_dir = Path(__file__).resolve().parent.parent
if str(scripts_dir) not in sys.path:
sys.path.insert(0, str(scripts_dir))
import ticket_reducer as _tr # noqa: PLC0415 — lazy import by design
_TICKET_REDUCER_MODULE = _tr
return _tr
def _normalize_adf_body(body: Any) -> str:
"""Coerce a Jira description (ADF dict or string) to plain text.
Defense-in-depth: the inbound differ should normalize ADF before
surfacing the field, but a raw ADF dict on the wire here would
otherwise be written verbatim into an EDIT event's ``description``
slot — corrupting the local ticket store (reducer would surface a
dict where a string is expected). See bug 1bb2-5da5.
"""
if isinstance(body, dict):
return _load_adf_module().adf_to_text(body)
return str(body) if body is not None else ""
def _apply_inbound_create(
mutation, *, client=None, repo_root=None, binding_store=None
) -> ApplyResult:
"""Materialise a remote Jira issue as a local jira-* ticket.
Writes a CREATE event (title, ticket_type, priority, description, tags
including ``imported:reconciler-bootstrap``) and, when the payload carries
a non-default status, a follow-up STATUS event reverse-mapped via
config.local_to_jira_status.
"""
mut_mod = _load_mutation_module()
_direction_guard(mutation, mut_mod.MutationDirection.inbound)
payload = dict(mutation.payload or {})
# Accept both shapes: payload with nested "fields" key (batch-dict shape)
# and payload with top-level field keys (differ Mutation shape).
fields = payload.get("fields") or payload
jira_key = mutation.target
local_id = _jira_key_to_local_id(jira_key)
# Defense-in-depth inbound dedup (ticket 1577). The snapshot differ normally
# stands down for an already-bound issue by recognising its dso-id:<local_id>
# label in the fetched fields (bug 4354) — that is the primary production
# dedup. This guard covers the narrow transient where the snapshot predates
# the label write-back yet the binding already exists in bindings.json: the
# differ then mis-emits an inbound CREATE which would materialise a duplicate
# local ticket. If the target Jira key is already bound, record the mapping
# and skip materialisation. Cheap (local reverse-index lookup, no Jira GET).
if binding_store is not None:
bound_local_id = binding_store.get_local_id(jira_key)
if bound_local_id:
if repo_root is None:
repo_root = Path(__file__).parents[4]
mapping_path = repo_root / "bridge_state" / "mapping.json"
_write_mapping_atomic(mapping_path, bound_local_id, jira_key)
return ApplyResult(
mutation.direction,
mutation.action,
{
"local_id": bound_local_id,
"jira_key": jira_key,
"dedup_skipped": True,
},
)
issuetype = _extract_name(fields.get("issuetype"), "Task")
ticket_type = _JIRA_TYPE_MAP.get(issuetype, "task")
tracker_dir = _resolve_tracker_dir(repo_root)
tags = list(payload.get("labels", []) or [])
if "imported:reconciler-bootstrap" not in tags:
tags.append("imported:reconciler-bootstrap")
# Parent sync (ticket 8b25): resolve the Jira parent field to a local id.
# Three sources, in priority order:
# 1. payload["_parent_local_id"] — pre-resolved by reconcile.py for the
# normal inbound-create path (reconcile already has binding_store).
# 2. fields["parent"]["key"] derived via _jira_key_to_local_id —
# best-effort local-id derivation for jira-originated parents where the
# local id is deterministic (jira-dig-N convention).
# 3. Empty string — safe fallback (hardcoded prior behaviour).
_raw_parent = fields.get("parent")
if payload.get("_parent_local_id"):
resolved_parent_id = payload["_parent_local_id"]
elif isinstance(_raw_parent, dict) and _raw_parent.get("key"):
resolved_parent_id = _jira_key_to_local_id(_raw_parent["key"])
else:
resolved_parent_id = ""
create_data: dict[str, Any] = {
"id": local_id,
"ticket_type": ticket_type,
"title": fields.get("summary", "") or jira_key,
"description": fields.get("description", "") or "",
"parent_id": resolved_parent_id,
"tags": tags,
}
if "priority" in fields:
create_data["priority"] = _resolve_priority(fields["priority"])
if fields.get("assignee"):
create_data["assignee"] = _extract_name(fields["assignee"])
create_path = _write_event_file(tracker_dir, local_id, "CREATE", create_data)
# Status: write a STATUS event when the Jira status reverse-maps to
# something other than the reducer default ('open').
jira_status = _extract_name(fields.get("status"))
if jira_status:
local_status = _jira_status_to_local(jira_status)
if local_status and local_status != "open":
_write_event_file(
tracker_dir,
local_id,
"STATUS",
{"status": local_status, "current_status": "open"},
)
# Write dso-id label + dso_local_id entity property back to Jira so the
# differ recognizes this issue as mirrored on subsequent passes (dedup).
if client is not None:
_call_with_retry(client.add_label, jira_key, f"dso-id:{local_id}")
_call_with_retry(client.set_entity_property, jira_key, "dso_local_id", local_id)
# Bug 221b: bootstrap pre-existing Jira comments so the local ticket has
# a complete comment history immediately after inbound create.
#
# Strategy (mirrors _diff_comments_inbound in inbound_differ.py):
# 1. Fetch the issue's comments via client.get_comments(jira_key).
# 2. Skip any comment whose body (ADF-normalized) contains the
# loop-breaker marker — those are our own outbound echoes.
# 3. Normalize ADF bodies to plain text (via _normalize_adf_body).
# 4. Write one COMMENT event per remaining comment, recording
# jira_comment_id so the next-pass inbound comment diff dedupes.
#
# On get_comments failure: log a warning and skip comment bootstrap —
# the CREATE still succeeds.
if client is not None:
try:
raw_comments = client.get_comments(jira_key)
if not isinstance(raw_comments, list):
raw_comments = []
except Exception as exc: # noqa: BLE001
import sys as _sys
print( # noqa: T201
f"WARNING: inbound_create: get_comments for {jira_key!r} failed "
f"({exc!r}). Skipping comment bootstrap — ticket created without "
f"pre-existing comments. Alert: jira_key={jira_key!r}",
file=_sys.stderr,
)
raw_comments = []
for jc in raw_comments:
if not isinstance(jc, dict):
continue
jid = jc.get("id")
if jid is None:
continue
body_text = _normalize_adf_body(jc.get("body"))
if _RECONCILER_MARKER_APPLIER in body_text:
continue # outbound echo — skip
if not body_text.strip():
continue
event_data: dict[str, Any] = {
"body": body_text,
"jira_comment_id": str(jid),
}
_write_event_file(tracker_dir, local_id, "COMMENT", event_data)
return ApplyResult(
mutation.direction,
mutation.action,
{"local_id": local_id, "create_event": str(create_path)},
)
def _apply_inbound_update(mutation, *, client=None, repo_root=None) -> ApplyResult:
"""Apply a remote-side update to an existing local jira-* ticket.
Writes one EDIT event with the changed fields, plus an additional STATUS
event when the payload includes a Jira status change. Unknown ticket
directories are tolerated (the EDIT is still written; the reducer will
surface fsck on the next read).
"""
mut_mod = _load_mutation_module()
_direction_guard(mutation, mut_mod.MutationDirection.inbound)
payload = dict(mutation.payload or {})
# Accept both shapes: payload with nested "fields" key (batch-dict shape)
# and payload with top-level field keys (differ Mutation shape).
fields = payload.get("fields") or payload
target = mutation.target
# Bug 1bb2: prefer payload['local_id'] (set by reconcile.py for bound
# tickets) over Jira-key-derived local_id. Without this, EDIT events
# for a bound UUID ticket are written under a new jira-dig-NNN/
# directory — creating a duplicate ticket and silently dropping the
# update on the bound UUID ticket.
local_id = payload.get("local_id") or (
target if target.startswith("jira-") else _jira_key_to_local_id(target)
)
tracker_dir = _resolve_tracker_dir(repo_root)
# Map field names to local reducer field names. The inbound differ
# ALREADY maps Jira → local (see inbound_differ._map_jira_to_local_fields:
# emits ``title`` / ``ticket_type`` / local-mapped ``status``). Accept
# the local-keyed shape from the differ AND the legacy Jira-keyed
# ``summary`` for back-compat with any caller that bypasses the differ.
edit_fields: dict[str, Any] = {}
if "title" in fields:
edit_fields["title"] = fields["title"]
elif "summary" in fields:
edit_fields["title"] = fields["summary"]
if "description" in fields:
desc = fields["description"]
# Bug 1bb2: normalize ADF dict → plain text. The differ should
# normalize at read time, but guard here too in case a caller
# forwards the raw ADF dict (defense-in-depth).
if isinstance(desc, dict):
desc = _normalize_adf_body(desc)
edit_fields["description"] = desc
if "priority" in fields:
edit_fields["priority"] = _resolve_priority(fields["priority"])
if "assignee" in fields:
edit_fields["assignee"] = _extract_name(fields["assignee"])
if "ticket_type" in fields:
edit_fields["ticket_type"] = fields["ticket_type"]
# Parent sync (ticket 8b25): inbound parent_id change → include in EDIT.
# The inbound differ surfaces this as ``fields["parent_id"] = <local_id>``.
if "parent_id" in fields:
edit_fields["parent_id"] = fields["parent_id"] or ""
written: list[str] = []
if edit_fields:
path = _write_event_file(tracker_dir, local_id, "EDIT", {"fields": edit_fields})
written.append(str(path))
if "status" in fields:
raw_status = fields["status"]
# Bug 1bb2 (llm-review finding 2): Trust the differ contract by
# SHAPE rather than VALUE. The inbound differ
# (_diff_jira_vs_local → _map_jira_to_local_fields) always emits
# status as a pre-mapped local string. Legacy callers that bypass
# the differ pass the raw Jira shape (a dict like {"name": "..."}).
# A value-membership check would mis-route a Jira tenant whose
# status is literally named one of the local values (e.g.
# 'in_progress') — but only the dict shape needs reverse-mapping,
# so the type itself is a reliable discriminator.
if isinstance(raw_status, dict):
local_status = _jira_status_to_local(_extract_name(raw_status))
else:
local_status = raw_status
# current_status is the PREVIOUS state (matched against state["status"]
# by the reducer for fork detection — see
# ticket_reducer/_processors.py:process_status).
# Read the latest STATUS event from the ticket dir to obtain it.
previous_status = _read_latest_status(tracker_dir, local_id)
path = _write_event_file(
tracker_dir,
local_id,
"STATUS",
{"status": local_status, "current_status": previous_status},
)
written.append(str(path))
# Bug 57b0: inbound labels — apply payload['labels'] add/remove ops as
# an EDIT event on `fields.tags`. The inbound differ surfaces label
# mutations under ``payload['labels']`` as