All files / src/entities/context context-manager.ts

100% Statements 70/70
95.12% Branches 39/41
100% Functions 19/19
100% Lines 70/70

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231                  29x 29x 29x   29x 29x             29x                     134x       134x 134x   1x         29x 9x 1x   8x 3x             5x 2x               3x 2x               1x 1x         29x 29x   139x     148x 148x         148x         165x         134x                 134x 134x 134x       1x                         134x 134x       3x 3x 17x 16x                   18x 18x 18x 18x 18x   1x 1x 1x   17x 17x                   11x   11x 11x 1x   10x 1x       9x         9x 9x 9x               2x 2x 2x                 2x 2x 2x                 2x 2x 2x               29x 145x    
/**
 * Context Manager - the manage_context view of the caller's configuration
 *
 * Every action applies to the caller of the running tool call and to its current MCP
 * session: another user's or another chat's context never changes. Presets and scopes are
 * session overrides of the configuration service, so they take effect in tool execution;
 * the account defaults are changed through the settings tools.
 */
 
import { GITLAB_BASE_URL, GITLAB_READ_ONLY_MODE } from '../../config';
import { logInfo, logError } from '../../logger';
import { ProfileLoader } from '../../profiles/loader';
import { ScopeConfig } from '../../profiles/types';
import { findNamespaceType } from '../../utils/namespace';
import {
  currentCaller,
  getConfigurationService,
  resolveCaller,
  type Caller,
  type ResolvedConfiguration,
} from '../../configuration';
import { EVERYWHERE } from '../../configuration/policy';
import {
  PresetInfo,
  ResetResult,
  RuntimeScope,
  SessionContext,
  SetScopeResult,
  SwitchResult,
} from './types';
 
function isOAuthMode(): boolean {
  return process.env.OAUTH_ENABLED === 'true';
}
 
function hostOf(url: string): string {
  try {
    return new URL(url).hostname;
  } catch {
    return url;
  }
}
 
/** Convert a scope configuration to the runtime view shown to the caller. */
export function runtimeScopeOf(scope: ScopeConfig, detected: boolean): RuntimeScope {
  if (scope.project) {
    return { type: 'project', path: scope.project, includeSubgroups: false, detected };
  }
  if (scope.group ?? scope.namespace) {
    return {
      type: 'group',
      path: (scope.group ?? scope.namespace) as string,
      includeSubgroups: scope.includeSubgroups !== false,
      detected,
    };
  }
  if (scope.projects && scope.projects.length > 0) {
    return {
      type: 'project',
      path: scope.projects[0],
      additionalPaths: scope.projects.length > 1 ? scope.projects.slice(1) : undefined,
      includeSubgroups: false,
      detected,
    };
  }
  if (scope.groups && scope.groups.length > 0) {
    return {
      type: 'group',
      path: scope.groups[0],
      additionalPaths: scope.groups.length > 1 ? scope.groups.slice(1) : undefined,
      includeSubgroups: scope.includeSubgroups !== false,
      detected,
    };
  }
  logError('Invalid scope configuration: no usable scope fields found', { scope });
  throw new Error(
    'Invalid scope configuration: expected project, group, namespace, projects, or groups to be defined',
  );
}
 
export class ContextManager {
  private static instance: ContextManager | null = null;
 
  private readonly profileLoader = new ProfileLoader();
 
  static getInstance(): ContextManager {
    ContextManager.instance ??= new ContextManager();
    return ContextManager.instance;
  }
 
  /** Reset the singleton instance (for testing) */
  static resetInstance(): void {
    ContextManager.instance = null;
  }
 
  /** The caller of the running tool call; the server's own token outside one. */
  private caller(): Caller {
    return currentCaller() ?? resolveCaller(undefined, GITLAB_BASE_URL);
  }
 
  /** What the operator configured, before any account or session choice. */
  private operatorContext(caller: Caller): Omit<SessionContext, 'initialContext'> {
    return {
      host: hostOf(caller.instanceUrl),
      apiUrl: caller.instanceUrl,
      readOnly: GITLAB_READ_ONLY_MODE,
      oauthMode: isOAuthMode(),
    };
  }
 
