@junctionpanel/server 0.1.60 → 0.1.62

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -29,7 +29,7 @@ import { expandTilde } from '../utils/path.js';
29
29
  import { searchHomeDirectories, searchWorkspaceEntries, searchWorkspaceEntriesAtGitRef, searchGitRepositories, checkIsGitRepo, } from '../utils/directory-suggestions.js';
30
30
  import { cloneRepository } from '../utils/git-clone.js';
31
31
  import { initRepository } from '../utils/git-init.js';
32
- import { resolveClientMessageId } from './client-message-id.js';
32
+ import { normalizeClientMessageId, resolveClientMessageId } from './client-message-id.js';
33
33
  import { deriveProjectGroupingKey, deriveProjectGroupingName } from '../shared/project-grouping.js';
34
34
  import { DEFAULT_DAEMON_PACKAGE_NAME, resolveDaemonPackageVersion, } from './daemon-package-context.js';
35
35
  import { runDaemonDoctor } from './daemon-doctor.js';
@@ -48,6 +48,8 @@ const READ_ONLY_GIT_ENV = {
48
48
  const DEFAULT_STORED_TIMELINE_FETCH_LIMIT = 200;
49
49
  const pendingAgentInitializations = new Map();
50
50
  const pendingAgentMessageExecutions = new Map();
51
+ const CREATE_AGENT_REPLAY_WINDOW_MS = 30000;
52
+ const pendingCreateAgentRequests = new Map();
51
53
  const sharedWorkspaceGitOperationStates = new Map();
52
54
  const DEFAULT_AGENT_PROVIDER = AGENT_PROVIDER_IDS[0];
53
55
  const CHECKOUT_DIFF_WATCH_DEBOUNCE_MS = 150;
@@ -59,6 +61,30 @@ const WORKSPACE_STATUS_PR_PASSIVE_REFRESH_MS = 60000;
59
61
  const TERMINAL_STREAM_WINDOW_BYTES = 256 * 1024;
60
62
  const TERMINAL_STREAM_MAX_PENDING_BYTES = 2 * 1024 * 1024;
61
63
  const TERMINAL_STREAM_MAX_PENDING_CHUNKS = 2048;
64
+ function buildCreateAgentReplayKey(clientId, clientMessageId) {
65
+ return JSON.stringify([clientId, clientMessageId]);
66
+ }
67
+ function deletePendingCreateAgentRequest(replayKey, expectedEntry) {
68
+ const existingEntry = pendingCreateAgentRequests.get(replayKey);
69
+ if (!existingEntry || (expectedEntry && existingEntry !== expectedEntry)) {
70
+ return;
71
+ }
72
+ clearTimeout(existingEntry.evictionTimer);
73
+ pendingCreateAgentRequests.delete(replayKey);
74
+ }
75
+ function storePendingCreateAgentRequest(replayKey, promise) {
76
+ const entry = {
77
+ promise,
78
+ evictionTimer: setTimeout(() => {
79
+ deletePendingCreateAgentRequest(replayKey, entry);
80
+ }, CREATE_AGENT_REPLAY_WINDOW_MS),
81
+ };
82
+ pendingCreateAgentRequests.set(replayKey, entry);
83
+ void promise.catch(() => {
84
+ deletePendingCreateAgentRequest(replayKey, entry);
85
+ });
86
+ return entry;
87
+ }
62
88
  const DIRTY_WORKTREE_CONFIRMATION_REQUIRED = 'dirty_worktree_confirmation_required';
63
89
  class SessionRequestError extends Error {
64
90
  constructor(code, message) {
@@ -312,21 +338,23 @@ export class Session {
312
338
  async performAgentMessageSend(input) {
313
339
  await this.ensureAgentLoaded(input.agentId);
314
340
  await this.interruptAgentIfRunning(input.agentId);
315
- try {
316
- this.agentManager.recordUserMessage(input.agentId, input.text, {
317
- messageId: input.messageId,
318
- emitState: false,
319
- images: input.images?.map((image) => ({
320
- data: image.preview?.data ?? image.data,
321
- mimeType: image.preview?.mimeType ?? image.mimeType,
322
- ...(image.label ? { label: image.label } : {}),
323
- ...(image.preview?.width != null ? { width: image.preview.width } : {}),
324
- ...(image.preview?.height != null ? { height: image.preview.height } : {}),
325
- })),
326
- });
327
- }
328
- catch (error) {
329
- this.sessionLogger.error({ err: error, agentId: input.agentId }, `Failed to record user message for agent ${input.agentId}`);
341
+ if (!input.suppressHistory) {
342
+ try {
343
+ this.agentManager.recordUserMessage(input.agentId, input.text, {
344
+ messageId: input.messageId,
345
+ emitState: false,
346
+ images: input.images?.map((image) => ({
347
+ data: image.preview?.data ?? image.data,
348
+ mimeType: image.preview?.mimeType ?? image.mimeType,
349
+ ...(image.label ? { label: image.label } : {}),
350
+ ...(image.preview?.width != null ? { width: image.preview.width } : {}),
351
+ ...(image.preview?.height != null ? { height: image.preview.height } : {}),
352
+ })),
353
+ });
354
+ }
355
+ catch (error) {
356
+ this.sessionLogger.error({ err: error, agentId: input.agentId }, `Failed to record user message for agent ${input.agentId}`);
357
+ }
330
358
  }
331
359
  const prompt = this.buildAgentPrompt(input.text, input.images);
332
360
  const started = this.startAgentStream(input.agentId, prompt, {
@@ -1555,7 +1583,7 @@ export class Session {
1555
1583
  const archivedAt = await this.getArchivedAt(agentId);
1556
1584
  if (archivedAt) {
1557
1585
  this.handleAgentRunError(agentId, new Error(`Agent ${agentId} is archived`), 'Refusing to send prompt to archived agent');
1558
- return;
1586
+ return false;
1559
1587
  }
1560
1588
  try {
1561
1589
  await this.performAgentMessageSend({
@@ -1565,9 +1593,11 @@ export class Session {
1565
1593
  images,
1566
1594
  runOptions,
1567
1595
  });
1596
+ return true;
1568
1597
  }
1569
1598
  catch (error) {
1570
1599
  this.handleAgentRunError(agentId, error, 'Failed to send prompt to agent');
1600
+ return false;
1571
1601
  }
1572
1602
  }
1573
1603
  /**
@@ -1576,96 +1606,147 @@ export class Session {
1576
1606
  async handleCreateAgentRequest(msg) {
1577
1607
  const { config, worktreeName, requestId, initialPrompt, clientMessageId, outputSchema, initialRunOptions, git, bootstrapSetupOverride, generalPreferencesApplied, images, labels, } = msg;
1578
1608
  this.sessionLogger.info({ cwd: config.cwd, provider: config.provider, worktreeName }, `Creating agent in ${config.cwd} (${config.provider})${worktreeName ? ` with worktree ${worktreeName}` : ''}`);
1609
+ const trimmedPrompt = initialPrompt?.trim() ?? '';
1610
+ const hasInitialMessage = trimmedPrompt.length > 0 || Boolean(images?.length);
1611
+ const normalizedClientMessageId = hasInitialMessage ? normalizeClientMessageId(clientMessageId) : undefined;
1612
+ const createReplayKey = normalizedClientMessageId
1613
+ ? buildCreateAgentReplayKey(this.clientId, normalizedClientMessageId)
1614
+ : null;
1579
1615
  try {
1580
- const { sessionConfig, worktreeConfig, autoWorkspaceName } = await this.buildAgentSessionConfig(config, git, worktreeName, labels);
1581
- const mergedLabels = autoWorkspaceName
1582
- ? { ...labels, 'junction:workspace': autoWorkspaceName }
1583
- : labels;
1584
- const snapshot = await this.agentManager.createAgent(sessionConfig, undefined, {
1585
- labels: applyNotificationRelayOwnerLabel(mergedLabels, this.userId),
1586
- });
1587
- await this.forwardAgentUpdate(snapshot);
1588
- if (requestId) {
1589
- const agentPayload = await this.getAgentPayloadById(snapshot.id);
1590
- if (!agentPayload) {
1591
- throw new Error(`Agent ${snapshot.id} not found after creation`);
1592
- }
1593
- this.emit({
1594
- type: 'status',
1595
- payload: {
1596
- status: 'agent_created',
1597
- agentId: snapshot.id,
1598
- requestId,
1599
- agent: agentPayload,
1600
- },
1616
+ const createAgentLaunch = async (clearReplayEntryOnDeferredFailure) => {
1617
+ const { sessionConfig, worktreeConfig, autoWorkspaceName } = await this.buildAgentSessionConfig(config, git, worktreeName, labels);
1618
+ const mergedLabels = autoWorkspaceName
1619
+ ? { ...labels, 'junction:workspace': autoWorkspaceName }
1620
+ : labels;
1621
+ const snapshot = await this.agentManager.createAgent(sessionConfig, undefined, {
1622
+ labels: applyNotificationRelayOwnerLabel(mergedLabels, this.userId),
1601
1623
  });
1602
- }
1603
- const trimmedPrompt = initialPrompt?.trim() ?? '';
1604
- const hasInitialMessage = trimmedPrompt.length > 0 || Boolean(images?.length);
1605
- const runInitialPrompt = async () => {
1606
- if (!hasInitialMessage) {
1607
- return;
1624
+ await this.forwardAgentUpdate(snapshot);
1625
+ const runInitialPrompt = async () => {
1626
+ if (!hasInitialMessage) {
1627
+ return;
1628
+ }
1629
+ if (trimmedPrompt.length > 0) {
1630
+ scheduleAgentMetadataGeneration({
1631
+ agentManager: this.agentManager,
1632
+ agentId: snapshot.id,
1633
+ cwd: snapshot.cwd,
1634
+ initialPrompt: trimmedPrompt,
1635
+ explicitTitle: snapshot.config.title,
1636
+ junctionHome: this.junctionHome,
1637
+ logger: this.sessionLogger,
1638
+ });
1639
+ }
1640
+ const sendSucceeded = await this.handleSendAgentMessage(snapshot.id, trimmedPrompt, normalizedClientMessageId ?? resolveClientMessageId(clientMessageId), images, normalizeAgentRunOptions({
1641
+ ...(outputSchema ? { outputSchema } : {}),
1642
+ ...(initialRunOptions ?? {}),
1643
+ }));
1644
+ if (!sendSucceeded) {
1645
+ clearReplayEntryOnDeferredFailure?.();
1646
+ }
1647
+ };
1648
+ const handleInitialPromptError = (promptError) => {
1649
+ clearReplayEntryOnDeferredFailure?.();
1650
+ this.sessionLogger.error({ err: promptError, agentId: snapshot.id }, `Failed to run initial prompt for agent ${snapshot.id}`);
1651
+ this.emit({
1652
+ type: 'activity_log',
1653
+ payload: {
1654
+ id: uuidv4(),
1655
+ timestamp: new Date(),
1656
+ type: 'error',
1657
+ content: `Initial prompt failed: ${promptError?.message ?? promptError}`,
1658
+ },
1659
+ });
1660
+ };
1661
+ if (hasInitialMessage && !worktreeConfig) {
1662
+ void runInitialPrompt().catch(handleInitialPromptError);
1608
1663
  }
1609
- if (trimmedPrompt.length > 0) {
1610
- scheduleAgentMetadataGeneration({
1611
- agentManager: this.agentManager,
1664
+ if (worktreeConfig) {
1665
+ void runAsyncWorktreeBootstrap({
1612
1666
  agentId: snapshot.id,
1613
- cwd: snapshot.cwd,
1614
- initialPrompt: trimmedPrompt,
1615
- explicitTitle: snapshot.config.title,
1616
- junctionHome: this.junctionHome,
1667
+ worktree: worktreeConfig,
1668
+ setupOverride: bootstrapSetupOverride,
1669
+ generalPreferencesApplied,
1670
+ terminalManager: this.terminalManager,
1671
+ appendTimelineItem: (item) => appendTimelineItemIfAgentKnown({
1672
+ agentManager: this.agentManager,
1673
+ agentId: snapshot.id,
1674
+ item,
1675
+ }),
1676
+ emitLiveTimelineItem: (item) => emitLiveTimelineItemIfAgentKnown({
1677
+ agentManager: this.agentManager,
1678
+ agentId: snapshot.id,
1679
+ item,
1680
+ }),
1681
+ onSetupSettled: async (result) => {
1682
+ if (!hasInitialMessage || result.setupStatus === 'failed') {
1683
+ if (hasInitialMessage && result.setupStatus === 'failed') {
1684
+ clearReplayEntryOnDeferredFailure?.();
1685
+ }
1686
+ return;
1687
+ }
1688
+ await runInitialPrompt().catch(handleInitialPromptError);
1689
+ },
1617
1690
  logger: this.sessionLogger,
1691
+ }).catch((bootstrapError) => {
1692
+ clearReplayEntryOnDeferredFailure?.();
1693
+ this.sessionLogger.error({ err: bootstrapError, agentId: snapshot.id }, `Failed to bootstrap worktree for agent ${snapshot.id}`);
1618
1694
  });
1619
1695
  }
1620
- await this.handleSendAgentMessage(snapshot.id, trimmedPrompt, resolveClientMessageId(clientMessageId), images, normalizeAgentRunOptions({
1621
- ...(outputSchema ? { outputSchema } : {}),
1622
- ...(initialRunOptions ?? {}),
1623
- }));
1696
+ this.sessionLogger.info({ agentId: snapshot.id, provider: snapshot.provider }, `Created agent ${snapshot.id} (${snapshot.provider})`);
1697
+ return snapshot;
1624
1698
  };
1625
- const handleInitialPromptError = (promptError) => {
1626
- this.sessionLogger.error({ err: promptError, agentId: snapshot.id }, `Failed to run initial prompt for agent ${snapshot.id}`);
1699
+ let reusedExisting = false;
1700
+ let snapshot;
1701
+ let agentPayload;
1702
+ if (createReplayKey) {
1703
+ const existingEntry = pendingCreateAgentRequests.get(createReplayKey);
1704
+ if (existingEntry) {
1705
+ reusedExisting = true;
1706
+ snapshot = await existingEntry.promise;
1707
+ }
1708
+ else {
1709
+ reusedExisting = false;
1710
+ let clearReplayEntryOnDeferredFailure = () => { };
1711
+ const nextPromise = createAgentLaunch(() => {
1712
+ clearReplayEntryOnDeferredFailure();
1713
+ });
1714
+ const nextEntry = storePendingCreateAgentRequest(createReplayKey, nextPromise);
1715
+ clearReplayEntryOnDeferredFailure = () => {
1716
+ deletePendingCreateAgentRequest(createReplayKey, nextEntry);
1717
+ };
1718
+ try {
1719
+ snapshot = await nextPromise;
1720
+ }
1721
+ catch (error) {
1722
+ deletePendingCreateAgentRequest(createReplayKey, nextEntry);
1723
+ throw error;
1724
+ }
1725
+ }
1726
+ agentPayload = await this.getAgentPayloadById(snapshot.id);
1727
+ if (!agentPayload) {
1728
+ agentPayload = await this.buildAgentPayload(snapshot);
1729
+ }
1730
+ }
1731
+ else {
1732
+ snapshot = await createAgentLaunch();
1733
+ agentPayload = await this.getAgentPayloadById(snapshot.id);
1734
+ if (!agentPayload) {
1735
+ throw new Error(`Agent ${snapshot.id} not found after creation`);
1736
+ }
1737
+ }
1738
+ if (requestId) {
1627
1739
  this.emit({
1628
- type: 'activity_log',
1740
+ type: 'status',
1629
1741
  payload: {
1630
- id: uuidv4(),
1631
- timestamp: new Date(),
1632
- type: 'error',
1633
- content: `Initial prompt failed: ${promptError?.message ?? promptError}`,
1634
- },
1635
- });
1636
- };
1637
- if (hasInitialMessage && !worktreeConfig) {
1638
- void runInitialPrompt().catch(handleInitialPromptError);
1639
- }
1640
- if (worktreeConfig) {
1641
- void runAsyncWorktreeBootstrap({
1642
- agentId: snapshot.id,
1643
- worktree: worktreeConfig,
1644
- setupOverride: bootstrapSetupOverride,
1645
- generalPreferencesApplied,
1646
- terminalManager: this.terminalManager,
1647
- appendTimelineItem: (item) => appendTimelineItemIfAgentKnown({
1648
- agentManager: this.agentManager,
1649
- agentId: snapshot.id,
1650
- item,
1651
- }),
1652
- emitLiveTimelineItem: (item) => emitLiveTimelineItemIfAgentKnown({
1653
- agentManager: this.agentManager,
1742
+ status: 'agent_created',
1743
+ reusedExisting,
1654
1744
  agentId: snapshot.id,
1655
- item,
1656
- }),
1657
- onSetupSettled: async (result) => {
1658
- if (!hasInitialMessage || result.setupStatus === 'failed') {
1659
- return;
1660
- }
1661
- await runInitialPrompt().catch(handleInitialPromptError);
1745
+ requestId,
1746
+ agent: agentPayload,
1662
1747
  },
1663
- logger: this.sessionLogger,
1664
- }).catch((bootstrapError) => {
1665
- this.sessionLogger.error({ err: bootstrapError, agentId: snapshot.id }, `Failed to bootstrap worktree for agent ${snapshot.id}`);
1666
1748
  });
1667
1749
  }
1668
- this.sessionLogger.info({ agentId: snapshot.id, provider: snapshot.provider }, `Created agent ${snapshot.id} (${snapshot.provider})`);
1669
1750
  }
1670
1751
  catch (error) {
1671
1752
  this.sessionLogger.error({ err: error }, 'Failed to create agent');
@@ -5585,6 +5666,7 @@ export class Session {
5585
5666
  agentId,
5586
5667
  text: msg.text,
5587
5668
  ...(msg.messageId ? { messageId: msg.messageId } : {}),
5669
+ ...(msg.suppressHistory ? { suppressHistory: true } : {}),
5588
5670
  ...(msg.images ? { images: msg.images } : {}),
5589
5671
  runOptions: normalizeAgentRunOptions(msg.runOptions),
5590
5672
  });