<commit_before>using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Baseline;
using Baseline.Dates;
using Jasper.Conneg;
using Jasper.Messaging.Logging;
using Jasper.Messaging.Runtime;
using Jasper.Messaging.Runtime.Invocation;
using Jasper.Messaging.Runtime.Routing;
using Jasper.Messaging.Runtime.Serializers;
using Jasper.Messaging.Transports;
using Jasper.Messaging.Transports.Configuration;
using Jasper.Util;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace Jasper.Messaging
{
    public class MessageContext : IMessageContext, IAdvancedMessagingActions
    {
        private readonly IMessageRouter _router;
        private readonly IReplyWatcher _watcher;
        private readonly IHandlerPipeline _pipeline;
        private readonly SerializationGraph _serialization;
        private readonly MessagingSettings _settings;
        private readonly IChannelGraph _channels;
        private readonly IMessageLogger _logger;

        // TODO -- just pull in MessagingRoot?
        public MessageContext(IMessageRouter router, IReplyWatcher watcher, IHandlerPipeline pipeline, MessagingSerializationGraph serialization, MessagingSettings settings, IChannelGraph channels, IPersistence persistence, IMessageLogger logger)
        {
            _router = router;
            _watcher = watcher;
            _pipeline = pipeline;
            _serialization = serialization;
            _settings = settings;
            _channels = channels;
            Persistence = persistence;
            _logger = logger;
        }

        // TODO -- just pull in MessagingRoot?
        public MessageContext(IMessageRouter router, IReplyWatcher watcher, IHandlerPipeline pipeline, MessagingSerializationGraph serialization, MessagingSettings settings, IChannelGraph channels, IPersistence persistence, IMessageLogger logger, Envelope originalEnvelope)
        {
            _router = router;
            _watcher = watcher;
            _pipeline = pipeline;
            _serialization = serialization;
            _settings = settings;
            _channels = channels;
            Persistence = persistence;
            _logger = logger;

            Envelope = originalEnvelope;
            var persistor = new InMemoryEnvelopePersistor();
            EnlistedInTransaction = true;
            Persistor = persistor;

            if (Envelope.AckRequested)
            {
                var ack = buildAcknowledgement();

                persistor.Queued.Fill(ack);
                _outstanding.Add(ack);
            }
        }

        private Envelope buildAcknowledgement()
        {
            var ack = new Envelope
            {
                ParentId = Envelope.Id,
                Destination = Envelope.ReplyUri,
                ResponseId = Envelope.Id,
                Message = new Acknowledgement {CorrelationId = Envelope.Id},
                Route = new MessageRoute(typeof(Acknowledgement), Envelope.ReplyUri, "application/json")
                {
                    Channel = _channels.GetOrBuildChannel(Envelope.ReplyUri),

                },
                Writer = _serialization.JsonWriterFor(typeof(Acknowledgement))
            };

            return ack;
        }

        public IPersistence Persistence { get; }
        public Envelope Envelope { get; }

        private readonly List<Envelope> _outstanding = new List<Envelope>();
        private object _sagaId;

        public IEnumerable<Envelope> Outstanding => _outstanding;

        public bool EnlistedInTransaction { get; private set; }

        public Task EnlistInTransaction(IEnvelopePersistor persistor)
        {
            var original = Persistor;
            Persistor = persistor;<commit_msg>Propagating the saga id in responses. Closes GH-356<commit_after>using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Baseline;
using Baseline.Dates;
using Jasper.Conneg;
using Jasper.Messaging.Logging;
using Jasper.Messaging.Runtime;
using Jasper.Messaging.Runtime.Invocation;
using Jasper.Messaging.Runtime.Routing;
using Jasper.Messaging.Runtime.Serializers;
using Jasper.Messaging.Transports;
using Jasper.Messaging.Transports.Configuration;
using Jasper.Util;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace Jasper.Messaging
{
    public class MessageContext : IMessageContext, IAdvancedMessagingActions
    {
        private readonly IMessageRouter _router;
        private readonly IReplyWatcher _watcher;
        private readonly IHandlerPipeline _pipeline;
        private readonly SerializationGraph _serialization;
        private readonly MessagingSettings _settings;
        private readonly IChannelGraph _channels;
        private readonly IMessageLogger _logger;

        // TODO -- just pull in MessagingRoot?
        public MessageContext(IMessageRouter router, IReplyWatcher watcher, IHandlerPipeline pipeline, MessagingSerializationGraph serialization, MessagingSettings settings, IChannelGraph channels, IPersistence persistence, IMessageLogger logger)
        {
            _router = router;
            _watcher = watcher;
            _pipeline = pipeline;
            _serialization = serialization;
            _settings = settings;
            _channels = channels;
            Persistence = persistence;
            _logger = logger;
        }

        // TODO -- just pull in MessagingRoot?
        public MessageContext(IMessageRouter router, IReplyWatcher watcher, IHandlerPipeline pipeline, MessagingSerializationGraph serialization, MessagingSettings settings, IChannelGraph channels, IPersistence persistence, IMessageLogger logger, Envelope originalEnvelope)
        {
            _router = router;
            _watcher = watcher;
            _pipeline = pipeline;
            _serialization = serialization;
            _settings = settings;
            _channels = channels;
            Persistence = persistence;
            _logger = logger;

            Envelope = originalEnvelope;
            _sagaId = originalEnvelope.SagaId;


            var persistor = new InMemoryEnvelopePersistor();
            EnlistedInTransaction = true;
            Persistor = persistor;

            if (Envelope.AckRequested)
            {
                var ack = buildAcknowledgement();

                persistor.Queued.Fill(ack);
                _outstanding.Add(ack);
            }
        }

        private Envelope buildAcknowledgement()
        {
            var ack = new Envelope
            {
                ParentId = Envelope.Id,
                Destination = Envelope.ReplyUri,
                ResponseId = Envelope.Id,
                SagaId = Envelope.SagaId,
                Message = new Acknowledgement {CorrelationId = Envelope.Id},
                Route = new MessageRoute(typeof(Acknowledgement), Envelope.ReplyUri, "application/json")
                {
                    Channel = _channels.GetOrBuildChannel(Envelope.ReplyUri),

                },
                Writer = _serialization.JsonWriterFor(typeof(Acknowledgement))
            };

            return ack;
        }

        public IPersistence Persistence { get; }
        public Envelope Envelope { get; }

        private readonly List<Envelope> _outstanding = new List<Envelope>();
        private object _sagaId;

        public IEnumerable<Envelope> Outstanding => _outstanding;

        public bool EnlistedInTransaction { get; private set; }

        public Task EnlistInTransaction(IEnvelopePersistor persistor)
        {
            var original = Persistor;
            Persistor = persistor;
{'fraction_non_alphanumeric': 0.06249179466981751, 'fraction_numerical': 0.00039385584875935406, 'mean_word_length': 2.3559471365638767, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2963325', 'n_tokens_mistral': 1933, 'n_tokens_neox': 1808, 'n_words': 464}