text:
import helloworld.HelloWorldSpeechlet;

import org.apache.log4j.BasicConfigurator;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.SecureRequestCustomizer;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.SslConnectionFactory;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.util.ssl.SslContextFactory;

import session.SessionSpeechlet;

import com.amazon.speech.Sdk;
import com.amazon.speech.speechlet.Speechlet;
import com.amazon.speech.speechlet.servlet.SpeechletServlet;

/**
 * Shared launcher for executing all sample skills within a single servlet container.
 */
public final class Launcher {
    /**
     * port number for the jetty server.
     */
    private static final int PORT = 8888;

    /**
     * Security scheme to use.
     */
    private static final String HTTPS_SCHEME = "https";

    /**
     * default constructor.
     */
    private Launcher() {
    }

    /**
     * Main entry point. Starts a Jetty server.
     *
     * @param args
     *            ignored.
     * @throws Exception
     *             if anything goes wrong.
     */
    public static void main(final String[] args) throws Exception {
        // Configure logging to output to the console with default level of INFO
        BasicConfigurator.configure();

        // Configure server and its associated servlets
        Server server = new Server();
        SslConnectionFactory sslConnectionFactory = new SslConnectionFactory();
        SslContextFactory sslContextFactory = sslConnectionFactory.getSslContextFactory();
        sslContextFactory.setKeyStorePath(System.getProperty("javax.net.ssl.keyStore"));
        sslContextFactory.setKeyStorePassword(System.getProperty("javax.net.ssl.keyStorePassword"));
        sslContextFactory.setIncludeCipherSuites(Sdk.SUPPORTED_CIPHER_SUITES);

        HttpConfiguration httpConf = new HttpConfiguration();
        httpConf.setSecurePort(PORT);
        httpConf.setSecureScheme(HTTPS_SCHEME);
        httpConf.addCustomizer(new SecureRequestCustomizer());
        HttpConnectionFactory httpConnectionFactory = new HttpConnectionFactory(httpConf);

        ServerConnector serverConnector =
                new ServerConnector(server, sslConnectionFactory, httpConnectionFactory);
        serverConnector.setPort(PORT);

        Connector[] connectors = new Connector[1];
        connectors[0] = serverConnector;
        server.setConnectors(connectors);

        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
        context.setContextPath("/");
        server.setHandler(context);
        context.addServlet(new ServletHolder(createServlet(new HelloWorldSpeechlet())), "/hello");
        context.addServlet(new ServletHolder(createServlet(new SessionSpeechlet())), "/session");
        server.start();
        server.join();
    }

    private static SpeechletServlet createServlet(final Speechlet speechlet) {
        SpeechletServlet servlet = new SpeechletServlet();
        servlet.setSpeechlet(speechlet);
        return servlet;
    }
}


text:
from ...util.log import getLogger
from ...util.config import config
from ...util.game import Hitman
from .base import Action
from ..const import CMD_TYPES as CTPYES
from ..const import EVENT_TYPES as ETYPES
from ..const import USER_STATUS as USTAT
from ..const import HIT_STATUS as HSTAT
from ..events import SendMessageEvent, UserRegisteredEvent, CollectInfoEvent, UserUpdatedEvent, StartGameEvent, SetupGameEvent, LockUsersEvent, AssignInitialHitsEvent, CheckFreeEvent, AssignmentNotifyEvent, AssignNextRoundEvent, KillConfirmedEvent, ConfirmKillMessageEvent, CheckForWinnerEvent, EndGameEvent, StructuredMessageEvent
from ...api.storage import get_free_users, lock_user, create_hit, create_game, set_status, get_game, session_scope, get_hit
from ...models import Game, Hit
import random

_log = getLogger('action.game')

class MonitorFreeAction(Action):
	def _install(self, proxy):
		proxy.register(ETYPES.USER, 'user_registered', self)
		proxy.register(ETYPES.CRON, 'cron_check_free', self)
		proxy.schedule(CheckFreeEvent('cron_check_free'), config.game.check_interval, 'cron_check_free', repeat = True)

	def _process(self, event):
		users = get_free_users()
		if len(users) >= config.game.size.min:
			self._log.info('Enough free players available. Starting a new game.')
			self._put(SetupGameEvent('game_setup'))

class SetupGameAction(Action):
	def _install(self, proxy):
		proxy.register(ETYPES.GAME, 'game_setup', self)

	def _process(self, event):
		uuid, slacks = create_game()
		if slacks:
			self._log.debug('Created game with %s'%slacks)
			self._schedule(LockUsersEvent('cron_lock', dict(users = slacks, game=uuid)), config.game.lockout, uuid)
			for u in slacks:
				set_status(u, USTAT.WAITING)
				self._put(StructuredMessageEvent('msg_structured', dict(user=u,
												content=config.resp.game_starting.content.format(lockout=config.game.lockout),
												title=config.resp.game_starting.title)))
		else:
			self._log.warning('Cannot create a new game not enough free users, someone probably resigned')

class LockUsersAction(Action):
	def _install(self, proxy):
		proxy.register(ETYPES.GAME, 'cron_lock', self)

	def _process(self, event):
		self._log.debug('Locking users in prepartion for game')
		passed = True
		for user in event.users:
			if not lock_user(user):
				passed = False
		if passed:
			self._put(AssignInitialHitsEvent('game_assign_initial', event.data()))
			return [SendMessageEvent('msg_send', dict(user=u, text=config.resp.user_locked)) for u in event.users]
		raise Exception('Failed to lock all users for game %s'%game.uuid)

class AssignInitialHitsAction(Action):
	def _install(self, proxy):
		proxy.register(ETYPES.GAME, 'game_assign_initial', self)

	def _process(self, event):
		self._log.debug('Assigning initial hits for game %s'%event.game)
		with session_scope() as session:
			game  = session.query(Game).filter_by(uuid=event.game).first()
			if game:
				assignments = Hitman.create_game(game.players, game.weapons, game.locations)
				for user, hit in assignments.items():
					h = Hit(user, *hit, game)
					session.add(h)
				self._schedule(StartGameEvent('game_start', dict(users = event.users, game = event.game)),
												config.game.game_start, key = event.game + '_game_start')
				# return StartGameEvent('game_start', dict(users = event.users, game = event.game))
			else:
				self._log.error('Unable to load game %s'%event.game)

class StartGameAction(Action):
	def _install(self, proxy):
		proxy.register(ETYPES.GAME, 'game_start', self)

	def _process(self, event):
		self._log.debug('Starting game %s'%event.game)
		for user in event.users:
			set_status(user, USTAT.INGAME)
			self._put(AssignmentNotifyEvent('msg_assignment', dict(user=user, game=event.game)))
		self._schedule(AssignNextRoundEvent('game_assign_next', dict(game=event.game)), config.game.assign_next, key = event.game + '_assign_next', repeat = True)
		# self._schedule(AssignNextRoundEvent('game_assign_next', dict(game=event.game)), '1m', key = event.game + '_assign_next', repeat = True)

