conflict

package
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 26, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

README

haistack-conflict (pkg/conflict)

FHIR-aware conflict policy and merge library for HAIStack.

What it does

pkg/conflict decides whether a sync conflict can be merged automatically and, when safe, produces a merged types.ResourceEnvelope plus a FHIR Patch (JSON Patch) rebase artifact. It keeps the policy layer separate from the sync transport layer: haistack-sync detects stale-base failures and enqueues sync.conflict_processing jobs; haistack-conflict evaluates those conflicts and builds merge or review artifacts.

Main components:

Component Role
Engine High-level entrypoint: Detect, CanAutoMerge, Merge
NewDefaultEngine / NewEngine Construct engine with built-in or custom policy
Config Policy registry, clock, and extension points
Result Conflict classification, risk level, changed paths, overlap info, auto-merge flag
MergeResult Merged resource, FHIR Patch bytes, resolution metadata, or review metadata
PolicyRegistry / RulePack / Rule Resource/path-specific merge rules
ReviewMetadata Human-review reason, path summaries, overlap paths, UI-safe labels
diff layer FHIR-aware JSON diff that emits normalized dotted element paths (PathChange)
patch builder JSON Patch operations (add, replace) that rebase local changes onto the current canonical resource

LocalEvent mirrors sync.LocalEvent so the engine can be used without importing pkg/sync.

It does not:

  • Detect stale-base push failures (pkg/sync does that)
  • Persist conflicts or write audit logs (pkg/store and pkg/sync do that)
  • Apply merged resources locally or resubmit to the hub automatically (the runtime uses ConflictResolutionHandler in pkg/sync)
  • Resolve terminology, profiles, or business rules beyond the configured merge policy

How it fits in the ecosystem

  stale-base push (pkg/sync.PostgresHub)
           |
           v
  ConflictRecord persisted (store.ConflictStore)
           |
           v
  JobTypeConflictProcessing (pkg/jobs)
           |
           v
  pkg/conflict.Engine.Detect / Merge
           |
     +-----+-----+
     v           v
 auto-merge     ReviewMetadata
 MergeResult    (UI / manual workflow)
     |
     v
 ConflictResolutionHandler (pkg/sync) -> replay / resubmit / surface review
Direction Package Relationship
Upstream sync Supplies base/current/local envelopes via job payload; default ConflictEngine on sync.Config
Upstream types ResourceEnvelope JSON for base, current, local, and merged states
Downstream sync ConflictResolutionHandler consumes MergeResult
Sidecar store Conflict persistence only (not owned by this package)

When to use it

  • From a sync.conflict_processing job handler to evaluate a persisted conflict
  • From a runtime or UI that needs to surface changed paths, overlap, and a recommended review reason
  • From a manual-resolution flow that wants to preview a merged resource and patch before applying it
  • When registering tenant-specific safe-list merge rules via PolicyRegistry

Usage modes

1. Default engine: detect and merge
import (
    "github.com/degoke/haistack/pkg/conflict"
)

engine := conflict.NewDefaultEngine()

local := conflict.LocalEvent{
    ResourceType:     "Patient",
    ResourceID:       "p1",
    Operation:        "resource.updated",
    BaseCloudVersion: "base-v1",
    LocalVersion:     "local-v2",
    ResourceAfter:    localEnvelope,
}

result := engine.Detect(local, baseEnvelope, currentEnvelope)

if result.AutoMergeable {
    mergeResult := engine.Merge(local, baseEnvelope, currentEnvelope)
    // mergeResult.Merged.JSON, mergeResult.Patch
} else {
    // result.ReviewReason, result.OverlappingPaths
    // mergeResult.Review.UILabels
}
2. Classify without merging (CanAutoMerge)
result := engine.Detect(local, baseEnvelope, currentEnvelope)
if engine.CanAutoMerge(result) {
    mergeResult := engine.Merge(local, baseEnvelope, currentEnvelope)
}

CanAutoMerge inspects a prior Detect result; Merge re-runs detection internally when building artifacts.

3. Custom policy registry and rule packs
registry := conflict.DefaultPolicyRegistry()
registry.Register(conflict.RulePack{
    Name: "tenant-a",
    Rules: []conflict.Rule{
        {
            ResourceType: "Patient",
            PathPrefix:   "Patient.contact",
            Semantics:    conflict.RuleSemanticsAppendOnly,
            Description:  "Allow contact append merges",
        },
    },
})
registry.Select("tenant-a")

engine := conflict.NewEngine(conflict.Config{Registry: registry})

Rule semantics include RuleSemanticsAutoMerge, RuleSemanticsAppendOnly, and RuleSemanticsReview.

4. Integration via pkg/sync engine config
syncEngine := hasync.NewEngine(hasync.Config{
    NodeID:   "node-a",
    TenantID: "tenant-a",
    // ConflictEngine defaults to conflict.NewDefaultEngine() if nil
    ConflictEngine:            customEngine,
    ConflictResolutionHandler: myHandler,
})

The handler receives the conflict.MergeResult for both auto-merge and review outcomes:

type MyHandler struct{}