  private contextOf(caller: Caller, resolved: ResolvedConfiguration): SessionContext {
    const operator = this.operatorContext(caller);
    const scope = resolved.policy.scope;
    return {
      ...operator,
      readOnly: operator.readOnly || resolved.policy.readOnly,
      presetName: resolved.policy.presetName,
      disabledToolGroups: [...resolved.policy.disabledGroups].sort((a, b) => a.localeCompare(b)),
      scope: scope
        ? runtimeScopeOf(
            scope,
            resolved.session.scope !== undefined && resolved.session.scope !== EVERYWHERE,
          )
        : undefined,
      initialContext: operator,
    };
  }
 
  /** The caller's current context: operator settings narrowed by account and session. */
  async getContext(): Promise<SessionContext> {
    const caller = this.caller();
    return this.contextOf(caller, await getConfigurationService().resolve(caller));
  }
 
  async listPresets(): Promise<PresetInfo[]> {
    const profiles = await this.profileLoader.listProfiles();
    return profiles
      .filter((p) => p.isPreset)
      .map((p) => ({
        name: p.name,
        description: p.description,
        readOnly: p.readOnly,
        isBuiltIn: p.isBuiltIn,
      }));
  }
 
  /** Use a preset in the current session; the account default stays as it is. */
  async switchPreset(presetName: string): Promise<SwitchResult> {
    const caller = this.caller();
    const service = getConfigurationService();
    const previous = (await service.resolve(caller)).policy.presetName;
    try {
      await service.updateSession(caller, { preset: presetName });
    } catch (error) {
      const message = error instanceof Error ? error.message : String(error);
      logError('Failed to switch preset', { error: message, preset: presetName });
      throw new Error(`Failed to switch to preset '${presetName}': ${message}`, { cause: error });
    }
    logInfo('Switched preset for session', { previous, current: presetName });
    return {
      success: true,
      previous,
      current: presetName,
      message: `Switched this session to preset '${presetName}'`,
    };
  }
 
  /** Limit the current session to a project or group, detecting which one it is. */
  async setScope(namespace: string, includeSubgroups: boolean = true): Promise<SetScopeResult> {
    try {
      // Confirmed by GitLab, so a typo is reported instead of becoming a guessed scope.
      let canonicalPath = namespace;
      const type = await findNamespaceType(namespace, (path) => {
        canonicalPath = path;
      });
      if (!type) {
        throw new Error(
          `No project or group '${namespace}' was found on GitLab, or GitLab could not be reached`,
        );
      }
      const scope = {
        type,
        path: canonicalPath,
        includeSubgroups: type === 'group' ? includeSubgroups : false,
      };
      await getConfigurationService().updateSession(this.caller(), { scope });
      logInfo('Scope set with auto-detection', { namespace, type, includeSubgroups });
      return {
        success: true,
        scope: { ...scope, detected: true },
        message: `Scope set to ${type} '${canonicalPath}'${
          type === 'group' && includeSubgroups ? ' (including subgroups)' : ''
        }`,
      };
    } catch (error) {
      const message = error instanceof Error ? error.message : String(error);
      logError('Failed to set scope', { error: message, namespace });
      throw new Error(`Failed to set scope for '${namespace}': ${message}`, { cause: error });
    }
  }
 
  /**
   * Let the current session work everywhere the account has access: its own scope and the
   * account's default scope no longer apply to it. Its other overrides stay.
   */
  async clearScope(): Promise<ResetResult> {
    await getConfigurationService().updateSession(this.caller(), { scope: EVERYWHERE });
    logInfo('Session scope cleared');
    return {
      success: true,
      message: 'This session now works everywhere the account has access',
      context: await this.getContext(),
    };
  }
 
  /** Drop the session's preset, scope and profile: the account settings apply again. */
  async reset(): Promise<ResetResult> {
    await getConfigurationService().resetSession(this.caller());
    logInfo('Session context reset');
    return {
      success: true,
      message: 'Session context reset to the account settings',
      context: await this.getContext(),
    };
  }
}
 
export function getContextManager(): ContextManager {
  return ContextManager.getInstance();
}