class ConfirmKillAction(Action):
	def _install(self, proxy):
		proxy.register(ETYPES.GAME, 'game_confirm', self)

	def _process(self, event):
		self._log.debug('Received ConfirmKillEvent for %s'%event.user)
		key = '%s_%s_auto_confirm'%(event.user, event.game)
		self._schedule(KillConfirmedEvent('game_confirmed', dict(user=event.user, game=event.game)), '12h', key=key)
		return ConfirmKillMessageEvent('msg_confirmation', dict(user=event.user))

class KillConfirmedAction(Action):
	def _install(self, proxy):
		proxy.register(ETYPES.GAME, 'game_confirmed', self)

	def _process(self, event):
		self._log.debug('Received KillConfirmedAction from %s'%event.user)
		self._cancel('%s_%s_auto_confirm'%(event.user, event.game))
		with get_hit(target = event.user) as hit:
			hit.status = HSTAT.CONFIRMED
			user = hit.hitman.slack.slack_id
		with get_hit(hitman = event.user) as hit:
			hit.status = HSTAT.OPEN
		set_status(event.user, USTAT.DEAD)
		set_status(user, USTAT.STANDBY)
		return [
				SendMessageEvent('msg_send', dict(user=user, text='Your target has confirmed your kill')),
				CheckForWinnerEvent('game_check_winner', dict(game=event.game))
			]

class AssignNextRoundAction(Action):
	def _install(self, proxy):
		proxy.register(ETYPES.GAME, 'game_assign_next', self)

	def _process(self, event):
		with session_scope() as session:
			game = session.query(Game).filter_by(uuid = event.game).first()
			if game and game.open_hits:
				assigned = self._assign_hits(game.free_players, game.open_hits)
				if assigned:
					for player, hit in assigned.items():
						hit.hitman = player
						hit.status = HSTAT.ACTIVE
						session.add(hit)
						set_status(player.slack.slack_id, USTAT.INGAME)
						self._put(AssignmentNotifyEvent('msg_assignment', dict(user=player.slack.slack_id, game=event.game)))
				else:
					self._log.warning('No suitable hits available for assignment')
			elif game and not game.open_hits:
				self._log.debug('No open hits, not assigning')
			else:
				self._log.warning('Unable to locate game %s'%event.game)

	def _assign_hits(self, players, hits):
		assigned = {}
		players = players[:]	# Shorthand to copy a list
		hits = hits[:]			# This is so we can list.remove() without modifying the list and
		player = players[0]		# effecting the function one step up in the call chain
		while True:
			h = random.choice(hits) # Choose a random hit
			if h.target != player:  # You can't be assigned yourself, so check that
				assigned[player] = h
				hits.remove(h)			# Remove the chosen hit
				players.remove(player)	# and player from their respective lists
				if len(players) > 0:	#space%2Fauth%2Fauthorized If there are any remaining hits/players
					ret = self._assign_hits(players, hits) # Recurse to assign the remaining
					if ret:	# If we were able to assign the remaining hits
						assigned.update(ret)	# Update the assigned dict
						return assigned			# And return it
					else: # If we couldnt assign the rest of the hits, probably because there was only one left and hit.target == player
						hits.append(h)			# Add the player
						players.append(player)	# and the chosen hit back to the list
				else:					# If there are no remaining payers/hits
					return assigned		# return assigned
			elif h.target == player and len(players) == 1:	# If target == player and its the only one left
				return None									# Then return None, and redo the previous step
		# If we make it here then target == player, but there a more to choose from so we loop and try again

class CheckForWinnerAction(Action):
	def _install(self, proxy):
		proxy.register(ETYPES.GAME, 'game_check_winner', self)

	def _process(self, event):
		with get_game(event.game) as game:
			remaining = game.remaining_players
			if len(remaining) == 1:
				self._log.info('One player remaining, %s is the winner!'%remaining[0].uid)
				self._cancel(event.game + '_assign_next')
				return EndGameEvent('game_end', dict(game=event.game, winner=remaining[0].uid))

class EndGameAction(Action):
	def _install(self, proxy):
		proxy.register(ETYPES.GAME, 'game_end', self)

	def _process(self, event):
		self._log.debug('Recieved EndGameEvent, notifying players of winner, and cleaning up game records')
		with session_scope() as session:
			game = session.query(Game).filter_by(uuid=event.game).first()
			for hit in game.hits:
				session.delete(hit)
			for player in game.players:
				player.status = USTAT.FREE
				session.add(player)
				self._put(SendMessageEvent('msg_send', dict(user=player.slack.slack_id, text='This game of ft_hitman is over!\n %s has emerged victorious!'%event.winner)))
			session.delete(game)

redpajama_set_name: RedPajamaGithub


text:
/* vi: set sw=4 ts=4: */
/*
 * Mini touch implementation for busybox
 *
 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
 *
 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
 */

/* BB_AUDIT SUSv3 _NOT_ compliant -- options -a, -m not supported. */
/* http://www.opengroup.org/onlinepubs/007904975/utilities/touch.html */

/* Mar 16, 2003      Manuel Novoa III   (mjn3@codepoet.org)
 *
 * Previous version called open() and then utime().  While this will be
 * be necessary to implement -r and -t, it currently only makes things bigger.
 * Also, exiting on a failure was a bug.  All args should be processed.
 */

#include "libbb.h"

//config:config TOUCH
//config:	bool "touch"
//config:	default y
//config:	help
//config:	  touch is used to create or change the access and/or
//config:	  modification timestamp of specified files.
//config:
//config:config FEATURE_TOUCH_NODEREF
//config:	bool "Add support for -h"
//config:	default y
//config:	depends on TOUCH
//config:	help
//config:	  Enable touch to have the -h option.
//config:	  This requires libc support for lutimes() function.
//config:
//config:config FEATURE_TOUCH_SUSV3
//config:	bool "Add support for SUSV3 features (-d -t -r)"
//config:	default y
//config:	depends on TOUCH
//config:	help
//config:	  Enable touch to use a reference file or a given date/time argument.

//applet:IF_TOUCH(APPLET_NOFORK(touch, touch, BB_DIR_BIN, BB_SUID_DROP, touch))

//kbuild:lib-$(CONFIG_TOUCH) += touch.o

//usage:#define touch_trivial_usage
//usage:       "[-c]" IF_FEATURE_TOUCH_SUSV3(" [-d DATE] [-t DATE] [-r FILE]") " FILE..."
//usage:#define touch_full_usage "\n\n"
//usage:       "Update the last-modified date on the given FILE[s]\n"
//usage:     "\n	-c	Don't create files"
//usage:	IF_FEATURE_TOUCH_NODEREF(
//usage:     "\n	-h	Don't follow links"
//usage:	)
//usage:	IF_FEATURE_TOUCH_SUSV3(
//usage:     "\n	-d DT	Date/time to use"
//usage:     "\n	-t DT	Date/time to use"
//usage:     "\n	-r FILE	Use FILE's date/time"
//usage:	)
//usage:
//usage:#define touch_example_usage
//usage:       "$ ls -l /tmp/foo\n"
//usage:       "/bin/ls: /tmp/foo: No such file or directory\n"
//usage:       "$ touch /tmp/foo\n"
//usage:       "$ ls -l /tmp/foo\n"
//usage:       "-rw-rw-r--    1 andersen andersen        0 Apr 15 01:11 /tmp/foo\n"