func (h *MyHandler) OnConflictResolution(
    ctx context.Context,
    payload hasync.ConflictJobPayload,
    result conflict.MergeResult,
) error {
    if result.AutoMergeable {
        // replay or resubmit result.Merged
    } else {
        // surface result.Review for UI
    }
    return nil
}
5. Inspect path-level diffs for tooling

Detect populates Result.LocalChanges and Result.RemoteChanges as []PathChange with ChangeKind (add, remove, replace). Use these for custom UIs without calling Merge.

6. Blocked or unsupported shapes

Operations resource.created and resource.deleted, missing envelopes, or identity mismatches return ClassificationUnsupportedMerge with RiskLevelBlocked — callers should route to manual workflows or abort replay.

Examples

Non-overlapping concurrent edits (typically review in v1):

// Local changed Patient.telecom; remote changed Patient.address only.
result := engine.Detect(local, base, current)
// result.Classification may be same_resource_non_overlapping_update
// result.AutoMergeable depends on policy safe-list

Append-only array overlap:

// Both sides appended to Appointment.note
result := engine.Detect(local, base, current)
// ClassificationAppendOnlyCompatible when policy allows

Clinical hot-spot (forced review):

// Remote and local both touch Observation.value
result := engine.Detect(local, base, current)
// RiskLevelReview, ClassificationClinicallySensitive or overlapping update

Classification buckets

Classification Meaning
no_conflict Local and remote changes are both empty (semantic)
stale_base_only Only one side changed relative to the stale base
same_resource_non_overlapping_update Both sides changed, but on distinct element paths
same_resource_overlapping_update Both sides changed the same path or a nested path
append_only_compatible Overlap is on an append-only array and both sides are pure appends
clinically_sensitive_conflict Risk tier review due to a clinical hot-spot path
unsupported_merge_shape Delete/create conflicts, missing payloads, or shapes that cannot be expressed as FHIR Patch

Risk levels

RiskLevel Typical use
safe Auto-merge candidate under policy
review Human review recommended
blocked Cannot produce a patch merge

Default v1 policy (strict safe-list)

Auto-merge is allowed only for explicit safe-list paths:

Resource Path Semantics
Patient Patient.telecom auto_merge
Patient Patient.address auto_merge
Appointment Appointment.note append_only
Encounter Encounter.statusHistory append_only

Everything else defaults to human review, including these clinical hot-spots:

Resource Path
MedicationRequest MedicationRequest.dosageInstruction
AllergyIntolerance AllergyIntolerance.clinicalStatus
Observation Observation.value
Consent Consent.provision
Appointment Appointment.start
Patient Patient.birthDate

Configuration / key types

Type Fields / notes
Engine Detect, CanAutoMerge, Merge
Config Registry *PolicyRegistry, Clock func() time.Time
LocalEvent Mirrors sync payload: versions, operation, ResourceAfter, optional Patch
MergeResult Merged, Patch []byte, Review, Resolution, AutoMergeable
Rule ResourceType, PathPrefix, Semantics, Description

Where it fits

Layer Role
sync Detects stale-base failures, persists ConflictRecord, enqueues jobs, writes audit side effects
conflict Semantic diffing, changed-path derivation, mergeability decisions, merge/rebase artifact generation
store Conflict and audit persistence
types Canonical JSON envelopes used for base, current, and merged states
jobs JobTypeConflictProcessing delivery

Limits

  • v1 is conservative: explicit safe-list only for auto-merge
  • FHIR Patch is the only resolution artifact; standalone RFC 6902 JSON Patch export is not a separate API
  • Delete and create conflicts are classified as unsupported merge shapes
  • Tenant-specific custom policies can be registered; per-tenant selection API is minimal (PolicyRegistry.Select)
  • Rich conflict UI persistence and full resolution audit workflows are deferred; the package produces metadata and pkg/sync writes audit actions
  • LocalEvent.ChangedPaths and Patch from sync are not required for Detect — diff is recomputed from envelopes

Documentation

Overview

Package conflict is the FHIR-aware conflict policy library for HAIStack. It detects conflicts, classifies clinical risk, and produces FHIR Patch-based merge or rebase artifacts while keeping sync transport concerns outside.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ChangeKind

type ChangeKind string

ChangeKind describes the nature of a change at a FHIR element path.

const (
	ChangeKindScalarReplace ChangeKind = "scalar_replace"
	ChangeKindArrayAppend   ChangeKind = "array_append"
	ChangeKindArrayRemove   ChangeKind = "array_remove"
	ChangeKindStructural    ChangeKind = "structural"
	ChangeKindUnsupported   ChangeKind = "unsupported"
)

type Classification

type Classification string

Classification is the conflict bucket assigned to a detect result.

const (
	ClassificationNoConflict             Classification = "no_conflict"
	ClassificationStaleBaseOnly          Classification = "stale_base_only"
	ClassificationSameResourceNonOverlap Classification = "same_resource_non_overlapping_update"
	ClassificationSameResourceOverlap    Classification = "same_resource_overlapping_update"
	ClassificationAppendOnlyCompatible   Classification = "append_only_compatible"
	ClassificationClinicallySensitive    Classification = "clinically_sensitive_conflict"
	ClassificationUnsupportedMerge       Classification = "unsupported_merge_shape"
)