/* This is a NOFORK applet. Be very careful! */

/* coreutils implements:
 * -a   change only the access time
 * -c, --no-create
 *      do not create any files
 * -d, --date=STRING
 *      parse STRING and use it instead of current time
 * -f   (ignored, BSD compat)
 * -m   change only the modification time
 * -h, --no-dereference
 * -r, --reference=FILE
 *      use this file's times instead of current time
 * -t STAMP
 *      use [[CC]YY]MMDDhhmm[.ss] instead of current time
 * --time=WORD
 *      change the specified time: WORD is access, atime, or use
 */

int touch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int touch_main(int argc UNUSED_PARAM, char **argv)
{
	int fd;
	int status = EXIT_SUCCESS;
	int opts;
	enum {
		OPT_c = (1 << 0),
		OPT_r = (1 << 1) * ENABLE_FEATURE_TOUCH_SUSV3,
		OPT_d = (1 << 2) * ENABLE_FEATURE_TOUCH_SUSV3,
		OPT_t = (1 << 3) * ENABLE_FEATURE_TOUCH_SUSV3,
		OPT_h = (1 << 4) * ENABLE_FEATURE_TOUCH_NODEREF,
	};
#if ENABLE_FEATURE_TOUCH_SUSV3
# if ENABLE_LONG_OPTS
	static const char touch_longopts[] ALIGN1 =
		/* name, has_arg, val */
		"no-create\0"         No_argument       "c"
		"reference\0"         Required_argument "r"
		"date\0"              Required_argument "d"
		IF_FEATURE_TOUCH_NODEREF("no-dereference\0" No_argument "h")
	;
# endif
	char *reference_file = NULL;
	char *date_str = NULL;
	struct timeval timebuf[2];
	timebuf[1].tv_usec = timebuf[0].tv_usec = 0;
#else
# define reference_file NULL
# define date_str       NULL
# define timebuf        ((struct timeval*)NULL)
#endif

#if ENABLE_FEATURE_TOUCH_SUSV3 && ENABLE_LONG_OPTS
	applet_long_options = touch_longopts;
#endif
	/* -d and -t both set time. In coreutils,
	 * accepted data format differs a bit between -d and -t.
	 * We accept the same formats for both */
	opts = getopt32(argv, "c" IF_FEATURE_TOUCH_SUSV3("r:d:t:")
				IF_FEATURE_TOUCH_NODEREF("h")
				/*ignored:*/ "fma"
				IF_FEATURE_TOUCH_SUSV3(, &reference_file)
				IF_FEATURE_TOUCH_SUSV3(, &date_str)
				IF_FEATURE_TOUCH_SUSV3(, &date_str)
	);

	argv += optind;
	if (!*argv) {
		bb_show_usage();
	}

	if (reference_file) {
		struct stat stbuf;
		xstat(reference_file, &stbuf);
		timebuf[1].tv_sec = timebuf[0].tv_sec = stbuf.st_mtime;
		/* Can use .st_mtim.tv_nsec
		 * (or is it .st_mtimensec?? see date.c)
		 * to set microseconds too.
		 */
	}

	if (date_str) {
		struct tm tm_time;
		time_t t;

		//memset(&tm_time, 0, sizeof(tm_time));
		/* Better than memset: makes "HH:MM" dates meaningful */
		time(&t);
		localtime_r(&t, &tm_time);
		parse_datestr(date_str, &tm_time);

		/* Correct any day of week and day of year etc. fields */
		tm_time.tm_isdst = -1;  /* Be sure to recheck dst */
		t = validate_tm_time(date_str, &tm_time);

		timebuf[1].tv_sec = timebuf[0].tv_sec = t;
	}

	do {
		int result;
		result = (
#if ENABLE_FEATURE_TOUCH_NODEREF
			(opts & OPT_h) ? lutimes :
#endif
			utimes)(*argv, (reference_file || date_str) ? timebuf : NULL);
		if (result != 0) {
			if (errno == ENOENT) { /* no such file? */
				if (opts & OPT_c) {
					/* Creation is disabled, so ignore */
					continue;
				}
				/* Try to create the file */
				fd = open(*argv, O_RDWR | O_CREAT, 0666);
				if (fd >= 0) {
					xclose(fd);
					if (reference_file || date_str)
						utimes(*argv, timebuf);
					continue;
				}
			}
			status = EXIT_FAILURE;
			bb_simple_perror_msg(*argv);
		}
	} while (*++argv);

	return status;
}


text:
"""Scratchpad/chain of thought, demonstrated on gsm8k.

Scratchpad: https://arxiv.org/abs/2112.00114
Chain of thought: https://arxiv.org/abs/2201.11903

Self consistency (https://arxiv.org/abs/2203.11171) is done by marginalizing out
  the `thought` in the `infer` operator.
"""
import dataclasses
import re
from typing import Dict, Iterable, Tuple, Optional, Text, List
import cascades as cc
from cascades.examples.tasks import gsm8k

# Standard chain of thought arithmetic prompts.
# From https://arxiv.org/abs/2112.00114
PROMPTS = """Q: There are 15 trees in the grove. Grove workers will plant trees in the grove today. After they are done, there will be 21 trees. How many trees did the grove workers plant today?
A: We start with 15 trees. Later we have 21 trees. The difference must be the number of trees they planted. So, they must have planted 21 - 15 = 6 trees. The answer is 6.
Q: If there are 3 cars in the parking lot and 2 more cars arrive, how many cars are in the parking lot?
A: There are 3 cars in the parking lot already. 2 more arrive. Now there are 3 + 2 = 5 cars. The answer is 5.
Q: Leah had 32 chocolates and her sister had 42. If they ate 35, how many pieces do they have left in total?
A: Leah had 32 chocolates and Leah's sister had 42. That means there were originally 32 + 42 = 74 chocolates. 35 have been eaten. So in total they still have 74 - 35 = 39 chocolates. The answer is 39.
Q: Jason had 20 lollipops. He gave Denny some lollipops. Now Jason has 12 lollipops. How many lollipops did Jason give to Denny?
A: Jason had 20 lollipops. Since he only has 12 now, he must have given the rest to Denny. The number of lollipops he has given to Denny must have been 20 - 12 = 8 lollipops. The answer is 8.
Q: Shawn has five toys. For Christmas, he got two toys each from his mom and dad. How many toys does he have now?
A: He has 5 toys. He got 2 from mom, so after that he has 5 + 2 = 7 toys. Then he got 2 more from dad, so in total he has 7 + 2 = 9 toys. The answer is 9.
Q: There were nine computers in the server room. Five more computers were installed each day, from monday to thursday. How many computers are now in the server room?
A: There are 4 days from monday to thursday. 5 computers were added each day. That means in total 4 * 5 = 20 computers were added. There were 9 computers in the beginning, so now there are 9 + 20 = 29 computers. The answer is 29.
Q: Michael had 58 golf balls. On tuesday, he lost 23 golf balls. On wednesday, he lost 2 more. How many golf balls did he have at the end of wednesday?
A: Michael initially had 58 balls. He lost 23 on Tuesday, so after that he has 58 - 23 = 35 balls. On Wednesday he lost 2 more so now he has 35 - 2 = 33 balls. The answer is 33.
Q: Olivia has $23. She bought five bagels for $3 each. How much money does she have left?
A: She bought 5 bagels for $3 each. This means she spent 5 * $3 = $15 on the bagels. She had $23 in beginning, so now she has $23 - $15 = $8. The answer is 8.
"""

@dataclasses.dataclass
class ReasonIO:
  """Represents a question and answer, together with reasoning."""
  question: Text
  reason: Optional[Text] = None
  answer: Optional[Text] = None
  id: Optional[Text] = dataclasses.field(default=None)

# From https://arxiv.org/abs/2203.11171
def _process_part(qa: Text) -> ReasonIO:
  question, reason = qa.split('A:')
  question = question.replace('Q:', '')
  reason, answer = reason.split('The answer is')
  answer = answer.replace('.', '')
  return ReasonIO(
      question=question.strip(), reason=reason.strip(), answer=answer.strip())

def load_chain_examples() -> Tuple[ReasonIO]:
  """Load the standard chain of thought prompts used in the paper."""
  parts = PROMPTS.split('Q:')[1:]
  chain_prompts = tuple(_process_part(x) for x in parts)
  return chain_prompts

def load_gsm8k(base_dir=gsm8k.GSM8K_PATH):
  """Load and process the gsm8k dataset."""
  splits = gsm8k.load_dataset(base_dir=base_dir)
  train = tuple(map(process_gsm8k_example, splits['train']))
  test = tuple(map(process_gsm8k_example, splits['test']))
  return dict(train=train, test=test)

def process_gsm8k_example(x: Dict[Text, Text]) -> ReasonIO:
  """Convert gsm8k dicts into ReasonIO."""
  return ReasonIO(
      question=x['question'],
      reason=x['answer'].split('####')[0].strip(),
      answer=x['final_answer'],
      id=x['id'])

def fewshot_prompt(examples: Iterable[ReasonIO],
                   target: Optional[ReasonIO] = None):
  """Construct a few shot prompt."""
  parts = []
  for x in examples:
    if x.reason is None:
      raise ValueError('Must provide reason for few shot.')
    if x.answer is None:
      raise ValueError('Must provide answer for few shot.')
    part = f'Question: {x.question}\nReason: {x.reason}\nAnswer: {x.answer}'
    parts.append(part)
  if target:
    part = f'Question: {target.question}\nReason'
    parts.append(part)
  else:
    parts.append('')

  prompt = '\n===\n'.join(parts).strip()
  return prompt

@cc.model
def sample_with_prompts(target: ReasonIO,
                        examples: List[ReasonIO],
                        n_prompts: Optional[int] = None,
                        lm=None,
                        max_calls=4):
  """Sample an answer for target given prompts.

  Args:
    target: Target task.
    examples: Prompting tasks.
    n_prompts: If None, use all examples in prompt. Otherwise, select this many
      examples to place in prompt.
    lm: Language model to use.
    max_calls: Max # of times to iterate the LM sampling.

  Yields:
    Cascade distributions (all LM sample nodes in this case).

   Returns:
     predicted answer string
  """
  # Select a random subset and ordering of the prompts.
  yield cc.log(value=target.id, name='problem_id')
  if n_prompts:
    chosen_examples = yield cc.sample(
        cc.Choose(k=n_prompts, options=examples), name='choose_prompts')
  else:
    chosen_examples = examples

  # Create few shot prompt
  prompt = fewshot_prompt(examples=chosen_examples, target=target)

  # Sample until we hit the end of example marker (`===`), then extract
  # the answer as rightmost digits.
  prediction = yield cc.sample(
      cc.String(prompt=prompt, lm=lm, k=max_calls, until='==='), name='thought')

  # Find right most number.
  nums = re.findall(r'\d+', prediction)
  if nums:
    answer = nums[-1]
  else:
    yield cc.reject(reason=f'No answer found in `{prediction}`')
  return answer  # pytype: disable=name-error  # py310-upgrade


text:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Nitra;
using Ammy.Infrastructure;

namespace Ammy.Build
{
    public class AmmyCompilerTask : Task
    {
        [Required]
        public string Items { get; set; }

        [Required]
        public string SourceFiles { get; set; }

        public string IncludedPages { get; set; }

        [Required]
        public string References { get; set; }

        [Required]
        public string OutputPath { get; set; }

        [Required]
        public string RootNamespace { get; set; }

        [Required]
        public string AssemblyName { get; set; }

        [Required]
        public string ProjectDir { get; set; }

        [Required]
        public string TargetPath { get; set; }

        [Required]
        public string DefineConstants { get; set; }

        [Output]
        public string[] GeneratedItems { get; set; }

        [Output]
        public string[] GeneratedXamlItems { get; set; }

        [Output]
        public bool HasErrors { get; set; }

        [Output]
        public string AmmyPlatform { get; set; }

        public override bool Execute()
        {
            //Debugger.Launch();

            // Don't rebuild 20 times when updating/installing from NuGet or adding item template
            string stackTrace = Environment.StackTrace;

            if (stackTrace.Contains("at NuGet.PackageManagement.VisualStudio") ||
                stackTrace.Contains("at System.Windows.Controls.PopupControlService") ||
                stackTrace.Contains("at Microsoft.VisualStudio.Build.ComInteropWrapper.ProjectShim.BuildTargetsImpl") ||
                stackTrace.Contains("at Microsoft.VisualStudio.TemplateWizard.Wizard")) {
                GeneratedItems = new string[0];
                GeneratedXamlItems = new string[0];

                HasErrors = false;
                AmmyPlatform = "<notset>";

                return true;
            }

            var allSources = Items.Split(';')
                                  .Concat(SourceFiles.Split(';'))
                                  .ToArray();

            var references = References.Split(';')
                                       .ToList();

            var pages = IncludedPages ?? "";
            var includedPages = pages.Split(';')
                                     .ToList();
            //var currentAssemblyPath = Path.Combine(ProjectDir, OutputPath, AssemblyName + ".exe");

            //if (File.Exists(currentAssemblyPath))
            //    references.Add(currentAssemblyPath);

            if (Items.Length == 0)
                return true;

            var constants = DefineConstants?.Split(';');
            var needUpdate = true;

            if (constants != null) {
                var isDebug = constants.Contains("DEBUG");
                var noAmmyUpdate = constants.Contains("NO_AMMY_UPDATE");
                needUpdate = isDebug && !noAmmyUpdate;
            }

            try {
                var compiler = new AmmyCompiler(isMsBuildCompilation: true);
                var result = compiler.Compile(RootNamespace, allSources, references, ProjectDir, OutputPath, AssemblyName, TargetPath, null, true, true, needUpdate);

                Log.LogMessage("Ammy update enabled: " + needUpdate);

                foreach (var msg in result.CompilerMessages) {
                    if (msg.Type == CompilerMessageType.Error || msg.Type == CompilerMessageType.FatalError) {
                        var loc = msg.Location;
                        var start = loc.StartLineColumn;
                        var end = loc.EndLineColumn;
                        Log.LogError("", "", "", msg.Location.Source.File.FullName, start.Line, start.Column, end.Line, end.Column, msg.Text);
                    } else if (msg.Type == CompilerMessageType.Warning) {
                        Log.LogWarning(msg.ToString());
                    }
                }

                GeneratedItems = result.GeneratedFiles.ToArray();
                GeneratedXamlItems = result.GeneratedXamlFiles
                                           .SelectMany(filename => new[] { filename.ToRelativeFile(ProjectDir) })
                                           .Where(fname => !ListContainsFile(fname, includedPages))
                                           .ToArray();

                HasErrors = !result.IsSuccess;
                AmmyPlatform = result.AmmyProject.PlatformName;

                return true;
            }
            catch (Exception e) {
                Log.LogError(e.Message);

                HasErrors = true;

                return false;
            }
        }

        private bool ListContainsFile(string file, List<string> fileList)
        {
            foreach (var fileListFile in fileList)
                if (file.SamePathAs(fileListFile))
                    return true;

            return false;
        }
    }
}


text:
cloud
=====

Cloud Test, Laravel 3.0 framework


text:
describe WatchersController, type: 'controller' do
  let(:app) do
    a = Fabricate(:app)
    Fabricate(:user_watcher, :app => a)
    a
  end

  describe "DELETE /apps/:app_id/watchers/:id/destroy" do
    context "with admin user" do
      before(:each) do
        sign_in Fabricate(:admin)
      end

      context "successful watcher deletion" do
        let(:problem) { Fabricate(:problem_with_comments) }
        let(:watcher) { app.watchers.first }

        before(:each) do
          delete :destroy, :app_id => app.id, :id => watcher.user.id.to_s
          problem.reload
        end

        it "should delete the watcher" do
          expect(app.watchers.detect{|w| w.id.to_s == watcher.id }).to be nil
        end

        it "should redirect to index page" do
          expect(response).to redirect_to(root_path)
        end
      end
    end
  end
end


text:
// Tensorflow Concat kernel implementation.

#ifndef TFRT_BACKENDS_CPU_LIB_KERNELS_CPU_CONCAT_KERNEL_H_
#define TFRT_BACKENDS_CPU_LIB_KERNELS_CPU_CONCAT_KERNEL_H_

#include "tfrt/common/compat/eigen/eigen_kernel.h"
#include "tfrt/support/error_util.h"
#include "tfrt/support/ranges.h"
#include "tfrt/tensor/dense_host_tensor.h"
#include "tfrt/tensor/tensor_shape.h"

namespace tfrt {
namespace cpu {

inline Expected<int64_t> ConcatAxis(const DenseHostTensor& axis_arg) {
  if (axis_arg.dtype() == DType::I32) {
    DHTArrayView<int32_t> view(&axis_arg);
    return *view.begin();
  } else if (axis_arg.dtype() == DType::I64) {
    DHTArrayView<int64_t> view(&axis_arg);
    return *view.begin();
  } else {
    return MakeStringError("Unsupported axis data type");
  }
}

template <typename TensorRange>
Expected<TensorMetadata> ConcatMetadataKernel(const TensorRange& args,
                                              int axis) {
  const Tensor& arg0 = args[0];
  const DType& arg0_dtype = arg0.dtype();
  const TensorShape& arg0_shape = arg0.shape();

  const int rank = arg0_shape.GetRank();
  // Compute the actual axis from a negative value.
  axis = axis < 0 ? axis + rank : axis;

  // The size of a result along the concatenation dimension.
  Index concat_axis_dim_size = 0;

  for (size_t i = 0; i < args.size(); ++i) {
    const Tensor& arg = args[i];
    const TensorShape& shape = arg.shape();

    // Implicitly convert scalars to vectors of length 1.
    concat_axis_dim_size += rank == 0 ? 1 : shape.GetDimensionSize(axis);

    // Inputs must be of the same rank and data type.
    if (arg.dtype() != arg0_dtype)
      return MakeStringError("Input dtypes do not match");
    if (shape.GetRank() != rank)
      return MakeStringError("Input ranks do not match");

    // Construct a error message for non-matching dimension.
    auto wrong_dim = [&](int dim) -> Error {
      return MakeStringError(
          StrCat("Wrong non-concatenating dimension. Dimension ", dim,
                 " for argument ", i,
                 " doesn't match the dimension of argument 0 (concatenate ",
                 shape, " and ", arg0_shape, " on axis ", axis, ")"));
    };

    // Dimensions before the concatenation axis must match.
    for (int d = 0; d < axis; ++d) {
      if (shape.GetDimensionSize(d) != arg0_shape.GetDimensionSize(d))
        return wrong_dim(d);
    }

    // Dimensions after the concatenation axis must match.
    for (int d = axis + 1; d < rank; ++d) {
      if (shape.GetDimensionSize(d) != arg0_shape.GetDimensionSize(d))
        return wrong_dim(d);
    }
  }

  // Build output tensor dimensions.
  llvm::SmallVector<Index, 4> output_dims;
  for (int d = 0; d < axis; ++d)
    output_dims.push_back(arg0_shape.GetDimensionSize(d));
  output_dims.push_back(concat_axis_dim_size);
  for (int d = axis + 1; d < rank; ++d)
    output_dims.push_back(arg0_shape.GetDimensionSize(d));

  return TensorMetadata(arg0_dtype, TensorShape(output_dims));
}

template <int rank>
using ConcatRankTag = std::integral_constant<int, rank>;

template <typename T, typename DHTRange>
Error ConcatKernel(const DHTRange& args, int axis, DenseHostTensor* output) {
  const int input_rank = args[0].shape().GetRank();

  // Handle scalars concatenation separately to keep the common path simple.
  if (input_rank == 0) {
    for (int i = 0; i < args.size(); ++i) {
      const T* inp = static_cast<const T*>(args[i].data());
      T* out = static_cast<T*>(output->data());
      out[i] = *inp;
    }

    return Error::success();
  }

  // Compute the actual axis from a negative value.
  axis = axis < 0 ? axis + input_rank : axis;

  // TODO(ezhulenev): Make this asynchronous/multithreaded.
  auto rank_dispatch = [&](auto rank_tag) -> void {
    static constexpr int rank = decltype(rank_tag)::value;

    auto output_view = MutableDHTIndexableView<T, rank>(output);
    auto output_t = compat::AsEigenTensor(output_view);

    // Offset for writing to the output along the axis dimension.
    Index offset = 0;

    for (auto& dht : args) {
      auto arg_view = DHTIndexableView<T, rank>(&dht);
      auto arg_t = compat::AsEigenConstTensor(arg_view);

      // Offsets for the output slice.
      Eigen::DSizes<Index, rank> offsets;
      for (int d = 0; d < rank; d++) offsets[d] = 0;
      offsets[axis] = offset;

      // Size of the output slice.
      Eigen::DSizes<Index, rank> sizes;
      for (int d = 0; d < rank; d++) sizes[d] = dht.shape().GetDimensionSize(d);

      // It's possible for this input tensor to have zero dimension along the
      // specified axis, in which case no work is needed, b/172595919
      if (sizes[axis] == 0) continue;

      offset += sizes[axis];

      // Write argument into the output tensor slice.
      output_t.slice(offsets, sizes) = arg_t;
    }
  };

  auto rank = output->shape().GetRank();
  // Dispatch based on the output tensor rank.
  if (rank == 1) {
    rank_dispatch(ConcatRankTag<1>{});
  } else if (rank == 2) {
    rank_dispatch(ConcatRankTag<2>{});
  } else if (rank == 3) {
    rank_dispatch(ConcatRankTag<3>{});
  } else if (rank == 4) {
    rank_dispatch(ConcatRankTag<4>{});
  } else if (rank == 5) {
    rank_dispatch(ConcatRankTag<4>{});
  } else {
    return MakeStringError("Unsupported output tensor rank");
  }

  return Error::success();
}

}  // namespace cpu
}  // namespace tfrt

#endif  // TFRT_BACKENDS_CPU_LIB_KERNELS_CPU_CONCAT_KERNEL_H_


text:
package com.zimbra.cs.mailbox;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import javax.mail.internet.MimeMessage;

import com.google.common.base.Objects;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.zimbra.common.account.ZAttrProvisioning.MailThreadingAlgorithm;
import com.zimbra.common.localconfig.DebugConfig;
import com.zimbra.common.mime.HeaderUtils;
import com.zimbra.common.mime.MimeHeader;
import com.zimbra.common.service.ServiceException;
import com.zimbra.common.util.ByteUtil;
import com.zimbra.common.util.Constants;
import com.zimbra.common.util.ZimbraLog;
import com.zimbra.cs.account.Account;
import com.zimbra.cs.account.Provisioning;
import com.zimbra.cs.db.DbMailItem;
import com.zimbra.cs.mime.Mime;
import com.zimbra.cs.mime.ParsedMessage;

/** Manages message threading into {@link Conversation}s based on the {@link
 *  Account} attribute {@link Provisioning#A_zimbraMailThreadingAlgorithm}.
 *

 *  If their threading mode is {@link MailThreadingAlgorithm#none}, no
 *  conversation threading is performed.
 *

 *  If their threading mode is {@link MailThreadingAlgorithm#subject}, the
 *  message will be threaded based solely on its normalized subject.
 *

 *  If their threading mode is {@link MailThreadingAlgorithm#strict}, only the
 *  threading message headers ({@code References}, {@code In-Reply-To}, {@code
 *  Message-ID}, and {@code Resent-Message-ID}) are used to correlate messages.
 *  No checking of normalized subjects is performed.
 *

 *  If their threading mode is {@link MailThreadingAlgorithm#references}, the
 *  same logic as {@link MailThreadingAlgorithm#strict} applies with the
 *  constraints slightly loosened so that the non-standard {@code Thread-Index}
 *  header is considered when threading messages and that a reply message
 *  lacking {@code References} and {@code In-Reply-To} headers will fall back
 *  to using subject-based threading.
 *

 *  If their threading mode is {@link MailThreadingAlgorithm#subjrefs}, the
 *  same logic as {@link MailThreadingAlgorithm#references} applies with the
 *  caveat that modifications to the subject will break a thread in two. */
public final class Threader {
    // references-based threading:
    //   - check OPEN_CONVERSATION for Message-Id, In-Reply-To, References
    //     - 0 matches: fall back to normal threading
    //     - 1 match: target conversation (also check subject?)
    //     - >1 matches: merge other convs into largest match
    //   - if vconv -> conv, shift open rows
    //   - once done, add all ids to OPEN_CONV pointing at target conv (may be vconv)

    private final MailThreadingAlgorithm mode;
    private final Mailbox mbox;
    private final ParsedMessage pm;
    private final String subjHash;
    private List<String> refHashes;
    private List<Conversation> matchedConversations;

    public Threader(Mailbox mbox, ParsedMessage pm) throws ServiceException {
        this.mbox = mbox;
        this.pm = pm;
        this.mode = getThreadingAlgorithm(mbox.getAccount());
        this.subjHash = isEnabled() ? Mailbox.getHash(pm.getNormalizedSubject()) : null;
        this.refHashes = isEnabled() && !mode.isSubject() ? getReferenceHashes(true) : null;
    }

    /** Retrieves the current algorithm used for threading new messages.
     *  Threading can be disabled on a system-wide basis via the localconfig
     *  key {@code debug_disable_conversation}.  If threading has not been
     *  disabled, the threading mode is retrieved from the {@link Account}
     *  attribute {@link Provisioning#A_zimbraMailThreadingAlgorithm}.
     *  If this attribute is unset, we default to {@link
     *  MailThreadingAlgorithm#references}.
     * @see DebugConfig#disableConversation */
    private static MailThreadingAlgorithm getThreadingAlgorithm(Account acct) {
        if (DebugConfig.disableConversation) {
            return MailThreadingAlgorithm.none;
        }

        MailThreadingAlgorithm tmode = acct.getMailThreadingAlgorithm();
        return tmode == null ? MailThreadingAlgorithm.references : tmode;
    }

    /** Returns the SHA-1 hashes for all of the {@code Threader}'s message's
     *  threading header values.  This always includes the contents of its
     *  standard threading headers (the message-ids from {@code Messsage-ID},
     *  {@code In-Reply-To}, and {@code References}).  If the threading mode
     *  is not {@link MailThreadingAlgorithm#strict}, we also include hashes
     *  based on the non-standard {@code Thread-Index} header, useful for
     *  interoperability with Microsoft Outlook and siblings.  We prepend two
     *  control characters to these values before generating the hashes to
     *  avoid OPEN_CONVERSATION conflicts with hashed normalized subjects.
     * @param includeParents  If {@code true}, includes the hashes for all
     *                        threading headers in the returned {@code List}.
     *                        If {@code false}, hashes only the {@code
     *                        Message-ID} header and (optionally) {@code
     *                        Thread-Index}.
     * @see Mailbox#getHash(String)
     * @see #getThreadIndexHashes(boolean)
     * @return a mutable {@code List} containing the base64-encoded hashes. */
    private List<String> getReferenceHashes(boolean includeParents) {
        List<String> hashes = new ArrayList<String>();

        Collection<String> references = includeParents ? pm.getAllReferences() : Mime.getReferences(pm.getMimeMessage(), "Message-ID");
        for (String reference : references) {
            hashes.add(Mailbox.getHash("\u0001\u0002" + Strings.nullToEmpty(reference)));
        }

        // in "strict" mode, we ignore the Thread-Index header
        if (!mode.isStrict()) {
            hashes.addAll(getThreadIndexHashes(true));
        }

        return hashes;
    }

    static final String IGNORE_THREAD_INDEX = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";

    /** Returns the SHA-1 hashes for the {@code Threader}'s message's {@code
     *  Thread-Index} header and those derived from it for all its parents.
     *  We prepend two control characters to these values before generating
     *  the hashes to avoid OPEN_CONVERSATION conflicts with hashed normalized
     *  subjects.
     * @param includeParents  If {@code true}, includes the hashes for parent
     *                        messages in the returned {@code List}.  If {@code
     *                        false}, returns only this message's hash.
     * @see ThreadIndex */
    private List<String> getThreadIndexHashes(boolean includeParents) {
        byte[] tindex = null;
        try {
            String header = pm.getMimeMessage().getHeader("Thread-Index", null);
            if (header != null && header.startsWith(IGNORE_THREAD_INDEX)) {
                //bogus thread index, let subject threading take over
                return Collections.emptyList();
            }
            tindex = ThreadIndex.parseHeader(header);
        } catch (Exception e) { }
        if (tindex == null)
            return Collections.emptyList();

        List<String> hashes = new ArrayList<String>(3);
        // technically shouldn't be shorter than 22 bytes, but always take 1 pass just in case
        int length = tindex.length;
        do {
            try {
                MessageDigest md = MessageDigest.getInstance("SHA1");
                md.update((byte) 1);  md.update((byte) 3);
                md.update(tindex, 0, length);
                hashes.add(ByteUtil.encodeFSSafeBase64(md.digest()));
            } catch (NoSuchAlgorithmException e) {
                return Collections.emptyList();
            }
            length -= 5;
        } while (length >= 22 && includeParents);
        return hashes;
    }

    public static class ThreadIndex {
        /** Generates a new {@code Thread-Index} value suitable for adding as
         *  a header for a message that is not a reply to a parent containing
         *  a {@code Thread-Index} header.  This value is a base64-encoded
         *  22-byte random value and has no relation to the message. */
        public static String newThreadIndex() {
            byte[] random = new byte[22];
            new Random().nextBytes(random);
            return HeaderUtils.encodeB2047(random).trim();
        }

        /** Generates a new {@code Thread-Index} value suitable for adding as
         *  a header for a reply to a parent containing a {@code Thread-Index}
         *  header.  This value is created by appending 5 random bytes to the
         *  end of the parent's decoded {@code Thread-Index} value. */
        public static String addChild(byte[] oldTIndex) {
            if (oldTIndex == null) {
                return null;
            }

            int oldLength = oldTIndex.length;
            byte[] tindex = new byte[oldLength + 5], random = new byte[5];
            new Random().nextBytes(random);
            System.arraycopy(oldTIndex, 0, tindex, 0, oldLength);
            System.arraycopy(random, 0, tindex, oldLength, 5);
            return HeaderUtils.encodeB2047(tindex).trim();
        }

        /** Parses the contents of a {@code Thread-Index} header and returns
         *  the base64-decoded thread-index {@code byte} array.  Such arrays
         *  must be of length 22, 27, 32, 37, 42, etc.; if the decoded array
         *  does not match the pattern, {@code null} is returned instead. */
        public static byte[] parseHeader(String tidxHdr) {
            if (tidxHdr == null || tidxHdr.trim().isEmpty()) {
                return null;
            }

            byte[] tindex = HeaderUtils.decodeB2047(tidxHdr.trim());
            if (tindex.length % 5 != 2) {
                ZimbraLog.mailbox.debug("  ignoring Thread-Index of decoded length %d", tindex.length);
                return null;
            }
            return tindex;
        }

        /** Generates a new {@code Thread-Topic} value suitable for adding as
         *  a header for a message that is not a reply to a parent containing
         *  a {@code Thread-Topic} header.  This value is the normalized
         *  version of the message's {@code Subject} header (i.e. with all
         *  prefixes and trailers stripped).
         * @see ParsedMessage#normalize(String) */
        public static String newThreadTopic(String subject) {
            String ttopic = ParsedMessage.normalize(subject);
            return ttopic == null ? null : MimeHeader.escape(ttopic, null, false);
        }
    }

    /** Returns whether threading is enabled for this {@code Mailbox}. */
    boolean isEnabled() {
        return !mode.isNone();
    }

    /** Returns whether the regular old-message mailbox purge task should also
     *  trim old entries from the OPEN_CONVERSATION table for the given {@code
     *  Mailbox}.  In general, when we thread using references as a criterion,
     *  we don't want to remove older entries. */
    static boolean isHashPurgeAllowed(Account acct) {
        MailThreadingAlgorithm tmode = getThreadingAlgorithm(acct);
        return tmode.isNone() || tmode.isSubject();
    }

    /** Searches for an existing {@code Conversation} matching the message
     *  being threaded.  If the user's threading mode is {@code subject}, this
     *  method always returns an empty list.  If the user's threading mode is
     *  {@code subject}, only the normalized subject is used for threading and
     *  only one {@code Conversation} will ever be returned.  For all other
     *  threading modes, 0, 1 or multiple {@code Conversation}s may be returned;
     *  in the multi-{@code Conversation} case, the caller is responsible for
     *  choosing one or merging them all. */
    List<Conversation> lookupConversation() throws ServiceException {
        if (matchedConversations == null) {
            if (mode.isNone()) {
                return Collections.emptyList();
            }
            ZimbraLog.mailbox.debug("  threading message \"%s\" (%s)", pm.getSubject(), pm.getMessageID());

            List<Conversation> matches = Collections.emptyList();
            if (!mode.isSubject()) {
                // check to see if there's a conversation matching this message's references
                matches = lookupByReference();
            }
            if (matches.isEmpty() && (mode.isSubject() || (!mode.isStrict() && isReplyWithoutReferences()))) {
                // check for an existing open conversation with the same normalized subject
                matches = lookupBySubject();
            }
            matchedConversations = matches;
        }
        return new ArrayList<Conversation>(matchedConversations);
    }

    /** Clears cached threader results.  Matching conversations will be
     *  recalculated the next time {@link #lookupConversation()} is called. */
    void reset() {
        matchedConversations = null;
    }

    /** Returns whether the message being threaded has a subject that looks
     *  like a reply but has no {@code In-Reply-To} or {@code References}
     *  header.  Several popular mailers do this, including at least some
     *  versions of Outlook and Lotus Notes. */
    private boolean isReplyWithoutReferences() {
        if (!pm.isReply()) {
            return false;
        }

        MimeMessage mm = pm.getMimeMessage();
        return Mime.getReferences(mm, "In-Reply-To").isEmpty() && Mime.getReferences(mm, "References").isEmpty();
    }

    /** Searches the {@code OPEN_CONVERSATION} table for existing {@code
     *  Conversation}s matching this new message's threading headers.  We
     *  thread on {@code References}, {@code In-Reply-To}, {@code Message-ID}
     *  (which also correlates mailing list duplicates), and {@code
     *  Resent-Message-ID}.  If the user's threading algorithm is not
     *  {@link MailThreadingAlgorithm#strict}, we also thread on {@code
     *  Thread-Index}.
     * @return a list of matching {@code Conversation}s
     */
    private List<Conversation> lookupByReference() throws ServiceException {
        if (refHashes == null || refHashes.isEmpty()) {
            return Collections.emptyList();
        }
        ZimbraLog.mailbox.debug("  lookup by references (%s): %s", mode, refHashes);
        List<MailItem.UnderlyingData> dlist = DbMailItem.getByHashes(mbox, refHashes);
        if (dlist == null || dlist.isEmpty()) {
            ZimbraLog.mailbox.debug("  no reference matches found");
            return Collections.emptyList();
        }

        List<Conversation> matches = new ArrayList<Conversation>(dlist.size());
        for (MailItem.UnderlyingData data : dlist) {
            if (data.type == MailItem.Type.CONVERSATION.toByte()) {
                matches.add(mbox.getConversation(data));
            } else {
                matches.add((Conversation) mbox.getMessage(data).getParent());
            }
        }
        ZimbraLog.mailbox.debug("  found %d reference match(es)", matches.size());

        if (mode.isSubjrefs()) {
            // constrain the matches to those with the same normalized subject
            for (int i = matches.size() - 1; i >= 0; i--) {
                Conversation hit = matches.get(i);
                if (!pm.getNormalizedSubject().equals(hit.getNormalizedSubject())) {
                    ZimbraLog.mailbox.debug("  dropping one reference match due to non-matching subjects");
                    matches.remove(i);
                    // need to trim refHashes so as not to overwrite the hashes that mapped to the other subject's thread
                    // FIXME: this trimming may be a little too aggressive, may miss some out-of-order delivery cases
                    refHashes = getReferenceHashes(false);
                }
            }
            if (matches.isEmpty()) {
                ZimbraLog.mailbox.debug("  no valid reference matches found");
            }
        }
        assert matches != null;
        return matches;
    }

    /** The number of milliseconds of inactivity (i.e. time since last message
     *  receipt) after which a conversation is considered "closed". */
    private static final long CONVERSATION_REPLY_WINDOW = Constants.MILLIS_PER_MONTH;

    /** The number of milliseconds of inactivity (i.e. time since last message
     *  receipt) after which a non-reply is not grouped with an existing
     *  conversation with the same subject. */
    private static final long CONVERSATION_NONREPLY_WINDOW = 2 * Constants.MILLIS_PER_DAY;

    /** The maximum size for a conversation beyond which non-reply messages
     *  are not grouped with it, even if their delivery time is within
     *  {@link #CONVERSATION_NONREPLY_WINDOW}. */
    private static final int CONVERSATION_NONREPLY_SIZE_LIMIT = 50;

    /** Searches the {@code OPEN_CONVERSATION} table for existing {@code
     *  Conversation}s matching this new message's normalized subject.
     *  A potential match may be discarded as stale if its latest message was
     *  added too long ago, or may be discarded if the new message is not a
     *  reply and the match is already too large.
     * @return a list containing one matching {@code Conversation}s
     * @see #CONVERSATION_REPLY_WINDOW
     * @see #CONVERSATION_NONREPLY_WINDOW
     * @see #CONVERSATION_NONREPLY_SIZE_LIMIT */
    private List<Conversation> lookupBySubject() throws ServiceException {
        if (subjHash == null) {
            return null;
        }

        ZimbraLog.mailbox.debug("  lookup by subject (%s): %s", mode, pm.getNormalizedSubject());
        Conversation conv = mbox.getConversationByHash(subjHash);
        if (conv == null) {
            ZimbraLog.mailbox.debug("  no subject matches found");
            return Collections.emptyList();
        }

        ZimbraLog.mailbox.debug("  found conversation %d for subject hash: %s", conv.getId(), subjHash);

        // the caller can specify the received date via ParsedMessge constructor or X-Zimbra-Received header
        long window = pm.isReply() ? CONVERSATION_REPLY_WINDOW : CONVERSATION_NONREPLY_WINDOW;
        if (pm.getReceivedDate() > conv.getDate() + window) {
            // if the last message in the conv was more than 1 month ago, it's probably not related...
            ZimbraLog.mailbox.debug("  but rejected it because it's too old");
            return Collections.emptyList();
        }

        if (!pm.isReply() && conv.getSize() > CONVERSATION_NONREPLY_SIZE_LIMIT) {
            // put a cap on the number of non-reply messages accumulating in a conversation
            ZimbraLog.mailbox.debug("  but rejected it because it's too big to add a non-reply");
            return Collections.emptyList();
        }

        return Lists.newArrayList(conv);
    }

    /** Updates the {@code OPEN_CONVERSATION} database table to force all of
     *  the new message's hashes to point at its {@code Conversation}.  This
     *  includes the subject hash (if the user's threading algorithm is not
     *  {@link MailThreadingAlgorithm#strict}) and the reference hashes (if
     *  the threading algorithm isn't {@link MailThreadingAlgorithm#subject}).
     *  If threading is disabled for the {@code Mailbox}, this method does
     *  nothing.
     * @param conv  The threaded message's target {@code Conversation}.
     * @see #getReferenceHashes(boolean) */
    void recordAddedMessage(Conversation conv) throws ServiceException {
        if (conv == null)
            return;

        if (subjHash != null) {
            // use the Mailbox version in order to update Mailbox.mConvHashes
            mbox.openConversation(conv, subjHash);
        }

        if (refHashes != null) {
            for (String refHash : refHashes) {
                conv.open(refHash);
            }
        }
    }

    /** Updates the {@code OPEN_CONVERSATION} database table to redirect any
     *  threading hashes currently pointing at the {@code Message} to instead
     *  refer to the given {@code Conversation}. */
    void changeThreadingTargets(Message msg, Conversation conv) throws ServiceException {
        if (conv != null && msg != null && isEnabled() && !mode.isSubject()) {
            DbMailItem.changeOpenTargets(msg, conv.getId());
            ZimbraLog.mailbox.debug("  transferred hashes from message %d to conv %d", msg.getId(), conv.getId());
        }
    }

    @Override
    public String toString() {
        Objects.ToStringHelper tostr = Objects.toStringHelper(this);
        tostr.add("mode", mode);
        if (subjHash != null) {
            tostr.add("subjHash", subjHash);
        }
        if (refHashes != null) {
            tostr.add("refHashes", refHashes);
        }
        return tostr.toString();
    }
}