type Config

type Config struct {
	Registry *PolicyRegistry
	Clock    func() time.Time
}

Config wires optional policy and clock into the engine.

type Engine

type Engine struct {
	// contains filtered or unexported fields
}

Engine is the high-level conflict detection and merge entrypoint.

func NewDefaultEngine

func NewDefaultEngine() *Engine

NewDefaultEngine builds an engine with the built-in default policy.

func NewEngine

func NewEngine(cfg Config) *Engine

NewEngine builds an engine from config, filling in defaults.

func (*Engine) CanAutoMerge

func (e *Engine) CanAutoMerge(r Result) bool

CanAutoMerge reports whether a detected conflict can be merged automatically.

func (*Engine) Detect

func (e *Engine) Detect(local LocalEvent, base, current *types.ResourceEnvelope) Result

Detect evaluates a local sync event against the base and current resource states.

func (*Engine) Merge

func (e *Engine) Merge(local LocalEvent, base, current *types.ResourceEnvelope) MergeResult

Merge produces a merged resource and FHIR Patch when the conflict is auto-mergeable.

type LocalEvent

type LocalEvent struct {
	EventID          string
	ResourceType     string
	ResourceID       string
	Operation        string
	BaseCloudVersion string
	LocalVersion     string
	ChangedPaths     []string
	Patch            []byte
	ResourceAfter    *types.ResourceEnvelope
	ResourceHash     string
}

LocalEvent is the mutation shape the conflict engine accepts. It mirrors sync.LocalEvent so the package can be used without importing sync.

type MergeResult

type MergeResult struct {
	Result        Result
	AutoMergeable bool
	Merged        *types.ResourceEnvelope
	Patch         []byte
	Resolution    Classification
	Review        ReviewMetadata
}

MergeResult is the output of a successful or blocked merge attempt.

type Operation

type Operation struct {
	Op    string `json:"op"`
	Path  string `json:"path"`
	Value any    `json:"value,omitempty"`
}

Operation is one FHIR Patch (JSON Patch) operation.

type PathChange

type PathChange struct {
	Path     []string
	Kind     ChangeKind
	Base     any
	Value    any
	Appended []any
	Removed  []any
}

PathChange records one detected change between two resource snapshots.

func (PathChange) Dotted

func (c PathChange) Dotted(resourceType string) string

Dotted returns the FHIR-style dotted path including the resource type.

type PolicyRegistry

type PolicyRegistry struct {
	// contains filtered or unexported fields
}

PolicyRegistry holds rule packs and resolves the active pack for a path.

func DefaultPolicyRegistry

func DefaultPolicyRegistry() *PolicyRegistry

DefaultPolicyRegistry returns a registry with the built-in strict safe-list pack.

func (*PolicyRegistry) Match

func (r *PolicyRegistry) Match(resourceType, path string) *Rule

Match returns the first rule whose resource type and path prefix match.

func (*PolicyRegistry) Register

func (r *PolicyRegistry) Register(pack RulePack)

Register adds a rule pack to the registry. Registering the same name replaces it.

func (*PolicyRegistry) Select

func (r *PolicyRegistry) Select(name string)

Select chooses the active rule pack by name.

type Result

type Result struct {
	Classification   Classification
	Risk             RiskLevel
	AutoMergeable    bool
	LocalChanges     []PathChange
	RemoteChanges    []PathChange
	OverlappingPaths []string
	ReviewReason     string
}

Result captures the classification and mergeability of a conflict.

type ReviewMetadata

type ReviewMetadata struct {
	Reason            string
	LocalPathSummary  []string
	RemotePathSummary []string
	OverlapPaths      []string
	UILabels          map[string]string
}

ReviewMetadata is the UI-facing summary for non-auto-mergeable conflicts.

type RiskLevel

type RiskLevel string

RiskLevel is the clinical review tier for a conflict.

const (
	RiskLevelSafe    RiskLevel = "safe"
	RiskLevelReview  RiskLevel = "review"
	RiskLevelBlocked RiskLevel = "blocked"
)

type Rule

type Rule struct {
	ResourceType string
	PathPrefix   string
	Semantics    RuleSemantics
	Description  string
}

Rule is one resource/path-specific merge policy.

type RulePack

type RulePack struct {
	Name  string
	Rules []Rule
}

RulePack is a named collection of rules. Packs can be registered in a PolicyRegistry and selected per engine configuration.

func DefaultRulePack

func DefaultRulePack() RulePack

DefaultRulePack returns the built-in v1 strict safe-list policy.

type RuleSemantics

type RuleSemantics string

RuleSemantics describes how a rule treats changes at a matched path.

const (
	RuleSemanticsAutoMerge  RuleSemantics = "auto_merge"
	RuleSemanticsAppendOnly RuleSemantics = "append_only"
	RuleSemanticsReviewOnly RuleSemantics = "review_only"
	RuleSemanticsBlock      RuleSemantics = "block"
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL